Commit8ce16469Recorded25 Sep 2026Repositorysigil-desktop

Cursor capture for mouse look: set-cursor-captured!, mouse-dx/dy

Message

(set-cursor-captured! on? [escape-releases?]) hides the cursor and holds it on the window (GLFW_CURSOR_DISABLED, raw motion where the platform has it). It answers, and (cursor-captured?) reports, GLFW's input mode, so a platform that cannot capture answers #f. Losing the window focus always releases the capture, and Escape does too unless escape-releases? is #f; the Escape key still reaches the game.

(mouse-dx) and (mouse-dy) are the mouse's motion since the last frame in framebuffer pixels, captured or not. A capture change re-reads the position rather than trusting the last callback's, so starting or ending a capture adds no jump and drops no motion (with the pointer already inside the window no jump is reported at all); the position reported as the pointer enters the window is not counted.

test/test-cursor-live.sgl drives it with xdotool on a headless X server: a move read back exactly with the capture started inside the window, entering the window not counted, Escape, escape-releases? #f, focus loss, and uncaptured motion after a release.

Changed
 src/c/desktop-internal.h  |  17 +++++++++++++++++
 src/c/desktop.c           | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/sigil/desktop.sgl     |   3 ++-
 test/test-cursor-live.sgl | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 250 insertions(+), 1 deletion(-)
