Initial commit: Geminux 0.1.0 (helio) for Planet Computers Gemini PDA (MT6797X)
This commit is contained in:
commit
034c1f61b1
816 files changed
+223435
No files matched your search
+212
@@ -0,0 +1,212 @@
|
||||
/* See LICENSE file for copyright and license details. */
|
||||
/* Pango + HarfBuzz + Xft drawing backend for Geminux DWM */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xft/Xft.h>
|
||||
#include <pango/pango.h>
|
||||
#include <pango/pangoxft.h>
|
||||
|
||||
#include "drw.h"
|
||||
#include "util.h"
|
||||
|
||||
Drw *drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h) {
|
||||
Drw *drw = ecalloc(1, sizeof(Drw));
|
||||
drw->dpy = dpy;
|
||||
drw->screen = screen;
|
||||
drw->root = root;
|
||||
drw->w = w;
|
||||
drw->h = h;
|
||||
drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
|
||||
drw->gc = XCreateGC(dpy, root, 0, NULL);
|
||||
XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
|
||||
return drw;
|
||||
}
|
||||
|
||||
void drw_resize(Drw *drw, unsigned int w, unsigned int h) {
|
||||
if (!drw) return;
|
||||
drw->w = w;
|
||||
drw->h = h;
|
||||
if (drw->drawable)
|
||||
XFreePixmap(drw->dpy, drw->drawable);
|
||||
drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen));
|
||||
}
|
||||
|
||||
void drw_free(Drw *drw) {
|
||||
if (!drw) return;
|
||||
if (drw->drawable)
|
||||
XFreePixmap(drw->dpy, drw->drawable);
|
||||
if (drw->gc)
|
||||
XFreeGC(drw->dpy, drw->gc);
|
||||
drw_font_free(drw->font);
|
||||
free(drw);
|
||||
}
|
||||
|
||||
Fnt *drw_font_create(Drw *drw, const char font[]) {
|
||||
Fnt *fnt = ecalloc(1, sizeof(Fnt));
|
||||
PangoFontMetrics *metrics;
|
||||
|
||||
fnt->dpy = drw->dpy;
|
||||
fnt->fontmap = pango_xft_get_font_map(drw->dpy, drw->screen);
|
||||
fnt->context = pango_font_map_create_context(fnt->fontmap);
|
||||
fnt->desc = pango_font_description_from_string(font && font[0] ? font : "Fira Code 18px");
|
||||
pango_context_set_font_description(fnt->context, fnt->desc);
|
||||
|
||||
metrics = pango_context_get_metrics(fnt->context, fnt->desc, pango_language_get_default());
|
||||
fnt->h = (unsigned int)((pango_font_metrics_get_ascent(metrics) +
|
||||
pango_font_metrics_get_descent(metrics)) / PANGO_SCALE);
|
||||
if (fnt->h < 12) fnt->h = 18;
|
||||
pango_font_metrics_unref(metrics);
|
||||
|
||||
fnt->layout = pango_layout_new(fnt->context);
|
||||
pango_layout_set_font_description(fnt->layout, fnt->desc);
|
||||
return fnt;
|
||||
}
|
||||
|
||||
void drw_font_free(Fnt *font) {
|
||||
if (!font) return;
|
||||
if (font->layout) g_object_unref(font->layout);
|
||||
if (font->context) g_object_unref(font->context);
|
||||
if (font->desc) pango_font_description_free(font->desc);
|
||||
free(font);
|
||||
}
|
||||
|
||||
void drw_clr_create(Drw *drw, Clr *dest, const char *clrname) {
|
||||
if (!drw || !dest || !clrname) return;
|
||||
if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
|
||||
DefaultColormap(drw->dpy, drw->screen), clrname, dest)) {
|
||||
fprintf(stderr, "dwm: warning: cannot allocate color '%s', using #ffffff\n", clrname);
|
||||
XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
|
||||
DefaultColormap(drw->dpy, drw->screen), "#ffffff", dest);
|
||||
}
|
||||
}
|
||||
|
||||
Clr *drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount) {
|
||||
Clr *ret;
|
||||
if (!drw || !clrnames || clrcount == 0) return NULL;
|
||||
ret = ecalloc(clrcount, sizeof(XftColor));
|
||||
for (size_t i = 0; i < clrcount; i++)
|
||||
drw_clr_create(drw, &ret[i], clrnames[i]);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void drw_setfont(Drw *drw, Fnt *font) {
|
||||
if (drw) drw->font = font;
|
||||
}
|
||||
|
||||
void drw_setscheme(Drw *drw, Clr *scm) {
|
||||
if (drw) drw->scheme = scm;
|
||||
}
|
||||
|
||||
void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert) {
|
||||
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);
|
||||
}
|
||||
|
||||
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) {
|
||||
char buf[1024];
|
||||
int ty, render = x || y || w || h;
|
||||
unsigned int ew, eh;
|
||||
XftDraw *d = NULL;
|
||||
|
||||
if (!drw || (render && !drw->scheme) || !text || !drw->font)
|
||||
return 0;
|
||||
|
||||
if (!render) {
|
||||
w = ~0;
|
||||
} else {
|
||||
XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
|
||||
XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
|
||||
d = XftDrawCreate(drw->dpy, drw->drawable,
|
||||
DefaultVisual(drw->dpy, drw->screen),
|
||||
DefaultColormap(drw->dpy, drw->screen));
|
||||
x += (int)lpad;
|
||||
if (w > lpad) w -= lpad;
|
||||
}
|
||||
|
||||
size_t len = strlen(text);
|
||||
if (len >= sizeof(buf)) len = sizeof(buf) - 1;
|
||||
memcpy(buf, text, len);
|
||||
buf[len] = '\0';
|
||||
|
||||
if (len > 0 && w > 0) {
|
||||
drw_font_getexts(drw->font, buf, (unsigned int)len, &ew, &eh, markup);
|
||||
while (len > 3 && ew > w) {
|
||||
len--;
|
||||
while (len > 0 && (buf[len] & 0xc0) == 0x80) len--;
|
||||
if (len >= 3) memcpy(buf + len - 3, "...", 3);
|
||||
buf[len] = '\0';
|
||||
drw_font_getexts(drw->font, buf, (unsigned int)len, &ew, &eh, markup);
|
||||
}
|
||||
|
||||
if (render && d) {
|
||||
if (markup)
|
||||
pango_layout_set_markup(drw->font->layout, buf, -1);
|
||||
else
|
||||
pango_layout_set_text(drw->font->layout, buf, -1);
|
||||
ty = y + (int)(h - eh) / 2;
|
||||
if (ty < y) ty = y;
|
||||
pango_xft_render_layout(d, &drw->scheme[invert ? ColBg : ColFg],
|
||||
drw->font->layout, x * PANGO_SCALE, ty * PANGO_SCALE);
|
||||
if (markup)
|
||||
pango_layout_set_attributes(drw->font->layout, NULL);
|
||||
}
|
||||
x += (int)ew;
|
||||
}
|
||||
|
||||
if (d) XftDrawDestroy(d);
|
||||
return x + (render ? (int)w : 0);
|
||||
}
|
||||
|
||||
void drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h) {
|
||||
if (!drw) return;
|
||||
XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
|
||||
XSync(drw->dpy, False);
|
||||
}
|
||||
|
||||
unsigned int drw_font_getwidth(Drw *drw, const char *text, Bool markup) {
|
||||
unsigned int w = 0, h = 0;
|
||||
if (!drw || !drw->font || !text || !text[0]) return 0;
|
||||
drw_font_getexts(drw->font, text, (unsigned int)strlen(text), &w, &h, markup);
|
||||
return w;
|
||||
}
|
||||
|
||||
void drw_font_getexts(Fnt *font, const char *text, unsigned int len,
|
||||
unsigned int *w, unsigned int *h, Bool markup) {
|
||||
PangoRectangle r;
|
||||
if (!font || !text) {
|
||||
if (w) *w = 0;
|
||||
if (h) *h = 0;
|
||||
return;
|
||||
}
|
||||
if (markup)
|
||||
pango_layout_set_markup(font->layout, text, (int)len);
|
||||
else
|
||||
pango_layout_set_text(font->layout, text, (int)len);
|
||||
pango_layout_get_extents(font->layout, NULL, &r);
|
||||
if (markup)
|
||||
pango_layout_set_attributes(font->layout, NULL);
|
||||
if (w) *w = (unsigned int)((r.width + PANGO_SCALE - 1) / PANGO_SCALE);
|
||||
if (h) *h = (unsigned int)((r.height + PANGO_SCALE - 1) / PANGO_SCALE);
|
||||
if (h && *h == 0) *h = font->h;
|
||||
}
|
||||
|
||||
Cur *drw_cur_create(Drw *drw, int shape) {
|
||||
Cur *cur;
|
||||
if (!drw) return NULL;
|
||||
cur = ecalloc(1, sizeof(Cur));
|
||||
cur->cursor = XCreateFontCursor(drw->dpy, shape);
|
||||
return cur;
|
||||
}
|
||||
|
||||
void drw_cur_free(Drw *drw, Cur *cursor) {
|
||||
if (!cursor) return;
|
||||
if (drw) XFreeCursor(drw->dpy, cursor->cursor);
|
||||
free(cursor);
|
||||
}
|
||||
Reference in new issue
Block a user