Function: frame-focus-state
frame-focus-state is a byte-compiled function defined in frame.el.gz.
Signature
(frame-focus-state &optional FRAME)
Documentation
Return FRAME's last known focus state.
If nil or omitted, FRAME defaults to the selected frame.
Return nil if the frame is definitely known not be focused, t if
the frame is known to be focused, and unknown if we don't know.
Probably introduced at or before Emacs version 27.1.
Source Code
;; Defined in /usr/src/emacs/lisp/frame.el.gz
(defun frame-focus-state (&optional frame)
"Return FRAME's last known focus state.
If nil or omitted, FRAME defaults to the selected frame.
Return nil if the frame is definitely known not be focused, t if
the frame is known to be focused, and `unknown' if we don't know."
(let* ((frame (or frame (selected-frame)))
(tty-top-frame (tty-top-frame frame)))
(if (not tty-top-frame)
(frame-parameter frame 'last-focus-update)
;; All tty frames are frame-visible-p if the terminal is
;; visible, so check whether the frame is the top tty frame
;; before checking visibility.
(cond ((not (eq tty-top-frame frame)) nil)
((not (frame-visible-p frame)) nil)
(t (let ((tty-focus-state
(terminal-parameter frame 'tty-focus-state)))
(cond ((eq tty-focus-state 'focused) t)
((eq tty-focus-state 'defocused) nil)
(t 'unknown))))))))