]> git.draconx.ca Git - rrace.git/blobdiff - src/x11.c
Implement window icons.
[rrace.git] / src / x11.c
index 611223f54b7ba888cafebd6cb547d5f3ba32cc24..95e0cf057646085d502929124552196118fb9b4d 100644 (file)
--- a/src/x11.c
+++ b/src/x11.c
 
 #include <config.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <assert.h>
 
 #include <X11/Intrinsic.h>
 #include <Xm/XmStrDefs.h>
+#include <X11/Xatom.h>
+#include <X11/Shell.h>
 #include "motif.h"
 
+/* Size of the traditional icon pixmap (multiple of 3) */
+#define ICON_SIZE 48
+
 /* TODO user-selectable colours */
 static const char * const colours[][3] = {
        /*primary    bottom      top */
@@ -61,6 +67,9 @@ void x11_initialize(struct app_state *state, Screen *screen)
 
        gcv.line_width = 1;
        state->tile_gc = XCreateGC(display, root, GCLineWidth, &gcv);
+
+       state->icon_pixmap = XCreatePixmap(display, root, ICON_SIZE, ICON_SIZE,
+                                          DefaultDepthOfScreen(screen));
 }
 
 static void draw_tile(struct app_state *state, Display *display, Drawable d,
@@ -94,7 +103,7 @@ static void draw_tile(struct app_state *state, Display *display, Drawable d,
        XFillRectangle(display, d, state->tile_gc, tx+2, ty+2, tw-4, th-4);
 }
 
-static void
+static int
 redraw_tile(struct app_state *state, Display *display, Drawable d,
             uint_fast32_t bit0, uint_fast32_t bit1, uint_fast32_t bit2,
             int x, int y, Dimension w, Dimension h)
@@ -112,24 +121,83 @@ redraw_tile(struct app_state *state, Display *display, Drawable d,
        } else {
                draw_tile(state, display, d, tile, x, y, w, h);
        }
+
+       return tile;
+}
+
+static int
+redraw_goal_tile(struct app_state *state, Display *display, Drawable d,
+                 int x, int y, Dimension w, Dimension h)
+{
+       uint_least16_t *gp = state->board.goal;
+
+       return redraw_tile(state, display, d, gp[0], gp[1], gp[2], x, y, w, h);
 }
 
 void x11_redraw_goal(struct app_state *state)
 {
        Display *display = XtDisplay(state->goal);
-       Window goal = XtWindow(state->goal);
+       Window window = XtWindow(state->goal);
        Dimension w, h;
        int i;
 
        XtVaGetValues(state->goal, XmNwidth, &w, XmNheight, &h, (char *)NULL);
-       w /= 3; h /= 3;
+       for (i = 0; i < 9; i++)
+               redraw_goal_tile(state, display, window, i%3, i/3, w/3, h/3);
+}
+
+/* Render the goal area as the window's icon */
+void x11_redraw_icon(struct app_state *state, Widget shell)
+{
+       Display *display = XtDisplay(shell);
+       Dimension tilesz = ICON_SIZE/3;
+       int i, j, tile;
+
+       XColor colours[(TILE_MAX-1)*COLOUR_MAX];
+       unsigned long *icons[ICON_MAX];
+       void *wm_icon = NULL;
+
+       if (state->use_ewmh_icons && (wm_icon = ewmh_icon_alloc(icons))) {
+               Colormap cmap = DefaultColormapOfScreen(XtScreen(shell));
+
+               for (i = 0; i < TILE_MAX-1; i++) {
+                       for (j = 0; j < COLOUR_MAX; j++) {
+                               XColor *c = &colours[i*COLOUR_MAX+j];
+                               c->pixel = state->tile_colour[i][j];
+                       }
+               }
+
+               XQueryColors(display, cmap, colours, i*j);
+       }
 
        for (i = 0; i < 9; i++) {
-               uint_least16_t *gp = state->board.goal;
+               tile = redraw_goal_tile(state, display, state->icon_pixmap,
+                                       i%3, i/3, tilesz, tilesz);
+
+               if (wm_icon) {
+                       XColor *c = &colours[(tile-1)*COLOUR_MAX];
+                       ewmh_tile16(icons[ICON_16x16], c, i%3, i/3);
+                       ewmh_tile24(icons[ICON_24x24], c, i%3, i/3);
+                       ewmh_tile32(icons[ICON_32x32], c, i%3, i/3);
+                       ewmh_tile48(icons[ICON_48x48], c, i%3, i/3);
+               }
+       }
 
-               redraw_tile(state, display, goal,
-                           gp[0], gp[1], gp[2],
-                           i%3, i/3, w, h);
+       /*
+        * Clear and reset XmNiconPixmap otherwise it seems dtwm will not
+        * notice the changed icon.
+        */
+       XtVaSetValues(shell, XmNiconPixmap, None, (char *)NULL);
+       XtVaSetValues(shell, XmNiconPixmap, state->icon_pixmap, (char *)NULL);
+
+       if (wm_icon) {
+               Atom net_wm_icon = XInternAtom(display, "_NET_WM_ICON", FALSE);
+
+               XChangeProperty(display, XtWindow(shell), net_wm_icon,
+                               XA_CARDINAL, 32, PropModeReplace,
+                               wm_icon, EWMH_ICON_NELEM);
+
+               free(wm_icon);
        }
 }