Krolyxon
Commit e2d9463

Branches

Refs →

nsxivwindow.c

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