commit 25b45bc
uint
·
2026-06-03 18:51:30 +0000 UTC
parent f65465f
Add polling events
2 files changed,
+100,
-0
M
maus.h
M
maus.h
+41,
-0
1@@ -36,6 +36,44 @@
2
3 #endif
4
5+typedef enum {
6+ MAUS_EV_NONE,
7+ MAUS_EV_CLOSE,
8+ MAUS_EV_KEY,
9+ MAUS_EV_MOUSE_BUTTON,
10+ MAUS_EV_MOUSE_MOTION,
11+ MAUS_EV_RESIZE,
12+} MausEventType;
13+
14+typedef struct {
15+ MausEventType type;
16+
17+ union {
18+ struct {
19+ uint32_t code;
20+ /* sym */
21+ bool pressed;
22+ } key;
23+
24+ struct {
25+ struct {
26+ uint8_t button;
27+ bool pressed;
28+ } button;
29+
30+ struct {
31+ int32_t x;
32+ int32_t y;
33+ } motion;
34+ } mouse;
35+
36+ struct {
37+ uint32_t width;
38+ uint32_t height;
39+ } resize;
40+ };
41+} MausEvent;
42+
43 /* close a Maus. returns false on fail */
44 bool maus_close(Maus* mw);
45
46@@ -54,5 +92,8 @@ Maus* maus_init(const char* title, int x, int y, int width, int height);
47 /* log message to output `fd` */
48 void maus_log(FILE* fd, const char* fmt, ...);
49
50+/* poll for events then fill `ev` with retrieved events */
51+bool maus_poll(Maus* mw, MausEvent* ev);
52+
53 #endif /* MAUSWIN_H */
54
+59,
-0
1@@ -83,3 +83,62 @@ Maus* maus_init(const char* title, int x, int y, int width, int height)
2 return win;
3 }
4
5+bool maus_poll(Maus* mw, MausEvent* ev)
6+{
7+ if (!XPending(mw->display))
8+ return false;
9+
10+ ev->type = MAUS_EV_NONE;
11+
12+ XEvent xev;
13+ XNextEvent(mw->display, &xev);
14+
15+ switch (xev.type) {
16+ case ClientMessage: /* TODO proper quit handling */
17+ ev->type = MAUS_EV_CLOSE;
18+ return true;
19+
20+ case KeyPress:
21+ ev->type = MAUS_EV_KEY;
22+ ev->key.code = xev.xkey.keycode;
23+ ev->key.pressed = true;
24+ return true;
25+
26+ case KeyRelease:
27+ ev->type = MAUS_EV_KEY;
28+ ev->key.code = xev.xkey.keycode;
29+ ev->key.pressed = false;
30+ return true;
31+
32+ case ButtonPress:
33+ ev->type = MAUS_EV_MOUSE_BUTTON;
34+ ev->mouse.button.button = xev.xbutton.button;
35+ ev->mouse.button.pressed = true;
36+ return true;
37+
38+ case ButtonRelease:
39+ ev->type = MAUS_EV_MOUSE_BUTTON;
40+ ev->mouse.button.button = xev.xbutton.button;
41+ ev->mouse.button.pressed = false;
42+ return true;
43+
44+ case MotionNotify:
45+ ev->type = MAUS_EV_MOUSE_MOTION;
46+ ev->mouse.motion.x = xev.xmotion.x;
47+ ev->mouse.motion.y = xev.xmotion.y;
48+ return true;
49+
50+ case ConfigureNotify:
51+ ev->type = MAUS_EV_RESIZE;
52+ ev->resize.width = xev.xconfigure.width;
53+ ev->resize.height = xev.xconfigure.height;
54+
55+ /* TODO put into maus_resize */
56+ mw->width = xev.xconfigure.width;
57+ mw->height = xev.xconfigure.height;
58+ return true;
59+ }
60+
61+ return false;
62+}
63+