2023-03-29 15:33:58 +02:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <X11/Xlib.h>
|
2023-05-21 04:16:55 +02:00
|
|
|
#include <cairo/cairo.h>
|
2023-03-29 15:33:58 +02:00
|
|
|
#include <pango/pango.h>
|
2023-05-21 04:16:55 +02:00
|
|
|
#include <pango/pangocairo.h>
|
2023-05-10 19:43:41 +02:00
|
|
|
#include <iconv.h>
|
2023-03-08 21:12:37 +01:00
|
|
|
|
2023-05-10 08:20:29 +02:00
|
|
|
#include "drw.h"
|
2023-05-03 11:49:56 +02:00
|
|
|
#include "../sl/main.h"
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-21 04:16:55 +02:00
|
|
|
Clr transcheme[3]; // transition colorscheme
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-21 04:16:55 +02:00
|
|
|
Drw *drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h, Visual *visual, unsigned int depth, Colormap cmap) {
|
2023-05-08 23:00:45 +02:00
|
|
|
Drw *drw = ecalloc(1, sizeof(Drw));
|
|
|
|
|
|
|
|
drw->dpy = dpy;
|
|
|
|
drw->screen = screen;
|
|
|
|
drw->root = root;
|
|
|
|
drw->w = w;
|
|
|
|
drw->h = h;
|
|
|
|
drw->visual = visual;
|
|
|
|
drw->depth = depth;
|
|
|
|
drw->cmap = cmap;
|
|
|
|
drw->drawable = XCreatePixmap(dpy, root, w, h, depth);
|
|
|
|
drw->gc = XCreateGC(dpy, drw->drawable, 0, NULL);
|
|
|
|
XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
|
|
|
|
|
|
|
|
return drw;
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:36:50 +02:00
|
|
|
void drw_resize(Drw *drw, unsigned int w, unsigned int h) {
|
2023-05-08 23:00:45 +02:00
|
|
|
if (!drw)
|
|
|
|
return;
|
|
|
|
|
|
|
|
drw->w = w;
|
|
|
|
drw->h = h;
|
2023-05-21 04:16:55 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
if (drw->drawable)
|
|
|
|
XFreePixmap(drw->dpy, drw->drawable);
|
2023-05-21 04:16:55 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, drw->depth);
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:36:50 +02:00
|
|
|
void drw_free(Drw *drw) {
|
2023-05-08 23:00:45 +02:00
|
|
|
XFreePixmap(drw->dpy, drw->drawable);
|
|
|
|
XFreeGC(drw->dpy, drw->gc);
|
|
|
|
drw_font_free(drw->font);
|
|
|
|
free(drw);
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-21 04:16:55 +02:00
|
|
|
static Fnt *xfont_create(Drw *drw, const char *fontname) {
|
2023-05-08 23:00:45 +02:00
|
|
|
Fnt *font;
|
|
|
|
PangoFontMap *fontmap;
|
|
|
|
PangoContext *context;
|
|
|
|
PangoFontDescription *desc;
|
|
|
|
PangoFontMetrics *metrics;
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
if (!fontname) {
|
|
|
|
die("spmenu: no font specified.");
|
|
|
|
}
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
font = ecalloc(1, sizeof(Fnt));
|
|
|
|
font->dpy = drw->dpy;
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-21 04:16:55 +02:00
|
|
|
fontmap = pango_cairo_font_map_new();
|
2023-05-08 23:00:45 +02:00
|
|
|
context = pango_font_map_create_context(fontmap);
|
|
|
|
desc = pango_font_description_from_string(fontname);
|
|
|
|
font->layout = pango_layout_new(context);
|
|
|
|
pango_layout_set_font_description(font->layout, desc);
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
metrics = pango_context_get_metrics(context, desc, pango_language_from_string ("en-us"));
|
|
|
|
font->h = pango_font_metrics_get_height(metrics) / PANGO_SCALE;
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
pango_font_metrics_unref(metrics);
|
|
|
|
g_object_unref(context);
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
return font;
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:36:50 +02:00
|
|
|
void xfont_free(Fnt *font) {
|
2023-05-08 23:00:45 +02:00
|
|
|
if (!font)
|
|
|
|
return;
|
|
|
|
if (font->layout)
|
|
|
|
g_object_unref(font->layout);
|
|
|
|
free(font);
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:36:50 +02:00
|
|
|
Fnt* drw_font_create(Drw* drw, char *font[], size_t fontcount) {
|
2023-05-08 23:00:45 +02:00
|
|
|
if (!drw || !font)
|
|
|
|
return NULL;
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
Fnt *fnt = NULL;
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
fnt = xfont_create(drw, *font);
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-21 04:16:55 +02:00
|
|
|
return (drw->font = fnt);
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:36:50 +02:00
|
|
|
void drw_font_free(Fnt *font) {
|
2023-05-08 23:00:45 +02:00
|
|
|
if (font) {
|
|
|
|
xfont_free(font);
|
|
|
|
}
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:36:50 +02:00
|
|
|
void drw_clr_create(Drw *drw, Clr *dest, char *clrname, unsigned int alpha) {
|
2023-05-21 04:16:55 +02:00
|
|
|
XColor color;
|
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
if (!drw || !dest || !clrname)
|
|
|
|
return;
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-21 04:16:55 +02:00
|
|
|
if (!XAllocNamedColor(drw->dpy, drw->cmap, clrname, &color, dest))
|
2023-05-08 23:00:45 +02:00
|
|
|
die("spmenu: cannot allocate color '%s'", clrname);
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-21 04:16:55 +02:00
|
|
|
dest->red = color.red;
|
|
|
|
dest->green = color.green;
|
|
|
|
dest->blue = color.blue;
|
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
dest->pixel = (dest->pixel & 0x00ffffffU) | (alpha << 24);
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wrapper to create color schemes. The caller has to call free(3) on the
|
|
|
|
* returned color scheme when done using it. */
|
2023-05-06 14:36:50 +02:00
|
|
|
Clr * drw_scm_create(Drw *drw, char *clrnames[], unsigned int alphas[], size_t clrcount) {
|
2023-05-08 23:00:45 +02:00
|
|
|
size_t i;
|
|
|
|
Clr *ret;
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
/* need at least two colors for a scheme */
|
2023-05-21 04:16:55 +02:00
|
|
|
if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XColor))))
|
2023-05-08 23:00:45 +02:00
|
|
|
return NULL;
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
for (i = 0; i < clrcount; i++)
|
|
|
|
drw_clr_create(drw, &ret[i], clrnames[i], alphas[i]);
|
|
|
|
return ret;
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:36:50 +02:00
|
|
|
void drw_setscheme(Drw *drw, Clr *scm) {
|
2023-05-08 23:00:45 +02:00
|
|
|
if (drw)
|
|
|
|
drw->scheme = scm;
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:36:50 +02:00
|
|
|
void drw_settrans(Drw *drw, Clr *psc, Clr *nsc) {
|
2023-05-08 23:00:45 +02:00
|
|
|
if (drw) {
|
|
|
|
transcheme[0] = psc[ColBg]; transcheme[1] = nsc[ColBg]; transcheme[2] = psc[ColPwl];
|
|
|
|
drw->scheme = transcheme;
|
|
|
|
}
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:36:50 +02:00
|
|
|
void drw_arrow(Drw *drw, int x, int y, unsigned int w, unsigned int h, int direction, int slash) {
|
2023-05-08 23:00:45 +02:00
|
|
|
if (!drw || !drw->scheme)
|
|
|
|
return;
|
|
|
|
|
|
|
|
x = direction ? x : x + w;
|
|
|
|
w = direction ? w : -w;
|
|
|
|
unsigned int hh = slash ? (direction ? 0 : h) : h/2;
|
|
|
|
|
|
|
|
XPoint points[] = {
|
|
|
|
{x , y },
|
|
|
|
{x + w, y + hh },
|
|
|
|
{x , y + h },
|
|
|
|
};
|
|
|
|
|
|
|
|
XPoint bg[] = {
|
|
|
|
{x , y },
|
|
|
|
{x + w, y },
|
|
|
|
{x + w, y + h},
|
|
|
|
{x , y + h},
|
|
|
|
};
|
|
|
|
|
|
|
|
XSetForeground(drw->dpy, drw->gc, drw->scheme[ColBg].pixel);
|
|
|
|
XFillPolygon(drw->dpy, drw->drawable, drw->gc, bg, 4, Convex, CoordModeOrigin);
|
|
|
|
XSetForeground(drw->dpy, drw->gc, drw->scheme[ColFg].pixel);
|
|
|
|
XFillPolygon(drw->dpy, drw->drawable, drw->gc, points, 3, Nonconvex, CoordModeOrigin);
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:36:50 +02:00
|
|
|
void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert) {
|
2023-05-08 23:00:45 +02:00
|
|
|
if (!drw || !drw->scheme)
|
|
|
|
return;
|
|
|
|
XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel);
|
|
|
|
if (filled)
|
|
|
|
XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
|
|
|
|
else
|
|
|
|
XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1);
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:36:50 +02:00
|
|
|
int xerrordummy(Display *dpy, XErrorEvent *ee) {
|
2023-05-08 23:00:45 +02:00
|
|
|
return 0;
|
2023-05-03 21:38:34 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:36:50 +02:00
|
|
|
int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert, Bool markup) {
|
2023-05-08 23:00:45 +02:00
|
|
|
char buf[1024];
|
|
|
|
int ty;
|
|
|
|
unsigned int ew = 0;
|
2023-05-21 04:16:55 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
size_t i, len;
|
|
|
|
int render = x || y || w || h;
|
2023-05-10 18:56:56 +02:00
|
|
|
char *t;
|
2023-05-08 23:00:45 +02:00
|
|
|
|
2023-05-21 04:16:55 +02:00
|
|
|
XSetErrorHandler(xerrordummy);
|
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
if (!drw || (render && !drw->scheme) || !text || !drw->font)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!render) {
|
|
|
|
w = ~w;
|
|
|
|
} else {
|
|
|
|
XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
|
|
|
|
XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
|
2023-05-21 04:16:55 +02:00
|
|
|
|
|
|
|
drw->surface = cairo_xlib_surface_create(drw->dpy, drw->drawable, drw->visual, drw->w, drw->h);
|
|
|
|
drw->d = cairo_create(drw->surface);
|
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
x += lpad;
|
|
|
|
w -= lpad;
|
|
|
|
}
|
|
|
|
|
2023-05-22 20:44:21 +02:00
|
|
|
t = strdup(text);
|
2023-05-21 04:16:55 +02:00
|
|
|
len = strlen(t);
|
2023-05-10 19:43:41 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
if (len) {
|
2023-05-10 18:56:56 +02:00
|
|
|
drw_font_getexts(drw->font, t, len, &ew, NULL, markup);
|
2023-05-21 04:16:55 +02:00
|
|
|
|
|
|
|
// shorten text if necessary
|
2023-05-10 18:56:56 +02:00
|
|
|
for (len = MIN(len, sizeof(buf) - 1); len && ew > w; drw_font_getexts(drw->font, t, len, &ew, NULL, markup))
|
2023-04-03 20:50:57 +02:00
|
|
|
len--;
|
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
if (len) {
|
2023-05-10 18:56:56 +02:00
|
|
|
memcpy(buf, t, len);
|
2023-05-08 23:00:45 +02:00
|
|
|
buf[len] = '\0';
|
2023-05-10 18:56:56 +02:00
|
|
|
if (len < strlen(t))
|
2023-05-08 23:00:45 +02:00
|
|
|
for (i = len; i && i > len - 3; buf[--i] = '.')
|
2023-05-21 04:16:55 +02:00
|
|
|
; // NOP
|
2023-05-08 23:00:45 +02:00
|
|
|
|
2023-05-21 04:16:55 +02:00
|
|
|
if (!strstr(buf, "</")) // must contain </
|
2023-05-10 19:43:41 +02:00
|
|
|
markup = 0;
|
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
if (render) {
|
|
|
|
ty = y + (h - drw->font->h) / 2;
|
2023-05-21 04:16:55 +02:00
|
|
|
|
|
|
|
if (markup) {
|
2023-05-08 23:00:45 +02:00
|
|
|
pango_layout_set_markup(drw->font->layout, buf, len);
|
2023-05-21 04:16:55 +02:00
|
|
|
} else {
|
2023-05-08 23:00:45 +02:00
|
|
|
pango_layout_set_text(drw->font->layout, buf, len);
|
|
|
|
}
|
|
|
|
|
2023-05-21 16:27:14 +02:00
|
|
|
pango_layout_set_single_paragraph_mode(drw->font->layout, True);
|
2023-05-08 23:00:45 +02:00
|
|
|
|
2023-05-21 04:16:55 +02:00
|
|
|
cairo_set_source_rgb(drw->d, convert_color(drw->scheme->red), convert_color(drw->scheme->green), convert_color(drw->scheme->blue));
|
|
|
|
cairo_move_to(drw->d, x, ty);
|
2023-05-08 23:00:45 +02:00
|
|
|
|
2023-05-21 04:16:55 +02:00
|
|
|
pango_cairo_update_layout(drw->d, drw->font->layout);
|
|
|
|
pango_cairo_show_layout(drw->d, drw->font->layout);
|
2023-05-08 23:00:45 +02:00
|
|
|
|
2023-05-21 04:16:55 +02:00
|
|
|
if (markup) // clear markup attributes
|
|
|
|
pango_layout_set_attributes(drw->font->layout, NULL);
|
2023-05-08 23:00:45 +02:00
|
|
|
}
|
2023-05-21 04:16:55 +02:00
|
|
|
|
|
|
|
x += ew;
|
|
|
|
w -= ew;
|
2023-05-08 23:00:45 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-03 21:38:34 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
return x + (render ? w : 0);
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:36:50 +02:00
|
|
|
void drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h) {
|
2023-05-08 23:00:45 +02:00
|
|
|
if (!drw)
|
|
|
|
return;
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
|
|
|
|
XSync(drw->dpy, False);
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:36:50 +02:00
|
|
|
unsigned int drw_font_getwidth(Drw *drw, const char *text, Bool markup) {
|
2023-05-08 23:00:45 +02:00
|
|
|
if (!drw || !drw->font || !text)
|
|
|
|
return 0;
|
|
|
|
return drw_text(drw, 0, 0, 0, 0, 0, text, 0, markup);
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:36:50 +02:00
|
|
|
void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h, Bool markup) {
|
2023-05-08 23:00:45 +02:00
|
|
|
if (!font || !text)
|
|
|
|
return;
|
|
|
|
|
2023-05-10 19:43:41 +02:00
|
|
|
char *t = strdup(text);
|
|
|
|
|
|
|
|
if (!strstr(t, "</"))
|
|
|
|
markup = 0;
|
2023-05-21 04:16:55 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
PangoRectangle r;
|
2023-05-21 04:16:55 +02:00
|
|
|
|
|
|
|
if (markup)
|
2023-05-10 19:43:41 +02:00
|
|
|
pango_layout_set_markup(font->layout, t, len);
|
2023-05-08 23:00:45 +02:00
|
|
|
else
|
2023-05-10 19:43:41 +02:00
|
|
|
pango_layout_set_text(font->layout, t, len);
|
2023-05-21 04:16:55 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
pango_layout_get_extents(font->layout, 0, &r);
|
2023-05-21 04:16:55 +02:00
|
|
|
|
|
|
|
if (markup) // clear markup attributes
|
2023-05-08 23:00:45 +02:00
|
|
|
pango_layout_set_attributes(font->layout, NULL);
|
2023-05-21 04:16:55 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
if (w)
|
|
|
|
*w = r.width / PANGO_SCALE;
|
2023-05-21 04:16:55 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
if (h)
|
|
|
|
*h = font->h;
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:36:50 +02:00
|
|
|
Cur * drw_cur_create(Drw *drw, int shape) {
|
2023-05-08 23:00:45 +02:00
|
|
|
Cur *cur;
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
if (!drw || !(cur = ecalloc(1, sizeof(Cur))))
|
|
|
|
return NULL;
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
cur->cursor = XCreateFontCursor(drw->dpy, shape);
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
return cur;
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:36:50 +02:00
|
|
|
void drw_cur_free(Drw *drw, Cur *cursor) {
|
2023-05-08 23:00:45 +02:00
|
|
|
if (!cursor)
|
|
|
|
return;
|
2023-03-29 15:33:58 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
XFreeCursor(drw->dpy, cursor->cursor);
|
|
|
|
free(cursor);
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:36:50 +02:00
|
|
|
unsigned int drw_fontset_getwidth_clamp(Drw *drw, const char *text, unsigned int n, Bool markup) {
|
2023-05-08 23:00:45 +02:00
|
|
|
unsigned int tmp = 0;
|
|
|
|
if (drw && drw->font && text && n)
|
|
|
|
tmp = drw_text(drw, 0, 0, 0, 0, 0, text, n, markup);
|
|
|
|
return MIN(n, tmp);
|
2023-03-29 15:33:58 +02:00
|
|
|
}
|