dwmblocks-custom/main.c

313 lines
6.7 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;
static int pipes[LEN(blocks)][2];
2022-04-21 15:34:41 +02:00
static int timer = 0, timerTick = 0, maxInterval = 1;
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;
2022-05-01 11:57:30 +02:00
while (buffer[j] != '\n' && count < CMDLENGTH) {
count++;
// Skip continuation bytes, if any.
char ch = buffer[j];
int skip = 1;
while ((ch & 0xc0) > 0x80)
ch <<= 1, skip++;
j += skip;
}
// 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
2022-05-01 11:57:30 +02:00
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);
}
2022-06-26 23:08:11 +02:00
int setupX() {
dpy = XOpenDisplay(NULL);
if (!dpy)
return 1;
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
return 0;
}
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
2021-04-17 12:17:40 +02:00
XStoreName(dpy, root, statusBar[0]);
2022-06-26 23:08:11 +02:00
XFlush(dpy);
2021-03-20 10:52:45 +01:00
}
void signalHandler() {
struct signalfd_siginfo info;
read(signalFD, &info, sizeof(info));
unsigned int signal = info.ssi_signo;
switch (signal) {
case SIGALRM:
// Schedule the next timer event and execute blocks
alarm(timerTick);
execBlocks(timer);
// Wrap `timer` to the interval [1, `maxInterval`]
timer = (timer + timerTick - 1) % maxInterval + 1;
return;
case SIGUSR1:
// Update all blocks on receiving 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() {
sigset_t handledSignals;
sigemptyset(&handledSignals);
sigaddset(&handledSignals, SIGUSR1);
sigaddset(&handledSignals, SIGALRM);
// Append all block signals to `handledSignals`
for (int i = 0; i < LEN(blocks); i++)
if (blocks[i].signal > 0)
sigaddset(&handledSignals, SIGRTMIN + blocks[i].signal);
// Create a signal file descriptor for epoll to watch
signalFD = signalfd(-1, &handledSignals, 0);
event.data.u32 = LEN(blocks);
epoll_ctl(epollFD, EPOLL_CTL_ADD, signalFD, &event);
// Block all realtime and handled signals
for (int i = SIGRTMIN; i <= SIGRTMAX; i++)
sigaddset(&handledSignals, i);
sigprocmask(SIG_BLOCK, &handledSignals, 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);
}
2021-04-13 07:35:21 +02:00
void statusLoop() {
// Update all blocks initially
2022-04-22 02:49:39 +02:00
raise(SIGALRM);
struct epoll_event events[LEN(blocks) + 1];
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))
updateBlock(id);
else
signalHandler();
}
if (eventCount != -1)
writeStatus();
}
}
void init() {
epollFD = epoll_create(LEN(blocks));
event.events = EPOLLIN;
for (int i = 0; i < LEN(blocks); i++) {
// Append each block's pipe to `epollFD`
pipe(pipes[i]);
event.data.u32 = i;
epoll_ctl(epollFD, EPOLL_CTL_ADD, pipes[i][0], &event);
// Calculate the max interval and tick size for the timer
if (blocks[i].interval) {
maxInterval = MAX(blocks[i].interval, maxInterval);
timerTick = gcd(blocks[i].interval, timerTick);
}
}
setupSignals();
2021-04-13 07:35:21 +02:00
}
int main(const int argc, const char* argv[]) {
2022-06-26 23:08:11 +02:00
if (setupX()) {
fprintf(stderr, "dwmblocks: Failed to open display\n");
return 1;
}
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();
statusLoop();
2022-06-26 23:08:11 +02:00
XCloseDisplay(dpy);
close(epollFD);
close(signalFD);
for (int i = 0; i < LEN(pipes); i++)
closePipe(pipes[i]);
return 0;
2021-03-20 10:52:45 +01:00
}