2021-10-28 10:41:16 +00:00
|
|
|
/* Copyright 2011-2020 Bert Muennich
|
2022-02-09 07:09:42 +00:00
|
|
|
* Copyright 2021-2022 nsxiv contributors
|
2011-01-17 13:57:59 +00:00
|
|
|
*
|
2021-09-16 09:32:59 +00:00
|
|
|
* This file is a part of nsxiv.
|
2011-08-17 23:18:26 +00:00
|
|
|
*
|
2021-09-16 09:32:59 +00:00
|
|
|
* nsxiv is free software; you can redistribute it and/or modify
|
2013-02-08 20:52:41 +00:00
|
|
|
* 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-17 23:18:26 +00:00
|
|
|
*
|
2021-09-16 09:32:59 +00:00
|
|
|
* nsxiv is distributed in the hope that it will be useful,
|
2013-02-08 20:52:41 +00:00
|
|
|
* 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
|
2021-09-16 09:32:59 +00:00
|
|
|
* along with nsxiv. If not, see <http://www.gnu.org/licenses/>.
|
2011-01-17 13:57:59 +00:00
|
|
|
*/
|
|
|
|
|
2021-09-16 09:32:59 +00:00
|
|
|
#include "nsxiv.h"
|
2017-10-16 19:10:35 +00:00
|
|
|
#define _WINDOW_CONFIG
|
|
|
|
#include "config.h"
|
|
|
|
#include "icon/data.h"
|
|
|
|
|
2014-01-15 21:40:34 +00:00
|
|
|
#include <stdlib.h>
|
2011-01-23 15:14:41 +00:00
|
|
|
#include <string.h>
|
2012-02-12 18:00:41 +00:00
|
|
|
#include <locale.h>
|
2021-09-11 14:14:34 +00:00
|
|
|
#include <unistd.h>
|
2011-01-29 21:37:40 +00:00
|
|
|
#include <X11/cursorfont.h>
|
2014-01-15 21:40:34 +00:00
|
|
|
#include <X11/Xatom.h>
|
2018-10-23 17:26:26 +00:00
|
|
|
#include <X11/Xresource.h>
|
2011-01-17 15:42:49 +00:00
|
|
|
|
2021-09-19 08:49:56 +00:00
|
|
|
#if HAVE_LIBFONTS
|
|
|
|
#include "utf8.h"
|
|
|
|
static XftFont *font;
|
|
|
|
static double fontsize;
|
2021-10-11 03:07:18 +00:00
|
|
|
#define TEXTWIDTH(win, text, len) \
|
|
|
|
win_draw_text(win, NULL, NULL, 0, 0, text, len, 0)
|
2021-09-19 08:49:56 +00:00
|
|
|
#endif
|
|
|
|
|
2021-09-16 09:32:59 +00:00
|
|
|
#define RES_CLASS "Nsxiv"
|
2019-01-23 19:04:17 +00:00
|
|
|
|
2021-10-11 03:07:18 +00:00
|
|
|
#define INIT_ATOM_(atom) \
|
|
|
|
atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False);
|
|
|
|
|
2012-02-12 18:00:41 +00:00
|
|
|
enum {
|
|
|
|
H_TEXT_PAD = 5,
|
|
|
|
V_TEXT_PAD = 1
|
|
|
|
};
|
|
|
|
|
2017-10-05 10:30:31 +00:00
|
|
|
static struct {
|
|
|
|
int name;
|
|
|
|
Cursor icon;
|
|
|
|
} cursors[CURSOR_COUNT] = {
|
2021-10-27 03:00:46 +00:00
|
|
|
{ XC_left_ptr }, { XC_dotbox }, { XC_fleur }, { XC_watch },
|
2017-10-05 11:53:29 +00:00
|
|
|
{ XC_sb_left_arrow }, { XC_sb_right_arrow }
|
2017-10-05 10:30:31 +00:00
|
|
|
};
|
|
|
|
|
2011-02-17 15:22:54 +00:00
|
|
|
static GC gc;
|
2011-01-23 15:14:41 +00:00
|
|
|
|
2013-01-07 13:30:42 +00:00
|
|
|
static int barheight;
|
|
|
|
|
2014-01-27 22:16:08 +00:00
|
|
|
Atom atoms[ATOM_COUNT];
|
|
|
|
|
2021-09-19 08:49:56 +00:00
|
|
|
#if HAVE_LIBFONTS
|
2021-11-20 03:51:49 +00:00
|
|
|
static void win_init_font(const win_env_t *e, const char *fontstr)
|
2013-02-08 21:05:31 +00:00
|
|
|
{
|
2021-09-19 08:49:56 +00:00
|
|
|
int fontheight = 0;
|
2016-08-06 13:27:58 +00:00
|
|
|
if ((font = XftFontOpenName(e->dpy, e->scr, fontstr)) == NULL)
|
|
|
|
error(EXIT_FAILURE, 0, "Error loading font '%s'", fontstr);
|
|
|
|
fontheight = font->ascent + font->descent;
|
2019-02-23 03:32:36 +00:00
|
|
|
FcPatternGetDouble(font->pattern, FC_SIZE, 0, &fontsize);
|
2013-01-07 13:30:42 +00:00
|
|
|
barheight = fontheight + 2 * V_TEXT_PAD;
|
2012-02-12 18:00:41 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 03:51:49 +00:00
|
|
|
static void xft_alloc_color(const win_env_t *e, const char *name, XftColor *col)
|
2013-02-08 21:05:31 +00:00
|
|
|
{
|
2021-09-19 08:49:56 +00:00
|
|
|
if (!XftColorAllocName(e->dpy, e->vis, e->cmap, name, col))
|
2015-10-28 22:03:37 +00:00
|
|
|
error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
|
2021-09-19 08:49:56 +00:00
|
|
|
}
|
|
|
|
#endif /* HAVE_LIBFONTS */
|
|
|
|
|
2021-11-20 03:51:49 +00:00
|
|
|
static void win_alloc_color(const win_env_t *e, const char *name, XColor *col)
|
2021-09-19 08:49:56 +00:00
|
|
|
{
|
2021-10-11 23:46:35 +00:00
|
|
|
XColor screen;
|
|
|
|
if (!XAllocNamedColor(e->dpy, e->cmap, name, &screen, col))
|
2021-09-19 08:49:56 +00:00
|
|
|
error(EXIT_FAILURE, 0, "Error allocating color '%s'", name);
|
2012-02-12 18:00:41 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 03:51:49 +00:00
|
|
|
static const char* win_res(XrmDatabase db, const char *name, const char *def)
|
2018-10-23 17:26:26 +00:00
|
|
|
{
|
|
|
|
char *type;
|
|
|
|
XrmValue ret;
|
|
|
|
|
2021-10-28 01:57:03 +00:00
|
|
|
if (db != NULL &&
|
2020-01-16 10:13:41 +00:00
|
|
|
XrmGetResource(db, name, name, &type, &ret) &&
|
|
|
|
STREQ(type, "String"))
|
2019-01-23 19:13:25 +00:00
|
|
|
{
|
|
|
|
return ret.addr;
|
|
|
|
} else {
|
|
|
|
return def;
|
|
|
|
}
|
2018-10-23 17:26:26 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void win_init(win_t *win)
|
|
|
|
{
|
2011-01-18 15:32:40 +00:00
|
|
|
win_env_t *e;
|
2021-09-19 08:49:56 +00:00
|
|
|
const char *win_bg, *win_fg, *mrk_fg;
|
2022-06-01 01:40:49 +00:00
|
|
|
char *res_man;
|
|
|
|
XrmDatabase db;
|
2021-09-19 08:49:56 +00:00
|
|
|
#if HAVE_LIBFONTS
|
|
|
|
const char *bar_fg, *bar_bg, *f;
|
2022-06-01 01:40:49 +00:00
|
|
|
|
|
|
|
static char lbuf[512 + 3], rbuf[64 + 3];
|
2021-09-19 08:49:56 +00:00
|
|
|
#endif
|
2011-01-17 15:42:49 +00:00
|
|
|
|
2013-01-07 13:30:42 +00:00
|
|
|
memset(win, 0, sizeof(win_t));
|
|
|
|
|
2011-01-21 11:57:35 +00:00
|
|
|
e = &win->env;
|
2011-09-29 10:43:36 +00:00
|
|
|
if ((e->dpy = XOpenDisplay(NULL)) == NULL)
|
2015-10-28 22:03:37 +00:00
|
|
|
error(EXIT_FAILURE, 0, "Error opening X display");
|
2011-01-21 11:57:35 +00:00
|
|
|
|
2011-01-18 15:32:40 +00:00
|
|
|
e->scr = DefaultScreen(e->dpy);
|
|
|
|
e->scrw = DisplayWidth(e->dpy, e->scr);
|
|
|
|
e->scrh = DisplayHeight(e->dpy, e->scr);
|
2021-10-11 23:46:35 +00:00
|
|
|
e->depth = DefaultDepth(e->dpy, e->scr);
|
|
|
|
e->vis = DefaultVisual(e->dpy, e->scr);
|
|
|
|
e->cmap = DefaultColormap(e->dpy, e->scr);
|
2011-01-17 19:14:15 +00:00
|
|
|
|
2013-04-06 12:04:46 +00:00
|
|
|
if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
|
2015-10-28 22:03:37 +00:00
|
|
|
error(0, 0, "No locale support");
|
2013-04-06 12:04:46 +00:00
|
|
|
|
2020-01-16 10:13:41 +00:00
|
|
|
XrmInitialize();
|
|
|
|
res_man = XResourceManagerString(e->dpy);
|
2021-10-28 01:57:03 +00:00
|
|
|
db = res_man == NULL ? NULL : XrmGetStringDatabase(res_man);
|
2020-01-16 10:13:41 +00:00
|
|
|
|
2021-10-09 19:04:08 +00:00
|
|
|
win_bg = win_res(db, RES_CLASS ".window.background", DEFAULT_WIN_BG);
|
|
|
|
win_fg = win_res(db, RES_CLASS ".window.foreground", DEFAULT_WIN_FG);
|
|
|
|
mrk_fg = win_res(db, RES_CLASS ".mark.foreground", DEFAULT_MARK_COLOR ? DEFAULT_MARK_COLOR : win_fg);
|
2021-09-11 14:38:22 +00:00
|
|
|
win_alloc_color(e, win_bg, &win->win_bg);
|
|
|
|
win_alloc_color(e, win_fg, &win->win_fg);
|
2021-09-16 20:31:03 +00:00
|
|
|
win_alloc_color(e, mrk_fg, &win->mrk_fg);
|
2014-10-01 20:35:22 +00:00
|
|
|
|
2021-09-19 08:49:56 +00:00
|
|
|
#if HAVE_LIBFONTS
|
2021-10-09 19:04:08 +00:00
|
|
|
bar_bg = win_res(db, RES_CLASS ".bar.background", DEFAULT_BAR_BG ? DEFAULT_BAR_BG : win_bg);
|
|
|
|
bar_fg = win_res(db, RES_CLASS ".bar.foreground", DEFAULT_BAR_FG ? DEFAULT_BAR_FG : win_fg);
|
2021-09-19 08:49:56 +00:00
|
|
|
xft_alloc_color(e, bar_bg, &win->bar_bg);
|
|
|
|
xft_alloc_color(e, bar_fg, &win->bar_fg);
|
|
|
|
|
2021-10-09 19:04:08 +00:00
|
|
|
f = win_res(db, RES_CLASS ".bar.font", DEFAULT_FONT);
|
2021-09-19 08:49:56 +00:00
|
|
|
win_init_font(e, f);
|
|
|
|
|
2022-06-01 01:40:49 +00:00
|
|
|
win->bar.l.buf = lbuf;
|
|
|
|
win->bar.r.buf = rbuf;
|
2017-12-07 13:57:02 +00:00
|
|
|
/* 3 padding bytes needed by utf8_decode */
|
2022-06-01 01:40:49 +00:00
|
|
|
win->bar.l.size = sizeof(lbuf) - 3;
|
|
|
|
win->bar.r.size = sizeof(rbuf) - 3;
|
2014-10-01 20:35:22 +00:00
|
|
|
win->bar.h = options->hide_bar ? 0 : barheight;
|
2022-02-26 16:38:53 +00:00
|
|
|
win->bar.top = TOP_STATUSBAR;
|
2021-09-19 08:49:56 +00:00
|
|
|
#endif /* HAVE_LIBFONTS */
|
2012-02-12 18:00:41 +00:00
|
|
|
|
2021-10-28 01:57:03 +00:00
|
|
|
XrmDestroyDatabase(db);
|
2014-01-27 22:16:08 +00: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);
|
2021-09-11 14:14:34 +00:00
|
|
|
INIT_ATOM_(_NET_WM_PID);
|
2014-01-27 22:16:08 +00:00
|
|
|
INIT_ATOM_(_NET_WM_STATE_FULLSCREEN);
|
2022-02-27 09:37:15 +00:00
|
|
|
INIT_ATOM_(UTF8_STRING);
|
2022-02-27 18:38:27 +00:00
|
|
|
INIT_ATOM_(WM_NAME);
|
|
|
|
INIT_ATOM_(WM_ICON_NAME);
|
2011-04-11 14:58:38 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void win_open(win_t *win)
|
|
|
|
{
|
2014-01-15 21:40:34 +00:00
|
|
|
int c, i, j, n;
|
2021-10-28 20:00:53 +00:00
|
|
|
Window parent;
|
2011-04-11 14:58:38 +00:00
|
|
|
win_env_t *e;
|
|
|
|
XClassHint classhint;
|
2014-01-15 21:40:34 +00:00
|
|
|
unsigned long *icon_data;
|
2011-04-11 14:58:38 +00:00
|
|
|
XColor col;
|
2017-10-05 10:30:31 +00:00
|
|
|
Cursor *cnone = &cursors[CURSOR_NONE].icon;
|
2011-09-11 19:01:24 +00:00
|
|
|
char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
2011-04-11 14:58:38 +00:00
|
|
|
Pixmap none;
|
|
|
|
int gmask;
|
2014-02-05 08:58:36 +00:00
|
|
|
XSizeHints sizehints;
|
2020-06-23 05:52:54 +00:00
|
|
|
XWMHints hints;
|
2022-03-31 04:34:04 +00:00
|
|
|
long pid;
|
2021-10-11 03:07:18 +00:00
|
|
|
char hostname[256];
|
2021-10-11 23:46:35 +00:00
|
|
|
XSetWindowAttributes attrs;
|
2021-12-27 08:32:44 +00:00
|
|
|
char res_class[] = RES_CLASS;
|
|
|
|
char res_name[] = "nsxiv";
|
2011-04-11 14:58:38 +00:00
|
|
|
|
|
|
|
e = &win->env;
|
2021-10-28 20:00:53 +00:00
|
|
|
parent = options->embed ? options->embed : RootWindow(e->dpy, e->scr);
|
2011-04-11 14:58:38 +00:00
|
|
|
|
2014-02-05 08:58:36 +00:00
|
|
|
sizehints.flags = PWinGravity;
|
|
|
|
sizehints.win_gravity = NorthWestGravity;
|
|
|
|
|
2011-01-30 21:00:58 +00:00
|
|
|
/* determine window offsets, width & height */
|
2011-09-29 10:43:36 +00:00
|
|
|
if (options->geometry == NULL)
|
2011-01-30 21:00:58 +00:00
|
|
|
gmask = 0;
|
|
|
|
else
|
|
|
|
gmask = XParseGeometry(options->geometry, &win->x, &win->y,
|
|
|
|
&win->w, &win->h);
|
2021-10-28 20:00:53 +00:00
|
|
|
if (gmask & WidthValue)
|
2014-02-05 08:58:36 +00:00
|
|
|
sizehints.flags |= USSize;
|
2013-02-09 00:36:10 +00:00
|
|
|
else
|
2011-01-30 21:00:58 +00:00
|
|
|
win->w = WIN_WIDTH;
|
2021-10-28 20:00:53 +00:00
|
|
|
if (gmask & HeightValue)
|
2014-02-05 08:58:36 +00:00
|
|
|
sizehints.flags |= USSize;
|
2013-02-09 00:36:10 +00:00
|
|
|
else
|
2011-01-30 21:00:58 +00:00
|
|
|
win->h = WIN_HEIGHT;
|
2021-10-28 20:00:53 +00:00
|
|
|
if (gmask & XValue) {
|
|
|
|
if (gmask & XNegative) {
|
2013-01-30 19:39:28 +00:00
|
|
|
win->x += e->scrw - win->w;
|
2014-02-05 08:58:36 +00:00
|
|
|
sizehints.win_gravity = NorthEastGravity;
|
2013-01-30 19:39:28 +00:00
|
|
|
}
|
2014-02-05 08:58:36 +00:00
|
|
|
sizehints.flags |= USPosition;
|
2013-01-30 19:39:28 +00:00
|
|
|
} else {
|
2014-04-21 18:40:01 +00:00
|
|
|
win->x = 0;
|
2013-02-09 00:36:10 +00:00
|
|
|
}
|
2021-10-28 20:00:53 +00:00
|
|
|
if (gmask & YValue) {
|
|
|
|
if (gmask & YNegative) {
|
2013-01-30 19:39:28 +00:00
|
|
|
win->y += e->scrh - win->h;
|
2014-02-05 08:58:36 +00:00
|
|
|
sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
|
2021-10-11 03:07:18 +00:00
|
|
|
? SouthEastGravity : SouthWestGravity;
|
2013-01-30 19:39:28 +00:00
|
|
|
}
|
2014-02-05 08:58:36 +00:00
|
|
|
sizehints.flags |= USPosition;
|
2013-02-09 00:36:10 +00:00
|
|
|
} else {
|
2014-04-21 18:40:01 +00:00
|
|
|
win->y = 0;
|
2013-01-30 19:39:28 +00:00
|
|
|
}
|
2011-01-17 15:42:49 +00:00
|
|
|
|
2021-10-11 23:46:35 +00:00
|
|
|
attrs.colormap = e->cmap;
|
|
|
|
attrs.border_pixel = 0;
|
|
|
|
|
2021-10-11 03:07:18 +00:00
|
|
|
win->xwin = XCreateWindow(e->dpy, parent, win->x, win->y, win->w, win->h, 0,
|
2021-10-11 23:46:35 +00:00
|
|
|
e->depth, InputOutput, e->vis,
|
|
|
|
CWColormap | CWBorderPixel, &attrs);
|
2011-01-17 15:42:49 +00:00
|
|
|
if (win->xwin == None)
|
2015-10-28 22:03:37 +00:00
|
|
|
error(EXIT_FAILURE, 0, "Error creating X window");
|
2018-10-23 17:26:26 +00:00
|
|
|
|
2021-09-11 14:14:34 +00:00
|
|
|
/* set the _NET_WM_PID */
|
|
|
|
pid = getpid();
|
2021-10-11 03:07:18 +00:00
|
|
|
XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_PID], XA_CARDINAL,
|
2022-03-31 04:34:04 +00:00
|
|
|
32, PropModeReplace, (unsigned char *) &pid, 1);
|
2021-10-11 03:07:18 +00:00
|
|
|
if (gethostname(hostname, ARRLEN(hostname)) == 0) {
|
2021-09-11 14:14:34 +00:00
|
|
|
XTextProperty tp;
|
|
|
|
tp.value = (unsigned char *)hostname;
|
2021-10-11 03:07:18 +00:00
|
|
|
tp.nitems = strnlen(hostname, ARRLEN(hostname));
|
2021-09-11 14:14:34 +00:00
|
|
|
tp.encoding = XA_STRING;
|
|
|
|
tp.format = 8;
|
|
|
|
XSetWMClientMachine(e->dpy, win->xwin, &tp);
|
|
|
|
}
|
|
|
|
|
2012-10-29 17:25:17 +00:00
|
|
|
XSelectInput(e->dpy, win->xwin,
|
2014-07-28 18:36:32 +00:00
|
|
|
ButtonReleaseMask | ButtonPressMask | KeyPressMask |
|
|
|
|
PointerMotionMask | StructureNotifyMask);
|
2011-01-29 21:37:40 +00:00
|
|
|
|
2017-10-05 10:30:31 +00:00
|
|
|
for (i = 0; i < ARRLEN(cursors); i++) {
|
|
|
|
if (i != CURSOR_NONE)
|
|
|
|
cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
|
|
|
|
}
|
2021-10-11 03:07:18 +00:00
|
|
|
if (XAllocNamedColor(e->dpy, e->cmap, "black", &col, &col) == 0)
|
2015-10-28 22:03:37 +00:00
|
|
|
error(EXIT_FAILURE, 0, "Error allocating color 'black'");
|
2021-10-11 03:07:18 +00:00
|
|
|
|
2011-02-21 17:26:21 +00:00
|
|
|
none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
|
2017-10-05 10:30:31 +00:00
|
|
|
*cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
|
2011-02-21 17:26:21 +00:00
|
|
|
|
2011-03-09 10:37:49 +00:00
|
|
|
gc = XCreateGC(e->dpy, win->xwin, 0, None);
|
2011-01-17 15:42:49 +00:00
|
|
|
|
2014-01-15 21:40:34 +00:00
|
|
|
n = icons[ARRLEN(icons)-1].size;
|
2015-10-28 21:29:01 +00:00
|
|
|
icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
|
2014-01-15 21:40:34 +00: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];
|
|
|
|
}
|
2021-10-11 03:07:18 +00:00
|
|
|
XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32,
|
2014-01-15 21:40:34 +00:00
|
|
|
i == 0 ? PropModeReplace : PropModeAppend,
|
|
|
|
(unsigned char *) icon_data, n);
|
|
|
|
}
|
|
|
|
free(icon_data);
|
|
|
|
|
2022-06-01 10:47:17 +00:00
|
|
|
win_set_title(win, res_name, strlen(res_name));
|
2021-12-27 08:32:44 +00:00
|
|
|
classhint.res_class = res_class;
|
|
|
|
classhint.res_name = options->res_name != NULL ? options->res_name : res_name;
|
2011-02-01 15:40:37 +00:00
|
|
|
XSetClassHint(e->dpy, win->xwin, &classhint);
|
|
|
|
|
2014-01-27 22:16:08 +00:00
|
|
|
XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1);
|
2012-08-07 10:09:52 +00:00
|
|
|
|
2014-02-05 08:58:36 +00:00
|
|
|
sizehints.width = win->w;
|
|
|
|
sizehints.height = win->h;
|
|
|
|
sizehints.x = win->x;
|
|
|
|
sizehints.y = win->y;
|
|
|
|
XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
|
|
|
|
|
2020-06-23 05:52:54 +00:00
|
|
|
hints.flags = InputHint | StateHint;
|
|
|
|
hints.input = 1;
|
|
|
|
hints.initial_state = NormalState;
|
|
|
|
XSetWMHints(win->env.dpy, win->xwin, &hints);
|
|
|
|
|
2022-03-31 04:41:32 +00:00
|
|
|
if (options->fullscreen) {
|
|
|
|
XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_STATE],
|
|
|
|
XA_ATOM, 32, PropModeReplace,
|
|
|
|
(unsigned char *) &atoms[ATOM__NET_WM_STATE_FULLSCREEN], 1);
|
|
|
|
}
|
|
|
|
|
2013-04-06 12:04:46 +00:00
|
|
|
win->h -= win->bar.h;
|
2011-01-17 15:42:49 +00:00
|
|
|
|
2014-07-28 18:36:32 +00:00
|
|
|
win->buf.w = e->scrw;
|
|
|
|
win->buf.h = e->scrh;
|
2021-10-11 03:07:18 +00:00
|
|
|
win->buf.pm = XCreatePixmap(e->dpy, win->xwin, win->buf.w, win->buf.h, e->depth);
|
|
|
|
|
2021-10-11 23:46:35 +00:00
|
|
|
XSetForeground(e->dpy, gc, win->win_bg.pixel);
|
2014-07-28 18:36:32 +00: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 15:32:40 +00:00
|
|
|
XMapWindow(e->dpy, win->xwin);
|
|
|
|
XFlush(e->dpy);
|
2011-01-17 15:42:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-28 22:03:37 +00:00
|
|
|
CLEANUP void win_close(win_t *win)
|
2013-02-08 21:05:31 +00:00
|
|
|
{
|
2021-10-28 20:00:53 +00:00
|
|
|
unsigned int i;
|
2017-10-05 10:30:31 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ARRLEN(cursors); i++)
|
|
|
|
XFreeCursor(win->env.dpy, cursors[i].icon);
|
2011-01-29 21:37:40 +00:00
|
|
|
|
2011-02-17 15:22:54 +00:00
|
|
|
XFreeGC(win->env.dpy, gc);
|
2022-04-10 12:36:09 +00:00
|
|
|
#if HAVE_LIBFONTS
|
|
|
|
XftFontClose(win->env.dpy, font);
|
|
|
|
#endif
|
2011-01-18 15:32: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
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
bool win_configure(win_t *win, XConfigureEvent *c)
|
|
|
|
{
|
2011-09-11 19:01:24 +00:00
|
|
|
bool changed;
|
2011-01-17 18:50:46 +00:00
|
|
|
|
2014-07-28 18:36:32 +00:00
|
|
|
changed = win->w != c->width || win->h + win->bar.h != c->height;
|
2011-01-22 22:27:29 +00:00
|
|
|
|
|
|
|
win->x = c->x;
|
|
|
|
win->y = c->y;
|
|
|
|
win->w = c->width;
|
2013-01-07 13:30:42 +00:00
|
|
|
win->h = c->height - win->bar.h;
|
2011-01-22 22:27:29 +00:00
|
|
|
win->bw = c->border_width;
|
|
|
|
|
2011-01-17 18:50:46 +00:00
|
|
|
return changed;
|
|
|
|
}
|
2011-01-18 19:11:28 +00:00
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void win_toggle_fullscreen(win_t *win)
|
|
|
|
{
|
2011-01-23 15:14:41 +00:00
|
|
|
XEvent ev;
|
|
|
|
XClientMessageEvent *cm;
|
|
|
|
|
|
|
|
memset(&ev, 0, sizeof(ev));
|
|
|
|
ev.type = ClientMessage;
|
|
|
|
|
|
|
|
cm = &ev.xclient;
|
|
|
|
cm->window = win->xwin;
|
2014-01-27 22:16:08 +00:00
|
|
|
cm->message_type = atoms[ATOM__NET_WM_STATE];
|
2011-01-23 15:14:41 +00:00
|
|
|
cm->format = 32;
|
2021-10-28 20:00:53 +00:00
|
|
|
cm->data.l[0] = 2; /* toggle */
|
2014-01-27 22:16:08 +00:00
|
|
|
cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
|
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
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void win_toggle_bar(win_t *win)
|
|
|
|
{
|
2013-01-07 13:30:42 +00:00
|
|
|
if (win->bar.h != 0) {
|
|
|
|
win->h += win->bar.h;
|
|
|
|
win->bar.h = 0;
|
2012-02-15 21:33:39 +00:00
|
|
|
} else {
|
2013-01-07 13:30:42 +00:00
|
|
|
win->bar.h = barheight;
|
|
|
|
win->h -= win->bar.h;
|
2012-02-15 21:33:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void win_clear(win_t *win)
|
|
|
|
{
|
2011-01-20 21:02:19 +00:00
|
|
|
win_env_t *e;
|
|
|
|
|
|
|
|
e = &win->env;
|
2011-03-09 10:37:49 +00:00
|
|
|
|
2014-07-28 18:36:32 +00: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);
|
|
|
|
}
|
2021-10-11 23:46:35 +00:00
|
|
|
XSetForeground(e->dpy, gc, win->win_bg.pixel);
|
2014-07-28 18:36:32 +00:00
|
|
|
XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h);
|
2011-02-17 15:22:54 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 08:49:56 +00:00
|
|
|
#if HAVE_LIBFONTS
|
2021-11-20 03:51:49 +00:00
|
|
|
static int win_draw_text(win_t *win, XftDraw *d, const XftColor *color,
|
|
|
|
int x, int y, char *text, int len, int w)
|
2017-12-07 13:19:35 +00:00
|
|
|
{
|
2017-12-07 20:29:41 +00:00
|
|
|
int err, tw = 0;
|
|
|
|
char *t, *next;
|
|
|
|
uint32_t rune;
|
|
|
|
XftFont *f;
|
|
|
|
FcCharSet *fccharset;
|
2017-12-07 13:19:35 +00:00
|
|
|
XGlyphInfo ext;
|
|
|
|
|
2017-12-07 20:29:41 +00:00
|
|
|
for (t = text; t - text < len; t = next) {
|
|
|
|
next = utf8_decode(t, &rune, &err);
|
|
|
|
if (XftCharExists(win->env.dpy, font, rune)) {
|
|
|
|
f = font;
|
|
|
|
} else { /* fallback font */
|
2017-12-06 19:56:00 +00:00
|
|
|
fccharset = FcCharSetCreate();
|
2017-12-07 20:29:41 +00:00
|
|
|
FcCharSetAddChar(fccharset, rune);
|
|
|
|
f = XftFontOpen(win->env.dpy, win->env.scr, FC_CHARSET, FcTypeCharSet,
|
2019-02-23 03:32:36 +00:00
|
|
|
fccharset, FC_SCALABLE, FcTypeBool, FcTrue,
|
|
|
|
FC_SIZE, FcTypeDouble, fontsize, NULL);
|
2017-12-06 19:56:00 +00:00
|
|
|
FcCharSetDestroy(fccharset);
|
|
|
|
}
|
2017-12-07 20:29:41 +00:00
|
|
|
XftTextExtentsUtf8(win->env.dpy, f, (XftChar8*)t, next - t, &ext);
|
|
|
|
tw += ext.xOff;
|
|
|
|
if (tw <= w) {
|
|
|
|
XftDrawStringUtf8(d, color, f, x, y, (XftChar8*)t, next - t);
|
|
|
|
x += ext.xOff;
|
2017-12-06 19:56:00 +00:00
|
|
|
}
|
2017-12-07 20:29:41 +00:00
|
|
|
if (f != font)
|
|
|
|
XftFontClose(win->env.dpy, f);
|
2017-12-06 19:56:00 +00:00
|
|
|
}
|
2017-12-07 20:29:41 +00:00
|
|
|
return tw;
|
2017-12-06 19:56:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 03:51:49 +00:00
|
|
|
static void win_draw_bar(win_t *win)
|
2013-02-08 21:05:31 +00:00
|
|
|
{
|
2017-12-07 20:29:41 +00:00
|
|
|
int len, x, y, w, tw;
|
2012-02-12 18:00:41 +00:00
|
|
|
win_env_t *e;
|
2014-10-01 20:35:22 +00:00
|
|
|
win_bar_t *l, *r;
|
2016-08-06 13:27:58 +00:00
|
|
|
XftDraw *d;
|
2012-02-12 18:00:41 +00:00
|
|
|
|
2014-10-01 20:35:22 +00:00
|
|
|
if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL)
|
|
|
|
return;
|
2012-02-12 18:00:41 +00:00
|
|
|
|
|
|
|
e = &win->env;
|
2022-02-26 16:38:53 +00:00
|
|
|
y = (win->bar.top ? 0 : win->h) + font->ascent + V_TEXT_PAD;
|
2017-12-07 20:29:41 +00:00
|
|
|
w = win->w - 2*H_TEXT_PAD;
|
2021-10-11 03:07:18 +00:00
|
|
|
d = XftDrawCreate(e->dpy, win->buf.pm, e->vis, e->cmap);
|
2012-02-12 18:00:41 +00:00
|
|
|
|
2021-09-11 14:38:22 +00:00
|
|
|
XSetForeground(e->dpy, gc, win->bar_bg.pixel);
|
2022-02-26 16:38:53 +00:00
|
|
|
XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->bar.top ? 0 : win->h, win->w, win->bar.h);
|
2012-02-12 18:00:41 +00:00
|
|
|
|
2021-10-11 23:46:35 +00:00
|
|
|
XSetForeground(e->dpy, gc, win->win_bg.pixel);
|
2021-09-11 14:38:22 +00:00
|
|
|
XSetBackground(e->dpy, gc, win->bar_bg.pixel);
|
2012-02-12 18:00:41 +00:00
|
|
|
|
2014-10-01 20:35:22 +00:00
|
|
|
if ((len = strlen(r->buf)) > 0) {
|
2017-12-07 20:29:41 +00:00
|
|
|
if ((tw = TEXTWIDTH(win, r->buf, len)) > w)
|
2013-02-11 22:05:26 +00:00
|
|
|
return;
|
2017-12-07 20:29:41 +00:00
|
|
|
x = win->w - tw - H_TEXT_PAD;
|
2013-02-11 22:05:26 +00:00
|
|
|
w -= tw;
|
2021-09-11 14:38:22 +00:00
|
|
|
win_draw_text(win, d, &win->bar_fg, x, y, r->buf, len, tw);
|
2013-01-07 13:30:42 +00:00
|
|
|
}
|
2014-10-01 20:35:22 +00:00
|
|
|
if ((len = strlen(l->buf)) > 0) {
|
2017-12-07 20:29:41 +00:00
|
|
|
x = H_TEXT_PAD;
|
|
|
|
w -= 2 * H_TEXT_PAD; /* gap between left and right parts */
|
2021-09-11 14:38:22 +00:00
|
|
|
win_draw_text(win, d, &win->bar_fg, x, y, l->buf, len, w);
|
2012-02-12 18:00:41 +00:00
|
|
|
}
|
2016-08-06 13:27:58 +00:00
|
|
|
XftDrawDestroy(d);
|
2012-02-12 18:00:41 +00:00
|
|
|
}
|
2021-09-19 08:49:56 +00:00
|
|
|
#else
|
2021-11-30 23:41:22 +00:00
|
|
|
static void win_draw_bar(win_t *win)
|
|
|
|
{
|
|
|
|
(void) win;
|
|
|
|
}
|
2021-09-19 08:49:56 +00:00
|
|
|
#endif /* HAVE_LIBFONTS */
|
2012-02-12 18:00:41 +00:00
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void win_draw(win_t *win)
|
|
|
|
{
|
2013-01-07 13:30:42 +00:00
|
|
|
if (win->bar.h > 0)
|
2012-03-16 20:03:49 +00:00
|
|
|
win_draw_bar(win);
|
2012-02-12 18:00:41 +00:00
|
|
|
|
2014-07-28 18:36:32 +00:00
|
|
|
XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm);
|
|
|
|
XClearWindow(win->env.dpy, win->xwin);
|
2014-06-15 12:15:48 +00:00
|
|
|
XFlush(win->env.dpy);
|
2011-02-17 15:22:54 +00:00
|
|
|
}
|
|
|
|
|
2014-08-17 20:58:02 +00: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 17:25:17 +00:00
|
|
|
{
|
2011-02-17 15:22:54 +00:00
|
|
|
XGCValues gcval;
|
|
|
|
|
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)
|
2014-08-17 20:58:02 +00:00
|
|
|
XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
|
2011-02-17 15:22:54 +00:00
|
|
|
else
|
2014-08-17 20:58:02 +00:00
|
|
|
XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h);
|
2011-01-20 20:44:34 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 10:47:17 +00:00
|
|
|
void win_set_title(win_t *win, const char *title, size_t len)
|
2013-02-08 21:05:31 +00:00
|
|
|
{
|
2022-06-01 10:47:17 +00:00
|
|
|
int i, targets[] = { ATOM_WM_NAME, ATOM_WM_ICON_NAME, ATOM__NET_WM_NAME, ATOM__NET_WM_ICON_NAME };
|
|
|
|
|
|
|
|
for (i = 0; i < ARRLEN(targets); ++i) {
|
|
|
|
XChangeProperty(win->env.dpy, win->xwin, atoms[targets[i]],
|
|
|
|
atoms[ATOM_UTF8_STRING], 8, PropModeReplace,
|
|
|
|
(unsigned char *)title, len);
|
2022-02-27 18:38:27 +00:00
|
|
|
}
|
2011-02-03 23:25:57 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void win_set_cursor(win_t *win, cursor_t cursor)
|
|
|
|
{
|
2017-10-05 10:30:31 +00:00
|
|
|
if (cursor >= 0 && cursor < ARRLEN(cursors)) {
|
|
|
|
XDefineCursor(win->env.dpy, win->xwin, cursors[cursor].icon);
|
|
|
|
XFlush(win->env.dpy);
|
2011-01-29 21:37:40 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-05 11:47:02 +00: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;
|
|
|
|
}
|