192 lines
5.3 KiB
C
192 lines
5.3 KiB
C
#define _POSIX_C_SOURCE 200809L
|
|||
|
|
#include "anode_plat.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/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 void build_status(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");
|
||
|
|
}
|
||
|
|
|
||
|
|
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]) {
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
Window root = DefaultRootWindow(dpy);
|
||
|
|
int xfd = ConnectionNumber(dpy);
|
||
|
|
int netfd = open_net_watch_fd();
|
||
|
|
|
||
|
|
char last_status[512] = {0};
|
||
|
|
bool running = true;
|
||
|
|
|
||
|
|
while (running) {
|
||
|
|
char cur[512];
|
||
|
|
build_status(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);
|
||
|
|
}
|
||
|
|
|
||
|
|
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 (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;
|
||
|
|
}
|