Encapsulate window environment in win_env_t

This commit is contained in:
Bert 2011-01-18 16:32:40 +01:00
parent ab480ecc47
commit fb6411ab5c
3 changed files with 42 additions and 41 deletions

View file

@ -28,8 +28,6 @@ void on_keypress(app_t*, XEvent*);
void on_configurenotify(app_t*, XEvent*); void on_configurenotify(app_t*, XEvent*);
void on_expose(app_t*, XEvent*); void on_expose(app_t*, XEvent*);
extern Display *dpy;
static void (*handler[LASTEvent])(app_t*, XEvent*) = { static void (*handler[LASTEvent])(app_t*, XEvent*) = {
[Expose] = on_expose, [Expose] = on_expose,
[ConfigureNotify] = on_configurenotify, [ConfigureNotify] = on_configurenotify,
@ -39,7 +37,7 @@ static void (*handler[LASTEvent])(app_t*, XEvent*) = {
void event_loop(app_t *app) { void event_loop(app_t *app) {
XEvent ev; XEvent ev;
while (!XNextEvent(dpy, &ev)) { while (!XNextEvent(app->win.env.dpy, &ev)) {
if (handler[ev.type]) if (handler[ev.type])
handler[ev.type](app, &ev); handler[ev.type](app, &ev);
} }
@ -53,7 +51,7 @@ void on_keypress(app_t *app, XEvent *ev) {
return; return;
kev = &ev->xkey; kev = &ev->xkey;
keysym = XKeycodeToKeysym(dpy, (KeyCode) kev->keycode, 0); keysym = XKeycodeToKeysym(app->win.env.dpy, (KeyCode) kev->keycode, 0);
switch (keysym) { switch (keysym) {
case XK_Escape: case XK_Escape:

View file

@ -24,78 +24,71 @@
#include "sxiv.h" #include "sxiv.h"
#include "window.h" #include "window.h"
Display *dpy;
int scr;
int scrw, scrh;
Visual *vis;
Colormap cmap;
int depth;
GC gc;
XColor bgcol;
void win_open(win_t *win) { void win_open(win_t *win) {
win_env_t *e;
XClassHint *classhint; XClassHint *classhint;
XColor bgcol;
XSetWindowAttributes attr; XSetWindowAttributes attr;
unsigned long mask; unsigned long mask;
if (!win) if (!win)
return; return;
e = &win->env;
if (!(dpy = XOpenDisplay(NULL))) if (!(e->dpy = XOpenDisplay(NULL)))
FATAL("could not open display"); FATAL("could not open display");
scr = DefaultScreen(dpy); e->scr = DefaultScreen(e->dpy);
scrw = DisplayWidth(dpy, scr); e->scrw = DisplayWidth(e->dpy, e->scr);
scrh = DisplayHeight(dpy, scr); e->scrh = DisplayHeight(e->dpy, e->scr);
vis = DefaultVisual(dpy, scr); e->vis = DefaultVisual(e->dpy, e->scr);
cmap = DefaultColormap(dpy, scr); e->cmap = DefaultColormap(e->dpy, e->scr);
depth = DefaultDepth(dpy, scr); e->depth = DefaultDepth(e->dpy, e->scr);
if (!XAllocNamedColor(dpy, DefaultColormap(dpy, scr), BG_COLOR, if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
&bgcol, &bgcol)) &bgcol, &bgcol))
FATAL("could not allocate color: %s", BG_COLOR); FATAL("could not allocate color: %s", BG_COLOR);
if (win->w > scrw) if (win->w > e->scrw)
win->w = scrw; win->w = e->scrw;
if (win->h > scrh) if (win->h > e->scrh)
win->h = scrh; win->h = e->scrh;
win->x = (scrw - win->w) / 2; win->x = (e->scrw - win->w) / 2;
win->y = (scrh - win->h) / 2; win->y = (e->scrh - win->h) / 2;
attr.backing_store = NotUseful; attr.backing_store = NotUseful;
attr.background_pixel = bgcol.pixel; attr.background_pixel = bgcol.pixel;
attr.save_under = False; attr.save_under = False;
mask = CWBackingStore | CWBackPixel | CWSaveUnder; mask = CWBackingStore | CWBackPixel | CWSaveUnder;
win->xwin = XCreateWindow(dpy, RootWindow(dpy, scr), win->x, win->y, win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
win->w, win->h, 0, depth, InputOutput, vis, mask, &attr); win->x, win->y, win->w, win->h, 0,
e->depth, InputOutput, e->vis, mask, &attr);
if (win->xwin == None) if (win->xwin == None)
FATAL("could not create window"); FATAL("could not create window");
XSelectInput(dpy, win->xwin, XSelectInput(e->dpy, win->xwin,
StructureNotifyMask | ExposureMask | KeyPressMask); StructureNotifyMask | ExposureMask | KeyPressMask);
gc = XCreateGC(dpy, win->xwin, 0, NULL);
if ((classhint = XAllocClassHint())) { if ((classhint = XAllocClassHint())) {
classhint->res_name = "sxvi"; classhint->res_name = "sxvi";
classhint->res_class = "sxvi"; classhint->res_class = "sxvi";
XSetClassHint(dpy, win->xwin, classhint); XSetClassHint(e->dpy, win->xwin, classhint);
XFree(classhint); XFree(classhint);
} }
XMapWindow(dpy, win->xwin); XMapWindow(e->dpy, win->xwin);
XFlush(dpy); XFlush(e->dpy);
} }
void win_close(win_t *win) { void win_close(win_t *win) {
if (!win) if (!win)
return; return;
XDestroyWindow(dpy, win->xwin); XDestroyWindow(win->env.dpy, win->xwin);
XFreeGC(dpy, gc); XCloseDisplay(win->env.dpy);
XCloseDisplay(dpy);
} }
int win_configure(win_t *win, XConfigureEvent *cev) { int win_configure(win_t *win, XConfigureEvent *cev) {
@ -105,7 +98,7 @@ int win_configure(win_t *win, XConfigureEvent *cev) {
return 0; return 0;
changed = win->x != cev->x || win->y != cev->y || changed = win->x != cev->x || win->y != cev->y ||
win->w != cev->width || win->h != cev->height; win->w != cev->width || win->h != cev->height;
win->x = cev->x; win->x = cev->x;
win->y = cev->y; win->y = cev->y;
win->w = cev->width; win->w = cev->width;

View file

@ -21,8 +21,18 @@
#include <X11/Xlib.h> #include <X11/Xlib.h>
typedef struct win_env_s {
Display *dpy;
int scr;
int scrw, scrh;
Visual *vis;
Colormap cmap;
int depth;
} win_env_t;
typedef struct win_s { typedef struct win_s {
Window xwin; Window xwin;
win_env_t env;
int w; int w;
int h; int h;