]> git.draconx.ca Git - rrace.git/blobdiff - src/ewmhicon.c
Performance improvements for runtime icon generation.
[rrace.git] / src / ewmhicon.c
index 6bdf8e90f7de55a949f4d98c7dc6e5d4025630b1..7e69ec883f5c36317c900abff0a947e03b149d8c 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * _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 <X11/Intrinsic.h>
 #  include <X11/Xatom.h>
 #endif
+
 #include "ewmhicon.h"
 #include "colour.h"
+#include "icon.h"
+
+enum { ICON_16x16, ICON_24x24, ICON_32x32, ICON_48x48, ICON_MAX };
 
 static unsigned long scale16to8(unsigned x)
 {
@@ -41,117 +45,51 @@ static unsigned long wm_pixel(const XColor *c)
             | scale16to8(c->blue);
 }
 
-static void format_row(unsigned long *row, const XColor *c,
-                       unsigned s, unsigned y, unsigned w, unsigned h)
+static void do_remap(void *icon, unsigned area, const XColor *map)
 {
-       unsigned x;
-
-       for (x = 0; x < w; x++) {
-               row[x] = wm_pixel(&c[COLOUR_PRIMARY]);
-               if (x < s || y < s)
-                       row[x] = wm_pixel(&c[COLOUR_LIGHT]);
-               if ((x+s >= w && x+y >= w) || (y+s >= h && x+y >= h))
-                       row[x] = wm_pixel(&c[COLOUR_DARK]);
-       }
-}
-
-/*
- * The 16x16 icon is drawn with 1px shadow, 6x6 tiles, with 1 pixel cropped off
- * all the edge tiles
- */
-void ewmh_tile16(unsigned long *icon, const XColor *c, int tile_x, int tile_y)
-{
-       int out_x, out_y, out_w, y;
-       unsigned long row[6];
-
-       out_x = tile_x * 11 / 2;
-       out_y = tile_y * 11 / 2;
-       out_w = (5 + (tile_x == 1)) * sizeof row[0];
+       unsigned char *index = icon;
+       unsigned long *argb = icon;
+       unsigned i;
 
-       for (y = 0+(tile_y == 0); y < 6-(tile_y == 2); y++) {
-               format_row(row, c, 1, y, 6, 6);
-               memcpy(&icon[16 * out_y++ + out_x], &row[tile_x == 0], out_w);
+       area--;
+       for (i = 0; i <= area; i++) {
+               argb[area-i] = wm_pixel(map + index[area-i]);
        }
 }
 
-/*
- * The 24x24 icon is drawn with 1px shadow and 8x8 tiles.
- */
-void ewmh_tile24(unsigned long *icon, const XColor *c, int tile_x, int tile_y)
+void *ewmh_icon_generate(const unsigned long *seq, const XColor *map)
 {
-       int out_x, out_y, y;
-       unsigned long row[8];
+       static const unsigned char dims[ICON_MAX] = { 16, 24, 32, 48 };
+       unsigned long *work, *ret;
+       unsigned i, j, fullsize, size;
 
-       out_x = tile_x * 8;
-       out_y = tile_y * 8;
+       work = ret = malloc(sizeof *work * EWMH_ICON_NELEM);
+       if (!work)
+               return NULL;
 
-       for (y = 0; y < 8; y++) {
-               format_row(row, c, 1, y, 8, 8);
-               memcpy(&icon[24 * out_y++ + out_x], row, sizeof row);
-       }
-}
+       for (i = fullsize = 0; i < ICON_MAX; i++) {
+               work += fullsize;
 
-/*
- * The 32x32 icon is drawn with 1px shadow with slightly uneven tiles on
- * an 11-10-11 pixel grid.
- */
-void ewmh_tile32(unsigned long *icon, const XColor *c, int tile_x, int tile_y)
-{
-       int out_x, out_y, out_w, out_h, y;
-       unsigned long row[11];
+               size = dims[i];
+               *work++ = size;
+               *work++ = size;
 
-       out_x = 10*tile_x + (tile_x > 0);
-       out_y = 10*tile_y + (tile_y > 0);
-       out_w = 10 + (tile_x != 1);
-       out_h = 10 + (tile_y != 1);
+               for (j = 0; j < 9; j++) {
+                       unsigned x = j%3, y = j/3;
 
-       for (y = 0; y < out_h; y++) {
-               format_row(row, c, 1, y, out_w, out_h);
-               memcpy(&icon[32 * out_y++ + out_x], row, out_w * sizeof row[0]);
-       }
-}
-
-/*
- * The 48x48 icon is drawn with 2px shadow and 16x16 tiles.
- */
-void ewmh_tile48(unsigned long *icon, const XColor *c, int tile_x, int tile_y)
-{
-       int out_x, out_y, y;
-       unsigned long row[16];
-
-       out_x = tile_x * 16;
-       out_y = tile_y * 16;
-
-       for (y = 0; y < 16; y++) {
-               format_row(row, c, 2, y, 16, 16);
-               memcpy(&icon[48 * out_y++ + out_x], row, sizeof row);
-       }
-}
-
-void *ewmh_icon_alloc(unsigned long **sizes)
-{
-       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;
-
-               sizes[ICON_32x32] = sizes[ICON_24x24] + 24*24;
-               *sizes[ICON_32x32]++ = 32;
-               *sizes[ICON_32x32]++ = 32;
+                       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;
+                       }
+               }
 
-               sizes[ICON_48x48] = sizes[ICON_32x32] + 32*32;
-               *sizes[ICON_48x48]++ = 48;
-               *sizes[ICON_48x48]++ = 48;
+               fullsize = size*size;
+               do_remap(work, fullsize, map);
        }
 
-       return buf;
+       return ret;
 }
 
 #if !X_DISPLAY_MISSING
@@ -187,9 +125,11 @@ int ewmh_probe_wm_icon(Widget shell)
                props = (void *)prop_return;
                for (i = 0; i < nitems; i++) {
                        if (props[i] == net_wm_icon) {
+                               XFree(props);
                                return 1;
                        }
                }
+               XFree(props);
        } while (nitems > 0 && bytes_after > 0);
 
        return 0;