xkblayout.c (975B)
1 #include <err.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 6 #include <X11/Xlib.h> 7 #include <X11/XKBlib.h> 8 #include <X11/extensions/XKBrules.h> 9 10 #include "util.h" 11 12 int 13 xkblayoutread(void *arg, char *buf, size_t len) 14 { 15 Display *dpy; 16 XkbStateRec state; 17 XkbRF_VarDefsRec vd; 18 char *tmp = NULL, *str, *tok; 19 int i, ret = 0; 20 21 dpy = XOpenDisplay(NULL); 22 if (dpy == NULL) { 23 warnx("cannot open display"); 24 return -1; 25 } 26 XkbGetState(dpy, XkbUseCoreKbd, &state); 27 if (XkbRF_GetNamesProp(dpy, &tmp, &vd) == 0){ 28 warnx("cannot extract keyboard properties"); 29 ret = -1; 30 goto out0; 31 } 32 str = strdup(vd.layout); 33 if (str == NULL) { 34 ret = -1; 35 goto out1; 36 } 37 tok = strtok(str, ","); 38 for (i = 0; i < state.group; i++) { 39 tok = strtok(NULL, ","); 40 if (tok == NULL) { 41 warnx("cannot extract layout"); 42 ret = -1; 43 goto out2; 44 } 45 } 46 strlcpy(buf, tok, len); 47 out2: 48 free(str); 49 out1: 50 free(tmp); 51 XFree(vd.options); 52 out0: 53 XCloseDisplay(dpy); 54 return ret; 55 }