]> git.draconx.ca Git - rrace.git/blobdiff - t/ewmhicon.c
Implement window icons.
[rrace.git] / t / ewmhicon.c
diff --git a/t/ewmhicon.c b/t/ewmhicon.c
new file mode 100644 (file)
index 0000000..e088d41
--- /dev/null
@@ -0,0 +1,114 @@
+/*
+ * Test app for _NET_WM_ICON formatting.
+ * Copyright © 2022 Nick Bowler
+ *
+ * Use a fake colour scheme to generate an icon of the chosen size (16x16,
+ * 24x24, 32x32 or 48x48) and display the pixels as characters.
+ *
+ * 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <X11/Intrinsic.h>
+#include "motif.h"
+
+static const char *progname = "ewmhicon";
+
+static void print_usage(FILE *f)
+{
+       fprintf(f, "Usage: %s size\n", progname);
+}
+
+static const char sizes[][6] = {
+       "16x16", "24x24", "32x32", "48x48"
+};
+
+int to_size_enum(const char *arg)
+{
+       char buf[8];
+       unsigned i;
+
+       if (!strchr(arg, 'x')) {
+               sprintf(buf, "%.3sx%.3s", arg, arg);
+               arg = buf;
+       }
+
+       for (i = 0; i < sizeof sizes / sizeof sizes[0]; i++) {
+               if (!strcmp(arg, sizes[i]))
+                       return i;
+       }
+
+       return -1;
+}
+
+static unsigned long icon_buf[48*48];
+
+int main(int argc, char **argv)
+{
+       void (*tilefunc)(unsigned long *, const XColor *, int, int);
+       XColor c[COLOUR_MAX] = {0};
+       int size, x, y, w, h;
+
+       if (argc > 0)
+               progname = argv[0];
+
+       if (argc != 2) {
+               print_usage(stderr);
+               return EXIT_FAILURE;
+       }
+
+       size = to_size_enum(argv[1]);
+       if (size < 0) {
+               printf("%s: error: invalid size %s\n", progname, argv[1]);
+               return EXIT_FAILURE;
+       }
+
+       switch (size) {
+       case ICON_16x16: tilefunc = ewmh_tile16; w = h = 16; break;
+       case ICON_24x24: tilefunc = ewmh_tile24; w = h = 24; break;
+       case ICON_32x32: tilefunc = ewmh_tile32; w = h = 32; break;
+       case ICON_48x48: tilefunc = ewmh_tile48; w = h = 48; break;
+       default: assert(0);
+       }
+
+       c[COLOUR_PRIMARY].red = 0xffff;
+       c[COLOUR_DARK].green  = 0xffff;
+       c[COLOUR_LIGHT].blue  = 0xffff;
+
+       for (x = 0; x < 3; x++) {
+               for (y = 0; y < 3; y++) {
+                       tilefunc(icon_buf, c, x, y);
+               }
+       }
+
+       for (y = 0; y < h; y++) {
+               for (x = 0; x < w; x++) {
+                       unsigned long val = icon_buf[y*h+x];
+                       int c = ' ';
+
+                       if      (val == 0xffff0000) c = '.'; // primary
+                       else if (val == 0xff00ff00) c = '%'; // dark
+                       else if (val == 0xff0000ff) c = '+'; // light
+
+                       putchar(c);
+               }
+               putchar('\n');
+       }
+
+       return 0;
+}