/*
* _NET_WM_ICON helpers for slide puzzle game
- * Copyright © 2022 Nick Bowler
+ * Copyright © 2022-2023 Nick Bowler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <stdint.h>
#if !X_DISPLAY_MISSING
# include <X11/Intrinsic.h>
enum { ICON_16x16, ICON_24x24, ICON_32x32, ICON_48x48, ICON_MAX };
-/*
- * Allocate storage for the EWMH _NET_WM_ICON array. The sizes array is
- * populated with pointers to the beginning of each icon's pixel data. For
- * example, sizes[ICON_24x24] points to the first pixel of the 24x24 image.
- *
- * The returned value can then be passed to XChangeProperty to set the icon,
- * (use EWMH_ICON_NELEM for the number of elements) and must be freed by the
- * caller.
- */
-static void *ewmh_icon_alloc(unsigned long **sizes)
+static unsigned long scale16to8(uint_least32_t x)
{
- unsigned long *buf;
-
- buf = calloc(sizeof *buf, EWMH_ICON_NELEM);
- if (buf) {
- sizes[ICON_16x16] = buf;
- *sizes[ICON_16x16]++ = 16;
- *sizes[ICON_16x16]++ = 16;
-
- sizes[ICON_24x24] = sizes[ICON_16x16] + 16*16;
- *sizes[ICON_24x24]++ = 24;
- *sizes[ICON_24x24]++ = 24;
+ return x*0xff / 0xffff;
+}
- sizes[ICON_32x32] = sizes[ICON_24x24] + 24*24;
- *sizes[ICON_32x32]++ = 32;
- *sizes[ICON_32x32]++ = 32;
+static unsigned long wm_pixel(const XColor *c)
+{
+ uint_least32_t t = -1;
- sizes[ICON_48x48] = sizes[ICON_32x32] + 32*32;
- *sizes[ICON_48x48]++ = 48;
- *sizes[ICON_48x48]++ = 48;
- }
+ t = (t << 8) + scale16to8(c->red);
+ t = (t << 8) + scale16to8(c->green);
+ t = (t << 8) + scale16to8(c->blue);
- return buf;
+ return t & 0xffffffff;
}
-static unsigned long scale16to8(unsigned x)
+void ewmh_icon_prepare_cmap(XColor *map, unsigned n)
{
- return x*0xfful / 0xffff;
-}
+ unsigned i;
-static unsigned long wm_pixel(const XColor *c)
-{
- return 0xff000000
- | scale16to8(c->red) << 16
- | scale16to8(c->green) << 8
- | scale16to8(c->blue);
+ for (i = 0; i < n; i++) {
+ map[i].pixel = wm_pixel(&map[i]);
+ }
}
-static void do_remap(void *icon, unsigned size, const XColor *map)
+static void do_remap(void *icon, unsigned area, const XColor *map)
{
unsigned char *index = icon;
unsigned long *argb = icon;
- unsigned i, count = size*size;
+ unsigned i;
- for (i = 0; i < count; i++) {
- argb[count-i-1] = wm_pixel(map + index[count-i-1]);
+ area--;
+ for (i = 0; i <= area; i++) {
+ argb[area-i] = (map + index[area-i])->pixel;
}
}
void *ewmh_icon_generate(const unsigned long *seq, const XColor *map)
{
- unsigned long *buf, *sizes[ICON_MAX];
- unsigned i, j;
+ static const unsigned char dims[ICON_MAX] = { 16, 24, 32, 48 };
+ unsigned long *work, *ret;
+ unsigned i, j, fullsize, size;
- buf = ewmh_icon_alloc(sizes);
- if (!buf)
+ work = ret = malloc(sizeof *work * EWMH_ICON_NELEM);
+ if (!work)
return NULL;
- for (i = 0; i < 9; i++) {
- unsigned x = i%3, y = i/3;
+ for (i = fullsize = 0; i < ICON_MAX; i++) {
+ work += fullsize;
- icon_tile16((void *)sizes[ICON_16x16], seq[i], x, y);
- icon_tile24((void *)sizes[ICON_24x24], seq[i], x, y);
- icon_tile32((void *)sizes[ICON_32x32], seq[i], x, y);
- icon_tile48((void *)sizes[ICON_48x48], seq[i], x, y);
- }
+ size = dims[i];
+ *work++ = size;
+ *work++ = size;
+
+ for (j = 0; j < 9; j++) {
+ unsigned x = j%3, y = j/3;
- for (i = 0; i < ICON_MAX; i++) {
- /* Produces the sequence 16, 24, 32, 48 */
- unsigned size = 16 + (1u<<i)/2 * 8;
+ switch (i) {
+ case 0: icon_tile16((void *)work, seq[j], x, y); break;
+ case 1: icon_tile24((void *)work, seq[j], x, y); break;
+ case 2: icon_tile32((void *)work, seq[j], x, y); break;
+ case 3: icon_tile48((void *)work, seq[j], x, y); break;
+ }
+ }
- do_remap(sizes[i], size, map);
+ fullsize = size*size;
+ do_remap(work, fullsize, map);
}
- return buf;
+ return ret;
}
#if !X_DISPLAY_MISSING