2 * Test app for _NET_WM_ICON formatting.
3 * Copyright © 2022 Nick Bowler
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.
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.
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.
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/>.
33 static const char *progname = "ewmhicon";
34 static const char sopts[] = "c:VH";
35 static const struct option lopts[] = {
36 { "colourseq", 1, NULL, 'c' },
37 { "version", 0, NULL, 'V' },
38 { "help", 0, NULL, 'H' },
42 #define PASTE(a, b) a ## b
45 #define S8TO16(x) ((x) * 0xfffful / 0xff)
47 #define RGB8_(r, g, b) { \
48 0xff000000 | (r << 16) | (g << 8) | b, \
49 S8TO16(r), S8TO16(g), S8TO16(b) }
51 #define RGB8(r, g, b) RGB8_(0x ## r ## ul, 0x ## g ## ul, 0x ## b ## ul)
53 #define COLOURTAB(n) { \
54 INDIR(RGB8 PASTE(COLOUR ## n, _PRIMARY_RGB_)), \
55 INDIR(RGB8 PASTE(COLOUR ## n, _DARK_RGB_)), \
56 INDIR(RGB8 PASTE(COLOUR ## n, _LIGHT_RGB_)) }
58 static const XColor colours[7][3] = {
60 { 0xffff0000, 0xffff },
61 { 0xff00ff00, 0, 0xffff },
62 { 0xff0000ff, 0, 0, 0xffff }
64 COLOURTAB(0), COLOURTAB(1), COLOURTAB(2),
65 COLOURTAB(3), COLOURTAB(4), COLOURTAB(5)
68 static void print_usage(FILE *f)
70 fprintf(f, "Usage: %s size\n", progname);
72 fprintf(f, "Try %s --help for more information.\n", progname);
75 static void print_help(void)
77 const struct option *opt;
82 for (opt = lopts; opt->name; opt++) {
83 if (help_print_optstring(opt, "ARG", 20))
88 printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
91 static void print_version(void)
93 printf("ewmhicon (%s) %s\n", PACKAGE, PACKAGE_VERSION);
94 printf("Copyright (C) 2022 Nick Bowler\n");
95 puts("License GPLv3+: GNU GPL version 3 or any later version");
96 puts("This is free software: you are free to change and redistribute it.");
97 puts("There is NO WARRANTY, to the extent permitted by law.");
100 static const char sizes[][6] = {
101 "16x16", "24x24", "32x32", "48x48"
104 static int to_size_enum(const char *arg)
109 if (!strchr(arg, 'x')) {
110 sprintf(buf, "%.3sx%.3s", arg, arg);
114 for (i = 0; i < sizeof sizes / sizeof sizes[0]; i++) {
115 if (!strcmp(arg, sizes[i]))
122 static void gen_icon(int size, const char *colourseq)
124 void (*tilefunc)(unsigned long *, const XColor *, int, int);
125 static unsigned long icon_buf[48*48];
126 const char colourchars[21] = ".%+"",Rr""-Oo""'Yy"":Gg"";Bb"" Ww";
127 unsigned i, j, x, y, w, h, n;
130 case ICON_16x16: tilefunc = ewmh_tile16; w = h = 16; break;
131 case ICON_24x24: tilefunc = ewmh_tile24; w = h = 24; break;
132 case ICON_32x32: tilefunc = ewmh_tile32; w = h = 32; break;
133 case ICON_48x48: tilefunc = ewmh_tile48; w = h = 48; break;
137 for (i = 0; i < 9; i++) {
140 assert(colourseq[i] >= '0' && colourseq[i] <= '6');
141 c = colours[colourseq[i]-'0'];
143 tilefunc(icon_buf, c, i%3, i/3);
147 for (i = 0; i < 7; i++)
148 n -= !strchr(colourseq, '0'+i);
151 puts("static char *icon[] = {");
152 printf("\"%u %u %u 1\",\n", w, h, 3*n);
153 for (i = 0; i < 7; i++) {
154 if (!strchr(colourseq, '0'+i))
157 for (j = 0; j < 3; j++) {
158 printf("\"%c c #%.6lx\",\n", colourchars[3*i+j],
159 colours[i][j].pixel & 0xffffff);
163 for (y = 0; y < h; y++) {
165 for (x = 0; x < w; x++) {
166 unsigned long val = icon_buf[y*h+x];
169 for (i = 0; i < sizeof colourchars; i++) {
170 if (colours[i/3][i%3].pixel == val) {
178 printf("\"%.*s\n", y+1<h, ",");
183 int decode_colourseq(char *out, const char *arg)
187 for (i = 0; i < 9; i++) {
190 case 'R': case 'r': out[i] = '1'; break;
191 case 'O': case 'o': out[i] = '2'; break;
192 case 'Y': case 'y': out[i] = '3'; break;
193 case 'G': case 'g': out[i] = '4'; break;
194 case 'B': case 'b': out[i] = '5'; break;
195 case 'W': case 'w': out[i] = '6'; break;
196 case '1': case '2': case '3': case '4': case '5': case '6':
197 case '0': out[i] = arg[i]; break;
204 fprintf(stderr, "%s: error: invalid colour sequence '%s'\n",
212 int main(int argc, char **argv)
214 char colourseq[9] = "000000000";
220 while ((opt = getopt_long(argc, argv, sopts, lopts, 0)) != -1) {
223 if (decode_colourseq(colourseq, optarg) != 0)
238 if (argc != optind+1) {
239 printf("%s: error: size not specified\n", progname);
244 size = to_size_enum(argv[optind]);
246 printf("%s: error: invalid size %s\n", progname, argv[optind]);
251 gen_icon(size, colourseq);