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