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 <Xm/XmStrDefs.h>
27 #include <X11/Xatom.h>
28 #include <X11/Shell.h>
31 /* Size of the traditional icon pixmap (multiple of 3) */
34 typedef String colour_tab[COLOUR_MAX*(TILE_MAX-1)];
36 #define COLOURRES(name, index, def) { \
37 (name), (NULL), XtRString, sizeof (String), \
38 offsetof(struct { colour_tab t; }, t[index]), \
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),
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),
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),
64 static void init_colours(struct app_state *state, Widget shell)
66 Display *display = XtDisplay(shell);
67 Screen *screen = XtScreen(shell);
68 Colormap cmap = DefaultColormapOfScreen(screen);
72 XtGetApplicationResources(shell, &names, colour_resources,
73 XtNumber(colour_resources), NULL, 0);
75 for (i = 0; i < XtNumber(names); i++) {
76 unsigned tile = i % (TILE_MAX-1), shade = i / (TILE_MAX-1);
80 if (!XAllocNamedColor(display, cmap, names[i], &c, &c)) {
81 printf("warning: could not allocate colour: %s\n",
85 state->tile_colour[tile][shade] = c.pixel;
89 void x11_initialize(struct app_state *state, Widget shell)
91 Display *display = XtDisplay(shell);
92 Screen *screen = XtScreen(shell);
93 Window root = RootWindowOfScreen(screen);
96 init_colours(state, shell);
99 state->tile_gc = XCreateGC(display, root, GCLineWidth, &gcv);
101 state->icon_pixmap = XCreatePixmap(display, root, ICON_SIZE, ICON_SIZE,
102 DefaultDepthOfScreen(screen));
105 static void draw_tile(struct app_state *state, Display *display, Drawable d,
106 int tile, int gx, int gy, Dimension tw, Dimension th)
108 int tx = gx * tw, ty = gy * th;
110 XSegment topshadow[] = {
111 { tx, ty, tx, ty+th },
112 { tx+1, ty, tx+tw, ty },
114 { tx+1, ty+1, tx+1, ty+th-1 },
115 { tx+2, ty+1, tx+tw-1, ty+1 }
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 },
122 { tx+2, ty+th-2, tx+tw-1, ty+th-2 },
123 { tx+tw-2, ty+th-2, tx+tw-2, ty+2 }
126 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_LIGHT]);
127 XDrawSegments(display, d, state->tile_gc, topshadow, XtNumber(topshadow));
129 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_DARK]);
130 XDrawSegments(display, d, state->tile_gc, bottomshadow, XtNumber(bottomshadow));
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);
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)
141 uint_fast32_t pos = board_position(x, y);
142 unsigned char tile = 0;
144 if (bit0 & pos) tile |= 1;
145 if (bit1 & pos) tile |= 2;
146 if (bit2 & pos) tile |= 4;
147 assert(tile < TILE_MAX);
149 if (tile == TILE_EMPTY) {
150 XClearArea(display, d, x*w, y*h, w, h, 0);
152 draw_tile(state, display, d, tile, x, y, w, h);
159 redraw_goal_tile(struct app_state *state, Display *display, Drawable d,
160 int x, int y, Dimension w, Dimension h)
162 uint_least16_t *gp = state->board.goal;
164 return redraw_tile(state, display, d, gp[0], gp[1], gp[2], x, y, w, h);
167 void x11_redraw_goal(struct app_state *state, uint_fast32_t mask)
169 Display *display = XtDisplay(state->goal);
170 Window window = XtWindow(state->goal);
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;
179 redraw_goal_tile(state, display, window,
184 * Goal bitmaps have a gap of 2 tiles between each row.
185 * This funny shift will accomodate that.
191 /* Render the goal area as the window's icon */
192 void x11_redraw_icon(struct app_state *state, Widget shell)
194 Display *display = XtDisplay(shell);
195 Dimension tilesz = ICON_SIZE/3;
198 XColor colours[(TILE_MAX-1)*COLOUR_MAX];
199 unsigned long *icons[ICON_MAX];
200 void *wm_icon = NULL;
202 if (state->use_ewmh_icons && (wm_icon = ewmh_icon_alloc(icons))) {
203 Colormap cmap = DefaultColormapOfScreen(XtScreen(shell));
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];
212 XQueryColors(display, cmap, colours, i*j);
215 for (i = 0; i < 9; i++) {
216 tile = redraw_goal_tile(state, display, state->icon_pixmap,
217 i%3, i/3, tilesz, tilesz);
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);
229 * Clear and reset XmNiconPixmap otherwise it seems dtwm will not
230 * notice the changed icon.
232 XtVaSetValues(shell, XmNiconPixmap, None, (char *)NULL);
233 XtVaSetValues(shell, XmNiconPixmap, state->icon_pixmap, (char *)NULL);
236 Atom net_wm_icon = XInternAtom(display, "_NET_WM_ICON", FALSE);
238 XChangeProperty(display, XtWindow(shell), net_wm_icon,
239 XA_CARDINAL, 32, PropModeReplace,
240 wm_icon, EWMH_ICON_NELEM);
246 void x11_redraw_game(struct app_state *state, uint_fast32_t mask)
248 Display *display = XtDisplay(state->goal);
249 Window game = XtWindow(state->game);
250 uint_least32_t *gp = state->board.game;
254 XtVaGetValues(state->game, XmNwidth, &w, XmNheight, &h, (char *)NULL);
257 for (i = 0; i < 25; i++) {
259 redraw_tile(state, display, game,