2 * X11 GUI for slide puzzle game
3 * Copyright © 2022-2023 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>
33 /* Size of the traditional icon pixmap (multiple of 3) */
36 /* Define to 1 to add highlights and delays for visually debugging redraws */
41 /* status flags in the upper bits are ORed into the game mask which only
42 * needs the lower 25 bits */
43 #define X11_TICK_INSTALLED (1ul << 31)
44 #define X11_PROC_INSTALLED (1ul << 30)
47 NUM_TILE_COLOURS = COLOUR_MAX*(TILE_MAX-1),
48 COLOUR_UI_GAMEBG = NUM_TILE_COLOURS,
52 static void init_colours(struct app_state *state, Widget shell)
54 static const char cdefs[] =
55 "colour0" "\0" COLOUR0_PRIMARY
56 "\0" "colourDark0" "\0" COLOUR0_DARK
57 "\0" "colourLight0" "\0" COLOUR0_LIGHT
58 "\0" "colour1" "\0" COLOUR1_PRIMARY
59 "\0" "colourDark1" "\0" COLOUR1_DARK
60 "\0" "colourLight1" "\0" COLOUR1_LIGHT
61 "\0" "colour2" "\0" COLOUR2_PRIMARY
62 "\0" "colourDark2" "\0" COLOUR2_DARK
63 "\0" "colourLight2" "\0" COLOUR2_LIGHT
64 "\0" "colour3" "\0" COLOUR3_PRIMARY
65 "\0" "colourDark3" "\0" COLOUR3_DARK
66 "\0" "colourLight3" "\0" COLOUR3_LIGHT
67 "\0" "colour4" "\0" COLOUR4_PRIMARY
68 "\0" "colourDark4" "\0" COLOUR4_DARK
69 "\0" "colourLight4" "\0" COLOUR4_LIGHT
70 "\0" "colour5" "\0" COLOUR5_PRIMARY
71 "\0" "colourDark5" "\0" COLOUR5_DARK
72 "\0" "colourLight5" "\0" COLOUR5_LIGHT
73 "\0" "gameBackground" "\0" "#181818";
75 XtResource res[COLOUR_UI_MAX], t = {
76 (char *)cdefs, 0, XtRPixel, sizeof (Pixel), 0, XtRString
78 Pixel colours[XtNumber(res)];
81 for (i = 0; i < XtNumber(res); i++) {
82 t.default_addr = strchr(t.resource_name, 0) + 1;
85 t.resource_name = (char *)t.default_addr + 8;
86 t.resource_offset += sizeof colours[0];
89 XtGetApplicationResources(shell, &colours, res, XtNumber(res), NULL, 0);
90 for (i = 0; i < NUM_TILE_COLOURS; i++) {
91 uint_least32_t *p = state->tile_colour[0];
94 XtVaSetValues(state->game, XtNbackground, colours[i], (char *)NULL);
97 void x11_initialize(struct app_state *state, Widget shell)
99 Display *display = XtDisplay(shell);
100 Screen *screen = XtScreen(shell);
101 Window root = RootWindowOfScreen(screen);
104 init_colours(state, shell);
107 state->tile_gc = XCreateGC(display, root, GCLineWidth, &gcv);
109 state->icon_pixmap = XCreatePixmap(display, root, ICON_SIZE, ICON_SIZE,
110 DefaultDepthOfScreen(screen));
113 static void draw_tile(struct app_state *state, Display *display, Drawable d,
114 int tile, int gx, int gy, Dimension tw, Dimension th)
116 int tx = gx * tw, ty = gy * th;
118 XSegment topshadow[] = {
119 { tx, ty, tx, ty+th },
120 { tx+1, ty, tx+tw, ty },
122 { tx+1, ty+1, tx+1, ty+th-1 },
123 { tx+2, ty+1, tx+tw-1, ty+1 }
126 XSegment bottomshadow[] = {
127 { tx+1, ty+th-1, tx+tw, ty+th-1 },
128 { tx+tw-1, ty+th-1, tx+tw-1, ty+1 },
130 { tx+2, ty+th-2, tx+tw-1, ty+th-2 },
131 { tx+tw-2, ty+th-2, tx+tw-2, ty+2 }
134 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_LIGHT]);
135 XDrawSegments(display, d, state->tile_gc, topshadow, XtNumber(topshadow));
137 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_DARK]);
138 XDrawSegments(display, d, state->tile_gc, bottomshadow, XtNumber(bottomshadow));
140 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_PRIMARY]);
141 XFillRectangle(display, d, state->tile_gc, tx+2, ty+2, tw-4, th-4);
145 clear_rect(Display *display, Drawable d, int x, int y, int w, int h)
148 XRectangle r = { x, y, w, h };
152 gcv.foreground = 0xff0000;
153 gc = XCreateGC(display, d, GCForeground, &gcv);
154 XFillRectangles(display, d, gc, &r, 1);
155 XFreeGC(display, gc);
161 XClearArea(display, d, x, y, w, h, 0);
165 * Efficiently clear all the border tiles in the game area. The mask indicates
166 * which tiles need clearing, but for the border clear it is safe to wipe an
167 * entire row or column of the border using a single XClearArea.
169 static void clear_border(Display *display, Drawable d, unsigned sz)
171 clear_rect(display, d, 0, 0, -1, sz);
172 clear_rect(display, d, 0, 4*sz, -1, sz);
173 clear_rect(display, d, 0, 0, sz, -1);
174 clear_rect(display, d, 4*sz, 0, sz, -1);
178 redraw_tile(struct app_state *state, Display *display, Drawable d,
179 uint_least32_t *gp, int x, int y, unsigned sz)
181 unsigned tile = board_tile(gp, 5*y+x);
182 unsigned w = sz, h = sz;
185 if (d == XtWindow(state->game) || d == XtWindow(state->goal)) {
186 XRectangle r = { x*w, y*h, w, h };
187 XSetForeground(display, state->tile_gc, 0xff0000);
188 XFillRectangles(display, d, state->tile_gc, &r, 1);
193 assert(tile < TILE_MAX);
194 if (tile == TILE_EMPTY) {
195 XClearArea(display, d, x*w, y*h, w, h, 0);
197 draw_tile(state, display, d, tile, x, y, w, h);
203 static void set_icon(struct app_state *state, Display *display,
204 Widget shell, unsigned long *ewmhseq)
207 * Clear and reset XtNiconPixmap otherwise it seems dtwm will not
208 * notice the changed icon.
210 XtVaSetValues(shell, XtNiconPixmap, None, (char *)NULL);
211 XtVaSetValues(shell, XtNiconPixmap, state->icon_pixmap, (char *)NULL);
213 if (state->flags & FLAG_USE_EWMH_ICONS) {
214 Atom net_wm_icon = XInternAtom(display, "_NET_WM_ICON", FALSE);
215 Colormap cmap = DefaultColormapOfScreen(XtScreen(shell));
216 XColor colours[NUM_TILE_COLOURS];
220 for (i = 0; i < XtNumber(colours); i++) {
221 uint_least32_t *p = state->tile_colour[0];
222 colours[i].pixel = p[i];
225 XQueryColors(display, cmap, colours, XtNumber(colours));
226 ewmh_icon_prepare_cmap(colours, XtNumber(colours));
227 wm_icon = ewmh_icon_generate(ewmhseq, colours);
229 XChangeProperty(display, XtWindow(shell), net_wm_icon,
230 XA_CARDINAL, 32, PropModeReplace,
231 wm_icon, EWMH_ICON_NELEM);
238 x11_redraw_goal(struct app_state *state, uint_fast32_t mask, Widget icon_shell)
240 Display *display = XtDisplay(state->goal);
241 Window goal = XtWindow(state->goal);
242 unsigned sz = state->goal_tile_sz;
243 unsigned long ewmhseq[9];
246 uint_least32_t gp[3] = {
247 state->board.goal[0],
248 state->board.goal[1],
252 for (i = 0; i < 9; i++) {
253 int x = i%3, y = i/3;
258 tile = redraw_tile(state, display, state->icon_pixmap,
259 gp, x, y, ICON_SIZE/3);
260 ewmhseq[i] = COLOUR_MAX*0x10101 * (tile-1) + 0x20100;
264 redraw_tile(state, display, goal, gp, x, y, sz);
267 * Goal bitmaps have a gap of 2 tiles between each row.
268 * This funny shift will accomodate that.
274 set_icon(state, display, icon_shell, ewmhseq);
277 void x11_redraw_game(struct app_state *state, uint_fast32_t mask)
279 Display *display = XtDisplay(state->goal);
280 Window game = XtWindow(state->game);
281 uint_least32_t buf[3], *gp = state->board.game;
282 unsigned sz = state->game_tile_sz;
285 if (state->flags & FLAG_VIEW_GOAL_ON_GAME)
286 gp = game_overlay_goal(&state->board, buf);
288 /* Optimize the game end case where the outer frame is cleared */
289 if (mask & GAME_MASK & ~GOAL_MASK) {
290 if (((gp[0] | gp[1] | gp[2]) & ~GOAL_MASK) == 0) {
291 clear_border(display, game, sz);
296 for (i = 0; i < 25; i++) {
298 redraw_tile(state, display, game, gp, i%5, i/5, sz);
304 * Deferred redraw of tiles after resize/expose to avoid redundant drawing.
306 * Record any tiles that need to be redrawn due to resizes or expose events,
307 * then, after a short delay, perform all the accumulated redraws at once.
309 * This is implemented using both a work proc and a timeout, because it seems
310 * that rendering directly inside the timeout callback gives poor results:
311 * possibly redrawing outdated intermediate positions during "fast" resizes
312 * long after the resizing has stopped. This does not happen when drawing
315 static Boolean do_render(void *data)
317 struct app_state *state = data;
319 x11_redraw_goal(state, state->render_goal_mask, NULL);
320 x11_redraw_game(state, state->render_game_mask);
322 state->render_goal_mask = state->render_game_mask = 0;
326 static void start_render(void *data, XtIntervalId *id)
328 struct app_state *state = data;
331 if (state->render_game_mask & X11_PROC_INSTALLED)
334 state->render_game_mask |= X11_PROC_INSTALLED;
335 app = XtWidgetToApplicationContext(state->game);
336 XtAppAddWorkProc(app, do_render, state);
339 void x11_queue_render(struct app_state *state, uint_fast32_t mask, int mode)
341 uint_fast32_t changed = 0;
345 if (mode & RENDER_MODE_GOAL)
346 changed |= state->render_goal_mask |= mask;
347 if (mode & RENDER_MODE_GAME)
348 changed |= state->render_game_mask |= mask;
350 if (changed & X11_TICK_INSTALLED || !changed)
353 state->render_game_mask |= X11_TICK_INSTALLED;
354 app = XtWidgetToApplicationContext(state->game);
355 XtAppAddTimeOut(app, 3, start_render, state);