Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Support mouse hotkeys #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config.def.h
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
45 changes: 39 additions & 6 deletions shotkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -44,6 +50,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, unsigned int 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:
Expand Down Expand Up @@ -152,10 +162,25 @@ 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, 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);
Expand All @@ -165,21 +190,29 @@ int main() {
bind_key(dpy, root, keys[i].mod, keys[i].key);
}

XSelectInput(dpy, root, KeyPressMask);
// 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);

handle_mode_change();

/* main event loop */
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;
}
}

Expand Down