commit af23f19
uint
·
2026-06-09 17:48:51 +0000 UTC
parent 8ddef5d
Set cursor state with maus_cur_set_mode
3 files changed,
+72,
-2
M
maus.h
+4,
-1
1@@ -5,7 +5,7 @@
2 #include <stdio.h>
3 #include <stdint.h>
4
5-#include "maus_keys.h"
6+#include "maus_input.h"
7
8 /* auto selection */
9 #if !defined(BACKEND_WIN) && !defined(BACKEND_MAC) && \
10@@ -129,5 +129,8 @@ void maus_present(Maus* mw);
11 on resize success, else false */
12 bool maus_resize(Maus* mw, uint32_t width, uint32_t height);
13
14+/* change behavior of mouse based on state passed */
15+void maus_cur_set_mode(Maus* mw, MausCursorState state);
16+
17 #endif /* MAUSWIN_H */
18
+65,
-0
1@@ -1,3 +1,4 @@
2+#include <X11/X.h>
3 #include <stdbool.h>
4 #include <stdlib.h>
5 #include <sys/shm.h>
6@@ -6,6 +7,7 @@
7 #include <X11/Xlib.h>
8 #include <X11/Xutil.h>
9 #include <X11/keysym.h>
10+#include <X11/cursorfont.h>
11 #include <X11/extensions/XShm.h>
12
13 #include "maus.h"
14@@ -480,3 +482,66 @@ bool maus_resize(Maus* mw, uint32_t width, uint32_t height)
15 return fb_create(mw);
16 }
17
18+void maus_cur_set_mode(Maus* mw, MausCursorState state)
19+{
20+ Display* dpy = mw->display;
21+ Window win = mw->win;
22+
23+ if (!dpy) {
24+ maus_log(stderr, "display is NULL");
25+ return;
26+ }
27+ if (win == None) {
28+ maus_log(stderr, "win is None");
29+ return;
30+ }
31+
32+ /* show cursor */
33+ if (state == MAUS_CURSOR_STATE_VISIBLE) {
34+ XUndefineCursor(dpy, win);
35+ XFlush(dpy);
36+ return;
37+ }
38+
39+ /* hide cursor */
40+ if (state == MAUS_CURSOR_STATE_HIDDEN) {
41+ Pixmap blank_pm = XCreatePixmap(dpy, win, 1, 1, 1);
42+ XColor black = {.red=0, .green=0, .blue=0};
43+ Cursor blank_cur = XCreatePixmapCursor(
44+ dpy, blank_pm, blank_pm, &black, &black, 0, 0
45+ );
46+ XFreePixmap(dpy, blank_pm);
47+
48+ XDefineCursor(dpy, win, blank_cur);
49+ XFreeCursor(dpy, blank_cur);
50+ XFlush(dpy);
51+ return;
52+ }
53+
54+ /* lock cursor */
55+ if (state == MAUS_CURSOR_STATE_LOCKED) {
56+ Mask mask = ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
57+ int grab = XGrabPointer(
58+ dpy, win, True, mask, GrabModeAsync,
59+ GrabModeAsync, win, None, CurrentTime
60+ );
61+
62+ if (grab != GrabSuccess) {
63+ maus_log(stderr, "failed to grab mouse pointer");
64+ return;
65+ }
66+
67+ /* TODO keep position just snap to window dimensions */
68+ XWarpPointer(dpy, None, win, 0, 0, 0, 0, mw->width/2, mw->height/2);
69+ XFlush(dpy);
70+ return;
71+ }
72+
73+ /* unlock cursor */
74+ if (state == MAUS_CURSOR_STATE_FREE) {
75+ XUngrabPointer(dpy, CurrentTime);
76+ XFlush(dpy);
77+ return;
78+ }
79+}
80+
+3,
-1
1@@ -7,7 +7,7 @@
2 #include <X11/Xlib.h>
3 #include <X11/extensions/XShm.h>
4
5-#include "maus_keys.h"
6+#include "maus_input.h"
7
8 #define MAUS_KEYCODE_LAST 256
9
10@@ -38,6 +38,8 @@ typedef struct {
11 bool key_codes[MAUS_KEYCODE_LAST]; /* physical keys */
12 bool key_syms[MAUS_KEY_LAST]; /* logical keys */
13 MausKey keymap[MAUS_KEYCODE_LAST]; /* X11 keycode->MausKey */
14+
15+ MausCursor cursor;
16 } Maus;
17
18 #endif /* MAUS_X11_H */