]> git.draconx.ca Git - rrace.git/blob - src/x11.c
Reduce the amount of redundant drawing in the goal area.
[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 <assert.h>
23
24 #include <X11/Intrinsic.h>
25 #include <Xm/XmStrDefs.h>
26 #include <X11/Xatom.h>
27 #include <X11/Shell.h>
28 #include "motif.h"
29
30 /* Size of the traditional icon pixmap (multiple of 3) */
31 #define ICON_SIZE 48
32
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 */
42 };
43
44 static void init_colours(struct app_state *state, Screen *screen)
45 {
46         Display *display = DisplayOfScreen(screen);
47         Colormap cmap = DefaultColormapOfScreen(screen);
48         XColor colour, junk;
49         unsigned i, j;
50
51         for (j = 0; j < COLOUR_MAX; j++) {
52                 for (i = 0; i < TILE_MAX-1; i++) {
53                         XAllocNamedColor(display, cmap, colours[i][j],
54                                                   &colour, &junk);
55                         state->tile_colour[i][j] = colour.pixel;
56                 }
57         }
58 }
59
60 void x11_initialize(struct app_state *state, Screen *screen)
61 {
62         Display *display = DisplayOfScreen(screen);
63         Window root = RootWindowOfScreen(screen);
64         XGCValues gcv;
65
66         init_colours(state, screen);
67
68         gcv.line_width = 1;
69         state->tile_gc = XCreateGC(display, root, GCLineWidth, &gcv);
70
71         state->icon_pixmap = XCreatePixmap(display, root, ICON_SIZE, ICON_SIZE,
72                                            DefaultDepthOfScreen(screen));
73 }
74
75 static void draw_tile(struct app_state *state, Display *display, Drawable d,
76                       int tile, int gx, int gy, Dimension tw, Dimension th)
77 {
78         int tx = gx * tw, ty = gy * th;
79
80         XSegment topshadow[] = {
81                 { tx,      ty,      tx,      ty+th   },
82                 { tx+1,    ty,      tx+tw,   ty      },
83
84                 { tx+1,    ty+1,    tx+1,    ty+th-1 },
85                 { tx+2,    ty+1,    tx+tw-1, ty+1    }
86         };
87
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    },
91
92                 { tx+2,    ty+th-2, tx+tw-1, ty+th-2 },
93                 { tx+tw-2, ty+th-2, tx+tw-2, ty+2    }
94         };
95
96         XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_LIGHT]);
97         XDrawSegments(display, d, state->tile_gc, topshadow, XtNumber(topshadow));
98
99         XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_DARK]);
100         XDrawSegments(display, d, state->tile_gc, bottomshadow, XtNumber(bottomshadow));
101
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);
104 }
105
106 static int
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)
110 {
111         uint_fast32_t pos = board_position(x, y);
112         unsigned char tile = 0;
113
114         if (bit0 & pos) tile |= 1;
115         if (bit1 & pos) tile |= 2;
116         if (bit2 & pos) tile |= 4;
117         assert(tile < TILE_MAX);
118
119         if (tile == TILE_EMPTY) {
120                 XClearArea(display, d, x*w, y*h, w, h, 0);
121         } else {
122                 draw_tile(state, display, d, tile, x, y, w, h);
123         }
124
125         return tile;
126 }
127
128 static int
129 redraw_goal_tile(struct app_state *state, Display *display, Drawable d,
130                  int x, int y, Dimension w, Dimension h)
131 {
132         uint_least16_t *gp = state->board.goal;
133
134         return redraw_tile(state, display, d, gp[0], gp[1], gp[2], x, y, w, h);
135 }
136
137 void x11_redraw_goal(struct app_state *state, uint_fast32_t mask)
138 {
139         Display *display = XtDisplay(state->goal);
140         Window window = XtWindow(state->goal);
141         Dimension w, h;
142         int i;
143
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;
147
148                 if (mask & 1) {
149                         redraw_goal_tile(state, display, window,
150                                                 x, y, w/3, h/3);
151                 }
152
153                 /*
154                  * Goal bitmaps have a gap of 2 tiles between each row.
155                  * This funny shift will accomodate that.
156                  */
157                 mask >>= 1|x;
158         }
159 }
160
161 /* Render the goal area as the window's icon */
162 void x11_redraw_icon(struct app_state *state, Widget shell)
163 {
164         Display *display = XtDisplay(shell);
165         Dimension tilesz = ICON_SIZE/3;
166         int i, j, tile;
167
168         XColor colours[(TILE_MAX-1)*COLOUR_MAX];
169         unsigned long *icons[ICON_MAX];
170         void *wm_icon = NULL;
171
172         if (state->use_ewmh_icons && (wm_icon = ewmh_icon_alloc(icons))) {
173                 Colormap cmap = DefaultColormapOfScreen(XtScreen(shell));
174
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];
179                         }
180                 }
181
182                 XQueryColors(display, cmap, colours, i*j);
183         }
184
185         for (i = 0; i < 9; i++) {
186                 tile = redraw_goal_tile(state, display, state->icon_pixmap,
187                                         i%3, i/3, tilesz, tilesz);
188
189                 if (wm_icon) {
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);
195                 }
196         }
197
198         /*
199          * Clear and reset XmNiconPixmap otherwise it seems dtwm will not
200          * notice the changed icon.
201          */
202         XtVaSetValues(shell, XmNiconPixmap, None, (char *)NULL);
203         XtVaSetValues(shell, XmNiconPixmap, state->icon_pixmap, (char *)NULL);
204
205         if (wm_icon) {
206                 Atom net_wm_icon = XInternAtom(display, "_NET_WM_ICON", FALSE);
207
208                 XChangeProperty(display, XtWindow(shell), net_wm_icon,
209                                 XA_CARDINAL, 32, PropModeReplace,
210                                 wm_icon, EWMH_ICON_NELEM);
211
212                 free(wm_icon);
213         }
214 }
215
216 void x11_redraw_game(struct app_state *state, uint_fast32_t mask)
217 {
218         Display *display = XtDisplay(state->goal);
219         Window game = XtWindow(state->game);
220         uint_least32_t *gp = state->board.game;
221         Dimension w, h;
222         int i;
223
224         XtVaGetValues(state->game, XmNwidth, &w, XmNheight, &h, (char *)NULL);
225         w /= 5; h /= 5;
226
227         for (i = 0; i < 25; i++) {
228                 if (mask & 1) {
229                         redraw_tile(state, display, game,
230                                     gp[0], gp[1], gp[2],
231                                     i%5, i/5, w, h);
232                 }
233                 mask >>= 1;
234         }
235 }