]> git.draconx.ca Git - rrace.git/blobdiff - src/x11.c
motif: Avoid constantly recalculating tile size.
[rrace.git] / src / x11.c
index 955b93945e892e7c586deea2fe9b7ed920de2425..6d09a7dcfc09f7359db1daeba62d980bae65cfd4 100644 (file)
--- a/src/x11.c
+++ b/src/x11.c
 #include <X11/StringDefs.h>
 #include <X11/Xatom.h>
 #include <X11/Shell.h>
+
 #include "motif.h"
+#include "ewmhicon.h"
 
 /* Size of the traditional icon pixmap (multiple of 3) */
 #define ICON_SIZE 48
 
+/* Define to 1 to add highlights and delays for visually debugging redraws */
+#if X11_RENDER_DEBUG
+#  include <unistd.h>
+#endif
+
 enum {
        NUM_TILE_COLOURS = COLOUR_MAX*(TILE_MAX-1),
        COLOUR_UI_GAMEBG = NUM_TILE_COLOURS,
@@ -133,19 +140,56 @@ 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
+clear_rect(Display *display, Drawable d, int x, int y, int w, int h)
+{
+#if X11_RENDER_DEBUG
+       XRectangle r = { x, y, w, h };
+       XGCValues gcv;
+       GC gc;
+
+       gcv.foreground = 0xff0000;
+       gc = XCreateGC(display, d, GCForeground, &gcv);
+       XFillRectangles(display, d, gc, &r, 1);
+       XFreeGC(display, gc);
+
+       XFlush(display);
+       usleep(70000);
+#endif
+
+       XClearArea(display, d, x, y, w, h, 0);
+}
+
+/*
+ * Efficiently clear all the border tiles in the game area.  The mask indicates
+ * which tiles need clearing, but for the border clear it is safe to wipe an
+ * entire row or column of the border using a single XClearArea.
+ */
+static void clear_border(Display *display, Drawable d, unsigned sz)
+{
+       clear_rect(display, d, 0,    0,    -1, sz);
+       clear_rect(display, d, 0,    4*sz, -1, sz);
+       clear_rect(display, d, 0,    0,    sz, -1);
+       clear_rect(display, d, 4*sz, 0,    sz, -1);
+}
+
 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)
