]> git.draconx.ca Git - rrace.git/blob - src/x11.c
Implement window icons.
[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)
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                 redraw_goal_tile(state, display, window, i%3, i/3, w/3, h/3);
147 }
148
149 /* Render the goal area as the window's icon */
150 void x11_redraw_icon(struct app_state *state, Widget shell)
151 {
152         Display *display = XtDisplay(shell);
153         Dimension tilesz = ICON_SIZE/3;
154         int i, j, tile;
155
156         XColor colours[(TILE_MAX-1)*COLOUR_MAX];
157         unsigned long *icons[ICON_MAX];
158         void *wm_icon = NULL;
159
160         if (state->use_ewmh_icons && (wm_icon = ewmh_icon_alloc(icons))) {
161                 Colormap cmap = DefaultColormapOfScreen(XtScreen(shell));
162
163                 for (i = 0; i < TILE_MAX-1; i++) {
164                         for (j = 0; j < COLOUR_MAX; j++) {
165                                 XColor *c = &colours[i*COLOUR_MAX+j];
166                                 c->pixel = state->tile_colour[i][j];
167                         }
168                 }
169
170                 XQueryColors(display, cmap, colours, i*j);
171         }
172
173         for (i = 0; i < 9; i++) {
174                 tile = redraw_goal_tile(state, display, state->icon_pixmap,
175                                         i%3, i/3, tilesz, tilesz);
176
177                 if (wm_icon) {
178                         XColor *c = &colours[(tile-1)*COLOUR_MAX];
179                         ewmh_tile16(icons[ICON_16x16], c, i%3, i/3);
180                         ewmh_tile24(icons[ICON_24x24], c, i%3, i/3);
181                         ewmh_tile32(icons[ICON_32x32], c, i%3, i/3);
182                         ewmh_tile48(icons[ICON_48x48], c, i%3, i/3);
183                 }
184         }
185
186         /*
187          * Clear and reset XmNiconPixmap otherwise it seems dtwm will not
188          * notice the changed icon.
189          */
190         XtVaSetValues(shell, XmNiconPixmap, None, (char *)NULL);
191         XtVaSetValues(shell, XmNiconPixmap, state->icon_pixmap, (char *)NULL);
192
193         if (wm_icon) {
194                 Atom net_wm_icon = XInternAtom(display, "_NET_WM_ICON", FALSE);
195
196                 XChangeProperty(display, XtWindow(shell), net_wm_icon,
197                                 XA_CARDINAL, 32, PropModeReplace,
198                                 wm_icon, EWMH_ICON_NELEM);
199
200                 free(wm_icon);
201         }
202 }
203
204 void x11_redraw_game(struct app_state *state)
205 {
206         Display *display = XtDisplay(state->goal);
207         Window game = XtWindow(state->game);
208         Dimension w, h;
209         int i;
210
211         XtVaGetValues(state->game, XmNwidth, &w, XmNheight, &h, (char *)NULL);
212         w /= 5; h /= 5;
213
214         for (i = 0; i < 25; i++) {
215                 uint_least32_t *gp = state->board.game;
216
217                 redraw_tile(state, display, game,
218                             gp[0], gp[1], gp[2],
219                             i%5, i/5, w, h);
220         }
221 }