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/>.
24 #include <X11/Intrinsic.h>
25 #include <Xm/XmStrDefs.h>
26 #include <X11/Xatom.h>
27 #include <X11/Shell.h>
30 /* Size of the traditional icon pixmap (multiple of 3) */
33 /* TODO user-selectable colours */
34 static const char * const colours[][3] = {
35 /*primary bottom top */
36 "#8d2e28", "#6a1b17", "#a14842", /* red */
37 "#b46e28", "#924a16", "#c7904f", /* orange */
38 "#d8b740", "#c59f39", "#e2c65d", /* yellow */
39 "#286428", "#194719", "#4e874e", /* green */
40 "#003471", "#001f4f", "#00528b", /* blue */
41 "#dcdcdc", "#c0c0c0", "#eaeaea", /* white */
44 static void init_colours(struct app_state *state, Screen *screen)
46 Display *display = DisplayOfScreen(screen);
47 Colormap cmap = DefaultColormapOfScreen(screen);
51 for (j = 0; j < COLOUR_MAX; j++) {
52 for (i = 0; i < TILE_MAX-1; i++) {
53 XAllocNamedColor(display, cmap, colours[i][j],
55 state->tile_colour[i][j] = colour.pixel;
60 void x11_initialize(struct app_state *state, Screen *screen)
62 Display *display = DisplayOfScreen(screen);
63 Window root = RootWindowOfScreen(screen);
66 init_colours(state, screen);
69 state->tile_gc = XCreateGC(display, root, GCLineWidth, &gcv);
71 state->icon_pixmap = XCreatePixmap(display, root, ICON_SIZE, ICON_SIZE,
72 DefaultDepthOfScreen(screen));
75 static void draw_tile(struct app_state *state, Display *display, Drawable d,
76 int tile, int gx, int gy, Dimension tw, Dimension th)
78 int tx = gx * tw, ty = gy * th;
80 XSegment topshadow[] = {
81 { tx, ty, tx, ty+th },
82 { tx+1, ty, tx+tw, ty },
84 { tx+1, ty+1, tx+1, ty+th-1 },
85 { tx+2, ty+1, tx+tw-1, ty+1 }
88 XSegment bottomshadow[] = {
89 { tx+1, ty+th-1, tx+tw, ty+th-1 },
90 { tx+tw-1, ty+th-1, tx+tw-1, ty+1 },
92 { tx+2, ty+th-2, tx+tw-1, ty+th-2 },
93 { tx+tw-2, ty+th-2, tx+tw-2, ty+2 }
96 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_LIGHT]);
97 XDrawSegments(display, d, state->tile_gc, topshadow, XtNumber(topshadow));
99 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_DARK]);
100 XDrawSegments(display, d, state->tile_gc, bottomshadow, XtNumber(bottomshadow));
102 XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_PRIMARY]);
103 XFillRectangle(display, d, state->tile_gc, tx+2, ty+2, tw-4, th-4);
107 redraw_tile(struct app_state *state, Display *display, Drawable d,
108 uint_fast32_t bit0, uint_fast32_t bit1, uint_fast32_t bit2,
109 int x, int y, Dimension w, Dimension h)
111 uint_fast32_t pos = board_position(x, y);
112 unsigned char tile = 0;
114 if (bit0 & pos) tile |= 1;
115 if (bit1 & pos) tile |= 2;
116 if (bit2 & pos) tile |= 4;
117 assert(tile < TILE_MAX);
119 if (tile == TILE_EMPTY) {
120 XClearArea(display, d, x*w, y*h, w, h, 0);
122 draw_tile(state, display, d, tile, x, y, w, h);
129 redraw_goal_tile(struct app_state *state, Display *display, Drawable d,
130 int x, int y, Dimension w, Dimension h)
132 uint_least16_t *gp = state->board.goal;
134 return redraw_tile(state, display, d, gp[0], gp[1], gp[2], x, y, w, h);
137 void x11_redraw_goal(struct app_state *state, uint_fast32_t mask)
139 Display *display = XtDisplay(state->goal);
140 Window window = XtWindow(state->goal);
144 XtVaGetValues(state->goal, XmNwidth, &w, XmNheight, &h, (char *)NULL);
145 for (i = 0; i < 9; i++) {
146 int x = i%3, y = i/3;
149 redraw_goal_tile(state, display, window,
154 * Goal bitmaps have a gap of 2 tiles between each row.
155 * This funny shift will accomodate that.
161 /* Render the goal area as the window's icon */
162 void x11_redraw_icon(struct app_state *state, Widget shell)
164 Display *display = XtDisplay(shell);
165 Dimension tilesz = ICON_SIZE/3;
168 XColor colours[(TILE_MAX-1)*COLOUR_MAX];
169 unsigned long *icons[ICON_MAX];
170 void *wm_icon = NULL;
172 if (state->use_ewmh_icons && (wm_icon = ewmh_icon_alloc(icons))) {
173 Colormap cmap = DefaultColormapOfScreen(XtScreen(shell));
175 for (i = 0; i < TILE_MAX-1; i++) {
176 for (j = 0; j < COLOUR_MAX; j++) {
177 XColor *c = &colours[i*COLOUR_MAX+j];
178 c->pixel = state->tile_colour[i][j];
182 XQueryColors(display, cmap, colours, i*j);
185 for (i = 0; i < 9; i++) {
186 tile = redraw_goal_tile(state, display, state->icon_pixmap,
187 i%3, i/3, tilesz, tilesz);
190 XColor *c = &colours[(tile-1)*COLOUR_MAX];
191 ewmh_tile16(icons[ICON_16x16], c, i%3, i/3);
192 ewmh_tile24(icons[ICON_24x24], c, i%3, i/3);
193 ewmh_tile32(icons[ICON_32x32], c, i%3, i/3);
194 ewmh_tile48(icons[ICON_48x48], c, i%3, i/3);
199 * Clear and reset XmNiconPixmap otherwise it seems dtwm will not
200 * notice the changed icon.
202 XtVaSetValues(shell, XmNiconPixmap, None, (char *)NULL);
203 XtVaSetValues(shell, XmNiconPixmap, state->icon_pixmap, (char *)NULL);
206 Atom net_wm_icon = XInternAtom(display, "_NET_WM_ICON", FALSE);
208 XChangeProperty(display, XtWindow(shell), net_wm_icon,
209 XA_CARDINAL, 32, PropModeReplace,
210 wm_icon, EWMH_ICON_NELEM);
216 void x11_redraw_game(struct app_state *state, uint_fast32_t mask)
218 Display *display = XtDisplay(state->goal);
219 Window game = XtWindow(state->game);
220 uint_least32_t *gp = state->board.game;
224 XtVaGetValues(state->game, XmNwidth, &w, XmNheight, &h, (char *)NULL);
227 for (i = 0; i < 25; i++) {
229 redraw_tile(state, display, game,