AtlatestRepositorysigil-desktop

sigil-desktop / tree / src / cdesktop.c

1/*
2 * desktop.c - the (sigil desktop) module
3 *
4 * The desktop host layer: window, GL context, input, frame clock and quit,
5 * over GLFW (vendored under vendor/glfw). One binary runs as a native
6 * Wayland client or under X11 on Linux, picked at startup; GLFW loads the
7 * platform libraries (libwayland-client, libxkbcommon, libX11, libGL/EGL,
8 * ...) with dlopen, so nothing platform-specific is linked.
9 *
10 * The exported Sigil API is the one (sigil app) had over sokol_app: app-run,
11 * frame-*, mouse-*, key-*, quit-requested?, request-quit.
12 */
14#include "desktop-internal.h"
15#include "sigil-desktop.h"
16#include "sigil-internal.h"
18/* No GL header: this layer never calls GL itself; sokol_gfx (in
19 * sigil-graphics) brings its own. */
20#define GLFW_INCLUDE_NONE
21#include <GLFW/glfw3.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
27/* Global app state */
28DesktopState *g_desktop = NULL;
30/*
31 * Initialize app state
32 */
33void sigil_desktop_state_init(SigilVM *vm)
35 if (g_desktop) return;
37 g_desktop = calloc(1, sizeof(DesktopState));
38 g_desktop->vm = vm;
39 g_desktop->init_callback = SIGIL_FALSE;
40 g_desktop->frame_callback = SIGIL_FALSE;
41 g_desktop->cleanup_callback = SIGIL_FALSE;
42 g_desktop->fb_width = 1;
43 g_desktop->fb_height = 1;
44 g_desktop->win_width = 1;
45 g_desktop->win_height = 1;
48void sigil_desktop_state_shutdown(void)
50 if (g_desktop) {
51 free(g_desktop);
52 g_desktop = NULL;
53 }
56/*
57 * Clear per-frame input state
58 */
59void sigil_desktop_clear_frame_input(void)
61 if (!g_desktop) return;
62 memset(g_desktop->keys_pressed, 0, sizeof(g_desktop->keys_pressed));
63 memset(g_desktop->keys_released, 0, sizeof(g_desktop->keys_released));
64 memset(g_desktop->mouse_pressed, 0, sizeof(g_desktop->mouse_pressed));
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;
72int sigil_desktop_framebuffer_width(void)
74 return (g_desktop && g_desktop->fb_width > 0) ? g_desktop->fb_width : 1;
77int sigil_desktop_framebuffer_height(void)
79 return (g_desktop && g_desktop->fb_height > 0) ? g_desktop->fb_height : 1;
82/*
83 * Convert Scheme key symbol to a GLFW key code (GLFW_KEY_UNKNOWN when the
84 * symbol names no key). Letters, digits, space, return/enter, escape/esc,
85 * tab, backspace, the arrows, shift/ctrl/alt (left), and since 0.10.1 f1..f25,
86 * page-up/page-down, home/end, insert/delete, right-shift/ctrl/alt,
87 * caps-lock, the punctuation row (comma period slash semicolon apostrophe
88 * minus equal left-bracket right-bracket backslash grave, or the character
89 * itself as a symbol) and the keypad (kp-0..kp-9, kp-enter, kp-add,
90 * kp-subtract).
91 */
92int sigil_desktop_key_code_from_symbol(SigilVM *vm, Value sym)
94 (void)vm;
95 if (!sigil_is_symbol(sym)) return GLFW_KEY_UNKNOWN;
97 SigilSymbol *s = (SigilSymbol *)sigil_as_ptr(sym);
98 const char *name = s->name;
100 /* Letters */
101 if (s->length == 1 && name[0] >= 'a' && name[0] <= 'z') {
102 return GLFW_KEY_A + (name[0] - 'a');
103 }
105 /* Numbers */
106 if (s->length == 1 && name[0] >= '0' && name[0] <= '9') {
107 return GLFW_KEY_0 + (name[0] - '0');
108 }
110 /* Special keys */
111 if (strcmp(name, "space") == 0) return GLFW_KEY_SPACE;
112 if (strcmp(name, "return") == 0 || strcmp(name, "enter") == 0) return GLFW_KEY_ENTER;
113 if (strcmp(name, "escape") == 0 || strcmp(name, "esc") == 0) return GLFW_KEY_ESCAPE;
114 if (strcmp(name, "tab") == 0) return GLFW_KEY_TAB;
115 if (strcmp(name, "backspace") == 0) return GLFW_KEY_BACKSPACE;
117 /* Arrow keys */
118 if (strcmp(name, "left") == 0) return GLFW_KEY_LEFT;
119 if (strcmp(name, "right") == 0) return GLFW_KEY_RIGHT;
120 if (strcmp(name, "up") == 0) return GLFW_KEY_UP;
121 if (strcmp(name, "down") == 0) return GLFW_KEY_DOWN;
123 /* Modifiers */
124 if (strcmp(name, "shift") == 0) return GLFW_KEY_LEFT_SHIFT;
125 if (strcmp(name, "ctrl") == 0 || strcmp(name, "control") == 0) return GLFW_KEY_LEFT_CONTROL;
126 if (strcmp(name, "alt") == 0) return GLFW_KEY_LEFT_ALT;
128 /* 0.10.1: the rest of a keyboard an editor needs (a tracker's FT2
129 * table: function keys, paging, the punctuation row, the right
130 * modifiers). The state arrays already span every GLFW code; only
131 * these names were missing. */
132 if (s->length >= 2 && s->length <= 3 && name[0] == 'f' && name[1] >= '1' && name[1] <= '9'
133 && (s->length == 2 || (name[2] >= '0' && name[2] <= '9'))) {
134 int n = (name[1] - '0');
135 if (s->length == 3) n = n * 10 + (name[2] - '0');
136 if (n >= 1 && n <= 25) return GLFW_KEY_F1 + (n - 1);
137 }
138 if (strcmp(name, "page-up") == 0 || strcmp(name, "pageup") == 0) return GLFW_KEY_PAGE_UP;
139 if (strcmp(name, "page-down") == 0 || strcmp(name, "pagedown") == 0) return GLFW_KEY_PAGE_DOWN;
140 if (strcmp(name, "home") == 0) return GLFW_KEY_HOME;
141 if (strcmp(name, "end") == 0) return GLFW_KEY_END;
142 if (strcmp(name, "insert") == 0) return GLFW_KEY_INSERT;
143 if (strcmp(name, "delete") == 0) return GLFW_KEY_DELETE;
144 if (strcmp(name, "right-shift") == 0 || strcmp(name, "rshift") == 0) return GLFW_KEY_RIGHT_SHIFT;
145 if (strcmp(name, "right-ctrl") == 0 || strcmp(name, "rctrl") == 0) return GLFW_KEY_RIGHT_CONTROL;
146 if (strcmp(name, "right-alt") == 0 || strcmp(name, "ralt") == 0) return GLFW_KEY_RIGHT_ALT;
147 if (strcmp(name, "caps-lock") == 0) return GLFW_KEY_CAPS_LOCK;
148 if (strcmp(name, "comma") == 0 || strcmp(name, ",") == 0) return GLFW_KEY_COMMA;
149 if (strcmp(name, "period") == 0 || strcmp(name, ".") == 0) return GLFW_KEY_PERIOD;
150 if (strcmp(name, "slash") == 0 || strcmp(name, "/") == 0) return GLFW_KEY_SLASH;
151 if (strcmp(name, "semicolon") == 0 || strcmp(name, ";") == 0) return GLFW_KEY_SEMICOLON;
152 if (strcmp(name, "apostrophe") == 0 || strcmp(name, "'") == 0) return GLFW_KEY_APOSTROPHE;
153 if (strcmp(name, "minus") == 0 || strcmp(name, "-") == 0) return GLFW_KEY_MINUS;
154 if (strcmp(name, "equal") == 0 || strcmp(name, "=") == 0) return GLFW_KEY_EQUAL;
155 if (strcmp(name, "left-bracket") == 0 || strcmp(name, "[") == 0) return GLFW_KEY_LEFT_BRACKET;
156 if (strcmp(name, "right-bracket") == 0 || strcmp(name, "]") == 0) return GLFW_KEY_RIGHT_BRACKET;
157 if (strcmp(name, "backslash") == 0 || strcmp(name, "\\") == 0) return GLFW_KEY_BACKSLASH;
158 if (strcmp(name, "grave") == 0 || strcmp(name, "backquote") == 0 || strcmp(name, "`") == 0) return GLFW_KEY_GRAVE_ACCENT;
159 if (strcmp(name, "kp-enter") == 0) return GLFW_KEY_KP_ENTER;
160 if (strcmp(name, "kp-add") == 0) return GLFW_KEY_KP_ADD;
161 if (strcmp(name, "kp-subtract") == 0) return GLFW_KEY_KP_SUBTRACT;
162 if (s->length == 4 && strncmp(name, "kp-", 3) == 0 && name[3] >= '0' && name[3] <= '9') {
163 return GLFW_KEY_KP_0 + (name[3] - '0');
164 }
166 return GLFW_KEY_UNKNOWN;
169/*
170 * Convert mouse button symbol to index
171 */
172static int mouse_button_from_symbol(SigilVM *vm, Value sym)
174 (void)vm;
175 if (!sigil_is_symbol(sym)) return -1;
177 SigilSymbol *s = (SigilSymbol *)sigil_as_ptr(sym);
178 const char *name = s->name;
180 if (strcmp(name, "left") == 0) return GLFW_MOUSE_BUTTON_LEFT;
181 if (strcmp(name, "right") == 0) return GLFW_MOUSE_BUTTON_RIGHT;
182 if (strcmp(name, "middle") == 0) return GLFW_MOUSE_BUTTON_MIDDLE;
184 return -1;
187/* ============================================================
188 * GLFW CALLBACKS
189 * ============================================================ */
191/* Check for VM errors and print them */
192static void check_vm_error(const char *context)
194 if (!g_desktop) return;
195 const char *err = sigil_error_message(g_desktop->vm);
196 if (err) {
197 fprintf(stderr, "Scheme error in %s: %s\n", context, err);
198 sigil_error_clear(g_desktop->vm);
199 g_desktop->quit_requested = true;
200 if (g_desktop->window) glfwSetWindowShouldClose(g_desktop->window, GLFW_TRUE);
201 }
204static void glfw_error_callback(int code, const char *description)
206 fprintf(stderr, "sigil-desktop: GLFW error 0x%X: %s\n", code, description);
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). */
211static bool captured_now(void)
213 return g_desktop && g_desktop->window &&
214 glfwGetInputMode(g_desktop->window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED;
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. */
226static bool set_capture(bool on)
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();
242static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
244 (void)window; (void)scancode; (void)mods;
245 if (!g_desktop) return;
246 if (key < 0 || key >= SIGIL_DESKTOP_MAX_KEYS) return;
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 }
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]) {
258 g_desktop->keys_pressed[key] = true;
259 }
260 g_desktop->keys_down[key] = true;
261 } else if (action == GLFW_RELEASE) {
262 g_desktop->keys_down[key] = false;
263 g_desktop->keys_released[key] = true;
264 }
267static void mouse_button_callback(GLFWwindow *window, int button, int action, int mods)
269 (void)window; (void)mods;
270 if (!g_desktop) return;
271 if (button < 0 || button >= SIGIL_DESKTOP_MAX_MOUSE_BUTTONS) return;
273 if (action == GLFW_PRESS) {
274 if (!g_desktop->mouse_buttons[button]) {
275 g_desktop->mouse_pressed[button] = true;
276 }
277 g_desktop->mouse_buttons[button] = true;
278 } else if (action == GLFW_RELEASE) {
279 g_desktop->mouse_buttons[button] = false;
280 g_desktop->mouse_released[button] = true;
281 }
284static void scroll_callback(GLFWwindow *window, double dx, double dy)
286 (void)window;
287 if (!g_desktop) return;
288 g_desktop->wheel_x += dx;
289 g_desktop->wheel_y += dy;
292static void cursor_pos_callback(GLFWwindow *window, double x, double y)
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;
307/* Losing focus gives the cursor back: another window must never find it
308 * captured by this one. */
309static void window_focus_callback(GLFWwindow *window, int focused)
311 (void)window;
312 if (!g_desktop) return;
313 if (!focused && captured_now()) set_capture(false);
316/* The position reported as the pointer enters is a jump from wherever it
317 * left the window, not motion: drop it. */
318static void cursor_enter_callback(GLFWwindow *window, int entered)
320 (void)window;
321 if (g_desktop && entered) g_desktop->skip_motion = true;
324static void window_close_callback(GLFWwindow *window)
326 (void)window;
327 if (!g_desktop) return;
328 /* The window manager asked; the game sees (quit-requested?) => #t
329 * and the loop ends after this frame, as with sokol_app's
330 * QUIT_REQUESTED event. */
331 g_desktop->quit_requested = true;
334static void framebuffer_size_callback(GLFWwindow *window, int width, int height)
336 if (!g_desktop) return;
337 g_desktop->fb_width = width;
338 g_desktop->fb_height = height;
339 if (getenv("SIGIL_DESKTOP_VERBOSE")) {
340 float sx = 1.0f, sy = 1.0f;
341 glfwGetWindowContentScale(window, &sx, &sy);
342 fprintf(stderr, "sigil-desktop: framebuffer=%dx%d window=%dx%d content-scale=%.2fx%.2f\n",
343 width, height, g_desktop->win_width, g_desktop->win_height, sx, sy);
344 }
347static void window_size_callback(GLFWwindow *window, int width, int height)
349 (void)window;
350 if (!g_desktop) return;
351 g_desktop->win_width = width;
352 g_desktop->win_height = height;
355/* Cursor position scaled from window units into framebuffer pixels, so
356 * mouse-x/mouse-y and frame-width/frame-height share one coordinate
357 * system whatever the output scale is. */
358static double cursor_to_fb_x(void)
360 if (!g_desktop || g_desktop->win_width <= 0) return 0.0;
361 return g_desktop->cursor_x * ((double)g_desktop->fb_width / (double)g_desktop->win_width);
364static double cursor_to_fb_y(void)
366 if (!g_desktop || g_desktop->win_height <= 0) return 0.0;
367 return g_desktop->cursor_y * ((double)g_desktop->fb_height / (double)g_desktop->win_height);
370/* ============================================================
371 * PLATFORM SELECTION
372 * ============================================================ */
374/*
375 * GLFW picks Wayland when XDG_SESSION_TYPE=wayland and WAYLAND_DISPLAY is
376 * set, X11 when XDG_SESSION_TYPE=x11 and DISPLAY is set, and otherwise
377 * tries Wayland then X11. SIGIL_DESKTOP_PLATFORM=wayland|x11 forces one, for
378 * tests that run under a private compositor or X server and for users
379 * whose session variables lie.
380 */
381static void apply_platform_hint(void)
383 const char *want = getenv("SIGIL_DESKTOP_PLATFORM");
384 if (!want || !*want) return;
385 if (strcmp(want, "wayland") == 0) {
386 glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND);
387 } else if (strcmp(want, "x11") == 0) {
388 glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11);
389 } else if (strcmp(want, "win32") == 0) {
390 glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WIN32);
391 } else if (strcmp(want, "cocoa") == 0) {
392 glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_COCOA);
393 } else {
394 fprintf(stderr, "sigil-desktop: SIGIL_DESKTOP_PLATFORM=%s is not one of wayland, x11, win32, cocoa; ignored\n", want);
395 }
398static const char *platform_name(int platform)
400 switch (platform) {
401 case GLFW_PLATFORM_WAYLAND: return "wayland";
402 case GLFW_PLATFORM_X11: return "x11";
403 case GLFW_PLATFORM_WIN32: return "win32";
404 case GLFW_PLATFORM_COCOA: return "cocoa";
405 case GLFW_PLATFORM_NULL: return "null";
406 default: return "unknown";
407 }
410/* ============================================================
411 * PUBLIC C SURFACE (include/sigil-desktop.h)
412 * ============================================================ */
414void sigil_desktop_framebuffer_size(int *width, int *height)
416 if (width) *width = sigil_desktop_framebuffer_width();
417 if (height) *height = sigil_desktop_framebuffer_height();
420void *sigil_desktop_gl_get_proc_address(const char *name)
422 if (!g_desktop || !g_desktop->window || !name) return NULL;
423 return (void *)glfwGetProcAddress(name);
426const char *sigil_desktop_platform(void)
428 if (!g_desktop || !g_desktop->window) return "none";
429 return platform_name(glfwGetPlatform());
432/* ============================================================
433 * MAIN LOOP
434 * ============================================================ */
436/* Returns false when no window could be opened; the GLFW error callback
437 * has already said why on stderr. */
438static bool run_main_loop(const char *title, int width, int height)
440 glfwSetErrorCallback(glfw_error_callback);
441 apply_platform_hint();
443 if (!glfwInit()) {
444 fprintf(stderr, "sigil-desktop: glfwInit failed; no usable display platform\n");
445 return false;
446 }
448 /* The GL core profile sokol_app asked for: 4.3 on Linux and Windows, 4.1
449 * on macOS (the newest it offers); sokol_gfx gates a few features on the
450 * reported version, and Mesa and NVIDIA hand back the highest core
451 * version either way. Forward-compat as sokol_app set it too (required
452 * on macOS). Depth 24 / stencil 8 are GLFW's defaults and match the
453 * sokol_app swapchain the graphics layer expects. */
454 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
455#if defined(__APPLE__)
456 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
457#else
458 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
459#endif
460 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
461 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
462 glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_TRUE);
463 /* Wayland app_id and X11 class both default to the title, so a
464 * compositor rule (sway: for_window [app_id="Crash The Stack"]) can
465 * name the window on either platform. GLFW's X11 backend already
466 * falls back to the title; Wayland's default is empty. */
467 glfwWindowHintString(GLFW_WAYLAND_APP_ID, title);
469 GLFWwindow *window = glfwCreateWindow(width, height, title, NULL, NULL);
470 if (!window) {
471 fprintf(stderr, "sigil-desktop: window creation failed (platform %s)\n",
472 platform_name(glfwGetPlatform()));
473 glfwTerminate();
474 return false;
475 }
476 g_desktop->window = window;
478 glfwMakeContextCurrent(window);
479 glfwSwapInterval(1);
481 glfwGetFramebufferSize(window, &g_desktop->fb_width, &g_desktop->fb_height);
482 glfwGetWindowSize(window, &g_desktop->win_width, &g_desktop->win_height);
483 glfwGetCursorPos(window, &g_desktop->cursor_x, &g_desktop->cursor_y);
485 if (getenv("SIGIL_DESKTOP_VERBOSE")) {
486 float sx = 1.0f, sy = 1.0f;
487 glfwGetWindowContentScale(window, &sx, &sy);
488 fprintf(stderr, "sigil-desktop: platform=%s window=%dx%d framebuffer=%dx%d content-scale=%.2fx%.2f gl=%d.%d\n",
489 platform_name(glfwGetPlatform()),
490 g_desktop->win_width, g_desktop->win_height,
491 g_desktop->fb_width, g_desktop->fb_height, sx, sy,
492 glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR),
493 glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR));
494#if defined(__linux__)
495 fprintf(stderr, "sigil-desktop: dlopen fallback dirs: %s\n", sigil_desktop_dlopen_search_summary());
496#endif
497 }
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);
507 glfwSetWindowSizeCallback(window, window_size_callback);
509 /* init */
510 g_desktop->last_time = glfwGetTime();
511 if (!sigil_is_false(g_desktop->init_callback)) {
512 sigil_apply0(g_desktop->vm, g_desktop->init_callback);
513 check_vm_error("init");
514 }
516 /* frames; the clock restarts after init so a long asset load is not
517 * frame 1's dt (sokol_app handed the load time to the first frame). */
518 g_desktop->last_time = glfwGetTime();
519 while (!glfwWindowShouldClose(window)) {
520 glfwPollEvents();
522 double now = glfwGetTime();
523 g_desktop->frame_time = now - g_desktop->last_time;
524 g_desktop->time_elapsed += g_desktop->frame_time;
525 g_desktop->last_time = now;
527 if (!sigil_is_false(g_desktop->frame_callback)) {
528 Value dt = sigil_flonum(g_desktop->frame_time);
529 sigil_apply1(g_desktop->vm, g_desktop->frame_callback, dt);
530 check_vm_error("frame");
531 }
533 /* The frame is drawn; present it. */
534 glfwSwapBuffers(window);
536 /* Clear per-frame input state for next frame */
537 sigil_desktop_clear_frame_input();
538 }
540 /* cleanup */
541 if (!sigil_is_false(g_desktop->cleanup_callback)) {
542 sigil_apply0(g_desktop->vm, g_desktop->cleanup_callback);
543 check_vm_error("cleanup");
544 }
546 g_desktop->window = NULL;
547 glfwDestroyWindow(window);
548 glfwTerminate();
549 return true;
552/* ============================================================
553 * NATIVE FUNCTIONS
554 * ============================================================ */
556/*
557 * (app-run init-proc frame-proc cleanup-proc [title] [width] [height])
558 *
559 * Run the application main loop.
560 * init-proc: called once at startup
561 * frame-proc: called each frame with delta-time argument
562 * cleanup-proc: called before shutdown
563 */
564static Value native_app_run(SigilVM *vm, int argc, Value *args)
566 if (argc < 3) {
567 sigil__vm_error(vm, SIGIL_ERR_ARITY, "app-run: requires init, frame, and cleanup procedures");
568 return SIGIL_UNDEFINED;
569 }
571 /* Initialize app state */
572 sigil_desktop_state_init(vm);
573 if (g_desktop->window) {
574 sigil__vm_error(vm, SIGIL_ERR_RUNTIME, "app-run: already running; app-run does not nest");
575 return SIGIL_UNDEFINED;
576 }
578 /* Store callbacks */
579 g_desktop->init_callback = args[0];
580 g_desktop->frame_callback = args[1];
581 g_desktop->cleanup_callback = args[2];
583 /* Parse optional arguments */
584 const char *title = "Sigil App";
585 int width = 800;
586 int height = 600;
588 if (argc > 3 && sigil_is_string(args[3])) {
589 SigilString *s = (SigilString *)sigil_as_ptr(args[3]);
590 title = s->data;
591 }
592 if (argc > 4 && sigil_is_fixnum(args[4])) {
593 width = (int)sigil_as_fixnum(args[4]);
594 }
595 if (argc > 5 && sigil_is_fixnum(args[5])) {
596 height = (int)sigil_as_fixnum(args[5]);
597 }
598 /* 0 (or less) means the default, as with sokol_app. */
599 if (width <= 0) width = 640;
600 if (height <= 0) height = 480;
602 bool ran = run_main_loop(title, width, height);
604 /* Cleanup */
605 sigil_desktop_state_shutdown();
607 if (!ran) {
608 sigil__vm_error(vm, SIGIL_ERR_RUNTIME,
609 "app-run: could not open a window (see the sigil-desktop: lines above)");
610 return SIGIL_UNDEFINED;
611 }
612 return SIGIL_NIL;
615/*
616 * (frame-width) -> integer, framebuffer pixels
617 */
618static Value native_frame_width(SigilVM *vm, int argc, Value *args)
620 (void)vm; (void)argc; (void)args;
621 return sigil_fixnum(sigil_desktop_framebuffer_width());
624/*
625 * (frame-height) -> integer, framebuffer pixels
626 */
627static Value native_frame_height(SigilVM *vm, int argc, Value *args)
629 (void)vm; (void)argc; (void)args;
630 return sigil_fixnum(sigil_desktop_framebuffer_height());
633/*
634 * (frame-time) -> float (seconds since last frame)
635 */
636static Value native_frame_time(SigilVM *vm, int argc, Value *args)
638 (void)vm; (void)argc; (void)args;
639 if (!g_desktop) return sigil_flonum(0.0);
640 return sigil_flonum(g_desktop->frame_time);
643/*
644 * (time-elapsed) -> float (seconds since app start)
645 */
646static Value native_time_elapsed(SigilVM *vm, int argc, Value *args)
648 (void)vm; (void)argc; (void)args;
649 if (!g_desktop) return sigil_flonum(0.0);
650 return sigil_flonum(g_desktop->time_elapsed);
653/*
654 * (mouse-x) -> float, framebuffer pixels
655 */
656static Value native_mouse_x(SigilVM *vm, int argc, Value *args)
658 (void)vm; (void)argc; (void)args;
659 return sigil_flonum(cursor_to_fb_x());
662/*
663 * (mouse-wheel) -> float: the wheel's vertical travel since the last frame
664 * in GLFW's units (a notch is 1.0 on most mice; + is away from the user),
665 * summed over the frame and cleared with the other per-frame input.
666 * (mouse-wheel-x) the horizontal travel the same way.
667 */
668static Value native_mouse_wheel(SigilVM *vm, int argc, Value *args)
670 (void)vm; (void)argc; (void)args;
671 return sigil_flonum(g_desktop ? g_desktop->wheel_y : 0.0);
674static Value native_mouse_wheel_x(SigilVM *vm, int argc, Value *args)
676 (void)vm; (void)argc; (void)args;
677 return sigil_flonum(g_desktop ? g_desktop->wheel_x : 0.0);
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 */
686static Value native_mouse_dx(SigilVM *vm, int argc, Value *args)
688 (void)vm; (void)argc; (void)args;
689 return sigil_flonum(g_desktop ? g_desktop->motion_x : 0.0);
692static Value native_mouse_dy(SigilVM *vm, int argc, Value *args)
694 (void)vm; (void)argc; (void)args;
695 return sigil_flonum(g_desktop ? g_desktop->motion_y : 0.0);
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 */
709static Value native_set_cursor_captured(SigilVM *vm, int argc, Value *args)
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;
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!) */
721static Value native_cursor_captured_p(SigilVM *vm, int argc, Value *args)
723 (void)vm; (void)argc; (void)args;
724 return captured_now() ? SIGIL_TRUE : SIGIL_FALSE;
727/*
728 * (mouse-y) -> float, framebuffer pixels
729 */
730static Value native_mouse_y(SigilVM *vm, int argc, Value *args)
732 (void)vm; (void)argc; (void)args;
733 return sigil_flonum(cursor_to_fb_y());
736/*
737 * (mouse-down? button) -> boolean
738 */
739static Value native_mouse_down(SigilVM *vm, int argc, Value *args)
741 if (argc < 1) return SIGIL_FALSE;
742 int btn = mouse_button_from_symbol(vm, args[0]);
743 if (btn < 0 || !g_desktop) return SIGIL_FALSE;
744 return g_desktop->mouse_buttons[btn] ? SIGIL_TRUE : SIGIL_FALSE;
747/*
748 * (mouse-pressed? button) -> boolean
749 */
750static Value native_mouse_pressed(SigilVM *vm, int argc, Value *args)
752 if (argc < 1) return SIGIL_FALSE;
753 int btn = mouse_button_from_symbol(vm, args[0]);
754 if (btn < 0 || !g_desktop) return SIGIL_FALSE;
755 return g_desktop->mouse_pressed[btn] ? SIGIL_TRUE : SIGIL_FALSE;
758/*
759 * (mouse-released? button) -> boolean
760 */
761static Value native_mouse_released(SigilVM *vm, int argc, Value *args)
763 if (argc < 1) return SIGIL_FALSE;
764 int btn = mouse_button_from_symbol(vm, args[0]);
765 if (btn < 0 || !g_desktop) return SIGIL_FALSE;
766 return g_desktop->mouse_released[btn] ? SIGIL_TRUE : SIGIL_FALSE;
769/*
770 * (key-down? key) -> boolean
771 */
772static Value native_key_down(SigilVM *vm, int argc, Value *args)
774 if (argc < 1) return SIGIL_FALSE;
775 int key = sigil_desktop_key_code_from_symbol(vm, args[0]);
776 if (key == GLFW_KEY_UNKNOWN || !g_desktop) return SIGIL_FALSE;
777 return g_desktop->keys_down[key] ? SIGIL_TRUE : SIGIL_FALSE;
780/*
781 * (key-pressed? key) -> boolean
782 */
783static Value native_key_pressed(SigilVM *vm, int argc, Value *args)
785 if (argc < 1) return SIGIL_FALSE;
786 int key = sigil_desktop_key_code_from_symbol(vm, args[0]);
787 if (key == GLFW_KEY_UNKNOWN || !g_desktop) return SIGIL_FALSE;
788 return g_desktop->keys_pressed[key] ? SIGIL_TRUE : SIGIL_FALSE;
791/*
792 * (key-released? key) -> boolean
793 */
794static Value native_key_released(SigilVM *vm, int argc, Value *args)
796 if (argc < 1) return SIGIL_FALSE;
797 int key = sigil_desktop_key_code_from_symbol(vm, args[0]);
798 if (key == GLFW_KEY_UNKNOWN || !g_desktop) return SIGIL_FALSE;
799 return g_desktop->keys_released[key] ? SIGIL_TRUE : SIGIL_FALSE;
802/*
803 * (key-code key) -> integer | #f
804 *
805 * The GLFW key code a key symbol names, or #f when the symbol names no
806 * key. Needs no window: the one way to test the symbol table, and how a
807 * program can ask whether a binding will ever fire before it is used.
808 */
809static Value native_key_code(SigilVM *vm, int argc, Value *args)
811 if (argc < 1) return SIGIL_FALSE;
812 int key = sigil_desktop_key_code_from_symbol(vm, args[0]);
813 if (key == GLFW_KEY_UNKNOWN) return SIGIL_FALSE;
814 return sigil_fixnum(key);
817/*
818 * (quit-requested?) -> boolean
819 */
820static Value native_quit_requested(SigilVM *vm, int argc, Value *args)
822 (void)vm; (void)argc; (void)args;
823 if (!g_desktop) return SIGIL_FALSE;
824 return g_desktop->quit_requested ? SIGIL_TRUE : SIGIL_FALSE;
827/*
828 * (request-quit)
829 *
830 * The loop ends after the current frame; (quit-requested?) answers #t
831 * from now on, as after sokol_app's sapp_request_quit.
832 */
833static Value native_request_quit(SigilVM *vm, int argc, Value *args)
835 (void)vm; (void)argc; (void)args;
836 if (!g_desktop) return SIGIL_NIL;
837 g_desktop->quit_requested = true;
838 if (g_desktop->window) glfwSetWindowShouldClose(g_desktop->window, GLFW_TRUE);
839 return SIGIL_NIL;
842/* ============================================================
843 * THE WINDOW: SIZE, FULLSCREEN, THE MONITOR
844 * ============================================================ */
846/* The monitor the window covers most of (a windowed window has no monitor
847 * of its own in GLFW); the primary monitor when it covers none or where
848 * the platform cannot say where a window is (Wayland reports no window
849 * position). NULL without monitors. */
850static GLFWmonitor *window_monitor(GLFWwindow *window)
852 GLFWmonitor *own = glfwGetWindowMonitor(window);
853 if (own) return own;
854 int count = 0;
855 GLFWmonitor **monitors = glfwGetMonitors(&count);
856 if (!monitors || count == 0) return NULL;
857 int wx = 0, wy = 0, ww = 0, wh = 0;
858 if (glfwGetPlatform() != GLFW_PLATFORM_WAYLAND) glfwGetWindowPos(window, &wx, &wy);
859 glfwGetWindowSize(window, &ww, &wh);
860 GLFWmonitor *best = glfwGetPrimaryMonitor();
861 long best_area = 0;
862 for (int i = 0; i < count; i++) {
863 int mx = 0, my = 0;
864 glfwGetMonitorPos(monitors[i], &mx, &my);
865 const GLFWvidmode *mode = glfwGetVideoMode(monitors[i]);
866 if (!mode) continue;
867 int x0 = wx > mx ? wx : mx;
868 int y0 = wy > my ? wy : my;
869 int x1 = (wx + ww) < (mx + mode->width) ? (wx + ww) : (mx + mode->width);
870 int y1 = (wy + wh) < (my + mode->height) ? (wy + wh) : (my + mode->height);
871 long area = (x1 > x0 && y1 > y0) ? (long)(x1 - x0) * (long)(y1 - y0) : 0;
872 if (area > best_area) { best_area = area; best = monitors[i]; }
873 }
874 return best ? best : monitors[0];
877/*
878 * (window-set-size! width height) -> #t, or #f without a window (or for a
879 * size that is not two positive integers)
880 *
881 * Asks for a window of width x height screen units (on a scaled Wayland
882 * output the framebuffer is that times the scale). While fullscreen it
883 * sets the size the window comes back to. A tiling window manager may not
884 * grant it: read (frame-width) and (frame-height) after, never assume.
885 */
886static Value native_window_set_size(SigilVM *vm, int argc, Value *args)
888 (void)vm; (void)argc;
889 if (!g_desktop || !g_desktop->window) return SIGIL_FALSE;
890 if (!sigil_is_fixnum(args[0]) || !sigil_is_fixnum(args[1])) return SIGIL_FALSE;
891 int w = (int)sigil_as_fixnum(args[0]);
892 int h = (int)sigil_as_fixnum(args[1]);
893 if (w < 1 || h < 1) return SIGIL_FALSE;
894 if (glfwGetWindowMonitor(g_desktop->window)) {
895 g_desktop->windowed_w = w;
896 g_desktop->windowed_h = h;
897 return SIGIL_TRUE;
898 }
899 glfwSetWindowSize(g_desktop->window, w, h);
900 return SIGIL_TRUE;
903/*
904 * (fullscreen?) -> #t while the window is fullscreen, else #f (and #f
905 * without a window)
906 */
907static Value native_fullscreen_p(SigilVM *vm, int argc, Value *args)
909 (void)vm; (void)argc; (void)args;
910 if (!g_desktop || !g_desktop->window) return SIGIL_FALSE;
911 return glfwGetWindowMonitor(g_desktop->window) ? SIGIL_TRUE : SIGIL_FALSE;
914/*
915 * (set-fullscreen! on?) -> #t once the window is as asked, #f without a
916 * window or a monitor
917 *
918 * On: the window takes the whole of the monitor it covers most of, at
919 * that monitor's own video mode (no mode change: the desktop's resolution
920 * and refresh rate), and its windowed position and size are kept. Off: it
921 * goes back to them. Asking for what already is does nothing.
922 */
923static Value native_set_fullscreen(SigilVM *vm, int argc, Value *args)
925 (void)vm; (void)argc;
926 if (!g_desktop || !g_desktop->window) return SIGIL_FALSE;
927 GLFWwindow *window = g_desktop->window;
928 bool on = !sigil_is_false(args[0]);
929 bool is = glfwGetWindowMonitor(window) != NULL;
930 if (on == is) return SIGIL_TRUE;
931 if (on) {
932 GLFWmonitor *monitor = window_monitor(window);
933 const GLFWvidmode *mode = monitor ? glfwGetVideoMode(monitor) : NULL;
934 if (!mode) return SIGIL_FALSE;
935 /* Wayland does not say where a window is (GLFW reports it as an error); the
936 * compositor places the window when it comes back */
937 if (glfwGetPlatform() != GLFW_PLATFORM_WAYLAND)
938 glfwGetWindowPos(window, &g_desktop->windowed_x, &g_desktop->windowed_y);
939 glfwGetWindowSize(window, &g_desktop->windowed_w, &g_desktop->windowed_h);
940 g_desktop->have_windowed = true;
941 glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
942 } else {
943 int w = g_desktop->have_windowed ? g_desktop->windowed_w : 800;
944 int h = g_desktop->have_windowed ? g_desktop->windowed_h : 600;
945 int x = g_desktop->have_windowed ? g_desktop->windowed_x : 64;
946 int y = g_desktop->have_windowed ? g_desktop->windowed_y : 64;
947 glfwSetWindowMonitor(window, NULL, x, y, w, h, 0);
948 }
949 return SIGIL_TRUE;
952/*
953 * (monitor-size) -> (width height), the video mode of the monitor the
954 * window covers most of (on Wayland, which reports no window position,
955 * the primary monitor), or #f without a window or a monitor. A proper
956 * list: its tail is SIGIL_EMPTY, the empty list (SIGIL_NIL is not one).
957 */
958static Value native_monitor_size(SigilVM *vm, int argc, Value *args)
960 (void)argc; (void)args;
961 if (!g_desktop || !g_desktop->window) return SIGIL_FALSE;
962 GLFWmonitor *monitor = window_monitor(g_desktop->window);
963 const GLFWvidmode *mode = monitor ? glfwGetVideoMode(monitor) : NULL;
964 if (!mode) return SIGIL_FALSE;
965 return sigil_cons(vm, sigil_fixnum(mode->width),
966 sigil_cons(vm, sigil_fixnum(mode->height), SIGIL_EMPTY));
969/*
970 * (window-width) (window-height) -> the window's size in SCREEN UNITS, the
971 * units window-set-size! takes, or #f without a window
972 *
973 * (frame-width) and (frame-height) are framebuffer pixels; on a scaled
974 * output (a HiDPI Wayland, a Retina Mac) the framebuffer is the scale times
975 * the window, so frame-width / window-width is the device pixel ratio.
976 * Read from GLFW at the call, not from the size callback, so a size asked
977 * for with window-set-size! shows as soon as the platform has granted it.
978 */
979static Value native_window_width(SigilVM *vm, int argc, Value *args)
981 (void)vm; (void)argc; (void)args;
982 if (!g_desktop || !g_desktop->window) return SIGIL_FALSE;
983 int w = 0, h = 0;
984 glfwGetWindowSize(g_desktop->window, &w, &h);
985 return sigil_fixnum(w);
988static Value native_window_height(SigilVM *vm, int argc, Value *args)
990 (void)vm; (void)argc; (void)args;
991 if (!g_desktop || !g_desktop->window) return SIGIL_FALSE;
992 int w = 0, h = 0;
993 glfwGetWindowSize(g_desktop->window, &w, &h);
994 return sigil_fixnum(h);
997/*
998 * (window-focused?) -> #t while the window has the input focus, #f when
999 * another window has it, and #f without a window
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.
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 */
1010static Value native_window_focused_p(SigilVM *vm, int argc, Value *args)
1012 (void)vm; (void)argc; (void)args;
1013 if (!g_desktop || !g_desktop->window) return SIGIL_FALSE;
1014 return glfwGetWindowAttrib(g_desktop->window, GLFW_FOCUSED) ? SIGIL_TRUE : SIGIL_FALSE;
1017/* ============================================================
1018 * MODULE INITIALIZATION
1019 * ============================================================ */
1021void sigil__init_sigil_desktop_module(SigilVM *vm)
1023 SigilModule *module = sigil_begin_module(vm, "(sigil desktop)");
1024 if (!module) return;
1026 /* Application lifecycle */
1027 sigil_module_register_native(vm, "app-run", native_app_run,
1028 SIGIL_ARITY_RANGE(3, 6),
1029 "Run application with init/frame/cleanup callbacks");
1031 /* Frame info */
1032 sigil_module_register_native(vm, "frame-width", native_frame_width,
1033 SIGIL_ARITY_EXACT(0), "Get frame buffer width");
1034 sigil_module_register_native(vm, "frame-height", native_frame_height,
1035 SIGIL_ARITY_EXACT(0), "Get frame buffer height");
1036 sigil_module_register_native(vm, "frame-time", native_frame_time,
1037 SIGIL_ARITY_EXACT(0), "Seconds since last frame");
1038 sigil_module_register_native(vm, "time-elapsed", native_time_elapsed,
1039 SIGIL_ARITY_EXACT(0), "Seconds since app start");
1041 /* Mouse input */
1042 sigil_module_register_native(vm, "mouse-x", native_mouse_x,
1043 SIGIL_ARITY_EXACT(0), "Mouse X position");
1044 sigil_module_register_native(vm, "mouse-y", native_mouse_y,
1045 SIGIL_ARITY_EXACT(0), "Mouse Y position");
1046 sigil_module_register_native(vm, "mouse-wheel", native_mouse_wheel,
1047 SIGIL_ARITY_EXACT(0), "Wheel travel since the last frame (vertical)");
1048 sigil_module_register_native(vm, "mouse-wheel-x", native_mouse_wheel_x,
1049 SIGIL_ARITY_EXACT(0), "Wheel travel since the last frame (horizontal)");
1050 sigil_module_register_native(vm, "mouse-dx", native_mouse_dx,
1051 SIGIL_ARITY_EXACT(0), "Mouse motion since the last frame (x)");
1052 sigil_module_register_native(vm, "mouse-dy", native_mouse_dy,
1053 SIGIL_ARITY_EXACT(0), "Mouse motion since the last frame (y)");
1054 sigil_module_register_native(vm, "set-cursor-captured!", native_set_cursor_captured,
1055 SIGIL_ARITY_RANGE(1, 2), "Capture the cursor for mouse look, or release it");
1056 sigil_module_register_native(vm, "cursor-captured?", native_cursor_captured_p,
1057 SIGIL_ARITY_EXACT(0), "Is the cursor captured");
1058 sigil_module_register_native(vm, "mouse-down?", native_mouse_down,
1059 SIGIL_ARITY_EXACT(1), "Is mouse button held?");
1060 sigil_module_register_native(vm, "mouse-pressed?", native_mouse_pressed,
1061 SIGIL_ARITY_EXACT(1), "Was mouse button just pressed?");
1062 sigil_module_register_native(vm, "mouse-released?", native_mouse_released,
1063 SIGIL_ARITY_EXACT(1), "Was mouse button just released?");
1065 /* Keyboard input */
1066 sigil_module_register_native(vm, "key-down?", native_key_down,
1067 SIGIL_ARITY_EXACT(1), "Is key held?");
1068 sigil_module_register_native(vm, "key-pressed?", native_key_pressed,
1069 SIGIL_ARITY_EXACT(1), "Was key just pressed?");
1070 sigil_module_register_native(vm, "key-released?", native_key_released,
1071 SIGIL_ARITY_EXACT(1), "Was key just released?");
1072 sigil_module_register_native(vm, "key-code", native_key_code,
1073 SIGIL_ARITY_EXACT(1), "GLFW key code of a key symbol, or #f");
1075 /* Quit handling */
1076 sigil_module_register_native(vm, "quit-requested?", native_quit_requested,
1077 SIGIL_ARITY_EXACT(0), "Was quit requested?");
1078 sigil_module_register_native(vm, "request-quit", native_request_quit,
1079 SIGIL_ARITY_EXACT(0), "Request application quit");
1081 /* The window: size, fullscreen, the monitor (0.11.0) */
1082 sigil_module_register_native(vm, "window-set-size!", native_window_set_size,
1083 SIGIL_ARITY_EXACT(2), "Ask for a window of width x height screen units");
1084 sigil_module_register_native(vm, "fullscreen?", native_fullscreen_p,
1085 SIGIL_ARITY_EXACT(0), "Is the window fullscreen");
1086 sigil_module_register_native(vm, "set-fullscreen!", native_set_fullscreen,
1087 SIGIL_ARITY_EXACT(1), "Take the whole monitor, or go back to the window");
1088 sigil_module_register_native(vm, "monitor-size", native_monitor_size,
1089 SIGIL_ARITY_EXACT(0), "The monitor the window is on: (width height)");
1090 sigil_module_register_native(vm, "window-width", native_window_width,
1091 SIGIL_ARITY_EXACT(0), "The window's width in screen units");
1092 sigil_module_register_native(vm, "window-height", native_window_height,
1093 SIGIL_ARITY_EXACT(0), "The window's height in screen units");
1094 sigil_module_register_native(vm, "window-focused?", native_window_focused_p,
1095 SIGIL_ARITY_EXACT(0), "Does the window have the input focus");
1097 /* Export all */
1098 sigil_module_export(vm, "app-run");
1099 sigil_module_export(vm, "frame-width");
1100 sigil_module_export(vm, "frame-height");
1101 sigil_module_export(vm, "frame-time");
1102 sigil_module_export(vm, "time-elapsed");
1103 sigil_module_export(vm, "mouse-x");
1104 sigil_module_export(vm, "mouse-y");
1105 sigil_module_export(vm, "mouse-dx");
1106 sigil_module_export(vm, "mouse-dy");
1107 sigil_module_export(vm, "set-cursor-captured!");
1108 sigil_module_export(vm, "cursor-captured?");
1109 sigil_module_export(vm, "mouse-down?");
1110 sigil_module_export(vm, "mouse-pressed?");
1111 sigil_module_export(vm, "mouse-released?");
1112 sigil_module_export(vm, "key-down?");
1113 sigil_module_export(vm, "key-pressed?");
1114 sigil_module_export(vm, "key-released?");
1115 sigil_module_export(vm, "key-code");
1116 sigil_module_export(vm, "quit-requested?");
1117 sigil_module_export(vm, "request-quit");
1118 sigil_module_export(vm, "window-set-size!");
1119 sigil_module_export(vm, "fullscreen?");
1120 sigil_module_export(vm, "set-fullscreen!");
1121 sigil_module_export(vm, "monitor-size");
1122 sigil_module_export(vm, "window-width");
1123 sigil_module_export(vm, "window-height");
1124 sigil_module_export(vm, "window-focused?");
1126 sigil_end_module(vm);