]> git.draconx.ca Git - rrace.git/blob - t/ewmhicon.c
Implement window icons.
[rrace.git] / t / ewmhicon.c
1 /*
2  * Test app for _NET_WM_ICON formatting.
3  * Copyright © 2022 Nick Bowler
4  *
5  * Use a fake colour scheme to generate an icon of the chosen size (16x16,
6  * 24x24, 32x32 or 48x48) and display the pixels as characters.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20  */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <X11/Intrinsic.h>
28 #include "motif.h"
29
30 static const char *progname = "ewmhicon";
31
32 static void print_usage(FILE *f)
33 {
34         fprintf(f, "Usage: %s size\n", progname);
35 }
36
37 static const char sizes[][6] = {
38         "16x16", "24x24", "32x32", "48x48"
39 };
40
41 int to_size_enum(const char *arg)
42 {
43         char buf[8];
44         unsigned i;
45
46         if (!strchr(arg, 'x')) {
47                 sprintf(buf, "%.3sx%.3s", arg, arg);
48                 arg = buf;
49         }
50
51         for (i = 0; i < sizeof sizes / sizeof sizes[0]; i++) {
52                 if (!strcmp(arg, sizes[i]))
53                         return i;
54         }
55
56         return -1;
57 }
58
59 static unsigned long icon_buf[48*48];
60
61 int main(int argc, char **argv)
62 {
63         void (*tilefunc)(unsigned long *, const XColor *, int, int);
64         XColor c[COLOUR_MAX] = {0};
65         int size, x, y, w, h;
66
67         if (argc > 0)
68                 progname = argv[0];
69
70         if (argc != 2) {
71                 print_usage(stderr);
72                 return EXIT_FAILURE;
73         }
74
75         size = to_size_enum(argv[1]);
76         if (size < 0) {
77                 printf("%s: error: invalid size %s\n", progname, argv[1]);
78                 return EXIT_FAILURE;
79         }
80
81         switch (size) {
82         case ICON_16x16: tilefunc = ewmh_tile16; w = h = 16; break;
83         case ICON_24x24: tilefunc = ewmh_tile24; w = h = 24; break;
84         case ICON_32x32: tilefunc = ewmh_tile32; w = h = 32; break;
85         case ICON_48x48: tilefunc = ewmh_tile48; w = h = 48; break;
86         default: assert(0);
87         }
88
89         c[COLOUR_PRIMARY].red = 0xffff;
90         c[COLOUR_DARK].green  = 0xffff;
91         c[COLOUR_LIGHT].blue  = 0xffff;
92
93         for (x = 0; x < 3; x++) {
94                 for (y = 0; y < 3; y++) {
95                         tilefunc(icon_buf, c, x, y);
96                 }
97         }
98
99         for (y = 0; y < h; y++) {
100                 for (x = 0; x < w; x++) {
101                         unsigned long val = icon_buf[y*h+x];
102                         int c = ' ';
103
104                         if      (val == 0xffff0000) c = '.'; // primary
105                         else if (val == 0xff00ff00) c = '%'; // dark
106                         else if (val == 0xff0000ff) c = '+'; // light
107
108                         putchar(c);
109                 }
110                 putchar('\n');
111         }
112
113         return 0;
114 }