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>
33 /* Size of the traditional icon pixmap (multiple of 3) */
36 /* Define to 1 to add highlights and delays for visually debugging redraws */
42 NUM_TILE_COLOURS = COLOUR_MAX*(TILE_MAX-1),
43 COLOUR_UI_GAMEBG = NUM_TILE_COLOURS,
46 typedef Pixel colour_tab[COLOUR_UI_MAX];
48 #define COLOURRES(name, index, def) { \
49 (name), (NULL), XtRPixel, sizeof (String), \
50 offsetof(struct { colour_tab t; }, t[index]), \
53 static XtResource colour_resources[] = {
54 COLOURRES("colour0", 0, COLOUR0_PRIMARY),
55 COLOURRES("colour1", 1, COLOUR1_PRIMARY),
56 COLOURRES("colour2", 2, COLOUR2_PRIMARY),
57 COLOURRES("colour3", 3, COLOUR3_PRIMARY),
58 COLOURRES("colour4", 4, COLOUR4_PRIMARY),
59 COLOURRES("colour5", 5, COLOUR5_PRIMARY),
61 COLOURRES("colourDark0", 6, COLOUR0_DARK),
62 COLOURRES("colourDark1", 7, COLOUR1_DARK),
63 COLOURRES("colourDark2", 8, COLOUR2_DARK),
64 COLOURRES("colourDark3", 9, COLOUR3_DARK),
65 COLOURRES("colourDark4", 10, COLOUR4_DARK),
66 COLOURRES("colourDark5", 11, COLOUR5_DARK),
68 COLOURRES("colourLight0", 12, COLOUR0_LIGHT),
69 COLOURRES("colourLight1", 13, COLOUR1_LIGHT),
70 COLOURRES("colourLight2", 14, COLOUR2_LIGHT),
71 COLOURRES("colourLight3", 15, COLOUR3_LIGHT),
72 COLOURRES("colourLight4", 16, COLOUR4_LIGHT),
73 COLOURRES("colourLight5", 17, COLOUR5_LIGHT),
75 COLOURRES("gameBackground", COLOUR_UI_GAMEBG, "#181818")
78 static void init_colours(struct app_state *state, Widget shell)
83 XtGetApplicationResources(shell, &colours, colour_resources,
84 XtNumber(colour_resources), NULL, 0);
86 for (i = 0; i < NUM_TILE_COLOURS; i++) {
87 unsigned tile = i % (TILE_MAX-1), shade = i / (TILE_MAX-1);
89 state->tile_colour[tile][shade] = colours[i];
92 XtVaSetValues(state->game, XtNbackground, colours[COLOUR_UI_GAMEBG],
96 void x11_initialize(struct app_state *state, Widget shell)
98 Display *display = XtDisplay(shell);
99 Screen *screen = XtScreen(shell);
100 Window root = RootWindowOfScreen(screen);
103 init_colours(state, shell);
106 state->tile_gc = XCreateGC(display, root, GCLineWidth, &gcv);
108 state->icon_pixmap = XCreatePixmap(display, root, ICON_SIZE, ICON_SIZE,
109 DefaultDepthOfScreen(screen));
112 static void draw_tile(struct app_state *state, Display *display, Drawable d,
113 int tile, int gx, int gy, Dimension tw, Dimension th)
115 int tx = gx * tw, ty = gy * th;
117 XSegment topshadow[] = {
118 { tx, ty, tx, ty+th },
119 { tx+1, ty, tx+tw, ty },
121 { tx+1, ty+1, tx+1, ty+th-1 },
122 { tx+2, ty+1, tx+tw-1, ty+1 }
125 XSegment bottomshadow[] = {
126 { tx+1, ty+th-1, tx+tw, ty+th-1 },
127 { tx+tw-1, ty+th-1, tx+tw-1, ty+1 },
129 { tx+2, ty+th-2, tx+tw-1, ty+th-2 },
130 { tx+tw-2, ty+th-2, tx+tw-2, ty+2 }
133 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_LIGHT]);
134 XDrawSegments(display, d, state->tile_gc, topshadow, XtNumber(topshadow));
136 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_DARK]);
137 XDrawSegments(display, d, state->tile_gc, bottomshadow, XtNumber(bottomshadow));
139 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_PRIMARY]);
140 XFillRectangle(display, d, state->tile_gc, tx+2, ty+2, tw-4, th-4);
144 clear_rect(Display *display, Drawable d, int x, int y, int w, int h)
147 XRectangle r = { x, y, w, h };
151 gcv.foreground = 0xff0000;
152 gc = XCreateGC(display, d, GCForeground, &gcv);
153 XFillRectangles(display, d, gc, &r, 1);
154 XFreeGC(display, gc);
160 XClearArea(display, d, x, y, w, h, 0);
164 * Efficiently clear all the border tiles in the game area. The mask indicates
165 * which tiles need clearing, but for the border clear it is safe to wipe an
166 * entire row or column of the border using a single XClearArea.
168 static void clear_border(Display *display, Drawable d, unsigned sz)
170 clear_rect(display, d, 0, 0, -1, sz);
171 clear_rect(display, d, 0, 4*sz, -1, sz);
172 clear_rect(display, d, 0, 0, sz, -1);
173 clear_rect(display, d, 4*sz, 0, sz, -1);
177 redraw_tile(struct app_state *state, Display *display, Drawable d,
178 uint_least32_t *gp, int x, int y, unsigned sz)
180 unsigned tile = board_tile(gp, 5*y+x);
181 unsigned w = sz, h = sz;
184 if (d == XtWindow(state->game) || d == XtWindow(state->goal)) {
185 XRectangle r = { x*w, y*h, w, h };
186 XSetForeground(display, state->tile_gc, 0xff0000);
187 XFillRectangles(display, d, state->tile_gc, &r, 1);
192 assert(tile < TILE_MAX);
193 if (tile == TILE_EMPTY) {
194 XClearArea(display, d, x*w, y*h, w, h, 0);
196 draw_tile(state, display, d, tile, x, y, w, h);
202 static void set_icon(struct app_state *state, Display *display,
203 Widget shell, unsigned long *ewmhseq)
206 * Clear and reset XtNiconPixmap otherwise it seems dtwm will not
207 * notice the changed icon.
209 XtVaSetValues(shell, XtNiconPixmap, None, (char *)NULL);
210 XtVaSetValues(shell, XtNiconPixmap, state->icon_pixmap, (char *)NULL);
212 if (state->use_ewmh_icons) {
213 Atom net_wm_icon = XInternAtom(display, "_NET_WM_ICON", FALSE);
214 Colormap cmap = DefaultColormapOfScreen(XtScreen(shell));
215 XColor colours[(TILE_MAX-1)*COLOUR_MAX];
219 for (i = 0; i < XtNumber(colours); i++) {
220 uint_least32_t *p = state->tile_colour[0];
221 colours[i].pixel = p[i];
224 XQueryColors(display, cmap, colours, XtNumber(colours));
225 wm_icon = ewmh_icon_generate(ewmhseq, colours);
227 XChangeProperty(display, XtWindow(shell), net_wm_icon,
228 XA_CARDINAL, 32, PropModeReplace,
229 wm_icon, EWMH_ICON_NELEM);
236 x11_redraw_goal(struct app_state *state, uint_fast32_t mask, Widget icon_shell)
238 Display *display = XtDisplay(state->goal);
239 Window goal = XtWindow(state->goal);
240 unsigned sz = state->goal_tile_sz;
241 unsigned long ewmhseq[9];
244 uint_least32_t gp[3] = {
245 state->board.goal[0],
246 state->board.goal[1],
250 for (i = 0; i < 9; i++) {
251 int x = i%3, y = i/3;
256 tile = redraw_tile(state, display, state->icon_pixmap,
257 gp, x, y, ICON_SIZE/3);
258 ewmhseq[i] = COLOUR_MAX*0x10101 * (tile-1) + 0x20100;
262 redraw_tile(state, display, goal, gp, x, y, sz);
265 * Goal bitmaps have a gap of 2 tiles between each row.
266 * This funny shift will accomodate that.
272 set_icon(state, display, icon_shell, ewmhseq);
275 void x11_redraw_game(struct app_state *state, uint_fast32_t mask)
277 Display *display = XtDisplay(state->goal);
278 Window game = XtWindow(state->game);
279 uint_least32_t buf[3], *gp = state->board.game;
280 unsigned sz = state->game_tile_sz;
283 if (state->view_goal_on_game) {
284 for (i = 0; i < 3; i++) {
285 buf[i] = state->board.goal[i];
286 buf[i] = (gp[i] & ~GOAL_MASK) | (buf[i] << GOAL_SHIFT);
291 /* Optimize the game end case where the outer frame is cleared */
292 if (((gp[0] | gp[1] | gp[2]) & ~GOAL_MASK) == 0) {
293 clear_border(display, game, sz);
297 for (i = 0; i < 25; i++) {
299 redraw_tile(state, display, game, gp, i%5, i/5, sz);
305 * Deferred redraw of tiles after resize/expose to avoid redundant drawing.
307 * Record any tiles that need to be redrawn due to resizes or expose events,
308 * then, after a short delay, perform all the accumulated redraws at once.
310 * This is implemented using both a work proc and a timeout, because it seems
311 * that rendering directly inside the timeout callback gives poor results:
312 * possibly redrawing outdated intermediate positions during "fast" resizes
313 * long after the resizing has stopped. This does not happen when drawing
316 static Boolean do_render(void *data)
318 struct app_state *state = data;
320 x11_redraw_goal(state, state->render_goal_mask, NULL);
321 x11_redraw_game(state, state->render_game_mask);
323 state->render_goal_mask = state->render_game_mask = 0;
324 state->render_proc = 0;
325 state->render_tick = 0;
329 static void start_render(void *data, XtIntervalId *id)
331 struct app_state *state = data;
334 if (state->render_proc)
337 app = XtWidgetToApplicationContext(state->game);
338 state->render_proc = XtAppAddWorkProc(app, do_render, state);
341 void x11_queue_render(struct app_state *state, uint_fast32_t mask, int mode)
343 uint_fast32_t changed = 0;
346 if (mode & RENDER_MODE_GOAL)
347 changed |= state->render_goal_mask |= mask;
348 if (mode & RENDER_MODE_GAME)
349 changed |= state->render_game_mask |= mask;
351 if (state->render_tick || !changed)
354 app = XtWidgetToApplicationContext(state->game);
355 state->render_tick = XtAppAddTimeOut(app, 3, start_render, state);