dwmblocks/main.c

321 lines
7.0 KiB
C
Raw Normal View History

2021-10-13 03:07:55 +02:00
#define _GNU_SOURCE
2021-03-20 10:52:45 +01:00
#include <X11/Xlib.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/signalfd.h>
2021-03-20 10:52:45 +01:00
#include <time.h>
#include <unistd.h>
#define LEN(arr) (sizeof(arr) / sizeof(arr[0]))
2022-01-16 12:00:39 +01:00
#define MAX(a, b) (a > b ? a : b)
2022-01-03 06:04:20 +01:00
#define BLOCK(cmd, interval, signal) \
{ "echo \"$(" cmd ")\"", interval, signal }
2022-01-03 06:04:20 +01:00
typedef const struct {
const char* command;
2021-10-12 06:36:31 +02:00
const unsigned int interval;
const unsigned int signal;
} Block;
2021-03-20 10:52:45 +01:00
#include "config.h"
2021-10-17 03:58:15 +02:00
#ifdef CLICKABLE_BLOCKS
#undef CLICKABLE_BLOCKS
#define CLICKABLE_BLOCKS 1
#else
#define CLICKABLE_BLOCKS 0
#endif
2022-01-03 06:04:20 +01:00
#ifdef LEADING_DELIMITER
#undef LEADING_DELIMITER
#define LEADING_DELIMITER 1
#else
#define LEADING_DELIMITER 0
#endif
static Display* dpy;
2021-03-20 10:52:45 +01:00
static int screen;
static Window root;
static unsigned short statusContinue = 1;
static struct epoll_event event, events[LEN(blocks) + 2];
static int pipes[LEN(blocks)][2];
static int timerPipe[2];
static int signalFD;
static int epollFD;
2022-01-18 19:28:48 +01:00
static int execLock = 0;
// Longest UTF-8 character is 4 bytes long
static char outputs[LEN(blocks)][CMDLENGTH * 4 + 1 + CLICKABLE_BLOCKS];
static char statusBar[2][LEN(blocks) * (LEN(outputs[0]) - 1) + (LEN(blocks) - 1 + LEADING_DELIMITER) * (LEN(DELIMITER) - 1) + 1];
2021-04-17 12:17:40 +02:00
void (*writeStatus)();
2021-04-13 07:35:21 +02:00
int gcd(int a, int b) {
int temp;
while (b > 0) {
temp = a % b;
a = b;
b = temp;
2021-03-20 10:52:45 +01:00
}
2021-04-13 07:35:21 +02:00
return a;
2021-03-20 10:52:45 +01:00
}
void closePipe(int* pipe) {
close(pipe[0]);
close(pipe[1]);
}
void execBlock(int i, const char* button) {
// Ensure only one child process exists per block at an instance
if (execLock & 1 << i)
return;
// Lock execution of block until current instance finishes execution
execLock |= 1 << i;
2021-04-17 12:17:40 +02:00
if (fork() == 0) {
close(pipes[i][0]);
dup2(pipes[i][1], STDOUT_FILENO);
2022-01-18 19:28:48 +01:00
close(pipes[i][1]);
2021-10-12 06:36:31 +02:00
if (button)
setenv("BLOCK_BUTTON", button, 1);
execl("/bin/sh", "sh", "-c", blocks[i].command, (char*)NULL);
2022-01-18 19:28:48 +01:00
_exit(1);
2021-03-20 10:52:45 +01:00
}
}
2022-01-16 12:00:39 +01:00
void execBlocks(unsigned int time) {
2021-04-17 12:17:40 +02:00
for (int i = 0; i < LEN(blocks); i++)
if (time == 0 || (blocks[i].interval != 0 && time % blocks[i].interval == 0))
execBlock(i, NULL);
2021-04-13 07:35:21 +02:00
}
int getStatus(char* new, char* old) {
2021-03-20 10:52:45 +01:00
strcpy(old, new);
new[0] = '\0';
2021-10-13 17:58:49 +02:00
2021-04-17 12:17:40 +02:00
for (int i = 0; i < LEN(blocks); i++) {
2022-01-03 06:04:20 +01:00
#if LEADING_DELIMITER
2021-10-15 11:56:08 +02:00
if (strlen(outputs[i]))
#else
2021-10-15 11:56:08 +02:00
if (strlen(new) && strlen(outputs[i]))
#endif
2021-04-17 12:17:40 +02:00
strcat(new, DELIMITER);
2021-10-12 06:36:31 +02:00
strcat(new, outputs[i]);
2021-03-20 10:52:45 +01:00
}
return strcmp(new, old);
}
void updateBlock(int i) {
2021-10-15 11:56:08 +02:00
char* output = outputs[i];
2022-01-03 06:04:20 +01:00
char buffer[LEN(outputs[0]) - CLICKABLE_BLOCKS];
int bytesRead = read(pipes[i][0], buffer, LEN(buffer));
// Trim UTF-8 string to desired length
int count = 0, j = 0;
while (buffer[j] != '\n' && count <= CMDLENGTH) {
// Increment count for non-continuation bytes
if ((buffer[j++] & 0xc0) != 0x80)
count++;
}
// Cache last character and replace it with a trailing space
char ch = buffer[j];
2021-12-30 06:33:42 +01:00
buffer[j] = ' ';
// Trim trailing spaces
while (j >= 0 && buffer[j] == ' ') j--;
buffer[j + 1] = 0;
// Clear the pipe
if (bytesRead == LEN(buffer)) {
while (ch != '\n' && read(pipes[i][0], &ch, 1) == 1)
;
}
2021-10-17 03:58:15 +02:00
#if CLICKABLE_BLOCKS
if (bytesRead > 1 && blocks[i].signal > 0) {
output[0] = blocks[i].signal;
output++;
}
2021-10-17 03:58:15 +02:00
#endif
2021-10-17 03:58:15 +02:00
strcpy(output, buffer);
// Remove execution lock for the current block
execLock &= ~(1 << i);
}
2021-04-13 07:35:21 +02:00
void debug() {
// Only write out if text has changed
2021-10-12 06:36:31 +02:00
if (!getStatus(statusBar[0], statusBar[1]))
return;
2021-04-13 07:35:21 +02:00
2021-04-17 12:17:40 +02:00
write(STDOUT_FILENO, statusBar[0], strlen(statusBar[0]));
2021-04-13 07:35:21 +02:00
write(STDOUT_FILENO, "\n", 1);
}
2021-03-20 10:52:45 +01:00
void setRoot() {
2021-03-22 06:42:52 +01:00
// Only set root if text has changed
2021-10-12 06:36:31 +02:00
if (!getStatus(statusBar[0], statusBar[1]))
return;
2021-03-20 10:52:45 +01:00
Display* d = XOpenDisplay(NULL);
2021-10-12 06:36:31 +02:00
if (d)
dpy = d;
2021-03-20 10:52:45 +01:00
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
2021-04-17 12:17:40 +02:00
XStoreName(dpy, root, statusBar[0]);
2021-03-20 10:52:45 +01:00
XCloseDisplay(dpy);
}
void signalHandler() {
struct signalfd_siginfo info;
read(signalFD, &info, sizeof(info));
unsigned int signal = info.ssi_signo;
// Update all blocks on receiving SIGUSR1
if (signal == SIGUSR1) {
execBlocks(0);
return;
}
2021-03-20 10:52:45 +01:00
for (int j = 0; j < LEN(blocks); j++) {
if (blocks[j].signal == signal - SIGRTMIN) {
char button[] = {'0' + info.ssi_int & 0xff, 0};
execBlock(j, button);
break;
}
2021-10-12 06:36:31 +02:00
}
}
void termHandler() {
statusContinue = 0;
2021-03-20 10:52:45 +01:00
}
2021-04-13 07:35:21 +02:00
void setupSignals() {
// Ignore SIGUSR1 and all realtime signals
sigset_t ignoredSignals;
sigemptyset(&ignoredSignals);
sigaddset(&ignoredSignals, SIGUSR1);
for (int i = SIGRTMIN; i <= SIGRTMAX; i++)
sigaddset(&ignoredSignals, i);
sigprocmask(SIG_BLOCK, &ignoredSignals, NULL);
// Handle termination signals
2021-04-17 12:17:40 +02:00
signal(SIGINT, termHandler);
signal(SIGTERM, termHandler);
2021-04-13 07:35:21 +02:00
// Avoid zombie subprocesses
2021-04-17 12:17:40 +02:00
struct sigaction sa;
sa.sa_handler = SIG_DFL;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_NOCLDWAIT;
sigaction(SIGCHLD, &sa, 0);
// Handle block update signals
sigset_t handledSignals;
sigemptyset(&handledSignals);
sigaddset(&handledSignals, SIGUSR1);
for (int i = 0; i < LEN(blocks); i++)
2021-08-31 13:48:19 +02:00
if (blocks[i].signal > 0)
sigaddset(&handledSignals, SIGRTMIN + blocks[i].signal);
signalFD = signalfd(-1, &handledSignals, 0);
event.data.u32 = LEN(blocks) + 1;
epoll_ctl(epollFD, EPOLL_CTL_ADD, signalFD, &event);
}
2021-04-13 07:35:21 +02:00
void statusLoop() {
while (statusContinue) {
int eventCount = epoll_wait(epollFD, events, LEN(events), -1);
for (int i = 0; i < eventCount; i++) {
unsigned short id = events[i].data.u32;
if (id == LEN(blocks)) {
unsigned int j = 0;
read(timerPipe[0], &j, sizeof(j));
execBlocks(j);
} else if (id < LEN(blocks)) {
updateBlock(events[i].data.u32);
} else {
signalHandler();
}
}
if (eventCount != -1)
writeStatus();
}
}
void timerLoop() {
close(timerPipe[0]);
2021-04-13 07:35:21 +02:00
2022-01-16 12:00:39 +01:00
unsigned int sleepInterval = 0;
unsigned int maxInterval = 0;
2021-04-17 12:17:40 +02:00
for (int i = 0; i < LEN(blocks); i++)
2022-01-16 12:00:39 +01:00
if (blocks[i].interval) {
maxInterval = MAX(blocks[i].interval, maxInterval);
2021-04-13 07:35:21 +02:00
sleepInterval = gcd(blocks[i].interval, sleepInterval);
2022-01-16 12:00:39 +01:00
}
2021-04-13 07:35:21 +02:00
2022-01-16 12:00:39 +01:00
unsigned int i = 0;
2021-04-13 07:35:21 +02:00
struct timespec sleepTime = {sleepInterval, 0};
struct timespec toSleep = sleepTime;
while (statusContinue) {
// Notify parent to update blocks
write(timerPipe[1], &i, sizeof(i));
2021-04-13 07:35:21 +02:00
// Wrap `i` to the interval [1, `maxInterval`]
2022-01-16 12:00:39 +01:00
i = (i + sleepInterval - 1) % maxInterval + 1;
// Sleep for `sleepTime` even on being interrupted
while (nanosleep(&toSleep, &toSleep) == -1)
;
toSleep = sleepTime;
2021-04-13 07:35:21 +02:00
}
close(timerPipe[1]);
2022-01-18 19:28:48 +01:00
_exit(0);
}
void init() {
epollFD = epoll_create(LEN(blocks) + 1);
event.events = EPOLLIN;
for (int i = 0; i < LEN(blocks); i++) {
pipe(pipes[i]);
event.data.u32 = i;
epoll_ctl(epollFD, EPOLL_CTL_ADD, pipes[i][0], &event);
}
pipe(timerPipe);
event.data.u32 = LEN(blocks);
epoll_ctl(epollFD, EPOLL_CTL_ADD, timerPipe[0], &event);
setupSignals();
2021-04-13 07:35:21 +02:00
}
int main(const int argc, const char* argv[]) {
2021-04-17 12:17:40 +02:00
writeStatus = setRoot;
2021-03-20 10:52:45 +01:00
for (int i = 0; i < argc; i++)
2021-10-12 06:36:31 +02:00
if (!strcmp("-d", argv[i]))
writeStatus = debug;
2021-04-17 12:17:40 +02:00
init();
// Ensure that `timerLoop()` only runs in the fork
if (fork() == 0)
2022-01-16 12:00:39 +01:00
timerLoop();
else
statusLoop();
close(epollFD);
close(signalFD);
closePipe(timerPipe);
for (int i = 0; i < LEN(pipes); i++)
closePipe(pipes[i]);
return 0;
2021-03-20 10:52:45 +01:00
}