added second thread

This commit is contained in:
davidovski 2021-04-13 00:01:43 +01:00
parent fc9fa1e4a2
commit dc1db3a1f5
6 changed files with 112 additions and 33 deletions

View File

@ -1,6 +1,7 @@
PROG = anyscroll PROG = anyscroll
FLAGS = -lX11 -lXtst FLAGS = -lX11 -lXtst -lXi -lpthread
${PROG}: ${PROG}.o ${PROG}: ${PROG}.o

View File

@ -4,25 +4,68 @@
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/extensions/XTest.h> #include <X11/extensions/XTest.h>
#include <unistd.h> #include <unistd.h>
#include <X11/extensions/XInput2.h>
#include <pthread.h>
static Display *dpy; static Display *dpy;
static int scr, sw, sh; static int scr, sw, sh;
static int sx, sy;
static Window root; static Window root;
static void pressButton(int btn) { static void select_events(Display *dpy, Window win) {
XTestFakeButtonEvent(dpy, btn, True, CurrentTime); XIEventMask evmasks[1];
XFlush(dpy); unsigned char mask1[(XI_LASTEVENT + 7)/8];
}
static void releaseButton(int btn) { memset(mask1, 0, sizeof(mask1));
XTestFakeButtonEvent(dpy, btn, False, CurrentTime);
XFlush(dpy); /* 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 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);
}
}
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) { int main(int argc, const char **argv) {
XInitThreads();
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
if (argv[i][1] == 'h') { if (argv[i][1] == 'h') {
fprintf(stdout, "there is no help to be given\n"); 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); sh = DisplayHeight(dpy, scr);
fprintf(stdout, "waiting for events\n"); 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; XEvent ev;
XButtonEvent *eb; XIEvent *xi_event;
XIRawEvent *xev;
int grab = 0; XGenericEventCookie *cookie = &ev.xcookie;
select_events(dpy, root);
pthread_t id[2];
pthread_create(&id[0], NULL, loop, &argv);
for (;;) { for (;;) {
XNextEvent(dpy, &ev); if (XCheckTypedEvent(dpy, GenericEvent ,&ev)) {
if (ev.type == ButtonPress) { if (cookie->type != GenericEvent || !XGetEventData(dpy, cookie)) {
eb = &ev.xbutton; continue;
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; xi_event = (XIEvent *) cookie->data;
printf("%d button released!\n", eb->button); xev = (XIRawEvent *) xi_event;
//releaseButton(2); switch (cookie->evtype) {
// XUngrabPointer(dpy, CurrentTime); 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);
} }
XAllowEvents(dpy, ReplayPointer, CurrentTime);
XSync(dpy, 0);
// XFlush(dpy);
} }
XCloseDisplay(dpy); XCloseDisplay(dpy);
return 0; return 0;

BIN
mids Executable file

Binary file not shown.

BIN
mids.o Normal file

Binary file not shown.

BIN
window Executable file

Binary file not shown.

32
window.c Normal file
View File

@ -0,0 +1,32 @@
#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;
}