aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-11-16 04:18:39 +0100
committerdec05eba <dec05eba@protonmail.com>2021-11-16 04:19:54 +0100
commit8ea04000bfa98798a0283bcf80ea136ea8b4dd42 (patch)
tree34637967d90c1fa975d2acacdcdfb654778667f4
parent61c7efc6dd2a036b5d14aa223d06a18cb7d1388e (diff)
Window: add function to get the current mouse button state
-rw-r--r--include/mgl/window/event.h10
-rw-r--r--include/mgl/window/mouse_button.h16
-rw-r--r--include/mgl/window/window.h2
-rw-r--r--src/window/window.c27
4 files changed, 46 insertions, 9 deletions
diff --git a/include/mgl/window/event.h b/include/mgl/window/event.h
index a0d3982..1e7cebf 100644
--- a/include/mgl/window/event.h
+++ b/include/mgl/window/event.h
@@ -2,6 +2,7 @@
#define MGL_EVENT_H
#include "key.h"
+#include "mouse_button.h"
#include <stdbool.h>
#include <stdint.h>
@@ -26,15 +27,6 @@ typedef struct {
bool system;
} mgl_key_event;
-typedef enum {
- MGL_BUTTON_UNKNOWN,
- MGL_BUTTON_LEFT,
- MGL_BUTTON_RIGHT,
- MGL_BUTTON_MIDDLE,
- MGL_BUTTON_XBUTTON1,
- MGL_BUTTON_XBUTTON2
-} mgl_mouse_button;
-
typedef struct {
int button; /* mgl_mouse_button */
int x; /* mouse position relative to left of the window */
diff --git a/include/mgl/window/mouse_button.h b/include/mgl/window/mouse_button.h
new file mode 100644
index 0000000..fccf0a7
--- /dev/null
+++ b/include/mgl/window/mouse_button.h
@@ -0,0 +1,16 @@
+#ifndef _MGL_MOUSE_BUTTON_H_
+#define _MGL_MOUSE_BUTTON_H_
+
+typedef enum {
+ MGL_BUTTON_UNKNOWN,
+ MGL_BUTTON_LEFT,
+ MGL_BUTTON_RIGHT,
+ MGL_BUTTON_MIDDLE,
+ MGL_BUTTON_XBUTTON1,
+ MGL_BUTTON_XBUTTON2,
+
+ /* This should always be the last mouse button */
+ __MGL_NUM_MOUSE_BUTTONS__
+} mgl_mouse_button;
+
+#endif /* _MGL_MOUSE_BUTTON_H_ */
diff --git a/include/mgl/window/window.h b/include/mgl/window/window.h
index 4bbb46d..a63bc03 100644
--- a/include/mgl/window/window.h
+++ b/include/mgl/window/window.h
@@ -5,6 +5,7 @@
#include "../system/vec.h"
#include "../system/clock.h"
#include "key.h"
+#include "mouse_button.h"
#include <stdbool.h>
/* Vsync is automatically set for created windows, if supported by the system */
@@ -55,6 +56,7 @@ void mgl_window_get_view(mgl_window *self, mgl_view *view);
bool mgl_window_is_open(const mgl_window *self);
bool mgl_window_has_focus(const mgl_window *self);
bool mgl_window_is_key_pressed(const mgl_window *self, mgl_key key);
+bool mgl_window_is_mouse_button_pressed(const mgl_window *self, mgl_mouse_button button);
void mgl_window_close(mgl_window *self);
void mgl_window_set_title(mgl_window *self, const char *title);
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);