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"
24 #include <X11/XKBlib.h>
25 #include <X11/Xutil.h>
28 # define pledge(a, b) 0
42 int ignored_modifiers[] = {
44 LockMask, /* caps lock */
45 Mod2Mask, /* num lock */
46 Mod3Mask, /* scroll lock */
50 #define IGNORE_MODIFIERS (LockMask | Mod2Mask | Mod3Mask | Mod5Mask)
53 yyerror(const char *e)
56 fprintf(stderr, "%s:%d %s\n", fname, yylineno, e);
65 home = getenv("HOME");
66 xdg = getenv("XDG_CONFIG_HOME");
68 if (home == NULL && xdg == NULL)
71 /* file in home directory >>>>>> XDG shit */
73 if (asprintf(&t, "%s/.star-platinum.conf", home) == -1)
75 if (access(t, R_OK) == 0)
79 /* continue to search */
82 /* sanitize XDG_CONFIG_HOME */
85 if (asprintf(&xdg, "%s/.config", home) == -1)
89 if (asprintf(&t, "%s/star-platinum.conf", xdg) == -1)
92 if (access(t, R_OK) == 0)
104 main(int argc, char **argv)
106 int status = 0, dump_config = 0, conftest = 0, ch;
113 while ((ch = getopt(argc, argv, "c:dn")) != -1) {
117 if ((fname = strdup(optarg)) == NULL)
130 fprintf(stderr, "USAGE: %s [-dn] [-c conf]\n", *argv);
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);
155 recfree_group(config);
159 if ((d = XOpenDisplay(NULL)) == NULL) {
160 recfree_group(config);
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)
171 XSelectInput(d, root, KeyPressMask);
181 process_event(config, (XKeyEvent*)&e);
185 printf("Unknown event %d\n", e.type);
191 recfree_group(config);
198 /* TODO: it should grab ALL POSSIBLE COMBINATIONS of `ignored_modifiers`! */
200 grabkey(struct key k)
202 static size_t len = sizeof(ignored_modifiers)/sizeof(int);
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);
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);
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
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
243 XGetInputFocus(d, &w, &revert_to);
248 send_fake(Window w, struct key k, int pressed)
252 e.type = pressed ? KeyPress : KeyRelease;
256 e.root = DefaultRootWindow(d);
258 e.time = CurrentTime;
260 /* TODO: fix these */
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);
275 window_match_class(Window w, const char *class)
280 if (!XGetClassHint(d, w, &ch)) {
281 fprintf(stderr, "XGetClassHint failed\n");
285 matched = !strcmp(ch.res_class, class);
297 do_action(struct action a, Window focused, int pressed)
301 send_fake(focused, a.send_key, pressed);
331 new_match(int prop, char *s)
335 if ((m = calloc(1, sizeof(*m))) == NULL)
343 recfree_match(struct match *m)
357 match_window(struct match *m, Window w)
361 return window_match_class(w, m->str);
374 new_rule(struct key k, struct action a)
378 if ((r = calloc(1, sizeof(*r))) == NULL)
380 memcpy(&r->key, &k, sizeof(k));
381 memcpy(&r->action, &a, sizeof(a));
386 recfree_rule(struct rule *r)
399 rule_matched(struct rule *r, struct key k)
404 m &= ~IGNORE_MODIFIERS; /* clear ignored modifiers */
406 return r->key.modifier == m
407 && r->key.key == k.key;
414 new_group(struct match *matches, struct rule *rules)
418 if ((g = calloc(1, sizeof(*g))) == NULL)
420 g->matches = matches;
426 recfree_group(struct group *g)
434 recfree_match(g->matches);
435 recfree_rule(g->rules);
441 process_event(struct group *g, XKeyEvent *e)
445 struct key pressed = {
446 .modifier = e->state,
447 .key = keycode_to_keysym(e->keycode),
450 focused = focused_window();
452 for (; g != NULL; g = g->next) {
453 if (!group_match(g, focused))
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);
464 send_fake(focused, pressed, e->type == KeyPress);
468 group_match(struct group *g, Window w)
472 for (m = g->matches; m != NULL; m = m->next)
473 if (match_window(m, w))
479 /* debug/dump stuff */
482 printkey(struct key k)
484 if (k.modifier & ControlMask)
486 if (k.modifier & ShiftMask)
488 if (k.modifier & Mod1Mask)
490 if (k.modifier & Mod4Mask)
493 printf("%s", XKeysymToString(k.key));
497 printaction(struct action a)
499 if (a.type == ASPECIAL) {
501 case ATOGGLE: printf("toggle"); break;
502 case AACTIVATE: printf("activate"); break;
503 case ADEACTIVATE: printf("deactivate"); break;
504 case AIGNORE: printf("ignore"); break;
508 printkey(a.send_key);
513 printmatch(struct match *m)
527 printf(" %s", m->str);
538 printrule(struct rule *r)
547 printaction(r->action);
555 printgroup(struct group *g)
561 printmatch(g->matches);