Skip to content

Instantly share code, notes, and snippets.

@mszczepanczyk
Created May 27, 2013 09:30
Show Gist options
  • Save mszczepanczyk/5656098 to your computer and use it in GitHub Desktop.
Save mszczepanczyk/5656098 to your computer and use it in GitHub Desktop.
ugly hack
// gcc grab.c -o grab -lX11
// ugly hack to catch windows key before application and change i3 workspace if windows+[0-9] is pressed
// usage: grab winid_1 winid_2 ...
// example:
// grab `xwininfo -root -tree -int |grep " - Oracle VM VirtualBox" | awk '{print $1}'`
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char **argv) {
int i, screen, status;
//XInitThreads();
Display *display;
Window root;
display = XOpenDisplay(NULL);
screen = DefaultScreen(display);
root = RootWindow(display, screen);
Window w;
printf("argc = %d\n", argc);
XEvent xev;
XKeyEvent event;
XKeyEvent *kev;
int keycode = 133;
unsigned int modifiers = AnyModifier;
int pointer_mode = GrabModeAsync;
int keyboard_mode = GrabModeAsync;
Bool owner_events = False;
for (i = 1; i < argc; i++) {
char *wstr = argv[i];
w = atoi(wstr);
printf("window = %d\n", w);
status = XGrabKey(display, keycode, modifiers, w, owner_events, pointer_mode, keyboard_mode);
printf("grabbing status %d\n", status);
}
while (1) {
XNextEvent(display, &xev);
kev = (XKeyEvent *)&xev;
printf("wind key: code = %d, state = %d \n", kev->keycode, kev->state);
if (kev->state == 64 && kev->keycode >= 10 && kev->keycode <= 19) {
int k = kev->keycode - 9;
if (k == 10) k = 0;
char cmd[100];
sprintf(cmd, "i3-msg \"workspace %d\"", k);
system(cmd);
}
}
XFlush(display);
XCloseDisplay(display);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment