Refactor X11 part

This commit is contained in:
Utkarsh Verma 2022-06-27 02:38:11 +05:30
parent 65bfd0eef5
commit cdc992cc41
No known key found for this signature in database
GPG Key ID: 817656CF818EFCCC

23
main.c
View File

@ -163,18 +163,23 @@ void debug() {
write(STDOUT_FILENO, "\n", 1); write(STDOUT_FILENO, "\n", 1);
} }
int setupX() {
dpy = XOpenDisplay(NULL);
if (!dpy)
return 1;
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
return 0;
}
void setRoot() { void setRoot() {
// Only set root if text has changed // Only set root if text has changed
if (!getStatus(statusBar[0], statusBar[1])) if (!getStatus(statusBar[0], statusBar[1]))
return; return;
Display* d = XOpenDisplay(NULL);
if (d)
dpy = d;
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
XStoreName(dpy, root, statusBar[0]); XStoreName(dpy, root, statusBar[0]);
XCloseDisplay(dpy); XFlush(dpy);
} }
void signalHandler() { void signalHandler() {
@ -284,6 +289,11 @@ void init() {
} }
int main(const int argc, const char* argv[]) { int main(const int argc, const char* argv[]) {
if (setupX()) {
fprintf(stderr, "dwmblocks: Failed to open display\n");
return 1;
}
writeStatus = setRoot; writeStatus = setRoot;
for (int i = 0; i < argc; i++) for (int i = 0; i < argc; i++)
if (!strcmp("-d", argv[i])) if (!strcmp("-d", argv[i]))
@ -292,6 +302,7 @@ int main(const int argc, const char* argv[]) {
init(); init();
statusLoop(); statusLoop();
XCloseDisplay(dpy);
close(epollFD); close(epollFD);
close(signalFD); close(signalFD);
for (int i = 0; i < LEN(pipes); i++) for (int i = 0; i < LEN(pipes); i++)