Compare commits

..

10 commits

Author SHA1 Message Date
filippo-ferrari
4aff88012a config changes 2024-01-27 15:31:01 +01:00
Luke Smith
1c9744ac7d
Merge branch 'frei-0xff-master' 2023-10-30 00:30:32 -04:00
Luke Smith
5d6b790129
Merge branch 'master' of https://github.com/frei-0xff/dwmblocks into frei-0xff-master 2023-10-30 00:30:27 -04:00
Luke Smith
7c2f342765
a 2023-10-30 00:30:14 -04:00
frei-0xff
c3fef3b7ee Replaced signal handlers with signalfd + pool 2023-10-29 21:50:45 +02:00
Luke Smith
30fe169632
sb-music inactive by default 2023-04-09 14:02:54 -04:00
Luke Smith
9928739768
Merge pull request #113 from appeasementPolitik/async
Give status bar codes as parameters for async dwmblocks modules
2023-04-09 17:59:20 +00:00
appeasementPolitik
2c031a5113
Reflect changes made to sb-price 2023-04-05 08:11:55 +00:00
appeasementPolitik
6ae770c5cd
Give status bar codes as parameters for async dwmblocks modules 2023-04-04 07:30:25 +00:00
Luke Smith
5a6fa8d550 sb-doppler script on by default 2022-08-20 08:01:57 -04:00
2 changed files with 69 additions and 67 deletions

View file

@ -7,24 +7,24 @@ static const Block blocks[] = {
{"", "sb-music", 0, 11},
{"", "sb-pacpackages", 0, 8},
{"", "sb-news", 0, 6},
/* {"", "sb-price lbc \"LBRY Token\" 📚", 9000, 22}, */
/* {"", "sb-price bat \"Basic Attention Token\" 🦁", 9000, 20}, */
/* {"", "sb-price link \"Chainlink\" 🔗", 300, 25}, */
/* {"", "sb-price xmr \"Monero\" 🔒", 9000, 24}, */
/* {"", "sb-price eth Ethereum 🍸", 9000, 23}, */
/* {"", "sb-price btc Bitcoin 💰", 9000, 21}, */
{"", "sb-torrent", 20, 7},
/* {"", "sb-memory", 10, 14}, */
/* {"", "sb-cpu", 10, 18}, */
/* {"", "sb-price xmr-btc \"Monero to Bitcoin\" 🔒 25", 9000, 25}, */
/* {"", "sb-price xmr Monero 🔒 24", 9000, 24}, */
/* {"", "sb-price eth Ethereum 🍸 23", 9000, 23}, */
/* {"", "sb-price btc Bitcoin 💰 21", 9000, 21}, */
/*{"", "sb-torrent", 20, 7},*/
{"", "sb-memory", 10, 14},
{"", "sb-cpu", 10, 18},
/* {"", "sb-moonphase", 18000, 17}, */
{"", "sb-forecast", 18000, 5},
/*{"", "sb-doppler", 0, 13},
{"", "sb-forecast", 18000, 5},*/
{"", "sb-mailbox", 180, 12},
{"", "sb-nettraf", 1, 16},
/*{"", "sb-nettraf", 1, 16},*/
{"", "sb-volume", 0, 10},
{"", "sb-battery", 5, 3},
{"", "sb-clock", 60, 1},
{"", "sb-internet", 5, 4},
{"", "sb-help-icon", 0, 15},
/* {"", "sb-iplocate", 0, 27},
{"", "sb-help-icon", 0, 15},*/
};
//Sets delimiter between status commands. NULL character ('\0') means no delimiter.
@ -34,3 +34,4 @@ static char *delim = " ";
// vim with the following line in your vimrc/init.vim:
// autocmd BufWritePost ~/.local/src/dwmblocks/config.h !cd ~/.local/src/dwmblocks/; sudo make install && { killall -q dwmblocks;setsid dwmblocks & }

View file

