Files

58 lines
2.0 KiB
C

#ifndef APP_IDENTITY_H
#define APP_IDENTITY_H
#include <stdbool.h>
#define APP_NAME "Game of Life Sandbox"
#if defined(__linux__)
#include <GL/glx.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
/* EWMH icons are embedded ARGB pixels, independent of files beside the binary.
The geometry and colors match assets/game-of-life-sandbox.svg. */
static unsigned long appIconPixel(int x, int y, int size) {
int scale = size / 64;
x /= scale;
y /= scale;
int dx = x < 12 ? 12 - x : x > 51 ? x - 51 : 0;
int dy = y < 12 ? 12 - y : y > 51 ? y - 51 : 0;
if (dx * dx + dy * dy > 144) return 0;
unsigned long color = 0xff160b25UL;
if (x >= 8 && x < 56 && y >= 8 && y < 56) {
int col = (x - 8) / 10, row = (y - 8) / 10;
if ((x - 8) % 10 < 8 && (y - 8) % 10 < 8) {
bool live = (col == 2 && row == 1) || (col == 3 && row == 2)
|| (row == 3 && col >= 1 && col <= 3);
color = live ? 0xffc58affUL : 0xff352047UL;
}
}
return color;
}
static void setAppIdentity(void) {
Display *display = glXGetCurrentDisplay();
Window window = (Window)glXGetCurrentDrawable();
if (!display || !window) return;
XClassHint identity = {"game-of-life-sandbox", "GameOfLifeSandbox"};
XSetClassHint(display, window, &identity);
XSetIconName(display, window, APP_NAME);
/* Xlib expects machine-word storage for format-32 properties on 64-bit Linux. */
unsigned long pixels[2 + 64 * 64 + 2 + 128 * 128];
int used = 0;
for (int size = 64; size <= 128; size *= 2) {
pixels[used++] = (unsigned long)size;
pixels[used++] = (unsigned long)size;
for (int y = 0; y < size; ++y)
for (int x = 0; x < size; ++x) pixels[used++] = appIconPixel(x, y, size);
}
XChangeProperty(display, window, XInternAtom(display, "_NET_WM_ICON", False),
XA_CARDINAL, 32, PropModeReplace, (unsigned char *)pixels, used);
XFlush(display);
}
#else
static void setAppIdentity(void) { }
#endif
#endif