X-Git-Url: http://git.draconx.ca/gitweb/rrace.git/blobdiff_plain/44fbf97825db6918cc7f4e31189f689ba7270128..1ad1b0dcce2ad67f1aaa559c7a76cf0a6cbab03b:/src/x11.c diff --git a/src/x11.c b/src/x11.c index 94a0619..b098d0e 100644 --- a/src/x11.c +++ b/src/x11.c @@ -18,50 +18,95 @@ #include #include +#include +#include #include #include -#include +#include +#include +#include + #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 +#endif + +enum { + NUM_TILE_COLOURS = COLOUR_MAX*(TILE_MAX-1), + COLOUR_UI_GAMEBG = NUM_TILE_COLOURS, + COLOUR_UI_MAX +}; +typedef Pixel colour_tab[COLOUR_UI_MAX]; -/* TODO user-selectable colours */ -static const char * const colours[][3] = { - /*primary bottom top */ - "#8d2e28", "#6a1b17", "#a14842", /* red */ - "#b46e28", "#924a16", "#c7904f", /* orange */ - "#d8b740", "#c59f39", "#e2c65d", /* yellow */ - "#286428", "#194719", "#4e874e", /* green */ - "#003471", "#001f4f", "#00528b", /* blue */ - "#dcdcdc", "#c0c0c0", "#eaeaea", /* white */ +#define COLOURRES(name, index, def) { \ + (name), (NULL), XtRPixel, sizeof (String), \ + offsetof(struct { colour_tab t; }, t[index]), \ + XtRString, (def) } + +static XtResource colour_resources[] = { + COLOURRES("colour0", 0, COLOUR0_PRIMARY), + COLOURRES("colour1", 1, COLOUR1_PRIMARY), + COLOURRES("colour2", 2, COLOUR2_PRIMARY), + COLOURRES("colour3", 3, COLOUR3_PRIMARY), + COLOURRES("colour4", 4, COLOUR4_PRIMARY), + COLOURRES("colour5", 5, COLOUR5_PRIMARY), + + COLOURRES("colourDark0", 6, COLOUR0_DARK), + COLOURRES("colourDark1", 7, COLOUR1_DARK), + COLOURRES("colourDark2", 8, COLOUR2_DARK), + COLOURRES("colourDark3", 9, COLOUR3_DARK), + COLOURRES("colourDark4", 10, COLOUR4_DARK), + COLOURRES("colourDark5", 11, COLOUR5_DARK), + + COLOURRES("colourLight0", 12, COLOUR0_LIGHT), + COLOURRES("colourLight1", 13, COLOUR1_LIGHT), + COLOURRES("colourLight2", 14, COLOUR2_LIGHT), + COLOURRES("colourLight3", 15, COLOUR3_LIGHT), + COLOURRES("colourLight4", 16, COLOUR4_LIGHT), + COLOURRES("colourLight5", 17, COLOUR5_LIGHT), + + COLOURRES("gameBackground", COLOUR_UI_GAMEBG, "#181818") }; -static void init_colours(struct app_state *state, Screen *screen) +static void init_colours(struct app_state *state, Widget shell) { - Display *display = DisplayOfScreen(screen); - Colormap cmap = DefaultColormapOfScreen(screen); - XColor colour, junk; - unsigned i, j; - - for (j = 0; j < COLOUR_MAX; j++) { - for (i = 0; i < TILE_MAX-1; i++) { - XAllocNamedColor(display, cmap, colours[i][j], - &colour, &junk); - state->tile_colour[i][j] = colour.pixel; - } + unsigned i; + + colour_tab colours; + XtGetApplicationResources(shell, &colours, colour_resources, + XtNumber(colour_resources), NULL, 0); + + for (i = 0; i < NUM_TILE_COLOURS; i++) { + unsigned tile = i % (TILE_MAX-1), shade = i / (TILE_MAX-1); + + state->tile_colour[tile][shade] = colours[i]; } + + XtVaSetValues(state->game, XtNbackground, colours[COLOUR_UI_GAMEBG], + (char *)NULL); } -void x11_initialize(struct app_state *state, Screen *screen) +void x11_initialize(struct app_state *state, Widget shell) { - Display *display = DisplayOfScreen(screen); + Display *display = XtDisplay(shell); + Screen *screen = XtScreen(shell); Window root = RootWindowOfScreen(screen); - Colormap cmap = DefaultColormapOfScreen(screen); XGCValues gcv; - init_colours(state, screen); + init_colours(state, shell); 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, @@ -95,7 +140,39 @@ 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 void clear_rect(struct app_state *state, Display *display, Drawable d, + int x, int y, int w, int h) +{ +#if X11_RENDER_DEBUG + XRectangle r = { x, y, w, h }; + + XSetForeground(display, state->tile_gc, 0xff0000); + XFillRectangles(display, d, state->tile_gc, &r, 1); + 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(struct app_state *state, Display *display, Drawable d, + Dimension w, Dimension h, uint_fast32_t mask) +{ + if (!(mask &= ~GOAL_MASK & 0x1ffffff)) + return; + + clear_rect(state, display, d, 0, 0, 5*w, h); + clear_rect(state, display, d, 0, 4*h, 5*w, h); + clear_rect(state, display, d, 0, 0, w, 5*h); + clear_rect(state, display, d, 4*w, 0, w, 5*h); +} + +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) @@ -103,6 +180,16 @@ redraw_tile(struct app_state *state, Display *display, Drawable d, uint_fast32_t pos = board_position(x, y); unsigned char tile = 0; +#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 + if (bit0 & pos) tile |= 1; if (bit1 & pos) tile |= 2; if (bit2 & pos) tile |= 4; @@ -113,42 +200,175 @@ redraw_tile(struct app_state *state, Display *display, Drawable d, } else { draw_tile(state, display, d, tile, x, y, w, h); } + + return tile; } -void x11_redraw_goal(struct app_state *state) +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, uint_fast32_t mask) { Display *display = XtDisplay(state->goal); Window goal = XtWindow(state->goal); Dimension w, h; int i; - XtVaGetValues(state->goal, XmNwidth, &w, XmNheight, &h, (char *)NULL); + XtVaGetValues(state->goal, XtNwidth, &w, XtNheight, &h, (char *)NULL); w /= 3; h /= 3; for (i = 0; i < 9; i++) { - uint_least16_t *gp = state->board.goal; + int x = i%3, y = i/3; + + if (mask & 1) { + redraw_goal_tile(state, display, goal, x, y, w, h); + } - redraw_tile(state, display, goal, - gp[0], gp[1], gp[2], - i%3, i/3, w, h); + /* + * Goal bitmaps have a gap of 2 tiles between each row. + * This funny shift will accomodate that. + */ + mask >>= 1|x; } } -void x11_redraw_game(struct app_state *state) +/* 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; + unsigned long ewmhseq[9]; + unsigned i; + + for (i = 0; i < 9; i++) { + int tile; + + tile = redraw_goal_tile(state, display, state->icon_pixmap, + i%3, i/3, tilesz, tilesz); + + ewmhseq[i] = COLOUR_MAX*0x10101 * (tile-1) + 0x20100; + } + + /* + * 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; + + for (i = 0; i < XtNumber(colours); i++) { + uint_least32_t *p = state->tile_colour[0]; + colours[i].pixel = p[i]; + } + + 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_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; int i; - XtVaGetValues(state->game, XmNwidth, &w, XmNheight, &h, (char *)NULL); + XtVaGetValues(state->game, XtNwidth, &w, XtNheight, &h, (char *)NULL); w /= 5; h /= 5; - for (i = 0; i < 25; i++) { - uint_least32_t *gp = state->board.game; + if (state->view_goal_on_game) { + for (i = 0; i < 3; i++) { + buf[i] = state->board.goal[i]; + buf[i] = (gp[i] & ~GOAL_MASK) | (buf[i] << GOAL_SHIFT); + } + gp = buf; + } - redraw_tile(state, display, game, - gp[0], gp[1], gp[2], - i%5, i/5, w, h); + /* Optimize the game end case where the outer frame is cleared */ + if (((gp[0] | gp[1] | gp[2]) & ~GOAL_MASK) == 0) { + clear_border(state, display, game, w, h, mask); + 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); + } + 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); + 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); }