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 13cc0916 2020-09-30 op #include <stdarg.h>
21 e573cb09 2020-09-27 op #include <stdio.h>
22 e573cb09 2020-09-27 op #include <string.h>
23 e573cb09 2020-09-27 op #include <unistd.h>
25 e573cb09 2020-09-27 op #include <X11/XKBlib.h>
26 e573cb09 2020-09-27 op #include <X11/Xutil.h>
28 e573cb09 2020-09-27 op #ifndef __OpenBSD__
29 e573cb09 2020-09-27 op # define pledge(a, b) 0
32 e573cb09 2020-09-27 op extern FILE *yyin;
35 e573cb09 2020-09-27 op extern int yylineno;
36 e573cb09 2020-09-27 op int yyparse(void);
38 e573cb09 2020-09-27 op struct group *config;
39 e573cb09 2020-09-27 op int goterror = 0;
43 e573cb09 2020-09-27 op int ignored_modifiers[] = {
44 ecc6fa05 2020-09-29 op 0, /* no modifiers */
45 e573cb09 2020-09-27 op LockMask, /* caps lock */
46 e573cb09 2020-09-27 op Mod2Mask, /* num lock */
47 e573cb09 2020-09-27 op Mod3Mask, /* scroll lock */
48 e573cb09 2020-09-27 op Mod5Mask, /* ? */
51 e573cb09 2020-09-27 op #define IGNORE_MODIFIERS (LockMask | Mod2Mask | Mod3Mask | Mod5Mask)
53 13cc0916 2020-09-30 op __attribute__((__format__ (__printf__, 1, 2)))
55 13cc0916 2020-09-30 op yyerror(const char *fmt, ...)
60 13cc0916 2020-09-30 op va_start(ap, fmt);
63 13cc0916 2020-09-30 op fprintf(stderr, "%s:%d ", fname, yylineno);
64 13cc0916 2020-09-30 op vfprintf(stderr, fmt, ap);
66 13cc0916 2020-09-30 op l = strlen(fmt);
67 13cc0916 2020-09-30 op if (l > 0 && fmt[l-1] != '\n')
68 13cc0916 2020-09-30 op fprintf(stderr, "\n");
76 e573cb09 2020-09-27 op char *home, *xdg, *t;
77 e573cb09 2020-09-27 op int free_xdg = 0;
79 e573cb09 2020-09-27 op home = getenv("HOME");
80 e573cb09 2020-09-27 op xdg = getenv("XDG_CONFIG_HOME");
82 e573cb09 2020-09-27 op if (home == NULL && xdg == NULL)
85 e573cb09 2020-09-27 op /* file in home directory >>>>>> XDG shit */
86 e573cb09 2020-09-27 op if (home != NULL) {
87 e573cb09 2020-09-27 op if (asprintf(&t, "%s/.star-platinum.conf", home) == -1)
89 e573cb09 2020-09-27 op if (access(t, R_OK) == 0)
93 e573cb09 2020-09-27 op /* continue to search */
96 e573cb09 2020-09-27 op /* sanitize XDG_CONFIG_HOME */
97 e573cb09 2020-09-27 op if (xdg == NULL) {
99 e573cb09 2020-09-27 op if (asprintf(&xdg, "%s/.config", home) == -1)
103 e573cb09 2020-09-27 op if (asprintf(&t, "%s/star-platinum.conf", xdg) == -1)
106 e573cb09 2020-09-27 op if (access(t, R_OK) == 0)
111 e573cb09 2020-09-27 op if (free_xdg)
118 e573cb09 2020-09-27 op main(int argc, char **argv)
120 e573cb09 2020-09-27 op int status = 0, dump_config = 0, conftest = 0, ch;
121 e573cb09 2020-09-27 op struct group *g;
122 e573cb09 2020-09-27 op struct rule *r;
126 e573cb09 2020-09-27 op fname = NULL;
127 e573cb09 2020-09-27 op while ((ch = getopt(argc, argv, "c:dn")) != -1) {
128 e573cb09 2020-09-27 op switch (ch) {
131 e573cb09 2020-09-27 op if ((fname = strdup(optarg)) == NULL)
132 e573cb09 2020-09-27 op err(1, "strdup");
136 e573cb09 2020-09-27 op dump_config = 1;
140 e573cb09 2020-09-27 op conftest = 1;
144 e573cb09 2020-09-27 op fprintf(stderr, "USAGE: %s [-dn] [-c conf]\n", *argv);
149 e573cb09 2020-09-27 op if (fname == NULL) {
150 e573cb09 2020-09-27 op if ((fname = find_config()) == NULL)
151 e573cb09 2020-09-27 op errx(1, "can't find a configuration file");
154 e573cb09 2020-09-27 op if ((yyin = fopen(fname, "r")) == NULL)
155 e573cb09 2020-09-27 op err(1, "cannot open %s", fname);
157 e573cb09 2020-09-27 op fclose(yyin);
160 e573cb09 2020-09-27 op fname = NULL;
162 e573cb09 2020-09-27 op if (goterror)
165 e573cb09 2020-09-27 op if (dump_config)
166 e573cb09 2020-09-27 op printgroup(config);
168 e573cb09 2020-09-27 op if (conftest) {
169 e573cb09 2020-09-27 op recfree_group(config);
173 e573cb09 2020-09-27 op if ((d = XOpenDisplay(NULL)) == NULL) {
174 e573cb09 2020-09-27 op recfree_group(config);
178 fe0dcf4e 2020-09-29 op XSetErrorHandler(&error_handler);
180 e573cb09 2020-09-27 op root = DefaultRootWindow(d);
182 e573cb09 2020-09-27 op /* grab all the keys */
183 e573cb09 2020-09-27 op for (g = config; g != NULL; g = g->next)
184 e573cb09 2020-09-27 op for (r = g->rules; r != NULL; r = r->next)
185 e573cb09 2020-09-27 op grabkey(r->key);
187 e573cb09 2020-09-27 op XSelectInput(d, root, KeyPressMask);
190 e573cb09 2020-09-27 op pledge("stdio", "");
193 e573cb09 2020-09-27 op XNextEvent(d, &e);
194 e573cb09 2020-09-27 op switch (e.type) {
195 e573cb09 2020-09-27 op case KeyRelease:
196 e573cb09 2020-09-27 op case KeyPress:
197 e573cb09 2020-09-27 op process_event(config, (XKeyEvent*)&e);
201 e573cb09 2020-09-27 op printf("Unknown event %d\n", e.type);
206 e573cb09 2020-09-27 op XCloseDisplay(d);
207 e573cb09 2020-09-27 op recfree_group(config);
208 e573cb09 2020-09-27 op return status;
215 fe0dcf4e 2020-09-29 op error_handler(Display *d, XErrorEvent *e)
217 fe0dcf4e 2020-09-29 op fprintf(stderr, "Xlib error %d\n", e->type);
221 e573cb09 2020-09-27 op /* TODO: it should grab ALL POSSIBLE COMBINATIONS of `ignored_modifiers`! */
223 e573cb09 2020-09-27 op grabkey(struct key k)
225 e573cb09 2020-09-27 op static size_t len = sizeof(ignored_modifiers)/sizeof(int);
229 e573cb09 2020-09-27 op root = DefaultRootWindow(d);
231 e573cb09 2020-09-27 op /* printf("Grabbing "); printkey(k); printf("\n"); */
232 e573cb09 2020-09-27 op for (i = 0; i < len; ++i) {
233 ecc6fa05 2020-09-29 op XGrabKey(d, XKeysymToKeycode(d, k.key),
234 e573cb09 2020-09-27 op k.modifier | ignored_modifiers[i],
235 e573cb09 2020-09-27 op root, False, GrabModeAsync, GrabModeAsync);
240 e573cb09 2020-09-27 op keycode_to_keysym(unsigned int kc)
242 e573cb09 2020-09-27 op /* group 0 (?). shift level is 0 because we don't want it*/
243 e573cb09 2020-09-27 op return XkbKeycodeToKeysym(d, kc, 0, 0);
247 e573cb09 2020-09-27 op focused_window()
250 e573cb09 2020-09-27 op int revert_to;
252 e573cb09 2020-09-27 op /* one can use (at least) three way to obtain the current
253 e573cb09 2020-09-27 op * focused window using xlib:
255 e573cb09 2020-09-27 op * - XQueryTree : you traverse tre tree until you find the
258 e573cb09 2020-09-27 op * - looking at _NET_ACTIVE_WINDOW in the root window, but
259 e573cb09 2020-09-27 op * depedns on the window manager to set it
261 e573cb09 2020-09-27 op * - using XGetInputFocus
263 e573cb09 2020-09-27 op * I don't know the pro/cons of these, but XGetInputFocus
264 e573cb09 2020-09-27 op * seems the easiest.
266 e573cb09 2020-09-27 op XGetInputFocus(d, &w, &revert_to);
271 e573cb09 2020-09-27 op send_fake(Window w, struct key k, int pressed)
275 e573cb09 2020-09-27 op e.type = pressed ? KeyPress : KeyRelease;
277 e573cb09 2020-09-27 op e.display = d;
278 e573cb09 2020-09-27 op e.window = w;
279 e573cb09 2020-09-27 op e.root = DefaultRootWindow(d);
280 e573cb09 2020-09-27 op e.subwindow = None;
281 e573cb09 2020-09-27 op e.time = CurrentTime;
283 e573cb09 2020-09-27 op /* TODO: fix these */
286 e573cb09 2020-09-27 op e.x_root = 1;
287 e573cb09 2020-09-27 op e.y_root = 1;
289 e573cb09 2020-09-27 op e.same_screen = True;
290 e573cb09 2020-09-27 op e.keycode = XKeysymToKeycode(d, k.key);
291 e573cb09 2020-09-27 op e.state = k.modifier;
293 e573cb09 2020-09-27 op XSendEvent(d, w, True, KeyPressMask, (XEvent*)&e);
298 e573cb09 2020-09-27 op window_match_class(Window w, const char *class)
300 e573cb09 2020-09-27 op XClassHint ch;
303 ecc6fa05 2020-09-29 op if (!XGetClassHint(d, w, &ch)) {
304 e573cb09 2020-09-27 op fprintf(stderr, "XGetClassHint failed\n");
308 e573cb09 2020-09-27 op matched = !strcmp(ch.res_class, class);
310 e573cb09 2020-09-27 op XFree(ch.res_name);
311 e573cb09 2020-09-27 op XFree(ch.res_class);
313 e573cb09 2020-09-27 op return matched;
320 e573cb09 2020-09-27 op do_action(struct action a, Window focused, int pressed)
322 e573cb09 2020-09-27 op switch (a.type) {
324 e573cb09 2020-09-27 op send_fake(focused, a.send_key, pressed);
327 e573cb09 2020-09-27 op case ASPECIAL:
328 e573cb09 2020-09-27 op switch (a.special) {
329 e573cb09 2020-09-27 op case ATOGGLE:
330 e573cb09 2020-09-27 op case AACTIVATE:
331 e573cb09 2020-09-27 op case ADEACTIVATE:
332 e573cb09 2020-09-27 op printf("TODO\n");
335 e573cb09 2020-09-27 op case AIGNORE:
339 e573cb09 2020-09-27 op /* unreachable */
345 e573cb09 2020-09-27 op /* unreachable */
353 e573cb09 2020-09-27 op struct match *
354 e573cb09 2020-09-27 op new_match(int prop, char *s)
356 e573cb09 2020-09-27 op struct match *m;
358 e573cb09 2020-09-27 op if ((m = calloc(1, sizeof(*m))) == NULL)
359 e573cb09 2020-09-27 op err(1, "calloc");
360 e573cb09 2020-09-27 op m->prop = prop;
366 e573cb09 2020-09-27 op recfree_match(struct match *m)
368 e573cb09 2020-09-27 op struct match *mt;
370 e573cb09 2020-09-27 op if (m == NULL)
373 e573cb09 2020-09-27 op mt = m->next;
374 e573cb09 2020-09-27 op free(m->str);
376 e573cb09 2020-09-27 op recfree_match(mt);
380 e573cb09 2020-09-27 op match_window(struct match *m, Window w)
382 ecc6fa05 2020-09-29 op switch (m->prop) {
387 e573cb09 2020-09-27 op return window_match_class(w, m->str);
391 e573cb09 2020-09-27 op /* unreachable */
399 e573cb09 2020-09-27 op struct rule *
400 e573cb09 2020-09-27 op new_rule(struct key k, struct action a)
402 e573cb09 2020-09-27 op struct rule *r;
404 e573cb09 2020-09-27 op if ((r = calloc(1, sizeof(*r))) == NULL)
405 e573cb09 2020-09-27 op err(1, "calloc");
406 e573cb09 2020-09-27 op memcpy(&r->key, &k, sizeof(k));
407 e573cb09 2020-09-27 op memcpy(&r->action, &a, sizeof(a));
412 e573cb09 2020-09-27 op recfree_rule(struct rule *r)
414 e573cb09 2020-09-27 op struct rule *rt;
416 e573cb09 2020-09-27 op if (r == NULL)
419 e573cb09 2020-09-27 op rt = r->next;
421 e573cb09 2020-09-27 op recfree_rule(rt);
425 e573cb09 2020-09-27 op rule_matched(struct rule *r, struct key k)
427 e573cb09 2020-09-27 op unsigned int m;
429 e573cb09 2020-09-27 op m = k.modifier;
430 e573cb09 2020-09-27 op m &= ~IGNORE_MODIFIERS; /* clear ignored modifiers */
432 e573cb09 2020-09-27 op return r->key.modifier == m
433 e573cb09 2020-09-27 op && r->key.key == k.key;
439 e573cb09 2020-09-27 op struct group *
440 e573cb09 2020-09-27 op new_group(struct match *matches, struct rule *rules)
442 e573cb09 2020-09-27 op struct group *g;
444 e573cb09 2020-09-27 op if ((g = calloc(1, sizeof(*g))) == NULL)
445 e573cb09 2020-09-27 op err(1, "calloc");
446 e573cb09 2020-09-27 op g->matches = matches;
447 e573cb09 2020-09-27 op g->rules = rules;
452 e573cb09 2020-09-27 op recfree_group(struct group *g)
454 e573cb09 2020-09-27 op struct group *gt;
456 e573cb09 2020-09-27 op if (g == NULL)
459 e573cb09 2020-09-27 op gt = g->next;
460 e573cb09 2020-09-27 op recfree_match(g->matches);
461 e573cb09 2020-09-27 op recfree_rule(g->rules);
463 e573cb09 2020-09-27 op recfree_group(gt);
467 e573cb09 2020-09-27 op process_event(struct group *g, XKeyEvent *e)
469 e573cb09 2020-09-27 op Window focused;
470 e573cb09 2020-09-27 op struct rule *r;
471 e573cb09 2020-09-27 op struct key pressed = {
472 e573cb09 2020-09-27 op .modifier = e->state,
473 e573cb09 2020-09-27 op .key = keycode_to_keysym(e->keycode),
476 e573cb09 2020-09-27 op focused = focused_window();
478 e573cb09 2020-09-27 op for (; g != NULL; g = g->next) {
479 e573cb09 2020-09-27 op if (!group_match(g, focused))
482 e573cb09 2020-09-27 op for (r = g->rules; r != NULL; r = r->next) {
483 e573cb09 2020-09-27 op if (rule_matched(r, pressed)) {
484 ecc6fa05 2020-09-29 op do_action(r->action, focused, e->type == KeyPress);
490 e573cb09 2020-09-27 op send_fake(focused, pressed, e->type == KeyPress);
494 e573cb09 2020-09-27 op group_match(struct group *g, Window w)
496 e573cb09 2020-09-27 op struct match *m;
498 e573cb09 2020-09-27 op for (m = g->matches; m != NULL; m = m->next)
499 e573cb09 2020-09-27 op if (match_window(m, w))
505 e573cb09 2020-09-27 op /* debug/dump stuff */
508 e573cb09 2020-09-27 op printkey(struct key k)
510 e573cb09 2020-09-27 op if (k.modifier & ControlMask)
511 e573cb09 2020-09-27 op printf("C-");
512 e573cb09 2020-09-27 op if (k.modifier & ShiftMask)
513 e573cb09 2020-09-27 op printf("S-");
514 e573cb09 2020-09-27 op if (k.modifier & Mod1Mask)
515 e573cb09 2020-09-27 op printf("M-");
516 e573cb09 2020-09-27 op if (k.modifier & Mod4Mask)
517 e573cb09 2020-09-27 op printf("s-");
519 e573cb09 2020-09-27 op printf("%s", XKeysymToString(k.key));
523 e573cb09 2020-09-27 op printaction(struct action a)
525 e573cb09 2020-09-27 op if (a.type == ASPECIAL) {
526 e573cb09 2020-09-27 op switch (a.special) {
527 e573cb09 2020-09-27 op case ATOGGLE: printf("toggle"); break;
528 e573cb09 2020-09-27 op case AACTIVATE: printf("activate"); break;
529 e573cb09 2020-09-27 op case ADEACTIVATE: printf("deactivate"); break;
530 e573cb09 2020-09-27 op case AIGNORE: printf("ignore"); break;
533 e573cb09 2020-09-27 op printf("send key ");
534 e573cb09 2020-09-27 op printkey(a.send_key);
539 e573cb09 2020-09-27 op printmatch(struct match *m)
541 e573cb09 2020-09-27 op if (m == NULL) {
542 e573cb09 2020-09-27 op printf("(null)");
545 e573cb09 2020-09-27 op printf("match ");
546 e573cb09 2020-09-27 op switch (m->prop) {
548 e573cb09 2020-09-27 op printf("class");
553 e573cb09 2020-09-27 op printf(" %s", m->str);
555 e573cb09 2020-09-27 op if (m->next == NULL)
556 e573cb09 2020-09-27 op printf("\n");
558 e573cb09 2020-09-27 op printf(" ; ");
559 e573cb09 2020-09-27 op printmatch(m->next);
564 e573cb09 2020-09-27 op printrule(struct rule *r)
566 e573cb09 2020-09-27 op if (r == NULL) {
567 e573cb09 2020-09-27 op printf("(null)");
570 e573cb09 2020-09-27 op printf("on ");
571 e573cb09 2020-09-27 op printkey(r->key);
572 e573cb09 2020-09-27 op printf(" do ");
573 e573cb09 2020-09-27 op printaction(r->action);
574 e573cb09 2020-09-27 op printf("\n");
576 e573cb09 2020-09-27 op if (r->next != NULL)
577 e573cb09 2020-09-27 op printrule(r->next);
581 e573cb09 2020-09-27 op printgroup(struct group *g)
583 e573cb09 2020-09-27 op if (g == NULL) {
584 e573cb09 2020-09-27 op printf("(null)");
587 e573cb09 2020-09-27 op printmatch(g->matches);
588 e573cb09 2020-09-27 op printf("\n");
589 e573cb09 2020-09-27 op printrule(g->rules);
590 e573cb09 2020-09-27 op printf("\n\n");
592 e573cb09 2020-09-27 op if (g->next != NULL)
593 e573cb09 2020-09-27 op printgroup(g->next);