Krolyxon
Commit e8a503b

Branches

Refs →

nsxivwindow.c

175 lines · 4.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#include <string.h>
22
23#include <X11/Xutil.h>
24
25#include "sxiv.h"
26#include "window.h"
27
28GC bgc;
29
30void win_open(win_t *win) {
31 win_env_t *e;
32 XClassHint *classhint;
33 XColor bgcol;
34
35 if (!win)
36 return;
37
38 e = &win->env;
39 if (!(e->dpy = XOpenDisplay(NULL)))
40 DIE("could not open display");
41
42 e->scr = DefaultScreen(e->dpy);
43 e->scrw = DisplayWidth(e->dpy, e->scr);
44 e->scrh = DisplayHeight(e->dpy, e->scr);
45 e->vis = DefaultVisual(e->dpy, e->scr);
46 e->cmap = DefaultColormap(e->dpy, e->scr);
47 e->depth = DefaultDepth(e->dpy, e->scr);
48
49 if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
50 &bgcol, &bgcol))
51 DIE("could not allocate color: %s", BG_COLOR);
52
53 win->bgcol = bgcol.pixel;
54 win->pm = 0;
55
56 win->w = WIN_WIDTH;
57 win->h = WIN_HEIGHT;
58 if (win->w > e->scrw)
59 win->w = e->scrw;
60 if (win->h > e->scrh)
61 win->h = e->scrh;
62 win->x = (e->scrw - win->w) / 2;
63 win->y = (e->scrh - win->h) / 2;
64
65 win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
66 win->x, win->y, win->w, win->h, 0,
67 e->depth, InputOutput, e->vis, 0, None);
68 if (win->xwin == None)
69 DIE("could not create window");
70
71 XSelectInput(e->dpy, win->xwin,
72 StructureNotifyMask | KeyPressMask | ButtonPressMask);
73
74 bgc = XCreateGC(e->dpy, win->xwin, 0, None);
75
76 win_set_title(win, "sxiv");
77
78 if ((classhint = XAllocClassHint())) {
79 classhint->res_name = "sxiv";
80 classhint->res_class = "sxiv";
81 XSetClassHint(e->dpy, win->xwin, classhint);
82 XFree(classhint);
83 }
84
85 XMapWindow(e->dpy, win->xwin);
86 XFlush(e->dpy);
87}
88
89void win_close(win_t *win) {
90 if (!win)
91 return;
92
93 XDestroyWindow(win->env.dpy, win->xwin);
94 XCloseDisplay(win->env.dpy);
95}
96
97void win_set_title(win_t *win, const char *title) {
98 if (!win)
99 return;
100
101 if (!title)
102 title = "sxiv";
103
104 XStoreName(win->env.dpy, win->xwin, title);
105 XSetIconName(win->env.dpy, win->xwin, title);
106}
107
108int win_configure(win_t *win, XConfigureEvent *c) {
109 int changed;
110
111 if (!win)
112 return 0;
113
114 changed = win->w != c->width || win->h != c->height;
115
116 win->x = c->x;
117 win->y = c->y;
118 win->w = c->width;
119 win->h = c->height;
120 win->bw = c->border_width;
121
122 return changed;
123}
124
125void win_toggle_fullscreen(win_t *win) {
126 XEvent ev;
127 XClientMessageEvent *cm;
128
129 if (!win)
130 return;
131
132 win->fullscreen ^= 1;
133
134 memset(&ev, 0, sizeof(ev));
135 ev.type = ClientMessage;
136
137 cm = &ev.xclient;
138 cm->window = win->xwin;
139 cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
140 cm->format = 32;
141 cm->data.l[0] = win->fullscreen;
142 cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
143 cm->data.l[2] = XInternAtom(win->env.dpy, "_NET_WM_STATE_ABOVE", False);
144 cm->data.l[3] = 0;
145
146 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
147 SubstructureNotifyMask, &ev);
148}
149
150void win_clear(win_t *win) {
151 win_env_t *e;
152 XGCValues gcval;
153
154 if (!win)
155 return;
156
157 e = &win->env;
158
159 if (win->pm)
160 XFreePixmap(e->dpy, win->pm);
161 win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
162
163 gcval.foreground = win->fullscreen ? BlackPixel(e->dpy, e->scr) : win->bgcol;
164 XChangeGC(e->dpy, bgc, GCForeground, &gcval);
165
166 XFillRectangle(e->dpy, win->pm, bgc, 0, 0, e->scrw, e->scrh);
167}
168
169void win_draw(win_t *win) {
170 if (!win)
171 return;
172
173 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
174 XClearWindow(win->env.dpy, win->xwin);
175}