2018-05-23 08:16:30 +02: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>
|
|
|
|
|
2022-10-26 22:14:53 +02:00
|
|
|
#include "../slstatus.h"
|
2022-10-27 23:18:30 +02:00
|
|
|
#include "../util.h"
|
2018-05-23 08:16:30 +02:00
|
|
|
|
|
|
|
static int
|
2018-05-23 15:29:37 +02:00
|
|
|
valid_layout_or_variant(char *sym)
|
2018-05-23 08:16:30 +02:00
|
|
|
{
|
|
|
|
size_t i;
|
2018-05-23 15:29:37 +02:00
|
|
|
/* invalid symbols from xkb rules config */
|
|
|
|
static const char *invalid[] = { "evdev", "inet", "pc", "base" };
|
|
|
|
|
2022-10-28 00:51:34 +02:00
|
|
|
for (i = 0; i < LEN(invalid); i++)
|
|
|
|
if (!strncmp(sym, invalid[i], strlen(invalid[i])))
|
2018-05-23 08:16:30 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-05-23 15:29:37 +02:00
|
|
|
static char *
|
|
|
|
get_layout(char *syms, int grp_num)
|
2018-05-23 08:16:30 +02:00
|
|
|
{
|
2018-05-23 15:29:37 +02: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 08:16:30 +02:00
|
|
|
}
|
2018-05-23 15:29:37 +02:00
|
|
|
layout = tok;
|
|
|
|
grp++;
|
2018-05-23 08:16:30 +02:00
|
|
|
}
|
|
|
|
|
2018-05-23 15:29:37 +02:00
|
|
|
return layout;
|
2018-05-23 08:16:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2022-10-26 22:16:05 +02:00
|
|
|
keymap(const char *unused)
|
2018-05-23 08:16:30 +02:00
|
|
|
{
|
|
|
|
Display *dpy;
|
2018-05-23 15:29:37 +02:00
|
|
|
XkbDescRec *desc;
|
|
|
|
XkbStateRec state;
|
2021-03-05 20:35:24 +01:00
|
|
|
char *symbols;
|
|
|
|
const char *layout;
|
2018-05-23 08:16:30 +02:00
|
|
|
|
2018-05-23 15:29:37 +02:00
|
|
|
layout = NULL;
|
2018-05-23 08:16:30 +02:00
|
|
|
|
|
|
|
if (!(dpy = XOpenDisplay(NULL))) {
|
|
|
|
warn("XOpenDisplay: Failed to open display");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!(desc = XkbAllocKeyboard())) {
|
2018-05-23 15:29:37 +02:00
|
|
|
warn("XkbAllocKeyboard: Failed to allocate keyboard");
|
|
|
|
goto end;
|
2018-05-23 08:16:30 +02:00
|
|
|
}
|
2018-05-23 15:29:37 +02:00
|
|
|
if (XkbGetNames(dpy, XkbSymbolsNameMask, desc)) {
|
|
|
|
warn("XkbGetNames: Failed to retrieve key symbols");
|
|
|
|
goto end;
|
2018-05-23 08:16:30 +02:00
|
|
|
}
|
2018-05-23 15:29:37 +02: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;
|
|
|
|
}
|
2021-03-05 20:35:24 +01:00
|
|
|
layout = bprintf("%s", get_layout(symbols, state.group));
|
2018-05-23 15:29:37 +02:00
|
|
|
XFree(symbols);
|
|
|
|
end:
|
2018-05-23 08:16:30 +02:00
|
|
|
XkbFreeKeyboard(desc, XkbSymbolsNameMask, 1);
|
2022-10-28 00:51:34 +02:00
|
|
|
if (XCloseDisplay(dpy))
|
2018-05-23 15:29:37 +02:00
|
|
|
warn("XCloseDisplay: Failed to close display");
|
2018-05-23 08:16:30 +02:00
|
|
|
|
|
|
|
return layout;
|
|
|
|
}
|