spoon

set dwm status
git clone git://git.2f30.org/spoon
Log | Files | Refs | LICENSE

key.c (1019B)


      1 #include <err.h>
      2 #include <stdio.h>
      3 
      4 #include <X11/keysym.h>
      5 #include <X11/Xlib.h>
      6 
      7 #include "types.h"
      8 #include "util.h"
      9 
     10 int
     11 keyread(void *arg, char *buf, size_t len)
     12 {
     13 	Display *dpy;
     14 	XModifierKeymap *map;
     15 	KeyCode keycode;
     16 	Window w1, w2;
     17 	int i1, i2, i3, i4;
     18 	unsigned modmask;
     19 	unsigned keymask;
     20 	struct keyarg *key = arg;
     21 	int on;
     22 	int i;
     23 
     24 	dpy = XOpenDisplay(NULL);
     25 	if (dpy == NULL) {
     26 		warnx("cannot open display");
     27 		return -1;
     28 	}
     29 	keycode = XKeysymToKeycode(dpy, key->sym);
     30 	if (keycode == NoSymbol) {
     31 		warnx("no key code for this symbol");
     32 		XCloseDisplay(dpy);
     33 		return -1;
     34 	}
     35 	map = XGetModifierMapping(dpy);
     36 	for (i = 0; i < 8; i++)
     37 		if (map->modifiermap[map->max_keypermod * i] == keycode)
     38 			keymask = 1U << i;
     39 	XFreeModifiermap(map);
     40 	XQueryPointer(dpy, DefaultRootWindow(dpy),
     41 		      &w1, &w2, &i1, &i2, &i3, &i4, &modmask);
     42 	XCloseDisplay(dpy);
     43 	on = (keymask & modmask) != 0;
     44 	DPRINTF_D(on);
     45 	if (on)
     46 		snprintf(buf, len, "%s", key->on);
     47 	else
     48 		snprintf(buf, len, "%s", key->off);
     49 	return 0;
     50 }