2018-05-23 06:16:30 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <X11/XKBlib.h>
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
|
|
|
|
#include "../util.h"
|
|
|
|
|
|
|
|
static int
|
2018-05-23 13:29:37 +00:00
|
|
|
valid_layout_or_variant(char *sym)
|
2018-05-23 06:16:30 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
2018-05-23 13:29:37 +00:00
|
|
|
/* invalid symbols from xkb rules config */
|
|
|
|
static const char *invalid[] = { "evdev", "inet", "pc", "base" };
|
|
|
|
|
|
|
|
for (i = 0; i < LEN(invalid); i++) {
|
|
|
|
if (!strncmp(sym, invalid[i], strlen(invalid[i]))) {
|
2018-05-23 06:16:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-05-23 11:30:45 +00:00
|
|
|
}
|
2018-05-23 06:16:30 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-05-23 13:29:37 +00:00
|
|
|
static char *
|
|
|
|
get_layout(char *syms, int grp_num)
|
2018-05-23 06:16:30 +00:00
|
|
|
{
|
2018-05-23 13:29:37 +00:00
|
|
|
char *tok, *layout;
|
|
|
|
int grp;
|
|
|
|
|
|
|
|
layout = NULL;
|
|
|
|
tok = strtok(syms, "+:");
|
|
|
|
for (grp = 0; tok && grp <= grp_num; tok = strtok(NULL, "+:")) {
|
|
|
|
if (!valid_layout_or_variant(tok)) {
|
|
|
|
continue;
|
|
|
|
} else if (strlen(tok) == 1 && isdigit(tok[0])) {
|
|
|
|
/* ignore :2, :3, :4 (additional layout groups) */
|
|
|
|
continue;
|
2018-05-23 06:16:30 +00:00
|
|
|
}
|
2018-05-23 13:29:37 +00:00
|
|
|
layout = tok;
|
|
|
|
grp++;
|
2018-05-23 06:16:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 13:29:37 +00:00
|
|
|
return layout;
|
2018-05-23 06:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
keymap(void)
|
|
|
|
{
|
|
|
|
Display *dpy;
|
2018-05-23 13:29:37 +00:00
|
|
|
XkbDescRec *desc;
|
|
|
|
XkbStateRec state;
|
|
|
|
char *symbols, *layout;
|
2018-05-23 06:16:30 +00:00
|
|
|
|
2018-05-23 13:29:37 +00:00
|
|
|
layout = NULL;
|
2018-05-23 06:16:30 +00:00
|
|
|
|
|
|
|
if (!(dpy = XOpenDisplay(NULL))) {
|
|
|
|
warn("XOpenDisplay: Failed to open display");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!(desc = XkbAllocKeyboard())) {
|
2018-05-23 13:29:37 +00:00
|
|
|
warn("XkbAllocKeyboard: Failed to allocate keyboard");
|
|
|
|
goto end;
|
2018-05-23 06:16:30 +00:00
|
|
|
}
|
2018-05-23 13:29:37 +00:00
|
|
|
if (XkbGetNames(dpy, XkbSymbolsNameMask, desc)) {
|
|
|
|
warn("XkbGetNames: Failed to retrieve key symbols");
|
|
|
|
goto end;
|
2018-05-23 06:16:30 +00:00
|
|
|
}
|
2018-05-23 13:29:37 +00:00
|
|
|
if (XkbGetState(dpy, XkbUseCoreKbd, &state)) {
|
|
|
|
warn("XkbGetState: Failed to retrieve keyboard state");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (!(symbols = XGetAtomName(dpy, desc->names->symbols))) {
|
|
|
|
warn("XGetAtomName: Failed to get atom name");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
layout = (char *)bprintf("%s", get_layout(symbols, state.group));
|
|
|
|
XFree(symbols);
|
|
|
|
end:
|
2018-05-23 06:16:30 +00:00
|
|
|
XkbFreeKeyboard(desc, XkbSymbolsNameMask, 1);
|
2018-05-23 13:29:37 +00:00
|
|
|
if (XCloseDisplay(dpy)) {
|
|
|
|
warn("XCloseDisplay: Failed to close display");
|
|
|
|
}
|
2018-05-23 06:16:30 +00:00
|
|
|
|
|
|
|
return layout;
|
|
|
|
}
|