diff options
author | dfuehrer <dfuehrer@email.arizona.edu> | 2021-03-17 00:38:37 -0700 |
---|---|---|
committer | dfuehrer <dfuehrer@email.arizona.edu> | 2021-03-17 00:38:37 -0700 |
commit | da9a2f7286bfbf6bfa79058fbd83901b96b32d66 (patch) | |
tree | b40d09e14c1489a79c875ec4a5a447e20e8f543f | |
parent | finally actually fixed the blocks dissappearing problem completely (diff) | |
download | dwmblocks-da9a2f7286bfbf6bfa79058fbd83901b96b32d66.tar.gz dwmblocks-da9a2f7286bfbf6bfa79058fbd83901b96b32d66.tar.bz2 dwmblocks-da9a2f7286bfbf6bfa79058fbd83901b96b32d66.tar.xz dwmblocks-da9a2f7286bfbf6bfa79058fbd83901b96b32d66.tar.zst dwmblocks-da9a2f7286bfbf6bfa79058fbd83901b96b32d66.zip |
Added greatest common denominator interval from
"Improve performance by sleeping as much as possible #69"
since the sleeping has been changed.
-rw-r--r-- | dwmblocks.c | 39 |
1 files 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 |