]> git.draconx.ca Git - rrace.git/blob - src/x11.c
ea0fdbac90a47688a38c4787a125118b93d655c1
[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 <Xm/XmStrDefs.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 typedef String colour_tab[COLOUR_MAX*(TILE_MAX-1)];
35
36 #define COLOURRES(name, index, def) { \
37         (name), (NULL), XtRString, sizeof (String), \
38         offsetof(struct { colour_tab t; }, t[index]), \
39         XtRString, (def) }
40
41 static XtResource colour_resources[] = {
42         COLOURRES("colour0", 0, COLOUR0_PRIMARY),
43         COLOURRES("colour1", 1, COLOUR1_PRIMARY),
44         COLOURRES("colour2", 2, COLOUR2_PRIMARY),
45         COLOURRES("colour3", 3, COLOUR3_PRIMARY),
46         COLOURRES("colour4", 4, COLOUR4_PRIMARY),
47         COLOURRES("colour5", 5, COLOUR5_PRIMARY),
48
49         COLOURRES("colourDark0",  6, COLOUR0_DARK),
50         COLOURRES("colourDark1",  7, COLOUR1_DARK),
51         COLOURRES("colourDark2",  8, COLOUR2_DARK),
52         COLOURRES("colourDark3",  9, COLOUR3_DARK),
53         COLOURRES("colourDark4", 10, COLOUR4_DARK),
54         COLOURRES("colourDark5", 11, COLOUR5_DARK),
55
56         COLOURRES("colourLight0", 12, COLOUR0_LIGHT),
57         COLOURRES("colourLight1", 13, COLOUR1_LIGHT),
58         COLOURRES("colourLight2", 14, COLOUR2_LIGHT),
59         COLOURRES("colourLight3", 15, COLOUR3_LIGHT),
60         COLOURRES("colourLight4", 16, COLOUR4_LIGHT),
61         COLOURRES("colourLight5", 17, COLOUR5_LIGHT),
62 };
63
64 static void init_colours(struct app_state *state, Widget shell)
65 {
66         Display *display = XtDisplay(shell);
67         Screen *screen = XtScreen(shell);
68         Colormap cmap = DefaultColormapOfScreen(screen);
69         unsigned i;
70
71         colour_tab names;
72         XtGetApplicationResources(shell, &names, colour_resources,
73                                   XtNumber(colour_resources), NULL, 0);
74
75         for (i = 0; i < XtNumber(names); i++) {
76                 unsigned tile = i % (TILE_MAX-1), shade = i / (TILE_MAX-1);
77                 XColor c;
78
79                 assert(names[i]);
80                 if (!XAllocNamedColor(display, cmap, names[i], &c, &c)) {
81                         printf("warning: could not allocate colour: %s\n",
82                                names[i]);
83                         c.pixel = 0;
84                 }
85                 state->tile_colour[tile][shade] = c.pixel;
86         }
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, XmNwidth, &w, XmNheight, &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 XmNiconPixmap otherwise it seems dtwm will not
230          * notice the changed icon.
231          */
232         XtVaSetValues(shell, XmNiconPixmap, None, (char *)NULL);
233         XtVaSetValues(shell, XmNiconPixmap, 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 *gp = state->board.game;
251         Dimension w, h;
252         int i;
253
254         XtVaGetValues(state->game, XmNwidth, &w, XmNheight, &h, (char *)NULL);
255         w /= 5; h /= 5;
256
257         for (i = 0; i < 25; i++) {
258                 if (mask & 1) {
259                         redraw_tile(state, display, game,
260                                     gp[0], gp[1], gp[2],
261                                     i%5, i/5, w, h);
262                 }
263                 mask >>= 1;
264         }
265 }