Add module "src/xbase.c"

This commit is contained in:
Alex Kotov 2021-11-22 09:07:13 +05:00
parent 854e9941a3
commit 567ec468dd
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
4 changed files with 77 additions and 27 deletions

View file

@ -28,7 +28,8 @@ MODULES_SRC = \
src/spawn.c \ src/spawn.c \
src/state.c \ src/state.c \
src/unit.c \ src/unit.c \
src/util.c src/util.c \
src/xbase.c
DWM_SRC = \ DWM_SRC = \
src/dwm/handlers.c \ src/dwm/handlers.c \

View file

@ -9,6 +9,7 @@
#include "state.h" #include "state.h"
#include "unit.h" #include "unit.h"
#include "util.h" #include "util.h"
#include "xbase.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -229,7 +230,7 @@ static void zoom(const Arg *arg);
* variables * * variables *
*************/ *************/
static const char *program_title = NULL; static Xbase xbase = NULL;
static struct Screen screen = { static struct Screen screen = {
.sizes = { 0, 0 }, .sizes = { 0, 0 },
@ -284,19 +285,12 @@ static void (*handler[LASTEvent])(XEvent*) = {
int dwm_main(const char *const new_program_title) int dwm_main(const char *const new_program_title)
{ {
if (!(program_title = new_program_title)) { xbase = xbase_new(new_program_title);
fatal("no program title is given");
}
if (!XSupportsLocale()) { dpy = xbase->x_display;
warning("no locale support in X"); screen.x_screen = xbase->x_screen;
} screen.sizes = xbase->screen_sizes;
root = xbase->x_root;
// Resource allocations start here.
if (!(dpy = XOpenDisplay(NULL))) {
fatal("cannot open display");
}
checkotherwm(); checkotherwm();
@ -308,14 +302,6 @@ int dwm_main(const char *const new_program_title)
fatal("cannot create atoms"); 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()) { if (!setup()) {
fatal("cannot setup"); fatal("cannot setup");
} }
@ -325,11 +311,9 @@ int dwm_main(const char *const new_program_title)
cleanup(); cleanup();
// Resource cleanups.
UNIT_DELETE(global_unit); UNIT_DELETE(global_unit);
ATOMS_DELETE(atoms); ATOMS_DELETE(atoms);
XCloseDisplay(dpy); XBASE_DELETE(xbase);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -2212,8 +2196,8 @@ void wmcheckwin_create()
8, 8,
PropModeReplace, PropModeReplace,
(unsigned char*) (unsigned char*)
program_title, xbase->program_title,
strlen(program_title) strlen(xbase->program_title)
); );
XChangeProperty( XChangeProperty(
dpy, dpy,

40
src/xbase.c Normal file
View file

@ -0,0 +1,40 @@
#include "xbase.h"
#include "main.h"
#include <stdlib.h>
#include <string.h>
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);
}

25
src/xbase.h Normal file
View file

@ -0,0 +1,25 @@
#ifndef _XBASE_H
#define _XBASE_H
#include "state.h"
#include <stddef.h>
#include <X11/Xutil.h>
#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