diff --git a/Makefile b/Makefile index 2bb0763..4b0d68d 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,8 @@ MODULES_SRC = \ src/spawn.c \ src/state.c \ src/unit.c \ - src/util.c + src/util.c \ + src/xbase.c DWM_SRC = \ src/dwm/handlers.c \ diff --git a/src/dwm.c b/src/dwm.c index 4d09cde..b27d18a 100644 --- a/src/dwm.c +++ b/src/dwm.c @@ -9,6 +9,7 @@ #include "state.h" #include "unit.h" #include "util.h" +#include "xbase.h" #include #include @@ -229,7 +230,7 @@ static void zoom(const Arg *arg); * variables * *************/ -static const char *program_title = NULL; +static Xbase xbase = NULL; static struct Screen screen = { .sizes = { 0, 0 }, @@ -284,19 +285,12 @@ static void (*handler[LASTEvent])(XEvent*) = { int dwm_main(const char *const new_program_title) { - if (!(program_title = new_program_title)) { - fatal("no program title is given"); - } + xbase = xbase_new(new_program_title); - if (!XSupportsLocale()) { - warning("no locale support in X"); - } - - // Resource allocations start here. - - if (!(dpy = XOpenDisplay(NULL))) { - fatal("cannot open display"); - } + dpy = xbase->x_display; + screen.x_screen = xbase->x_screen; + screen.sizes = xbase->screen_sizes; + root = xbase->x_root; checkotherwm(); @@ -308,14 +302,6 @@ int dwm_main(const char *const new_program_title) fatal("cannot create atoms"); } - screen.x_screen = DefaultScreen(dpy); - screen.sizes.w = DisplayWidth(dpy, screen.x_screen); - screen.sizes.h = DisplayHeight(dpy, screen.x_screen); - - root = RootWindow(dpy, screen.x_screen); - - // Old code. - if (!setup()) { fatal("cannot setup"); } @@ -325,11 +311,9 @@ int dwm_main(const char *const new_program_title) cleanup(); - // Resource cleanups. - UNIT_DELETE(global_unit); ATOMS_DELETE(atoms); - XCloseDisplay(dpy); + XBASE_DELETE(xbase); return EXIT_SUCCESS; } @@ -2212,8 +2196,8 @@ void wmcheckwin_create() 8, PropModeReplace, (unsigned char*) - program_title, - strlen(program_title) + xbase->program_title, + strlen(xbase->program_title) ); XChangeProperty( dpy, diff --git a/src/xbase.c b/src/xbase.c new file mode 100644 index 0000000..b50ac7b --- /dev/null +++ b/src/xbase.c @@ -0,0 +1,40 @@ +#include "xbase.h" + +#include "main.h" + +#include +#include + +Xbase xbase_new(const char *const program_title) +{ + if (!XSupportsLocale()) warning("no locale support in X"); + + Xbase xbase = malloc(sizeof(struct Xbase)); + if (!xbase) fatal_perror("cannot allocate xbase"); + + if (!(xbase->program_title = strdup(program_title))) { + fatal_perror("no program title is given"); + } + + if (!(xbase->x_display = XOpenDisplay(NULL))) { + fatal("cannot open X display"); + } + + xbase->x_screen = DefaultScreen(xbase->x_display); + xbase->screen_sizes = sizes_create_from_args( + DisplayWidth(xbase->x_display, xbase->x_screen), + DisplayHeight(xbase->x_display, xbase->x_screen) + ); + xbase->x_root = RootWindow(xbase->x_display, xbase->x_screen); + + return xbase; +} + +void xbase_delete(const Xbase xbase) +{ + // TODO: maybe we should assert here + if (xbase == NULL) return; + + XCloseDisplay(xbase->x_display); + free(xbase); +} diff --git a/src/xbase.h b/src/xbase.h new file mode 100644 index 0000000..fc8682b --- /dev/null +++ b/src/xbase.h @@ -0,0 +1,25 @@ +#ifndef _XBASE_H +#define _XBASE_H + +#include "state.h" + +#include +#include + +#define XBASE_DELETE(xbase) { \ + xbase_delete(xbase); \ + xbase = NULL; \ +} + +typedef struct Xbase { + const char *program_title; + Display *x_display; + int x_screen; + struct Sizes screen_sizes; + int x_root; +} *Xbase; + +Xbase xbase_new(const char *program_title); +void xbase_delete(Xbase xbase); + +#endif // _XBASE_H