]> git.draconx.ca Git - rrace.git/blob - t/ewmhicon.c
Use XPM as icon test output format.
[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 <getopt.h>
28
29 #include "ewmhicon.h"
30 #include "help.h"
31
32 static const char *progname = "ewmhicon";
33 static const char sopts[] = "c:VH";
34 static const struct option lopts[] = {
35         { "colourseq", 1, NULL, 'c' },
36         { "version",   0, NULL, 'V' },
37         { "help",      0, NULL, 'H' },
38         { 0 }
39 };
40
41 #define S8TO16(x) ((x) * 0xfffful / 0xff)
42 #define RGB8(r, g, b) { \
43         0xff000000|(r ## ul)<<16|(g ## ul)<<8|(b ## ul), \
44         S8TO16(r), S8TO16(g), S8TO16(b) }
45
46 static const XColor colours[7][3] = {
47         {
48                 { 0xffff0000, 0xffff },
49                 { 0xff00ff00, 0, 0xffff },
50                 { 0xff0000ff, 0, 0, 0xffff }
51         },
52         { RGB8(0x8d,0x2e,0x28), RGB8(0x6a,0x1b,0x17), RGB8(0xa1,0x48,0x42) },
53         { RGB8(0xb4,0x6e,0x28), RGB8(0x92,0x4a,0x16), RGB8(0xc7,0x90,0x4f) },
54         { RGB8(0xd8,0xb7,0x40), RGB8(0xc5,0x9f,0x39), RGB8(0xe2,0xc6,0x5d) },
55         { RGB8(0x28,0x64,0x28), RGB8(0x19,0x47,0x19), RGB8(0x4e,0x87,0x4e) },
56         { RGB8(0x00,0x34,0x71), RGB8(0x00,0x1f,0x4f), RGB8(0x00,0x52,0x8b) },
57         { RGB8(0xdc,0xdc,0xdc), RGB8(0xc0,0xc0,0xc0), RGB8(0xea,0xea,0xea) }
58 };
59
60 static void print_usage(FILE *f)
61 {
62         fprintf(f, "Usage: %s size\n", progname);
63         if (f != stdout)
64                 fprintf(f, "Try %s --help for more information.\n", progname);
65 }
66
67 static void print_help(void)
68 {
69         const struct option *opt;
70
71         print_usage(stdout);
72         putchar('\n');
73         puts("Options:");
74         for (opt = lopts; opt->name; opt++) {
75                 if (help_print_optstring(opt, "ARG", 20))
76                         putchar('\n');
77         }
78         putchar('\n');
79
80         printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
81 }
82
83 static void print_version(void)
84 {
85         printf("ewmhicon (%s) %s\n", PACKAGE, PACKAGE_VERSION);
86         printf("Copyright (C) 2022 Nick Bowler\n");
87         puts("License GPLv3+: GNU GPL version 3 or any later version");
88         puts("This is free software: you are free to change and redistribute it.");
89         puts("There is NO WARRANTY, to the extent permitted by law.");
90 }
91
92 static const char sizes[][6] = {
93         "16x16", "24x24", "32x32", "48x48"
94 };
95
96 static int to_size_enum(const char *arg)
97 {
98         char buf[8];
99         unsigned i;
100
101         if (!strchr(arg, 'x')) {
102                 sprintf(buf, "%.3sx%.3s", arg, arg);
103                 arg = buf;
104         }
105
106         for (i = 0; i < sizeof sizes / sizeof sizes[0]; i++) {
107                 if (!strcmp(arg, sizes[i]))
108                         return i;
109         }
110
111         return -1;
112 }
113
114 static void gen_icon(int size, const char *colourseq)
115 {
116         void (*tilefunc)(unsigned long *, const XColor *, int, int);
117         static unsigned long icon_buf[48*48];
118         const char colourchars[21] = ".%+"",Rr""-Oo""'Yy"":Gg"";Bb"" Ww";
119         unsigned i, j, x, y, w, h, n;
120
121         switch (size) {
122         case ICON_16x16: tilefunc = ewmh_tile16; w = h = 16; break;
123         case ICON_24x24: tilefunc = ewmh_tile24; w = h = 24; break;
124         case ICON_32x32: tilefunc = ewmh_tile32; w = h = 32; break;
125         case ICON_48x48: tilefunc = ewmh_tile48; w = h = 48; break;
126         default: assert(0);
127         }
128
129         for (i = 0; i < 9; i++) {
130                 const XColor *c;
131
132                 assert(colourseq[i] >= '0' && colourseq[i] <= '6');
133                 c = colours[colourseq[i]-'0'];
134
135                 tilefunc(icon_buf, c, i%3, i/3);
136         }
137
138         n = 7;
139         for (i = 0; i < 7; i++)
140                 n -= !strchr(colourseq, '0'+i);
141
142         puts("/* XPM */");
143         puts("static char *icon[] = {");
144         printf("\"%u %u %u 1\",\n", w, h, 3*n);
145         for (i = 0; i < 7; i++) {
146                 if (!strchr(colourseq, '0'+i))
147                         continue;
148
149                 for (j = 0; j < 3; j++) {
150                         printf("\"%c c #%.6lx\",\n", colourchars[3*i+j],
151                                        colours[i][j].pixel & 0xffffff);
152                 }
153         }
154
155         for (y = 0; y < h; y++) {
156                 putchar('"');
157                 for (x = 0; x < w; x++) {
158                         unsigned long val = icon_buf[y*h+x];
159                         int c = '#';
160
161                         for (i = 0; i < sizeof colourchars; i++) {
162                                 if (colours[i/3][i%3].pixel == val) {
163                                         c = colourchars[i];
164                                         break;
165                                 }
166                         }
167
168                         putchar(c);
169                 }
170                 printf("\"%.*s\n", y+1<h, ",");
171         }
172         printf("};\n");
173 }
174
175 int decode_colourseq(char *out, const char *arg)
176 {
177         int i;
178
179         for (i = 0; i < 9; i++) {
180                 switch (arg[i]) {
181                 case 0: return 0;
182                 case 'R': case 'r': out[i] = '1'; break;
183                 case 'O': case 'o': out[i] = '2'; break;
184                 case 'Y': case 'y': out[i] = '3'; break;
185                 case 'G': case 'g': out[i] = '4'; break;
186                 case 'B': case 'b': out[i] = '5'; break;
187                 case 'W': case 'w': out[i] = '6'; break;
188                 case '1': case '2': case '3': case '4': case '5': case '6':
189                         case '0': out[i] = arg[i]; break;
190                 default: goto err;
191                 }
192         }
193
194         if (arg[i]) {
195 err:
196                 fprintf(stderr, "%s: error: invalid colour sequence '%s'\n",
197                                 progname, arg);
198                 return -1;
199         }
200
201         return 0;
202 }
203
204 int main(int argc, char **argv)
205 {
206         char colourseq[9] = "000000000";
207         int opt, size;
208
209         if (argc > 0)
210                 progname = argv[0];
211
212         while ((opt = getopt_long(argc, argv, sopts, lopts, 0)) != -1) {
213                 switch (opt) {
214                 case 'c':
215                         if (decode_colourseq(colourseq, optarg) != 0)
216                                 return EXIT_FAILURE;
217                         break;
218                 case 'H':
219                         print_help();
220                         return EXIT_SUCCESS;
221                 case 'V':
222                         print_version();
223                         return EXIT_SUCCESS;
224                 default:
225                         print_usage(stderr);
226                         return EXIT_FAILURE;
227                 }
228         }
229
230         if (argc != optind+1) {
231                 printf("%s: error: size not specified\n", progname);
232                 print_usage(stderr);
233                 return EXIT_FAILURE;
234         }
235
236         size = to_size_enum(argv[optind]);
237         if (size < 0) {
238                 printf("%s: error: invalid size %s\n", progname, argv[optind]);
239                 print_usage(stderr);
240                 return EXIT_FAILURE;
241         }
242
243         gen_icon(size, colourseq);
244         return 0;
245 }