+            uint_least32_t *gp, int x, int y, unsigned sz)
 {
-       uint_fast32_t pos = board_position(x, y);
-       unsigned char tile = 0;
-
-       if (bit0 & pos) tile |= 1;
-       if (bit1 & pos) tile |= 2;
-       if (bit2 & pos) tile |= 4;
+       unsigned tile = board_tile(gp, 5*y+x);
+       unsigned w = sz, h = sz;
+
+#if X11_RENDER_DEBUG
+       if (d == XtWindow(state->game) || d == XtWindow(state->goal)) {
+               XRectangle r = { x*w, y*h, w, h };
+               XSetForeground(display, state->tile_gc, 0xff0000);
+               XFillRectangles(display, d, state->tile_gc, &r, 1);
+               XFlush(display);
+               usleep(70000);
+       }
+#endif
        assert(tile < TILE_MAX);
-
        if (tile == TILE_EMPTY) {
                XClearArea(display, d, x*w, y*h, w, h, 0);
        } else {
@@ -155,92 +199,77 @@ redraw_tile(struct app_state *state, Display *display, Drawable d,
        return tile;
 }
 
-static int
-redraw_goal_tile(struct app_state *state, Display *display, Drawable d,
-                 int x, int y, Dimension w, Dimension h)
+static void set_icon(struct app_state *state, Display *display,
+                     Widget shell, unsigned long *ewmhseq)
 {
-       uint_least16_t *gp = state->board.goal;
+       /*
+        * Clear and reset XtNiconPixmap otherwise it seems dtwm will not
+        * notice the changed icon.
+        */
+       XtVaSetValues(shell, XtNiconPixmap, None, (char *)NULL);
+       XtVaSetValues(shell, XtNiconPixmap, state->icon_pixmap, (char *)NULL);
+
+       if (state->use_ewmh_icons) {
+               Atom net_wm_icon = XInternAtom(display, "_NET_WM_ICON", FALSE);
+               Colormap cmap = DefaultColormapOfScreen(XtScreen(shell));
+               XColor colours[(TILE_MAX-1)*COLOUR_MAX];
+               void *wm_icon;
+               int i;
+
+               for (i = 0; i < XtNumber(colours); i++) {
+                       uint_least32_t *p = state->tile_colour[0];
+                       colours[i].pixel = p[i];
+               }
 
-       return redraw_tile(state, display, d, gp[0], gp[1], gp[2], x, y, w, h);
+               XQueryColors(display, cmap, colours, XtNumber(colours));
+               wm_icon = ewmh_icon_generate(ewmhseq, colours);
+               if (wm_icon) {
+                       XChangeProperty(display, XtWindow(shell), net_wm_icon,
+                                       XA_CARDINAL, 32, PropModeReplace,
+                                       wm_icon, EWMH_ICON_NELEM);
+               }
+               free(wm_icon);
+       }
 }
 
-void x11_redraw_goal(struct app_state *state, uint_fast32_t mask)
+void
+x11_redraw_goal(struct app_state *state, uint_fast32_t mask, Widget icon_shell)
 {
        Display *display = XtDisplay(state->goal);
-       Window window = XtWindow(state->goal);
-       Dimension w, h;
+       Window goal = XtWindow(state->goal);
+       unsigned sz = state->goal_tile_sz;
+       unsigned long ewmhseq[9];
        int i;
 
-       XtVaGetValues(state->goal, XtNwidth, &w, XtNheight, &h, (char *)NULL);
+       uint_least32_t gp[3] = {
+               state->board.goal[0],
+               state->board.goal[1],
+               state->board.goal[2]
+       };
+
        for (i = 0; i < 9; i++) {
                int x = i%3, y = i/3;
 
-               if (mask & 1) {
-                       redraw_goal_tile(state, display, window,
-                                               x, y, w/3, h/3);
+               if (icon_shell) {
+                       unsigned tile;
+
+                       tile = redraw_tile(state, display, state->icon_pixmap,
+                                          gp, x, y, ICON_SIZE/3);
+                       ewmhseq[i] = COLOUR_MAX*0x10101 * (tile-1) + 0x20100;
                }
 
+               if (mask & 1)
+                       redraw_tile(state, display, goal, gp, x, y, sz);
+
                /*
                 * Goal bitmaps have a gap of 2 tiles between each row.
                 * This funny shift will accomodate that.
                 */
                mask >>= 1|x;
        }
-}
-
-/* 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++) {
-               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);
-               }
-       }
 
-       /*
-        * Clear and reset XtNiconPixmap otherwise it seems dtwm will not
-        * notice the changed icon.
-        */
-       XtVaSetValues(shell, XtNiconPixmap, None, (char *)NULL);
-       XtVaSetValues(shell, XtNiconPixmap, 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);
-       }
+       if (icon_shell)
+               set_icon(state, display, icon_shell, ewmhseq);
 }
 
 void x11_redraw_game(struct app_state *state, uint_fast32_t mask)
@@ -248,12 +277,9 @@ void x11_redraw_game(struct app_state *state, uint_fast32_t mask)
        Display *display = XtDisplay(state->goal);
        Window game = XtWindow(state->game);
        uint_least32_t buf[3], *gp = state->board.game;
-       Dimension w, h;
+       unsigned sz = state->game_tile_sz;
        int i;
 
-       XtVaGetValues(state->game, XtNwidth, &w, XtNheight, &h, (char *)NULL);
-       w /= 5; h /= 5;
-
        if (state->view_goal_on_game) {
                for (i = 0; i < 3; i++) {
                        buf[i] = state->board.goal[i];
@@ -262,12 +288,69 @@ void x11_redraw_game(struct app_state *state, uint_fast32_t mask)
                gp = buf;
        }
 
+       /* Optimize the game end case where the outer frame is cleared */
+       if (((gp[0] | gp[1] | gp[2]) & ~GOAL_MASK) == 0) {
+               clear_border(display, game, sz);
+               mask &= GOAL_MASK;
+       }
+
        for (i = 0; i < 25; i++) {
-               if (mask & 1) {
-                       redraw_tile(state, display, game,
-                                   gp[0], gp[1], gp[2],
-                                   i%5, i/5, w, h);
-               }
+               if (mask & 1)
+                       redraw_tile(state, display, game, gp, i%5, i/5, sz);
                mask >>= 1;
        }
 }
+
+/*
+ * Deferred redraw of tiles after resize/expose to avoid redundant drawing.
+ *
+ * Record any tiles that need to be redrawn due to resizes or expose events,
+ * then, after a short delay, perform all the accumulated redraws at once.
+ *
+ * This is implemented using both a work proc and a timeout, because it seems
+ * that rendering directly inside the timeout callback gives poor results:
+ * possibly redrawing outdated intermediate positions during "fast" resizes
+ * long after the resizing has stopped.  This does not happen when drawing
+ * from a work proc.
+ */
+static Boolean do_render(void *data)
+{
+       struct app_state *state = data;
+
+       x11_redraw_goal(state, state->render_goal_mask, NULL);
+       x11_redraw_game(state, state->render_game_mask);
+
+       state->render_goal_mask = state->render_game_mask = 0;
+       state->render_proc = 0;
+       state->render_tick = 0;
+       return True;
+}
+
+static void start_render(void *data, XtIntervalId *id)
+{
+       struct app_state *state = data;
+       XtAppContext app;
+
+       if (state->render_proc)
+               return;
+
+       app = XtWidgetToApplicationContext(state->game);
+       state->render_proc = XtAppAddWorkProc(app, do_render, state);
+}
+
+void x11_queue_render(struct app_state *state, uint_fast32_t mask, int mode)
+{
+       uint_fast32_t changed = 0;
+       XtAppContext app;
+
+       if (mode & RENDER_MODE_GOAL)
+               changed |= state->render_goal_mask |= mask;
+       if (mode & RENDER_MODE_GAME)
+               changed |= state->render_game_mask |= mask;
+
+       if (state->render_tick || !changed)
+               return;
+
+       app = XtWidgetToApplicationContext(state->game);
+       state->render_tick = XtAppAddTimeOut(app, 3, start_render, state);
+}