tidied up

This commit is contained in:
davidovski 2021-04-13 11:51:46 +01:00
parent d6bb142887
commit b51a0bc53d
5 changed files with 26 additions and 54 deletions

View File

@ -6,44 +6,49 @@
#include <unistd.h> #include <unistd.h>
#include <X11/extensions/XInput2.h> #include <X11/extensions/XInput2.h>
#include <pthread.h> #include <pthread.h>
#include <math.h>
static Display *dpy; static Display *dpy;
static int scr, sw, sh; static int scr, sw, sh;
static int sx = -1, sy = -1; static int sx = -1, sy = -1;
static Window root; static Window root;
static const int direction = -1;
static const int pixels_to_scroll = 16;
static void scroll(int v) { static void scroll(int v) {
v = v * direction;
int btn; int btn;
if (v > 0) btn = 4; if (v > 0) btn = 4;
else if (v < 0) btn = 5; else if (v < 0) btn = 5;
else return; else return;
for (int i = 0; i < abs(v); i++) { int times = ceil(abs(v) / pixels_to_scroll) + 1;
for (int i = 0; i < times; i++) {
XTestFakeButtonEvent(dpy, btn, True, CurrentTime); XTestFakeButtonEvent(dpy, btn, True, CurrentTime);
XFlush(dpy); XFlush(dpy);
// usleep(100000);
XTestFakeButtonEvent(dpy, btn, False, CurrentTime); XTestFakeButtonEvent(dpy, btn, False, CurrentTime);
XFlush(dpy); XFlush(dpy);
} }
} }
static void select_events(Display *dpy, Window win) { static void select_events(Display *dpy, Window win) {
XIEventMask evmasks[1]; XIEventMask evmasks[1];
unsigned char mask1[(XI_LASTEVENT + 7)/8]; unsigned char mask1[(XI_LASTEVENT + 7)/8];
memset(mask1, 0, sizeof(mask1)); memset(mask1, 0, sizeof(mask1));
/* select for button and key events from all master devices */ XISetMask(mask1, XI_RawButtonPress);
XISetMask(mask1, XI_RawButtonPress); XISetMask(mask1, XI_RawButtonRelease);
XISetMask(mask1, XI_RawButtonRelease);
evmasks[0].deviceid = XIAllMasterDevices; evmasks[0].deviceid = XIAllMasterDevices;
evmasks[0].mask_len = sizeof(mask1); evmasks[0].mask_len = sizeof(mask1);
evmasks[0].mask = mask1; evmasks[0].mask = mask1;
XISelectEvents(dpy, win, evmasks, 1); XISelectEvents(dpy, win, evmasks, 1);
XFlush(dpy); XFlush(dpy);
} }
static void getmousepos(int *px, int *py) { static void getmousepos(int *px, int *py) {
@ -62,22 +67,21 @@ static void* loop() {
dy = py - ly; dy = py - ly;
if (sx != -1 && sy != -1) scroll(dy); if (sx != -1 && sy != -1) scroll(dy);
printf("%d, %d with deltas (%d, %d)\n", sx, sy, dx, dy);
if (dx != 0 | dy != 0) {
printf("%d, %d with deltas (%d, %d)\n", sx, sy, dx, dy);
}
sleep(0.1); sleep(0.1);
} }
} }
static void mouse_down(XIRawEvent *xev) { static void mouse_down(XIRawEvent *xev) {
getmousepos(&sx, &sy); getmousepos(&sx, &sy);
printf("Button pressed %d @ %d, %d\n", xev->detail, sx, sy);
} }
static void mouse_up(XIRawEvent *xev) { static void mouse_up(XIRawEvent *xev) {
sx = -1; sx = -1;
sy = -1; sy = -1;
printf("Button released %d %d\n", xev->detail);
} }
int main(int argc, const char **argv) { int main(int argc, const char **argv) {
@ -95,7 +99,7 @@ int main(int argc, const char **argv) {
sw = DisplayWidth(dpy, scr); sw = DisplayWidth(dpy, scr);
sh = DisplayHeight(dpy, scr); sh = DisplayHeight(dpy, scr);
fprintf(stdout, "waiting for events\n"); fprintf(stdout, "waiting for events...\n");
XEvent ev; XEvent ev;
XIEvent *xi_event; XIEvent *xi_event;
@ -119,15 +123,15 @@ int main(int argc, const char **argv) {
switch (cookie->evtype) { switch (cookie->evtype) {
case XI_RawButtonPress: case XI_RawButtonPress:
if (xev->detail == 2) mouse_down(xev); if (xev->detail == 2) mouse_down(xev);
break; break;
case XI_RawButtonRelease: case XI_RawButtonRelease:
if (xev->detail == 2) mouse_up(xev); if (xev->detail == 2) mouse_up(xev);
break; break;
} }
XFreeEventData(dpy, cookie); XFreeEventData(dpy, cookie);
} }
} } // there is no way out of this loop lol
XCloseDisplay(dpy); XCloseDisplay(dpy);
return 0; return 0;
} }

BIN
mids

Binary file not shown.

BIN
mids.o

Binary file not shown.

BIN
window

Binary file not shown.

View File

@ -1,32 +0,0 @@
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char **argv) {
Display *d;
XEvent e;
const char *message = "do you see the fun in gcc?";
d = XOpenDisplay(NULL);
if (d == NULL) {
fprintf(stderr, "error opening display\n");
exit(1);
}
int s = DefaultScreen(d);
Window w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 100, 100, 1, BlackPixel(d, s), WhitePixel(d, s));
XSelectInput(d, w, ExposureMask | KeyPressMask);
XMapWindow(d, w);
while (1) {
XNextEvent(d, &e);
if (e.type == Expose) {
XDrawString(d, w, DefaultGC(d, s), 10, 50, message, strlen(message));
}
}
XCloseDisplay(d);
return 0;
}