]> git.draconx.ca Git - rrace.git/blob - src/x11.c
Make EWMH icon generation more abstract.
[rrace.git] / src / x11.c
1 /*
2  * X11 GUI for slide puzzle game
3  * Copyright © 2022 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 enum {
42         NUM_TILE_COLOURS = COLOUR_MAX*(TILE_MAX-1),
43         COLOUR_UI_GAMEBG = NUM_TILE_COLOURS,
44         COLOUR_UI_MAX
45 };
46 typedef Pixel colour_tab[COLOUR_UI_MAX];
47
48 #define COLOURRES(name, index, def) { \
49         (name), (NULL), XtRPixel, sizeof (String), \
50         offsetof(struct { colour_tab t; }, t[index]), \
51         XtRString, (def) }
52
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),
60
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),
67
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),
74
75         COLOURRES("gameBackground", COLOUR_UI_GAMEBG, "#181818")
76 };
77
78 static void init_colours(struct app_state *state, Widget shell)
79 {
80         unsigned i;
81
82         colour_tab colours;
83         XtGetApplicationResources(shell, &colours, colour_resources,
84                                   XtNumber(colour_resources), NULL, 0);
85
86         for (i = 0; i < NUM_TILE_COLOURS; i++) {
87                 unsigned tile = i % (TILE_MAX-1), shade = i / (TILE_MAX-1);
88
89                 state->tile_colour[tile][shade] = colours[i];
90         }
91
92         XtVaSetValues(state->game, XtNbackground, colours[COLOUR_UI_GAMEBG],
93                                    (char *)NULL);
94 }
95
96 void x11_initialize(struct app_state *state, Widget shell)
97 {
98         Display *display = XtDisplay(shell);
99         Screen *screen = XtScreen(shell);
100         Window root = RootWindowOfScreen(screen);
101         XGCValues gcv;
102
103         init_colours(state, shell);
104
105         gcv.line_width = 1;
106         state->tile_gc = XCreateGC(display, root, GCLineWidth, &gcv);
107
108         state->icon_pixmap = XCreatePixmap(display, root, ICON_SIZE, ICON_SIZE,
109                                            DefaultDepthOfScreen(screen));
110 }
111
112 static void draw_tile(struct app_state *state, Display *display, Drawable d,
113                       int tile, int gx, int gy, Dimension tw, Dimension th)
114 {
115         int tx = gx * tw, ty = gy * th;
116
117         XSegment topshadow[] = {
118                 { tx,      ty,      tx,      ty+th   },
119                 { tx+1,    ty,      tx+tw,   ty      },
120
121                 { tx+1,    ty+1,    tx+1,    ty+th-1 },
122                 { tx+2,    ty+1,    tx+tw-1, ty+1    }
123         };
124
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    },
128
129                 { tx+2,    ty+th-2, tx+tw-1, ty+th-2 },
130                 { tx+tw-2, ty+th-2, tx+tw-2, ty+2    }
131         };
132
133         XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_LIGHT]);
134         XDrawSegments(display, d, state->tile_gc, topshadow, XtNumber(topshadow));
135
136         XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_DARK]);
137         XDrawSegments(display, d, state->tile_gc, bottomshadow, XtNumber(bottomshadow));
138
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);
141 }
142
143 /*
144  * Clear a contiguous rectangle of tiles from top-left (x0, y0) to
145  * bottom-right (x1, y1).
146  */
147 static void
148 clear_tiles(struct app_state *state, Display *display, Drawable d,
149             int x0, int y0, int x1, int y1, Dimension w, Dimension h)
150 {
151         XRectangle r = { x0*w, y0*h, (x1+1)*w, (y1+1)*h };
152
153 #if X11_RENDER_DEBUG
154         XSetForeground(display, state->tile_gc, 0xff0000);
155         XFillRectangles(display, d, state->tile_gc, &r, 1);
156         XFlush(display);
157         usleep(70000);
158 #endif
159
160         XClearArea(display, d, r.x, r.y, r.width, r.height, 0);
161 }
162
163 /*
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.
167  *
168  * The idea is to pick whichever row or column has the most tiles to clear,
169  * clear them, and then repeat until none are left (repeats at most 4 times).
170  */
171 static void clear_border(struct app_state *state, Display *display, Drawable d,
172                          Dimension w, Dimension h, uint_fast32_t mask)
173 {
174         uint_fast32_t best_mask = 0;
175         int best_count = -1;
176         int i, best = -1;
177
178         if (!(mask &= ~GOAL_MASK & 0x1ffffff))
179                 return;
180
181         for (i = 0; i < 4; i++) {
182                 uint_fast32_t this_mask, tmp;
183                 int this_count = 0;
184
185                 if (i & 2)
186                         this_mask = board_column(4*(i & 1));
187                 else
188                         this_mask = board_row(4*(i & 1));
189
190                 /* Count set bits */
191                 for (tmp = mask & this_mask; tmp; this_count++)
192                         tmp &= tmp - 1;
193
194                 if (this_count > best_count) {
195                         best_count = this_count;
196                         best_mask = this_mask;
197                         best = i;
198                 }
199         }
200
201         switch (best) {
202         case 0: clear_tiles(state, display, d, 0, 0, 4, 0, w, h); break;
203         case 1: clear_tiles(state, display, d, 0, 4, 4, 4, w, h); break;
204         case 2: clear_tiles(state, display, d, 0, 0, 0, 4, w, h); break;
205         case 3: clear_tiles(state, display, d, 4, 0, 4, 4, w, h); break;
206         }
207
208         clear_border(state, display, d, w, h, mask & ~best_mask);
209 }
210
211 static int
212 redraw_tile(struct app_state *state, Display *display, Drawable d,
213             uint_fast32_t bit0, uint_fast32_t bit1, uint_fast32_t bit2,
214             int x, int y, Dimension w, Dimension h)
215 {
216         uint_fast32_t pos = board_position(x, y);
217         unsigned char tile = 0;
218
219 #if X11_RENDER_DEBUG
220         if (d == XtWindow(state->game) || d == XtWindow(state->goal)) {
221                 XRectangle r = { x*w, y*h, w, h };
222                 XSetForeground(display, state->tile_gc, 0xff0000);
223                 XFillRectangles(display, d, state->tile_gc, &r, 1);
224                 XFlush(display);
225                 usleep(70000);
226         }
227 #endif
228
229         if (bit0 & pos) tile |= 1;
230         if (bit1 & pos) tile |= 2;
231         if (bit2 & pos) tile |= 4;
232         assert(tile < TILE_MAX);
233
234         if (tile == TILE_EMPTY) {
235                 XClearArea(display, d, x*w, y*h, w, h, 0);
236         } else {
237                 draw_tile(state, display, d, tile, x, y, w, h);
238         }
239
240         return tile;
241 }
242
243 static int
244 redraw_goal_tile(struct app_state *state, Display *display, Drawable d,
245                  int x, int y, Dimension w, Dimension h)
246 {
247         uint_least16_t *gp = state->board.goal;
248
249         return redraw_tile(state, display, d, gp[0], gp[1], gp[2], x, y, w, h);
250 }
251
252 void x11_redraw_goal(struct app_state *state, uint_fast32_t mask)
253 {
254         Display *display = XtDisplay(state->goal);
255         Window goal = XtWindow(state->goal);
256         Dimension w, h;
257         int i;
258
259         XtVaGetValues(state->goal, XtNwidth, &w, XtNheight, &h, (char *)NULL);
260         w /= 3; h /= 3;
261
262         for (i = 0; i < 9; i++) {
263                 int x = i%3, y = i/3;
264
265                 if (mask & 1) {
266                         redraw_goal_tile(state, display, goal, x, y, w, h);
267                 }
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
277 /* Render the goal area as the window's icon */
278 void x11_redraw_icon(struct app_state *state, Widget shell)
279 {
280         Display *display = XtDisplay(shell);
281         Dimension tilesz = ICON_SIZE/3;
282         unsigned long ewmhseq[9];
283         unsigned i;
284
285         for (i = 0; i < 9; i++) {
286                 int tile;
287
288                 tile = redraw_goal_tile(state, display, state->icon_pixmap,
289                                                i%3, i/3, tilesz, tilesz);
290
291                 ewmhseq[i] = COLOUR_MAX*0x10101 * (tile-1) + 0x20100;
292         }
293
294         /*
295          * Clear and reset XtNiconPixmap otherwise it seems dtwm will not
296          * notice the changed icon.
297          */
298         XtVaSetValues(shell, XtNiconPixmap, None, (char *)NULL);
299         XtVaSetValues(shell, XtNiconPixmap, state->icon_pixmap, (char *)NULL);
300
301         if (state->use_ewmh_icons) {
302                 Atom net_wm_icon = XInternAtom(display, "_NET_WM_ICON", FALSE);
303                 Colormap cmap = DefaultColormapOfScreen(XtScreen(shell));
304                 XColor colours[(TILE_MAX-1)*COLOUR_MAX];
305                 void *wm_icon;
306
307                 for (i = 0; i < XtNumber(colours); i++) {
308                         uint_least32_t *p = state->tile_colour[0];
309                         colours[i].pixel = p[i];
310                 }
311
312                 XQueryColors(display, cmap, colours, XtNumber(colours));
313                 wm_icon = ewmh_icon_generate(ewmhseq, colours);
314                 if (wm_icon) {
315                         XChangeProperty(display, XtWindow(shell), net_wm_icon,
316                                         XA_CARDINAL, 32, PropModeReplace,
317                                         wm_icon, EWMH_ICON_NELEM);
318                 }
319                 free(wm_icon);
320         }
321 }
322
323 void x11_redraw_game(struct app_state *state, uint_fast32_t mask)
324 {
325         Display *display = XtDisplay(state->goal);
326         Window game = XtWindow(state->game);
327         uint_least32_t buf[3], *gp = state->board.game;
328         Dimension w, h;
329         int i;
330
331         XtVaGetValues(state->game, XtNwidth, &w, XtNheight, &h, (char *)NULL);
332         w /= 5; h /= 5;
333
334         if (state->view_goal_on_game) {
335                 for (i = 0; i < 3; i++) {
336                         buf[i] = state->board.goal[i];
337                         buf[i] = (gp[i] & ~GOAL_MASK) | (buf[i] << GOAL_SHIFT);
338                 }
339                 gp = buf;
340         }
341
342         /* Optimize the game end case where the outer frame is cleared */
343         if (((gp[0] | gp[1] | gp[2]) & ~GOAL_MASK) == 0) {
344                 clear_border(state, display, game, w, h, mask);
345                 mask &= GOAL_MASK;
346         }
347
348         for (i = 0; i < 25; i++) {
349                 if (mask & 1) {
350                         redraw_tile(state, display, game,
351                                     gp[0], gp[1], gp[2],
352                                     i%5, i/5, w, h);
353                 }
354                 mask >>= 1;
355         }
356 }
357
358 /*
359  * Deferred redraw of tiles after resize/expose to avoid redundant drawing.
360  *
361  * Record any tiles that need to be redrawn due to resizes or expose events,
362  * then, after a short delay, perform all the accumulated redraws at once.
363  *
364  * This is implemented using both a work proc and a timeout, because it seems
365  * that rendering directly inside the timeout callback gives poor results:
366  * possibly redrawing outdated intermediate positions during "fast" resizes
367  * long after the resizing has stopped.  This does not happen when drawing
368  * from a work proc.
369  */
370 static Boolean do_render(void *data)
371 {
372         struct app_state *state = data;
373
374         x11_redraw_goal(state, state->render_goal_mask);
375         x11_redraw_game(state, state->render_game_mask);
376
377         state->render_goal_mask = state->render_game_mask = 0;
378         state->render_proc = 0;
379         state->render_tick = 0;
380         return True;
381 }
382
383 static void start_render(void *data, XtIntervalId *id)
384 {
385         struct app_state *state = data;
386         XtAppContext app;
387
388         if (state->render_proc)
389                 return;
390
391         app = XtWidgetToApplicationContext(state->game);
392         state->render_proc = XtAppAddWorkProc(app, do_render, state);
393 }
394
395 void x11_queue_render(struct app_state *state, uint_fast32_t game_mask,
396                                                uint_fast16_t goal_mask)
397 {
398         uint_fast32_t changed = 0;
399         XtAppContext app;
400
401         changed |= state->render_game_mask |= game_mask;
402         changed |= state->render_goal_mask |= goal_mask;
403
404         if (state->render_tick || !changed)
405                 return;
406
407         app = XtWidgetToApplicationContext(state->game);
408         state->render_tick = XtAppAddTimeOut(app, 3, start_render, state);
409 }