/*
* _NET_WM_ICON helpers for slide puzzle game
* Copyright © 2022 Nick Bowler
*
* 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
#if !X_DISPLAY_MISSING
# include
# include
#endif
#include "ewmhicon.h"
static unsigned long scale16to8(unsigned x)
{
return x*0xfful / 0xffff;
}
static unsigned long wm_pixel(const XColor *c)
{
return 0xff000000
| scale16to8(c->red) << 16
| scale16to8(c->green) << 8
| scale16to8(c->blue);
}
/*
* The 16x16 icon is drawn with 1px shadow, 6x6 tiles, with 1 pixel cropped off
* all the edge tiles
*/
void ewmh_tile16(unsigned long *icon, const XColor *c, int tile_x, int tile_y)
{
int out_x, out_y, out_w, x, y;
unsigned long row[6];
out_x = tile_x * 11 / 2;
out_y = tile_y * 11 / 2;
out_w = (5 + (tile_x == 1)) * sizeof row[0];
for (y = 0+(tile_y == 0); y < 6-(tile_y == 2); y++) {
for (x = 0+(tile_x == 0); x < 6-(tile_x==2); x++) {
if (x == 0 || y == 0)
row[x] = wm_pixel(&c[COLOUR_LIGHT]);
else if (x == 5 || y == 5)
row[x] = wm_pixel(&c[COLOUR_DARK]);
else
row[x] = wm_pixel(&c[COLOUR_PRIMARY]);
}
memcpy(&icon[16 * out_y++ + out_x], &row[tile_x == 0], out_w);
}
}
/*
* The 24x24 icon is drawn with 1px shadow and 8x8 tiles.
*/
void ewmh_tile24(unsigned long *icon, const XColor *c, int tile_x, int tile_y)
{
int out_x, out_y, x, y;
unsigned long row[8];
out_x = tile_x * 8;
out_y = tile_y * 8;
for (y = 0; y < 8; y++) {
for (x = 0; x < 8; x++) {
if (x == 0 || y == 0)
row[x] = wm_pixel(&c[COLOUR_LIGHT]);
else if (x == 7 || y == 7)
row[x] = wm_pixel(&c[COLOUR_DARK]);
else
row[x] = wm_pixel(&c[COLOUR_PRIMARY]);
}
memcpy(&icon[24 * out_y++ + out_x], row, sizeof row);
}
}
/*
* The 32x32 icon is drawn with 1px shadow with slightly uneven tiles on
* an 11-10-11 pixel grid.
*/
void ewmh_tile32(unsigned long *icon, const XColor *c, int tile_x, int tile_y)
{
int out_x, out_y, out_w, out_h, x, y;
unsigned long row[11];
out_x = 10*tile_x + (tile_x > 0);
out_y = 10*tile_y + (tile_y > 0);
out_w = 10 + (tile_x != 1);
out_h = 10 + (tile_y != 1);
for (y = 0; y < out_h; y++) {
for (x = 0; x < out_w; x++) {
if (x == 0 || y == 0)
row[x] = wm_pixel(&c[COLOUR_LIGHT]);
else if (x == out_w-1 || y == out_h-1)
row[x] = wm_pixel(&c[COLOUR_DARK]);
else
row[x] = wm_pixel(&c[COLOUR_PRIMARY]);
}
memcpy(&icon[32 * out_y++ + out_x], row, out_w * sizeof row[0]);
}
}
/*
* The 48x48 icon is drawn with 2px shadow and 16x16 tiles.
*/
void ewmh_tile48(unsigned long *icon, const XColor *c, int tile_x, int tile_y)
{
int out_x, out_y, x, y;
unsigned long row[16];
out_x = tile_x * 16;
out_y = tile_y * 16;
for (y = 0; y < 16; y++) {
for (x = 0; x < 16; x++) {
if (x == 0 || y == 0)
row[x] = wm_pixel(&c[COLOUR_LIGHT]);
else if (x == 15 || y == 15)
row[x] = wm_pixel(&c[COLOUR_DARK]);
else if (x == 1 || y == 1)
row[x] = wm_pixel(&c[COLOUR_LIGHT]);
else if (x == 14 || y == 14)
row[x] = wm_pixel(&c[COLOUR_DARK]);
else
row[x] = wm_pixel(&c[COLOUR_PRIMARY]);
}
memcpy(&icon[48 * out_y++ + out_x], row, sizeof row);
}
}
void *ewmh_icon_alloc(unsigned long **sizes)
{
unsigned long *buf;
buf = calloc(sizeof *buf, EWMH_ICON_NELEM);
if (buf) {
sizes[ICON_16x16] = buf;
*sizes[ICON_16x16]++ = 16;
*sizes[ICON_16x16]++ = 16;
sizes[ICON_24x24] = sizes[ICON_16x16] + 16*16;
*sizes[ICON_24x24]++ = 24;
*sizes[ICON_24x24]++ = 24;
sizes[ICON_32x32] = sizes[ICON_24x24] + 24*24;
*sizes[ICON_32x32]++ = 32;
*sizes[ICON_32x32]++ = 32;
sizes[ICON_48x48] = sizes[ICON_32x32] + 32*32;
*sizes[ICON_48x48]++ = 48;
*sizes[ICON_48x48]++ = 48;
}
return buf;
}
#if !X_DISPLAY_MISSING
/*
* EWMH-supporting window managers that handle _NET_WM_ICON add this atom to
* the _NET_SUPPORTED list on the root window. Look for that and return 1
* if it is found, or 0 otherwise.
*/
int ewmh_probe_wm_icon(Widget shell)
{
Display *display = XtDisplay(shell);
Screen *screen = XtScreen(shell);
Window root = RootWindowOfScreen(screen);
Atom net_supported, net_wm_icon, type;
unsigned long offset = 0, i, nitems, bytes_after, *props;
unsigned char *prop_return;
int format;
net_wm_icon = XInternAtom(display, "_NET_WM_ICON", 0);
net_supported = XInternAtom(display, "_NET_SUPPORTED", 0);
do {
XGetWindowProperty(display, root, net_supported, offset, 10,
0, XA_ATOM, &type, &format,
&nitems, &bytes_after,
&prop_return);
if (format != 32 || type != XA_ATOM)
break;
offset += nitems;
props = (void *)prop_return;
for (i = 0; i < nitems; i++) {
if (props[i] == net_wm_icon) {
return 1;
}
}
} while (nitems > 0 && bytes_after > 0);
return 0;
}
#endif