/
xkb.c
60 lines (48 loc) · 1.19 KB
/
xkb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/XKBlib.h>
#include <X11/Xlibint.h>
#include <X11/Xatom.h>
#include "statusbar.h"
char* xkbGetGroup(char* buf, int len) {
int xkbEventType, xkbError, reason_rtrn, mjr, mnr;
char *display_name;
XkbStateRec xkbstate;
static Display *dpy = NULL;
static XkbDescPtr xkbdesc = NULL;
/* Lets begin */
if (!dpy) {
display_name = NULL;
mjr = XkbMajorVersion;
mnr = XkbMinorVersion;
dpy = XkbOpenDisplay(display_name, &xkbEventType, &xkbError,
&mjr, &mnr, &reason_rtrn);
}
if (dpy == NULL) {
strcpy(buf, "?");
return buf;
}
if ( Success != XkbGetState(dpy, XkbUseCoreKbd, &xkbstate) ) {
strcpy(buf, "?");
return buf;
}
if (!xkbdesc)
xkbdesc = XkbAllocKeyboard();
if (!xkbdesc) {
strcpy(buf, "?");
return buf;
}
/* get the names of the layout */
if ( Success == XkbGetNames(dpy, 1<<12, xkbdesc)) {
Atom iatoms[4];
char *iatomnames[4];
int i, j;
for (i = 0, j = 0; i < 4; i++)
if (xkbdesc->names->groups[i] != None)
iatoms[j++] = xkbdesc->names->groups[i];
if (XGetAtomNames(dpy, iatoms, j, iatomnames))
buf = strndup(iatomnames[xkbstate.locked_group], len);
}
return buf;
}