From dc1db3a1f58b1bd7004153b2f04e85e719489209 Mon Sep 17 00:00:00 2001 From: davidovski Date: Tue, 13 Apr 2021 00:01:43 +0100 Subject: added second thread --- Makefile | 3 +- anyscroll.c | 110 ++++++++++++++++++++++++++++++++++++++++++------------------ mids | Bin 0 -> 16640 bytes mids.o | Bin 0 -> 3400 bytes window | Bin 0 -> 16504 bytes window.c | 32 ++++++++++++++++++ 6 files changed, 112 insertions(+), 33 deletions(-) create mode 100755 mids create mode 100644 mids.o create mode 100755 window create mode 100644 window.c diff --git a/Makefile b/Makefile index 3bd1e01..3c0cebe 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ PROG = anyscroll -FLAGS = -lX11 -lXtst +FLAGS = -lX11 -lXtst -lXi -lpthread + ${PROG}: ${PROG}.o diff --git a/anyscroll.c b/anyscroll.c index 2dbd8b8..ab24876 100644 --- a/anyscroll.c +++ b/anyscroll.c @@ -4,25 +4,68 @@ #include #include #include - +#include +#include static Display *dpy; static int scr, sw, sh; - +static int sx, sy; static Window root; -static void pressButton(int btn) { - XTestFakeButtonEvent(dpy, btn, True, CurrentTime); - XFlush(dpy); +static void select_events(Display *dpy, Window win) { + XIEventMask evmasks[1]; + unsigned char mask1[(XI_LASTEVENT + 7)/8]; + + memset(mask1, 0, sizeof(mask1)); + + /* select for button and key events from all master devices */ + XISetMask(mask1, XI_RawButtonPress); + XISetMask(mask1, XI_RawButtonRelease); + + evmasks[0].deviceid = XIAllMasterDevices; + evmasks[0].mask_len = sizeof(mask1); + evmasks[0].mask = mask1; + + XISelectEvents(dpy, win, evmasks, 1); + XFlush(dpy); } -static void releaseButton(int btn) { - XTestFakeButtonEvent(dpy, btn, False, CurrentTime); - XFlush(dpy); + +static void getmousepos(int *px, int *py) { + Window r, child; + int rx, ry; + unsigned int mask; + XQueryPointer(dpy, root, &r, &child, px, py, &rx, &ry, &mask); } +static void* loop() { + int px, py, lx, ly, dx, dy; + while (1) { + lx = px, ly = py; + getmousepos(&px, &py); + dx = px - lx; + dy = py - ly; + printf("(%d, %d) from (%d, %d)\n", px, py, sx, sy); + sleep(1); -int main(int argc, const char **argv) { + + } +} + +static void mouse_down(XIRawEvent *xev) { + getmousepos(&sx, &sy); + printf("Button pressed %d @ %d, %d\n", xev->detail, sx, sy); + +} + +static void mouse_up(XIRawEvent *xev) { + sx = -1; + sy = -1; + printf("Button released %d %d\n", xev->detail); +} + +int main(int argc, const char **argv) { + XInitThreads(); for (int i = 1; i < argc; i++) { if (argv[i][1] == 'h') { fprintf(stdout, "there is no help to be given\n"); @@ -37,34 +80,37 @@ int main(int argc, const char **argv) { sh = DisplayHeight(dpy, scr); fprintf(stdout, "waiting for events\n"); - - const long mask = ButtonPressMask|ButtonReleaseMask; -// XGrabPointer(dpy, root, True, mask, GrabModeSync, GrabModeAsync, None, None, CurrentTime); - XGrabButton(dpy, 2, AnyModifier, root, True, mask, GrabModeSync, GrabModeAsync, None, None); - //XSelectInput(dpy, root, KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask); XEvent ev; - XButtonEvent *eb; - - int grab = 0; + XIEvent *xi_event; + XIRawEvent *xev; + XGenericEventCookie *cookie = &ev.xcookie; + + select_events(dpy, root); + pthread_t id[2]; + pthread_create(&id[0], NULL, loop, &argv); + for (;;) { - XNextEvent(dpy, &ev); - if (ev.type == ButtonPress) { - eb = &ev.xbutton; - printf("%d button pressed!\n", eb->button); -// XGrabPointer(dpy, root, True, PointerMotionMask|ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime); - - } else if (ev.type == ButtonRelease) { - eb = &ev.xbutton; - printf("%d button released!\n", eb->button); - //releaseButton(2); - // XUngrabPointer(dpy, CurrentTime); - } + if (XCheckTypedEvent(dpy, GenericEvent ,&ev)) { + if (cookie->type != GenericEvent || !XGetEventData(dpy, cookie)) { + continue; + } - XAllowEvents(dpy, ReplayPointer, CurrentTime); - XSync(dpy, 0); -// XFlush(dpy); + + xi_event = (XIEvent *) cookie->data; + xev = (XIRawEvent *) xi_event; + switch (cookie->evtype) { + case XI_RawButtonPress: + if (xev->detail == 2) mouse_down(xev); + break; + case XI_RawButtonRelease: + if (xev->detail == 2) mouse_up(xev); + break; + } + + XFreeEventData(dpy, cookie); + } } XCloseDisplay(dpy); return 0; diff --git a/mids b/mids new file mode 100755 index 0000000..e1d9983 Binary files /dev/null and b/mids differ diff --git a/mids.o b/mids.o new file mode 100644 index 0000000..8a0fc6b Binary files /dev/null and b/mids.o differ diff --git a/window b/window new file mode 100755 index 0000000..2ccfaca Binary files /dev/null and b/window differ diff --git a/window.c b/window.c new file mode 100644 index 0000000..0d55011 --- /dev/null +++ b/window.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +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; +} -- cgit v1.2.1