feat: add XKB layout indicator, live X11 session replace, and ANODE WM/DE identity
This commit is contained in:
1 parent
8d6236a63f
commit
158345c168
12 files changed
+1303
-48
No files matched your search
@@ -1,5 +1,6 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include "anode_plat.h"
|
||||
#include "anode_xkb_parse.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
@@ -12,6 +13,8 @@
|
||||
#include <sys/socket.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <X11/Xatom.h>
|
||||
#include <X11/XKBlib.h>
|
||||
#include <X11/Xlib.h>
|
||||
|
||||
#ifdef __linux__
|
||||
@@ -59,7 +62,65 @@ static int open_net_watch_fd(void) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void build_status(char *out, size_t sz) {
|
||||
static bool query_xkb_layout(Display *dpy, Window root, bool has_xkb, char *out, size_t out_sz) {
|
||||
if (!out || out_sz == 0) return false;
|
||||
out[0] = '\0';
|
||||
if (!has_xkb || !dpy) return false;
|
||||
|
||||
XkbStateRec state;
|
||||
memset(&state, 0, sizeof(state));
|
||||
if (XkbGetState(dpy, XkbUseCoreKbd, &state) != Success) return false;
|
||||
unsigned int grp = (unsigned int)state.group;
|
||||
|
||||
bool ok = false;
|
||||
XkbDescPtr desc = XkbAllocKeyboard();
|
||||
if (desc) {
|
||||
if (XkbGetNames(dpy, XkbSymbolsNameMask | XkbGroupNamesMask, desc) != Success) {
|
||||
XkbFreeKeyboard(desc, 0, True);
|
||||
desc = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Priority 1: XKB symbols name (tracks active groups cleanly across setxkbmap / xkbcomp) */
|
||||
if (desc && desc->names && desc->names->symbols != None) {
|
||||
char *sym = XGetAtomName(dpy, desc->names->symbols);
|
||||
if (sym) {
|
||||
ok = anode_xkb_layout_from_symbols(sym, grp, out, out_sz);
|
||||
XFree(sym);
|
||||
}
|
||||
}
|
||||
|
||||
/* Priority 2: root _XKB_RULES_NAMES property layouts CSV */
|
||||
if (!ok) {
|
||||
Atom r_atom = XInternAtom(dpy, "_XKB_RULES_NAMES", True);
|
||||
if (r_atom != None) {
|
||||
Atom act_type = None;
|
||||
int act_fmt = 0;
|
||||
unsigned long nitems = 0, bytes_after = 0;
|
||||
unsigned char *prop = NULL;
|
||||
if (XGetWindowProperty(dpy, root, r_atom, 0, 1024, False, XA_STRING,
|
||||
&act_type, &act_fmt, &nitems, &bytes_after, &prop) == Success &&
|
||||
prop && act_type == XA_STRING && act_fmt == 8 && nitems > 0) {
|
||||
ok = anode_xkb_layout_from_rules_prop(prop, nitems, grp, out, out_sz);
|
||||
}
|
||||
if (prop) XFree(prop);
|
||||
}
|
||||
}
|
||||
|
||||
/* Priority 3: first two ASCII letters of the XKB group name */
|
||||
if (!ok && desc && desc->names && grp < XkbNumKbdGroups && desc->names->groups[grp] != None) {
|
||||
char *gname = XGetAtomName(dpy, desc->names->groups[grp]);
|
||||
if (gname) {
|
||||
ok = anode_xkb_layout_from_group_name(gname, out, out_sz);
|
||||
XFree(gname);
|
||||
}
|
||||
}
|
||||
|
||||
if (desc) XkbFreeKeyboard(desc, 0, True);
|
||||
return ok;
|
||||
}
|
||||
|
||||
static void build_status(Display *dpy, Window root, bool has_xkb, char *out, size_t sz) {
|
||||
int vol = 0;
|
||||
bool muted = false;
|
||||
char vol_str[32] = "";
|
||||
@@ -100,13 +161,20 @@ static void build_status(char *out, size_t sz) {
|
||||
snprintf(bat_str, sizeof(bat_str), "AC");
|
||||
}
|
||||
|
||||
char kbd_str[16] = "";
|
||||
query_xkb_layout(dpy, root, has_xkb, kbd_str, sizeof(kbd_str));
|
||||
|
||||
time_t now = time(NULL);
|
||||
struct tm tmbuf;
|
||||
localtime_r(&now, &tmbuf);
|
||||
char time_str[32];
|
||||
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M", &tmbuf);
|
||||
|
||||
if (vol_str[0]) {
|
||||
if (vol_str[0] && kbd_str[0]) {
|
||||
snprintf(out, sz, " %s | %s | %s | %s | %s ", vol_str, net_str, bat_str, kbd_str, time_str);
|
||||
} else if (kbd_str[0]) {
|
||||
snprintf(out, sz, " %s | %s | %s | %s ", net_str, bat_str, kbd_str, time_str);
|
||||
} else if (vol_str[0]) {
|
||||
snprintf(out, sz, " %s | %s | %s | %s ", vol_str, net_str, bat_str, time_str);
|
||||
} else {
|
||||
snprintf(out, sz, " %s | %s | %s ", net_str, bat_str, time_str);
|
||||
@@ -133,6 +201,19 @@ int main(void) {
|
||||
fprintf(stderr, "anode-statusd: cannot open X11 display\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int xkb_op = 0, xkb_ev = 0, xkb_err = 0;
|
||||
int xkb_maj = XkbMajorVersion, xkb_min = XkbMinorVersion;
|
||||
bool has_xkb = XkbQueryExtension(dpy, &xkb_op, &xkb_ev, &xkb_err, &xkb_maj, &xkb_min) != 0;
|
||||
if (has_xkb) {
|
||||
XkbSelectEvents(dpy, XkbUseCoreKbd,
|
||||
XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbNamesNotifyMask,
|
||||
XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbNamesNotifyMask);
|
||||
XkbSelectEventDetails(dpy, XkbUseCoreKbd, XkbStateNotify,
|
||||
XkbAllStateComponentsMask, XkbGroupStateMask);
|
||||
XSync(dpy, False);
|
||||
}
|
||||
|
||||
Window root = DefaultRootWindow(dpy);
|
||||
int xfd = ConnectionNumber(dpy);
|
||||
int netfd = open_net_watch_fd();
|
||||
@@ -141,18 +222,20 @@ int main(void) {
|
||||
bool running = true;
|
||||
|
||||
while (running) {
|
||||
while (XPending(dpy) > 0) {
|
||||
XEvent ev;
|
||||
XNextEvent(dpy, &ev);
|
||||
}
|
||||
|
||||
char cur[512];
|
||||
build_status(cur, sizeof(cur));
|
||||
build_status(dpy, root, has_xkb, cur, sizeof(cur));
|
||||
if (strcmp(cur, last_status) != 0) {
|
||||
snprintf(last_status, sizeof(last_status), "%s", cur);
|
||||
XStoreName(dpy, root, cur);
|
||||
XFlush(dpy);
|
||||
}
|
||||
|
||||
while (XPending(dpy) > 0) {
|
||||
XEvent ev;
|
||||
XNextEvent(dpy, &ev);
|
||||
}
|
||||
if (XPending(dpy) > 0) continue;
|
||||
|
||||
time_t now = time(NULL);
|
||||
struct tm tmbuf;
|
||||
@@ -178,6 +261,9 @@ int main(void) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (pfds[1].revents & (POLLERR | POLLHUP)) {
|
||||
running = false;
|
||||
}
|
||||
if (netfd >= 0 && nfds > 2 && (pfds[2].revents & POLLIN)) {
|
||||
char nbuf[1024];
|
||||
while (read(netfd, nbuf, sizeof(nbuf)) > 0) {}
|
||||
|
||||
Reference in new issue
Block a user