]> git.draconx.ca Git - rrace.git/blob - src/x11.c
44bb8670ebec32c805917630acc9b3e32f1158d5
[rrace.git] / src / x11.c
1 /*
2  * X11 GUI for slide puzzle game
3  * Copyright © 2022-2023 Nick Bowler
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <assert.h>
24
25 #include <X11/Intrinsic.h>
26 #include <X11/StringDefs.h>
27 #include <X11/Xatom.h>
28 #include <X11/Shell.h>
29
30 #include "motif.h"
31 #include "ewmhicon.h"
32
33 /* Size of the traditional icon pixmap (multiple of 3) */
34 #define ICON_SIZE 48
35
36 /* Define to 1 to add highlights and delays for visually debugging redraws */
37 #if X11_RENDER_DEBUG
38 #  include <unistd.h>
39 #endif
40
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)
45
46 enum {
47         NUM_TILE_COLOURS = COLOUR_MAX*(TILE_MAX-1),
48         COLOUR_UI_GAMEBG = NUM_TILE_COLOURS,
49         COLOUR_UI_MAX
50 };
51 typedef Pixel colour_tab[COLOUR_UI_MAX];
52
53 #define COLOURRES(name, index, def) { \
54         (name), (NULL), XtRPixel, sizeof (String), \
55         offsetof(struct { colour_tab t; }, t[index]), \
56         XtRString, (def) }
57
58 static XtResource colour_resources[] = {
59         COLOURRES("colour0", 0, COLOUR0_PRIMARY),
60         COLOURRES("colour1", 1, COLOUR1_PRIMARY),
61         COLOURRES("colour2", 2, COLOUR2_PRIMARY),
62         COLOURRES("colour3", 3, COLOUR3_PRIMARY),
63         COLOURRES("colour4", 4, COLOUR4_PRIMARY),
64         COLOURRES("colour5", 5, COLOUR5_PRIMARY),
65
66         COLOURRES("colourDark0",  6, COLOUR0_DARK),
67         COLOURRES("colourDark1",  7, COLOUR1_DARK),
68         COLOURRES("colourDark2",  8, COLOUR2_DARK),
69         COLOURRES("colourDark3",  9, COLOUR3_DARK),
70         COLOURRES("colourDark4", 10, COLOUR4_DARK),
71         COLOURRES("colourDark5", 11, COLOUR5_DARK),
72
73         COLOURRES("colourLight0", 12, COLOUR0_LIGHT),
74         COLOURRES("colourLight1", 13, COLOUR1_LIGHT),
75         COLOURRES("colourLight2", 14, COLOUR2_LIGHT),
76         COLOURRES("colourLight3", 15, COLOUR3_LIGHT),
77         COLOURRES("colourLight4", 16, COLOUR4_LIGHT),
78         COLOURRES("colourLight5", 17, COLOUR5_LIGHT),
79
80         COLOURRES("gameBackground", COLOUR_UI_GAMEBG, "#181818")
81 };
82
83 static void init_colours(struct app_state *state, Widget shell)
84 {
85         unsigned i;
86
87         colour_tab colours;
88         XtGetApplicationResources(shell, &colours, colour_resources,
89                                   XtNumber(colour_resources), NULL, 0);
90
91         for (i = 0; i < NUM_TILE_COLOURS; i++) {
92                 unsigned tile = i % (TILE_MAX-1), shade = i / (TILE_MAX-1);
93
94                 state->tile_colour[tile][shade] = colours[i];
95         }
96
97         XtVaSetValues(state->game, XtNbackground, colours[COLOUR_UI_GAMEBG],
98                                    (char *)NULL);
99 }
100
101 void x11_initialize(struct app_state *state, Widget shell)
102 {
103         Display *display = XtDisplay(shell);
104         Screen *screen = XtScreen(shell);
105         Window root = RootWindowOfScreen(screen);
106         XGCValues gcv;
107
108         init_colours(state, shell);
109
110         gcv.line_width = 1;
111         state->tile_gc = XCreateGC(display, root, GCLineWidth, &gcv);
112
113         state->icon_pixmap = XCreatePixmap(display, root, ICON_SIZE, ICON_SIZE,
114                                            DefaultDepthOfScreen(screen));
115 }
116
117 static void draw_tile(struct app_state *state, Display *display, Drawable d,
118                       int tile, int gx, int gy, Dimension tw, Dimension th)
119 {
120         int tx = gx * tw, ty = gy * th;
121
122         XSegment topshadow[] = {
123                 { tx,      ty,      tx,      ty+th   },
124                 { tx+1,    ty,      tx+tw,   ty      },
125
126                 { tx+1,    ty+1,    tx+1,    ty+th-1 },
127                 { tx+2,    ty+1,    tx+tw-1, ty+1    }
128         };
129
130         XSegment bottomshadow[] = {
131                 { tx+1,    ty+th-1, tx+tw,   ty+th-1 },
132                 { tx+tw-1, ty+th-1, tx+tw-1, ty+1    },
133
134                 { tx+2,    ty+th-2, tx+tw-1, ty+th-2 },
135                 { tx+tw-2, ty+th-2, tx+tw-2, ty+2    }
136         };
137
138         XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_LIGHT]);
139         XDrawSegments(display, d, state->tile_gc, topshadow, XtNumber(topshadow));
140
141         XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_DARK]);
142         XDrawSegments(display, d, state->tile_gc, bottomshadow, XtNumber(bottomshadow));
143
144         XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_PRIMARY]);
145         XFillRectangle(display, d, state->tile_gc, tx+2, ty+2, tw-4, th-4);
146 }
147
148 static void
149 clear_rect(Display *display, Drawable d, int x, int y, int w, int h)
150 {
151 #if X11_RENDER_DEBUG
152         XRectangle r = { x, y, w, h };
153         XGCValues gcv;
154         GC gc;
155
156         gcv.foreground = 0xff0000;
157         gc = XCreateGC(display, d, GCForeground, &gcv);
158         XFillRectangles(display, d, gc, &r, 1);
159         XFreeGC(display, gc);
160
161         XFlush(display);
162         usleep(70000);
163 #endif
164
165         XClearArea(display, d, x, y, w, h, 0);
166 }
167
168 /*
169  * Efficiently clear all the border tiles in the game area.  The mask indicates
170  * which tiles need clearing, but for the border clear it is safe to wipe an
171  * entire row or column of the border using a single XClearArea.
172  */
173 static void clear_border(Display *display, Drawable d, unsigned sz)
174 {
175         clear_rect(display, d, 0,    0,    -1, sz);
176         clear_rect(display, d, 0,    4*sz, -1, sz);
177         clear_rect(display, d, 0,    0,    sz, -1);
178         clear_rect(display, d, 4*sz, 0,    sz, -1);
179 }
180
181 static int
182 redraw_tile(struct app_state *state, Display *display, Drawable d,
183             uint_least32_t *gp, int x, int y, unsigned sz)
184 {
185         unsigned tile = board_tile(gp, 5*y+x);
186         unsigned w = sz, h = sz;
187
188 #if X11_RENDER_DEBUG
189         if (d == XtWindow(state->game) || d == XtWindow(state->goal)) {
190                 XRectangle r = { x*w, y*h, w, h };
191                 XSetForeground(display, state->tile_gc, 0xff0000);
192                 XFillRectangles(display, d, state->tile_gc, &r, 1);
193                 XFlush(display);
194                 usleep(70000);
195         }
196 #endif
197         assert(tile < TILE_MAX);
198         if (tile == TILE_EMPTY) {
199                 XClearArea(display, d, x*w, y*h, w, h, 0);
200         } else {
201                 draw_tile(state, display, d, tile, x, y, w, h);
202         }
203
204         return tile;
205 }
206
207 static void set_icon(struct app_state *state, Display *display,
208                      Widget shell, unsigned long *ewmhseq)
209 {
210         /*
211          * Clear and reset XtNiconPixmap otherwise it seems dtwm will not
212          * notice the changed icon.
213          */
214         XtVaSetValues(shell, XtNiconPixmap, None, (char *)NULL);
215         XtVaSetValues(shell, XtNiconPixmap, state->icon_pixmap, (char *)NULL);
216
217         if (state->flags & FLAG_USE_EWMH_ICONS) {
218                 Atom net_wm_icon = XInternAtom(display, "_NET_WM_ICON", FALSE);
219                 Colormap cmap = DefaultColormapOfScreen(XtScreen(shell));
220                 XColor colours[(TILE_MAX-1)*COLOUR_MAX];
221                 void *wm_icon;
222                 int i;
223
224                 for (i = 0; i < XtNumber(colours); i++) {
225                         uint_least32_t *p = state->tile_colour[0];
226                         colours[i].pixel = p[i];
227                 }
228
229                 XQueryColors(display, cmap, colours, XtNumber(colours));
230                 wm_icon = ewmh_icon_generate(ewmhseq, colours);
231                 if (wm_icon) {
232                         XChangeProperty(display, XtWindow(shell), net_wm_icon,
233                                         XA_CARDINAL, 32, PropModeReplace,
234                                         wm_icon, EWMH_ICON_NELEM);
235                 }
236                 free(wm_icon);
237         }
238 }
239
240 void
241 x11_redraw_goal(struct app_state *state, uint_fast32_t mask, Widget icon_shell)
242 {
243         Display *display = XtDisplay(state->goal);
244         Window goal = XtWindow(state->goal);
245         unsigned sz = state->goal_tile_sz;
246         unsigned long ewmhseq[9];
247         int i;
248
249         uint_least32_t gp[3] = {
250                 state->board.goal[0],
251                 state->board.goal[1],
252                 state->board.goal[2]
253         };
254
255         for (i = 0; i < 9; i++) {
256                 int x = i%3, y = i/3;
257
258                 if (icon_shell) {
259                         unsigned tile;
260
261                         tile = redraw_tile(state, display, state->icon_pixmap,
262                                            gp, x, y, ICON_SIZE/3);
263                         ewmhseq[i] = COLOUR_MAX*0x10101 * (tile-1) + 0x20100;
264                 }
265
266                 if (mask & 1)
267                         redraw_tile(state, display, goal, gp, x, y, sz);
268
269                 /*
270                  * Goal bitmaps have a gap of 2 tiles between each row.
271                  * This funny shift will accomodate that.
272                  */
273                 mask >>= 1|x;
274         }
275
276         if (icon_shell)
277                 set_icon(state, display, icon_shell, ewmhseq);
278 }
279
280 void x11_redraw_game(struct app_state *state, uint_fast32_t mask)
281 {
282         Display *display = XtDisplay(state->goal);
283         Window game = XtWindow(state->game);
284         uint_least32_t buf[3], *gp = state->board.game;
285         unsigned sz = state->game_tile_sz;
286         int i;
287
288         if (state->flags & FLAG_VIEW_GOAL_ON_GAME) {
289                 for (i = 0; i < 3; i++) {
290                         buf[i] = state->board.goal[i];
291                         buf[i] = (gp[i] & ~GOAL_MASK) | (buf[i] << GOAL_SHIFT);
292                 }
293                 gp = buf;
294         }
295
296         /* Optimize the game end case where the outer frame is cleared */
297         if (((gp[0] | gp[1] | gp[2]) & ~GOAL_MASK) == 0) {
298                 clear_border(display, game, sz);
299                 mask &= GOAL_MASK;
300         }
301
302         for (i = 0; i < 25; i++) {
303                 if (mask & 1)
304                         redraw_tile(state, display, game, gp, i%5, i/5, sz);
305                 mask >>= 1;
306         }
307 }
308
309 /*
310  * Deferred redraw of tiles after resize/expose to avoid redundant drawing.
311  *
312  * Record any tiles that need to be redrawn due to resizes or expose events,
313  * then, after a short delay, perform all the accumulated redraws at once.
314  *
315  * This is implemented using both a work proc and a timeout, because it seems
316  * that rendering directly inside the timeout callback gives poor results:
317  * possibly redrawing outdated intermediate positions during "fast" resizes
318  * long after the resizing has stopped.  This does not happen when drawing
319  * from a work proc.
320  */
321 static Boolean do_render(void *data)
322 {
323         struct app_state *state = data;
324
325         x11_redraw_goal(state, state->render_goal_mask, NULL);
326         x11_redraw_game(state, state->render_game_mask);
327
328         state->render_goal_mask = state->render_game_mask = 0;
329         return True;
330 }
331
332 static void start_render(void *data, XtIntervalId *id)
333 {
334         struct app_state *state = data;
335         XtAppContext app;
336
337         if (state->render_game_mask & X11_PROC_INSTALLED)
338                 return;
339
340         state->render_game_mask |= X11_PROC_INSTALLED;
341         app = XtWidgetToApplicationContext(state->game);
342         XtAppAddWorkProc(app, do_render, state);
343 }
344
345 void x11_queue_render(struct app_state *state, uint_fast32_t mask, int mode)
346 {
347         uint_fast32_t changed = 0;
348         XtAppContext app;
349
350         mask &= GAME_MASK;
351         if (mode & RENDER_MODE_GOAL)
352                 changed |= state->render_goal_mask |= mask;
353         if (mode & RENDER_MODE_GAME)
354                 changed |= state->render_game_mask |= mask;
355
356         if (changed & X11_TICK_INSTALLED || !changed)
357                 return;
358
359         state->render_game_mask |= X11_TICK_INSTALLED;
360         app = XtWidgetToApplicationContext(state->game);
361         XtAppAddTimeOut(app, 3, start_render, state);
362 }