chamesle

chamesle.org: the site of Chamesle, a small Linux distribution
git clone https://github.com/tortr-rs/chamesle
Log | Files | Refs | README

ttywmbg.c (7294B)


      1 /* ttywmbg - a picture or a colour behind the windows
      2  *
      3  *   ttywmbg ~/pictures/sea.png [fill|fit|centre|tile]
      4  *   ttywmbg '#000000'   or   ttywmbg black
      5  *
      6  * png and jpeg. it stays after ttywmbg is gone, and programs that look for the
      7  * wallpaper (_XROOTPMAP_ID) find it */
      8 #include <ctype.h>
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 #include <string.h>
     12 #include <strings.h>
     13 #include <setjmp.h>
     14 #include <X11/Xlib.h>
     15 #include <X11/Xatom.h>
     16 #include <X11/Xutil.h>
     17 #include <png.h>
     18 #include <jpeglib.h>
     19 
     20 #define MIN(A, B)       ((A) < (B) ? (A) : (B))
     21 #define MAX(A, B)       ((A) > (B) ? (A) : (B))
     22 
     23 enum { Fill, Fit, Centre, Tile };
     24 
     25 static Display *dpy;
     26 static int scr, sw, sh;
     27 
     28 static void
     29 die(const char *msg)
     30 {
     31 	fprintf(stderr, "ttywmbg: %s\n", msg);
     32 	exit(1);
     33 }
     34 
     35 static int
     36 parsecolour(const char *name, unsigned int *rgb)
     37 {
     38 	static const char *names[][2] = {
     39 		{ "black", "000000" }, { "blue", "0000aa" }, { "green", "00aa00" }, { "cyan", "00aaaa" },
     40 		{ "red", "aa0000" }, { "magenta", "aa00aa" }, { "brown", "aa5500" }, { "grey", "aaaaaa" },
     41 		{ "gray", "aaaaaa" }, { "darkgrey", "555555" }, { "darkgray", "555555" },
     42 		{ "lightblue", "5555ff" }, { "lightgreen", "55ff55" }, { "lightcyan", "55ffff" },
     43 		{ "lightred", "ff5555" }, { "pink", "ff55ff" }, { "yellow", "ffff55" }, { "white", "ffffff" },
     44 	};
     45 	char s[32];
     46 	size_t i, n = 0;
     47 
     48 	for (; *name && n < sizeof s - 1; name++)
     49 		if (!isspace((unsigned char)*name))
     50 			s[n++] = tolower((unsigned char)*name);
     51 	s[n] = '\0';
     52 	for (i = 0; i < sizeof names / sizeof names[0]; i++)
     53 		if (!strcmp(s, names[i][0]))
     54 			return sscanf(names[i][1], "%x", rgb) == 1;
     55 	return s[0] == '#' && strlen(s) == 7 && sscanf(s + 1, "%x", rgb) == 1;
     56 }
     57 
     58 /* ---- pictures, into 0xRRGGBB ---- */
     59 
     60 static unsigned int *
     61 loadpng(const char *path, int *w, int *h)
     62 {
     63 	png_image pi;
     64 	unsigned int *px;
     65 
     66 	memset(&pi, 0, sizeof pi);
     67 	pi.version = PNG_IMAGE_VERSION;
     68 	if (!png_image_begin_read_from_file(&pi, path))
     69 		return NULL;
     70 	pi.format = PNG_FORMAT_BGRA;
     71 	if (!(px = malloc(PNG_IMAGE_SIZE(pi))) || !png_image_finish_read(&pi, NULL, px, 0, NULL)) {
     72 		free(px);
     73 		png_image_free(&pi);
     74 		return NULL;
     75 	}
     76 	*w = pi.width, *h = pi.height;
     77 	return px;
     78 }
     79 
     80 struct jerr {
     81 	struct jpeg_error_mgr mgr;
     82 	jmp_buf jb;
     83 };
     84 
     85 static void
     86 jpegfail(j_common_ptr c)
     87 {
     88 	longjmp(((struct jerr *)c->err)->jb, 1);
     89 }
     90 
     91 static unsigned int *
     92 loadjpeg(const char *path, int *w, int *h)
     93 {
     94 	struct jpeg_decompress_struct c;
     95 	struct jerr e;
     96 	unsigned char *row = NULL;
     97 	unsigned int *px = NULL;
     98 	FILE *f;
     99 	int x, y;
    100 
    101 	if (!(f = fopen(path, "rb")))
    102 		return NULL;
    103 	c.err = jpeg_std_error(&e.mgr);
    104 	e.mgr.error_exit = jpegfail;
    105 	if (setjmp(e.jb)) {
    106 		jpeg_destroy_decompress(&c);
    107 		fclose(f);
    108 		free(row);
    109 		free(px);
    110 		return NULL;
    111 	}
    112 	jpeg_create_decompress(&c);
    113 	jpeg_stdio_src(&c, f);
    114 	jpeg_read_header(&c, TRUE);
    115 	c.out_color_space = JCS_RGB;
    116 	jpeg_start_decompress(&c);
    117 	*w = c.output_width, *h = c.output_height;
    118 	px = malloc((size_t)*w * *h * 4);
    119 	row = malloc((size_t)*w * 3);
    120 	for (y = 0; px && row && c.output_scanline < c.output_height; y++) {
    121 		jpeg_read_scanlines(&c, &row, 1);
    122 		for (x = 0; x < *w; x++)
    123 			px[y * *w + x] = row[x * 3] << 16 | row[x * 3 + 1] << 8 | row[x * 3 + 2];
    124 	}
    125 	jpeg_finish_decompress(&c);
    126 	jpeg_destroy_decompress(&c);
    127 	fclose(f);
    128 	free(row);
    129 	return px;
    130 }
    131 
    132 /* one pixel of the picture at (x, y) in its own size, averaged over the box it covers on screen */
    133 static unsigned int
    134 sample(const unsigned int *px, int w, int h, double x0, double y0, double x1, double y1)
    135 {
    136 	unsigned long r = 0, g = 0, b = 0, n = 0;
    137 	int x, y, xa = x0, ya = y0, xb = MAX(xa + 1, (int)x1), yb = MAX(ya + 1, (int)y1);
    138 	unsigned int p;
    139 
    140 	xb = MIN(xb, w), yb = MIN(yb, h);
    141 	/* no more than 4x4 samples: big pictures shrunk a lot still look smooth enough */
    142 	for (y = ya; y < yb; y += MAX(1, (yb - ya) / 4))
    143 		for (x = xa; x < xb; x += MAX(1, (xb - xa) / 4)) {
    144 			p = px[y * w + x];
    145 			r += p >> 16 & 255, g += p >> 8 & 255, b += p & 255, n++;
    146 		}
    147 	return n ? (r / n) << 16 | (g / n) << 8 | b / n : 0;
    148 }
    149 
    150 static void
    151 paint(unsigned int *out, const unsigned int *px, int w, int h, int how, unsigned int bg)
    152 {
    153 	double scale, ox, oy;
    154 	int x, y, dw, dh;
    155 
    156 	for (x = 0; x < sw * sh; x++)
    157 		out[x] = bg;
    158 	if (how == Tile) {
    159 		for (y = 0; y < sh; y++)
    160 			for (x = 0; x < sw; x++)
    161 				out[y * sw + x] = px[(y % h) * w + x % w];
    162 		return;
    163 	}
    164 	if (how == Centre)
    165 		scale = 1;
    166 	else if (how == Fit)
    167 		scale = MIN((double)sw / w, (double)sh / h);
    168 	else
    169 		scale = MAX((double)sw / w, (double)sh / h);
    170 	dw = w * scale, dh = h * scale;
    171 	ox = (sw - dw) / 2, oy = (sh - dh) / 2;
    172 	for (y = MAX(0, (int)oy); y < MIN(sh, (int)oy + dh); y++)
    173 		for (x = MAX(0, (int)ox); x < MIN(sw, (int)ox + dw); x++)
    174 			out[y * sw + x] = sample(px, w, h, (x - ox) / scale, (y - oy) / scale,
    175 			                         (x + 1 - ox) / scale, (y + 1 - oy) / scale);
    176 }
    177 
    178 /* ---- onto the root window ---- */
    179 
    180 static void
    181 setroot(Pixmap pm)
    182 {
    183 	Window root = RootWindow(dpy, scr);
    184 	Atom xroot = XInternAtom(dpy, "_XROOTPMAP_ID", False), eset = XInternAtom(dpy, "ESETROOT_PMAP_ID", False);
    185 	Atom type;
    186 	int fmt;
    187 	unsigned long n, after;
    188 	unsigned char *data;
    189 
    190 	/* the one before: whoever made it kept it on the server, so it goes now */
    191 	if (XGetWindowProperty(dpy, root, eset, 0, 1, False, XA_PIXMAP, &type, &fmt, &n, &after, &data) == Success && data) {
    192 		if (type == XA_PIXMAP && n == 1)
    193 			XKillClient(dpy, *(Pixmap *)data);
    194 		XFree(data);
    195 	}
    196 	XChangeProperty(dpy, root, xroot, XA_PIXMAP, 32, PropModeReplace, (unsigned char *)&pm, 1);
    197 	XChangeProperty(dpy, root, eset, XA_PIXMAP, 32, PropModeReplace, (unsigned char *)&pm, 1);
    198 	XSetWindowBackgroundPixmap(dpy, root, pm);
    199 	XClearWindow(dpy, root);
    200 }
    201 
    202 int
    203 main(int argc, char *argv[])
    204 {
    205 	unsigned int *px = NULL, *out, bg = 0;
    206 	const char *how = argc > 2 ? argv[2] : "fill", *ext;
    207 	XImage *xi;
    208 	Pixmap pm;
    209 	GC gc;
    210 	int w, h, mode;
    211 
    212 	if (argc < 2 || argc > 3)
    213 		die("usage: ttywmbg picture [fill|fit|centre|tile] | ttywmbg colour");
    214 	/* the wallpaper must outlive us: the server keeps it when we close */
    215 	if (!(dpy = XOpenDisplay(NULL)))
    216 		die("can't open the display");
    217 	XSetCloseDownMode(dpy, RetainPermanent);
    218 	scr = DefaultScreen(dpy);
    219 	sw = DisplayWidth(dpy, scr);
    220 	sh = DisplayHeight(dpy, scr);
    221 	if (DefaultDepth(dpy, scr) < 24)
    222 		die("needs a 24 bit screen");
    223 
    224 	mode = !strcmp(how, "fit") ? Fit : !strcmp(how, "centre") || !strcmp(how, "center") ? Centre :
    225 	       !strcmp(how, "tile") ? Tile : Fill;
    226 	if (!parsecolour(argv[1], &bg)) {
    227 		ext = strrchr(argv[1], '.');
    228 		if (ext && (!strcasecmp(ext, ".jpg") || !strcasecmp(ext, ".jpeg")))
    229 			px = loadjpeg(argv[1], &w, &h);
    230 		else
    231 			px = loadpng(argv[1], &w, &h);
    232 		if (!px && !(px = loadjpeg(argv[1], &w, &h)))
    233 			die("can't read the picture: png or jpeg only");
    234 		bg = 0;
    235 	}
    236 	if (!(out = malloc((size_t)sw * sh * 4)))
    237 		die("out of memory");
    238 	if (px)
    239 		paint(out, px, w, h, mode, bg);
    240 	else
    241 		for (w = 0; w < sw * sh; w++)
    242 			out[w] = bg;
    243 	free(px);
    244 
    245 	pm = XCreatePixmap(dpy, RootWindow(dpy, scr), sw, sh, DefaultDepth(dpy, scr));
    246 	gc = XCreateGC(dpy, pm, 0, NULL);
    247 	xi = XCreateImage(dpy, DefaultVisual(dpy, scr), DefaultDepth(dpy, scr), ZPixmap, 0, (char *)out, sw, sh, 32, 0);
    248 	XPutImage(dpy, pm, gc, xi, 0, 0, 0, 0, sw, sh);
    249 	XDestroyImage(xi);
    250 	XFreeGC(dpy, gc);
    251 	setroot(pm);
    252 	XCloseDisplay(dpy);
    253 	return 0;
    254 }