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 e573cb09 2020-09-27 op #include <stdio.h>
21 e573cb09 2020-09-27 op #include <string.h>
22 e573cb09 2020-09-27 op #include <unistd.h>
24 e573cb09 2020-09-27 op #include <X11/XKBlib.h>
25 e573cb09 2020-09-27 op #include <X11/Xutil.h>
27 e573cb09 2020-09-27 op #ifndef __OpenBSD__
28 e573cb09 2020-09-27 op # define pledge(a, b) 0
31 e573cb09 2020-09-27 op extern FILE *yyin;
34 e573cb09 2020-09-27 op extern int yylineno;
35 e573cb09 2020-09-27 op int yyparse(void);
37 e573cb09 2020-09-27 op struct group *config;
38 e573cb09 2020-09-27 op int goterror = 0;
42 e573cb09 2020-09-27 op int ignored_modifiers[] = {
43 ecc6fa05 2020-09-29 op 0, /* no modifiers */
44 e573cb09 2020-09-27 op LockMask, /* caps lock */
45 e573cb09 2020-09-27 op Mod2Mask, /* num lock */
46 e573cb09 2020-09-27 op Mod3Mask, /* scroll lock */
47 e573cb09 2020-09-27 op Mod5Mask, /* ? */
50 e573cb09 2020-09-27 op #define IGNORE_MODIFIERS (LockMask | Mod2Mask | Mod3Mask | Mod5Mask)
53 e573cb09 2020-09-27 op yyerror(const char *e)
56 e573cb09 2020-09-27 op fprintf(stderr, "%s:%d %s\n", fname, yylineno, e);
62 e573cb09 2020-09-27 op char *home, *xdg, *t;
63 e573cb09 2020-09-27 op int free_xdg = 0;
65 e573cb09 2020-09-27 op home = getenv("HOME");
66 e573cb09 2020-09-27 op xdg = getenv("XDG_CONFIG_HOME");
68 e573cb09 2020-09-27 op if (home == NULL && xdg == NULL)
71 e573cb09 2020-09-27 op /* file in home directory >>>>>> XDG shit */
72 e573cb09 2020-09-27 op if (home != NULL) {
73 e573cb09 2020-09-27 op if (asprintf(&t, "%s/.star-platinum.conf", home) == -1)
75 e573cb09 2020-09-27 op if (access(t, R_OK) == 0)
79 e573cb09 2020-09-27 op /* continue to search */
82 e573cb09 2020-09-27 op /* sanitize XDG_CONFIG_HOME */
83 e573cb09 2020-09-27 op if (xdg == NULL) {
85 e573cb09 2020-09-27 op if (asprintf(&xdg, "%s/.config", home) == -1)
89 e573cb09 2020-09-27 op if (asprintf(&t, "%s/star-platinum.conf", xdg) == -1)
92 e573cb09 2020-09-27 op if (access(t, R_OK) == 0)
104 e573cb09 2020-09-27 op main(int argc, char **argv)
106 e573cb09 2020-09-27 op int status = 0, dump_config = 0, conftest = 0, ch;
107 e573cb09 2020-09-27 op struct group *g;
108 e573cb09 2020-09-27 op struct rule *r;
112 e573cb09 2020-09-27 op fname = NULL;
113 e573cb09 2020-09-27 op while ((ch = getopt(argc, argv, "c:dn")) != -1) {
114 e573cb09 2020-09-27 op switch (ch) {
117 e573cb09 2020-09-27 op if ((fname = strdup(optarg)) == NULL)
118 e573cb09 2020-09-27 op err(1, "strdup");
122 e573cb09 2020-09-27 op dump_config = 1;
126 e573cb09 2020-09-27 op conftest = 1;
130 e573cb09 2020-09-27 op fprintf(stderr, "USAGE: %s [-dn] [-c conf]\n", *argv);
135 e573cb09 2020-09-27 op if (fname == NULL) {
136 e573cb09 2020-09-27 op if ((fname = find_config()) == NULL)
137 e573cb09 2020-09-27 op errx(1, "can't find a configuration file");
140 e573cb09 2020-09-27 op if ((yyin = fopen(fname, "r")) == NULL)
141 e573cb09 2020-09-27 op err(1, "cannot open %s", fname);
143 e573cb09 2020-09-27 op fclose(yyin);
146 e573cb09 2020-09-27 op fname = NULL;
148 e573cb09 2020-09-27 op if (goterror)
151 e573cb09 2020-09-27 op if (dump_config)
152 e573cb09 2020-09-27 op printgroup(config);
154 e573cb09 2020-09-27 op if (conftest) {
155 e573cb09 2020-09-27 op recfree_group(config);
159 e573cb09 2020-09-27 op if ((d = XOpenDisplay(NULL)) == NULL) {
160 e573cb09 2020-09-27 op recfree_group(config);
164 fe0dcf4e 2020-09-29 op XSetErrorHandler(&error_handler);
166 e573cb09 2020-09-27 op root = DefaultRootWindow(d);
168 e573cb09 2020-09-27 op /* grab all the keys */
169 e573cb09 2020-09-27 op for (g = config; g != NULL; g = g->next)
170 e573cb09 2020-09-27 op for (r = g->rules; r != NULL; r = r->next)
171 e573cb09 2020-09-27 op grabkey(r->key);
173 e573cb09 2020-09-27 op XSelectInput(d, root, KeyPressMask);
176 e573cb09 2020-09-27 op pledge("stdio", "");
179 e573cb09 2020-09-27 op XNextEvent(d, &e);
180 e573cb09 2020-09-27 op switch (e.type) {
181 e573cb09 2020-09-27 op case KeyRelease:
182 e573cb09 2020-09-27 op case KeyPress:
183 e573cb09 2020-09-27 op process_event(config, (XKeyEvent*)&e);
187 e573cb09 2020-09-27 op printf("Unknown event %d\n", e.type);
192 e573cb09 2020-09-27 op XCloseDisplay(d);
193 e573cb09 2020-09-27 op recfree_group(config);
194 e573cb09 2020-09-27 op return status;
201 fe0dcf4e 2020-09-29 op error_handler(Display *d, XErrorEvent *e)
203 fe0dcf4e 2020-09-29 op fprintf(stderr, "Xlib error %d\n", e->type);
207 e573cb09 2020-09-27 op /* TODO: it should grab ALL POSSIBLE COMBINATIONS of `ignored_modifiers`! */
209 e573cb09 2020-09-27 op grabkey(struct key k)
211 e573cb09 2020-09-27 op static size_t len = sizeof(ignored_modifiers)/sizeof(int);
215 e573cb09 2020-09-27 op root = DefaultRootWindow(d);
217 e573cb09 2020-09-27 op /* printf("Grabbing "); printkey(k); printf("\n"); */
218 e573cb09 2020-09-27 op for (i = 0; i < len; ++i) {
219 ecc6fa05 2020-09-29 op XGrabKey(d, XKeysymToKeycode(d, k.key),
220 e573cb09 2020-09-27 op k.modifier | ignored_modifiers[i],
221 e573cb09 2020-09-27 op root, False, GrabModeAsync, GrabModeAsync);
226 e573cb09 2020-09-27 op keycode_to_keysym(unsigned int kc)
228 e573cb09 2020-09-27 op /* group 0 (?). shift level is 0 because we don't want it*/
229 e573cb09 2020-09-27 op return XkbKeycodeToKeysym(d, kc, 0, 0);
233 e573cb09 2020-09-27 op focused_window()
236 e573cb09 2020-09-27 op int revert_to;
238 e573cb09 2020-09-27 op /* one can use (at least) three way to obtain the current
239 e573cb09 2020-09-27 op * focused window using xlib:
241 e573cb09 2020-09-27 op * - XQueryTree : you traverse tre tree until you find the
244 e573cb09 2020-09-27 op * - looking at _NET_ACTIVE_WINDOW in the root window, but
245 e573cb09 2020-09-27 op * depedns on the window manager to set it
247 e573cb09 2020-09-27 op * - using XGetInputFocus
249 e573cb09 2020-09-27 op * I don't know the pro/cons of these, but XGetInputFocus
250 e573cb09 2020-09-27 op * seems the easiest.
252 e573cb09 2020-09-27 op XGetInputFocus(d, &w, &revert_to);
257 e573cb09 2020-09-27 op send_fake(Window w, struct key k, int pressed)
261 e573cb09 2020-09-27 op e.type = pressed ? KeyPress : KeyRelease;
263 e573cb09 2020-09-27 op e.display = d;
264 e573cb09 2020-09-27 op e.window = w;
265 e573cb09 2020-09-27 op e.root = DefaultRootWindow(d);
266 e573cb09 2020-09-27 op e.subwindow = None;
267 e573cb09 2020-09-27 op e.time = CurrentTime;
269 e573cb09 2020-09-27 op /* TODO: fix these */
272 e573cb09 2020-09-27 op e.x_root = 1;
273 e573cb09 2020-09-27 op e.y_root = 1;
275 e573cb09 2020-09-27 op e.same_screen = True;
276 e573cb09 2020-09-27 op e.keycode = XKeysymToKeycode(d, k.key);
277 e573cb09 2020-09-27 op e.state = k.modifier;
279 e573cb09 2020-09-27 op XSendEvent(d, w, True, KeyPressMask, (XEvent*)&e);
284 e573cb09 2020-09-27 op window_match_class(Window w, const char *class)
286 e573cb09 2020-09-27 op XClassHint ch;
289 ecc6fa05 2020-09-29 op if (!XGetClassHint(d, w, &ch)) {
290 e573cb09 2020-09-27 op fprintf(stderr, "XGetClassHint failed\n");
294 e573cb09 2020-09-27 op matched = !strcmp(ch.res_class, class);
296 e573cb09 2020-09-27 op XFree(ch.res_name);
297 e573cb09 2020-09-27 op XFree(ch.res_class);
299 e573cb09 2020-09-27 op return matched;
306 e573cb09 2020-09-27 op do_action(struct action a, Window focused, int pressed)
308 e573cb09 2020-09-27 op switch (a.type) {
310 e573cb09 2020-09-27 op send_fake(focused, a.send_key, pressed);
313 e573cb09 2020-09-27 op case ASPECIAL:
314 e573cb09 2020-09-27 op switch (a.special) {
315 e573cb09 2020-09-27 op case ATOGGLE:
316 e573cb09 2020-09-27 op case AACTIVATE:
317 e573cb09 2020-09-27 op case ADEACTIVATE:
318 e573cb09 2020-09-27 op printf("TODO\n");
321 e573cb09 2020-09-27 op case AIGNORE:
325 e573cb09 2020-09-27 op /* unreachable */
331 e573cb09 2020-09-27 op /* unreachable */
339 e573cb09 2020-09-27 op struct match *
340 e573cb09 2020-09-27 op new_match(int prop, char *s)
342 e573cb09 2020-09-27 op struct match *m;
344 e573cb09 2020-09-27 op if ((m = calloc(1, sizeof(*m))) == NULL)
345 e573cb09 2020-09-27 op err(1, "calloc");
346 e573cb09 2020-09-27 op m->prop = prop;
352 e573cb09 2020-09-27 op recfree_match(struct match *m)
354 e573cb09 2020-09-27 op struct match *mt;
356 e573cb09 2020-09-27 op if (m == NULL)
359 e573cb09 2020-09-27 op mt = m->next;
360 e573cb09 2020-09-27 op free(m->str);
362 e573cb09 2020-09-27 op recfree_match(mt);
366 e573cb09 2020-09-27 op match_window(struct match *m, Window w)
368 ecc6fa05 2020-09-29 op switch (m->prop) {
373 e573cb09 2020-09-27 op return window_match_class(w, m->str);
377 e573cb09 2020-09-27 op /* unreachable */
385 e573cb09 2020-09-27 op struct rule *
386 e573cb09 2020-09-27 op new_rule(struct key k, struct action a)
388 e573cb09 2020-09-27 op struct rule *r;
390 e573cb09 2020-09-27 op if ((r = calloc(1, sizeof(*r))) == NULL)
391 e573cb09 2020-09-27 op err(1, "calloc");
392 e573cb09 2020-09-27 op memcpy(&r->key, &k, sizeof(k));
393 e573cb09 2020-09-27 op memcpy(&r->action, &a, sizeof(a));
398 e573cb09 2020-09-27 op recfree_rule(struct rule *r)
400 e573cb09 2020-09-27 op struct rule *rt;
402 e573cb09 2020-09-27 op if (r == NULL)
405 e573cb09 2020-09-27 op rt = r->next;
407 e573cb09 2020-09-27 op recfree_rule(rt);
411 e573cb09 2020-09-27 op rule_matched(struct rule *r, struct key k)
413 e573cb09 2020-09-27 op unsigned int m;
415 e573cb09 2020-09-27 op m = k.modifier;
416 e573cb09 2020-09-27 op m &= ~IGNORE_MODIFIERS; /* clear ignored modifiers */
418 e573cb09 2020-09-27 op return r->key.modifier == m
419 e573cb09 2020-09-27 op && r->key.key == k.key;
425 e573cb09 2020-09-27 op struct group *
426 e573cb09 2020-09-27 op new_group(struct match *matches, struct rule *rules)
428 e573cb09 2020-09-27 op struct group *g;
430 e573cb09 2020-09-27 op if ((g = calloc(1, sizeof(*g))) == NULL)
431 e573cb09 2020-09-27 op err(1, "calloc");
432 e573cb09 2020-09-27 op g->matches = matches;
433 e573cb09 2020-09-27 op g->rules = rules;
438 e573cb09 2020-09-27 op recfree_group(struct group *g)
440 e573cb09 2020-09-27 op struct group *gt;
442 e573cb09 2020-09-27 op if (g == NULL)
445 e573cb09 2020-09-27 op gt = g->next;
446 e573cb09 2020-09-27 op recfree_match(g->matches);
447 e573cb09 2020-09-27 op recfree_rule(g->rules);
449 e573cb09 2020-09-27 op recfree_group(gt);
453 e573cb09 2020-09-27 op process_event(struct group *g, XKeyEvent *e)
455 e573cb09 2020-09-27 op Window focused;
456 e573cb09 2020-09-27 op struct rule *r;
457 e573cb09 2020-09-27 op struct key pressed = {
458 e573cb09 2020-09-27 op .modifier = e->state,
459 e573cb09 2020-09-27 op .key = keycode_to_keysym(e->keycode),
462 e573cb09 2020-09-27 op focused = focused_window();
464 e573cb09 2020-09-27 op for (; g != NULL; g = g->next) {
465 e573cb09 2020-09-27 op if (!group_match(g, focused))
468 e573cb09 2020-09-27 op for (r = g->rules; r != NULL; r = r->next) {
469 e573cb09 2020-09-27 op if (rule_matched(r, pressed)) {
470 ecc6fa05 2020-09-29 op do_action(r->action, focused, e->type == KeyPress);
476 e573cb09 2020-09-27 op send_fake(focused, pressed, e->type == KeyPress);
480 e573cb09 2020-09-27 op group_match(struct group *g, Window w)
482 e573cb09 2020-09-27 op struct match *m;
484 e573cb09 2020-09-27 op for (m = g->matches; m != NULL; m = m->next)
485 e573cb09 2020-09-27 op if (match_window(m, w))
491 e573cb09 2020-09-27 op /* debug/dump stuff */
494 e573cb09 2020-09-27 op printkey(struct key k)
496 e573cb09 2020-09-27 op if (k.modifier & ControlMask)
497 e573cb09 2020-09-27 op printf("C-");
498 e573cb09 2020-09-27 op if (k.modifier & ShiftMask)
499 e573cb09 2020-09-27 op printf("S-");
500 e573cb09 2020-09-27 op if (k.modifier & Mod1Mask)
501 e573cb09 2020-09-27 op printf("M-");
502 e573cb09 2020-09-27 op if (k.modifier & Mod4Mask)
503 e573cb09 2020-09-27 op printf("s-");
505 e573cb09 2020-09-27 op printf("%s", XKeysymToString(k.key));
509 e573cb09 2020-09-27 op printaction(struct action a)
511 e573cb09 2020-09-27 op if (a.type == ASPECIAL) {
512 e573cb09 2020-09-27 op switch (a.special) {
513 e573cb09 2020-09-27 op case ATOGGLE: printf("toggle"); break;
514 e573cb09 2020-09-27 op case AACTIVATE: printf("activate"); break;
515 e573cb09 2020-09-27 op case ADEACTIVATE: printf("deactivate"); break;
516 e573cb09 2020-09-27 op case AIGNORE: printf("ignore"); break;
519 e573cb09 2020-09-27 op printf("send key ");
520 e573cb09 2020-09-27 op printkey(a.send_key);
525 e573cb09 2020-09-27 op printmatch(struct match *m)
527 e573cb09 2020-09-27 op if (m == NULL) {
528 e573cb09 2020-09-27 op printf("(null)");
531 e573cb09 2020-09-27 op printf("match ");
532 e573cb09 2020-09-27 op switch (m->prop) {
534 e573cb09 2020-09-27 op printf("class");
539 e573cb09 2020-09-27 op printf(" %s", m->str);
541 e573cb09 2020-09-27 op if (m->next == NULL)
542 e573cb09 2020-09-27 op printf("\n");
544 e573cb09 2020-09-27 op printf(" ; ");
545 e573cb09 2020-09-27 op printmatch(m->next);
550 e573cb09 2020-09-27 op printrule(struct rule *r)
552 e573cb09 2020-09-27 op if (r == NULL) {
553 e573cb09 2020-09-27 op printf("(null)");
556 e573cb09 2020-09-27 op printf("on ");
557 e573cb09 2020-09-27 op printkey(r->key);
558 e573cb09 2020-09-27 op printf(" do ");
559 e573cb09 2020-09-27 op printaction(r->action);
560 e573cb09 2020-09-27 op printf("\n");
562 e573cb09 2020-09-27 op if (r->next != NULL)
563 e573cb09 2020-09-27 op printrule(r->next);
567 e573cb09 2020-09-27 op printgroup(struct group *g)
569 e573cb09 2020-09-27 op if (g == NULL) {
570 e573cb09 2020-09-27 op printf("(null)");
573 e573cb09 2020-09-27 op printmatch(g->matches);
574 e573cb09 2020-09-27 op printf("\n");
575 e573cb09 2020-09-27 op printrule(g->rules);
576 e573cb09 2020-09-27 op printf("\n\n");
578 e573cb09 2020-09-27 op if (g->next != NULL)
579 e573cb09 2020-09-27 op printgroup(g->next);