Krolyxon
Commit c937570

Branches

Refs →

nsxivwindow.c

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