| 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 | |
| 22 | #include <X11/Xutil.h> |
| 23 | |
| 24 | #include "sxiv.h" |
| 25 | #include "window.h" |
| 26 | |
| 27 | Display *dpy; |
| 28 | int scr; |
| 29 | int scrw, scrh; |
| 30 | Visual *vis; |
| 31 | Colormap cmap; |
| 32 | int depth; |
| 33 | GC gc; |
| 34 | XColor bgcol; |
| 35 | |
| 36 | void win_open(win_t *win) { |
| 37 | XClassHint *classhint; |
| 38 | XSetWindowAttributes attr; |
| 39 | unsigned long mask; |
| 40 | |
| 41 | if (!win) |
| 42 | return; |
| 43 | |
| 44 | if (!(dpy = XOpenDisplay(NULL))) |
| 45 | FATAL("could not open display"); |
| 46 | |
| 47 | scr = DefaultScreen(dpy); |
| 48 | scrw = DisplayWidth(dpy, scr); |
| 49 | scrh = DisplayHeight(dpy, scr); |
| 50 | |
| 51 | vis = DefaultVisual(dpy, scr); |
| 52 | cmap = DefaultColormap(dpy, scr); |
| 53 | depth = DefaultDepth(dpy, scr); |
| 54 | |
| 55 | if (!XAllocNamedColor(dpy, DefaultColormap(dpy, scr), BG_COLOR, |
| 56 | &bgcol, &bgcol)) |
| 57 | FATAL("could not allocate color: %s", BG_COLOR); |
| 58 | |
| 59 | if (win->w > scrw) |
| 60 | win->w = scrw; |
| 61 | if (win->h > scrh) |
| 62 | win->h = scrh; |
| 63 | win->x = (scrw - win->w) / 2; |
| 64 | win->y = (scrh - win->h) / 2; |
| 65 | |
| 66 | attr.backing_store = NotUseful; |
| 67 | attr.background_pixel = bgcol.pixel; |
| 68 | attr.save_under = False; |
| 69 | mask = CWBackingStore | CWBackPixel | CWSaveUnder; |
| 70 | |
| 71 | win->xwin = XCreateWindow(dpy, RootWindow(dpy, scr), win->x, win->y, |
| 72 | win->w, win->h, 0, depth, InputOutput, vis, mask, &attr); |
| 73 | if (win->xwin == None) |
| 74 | FATAL("could not create window"); |
| 75 | |
| 76 | XSelectInput(dpy, win->xwin, |
| 77 | StructureNotifyMask | ExposureMask | KeyPressMask); |
| 78 | |
| 79 | gc = XCreateGC(dpy, win->xwin, 0, NULL); |
| 80 | |
| 81 | if ((classhint = XAllocClassHint())) { |
| 82 | classhint->res_name = "sxvi"; |
| 83 | classhint->res_class = "sxvi"; |
| 84 | XSetClassHint(dpy, win->xwin, classhint); |
| 85 | XFree(classhint); |
| 86 | } |
| 87 | |
| 88 | XMapWindow(dpy, win->xwin); |
| 89 | XFlush(dpy); |
| 90 | } |
| 91 | |
| 92 | void win_close(win_t *win) { |
| 93 | if (!win) |
| 94 | return; |
| 95 | |
| 96 | XDestroyWindow(dpy, win->xwin); |
| 97 | XFreeGC(dpy, gc); |
| 98 | XCloseDisplay(dpy); |
| 99 | } |
| 100 | |
| 101 | int win_configure(win_t *win, XConfigureEvent *cev) { |
| 102 | int changed; |
| 103 | |
| 104 | if (!win) |
| 105 | return 0; |
| 106 | |
| 107 | changed = win->x != cev->x || win->y != cev->y || |
| 108 | win->w != cev->width || win->h != cev->height; |
| 109 | win->x = cev->x; |
| 110 | win->y = cev->y; |
| 111 | win->w = cev->width; |
| 112 | win->h = cev->height; |
| 113 | win->bw = cev->border_width; |
| 114 | return changed; |
| 115 | } |