From 3da92ebccb863394a3501ce2d9cbe60ad9aafc8c Mon Sep 17 00:00:00 2001 From: Aidan Date: Mon, 4 Nov 2019 15:34:34 -0700 Subject: [PATCH 01/10] Changed delimeter to accept multiple characters. --- blocks.h | 4 ++-- dwmblocks.c | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/blocks.h b/blocks.h index 31e98af..3493ce1 100644 --- a/blocks.h +++ b/blocks.h @@ -16,5 +16,5 @@ static const Block blocks[] = { {"", "~/bin/statusbar/clock", 5, 0}, }; -//sets delimeter between status commands. NULL character ('\0') means no delimeter. -static char delim = '|'; +//sets delimeter between status commands. Empty string ("") means no delimeter. +static char* delim = "|"; diff --git a/dwmblocks.c b/dwmblocks.c index 2db3489..76e7377 100644 --- a/dwmblocks.c +++ b/dwmblocks.c @@ -56,13 +56,20 @@ void getcmd(const Block *block, char *output) return; //int N = strlen(output); char c; - int i = strlen(block->icon); + int sl; + int i = sl = strlen(block->icon); while((c = fgetc(cmdf)) != EOF) output[i++] = c; - if (delim != '\0' && --i) - output[i++] = delim; - output[i++] = '\0'; pclose(cmdf); + if (i == sl) { //return empty string if command has no output + output[0] = '\0'; + return; + } + if (delim[0] != '\0' && --i) + for (int j = 0; delim[j]; j++) + output[i++] = delim[j]; + else + output[--i] = '\0'; } void getcmds(int time) @@ -104,7 +111,8 @@ void getstatus(char *str) { strcpy(str + j, statusbar[i]); } - str[--j] = '\0'; + for (int i = 0; delim[i]; i++) + str[--j] = '\0'; } @@ -159,7 +167,7 @@ int main(int argc, char** argv) for(int i = 0; i < argc; i++) { if (!strcmp("-d",argv[i])) - delim = argv[++i][0]; + delim = argv[++i]; } signal(SIGTERM, termhandler); signal(SIGINT, termhandler); From 72dc44f963bfdfbef141cbba9cf23f1358a335a3 Mon Sep 17 00:00:00 2001 From: Aidan Date: Thu, 7 Nov 2019 13:07:19 -0700 Subject: [PATCH 02/10] Revert "Changed delimeter to accept multiple characters." Bug was found where certain modules caused dwm to lag horribly. Reverting until a fix can be found. This reverts commit 3da92ebccb863394a3501ce2d9cbe60ad9aafc8c. --- blocks.h | 4 ++-- dwmblocks.c | 20 ++++++-------------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/blocks.h b/blocks.h index 3493ce1..31e98af 100644 --- a/blocks.h +++ b/blocks.h @@ -16,5 +16,5 @@ static const Block blocks[] = { {"", "~/bin/statusbar/clock", 5, 0}, }; -//sets delimeter between status commands. Empty string ("") means no delimeter. -static char* delim = "|"; +//sets delimeter between status commands. NULL character ('\0') means no delimeter. +static char delim = '|'; diff --git a/dwmblocks.c b/dwmblocks.c index 76e7377..2db3489 100644 --- a/dwmblocks.c +++ b/dwmblocks.c @@ -56,20 +56,13 @@ void getcmd(const Block *block, char *output) return; //int N = strlen(output); char c; - int sl; - int i = sl = strlen(block->icon); + int i = strlen(block->icon); while((c = fgetc(cmdf)) != EOF) output[i++] = c; + if (delim != '\0' && --i) + output[i++] = delim; + output[i++] = '\0'; pclose(cmdf); - if (i == sl) { //return empty string if command has no output - output[0] = '\0'; - return; - } - if (delim[0] != '\0' && --i) - for (int j = 0; delim[j]; j++) - output[i++] = delim[j]; - else - output[--i] = '\0'; } void getcmds(int time) @@ -111,8 +104,7 @@ void getstatus(char *str) { strcpy(str + j, statusbar[i]); } - for (int i = 0; delim[i]; i++) - str[--j] = '\0'; + str[--j] = '\0'; } @@ -167,7 +159,7 @@ int main(int argc, char** argv) for(int i = 0; i < argc; i++) { if (!strcmp("-d",argv[i])) - delim = argv[++i]; + delim = argv[++i][0]; } signal(SIGTERM, termhandler); signal(SIGINT, termhandler); From 5ff59d4e8ba9c64963d36c8ea51e7a1d644aef48 Mon Sep 17 00:00:00 2001 From: Aidan Date: Wed, 13 Nov 2019 13:58:20 -0700 Subject: [PATCH 03/10] Made dwmblocks way more efficient by only setting the name of the root window when something has changed. --- dwmblocks.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/dwmblocks.c b/dwmblocks.c index 2db3489..71760aa 100644 --- a/dwmblocks.c +++ b/dwmblocks.c @@ -17,7 +17,7 @@ void replace(char *str, char old, char new); void getcmds(int time); void getsigcmds(int signal); void setupsignals(); -void getstatus(char *str); +int getstatus(char *str, char *last); void setroot(); void statusloop(); void statusinit(); @@ -31,12 +31,8 @@ static Display *dpy; static int screen; static Window root; static char statusbar[LENGTH(blocks)][50] = {0}; -static char statusstr[256]; -static char *statuscat; -static const char *volupcmd[] = { "volup", NULL }; -static const char *voldowncmd[] = { "voldown", NULL }; -static const char *volmutecmd[] = { "volmute", NULL }; -static int statusContinue = 1,volmuted = 0; +static char statusstr[2][256]; +static int statusContinue = 1; void replace(char *str, char old, char new) { @@ -97,27 +93,29 @@ void setupsignals() } -void getstatus(char *str) +int getstatus(char *str, char *last) { + strcpy(last, str); int j = 0; for(int i = 0; i < LENGTH(blocks); j+=strlen(statusbar[i++])) { strcpy(str + j, statusbar[i]); } str[--j] = '\0'; - + return strcmp(str, last);//0 if they are the same } void setroot() { + if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed. + return; Display *d = XOpenDisplay(NULL); if (d) { dpy = d; } screen = DefaultScreen(dpy); root = RootWindow(dpy, screen); - getstatus(statusstr); - XStoreName(dpy, root, statusstr); + XStoreName(dpy, root, statusstr[0]); XCloseDisplay(dpy); } From a22c6311cc5fcc01c483fa110256f0d18e5d7271 Mon Sep 17 00:00:00 2001 From: Aidan Date: Tue, 26 Nov 2019 22:42:27 -0700 Subject: [PATCH 04/10] Added -f option to rm in clean command. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 05754f8..695d123 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ output: dwmblocks.o dwmblocks.o: dwmblocks.c blocks.h gcc -c -lX11 dwmblocks.c clean: - rm *.o *.gch dwmblocks + rm -f *.o *.gch dwmblocks install: output mkdir -p /usr/local/bin cp -f dwmblocks /usr/local/bin From 1766af7d5e0facb911c664584fc703c5f8f89092 Mon Sep 17 00:00:00 2001 From: Aidan Date: Tue, 26 Nov 2019 22:43:16 -0700 Subject: [PATCH 05/10] Added -p option for printing to stdout rather then setting the root window for compatibility with other wms. --- dwmblocks.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/dwmblocks.c b/dwmblocks.c index 71760aa..3071243 100644 --- a/dwmblocks.c +++ b/dwmblocks.c @@ -33,6 +33,10 @@ static Window root; static char statusbar[LENGTH(blocks)][50] = {0}; static char statusstr[2][256]; static int statusContinue = 1; +<<<<<<< HEAD +static void (*writestatus) () = setroot; +======= +>>>>>>> 5ff59d4e8ba9c64963d36c8ea51e7a1d644aef48 void replace(char *str, char old, char new) { @@ -50,7 +54,6 @@ void getcmd(const Block *block, char *output) FILE *cmdf = popen(cmd,"r"); if (!cmdf) return; - //int N = strlen(output); char c; int i = strlen(block->icon); while((c = fgetc(cmdf)) != EOF) @@ -96,12 +99,21 @@ void setupsignals() int getstatus(char *str, char *last) { strcpy(last, str); +<<<<<<< HEAD + str[0] = '\0'; + for(int i = 0; i < LENGTH(blocks); i++) +======= int j = 0; for(int i = 0; i < LENGTH(blocks); j+=strlen(statusbar[i++])) +>>>>>>> 5ff59d4e8ba9c64963d36c8ea51e7a1d644aef48 { - strcpy(str + j, statusbar[i]); + strcat(str, statusbar[i]); } +<<<<<<< HEAD + str[strlen(str)-1] = '\0'; +======= str[--j] = '\0'; +>>>>>>> 5ff59d4e8ba9c64963d36c8ea51e7a1d644aef48 return strcmp(str, last);//0 if they are the same } @@ -119,6 +131,14 @@ void setroot() XCloseDisplay(dpy); } +void pstdout() +{ + if (!getstatus(statusstr[0], statusstr[1]))//Only write out if text has changed. + return; + printf("%s\n",statusstr[0]); + fflush(stdout); +} + void statusloop() { @@ -128,7 +148,7 @@ void statusloop() while(statusContinue) { getcmds(i); - setroot(); + writestatus(); sleep(1.0); i++; } @@ -143,7 +163,7 @@ void statusinit() void sighandler(int signum) { getsigcmds(signum-SIGRTMIN); - setroot(); + writestatus(); } void termhandler(int signum) @@ -158,6 +178,8 @@ int main(int argc, char** argv) { if (!strcmp("-d",argv[i])) delim = argv[++i][0]; + else if(!strcmp("-p",argv[i])) + writestatus = pstdout; } signal(SIGTERM, termhandler); signal(SIGINT, termhandler); From b685f2805f483c2a0df89695ac7a7f472172debe Mon Sep 17 00:00:00 2001 From: Aidan Hubert Date: Wed, 27 Nov 2019 05:50:38 +0000 Subject: [PATCH 06/10] Fixed garbled last commit --- dwmblocks.c | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/dwmblocks.c b/dwmblocks.c index 3071243..395b565 100644 --- a/dwmblocks.c +++ b/dwmblocks.c @@ -33,10 +33,7 @@ static Window root; static char statusbar[LENGTH(blocks)][50] = {0}; static char statusstr[2][256]; static int statusContinue = 1; -<<<<<<< HEAD static void (*writestatus) () = setroot; -======= ->>>>>>> 5ff59d4e8ba9c64963d36c8ea51e7a1d644aef48 void replace(char *str, char old, char new) { @@ -99,21 +96,12 @@ void setupsignals() int getstatus(char *str, char *last) { strcpy(last, str); -<<<<<<< HEAD str[0] = '\0'; for(int i = 0; i < LENGTH(blocks); i++) -======= - int j = 0; - for(int i = 0; i < LENGTH(blocks); j+=strlen(statusbar[i++])) ->>>>>>> 5ff59d4e8ba9c64963d36c8ea51e7a1d644aef48 { strcat(str, statusbar[i]); } -<<<<<<< HEAD str[strlen(str)-1] = '\0'; -======= - str[--j] = '\0'; ->>>>>>> 5ff59d4e8ba9c64963d36c8ea51e7a1d644aef48 return strcmp(str, last);//0 if they are the same } From 09a00ab345f1c1c4bce5496f7c7d054476e35392 Mon Sep 17 00:00:00 2001 From: Aidan Date: Sat, 30 Nov 2019 02:05:10 -0700 Subject: [PATCH 07/10] fixed issue causing segfault on ARM systems --- dwmblocks.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dwmblocks.c b/dwmblocks.c index 395b565..f7f23cc 100644 --- a/dwmblocks.c +++ b/dwmblocks.c @@ -54,7 +54,11 @@ void getcmd(const Block *block, char *output) char c; int i = strlen(block->icon); while((c = fgetc(cmdf)) != EOF) + { output[i++] = c; + if(c == '\n') + break; + } if (delim != '\0' && --i) output[i++] = delim; output[i++] = '\0'; From 377f9903318d89934593348cda91c3c8fef4158c Mon Sep 17 00:00:00 2001 From: Aidan Date: Sat, 30 Nov 2019 14:43:33 -0700 Subject: [PATCH 08/10] Switched to fgets instead of using fgetc --- dwmblocks.c | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/dwmblocks.c b/dwmblocks.c index f7f23cc..ad8e91c 100644 --- a/dwmblocks.c +++ b/dwmblocks.c @@ -5,6 +5,7 @@ #include #include #define LENGTH(X) (sizeof(X) / sizeof (X[0])) +#define CMDLENGTH 50 typedef struct { char* icon; @@ -20,7 +21,6 @@ void setupsignals(); int getstatus(char *str, char *last); void setroot(); void statusloop(); -void statusinit(); void sighandler(int signum); void termhandler(int signum); @@ -30,7 +30,7 @@ void termhandler(int signum); static Display *dpy; static int screen; static Window root; -static char statusbar[LENGTH(blocks)][50] = {0}; +static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0}; static char statusstr[2][256]; static int statusContinue = 1; static void (*writestatus) () = setroot; @@ -53,12 +53,8 @@ void getcmd(const Block *block, char *output) return; char c; int i = strlen(block->icon); - while((c = fgetc(cmdf)) != EOF) - { - output[i++] = c; - if(c == '\n') - break; - } + fgets(output+i, CMDLENGTH-i, cmdf); + i = strlen(output); if (delim != '\0' && --i) output[i++] = delim; output[i++] = '\0'; @@ -102,9 +98,7 @@ int getstatus(char *str, char *last) strcpy(last, str); str[0] = '\0'; for(int i = 0; i < LENGTH(blocks); i++) - { strcat(str, statusbar[i]); - } str[strlen(str)-1] = '\0'; return strcmp(str, last);//0 if they are the same } @@ -146,11 +140,6 @@ void statusloop() } } -void statusinit() -{ - statusloop(); -} - void sighandler(int signum) { @@ -175,5 +164,5 @@ int main(int argc, char** argv) } signal(SIGTERM, termhandler); signal(SIGINT, termhandler); - statusinit(); + statusloop(); } From 3906995607d60e2721a03db78d4f3f4b63cd6afb Mon Sep 17 00:00:00 2001 From: Aidan Date: Thu, 12 Dec 2019 10:28:06 -0700 Subject: [PATCH 09/10] Changed Makefile to use pkg-config to find libraries. --- Makefile | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 695d123..b75ae9a 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,5 @@ -output: dwmblocks.o - gcc dwmblocks.o -lX11 -o dwmblocks -dwmblocks.o: dwmblocks.c blocks.h - gcc -c -lX11 dwmblocks.c +output: dwmblocks.c blocks.h + cc `pkg-config --cflags x11` `pkg-config --libs x11` dwmblocks.c -o dwmblocks clean: rm -f *.o *.gch dwmblocks install: output From fa4eeff38d5c2787938b2defde4390d585ae4887 Mon Sep 17 00:00:00 2001 From: Aidan Date: Mon, 16 Dec 2019 14:35:29 -0700 Subject: [PATCH 10/10] Added ifndef for openbsd compatibility. SIGRTMIN is not defined on OpenBSD. --- dwmblocks.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/dwmblocks.c b/dwmblocks.c index ad8e91c..88bdfb0 100644 --- a/dwmblocks.c +++ b/dwmblocks.c @@ -16,12 +16,14 @@ typedef struct { void sighandler(int num); void replace(char *str, char old, char new); 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(); -void sighandler(int signum); void termhandler(int signum); @@ -72,6 +74,7 @@ void getcmds(int time) } } +#ifndef __OpenBSD__ void getsigcmds(int signal) { const Block *current; @@ -92,6 +95,7 @@ void setupsignals() } } +#endif int getstatus(char *str, char *last) { @@ -128,7 +132,9 @@ void pstdout() void statusloop() { +#ifndef __OpenBSD__ setupsignals(); +#endif int i = 0; getcmds(-1); while(statusContinue) @@ -140,12 +146,13 @@ void statusloop() } } - +#ifndef __OpenBSD__ void sighandler(int signum) { getsigcmds(signum-SIGRTMIN); writestatus(); } +#endif void termhandler(int signum) {