Krolyxon
Commit 8fb3110

Branches

Refs →

nsxivwindow.c

222 lines · 5.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#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 else if (gmask & XNegative)
80 win->x += e->scrw - win->w;
81 if (!(gmask & YValue))
82 win->y = (e->scrh - win->h) / 2;
83 else if (gmask & YNegative)
84 win->y += e->scrh - win->h;
85
86 win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
87 win->x, win->y, win->w, win->h, 0,
88 e->depth, InputOutput, e->vis, 0, None);
89 if (win->xwin == None)
90 DIE("could not create window");
91
92 XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
93 ButtonPressMask | ButtonReleaseMask | Button2MotionMask);
94
95 arrow = XCreateFontCursor(e->dpy, XC_left_ptr);
96 hand = XCreateFontCursor(e->dpy, XC_fleur);
97
98 bgc = XCreateGC(e->dpy, win->xwin, 0, None);
99
100 win_set_title(win, "sxiv");
101
102 if ((classhint = XAllocClassHint())) {
103 classhint->res_name = "sxiv";
104 classhint->res_class = "sxiv";
105 XSetClassHint(e->dpy, win->xwin, classhint);
106 XFree(classhint);
107 }
108
109 XMapWindow(e->dpy, win->xwin);
110 XFlush(e->dpy);
111
112 if (options->fullscreen)
113 win_toggle_fullscreen(win);
114}
115
116void win_close(win_t *win) {
117 if (!win)
118 return;
119
120 XFreeCursor(win->env.dpy, arrow);
121 XFreeCursor(win->env.dpy, hand);
122
123 XFreeGC(win->env.dpy, bgc);
124
125 XDestroyWindow(win->env.dpy, win->xwin);
126 XCloseDisplay(win->env.dpy);
127}
128
129void win_set_title(win_t *win, const char *title) {
130 if (!win)
131 return;
132
133 if (!title)
134 title = "sxiv";
135
136 XStoreName(win->env.dpy, win->xwin, title);
137 XSetIconName(win->env.dpy, win->xwin, title);
138}
139
140int win_configure(win_t *win, XConfigureEvent *c) {
141 int changed;
142
143 if (!win)
144 return 0;
145
146 changed = win->w != c->width || win->h != c->height;
147
148 win->x = c->x;
149 win->y = c->y;
150 win->w = c->width;
151 win->h = c->height;
152 win->bw = c->border_width;
153
154 return changed;
155}
156
157void win_toggle_fullscreen(win_t *win) {
158 XEvent ev;
159 XClientMessageEvent *cm;
160
161 if (!win)
162 return;
163
164 win->fullscreen ^= 1;
165
166 memset(&ev, 0, sizeof(ev));
167 ev.type = ClientMessage;
168
169 cm = &ev.xclient;
170 cm->window = win->xwin;
171 cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
172 cm->format = 32;
173 cm->data.l[0] = win->fullscreen;
174 cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
175 cm->data.l[2] = XInternAtom(win->env.dpy, "_NET_WM_STATE_ABOVE", False);
176 cm->data.l[3] = 0;
177
178 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
179 SubstructureNotifyMask, &ev);
180}
181
182void win_clear(win_t *win) {
183 win_env_t *e;
184 XGCValues gcval;
185
186 if (!win)
187 return;
188
189 e = &win->env;
190
191 if (win->pm)
192 XFreePixmap(e->dpy, win->pm);
193 win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
194
195 gcval.foreground = win->fullscreen ? BlackPixel(e->dpy, e->scr) : win->bgcol;
196 XChangeGC(e->dpy, bgc, GCForeground, &gcval);
197
198 XFillRectangle(e->dpy, win->pm, bgc, 0, 0, e->scrw, e->scrh);
199}
200
201void win_draw(win_t *win) {
202 if (!win)
203 return;
204
205 XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
206 XClearWindow(win->env.dpy, win->xwin);
207}
208
209void win_set_cursor(win_t *win, win_cur_t cursor) {
210 if (!win)
211 return;
212
213 switch (cursor) {
214 case CURSOR_HAND:
215 XDefineCursor(win->env.dpy, win->xwin, hand);
216 break;
217 case CURSOR_ARROW:
218 default:
219 XDefineCursor(win->env.dpy, win->xwin, arrow);
220 break;
221 }
222}