279 lines
8.4 KiB
C
279 lines
8.4 KiB
C
#define _POSIX_C_SOURCE 200809L
|
|
#include "anode_plat.h"
|
|
#include "anode_xkb_parse.h"
|
|
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <poll.h>
|
|
#include <signal.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
#include <X11/Xatom.h>
|
|
#include <X11/XKBlib.h>
|
|
#include <X11/Xlib.h>
|
|
|
|
#ifdef __linux__
|
|
#include <linux/netlink.h>
|
|
#include <linux/rtnetlink.h>
|
|
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
|
|
#include <net/route.h>
|
|
#endif
|
|
|
|
static int g_sig_pipe[2] = {-1, -1};
|
|
|
|
static void sig_handler(int sig) {
|
|
int saved = errno;
|
|
char c = (sig == SIGUSR1 || sig == SIGHUP) ? 'R' : 'Q';
|
|
if (g_sig_pipe[1] >= 0) {
|
|
ssize_t unused = write(g_sig_pipe[1], &c, 1);
|
|
(void)unused;
|
|
}
|
|
errno = saved;
|
|
}
|
|
|
|
static int open_net_watch_fd(void) {
|
|
#ifdef __linux__
|
|
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
|
if (fd >= 0) {
|
|
struct sockaddr_nl sa;
|
|
memset(&sa, 0, sizeof(sa));
|
|
sa.nl_family = AF_NETLINK;
|
|
sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR;
|
|
if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0) {
|
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
|
|
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
|
|
return fd;
|
|
}
|
|
close(fd);
|
|
}
|
|
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
|
|
int fd = socket(PF_ROUTE, SOCK_RAW, 0);
|
|
if (fd >= 0) {
|
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
|
|
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
|
|
return fd;
|
|
}
|
|
#endif
|
|
return -1;
|
|
}
|
|
|
|
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] = "";
|
|
if (anode_audio_get_default(false, &vol, &muted)) {
|
|
if (muted) snprintf(vol_str, sizeof(vol_str), "VOL MUTE");
|
|
else snprintf(vol_str, sizeof(vol_str), "VOL %d%%", vol);
|
|
}
|
|
|
|
anode_net_iface_t ifaces[ANODE_MAX_IFACES];
|
|
int n_if = anode_list_net_ifaces(ifaces, ANODE_MAX_IFACES);
|
|
const anode_net_iface_t *best = NULL;
|
|
for (int i = 0; i < n_if; i++) {
|
|
if (ifaces[i].is_loopback || !ifaces[i].is_up) continue;
|
|
if (ifaces[i].ipv4[0]) {
|
|
best = &ifaces[i];
|
|
if (ifaces[i].is_wireless) break;
|
|
} else if (!best && ifaces[i].ipv6[0]) {
|
|
best = &ifaces[i];
|
|
}
|
|
}
|
|
char net_str[128];
|
|
if (best) {
|
|
snprintf(net_str, sizeof(net_str), "%s %s",
|
|
best->is_wireless ? "WiFi" : best->name,
|
|
best->ipv4[0] ? best->ipv4 : best->ipv6);
|
|
} else {
|
|
snprintf(net_str, sizeof(net_str), "NET off");
|
|
}
|
|
|
|
anode_battery_info_t bat;
|
|
anode_read_battery(&bat);
|
|
char bat_str[48] = "";
|
|
if (bat.present) {
|
|
const char *tag = bat.on_ac ? "+" : "-";
|
|
if (bat.capacity >= 0) snprintf(bat_str, sizeof(bat_str), "BAT %d%%%s", bat.capacity, tag);
|
|
else snprintf(bat_str, sizeof(bat_str), "BAT %s", bat.status);
|
|
} else {
|
|
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] && 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);
|
|
}
|
|
}
|
|
|
|
int main(void) {
|
|
if (pipe(g_sig_pipe) != 0) return 1;
|
|
for (int i = 0; i < 2; i++) {
|
|
fcntl(g_sig_pipe[i], F_SETFL, fcntl(g_sig_pipe[i], F_GETFL, 0) | O_NONBLOCK);
|
|
fcntl(g_sig_pipe[i], F_SETFD, fcntl(g_sig_pipe[i], F_GETFD, 0) | FD_CLOEXEC);
|
|
}
|
|
|
|
struct sigaction sa;
|
|
memset(&sa, 0, sizeof(sa));
|
|
sa.sa_handler = sig_handler;
|
|
sigaction(SIGTERM, &sa, NULL);
|
|
sigaction(SIGINT, &sa, NULL);
|
|
sigaction(SIGUSR1, &sa, NULL);
|
|
sigaction(SIGHUP, &sa, NULL);
|
|
|
|
Display *dpy = XOpenDisplay(NULL);
|
|
if (!dpy) {
|
|
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();
|
|
|
|
char last_status[512] = {0};
|
|
bool running = true;
|
|
|
|
while (running) {
|
|
while (XPending(dpy) > 0) {
|
|
XEvent ev;
|
|
XNextEvent(dpy, &ev);
|
|
}
|
|
|
|
char cur[512];
|
|
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);
|
|
}
|
|
|
|
if (XPending(dpy) > 0) continue;
|
|
|
|
time_t now = time(NULL);
|
|
struct tm tmbuf;
|
|
localtime_r(&now, &tmbuf);
|
|
int timeout_ms = (60 - tmbuf.tm_sec) * 1000 + 50;
|
|
if (timeout_ms < 1000) timeout_ms = 60000;
|
|
|
|
struct pollfd pfds[3];
|
|
nfds_t nfds = 0;
|
|
pfds[nfds++] = (struct pollfd){.fd = g_sig_pipe[0], .events = POLLIN};
|
|
pfds[nfds++] = (struct pollfd){.fd = xfd, .events = POLLIN};
|
|
if (netfd >= 0) pfds[nfds++] = (struct pollfd){.fd = netfd, .events = POLLIN};
|
|
|
|
int pr = poll(pfds, nfds, timeout_ms);
|
|
if (pr < 0 && errno != EINTR) break;
|
|
|
|
if (pfds[0].revents & POLLIN) {
|
|
char buf[32];
|
|
ssize_t n;
|
|
while ((n = read(g_sig_pipe[0], buf, sizeof(buf))) > 0) {
|
|
for (ssize_t i = 0; i < n; i++) {
|
|
if (buf[i] == 'Q') running = false;
|
|
}
|
|
}
|
|
}
|
|
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) {}
|
|
}
|
|
}
|
|
|
|
if (netfd >= 0) close(netfd);
|
|
close(g_sig_pipe[0]);
|
|
close(g_sig_pipe[1]);
|
|
XCloseDisplay(dpy);
|
|
return 0;
|
|
}
|