Commite9032396Recorded26 Sep 2026Repositorysigil-desktop

window-focused?: the review's notes

Message

The live test's comment said no window manager hands the focus over on a bare X server; GLFW takes it itself when the window is shown, so the last give is what proves xdotool reached the window. test-window.sgl's header still said no test could open a window. The C comment and the CHANGELOG say that on Wayland the answer is #f until the compositor's keyboard-enter arrives, so a program should pause on #f only after a first #t.

Changed
 CHANGELOG.md             |  5 ++++-
 src/c/desktop.c          |  5 +++++
 test/test-focus-live.sgl | 10 ++++++----
 test/test-window.sgl     |  5 ++---
 4 files changed, 17 insertions(+), 8 deletions(-)
Diff
CHANGELOG.mdmodified
@@ -14,7 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
14
- `(window-focused?)`: `#t` while the window has the input focus, `#f`
15
when another window has it (and `#f` without a window), read from GLFW
16
at the call (`GLFW_FOCUSED`). A program polls it once a frame to pause
−17
when the player switches away. `test/test-window.sgl` covers the
+17
when the player switches away. On Wayland it is `#f` until the
+18
compositor's keyboard-enter arrives (often a frame or two after the
+19
window opens), so pause on `#f` only after a first `#t`.
+20
`test/test-window.sgl` covers the
21
no-window answer; `test/test-focus-live.sgl` (Xvfb, xdotool) gives the
22
window the focus, moves it to the root window and gives it back, and was
23
seen red with a planted defect.
src/c/desktop.cmodified
@@ -1001,6 +1001,11 @@ static Value native_window_height(SigilVM *vm, int argc, Value *args)
1001
* Read from GLFW at the call (GLFW_FOCUSED), not kept from the focus
1002
* callback, so it is true of the frame that asks. A program polls it once
1003
* a frame to pause when the player switches away.
+1004
*
+1005
* On Wayland the answer is GLFW's record of the keyboard focus: #f until
+1006
* the compositor's keyboard-enter arrives (often a frame or two after the
+1007
* window opens), and always #f on a seat with no keyboard. A program
+1008
* that pauses on #f should wait for a first #t before pausing.
1009
*/
1010
static Value native_window_focused_p(SigilVM *vm, int argc, Value *args)
1011
{
test/test-focus-live.sglmodified
@@ -8,10 +8,12 @@
8
;;; Without DISPLAY or without xdotool it says so and checks nothing; the
9
;;; no-window answer is test-window.sgl's.
10
;;;
−11
;;; Frame by frame: give the window the focus (xdotool, by its title: on a
−12
;;; bare X server no window manager hands it over) and read #t; move the
−13
;;; focus to the root window and read #f; give it back and read #t again.
−14
;;; The read is GLFW's own attribute, so each answer follows the X focus.
+11
;;; Frame by frame: give the window the focus (xdotool, by its title; GLFW
+12
;;; also takes it when the window is shown, so this first give changes
+13
;;; nothing, and the last one is what proves xdotool reached the window)
+14
;;; and read #t; move the focus to the root window and read #f; give it
+15
;;; back and read #t again. On X11 GLFW asks the server (XGetInputFocus) at
+16
;;; each read, so each answer follows the X focus.
17
18
(import (sigil test)
19
(sigil desktop)
test/test-window.sglmodified
@@ -1,8 +1,7 @@
1
;;; The window calls (0.11.0; window-focused?, 0.14.0) answer safely
2
;;; before (and without) a window: a program can call them from init, or
−3
;;; from a headless test, and get #f rather than a crash. With a window they
−4
;;; are driven by hand (a Harkfell native build: F11, a resize), since a
−5
;;; test cannot open one here.
+3
;;; from a headless test, and get #f rather than a crash. With a window
+4
;;; open they are test-window-live.sgl's and test-focus-live.sgl's.
5
6
(import (sigil test)
7
(sigil desktop))