2013-02-08 21:52:41 +01:00
|
|
|
/* Copyright 2011-2013 Bert Muennich
|
2011-01-17 14:57:59 +01:00
|
|
|
*
|
2013-02-08 21:52:41 +01:00
|
|
|
* This file is part of sxiv.
|
2011-08-18 01:18:26 +02:00
|
|
|
*
|
2013-02-08 21:52:41 +01:00
|
|
|
* sxiv 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-08-18 01:18:26 +02:00
|
|
|
*
|
2013-02-08 21:52:41 +01:00
|
|
|
* sxiv 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with sxiv. If not, see <http://www.gnu.org/licenses/>.
|
2011-01-17 14:57:59 +01:00
|
|
|
*/
|
|
|
|
|
2017-10-16 21:10:35 +02:00
|
|
|
#include "sxiv.h"
|
|
|
|
#define _WINDOW_CONFIG
|
|
|
|
#include "config.h"
|
|
|
|
#include "icon/data.h"
|
2017-12-07 14:57:02 +01:00
|
|
|
#include "utf8.h"
|
2017-10-16 21:10:35 +02:00
|
|
|
|
2014-01-15 22:40:34 +01:00
|
|
|
#include <stdlib.h>
|
2011-01-23 16:14:41 +01:00
|
|
|
#include <string.h>
|
2012-02-12 19:00:41 +01:00
|
|
|
#include <locale.h>
|
2011-01-29 22:37:40 +01:00
|
|
|
#include <X11/cursorfont.h>
|
2014-01-15 22:40:34 +01:00
|
|
|
#include <X11/Xatom.h>
|
2011-01-17 16:42:49 +01:00
|
|
|
|
2012-02-12 19:00:41 +01:00
|
|
|
enum {
|
|
|
|
H_TEXT_PAD = 5,
|
|
|
|
V_TEXT_PAD = 1
|
|
|
|
};
|
|
|
|
|
2017-10-05 12:30:31 +02:00
|
|
|
static struct {
|
|
|
|
int name;
|
|
|
|
Cursor icon;
|
|
|
|
} cursors[CURSOR_COUNT] = {
|
2017-10-05 13:53:29 +02:00
|
|
|
{ XC_left_ptr }, { XC_dotbox }, { XC_watch },
|
|
|
|
{ XC_sb_left_arrow }, { XC_sb_right_arrow }
|
2017-10-05 12:30:31 +02:00
|
|
|
};
|
|
|
|
|
2011-02-17 16:22:54 +01:00
|
|
|
static GC gc;
|
2011-01-23 16:14:41 +01:00
|
|
|
|
2016-08-06 15:27:58 +02:00
|
|
|
static XftFont *font;
|
2013-01-07 14:30:42 +01:00
|
|
|
static int fontheight;
|
|
|
|
static int barheight;
|
|
|
|
|
2014-01-27 23:16:08 +01:00
|
|
|
Atom atoms[ATOM_COUNT];
|
|
|
|
|
2014-02-02 14:56:01 +01:00
|
|
|
static Bool fs_support;
|
|
|
|
static Bool fs_warned;
|
|
|
|
|
2016-08-06 15:27:58 +02:00
|
|
|
void win_init_font(const win_env_t *e, const char *fontstr)
|
2013-02-08 22:05:31 +01:00
|
|
|
{
|
2016-08-06 15:27:58 +02:00
|
|
|
if ((font = XftFontOpenName(e->dpy, e->scr, fontstr)) == NULL)
|
|
|
|
error(EXIT_FAILURE, 0, "Error loading font '%s'", fontstr);
|
|
|
|
fontheight = font->ascent + font->descent;
|
2013-01-07 14:30:42 +01:00
|
|
|
barheight = fontheight + 2 * V_TEXT_PAD;
|
2012-02-12 19:00:41 +01:00
|
|
|
}
|
|
|
|
|
2016-08-06 15:27:58 +02:00
|
|
|
void win_alloc_color(const win_env_t *e, const char *name, XftColor *col)
|
2013-02-08 22:05:31 +01:00
|
|
|
{
|
2016-08-06 15:27:58 +02:00
|
|
|
if (!XftColorAllocName(e->dpy, DefaultVisual(e->dpy, e->scr),
|
|
|
|
DefaultColormap(e->dpy, e->scr), name, col))
|
2012-02-12 19:00:41 +01:00
|
|
|
{
|
2015-10-28 23:03:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
|
2012-02-12 19:00:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-02 14:56:01 +01:00
|
|
|
void win_check_wm_support(Display *dpy, Window root)
|
|
|
|
{
|
|
|
|
int format;
|
|
|
|
long offset = 0, length = 16;
|
|
|
|
Atom *data, type;
|
|
|
|
unsigned long i, nitems, bytes_left;
|
|
|
|
Bool found = False;
|
|
|
|
|
|
|
|
while (!found && length > 0) {
|
|
|
|
if (XGetWindowProperty(dpy, root, atoms[ATOM__NET_SUPPORTED],
|
|
|
|
offset, length, False, XA_ATOM, &type, &format,
|
|
|
|
&nitems, &bytes_left, (unsigned char**) &data))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (type == XA_ATOM && format == 32) {
|
|
|
|
for (i = 0; i < nitems; i++) {
|
|
|
|
if (data[i] == atoms[ATOM__NET_WM_STATE_FULLSCREEN]) {
|
|
|
|
found = True;
|
|
|
|
fs_support = True;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
XFree(data);
|
|
|
|
offset += nitems;
|
|
|
|
length = MIN(length, bytes_left / 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-27 23:16:08 +01:00
|
|
|
#define INIT_ATOM_(atom) \
|
|
|
|
atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_init(win_t *win)
|
|
|
|
{
|
2011-01-18 16:32:40 +01:00
|
|
|
win_env_t *e;
|
2011-01-17 16:42:49 +01:00
|
|
|
|
2013-01-07 14:30:42 +01:00
|
|
|
memset(win, 0, sizeof(win_t));
|
|
|
|
|
2011-01-21 12:57:35 +01:00
|
|
|
e = &win->env;
|
2011-09-29 12:43:36 +02:00
|
|
|
if ((e->dpy = XOpenDisplay(NULL)) == NULL)
|
2015-10-28 23:03:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Error opening X display");
|
2011-01-21 12:57:35 +01:00
|
|
|
|
2011-01-18 16:32:40 +01: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 20:14:15 +01:00
|
|
|
|
2013-04-06 14:04:46 +02:00
|
|
|
if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
|
2015-10-28 23:03:37 +01:00
|
|
|
error(0, 0, "No locale support");
|
2013-04-06 14:04:46 +02:00
|
|
|
|
2016-08-06 15:27:58 +02:00
|
|
|
win_init_font(e, BAR_FONT);
|
2013-02-12 17:55:47 +01:00
|
|
|
|
2016-08-06 15:27:58 +02:00
|
|
|
win_alloc_color(e, WIN_BG_COLOR, &win->bgcol);
|
|
|
|
win_alloc_color(e, WIN_FS_COLOR, &win->fscol);
|
|
|
|
win_alloc_color(e, SEL_COLOR, &win->selcol);
|
|
|
|
win_alloc_color(e, BAR_BG_COLOR, &win->bar.bgcol);
|
|
|
|
win_alloc_color(e, BAR_FG_COLOR, &win->bar.fgcol);
|
2014-10-01 22:35:22 +02:00
|
|
|
|
|
|
|
win->bar.l.size = BAR_L_LEN;
|
|
|
|
win->bar.r.size = BAR_R_LEN;
|
2017-12-07 14:57:02 +01:00
|
|
|
/* 3 padding bytes needed by utf8_decode */
|
|
|
|
win->bar.l.buf = emalloc(win->bar.l.size + 3);
|
|
|
|
win->bar.r.buf = emalloc(win->bar.r.size + 3);
|
2014-10-01 22:35:22 +02:00
|
|
|
win->bar.h = options->hide_bar ? 0 : barheight;
|
2012-02-12 19:00:41 +01:00
|
|
|
|
2014-01-27 23:16:08 +01:00
|
|
|
INIT_ATOM_(WM_DELETE_WINDOW);
|
|
|
|
INIT_ATOM_(_NET_WM_NAME);
|
|
|
|
INIT_ATOM_(_NET_WM_ICON_NAME);
|
|
|
|
INIT_ATOM_(_NET_WM_ICON);
|
|
|
|
INIT_ATOM_(_NET_WM_STATE);
|
|
|
|
INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
|
2014-02-02 14:56:01 +01:00
|
|
|
INIT_ATOM_(_NET_SUPPORTED);
|
|
|
|
|
|
|
|
win_check_wm_support(e->dpy, RootWindow(e->dpy, e->scr));
|
2011-04-11 16:58:38 +02:00
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_open(win_t *win)
|
|
|
|
{
|
2014-01-15 22:40:34 +01:00
|
|
|
int c, i, j, n;
|
2016-10-30 19:10:25 +01:00
|
|
|
long parent;
|
2011-04-11 16:58:38 +02:00
|
|
|
win_env_t *e;
|
|
|
|
XClassHint classhint;
|
2014-01-15 22:40:34 +01:00
|
|
|
unsigned long *icon_data;
|
2011-04-11 16:58:38 +02:00
|
|
|
XColor col;
|
2017-10-05 12:30:31 +02:00
|
|
|
Cursor *cnone = &cursors[CURSOR_NONE].icon;
|
2011-09-11 21:01:24 +02:00
|
|
|
char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
2011-04-11 16:58:38 +02:00
|
|
|
Pixmap none;
|
|
|
|
int gmask;
|
2014-02-05 09:58:36 +01:00
|
|
|
XSizeHints sizehints;
|
2014-07-28 20:36:32 +02:00
|
|
|
Bool fullscreen = options->fullscreen && fs_support;
|
2011-04-11 16:58:38 +02:00
|
|
|
|
|
|
|
e = &win->env;
|
2016-10-30 19:10:25 +01:00
|
|
|
parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
|
2011-04-11 16:58:38 +02:00
|
|
|
|
2014-02-05 09:58:36 +01:00
|
|
|
sizehints.flags = PWinGravity;
|
|
|
|
sizehints.win_gravity = NorthWestGravity;
|
|
|
|
|
2011-01-30 22:00:58 +01:00
|
|
|
/* determine window offsets, width & height */
|
2011-09-29 12:43:36 +02:00
|
|
|
if (options->geometry == NULL)
|
2011-01-30 22:00:58 +01:00
|
|
|
gmask = 0;
|
|
|
|
else
|
|
|
|
gmask = XParseGeometry(options->geometry, &win->x, &win->y,
|
|
|
|
&win->w, &win->h);
|
2013-02-09 01:36:10 +01:00
|
|
|
if ((gmask & WidthValue) != 0)
|
2014-02-05 09:58:36 +01:00
|
|
|
sizehints.flags |= USSize;
|
2013-02-09 01:36:10 +01:00
|
|
|
else
|
2011-01-30 22:00:58 +01:00
|
|
|
win->w = WIN_WIDTH;
|
2013-02-09 01:36:10 +01:00
|
|
|
if ((gmask & HeightValue) != 0)
|
2014-02-05 09:58:36 +01:00
|
|
|
sizehints.flags |= USSize;
|
2013-02-09 01:36:10 +01:00
|
|
|
else
|
2011-01-30 22:00:58 +01:00
|
|
|
win->h = WIN_HEIGHT;
|
2013-02-09 01:36:10 +01:00
|
|
|
if ((gmask & XValue) != 0) {
|
2013-01-30 20:39:28 +01:00
|
|
|
if ((gmask & XNegative) != 0) {
|
|
|
|
win->x += e->scrw - win->w;
|
2014-02-05 09:58:36 +01:00
|
|
|
sizehints.win_gravity = NorthEastGravity;
|
2013-01-30 20:39:28 +01:00
|
|
|
}
|
2014-02-05 09:58:36 +01:00
|
|
|
sizehints.flags |= USPosition;
|
2013-01-30 20:39:28 +01:00
|
|
|
} else {
|
2014-04-21 20:40:01 +02:00
|
|
|
win->x = 0;
|
2013-02-09 01:36:10 +01:00
|
|
|
}
|
|
|
|
if ((gmask & YValue) != 0) {
|
2013-01-30 20:39:28 +01:00
|
|
|
if ((gmask & YNegative) != 0) {
|
|
|
|
win->y += e->scrh - win->h;
|
2014-02-05 09:58:36 +01:00
|
|
|
sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
|
|
|
|
? SouthEastGravity : SouthWestGravity;
|
2013-01-30 20:39:28 +01:00
|
|
|
}
|
2014-02-05 09:58:36 +01:00
|
|
|
sizehints.flags |= USPosition;
|
2013-02-09 01:36:10 +01:00
|
|
|
} else {
|
2014-04-21 20:40:01 +02:00
|
|
|
win->y = 0;
|
2013-01-30 20:39:28 +01:00
|
|
|
}
|
2011-01-17 16:42:49 +01:00
|
|
|
|
2016-10-30 19:10:25 +01:00
|
|
|
win->xwin = XCreateWindow(e->dpy, parent,
|
2011-01-18 16:32:40 +01:00
|
|
|
win->x, win->y, win->w, win->h, 0,
|
2014-07-28 20:36:32 +02:00
|
|
|
e->depth, InputOutput, e->vis, 0, NULL);
|
2011-01-17 16:42:49 +01:00
|
|
|
if (win->xwin == None)
|
2015-10-28 23:03:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Error creating X window");
|
2011-01-17 16:42:49 +01:00
|
|
|
|
2012-10-29 18:25:17 +01:00
|
|
|
XSelectInput(e->dpy, win->xwin,
|
2014-07-28 20:36:32 +02:00
|
|
|
ButtonReleaseMask | ButtonPressMask | KeyPressMask |
|
|
|
|
PointerMotionMask | StructureNotifyMask);
|
2011-01-29 22:37:40 +01:00
|
|
|
|
2017-10-05 12:30:31 +02:00
|
|
|
for (i = 0; i < ARRLEN(cursors); i++) {
|
|
|
|
if (i != CURSOR_NONE)
|
|
|
|
cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
|
|
|
|
}
|
2011-09-29 12:43:36 +02:00
|
|
|
if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
|
2012-03-02 19:42:14 +01:00
|
|
|
&col, &col) == 0)
|
2011-04-11 16:58:38 +02:00
|
|
|
{
|
2015-10-28 23:03:37 +01:00
|
|
|
error(EXIT_FAILURE, 0, "Error allocating color 'black'");
|
2011-04-11 16:58:38 +02:00
|
|
|
}
|
2011-02-21 18:26:21 +01:00
|
|
|
none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
|
2017-10-05 12:30:31 +02:00
|
|
|
*cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
|
2011-02-21 18:26:21 +01:00
|
|
|
|
2011-03-09 11:37:49 +01:00
|
|
|
gc = XCreateGC(e->dpy, win->xwin, 0, None);
|
2011-01-17 16:42:49 +01:00
|
|
|
|
2014-01-15 22:40:34 +01:00
|
|
|
n = icons[ARRLEN(icons)-1].size;
|
2015-10-28 22:29:01 +01:00
|
|
|
icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
|
2014-01-15 22:40:34 +01:00
|
|
|
|
|
|
|
for (i = 0; i < ARRLEN(icons); i++) {
|
|
|
|
n = 0;
|
|
|
|
icon_data[n++] = icons[i].size;
|
|
|
|
icon_data[n++] = icons[i].size;
|
|
|
|
|
|
|
|
for (j = 0; j < icons[i].cnt; j++) {
|
|
|
|
for (c = icons[i].data[j] >> 4; c >= 0; c--)
|
|
|
|
icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F];
|
|
|
|
}
|
|
|
|
XChangeProperty(e->dpy, win->xwin,
|
2014-01-27 23:16:08 +01:00
|
|
|
atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
|
2014-01-15 22:40:34 +01:00
|
|
|
i == 0 ? PropModeReplace : PropModeAppend,
|
|
|
|
(unsigned char *) icon_data, n);
|
|
|
|
}
|
|
|
|
free(icon_data);
|
|
|
|
|
2011-01-20 17:00:59 +01:00
|
|
|
win_set_title(win, "sxiv");
|
2011-01-20 16:32:16 +01:00
|
|
|
|
2012-03-28 21:14:01 +02:00
|
|
|
classhint.res_class = "Sxiv";
|
2012-10-29 00:53:50 +01:00
|
|
|
classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
|
2011-02-01 16:40:37 +01:00
|
|
|
XSetClassHint(e->dpy, win->xwin, &classhint);
|
|
|
|
|
2014-01-27 23:16:08 +01:00
|
|
|
XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
|
2012-08-07 12:09:52 +02:00
|
|
|
|
2014-02-05 09:58:36 +01:00
|
|
|
sizehints.width = win->w;
|
|
|
|
sizehints.height = win->h;
|
|
|
|
sizehints.x = win->x;
|
|
|
|
sizehints.y = win->y;
|
|
|
|
XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
|
|
|
|
|
2013-04-06 14:04:46 +02:00
|
|
|
win->h -= win->bar.h;
|
2011-01-17 16:42:49 +01:00
|
|
|
|
2014-07-28 20:36:32 +02:00
|
|
|
win->buf.w = e->scrw;
|
|
|
|
win->buf.h = e->scrh;
|
|
|
|
win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
|
|
|
|
win->buf.w, win->buf.h, e->depth);
|
2016-08-06 15:27:58 +02:00
|
|
|
XSetForeground(e->dpy, gc, fullscreen ? win->fscol.pixel : win->bgcol.pixel);
|
2014-07-28 20:36:32 +02:00
|
|
|
XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
|
|
|
|
XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm);
|
|
|
|
|
2011-01-18 16:32:40 +01:00
|
|
|
XMapWindow(e->dpy, win->xwin);
|
|
|
|
XFlush(e->dpy);
|
2011-02-09 10:01:49 +01:00
|
|
|
|
2014-07-28 20:36:32 +02:00
|
|
|
if (fullscreen)
|
2011-01-28 13:34:16 +01:00
|
|
|
win_toggle_fullscreen(win);
|
2011-01-17 16:42:49 +01:00
|
|
|
}
|
|
|
|
|
2015-10-28 23:03:37 +01:00
|
|
|
CLEANUP void win_close(win_t *win)
|
2013-02-08 22:05:31 +01:00
|
|
|
{
|
2017-10-05 12:30:31 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRLEN(cursors); i++)
|
|
|
|
XFreeCursor(win->env.dpy, cursors[i].icon);
|
2011-01-29 22:37:40 +01:00
|
|
|
|
2011-02-17 16:22:54 +01:00
|
|
|
XFreeGC(win->env.dpy, gc);
|
2011-01-29 22:37:40 +01:00
|
|
|
|
2011-01-18 16:32:40 +01:00
|
|
|
XDestroyWindow(win->env.dpy, win->xwin);
|
|
|
|
XCloseDisplay(win->env.dpy);
|
2011-01-17 16:42:49 +01:00
|
|
|
}
|
2011-01-17 19:50:46 +01:00
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
bool win_configure(win_t *win, XConfigureEvent *c)
|
|
|
|
{
|
2011-09-11 21:01:24 +02:00
|
|
|
bool changed;
|
2011-01-17 19:50:46 +01:00
|
|
|
|
2014-07-28 20:36:32 +02:00
|
|
|
changed = win->w != c->width || win->h + win->bar.h != c->height;
|
2011-01-22 23:27:29 +01:00
|
|
|
|
|
|
|
win->x = c->x;
|
|
|
|
win->y = c->y;
|
|
|
|
win->w = c->width;
|
2013-01-07 14:30:42 +01:00
|
|
|
win->h = c->height - win->bar.h;
|
2011-01-22 23:27:29 +01:00
|
|
|
win->bw = c->border_width;
|
|
|
|
|
2011-01-17 19:50:46 +01:00
|
|
|
return changed;
|
|
|
|
}
|
2011-01-18 20:11:28 +01:00
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_toggle_fullscreen(win_t *win)
|
|
|
|
{
|
2011-01-23 16:14:41 +01:00
|
|
|
XEvent ev;
|
|
|
|
XClientMessageEvent *cm;
|
|
|
|
|
2014-02-02 14:56:01 +01:00
|
|
|
if (!fs_support) {
|
|
|
|
if (!fs_warned) {
|
2015-10-28 23:03:37 +01:00
|
|
|
error(0, 0, "No fullscreen support");
|
2014-02-02 14:56:01 +01:00
|
|
|
fs_warned = True;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2011-09-11 21:01:24 +02:00
|
|
|
win->fullscreen = !win->fullscreen;
|
2011-01-23 16:14:41 +01:00
|
|
|
|
|
|
|
memset(&ev, 0, sizeof(ev));
|
|
|
|
ev.type = ClientMessage;
|
|
|
|
|
|
|
|
cm = &ev.xclient;
|
|
|
|
cm->window = win->xwin;
|
2014-01-27 23:16:08 +01:00
|
|
|
cm->message_type = atoms[ATOM__NET_WM_STATE];
|
2011-01-23 16:14:41 +01:00
|
|
|
cm->format = 32;
|
|
|
|
cm->data.l[0] = win->fullscreen;
|
2014-01-27 23:16:08 +01:00
|
|
|
cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
|
2011-04-19 14:44:26 +02:00
|
|
|
cm->data.l[2] = cm->data.l[3] = 0;
|
2011-01-23 16:14:41 +01:00
|
|
|
|
|
|
|
XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
|
2011-04-14 20:01:11 +02:00
|
|
|
SubstructureNotifyMask | SubstructureRedirectMask, &ev);
|
2011-01-23 16:14:41 +01:00
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_toggle_bar(win_t *win)
|
|
|
|
{
|
2013-01-07 14:30:42 +01:00
|
|
|
if (win->bar.h != 0) {
|
|
|
|
win->h += win->bar.h;
|
|
|
|
win->bar.h = 0;
|
2012-02-15 22:33:39 +01:00
|
|
|
} else {
|
2013-01-07 14:30:42 +01:00
|
|
|
win->bar.h = barheight;
|
|
|
|
win->h -= win->bar.h;
|
2012-02-15 22:33:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_clear(win_t *win)
|
|
|
|
{
|
2011-01-20 22:02:19 +01:00
|
|
|
win_env_t *e;
|
|
|
|
|
|
|
|
e = &win->env;
|
2011-03-09 11:37:49 +01:00
|
|
|
|
2014-07-28 20:36:32 +02:00
|
|
|
if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) {
|
|
|
|
XFreePixmap(e->dpy, win->buf.pm);
|
|
|
|
win->buf.w = MAX(win->buf.w, win->w);
|
|
|
|
win->buf.h = MAX(win->buf.h, win->h + win->bar.h);
|
|
|
|
win->buf.pm = XCreatePixmap(e->dpy, win->xwin,
|
|
|
|
win->buf.w, win->buf.h, e->depth);
|
|
|
|
}
|
2016-08-06 15:27:58 +02:00
|
|
|
XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol.pixel : win->bgcol.pixel);
|
2014-07-28 20:36:32 +02:00
|
|
|
XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
|
2011-02-17 16:22:54 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 14:19:35 +01:00
|
|
|
int win_textwidth(const win_env_t *e, const char *text, unsigned int len, bool with_padding, XftFont *fnt)
|
|
|
|
{
|
|
|
|
XGlyphInfo ext;
|
|
|
|
|
|
|
|
XftTextExtentsUtf8(e->dpy, fnt, (XftChar8*)text, len, &ext);
|
|
|
|
return ext.xOff + (with_padding ? 2 * H_TEXT_PAD : 0);
|
|
|
|
}
|
|
|
|
|
2017-12-06 20:56:00 +01:00
|
|
|
void win_draw_bar_text(win_t *win, XftDraw *d, XftColor *color, XftFont *font, int x, int y, char *text, int maxlen, int maximum_x)
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
2017-12-07 14:57:02 +01:00
|
|
|
int err, xshift = 0, newshift;
|
|
|
|
uint32_t codep;
|
2017-12-06 20:56:00 +01:00
|
|
|
char *p, *nextp;
|
|
|
|
FcCharSet* fccharset;
|
|
|
|
XftFont* fallback = NULL;
|
|
|
|
|
|
|
|
for (p = text; *p && (len < maxlen); p = nextp, len++) {
|
2017-12-07 14:57:02 +01:00
|
|
|
nextp = utf8_decode(p, &codep, &err);
|
2017-12-06 20:56:00 +01:00
|
|
|
if (!XftCharExists(win->env.dpy, font, codep)) {
|
|
|
|
fccharset = FcCharSetCreate();
|
|
|
|
FcCharSetAddChar(fccharset, codep);
|
|
|
|
fallback = XftFontOpen(win->env.dpy, win->env.scr,
|
|
|
|
FC_CHARSET, FcTypeCharSet, fccharset,
|
|
|
|
FC_SCALABLE, FcTypeBool, FcTrue,
|
|
|
|
NULL);
|
|
|
|
FcCharSetDestroy(fccharset);
|
|
|
|
}
|
|
|
|
newshift = win_textwidth(&win->env, p, (int) (nextp-p), false, (fallback ? fallback : font));
|
|
|
|
if (xshift + newshift <= maximum_x)
|
|
|
|
XftDrawStringUtf8(d, color, (fallback ? fallback : font), x + xshift, y, (XftChar8*)p, (int) (nextp-p));
|
|
|
|
xshift += newshift;
|
|
|
|
if (fallback) {
|
|
|
|
XftFontClose(win->env.dpy, fallback);
|
|
|
|
fallback = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_draw_bar(win_t *win)
|
|
|
|
{
|
2017-12-06 20:56:00 +01:00
|
|
|
int len, olen, x, y, w, tw, maximum_x;
|
2013-01-07 14:30:42 +01:00
|
|
|
char rest[3];
|
|
|
|
const char *dots = "...";
|
2012-02-12 19:00:41 +01:00
|
|
|
win_env_t *e;
|
2014-10-01 22:35:22 +02:00
|
|
|
win_bar_t *l, *r;
|
2016-08-06 15:27:58 +02:00
|
|
|
XftDraw *d;
|
2012-02-12 19:00:41 +01:00
|
|
|
|
2014-10-01 22:35:22 +02:00
|
|
|
if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
|
|
|
|
return;
|
2012-02-12 19:00:41 +01:00
|
|
|
|
|
|
|
e = &win->env;
|
2016-08-06 15:27:58 +02:00
|
|
|
y = win->h + font->ascent + V_TEXT_PAD;
|
2013-01-07 14:30:42 +01:00
|
|
|
w = win->w;
|
2016-08-06 15:27:58 +02:00
|
|
|
d = XftDrawCreate(e->dpy, win->buf.pm, DefaultVisual(e->dpy, e->scr),
|
|
|
|
DefaultColormap(e->dpy, e->scr));
|
2012-02-12 19:00:41 +01:00
|
|
|
|
2016-08-06 15:27:58 +02:00
|
|
|
XSetForeground(e->dpy, gc, win->bar.bgcol.pixel);
|
2014-07-28 20:36:32 +02:00
|
|
|
XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
|
2012-02-12 19:00:41 +01:00
|
|
|
|
2016-08-06 15:27:58 +02:00
|
|
|
XSetForeground(e->dpy, gc, win->bar.fgcol.pixel);
|
|
|
|
XSetBackground(e->dpy, gc, win->bar.bgcol.pixel);
|
2012-02-12 19:00:41 +01:00
|
|
|
|
2014-10-01 22:35:22 +02:00
|
|
|
if ((len = strlen(r->buf)) > 0) {
|
2017-12-06 20:56:00 +01:00
|
|
|
if ((tw = win_textwidth(e, r->buf, len, true, font)) > w)
|
2013-02-11 23:05:26 +01:00
|
|
|
return;
|
|
|
|
x = win->w - tw + H_TEXT_PAD;
|
|
|
|
w -= tw;
|
2016-08-06 15:27:58 +02:00
|
|
|
XftDrawStringUtf8(d, &win->bar.fgcol, font, x, y, (XftChar8*)r->buf, len);
|
2013-01-07 14:30:42 +01:00
|
|
|
}
|
2014-10-01 22:35:22 +02:00
|
|
|
if ((len = strlen(l->buf)) > 0) {
|
2013-02-11 23:05:26 +01:00
|
|
|
olen = len;
|
2017-12-06 20:56:00 +01:00
|
|
|
while (len > 0 && (tw = win_textwidth(e, l->buf, len, true, font)) > w)
|
2013-01-07 14:30:42 +01:00
|
|
|
len--;
|
2012-02-12 19:00:41 +01:00
|
|
|
if (len > 0) {
|
2017-12-06 20:56:00 +01:00
|
|
|
maximum_x = w;
|
2013-02-11 23:05:26 +01:00
|
|
|
if (len != olen) {
|
|
|
|
w = strlen(dots);
|
|
|
|
if (len <= w)
|
|
|
|
return;
|
2014-10-01 22:35:22 +02:00
|
|
|
memcpy(rest, l->buf + len - w, w);
|
|
|
|
memcpy(l->buf + len - w, dots, w);
|
2013-02-11 23:05:26 +01:00
|
|
|
}
|
2013-01-07 14:30:42 +01:00
|
|
|
x = H_TEXT_PAD;
|
2017-12-06 20:56:00 +01:00
|
|
|
win_draw_bar_text(win, d, &win->bar.fgcol, font, x, y, l->buf, len, maximum_x);
|
2013-01-07 14:30:42 +01:00
|
|
|
if (len != olen)
|
2014-10-01 22:35:22 +02:00
|
|
|
memcpy(l->buf + len - w, rest, w);
|
2012-02-12 19:00:41 +01:00
|
|
|
}
|
|
|
|
}
|
2016-08-06 15:27:58 +02:00
|
|
|
XftDrawDestroy(d);
|
2012-02-12 19:00:41 +01:00
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_draw(win_t *win)
|
|
|
|
{
|
2013-01-07 14:30:42 +01:00
|
|
|
if (win->bar.h > 0)
|
2012-03-16 21:03:49 +01:00
|
|
|
win_draw_bar(win);
|
2012-02-12 19:00:41 +01:00
|
|
|
|
2014-07-28 20:36:32 +02:00
|
|
|
XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
|
|
|
|
XClearWindow(win->env.dpy, win->xwin);
|
2014-06-15 14:15:48 +02:00
|
|
|
XFlush(win->env.dpy);
|
2011-02-17 16:22:54 +01:00
|
|
|
}
|
|
|
|
|
2014-08-17 22:58:02 +02:00
|
|
|
void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw,
|
|
|
|
unsigned long col)
|
2012-10-29 18:25:17 +01:00
|
|
|
{
|
2011-02-17 16:22:54 +01:00
|
|
|
XGCValues gcval;
|
|
|
|
|
2011-03-09 11:37:49 +01:00
|
|
|
gcval.line_width = lw;
|
|
|
|
gcval.foreground = col;
|
|
|
|
XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
|
2011-02-17 16:22:54 +01:00
|
|
|
|
2011-03-09 11:37:49 +01:00
|
|
|
if (fill)
|
2014-08-17 22:58:02 +02:00
|
|
|
XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
|
2011-02-17 16:22:54 +01:00
|
|
|
else
|
2014-08-17 22:58:02 +02:00
|
|
|
XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
|
2011-01-20 21:44:34 +01:00
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_set_title(win_t *win, const char *title)
|
|
|
|
{
|
2011-09-29 12:43:36 +02:00
|
|
|
if (title == NULL)
|
2011-02-04 00:25:57 +01:00
|
|
|
title = "sxiv";
|
|
|
|
|
|
|
|
XStoreName(win->env.dpy, win->xwin, title);
|
|
|
|
XSetIconName(win->env.dpy, win->xwin, title);
|
2011-02-09 09:45:35 +01:00
|
|
|
|
2014-01-27 23:16:08 +01:00
|
|
|
XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_NAME],
|
2011-02-09 09:45:35 +01:00
|
|
|
XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
|
|
|
|
PropModeReplace, (unsigned char *) title, strlen(title));
|
2014-01-27 23:16:08 +01:00
|
|
|
XChangeProperty(win->env.dpy, win->xwin, atoms[ATOM__NET_WM_ICON_NAME],
|
2011-02-09 09:45:35 +01:00
|
|
|
XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
|
|
|
|
PropModeReplace, (unsigned char *) title, strlen(title));
|
2011-02-04 00:25:57 +01:00
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_set_cursor(win_t *win, cursor_t cursor)
|
|
|
|
{
|
2017-10-05 12:30:31 +02:00
|
|
|
if (cursor >= 0 && cursor < ARRLEN(cursors)) {
|
|
|
|
XDefineCursor(win->env.dpy, win->xwin, cursors[cursor].icon);
|
|
|
|
XFlush(win->env.dpy);
|
2011-01-29 22:37:40 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-05 13:47:02 +02:00
|
|
|
|
|
|
|
void win_cursor_pos(win_t *win, int *x, int *y)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned int ui;
|
|
|
|
Window w;
|
|
|
|
|
|
|
|
if (!XQueryPointer(win->env.dpy, win->xwin, &w, &w, &i, &i, x, y, &ui))
|
|
|
|
*x = *y = 0;
|
|
|
|
}
|
|
|
|
|