2 * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "star-platinum.h"
26 #include <X11/XKBlib.h>
27 #include <X11/Xutil.h>
30 # define pledge(a, b) 0
44 int ignored_modifiers[] = {
46 LockMask, /* caps lock */
47 Mod2Mask, /* num lock */
48 Mod3Mask, /* scroll lock */
52 #define IGNORE_MODIFIERS (LockMask | Mod2Mask | Mod3Mask | Mod5Mask)
54 __attribute__((__format__ (__printf__, 1, 2)))
56 yyerror(const char *fmt, ...)
64 fprintf(stderr, "%s:%d ", fname, yylineno);
65 vfprintf(stderr, fmt, ap);
68 if (l > 0 && fmt[l-1] != '\n')
69 fprintf(stderr, "\n");
80 home = getenv("HOME");
81 xdg = getenv("XDG_CONFIG_HOME");
83 if (home == NULL && xdg == NULL)
86 /* file in home directory >>>>>> XDG shit */
88 if (asprintf(&t, "%s/.star-platinum.conf", home) == -1)
90 if (access(t, R_OK) == 0)
94 /* continue to search */
97 /* sanitize XDG_CONFIG_HOME */
100 if (asprintf(&xdg, "%s/.config", home) == -1)
104 if (asprintf(&t, "%s/star-platinum.conf", xdg) == -1)
107 if (access(t, R_OK) == 0)
119 main(int argc, char **argv)
121 int status = 0, dump_config = 0, conftest = 0, ch;
127 signal(SIGCHLD, SIG_IGN); /* don't allow zombies */
130 while ((ch = getopt(argc, argv, "c:dn")) != -1) {
134 if ((fname = strdup(optarg)) == NULL)
147 fprintf(stderr, "USAGE: %s [-dn] [-c conf]\n", *argv);
153 if ((fname = find_config()) == NULL)
154 errx(1, "can't find a configuration file");
157 if ((yyin = fopen(fname, "r")) == NULL)
158 err(1, "cannot open %s", fname);
172 recfree_group(config);
176 if ((d = XOpenDisplay(NULL)) == NULL) {
177 recfree_group(config);
181 XSetErrorHandler(&error_handler);
183 root = DefaultRootWindow(d);
185 /* grab all the keys */
186 for (g = config; g != NULL; g = g->next)
187 for (r = g->rules; r != NULL; r = r->next)
190 XSelectInput(d, root, KeyPressMask);
193 pledge("stdio proc exec", NULL);
200 process_event(config, (XKeyEvent*)&e);
204 printf("Unknown event %d\n", e.type);
210 recfree_group(config);
218 error_handler(Display *d, XErrorEvent *e)
220 fprintf(stderr, "Xlib error %d\n", e->type);
224 /* TODO: it should grab ALL POSSIBLE COMBINATIONS of `ignored_modifiers`! */
226 grabkey(struct key k)
228 static size_t len = sizeof(ignored_modifiers)/sizeof(int);
232 root = DefaultRootWindow(d);
234 /* printf("Grabbing "); printkey(k); printf("\n"); */
235 for (i = 0; i < len; ++i) {
236 XGrabKey(d, XKeysymToKeycode(d, k.key),
237 k.modifier | ignored_modifiers[i],
238 root, False, GrabModeAsync, GrabModeAsync);
243 keycode_to_keysym(unsigned int kc)
245 /* group 0 (?). shift level is 0 because we don't want it*/
246 return XkbKeycodeToKeysym(d, kc, 0, 0);
255 /* one can use (at least) three way to obtain the current
256 * focused window using xlib:
258 * - XQueryTree : you traverse tre tree until you find the
261 * - looking at _NET_ACTIVE_WINDOW in the root window, but
262 * depedns on the window manager to set it
264 * - using XGetInputFocus
266 * I don't know the pro/cons of these, but XGetInputFocus
269 XGetInputFocus(d, &w, &revert_to);
274 send_fake(Window w, struct key k, XKeyEvent *original)
279 * this needs to be hijacked. original->window is the root
280 * window (since we grabbed the key there) and we want to
281 * deliver the key to another window.
285 /* this is the fake key */
286 e.keycode = XKeysymToKeycode(d, k.key);
287 e.state = k.modifier;
289 /* the rest is just copying fields from the original event */
290 e.type = original->type;
291 e.display = original->display;
292 e.root = original->root;
293 e.subwindow = original->subwindow;
294 e.time = original->time;
295 e.same_screen = original->same_screen;
298 e.x_root = original->x_root;
299 e.y_root = original->y_root;
301 XSendEvent(d, w, True, KeyPressMask, (XEvent*)&e);
306 window_match_class(Window w, const char *class)
311 if (!XGetClassHint(d, w, &ch)) {
312 fprintf(stderr, "XGetClassHint failed\n");
316 matched = !strcmp(ch.res_class, class);
328 do_action(struct action a, Window focused, XKeyEvent *original)
332 send_fake(focused, a.send_key, original);
356 switch (p = fork()) {
361 if ((sh = getenv("SHELL")) == NULL)
363 printf("before exec'ing %s -c '%s'\n", sh, a.str);
364 execlp(sh, sh, "-c", a.str, NULL);
378 free_action(struct action a)
388 new_match(int prop, char *s)
392 if ((m = calloc(1, sizeof(*m))) == NULL)
400 recfree_match(struct match *m)
414 match_window(struct match *m, Window w)
421 return window_match_class(w, m->str);
434 new_rule(struct key k, struct action a)
438 if ((r = calloc(1, sizeof(*r))) == NULL)
440 memcpy(&r->key, &k, sizeof(k));
441 memcpy(&r->action, &a, sizeof(a));
446 recfree_rule(struct rule *r)
459 rule_matched(struct rule *r, struct key k)
464 m &= ~IGNORE_MODIFIERS; /* clear ignored modifiers */
466 return r->key.modifier == m
467 && r->key.key == k.key;
474 new_group(struct match *matches, struct rule *rules)
478 if ((g = calloc(1, sizeof(*g))) == NULL)
480 g->matches = matches;
486 recfree_group(struct group *g)
494 recfree_match(g->matches);
495 recfree_rule(g->rules);
501 process_event(struct group *g, XKeyEvent *e)
505 struct key pressed = {
506 .modifier = e->state,
507 .key = keycode_to_keysym(e->keycode),
510 focused = focused_window();
512 for (; g != NULL; g = g->next) {
513 if (!group_match(g, focused))
516 for (r = g->rules; r != NULL; r = r->next) {
517 if (rule_matched(r, pressed)) {
518 do_action(r->action, focused, e);
524 send_fake(focused, pressed, e);
528 group_match(struct group *g, Window w)
532 for (m = g->matches; m != NULL; m = m->next)
533 if (match_window(m, w))
539 /* debug/dump stuff */
542 printkey(struct key k)
544 if (k.modifier & ControlMask)
546 if (k.modifier & ShiftMask)
548 if (k.modifier & Mod1Mask)
550 if (k.modifier & Mod4Mask)
553 printf("%s", XKeysymToString(k.key));
557 printaction(struct action a)
562 case ATOGGLE: printf("toggle"); break;
563 case AACTIVATE: printf("activate"); break;
564 case ADEACTIVATE: printf("deactivate"); break;
565 case AIGNORE: printf("ignore"); break;
566 default: abort(); /* unreachable */
572 printkey(a.send_key);
576 printf("exec %s", a.str);
586 printmatch(struct match *m)
598 printf("class %s", m->str);
614 printrule(struct rule *r)
623 printaction(r->action);
631 printgroup(struct group *g)
637 printmatch(g->matches);