xkblayout

prints current xkb layout
git clone git://git.2f30.org/xkblayout
Log | Files | Refs | README | LICENSE

xkblayout.c (1296B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #include <X11/XKBlib.h>
      6 #include <X11/extensions/XKBrules.h>
      7 
      8 #ifdef DEBUG
      9 #define DPRINTF_U(t) printf(#t"=%u\n", t)
     10 #define DPRINTF_S(t) printf(#t"=%s\n", t)
     11 #else
     12 #define DPRINTF_U(t)
     13 #define DPRINTF_S(t)
     14 #endif
     15 
     16 int
     17 main(void)
     18 {
     19 	Display *dpy;
     20 	XkbDescPtr desc;
     21 	XkbStateRec state;
     22 	XkbRF_VarDefsRec vd;
     23 	char *group, *symbols;
     24 	char *tmp, *str, *tok;
     25 	unsigned int i;
     26 
     27 	dpy = XOpenDisplay(NULL);
     28 	if (dpy == NULL) {
     29 		fprintf(stderr, "Cannot open display\n");
     30 		exit(1);
     31 	}
     32 
     33 	/* Get layout group index in [0..3] */
     34 	XkbGetState(dpy, XkbUseCoreKbd, &state);
     35 	DPRINTF_U(state.group);
     36 
     37 	/* Get layout names */
     38 	desc = XkbGetKeyboard(dpy, XkbAllComponentsMask, XkbUseCoreKbd);
     39 	group = XGetAtomName(dpy, desc->names->groups[state.group]);
     40 	DPRINTF_S(group);
     41 	symbols = XGetAtomName(dpy, desc->names->symbols);
     42 	DPRINTF_S(symbols);
     43 
     44 	/* Get clean layouts string */
     45 	XkbRF_GetNamesProp(dpy, &tmp, &vd);
     46 	DPRINTF_S(tmp);
     47 	DPRINTF_S(vd.layout);
     48 
     49 	str = strdup(vd.layout);
     50 
     51 	/* Tokenize until we reach the current layout */
     52 	tok = strtok(str, ",");
     53 	for (i = 0; i < state.group; i++) {
     54 		tok = strtok(NULL, ",");
     55 		if (tok == NULL)
     56 			return 1;
     57 		DPRINTF_S(tok);
     58 	}
     59 
     60 	/* Print layout name */
     61 	printf("%s\n", tok);
     62 
     63 	free(str);
     64 
     65 	return 0;
     66 }