| 1 | /* Copyright 2011-2020 Bert Muennich |
| 2 | * Copyright 2021-2022 nsxiv contributors |
| 3 | * |
| 4 | * This file is a part of nsxiv. |
| 5 | * |
| 6 | * nsxiv is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published |
| 8 | * by the Free Software Foundation; either version 2 of the License, |
| 9 | * or (at your option) any later version. |
| 10 | * |
| 11 | * nsxiv is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with nsxiv. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "nsxiv.h" |
| 21 | #define _WINDOW_CONFIG |
| 22 | #include "config.h" |
| 23 | #include "icon/data.h" |
| 24 | |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <locale.h> |
| 28 | #include <unistd.h> |
| 29 | #include <X11/cursorfont.h> |
| 30 | #include <X11/Xatom.h> |
| 31 | #include <X11/Xresource.h> |
| 32 | |
| 33 | extern size_t get_win_title(unsigned char *, int, bool); |
| 34 | |
| 35 | #if HAVE_LIBFONTS |
| 36 | #include "utf8.h" |
| 37 | static XftFont *font; |
| 38 | static double fontsize; |
| 39 | #define TEXTWIDTH(win, text, len) \ |
| 40 | win_draw_text(win, NULL, NULL, 0, 0, text, len, 0) |
| 41 | #endif |
| 42 | |
| 43 | #define RES_CLASS "Nsxiv" |
| 44 | |
| 45 | #define INIT_ATOM_(atom) \ |
| 46 | atoms[ATOM_##atom] = XInternAtom(e->dpy, #atom, False); |
| 47 | |
| 48 | enum { |
| 49 | H_TEXT_PAD = 5, |
| 50 | V_TEXT_PAD = 1 |
| 51 | }; |
| 52 | |
| 53 | static struct { |
| 54 | int name; |
| 55 | Cursor icon; |
| 56 | } cursors[CURSOR_COUNT] = { |
| 57 | { XC_left_ptr }, { XC_dotbox }, { XC_fleur }, { XC_watch }, |
| 58 | { XC_sb_left_arrow }, { XC_sb_right_arrow } |
| 59 | }; |
| 60 | |
| 61 | static GC gc; |
| 62 | |
| 63 | static int barheight; |
| 64 | |
| 65 | Atom atoms[ATOM_COUNT]; |
| 66 | |
| 67 | #if HAVE_LIBFONTS |
| 68 | static void win_init_font(const win_env_t *e, const char *fontstr) |
| 69 | { |
| 70 | int fontheight = 0; |
| 71 | if ((font = XftFontOpenName(e->dpy, e->scr, fontstr)) == NULL) |
| 72 | error(EXIT_FAILURE, 0, "Error loading font '%s'", fontstr); |
| 73 | fontheight = font->ascent + font->descent; |
| 74 | FcPatternGetDouble(font->pattern, FC_SIZE, 0, &fontsize); |
| 75 | barheight = fontheight + 2 * V_TEXT_PAD; |
| 76 | XftFontClose(e->dpy, font); |
| 77 | } |
| 78 | |
| 79 | static void xft_alloc_color(const win_env_t *e, const char *name, XftColor *col) |
| 80 | { |
| 81 | if (!XftColorAllocName(e->dpy, e->vis, e->cmap, name, col)) |
| 82 | error(EXIT_FAILURE, 0, "Error allocating color '%s'", name); |
| 83 | } |
| 84 | #endif /* HAVE_LIBFONTS */ |
| 85 | |
| 86 | static void win_alloc_color(const win_env_t *e, const char *name, XColor *col) |
| 87 | { |
| 88 | XColor screen; |
| 89 | if (!XAllocNamedColor(e->dpy, e->cmap, name, &screen, col)) |
| 90 | error(EXIT_FAILURE, 0, "Error allocating color '%s'", name); |
| 91 | } |
| 92 | |
| 93 | static const char* win_res(XrmDatabase db, const char *name, const char *def) |
| 94 | { |
| 95 | char *type; |
| 96 | XrmValue ret; |
| 97 | |
| 98 | if (db != NULL && |
| 99 | XrmGetResource(db, name, name, &type, &ret) && |
| 100 | STREQ(type, "String")) |
| 101 | { |
| 102 | return ret.addr; |
| 103 | } else { |
| 104 | return def; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void win_init(win_t *win) |
| 109 | { |
| 110 | win_env_t *e; |
| 111 | const char *win_bg, *win_fg, *mrk_fg; |
| 112 | #if HAVE_LIBFONTS |
| 113 | const char *bar_fg, *bar_bg, *f; |
| 114 | #endif |
| 115 | char *res_man; |
| 116 | XrmDatabase db; |
| 117 | |
| 118 | memset(win, 0, sizeof(win_t)); |
| 119 | |
| 120 | e = &win->env; |
| 121 | if ((e->dpy = XOpenDisplay(NULL)) == NULL) |
| 122 | error(EXIT_FAILURE, 0, "Error opening X display"); |
| 123 | |
| 124 | e->scr = DefaultScreen(e->dpy); |
| 125 | e->scrw = DisplayWidth(e->dpy, e->scr); |
| 126 | e->scrh = DisplayHeight(e->dpy, e->scr); |
| 127 | e->depth = DefaultDepth(e->dpy, e->scr); |
| 128 | e->vis = DefaultVisual(e->dpy, e->scr); |
| 129 | e->cmap = DefaultColormap(e->dpy, e->scr); |
| 130 | |
| 131 | if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0) |
| 132 | error(0, 0, "No locale support"); |
| 133 | |
| 134 | XrmInitialize(); |
| 135 | res_man = XResourceManagerString(e->dpy); |
| 136 | db = res_man == NULL ? NULL : XrmGetStringDatabase(res_man); |
| 137 | |
| 138 | win_bg = win_res(db, RES_CLASS ".window.background", DEFAULT_WIN_BG); |
| 139 | win_fg = win_res(db, RES_CLASS ".window.foreground", DEFAULT_WIN_FG); |
| 140 | mrk_fg = win_res(db, RES_CLASS ".mark.foreground", DEFAULT_MARK_COLOR ? DEFAULT_MARK_COLOR : win_fg); |
| 141 | win_alloc_color(e, win_bg, &win->win_bg); |
| 142 | win_alloc_color(e, win_fg, &win->win_fg); |
| 143 | win_alloc_color(e, mrk_fg, &win->mrk_fg); |
| 144 | |
| 145 | #if HAVE_LIBFONTS |
| 146 | bar_bg = win_res(db, RES_CLASS ".bar.background", DEFAULT_BAR_BG ? DEFAULT_BAR_BG : win_bg); |
| 147 | bar_fg = win_res(db, RES_CLASS ".bar.foreground", DEFAULT_BAR_FG ? DEFAULT_BAR_FG : win_fg); |
| 148 | xft_alloc_color(e, bar_bg, &win->bar_bg); |
| 149 | xft_alloc_color(e, bar_fg, &win->bar_fg); |
| 150 | |
| 151 | f = win_res(db, RES_CLASS ".bar.font", DEFAULT_FONT); |
| 152 | win_init_font(e, f); |
| 153 | |
| 154 | win->bar.l.size = BAR_L_LEN; |
| 155 | win->bar.r.size = BAR_R_LEN; |
| 156 | /* 3 padding bytes needed by utf8_decode */ |
| 157 | win->bar.l.buf = emalloc(win->bar.l.size + 3); |
| 158 | win->bar.l.buf[0] = '\0'; |
| 159 | win->bar.r.buf = emalloc(win->bar.r.size + 3); |
| 160 | win->bar.r.buf[0] = '\0'; |
| 161 | win->bar.h = options->hide_bar ? 0 : barheight; |
| 162 | win->bar.top = TOP_STATUSBAR; |
| 163 | #endif /* HAVE_LIBFONTS */ |
| 164 | |
| 165 | XrmDestroyDatabase(db); |
| 166 | INIT_ATOM_(WM_DELETE_WINDOW); |
| 167 | INIT_ATOM_(_NET_WM_NAME); |
| 168 | INIT_ATOM_(_NET_WM_ICON_NAME); |
| 169 | INIT_ATOM_(_NET_WM_ICON); |
| 170 | INIT_ATOM_(_NET_WM_STATE); |
| 171 | INIT_ATOM_(_NET_WM_PID); |
| 172 | INIT_ATOM_(_NET_WM_STATE_FULLSCREEN); |
| 173 | INIT_ATOM_(UTF8_STRING); |
| 174 | INIT_ATOM_(WM_NAME); |
| 175 | INIT_ATOM_(WM_ICON_NAME); |
| 176 | } |
| 177 | |
| 178 | void win_open(win_t *win) |
| 179 | { |
| 180 | int c, i, j, n; |
| 181 | Window parent; |
| 182 | win_env_t *e; |
| 183 | XClassHint classhint; |
| 184 | unsigned long *icon_data; |
| 185 | XColor col; |
| 186 | Cursor *cnone = &cursors[CURSOR_NONE].icon; |
| 187 | char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 188 | Pixmap none; |
| 189 | int gmask; |
| 190 | XSizeHints sizehints; |
| 191 | XWMHints hints; |
| 192 | long pid; |
| 193 | char hostname[256]; |
| 194 | XSetWindowAttributes attrs; |
| 195 | char res_class[] = RES_CLASS; |
| 196 | char res_name[] = "nsxiv"; |
| 197 | |
| 198 | e = &win->env; |
| 199 | parent = options->embed ? options->embed : RootWindow(e->dpy, e->scr); |
| 200 | |
| 201 | sizehints.flags = PWinGravity; |
| 202 | sizehints.win_gravity = NorthWestGravity; |
| 203 | |
| 204 | /* determine window offsets, width & height */ |
| 205 | if (options->geometry == NULL) |
| 206 | gmask = 0; |
| 207 | else |
| 208 | gmask = XParseGeometry(options->geometry, &win->x, &win->y, |
| 209 | &win->w, &win->h); |
| 210 | if (gmask & WidthValue) |
| 211 | sizehints.flags |= USSize; |
| 212 | else |
| 213 | win->w = WIN_WIDTH; |
| 214 | if (gmask & HeightValue) |
| 215 | sizehints.flags |= USSize; |
| 216 | else |
| 217 | win->h = WIN_HEIGHT; |
| 218 | if (gmask & XValue) { |
| 219 | if (gmask & XNegative) { |
| 220 | win->x += e->scrw - win->w; |
| 221 | sizehints.win_gravity = NorthEastGravity; |
| 222 | } |
| 223 | sizehints.flags |= USPosition; |
| 224 | } else { |
| 225 | win->x = 0; |
| 226 | } |
| 227 | if (gmask & YValue) { |
| 228 | if (gmask & YNegative) { |
| 229 | win->y += e->scrh - win->h; |
| 230 | sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity |
| 231 | ? SouthEastGravity : SouthWestGravity; |
| 232 | } |
| 233 | sizehints.flags |= USPosition; |
| 234 | } else { |
| 235 | win->y = 0; |
| 236 | } |
| 237 | |
| 238 | attrs.colormap = e->cmap; |
| 239 | attrs.border_pixel = 0; |
| 240 | |
| 241 | win->xwin = XCreateWindow(e->dpy, parent, win->x, win->y, win->w, win->h, 0, |
| 242 | e->depth, InputOutput, e->vis, |
| 243 | CWColormap | CWBorderPixel, &attrs); |
| 244 | if (win->xwin == None) |
| 245 | error(EXIT_FAILURE, 0, "Error creating X window"); |
| 246 | |
| 247 | /* set the _NET_WM_PID */ |
| 248 | pid = getpid(); |
| 249 | XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_PID], XA_CARDINAL, |
| 250 | 32, PropModeReplace, (unsigned char *) &pid, 1); |
| 251 | if (gethostname(hostname, ARRLEN(hostname)) == 0) { |
| 252 | XTextProperty tp; |
| 253 | tp.value = (unsigned char *)hostname; |
| 254 | tp.nitems = strnlen(hostname, ARRLEN(hostname)); |
| 255 | tp.encoding = XA_STRING; |
| 256 | tp.format = 8; |
| 257 | XSetWMClientMachine(e->dpy, win->xwin, &tp); |
| 258 | } |
| 259 | |
| 260 | XSelectInput(e->dpy, win->xwin, |
| 261 | ButtonReleaseMask | ButtonPressMask | KeyPressMask | |
| 262 | PointerMotionMask | StructureNotifyMask); |
| 263 | |
| 264 | for (i = 0; i < ARRLEN(cursors); i++) { |
| 265 | if (i != CURSOR_NONE) |
| 266 | cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name); |
| 267 | } |
| 268 | if (XAllocNamedColor(e->dpy, e->cmap, "black", &col, &col) == 0) |
| 269 | error(EXIT_FAILURE, 0, "Error allocating color 'black'"); |
| 270 | |
| 271 | none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8); |
| 272 | *cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0); |
| 273 | |
| 274 | gc = XCreateGC(e->dpy, win->xwin, 0, None); |
| 275 | |
| 276 | n = icons[ARRLEN(icons)-1].size; |
| 277 | icon_data = emalloc((n * n + 2) * sizeof(*icon_data)); |
| 278 | |
| 279 | for (i = 0; i < ARRLEN(icons); i++) { |
| 280 | n = 0; |
| 281 | icon_data[n++] = icons[i].size; |
| 282 | icon_data[n++] = icons[i].size; |
| 283 | |
| 284 | for (j = 0; j < icons[i].cnt; j++) { |
| 285 | for (c = icons[i].data[j] >> 4; c >= 0; c--) |
| 286 | icon_data[n++] = icon_colors[icons[i].data[j] & 0x0F]; |
| 287 | } |
| 288 | XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_ICON], XA_CARDINAL, 32, |
| 289 | i == 0 ? PropModeReplace : PropModeAppend, |
| 290 | (unsigned char *) icon_data, n); |
| 291 | } |
| 292 | free(icon_data); |
| 293 | |
| 294 | win_set_title(win, true); |
| 295 | classhint.res_class = res_class; |
| 296 | classhint.res_name = options->res_name != NULL ? options->res_name : res_name; |
| 297 | XSetClassHint(e->dpy, win->xwin, &classhint); |
| 298 | |
| 299 | XSetWMProtocols(e->dpy, win->xwin, &atoms[ATOM_WM_DELETE_WINDOW], 1); |
| 300 | |
| 301 | sizehints.width = win->w; |
| 302 | sizehints.height = win->h; |
| 303 | sizehints.x = win->x; |
| 304 | sizehints.y = win->y; |
| 305 | XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints); |
| 306 | |
| 307 | hints.flags = InputHint | StateHint; |
| 308 | hints.input = 1; |
| 309 | hints.initial_state = NormalState; |
| 310 | XSetWMHints(win->env.dpy, win->xwin, &hints); |
| 311 | |
| 312 | if (options->fullscreen) { |
| 313 | XChangeProperty(e->dpy, win->xwin, atoms[ATOM__NET_WM_STATE], |
| 314 | XA_ATOM, 32, PropModeReplace, |
| 315 | (unsigned char *) &atoms[ATOM__NET_WM_STATE_FULLSCREEN], 1); |
| 316 | } |
| 317 | |
| 318 | win->h -= win->bar.h; |
| 319 | |
| 320 | win->buf.w = e->scrw; |
| 321 | win->buf.h = e->scrh; |
| 322 | win->buf.pm = XCreatePixmap(e->dpy, win->xwin, win->buf.w, win->buf.h, e->depth); |
| 323 | |
| 324 | XSetForeground(e->dpy, gc, win->win_bg.pixel); |
| 325 | XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h); |
| 326 | XSetWindowBackgroundPixmap(e->dpy, win->xwin, win->buf.pm); |
| 327 | XMapWindow(e->dpy, win->xwin); |
| 328 | XFlush(e->dpy); |
| 329 | } |
| 330 | |
| 331 | CLEANUP void win_close(win_t *win) |
| 332 | { |
| 333 | unsigned int i; |
| 334 | |
| 335 | for (i = 0; i < ARRLEN(cursors); i++) |
| 336 | XFreeCursor(win->env.dpy, cursors[i].icon); |
| 337 | |
| 338 | XFreeGC(win->env.dpy, gc); |
| 339 | |
| 340 | XDestroyWindow(win->env.dpy, win->xwin); |
| 341 | XCloseDisplay(win->env.dpy); |
| 342 | } |
| 343 | |
| 344 | bool win_configure(win_t *win, XConfigureEvent *c) |
| 345 | { |
| 346 | bool changed; |
| 347 | |
| 348 | changed = win->w != c->width || win->h + win->bar.h != c->height; |
| 349 | |
| 350 | win->x = c->x; |
| 351 | win->y = c->y; |
| 352 | win->w = c->width; |
| 353 | win->h = c->height - win->bar.h; |
| 354 | win->bw = c->border_width; |
| 355 | |
| 356 | return changed; |
| 357 | } |
| 358 | |
| 359 | void win_toggle_fullscreen(win_t *win) |
| 360 | { |
| 361 | XEvent ev; |
| 362 | XClientMessageEvent *cm; |
| 363 | |
| 364 | memset(&ev, 0, sizeof(ev)); |
| 365 | ev.type = ClientMessage; |
| 366 | |
| 367 | cm = &ev.xclient; |
| 368 | cm->window = win->xwin; |
| 369 | cm->message_type = atoms[ATOM__NET_WM_STATE]; |
| 370 | cm->format = 32; |
| 371 | cm->data.l[0] = 2; /* toggle */ |
| 372 | cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN]; |
| 373 | |
| 374 | XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False, |
| 375 | SubstructureNotifyMask | SubstructureRedirectMask, &ev); |
| 376 | } |
| 377 | |
| 378 | void win_toggle_bar(win_t *win) |
| 379 | { |
| 380 | if (win->bar.h != 0) { |
| 381 | win->h += win->bar.h; |
| 382 | win->bar.h = 0; |
| 383 | } else { |
| 384 | win->bar.h = barheight; |
| 385 | win->h -= win->bar.h; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | void win_clear(win_t *win) |
| 390 | { |
| 391 | win_env_t *e; |
| 392 | |
| 393 | e = &win->env; |
| 394 | |
| 395 | if (win->w > win->buf.w || win->h + win->bar.h > win->buf.h) { |
| 396 | XFreePixmap(e->dpy, win->buf.pm); |
| 397 | win->buf.w = MAX(win->buf.w, win->w); |
| 398 | win->buf.h = MAX(win->buf.h, win->h + win->bar.h); |
| 399 | win->buf.pm = XCreatePixmap(e->dpy, win->xwin, |
| 400 | win->buf.w, win->buf.h, e->depth); |
| 401 | } |
| 402 | XSetForeground(e->dpy, gc, win->win_bg.pixel); |
| 403 | XFillRectangle(e->dpy, win->buf.pm, gc, 0, 0, win->buf.w, win->buf.h); |
| 404 | } |
| 405 | |
| 406 | #if HAVE_LIBFONTS |
| 407 | static int win_draw_text(win_t *win, XftDraw *d, const XftColor *color, |
| 408 | int x, int y, char *text, int len, int w) |
| 409 | { |
| 410 | int err, tw = 0; |
| 411 | char *t, *next; |
| 412 | uint32_t rune; |
| 413 | XftFont *f; |
| 414 | FcCharSet *fccharset; |
| 415 | XGlyphInfo ext; |
| 416 | |
| 417 | for (t = text; t - text < len; t = next) { |
| 418 | next = utf8_decode(t, &rune, &err); |
| 419 | if (XftCharExists(win->env.dpy, font, rune)) { |
| 420 | f = font; |
| 421 | } else { /* fallback font */ |
| 422 | fccharset = FcCharSetCreate(); |
| 423 | FcCharSetAddChar(fccharset, rune); |
| 424 | f = XftFontOpen(win->env.dpy, win->env.scr, FC_CHARSET, FcTypeCharSet, |
| 425 | fccharset, FC_SCALABLE, FcTypeBool, FcTrue, |
| 426 | FC_SIZE, FcTypeDouble, fontsize, NULL); |
| 427 | FcCharSetDestroy(fccharset); |
| 428 | } |
| 429 | XftTextExtentsUtf8(win->env.dpy, f, (XftChar8*)t, next - t, &ext); |
| 430 | tw += ext.xOff; |
| 431 | if (tw <= w) { |
| 432 | XftDrawStringUtf8(d, color, f, x, y, (XftChar8*)t, next - t); |
| 433 | x += ext.xOff; |
| 434 | } |
| 435 | if (f != font) |
| 436 | XftFontClose(win->env.dpy, f); |
| 437 | } |
| 438 | return tw; |
| 439 | } |
| 440 | |
| 441 | static void win_draw_bar(win_t *win) |
| 442 | { |
| 443 | int len, x, y, w, tw; |
| 444 | win_env_t *e; |
| 445 | win_bar_t *l, *r; |
| 446 | XftDraw *d; |
| 447 | |
| 448 | if ((l = &win->bar.l)->buf == NULL || (r = &win->bar.r)->buf == NULL) |
| 449 | return; |
| 450 | |
| 451 | e = &win->env; |
| 452 | y = (win->bar.top ? 0 : win->h) + font->ascent + V_TEXT_PAD; |
| 453 | w = win->w - 2*H_TEXT_PAD; |
| 454 | d = XftDrawCreate(e->dpy, win->buf.pm, e->vis, e->cmap); |
| 455 | |
| 456 | XSetForeground(e->dpy, gc, win->bar_bg.pixel); |
| 457 | XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->bar.top ? 0 : win->h, win->w, win->bar.h); |
| 458 | |
| 459 | XSetForeground(e->dpy, gc, win->win_bg.pixel); |
| 460 | XSetBackground(e->dpy, gc, win->bar_bg.pixel); |
| 461 | |
| 462 | if ((len = strlen(r->buf)) > 0) { |
| 463 | if ((tw = TEXTWIDTH(win, r->buf, len)) > w) |
| 464 | return; |
| 465 | x = win->w - tw - H_TEXT_PAD; |
| 466 | w -= tw; |
| 467 | win_draw_text(win, d, &win->bar_fg, x, y, r->buf, len, tw); |
| 468 | } |
| 469 | if ((len = strlen(l->buf)) > 0) { |
| 470 | x = H_TEXT_PAD; |
| 471 | w -= 2 * H_TEXT_PAD; /* gap between left and right parts */ |
| 472 | win_draw_text(win, d, &win->bar_fg, x, y, l->buf, len, w); |
| 473 | } |
| 474 | XftDrawDestroy(d); |
| 475 | } |
| 476 | #else |
| 477 | static void win_draw_bar(win_t *win) |
| 478 | { |
| 479 | (void) win; |
| 480 | } |
| 481 | #endif /* HAVE_LIBFONTS */ |
| 482 | |
| 483 | void win_draw(win_t *win) |
| 484 | { |
| 485 | if (win->bar.h > 0) |
| 486 | win_draw_bar(win); |
| 487 | |
| 488 | XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->buf.pm); |
| 489 | XClearWindow(win->env.dpy, win->xwin); |
| 490 | XFlush(win->env.dpy); |
| 491 | } |
| 492 | |
| 493 | void win_draw_rect(win_t *win, int x, int y, int w, int h, bool fill, int lw, |
| 494 | unsigned long col) |
| 495 | { |
| 496 | XGCValues gcval; |
| 497 | |
| 498 | gcval.line_width = lw; |
| 499 | gcval.foreground = col; |
| 500 | XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval); |
| 501 | |
| 502 | if (fill) |
| 503 | XFillRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h); |
| 504 | else |
| 505 | XDrawRectangle(win->env.dpy, win->buf.pm, gc, x, y, w, h); |
| 506 | } |
| 507 | |
| 508 | void win_set_title(win_t *win, bool init) |
| 509 | { |
| 510 | size_t len, i; |
| 511 | unsigned char title[512]; |
| 512 | int targets[] = { ATOM_WM_NAME, ATOM_WM_ICON_NAME, ATOM__NET_WM_NAME, ATOM__NET_WM_ICON_NAME }; |
| 513 | |
| 514 | if ((len = get_win_title(title, ARRLEN(title), init)) > 0) { |
| 515 | for (i = 0; i < ARRLEN(targets); ++i) { |
| 516 | XChangeProperty(win->env.dpy, win->xwin, atoms[targets[i]], |
| 517 | atoms[ATOM_UTF8_STRING], 8, PropModeReplace, title, len); |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | void win_set_cursor(win_t *win, cursor_t cursor) |
| 523 | { |
| 524 | if (cursor >= 0 && cursor < ARRLEN(cursors)) { |
| 525 | XDefineCursor(win->env.dpy, win->xwin, cursors[cursor].icon); |
| 526 | XFlush(win->env.dpy); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | void win_cursor_pos(win_t *win, int *x, int *y) |
| 531 | { |
| 532 | int i; |
| 533 | unsigned int ui; |
| 534 | Window w; |
| 535 | |
| 536 | if (!XQueryPointer(win->env.dpy, win->xwin, &w, &w, &i, &i, x, y, &ui)) |
| 537 | *x = *y = 0; |
| 538 | } |