From d747fa7bc113b32b7e07133452e85a48b4d2796f Mon Sep 17 00:00:00 2001 From: dfuehrer Date: Sat, 13 Mar 2021 22:48:50 -0700 Subject: [PATCH 1/7] changed to my status bar things that i want --- config.h | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/config.h b/config.h index 6106bba..ce85fe0 100644 --- a/config.h +++ b/config.h @@ -1,32 +1,20 @@ //Modify this file to change what commands output to your statusbar, and recompile using the make command. static const Block blocks[] = { /*Icon*/ /*Command*/ /*Update Interval*/ /*Update Signal*/ - {"", "cat /tmp/recordingicon 2>/dev/null", 0, 9}, /* {"", "music", 0, 11},*/ - {"", "pacpackages", 0, 8}, - {"", "news", 0, 6}, - /* {"", "crypto", 0, 13}, */ - /* {"", "price bat \"Basic Attention Token\" 🦁", 0, 20}, */ - /* {"", "price btc Bitcoin 💰", 0, 21}, */ - /* {"", "price lbc \"LBRY Token\" 📚", 0, 22}, */ - {"", "torrent", 20, 7}, + /* {"", "pacpackages", 0, 8}, */ /* {"", "memory", 10, 14}, */ - /* {"", "cpu", 10, 18}, */ - /* {"", "moonphase", 18000, 17}, */ - {"", "weather", 18000, 5}, - {"", "mailbox", 180, 12}, + /* {"", "weather", 18000, 5}, */ + /* {"", "mailbox", 180, 12}, */ /* {"", "nettraf", 1, 16}, */ - {"", "volume", 0, 10}, + /* {"", "volume", 0, 10}, */ + /* {"", "internet", 5, 4}, */ + {"", "stalonetray.sh", 0, 27}, + {"", "cpupercent.sh", 1, 19}, {"", "battery", 5, 3}, - {"", "clock", 60, 1}, - {"", "internet", 5, 4}, - {"", "help-icon", 0, 15}, + {"", "clock", 1, 1}, }; //sets delimeter between status commands. NULL character ('\0') means no delimeter. -static char *delim = " "; +static char *delim = "|"; -// Have dwmblocks automatically recompile and run when you edit this file in -// 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 & } From 73fd031a1d8743bae3b8988a3597aa613cc86a92 Mon Sep 17 00:00:00 2001 From: dfuehrer Date: Sat, 13 Mar 2021 22:49:34 -0700 Subject: [PATCH 2/7] sorta fixed bug with sleep and timing where interrupting dwmblocks by clicking on the bar would interrupt the sleep causing it to reload everything sooner than it should, which would cause slower things to not update and disappear untill they were reloaded. This was especially a problem with using scrolling on blocks. Still occasionally breaks but is much less horrible. --- dwmblocks.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/dwmblocks.c b/dwmblocks.c index d135a7d..1d31a65 100644 --- a/dwmblocks.c +++ b/dwmblocks.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #define LENGTH(X) (sizeof(X) / sizeof (X[0])) @@ -91,8 +92,9 @@ void getcmds(int time) for(int i = 0; i < LENGTH(blocks); i++) { current = blocks + i; - if ((current->interval != 0 && time % current->interval == 0) || time == -1) + if ((current->interval != 0 && time % current->interval == 0) || time == -1){ getcmd(current,statusbar[i]); + } } } @@ -103,8 +105,9 @@ void getsigcmds(int signal) for (int i = 0; i < LENGTH(blocks); i++) { current = blocks + i; - if (current->signal == signal) + if (current->signal == signal){ getcmd(current,statusbar[i]); + } } } @@ -173,13 +176,27 @@ void statusloop() setupsignals(); #endif int i = 0; + int previ = -1; + int gotscrewed = 0; + struct timespec sleeptime = {1, 0}; + struct timespec left; getcmds(-1); while(statusContinue) { - getcmds(i); - writestatus(); - sleep(1.0); - i++; + if(i != previ){ + getcmds(i); + writestatus(); + } + gotscrewed = nanosleep(&sleeptime, &left); + previ = i; + /* long diff = (left.tv_sec + (left.tv_nsec + 500000000l) / 1000000000l); */ + /* i += 1 - diff; */ + if(gotscrewed != -1){ + i++; + /* }else{ */ + /* printf("sec and nanosec left: %d, %d", left.tv_sec, left.tv_nsec); */ + } + } } From d45328c4442d0bc02550670e9ec401040b0f0938 Mon Sep 17 00:00:00 2001 From: dfuehrer Date: Sun, 14 Mar 2021 04:16:28 -0700 Subject: [PATCH 3/7] finally actually fixed the blocks dissappearing problem completely --- config.h | 9 +++++ dwmblocks.c | 96 ++++++++++++++++++++++++++++++----------------------- 2 files changed, 64 insertions(+), 41 deletions(-) diff --git a/config.h b/config.h index 8bd92b9..76d24f1 100644 --- a/config.h +++ b/config.h @@ -10,7 +10,16 @@ static const Block blocks[] = { /* {"", "volume", 0, 10}, */ /* {"", "internet", 5, 4}, */ {"", "stalonetray.sh", 0, 27}, + /* {"?", "sleep 2s && date", 1, 8}, */ {"", "cpupercent.sh", 1, 19}, {"", "battery", 5, 3}, {"", "clock", 1, 1}, }; + +//Sets delimiter between status commands. NULL character ('\0') means no delimiter. +static char *delim = "|"; + +// Have dwmblocks automatically recompile and run when you edit this file in +// 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 & } diff --git a/dwmblocks.c b/dwmblocks.c index 26ca6b0..ad5f379 100644 --- a/dwmblocks.c +++ b/dwmblocks.c @@ -1,11 +1,12 @@ -#include -#include -#include -#include +#include +#include +#include +#include #include -#include -#include -#define LENGTH(X) (sizeof(X) / sizeof (X[0])) +#include +#include +#include +#define LENGTH(X) (sizeof(X) / sizeof (X[0])) #define CMDLENGTH 50 typedef struct { @@ -42,23 +43,21 @@ static void (*writestatus) () = setroot; void replace(char *str, char old, char new) { - int N = strlen(str); - for(int i = 0; i < N; i++) - if(str[i] == old) - str[i] = new; + for(char * c = str; *c; c++) + if(*c == old) + *c = new; } +// the previous function looked nice but unfortunately it didnt work if to_remove was in any position other than the last character +// theres probably still a better way of doing this void remove_all(char *str, char to_remove) { char *read = str; char *write = str; - while (*read) { - if (*read == to_remove) { - read++; - *write = *read; - } - read++; - write++; - } + do { + while (*read == to_remove) read++; + *write++ = *read; + read++; + } while (*(read-1)); } //opens process *cmd and stores output in *output @@ -69,21 +68,38 @@ void getcmd(const Block *block, char *output) output[0] = block->signal; output++; } - strcpy(output, block->icon); char *cmd = block->command; FILE *cmdf = popen(cmd,"r"); - if (!cmdf) + if (!cmdf){ + //printf("failed to run: %s, %d\n", block->command, errno); return; - char c; + } + char tmpstr[CMDLENGTH] = ""; + // TODO decide whether its better to use the last value till next time or just keep trying while the error was the interrupt + // this keeps trying to read if it got nothing and the error was and interrupt + // could also just read to a separate buffer and not move the data over if interrupted + // this way will take longer trying to complete 1 thing but will get it done + // the other way will move on to keep going with everything and the part that failed to read will be wrong till its updated again + // either way you have to save the data to a temp buffer because when it fails it writes nothing and then then it gets displayed before this finishes + char * s; + int e; + do { + s = fgets(tmpstr, CMDLENGTH-(strlen(delim)+1), cmdf); + e = errno; + } while (!s && e == EINTR); + // this is equivalent but less readable and stuff + //while(!fgets(tmpstr, CMDLENGTH-(strlen(delim)+1), cmdf) && errno == EINTR); + pclose(cmdf); int i = strlen(block->icon); - fgets(output+i, CMDLENGTH-(strlen(delim)+1), cmdf); + strcpy(output, block->icon); + strcpy(output+i, tmpstr); remove_all(output, '\n'); i = strlen(output); - if ((i > 0 && block != &blocks[LENGTH(blocks) - 1])) + if ((i > 0 && block != &blocks[LENGTH(blocks) - 1])){ strcat(output, delim); + } i+=strlen(delim); output[i++] = '\0'; - pclose(cmdf); } void getcmds(int time) @@ -179,28 +195,26 @@ void statusloop() #ifndef __OpenBSD__ setupsignals(); #endif - int i = 0; - int previ = -1; + unsigned int i = 0; int gotscrewed = 0; struct timespec sleeptime = {1, 0}; - struct timespec left; + struct timespec tosleep = sleeptime; getcmds(-1); while(statusContinue) { - if(i != previ){ - getcmds(i); - writestatus(); + // sleep for tosleep (should be a sleeptime of 1s) and put what was left if interrupted back into tosleep + gotscrewed = nanosleep(&tosleep, &tosleep); + // if interrupted then just go sleep again for the remaining time + if(gotscrewed == -1){ + continue; } - gotscrewed = nanosleep(&sleeptime, &left); - previ = i; - /* long diff = (left.tv_sec + (left.tv_nsec + 500000000l) / 1000000000l); */ - /* i += 1 - diff; */ - if(gotscrewed != -1){ - i++; - /* }else{ */ - /* printf("sec and nanosec left: %d, %d", left.tv_sec, left.tv_nsec); */ - } - + // 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++; + // set the time to sleep back to the sleeptime of 1s + tosleep = sleeptime; } } From d68479db242c5f55572af169c1ba3d5bf2f38555 Mon Sep 17 00:00:00 2001 From: dfuehrer Date: Sun, 14 Mar 2021 04:52:41 -0700 Subject: [PATCH 4/7] copied over the master config since it is unrelated to the fix --- config.h | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/config.h b/config.h index 76d24f1..ec4ff6e 100644 --- a/config.h +++ b/config.h @@ -1,23 +1,34 @@ //Modify this file to change what commands output to your statusbar, and recompile using the make command. static const Block blocks[] = { /*Icon*/ /*Command*/ /*Update Interval*/ /*Update Signal*/ - /* {"", "music", 0, 11},*/ - /* {"", "pacpackages", 0, 8}, */ - /* {"", "memory", 10, 14}, */ - /* {"", "weather", 18000, 5}, */ - /* {"", "mailbox", 180, 12}, */ - /* {"", "nettraf", 1, 16}, */ - /* {"", "volume", 0, 10}, */ - /* {"", "internet", 5, 4}, */ - {"", "stalonetray.sh", 0, 27}, - /* {"?", "sleep 2s && date", 1, 8}, */ - {"", "cpupercent.sh", 1, 19}, - {"", "battery", 5, 3}, - {"", "clock", 1, 1}, + /* {"⌨", "sb-kbselect", 0, 30}, */ + {"", "cat /tmp/recordingicon 2>/dev/null", 0, 9}, + {"", "sb-tasks", 10, 26}, + {"", "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-moonphase", 18000, 17}, */ + {"", "sb-forecast", 18000, 5}, + {"", "sb-mailbox", 180, 12}, + {"", "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}, }; //Sets delimiter between status commands. NULL character ('\0') means no delimiter. -static char *delim = "|"; +static char *delim = " "; // Have dwmblocks automatically recompile and run when you edit this file in // vim with the following line in your vimrc/init.vim: From da9a2f7286bfbf6bfa79058fbd83901b96b32d66 Mon Sep 17 00:00:00 2001 From: dfuehrer Date: Wed, 17 Mar 2021 00:38:37 -0700 Subject: [PATCH 5/7] Added greatest common denominator interval from "Improve performance by sleeping as much as possible #69" since the sleeping has been changed. --- dwmblocks.c | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/dwmblocks.c b/dwmblocks.c index ad5f379..bbfad2d 100644 --- a/dwmblocks.c +++ b/dwmblocks.c @@ -60,6 +60,19 @@ void remove_all(char *str, char to_remove) { } while (*(read-1)); } +int gcd(int a, int b) +{ + int temp; + while (b > 0){ + temp = a % b; + + a = b; + b = temp; + } + return a; +} + + //opens process *cmd and stores output in *output void getcmd(const Block *block, char *output) { @@ -76,10 +89,10 @@ void getcmd(const Block *block, char *output) } char tmpstr[CMDLENGTH] = ""; // TODO decide whether its better to use the last value till next time or just keep trying while the error was the interrupt - // this keeps trying to read if it got nothing and the error was and interrupt - // could also just read to a separate buffer and not move the data over if interrupted - // this way will take longer trying to complete 1 thing but will get it done - // the other way will move on to keep going with everything and the part that failed to read will be wrong till its updated again + // this keeps trying to read if it got nothing and the error was an interrupt + // could also just read to a separate buffer and not move the data over if interrupted + // this way will take longer trying to complete 1 thing but will get it done + // the other way will move on to keep going with everything and the part that failed to read will be wrong till its updated again // either way you have to save the data to a temp buffer because when it fails it writes nothing and then then it gets displayed before this finishes char * s; int e; @@ -195,17 +208,25 @@ 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); + } + } unsigned int i = 0; - int gotscrewed = 0; - struct timespec sleeptime = {1, 0}; + int interrupted = 0; + struct timespec sleeptime = {interval, 0}; struct timespec tosleep = sleeptime; getcmds(-1); while(statusContinue) { - // sleep for tosleep (should be a sleeptime of 1s) and put what was left if interrupted back into tosleep - gotscrewed = nanosleep(&tosleep, &tosleep); + // 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(gotscrewed == -1){ + if(interrupted == -1){ continue; } // if not interrupted then do the calling and writing From fc80bd975c264959a73e074bc2ff49c52c384ac3 Mon Sep 17 00:00:00 2001 From: dfuehrer Date: Tue, 23 Mar 2021 18:29:19 -0700 Subject: [PATCH 6/7] reset errno so previous error doesn't cause next check to fail --- dwmblocks.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dwmblocks.c b/dwmblocks.c index bbfad2d..0a0644f 100644 --- a/dwmblocks.c +++ b/dwmblocks.c @@ -97,11 +97,10 @@ void getcmd(const Block *block, char *output) char * s; int e; do { + errno = 0; s = fgets(tmpstr, CMDLENGTH-(strlen(delim)+1), cmdf); e = errno; } while (!s && e == EINTR); - // this is equivalent but less readable and stuff - //while(!fgets(tmpstr, CMDLENGTH-(strlen(delim)+1), cmdf) && errno == EINTR); pclose(cmdf); int i = strlen(block->icon); strcpy(output, block->icon); From c306fea42674a3ee0387a98a05724c4507cd90f9 Mon Sep 17 00:00:00 2001 From: dfuehrer Date: Mon, 5 Jul 2021 19:17:34 -0700 Subject: [PATCH 7/7] fixed a bug in interval time updating --- dwmblocks.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dwmblocks.c b/dwmblocks.c index d4cc1a4..0969ed7 100644 --- a/dwmblocks.c +++ b/dwmblocks.c @@ -219,7 +219,7 @@ void statusloop() } unsigned int i = 0; int interrupted = 0; - struct timespec sleeptime = {interval, 0}; + const struct timespec sleeptime = {interval, 0}; struct timespec tosleep = sleeptime; getcmds(-1); while(statusContinue) @@ -234,7 +234,7 @@ void statusloop() getcmds(i); writestatus(); // then increment since its actually been a second (plus the time it took the commands to run) - i++; + i += interval; // set the time to sleep back to the sleeptime of 1s tosleep = sleeptime; }