Skip to content

Instantly share code, notes, and snippets.

@Libbum
Last active March 13, 2018 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Libbum/4929284c1e4e23b2871724270dfd476d to your computer and use it in GitHub Desktop.
Save Libbum/4929284c1e4e23b2871724270dfd476d to your computer and use it in GitHub Desktop.
Outputs current layout for the keyboard
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/XKBlib.h>
#include <X11/extensions/XKBrules.h>
int main(int argc, char **argv) {
Display *dpy = XOpenDisplay(NULL);
if (dpy == NULL) {
fprintf(stderr, "Cannot open display\n");
exit(1);
}
XkbStateRec state;
XkbGetState(dpy, XkbUseCoreKbd, &state);
XkbRF_VarDefsRec vd;
XkbRF_GetNamesProp(dpy, NULL, &vd);
char *tok = strtok(vd.layout, ",");
for (int i = 0; i < state.group; i++) {
tok = strtok(NULL, ",");
if (tok == NULL) {
return 1;
}
}
printf("%s\n", tok);
return 0;
}
@Libbum
Copy link
Author

Libbum commented Mar 13, 2018

Compile with gcc -I/usr/include getxkblayout.c -lX11 -lxkbfile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment