Refactor code and remove rate-limiting logic

This commit is contained in:
Utkarsh Verma 2022-01-17 13:10:43 +05:30
parent 589d320b2e
commit 2e1e93c3da
No known key found for this signature in database
GPG Key ID: 817656CF818EFCCC

26
main.c
View File

@ -1,6 +1,5 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <fcntl.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -12,8 +11,6 @@
#define LEN(arr) (sizeof(arr) / sizeof(arr[0])) #define LEN(arr) (sizeof(arr) / sizeof(arr[0]))
#define MAX(a, b) (a > b ? a : b) #define MAX(a, b) (a > b ? a : b)
#define POLL_INTERVAL 50
#define BLOCK(cmd, interval, signal) \ #define BLOCK(cmd, interval, signal) \
{ "echo \"$(" cmd ")\"", interval, signal } { "echo \"$(" cmd ")\"", interval, signal }
@ -176,10 +173,6 @@ void signalHandler() {
break; break;
} }
} }
// Clear the pipe after each poll to limit number of signals handled
while (read(signalFD, &info, sizeof(info)) != -1)
;
} }
void termHandler() { void termHandler() {
@ -205,19 +198,14 @@ void setupSignals() {
sigaddset(&sigset, SIGRTMIN + blocks[i].signal); sigaddset(&sigset, SIGRTMIN + blocks[i].signal);
signalFD = signalfd(-1, &sigset, 0); signalFD = signalfd(-1, &sigset, 0);
fcntl(signalFD, F_SETFL, O_NONBLOCK);
sigprocmask(SIG_BLOCK, &sigset, NULL); sigprocmask(SIG_BLOCK, &sigset, NULL);
event.data.u32 = LEN(blocks) + 1; event.data.u32 = LEN(blocks) + 1;
epoll_ctl(epollFD, EPOLL_CTL_ADD, signalFD, &event); epoll_ctl(epollFD, EPOLL_CTL_ADD, signalFD, &event);
} }
void statusLoop() { void statusLoop() {
// Poll every `POLL_INTERVAL` milliseconds
const struct timespec pollInterval = {.tv_nsec = POLL_INTERVAL * 1000000L};
struct timespec toSleep = pollInterval;
while (statusContinue) { while (statusContinue) {
int eventCount = epoll_wait(epollFD, events, LEN(events), 1); int eventCount = epoll_wait(epollFD, events, LEN(events), -1);
for (int i = 0; i < eventCount; i++) { for (int i = 0; i < eventCount; i++) {
unsigned int id = events[i].data.u32; unsigned int id = events[i].data.u32;
@ -233,11 +221,6 @@ void statusLoop() {
} }
if (eventCount != -1) if (eventCount != -1)
writeStatus(); writeStatus();
// Sleep for `pollInterval` even on being interrupted
while (nanosleep(&toSleep, &toSleep) == -1)
;
toSleep = pollInterval;
} }
} }
@ -297,10 +280,11 @@ int main(const int argc, const char* argv[]) {
init(); init();
if (fork()) // Ensure that `timerLoop()` only runs in the fork
statusLoop(); if (fork() == 0)
else
timerLoop(); timerLoop();
else
statusLoop();
close(epollFD); close(epollFD);
close(signalFD); close(signalFD);