summaryrefslogtreecommitdiffstats
path: root/window.c
blob: 3bc35f9f6b31ce25d8ccb54bbcad0cbcb8aacfb9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* sxiv: window.c
 * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include <stdio.h>

#include <X11/Xutil.h>

#include "sxiv.h"
#include "window.h"

void win_open(win_t *win) {
	win_env_t *e;
	XClassHint *classhint;
	XColor bgcol;
	XSetWindowAttributes attr;
	unsigned long mask;

	if (!win)
		return;
	
	e = &win->env;

	if (!(e->dpy = XOpenDisplay(NULL)))
		DIE("could not open display");
	
	e->scr = DefaultScreen(e->dpy);
	e->scrw = DisplayWidth(e->dpy, e->scr);
	e->scrh = DisplayHeight(e->dpy, e->scr);

	e->vis = DefaultVisual(e->dpy, e->scr);
	e->cmap = DefaultColormap(e->dpy, e->scr);
	e->depth = DefaultDepth(e->dpy, e->scr);

	if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
		                    &bgcol, &bgcol))
		DIE("could not allocate color: %s", BG_COLOR);

	if (win->w > e->scrw)
		win->w = e->scrw;
	if (win->h > e->scrh)
		win->h = e->scrh;
	win->x = (e->scrw - win->w) / 2;
	win->y = (e->scrh - win->h) / 2;

	attr.backing_store = NotUseful;
	attr.background_pixel = bgcol.pixel;
	attr.save_under = False;
	mask = CWBackingStore | CWBackPixel | CWSaveUnder;

	win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
	                          win->x, win->y, win->w, win->h, 0,
	                          e->depth, InputOutput, e->vis, mask, &attr);
	if (win->xwin == None)
		DIE("could not create window");
	
	XSelectInput(e->dpy, win->xwin,
	             StructureNotifyMask | ExposureMask | KeyPressMask);

	XStoreName(e->dpy, win->xwin, "sxiv");
	XSetIconName(e->dpy, win->xwin, "Sxiv");

	if ((classhint = XAllocClassHint())) {
		classhint->res_name = "sxiv";
		classhint->res_class = "sxiv";
		XSetClassHint(e->dpy, win->xwin, classhint);
		XFree(classhint);
	}

	XMapWindow(e->dpy, win->xwin);
	XFlush(e->dpy);
}

void win_close(win_t *win) {
	if (!win)
		return;

	XDestroyWindow(win->env.dpy, win->xwin);
	XCloseDisplay(win->env.dpy);
}

int win_configure(win_t *win, XConfigureEvent *cev) {
	int changed;

	if (!win)
		return 0;
	
	changed = win->x != cev->x || win->y != cev->y ||
	          win->w != cev->width || win->h != cev->height;
	win->x = cev->x;
	win->y = cev->y;
	win->w = cev->width;
	win->h = cev->height;
	win->bw = cev->border_width;
	return changed;
}

void win_clear(win_t *win) {
	if (!win)
		return;

	XClearWindow(win->env.dpy, win->xwin);
}