summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2024-07-17 20:26:29 +1000
committerquou <quou@disroot.org>2024-07-17 20:26:47 +1000
commit1453334863205022b9d8a830cecf3ed01390a3cc (patch)
treef756cf793c668dabdc7a6557938191152c71a184
parent7564b36e12e51b0298234c6856b757ff15402b24 (diff)
raw mouse input
-rw-r--r--3de.c1
-rw-r--r--Makefile2
-rw-r--r--plat.c70
-rw-r--r--plat.h2
4 files changed, 72 insertions, 3 deletions
diff --git a/3de.c b/3de.c
index 5b5bcc3..23e4d13 100644
--- a/3de.c
+++ b/3de.c
@@ -72,6 +72,7 @@ int entrypoint(int argc, const char** argv, Arena* a) {
return error_out_of_memory;
}
preload_assets(&preload);
+ cfg_mouse(app, 0);
while (app->o) {
fps_begin(&f);
while (f.now >= f.next) {
diff --git a/Makefile b/Makefile
index 55499da..d183d38 100644
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ linker = gcc
cflags = -I./ -g -DDEBUG -Dplat_x11 -Dplat_x86 \
-Dplat_posix -Dallocation_default_alignment=8 \
-Wall -Wextra -pedantic -std=c90
-lflags = -lX11
+lflags = -lX11 -lXi
int_dir = intermediate
data_dir = data
target = 3de
diff --git a/plat.c b/plat.c
index 09c9444..5fbfb19 100644
--- a/plat.c
+++ b/plat.c
@@ -256,6 +256,7 @@ int main(int argc, const char** argv) {
#include <X11/Xlib.h>
#include <X11/Xutil.h>
+#include <X11/extensions/XInput2.h>
#include <stdlib.h>
@@ -264,6 +265,8 @@ typedef struct {
Window wi;
GC gc;
int w, h;
+ int rmx, rmy;
+ int ms;
unsigned long begin, end;
XImage* bb;
Colour* bbp;
@@ -425,8 +428,32 @@ void app_begin(App* a) {
i->h = mini(wa.height, max_pc_window_h);
break;
case MotionNotify:
- a->mx = e.xmotion.x / a->s;
- a->my = e.xmotion.y / a->s;
+ if (i->ms) {
+ a->mx = e.xmotion.x / a->s;
+ a->my = e.xmotion.y / a->s;
+ }
+ break;
+ case GenericEvent:
+ if (!i->ms) {
+ if (
+ XGetEventData(d, &e.xcookie) &&
+ e.xcookie.evtype == XI_RawMotion
+ ) {
+ XIRawEvent* re = e.xcookie.data;
+ if (re->valuators.mask_len) {
+ const double* values = re->raw_values;
+ if (XIMaskIsSet(re->valuators.mask, 0)) {
+ i->rmx += (int)values[0];
+ }
+ if (XIMaskIsSet(re->valuators.mask, 1)) {
+ i->rmy += (int)values[1];
+ }
+ a->mx = i->rmx / a->s;
+ a->my = i->rmy / a->s;
+ }
+ XFreeEventData(d, &e.xcookie);
+ }
+ }
break;
default:
break;
@@ -496,4 +523,43 @@ void app_end(App* a) {
a->fps = 1000000000 / (i->end - i->begin);
}
+void cfg_mouse(App* a, int show) {
+ App_Internal* i = (App_Internal*)(&a[1]);
+ Display* d = i->d;
+ Window w = i->wi;
+ i->ms = show;
+ if (show) {
+ XUndefineCursor(d, w);
+ XUngrabPointer(d, CurrentTime);
+ } else {
+ XColor c;
+ char bd[1] = { 0 };
+ Pixmap b = XCreateBitmapFromData(d, w, bd, 1, 1);
+ Cursor cur = XCreatePixmapCursor(d, b, b, &c, &c, 0, 0);
+ XIEventMask em;
+ unsigned char mask[XIMaskLen(XI_RawMotion)] = { 0 };
+ XDefineCursor(d, w, cur);
+ XGrabPointer(
+ d,
+ w,
+ 1,
+ ButtonPressMask |
+ ButtonReleaseMask |
+ PointerMotionMask,
+ GrabModeAsync,
+ GrabModeAsync,
+ w,
+ None,
+ CurrentTime
+ );
+ em.deviceid = XIAllMasterDevices;
+ em.mask_len = sizeof(mask);
+ em.mask = mask;
+ XISetMask(mask, XI_RawMotion);
+ XISelectEvents(d, DefaultRootWindow(d), &em, 1);
+ i->rmx = a->mx;
+ i->rmy = a->my;
+ }
+}
+
#endif
diff --git a/plat.h b/plat.h
index 419d7fe..a4e2758 100644
--- a/plat.h
+++ b/plat.h
@@ -78,4 +78,6 @@ void deinit_app(App* a);
void app_begin(App* a);
void app_end(App* a);
+void cfg_mouse(App* a, int show);
+
#endif