Commitb1d55713Recorded24 Sep 2026Repositorysigil-desktop

The window: window-set-size!, set-fullscreen!, fullscreen? and monitor-size

Message

For Harkfell's D37 (a small window at whole scales of its view, and fullscreen at the largest whole scale that fits). set-fullscreen! takes the monitor the window covers most of (on Wayland, which reports no window position, the primary monitor), at its own video mode, and comes back to the windowed size and position (the position where the platform reports one). monitor-size is a proper list: its tail is SIGIL_EMPTY (review: SIGIL_NIL is not the empty list; it made an improper list; the only list built in desktop.c).

Tests: test/test-window.sgl (all four answer #f without a window) and test/test-window-live.sgl (with a window, on a display: monitor-size a proper list of two positive integers, fullscreen at the monitor's size and back, window-set-size! taken; it checks nothing without a display). The live test went red on the SIGIL_NIL code (2 of 5) before the fix; 56/56 with a display, 52/52 without. desktop.c compiles clean with -Wall for aarch64-macos, x86_64-macos, x86_64-windows-gnu and x86_64-linux-gnu, and the windows-amd64 config builds.

Changed
 CHANGELOG.md              |  23 +++++++++++++++++++++++
 src/c/desktop-internal.h  |   8 ++++++++
 src/c/desktop.c           | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/sigil/desktop.sgl     |   1 +
 test/test-window-live.sgl |  72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/test-window.sgl      |  18 ++++++++++++++++++
 6 files changed, 263 insertions(+)
Diff
CHANGELOG.mdmodified
@@ -7,6 +7,29 @@ All notable changes to this project will be documented in this file.
7
The format is based on [Keep a Changelog](https://keepachangelog.com/),
8
and this project adheres to [Semantic Versioning](https://semver.org/).
9
+10
## [Unreleased]
+11
+12
### Added
+13
+14
- **The window: its size, fullscreen, the monitor.** Wanted by Harkfell
+15
(ruling D37, David, 2026-09-24: a Knytt-style small window at whole scales
+16
of the view, and fullscreen at the largest whole scale that fits).
+17
- `(window-set-size! width height)` asks for a window of that size in
+18
screen units (on a scaled Wayland output the framebuffer is that times
+19
the scale). While fullscreen it sets the size the window comes back to.
+20
A tiling window manager may not grant it: read `frame-width` and
+21
`frame-height` after.
+22
- `(set-fullscreen! on?)` makes the window take the whole of the monitor
+23
it covers most of, at that monitor's own video mode (no mode change),
+24
keeping its windowed size and position (the position where the platform
+25
reports one: Wayland does not, and the compositor places the window when
+26
it comes back); `#f` goes back to them. `(fullscreen?)` answers which.
+27
- `(monitor-size)` answers `(width height)` of that monitor's video mode.
+28
On Wayland, which reports no window position, "the monitor the window
+29
covers most of" is the primary monitor, for `set-fullscreen!` too.
+30
- Without a window, all four answer `#f` (`test/test-window.sgl`); with
+31
one, `test/test-window-live.sgl` drives them on a display (Xvfb).
+32
33
## [0.10.2] - 2026-09-21
34
35
### Added
src/c/desktop-internal.hmodified
@@ -43,6 +43,14 @@ typedef struct {
43
int win_width;
44
int win_height;
45
+46
/* The windowed geometry kept while the window is fullscreen
+47
* (set-fullscreen!), in screen units, restored when it leaves. */
+48
bool have_windowed;
+49
int windowed_x;
+50
int windowed_y;
+51
int windowed_w;
+52
int windowed_h;
+53
54
/* Frame timing */
55
double last_time; /* glfwGetTime() at the previous frame */
56
double frame_time; /* Seconds since last frame */
src/c/desktop.cmodified
@@ -724,6 +724,133 @@ static Value native_request_quit(SigilVM *vm, int argc, Value *args)
724
return SIGIL_NIL;
725
}
726
+727
/* ============================================================
+728
* THE WINDOW: SIZE, FULLSCREEN, THE MONITOR
+729
* ============================================================ */
+730
+731
/* The monitor the window covers most of (a windowed window has no monitor
+732
* of its own in GLFW); the primary monitor when it covers none or where
+733
* the platform cannot say where a window is (Wayland reports no window
+734
* position). NULL without monitors. */
+735
static GLFWmonitor *window_monitor(GLFWwindow *window)
+736
{
+737
GLFWmonitor *own = glfwGetWindowMonitor(window);
+738
if (own) return own;
+739
int count = 0;
+740
GLFWmonitor **monitors = glfwGetMonitors(&count);
+741
if (!monitors || count == 0) return NULL;
+742
int wx = 0, wy = 0, ww = 0, wh = 0;
+743
if (glfwGetPlatform() != GLFW_PLATFORM_WAYLAND) glfwGetWindowPos(window, &wx, &wy);
+744
glfwGetWindowSize(window, &ww, &wh);
+745
GLFWmonitor *best = glfwGetPrimaryMonitor();
+746
long best_area = 0;
+747
for (int i = 0; i < count; i++) {
+748
int mx = 0, my = 0;
+749
glfwGetMonitorPos(monitors[i], &mx, &my);
+750
const GLFWvidmode *mode = glfwGetVideoMode(monitors[i]);
+751
if (!mode) continue;
+752
int x0 = wx > mx ? wx : mx;
+753
int y0 = wy > my ? wy : my;
+754
int x1 = (wx + ww) < (mx + mode->width) ? (wx + ww) : (mx + mode->width);
+755
int y1 = (wy + wh) < (my + mode->height) ? (wy + wh) : (my + mode->height);
+756
long area = (x1 > x0 && y1 > y0) ? (long)(x1 - x0) * (long)(y1 - y0) : 0;
+757
if (area > best_area) { best_area = area; best = monitors[i]; }
+758
}
+759
return best ? best : monitors[0];
+760
}
+761
+762
/*
+763
* (window-set-size! width height) -> #t, or #f without a window (or for a
+764
* size that is not two positive integers)
+765
*
+766
* Asks for a window of width x height screen units (on a scaled Wayland
+767
* output the framebuffer is that times the scale). While fullscreen it
+768
* sets the size the window comes back to. A tiling window manager may not
+769
* grant it: read (frame-width) and (frame-height) after, never assume.
+770
*/
+771
static Value native_window_set_size(SigilVM *vm, int argc, Value *args)
+772
{
+773
(void)vm; (void)argc;
+774
if (!g_desktop || !g_desktop->window) return SIGIL_FALSE;
+775
if (!sigil_is_fixnum(args[0]) || !sigil_is_fixnum(args[1])) return SIGIL_FALSE;
+776
int w = (int)sigil_as_fixnum(args[0]);
+777
int h = (int)sigil_as_fixnum(args[1]);
+778
if (w < 1 || h < 1) return SIGIL_FALSE;
+779
if (glfwGetWindowMonitor(g_desktop->window)) {
+780
g_desktop->windowed_w = w;
+781
g_desktop->windowed_h = h;
+782
return SIGIL_TRUE;
+783
}
+784
glfwSetWindowSize(g_desktop->window, w, h);
+785
return SIGIL_TRUE;
+786
}
+787
+788
/*
+789
* (fullscreen?) -> #t while the window is fullscreen, else #f (and #f
+790
* without a window)
+791
*/
+792
static Value native_fullscreen_p(SigilVM *vm, int argc, Value *args)
+793
{
+794
(void)vm; (void)argc; (void)args;
+795
if (!g_desktop || !g_desktop->window) return SIGIL_FALSE;
+796
return glfwGetWindowMonitor(g_desktop->window) ? SIGIL_TRUE : SIGIL_FALSE;
+797
}
+798
+799
/*
+800
* (set-fullscreen! on?) -> #t once the window is as asked, #f without a
+801
* window or a monitor
+802
*
+803
* On: the window takes the whole of the monitor it covers most of, at
+804
* that monitor's own video mode (no mode change: the desktop's resolution
+805
* and refresh rate), and its windowed position and size are kept. Off: it
+806
* goes back to them. Asking for what already is does nothing.
+807
*/
+808
static Value native_set_fullscreen(SigilVM *vm, int argc, Value *args)
+809
{
+810
(void)vm; (void)argc;
+811
if (!g_desktop || !g_desktop->window) return SIGIL_FALSE;
+812
GLFWwindow *window = g_desktop->window;
+813
bool on = !sigil_is_false(args[0]);
+814
bool is = glfwGetWindowMonitor(window) != NULL;
+815
if (on == is) return SIGIL_TRUE;
+816
if (on) {
+817
GLFWmonitor *monitor = window_monitor(window);
+818
const GLFWvidmode *mode = monitor ? glfwGetVideoMode(monitor) : NULL;
+819
if (!mode) return SIGIL_FALSE;
+820
/* Wayland does not say where a window is (GLFW reports it as an error); the
+821
* compositor places the window when it comes back */
+822
if (glfwGetPlatform() != GLFW_PLATFORM_WAYLAND)
+823
glfwGetWindowPos(window, &g_desktop->windowed_x, &g_desktop->windowed_y);
+824
glfwGetWindowSize(window, &g_desktop->windowed_w, &g_desktop->windowed_h);
+825
g_desktop->have_windowed = true;
+826
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
+827
} else {
+828
int w = g_desktop->have_windowed ? g_desktop->windowed_w : 800;
+829
int h = g_desktop->have_windowed ? g_desktop->windowed_h : 600;
+830
int x = g_desktop->have_windowed ? g_desktop->windowed_x : 64;
+831
int y = g_desktop->have_windowed ? g_desktop->windowed_y : 64;
+832
glfwSetWindowMonitor(window, NULL, x, y, w, h, 0);
+833
}
+834
return SIGIL_TRUE;
+835
}
+836
+837
/*
+838
* (monitor-size) -> (width height), the video mode of the monitor the
+839
* window covers most of (on Wayland, which reports no window position,
+840
* the primary monitor), or #f without a window or a monitor. A proper
+841
* list: its tail is SIGIL_EMPTY, the empty list (SIGIL_NIL is not one).
+842
*/
+843
static Value native_monitor_size(SigilVM *vm, int argc, Value *args)
+844
{
+845
(void)argc; (void)args;
+846
if (!g_desktop || !g_desktop->window) return SIGIL_FALSE;
+847
GLFWmonitor *monitor = window_monitor(g_desktop->window);
+848
const GLFWvidmode *mode = monitor ? glfwGetVideoMode(monitor) : NULL;
+849
if (!mode) return SIGIL_FALSE;
+850
return sigil_cons(vm, sigil_fixnum(mode->width),
+851
sigil_cons(vm, sigil_fixnum(mode->height), SIGIL_EMPTY));
+852
}
+853
854
/* ============================================================
855
* MODULE INITIALIZATION
856
* ============================================================ */
@@ -780,6 +907,16 @@ void sigil__init_sigil_desktop_module(SigilVM *vm)
907
sigil_module_register_native(vm, "request-quit", native_request_quit,
908
SIGIL_ARITY_EXACT(0), "Request application quit");
909
+910
/* The window: size, fullscreen, the monitor (0.11.0) */
+911
sigil_module_register_native(vm, "window-set-size!", native_window_set_size,
+912
SIGIL_ARITY_EXACT(2), "Ask for a window of width x height screen units");
+913
sigil_module_register_native(vm, "fullscreen?", native_fullscreen_p,
+914
SIGIL_ARITY_EXACT(0), "Is the window fullscreen");
+915
sigil_module_register_native(vm, "set-fullscreen!", native_set_fullscreen,
+916
SIGIL_ARITY_EXACT(1), "Take the whole monitor, or go back to the window");
+917
sigil_module_register_native(vm, "monitor-size", native_monitor_size,
+918
SIGIL_ARITY_EXACT(0), "The monitor the window is on: (width height)");
+919
920
/* Export all */
921
sigil_module_export(vm, "app-run");
922
sigil_module_export(vm, "frame-width");
@@ -797,6 +934,10 @@ void sigil__init_sigil_desktop_module(SigilVM *vm)
934
sigil_module_export(vm, "key-code");
935
sigil_module_export(vm, "quit-requested?");
936
sigil_module_export(vm, "request-quit");
+937
sigil_module_export(vm, "window-set-size!");
+938
sigil_module_export(vm, "fullscreen?");
+939
sigil_module_export(vm, "set-fullscreen!");
+940
sigil_module_export(vm, "monitor-size");
941
942
sigil_end_module(vm);
943
}
src/sigil/desktop.sglmodified
@@ -20,6 +20,7 @@
20
mouse-down? mouse-pressed? mouse-released?
21
key-down? key-pressed? key-released? key-code
22
quit-requested? request-quit
+23
window-set-size! fullscreen? set-fullscreen! monitor-size
24
25
;; Cooperative frame scheduling
26
run-game
test/test-window-live.sgladded
@@ -0,0 +1,72 @@
+1
;;; The window calls with a window open (0.11.0). Needs a display: run it
+2
;;; on a headless X server, never on someone's desktop, e.g.
+3
;;;
+4
;;; Xvfb :74 -screen 0 1700x900x24 +extension GLX -nolisten tcp &
+5
;;; env -u WAYLAND_DISPLAY DISPLAY=:74 sigil test test/test-window-live.sgl
+6
;;;
+7
;;; With no display (DISPLAY and WAYLAND_DISPLAY both unset) it says so and
+8
;;; checks nothing; the no-window answers are test-window.sgl's.
+9
;;;
+10
;;; On the first frame: monitor-size is a proper list of two positive
+11
;;; integers; set-fullscreen! #t makes fullscreen? #t and the framebuffer
+12
;;; the monitor's size (a frame later: the platform resizes between frames);
+13
;;; set-fullscreen! #f brings back the windowed size; window-set-size! asks
+14
;;; for a new size and gets it (no window manager on Xvfb to refuse).
+15
+16
(import (sigil test)
+17
(sigil desktop)
+18
(sigil list)
+19
(sigil process))
+20
+21
(define (display?)
+22
(or (getenv "DISPLAY") (getenv "WAYLAND_DISPLAY")))
+23
+24
(define *seen* '()) ; (name . value) in the order seen
+25
(define (see! name v) (set! *seen* (append *seen* (list (cons name v)))))
+26
(define (seen name) (let ((e (assoc name *seen*))) (and e (cdr e))))
+27
+28
(define *n* 0)
+29
(define (frame dt)
+30
(set! *n* (+ *n* 1))
+31
(case *n*
+32
((1) (see! "monitor-size" (monitor-size))
+33
(see! "windowed" (list (frame-width) (frame-height)))
+34
(see! "fullscreen? before" (fullscreen?))
+35
(see! "set-fullscreen! on" (set-fullscreen! #t)))
+36
((4) (see! "fullscreen? on" (fullscreen?))
+37
(see! "size on" (list (frame-width) (frame-height)))
+38
(see! "set-fullscreen! off" (set-fullscreen! #f)))
+39
((7) (see! "fullscreen? off" (fullscreen?))
+40
(see! "size off" (list (frame-width) (frame-height)))
+41
(see! "window-set-size!" (window-set-size! 400 176)))
+42
((10) (see! "size set" (list (frame-width) (frame-height)))
+43
(request-quit))
+44
(else #f)))
+45
+46
(when (display?)
+47
(app-run (lambda () #f) frame (lambda () #f) "sigil-desktop window test" 640 360))
+48
+49
(define (positive-ints? l n)
+50
(and (list? l) (= (length l) n) (every (lambda (x) (and (integer? x) (> x 0))) l)))
+51
+52
(test-group "the window, with a window open"
+53
(if (not (display?))
+54
(test "no display: nothing checked (see the header)"
+55
(assert-true #t))
+56
(begin
+57
(test "monitor-size is a proper list of two positive integers"
+58
(assert-true (positive-ints? (seen "monitor-size") 2)))
+59
(test "the window opened at its asked size"
+60
(assert-equal (list 640 360) (seen "windowed")))
+61
(test "set-fullscreen! #t: fullscreen, at the monitor's size"
+62
(assert-false (seen "fullscreen? before"))
+63
(assert-true (seen "set-fullscreen! on"))
+64
(assert-true (seen "fullscreen? on"))
+65
(assert-equal (seen "monitor-size") (seen "size on")))
+66
(test "set-fullscreen! #f: back to the windowed size"
+67
(assert-true (seen "set-fullscreen! off"))
+68
(assert-false (seen "fullscreen? off"))
+69
(assert-equal (list 640 360) (seen "size off")))
+70
(test "window-set-size! asks, and the window takes it"
+71
(assert-true (seen "window-set-size!"))
+72
(assert-equal (list 400 176) (seen "size set"))))))
test/test-window.sgladded
@@ -0,0 +1,18 @@
+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.
+5
+6
(import (sigil test)
+7
(sigil desktop))
+8
+9
(test-group "the window, with no window open"
+10
(test "fullscreen? is #f"
+11
(assert-false (fullscreen?)))
+12
(test "monitor-size is #f"
+13
(assert-false (monitor-size)))
+14
(test "window-set-size! is #f"
+15
(assert-false (window-set-size! 800 352)))
+16
(test "set-fullscreen! is #f, on or off"
+17
(assert-false (set-fullscreen! #t))
+18
(assert-false (set-fullscreen! #f))))