diff options
author | Anton Kling <anton@kling.gg> | 2023-10-23 20:10:01 +0200 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2023-10-23 20:10:01 +0200 |
commit | 3f44d16f5297a0f7d63f98e48eba46c1e2538a3e (patch) | |
tree | 91ab1f4b0e3eca45d3f6bc125379e3c1ae290a1f /userland/libgui | |
parent | 6382172ac01ef17913c54177e556706d6ae2ec7f (diff) |
WindowServer/LibGUI: Add a eventloop
This can be used by any program does nothing until it gets user input.
Diffstat (limited to 'userland/libgui')
-rw-r--r-- | userland/libgui/libgui.c | 27 | ||||
-rw-r--r-- | userland/libgui/libgui.h | 4 |
2 files changed, 29 insertions, 2 deletions
diff --git a/userland/libgui/libgui.c b/userland/libgui/libgui.c index 5f5526d..a344778 100644 --- a/userland/libgui/libgui.c +++ b/userland/libgui/libgui.c @@ -2,6 +2,7 @@ #include "font.h" #include <assert.h> #include <fcntl.h> +#include <poll.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -155,7 +156,6 @@ char *random_string(void) { } close(fd); r[9] = '\0'; - printf("r: %s\n", r); return r; } @@ -230,7 +230,30 @@ typedef struct { void GUI_ClearScreen(GUI_Window *w, uint32_t color) { for (int i = 0; i < w->sx * w->sy; i++) - w->bitmap_ptr[i] =color; + w->bitmap_ptr[i] = color; +} + +void GUI_EventLoop(GUI_Window *w, void (*event_handler)(WS_EVENT ev)) { + struct pollfd fds[1]; + fds[0].fd = w->ws_socket; + fds[0].events = POLLIN; + fds[0].revents = 0; + for (;; fds[0].revents = 0) { + poll(fds, 1, 0); + if (!(fds[0].revents & POLLIN)) + continue; + + int rc; + WS_EVENT e; + if (0 >= (rc = read(w->ws_socket, &e, sizeof(e)))) + continue; + if (event_handler) { + event_handler(e); + continue; + } + if (WINDOWSERVER_EVENT_WINDOW_EXIT == e.type) + exit(0); + } } GUI_Window *GUI_CreateWindow(uint32_t x, uint32_t y, uint32_t sx, uint32_t sy) { diff --git a/userland/libgui/libgui.h b/userland/libgui/libgui.h index d58c23c..07fd3db 100644 --- a/userland/libgui/libgui.h +++ b/userland/libgui/libgui.h @@ -3,6 +3,9 @@ #include <stddef.h> #include <stdint.h> +#define WINDOWSERVER_EVENT_KEYPRESS 0 +#define WINDOWSERVER_EVENT_WINDOW_EXIT 1 + typedef struct { int ws_socket; int bitmap_fd; @@ -31,4 +34,5 @@ void GUI_UpdateWindow(GUI_Window *w); void GUI_OverwriteFont(GUI_Window *w, uint32_t px, uint32_t py, const uint32_t color); void GUI_ClearScreen(GUI_Window *w, uint32_t color); +void GUI_EventLoop(GUI_Window *w, void (*event_handler)(WS_EVENT ev)); #endif |