#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
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]);
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]);
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);
}
#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
setupsignals();
int i = 0;
getcmds(-1);
- while (1)
- {
+ while (1) {
getcmds(i++);
writestatus();
if (!statusContinue)
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;
}