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"
25 #include <X11/XKBlib.h>
26 #include <X11/Xutil.h>
29 # define pledge(a, b) 0
43 int ignored_modifiers[] = {
45 LockMask, /* caps lock */
46 Mod2Mask, /* num lock */
47 Mod3Mask, /* scroll lock */
51 #define IGNORE_MODIFIERS (LockMask | Mod2Mask | Mod3Mask | Mod5Mask)
53 __attribute__((__format__ (__printf__, 1, 2)))
55 yyerror(const char *fmt, ...)
63 fprintf(stderr, "%s:%d ", fname, yylineno);
64 vfprintf(stderr, fmt, ap);
67 if (l > 0 && fmt[l-1] != '\n')
68 fprintf(stderr, "\n");
79 home = getenv("HOME");
80 xdg = getenv("XDG_CONFIG_HOME");
82 if (home == NULL && xdg == NULL)
85 /* file in home directory >>>>>> XDG shit */
87 if (asprintf(&t, "%s/.star-platinum.conf", home) == -1)
89 if (access(t, R_OK) == 0)
93 /* continue to search */
96 /* sanitize XDG_CONFIG_HOME */
99 if (asprintf(&xdg, "%s/.config", home) == -1)
103 if (asprintf(&t, "%s/star-platinum.conf", xdg) == -1)
106 if (access(t, R_OK) == 0)
118 main(int argc, char **argv)
120 int status = 0, dump_config = 0, conftest = 0, ch;
127 while ((ch = getopt(argc, argv, "c:dn")) != -1) {
131 if ((fname = strdup(optarg)) == NULL)
144 fprintf(stderr, "USAGE: %s [-dn] [-c conf]\n", *argv);
150 if ((fname = find_config()) == NULL)
151 errx(1, "can't find a configuration file");
154 if ((yyin = fopen(fname, "r")) == NULL)
155 err(1, "cannot open %s", fname);
169 recfree_group(config);
173 if ((d = XOpenDisplay(NULL)) == NULL) {
174 recfree_group(config);
178 XSetErrorHandler(&error_handler);
180 root = DefaultRootWindow(d);
182 /* grab all the keys */
183 for (g = config; g != NULL; g = g->next)
184 for (r = g->rules; r != NULL; r = r->next)
187 XSelectInput(d, root, KeyPressMask);
197 process_event(config, (XKeyEvent*)&e);
201 printf("Unknown event %d\n", e.type);
207 recfree_group(config);
215 error_handler(Display *d, XErrorEvent *e)
217 fprintf(stderr, "Xlib error %d\n", e->type);
221 /* TODO: it should grab ALL POSSIBLE COMBINATIONS of `ignored_modifiers`! */
223 grabkey(struct key k)
225 static size_t len = sizeof(ignored_modifiers)/sizeof(int);
229 root = DefaultRootWindow(d);
231 /* printf("Grabbing "); printkey(k); printf("\n"); */
232 for (i = 0; i < len; ++i) {
233 XGrabKey(d, XKeysymToKeycode(d, k.key),
234 k.modifier | ignored_modifiers[i],
235 root, False, GrabModeAsync, GrabModeAsync);
240 keycode_to_keysym(unsigned int kc)
242 /* group 0 (?). shift level is 0 because we don't want it*/
243 return XkbKeycodeToKeysym(d, kc, 0, 0);
252 /* one can use (at least) three way to obtain the current
253 * focused window using xlib:
255 * - XQueryTree : you traverse tre tree until you find the
258 * - looking at _NET_ACTIVE_WINDOW in the root window, but
259 * depedns on the window manager to set it
261 * - using XGetInputFocus
263 * I don't know the pro/cons of these, but XGetInputFocus
266 XGetInputFocus(d, &w, &revert_to);
271 send_fake(Window w, struct key k, int pressed)
275 e.type = pressed ? KeyPress : KeyRelease;
279 e.root = DefaultRootWindow(d);
281 e.time = CurrentTime;
283 /* TODO: fix these */
289 e.same_screen = True;
290 e.keycode = XKeysymToKeycode(d, k.key);
291 e.state = k.modifier;
293 XSendEvent(d, w, True, KeyPressMask, (XEvent*)&e);
298 window_match_class(Window w, const char *class)
303 if (!XGetClassHint(d, w, &ch)) {
304 fprintf(stderr, "XGetClassHint failed\n");
308 matched = !strcmp(ch.res_class, class);
320 do_action(struct action a, Window focused, int pressed)
324 send_fake(focused, a.send_key, pressed);
354 new_match(int prop, char *s)
358 if ((m = calloc(1, sizeof(*m))) == NULL)
366 recfree_match(struct match *m)
380 match_window(struct match *m, Window w)
387 return window_match_class(w, m->str);
400 new_rule(struct key k, struct action a)
404 if ((r = calloc(1, sizeof(*r))) == NULL)
406 memcpy(&r->key, &k, sizeof(k));
407 memcpy(&r->action, &a, sizeof(a));
412 recfree_rule(struct rule *r)
425 rule_matched(struct rule *r, struct key k)
430 m &= ~IGNORE_MODIFIERS; /* clear ignored modifiers */
432 return r->key.modifier == m
433 && r->key.key == k.key;
440 new_group(struct match *matches, struct rule *rules)
444 if ((g = calloc(1, sizeof(*g))) == NULL)
446 g->matches = matches;
452 recfree_group(struct group *g)
460 recfree_match(g->matches);
461 recfree_rule(g->rules);
467 process_event(struct group *g, XKeyEvent *e)
471 struct key pressed = {
472 .modifier = e->state,
473 .key = keycode_to_keysym(e->keycode),
476 focused = focused_window();
478 for (; g != NULL; g = g->next) {
479 if (!group_match(g, focused))
482 for (r = g->rules; r != NULL; r = r->next) {
483 if (rule_matched(r, pressed)) {
484 do_action(r->action, focused, e->type == KeyPress);
490 send_fake(focused, pressed, e->type == KeyPress);
494 group_match(struct group *g, Window w)
498 for (m = g->matches; m != NULL; m = m->next)
499 if (match_window(m, w))
505 /* debug/dump stuff */
508 printkey(struct key k)
510 if (k.modifier & ControlMask)
512 if (k.modifier & ShiftMask)
514 if (k.modifier & Mod1Mask)
516 if (k.modifier & Mod4Mask)
519 printf("%s", XKeysymToString(k.key));
523 printaction(struct action a)
525 if (a.type == ASPECIAL) {
527 case ATOGGLE: printf("toggle"); break;
528 case AACTIVATE: printf("activate"); break;
529 case ADEACTIVATE: printf("deactivate"); break;
530 case AIGNORE: printf("ignore"); break;
534 printkey(a.send_key);
539 printmatch(struct match *m)
553 printf(" %s", m->str);
564 printrule(struct rule *r)
573 printaction(r->action);
581 printgroup(struct group *g)
587 printmatch(g->matches);