Krolyxon
Commit fb6411a

Branches

Refs →

nsxivfb6411a

Encapsulate window environment in win_env_t

Author
Bert
Date
2011-01-18 16:32 +0100
Parent
ab480ec
Commit
fb6411ab5c887fcd39f01d406eda21ce02f708ec

+42 −41

3 files changed

  1. M events.c +2 −4
  2. M window.c +30 −37
  3. M window.h +10 −0
M events.c View
@@ -28,8 +28,6 @@ void on_keypress(app_t*, XEvent*);
2828 void on_configurenotify(app_t*, XEvent*);
2929 void on_expose(app_t*, XEvent*);
3030
31−extern Display *dpy;
32−
3331 static void (*handler[LASTEvent])(app_t*, XEvent*) = {
3432 [Expose] = on_expose,
3533 [ConfigureNotify] = on_configurenotify,
@@ -39,7 +37,7 @@ static void (*handler[LASTEvent])(app_t*, XEvent*) = {
3937 void event_loop(app_t *app) {
4038 XEvent ev;
4139
42− while (!XNextEvent(dpy, &ev)) {
40+ while (!XNextEvent(app->win.env.dpy, &ev)) {
4341 if (handler[ev.type])
4442 handler[ev.type](app, &ev);
4543 }
@@ -53,7 +51,7 @@ void on_keypress(app_t *app, XEvent *ev) {
5351 return;
5452
5553 kev = &ev->xkey;
56− keysym = XKeycodeToKeysym(dpy, (KeyCode) kev->keycode, 0);
54+ keysym = XKeycodeToKeysym(app->win.env.dpy, (KeyCode) kev->keycode, 0);
5755
5856 switch (keysym) {
5957 case XK_Escape:
M window.c View
@@ -24,78 +24,71 @@
2424 #include "sxiv.h"
2525 #include "window.h"
2626
27−Display *dpy;
28−int scr;
29−int scrw, scrh;
30−Visual *vis;
31−Colormap cmap;
32−int depth;
33−GC gc;
34−XColor bgcol;
35−
3627 void win_open(win_t *win) {
28+ win_env_t *e;
3729 XClassHint *classhint;
30+ XColor bgcol;
3831 XSetWindowAttributes attr;
3932 unsigned long mask;
4033
4134 if (!win)
4235 return;
36+
37+ e = &win->env;
4338
44− if (!(dpy = XOpenDisplay(NULL)))
39+ if (!(e->dpy = XOpenDisplay(NULL)))
4540 FATAL("could not open display");
4641
47− scr = DefaultScreen(dpy);
48− scrw = DisplayWidth(dpy, scr);
49− scrh = DisplayHeight(dpy, scr);
42+ e->scr = DefaultScreen(e->dpy);
43+ e->scrw = DisplayWidth(e->dpy, e->scr);
44+ e->scrh = DisplayHeight(e->dpy, e->scr);
5045
51− vis = DefaultVisual(dpy, scr);
52− cmap = DefaultColormap(dpy, scr);
53− depth = DefaultDepth(dpy, scr);
46+ e->vis = DefaultVisual(e->dpy, e->scr);
47+ e->cmap = DefaultColormap(e->dpy, e->scr);
48+ e->depth = DefaultDepth(e->dpy, e->scr);
5449
55− if (!XAllocNamedColor(dpy, DefaultColormap(dpy, scr), BG_COLOR,
56− &bgcol, &bgcol))
50+ if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
51+ &bgcol, &bgcol))
5752 FATAL("could not allocate color: %s", BG_COLOR);
5853
59− if (win->w > scrw)
60− win->w = scrw;
61− if (win->h > scrh)
62− win->h = scrh;
63− win->x = (scrw - win->w) / 2;
64− win->y = (scrh - win->h) / 2;
54+ if (win->w > e->scrw)
55+ win->w = e->scrw;
56+ if (win->h > e->scrh)
57+ win->h = e->scrh;
58+ win->x = (e->scrw - win->w) / 2;
59+ win->y = (e->scrh - win->h) / 2;
6560
6661 attr.backing_store = NotUseful;
6762 attr.background_pixel = bgcol.pixel;
6863 attr.save_under = False;
6964 mask = CWBackingStore | CWBackPixel | CWSaveUnder;
7065
71− win->xwin = XCreateWindow(dpy, RootWindow(dpy, scr), win->x, win->y,
72− win->w, win->h, 0, depth, InputOutput, vis, mask, &attr);
66+ win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
67+ win->x, win->y, win->w, win->h, 0,
68+ e->depth, InputOutput, e->vis, mask, &attr);
7369 if (win->xwin == None)
7470 FATAL("could not create window");
7571
76− XSelectInput(dpy, win->xwin,
77− StructureNotifyMask | ExposureMask | KeyPressMask);
78−
79− gc = XCreateGC(dpy, win->xwin, 0, NULL);
72+ XSelectInput(e->dpy, win->xwin,
73+ StructureNotifyMask | ExposureMask | KeyPressMask);
8074
8175 if ((classhint = XAllocClassHint())) {
8276 classhint->res_name = "sxvi";
8377 classhint->res_class = "sxvi";
84− XSetClassHint(dpy, win->xwin, classhint);
78+ XSetClassHint(e->dpy, win->xwin, classhint);
8579 XFree(classhint);
8680 }
8781
88− XMapWindow(dpy, win->xwin);
89− XFlush(dpy);
82+ XMapWindow(e->dpy, win->xwin);
83+ XFlush(e->dpy);
9084 }
9185
9286 void win_close(win_t *win) {
9387 if (!win)
9488 return;
9589
96− XDestroyWindow(dpy, win->xwin);
97− XFreeGC(dpy, gc);
98− XCloseDisplay(dpy);
90+ XDestroyWindow(win->env.dpy, win->xwin);
91+ XCloseDisplay(win->env.dpy);
9992 }
10093
10194 int win_configure(win_t *win, XConfigureEvent *cev) {
@@ -105,7 +98,7 @@ int win_configure(win_t *win, XConfigureEvent *cev) {
10598 return 0;
10699
107100 changed = win->x != cev->x || win->y != cev->y ||
108− win->w != cev->width || win->h != cev->height;
101+ win->w != cev->width || win->h != cev->height;
109102 win->x = cev->x;
110103 win->y = cev->y;
111104 win->w = cev->width;
M window.h View
@@ -21,8 +21,18 @@
2121
2222 #include <X11/Xlib.h>
2323
24+typedef struct win_env_s {
25+ Display *dpy;
26+ int scr;
27+ int scrw, scrh;
28+ Visual *vis;
29+ Colormap cmap;
30+ int depth;
31+} win_env_t;
32+
2433 typedef struct win_s {
2534 Window xwin;
35+ win_env_t env;
2636
2737 int w;
2838 int h;