diff options
Diffstat (limited to 'xmouseless.c')
-rw-r--r-- | xmouseless.c | 120 |
1 files changed, 44 insertions, 76 deletions
diff --git a/xmouseless.c b/xmouseless.c index 5e714e7..f1f169e 100644 --- a/xmouseless.c +++ b/xmouseless.c @@ -23,12 +23,12 @@ typedef struct { typedef struct { KeySym keysym; - int button; + unsigned int button; } ClickBinding; typedef struct { KeySym keysym; - int speed; + unsigned int speed; } SpeedBindings; typedef struct { @@ -41,7 +41,6 @@ typedef struct { Display *dpy; -int screen; Window root; pthread_t movethread; @@ -50,25 +49,25 @@ static unsigned int speed = default_speed; struct { int x; int y; - int move_x; - int move_y; + int speed_x; + int speed_y; } mouseinfo; -int getrootptr(int *x, int *y); +void getrootptr(int *x, int *y); void moverelative(int x, int y); -void click(int button, int is_press); +void click(unsigned int button, Bool is_press); void handle_keypress(XKeyEvent event); void handle_keyrelease(XKeyEvent event); void init_x(); void close_x(); -int getrootptr(int *x, int *y) { +void getrootptr(int *x, int *y) { int di; unsigned int dui; Window dummy; - return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui); + XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui); } void moverelative(int x, int y) { @@ -78,19 +77,20 @@ void moverelative(int x, int y) { XFlush(dpy); } -void click(int button, int is_press) { +void click(unsigned int button, Bool is_press) { XTestFakeButtonEvent(dpy, button, is_press, 0); XFlush(dpy); } void init_x() { int i; + int screen; /* initialize support for concurrent threads */ XInitThreads(); - dpy=XOpenDisplay((char *)0); - screen=DefaultScreen(dpy); + dpy = XOpenDisplay((char *) 0); + screen = DefaultScreen(dpy); root = RootWindow(dpy, screen); /* turn auto key repeat off */ @@ -104,7 +104,7 @@ void init_x() { usleep(10000); } - printf("grab keyboard failed"); + printf("grab keyboard failed\n"); close_x(EXIT_FAILURE); } @@ -117,94 +117,66 @@ void close_x(int exit_status) { } void *moveforever(void *val) { - /* this is executed in a thread */ + /* this function is executed in a seperate thread */ while (1) { - if (mouseinfo.move_x != 0 || mouseinfo.move_y != 0) { - moverelative(speed * mouseinfo.move_x, speed * mouseinfo.move_y); + if (mouseinfo.speed_x != 0 || mouseinfo.speed_y != 0) { + moverelative(speed * mouseinfo.speed_x, speed * mouseinfo.speed_y); } usleep(1000000 / move_rate); } } -void handle_keypress(XKeyEvent event) { +void handle_key(XKeyEvent event) { unsigned int i; KeySym keysym; + Bool is_press; - keysym = XkbKeycodeToKeysym(dpy, event.keycode, - 0, event.state & ShiftMask ? 1 : 0); + keysym = XkbKeycodeToKeysym(dpy, event.keycode, 0, 0); + is_press = (event.type == KeyPress); /* move bindings */ for (i = 0; i < LENGTH(move_bindings); i++) { if (move_bindings[i].keysym == keysym) { - printf("move: %i, %i\n", move_bindings[i].x, move_bindings[i].y); - mouseinfo.move_x += move_bindings[i].x; - mouseinfo.move_y += move_bindings[i].y; + int sign = is_press ? 1 : -1; + mouseinfo.speed_x += sign * move_bindings[i].x; + mouseinfo.speed_y += sign * move_bindings[i].y; } } /* click bindings */ for (i = 0; i < LENGTH(click_bindings); i++) { if (click_bindings[i].keysym == keysym) { - printf("click: %i\n", click_bindings[i].button); - click(click_bindings[i].button, True); + click(click_bindings[i].button, is_press); + printf("click: %i %i\n", click_bindings[i].button, is_press); } } /* speed bindings */ for (i = 0; i < LENGTH(speed_bindings); i++) { if (speed_bindings[i].keysym == keysym) { - speed = speed_bindings[i].speed; + speed = is_press ? speed_bindings[i].speed : default_speed; printf("speed: %i\n", speed); } } -} - -void handle_keyrelease(XKeyEvent event) { - unsigned int i; - KeySym keysym; - keysym = XkbKeycodeToKeysym(dpy, event.keycode, - 0, event.state & ShiftMask ? 1 : 0); - - /* move bindings */ - for (i = 0; i < LENGTH(move_bindings); i++) { - if (move_bindings[i].keysym == keysym) { - mouseinfo.move_x -= move_bindings[i].x; - mouseinfo.move_y -= move_bindings[i].y; - } - } - - /* click bindings */ - for (i = 0; i < LENGTH(click_bindings); i++) { - if (click_bindings[i].keysym == keysym) { - printf("click release: %i\n", click_bindings[i].button); - click(click_bindings[i].button, False); - } - } - - /* speed bindings */ - for (i = 0; i < LENGTH(speed_bindings); i++) { - if (speed_bindings[i].keysym == keysym) { - speed = default_speed; - printf("speed: %i\n", speed); - } - } - - /* shell bindings */ - for (i = 0; i < LENGTH(shell_bindings); i++) { - if (shell_bindings[i].keysym == keysym) { - printf("executing: %s\n", shell_bindings[i].command); - if (fork() == 0) { - system(shell_bindings[i].command); - exit(EXIT_SUCCESS); + /* shell and exit bindings only on key release */ + if (!is_press) { + /* shell bindings */ + for (i = 0; i < LENGTH(shell_bindings); i++) { + if (shell_bindings[i].keysym == keysym) { + printf("executing: %s\n", shell_bindings[i].command); + if (fork() == 0) { + system(shell_bindings[i].command); + exit(EXIT_SUCCESS); + } } } - } - /* exit */ - for (i = 0; i < LENGTH(exit_keys); i++) { - if (exit_keys[i] == keysym) { - close_x(EXIT_SUCCESS); + /* exit */ + for (i = 0; i < LENGTH(exit_keys); i++) { + if (exit_keys[i] == keysym) { + close_x(EXIT_SUCCESS); + } } } } @@ -216,8 +188,8 @@ int main () { init_x(); getrootptr(&mouseinfo.x, &mouseinfo.y); - mouseinfo.move_x = 0; - mouseinfo.move_y = 0; + mouseinfo.speed_x = 0; + mouseinfo.speed_y = 0; // start the thread for mouse movement rc = pthread_create(&movethread, NULL, &moveforever, NULL); @@ -231,13 +203,9 @@ int main () { switch (event.type) { case KeyPress: - getrootptr(&mouseinfo.x, &mouseinfo.y); - handle_keypress(event.xkey); - break; - case KeyRelease: getrootptr(&mouseinfo.x, &mouseinfo.y); - handle_keyrelease(event.xkey); + handle_key(event.xkey); break; } } |