2 e573cb09 2020-09-27 op * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
4 e573cb09 2020-09-27 op * Permission to use, copy, modify, and distribute this software for any
5 e573cb09 2020-09-27 op * purpose with or without fee is hereby granted, provided that the above
6 e573cb09 2020-09-27 op * copyright notice and this permission notice appear in all copies.
8 e573cb09 2020-09-27 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 e573cb09 2020-09-27 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 e573cb09 2020-09-27 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 e573cb09 2020-09-27 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 e573cb09 2020-09-27 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 e573cb09 2020-09-27 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 e573cb09 2020-09-27 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 e573cb09 2020-09-27 op #include "star-platinum.h"
19 e573cb09 2020-09-27 op #include <err.h>
20 e0041ee5 2020-10-01 op #include <signal.h>
21 13cc0916 2020-09-30 op #include <stdarg.h>
22 e573cb09 2020-09-27 op #include <stdio.h>
23 e573cb09 2020-09-27 op #include <string.h>
24 e573cb09 2020-09-27 op #include <unistd.h>
26 e573cb09 2020-09-27 op #include <X11/XKBlib.h>
27 e573cb09 2020-09-27 op #include <X11/Xutil.h>
29 e573cb09 2020-09-27 op #ifndef __OpenBSD__
30 e573cb09 2020-09-27 op # define pledge(a, b) 0
33 e573cb09 2020-09-27 op extern FILE *yyin;
36 e573cb09 2020-09-27 op extern int yylineno;
37 e573cb09 2020-09-27 op int yyparse(void);
39 e573cb09 2020-09-27 op struct group *config;
40 e573cb09 2020-09-27 op int goterror = 0;
44 e573cb09 2020-09-27 op int ignored_modifiers[] = {
45 ecc6fa05 2020-09-29 op 0, /* no modifiers */
46 e573cb09 2020-09-27 op LockMask, /* caps lock */
47 e573cb09 2020-09-27 op Mod2Mask, /* num lock */
48 e573cb09 2020-09-27 op Mod3Mask, /* scroll lock */
49 e573cb09 2020-09-27 op Mod5Mask, /* ? */
52 e573cb09 2020-09-27 op #define IGNORE_MODIFIERS (LockMask | Mod2Mask | Mod3Mask | Mod5Mask)
54 13cc0916 2020-09-30 op __attribute__((__format__ (__printf__, 1, 2)))
56 13cc0916 2020-09-30 op yyerror(const char *fmt, ...)
61 13cc0916 2020-09-30 op va_start(ap, fmt);
64 13cc0916 2020-09-30 op fprintf(stderr, "%s:%d ", fname, yylineno);
65 13cc0916 2020-09-30 op vfprintf(stderr, fmt, ap);
67 13cc0916 2020-09-30 op l = strlen(fmt);
68 13cc0916 2020-09-30 op if (l > 0 && fmt[l-1] != '\n')
69 13cc0916 2020-09-30 op fprintf(stderr, "\n");
77 e573cb09 2020-09-27 op char *home, *xdg, *t;
78 e573cb09 2020-09-27 op int free_xdg = 0;
80 e573cb09 2020-09-27 op home = getenv("HOME");
81 e573cb09 2020-09-27 op xdg = getenv("XDG_CONFIG_HOME");
83 e573cb09 2020-09-27 op if (home == NULL && xdg == NULL)
86 e573cb09 2020-09-27 op /* file in home directory >>>>>> XDG shit */
87 e573cb09 2020-09-27 op if (home != NULL) {
88 e573cb09 2020-09-27 op if (asprintf(&t, "%s/.star-platinum.conf", home) == -1)
90 e573cb09 2020-09-27 op if (access(t, R_OK) == 0)
94 e573cb09 2020-09-27 op /* continue to search */
97 e573cb09 2020-09-27 op /* sanitize XDG_CONFIG_HOME */
98 e573cb09 2020-09-27 op if (xdg == NULL) {
100 e573cb09 2020-09-27 op if (asprintf(&xdg, "%s/.config", home) == -1)
104 e573cb09 2020-09-27 op if (asprintf(&t, "%s/star-platinum.conf", xdg) == -1)
107 e573cb09 2020-09-27 op if (access(t, R_OK) == 0)
112 e573cb09 2020-09-27 op if (free_xdg)
119 e573cb09 2020-09-27 op main(int argc, char **argv)
121 e573cb09 2020-09-27 op int status = 0, dump_config = 0, conftest = 0, ch;
125 e0041ee5 2020-10-01 op signal(SIGCHLD, SIG_IGN); /* don't allow zombies */
127 e573cb09 2020-09-27 op fname = NULL;
128 e573cb09 2020-09-27 op while ((ch = getopt(argc, argv, "c:dn")) != -1) {
129 e573cb09 2020-09-27 op switch (ch) {
132 e573cb09 2020-09-27 op if ((fname = strdup(optarg)) == NULL)
133 e573cb09 2020-09-27 op err(1, "strdup");
137 e573cb09 2020-09-27 op dump_config = 1;
141 e573cb09 2020-09-27 op conftest = 1;
145 e573cb09 2020-09-27 op fprintf(stderr, "USAGE: %s [-dn] [-c conf]\n", *argv);
150 e573cb09 2020-09-27 op if (fname == NULL) {
151 e573cb09 2020-09-27 op if ((fname = find_config()) == NULL)
152 e573cb09 2020-09-27 op errx(1, "can't find a configuration file");
155 e573cb09 2020-09-27 op if ((yyin = fopen(fname, "r")) == NULL)
156 e573cb09 2020-09-27 op err(1, "cannot open %s", fname);
158 e573cb09 2020-09-27 op fclose(yyin);
161 e573cb09 2020-09-27 op fname = NULL;
163 e573cb09 2020-09-27 op if (goterror)
166 e573cb09 2020-09-27 op if (dump_config)
167 e573cb09 2020-09-27 op printgroup(config);
169 e573cb09 2020-09-27 op if (conftest) {
170 e573cb09 2020-09-27 op recfree_group(config);
174 e573cb09 2020-09-27 op if ((d = XOpenDisplay(NULL)) == NULL) {
175 e573cb09 2020-09-27 op recfree_group(config);
179 fe0dcf4e 2020-09-29 op XSetErrorHandler(&error_handler);
181 e573cb09 2020-09-27 op root = DefaultRootWindow(d);
183 c21d6eaf 2020-10-08 op grabkey_matching_windows();
185 c21d6eaf 2020-10-08 op XSelectInput(d, root, SubstructureNotifyMask | KeyPressMask);
188 e0041ee5 2020-10-01 op pledge("stdio proc exec", NULL);
191 e573cb09 2020-09-27 op XNextEvent(d, &e);
192 e573cb09 2020-09-27 op switch (e.type) {
193 e573cb09 2020-09-27 op case KeyRelease:
194 e573cb09 2020-09-27 op case KeyPress:
195 e573cb09 2020-09-27 op process_event(config, (XKeyEvent*)&e);
198 c21d6eaf 2020-10-08 op case MapNotify: {
199 c21d6eaf 2020-10-08 op XMapEvent *ev = (XMapEvent*)&e;
200 c21d6eaf 2020-10-08 op grab_matching_keys(ev->window);
204 c21d6eaf 2020-10-08 op case UnmapNotify:
205 c21d6eaf 2020-10-08 op case ConfigureNotify:
206 c21d6eaf 2020-10-08 op case CreateNotify:
207 c21d6eaf 2020-10-08 op case DestroyNotify:
208 c21d6eaf 2020-10-08 op case ClientMessage:
209 c21d6eaf 2020-10-08 op /* ignored */
213 e573cb09 2020-09-27 op printf("Unknown event %d\n", e.type);
218 e573cb09 2020-09-27 op XCloseDisplay(d);
219 e573cb09 2020-09-27 op recfree_group(config);
220 e573cb09 2020-09-27 op return status;
227 fe0dcf4e 2020-09-29 op error_handler(Display *d, XErrorEvent *e)
229 fe0dcf4e 2020-09-29 op fprintf(stderr, "Xlib error %d\n", e->type);
233 e573cb09 2020-09-27 op /* TODO: it should grab ALL POSSIBLE COMBINATIONS of `ignored_modifiers`! */
235 c85d4bf3 2020-10-08 op grabkey(struct key k, Window w)
237 e573cb09 2020-09-27 op static size_t len = sizeof(ignored_modifiers)/sizeof(int);
240 e573cb09 2020-09-27 op /* printf("Grabbing "); printkey(k); printf("\n"); */
241 e573cb09 2020-09-27 op for (i = 0; i < len; ++i) {
242 ecc6fa05 2020-09-29 op XGrabKey(d, XKeysymToKeycode(d, k.key),
243 e573cb09 2020-09-27 op k.modifier | ignored_modifiers[i],
244 c85d4bf3 2020-10-08 op w, False, GrabModeAsync, GrabModeAsync);
249 c21d6eaf 2020-10-08 op grab_matching_keys(Window w)
251 c21d6eaf 2020-10-08 op XClassHint ch;
252 c21d6eaf 2020-10-08 op struct group *g;
253 c21d6eaf 2020-10-08 op struct rule *r;
255 c21d6eaf 2020-10-08 op if (!XGetClassHint(d, w, &ch))
258 c21d6eaf 2020-10-08 op for (g = config; g != NULL; g = g->next) {
259 c21d6eaf 2020-10-08 op if (!group_match(g, w))
262 c21d6eaf 2020-10-08 op for (r = g->rules; r != NULL; r = r->next)
263 c21d6eaf 2020-10-08 op grabkey(r->key, w);
268 c21d6eaf 2020-10-08 op grabkey_matching_windows()
270 c21d6eaf 2020-10-08 op Window root, parent, *children;
271 c21d6eaf 2020-10-08 op unsigned int len, i;
273 c21d6eaf 2020-10-08 op root = DefaultRootWindow(d);
274 c21d6eaf 2020-10-08 op if (!XQueryTree(d, root, &root, &parent, &children, &len))
277 c21d6eaf 2020-10-08 op for (i = 0; i < len; ++i)
278 c21d6eaf 2020-10-08 op grab_matching_keys(children[i]);
284 e573cb09 2020-09-27 op keycode_to_keysym(unsigned int kc)
286 e573cb09 2020-09-27 op /* group 0 (?). shift level is 0 because we don't want it*/
287 e573cb09 2020-09-27 op return XkbKeycodeToKeysym(d, kc, 0, 0);
291 e573cb09 2020-09-27 op focused_window()
294 e573cb09 2020-09-27 op int revert_to;
296 e573cb09 2020-09-27 op /* one can use (at least) three way to obtain the current
297 e573cb09 2020-09-27 op * focused window using xlib:
299 e573cb09 2020-09-27 op * - XQueryTree : you traverse tre tree until you find the
302 e573cb09 2020-09-27 op * - looking at _NET_ACTIVE_WINDOW in the root window, but
303 e573cb09 2020-09-27 op * depedns on the window manager to set it
305 e573cb09 2020-09-27 op * - using XGetInputFocus
307 e573cb09 2020-09-27 op * I don't know the pro/cons of these, but XGetInputFocus
308 e573cb09 2020-09-27 op * seems the easiest.
310 e573cb09 2020-09-27 op XGetInputFocus(d, &w, &revert_to);
315 2eb11d8e 2020-10-04 op send_fake(Window w, struct key k, XKeyEvent *original)
320 2eb11d8e 2020-10-04 op * this needs to be hijacked. original->window is the root
321 2eb11d8e 2020-10-04 op * window (since we grabbed the key there) and we want to
322 2eb11d8e 2020-10-04 op * deliver the key to another window.
324 2eb11d8e 2020-10-04 op e.window = w;
326 2eb11d8e 2020-10-04 op /* this is the fake key */
327 e573cb09 2020-09-27 op e.keycode = XKeysymToKeycode(d, k.key);
328 e573cb09 2020-09-27 op e.state = k.modifier;
330 2eb11d8e 2020-10-04 op /* the rest is just copying fields from the original event */
331 2eb11d8e 2020-10-04 op e.type = original->type;
332 2eb11d8e 2020-10-04 op e.display = original->display;
333 2eb11d8e 2020-10-04 op e.root = original->root;
334 2eb11d8e 2020-10-04 op e.subwindow = original->subwindow;
335 2eb11d8e 2020-10-04 op e.time = original->time;
336 2eb11d8e 2020-10-04 op e.same_screen = original->same_screen;
337 2eb11d8e 2020-10-04 op e.x = original->x;
338 2eb11d8e 2020-10-04 op e.y = original->y;
339 2eb11d8e 2020-10-04 op e.x_root = original->x_root;
340 2eb11d8e 2020-10-04 op e.y_root = original->y_root;
342 e573cb09 2020-09-27 op XSendEvent(d, w, True, KeyPressMask, (XEvent*)&e);
347 e573cb09 2020-09-27 op window_match_class(Window w, const char *class)
349 e573cb09 2020-09-27 op XClassHint ch;
352 ecc6fa05 2020-09-29 op if (!XGetClassHint(d, w, &ch)) {
353 e573cb09 2020-09-27 op fprintf(stderr, "XGetClassHint failed\n");
357 e573cb09 2020-09-27 op matched = !strcmp(ch.res_class, class);
359 e573cb09 2020-09-27 op XFree(ch.res_name);
360 e573cb09 2020-09-27 op XFree(ch.res_class);
362 e573cb09 2020-09-27 op return matched;
369 2eb11d8e 2020-10-04 op do_action(struct action a, Window focused, XKeyEvent *original)
371 e573cb09 2020-09-27 op switch (a.type) {
373 2eb11d8e 2020-10-04 op send_fake(focused, a.send_key, original);
376 e573cb09 2020-09-27 op case ASPECIAL:
377 e573cb09 2020-09-27 op switch (a.special) {
378 e573cb09 2020-09-27 op case ATOGGLE:
379 e573cb09 2020-09-27 op case AACTIVATE:
380 e573cb09 2020-09-27 op case ADEACTIVATE:
381 e573cb09 2020-09-27 op printf("TODO\n");
384 e573cb09 2020-09-27 op case AIGNORE:
388 e573cb09 2020-09-27 op /* unreachable */
393 e0041ee5 2020-10-01 op case AEXEC: {
395 e0041ee5 2020-10-01 op const char *sh;
397 c7063e0e 2020-10-04 op /* exec only on key press */
398 c7063e0e 2020-10-04 op if (original->type == KeyRelease)
401 e0041ee5 2020-10-01 op switch (p = fork()) {
403 e0041ee5 2020-10-01 op err(1, "fork");
406 e0041ee5 2020-10-01 op if ((sh = getenv("SHELL")) == NULL)
407 e0041ee5 2020-10-01 op sh = "/bin/sh";
408 e0041ee5 2020-10-01 op printf("before exec'ing %s -c '%s'\n", sh, a.str);
409 e0041ee5 2020-10-01 op execlp(sh, sh, "-c", a.str, NULL);
410 e0041ee5 2020-10-01 op err(1, "execlp");
417 e573cb09 2020-09-27 op /* unreachable */
423 e0041ee5 2020-10-01 op free_action(struct action a)
425 e0041ee5 2020-10-01 op if (a.type == AEXEC)
432 e573cb09 2020-09-27 op struct match *
433 e573cb09 2020-09-27 op new_match(int prop, char *s)
435 e573cb09 2020-09-27 op struct match *m;
437 e573cb09 2020-09-27 op if ((m = calloc(1, sizeof(*m))) == NULL)
438 e573cb09 2020-09-27 op err(1, "calloc");
439 e573cb09 2020-09-27 op m->prop = prop;
445 e573cb09 2020-09-27 op recfree_match(struct match *m)
447 e573cb09 2020-09-27 op struct match *mt;
449 e573cb09 2020-09-27 op if (m == NULL)
452 e573cb09 2020-09-27 op mt = m->next;
453 e573cb09 2020-09-27 op free(m->str);
455 e573cb09 2020-09-27 op recfree_match(mt);
459 e573cb09 2020-09-27 op match_window(struct match *m, Window w)
461 ecc6fa05 2020-09-29 op switch (m->prop) {
466 e573cb09 2020-09-27 op return window_match_class(w, m->str);
470 e573cb09 2020-09-27 op /* unreachable */
478 e573cb09 2020-09-27 op struct rule *
479 e573cb09 2020-09-27 op new_rule(struct key k, struct action a)
481 e573cb09 2020-09-27 op struct rule *r;
483 e573cb09 2020-09-27 op if ((r = calloc(1, sizeof(*r))) == NULL)
484 e573cb09 2020-09-27 op err(1, "calloc");
485 e573cb09 2020-09-27 op memcpy(&r->key, &k, sizeof(k));
486 e573cb09 2020-09-27 op memcpy(&r->action, &a, sizeof(a));
491 e573cb09 2020-09-27 op recfree_rule(struct rule *r)
493 e573cb09 2020-09-27 op struct rule *rt;
495 e573cb09 2020-09-27 op if (r == NULL)
498 e573cb09 2020-09-27 op rt = r->next;
500 e573cb09 2020-09-27 op recfree_rule(rt);
504 e573cb09 2020-09-27 op rule_matched(struct rule *r, struct key k)
506 e573cb09 2020-09-27 op unsigned int m;
508 e573cb09 2020-09-27 op m = k.modifier;
509 e573cb09 2020-09-27 op m &= ~IGNORE_MODIFIERS; /* clear ignored modifiers */
511 e573cb09 2020-09-27 op return r->key.modifier == m
512 e573cb09 2020-09-27 op && r->key.key == k.key;
518 e573cb09 2020-09-27 op struct group *
519 e573cb09 2020-09-27 op new_group(struct match *matches, struct rule *rules)
521 e573cb09 2020-09-27 op struct group *g;
523 e573cb09 2020-09-27 op if ((g = calloc(1, sizeof(*g))) == NULL)
524 e573cb09 2020-09-27 op err(1, "calloc");
525 e573cb09 2020-09-27 op g->matches = matches;
526 e573cb09 2020-09-27 op g->rules = rules;
531 e573cb09 2020-09-27 op recfree_group(struct group *g)
533 e573cb09 2020-09-27 op struct group *gt;
535 e573cb09 2020-09-27 op if (g == NULL)
538 e573cb09 2020-09-27 op gt = g->next;
539 e573cb09 2020-09-27 op recfree_match(g->matches);
540 e573cb09 2020-09-27 op recfree_rule(g->rules);
542 e573cb09 2020-09-27 op recfree_group(gt);
546 e573cb09 2020-09-27 op process_event(struct group *g, XKeyEvent *e)
548 e573cb09 2020-09-27 op Window focused;
549 e573cb09 2020-09-27 op struct rule *r;
550 e573cb09 2020-09-27 op struct key pressed = {
551 e573cb09 2020-09-27 op .modifier = e->state,
552 e573cb09 2020-09-27 op .key = keycode_to_keysym(e->keycode),
555 e573cb09 2020-09-27 op focused = focused_window();
557 e573cb09 2020-09-27 op for (; g != NULL; g = g->next) {
558 e573cb09 2020-09-27 op if (!group_match(g, focused))
561 e573cb09 2020-09-27 op for (r = g->rules; r != NULL; r = r->next) {
562 e573cb09 2020-09-27 op if (rule_matched(r, pressed)) {
563 2eb11d8e 2020-10-04 op do_action(r->action, focused, e);
569 2eb11d8e 2020-10-04 op send_fake(focused, pressed, e);
573 e573cb09 2020-09-27 op group_match(struct group *g, Window w)
575 e573cb09 2020-09-27 op struct match *m;
577 e573cb09 2020-09-27 op for (m = g->matches; m != NULL; m = m->next)
578 e573cb09 2020-09-27 op if (match_window(m, w))
584 e573cb09 2020-09-27 op /* debug/dump stuff */
587 e573cb09 2020-09-27 op printkey(struct key k)
589 e573cb09 2020-09-27 op if (k.modifier & ControlMask)
590 e573cb09 2020-09-27 op printf("C-");
591 e573cb09 2020-09-27 op if (k.modifier & ShiftMask)
592 e573cb09 2020-09-27 op printf("S-");
593 e573cb09 2020-09-27 op if (k.modifier & Mod1Mask)
594 e573cb09 2020-09-27 op printf("M-");
595 e573cb09 2020-09-27 op if (k.modifier & Mod4Mask)
596 e573cb09 2020-09-27 op printf("s-");
598 e573cb09 2020-09-27 op printf("%s", XKeysymToString(k.key));
602 e573cb09 2020-09-27 op printaction(struct action a)
604 e0041ee5 2020-10-01 op switch (a.type) {
605 e0041ee5 2020-10-01 op case ASPECIAL:
606 e573cb09 2020-09-27 op switch (a.special) {
607 e573cb09 2020-09-27 op case ATOGGLE: printf("toggle"); break;
608 e573cb09 2020-09-27 op case AACTIVATE: printf("activate"); break;
609 e573cb09 2020-09-27 op case ADEACTIVATE: printf("deactivate"); break;
610 e573cb09 2020-09-27 op case AIGNORE: printf("ignore"); break;
611 e0041ee5 2020-10-01 op default: abort(); /* unreachable */
616 e573cb09 2020-09-27 op printf("send key ");
617 e573cb09 2020-09-27 op printkey(a.send_key);
621 e0041ee5 2020-10-01 op printf("exec %s", a.str);
625 e0041ee5 2020-10-01 op /* unreachable */
631 e573cb09 2020-09-27 op printmatch(struct match *m)
633 e573cb09 2020-09-27 op if (m == NULL) {
634 e573cb09 2020-09-27 op printf("(null)");
637 e573cb09 2020-09-27 op printf("match ");
638 e573cb09 2020-09-27 op switch (m->prop) {
640 e0041ee5 2020-10-01 op printf("all");
643 e0041ee5 2020-10-01 op printf("class %s", m->str);
646 e0041ee5 2020-10-01 op /* unreachable */
650 e573cb09 2020-09-27 op if (m->next == NULL)
651 e573cb09 2020-09-27 op printf("\n");
653 e573cb09 2020-09-27 op printf(" ; ");
654 e573cb09 2020-09-27 op printmatch(m->next);
659 e573cb09 2020-09-27 op printrule(struct rule *r)
661 e573cb09 2020-09-27 op if (r == NULL) {
662 e573cb09 2020-09-27 op printf("(null)");
665 e573cb09 2020-09-27 op printf("on ");
666 e573cb09 2020-09-27 op printkey(r->key);
667 e573cb09 2020-09-27 op printf(" do ");
668 e573cb09 2020-09-27 op printaction(r->action);
669 e573cb09 2020-09-27 op printf("\n");
671 e573cb09 2020-09-27 op if (r->next != NULL)
672 e573cb09 2020-09-27 op printrule(r->next);
676 e573cb09 2020-09-27 op printgroup(struct group *g)
678 e573cb09 2020-09-27 op if (g == NULL) {
679 e573cb09 2020-09-27 op printf("(null)");
682 e573cb09 2020-09-27 op printmatch(g->matches);
683 e573cb09 2020-09-27 op printf("\n");
684 e573cb09 2020-09-27 op printrule(g->rules);
685 e573cb09 2020-09-27 op printf("\n\n");
687 e573cb09 2020-09-27 op if (g->next != NULL)
688 e573cb09 2020-09-27 op printgroup(g->next);