From: aidan Date: Wed, 9 Sep 2020 19:10:49 +0000 (-0600) Subject: Only open X server once rather than open and close after every writeout. X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=b6b0be4f4fe77fea6f1aef07232a44abd0139098;p=dwmblocks.git Only open X server once rather than open and close after every writeout. --- diff --git a/dwmblocks.c b/dwmblocks.c index ef25785..64ad7a3 100644 --- a/dwmblocks.c +++ b/dwmblocks.c @@ -39,6 +39,10 @@ void pstdout(); #ifndef NO_X void setroot(); static void (*writestatus) () = setroot; +static int setupX(); +static Display *dpy; +static int screen; +static Window root; #else static void (*writestatus) () = pstdout; #endif @@ -76,8 +80,7 @@ void getcmd(const Block *block, char *output) void getcmds(int time) { const Block* current; - for (unsigned int i = 0; i < LENGTH(blocks); i++) - { + for (unsigned int i = 0; i < LENGTH(blocks); i++) { current = blocks + i; if ((current->interval != 0 && time % current->interval == 0) || time == -1) getcmd(current,statusbar[i]); @@ -87,8 +90,7 @@ void getcmds(int time) void getsigcmds(unsigned int signal) { const Block *current; - for (unsigned int i = 0; i < LENGTH(blocks); i++) - { + for (unsigned int i = 0; i < LENGTH(blocks); i++) { current = blocks + i; if (current->signal == signal) getcmd(current,statusbar[i]); @@ -103,8 +105,7 @@ void setupsignals() signal(i, dummysighandler); #endif - for (unsigned int i = 0; i < LENGTH(blocks); i++) - { + for (unsigned int i = 0; i < LENGTH(blocks); i++) { if (blocks[i].signal > 0) signal(SIGMINUS+blocks[i].signal, sighandler); } @@ -124,22 +125,22 @@ int getstatus(char *str, char *last) #ifndef NO_X void setroot() { - static Display *dpy; - static int screen; - static Window root; if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed. return; + XStoreName(dpy, root, statusstr[0]); + XFlush(dpy); +} + +int setupX() +{ dpy = XOpenDisplay(NULL); if (!dpy) { - fprintf(stderr, "Failed to open display\n"); - statusContinue = 0; - returnStatus = 1; - return; + fprintf(stderr, "dwmblocks: Failed to open display\n"); + return 0; } screen = DefaultScreen(dpy); root = RootWindow(dpy, screen); - XStoreName(dpy, root, statusstr[0]); - XCloseDisplay(dpy); + return 1; } #endif @@ -157,8 +158,7 @@ void statusloop() setupsignals(); int i = 0; getcmds(-1); - while (1) - { + while (1) { getcmds(i++); writestatus(); if (!statusContinue) @@ -188,17 +188,23 @@ void termhandler() int main(int argc, char** argv) { - for (int i = 0; i < argc; i++) //Handle command line arguments - { + for (int i = 0; i < argc; i++) {//Handle command line arguments if (!strcmp("-d",argv[i])) strncpy(delim, argv[++i], delimLen); else if (!strcmp("-p",argv[i])) writestatus = pstdout; } +#ifndef NO_X + if (!setupX()) + return 1; +#endif delimLen = MIN(delimLen, strlen(delim)); delim[delimLen++] = '\0'; signal(SIGTERM, termhandler); signal(SIGINT, termhandler); statusloop(); - return returnStatus; +#ifndef NO_X + XCloseDisplay(dpy); +#endif + return 0; }