implement os wakeup

This commit is contained in:
Nikita Smith
2026-06-02 12:10:03 -07:00
committed by Ryan Fleury
parent 559e730986
commit b4dd1f6fd3
3 changed files with 51 additions and 20 deletions
@@ -68,6 +68,10 @@ wm_init(void)
lnx_wm_state->cursors[map[idx].cursor] = XCreateFontCursor(lnx_wm_state->display, map[idx].id); lnx_wm_state->cursors[map[idx].cursor] = XCreateFontCursor(lnx_wm_state->display, map[idx].id);
} }
} }
// create wakeup event for polling
os_lnx_gfx_state->wakeup_fd = eventfd(0, EFD_CLOEXEC);
Assert(os_lnx_gfx_state->wakeup_fd > 0);
} }
//////////////////////////////// ////////////////////////////////
@@ -398,17 +402,38 @@ wm_dpi_from_monitor(WM_Monitor monitor)
internal void internal void
wm_send_wakeup_event(void) wm_send_wakeup_event(void)
{ {
// TODO(rjf) U64 dummy = 1;
ssize_t size = OS_LNX_RETRY_ON_EINTR(write(os_lnx_gfx_state->wakeup_fd, &dummy, sizeof(dummy)));
Assert(size == sizeof(dummy));
} }
internal WM_EventList internal WM_EventList
wm_get_events(Arena *arena, B32 wait) wm_get_events(Arena *arena, B32 wait)
{ {
WM_EventList evts = {0}; WM_EventList evts = {0};
for(;XPending(lnx_wm_state->display) > 0 || (wait && evts.count == 0);) for(;;)
{
if(XPending(os_lnx_gfx_state->display) == 0)
{
struct pollfd poll_fds[2] =
{
{ .fd = ConnectionNumber(os_lnx_gfx_state->display), .events = POLLIN },
{ .fd = os_lnx_gfx_state->wakeup_fd, .events = POLLIN },
};
int timeout = wait && evts.count == 0 ? -1 : 0;
int poll_status = poll(poll_fds, ArrayCount(poll_fds), timeout);
Assert(poll_status >= 0);
if(poll_fds[1].revents & POLLIN)
{
U64 dummy = 0;
read(os_lnx_gfx_state->wakeup_fd, &dummy, sizeof(dummy));
wait = 0;
}
}
while(XPending(os_lnx_gfx_state->display))
{ {
XEvent evt = {0}; XEvent evt = {0};
XNextEvent(lnx_wm_state->display, &evt); XNextEvent(os_lnx_gfx_state->display, &evt);
B32 set_mouse_cursor = 0; B32 set_mouse_cursor = 0;
switch(evt.type) switch(evt.type)
{ {
@@ -618,6 +643,7 @@ wm_get_events(Arena *arena, B32 wait)
} }
}break; }break;
} }
if(set_mouse_cursor) if(set_mouse_cursor)
{ {
Window root_window = 0; Window root_window = 0;
@@ -627,10 +653,11 @@ wm_get_events(Arena *arena, B32 wait)
int child_rel_x = 0; int child_rel_x = 0;
int child_rel_y = 0; int child_rel_y = 0;
unsigned int mask = 0; unsigned int mask = 0;
if(XQueryPointer(lnx_wm_state->display, XDefaultRootWindow(lnx_wm_state->display), &root_window, &child_window, &root_rel_x, &root_rel_y, &child_rel_x, &child_rel_y, &mask)) if(XQueryPointer(os_lnx_gfx_state->display, XDefaultRootWindow(os_lnx_gfx_state->display), &root_window, &child_window, &root_rel_x, &root_rel_y, &child_rel_x, &child_rel_y, &mask))
{ {
XDefineCursor(lnx_wm_state->display, root_window, lnx_wm_state->cursors[lnx_wm_state->last_set_cursor]); XDefineCursor(os_lnx_gfx_state->display, root_window, os_lnx_gfx_state->cursors[os_lnx_gfx_state->last_set_cursor]);
XFlush(lnx_wm_state->display); XFlush(os_lnx_gfx_state->display);
}
} }
} }
} }
@@ -13,6 +13,8 @@
#include <X11/extensions/sync.h> #include <X11/extensions/sync.h>
#include <X11/keysym.h> #include <X11/keysym.h>
#include <X11/keysymdef.h> #include <X11/keysymdef.h>
#include <poll.h>
#include <sys/eventfd.h>
//////////////////////////////// ////////////////////////////////
//~ rjf: Window State //~ rjf: Window State
@@ -46,6 +48,7 @@ struct LNX_WM_State
Cursor cursors[WM_Cursor_COUNT]; Cursor cursors[WM_Cursor_COUNT];
WM_Cursor last_set_cursor; WM_Cursor last_set_cursor;
WM_SystemInfo gfx_info; WM_SystemInfo gfx_info;
int wakeup_fd;
}; };
//////////////////////////////// ////////////////////////////////
@@ -0,0 +1 @@