2 * X11 GUI for slide puzzle game
3 * Copyright © 2022 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
25 #include <X11/Intrinsic.h>
26 #include <X11/StringDefs.h>
27 #include <X11/Xatom.h>
28 #include <X11/Shell.h>
31 /* Size of the traditional icon pixmap (multiple of 3) */
34 /* Define to 1 to add highlights and delays for visually debugging redraws */
40 NUM_TILE_COLOURS = COLOUR_MAX*(TILE_MAX-1),
41 COLOUR_UI_GAMEBG = NUM_TILE_COLOURS,
44 typedef Pixel colour_tab[COLOUR_UI_MAX];
46 #define COLOURRES(name, index, def) { \
47 (name), (NULL), XtRPixel, sizeof (String), \
48 offsetof(struct { colour_tab t; }, t[index]), \
51 static XtResource colour_resources[] = {
52 COLOURRES("colour0", 0, COLOUR0_PRIMARY),
53 COLOURRES("colour1", 1, COLOUR1_PRIMARY),
54 COLOURRES("colour2", 2, COLOUR2_PRIMARY),
55 COLOURRES("colour3", 3, COLOUR3_PRIMARY),
56 COLOURRES("colour4", 4, COLOUR4_PRIMARY),
57 COLOURRES("colour5", 5, COLOUR5_PRIMARY),
59 COLOURRES("colourDark0", 6, COLOUR0_DARK),
60 COLOURRES("colourDark1", 7, COLOUR1_DARK),
61 COLOURRES("colourDark2", 8, COLOUR2_DARK),
62 COLOURRES("colourDark3", 9, COLOUR3_DARK),
63 COLOURRES("colourDark4", 10, COLOUR4_DARK),
64 COLOURRES("colourDark5", 11, COLOUR5_DARK),
66 COLOURRES("colourLight0", 12, COLOUR0_LIGHT),
67 COLOURRES("colourLight1", 13, COLOUR1_LIGHT),
68 COLOURRES("colourLight2", 14, COLOUR2_LIGHT),
69 COLOURRES("colourLight3", 15, COLOUR3_LIGHT),
70 COLOURRES("colourLight4", 16, COLOUR4_LIGHT),
71 COLOURRES("colourLight5", 17, COLOUR5_LIGHT),
73 COLOURRES("gameBackground", COLOUR_UI_GAMEBG, "#181818")
76 static void init_colours(struct app_state *state, Widget shell)
81 XtGetApplicationResources(shell, &colours, colour_resources,
82 XtNumber(colour_resources), NULL, 0);
84 for (i = 0; i < NUM_TILE_COLOURS; i++) {
85 unsigned tile = i % (TILE_MAX-1), shade = i / (TILE_MAX-1);
87 state->tile_colour[tile][shade] = colours[i];
90 XtVaSetValues(state->game, XtNbackground, colours[COLOUR_UI_GAMEBG],
94 void x11_initialize(struct app_state *state, Widget shell)
96 Display *display = XtDisplay(shell);
97 Screen *screen = XtScreen(shell);
98 Window root = RootWindowOfScreen(screen);
101 init_colours(state, shell);
104 state->tile_gc = XCreateGC(display, root, GCLineWidth, &gcv);
106 state->icon_pixmap = XCreatePixmap(display, root, ICON_SIZE, ICON_SIZE,
107 DefaultDepthOfScreen(screen));
110 static void draw_tile(struct app_state *state, Display *display, Drawable d,
111 int tile, int gx, int gy, Dimension tw, Dimension th)
113 int tx = gx * tw, ty = gy * th;
115 XSegment topshadow[] = {
116 { tx, ty, tx, ty+th },
117 { tx+1, ty, tx+tw, ty },
119 { tx+1, ty+1, tx+1, ty+th-1 },
120 { tx+2, ty+1, tx+tw-1, ty+1 }
123 XSegment bottomshadow[] = {
124 { tx+1, ty+th-1, tx+tw, ty+th-1 },
125 { tx+tw-1, ty+th-1, tx+tw-1, ty+1 },
127 { tx+2, ty+th-2, tx+tw-1, ty+th-2 },
128 { tx+tw-2, ty+th-2, tx+tw-2, ty+2 }
131 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_LIGHT]);
132 XDrawSegments(display, d, state->tile_gc, topshadow, XtNumber(topshadow));
134 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_DARK]);
135 XDrawSegments(display, d, state->tile_gc, bottomshadow, XtNumber(bottomshadow));
137 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_PRIMARY]);
138 XFillRectangle(display, d, state->tile_gc, tx+2, ty+2, tw-4, th-4);
142 * Clear a contiguous rectangle of tiles from top-left (x0, y0) to
143 * bottom-right (x1, y1).
146 clear_tiles(struct app_state *state, Display *display, Drawable d,
147 int x0, int y0, int x1, int y1, Dimension w, Dimension h)
149 XRectangle r = { x0*w, y0*h, (x1+1)*w, (y1+1)*h };
152 XSetForeground(display, state->tile_gc, 0xff0000);
153 XFillRectangles(display, d, state->tile_gc, &r, 1);
158 XClearArea(display, d, r.x, r.y, r.width, r.height, 0);
162 * Efficiently clear all the border tiles in the game area. The mask indicates
163 * which tiles need clearing, but for the border clear it is safe to wipe an
164 * entire row or column of the border using a single XClearArea.
166 * The idea is to pick whichever row or column has the most tiles to clear,
167 * clear them, and then repeat until none are left (repeats at most 4 times).
169 static void clear_border(struct app_state *state, Display *display, Drawable d,
170 Dimension w, Dimension h, uint_fast32_t mask)
172 uint_fast32_t best_mask = 0;
176 if (!(mask &= ~GOAL_MASK & 0x1ffffff))
179 for (i = 0; i < 4; i++) {
180 uint_fast32_t this_mask, tmp;
184 this_mask = board_column(4*(i & 1));
186 this_mask = board_row(4*(i & 1));
189 for (tmp = mask & this_mask; tmp; this_count++)
192 if (this_count > best_count) {
193 best_count = this_count;
194 best_mask = this_mask;
200 case 0: clear_tiles(state, display, d, 0, 0, 4, 0, w, h); break;
201 case 1: clear_tiles(state, display, d, 0, 4, 4, 4, w, h); break;
202 case 2: clear_tiles(state, display, d, 0, 0, 0, 4, w, h); break;
203 case 3: clear_tiles(state, display, d, 4, 0, 4, 4, w, h); break;
206 clear_border(state, display, d, w, h, mask & ~best_mask);
210 redraw_tile(struct app_state *state, Display *display, Drawable d,
211 uint_fast32_t bit0, uint_fast32_t bit1, uint_fast32_t bit2,
212 int x, int y, Dimension w, Dimension h)
214 uint_fast32_t pos = board_position(x, y);
215 unsigned char tile = 0;
218 if (d == XtWindow(state->game) || d == XtWindow(state->goal)) {
219 XRectangle r = { x*w, y*h, w, h };
220 XSetForeground(display, state->tile_gc, 0xff0000);
221 XFillRectangles(display, d, state->tile_gc, &r, 1);
227 if (bit0 & pos) tile |= 1;
228 if (bit1 & pos) tile |= 2;
229 if (bit2 & pos) tile |= 4;
230 assert(tile < TILE_MAX);
232 if (tile == TILE_EMPTY) {
233 XClearArea(display, d, x*w, y*h, w, h, 0);
235 draw_tile(state, display, d, tile, x, y, w, h);
242 redraw_goal_tile(struct app_state *state, Display *display, Drawable d,
243 int x, int y, Dimension w, Dimension h)
245 uint_least16_t *gp = state->board.goal;
247 return redraw_tile(state, display, d, gp[0], gp[1], gp[2], x, y, w, h);
250 void x11_redraw_goal(struct app_state *state, uint_fast32_t mask)
252 Display *display = XtDisplay(state->goal);
253 Window goal = XtWindow(state->goal);
257 XtVaGetValues(state->goal, XtNwidth, &w, XtNheight, &h, (char *)NULL);
260 for (i = 0; i < 9; i++) {
261 int x = i%3, y = i/3;
264 redraw_goal_tile(state, display, goal, x, y, w, h);
268 * Goal bitmaps have a gap of 2 tiles between each row.
269 * This funny shift will accomodate that.
275 /* Render the goal area as the window's icon */
276 void x11_redraw_icon(struct app_state *state, Widget shell)
278 Display *display = XtDisplay(shell);
279 Dimension tilesz = ICON_SIZE/3;
282 XColor colours[(TILE_MAX-1)*COLOUR_MAX];
283 unsigned long *icons[ICON_MAX];
284 void *wm_icon = NULL;
286 if (state->use_ewmh_icons && (wm_icon = ewmh_icon_alloc(icons))) {
287 Colormap cmap = DefaultColormapOfScreen(XtScreen(shell));
289 for (i = 0; i < TILE_MAX-1; i++) {
290 for (j = 0; j < COLOUR_MAX; j++) {
291 XColor *c = &colours[i*COLOUR_MAX+j];
292 c->pixel = state->tile_colour[i][j];
296 XQueryColors(display, cmap, colours, i*j);
299 for (i = 0; i < 9; i++) {
300 tile = redraw_goal_tile(state, display, state->icon_pixmap,
301 i%3, i/3, tilesz, tilesz);
304 XColor *c = &colours[(tile-1)*COLOUR_MAX];
305 ewmh_tile16(icons[ICON_16x16], c, i%3, i/3);
306 ewmh_tile24(icons[ICON_24x24], c, i%3, i/3);
307 ewmh_tile32(icons[ICON_32x32], c, i%3, i/3);
308 ewmh_tile48(icons[ICON_48x48], c, i%3, i/3);
313 * Clear and reset XtNiconPixmap otherwise it seems dtwm will not
314 * notice the changed icon.
316 XtVaSetValues(shell, XtNiconPixmap, None, (char *)NULL);
317 XtVaSetValues(shell, XtNiconPixmap, state->icon_pixmap, (char *)NULL);
320 Atom net_wm_icon = XInternAtom(display, "_NET_WM_ICON", FALSE);
322 XChangeProperty(display, XtWindow(shell), net_wm_icon,
323 XA_CARDINAL, 32, PropModeReplace,
324 wm_icon, EWMH_ICON_NELEM);
330 void x11_redraw_game(struct app_state *state, uint_fast32_t mask)
332 Display *display = XtDisplay(state->goal);
333 Window game = XtWindow(state->game);
334 uint_least32_t buf[3], *gp = state->board.game;
338 XtVaGetValues(state->game, XtNwidth, &w, XtNheight, &h, (char *)NULL);
341 if (state->view_goal_on_game) {
342 for (i = 0; i < 3; i++) {
343 buf[i] = state->board.goal[i];
344 buf[i] = (gp[i] & ~GOAL_MASK) | (buf[i] << GOAL_SHIFT);
349 /* Optimize the game end case where the outer frame is cleared */
350 if (((gp[0] | gp[1] | gp[2]) & ~GOAL_MASK) == 0) {
351 clear_border(state, display, game, w, h, mask);
355 for (i = 0; i < 25; i++) {
357 redraw_tile(state, display, game,