From 45ff5f2ac7622f120b8037b2aab5babeb3af8d5a Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sat, 18 Jul 2020 00:04:02 +0530 Subject: [PATCH 1/2] Adds buttonpress event handler --- shotkey.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/shotkey.c b/shotkey.c index a8c5110..20d507b 100644 --- a/shotkey.c +++ b/shotkey.c @@ -44,6 +44,10 @@ void unbind_key(Display *dpy, Window win, unsigned int mod, KeySym key) { XUngrabKey(dpy, keycode, mod, win); } +void bind_mouse_button(Display *dpy, Window win, unsigned int mod, KeySym button) { + XGrabButton(dpy, button, mod, win, False, ButtonPress, GrabModeSync, GrabModeAsync, None, None); +} + int error_handler(Display *disp, XErrorEvent *xe) { switch(xe->error_code) { case BadAccess: @@ -152,6 +156,13 @@ void keypress(Display *dpy, Window win, XKeyEvent *ev) { } } +void buttonpress(Display* dpy, Window win, XButtonEvent* ev) { + printf("Muse click\n"); + + // Replay event + XAllowEvents(dpy, ReplayPointer, CurrentTime); +} + int main() { XSetErrorHandler(error_handler); @@ -165,7 +176,10 @@ int main() { bind_key(dpy, root, keys[i].mod, keys[i].key); } - XSelectInput(dpy, root, KeyPressMask); + bind_mouse_button(dpy, root, 0, Button1); + + long int event_mask = KeyPressMask | ButtonPressMask; + XSelectInput(dpy, root, event_mask); handle_mode_change(); @@ -173,13 +187,15 @@ int main() { XEvent ev; XSync(dpy, False); while (running) { - XMaskEvent(dpy, KeyPressMask, &ev); + XMaskEvent(dpy, event_mask, &ev); switch (ev.type) { - case KeyPress: { + case KeyPress: keypress(dpy, root, &ev.xkey); break; - } + case ButtonPress: + buttonpress(dpy, root, &ev.xbutton); + break; } } From b4382369119c2bb30087f330f9be43dc111d2642 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Fri, 24 Jul 2020 21:30:26 +0530 Subject: [PATCH 2/2] Adds mouse hotkeys --- config.def.h | 6 ++++++ shotkey.c | 27 ++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/config.def.h b/config.def.h index d93a341..4ab1775 100644 --- a/config.def.h +++ b/config.def.h @@ -34,6 +34,12 @@ Key keys[] = { { Mod1Mask, XF86XK_PowerOff, mode(Power, False) }, }; +Button buttons[] = { + { Button1Mask, Button3, cmd("notify-send wow_1") }, + { Mod1Mask, Button2, cmd("notify-send wow_2") }, + { Mod1Mask, Button3, cmd("notify-send wow_3") }, +}; + ModeProperties mode_properties[MODE_SIZE] = { [Music] = { "Music player" }, [Power] = { "Power menu" }, diff --git a/shotkey.c b/shotkey.c index 20d507b..a9c2da2 100644 --- a/shotkey.c +++ b/shotkey.c @@ -16,6 +16,12 @@ typedef struct Key { Command command; } Key; +typedef struct Button { + unsigned int mod; + unsigned int button; + Command command; +} Button; + typedef struct ModeProperties { char* label; } ModeProperties; @@ -28,7 +34,7 @@ typedef struct ModeProperties { #include "config.h" #define LENGTH(X) (sizeof X / sizeof X[0]) -#define CLEANMASK(mask) (mask & ~LockMask & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask)) +#define CLEANMASK(mask) (mask & ~LockMask & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask|Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask)) int current_mode = NormalMode; int is_mode_persistent = 0; @@ -44,7 +50,7 @@ void unbind_key(Display *dpy, Window win, unsigned int mod, KeySym key) { XUngrabKey(dpy, keycode, mod, win); } -void bind_mouse_button(Display *dpy, Window win, unsigned int mod, KeySym button) { +void bind_mouse_button(Display *dpy, Window win, unsigned int mod, unsigned int button) { XGrabButton(dpy, button, mod, win, False, ButtonPress, GrabModeSync, GrabModeAsync, None, None); } @@ -158,15 +164,23 @@ void keypress(Display *dpy, Window win, XKeyEvent *ev) { void buttonpress(Display* dpy, Window win, XButtonEvent* ev) { printf("Muse click\n"); + int i; + short int should_forward_event = False; + + for (i = 0; i < LENGTH(buttons); i++) { + if (buttons[i].button == ev->button && CLEANMASK(buttons[i].mod) == CLEANMASK(ev->state)) { + run(dpy, win, buttons[i].command); + } + } // Replay event - XAllowEvents(dpy, ReplayPointer, CurrentTime); + XAllowEvents(dpy, should_forward_event ? ReplayPointer : SyncPointer, CurrentTime); } int main() { XSetErrorHandler(error_handler); - int running = 1, i = 0; + int running = True, i = 0; Display *dpy = XOpenDisplay(0); Window root = DefaultRootWindow(dpy); @@ -176,7 +190,10 @@ int main() { bind_key(dpy, root, keys[i].mod, keys[i].key); } - bind_mouse_button(dpy, root, 0, Button1); + // Grab mouse buttons + for (i = 0; i < LENGTH(buttons); i++) { + bind_mouse_button(dpy, root, buttons[i].mod, buttons[i].button); + } long int event_mask = KeyPressMask | ButtonPressMask; XSelectInput(dpy, root, event_mask);