]> git.draconx.ca Git - rrace.git/blob - src/x11.c
Add build-time option to help debug X11 rendering.
[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 #include "motif.h"
30
31 /* Size of the traditional icon pixmap (multiple of 3) */
32 #define ICON_SIZE 48
33
34 /* Define to 1 to add highlights and delays for visually debugging redraws */
35 #if X11_RENDER_DEBUG
36 #  include <unistd.h>
37 #endif
38
39 enum {
40         NUM_TILE_COLOURS = COLOUR_MAX*(TILE_MAX-1),
41         COLOUR_UI_GAMEBG = NUM_TILE_COLOURS,
42         COLOUR_UI_MAX
43 };
44 typedef Pixel colour_tab[COLOUR_UI_MAX];
45
46 #define COLOURRES(name, index, def) { \
47         (name), (NULL), XtRPixel, sizeof (String), \
48         offsetof(struct { colour_tab t; }, t[index]), \
49         XtRString, (def) }
50
51 static XtResource colour_resources[] = {
52         COLOURRES("colour0", 0, COLOUR0_PRIMARY),
53         COLOURRES("colour1", 1, COLOUR1_PRIMARY),
54         COLOURRES("colour2", 2, COLOUR2_PRIMARY),
55         COLOURRES("colour3", 3, COLOUR3_PRIMARY),
56         COLOURRES("colour4", 4, COLOUR4_PRIMARY),
57         COLOURRES("colour5", 5, COLOUR5_PRIMARY),
58
59         COLOURRES("colourDark0",  6, COLOUR0_DARK),
60         COLOURRES("colourDark1",  7, COLOUR1_DARK),
61         COLOURRES("colourDark2",  8, COLOUR2_DARK),
62         COLOURRES("colourDark3",  9, COLOUR3_DARK),
63         COLOURRES("colourDark4", 10, COLOUR4_DARK),
64         COLOURRES("colourDark5", 11, COLOUR5_DARK),
65
66         COLOURRES("colourLight0", 12, COLOUR0_LIGHT),
67         COLOURRES("colourLight1", 13, COLOUR1_LIGHT),
68         COLOURRES("colourLight2", 14, COLOUR2_LIGHT),
69         COLOURRES("colourLight3", 15, COLOUR3_LIGHT),
70         COLOURRES("colourLight4", 16, COLOUR4_LIGHT),
71         COLOURRES("colourLight5", 17, COLOUR5_LIGHT),
72
73         COLOURRES("gameBackground", COLOUR_UI_GAMEBG, "#181818")
74 };
75
76 static void init_colours(struct app_state *state, Widget shell)
77 {
78         unsigned i;
79
80         colour_tab colours;
81         XtGetApplicationResources(shell, &colours, colour_resources,
82                                   XtNumber(colour_resources), NULL, 0);
83
84         for (i = 0; i < NUM_TILE_COLOURS; i++) {
85                 unsigned tile = i % (TILE_MAX-1), shade = i / (TILE_MAX-1);
86
87                 state->tile_colour[tile][shade] = colours[i];
88         }
89
90         XtVaSetValues(state->game, XtNbackground, colours[COLOUR_UI_GAMEBG],
91                                    (char *)NULL);
92 }
93
94 void x11_initialize(struct app_state *state, Widget shell)
95 {
96         Display *display = XtDisplay(shell);
97         Screen *screen = XtScreen(shell);
98         Window root = RootWindowOfScreen(screen);
99         XGCValues gcv;
100
101         init_colours(state, shell);
102
103         gcv.line_width = 1;
104         state->tile_gc = XCreateGC(display, root, GCLineWidth, &gcv);
105
106         state->icon_pixmap = XCreatePixmap(display, root, ICON_SIZE, ICON_SIZE,
107                                            DefaultDepthOfScreen(screen));
108 }
109
110 static void draw_tile(struct app_state *state, Display *display, Drawable d,
111                       int tile, int gx, int gy, Dimension tw, Dimension th)
112 {
113         int tx = gx * tw, ty = gy * th;
114
115         XSegment topshadow[] = {
116                 { tx,      ty,      tx,      ty+th   },
117                 { tx+1,    ty,      tx+tw,   ty      },
118
119                 { tx+1,    ty+1,    tx+1,    ty+th-1 },
120                 { tx+2,    ty+1,    tx+tw-1, ty+1    }
121         };
122
123         XSegment bottomshadow[] = {
124                 { tx+1,    ty+th-1, tx+tw,   ty+th-1 },
125                 { tx+tw-1, ty+th-1, tx+tw-1, ty+1    },
126
127                 { tx+2,    ty+th-2, tx+tw-1, ty+th-2 },
128                 { tx+tw-2, ty+th-2, tx+tw-2, ty+2    }
129         };
130
131         XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_LIGHT]);
132         XDrawSegments(display, d, state->tile_gc, topshadow, XtNumber(topshadow));
133
134         XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_DARK]);
135         XDrawSegments(display, d, state->tile_gc, bottomshadow, XtNumber(bottomshadow));
136
137         XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_PRIMARY]);
138         XFillRectangle(display, d, state->tile_gc, tx+2, ty+2, tw-4, th-4);
139 }
140
141 static int
142 redraw_tile(struct app_state *state, Display *display, Drawable d,
143             uint_fast32_t bit0, uint_fast32_t bit1, uint_fast32_t bit2,
144             int x, int y, Dimension w, Dimension h)
145 {
146         uint_fast32_t pos = board_position(x, y);
147         unsigned char tile = 0;
148
149         if (bit0 & pos) tile |= 1;
150         if (bit1 & pos) tile |= 2;
151         if (bit2 & pos) tile |= 4;
152         assert(tile < TILE_MAX);
153
154         if (tile == TILE_EMPTY) {
155                 XClearArea(display, d, x*w, y*h, w, h, 0);
156         } else {
157                 draw_tile(state, display, d, tile, x, y, w, h);
158         }
159
160         return tile;
161 }
162
163 static int
164 redraw_goal_tile(struct app_state *state, Display *display, Drawable d,
165                  int x, int y, Dimension w, Dimension h)
166 {
167         uint_least16_t *gp = state->board.goal;
168
169         return redraw_tile(state, display, d, gp[0], gp[1], gp[2], x, y, w, h);
170 }
171
172 void x11_redraw_goal(struct app_state *state, uint_fast32_t mask)
173 {
174         Display *display = XtDisplay(state->goal);
175         Window goal = XtWindow(state->goal);
176         Dimension w, h;
177         int i;
178
179         XtVaGetValues(state->goal, XtNwidth, &w, XtNheight, &h, (char *)NULL);
180         for (i = 0; i < 9; i++) {
181                 int x = i%3, y = i/3;
182
183                 if (mask & 1) {
184 #if X11_RENDER_DEBUG
185                         XRectangle r = { w/3*(i%3), w/3*(i/3), w/3, h/3 };
186                         XSetForeground(display, state->tile_gc, 0xff0000);
187                         XFillRectangles(display, goal, state->tile_gc, &r, 1);
188                         XFlush(display);
189                         usleep(70000);
190 #endif
191                         redraw_goal_tile(state, display, goal, x, y, w/3, h/3);
192                 }
193
194                 /*
195                  * Goal bitmaps have a gap of 2 tiles between each row.
196                  * This funny shift will accomodate that.
197                  */
198                 mask >>= 1|x;
199         }
200 }
201
202 /* Render the goal area as the window's icon */
203 void x11_redraw_icon(struct app_state *state, Widget shell)
204 {
205         Display *display = XtDisplay(shell);
206         Dimension tilesz = ICON_SIZE/3;
207         int i, j, tile;
208
209         XColor colours[(TILE_MAX-1)*COLOUR_MAX];
210         unsigned long *icons[ICON_MAX];
211         void *wm_icon = NULL;
212
213         if (state->use_ewmh_icons && (wm_icon = ewmh_icon_alloc(icons))) {
214                 Colormap cmap = DefaultColormapOfScreen(XtScreen(shell));
215
216                 for (i = 0; i < TILE_MAX-1; i++) {
217                         for (j = 0; j < COLOUR_MAX; j++) {
218                                 XColor *c = &colours[i*COLOUR_MAX+j];
219                                 c->pixel = state->tile_colour[i][j];
220                         }
221                 }
222
223                 XQueryColors(display, cmap, colours, i*j);
224         }
225
226         for (i = 0; i < 9; i++) {
227                 tile = redraw_goal_tile(state, display, state->icon_pixmap,
228                                         i%3, i/3, tilesz, tilesz);
229
230                 if (wm_icon) {
231                         XColor *c = &colours[(tile-1)*COLOUR_MAX];
232                         ewmh_tile16(icons[ICON_16x16], c, i%3, i/3);
233                         ewmh_tile24(icons[ICON_24x24], c, i%3, i/3);
234                         ewmh_tile32(icons[ICON_32x32], c, i%3, i/3);
235                         ewmh_tile48(icons[ICON_48x48], c, i%3, i/3);
236                 }
237         }
238
239         /*
240          * Clear and reset XtNiconPixmap otherwise it seems dtwm will not
241          * notice the changed icon.
242          */
243         XtVaSetValues(shell, XtNiconPixmap, None, (char *)NULL);
244         XtVaSetValues(shell, XtNiconPixmap, state->icon_pixmap, (char *)NULL);
245
246         if (wm_icon) {
247                 Atom net_wm_icon = XInternAtom(display, "_NET_WM_ICON", FALSE);
248
249                 XChangeProperty(display, XtWindow(shell), net_wm_icon,
250                                 XA_CARDINAL, 32, PropModeReplace,
251                                 wm_icon, EWMH_ICON_NELEM);
252
253                 free(wm_icon);
254         }
255 }
256
257 void x11_redraw_game(struct app_state *state, uint_fast32_t mask)
258 {
259         Display *display = XtDisplay(state->goal);
260         Window game = XtWindow(state->game);
261         uint_least32_t buf[3], *gp = state->board.game;
262         Dimension w, h;
263         int i;
264
265         XtVaGetValues(state->game, XtNwidth, &w, XtNheight, &h, (char *)NULL);
266         w /= 5; h /= 5;
267
268         if (state->view_goal_on_game) {
269                 for (i = 0; i < 3; i++) {
270                         buf[i] = state->board.goal[i];
271                         buf[i] = (gp[i] & ~GOAL_MASK) | (buf[i] << GOAL_SHIFT);
272                 }
273                 gp = buf;
274         }
275
276         for (i = 0; i < 25; i++) {
277                 if (mask & 1) {
278 #if X11_RENDER_DEBUG
279                         XRectangle r = { w*(i%5), w*(i/5), w, h };
280                         XSetForeground(display, state->tile_gc, 0xff0000);
281                         XFillRectangles(display, game, state->tile_gc, &r, 1);
282                         XFlush(display);
283                         usleep(70000);
284 #endif
285                         redraw_tile(state, display, game,
286                                     gp[0], gp[1], gp[2],
287                                     i%5, i/5, w, h);
288                 }
289                 mask >>= 1;
290         }
291 }