/*
* 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 .
*/
#include
#include
#include
#include
#include
#include
#include "ewmhicon.h"
#include "colour.h"
#include "help.h"
static const char *progname = "ewmhicon";
static const char sopts[] = "c:VH";
static const struct option lopts[] = {
{ "colourseq", 1, NULL, 'c' },
{ "version", 0, NULL, 'V' },
{ "help", 0, NULL, 'H' },
{ 0 }
};
#define PASTE(a, b) a ## b
#define INDIR(a) a
#define S8TO16(x) ((x) * 0xfffful / 0xff)
#define RGB8_(r, g, b) { \
0xff000000 | (r << 16) | (g << 8) | b, \
S8TO16(r), S8TO16(g), S8TO16(b) }
#define RGB8(r, g, b) RGB8_(0x ## r ## ul, 0x ## g ## ul, 0x ## b ## ul)
#define COLOURTAB(n) { \
INDIR(RGB8 PASTE(COLOUR ## n, _PRIMARY_RGB_)), \
INDIR(RGB8 PASTE(COLOUR ## n, _DARK_RGB_)), \
INDIR(RGB8 PASTE(COLOUR ## n, _LIGHT_RGB_)) }
static const XColor colours[7][3] = {
{
{ 0xffff0000, 0xffff },
{ 0xff00ff00, 0, 0xffff },
{ 0xff0000ff, 0, 0, 0xffff }
},
COLOURTAB(0), COLOURTAB(1), COLOURTAB(2),
COLOURTAB(3), COLOURTAB(4), COLOURTAB(5)
};
static void print_usage(FILE *f)
{
fprintf(f, "Usage: %s size\n", progname);
if (f != stdout)
fprintf(f, "Try %s --help for more information.\n", progname);
}
static void print_help(void)
{
const struct option *opt;
print_usage(stdout);
putchar('\n');
puts("Options:");
for (opt = lopts; opt->name; opt++) {
if (help_print_optstring(opt, "ARG", 20))
putchar('\n');
}
putchar('\n');
printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
}
static void print_version(void)
{
printf("ewmhicon (%s) %s\n", PACKAGE, PACKAGE_VERSION);
printf("Copyright (C) 2022 Nick Bowler\n");
puts("License GPLv3+: GNU GPL version 3 or any later version");
puts("This is free software: you are free to change and redistribute it.");
puts("There is NO WARRANTY, to the extent permitted by law.");
}
static const char sizes[][6] = {
"16x16", "24x24", "32x32", "48x48"
};
static 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 void gen_icon(int size, const char *colourseq)
{
void (*tilefunc)(unsigned long *, const XColor *, int, int);
static unsigned long icon_buf[48*48];
const char colourchars[21] = ".%+"",Rr""-Oo""'Yy"":Gg"";Bb"" Ww";
unsigned i, j, x, y, w, h, n;
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);
}
for (i = 0; i < 9; i++) {
const XColor *c;
assert(colourseq[i] >= '0' && colourseq[i] <= '6');
c = colours[colourseq[i]-'0'];
tilefunc(icon_buf, c, i%3, i/3);
}
n = 7;
for (i = 0; i < 7; i++)
n -= !strchr(colourseq, '0'+i);
puts("/* XPM */");
puts("static char *icon[] = {");
printf("\"%u %u %u 1\",\n", w, h, 3*n);
for (i = 0; i < 7; i++) {
if (!strchr(colourseq, '0'+i))
continue;
for (j = 0; j < 3; j++) {
printf("\"%c c #%.6lx\",\n", colourchars[3*i+j],
colours[i][j].pixel & 0xffffff);
}
}
for (y = 0; y < h; y++) {
putchar('"');
for (x = 0; x < w; x++) {
unsigned long val = icon_buf[y*h+x];
int c = '#';
for (i = 0; i < sizeof colourchars; i++) {
if (colours[i/3][i%3].pixel == val) {
c = colourchars[i];
break;
}
}
putchar(c);
}
printf("\"%.*s\n", y+1 0)
progname = argv[0];
while ((opt = getopt_long(argc, argv, sopts, lopts, 0)) != -1) {
switch (opt) {
case 'c':
if (decode_colourseq(colourseq, optarg) != 0)
return EXIT_FAILURE;
break;
case 'H':
print_help();
return EXIT_SUCCESS;
case 'V':
print_version();
return EXIT_SUCCESS;
default:
print_usage(stderr);
return EXIT_FAILURE;
}
}
if (argc != optind+1) {
printf("%s: error: size not specified\n", progname);
print_usage(stderr);
return EXIT_FAILURE;
}
size = to_size_enum(argv[optind]);
if (size < 0) {
printf("%s: error: invalid size %s\n", progname, argv[optind]);
print_usage(stderr);
return EXIT_FAILURE;
}
gen_icon(size, colourseq);
return 0;
}