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
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;
}