X-Git-Url: http://git.draconx.ca/gitweb/rrace.git/blobdiff_plain/1ad1b0dcce2ad67f1aaa559c7a76cf0a6cbab03b..bbc28a6dec903c362ed1249ca3b31b26e0d44c15:/src/x11.c diff --git a/src/x11.c b/src/x11.c index b098d0e..dd59fd7 100644 --- a/src/x11.c +++ b/src/x11.c @@ -1,6 +1,6 @@ /* * X11 GUI for slide puzzle game - * Copyright © 2022 Nick Bowler + * Copyright © 2022-2023 Nick Bowler * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,59 +38,60 @@ # include #endif +/* status flags in the upper bits are ORed into the game mask which only + * needs the lower 25 bits */ +#define X11_TICK_INSTALLED (1ul << 31) +#define X11_PROC_INSTALLED (1ul << 30) + 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]; - -#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, Widget shell) { + static const char cdefs[] = + "colour0" "\0" COLOUR0_PRIMARY + "\0" "colourDark0" "\0" COLOUR0_DARK + "\0" "colourLight0" "\0" COLOUR0_LIGHT + "\0" "colour1" "\0" COLOUR1_PRIMARY + "\0" "colourDark1" "\0" COLOUR1_DARK + "\0" "colourLight1" "\0" COLOUR1_LIGHT + "\0" "colour2" "\0" COLOUR2_PRIMARY + "\0" "colourDark2" "\0" COLOUR2_DARK + "\0" "colourLight2" "\0" COLOUR2_LIGHT + "\0" "colour3" "\0" COLOUR3_PRIMARY + "\0" "colourDark3" "\0" COLOUR3_DARK + "\0" "colourLight3" "\0" COLOUR3_LIGHT + "\0" "colour4" "\0" COLOUR4_PRIMARY + "\0" "colourDark4" "\0" COLOUR4_DARK + "\0" "colourLight4" "\0" COLOUR4_LIGHT + "\0" "colour5" "\0" COLOUR5_PRIMARY + "\0" "colourDark5" "\0" COLOUR5_DARK + "\0" "colourLight5" "\0" COLOUR5_LIGHT + "\0" "gameBackground" "\0" "#181818"; + + XtResource res[COLOUR_UI_MAX], t = { + (char *)cdefs, 0, XtRPixel, sizeof (Pixel), 0, XtRString + }; + Pixel colours[XtNumber(res)]; 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); + for (i = 0; i < XtNumber(res); i++) { + t.default_addr = strchr(t.resource_name, 0) + 1; + res[i] = t; - state->tile_colour[tile][shade] = colours[i]; + t.resource_name = (char *)t.default_addr + 8; + t.resource_offset += sizeof colours[0]; } - XtVaSetValues(state->game, XtNbackground, colours[COLOUR_UI_GAMEBG], - (char *)NULL); + XtGetApplicationResources(shell, &colours, res, XtNumber(res), NULL, 0); + for (i = 0; i < NUM_TILE_COLOURS; i++) { + uint_least32_t *p = state->tile_colour[0]; + p[i] = colours[i]; + } + XtVaSetValues(state->game, XtNbackground, colours[i], (char *)NULL); } void x11_initialize(struct app_state *state, Widget shell) @@ -140,14 +141,19 @@ 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(struct app_state *state, Display *display, Drawable d, - int x, int y, int w, int h) +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); - XSetForeground(display, state->tile_gc, 0xff0000); - XFillRectangles(display, d, state->tile_gc, &r, 1); XFlush(display); usleep(70000); #endif @@ -160,25 +166,20 @@ static void clear_rect(struct app_state *state, Display *display, Drawable d, * 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) +static void clear_border(Display *display, Drawable d, unsigned sz) { - 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); + 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; + 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)) { @@ -189,12 +190,7 @@ redraw_tile(struct app_state *state, Display *display, Drawable d, usleep(70000); } #endif - - if (bit0 & pos) tile |= 1; - if (bit1 & pos) tile |= 2; - if (bit2 & pos) tile |= 4; assert(tile < TILE_MAX); - if (tile == TILE_EMPTY) { XClearArea(display, d, x*w, y*h, w, h, 0); } else { @@ -204,57 +200,9 @@ 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) -{ - 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, XtNwidth, &w, XtNheight, &h, (char *)NULL); - w /= 3; h /= 3; - - for (i = 0; i < 9; i++) { - int x = i%3, y = i/3; - - if (mask & 1) { - redraw_goal_tile(state, display, goal, x, y, w, h); - } - - /* - * 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) +static void set_icon(struct app_state *state, Display *display, + Widget shell, unsigned long *ewmhseq) { - 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. @@ -262,11 +210,12 @@ void x11_redraw_icon(struct app_state *state, Widget shell) XtVaSetValues(shell, XtNiconPixmap, None, (char *)NULL); XtVaSetValues(shell, XtNiconPixmap, state->icon_pixmap, (char *)NULL); - if (state->use_ewmh_icons) { + if (state->flags & FLAG_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]; + XColor colours[NUM_TILE_COLOURS]; void *wm_icon; + int i; for (i = 0; i < XtNumber(colours); i++) { uint_least32_t *p = state->tile_colour[0]; @@ -274,6 +223,7 @@ void x11_redraw_icon(struct app_state *state, Widget shell) } XQueryColors(display, cmap, colours, XtNumber(colours)); + ewmh_icon_prepare_cmap(colours, XtNumber(colours)); wm_icon = ewmh_icon_generate(ewmhseq, colours); if (wm_icon) { XChangeProperty(display, XtWindow(shell), net_wm_icon, @@ -284,37 +234,68 @@ void x11_redraw_icon(struct app_state *state, Widget shell) } } -void x11_redraw_game(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 game = XtWindow(state->game); - uint_least32_t buf[3], *gp = state->board.game; - Dimension w, h; + Window goal = XtWindow(state->goal); + unsigned sz = state->goal_tile_sz; + unsigned long ewmhseq[9]; int i; - XtVaGetValues(state->game, XtNwidth, &w, XtNheight, &h, (char *)NULL); - w /= 5; h /= 5; + uint_least32_t gp[3] = { + state->board.goal[0], + state->board.goal[1], + state->board.goal[2] + }; - 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); + for (i = 0; i < 9; i++) { + int x = i%3, y = i/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; } - gp = buf; + + 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; } + if (icon_shell) + set_icon(state, display, icon_shell, ewmhseq); +} + +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; + unsigned sz = state->game_tile_sz; + int i; + + if (state->flags & FLAG_VIEW_GOAL_ON_GAME) + gp = game_overlay_goal(&state->board, buf); + /* 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; + if (mask & GAME_MASK & ~GOAL_MASK) { + 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; } } @@ -335,12 +316,10 @@ static Boolean do_render(void *data) { struct app_state *state = data; - x11_redraw_goal(state, state->render_goal_mask); + 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; } @@ -349,11 +328,12 @@ static void start_render(void *data, XtIntervalId *id) struct app_state *state = data; XtAppContext app; - if (state->render_proc) + if (state->render_game_mask & X11_PROC_INSTALLED) return; + state->render_game_mask |= X11_PROC_INSTALLED; app = XtWidgetToApplicationContext(state->game); - state->render_proc = XtAppAddWorkProc(app, do_render, state); + XtAppAddWorkProc(app, do_render, state); } void x11_queue_render(struct app_state *state, uint_fast32_t mask, int mode) @@ -361,14 +341,16 @@ void x11_queue_render(struct app_state *state, uint_fast32_t mask, int mode) uint_fast32_t changed = 0; XtAppContext app; + mask &= GAME_MASK; 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) + if (changed & X11_TICK_INSTALLED || !changed) return; + state->render_game_mask |= X11_TICK_INSTALLED; app = XtWidgetToApplicationContext(state->game); - state->render_tick = XtAppAddTimeOut(app, 3, start_render, state); + XtAppAddTimeOut(app, 3, start_render, state); }