aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2019-07-16 00:18:16 +0100
committerHarry Jeffery <harry@exec64.co.uk>2019-07-16 00:18:16 +0100
commit2f79eb8965bfacfb438dde9ddd503eb2cb841443 (patch)
treeb313efd9ae7c585b99ba8a2bf8e69077707fa7f6 /src
parentd49a63d063e24621e296c07a65f62665637d908d (diff)
downloadimv-2f79eb8965bfacfb438dde9ddd503eb2cb841443.tar.gz
x11: Implement mouse input
Diffstat (limited to 'src')
-rw-r--r--src/x11_window.c80
1 files changed, 75 insertions, 5 deletions
diff --git a/src/x11_window.c b/src/x11_window.c
index 0f488e2..19c7601 100644
--- a/src/x11_window.c
+++ b/src/x11_window.c
@@ -17,11 +17,23 @@ struct imv_window {
Atom x_fullscreen;
int width;
int height;
+ struct {
+ struct {
+ int x, y;
+ } last;
+ struct {
+ int x, y;
+ } current;
+ bool mouse1;
+ } pointer;
};
struct imv_window *imv_window_create(int w, int h, const char *title)
{
struct imv_window *window = calloc(1, sizeof *window);
+ window->pointer.last.x = -1;
+ window->pointer.last.y = -1;
+
window->x_display = XOpenDisplay(NULL);
assert(window->x_display);
Window root = DefaultRootWindow(window->x_display);
@@ -44,6 +56,7 @@ struct imv_window *imv_window_create(int w, int h, const char *title)
XSetWindowAttributes wa = {
.colormap = cmap,
.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask
+ | ButtonPressMask | ButtonReleaseMask | PointerMotionMask
};
window->x_window = XCreateWindow(window->x_display, root, 0, 0, w, h,
@@ -148,19 +161,19 @@ void imv_window_set_fullscreen(struct imv_window *window, bool fullscreen)
bool imv_window_get_mouse_button(struct imv_window *window, int button)
{
- (void)window;
- (void)button;
+ if (button == 1) {
+ return window->pointer.mouse1;
+ }
return false;
}
void imv_window_get_mouse_position(struct imv_window *window, double *x, double *y)
{
- (void)window;
if (x) {
- *x = 0;
+ *x = window->pointer.current.x;
}
if (y) {
- *y = 0;
+ *y = window->pointer.current.y;
}
}
@@ -231,6 +244,63 @@ void imv_window_pump_events(struct imv_window *window, imv_event_handler handler
if (handler) {
handler(data, &e);
}
+ } else if (xev.type == ButtonPress || xev.type == ButtonRelease) {
+ if (xev.xbutton.button == Button1) {
+ window->pointer.mouse1 = xev.type == ButtonPress;
+ struct imv_event e = {
+ .type = IMV_EVENT_MOUSE_BUTTON,
+ .data = {
+ .mouse_button = {
+ .button = 1,
+ .pressed = xev.type == ButtonPress
+ }
+ }
+ };
+ if (handler) {
+ handler(data, &e);
+ }
+ } else if (xev.xbutton.button == Button4 || xev.xbutton.button == Button5) {
+ struct imv_event e = {
+ .type = IMV_EVENT_MOUSE_SCROLL,
+ .data = {
+ .mouse_scroll = {
+ .dx = 0,
+ .dy = xev.xbutton.button == Button4 ? -1 : 1
+ }
+ }
+ };
+ if (handler) {
+ handler(data, &e);
+ }
+ }
+ } else if (xev.type == MotionNotify) {
+ window->pointer.current.x = xev.xmotion.x;
+ window->pointer.current.y = xev.xmotion.y;
+ int dx = window->pointer.current.x - window->pointer.last.x;
+ int dy = window->pointer.current.y - window->pointer.last.y;
+ if (window->pointer.last.x == -1) {
+ dx = 0;
+ }
+ if (window->pointer.last.y == -1) {
+ dy = 0;
+ }
+ window->pointer.last.x = window->pointer.current.x;
+ window->pointer.last.y = window->pointer.current.y;
+
+ struct imv_event e = {
+ .type = IMV_EVENT_MOUSE_MOTION,
+ .data = {
+ .mouse_motion = {
+ .x = window->pointer.current.x,
+ .y = window->pointer.current.y,
+ .dx = dx,
+ .dy = dy
+ }
+ }
+ };
+ if (handler) {
+ handler(data, &e);
+ }
} else if (xev.type == ClientMessage) {
struct imv_event e = {
.type = IMV_EVENT_CUSTOM,