Commit697a7348Recorded26 Sep 2026Repositorysigil-desktop

window-focused?: does the window have the input focus

Message

A program that pauses when the player switches away had no way to ask: the focus callback in desktop.c only released a captured cursor. (window-focused?) reads GLFW_FOCUSED at the call, and answers #f without a window. Tests: test-window.sgl (no window) and test-focus-live.sgl (Xvfb and xdotool: given the focus #t, focus moved to the root window #f, given back #t), the live test seen red with the read planted as always #t. 56/56 without a display, 17/17 live on Xvfb.

Changed
 CHANGELOG.md             | 10 ++++++++++
 README.md                |  3 ++-
 src/c/desktop.c          | 18 ++++++++++++++++++
 src/sigil/desktop.sgl    |  2 +-
 test/test-focus-live.sgl | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/test-window.sgl     | 13 ++++++++-----
 6 files changed, 100 insertions(+), 7 deletions(-)
Diff
CHANGELOG.mdmodified
@@ -9,6 +9,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
9
10
## [Unreleased]
11
+12
### Added
+13
+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
+18
no-window answer; `test/test-focus-live.sgl` (Xvfb, xdotool) gives the
+19
window the focus, moves it to the root window and gives it back, and was
+20
seen red with a planted defect.
+21
22
## [0.13.0] - 2026-09-25
23
24
### Added
README.mdmodified
@@ -54,7 +54,8 @@ The exports: `app-run`, `run-game`, `wait-frame`, `frame-width`,
54
`mouse-pressed?`, `mouse-released?`, `set-cursor-captured!`,
55
`cursor-captured?`, `key-down?`, `key-pressed?`, `key-released?`,
56
`key-code`, `quit-requested?`, `request-quit`, `window-set-size!`,
−57
`fullscreen?`, `set-fullscreen!`, `monitor-size`.
+57
`fullscreen?`, `set-fullscreen!`, `monitor-size`, `window-width`,
+58
`window-height`, `window-focused?`.
59
60
Sizes and positions are framebuffer pixels: on a scaled Wayland output a
61
window asked for at 960x720 has a framebuffer of 1920x1440 at scale 2 (or
src/c/desktop.cmodified
@@ -994,6 +994,21 @@ static Value native_window_height(SigilVM *vm, int argc, Value *args)
994
return sigil_fixnum(h);
995
}
996
+997
/*
+998
* (window-focused?) -> #t while the window has the input focus, #f when
+999
* another window has it, and #f without a window
+1000
*
+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
static Value native_window_focused_p(SigilVM *vm, int argc, Value *args)
+1006
{
+1007
(void)vm; (void)argc; (void)args;
+1008
if (!g_desktop || !g_desktop->window) return SIGIL_FALSE;
+1009
return glfwGetWindowAttrib(g_desktop->window, GLFW_FOCUSED) ? SIGIL_TRUE : SIGIL_FALSE;
+1010
}
+1011
1012
/* ============================================================
1013
* MODULE INITIALIZATION
1014
* ============================================================ */
@@ -1071,6 +1086,8 @@ void sigil__init_sigil_desktop_module(SigilVM *vm)
1086
SIGIL_ARITY_EXACT(0), "The window's width in screen units");
1087
sigil_module_register_native(vm, "window-height", native_window_height,
1088
SIGIL_ARITY_EXACT(0), "The window's height in screen units");
+1089
sigil_module_register_native(vm, "window-focused?", native_window_focused_p,
+1090
SIGIL_ARITY_EXACT(0), "Does the window have the input focus");
1091
1092
/* Export all */
1093
sigil_module_export(vm, "app-run");
@@ -1099,6 +1116,7 @@ void sigil__init_sigil_desktop_module(SigilVM *vm)
1116
sigil_module_export(vm, "monitor-size");
1117
sigil_module_export(vm, "window-width");
1118
sigil_module_export(vm, "window-height");
+1119
sigil_module_export(vm, "window-focused?");
1120
1121
sigil_end_module(vm);
1122
}
src/sigil/desktop.sglmodified
@@ -22,7 +22,7 @@
22
key-down? key-pressed? key-released? key-code
23
quit-requested? request-quit
24
window-set-size! fullscreen? set-fullscreen! monitor-size
−25
window-width window-height
+25
window-width window-height window-focused?
26
27
;; Cooperative frame scheduling
28
run-game
test/test-focus-live.sgladded
@@ -0,0 +1,61 @@
+1
;;; window-focused? with a window open (0.14.0). Needs an X display the test
+2
;;; may move the input focus on, xdotool and xwininfo; run it on a headless
+3
;;; X server, never on someone's desktop, e.g.
+4
;;;
+5
;;; Xvfb :74 -screen 0 1700x900x24 +extension GLX -nolisten tcp &
+6
;;; env -u WAYLAND_DISPLAY DISPLAY=:74 guix shell xdotool xwininfo -- sigil test test/test-focus-live.sgl
+7
;;;
+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.
+15
+16
(import (sigil test)
+17
(sigil desktop)
+18
(sigil list)
+19
(sigil process))
+20
+21
(define (display?) (and (getenv "DISPLAY") (not (getenv "WAYLAND_DISPLAY"))))
+22
(define (xdotool?) (and (display?) (= 0 (process-run "sh" "-c" "command -v xdotool >/dev/null && command -v xwininfo >/dev/null"))))
+23
+24
(define TITLE "sigil-desktop focus test")
+25
(define (focus-window!)
+26
(process-run "sh" "-c" (string-append "xdotool search --name '" TITLE "' | head -1 | xargs xdotool windowfocus")))
+27
(define (focus-root!)
+28
(process-run "sh" "-c" "xdotool windowfocus $(xwininfo -root | awk '/Window id/ {print $4}')"))
+29
+30
(define *seen* '())
+31
(define (see! name v) (set! *seen* (append *seen* (list (cons name v)))))
+32
(define (seen name) (let ((e (assoc name *seen*))) (and e (cdr e))))
+33
+34
(define *n* 0)
+35
(define (frame dt)
+36
(set! *n* (+ *n* 1))
+37
(case *n*
+38
((2) (focus-window!))
+39
((6) (see! "focused, given" (window-focused?))
+40
(focus-root!))
+41
((10) (see! "focused, taken away" (window-focused?))
+42
(focus-window!))
+43
((14) (see! "focused, given back" (window-focused?))
+44
(request-quit))
+45
(else #f)))
+46
+47
(when (xdotool?)
+48
(app-run (lambda () #f) frame (lambda () #f) TITLE 640 360))
+49
+50
(test-group "window-focused?, with a window open"
+51
(if (not (xdotool?))
+52
(test "no X display or no xdotool/xwininfo: nothing checked (see the header)"
+53
(assert-true #t))
+54
(begin
+55
(test "given the focus, it is #t"
+56
(assert-equal #t (seen "focused, given")))
+57
(test "with the focus on the root window, it is #f"
+58
(assert-equal #f (seen "focused, taken away"))
+59
(assert-true (assoc "focused, taken away" *seen*)))
+60
(test "given it back, it is #t again"
+61
(assert-equal #t (seen "focused, given back"))))))
test/test-window.sglmodified
@@ -1,7 +1,8 @@
−1
;;; The window calls (0.11.0) answer safely before (and without) a window:
−2
;;; a program can call them from init, or from a headless test, and get #f
−3
;;; rather than a crash. With a window they are driven by hand (a Harkfell
−4
;;; native build: F11, a resize), since a test cannot open one here.
+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.
6
7
(import (sigil test)
8
(sigil desktop))
@@ -18,4 +19,6 @@
19
(assert-false (set-fullscreen! #f)))
20
(test "window-width and window-height are #f"
21
(assert-false (window-width))
−21
(assert-false (window-height))))
+22
(assert-false (window-height)))
+23
(test "window-focused? is #f"
+24
(assert-false (window-focused?))))