Compare commits
No commits in common. "4aff88012a518a0fc91d8c496f310135c863c4df" and "66f31c307adbdcc2505239260ecda24a49eea7af" have entirely different histories.
4aff88012a
...
66f31c307a
2 changed files with 67 additions and 69 deletions
25
config.h
25
config.h
|
@ -7,24 +7,24 @@ static const Block blocks[] = {
|
||||||
{"", "sb-music", 0, 11},
|
{"", "sb-music", 0, 11},
|
||||||
{"", "sb-pacpackages", 0, 8},
|
{"", "sb-pacpackages", 0, 8},
|
||||||
{"", "sb-news", 0, 6},
|
{"", "sb-news", 0, 6},
|
||||||
/* {"", "sb-price xmr-btc \"Monero to Bitcoin\" 🔒 25", 9000, 25}, */
|
/* {"", "sb-price lbc \"LBRY Token\" 📚", 9000, 22}, */
|
||||||
/* {"", "sb-price xmr Monero 🔒 24", 9000, 24}, */
|
/* {"", "sb-price bat \"Basic Attention Token\" 🦁", 9000, 20}, */
|
||||||
/* {"", "sb-price eth Ethereum 🍸 23", 9000, 23}, */
|
/* {"", "sb-price link \"Chainlink\" 🔗", 300, 25}, */
|
||||||
/* {"", "sb-price btc Bitcoin 💰 21", 9000, 21}, */
|
/* {"", "sb-price xmr \"Monero\" 🔒", 9000, 24}, */
|
||||||
/*{"", "sb-torrent", 20, 7},*/
|
/* {"", "sb-price eth Ethereum 🍸", 9000, 23}, */
|
||||||
{"", "sb-memory", 10, 14},
|
/* {"", "sb-price btc Bitcoin 💰", 9000, 21}, */
|
||||||
{"", "sb-cpu", 10, 18},
|
{"", "sb-torrent", 20, 7},
|
||||||
|
/* {"", "sb-memory", 10, 14}, */
|
||||||
|
/* {"", "sb-cpu", 10, 18}, */
|
||||||
/* {"", "sb-moonphase", 18000, 17}, */
|
/* {"", "sb-moonphase", 18000, 17}, */
|
||||||
/*{"", "sb-doppler", 0, 13},
|
{"", "sb-forecast", 18000, 5},
|
||||||
{"", "sb-forecast", 18000, 5},*/
|
|
||||||
{"", "sb-mailbox", 180, 12},
|
{"", "sb-mailbox", 180, 12},
|
||||||
/*{"", "sb-nettraf", 1, 16},*/
|
{"", "sb-nettraf", 1, 16},
|
||||||
{"", "sb-volume", 0, 10},
|
{"", "sb-volume", 0, 10},
|
||||||
{"", "sb-battery", 5, 3},
|
{"", "sb-battery", 5, 3},
|
||||||
{"", "sb-clock", 60, 1},
|
{"", "sb-clock", 60, 1},
|
||||||
{"", "sb-internet", 5, 4},
|
{"", "sb-internet", 5, 4},
|
||||||
/* {"", "sb-iplocate", 0, 27},
|
{"", "sb-help-icon", 0, 15},
|
||||||
{"", "sb-help-icon", 0, 15},*/
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//Sets delimiter between status commands. NULL character ('\0') means no delimiter.
|
//Sets delimiter between status commands. NULL character ('\0') means no delimiter.
|
||||||
|
@ -34,4 +34,3 @@ static char *delim = " ";
|
||||||
// vim with the following line in your vimrc/init.vim:
|
// 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 & }
|
// autocmd BufWritePost ~/.local/src/dwmblocks/config.h !cd ~/.local/src/dwmblocks/; sudo make install && { killall -q dwmblocks;setsid dwmblocks & }
|
||||||
|
|
||||||
|
|
109
dwmblocks.c
109
dwmblocks.c
|
@ -6,8 +6,6 @@
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <X11/Xlib.h>
|
#include <X11/Xlib.h>
|
||||||
#include <sys/signalfd.h>
|
|
||||||
#include <poll.h>
|
|
||||||
#define LENGTH(X) (sizeof(X) / sizeof (X[0]))
|
#define LENGTH(X) (sizeof(X) / sizeof (X[0]))
|
||||||
#define CMDLENGTH 50
|
#define CMDLENGTH 50
|
||||||
|
|
||||||
|
@ -17,13 +15,16 @@ typedef struct {
|
||||||
unsigned int interval;
|
unsigned int interval;
|
||||||
unsigned int signal;
|
unsigned int signal;
|
||||||
} Block;
|
} Block;
|
||||||
void sighandler();
|
void sighandler(int num);
|
||||||
void buttonhandler(int ssi_int);
|
void buttonhandler(int sig, siginfo_t *si, void *ucontext);
|
||||||
void replace(char *str, char old, char new);
|
void replace(char *str, char old, char new);
|
||||||
void remove_all(char *str, char to_remove);
|
void remove_all(char *str, char to_remove);
|
||||||
void getcmds(int time);
|
void getcmds(int time);
|
||||||
|
#ifndef __OpenBSD__
|
||||||
void getsigcmds(int signal);
|
void getsigcmds(int signal);
|
||||||
void setupsignals();
|
void setupsignals();
|
||||||
|
void sighandler(int signum);
|
||||||
|
#endif
|
||||||
int getstatus(char *str, char *last);
|
int getstatus(char *str, char *last);
|
||||||
void setroot();
|
void setroot();
|
||||||
void statusloop();
|
void statusloop();
|
||||||
|
@ -38,8 +39,6 @@ static Window root;
|
||||||
static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0};
|
static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0};
|
||||||
static char statusstr[2][256];
|
static char statusstr[2][256];
|
||||||
static int statusContinue = 1;
|
static int statusContinue = 1;
|
||||||
static int signalFD;
|
|
||||||
static int timerInterval = -1;
|
|
||||||
static void (*writestatus) () = setroot;
|
static void (*writestatus) () = setroot;
|
||||||
|
|
||||||
void replace(char *str, char old, char new)
|
void replace(char *str, char old, char new)
|
||||||
|
@ -129,6 +128,7 @@ void getcmds(int time)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef __OpenBSD__
|
||||||
void getsigcmds(int signal)
|
void getsigcmds(int signal)
|
||||||
{
|
{
|
||||||
const Block *current;
|
const Block *current;
|
||||||
|
@ -143,20 +143,22 @@ void getsigcmds(int signal)
|
||||||
|
|
||||||
void setupsignals()
|
void setupsignals()
|
||||||
{
|
{
|
||||||
sigset_t signals;
|
struct sigaction sa;
|
||||||
sigemptyset(&signals);
|
|
||||||
sigaddset(&signals, SIGALRM); // Timer events
|
for(int i = SIGRTMIN; i <= SIGRTMAX; i++)
|
||||||
sigaddset(&signals, SIGUSR1); // Button events
|
signal(i, SIG_IGN);
|
||||||
// All signals assigned to blocks
|
|
||||||
for (size_t i = 0; i < LENGTH(blocks); i++)
|
for(int i = 0; i < LENGTH(blocks); i++)
|
||||||
|
{
|
||||||
if (blocks[i].signal > 0)
|
if (blocks[i].signal > 0)
|
||||||
sigaddset(&signals, SIGRTMIN + blocks[i].signal);
|
{
|
||||||
// Create signal file descriptor for pooling
|
signal(SIGRTMIN+blocks[i].signal, sighandler);
|
||||||
signalFD = signalfd(-1, &signals, 0);
|
sigaddset(&sa.sa_mask, SIGRTMIN+blocks[i].signal);
|
||||||
// Block all real-time signals
|
}
|
||||||
for (int i = SIGRTMIN; i <= SIGRTMAX; i++) sigaddset(&signals, i);
|
}
|
||||||
sigprocmask(SIG_BLOCK, &signals, NULL);
|
sa.sa_sigaction = buttonhandler;
|
||||||
// Do not transform children into zombies
|
sa.sa_flags = SA_SIGINFO;
|
||||||
|
sigaction(SIGUSR1, &sa, NULL);
|
||||||
struct sigaction sigchld_action = {
|
struct sigaction sigchld_action = {
|
||||||
.sa_handler = SIG_DFL,
|
.sa_handler = SIG_DFL,
|
||||||
.sa_flags = SA_NOCLDWAIT
|
.sa_flags = SA_NOCLDWAIT
|
||||||
|
@ -164,6 +166,7 @@ void setupsignals()
|
||||||
sigaction(SIGCHLD, &sigchld_action, NULL);
|
sigaction(SIGCHLD, &sigchld_action, NULL);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
int getstatus(char *str, char *last)
|
int getstatus(char *str, char *last)
|
||||||
{
|
{
|
||||||
|
@ -203,57 +206,52 @@ void pstdout()
|
||||||
|
|
||||||
void statusloop()
|
void statusloop()
|
||||||
{
|
{
|
||||||
|
#ifndef __OpenBSD__
|
||||||
setupsignals();
|
setupsignals();
|
||||||
|
#endif
|
||||||
// first figure out the default wait interval by finding the
|
// first figure out the default wait interval by finding the
|
||||||
// greatest common denominator of the intervals
|
// greatest common denominator of the intervals
|
||||||
|
unsigned int interval = -1;
|
||||||
for(int i = 0; i < LENGTH(blocks); i++){
|
for(int i = 0; i < LENGTH(blocks); i++){
|
||||||
if(blocks[i].interval){
|
if(blocks[i].interval){
|
||||||
timerInterval = gcd(blocks[i].interval, timerInterval);
|
interval = gcd(blocks[i].interval, interval);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
getcmds(-1); // Fist time run all commands
|
unsigned int i = 0;
|
||||||
raise(SIGALRM); // Schedule first timer event
|
int interrupted = 0;
|
||||||
int ret;
|
const struct timespec sleeptime = {interval, 0};
|
||||||
struct pollfd pfd[] = {{.fd = signalFD, .events = POLLIN}};
|
struct timespec tosleep = sleeptime;
|
||||||
while (statusContinue) {
|
getcmds(-1);
|
||||||
// Wait for new signal
|
while(statusContinue)
|
||||||
ret = poll(pfd, sizeof(pfd) / sizeof(pfd[0]), -1);
|
{
|
||||||
if (ret < 0 || !(pfd[0].revents & POLLIN)) break;
|
// sleep for tosleep (should be a sleeptime of interval seconds) and put what was left if interrupted back into tosleep
|
||||||
sighandler(); // Handle signal
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void sighandler()
|
#ifndef __OpenBSD__
|
||||||
|
void sighandler(int signum)
|
||||||
{
|
{
|
||||||
static int time = 0;
|
getsigcmds(signum-SIGRTMIN);
|
||||||
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();
|
writestatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
void buttonhandler(int ssi_int)
|
void buttonhandler(int sig, siginfo_t *si, void *ucontext)
|
||||||
{
|
{
|
||||||
char button[2] = {'0' + ssi_int & 0xff, '\0'};
|
char button[2] = {'0' + si->si_value.sival_int & 0xff, '\0'};
|
||||||
pid_t process_id = getpid();
|
pid_t process_id = getpid();
|
||||||
int sig = ssi_int >> 8;
|
sig = si->si_value.sival_int >> 8;
|
||||||
if (fork() == 0)
|
if (fork() == 0)
|
||||||
{
|
{
|
||||||
const Block *current;
|
const Block *current;
|
||||||
|
@ -273,10 +271,12 @@ void buttonhandler(int ssi_int)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
void termhandler(int signum)
|
void termhandler(int signum)
|
||||||
{
|
{
|
||||||
statusContinue = 0;
|
statusContinue = 0;
|
||||||
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
|
@ -291,5 +291,4 @@ int main(int argc, char** argv)
|
||||||
signal(SIGTERM, termhandler);
|
signal(SIGTERM, termhandler);
|
||||||
signal(SIGINT, termhandler);
|
signal(SIGINT, termhandler);
|
||||||
statusloop();
|
statusloop();
|
||||||
close(signalFD);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue