-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
46 lines (34 loc) · 921 Bytes
/
main.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
#include <stdio.h>
#include <string.h>
#include <X11/XKBlib.h>
const size_t LAYOUT_MAX_LEN = 32;
int layout_name(char *out) {
Display *dpy = XOpenDisplay(NULL);
if (dpy == NULL) {
return -1;
}
int res = -1;
XkbStateRec state;
XkbGetState(dpy, XkbUseCoreKbd, &state);
XkbDescPtr desc = XkbGetKeyboard(dpy, XkbAllComponentsMask, XkbUseCoreKbd);
if (desc) {
char *group = XGetAtomName(dpy, desc->names->groups[state.group]);
if (group) {
strncpy(out, group, LAYOUT_MAX_LEN);
out[LAYOUT_MAX_LEN - 1] = '\0';
XFree(group);
res = 0;
}
XkbFreeKeyboard(desc, XkbAllComponentsMask, 1);
}
XCloseDisplay(dpy);
return res;
}
int main(int argc, char** argv) {
char layout[LAYOUT_MAX_LEN];
if (layout_name(layout)) {
return 1;
}
printf("%s", layout);
return 0;
}