[wiki] [sites] [st][patch][background_image] Introducing new background_image patch for st || Matthias Schoth

From: <git_AT_suckless.org>
Date: Sat, 24 Apr 2021 22:18:28 +0200

commit fc831cb2f1a6aaa330fb2be31954733a54b8a673
Author: Matthias Schoth <mschoth_AT_gmail.com>
Date: Sat Apr 24 22:14:54 2021 +0200

    [st][patch][background_image] Introducing new background_image patch for st

diff --git a/st.suckless.org/patches/background_image/index.md b/st.suckless.org/patches/background_image/index.md
new file mode 100644
index 00000000..e5956112
--- /dev/null
+++ b/st.suckless.org/patches/background_image/index.md
_AT_@ -0,0 +1,40 @@
+background image
+================
+
+Description
+-----------
+
+Draws a background image in place of the defaultbg color.
+
+Notes
+-----
+
+The path to the image file has to be configured in `config.h` using the variable
+`bgimg` (patch modifies `config.def.h`). The image format is expected to be
+[farbfeld](//tools.suckless.org/farbfeld). In case the background image
+is smaller than the window size the background will be tiled.
+
+Pseudo Transparency
+-------------------
+
+The variable `pseudotransparency` enables functionality which fixes the
+coordinates of the background image to the screen origin. This emulates the
+effect of transparency without the need for an *X composite manager*.
+
+*Hint*: With the use of [farbfeld utilities](http://zzo38computer.org/fossil/farbfeld.ui/)
+effects can be applied to the desktop background in an automated fashion.
+Pictured below is an example of the result of a darken and blur operation
+invoked with the following command:
+
+ cat wallpaper.jpg | jpg2ff | ff-border e 50 | ff-bright rgba 0 0.5 1 | ff-blur 50 15 > st_wallpaper.ff
+
+![Screenshot](pseudo-transparency.png)
+
+Download
+--------
+* [st-background-image-0.8.4.diff](st-background-image-0.8.4.diff)
+
+Authors
+-------
+* Matthias Schoth - <mschoth_AT_gmail.com>
+
diff --git a/st.suckless.org/patches/background_image/pseudo-transparency.png b/st.suckless.org/patches/background_image/pseudo-transparency.png
new file mode 100644
index 00000000..2272658d
Binary files /dev/null and b/st.suckless.org/patches/background_image/pseudo-transparency.png differ
diff --git a/st.suckless.org/patches/background_image/st-background-image-0.8.4.diff b/st.suckless.org/patches/background_image/st-background-image-0.8.4.diff
new file mode 100644
index 00000000..6bbabc54
--- /dev/null
+++ b/st.suckless.org/patches/background_image/st-background-image-0.8.4.diff
_AT_@ -0,0 +1,206 @@
+From a92db07df3cd81ee51ac9650b65adb8685e52187 Mon Sep 17 00:00:00 2001
+From: Matthias Schoth <mschoth_AT_gmail.com>
+Date: Sat, 24 Apr 2021 21:24:41 +0200
+Subject: [PATCH] Implements background image and pseudo transparancy support.
+
+---
+ config.def.h | 8 +++++
+ x.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++----
+ 2 files changed, 95 insertions(+), 6 deletions(-)
+
+diff --git a/config.def.h b/config.def.h
+index 6f05dce..3d352db 100644
+--- a/config.def.h
++++ b/config.def.h
+_AT_@ -8,6 +8,14 @@
+ static char *font = "Liberation Mono:pixelsize=12:antialias=true:autohint=true";
+ static int borderpx = 2;
+
++/*
++ * background image
++ * expects farbfeld format
++ * pseudo transparency fixes coordinates to the screen origin
++ */
++static const char *bgfile = "/path/to/image.ff";
++static const int pseudotransparency = 0;
++
+ /*
+ * What program is execed by st depends of these precedence rules:
+ * 1: program passed with -e
+diff --git a/x.c b/x.c
+index 210f184..400cceb 100644
+--- a/x.c
++++ b/x.c
+_AT_@ -14,6 +14,7 @@
+ #include <X11/keysym.h>
+ #include <X11/Xft/Xft.h>
+ #include <X11/XKBlib.h>
++#include <arpa/inet.h>
+
+ char *argv0;
+ #include "arg.h"
+_AT_@ -81,6 +82,7 @@ typedef XftGlyphFontSpec GlyphFontSpec;
+ typedef struct {
+ int tw, th; /* tty width and height */
+ int w, h; /* window width and height */
++ int x, y; /* window location */
+ int ch; /* char height */
+ int cw; /* char width */
+ int mode; /* window state/mode flags */
+_AT_@ -101,6 +103,9 @@ typedef struct {
+ XVaNestedList spotlist;
+ } ime;
+ Draw draw;
++ Drawable xftdraw; /* buffer that xft draws to */
++ Drawable bgimg; /* background image */
++ GC bggc; /* Graphics Context for background */
+ Visual *vis;
+ XSetWindowAttributes attrs;
+ int scr;
+_AT_@ -151,6 +156,7 @@ static void ximinstantiate(Display *, XPointer, XPointer);
+ static void ximdestroy(XIM, XPointer, XPointer);
+ static int xicdestroy(XIC, XPointer, XPointer);
+ static void xinit(int, int);
++static void bginit();
+ static void cresize(int, int);
+ static void xresize(int, int);
+ static void xhints(void);
+_AT_@ -736,6 +742,7 @@ xresize(int col, int row)
+ xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
+ DefaultDepth(xw.dpy, xw.scr));
+ XftDrawChange(xw.draw, xw.buf);
++ xw.xftdraw = XftDrawDrawable(xw.draw);
+ xclear(0, 0, win.w, win.h);
+
+ /* resize to new width */
+_AT_@ -820,9 +827,9 @@ xsetcolorname(int x, const char *name)
+ void
+ xclear(int x1, int y1, int x2, int y2)
+ {
+- XftDrawRect(xw.draw,
+- &dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg],
+- x1, y1, x2-x1, y2-y1);
++ if (pseudotransparency)
++ XSetTSOrigin(xw.dpy, xw.bggc, -win.x, -win.y);
++ XFillRectangle(xw.dpy, xw.xftdraw, xw.bggc, x1, y1, x2-x1, y2-y1);
+ }
+
+ void
+_AT_@ -1158,6 +1165,7 @@ xinit(int cols, int rows)
+
+ /* Xft rendering context */
+ xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
++ xw.xftdraw = XftDrawDrawable(xw.draw);
+
+ /* input methods */
+ if (!ximopen(xw.dpy)) {
+_AT_@ -1207,6 +1215,64 @@ xinit(int cols, int rows)
+ xsel.xtarget = XA_STRING;
+ }
+
++/*
++ * initialize background image
++ */
++void
++bginit()
++{
++ uint32_t hdr[4], bgw, bgh, i = 0;
++ char buf[8], *image32;
++ FILE *bgf = fopen(bgfile, "rb");
++ XGCValues gcvalues;
++ XImage *bgxi;
++
++ if (bgf == NULL) die("could not load background image.
");
++
++ if (fread(hdr, sizeof(*hdr), LEN(hdr), bgf) != LEN(hdr))
++ if (ferror(bgf))
++ die("fread:");
++ else
++ die("fread: Unexpected end of file
");
++
++ if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1))
++ die("Invalid magic value");
++
++ bgw = ntohl(hdr[2]);
++ bgh = ntohl(hdr[3]);
++ image32 = (char *)malloc(bgw * bgh * 4 * sizeof(char));
++
++ while (i < bgh * bgw * 4) {
++ if (fread(buf, sizeof(*buf), LEN(buf), bgf) != LEN(buf))
++ if (ferror(bgf))
++ die("fread:");
++ else
++ die("fread: Unexpected end of file");
++
++ image32[i++] = buf[4]; /* convert 16 bit RGBA to 8 bit BGRA */
++ image32[i++] = buf[2];
++ image32[i++] = buf[0];
++ image32[i++] = buf[6];
++ }
++
++ bgxi = XCreateImage(xw.dpy, DefaultVisual(xw.dpy, xw.scr),
++ 24, ZPixmap, 0, image32, bgw, bgh, 32, 0);
++ xw.bgimg = XCreatePixmap(xw.dpy, xw.win, bgw, bgh,
++ DefaultDepth(xw.dpy, xw.scr));
++ XPutImage(xw.dpy, xw.bgimg, dc.gc, bgxi, 0, 0, 0, 0, bgw, bgh);
++ XDestroyImage(bgxi);
++ memset(&gcvalues, 0, sizeof(gcvalues));
++ xw.bggc = XCreateGC(xw.dpy, xw.win, 0, &gcvalues);
++ XSetTile(xw.dpy, xw.bggc, xw.bgimg);
++ XSetFillStyle(xw.dpy, xw.bggc, FillTiled);
++ if (pseudotransparency) {
++ XWindowAttributes xwa;
++ XGetWindowAttributes(xw.dpy, xw.win, &xwa);
++ win.x = xwa.x;
++ win.y = xwa.y;
++ }
++}
++
+ int
+ xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x, int y)
+ {
+_AT_@ -1447,7 +1513,10 @@ xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, i
+ xclear(winx, winy + win.ch, winx + width, win.h);
+
+ /* Clean up the region we want to draw to. */
+- XftDrawRect(xw.draw, bg, winx, winy, width, win.ch);
++ if (bg == &dc.col[defaultbg])
++ xclear(winx, winy, winx + width, winy + win.ch);
++ else
++ XftDrawRect(xw.draw, bg, winx, winy, width, win.ch);
+
+ /* Set the clip region because Xft is sometimes dirty. */
+ r.x = 0;
+_AT_@ -1855,8 +1924,19 @@ cmessage(XEvent *e)
+ void
+ resize(XEvent *e)
+ {
+- if (e->xconfigure.width == win.w && e->xconfigure.height == win.h)
+- return;
++ if (pseudotransparency) {
++ if (e->xconfigure.width == win.w &&
++ e->xconfigure.height == win.h &&
++ e->xconfigure.x == win.x && e->xconfigure.y == win.y)
++ return;
++
++ win.x = e->xconfigure.x;
++ win.y = e->xconfigure.y;
++ } else {
++ if (e->xconfigure.width == win.w &&
++ e->xconfigure.height == win.h)
++ return;
++ }
+
+ cresize(e->xconfigure.width, e->xconfigure.height);
+ }
+_AT_@ -2041,6 +2121,7 @@ run:
+ rows = MAX(rows, 1);
+ tnew(cols, rows);
+ xinit(cols, rows);
++ bginit();
+ xsetenv();
+ selinit();
+ run();
+--
+2.31.1
+
Received on Sat Apr 24 2021 - 22:18:28 CEST

This archive was generated by hypermail 2.3.0 : Sat Apr 24 2021 - 22:24:52 CEST