Cursor capture for mouse look: set-cursor-captured!, mouse-dx/dy
(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.
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(-)src/c/desktop-internal.hmodified
* summed, cleared with the other per-frame input; + is up/right. */ double wheel_x; double wheel_y; /* Mouse motion since the last frame (0.12.0), in framebuffer pixels: * the cursor callback's position differences, summed, cleared with the * other per-frame input. It keeps counting while the cursor is * captured, when the position itself no longer means a place on the * window. A capture change re-reads the position instead of trusting * the last callback's; skip_motion drops the position reported with * the pointer entering the window, which is a jump from wherever it * left. */ double motion_x; double motion_y; bool skip_motion; /* Cursor capture (set-cursor-captured!): the cursor hidden and held, * motion unbounded (GLFW_CURSOR_DISABLED, raw motion when the platform * has it). Released when the window loses focus, and on Escape unless * the app asked to keep it. Whether it holds is read from GLFW's input * mode, not remembered. */ bool escape_releases; /* Quit handling */ bool quit_requested;src/c/desktop.cmodified
memset(g_desktop->mouse_released, 0, sizeof(g_desktop->mouse_released)); g_desktop->wheel_x = 0.0; g_desktop->wheel_y = 0.0; g_desktop->motion_x = 0.0; g_desktop->motion_y = 0.0;}int sigil_desktop_framebuffer_width(void) fprintf(stderr, "sigil-desktop: GLFW error 0x%X: %s\n", code, description);}/* Is the cursor captured now, as GLFW has it (a focus loss or Escape may * have released it since it was asked for). */static bool captured_now(void){ return g_desktop && g_desktop->window && glfwGetInputMode(g_desktop->window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED;}/* Capture or release the cursor. GLFW_CURSOR_DISABLED hides the cursor * and keeps it on the window with unbounded motion (on Wayland through the * compositor's pointer constraints); raw motion, where the platform has it, * skips pointer acceleration. The position GLFW reports changes meaning * with the mode (a virtual position while captured), so it is read again * here rather than differenced against the last callback's; no motion is * dropped. A platform that cannot capture (a Wayland compositor without * pointer constraints) leaves the cursor as it was. Returns whether the * cursor is captured now. */static bool set_capture(bool on){ if (!g_desktop || !g_desktop->window) return false; GLFWwindow *w = g_desktop->window; if (on == captured_now()) return on; glfwGetError(NULL); /* clear any earlier error */ glfwSetInputMode(w, GLFW_CURSOR, on ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL); if (on && glfwGetError(NULL) != GLFW_NO_ERROR) { glfwSetInputMode(w, GLFW_CURSOR, GLFW_CURSOR_NORMAL); } else if (glfwRawMouseMotionSupported()) { glfwSetInputMode(w, GLFW_RAW_MOUSE_MOTION, on ? GLFW_TRUE : GLFW_FALSE); } glfwGetCursorPos(w, &g_desktop->cursor_x, &g_desktop->cursor_y); return captured_now();}static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods){ (void)window; (void)scancode; (void)mods; if (!g_desktop) return; if (key < 0 || key >= SIGIL_DESKTOP_MAX_KEYS) return; /* Escape always gives the cursor back unless the app asked to keep it, * so nobody is left with a hidden cursor. The key still reaches the app. */ if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS && g_desktop->escape_releases && captured_now()) { set_capture(false); } if (action == GLFW_PRESS || action == GLFW_REPEAT) { /* A repeat is a held key, not a new press: pressed? fires once. */ if (!g_desktop->keys_down[key]) {{ (void)window; if (!g_desktop) return; if (g_desktop->skip_motion) { g_desktop->skip_motion = false; } else if (g_desktop->win_width > 0 && g_desktop->win_height > 0) { /* in framebuffer pixels, as mouse-x / mouse-y */ g_desktop->motion_x += (x - g_desktop->cursor_x) * ((double)g_desktop->fb_width / (double)g_desktop->win_width); g_desktop->motion_y += (y - g_desktop->cursor_y) * ((double)g_desktop->fb_height / (double)g_desktop->win_height); } g_desktop->cursor_x = x; g_desktop->cursor_y = y;}/* Losing focus gives the cursor back: another window must never find it * captured by this one. */static void window_focus_callback(GLFWwindow *window, int focused){ (void)window; if (!g_desktop) return; if (!focused && captured_now()) set_capture(false);}/* The position reported as the pointer enters is a jump from wherever it * left the window, not motion: drop it. */static void cursor_enter_callback(GLFWwindow *window, int entered){ (void)window; if (g_desktop && entered) g_desktop->skip_motion = true;}static void window_close_callback(GLFWwindow *window){ (void)window; glfwSetKeyCallback(window, key_callback); glfwSetMouseButtonCallback(window, mouse_button_callback); glfwSetCursorPosCallback(window, cursor_pos_callback); glfwSetWindowFocusCallback(window, window_focus_callback); glfwSetCursorEnterCallback(window, cursor_enter_callback); glfwSetScrollCallback(window, scroll_callback); glfwSetWindowCloseCallback(window, window_close_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); return sigil_flonum(g_desktop ? g_desktop->wheel_x : 0.0);}/* * (mouse-dx) / (mouse-dy) -> float: the mouse's motion since the last frame * in framebuffer pixels (+x right, +y down), captured or not. With the * cursor captured this is the only reading that means anything: the * position keeps growing without bound. */static Value native_mouse_dx(SigilVM *vm, int argc, Value *args){ (void)vm; (void)argc; (void)args; return sigil_flonum(g_desktop ? g_desktop->motion_x : 0.0);}static Value native_mouse_dy(SigilVM *vm, int argc, Value *args){ (void)vm; (void)argc; (void)args; return sigil_flonum(g_desktop ? g_desktop->motion_y : 0.0);}/* * (set-cursor-captured! on? [escape-releases?]) -> whether the cursor is now * captured * * #t hides the cursor and holds it on the window for mouse look: read * mouse-dx / mouse-dy. #f gives it back. The window losing focus always * releases it, and so does Escape unless escape-releases? is #f (default * #t; the Escape key still reaches the app either way). The answer is what * GLFW has: #f when the platform cannot capture. Before the window exists * this returns #f and does nothing. */static Value native_set_cursor_captured(SigilVM *vm, int argc, Value *args){ (void)vm; if (!g_desktop) return SIGIL_FALSE; bool on = !sigil_is_false(args[0]); g_desktop->escape_releases = argc > 1 ? !sigil_is_false(args[1]) : true; return set_capture(on) ? SIGIL_TRUE : SIGIL_FALSE;}/* (cursor-captured?) -> is the cursor captured now, read from GLFW's input * mode (a focus loss or Escape may have released it since * set-cursor-captured!) */static Value native_cursor_captured_p(SigilVM *vm, int argc, Value *args){ (void)vm; (void)argc; (void)args; return captured_now() ? SIGIL_TRUE : SIGIL_FALSE;}/* * (mouse-y) -> float, framebuffer pixels */ SIGIL_ARITY_EXACT(0), "Wheel travel since the last frame (vertical)"); sigil_module_register_native(vm, "mouse-wheel-x", native_mouse_wheel_x, SIGIL_ARITY_EXACT(0), "Wheel travel since the last frame (horizontal)"); sigil_module_register_native(vm, "mouse-dx", native_mouse_dx, SIGIL_ARITY_EXACT(0), "Mouse motion since the last frame (x)"); sigil_module_register_native(vm, "mouse-dy", native_mouse_dy, SIGIL_ARITY_EXACT(0), "Mouse motion since the last frame (y)"); sigil_module_register_native(vm, "set-cursor-captured!", native_set_cursor_captured, SIGIL_ARITY_RANGE(1, 2), "Capture the cursor for mouse look, or release it"); sigil_module_register_native(vm, "cursor-captured?", native_cursor_captured_p, SIGIL_ARITY_EXACT(0), "Is the cursor captured"); sigil_module_register_native(vm, "mouse-down?", native_mouse_down, SIGIL_ARITY_EXACT(1), "Is mouse button held?"); sigil_module_register_native(vm, "mouse-pressed?", native_mouse_pressed, sigil_module_export(vm, "time-elapsed"); sigil_module_export(vm, "mouse-x"); sigil_module_export(vm, "mouse-y"); sigil_module_export(vm, "mouse-dx"); sigil_module_export(vm, "mouse-dy"); sigil_module_export(vm, "set-cursor-captured!"); sigil_module_export(vm, "cursor-captured?"); sigil_module_export(vm, "mouse-down?"); sigil_module_export(vm, "mouse-pressed?"); sigil_module_export(vm, "mouse-released?");src/sigil/desktop.sglmodified
app-run frame-width frame-height frame-time time-elapsed mouse-x mouse-y mouse-wheel mouse-wheel-x mouse-x mouse-y mouse-wheel mouse-wheel-x mouse-dx mouse-dy set-cursor-captured! cursor-captured? mouse-down? mouse-pressed? mouse-released? key-down? key-pressed? key-released? key-code quit-requested? request-quittest/test-cursor-live.sgladded
;;; Cursor capture with a window open (0.12.0). Needs an X display the;;; test may move the pointer on, xdotool and xwininfo; run it on a headless;;; X server, never on someone's desktop, e.g.;;;;;; Xvfb :74 -screen 0 1700x900x24 +extension GLX -nolisten tcp &;;; env -u WAYLAND_DISPLAY DISPLAY=:74 guix shell xdotool xwininfo -- sigil test test/test-cursor-live.sgl;;;;;; Without DISPLAY or without xdotool it says so and checks nothing.;;;;;; Frame by frame: put the pointer inside the window from outside (the;;; position reported on entering is a jump, not motion: the sum must stay 0;;;; and a capture that starts inside reports no jump, so nothing may be;;; dropped); capture; move the;;; pointer 40 right and 25 down with xdotool (XTEST) and read the motion;;; back from mouse-dx / mouse-dy, summed over every frame from the capture;;; on (a jump at the capture would show in the sum); press Escape, which;;; releases; move 10 right and 5 down uncaptured and read that back (no jump;;; at the release); capture again with escape-releases? #f and press Escape,;;; which must not release; move the input focus to the root window, which;;; releases it (cursor-captured? reads GLFW's own input mode); and release,;;; which must answer #f.(import (sigil test) (sigil desktop) (sigil list) (sigil process))(define (display?) (and (getenv "DISPLAY") (not (getenv "WAYLAND_DISPLAY"))))(define (xdotool?) (and (display?) (= 0 (process-run "sh" "-c" "command -v xdotool >/dev/null && command -v xwininfo >/dev/null"))))(define (xdo . args) (apply process-run "xdotool" args))(define TITLE "sigil-desktop cursor test")(define *seen* '())(define (see! name v) (set! *seen* (append *seen* (list (cons name v)))))(define (seen name) (let ((e (assoc name *seen*))) (and e (cdr e))))(define *dx* 0.0)(define *dy* 0.0)(define (sum!) (set! *dx* (+ *dx* (mouse-dx))) (set! *dy* (+ *dy* (mouse-dy))))(define (reset-sum!) (set! *dx* 0.0) (set! *dy* 0.0))(define *n* 0)(define (frame dt) (set! *n* (+ *n* 1)) ;; motion is summed in every frame after the capture (frame 3) up to 12, ;; and after the release (frame 14) up to 20 (when (or (and (> *n* 1) (<= *n* 3)) (and (> *n* 3) (<= *n* 12)) (and (> *n* 14) (<= *n* 20))) (sum!)) (case *n* ((1) (process-run "sh" "-c" (string-append "xdotool mousemove --window $(xdotool search --name '" TITLE "' | head -1) 320 180"))) ((3) (see! "motion entering" (list *dx* *dy*)) (see! "captured? before" (cursor-captured?)) (see! "capture" (set-cursor-captured! #t)) (reset-sum!)) ((5) (see! "captured? after" (cursor-captured?)) (xdo "mousemove_relative" "--" "40" "25")) ((12) (see! "motion captured" (list *dx* *dy*)) (xdo "key" "Escape")) ((14) (see! "captured? after escape" (cursor-captured?)) (reset-sum!)) ((16) (xdo "mousemove_relative" "--" "10" "5")) ((20) (see! "motion released" (list *dx* *dy*)) (see! "capture, escape kept" (set-cursor-captured! #t #f))) ((22) (xdo "key" "Escape")) ((25) (see! "captured? after kept escape" (cursor-captured?)) (process-run "sh" "-c" "xdotool windowfocus $(xwininfo -root | awk '/Window id/ {print $4}')")) ((29) (see! "captured? after focus loss" (cursor-captured?)) (see! "capture again" (set-cursor-captured! #t)) (see! "release" (set-cursor-captured! #f)) (see! "captured? after release" (cursor-captured?)) (request-quit)) (else #f)))(when (xdotool?) (app-run (lambda () #f) frame (lambda () #f) TITLE 640 360))(define (near? a b) (< (abs (- a b)) 0.51))(define (motion? m x y) (and (pair? m) (near? (car m) x) (near? (cadr m) y)))(test-group "cursor capture, with a window open" (if (not (xdotool?)) (test "no X display or no xdotool/xwininfo: nothing checked (see the header)" (assert-true #t)) (begin (test "the pointer entering the window is not motion" (assert-true (motion? (seen "motion entering") 0.0 0.0))) (test "set-cursor-captured! #t captures it (pointer inside the window)" (assert-false (seen "captured? before")) (assert-true (seen "capture")) (assert-true (seen "captured? after"))) (test "a 40,25 move reads back as 40,25, summed from the capture on (nothing dropped, no jump)" (assert-true (motion? (seen "motion captured") 40.0 25.0))) (test "Escape releases the capture" (assert-false (seen "captured? after escape"))) (test "after the release, a 10,5 move reads back as 10,5 (no jump)" (assert-true (motion? (seen "motion released") 10.0 5.0))) (test "with escape-releases? #f, Escape keeps it" (assert-true (seen "capture, escape kept")) (assert-true (seen "captured? after kept escape"))) (test "losing the focus releases it (GLFW's input mode, not a flag)" (assert-false (seen "captured? after focus loss"))) (test "set-cursor-captured! #f releases and answers #f" (assert-true (seen "capture again")) (assert-false (seen "release")) (assert-false (seen "captured? after release"))))))