Only open X server once rather than open and close after every writeout.

This commit is contained in:
aidan 2020-09-09 13:10:49 -06:00
parent b7d1970488
commit b6b0be4f4f

View file

@ -39,6 +39,10 @@ void pstdout();
#ifndef NO_X #ifndef NO_X
void setroot(); void setroot();
static void (*writestatus) () = setroot; static void (*writestatus) () = setroot;
static int setupX();
static Display *dpy;
static int screen;
static Window root;
#else #else
static void (*writestatus) () = pstdout; static void (*writestatus) () = pstdout;
#endif #endif
@ -76,8 +80,7 @@ void getcmd(const Block *block, char *output)
void getcmds(int time) void getcmds(int time)
{ {
const Block* current; const Block* current;
for (unsigned int i = 0; i < LENGTH(blocks); i++) for (unsigned int i = 0; i < LENGTH(blocks); i++) {
{
current = 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]); getcmd(current,statusbar[i]);
@ -87,8 +90,7 @@ void getcmds(int time)
void getsigcmds(unsigned int signal) void getsigcmds(unsigned int signal)
{ {
const Block *current; const Block *current;
for (unsigned int i = 0; i < LENGTH(blocks); i++) for (unsigned int i = 0; i < LENGTH(blocks); i++) {
{
current = blocks + i; current = blocks + i;
if (current->signal == signal) if (current->signal == signal)
getcmd(current,statusbar[i]); getcmd(current,statusbar[i]);
@ -103,8 +105,7 @@ void setupsignals()
signal(i, dummysighandler); signal(i, dummysighandler);
#endif #endif
for (unsigned int i = 0; i < LENGTH(blocks); i++) for (unsigned int i = 0; i < LENGTH(blocks); i++) {
{
if (blocks[i].signal > 0) if (blocks[i].signal > 0)
signal(SIGMINUS+blocks[i].signal, sighandler); signal(SIGMINUS+blocks[i].signal, sighandler);
} }
@ -124,22 +125,22 @@ int getstatus(char *str, char *last)
#ifndef NO_X #ifndef NO_X
void setroot() void setroot()
{ {
static Display *dpy;
static int screen;
static Window root;
if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed. if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
return; return;
XStoreName(dpy, root, statusstr[0]);
XFlush(dpy);
}
int setupX()
{
dpy = XOpenDisplay(NULL); dpy = XOpenDisplay(NULL);
if (!dpy) { if (!dpy) {
fprintf(stderr, "Failed to open display\n"); fprintf(stderr, "dwmblocks: Failed to open display\n");
statusContinue = 0; return 0;
returnStatus = 1;
return;
} }
screen = DefaultScreen(dpy); screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen); root = RootWindow(dpy, screen);
XStoreName(dpy, root, statusstr[0]); return 1;
XCloseDisplay(dpy);
} }
#endif #endif
@ -157,8 +158,7 @@ void statusloop()
setupsignals(); setupsignals();
int i = 0; int i = 0;
getcmds(-1); getcmds(-1);
while (1) while (1) {
{
getcmds(i++); getcmds(i++);
writestatus(); writestatus();
if (!statusContinue) if (!statusContinue)
@ -188,17 +188,23 @@ void termhandler()
int main(int argc, char** argv) 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])) if (!strcmp("-d",argv[i]))
strncpy(delim, argv[++i], delimLen); strncpy(delim, argv[++i], delimLen);
else if (!strcmp("-p",argv[i])) else if (!strcmp("-p",argv[i]))
writestatus = pstdout; writestatus = pstdout;
} }
#ifndef NO_X
if (!setupX())
return 1;
#endif
delimLen = MIN(delimLen, strlen(delim)); delimLen = MIN(delimLen, strlen(delim));
delim[delimLen++] = '\0'; delim[delimLen++] = '\0';
signal(SIGTERM, termhandler); signal(SIGTERM, termhandler);
signal(SIGINT, termhandler); signal(SIGINT, termhandler);
statusloop(); statusloop();
return returnStatus; #ifndef NO_X
XCloseDisplay(dpy);
#endif
return 0;
} }