@ -6,6 +6,8 @@
#include <signal.h>
#include <errno.h>
#include <X11/Xlib.h>
#include <sys/signalfd.h>
#include <poll.h>
#define LENGTH(X) (sizeof(X) / sizeof (X[0]))
#define CMDLENGTH 50
@ -15,16 +17,13 @@ typedef struct {
unsigned int interval;
unsigned int signal;
} Block;
void sighandler(int num);
void buttonhandler(int sig, siginfo_t *si, void *ucontext);
void sighandler();
void buttonhandler(int ssi_int);
void replace(char *str, char old, char new);
void remove_all(char *str, char to_remove);
void getcmds(int time);
#ifndef __OpenBSD__
void getsigcmds(int signal);
void setupsignals();
void sighandler(int signum);
#endif
int getstatus(char *str, char *last);
void setroot();
void statusloop();
@ -39,6 +38,8 @@ static Window root;
static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0};
static char statusstr[2][256];
static int statusContinue = 1;
static int signalFD;
static int timerInterval = -1;
static void (*writestatus) () = setroot;
void replace(char *str, char old, char new)
@ -128,7 +129,6 @@ void getcmds(int time)
}
}
#ifndef __OpenBSD__
void getsigcmds(int signal)
{
const Block *current;
@ -143,22 +143,20 @@ void getsigcmds(int signal)
void setupsignals()
{
struct sigaction sa;
for(int i = SIGRTMIN; i <= SIGRTMAX; i++)
signal(i, SIG_IGN);
for(int i = 0; i < LENGTH(blocks); i++)
{
sigset_t signals;
sigemptyset(&signals);
sigaddset(&signals, SIGALRM); // Timer events
sigaddset(&signals, SIGUSR1); // Button events
// All signals assigned to blocks
for (size_t i = 0; i < LENGTH(blocks); i++)
if (blocks[i].signal > 0)
{
signal(SIGRTMIN+blocks[i].signal, sighandler);
sigaddset(&sa.sa_mask, SIGRTMIN+blocks[i].signal);
}
}
sa.sa_sigaction = buttonhandler;
sa.sa_flags = SA_SIGINFO;
sigaction(SIGUSR1, &sa, NULL);
sigaddset(&signals, SIGRTMIN + blocks[i].signal);
// Create signal file descriptor for pooling
signalFD = signalfd(-1, &signals, 0);
// Block all real-time signals
for (int i = SIGRTMIN; i <= SIGRTMAX; i++) sigaddset(&signals, i);
sigprocmask(SIG_BLOCK, &signals, NULL);
// Do not transform children into zombies
struct sigaction sigchld_action = {
.sa_handler = SIG_DFL,
.sa_flags = SA_NOCLDWAIT
@ -166,7 +164,6 @@ void setupsignals()
sigaction(SIGCHLD, &sigchld_action, NULL);
}
#endif
int getstatus(char *str, char *last)
{
@ -206,52 +203,57 @@ void pstdout()
void statusloop()
{
#ifndef __OpenBSD__
setupsignals();
#endif
// first figure out the default wait interval by finding the
// greatest common denominator of the intervals
unsigned int interval = -1;
for(int i = 0; i < LENGTH(blocks); i++){
if(blocks[i].interval){
interval = gcd(blocks[i].interval, interval);
timerInterval = gcd(blocks[i].interval, timerInterval);
}
}
unsigned int i = 0;
int interrupted = 0;
const struct timespec sleeptime = {interval, 0};
struct timespec tosleep = sleeptime;
getcmds(-1);
while(statusContinue)
{
// sleep for tosleep (should be a sleeptime of interval seconds) and put what was left if interrupted back into tosleep
interrupted = nanosleep(&tosleep, &tosleep);
// if interrupted then just go sleep again for the remaining time
if(interrupted == -1){
continue;
}
// if not interrupted then do the calling and writing
getcmds(i);
writestatus();
// then increment since its actually been a second (plus the time it took the commands to run)
i += interval;
// set the time to sleep back to the sleeptime of 1s
tosleep = sleeptime;
getcmds(-1); // Fist time run all commands
raise(SIGALRM); // Schedule first timer event
int ret;
struct pollfd pfd[] = {{.fd = signalFD, .events = POLLIN}};
while (statusContinue) {
// Wait for new signal
ret = poll(pfd, sizeof(pfd) / sizeof(pfd[0]), -1);
if (ret < 0 || !(pfd[0].revents & POLLIN)) break;
sighandler(); // Handle signal
}
}
#ifndef __OpenBSD__
void sighandler(int signum)
void sighandler()
{
getsigcmds(signum-SIGRTMIN);
static int time = 0;
struct signalfd_siginfo si;
int ret = read(signalFD, &si, sizeof(si));
if (ret < 0) return;
int signal = si.ssi_signo;
switch (signal) {
case SIGALRM:
// Execute blocks and schedule the next timer event
getcmds(time);
alarm(timerInterval);
time += timerInterval;
break;
case SIGUSR1:
// Handle buttons
buttonhandler(si.ssi_int);
return;
default:
// Execute the block that has the given signal
getsigcmds(signal - SIGRTMIN);
break;
}
writestatus();
}
void buttonhandler(int sig, siginfo_t *si, void *ucontext)
void buttonhandler(int ssi_int)
{
char button[2] = {'0' + si->si_value.sival_int & 0xff, '\0'};
char button[2] = {'0' + ssi_int & 0xff, '\0'};
pid_t process_id = getpid();
sig = si->si_value.sival_int >> 8;
int sig = ssi_int >> 8;
if (fork() == 0)
{
const Block *current;
@ -271,12 +273,10 @@ void buttonhandler(int sig, siginfo_t *si, void *ucontext)
}
}
#endif
void termhandler(int signum)
{
statusContinue = 0;
exit(0);
}
int main(int argc, char** argv)
@ -291,4 +291,5 @@ int main(int argc, char** argv)
signal(SIGTERM, termhandler);
signal(SIGINT, termhandler);
statusloop();
close(signalFD);
}