Krolyxon
Commit 6e0db8c

Branches

Refs →

nsxivwindow.c

86 lines · 2.2 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
36 if (win == NULL)
37 return;
38
39 if (!(dpy = XOpenDisplay(NULL)))
40 FATAL("could not open display");
41
42 scr = DefaultScreen(dpy);
43 scrw = DisplayWidth(dpy, scr);
44 scrh = DisplayHeight(dpy, scr);
45
46 if (!XAllocNamedColor(dpy, DefaultColormap(dpy, scr), BG_COLOR,
47 &bgcol, &bgcol))
48 FATAL("could not allocate color: %s", BG_COLOR);
49
50 if (win->w > scrw)
51 win->w = scrw;
52 if (win->h > scrh)
53 win->h = scrh;
54 win->x = (scrw - win->w) / 2;
55 win->y = (scrh - win->h) / 2;
56
57 win->xwin = XCreateWindow(dpy, RootWindow(dpy, scr),
58 win->x, win->y, win->w, win->h, 0, DefaultDepth(dpy, scr), InputOutput,
59 DefaultVisual(dpy, scr), 0, NULL);
60 if (win->xwin == None)
61 FATAL("could not create window");
62
63 XSelectInput(dpy, win->xwin,
64 StructureNotifyMask | ExposureMask | KeyPressMask);
65
66 gc = XCreateGC(dpy, win->xwin, 0, NULL);
67
68 if ((classhint = XAllocClassHint())) {
69 classhint->res_name = "sxvi";
70 classhint->res_class = "sxvi";
71 XSetClassHint(dpy, win->xwin, classhint);
72 XFree(classhint);
73 }
74
75 XMapWindow(dpy, win->xwin);
76 XFlush(dpy);
77}
78
79void win_close(win_t *win) {
80 if (win == NULL)
81 return;
82
83 XDestroyWindow(dpy, win->xwin);
84 XFreeGC(dpy, gc);
85 XCloseDisplay(dpy);
86}