nsxiv/window.c

339 lines
8.3 KiB
C
Raw Normal View History

2011-01-17 13:57:59 +00:00
/* sxiv: window.c
* Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
*
2011-09-03 21:11:45 +00:00
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
2011-09-03 21:11:45 +00:00
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
2011-09-03 21:11:45 +00:00
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2011-01-17 13:57:59 +00:00
*/
#define _POSIX_C_SOURCE 200112L
2011-09-11 19:01:24 +00:00
#define _WINDOW_CONFIG
2011-01-23 15:14:41 +00:00
#include <string.h>
2011-01-17 15:42:49 +00:00
#include <X11/Xutil.h>
2011-01-29 21:37:40 +00:00
#include <X11/cursorfont.h>
2011-01-17 15:42:49 +00:00
2011-02-03 09:15:01 +00:00
#include "options.h"
#include "util.h"
2011-01-17 13:57:59 +00:00
#include "window.h"
2011-07-22 12:49:06 +00:00
#include "config.h"
2011-01-17 13:57:59 +00:00
static Cursor carrow;
static Cursor cnone;
static Cursor chand;
static Cursor cwatch;
2011-02-17 15:22:54 +00:00
static GC gc;
2011-01-23 15:14:41 +00:00
Atom wm_delete_win;
void win_init(win_t *win) {
win_env_t *e;
2011-02-17 15:22:54 +00:00
XColor col;
2011-01-17 15:42:49 +00:00
2011-01-17 19:14:15 +00:00
if (!win)
2011-01-17 15:42:49 +00:00
return;
2011-01-21 11:57:35 +00:00
e = &win->env;
if (!(e->dpy = XOpenDisplay(NULL)))
2011-02-03 09:15:01 +00:00
die("could not open display");
2011-01-21 11:57:35 +00:00
e->scr = DefaultScreen(e->dpy);
e->scrw = DisplayWidth(e->dpy, e->scr);
e->scrh = DisplayHeight(e->dpy, e->scr);
e->vis = DefaultVisual(e->dpy, e->scr);
e->cmap = DefaultColormap(e->dpy, e->scr);
e->depth = DefaultDepth(e->dpy, e->scr);
2011-01-17 19:14:15 +00:00
2011-03-09 10:37:49 +00:00
win->black = BlackPixel(e->dpy, e->scr);
win->white = WhitePixel(e->dpy, e->scr);
2011-02-17 15:22:54 +00:00
if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
&col, &col))
{
2011-02-17 15:22:54 +00:00
win->bgcol = col.pixel;
} else {
2011-02-17 15:22:54 +00:00
die("could not allocate color: %s", BG_COLOR);
}
2011-02-17 15:22:54 +00:00
if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), SEL_COLOR,
&col, &col))
{
2011-02-17 15:22:54 +00:00
win->selcol = col.pixel;
} else {
die("could not allocate color: %s", SEL_COLOR);
}
2011-01-17 15:42:49 +00:00
win->xwin = 0;
2011-01-23 15:14:41 +00:00
win->pm = 0;
2011-09-11 19:01:24 +00:00
win->fullscreen = false;
}
void win_set_sizehints(win_t *win) {
XSizeHints sizehints;
if (!win || !win->xwin)
return;
sizehints.flags = PMinSize | PMaxSize;
sizehints.min_width = win->w;
sizehints.max_width = win->w;
sizehints.min_height = win->h;
sizehints.max_height = win->h;
XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
}
void win_open(win_t *win) {
win_env_t *e;
XClassHint classhint;
XColor col;
2011-09-11 19:01:24 +00:00
char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Pixmap none;
int gmask;
if (!win)
return;
e = &win->env;
/* determine window offsets, width & height */
if (!options->geometry)
gmask = 0;
else
gmask = XParseGeometry(options->geometry, &win->x, &win->y,
&win->w, &win->h);
if (!(gmask & WidthValue))
win->w = WIN_WIDTH;
if (win->w > e->scrw)
win->w = e->scrw;
if (!(gmask & HeightValue))
win->h = WIN_HEIGHT;
if (win->h > e->scrh)
win->h = e->scrh;
if (!(gmask & XValue))
win->x = (e->scrw - win->w) / 2;
else if (gmask & XNegative)
win->x += e->scrw - win->w;
if (!(gmask & YValue))
win->y = (e->scrh - win->h) / 2;
else if (gmask & YNegative)
win->y += e->scrh - win->h;
2011-01-17 15:42:49 +00:00
win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
win->x, win->y, win->w, win->h, 0,
2011-01-20 20:44:34 +00:00
e->depth, InputOutput, e->vis, 0, None);
2011-01-17 15:42:49 +00:00
if (win->xwin == None)
2011-02-03 09:15:01 +00:00
die("could not create window");
2011-01-17 15:42:49 +00:00
2011-01-29 21:37:40 +00:00
XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
2011-01-29 21:37:40 +00:00
carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
chand = XCreateFontCursor(e->dpy, XC_fleur);
cwatch = XCreateFontCursor(e->dpy, XC_watch);
2011-01-20 20:44:34 +00:00
if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
&col, &col))
{
die("could not allocate color: black");
}
none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
2011-03-09 10:37:49 +00:00
gc = XCreateGC(e->dpy, win->xwin, 0, None);
2011-01-17 15:42:49 +00:00
win_set_title(win, "sxiv");
2011-01-20 15:32:16 +00:00
2011-02-01 15:40:37 +00:00
classhint.res_name = "sxiv";
classhint.res_class = "sxiv";
XSetClassHint(e->dpy, win->xwin, &classhint);
2011-09-11 19:01:24 +00:00
if (options->fixed_win)
win_set_sizehints(win);
2011-01-17 15:42:49 +00:00
XMapWindow(e->dpy, win->xwin);
XFlush(e->dpy);
wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1);
2011-01-28 12:34:16 +00:00
if (options->fullscreen)
win_toggle_fullscreen(win);
2011-01-17 15:42:49 +00:00
}
void win_close(win_t *win) {
if (!win || !win->xwin)
2011-01-17 15:42:49 +00:00
return;
XFreeCursor(win->env.dpy, carrow);
XFreeCursor(win->env.dpy, cnone);
XFreeCursor(win->env.dpy, chand);
XFreeCursor(win->env.dpy, cwatch);
2011-01-29 21:37:40 +00:00
2011-02-17 15:22:54 +00:00
XFreeGC(win->env.dpy, gc);
2011-01-29 21:37:40 +00:00
XDestroyWindow(win->env.dpy, win->xwin);
XCloseDisplay(win->env.dpy);
2011-01-17 15:42:49 +00:00
}
2011-01-17 18:50:46 +00:00
2011-09-11 19:01:24 +00:00
bool win_configure(win_t *win, XConfigureEvent *c) {
bool changed;
2011-01-17 18:50:46 +00:00
2011-01-17 19:14:15 +00:00
if (!win)
2011-09-11 19:01:24 +00:00
return false;
2011-01-17 18:50:46 +00:00
2011-01-22 22:27:29 +00:00
changed = win->w != c->width || win->h != c->height;
win->x = c->x;
win->y = c->y;
win->w = c->width;
win->h = c->height;
win->bw = c->border_width;
2011-01-17 18:50:46 +00:00
return changed;
}
2011-01-18 19:11:28 +00:00
2011-09-11 19:01:24 +00:00
bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) {
if (!win || !win->xwin)
2011-09-11 19:01:24 +00:00
return false;
2011-02-06 16:40:48 +00:00
x = MAX(0, x);
y = MAX(0, y);
w = MIN(w, win->env.scrw - 2 * win->bw);
h = MIN(h, win->env.scrh - 2 * win->bw);
2011-02-06 16:40:48 +00:00
if (win->x == x && win->y == y && win->w == w && win->h == h)
2011-09-11 19:01:24 +00:00
return false;
2011-02-06 16:40:48 +00:00
win->x = x;
win->y = y;
win->w = w;
win->h = h;
2011-09-11 19:01:24 +00:00
if (options->fixed_win)
win_set_sizehints(win);
XMoveResizeWindow(win->env.dpy, win->xwin, win->x, win->y, win->w, win->h);
2011-09-11 19:01:24 +00:00
return true;
}
2011-01-23 15:14:41 +00:00
void win_toggle_fullscreen(win_t *win) {
XEvent ev;
XClientMessageEvent *cm;
if (!win || !win->xwin)
2011-01-23 15:14:41 +00:00
return;
2011-09-11 19:01:24 +00:00
win->fullscreen = !win->fullscreen;
2011-01-23 15:14:41 +00:00
memset(&ev, 0, sizeof(ev));
ev.type = ClientMessage;
cm = &ev.xclient;
cm->window = win->xwin;
cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
cm->format = 32;
cm->data.l[0] = win->fullscreen;
cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
cm->data.l[2] = cm->data.l[3] = 0;
2011-01-23 15:14:41 +00:00
XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
2011-04-14 18:01:11 +00:00
SubstructureNotifyMask | SubstructureRedirectMask, &ev);
2011-01-23 15:14:41 +00:00
}
2011-01-18 19:11:28 +00:00
void win_clear(win_t *win) {
2011-01-20 21:02:19 +00:00
win_env_t *e;
2011-01-23 15:14:41 +00:00
XGCValues gcval;
2011-01-20 21:02:19 +00:00
if (!win || !win->xwin)
2011-01-18 19:11:28 +00:00
return;
2011-01-20 21:02:19 +00:00
e = &win->env;
2011-03-09 10:37:49 +00:00
gcval.foreground = win->fullscreen ? win->black : win->bgcol;
2011-01-20 20:44:34 +00:00
if (win->pm)
2011-01-20 21:02:19 +00:00
XFreePixmap(e->dpy, win->pm);
win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
2011-01-23 15:14:41 +00:00
2011-02-17 15:22:54 +00:00
XChangeGC(e->dpy, gc, GCForeground, &gcval);
XFillRectangle(e->dpy, win->pm, gc, 0, 0, e->scrw, e->scrh);
}
void win_draw(win_t *win) {
if (!win || !win->xwin)
return;
XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
XClearWindow(win->env.dpy, win->xwin);
2011-02-17 15:22:54 +00:00
}
2011-03-09 10:37:49 +00:00
void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
2011-09-11 19:01:24 +00:00
bool fill, int lw, unsigned long col) {
2011-02-17 15:22:54 +00:00
XGCValues gcval;
2011-03-09 10:37:49 +00:00
if (!win || !pm)
2011-02-17 15:22:54 +00:00
return;
2011-03-09 10:37:49 +00:00
gcval.line_width = lw;
gcval.foreground = col;
XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
2011-02-17 15:22:54 +00:00
2011-03-09 10:37:49 +00:00
if (fill)
XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
2011-02-17 15:22:54 +00:00
else
2011-03-09 10:37:49 +00:00
XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
2011-01-20 20:44:34 +00:00
}
void win_set_title(win_t *win, const char *title) {
if (!win || !win->xwin)
return;
if (!title)
title = "sxiv";
XStoreName(win->env.dpy, win->xwin, title);
XSetIconName(win->env.dpy, win->xwin, title);
XChangeProperty(win->env.dpy, win->xwin,
XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
PropModeReplace, (unsigned char *) title, strlen(title));
XChangeProperty(win->env.dpy, win->xwin,
XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
PropModeReplace, (unsigned char *) title, strlen(title));
}
2011-07-22 12:49:06 +00:00
void win_set_cursor(win_t *win, cursor_t cursor) {
if (!win || !win->xwin)
2011-01-29 21:37:40 +00:00
return;
switch (cursor) {
case CURSOR_NONE:
XDefineCursor(win->env.dpy, win->xwin, cnone);
break;
2011-01-29 21:37:40 +00:00
case CURSOR_HAND:
XDefineCursor(win->env.dpy, win->xwin, chand);
break;
case CURSOR_WATCH:
XDefineCursor(win->env.dpy, win->xwin, cwatch);
2011-01-29 21:37:40 +00:00
break;
case CURSOR_ARROW:
default:
XDefineCursor(win->env.dpy, win->xwin, carrow);
2011-01-29 21:37:40 +00:00
break;
}
XFlush(win->env.dpy);
2011-01-29 21:37:40 +00:00
}