Diff
src/c/desktop-internal.hmodified
@@ -72,6 +72,23 @@ typedef struct {
72
* summed, cleared with the other per-frame input; + is up/right. */
73
double wheel_x;
74
double wheel_y;
+75
/* Mouse motion since the last frame (0.12.0), in framebuffer pixels:
+76
* the cursor callback's position differences, summed, cleared with the
+77
* other per-frame input. It keeps counting while the cursor is
+78
* captured, when the position itself no longer means a place on the
+79
* window. A capture change re-reads the position instead of trusting
+80
* the last callback's; skip_motion drops the position reported with
+81
* the pointer entering the window, which is a jump from wherever it
+82
* left. */
+83
double motion_x;
+84
double motion_y;
+85
bool skip_motion;
+86
/* Cursor capture (set-cursor-captured!): the cursor hidden and held,
+87
* motion unbounded (GLFW_CURSOR_DISABLED, raw motion when the platform
+88
* has it). Released when the window loses focus, and on Escape unless
+89
* the app asked to keep it. Whether it holds is read from GLFW's input
+90
* mode, not remembered. */
+91
bool escape_releases;
92
93
/* Quit handling */
94
bool quit_requested;
src/c/desktop.cmodified
@@ -65,6 +65,8 @@ void sigil_desktop_clear_frame_input(void)
65
memset(g_desktop->mouse_released, 0, sizeof(g_desktop->mouse_released));
66
g_desktop->wheel_x = 0.0;
67
g_desktop->wheel_y = 0.0;
+68
g_desktop->motion_x = 0.0;
+69
g_desktop->motion_y = 0.0;
70
}
71
72
int sigil_desktop_framebuffer_width(void)
@@ -204,12 +206,52 @@ static void glfw_error_callback(int code, const char *description)
206
fprintf(stderr, "sigil-desktop: GLFW error 0x%X: %s\n", code, description);
207
}
208
+209
/* Is the cursor captured now, as GLFW has it (a focus loss or Escape may
+210
* have released it since it was asked for). */
+211
static bool captured_now(void)
+212
{
+213
return g_desktop && g_desktop->window &&
+214
glfwGetInputMode(g_desktop->window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED;
+215
}
+216
+217
/* Capture or release the cursor. GLFW_CURSOR_DISABLED hides the cursor
+218
* and keeps it on the window with unbounded motion (on Wayland through the
+219
* compositor's pointer constraints); raw motion, where the platform has it,
+220
* skips pointer acceleration. The position GLFW reports changes meaning
+221
* with the mode (a virtual position while captured), so it is read again
+222
* here rather than differenced against the last callback's; no motion is
+223
* dropped. A platform that cannot capture (a Wayland compositor without
+224
* pointer constraints) leaves the cursor as it was. Returns whether the
+225
* cursor is captured now. */
+226
static bool set_capture(bool on)
+227
{
+228
if (!g_desktop || !g_desktop->window) return false;
+229
GLFWwindow *w = g_desktop->window;
+230
if (on == captured_now()) return on;
+231
glfwGetError(NULL); /* clear any earlier error */
+232
glfwSetInputMode(w, GLFW_CURSOR, on ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
+233
if (on && glfwGetError(NULL) != GLFW_NO_ERROR) {
+234
glfwSetInputMode(w, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
+235
} else if (glfwRawMouseMotionSupported()) {
+236
glfwSetInputMode(w, GLFW_RAW_MOUSE_MOTION, on ? GLFW_TRUE : GLFW_FALSE);
+237
}
+238
glfwGetCursorPos(w, &g_desktop->cursor_x, &g_desktop->cursor_y);
+239
return captured_now();
+240
}
+241
242
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
243
{
244
(void)window; (void)scancode; (void)mods;
245
if (!g_desktop) return;
246
if (key < 0 || key >= SIGIL_DESKTOP_MAX_KEYS) return;
247
+248
/* Escape always gives the cursor back unless the app asked to keep it,
+249
* so nobody is left with a hidden cursor. The key still reaches the app. */
+250
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS &&
+251
g_desktop->escape_releases && captured_now()) {
+252
set_capture(false);
+253
}
+254
255
if (action == GLFW_PRESS || action == GLFW_REPEAT) {
256
/* A repeat is a held key, not a new press: pressed? fires once. */
257
if (!g_desktop->keys_down[key]) {
@@ -251,10 +293,34 @@ static void cursor_pos_callback(GLFWwindow *window, double x, double y)
293
{
294
(void)window;
295
if (!g_desktop) return;
+296
if (g_desktop->skip_motion) {
+297
g_desktop->skip_motion = false;
+298
} else if (g_desktop->win_width > 0 && g_desktop->win_height > 0) {
+299
/* in framebuffer pixels, as mouse-x / mouse-y */
+300
g_desktop->motion_x += (x - g_desktop->cursor_x) * ((double)g_desktop->fb_width / (double)g_desktop->win_width);
+301
g_desktop->motion_y += (y - g_desktop->cursor_y) * ((double)g_desktop->fb_height / (double)g_desktop->win_height);
+302
}
303
g_desktop->cursor_x = x;
304
g_desktop->cursor_y = y;
305
}
306
+307
/* Losing focus gives the cursor back: another window must never find it
+308
* captured by this one. */
+309
static void window_focus_callback(GLFWwindow *window, int focused)
+310
{
+311
(void)window;
+312
if (!g_desktop) return;
+313
if (!focused && captured_now()) set_capture(false);
+314
}
+315
+316
/* The position reported as the pointer enters is a jump from wherever it
+317
* left the window, not motion: drop it. */
+318
static void cursor_enter_callback(GLFWwindow *window, int entered)
+319
{
+320
(void)window;
+321
if (g_desktop && entered) g_desktop->skip_motion = true;
+322
}
+323
324
static void window_close_callback(GLFWwindow *window)
325
{
326
(void)window;
@@ -433,6 +499,8 @@ static bool run_main_loop(const char *title, int width, int height)
499
glfwSetKeyCallback(window, key_callback);
500
glfwSetMouseButtonCallback(window, mouse_button_callback);
501
glfwSetCursorPosCallback(window, cursor_pos_callback);
+502
glfwSetWindowFocusCallback(window, window_focus_callback);
+503
glfwSetCursorEnterCallback(window, cursor_enter_callback);
504
glfwSetScrollCallback(window, scroll_callback);
505
glfwSetWindowCloseCallback(window, window_close_callback);
506
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
@@ -609,6 +677,53 @@ static Value native_mouse_wheel_x(SigilVM *vm, int argc, Value *args)
677
return sigil_flonum(g_desktop ? g_desktop->wheel_x : 0.0);
678
}
679
+680
/*
+681
* (mouse-dx) / (mouse-dy) -> float: the mouse's motion since the last frame
+682
* in framebuffer pixels (+x right, +y down), captured or not. With the
+683
* cursor captured this is the only reading that means anything: the
+684
* position keeps growing without bound.
+685
*/
+686
static Value native_mouse_dx(SigilVM *vm, int argc, Value *args)
+687
{
+688
(void)vm; (void)argc; (void)args;
+689
return sigil_flonum(g_desktop ? g_desktop->motion_x : 0.0);
+690
}
+691
+692
static Value native_mouse_dy(SigilVM *vm, int argc, Value *args)
+693
{
+694
(void)vm; (void)argc; (void)args;
+695
return sigil_flonum(g_desktop ? g_desktop->motion_y : 0.0);
+696
}
+697
+698
/*
+699
* (set-cursor-captured! on? [escape-releases?]) -> whether the cursor is now
+700
* captured
+701
*
+702
* #t hides the cursor and holds it on the window for mouse look: read
+703
* mouse-dx / mouse-dy. #f gives it back. The window losing focus always
+704
* releases it, and so does Escape unless escape-releases? is #f (default
+705
* #t; the Escape key still reaches the app either way). The answer is what
+706
* GLFW has: #f when the platform cannot capture. Before the window exists
+707
* this returns #f and does nothing.
+708
*/
+709
static Value native_set_cursor_captured(SigilVM *vm, int argc, Value *args)
+710
{
+711
(void)vm;
+712
if (!g_desktop) return SIGIL_FALSE;
+713
bool on = !sigil_is_false(args[0]);
+714
g_desktop->escape_releases = argc > 1 ? !sigil_is_false(args[1]) : true;
+715
return set_capture(on) ? SIGIL_TRUE : SIGIL_FALSE;
+716
}
+717
+718
/* (cursor-captured?) -> is the cursor captured now, read from GLFW's input
+719
* mode (a focus loss or Escape may have released it since
+720
* set-cursor-captured!) */
+721
static Value native_cursor_captured_p(SigilVM *vm, int argc, Value *args)
+722
{
+723
(void)vm; (void)argc; (void)args;
+724
return captured_now() ? SIGIL_TRUE : SIGIL_FALSE;
+725
}
+726
727
/*
728
* (mouse-y) -> float, framebuffer pixels
729
*/
@@ -884,6 +999,14 @@ void sigil__init_sigil_desktop_module(SigilVM *vm)
999
SIGIL_ARITY_EXACT(0), "Wheel travel since the last frame (vertical)");
1000
sigil_module_register_native(vm, "mouse-wheel-x", native_mouse_wheel_x,
1001
SIGIL_ARITY_EXACT(0), "Wheel travel since the last frame (horizontal)");
+1002
sigil_module_register_native(vm, "mouse-dx", native_mouse_dx,
+1003
SIGIL_ARITY_EXACT(0), "Mouse motion since the last frame (x)");
+1004
sigil_module_register_native(vm, "mouse-dy", native_mouse_dy,
+1005
SIGIL_ARITY_EXACT(0), "Mouse motion since the last frame (y)");
+1006
sigil_module_register_native(vm, "set-cursor-captured!", native_set_cursor_captured,
+1007
SIGIL_ARITY_RANGE(1, 2), "Capture the cursor for mouse look, or release it");
+1008
sigil_module_register_native(vm, "cursor-captured?", native_cursor_captured_p,
+1009
SIGIL_ARITY_EXACT(0), "Is the cursor captured");
1010
sigil_module_register_native(vm, "mouse-down?", native_mouse_down,
1011
SIGIL_ARITY_EXACT(1), "Is mouse button held?");
1012
sigil_module_register_native(vm, "mouse-pressed?", native_mouse_pressed,
@@ -925,6 +1048,10 @@ void sigil__init_sigil_desktop_module(SigilVM *vm)
1048
sigil_module_export(vm, "time-elapsed");
1049
sigil_module_export(vm, "mouse-x");
1050
sigil_module_export(vm, "mouse-y");
+1051
sigil_module_export(vm, "mouse-dx");
+1052
sigil_module_export(vm, "mouse-dy");
+1053
sigil_module_export(vm, "set-cursor-captured!");
+1054
sigil_module_export(vm, "cursor-captured?");
1055
sigil_module_export(vm, "mouse-down?");
1056
sigil_module_export(vm, "mouse-pressed?");
1057
sigil_module_export(vm, "mouse-released?");
src/sigil/desktop.sglmodified
@@ -16,7 +16,8 @@
16
app-run
17
frame-width frame-height
18
frame-time time-elapsed
−19
mouse-x mouse-y mouse-wheel mouse-wheel-x
+19
mouse-x mouse-y mouse-wheel mouse-wheel-x mouse-dx mouse-dy
+20
set-cursor-captured! cursor-captured?
21
mouse-down? mouse-pressed? mouse-released?
22
key-down? key-pressed? key-released? key-code
23
quit-requested? request-quit
test/test-cursor-live.sgladded
@@ -0,0 +1,104 @@
+1
;;; Cursor capture with a window open (0.12.0). Needs an X display the
+2
;;; test may move the pointer 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-cursor-live.sgl
+7
;;;
+8
;;; Without DISPLAY or without xdotool it says so and checks nothing.
+9
;;;
+10
;;; Frame by frame: put the pointer inside the window from outside (the
+11
;;; position reported on entering is a jump, not motion: the sum must stay 0;
+12
;;; and a capture that starts inside reports no jump, so nothing may be
+13
;;; dropped); capture; move the
+14
;;; pointer 40 right and 25 down with xdotool (XTEST) and read the motion
+15
;;; back from mouse-dx / mouse-dy, summed over every frame from the capture
+16
;;; on (a jump at the capture would show in the sum); press Escape, which
+17
;;; releases; move 10 right and 5 down uncaptured and read that back (no jump
+18
;;; at the release); capture again with escape-releases? #f and press Escape,
+19
;;; which must not release; move the input focus to the root window, which
+20
;;; releases it (cursor-captured? reads GLFW's own input mode); and release,
+21
;;; which must answer #f.
+22
+23
(import (sigil test)
+24
(sigil desktop)
+25
(sigil list)
+26
(sigil process))
+27
+28
(define (display?) (and (getenv "DISPLAY") (not (getenv "WAYLAND_DISPLAY"))))
+29
(define (xdotool?) (and (display?) (= 0 (process-run "sh" "-c" "command -v xdotool >/dev/null && command -v xwininfo >/dev/null"))))
+30
(define (xdo . args) (apply process-run "xdotool" args))
+31
(define TITLE "sigil-desktop cursor test")
+32
+33
(define *seen* '())
+34
(define (see! name v) (set! *seen* (append *seen* (list (cons name v)))))
+35
(define (seen name) (let ((e (assoc name *seen*))) (and e (cdr e))))
+36
+37
(define *dx* 0.0)
+38
(define *dy* 0.0)
+39
(define (sum!) (set! *dx* (+ *dx* (mouse-dx))) (set! *dy* (+ *dy* (mouse-dy))))
+40
(define (reset-sum!) (set! *dx* 0.0) (set! *dy* 0.0))
+41
+42
(define *n* 0)
+43
(define (frame dt)
+44
(set! *n* (+ *n* 1))
+45
;; motion is summed in every frame after the capture (frame 3) up to 12,
+46
;; and after the release (frame 14) up to 20
+47
(when (or (and (> *n* 1) (<= *n* 3)) (and (> *n* 3) (<= *n* 12)) (and (> *n* 14) (<= *n* 20))) (sum!))
+48
(case *n*
+49
((1) (process-run "sh" "-c" (string-append "xdotool mousemove --window $(xdotool search --name '" TITLE "' | head -1) 320 180")))
+50
((3) (see! "motion entering" (list *dx* *dy*))
+51
(see! "captured? before" (cursor-captured?))
+52
(see! "capture" (set-cursor-captured! #t))
+53
(reset-sum!))
+54
((5) (see! "captured? after" (cursor-captured?))
+55
(xdo "mousemove_relative" "--" "40" "25"))
+56
((12) (see! "motion captured" (list *dx* *dy*))
+57
(xdo "key" "Escape"))
+58
((14) (see! "captured? after escape" (cursor-captured?))
+59
(reset-sum!))
+60
((16) (xdo "mousemove_relative" "--" "10" "5"))
+61
((20) (see! "motion released" (list *dx* *dy*))
+62
(see! "capture, escape kept" (set-cursor-captured! #t #f)))
+63
((22) (xdo "key" "Escape"))
+64
((25) (see! "captured? after kept escape" (cursor-captured?))
+65
(process-run "sh" "-c" "xdotool windowfocus $(xwininfo -root | awk '/Window id/ {print $4}')"))
+66
((29) (see! "captured? after focus loss" (cursor-captured?))
+67
(see! "capture again" (set-cursor-captured! #t))
+68
(see! "release" (set-cursor-captured! #f))
+69
(see! "captured? after release" (cursor-captured?))
+70
(request-quit))
+71
(else #f)))
+72
+73
(when (xdotool?)
+74
(app-run (lambda () #f) frame (lambda () #f) TITLE 640 360))
+75
+76
(define (near? a b) (< (abs (- a b)) 0.51))
+77
(define (motion? m x y) (and (pair? m) (near? (car m) x) (near? (cadr m) y)))
+78
+79
(test-group "cursor capture, with a window open"
+80
(if (not (xdotool?))
+81
(test "no X display or no xdotool/xwininfo: nothing checked (see the header)"
+82
(assert-true #t))
+83
(begin
+84
(test "the pointer entering the window is not motion"
+85
(assert-true (motion? (seen "motion entering") 0.0 0.0)))
+86
(test "set-cursor-captured! #t captures it (pointer inside the window)"
+87
(assert-false (seen "captured? before"))
+88
(assert-true (seen "capture"))
+89
(assert-true (seen "captured? after")))
+90
(test "a 40,25 move reads back as 40,25, summed from the capture on (nothing dropped, no jump)"
+91
(assert-true (motion? (seen "motion captured") 40.0 25.0)))
+92
(test "Escape releases the capture"
+93
(assert-false (seen "captured? after escape")))
+94
(test "after the release, a 10,5 move reads back as 10,5 (no jump)"
+95
(assert-true (motion? (seen "motion released") 10.0 5.0)))
+96
(test "with escape-releases? #f, Escape keeps it"
+97
(assert-true (seen "capture, escape kept"))
+98
(assert-true (seen "captured? after kept escape")))
+99
(test "losing the focus releases it (GLFW's input mode, not a flag)"
+100
(assert-false (seen "captured? after focus loss")))
+101
(test "set-cursor-captured! #f releases and answers #f"
+102
(assert-true (seen "capture again"))
+103
(assert-false (seen "release"))
+104
(assert-false (seen "captured? after release"))))))