commit ab6918a
uint
·
2026-06-04 11:21:11 +0000 UTC
parent 45b4500
Add maus_event_wait as a cheaper alternative to polling
3 files changed,
+32,
-7
M
maus.h
+5,
-0
1@@ -97,5 +97,10 @@ void maus_log(FILE* fd, const char* fmt, ...);
2 note: can burn cpu cycles */
3 bool maus_event_poll(Maus* mw, MausEvent* ev);
4
5+/* poll for events then fill `ev` with retrieved events.
6+ returns true if event polled, else false
7+ note: thread goes to sleep until an event arrives */
8+void maus_event_wait(Maus* mw, MausEvent* ev);
9+
10 #endif /* MAUSWIN_H */
11
+18,
-6
1@@ -138,14 +138,26 @@ Maus* maus_init(const char* title, int x, int y, int width, int height)
2
3 bool maus_event_poll(Maus* mw, MausEvent* ev)
4 {
5- if (!XPending(mw->display))
6- return false;
7+ XEvent xev;
8+ while (XPending(mw->display)) {
9+ XNextEvent(mw->display, &xev);
10+ ev->type = MAUS_EV_NONE;
11+ if (handle_event(&xev, ev, mw))
12+ return true;
13
14- ev->type = MAUS_EV_NONE;
15+ }
16
17- XEvent xev;
18- XNextEvent(mw->display, &xev);
19+ return false;
20+}
21
22- return handle_event(&xev, ev, mw);
23+void maus_event_wait(Maus* mw, MausEvent* ev)
24+{
25+ XEvent xev;
26+ for (;;) {
27+ XNextEvent(mw->display, &xev);
28+ ev->type = MAUS_EV_NONE;
29+ if (handle_event(&xev, ev, mw))
30+ return;
31+ }
32 }
33
+9,
-1
1@@ -30,9 +30,17 @@ int main(void)
2 maus_create_window(mw);
3
4 MausEvent ev;
5+ /* waiting -- used in applications
6+ for (;;) {
7+ maus_event_wait(mw, &ev);
8+ handle_ev(&ev, mw);
9+ }
10+ */
11+
12+ /* constant polling -- used in games */
13 for (;;) {
14 while (maus_event_poll(mw, &ev))
15- handle_ev(&ev, mw);
16+ (void) handle_ev(&ev, mw);
17 }
18
19 maus_close(mw);