Krolyxon
Commit b1ce42e

Branches

Refs →

nsxivwindow.c

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