Krolyxon
Commit 75b143a

Branches

Refs →

nsxivwindow.c

109 lines · 2.7 KB · C

1/* sxiv: window.c
2 * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#include <stdlib.h>
20#include <stdio.h>
21
22#include <X11/Xutil.h>
23
24#include "sxiv.h"
25#include "window.h"
26
27Display *dpy;
28int scr;
29int scrw, scrh;
30GC gc;
31XColor bgcol;
32
33void win_open(win_t *win) {
34 XClassHint *classhint;
35 XSetWindowAttributes attr;
36 unsigned long mask;
37
38 if (win == NULL)
39 return;
40
41 if (!(dpy = XOpenDisplay(NULL)))
42 FATAL("could not open display");
43
44 scr = DefaultScreen(dpy);
45 scrw = DisplayWidth(dpy, scr);
46 scrh = DisplayHeight(dpy, scr);
47
48 if (!XAllocNamedColor(dpy, DefaultColormap(dpy, scr), BG_COLOR,
49 &bgcol, &bgcol))
50 FATAL("could not allocate color: %s", BG_COLOR);
51
52 if (win->w > scrw)
53 win->w = scrw;
54 if (win->h > scrh)
55 win->h = scrh;
56 win->x = (scrw - win->w) / 2;
57 win->y = (scrh - win->h) / 2;
58
59 attr.backing_store = NotUseful;
60 attr.background_pixel = bgcol.pixel;
61 attr.save_under = False;
62 mask = CWBackingStore | CWBackPixel | CWSaveUnder;
63
64 win->xwin = XCreateWindow(dpy, RootWindow(dpy, scr),
65 win->x, win->y, win->w, win->h, 0, DefaultDepth(dpy, scr), InputOutput,
66 DefaultVisual(dpy, scr), mask, &attr);
67 if (win->xwin == None)
68 FATAL("could not create window");
69
70 XSelectInput(dpy, win->xwin,
71 StructureNotifyMask | ExposureMask | KeyPressMask);
72
73 gc = XCreateGC(dpy, win->xwin, 0, NULL);
74
75 if ((classhint = XAllocClassHint())) {
76 classhint->res_name = "sxvi";
77 classhint->res_class = "sxvi";
78 XSetClassHint(dpy, win->xwin, classhint);
79 XFree(classhint);
80 }
81
82 XMapWindow(dpy, win->xwin);
83 XFlush(dpy);
84}
85
86void win_close(win_t *win) {
87 if (win == NULL)
88 return;
89
90 XDestroyWindow(dpy, win->xwin);
91 XFreeGC(dpy, gc);
92 XCloseDisplay(dpy);
93}
94
95int win_configure(win_t *win, XConfigureEvent *cev) {
96 int changed;
97
98 if (win == NULL)
99 return 0;
100
101 changed = win->x != cev->x || win->y != cev->y ||
102 win->w != cev->width || win->h != cev->height;
103 win->x = cev->x;
104 win->y = cev->y;
105 win->w = cev->width;
106 win->h = cev->height;
107 win->bw = cev->border_width;
108 return changed;
109}