From 5cbff06ff9153f7a7958202a777d98ebeae59393 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 16 Oct 2021 08:54:21 +0200 Subject: Cursor motion --- src/window.c | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/window.c b/src/window.c index 62f7e88..253ea3a 100644 --- a/src/window.c +++ b/src/window.c @@ -29,9 +29,9 @@ static void set_vertical_sync_enabled(Window window, int enabled) { static void mgl_window_on_resize(mgl_window *self, int width, int height) { mgl_context *context = mgl_get_context(); - self->width = width; - self->height = height; - context->gl.glViewport(0, 0, self->width, self->height); + self->size.x = width; + self->size.y = height; + context->gl.glViewport(0, 0, self->size.x, self->size.y); context->gl.glMatrixMode(GL_PROJECTION); context->gl.glLoadIdentity(); context->gl.glOrtho(0.0, width, height, 0.0, -1.0, 1.0); @@ -55,10 +55,16 @@ int mgl_window_create_with_params(mgl_window *self, const char *title, int width XSetWindowAttributes window_attr; window_attr.colormap = color_map; - window_attr.event_mask = KeyPressMask | StructureNotifyMask; + window_attr.event_mask = + KeyPressMask | KeyReleaseMask | + ButtonPressMask | ButtonReleaseMask | + PointerMotionMask | Button1MotionMask | Button2MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask | ButtonMotionMask | + StructureNotifyMask; window_attr.bit_gravity = NorthWestGravity; - self->window = XCreateWindow(context->connection, parent_window, 0, 0, width, height, 0, ((XVisualInfo*)context->visual_info)->depth, InputOutput, ((XVisualInfo*)context->visual_info)->visual, CWColormap | CWEventMask | CWBitGravity, &window_attr); + self->window = XCreateWindow(context->connection, parent_window, 0, 0, width, height, 0, + ((XVisualInfo*)context->visual_info)->depth, InputOutput, ((XVisualInfo*)context->visual_info)->visual, + CWColormap | CWEventMask | CWBitGravity, &window_attr); XFreeColormap(context->connection, color_map); if(!self->window) { fprintf(stderr, "XCreateWindow failed\n"); @@ -83,6 +89,11 @@ int mgl_window_create_with_params(mgl_window *self, const char *title, int width gwa.height = 0; XGetWindowAttributes(context->connection, self->window, &gwa); + Window dummy_w; + int dummy_i; + unsigned int dummy_u; + XQueryPointer(context->connection, self->window, &dummy_w, &dummy_w, &dummy_i, &dummy_i, &self->cursor_position.x, &self->cursor_position.y, &dummy_u); + mgl_window_on_resize(self, gwa.width, gwa.height); mgl_window_draw(self); return 0; @@ -96,11 +107,21 @@ void mgl_window_deinit(mgl_window *self) { } } -static void on_receive_x11_event(mgl_window *window, XEvent *xev) { - if(xev->type == ConfigureNotify) { - if(xev->xconfigure.width != window->width || xev->xconfigure.height != window->height) { - mgl_window_on_resize(window, xev->xconfigure.width, xev->xconfigure.height); - /*fprintf(stderr, "resize!\n");*/ +static void mgl_window_on_receive_event(mgl_window *self, XEvent *xev) { + switch(xev->type) { + case ConfigureNotify: { + while(XCheckTypedWindowEvent(mgl_get_context()->connection, self->window, ConfigureNotify, xev)) {} + if(xev->xconfigure.width != self->size.x || xev->xconfigure.height != self->size.x) { + mgl_window_on_resize(self, xev->xconfigure.width, xev->xconfigure.height); + /*fprintf(stderr, "resize!\n");*/ + } + break; + } + case MotionNotify: { + while(XCheckTypedWindowEvent(mgl_get_context()->connection, self->window, MotionNotify, xev)) {} + self->cursor_position.x = xev->xmotion.x; + self->cursor_position.y = xev->xmotion.y; + break; } } } @@ -120,12 +141,13 @@ void mgl_window_events_poll(mgl_window *self) { tv.tv_usec = (timeout_ms * 1000) - (tv.tv_sec * 1000 * 1000);*/ /* TODO: Is this needed when using XPending? */ - const int num_ready_fds = select(1 + x11_fd, &in_fds, NULL, NULL, &tv); + /*const int num_ready_fds = select(1 + x11_fd, &in_fds, NULL, NULL, &tv);*/ + const int num_ready_fds = 1; if(num_ready_fds > 0) { XEvent xev; - while(XPending(display)) { + while(self->window && XPending(display)) { XNextEvent(display, &xev); - on_receive_x11_event(self, &xev); + mgl_window_on_receive_event(self, &xev); } } else if(num_ready_fds == -1 && errno != EINTR) { /* TODO: */ -- cgit v1.2.3