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