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;
122 e573cb09 2020-09-27 op struct group *g;
123 e573cb09 2020-09-27 op struct rule *r;
127 e0041ee5 2020-10-01 op signal(SIGCHLD, SIG_IGN); /* don't allow zombies */
129 e573cb09 2020-09-27 op fname = NULL;
130 e573cb09 2020-09-27 op while ((ch = getopt(argc, argv, "c:dn")) != -1) {
131 e573cb09 2020-09-27 op switch (ch) {
134 e573cb09 2020-09-27 op if ((fname = strdup(optarg)) == NULL)
135 e573cb09 2020-09-27 op err(1, "strdup");
139 e573cb09 2020-09-27 op dump_config = 1;
143 e573cb09 2020-09-27 op conftest = 1;
147 e573cb09 2020-09-27 op fprintf(stderr, "USAGE: %s [-dn] [-c conf]\n", *argv);
152 e573cb09 2020-09-27 op if (fname == NULL) {
153 e573cb09 2020-09-27 op if ((fname = find_config()) == NULL)
154 e573cb09 2020-09-27 op errx(1, "can't find a configuration file");
157 e573cb09 2020-09-27 op if ((yyin = fopen(fname, "r")) == NULL)
158 e573cb09 2020-09-27 op err(1, "cannot open %s", fname);
160 e573cb09 2020-09-27 op fclose(yyin);
163 e573cb09 2020-09-27 op fname = NULL;
165 e573cb09 2020-09-27 op if (goterror)
168 e573cb09 2020-09-27 op if (dump_config)
169 e573cb09 2020-09-27 op printgroup(config);
171 e573cb09 2020-09-27 op if (conftest) {
172 e573cb09 2020-09-27 op recfree_group(config);
176 e573cb09 2020-09-27 op if ((d = XOpenDisplay(NULL)) == NULL) {
177 e573cb09 2020-09-27 op recfree_group(config);
181 fe0dcf4e 2020-09-29 op XSetErrorHandler(&error_handler);
183 e573cb09 2020-09-27 op root = DefaultRootWindow(d);
185 e573cb09 2020-09-27 op /* grab all the keys */
186 e573cb09 2020-09-27 op for (g = config; g != NULL; g = g->next)
187 e573cb09 2020-09-27 op for (r = g->rules; r != NULL; r = r->next)
188 e573cb09 2020-09-27 op grabkey(r->key);
190 e573cb09 2020-09-27 op XSelectInput(d, root, KeyPressMask);
193 e0041ee5 2020-10-01 op pledge("stdio proc exec", NULL);
196 e573cb09 2020-09-27 op XNextEvent(d, &e);
197 e573cb09 2020-09-27 op switch (e.type) {
198 e573cb09 2020-09-27 op case KeyRelease:
199 e573cb09 2020-09-27 op case KeyPress:
200 e573cb09 2020-09-27 op process_event(config, (XKeyEvent*)&e);
204 e573cb09 2020-09-27 op printf("Unknown event %d\n", e.type);
209 e573cb09 2020-09-27 op XCloseDisplay(d);
210 e573cb09 2020-09-27 op recfree_group(config);
211 e573cb09 2020-09-27 op return status;
218 fe0dcf4e 2020-09-29 op error_handler(Display *d, XErrorEvent *e)
220 fe0dcf4e 2020-09-29 op fprintf(stderr, "Xlib error %d\n", e->type);
224 e573cb09 2020-09-27 op /* TODO: it should grab ALL POSSIBLE COMBINATIONS of `ignored_modifiers`! */
226 e573cb09 2020-09-27 op grabkey(struct key k)
228 e573cb09 2020-09-27 op static size_t len = sizeof(ignored_modifiers)/sizeof(int);
232 e573cb09 2020-09-27 op root = DefaultRootWindow(d);
234 e573cb09 2020-09-27 op /* printf("Grabbing "); printkey(k); printf("\n"); */
235 e573cb09 2020-09-27 op for (i = 0; i < len; ++i) {
236 ecc6fa05 2020-09-29 op XGrabKey(d, XKeysymToKeycode(d, k.key),
237 e573cb09 2020-09-27 op k.modifier | ignored_modifiers[i],
238 e573cb09 2020-09-27 op root, False, GrabModeAsync, GrabModeAsync);
243 e573cb09 2020-09-27 op keycode_to_keysym(unsigned int kc)
245 e573cb09 2020-09-27 op /* group 0 (?). shift level is 0 because we don't want it*/
246 e573cb09 2020-09-27 op return XkbKeycodeToKeysym(d, kc, 0, 0);
250 e573cb09 2020-09-27 op focused_window()
253 e573cb09 2020-09-27 op int revert_to;
255 e573cb09 2020-09-27 op /* one can use (at least) three way to obtain the current
256 e573cb09 2020-09-27 op * focused window using xlib:
258 e573cb09 2020-09-27 op * - XQueryTree : you traverse tre tree until you find the
261 e573cb09 2020-09-27 op * - looking at _NET_ACTIVE_WINDOW in the root window, but
262 e573cb09 2020-09-27 op * depedns on the window manager to set it
264 e573cb09 2020-09-27 op * - using XGetInputFocus
266 e573cb09 2020-09-27 op * I don't know the pro/cons of these, but XGetInputFocus
267 e573cb09 2020-09-27 op * seems the easiest.
269 e573cb09 2020-09-27 op XGetInputFocus(d, &w, &revert_to);
274 e573cb09 2020-09-27 op send_fake(Window w, struct key k, int pressed)
278 e573cb09 2020-09-27 op e.type = pressed ? KeyPress : KeyRelease;
280 e573cb09 2020-09-27 op e.display = d;
281 e573cb09 2020-09-27 op e.window = w;
282 e573cb09 2020-09-27 op e.root = DefaultRootWindow(d);
283 e573cb09 2020-09-27 op e.subwindow = None;
284 e573cb09 2020-09-27 op e.time = CurrentTime;
286 e573cb09 2020-09-27 op /* TODO: fix these */
289 e573cb09 2020-09-27 op e.x_root = 1;
290 e573cb09 2020-09-27 op e.y_root = 1;
292 e573cb09 2020-09-27 op e.same_screen = True;
293 e573cb09 2020-09-27 op e.keycode = XKeysymToKeycode(d, k.key);
294 e573cb09 2020-09-27 op e.state = k.modifier;
296 e573cb09 2020-09-27 op XSendEvent(d, w, True, KeyPressMask, (XEvent*)&e);
301 e573cb09 2020-09-27 op window_match_class(Window w, const char *class)
303 e573cb09 2020-09-27 op XClassHint ch;
306 ecc6fa05 2020-09-29 op if (!XGetClassHint(d, w, &ch)) {
307 e573cb09 2020-09-27 op fprintf(stderr, "XGetClassHint failed\n");
311 e573cb09 2020-09-27 op matched = !strcmp(ch.res_class, class);
313 e573cb09 2020-09-27 op XFree(ch.res_name);
314 e573cb09 2020-09-27 op XFree(ch.res_class);
316 e573cb09 2020-09-27 op return matched;
323 e573cb09 2020-09-27 op do_action(struct action a, Window focused, int pressed)
325 e573cb09 2020-09-27 op switch (a.type) {
327 e573cb09 2020-09-27 op send_fake(focused, a.send_key, pressed);
330 e573cb09 2020-09-27 op case ASPECIAL:
331 e573cb09 2020-09-27 op switch (a.special) {
332 e573cb09 2020-09-27 op case ATOGGLE:
333 e573cb09 2020-09-27 op case AACTIVATE:
334 e573cb09 2020-09-27 op case ADEACTIVATE:
335 e573cb09 2020-09-27 op printf("TODO\n");
338 e573cb09 2020-09-27 op case AIGNORE:
342 e573cb09 2020-09-27 op /* unreachable */
347 e0041ee5 2020-10-01 op case AEXEC: {
349 e0041ee5 2020-10-01 op const char *sh;
351 e0041ee5 2020-10-01 op switch (p = fork()) {
353 e0041ee5 2020-10-01 op err(1, "fork");
356 e0041ee5 2020-10-01 op if ((sh = getenv("SHELL")) == NULL)
357 e0041ee5 2020-10-01 op sh = "/bin/sh";
358 e0041ee5 2020-10-01 op printf("before exec'ing %s -c '%s'\n", sh, a.str);
359 e0041ee5 2020-10-01 op execlp(sh, sh, "-c", a.str, NULL);
360 e0041ee5 2020-10-01 op err(1, "execlp");
367 e573cb09 2020-09-27 op /* unreachable */
373 e0041ee5 2020-10-01 op free_action(struct action a)
375 e0041ee5 2020-10-01 op if (a.type == AEXEC)
382 e573cb09 2020-09-27 op struct match *
383 e573cb09 2020-09-27 op new_match(int prop, char *s)
385 e573cb09 2020-09-27 op struct match *m;
387 e573cb09 2020-09-27 op if ((m = calloc(1, sizeof(*m))) == NULL)
388 e573cb09 2020-09-27 op err(1, "calloc");
389 e573cb09 2020-09-27 op m->prop = prop;
395 e573cb09 2020-09-27 op recfree_match(struct match *m)
397 e573cb09 2020-09-27 op struct match *mt;
399 e573cb09 2020-09-27 op if (m == NULL)
402 e573cb09 2020-09-27 op mt = m->next;
403 e573cb09 2020-09-27 op free(m->str);
405 e573cb09 2020-09-27 op recfree_match(mt);
409 e573cb09 2020-09-27 op match_window(struct match *m, Window w)
411 ecc6fa05 2020-09-29 op switch (m->prop) {
416 e573cb09 2020-09-27 op return window_match_class(w, m->str);
420 e573cb09 2020-09-27 op /* unreachable */
428 e573cb09 2020-09-27 op struct rule *
429 e573cb09 2020-09-27 op new_rule(struct key k, struct action a)
431 e573cb09 2020-09-27 op struct rule *r;
433 e573cb09 2020-09-27 op if ((r = calloc(1, sizeof(*r))) == NULL)
434 e573cb09 2020-09-27 op err(1, "calloc");
435 e573cb09 2020-09-27 op memcpy(&r->key, &k, sizeof(k));
436 e573cb09 2020-09-27 op memcpy(&r->action, &a, sizeof(a));
441 e573cb09 2020-09-27 op recfree_rule(struct rule *r)
443 e573cb09 2020-09-27 op struct rule *rt;
445 e573cb09 2020-09-27 op if (r == NULL)
448 e573cb09 2020-09-27 op rt = r->next;
450 e573cb09 2020-09-27 op recfree_rule(rt);
454 e573cb09 2020-09-27 op rule_matched(struct rule *r, struct key k)
456 e573cb09 2020-09-27 op unsigned int m;
458 e573cb09 2020-09-27 op m = k.modifier;
459 e573cb09 2020-09-27 op m &= ~IGNORE_MODIFIERS; /* clear ignored modifiers */
461 e573cb09 2020-09-27 op return r->key.modifier == m
462 e573cb09 2020-09-27 op && r->key.key == k.key;
468 e573cb09 2020-09-27 op struct group *
469 e573cb09 2020-09-27 op new_group(struct match *matches, struct rule *rules)
471 e573cb09 2020-09-27 op struct group *g;
473 e573cb09 2020-09-27 op if ((g = calloc(1, sizeof(*g))) == NULL)
474 e573cb09 2020-09-27 op err(1, "calloc");
475 e573cb09 2020-09-27 op g->matches = matches;
476 e573cb09 2020-09-27 op g->rules = rules;
481 e573cb09 2020-09-27 op recfree_group(struct group *g)
483 e573cb09 2020-09-27 op struct group *gt;
485 e573cb09 2020-09-27 op if (g == NULL)
488 e573cb09 2020-09-27 op gt = g->next;
489 e573cb09 2020-09-27 op recfree_match(g->matches);
490 e573cb09 2020-09-27 op recfree_rule(g->rules);
492 e573cb09 2020-09-27 op recfree_group(gt);
496 e573cb09 2020-09-27 op process_event(struct group *g, XKeyEvent *e)
498 e573cb09 2020-09-27 op Window focused;
499 e573cb09 2020-09-27 op struct rule *r;
500 e573cb09 2020-09-27 op struct key pressed = {
501 e573cb09 2020-09-27 op .modifier = e->state,
502 e573cb09 2020-09-27 op .key = keycode_to_keysym(e->keycode),
505 e573cb09 2020-09-27 op focused = focused_window();
507 e573cb09 2020-09-27 op for (; g != NULL; g = g->next) {
508 e573cb09 2020-09-27 op if (!group_match(g, focused))
511 e573cb09 2020-09-27 op for (r = g->rules; r != NULL; r = r->next) {
512 e573cb09 2020-09-27 op if (rule_matched(r, pressed)) {
513 ecc6fa05 2020-09-29 op do_action(r->action, focused, e->type == KeyPress);
519 e573cb09 2020-09-27 op send_fake(focused, pressed, e->type == KeyPress);
523 e573cb09 2020-09-27 op group_match(struct group *g, Window w)
525 e573cb09 2020-09-27 op struct match *m;
527 e573cb09 2020-09-27 op for (m = g->matches; m != NULL; m = m->next)
528 e573cb09 2020-09-27 op if (match_window(m, w))
534 e573cb09 2020-09-27 op /* debug/dump stuff */
537 e573cb09 2020-09-27 op printkey(struct key k)
539 e573cb09 2020-09-27 op if (k.modifier & ControlMask)
540 e573cb09 2020-09-27 op printf("C-");
541 e573cb09 2020-09-27 op if (k.modifier & ShiftMask)
542 e573cb09 2020-09-27 op printf("S-");
543 e573cb09 2020-09-27 op if (k.modifier & Mod1Mask)
544 e573cb09 2020-09-27 op printf("M-");
545 e573cb09 2020-09-27 op if (k.modifier & Mod4Mask)
546 e573cb09 2020-09-27 op printf("s-");
548 e573cb09 2020-09-27 op printf("%s", XKeysymToString(k.key));
552 e573cb09 2020-09-27 op printaction(struct action a)
554 e0041ee5 2020-10-01 op switch (a.type) {
555 e0041ee5 2020-10-01 op case ASPECIAL:
556 e573cb09 2020-09-27 op switch (a.special) {
557 e573cb09 2020-09-27 op case ATOGGLE: printf("toggle"); break;
558 e573cb09 2020-09-27 op case AACTIVATE: printf("activate"); break;
559 e573cb09 2020-09-27 op case ADEACTIVATE: printf("deactivate"); break;
560 e573cb09 2020-09-27 op case AIGNORE: printf("ignore"); break;
561 e0041ee5 2020-10-01 op default: abort(); /* unreachable */
566 e573cb09 2020-09-27 op printf("send key ");
567 e573cb09 2020-09-27 op printkey(a.send_key);
571 e0041ee5 2020-10-01 op printf("exec %s", a.str);
575 e0041ee5 2020-10-01 op /* unreachable */
581 e573cb09 2020-09-27 op printmatch(struct match *m)
583 e573cb09 2020-09-27 op if (m == NULL) {
584 e573cb09 2020-09-27 op printf("(null)");
587 e573cb09 2020-09-27 op printf("match ");
588 e573cb09 2020-09-27 op switch (m->prop) {
590 e0041ee5 2020-10-01 op printf("all");
593 e0041ee5 2020-10-01 op printf("class %s", m->str);
596 e0041ee5 2020-10-01 op /* unreachable */
600 e573cb09 2020-09-27 op if (m->next == NULL)
601 e573cb09 2020-09-27 op printf("\n");
603 e573cb09 2020-09-27 op printf(" ; ");
604 e573cb09 2020-09-27 op printmatch(m->next);
609 e573cb09 2020-09-27 op printrule(struct rule *r)
611 e573cb09 2020-09-27 op if (r == NULL) {
612 e573cb09 2020-09-27 op printf("(null)");
615 e573cb09 2020-09-27 op printf("on ");
616 e573cb09 2020-09-27 op printkey(r->key);
617 e573cb09 2020-09-27 op printf(" do ");
618 e573cb09 2020-09-27 op printaction(r->action);
619 e573cb09 2020-09-27 op printf("\n");
621 e573cb09 2020-09-27 op if (r->next != NULL)
622 e573cb09 2020-09-27 op printrule(r->next);
626 e573cb09 2020-09-27 op printgroup(struct group *g)
628 e573cb09 2020-09-27 op if (g == NULL) {
629 e573cb09 2020-09-27 op printf("(null)");
632 e573cb09 2020-09-27 op printmatch(g->matches);
633 e573cb09 2020-09-27 op printf("\n");
634 e573cb09 2020-09-27 op printrule(g->rules);
635 e573cb09 2020-09-27 op printf("\n\n");
637 e573cb09 2020-09-27 op if (g->next != NULL)
638 e573cb09 2020-09-27 op printgroup(g->next);