| 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 <string.h> |
| 20 | |
| 21 | #include <X11/Xutil.h> |
| 22 | #include <X11/cursorfont.h> |
| 23 | |
| 24 | #include "config.h" |
| 25 | #include "options.h" |
| 26 | #include "util.h" |
| 27 | #include "window.h" |
| 28 | |
| 29 | static Cursor carrow; |
| 30 | static Cursor cnone; |
| 31 | static Cursor chand; |
| 32 | static Cursor cwatch; |
| 33 | static GC gc; |
| 34 | |
| 35 | Atom wm_delete_win; |
| 36 | |
| 37 | void win_init(win_t *win) { |
| 38 | win_env_t *e; |
| 39 | XColor col; |
| 40 | |
| 41 | if (!win) |
| 42 | return; |
| 43 | |
| 44 | e = &win->env; |
| 45 | if (!(e->dpy = XOpenDisplay(NULL))) |
| 46 | die("could not open display"); |
| 47 | |
| 48 | e->scr = DefaultScreen(e->dpy); |
| 49 | e->scrw = DisplayWidth(e->dpy, e->scr); |
| 50 | e->scrh = DisplayHeight(e->dpy, e->scr); |
| 51 | e->vis = DefaultVisual(e->dpy, e->scr); |
| 52 | e->cmap = DefaultColormap(e->dpy, e->scr); |
| 53 | e->depth = DefaultDepth(e->dpy, e->scr); |
| 54 | |
| 55 | win->black = BlackPixel(e->dpy, e->scr); |
| 56 | win->white = WhitePixel(e->dpy, e->scr); |
| 57 | |
| 58 | if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR, |
| 59 | &col, &col)) |
| 60 | { |
| 61 | win->bgcol = col.pixel; |
| 62 | } else { |
| 63 | die("could not allocate color: %s", BG_COLOR); |
| 64 | } |
| 65 | |
| 66 | if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), SEL_COLOR, |
| 67 | &col, &col)) |
| 68 | { |
| 69 | win->selcol = col.pixel; |
| 70 | } else { |
| 71 | die("could not allocate color: %s", SEL_COLOR); |
| 72 | } |
| 73 | |
| 74 | win->xwin = 0; |
| 75 | win->pm = 0; |
| 76 | win->fullscreen = 0; |
| 77 | } |
| 78 | |
| 79 | void win_set_sizehints(win_t *win) { |
| 80 | XSizeHints sizehints; |
| 81 | |
| 82 | if (!win || !win->xwin) |
| 83 | return; |
| 84 | |
| 85 | sizehints.flags = PMinSize | PMaxSize; |
| 86 | sizehints.min_width = win->w; |
| 87 | sizehints.max_width = win->w; |
| 88 | sizehints.min_height = win->h; |
| 89 | sizehints.max_height = win->h; |
| 90 | XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints); |
| 91 | } |
| 92 | |
| 93 | void win_open(win_t *win) { |
| 94 | win_env_t *e; |
| 95 | XClassHint classhint; |
| 96 | XColor col; |
| 97 | char none_data[] = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 98 | Pixmap none; |
| 99 | int gmask; |
| 100 | |
| 101 | if (!win) |
| 102 | return; |
| 103 | |
| 104 | e = &win->env; |
| 105 | |
| 106 | /* determine window offsets, width & height */ |
| 107 | if (!options->geometry) |
| 108 | gmask = 0; |
| 109 | else |
| 110 | gmask = XParseGeometry(options->geometry, &win->x, &win->y, |
| 111 | &win->w, &win->h); |
| 112 | if (!(gmask & WidthValue)) |
| 113 | win->w = WIN_WIDTH; |
| 114 | if (win->w > e->scrw) |
| 115 | win->w = e->scrw; |
| 116 | if (!(gmask & HeightValue)) |
| 117 | win->h = WIN_HEIGHT; |
| 118 | if (win->h > e->scrh) |
| 119 | win->h = e->scrh; |
| 120 | if (!(gmask & XValue)) |
| 121 | win->x = (e->scrw - win->w) / 2; |
| 122 | else if (gmask & XNegative) |
| 123 | win->x += e->scrw - win->w; |
| 124 | if (!(gmask & YValue)) |
| 125 | win->y = (e->scrh - win->h) / 2; |
| 126 | else if (gmask & YNegative) |
| 127 | win->y += e->scrh - win->h; |
| 128 | |
| 129 | win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr), |
| 130 | win->x, win->y, win->w, win->h, 0, |
| 131 | e->depth, InputOutput, e->vis, 0, None); |
| 132 | if (win->xwin == None) |
| 133 | die("could not create window"); |
| 134 | |
| 135 | XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask | |
| 136 | ButtonPressMask | ButtonReleaseMask | PointerMotionMask); |
| 137 | |
| 138 | carrow = XCreateFontCursor(e->dpy, XC_left_ptr); |
| 139 | chand = XCreateFontCursor(e->dpy, XC_fleur); |
| 140 | cwatch = XCreateFontCursor(e->dpy, XC_watch); |
| 141 | |
| 142 | if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black", |
| 143 | &col, &col)) |
| 144 | { |
| 145 | die("could not allocate color: black"); |
| 146 | } |
| 147 | none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8); |
| 148 | cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0); |
| 149 | |
| 150 | gc = XCreateGC(e->dpy, win->xwin, 0, None); |
| 151 | |
| 152 | win_set_title(win, "sxiv"); |
| 153 | |
| 154 | classhint.res_name = "sxiv"; |
| 155 | classhint.res_class = "sxiv"; |
| 156 | XSetClassHint(e->dpy, win->xwin, &classhint); |
| 157 | |
| 158 | if (options->fixed) |
| 159 | win_set_sizehints(win); |
| 160 | |
| 161 | XMapWindow(e->dpy, win->xwin); |
| 162 | XFlush(e->dpy); |
| 163 | |
| 164 | wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False); |
| 165 | XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1); |
| 166 | |
| 167 | if (options->fullscreen) |
| 168 | win_toggle_fullscreen(win); |
| 169 | } |
| 170 | |
| 171 | void win_close(win_t *win) { |
| 172 | if (!win || !win->xwin) |
| 173 | return; |
| 174 | |
| 175 | XFreeCursor(win->env.dpy, carrow); |
| 176 | XFreeCursor(win->env.dpy, cnone); |
| 177 | XFreeCursor(win->env.dpy, chand); |
| 178 | XFreeCursor(win->env.dpy, cwatch); |
| 179 | |
| 180 | XFreeGC(win->env.dpy, gc); |
| 181 | |
| 182 | XDestroyWindow(win->env.dpy, win->xwin); |
| 183 | XCloseDisplay(win->env.dpy); |
| 184 | } |
| 185 | |
| 186 | int win_configure(win_t *win, XConfigureEvent *c) { |
| 187 | int changed; |
| 188 | |
| 189 | if (!win) |
| 190 | return 0; |
| 191 | |
| 192 | changed = win->w != c->width || win->h != c->height; |
| 193 | |
| 194 | win->x = c->x; |
| 195 | win->y = c->y; |
| 196 | win->w = c->width; |
| 197 | win->h = c->height; |
| 198 | win->bw = c->border_width; |
| 199 | |
| 200 | return changed; |
| 201 | } |
| 202 | |
| 203 | int win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) { |
| 204 | if (!win || !win->xwin) |
| 205 | return 0; |
| 206 | |
| 207 | x = MAX(0, x); |
| 208 | y = MAX(0, y); |
| 209 | w = MIN(w, win->env.scrw - 2 * win->bw); |
| 210 | h = MIN(h, win->env.scrh - 2 * win->bw); |
| 211 | |
| 212 | if (win->x == x && win->y == y && win->w == w && win->h == h) |
| 213 | return 0; |
| 214 | |
| 215 | win->x = x; |
| 216 | win->y = y; |
| 217 | win->w = w; |
| 218 | win->h = h; |
| 219 | |
| 220 | if (options->fixed) |
| 221 | win_set_sizehints(win); |
| 222 | |
| 223 | XMoveResizeWindow(win->env.dpy, win->xwin, win->x, win->y, win->w, win->h); |
| 224 | |
| 225 | return 1; |
| 226 | } |
| 227 | |
| 228 | void win_toggle_fullscreen(win_t *win) { |
| 229 | XEvent ev; |
| 230 | XClientMessageEvent *cm; |
| 231 | |
| 232 | if (!win || !win->xwin) |
| 233 | return; |
| 234 | |
| 235 | win->fullscreen ^= 1; |
| 236 | |
| 237 | memset(&ev, 0, sizeof(ev)); |
| 238 | ev.type = ClientMessage; |
| 239 | |
| 240 | cm = &ev.xclient; |
| 241 | cm->window = win->xwin; |
| 242 | cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False); |
| 243 | cm->format = 32; |
| 244 | cm->data.l[0] = win->fullscreen; |
| 245 | cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False); |
| 246 | cm->data.l[2] = XInternAtom(win->env.dpy, "_NET_WM_STATE_ABOVE", False); |
| 247 | cm->data.l[3] = 0; |
| 248 | |
| 249 | XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False, |
| 250 | SubstructureNotifyMask, &ev); |
| 251 | } |
| 252 | |
| 253 | void win_clear(win_t *win) { |
| 254 | win_env_t *e; |
| 255 | XGCValues gcval; |
| 256 | |
| 257 | if (!win || !win->xwin) |
| 258 | return; |
| 259 | |
| 260 | e = &win->env; |
| 261 | gcval.foreground = win->fullscreen ? win->black : win->bgcol; |
| 262 | |
| 263 | if (win->pm) |
| 264 | XFreePixmap(e->dpy, win->pm); |
| 265 | win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth); |
| 266 | |
| 267 | XChangeGC(e->dpy, gc, GCForeground, &gcval); |
| 268 | XFillRectangle(e->dpy, win->pm, gc, 0, 0, e->scrw, e->scrh); |
| 269 | } |
| 270 | |
| 271 | void win_draw(win_t *win) { |
| 272 | if (!win || !win->xwin) |
| 273 | return; |
| 274 | |
| 275 | XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm); |
| 276 | XClearWindow(win->env.dpy, win->xwin); |
| 277 | } |
| 278 | |
| 279 | void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h, |
| 280 | Bool fill, int lw, unsigned long col) { |
| 281 | XGCValues gcval; |
| 282 | |
| 283 | if (!win || !pm) |
| 284 | return; |
| 285 | |
| 286 | gcval.line_width = lw; |
| 287 | gcval.foreground = col; |
| 288 | XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval); |
| 289 | |
| 290 | if (fill) |
| 291 | XFillRectangle(win->env.dpy, pm, gc, x, y, w, h); |
| 292 | else |
| 293 | XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h); |
| 294 | } |
| 295 | |
| 296 | void win_set_title(win_t *win, const char *title) { |
| 297 | if (!win || !win->xwin) |
| 298 | return; |
| 299 | |
| 300 | if (!title) |
| 301 | title = "sxiv"; |
| 302 | |
| 303 | XStoreName(win->env.dpy, win->xwin, title); |
| 304 | XSetIconName(win->env.dpy, win->xwin, title); |
| 305 | |
| 306 | XChangeProperty(win->env.dpy, win->xwin, |
| 307 | XInternAtom(win->env.dpy, "_NET_WM_NAME", False), |
| 308 | XInternAtom(win->env.dpy, "UTF8_STRING", False), 8, |
| 309 | PropModeReplace, (unsigned char *) title, strlen(title)); |
| 310 | XChangeProperty(win->env.dpy, win->xwin, |
| 311 | XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False), |
| 312 | XInternAtom(win->env.dpy, "UTF8_STRING", False), 8, |
| 313 | PropModeReplace, (unsigned char *) title, strlen(title)); |
| 314 | } |
| 315 | |
| 316 | void win_set_cursor(win_t *win, win_cur_t cursor) { |
| 317 | if (!win || !win->xwin) |
| 318 | return; |
| 319 | |
| 320 | switch (cursor) { |
| 321 | case CURSOR_NONE: |
| 322 | XDefineCursor(win->env.dpy, win->xwin, cnone); |
| 323 | break; |
| 324 | case CURSOR_HAND: |
| 325 | XDefineCursor(win->env.dpy, win->xwin, chand); |
| 326 | break; |
| 327 | case CURSOR_WATCH: |
| 328 | XDefineCursor(win->env.dpy, win->xwin, cwatch); |
| 329 | break; |
| 330 | case CURSOR_ARROW: |
| 331 | default: |
| 332 | XDefineCursor(win->env.dpy, win->xwin, carrow); |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | XFlush(win->env.dpy); |
| 337 | } |