2 * X11 GUI for slide puzzle game
3 * Copyright © 2022 Nick Bowler
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.
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.
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/>.
25 #include <X11/Intrinsic.h>
26 #include <X11/StringDefs.h>
27 #include <X11/Xatom.h>
28 #include <X11/Shell.h>
31 /* Size of the traditional icon pixmap (multiple of 3) */
34 /* Define to 1 to add highlights and delays for visually debugging redraws */
40 NUM_TILE_COLOURS = COLOUR_MAX*(TILE_MAX-1),
41 COLOUR_UI_GAMEBG = NUM_TILE_COLOURS,
44 typedef Pixel colour_tab[COLOUR_UI_MAX];
46 #define COLOURRES(name, index, def) { \
47 (name), (NULL), XtRPixel, sizeof (String), \
48 offsetof(struct { colour_tab t; }, t[index]), \
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),
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),
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),
73 COLOURRES("gameBackground", COLOUR_UI_GAMEBG, "#181818")
76 static void init_colours(struct app_state *state, Widget shell)
81 XtGetApplicationResources(shell, &colours, colour_resources,
82 XtNumber(colour_resources), NULL, 0);
84 for (i = 0; i < NUM_TILE_COLOURS; i++) {
85 unsigned tile = i % (TILE_MAX-1), shade = i / (TILE_MAX-1);
87 state->tile_colour[tile][shade] = colours[i];
90 XtVaSetValues(state->game, XtNbackground, colours[COLOUR_UI_GAMEBG],
94 void x11_initialize(struct app_state *state, Widget shell)
96 Display *display = XtDisplay(shell);
97 Screen *screen = XtScreen(shell);
98 Window root = RootWindowOfScreen(screen);
101 init_colours(state, shell);
104 state->tile_gc = XCreateGC(display, root, GCLineWidth, &gcv);
106 state->icon_pixmap = XCreatePixmap(display, root, ICON_SIZE, ICON_SIZE,
107 DefaultDepthOfScreen(screen));
110 static void draw_tile(struct app_state *state, Display *display, Drawable d,
111 int tile, int gx, int gy, Dimension tw, Dimension th)
113 int tx = gx * tw, ty = gy * th;
115 XSegment topshadow[] = {
116 { tx, ty, tx, ty+th },
117 { tx+1, ty, tx+tw, ty },
119 { tx+1, ty+1, tx+1, ty+th-1 },
120 { tx+2, ty+1, tx+tw-1, ty+1 }
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 },
127 { tx+2, ty+th-2, tx+tw-1, ty+th-2 },
128 { tx+tw-2, ty+th-2, tx+tw-2, ty+2 }
131 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_LIGHT]);
132 XDrawSegments(display, d, state->tile_gc, topshadow, XtNumber(topshadow));
134 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_DARK]);
135 XDrawSegments(display, d, state->tile_gc, bottomshadow, XtNumber(bottomshadow));
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);
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)
146 uint_fast32_t pos = board_position(x, y);
147 unsigned char tile = 0;
149 if (bit0 & pos) tile |= 1;
150 if (bit1 & pos) tile |= 2;
151 if (bit2 & pos) tile |= 4;
152 assert(tile < TILE_MAX);
154 if (tile == TILE_EMPTY) {
155 XClearArea(display, d, x*w, y*h, w, h, 0);
157 draw_tile(state, display, d, tile, x, y, w, h);
164 redraw_goal_tile(struct app_state *state, Display *display, Drawable d,
165 int x, int y, Dimension w, Dimension h)
167 uint_least16_t *gp = state->board.goal;
169 return redraw_tile(state, display, d, gp[0], gp[1], gp[2], x, y, w, h);
172 void x11_redraw_goal(struct app_state *state, uint_fast32_t mask)
174 Display *display = XtDisplay(state->goal);
175 Window goal = XtWindow(state->goal);
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;
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);
191 redraw_goal_tile(state, display, goal, x, y, w/3, h/3);
195 * Goal bitmaps have a gap of 2 tiles between each row.
196 * This funny shift will accomodate that.
202 /* Render the goal area as the window's icon */
203 void x11_redraw_icon(struct app_state *state, Widget shell)
205 Display *display = XtDisplay(shell);
206 Dimension tilesz = ICON_SIZE/3;
209 XColor colours[(TILE_MAX-1)*COLOUR_MAX];
210 unsigned long *icons[ICON_MAX];
211 void *wm_icon = NULL;
213 if (state->use_ewmh_icons && (wm_icon = ewmh_icon_alloc(icons))) {
214 Colormap cmap = DefaultColormapOfScreen(XtScreen(shell));
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];
223 XQueryColors(display, cmap, colours, i*j);
226 for (i = 0; i < 9; i++) {
227 tile = redraw_goal_tile(state, display, state->icon_pixmap,
228 i%3, i/3, tilesz, tilesz);
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);
240 * Clear and reset XtNiconPixmap otherwise it seems dtwm will not
241 * notice the changed icon.
243 XtVaSetValues(shell, XtNiconPixmap, None, (char *)NULL);
244 XtVaSetValues(shell, XtNiconPixmap, state->icon_pixmap, (char *)NULL);
247 Atom net_wm_icon = XInternAtom(display, "_NET_WM_ICON", FALSE);
249 XChangeProperty(display, XtWindow(shell), net_wm_icon,
250 XA_CARDINAL, 32, PropModeReplace,
251 wm_icon, EWMH_ICON_NELEM);
257 void x11_redraw_game(struct app_state *state, uint_fast32_t mask)
259 Display *display = XtDisplay(state->goal);
260 Window game = XtWindow(state->game);
261 uint_least32_t buf[3], *gp = state->board.game;
265 XtVaGetValues(state->game, XtNwidth, &w, XtNheight, &h, (char *)NULL);
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);
276 for (i = 0; i < 25; i++) {
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);
285 redraw_tile(state, display, game,