Consolidated X11 based code into the setroot function

This is to make it easier to build dwmblocks without xlib so you can
use it on wayland or other x wms.
This commit is contained in:
aidan 2020-09-05 15:48:54 -06:00
parent 13c7700f34
commit b7d1970488
1 changed files with 25 additions and 9 deletions

View File

@ -3,7 +3,9 @@
#include<string.h> #include<string.h>
#include<unistd.h> #include<unistd.h>
#include<signal.h> #include<signal.h>
#ifndef NO_X
#include<X11/Xlib.h> #include<X11/Xlib.h>
#endif
#ifdef __OpenBSD__ #ifdef __OpenBSD__
#define SIGPLUS SIGUSR1+1 #define SIGPLUS SIGUSR1+1
#define SIGMINUS SIGUSR1-1 #define SIGMINUS SIGUSR1-1
@ -31,20 +33,23 @@ void getsigcmds(unsigned int signal);
void setupsignals(); void setupsignals();
void sighandler(int signum); void sighandler(int signum);
int getstatus(char *str, char *last); int getstatus(char *str, char *last);
void setroot();
void statusloop(); void statusloop();
void termhandler(); void termhandler();
void pstdout();
#ifndef NO_X
void setroot();
static void (*writestatus) () = setroot;
#else
static void (*writestatus) () = pstdout;
#endif
#include "blocks.h" #include "blocks.h"
static Display *dpy;
static int screen;
static Window root;
static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0}; static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0};
static char statusstr[2][STATUSLENGTH]; static char statusstr[2][STATUSLENGTH];
static int statusContinue = 1; static int statusContinue = 1;
static void (*writestatus) () = setroot; static int returnStatus = 0;
//opens process *cmd and stores output in *output //opens process *cmd and stores output in *output
void getcmd(const Block *block, char *output) void getcmd(const Block *block, char *output)
@ -116,19 +121,27 @@ int getstatus(char *str, char *last)
return strcmp(str, last);//0 if they are the same return strcmp(str, last);//0 if they are the same
} }
#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;
Display *d = XOpenDisplay(NULL); dpy = XOpenDisplay(NULL);
if (d) { if (!dpy) {
dpy = d; fprintf(stderr, "Failed to open display\n");
statusContinue = 0;
returnStatus = 1;
return;
} }
screen = DefaultScreen(dpy); screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen); root = RootWindow(dpy, screen);
XStoreName(dpy, root, statusstr[0]); XStoreName(dpy, root, statusstr[0]);
XCloseDisplay(dpy); XCloseDisplay(dpy);
} }
#endif
void pstdout() void pstdout()
{ {
@ -144,10 +157,12 @@ void statusloop()
setupsignals(); setupsignals();
int i = 0; int i = 0;
getcmds(-1); getcmds(-1);
while (statusContinue) while (1)
{ {
getcmds(i++); getcmds(i++);
writestatus(); writestatus();
if (!statusContinue)
break;
sleep(1.0); sleep(1.0);
} }
} }
@ -185,4 +200,5 @@ int main(int argc, char** argv)
signal(SIGTERM, termhandler); signal(SIGTERM, termhandler);
signal(SIGINT, termhandler); signal(SIGINT, termhandler);
statusloop(); statusloop();
return returnStatus;
} }