commit 45b4500
uint
·
2026-06-04 11:03:46 +0000 UTC
parent bf34812
Rename maus_poll->maus_event_poll, move polling logic into handle_event
3 files changed,
+60,
-51
M
maus.h
+4,
-2
1@@ -92,8 +92,10 @@ Maus* maus_init(const char* title, int x, int y, int width, int height);
2 /* log message to output `fd` */
3 void maus_log(FILE* fd, const char* fmt, ...);
4
5-/* poll for events then fill `ev` with retrieved events */
6-bool maus_poll(Maus* mw, MausEvent* ev);
7+/* poll for events then fill `ev` with retrieved events.
8+ returns true if event polled, else false.
9+ note: can burn cpu cycles */
10+bool maus_event_poll(Maus* mw, MausEvent* ev);
11
12 #endif /* MAUSWIN_H */
13
+55,
-48
1@@ -6,6 +6,59 @@
2
3 #include "maus.h"
4
5+static bool handle_event(XEvent* xev, MausEvent* ev, Maus* mw);
6+
7+static bool handle_event(XEvent* xev, MausEvent* ev, Maus* mw)
8+{
9+ switch (xev->type) {
10+ case ClientMessage: /* TODO proper quit handling */
11+ ev->type = MAUS_EV_CLOSE;
12+ return true;
13+
14+ case KeyPress:
15+ ev->type = MAUS_EV_KEY;
16+ ev->key.code = xev->xkey.keycode;
17+ ev->key.pressed = true;
18+ return true;
19+
20+ case KeyRelease:
21+ ev->type = MAUS_EV_KEY;
22+ ev->key.code = xev->xkey.keycode;
23+ ev->key.pressed = false;
24+ return true;
25+
26+ case ButtonPress:
27+ ev->type = MAUS_EV_MOUSE_BUTTON;
28+ ev->mouse.button.button = xev->xbutton.button;
29+ ev->mouse.button.pressed = true;
30+ return true;
31+
32+ case ButtonRelease:
33+ ev->type = MAUS_EV_MOUSE_BUTTON;
34+ ev->mouse.button.button = xev->xbutton.button;
35+ ev->mouse.button.pressed = false;
36+ return true;
37+
38+ case MotionNotify:
39+ ev->type = MAUS_EV_MOUSE_MOTION;
40+ ev->mouse.motion.x = xev->xmotion.x;
41+ ev->mouse.motion.y = xev->xmotion.y;
42+ return true;
43+
44+ case ConfigureNotify:
45+ ev->type = MAUS_EV_RESIZE;
46+ ev->resize.width = xev->xconfigure.width;
47+ ev->resize.height = xev->xconfigure.height;
48+
49+ /* TODO put into maus_resize */
50+ mw->width = xev->xconfigure.width;
51+ mw->height = xev->xconfigure.height;
52+ return true;
53+ }
54+
55+ return false;
56+}
57+
58 bool maus_close(Maus* mw)
59 {
60 if (mw->pixels)
61@@ -83,7 +136,7 @@ Maus* maus_init(const char* title, int x, int y, int width, int height)
62 return win;
63 }
64
65-bool maus_poll(Maus* mw, MausEvent* ev)
66+bool maus_event_poll(Maus* mw, MausEvent* ev)
67 {
68 if (!XPending(mw->display))
69 return false;
70@@ -93,52 +146,6 @@ bool maus_poll(Maus* mw, MausEvent* ev)
71 XEvent xev;
72 XNextEvent(mw->display, &xev);
73
74- switch (xev.type) {
75- case ClientMessage: /* TODO proper quit handling */
76- ev->type = MAUS_EV_CLOSE;
77- return true;
78-
79- case KeyPress:
80- ev->type = MAUS_EV_KEY;
81- ev->key.code = xev.xkey.keycode;
82- ev->key.pressed = true;
83- return true;
84-
85- case KeyRelease:
86- ev->type = MAUS_EV_KEY;
87- ev->key.code = xev.xkey.keycode;
88- ev->key.pressed = false;
89- return true;
90-
91- case ButtonPress:
92- ev->type = MAUS_EV_MOUSE_BUTTON;
93- ev->mouse.button.button = xev.xbutton.button;
94- ev->mouse.button.pressed = true;
95- return true;
96-
97- case ButtonRelease:
98- ev->type = MAUS_EV_MOUSE_BUTTON;
99- ev->mouse.button.button = xev.xbutton.button;
100- ev->mouse.button.pressed = false;
101- return true;
102-
103- case MotionNotify:
104- ev->type = MAUS_EV_MOUSE_MOTION;
105- ev->mouse.motion.x = xev.xmotion.x;
106- ev->mouse.motion.y = xev.xmotion.y;
107- return true;
108-
109- case ConfigureNotify:
110- ev->type = MAUS_EV_RESIZE;
111- ev->resize.width = xev.xconfigure.width;
112- ev->resize.height = xev.xconfigure.height;
113-
114- /* TODO put into maus_resize */
115- mw->width = xev.xconfigure.width;
116- mw->height = xev.xconfigure.height;
117- return true;
118- }
119-
120- return false;
121+ return handle_event(&xev, ev, mw);
122 }
123
+1,
-1
1@@ -31,7 +31,7 @@ int main(void)
2
3 MausEvent ev;
4 for (;;) {
5- while (maus_poll(mw, &ev))
6+ while (maus_event_poll(mw, &ev))
7 handle_ev(&ev, mw);
8 }
9