diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-11-16 04:18:39 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-11-16 04:19:54 +0100 |
commit | 8ea04000bfa98798a0283bcf80ea136ea8b4dd42 (patch) | |
tree | 34637967d90c1fa975d2acacdcdfb654778667f4 /src/window | |
parent | 61c7efc6dd2a036b5d14aa223d06a18cb7d1388e (diff) |
Window: add function to get the current mouse button state
Diffstat (limited to 'src/window')
-rw-r--r-- | src/window/window.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/window/window.c b/src/window/window.c index 4523471..5cc7ba1 100644 --- a/src/window/window.c +++ b/src/window/window.c @@ -62,6 +62,7 @@ typedef struct { */ x11_events_circular_buffer events; bool key_pressed[__MGL_NUM_KEYS__]; + bool mouse_button_pressed[__MGL_NUM_MOUSE_BUTTONS__]; } x11_context; static void x11_context_deinit(x11_context *self); @@ -100,6 +101,10 @@ static int x11_context_init(x11_context *self) { self->key_pressed[i] = false; } + for(size_t i = 0; i < __MGL_NUM_MOUSE_BUTTONS__; ++i) { + self->mouse_button_pressed[i] = false; + } + return 0; } @@ -150,10 +155,24 @@ static bool x11_context_update_key_state(x11_context *self, mgl_key key, bool ke } } +/* Returns true if the state changed */ +static bool x11_context_update_mouse_button_state(x11_context *self, mgl_mouse_button button, bool button_pressed) { + if(self->mouse_button_pressed[button] != button_pressed) { + self->mouse_button_pressed[button] = button_pressed; + return true; + } else { + return false; + } +} + static bool x11_context_is_key_pressed(const x11_context *self, mgl_key key) { return self->key_pressed[key]; } +static bool x11_context_is_mouse_button_pressed(const x11_context *self, mgl_mouse_button button) { + return self->mouse_button_pressed[button]; +} + /* TODO: Use gl OML present for other platforms than nvidia? nvidia doesn't support present yet */ /* TODO: check for glx swap control extension string (GLX_EXT_swap_control, etc) */ @@ -487,6 +506,7 @@ static void mgl_window_on_receive_event(mgl_window *self, XEvent *xev, mgl_event 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; + x11_context_update_mouse_button_state(self->context, event->mouse_button.button, true); } return; } @@ -495,6 +515,7 @@ static void mgl_window_on_receive_event(mgl_window *self, XEvent *xev, mgl_event 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; + x11_context_update_mouse_button_state(self->context, event->mouse_button.button, false); return; } case FocusIn: { @@ -613,6 +634,12 @@ bool mgl_window_is_key_pressed(const mgl_window *self, mgl_key key) { return x11_context_is_key_pressed(self->context, key); } +bool mgl_window_is_mouse_button_pressed(const mgl_window *self, mgl_mouse_button button) { + if(button < 0 || button >= __MGL_NUM_MOUSE_BUTTONS__) + return false; + return x11_context_is_mouse_button_pressed(self->context, button); +} + void mgl_window_close(mgl_window *self) { mgl_context *context = mgl_get_context(); XUnmapWindow(context->connection, self->window); |