Krolyxon
Commit 17e2a79

Branches

Refs →

nsxivwindow.c

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