Blob


1 /*
2 * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include "star-platinum.h"
19 #include <err.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <unistd.h>
24 #include <X11/XKBlib.h>
25 #include <X11/Xutil.h>
27 #ifndef __OpenBSD__
28 # define pledge(a, b) 0
29 #endif
31 extern FILE *yyin;
32 char *fname;
34 extern int yylineno;
35 int yyparse(void);
37 struct group *config;
38 int goterror = 0;
40 Display *d;
42 int ignored_modifiers[] = {
43 0, /* no modifiers */
44 LockMask, /* caps lock */
45 Mod2Mask, /* num lock */
46 Mod3Mask, /* scroll lock */
47 Mod5Mask, /* ? */
48 };
50 #define IGNORE_MODIFIERS (LockMask | Mod2Mask | Mod3Mask | Mod5Mask)
52 void
53 yyerror(const char *e)
54 {
55 goterror = 1;
56 fprintf(stderr, "%s:%d %s\n", fname, yylineno, e);
57 }
59 char *
60 find_config()
61 {
62 char *home, *xdg, *t;
63 int free_xdg = 0;
65 home = getenv("HOME");
66 xdg = getenv("XDG_CONFIG_HOME");
68 if (home == NULL && xdg == NULL)
69 return NULL;
71 /* file in home directory >>>>>> XDG shit */
72 if (home != NULL) {
73 if (asprintf(&t, "%s/.star-platinum.conf", home) == -1)
74 return NULL;
75 if (access(t, R_OK) == 0)
76 return t;
78 free(t);
79 /* continue to search */
80 }
82 /* sanitize XDG_CONFIG_HOME */
83 if (xdg == NULL) {
84 free_xdg = 1;
85 if (asprintf(&xdg, "%s/.config", home) == -1)
86 return NULL;
87 }
89 if (asprintf(&t, "%s/star-platinum.conf", xdg) == -1)
90 goto err;
92 if (access(t, R_OK) == 0)
93 return t;
94 free(t);
96 err:
97 if (free_xdg)
98 free(xdg);
100 return NULL;
103 int
104 main(int argc, char **argv)
106 int status = 0, dump_config = 0, conftest = 0, ch;
107 struct group *g;
108 struct rule *r;
109 XEvent e;
110 Window root;
112 fname = NULL;
113 while ((ch = getopt(argc, argv, "c:dn")) != -1) {
114 switch (ch) {
115 case 'c':
116 free(fname);
117 if ((fname = strdup(optarg)) == NULL)
118 err(1, "strdup");
119 break;
121 case 'd':
122 dump_config = 1;
123 break;
125 case 'n':
126 conftest = 1;
127 break;
129 default:
130 fprintf(stderr, "USAGE: %s [-dn] [-c conf]\n", *argv);
131 return 1;
135 if (fname == NULL) {
136 if ((fname = find_config()) == NULL)
137 errx(1, "can't find a configuration file");
140 if ((yyin = fopen(fname, "r")) == NULL)
141 err(1, "cannot open %s", fname);
142 yyparse();
143 fclose(yyin);
145 free(fname);
146 fname = NULL;
148 if (goterror)
149 return 1;
151 if (dump_config)
152 printgroup(config);
154 if (conftest) {
155 recfree_group(config);
156 return 0;
159 if ((d = XOpenDisplay(NULL)) == NULL) {
160 recfree_group(config);
161 return 1;
164 root = DefaultRootWindow(d);
166 /* grab all the keys */
167 for (g = config; g != NULL; g = g->next)
168 for (r = g->rules; r != NULL; r = r->next)
169 grabkey(r->key);
171 XSelectInput(d, root, KeyPressMask);
172 XFlush(d);
174 pledge("stdio", "");
176 while (1) {
177 XNextEvent(d, &e);
178 switch (e.type) {
179 case KeyRelease:
180 case KeyPress:
181 process_event(config, (XKeyEvent*)&e);
182 break;
184 default:
185 printf("Unknown event %d\n", e.type);
186 break;
190 XCloseDisplay(d);
191 recfree_group(config);
192 return status;
196 /* xlib */
198 /* TODO: it should grab ALL POSSIBLE COMBINATIONS of `ignored_modifiers`! */
199 void
200 grabkey(struct key k)
202 static size_t len = sizeof(ignored_modifiers)/sizeof(int);
203 size_t i;
204 Window root;
206 root = DefaultRootWindow(d);
208 /* printf("Grabbing "); printkey(k); printf("\n"); */
209 for (i = 0; i < len; ++i) {
210 XGrabKey(d, XKeysymToKeycode(d, k.key),
211 k.modifier | ignored_modifiers[i],
212 root, False, GrabModeAsync, GrabModeAsync);
216 KeySym
217 keycode_to_keysym(unsigned int kc)
219 /* group 0 (?). shift level is 0 because we don't want it*/
220 return XkbKeycodeToKeysym(d, kc, 0, 0);
223 Window
224 focused_window()
226 Window w;
227 int revert_to;
229 /* one can use (at least) three way to obtain the current
230 * focused window using xlib:
232 * - XQueryTree : you traverse tre tree until you find the
233 * window
235 * - looking at _NET_ACTIVE_WINDOW in the root window, but
236 * depedns on the window manager to set it
238 * - using XGetInputFocus
240 * I don't know the pro/cons of these, but XGetInputFocus
241 * seems the easiest.
242 */
243 XGetInputFocus(d, &w, &revert_to);
244 return w;
247 void
248 send_fake(Window w, struct key k, int pressed)
250 XKeyEvent e;
252 e.type = pressed ? KeyPress : KeyRelease;
254 e.display = d;
255 e.window = w;
256 e.root = DefaultRootWindow(d);
257 e.subwindow = None;
258 e.time = CurrentTime;
260 /* TODO: fix these */
261 e.x = 1;
262 e.y = 1;
263 e.x_root = 1;
264 e.y_root = 1;
266 e.same_screen = True;
267 e.keycode = XKeysymToKeycode(d, k.key);
268 e.state = k.modifier;
270 XSendEvent(d, w, True, KeyPressMask, (XEvent*)&e);
271 XFlush(d);
274 int
275 window_match_class(Window w, const char *class)
277 XClassHint ch;
278 int matched;
280 if (!XGetClassHint(d, w, &ch)) {
281 fprintf(stderr, "XGetClassHint failed\n");
282 return 0;
285 matched = !strcmp(ch.res_class, class);
287 XFree(ch.res_name);
288 XFree(ch.res_class);
290 return matched;
294 /* action */
296 void
297 do_action(struct action a, Window focused, int pressed)
299 switch (a.type) {
300 case AFAKE:
301 send_fake(focused, a.send_key, pressed);
302 break;
304 case ASPECIAL:
305 switch (a.special) {
306 case ATOGGLE:
307 case AACTIVATE:
308 case ADEACTIVATE:
309 printf("TODO\n");
310 break;
312 case AIGNORE:
313 break;
315 default:
316 /* unreachable */
317 abort();
319 break;
321 default:
322 /* unreachable */
323 abort();
328 /* match */
330 struct match *
331 new_match(int prop, char *s)
333 struct match *m;
335 if ((m = calloc(1, sizeof(*m))) == NULL)
336 err(1, "calloc");
337 m->prop = prop;
338 m->str = s;
339 return m;
342 void
343 recfree_match(struct match *m)
345 struct match *mt;
347 if (m == NULL)
348 return;
350 mt = m->next;
351 free(m->str);
352 free(m);
353 recfree_match(mt);
356 int
357 match_window(struct match *m, Window w)
359 switch (m->prop) {
360 case MCLASS:
361 return window_match_class(w, m->str);
362 break;
364 default:
365 /* unreachable */
366 abort();
371 /* rule */
373 struct rule *
374 new_rule(struct key k, struct action a)
376 struct rule *r;
378 if ((r = calloc(1, sizeof(*r))) == NULL)
379 err(1, "calloc");
380 memcpy(&r->key, &k, sizeof(k));
381 memcpy(&r->action, &a, sizeof(a));
382 return r;
385 void
386 recfree_rule(struct rule *r)
388 struct rule *rt;
390 if (r == NULL)
391 return;
393 rt = r->next;
394 free(r);
395 recfree_rule(rt);
398 int
399 rule_matched(struct rule *r, struct key k)
401 unsigned int m;
403 m = k.modifier;
404 m &= ~IGNORE_MODIFIERS; /* clear ignored modifiers */
406 return r->key.modifier == m
407 && r->key.key == k.key;
411 /* group */
413 struct group *
414 new_group(struct match *matches, struct rule *rules)
416 struct group *g;
418 if ((g = calloc(1, sizeof(*g))) == NULL)
419 err(1, "calloc");
420 g->matches = matches;
421 g->rules = rules;
422 return g;
425 void
426 recfree_group(struct group *g)
428 struct group *gt;
430 if (g == NULL)
431 return;
433 gt = g->next;
434 recfree_match(g->matches);
435 recfree_rule(g->rules);
436 free(g);
437 recfree_group(gt);
440 void
441 process_event(struct group *g, XKeyEvent *e)
443 Window focused;
444 struct rule *r;
445 struct key pressed = {
446 .modifier = e->state,
447 .key = keycode_to_keysym(e->keycode),
448 };
450 focused = focused_window();
452 for (; g != NULL; g = g->next) {
453 if (!group_match(g, focused))
454 continue;
456 for (r = g->rules; r != NULL; r = r->next) {
457 if (rule_matched(r, pressed)) {
458 do_action(r->action, focused, e->type == KeyPress);
459 return;
464 send_fake(focused, pressed, e->type == KeyPress);
467 int
468 group_match(struct group *g, Window w)
470 struct match *m;
472 for (m = g->matches; m != NULL; m = m->next)
473 if (match_window(m, w))
474 return 1;
475 return 0;
479 /* debug/dump stuff */
481 void
482 printkey(struct key k)
484 if (k.modifier & ControlMask)
485 printf("C-");
486 if (k.modifier & ShiftMask)
487 printf("S-");
488 if (k.modifier & Mod1Mask)
489 printf("M-");
490 if (k.modifier & Mod4Mask)
491 printf("s-");
493 printf("%s", XKeysymToString(k.key));
496 void
497 printaction(struct action a)
499 if (a.type == ASPECIAL) {
500 switch (a.special) {
501 case ATOGGLE: printf("toggle"); break;
502 case AACTIVATE: printf("activate"); break;
503 case ADEACTIVATE: printf("deactivate"); break;
504 case AIGNORE: printf("ignore"); break;
506 } else {
507 printf("send key ");
508 printkey(a.send_key);
512 void
513 printmatch(struct match *m)
515 if (m == NULL) {
516 printf("(null)");
517 return;
519 printf("match ");
520 switch (m->prop) {
521 case MCLASS:
522 printf("class");
523 break;
524 default:
525 abort();
527 printf(" %s", m->str);
529 if (m->next == NULL)
530 printf("\n");
531 else {
532 printf(" ; ");
533 printmatch(m->next);
537 void
538 printrule(struct rule *r)
540 if (r == NULL) {
541 printf("(null)");
542 return;
544 printf("on ");
545 printkey(r->key);
546 printf(" do ");
547 printaction(r->action);
548 printf("\n");
550 if (r->next != NULL)
551 printrule(r->next);
554 void
555 printgroup(struct group *g)
557 if (g == NULL) {
558 printf("(null)");
559 return;
561 printmatch(g->matches);
562 printf("\n");
563 printrule(g->rules);
564 printf("\n\n");
566 if (g->next != NULL)
567 printgroup(g->next);