1 /* See LICENSE file for copyright and license details.
3 * X background color daemon is really simple. It just uses interpolation
4 * between steps to get the new background color. If the substep reaches 1.0f,
5 * it resets it, and starts the next step.
20 const size_t num_steps = (sizeof steps) / (sizeof *steps);
26 static unsigned long MyColorToPixel (const struct MyColor *color);
27 static unsigned long NameToPixel (const char *color);
28 static void FillBackground (const struct MyColor *color);
29 static void die (const char *s);
32 main(int argc, char *argv[])
34 if (argc == 2 && !strcmp (argv[1], "-v")) {
35 die ("xkbcd-" VERSION);
36 } else if (argc != 1) {
37 die ("usage: xbcd [-v]");
40 dpy = XOpenDisplay (NULL);
42 die ("xkbcd: unable to open display");
45 if (pledge ("stdio", NULL) == -1)
49 screen = DefaultScreen (dpy);
50 root = RootWindow (dpy, screen);
53 float sub_step = 0.0f;
54 struct MyColor prev = initial_color;
57 const struct MyColor cur = steps[step];
58 const struct MyColor c = {
59 .r = prev.r + (cur.r - prev.r) * sub_step,
60 .g = prev.g + (cur.g - prev.g) * sub_step,
61 .b = prev.b + (cur.b - prev.b) * sub_step,
66 sub_step += sub_step_inc;
68 if (sub_step > 1.0f) {
69 step = (step + 1) % num_steps;
73 usleep (delay * 1000);
80 MyColorToPixel (const struct MyColor *color)
83 const int r = (int)(color->r * 255.0f);
84 const int g = (int)(color->g * 255.0f);
85 const int b = (int)(color->b * 255.0f);
87 snprintf (str, sizeof str, "#%02X%02X%02X", r, g, b);
89 return NameToPixel (str);
92 // This function is taken from xsetroot: https://github.com/freedesktop/xorg-xsetroot/blob/master/xsetroot.c
94 NameToPixel (const char *name)
99 return BlackPixel (dpy, screen);
101 Colormap colormap = DefaultColormap (dpy, screen);
103 if (!XParseColor (dpy, colormap, name, &ecolor)) {
104 fprintf (stderr, "xbgcd: unknown color '%s'\n", name);
105 return BlackPixel (dpy, screen);
108 if (!XAllocColor (dpy, colormap, &ecolor)) {
109 fprintf (stderr, "xbgcd: unable to allocate color for '%s'\n", name);
110 return BlackPixel (dpy, screen);
117 FillBackground (const struct MyColor *color)
119 unsigned long pixel = MyColorToPixel (color);
120 XSetWindowBackground (dpy, root, pixel);
121 XClearWindow (dpy, root);
128 fprintf (stderr, "%s\n", s);