Krolyxon
Commit f2ceac2

Branches

Refs →

nsxivwindow.c

229 lines · 5.4 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#include <X11/cursorfont.h>
25
26#include "sxiv.h"
27#include "window.h"
28
29static Cursor arrow;
30static Cursor hand;
31
32static GC bgc;
33
34void win_open(win_t *win) {
35 win_env_t *e;
36 XClassHint classhint;
37 XSizeHints sizehints;
38 XColor bgcol;
39 int gmask;
40
41 if (!win)
42 return;
43
44 e = &win->env;
45 if (!(e->dpy = XOpenDisplay(NULL)))
46 DIE("could not open display");
47
48 e->scr = DefaultScreen(e->dpy);
49 e->scrw = DisplayWidth(e->dpy, e->scr);
50 e->scrh = DisplayHeight(e->dpy, e->scr);
51 e->vis = DefaultVisual(e->dpy, e->scr);
52 e->cmap = DefaultColormap(e->dpy, e->scr);
53 e->depth = DefaultDepth(e->dpy, e->scr);
54
55 if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
56 &bgcol, &bgcol))
57 DIE("could not allocate color: %s", BG_COLOR);
58
59 win->bgcol = bgcol.pixel;
60 win->pm = 0;
61
62 win->fullscreen = 0;
63
64 /* determine window offsets, width & height */
65 if (!options->geometry)
66 gmask = 0;
67 else
68 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
69 &win->w, &win->h);
70 if (!(gmask & WidthValue))
71 win->w = WIN_WIDTH;
72 if (win->w > e->scrw)
73 win->w = e->scrw;
74 if (!(gmask & HeightValue))
75 win->h = WIN_HEIGHT;
76 if (win->h > e->scrh)
77 win->h = e->scrh;
78 if (!(gmask & XValue))
79 win->x = (e->scrw - win->w) / 2;
80 else if (gmask & XNegative)
81 win->x += e->scrw - win->w;
82 if (!(gmask & YValue))
83 win->y = (e->scrh - win->h) / 2;
84 else if (gmask & YNegative)
85 win->y += e->scrh - win->h;
86
87 win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
88 win->x, win->y, win->w, win->h, 0,
89 e->depth, InputOutput, e->vis, 0, None);
90 if (win->xwin == None)
91 DIE("could not create window");
92
93 XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
94 ButtonPressMask | ButtonReleaseMask | Button2MotionMask);
95
96 arrow = XCreateFontCursor(e->dpy, XC_left_ptr);
97 hand = XCreateFontCursor(e->dpy, XC_fleur);
98
99 bgc = XCreateGC(e->dpy, win->xwin, 0, None);
100
101 win_set_title(win, "sxiv");
102
103 classhint.res_name = "sxiv";
104 classhint.res_class = "sxiv";
105 XSetClassHint(e->dpy, win->xwin, &classhint);
106
107 if (options->fixed) {
108 sizehints.flags = PMinSize | PMaxSize;
109 sizehints.min_width = win->w;
110 sizehints.max_width = win->w;
111 sizehints.min_height = win->h;
112 sizehints.max_height = win->h;
113 XSetWMNormalHints(e->dpy, win->xwin, &sizehints);
114 }
115
116 XMapWindow(e->dpy, win->xwin);
117 XFlush(e->dpy);
118
119 if (options->fullscreen)
120 win_toggle_fullscreen(win);
121}
122
123void win_close(win_t *win) {
124 if (!win)
125 return;
126
127 XFreeCursor(win->env.dpy, arrow);
128 XFreeCursor(win->env.dpy, hand);
129
130 XFreeGC(win->env.dpy, bgc);
131
132 XDestroyWindow(win->env.dpy, win->xwin);
133 XCloseDisplay(win->env.dpy);
134}
135
136void win_set_title(win_t *win, const char *title) {
137 if (!win)
138 return;
139
140 if (!title)
141 title = "sxiv";
142
143 XStoreName(win->env.dpy, win->xwin, title);
144 XSetIconName(win->env.dpy, win->xwin, title);
145}
146
147int win_configure(win_t *win, XConfigureEvent *c) {
148 int changed;
149
150 if (!win)
151 return 0;
152
153 changed = win->w != c->width || win->h != c->height;
154
155 win->x = c->x;
156 win->y = c->y;
157 win->w = c->width;
158 win->h = c->height;
159 win->bw = c->border_width;
160
161 return changed;
162}
163
164void win_toggle_fullscreen(win_t *win) {
165 XEvent ev;
166 XClientMessageEvent *cm;
167
168 if (!win)
169 return;
170
171 win->fullscreen ^= 1;
172
173 memset(&ev, 0, sizeof(ev));
174 ev.type = ClientMessage;
175
176 cm = &ev.xclient;
177 cm->window = win->xwin;
178 cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
179 cm->format = 32;
180 cm->data.l[0] = win->fullscreen;
181 cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
182 cm->data.l[2] = XInternAtom(win->env.dpy, "_NET_WM_STATE_ABOVE", False);
183 cm->data.l[3] = 0;
184
185 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
186 SubstructureNotifyMask, &ev);
187}
188
189void win_clear(win_t *win) {
190 win_env_t *e;
191 XGCValues gcval;
192
193 if (!win)
194 return;
195
196 e = &win->env;
197
198 if (win->pm)
199 XFreePixmap(e->dpy, win->pm);
200 win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
201
202 gcval.foreground = win->fullscreen ? BlackPixel(e->dpy, e->scr) : win->bgcol;
203 XChangeGC(e->dpy, bgc, GCForeground, &gcval);
204
205 XFillRectangle(e->dpy, win->pm, bgc, 0, 0, e->scrw, e->scrh);
206}
207
208void win_draw(win_t *win) {
209 if (!win)
210 return;
211
212 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
213 XClearWindow(win->env.dpy, win->xwin);
214}
215
216void win_set_cursor(win_t *win, win_cur_t cursor) {
217 if (!win)
218 return;
219
220 switch (cursor) {
221 case CURSOR_HAND:
222 XDefineCursor(win->env.dpy, win->xwin, hand);
223 break;
224 case CURSOR_ARROW:
225 default:
226 XDefineCursor(win->env.dpy, win->xwin, arrow);
227 break;
228 }
229}