]> git.draconx.ca Git - rrace.git/blob - src/x11.c
x11: Shrink colour initialization code.
[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
52 static void init_colours(struct app_state *state, Widget shell)
53 {
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";
74
75         XtResource res[COLOUR_UI_MAX], t = {
76                 (char *)cdefs, 0, XtRPixel, sizeof (Pixel), 0, XtRString
77         };
78         Pixel colours[XtNumber(res)];
79         unsigned i;
80
81         for (i = 0; i < XtNumber(res); i++) {
82                 t.default_addr = strchr(t.resource_name, 0) + 1;
83                 res[i] = t;
84
85                 t.resource_name = (char *)t.default_addr + 8;
86                 t.resource_offset += sizeof colours[0];
87         }
88
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];
92                 p[i] = colours[i];
93         }
94         XtVaSetValues(state->game, XtNbackground, colours[i], (char *)NULL);
95 }
96
97 void x11_initialize(struct app_state *state, Widget shell)
98 {
99         Display *display = XtDisplay(shell);
100         Screen *screen = XtScreen(shell);
101         Window root = RootWindowOfScreen(screen);
102         XGCValues gcv;
103
104         init_colours(state, shell);
105
106         gcv.line_width = 1;
107         state->tile_gc = XCreateGC(display, root, GCLineWidth, &gcv);
108
109         state->icon_pixmap = XCreatePixmap(display, root, ICON_SIZE, ICON_SIZE,
110                                            DefaultDepthOfScreen(screen));
111 }
112
113 static void draw_tile(struct app_state *state, Display *display, Drawable d,
114                       int tile, int gx, int gy, Dimension tw, Dimension th)
115 {
116         int tx = gx * tw, ty = gy * th;
117
118         XSegment topshadow[] = {
119                 { tx,      ty,      tx,      ty+th   },
120                 { tx+1,    ty,      tx+tw,   ty      },
121
122                 { tx+1,    ty+1,    tx+1,    ty+th-1 },
123                 { tx+2,    ty+1,    tx+tw-1, ty+1    }
124         };
125
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    },
129
130                 { tx+2,    ty+th-2, tx+tw-1, ty+th-2 },
131                 { tx+tw-2, ty+th-2, tx+tw-2, ty+2    }
132         };
133
134         XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_LIGHT]);
135         XDrawSegments(display, d, state->tile_gc, topshadow, XtNumber(topshadow));
136
137         XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_DARK]);
138         XDrawSegments(display, d, state->tile_gc, bottomshadow, XtNumber(bottomshadow));
139
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);
142 }
143
144 static void
145 clear_rect(Display *display, Drawable d, int x, int y, int w, int h)
146 {
147 #if X11_RENDER_DEBUG
148         XRectangle r = { x, y, w, h };
149         XGCValues gcv;
150         GC gc;
151
152         gcv.foreground = 0xff0000;
153         gc = XCreateGC(display, d, GCForeground, &gcv);
154         XFillRectangles(display, d, gc, &r, 1);
155         XFreeGC(display, gc);
156
157         XFlush(display);
158         usleep(70000);
159 #endif
160
161         XClearArea(display, d, x, y, w, h, 0);
162 }
163
164 /*
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.
168  */
169 static void clear_border(Display *display, Drawable d, unsigned sz)
170 {
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);
175 }
176
177 static int
178 redraw_tile(struct app_state *state, Display *display, Drawable d,
179             uint_least32_t *gp, int x, int y, unsigned sz)
180 {
181         unsigned tile = board_tile(gp, 5*y+x);
182         unsigned w = sz, h = sz;
183
184 #if X11_RENDER_DEBUG
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);
189                 XFlush(display);
190                 usleep(70000);
191         }
192 #endif
193         assert(tile < TILE_MAX);
194         if (tile == TILE_EMPTY) {
195                 XClearArea(display, d, x*w, y*h, w, h, 0);
196         } else {
197                 draw_tile(state, display, d, tile, x, y, w, h);
198         }
199
200         return tile;
201 }
202
203 static void set_icon(struct app_state *state, Display *display,
204                      Widget shell, unsigned long *ewmhseq)
205 {
206         /*
207          * Clear and reset XtNiconPixmap otherwise it seems dtwm will not
208          * notice the changed icon.
209          */
210         XtVaSetValues(shell, XtNiconPixmap, None, (char *)NULL);
211         XtVaSetValues(shell, XtNiconPixmap, state->icon_pixmap, (char *)NULL);
212
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];
217                 void *wm_icon;
218                 int i;
219
220                 for (i = 0; i < XtNumber(colours); i++) {
221                         uint_least32_t *p = state->tile_colour[0];
222                         colours[i].pixel = p[i];
223                 }
224
225                 XQueryColors(display, cmap, colours, XtNumber(colours));
226                 wm_icon = ewmh_icon_generate(ewmhseq, colours);
227                 if (wm_icon) {
228                         XChangeProperty(display, XtWindow(shell), net_wm_icon,
229                                         XA_CARDINAL, 32, PropModeReplace,
230                                         wm_icon, EWMH_ICON_NELEM);
231                 }
232                 free(wm_icon);
233         }
234 }
235
236 void
237 x11_redraw_goal(struct app_state *state, uint_fast32_t mask, Widget icon_shell)
238 {
239         Display *display = XtDisplay(state->goal);
240         Window goal = XtWindow(state->goal);
241         unsigned sz = state->goal_tile_sz;
242         unsigned long ewmhseq[9];
243         int i;
244
245         uint_least32_t gp[3] = {
246                 state->board.goal[0],
247                 state->board.goal[1],
248                 state->board.goal[2]
249         };
250
251         for (i = 0; i < 9; i++) {
252                 int x = i%3, y = i/3;
253
254                 if (icon_shell) {
255                         unsigned tile;
256
257                         tile = redraw_tile(state, display, state->icon_pixmap,
258                                            gp, x, y, ICON_SIZE/3);
259                         ewmhseq[i] = COLOUR_MAX*0x10101 * (tile-1) + 0x20100;
260                 }
261
262                 if (mask & 1)
263                         redraw_tile(state, display, goal, gp, x, y, sz);
264
265                 /*
266                  * Goal bitmaps have a gap of 2 tiles between each row.
267                  * This funny shift will accomodate that.
268                  */
269                 mask >>= 1|x;
270         }
271
272         if (icon_shell)
273                 set_icon(state, display, icon_shell, ewmhseq);
274 }
275
276 void x11_redraw_game(struct app_state *state, uint_fast32_t mask)
277 {
278         Display *display = XtDisplay(state->goal);
279         Window game = XtWindow(state->game);
280         uint_least32_t buf[3], *gp = state->board.game;
281         unsigned sz = state->game_tile_sz;
282         int i;
283
284         if (state->flags & FLAG_VIEW_GOAL_ON_GAME) {
285                 for (i = 0; i < 3; i++) {
286                         buf[i] = state->board.goal[i];
287                         buf[i] = (gp[i] & ~GOAL_MASK) | (buf[i] << GOAL_SHIFT);
288                 }
289                 gp = buf;
290         }
291
292         /* Optimize the game end case where the outer frame is cleared */
293         if (((gp[0] | gp[1] | gp[2]) & ~GOAL_MASK) == 0) {
294                 clear_border(display, game, sz);
295                 mask &= GOAL_MASK;
296         }
297
298         for (i = 0; i < 25; i++) {
299                 if (mask & 1)
300                         redraw_tile(state, display, game, gp, i%5, i/5, sz);
301                 mask >>= 1;
302         }
303 }
304
305 /*
306  * Deferred redraw of tiles after resize/expose to avoid redundant drawing.
307  *
308  * Record any tiles that need to be redrawn due to resizes or expose events,
309  * then, after a short delay, perform all the accumulated redraws at once.
310  *
311  * This is implemented using both a work proc and a timeout, because it seems
312  * that rendering directly inside the timeout callback gives poor results:
313  * possibly redrawing outdated intermediate positions during "fast" resizes
314  * long after the resizing has stopped.  This does not happen when drawing
315  * from a work proc.
316  */
317 static Boolean do_render(void *data)
318 {
319         struct app_state *state = data;
320
321         x11_redraw_goal(state, state->render_goal_mask, NULL);
322         x11_redraw_game(state, state->render_game_mask);
323
324         state->render_goal_mask = state->render_game_mask = 0;
325         return True;
326 }
327
328 static void start_render(void *data, XtIntervalId *id)
329 {
330         struct app_state *state = data;
331         XtAppContext app;
332
333         if (state->render_game_mask & X11_PROC_INSTALLED)
334                 return;
335
336         state->render_game_mask |= X11_PROC_INSTALLED;
337         app = XtWidgetToApplicationContext(state->game);
338         XtAppAddWorkProc(app, do_render, state);
339 }
340
341 void x11_queue_render(struct app_state *state, uint_fast32_t mask, int mode)
342 {
343         uint_fast32_t changed = 0;
344         XtAppContext app;
345
346         mask &= GAME_MASK;
347         if (mode & RENDER_MODE_GOAL)
348                 changed |= state->render_goal_mask |= mask;
349         if (mode & RENDER_MODE_GAME)
350                 changed |= state->render_game_mask |= mask;
351
352         if (changed & X11_TICK_INSTALLED || !changed)
353                 return;
354
355         state->render_game_mask |= X11_TICK_INSTALLED;
356         app = XtWidgetToApplicationContext(state->game);
357         XtAppAddTimeOut(app, 3, start_render, state);
358 }