From b82049c7ad77603537d419bdd0ebebfd3f007916 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 16 Nov 2021 02:56:10 +0100 Subject: Window: add mouse scroll event --- TODO | 3 ++- include/mgl/window/event.h | 22 +++++++++++++++------- src/window/window.c | 32 +++++++++++++++++++++++--------- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/TODO b/TODO index 7e867a1..bd0a782 100644 --- a/TODO +++ b/TODO @@ -3,4 +3,5 @@ Use gl triangle instead of quad.\ Support using multiple textures in shaders by using glActiveTexture for each one and set the uniform sampler2D value for each as as the index.\ Make sure clock is monotonic (there has been some issues with CLOCK\_MONOTONIC not being monotonic on linux).\ Verify if using a separate glx context for every window is the correct approach.\ -Experiment with reducing latency by removing GLX_DOUBLEBUFFER and using glFlush&&glFinish&&glXWaitVideoSyncSGI(1, 0, &rem);. Only glFinish is needed when using a compositor and that fixes the flickering. Flipping also needs to be enabled in gpu driver settings. Use glXWaitVideoSyncSGI to limit fps to vsync. Tearing fix with flipping for fullscreen applications only works when fullscreen covers all monitors which is usually not the case, so it really only works when one monitor is plugged in. +Experiment with reducing latency by removing GLX_DOUBLEBUFFER and using glFlush&&glFinish&&glXWaitVideoSyncSGI(1, 0, &rem);. Only glFinish is needed when using a compositor and that fixes the flickering. Flipping also needs to be enabled in gpu driver settings. Use glXWaitVideoSyncSGI to limit fps to vsync. Tearing fix with flipping for fullscreen applications only works when fullscreen covers all monitors which is usually not the case, so it really only works when one monitor is plugged in.\ +High precision mouse wheel event by using xi2, which also allows us to get which scroll wheel was used and scrolling in y direction. \ No newline at end of file diff --git a/include/mgl/window/event.h b/include/mgl/window/event.h index f6bf7fd..a0d3982 100644 --- a/include/mgl/window/event.h +++ b/include/mgl/window/event.h @@ -26,11 +26,6 @@ typedef struct { bool system; } mgl_key_event; -typedef struct { - int x; /* relative to left of the window */ - int y; /* relative to the top of the window */ -} mgl_mouse_move_event; - typedef enum { MGL_BUTTON_UNKNOWN, MGL_BUTTON_LEFT, @@ -42,10 +37,21 @@ typedef enum { typedef struct { int button; /* mgl_mouse_button */ - int x; /* relative to left of the window */ - int y; /* relative to top of the window */ + int x; /* mouse position relative to left of the window */ + int y; /* mouse position relative to top of the window */ } mgl_mouse_button_event; +typedef struct { + int delta; /* positive = up, negative = down */ + int x; /* mouse position relative to left of the window */ + int y; /* mouse position relative to left of the window */ +} mgl_mouse_wheel_scroll_event; + +typedef struct { + int x; /* relative to left of the window */ + int y; /* relative to the top of the window */ +} mgl_mouse_move_event; + typedef enum { MGL_EVENT_UNKNOWN, MGL_EVENT_CLOSED, /* Window closed */ @@ -57,6 +63,7 @@ typedef enum { MGL_EVENT_KEY_RELEASED, MGL_EVENT_MOUSE_BUTTON_PRESSED, MGL_EVENT_MOUSE_BUTTON_RELEASED, + MGL_EVENT_MOUSE_WHEEL_SCROLLED, MGL_EVENT_MOUSE_MOVED } mgl_event_type; @@ -67,6 +74,7 @@ struct mgl_event { mgl_text_event text; mgl_key_event key; mgl_mouse_button_event mouse_button; + mgl_mouse_wheel_scroll_event mouse_wheel_scroll; mgl_mouse_move_event mouse_move; }; }; diff --git a/src/window/window.c b/src/window/window.c index 440eecb..4523471 100644 --- a/src/window/window.c +++ b/src/window/window.c @@ -397,11 +397,11 @@ static mgl_key x11_keysym_to_mgl_key(KeySym key_sym) { static mgl_mouse_button x11_button_to_mgl_button(unsigned int button) { switch(button) { - case 1: return MGL_BUTTON_LEFT; - case 2: return MGL_BUTTON_MIDDLE; - case 3: return MGL_BUTTON_RIGHT; - case 8: return MGL_BUTTON_XBUTTON1; - case 9: return MGL_BUTTON_XBUTTON2; + case Button1: return MGL_BUTTON_LEFT; + case Button2: return MGL_BUTTON_MIDDLE; + case Button3: return MGL_BUTTON_RIGHT; + case 8: return MGL_BUTTON_XBUTTON1; + case 9: return MGL_BUTTON_XBUTTON2; } return MGL_BUTTON_UNKNOWN; } @@ -470,10 +470,24 @@ static void mgl_window_on_receive_event(mgl_window *self, XEvent *xev, mgl_event return; } case ButtonPress: { - event->type = MGL_EVENT_MOUSE_BUTTON_PRESSED; - event->mouse_button.button = x11_button_to_mgl_button(xev->xbutton.button); - event->mouse_button.x = xev->xbutton.x; - event->mouse_button.y = xev->xbutton.y; + if(xev->xbutton.button == Button4) { + /* Mouse scroll up */ + event->type = MGL_EVENT_MOUSE_WHEEL_SCROLLED; + event->mouse_wheel_scroll.delta = 1; + event->mouse_wheel_scroll.x = xev->xbutton.x; + event->mouse_wheel_scroll.y = xev->xbutton.y; + } else if(xev->xbutton.button == Button5) { + /* Mouse scroll down */ + event->type = MGL_EVENT_MOUSE_WHEEL_SCROLLED; + event->mouse_wheel_scroll.delta = -1; + event->mouse_wheel_scroll.x = xev->xbutton.x; + event->mouse_wheel_scroll.y = xev->xbutton.y; + } else { + event->type = MGL_EVENT_MOUSE_BUTTON_PRESSED; + event->mouse_button.button = x11_button_to_mgl_button(xev->xbutton.button); + event->mouse_button.x = xev->xbutton.x; + event->mouse_button.y = xev->xbutton.y; + } return; } case ButtonRelease: { -- cgit v1.2.3