2 * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
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.
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.
17 #include "star-platinum.h"
26 #include <X11/XKBlib.h>
27 #include <X11/Xutil.h>
30 # define pledge(a, b) 0
44 int ignored_modifiers[] = {
46 LockMask, /* caps lock */
47 Mod2Mask, /* num lock */
48 Mod3Mask, /* scroll lock */
52 #define IGNORE_MODIFIERS (LockMask | Mod2Mask | Mod3Mask | Mod5Mask)
54 __attribute__((__format__ (__printf__, 1, 2)))
56 yyerror(const char *fmt, ...)
64 fprintf(stderr, "%s:%d ", fname, yylineno);
65 vfprintf(stderr, fmt, ap);
68 if (l > 0 && fmt[l-1] != '\n')
69 fprintf(stderr, "\n");
80 home = getenv("HOME");
81 xdg = getenv("XDG_CONFIG_HOME");
83 if (home == NULL && xdg == NULL)
86 /* file in home directory >>>>>> XDG shit */
88 if (asprintf(&t, "%s/.star-platinum.conf", home) == -1)
90 if (access(t, R_OK) == 0)
94 /* continue to search */
97 /* sanitize XDG_CONFIG_HOME */
100 if (asprintf(&xdg, "%s/.config", home) == -1)
104 if (asprintf(&t, "%s/star-platinum.conf", xdg) == -1)
107 if (access(t, R_OK) == 0)
119 main(int argc, char **argv)
121 int status = 0, dump_config = 0, conftest = 0, ch;
127 signal(SIGCHLD, SIG_IGN); /* don't allow zombies */
130 while ((ch = getopt(argc, argv, "c:dn")) != -1) {
134 if ((fname = strdup(optarg)) == NULL)
147 fprintf(stderr, "USAGE: %s [-dn] [-c conf]\n", *argv);
153 if ((fname = find_config()) == NULL)
154 errx(1, "can't find a configuration file");
157 if ((yyin = fopen(fname, "r")) == NULL)
158 err(1, "cannot open %s", fname);
172 recfree_group(config);
176 if ((d = XOpenDisplay(NULL)) == NULL) {
177 recfree_group(config);
181 XSetErrorHandler(&error_handler);
183 root = DefaultRootWindow(d);
185 /* grab all the keys */
186 for (g = config; g != NULL; g = g->next)
187 for (r = g->rules; r != NULL; r = r->next)
190 XSelectInput(d, root, KeyPressMask);
193 pledge("stdio proc exec", NULL);
200 process_event(config, (XKeyEvent*)&e);
204 printf("Unknown event %d\n", e.type);
210 recfree_group(config);
218 error_handler(Display *d, XErrorEvent *e)
220 fprintf(stderr, "Xlib error %d\n", e->type);
224 /* TODO: it should grab ALL POSSIBLE COMBINATIONS of `ignored_modifiers`! */
226 grabkey(struct key k)
228 static size_t len = sizeof(ignored_modifiers)/sizeof(int);
232 root = DefaultRootWindow(d);
234 /* printf("Grabbing "); printkey(k); printf("\n"); */
235 for (i = 0; i < len; ++i) {
236 XGrabKey(d, XKeysymToKeycode(d, k.key),
237 k.modifier | ignored_modifiers[i],
238 root, False, GrabModeAsync, GrabModeAsync);
243 keycode_to_keysym(unsigned int kc)
245 /* group 0 (?). shift level is 0 because we don't want it*/
246 return XkbKeycodeToKeysym(d, kc, 0, 0);
255 /* one can use (at least) three way to obtain the current
256 * focused window using xlib:
258 * - XQueryTree : you traverse tre tree until you find the
261 * - looking at _NET_ACTIVE_WINDOW in the root window, but
262 * depedns on the window manager to set it
264 * - using XGetInputFocus
266 * I don't know the pro/cons of these, but XGetInputFocus
269 XGetInputFocus(d, &w, &revert_to);
274 send_fake(Window w, struct key k, int pressed)
278 e.type = pressed ? KeyPress : KeyRelease;
282 e.root = DefaultRootWindow(d);
284 e.time = CurrentTime;
286 /* TODO: fix these */
292 e.same_screen = True;
293 e.keycode = XKeysymToKeycode(d, k.key);
294 e.state = k.modifier;
296 XSendEvent(d, w, True, KeyPressMask, (XEvent*)&e);
301 window_match_class(Window w, const char *class)
306 if (!XGetClassHint(d, w, &ch)) {
307 fprintf(stderr, "XGetClassHint failed\n");
311 matched = !strcmp(ch.res_class, class);
323 do_action(struct action a, Window focused, int pressed)
327 send_fake(focused, a.send_key, pressed);
351 switch (p = fork()) {
356 if ((sh = getenv("SHELL")) == NULL)
358 printf("before exec'ing %s -c '%s'\n", sh, a.str);
359 execlp(sh, sh, "-c", a.str, NULL);
373 free_action(struct action a)
383 new_match(int prop, char *s)
387 if ((m = calloc(1, sizeof(*m))) == NULL)
395 recfree_match(struct match *m)
409 match_window(struct match *m, Window w)
416 return window_match_class(w, m->str);
429 new_rule(struct key k, struct action a)
433 if ((r = calloc(1, sizeof(*r))) == NULL)
435 memcpy(&r->key, &k, sizeof(k));
436 memcpy(&r->action, &a, sizeof(a));
441 recfree_rule(struct rule *r)
454 rule_matched(struct rule *r, struct key k)
459 m &= ~IGNORE_MODIFIERS; /* clear ignored modifiers */
461 return r->key.modifier == m
462 && r->key.key == k.key;
469 new_group(struct match *matches, struct rule *rules)
473 if ((g = calloc(1, sizeof(*g))) == NULL)
475 g->matches = matches;
481 recfree_group(struct group *g)
489 recfree_match(g->matches);
490 recfree_rule(g->rules);
496 process_event(struct group *g, XKeyEvent *e)
500 struct key pressed = {
501 .modifier = e->state,
502 .key = keycode_to_keysym(e->keycode),
505 focused = focused_window();
507 for (; g != NULL; g = g->next) {
508 if (!group_match(g, focused))
511 for (r = g->rules; r != NULL; r = r->next) {
512 if (rule_matched(r, pressed)) {
513 do_action(r->action, focused, e->type == KeyPress);
519 send_fake(focused, pressed, e->type == KeyPress);
523 group_match(struct group *g, Window w)
527 for (m = g->matches; m != NULL; m = m->next)
528 if (match_window(m, w))
534 /* debug/dump stuff */
537 printkey(struct key k)
539 if (k.modifier & ControlMask)
541 if (k.modifier & ShiftMask)
543 if (k.modifier & Mod1Mask)
545 if (k.modifier & Mod4Mask)
548 printf("%s", XKeysymToString(k.key));
552 printaction(struct action a)
557 case ATOGGLE: printf("toggle"); break;
558 case AACTIVATE: printf("activate"); break;
559 case ADEACTIVATE: printf("deactivate"); break;
560 case AIGNORE: printf("ignore"); break;
561 default: abort(); /* unreachable */
567 printkey(a.send_key);
571 printf("exec %s", a.str);
581 printmatch(struct match *m)
593 printf("class %s", m->str);
609 printrule(struct rule *r)
618 printaction(r->action);
626 printgroup(struct group *g)
632 printmatch(g->matches);