/* * X11 GUI for slide puzzle game * Copyright © 2022 Nick Bowler * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include #include #include #include #include #include #include "motif.h" /* Size of the traditional icon pixmap (multiple of 3) */ #define ICON_SIZE 48 typedef String colour_tab[COLOUR_MAX*(TILE_MAX-1)]; #define COLOURRES(name, index, def) { \ (name), (NULL), XtRString, sizeof (String), \ offsetof(struct { colour_tab t; }, t[index]), \ XtRString, (def) } static XtResource colour_resources[] = { COLOURRES("colour0", 0, COLOUR0_PRIMARY), COLOURRES("colour1", 1, COLOUR1_PRIMARY), COLOURRES("colour2", 2, COLOUR2_PRIMARY), COLOURRES("colour3", 3, COLOUR3_PRIMARY), COLOURRES("colour4", 4, COLOUR4_PRIMARY), COLOURRES("colour5", 5, COLOUR5_PRIMARY), COLOURRES("colourDark0", 6, COLOUR0_DARK), COLOURRES("colourDark1", 7, COLOUR1_DARK), COLOURRES("colourDark2", 8, COLOUR2_DARK), COLOURRES("colourDark3", 9, COLOUR3_DARK), COLOURRES("colourDark4", 10, COLOUR4_DARK), COLOURRES("colourDark5", 11, COLOUR5_DARK), COLOURRES("colourLight0", 12, COLOUR0_LIGHT), COLOURRES("colourLight1", 13, COLOUR1_LIGHT), COLOURRES("colourLight2", 14, COLOUR2_LIGHT), COLOURRES("colourLight3", 15, COLOUR3_LIGHT), COLOURRES("colourLight4", 16, COLOUR4_LIGHT), COLOURRES("colourLight5", 17, COLOUR5_LIGHT), }; static void init_colours(struct app_state *state, Widget shell) { Display *display = XtDisplay(shell); Screen *screen = XtScreen(shell); Colormap cmap = DefaultColormapOfScreen(screen); unsigned i; colour_tab names; XtGetApplicationResources(shell, &names, colour_resources, XtNumber(colour_resources), NULL, 0); for (i = 0; i < XtNumber(names); i++) { unsigned tile = i % (TILE_MAX-1), shade = i / (TILE_MAX-1); XColor c; assert(names[i]); if (!XAllocNamedColor(display, cmap, names[i], &c, &c)) { printf("warning: could not allocate colour: %s\n", names[i]); c.pixel = 0; } state->tile_colour[tile][shade] = c.pixel; } } void x11_initialize(struct app_state *state, Widget shell) { Display *display = XtDisplay(shell); Screen *screen = XtScreen(shell); Window root = RootWindowOfScreen(screen); XGCValues gcv; init_colours(state, shell); gcv.line_width = 1; state->tile_gc = XCreateGC(display, root, GCLineWidth, &gcv); state->icon_pixmap = XCreatePixmap(display, root, ICON_SIZE, ICON_SIZE, DefaultDepthOfScreen(screen)); } static void draw_tile(struct app_state *state, Display *display, Drawable d, int tile, int gx, int gy, Dimension tw, Dimension th) { int tx = gx * tw, ty = gy * th; XSegment topshadow[] = { { tx, ty, tx, ty+th }, { tx+1, ty, tx+tw, ty }, { tx+1, ty+1, tx+1, ty+th-1 }, { tx+2, ty+1, tx+tw-1, ty+1 } }; XSegment bottomshadow[] = { { tx+1, ty+th-1, tx+tw, ty+th-1 }, { tx+tw-1, ty+th-1, tx+tw-1, ty+1 }, { tx+2, ty+th-2, tx+tw-1, ty+th-2 }, { tx+tw-2, ty+th-2, tx+tw-2, ty+2 } }; XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_LIGHT]); XDrawSegments(display, d, state->tile_gc, topshadow, XtNumber(topshadow)); XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_DARK]); XDrawSegments(display, d, state->tile_gc, bottomshadow, XtNumber(bottomshadow)); XSetForeground(display, state->tile_gc, state->tile_colour[tile-1][COLOUR_PRIMARY]); XFillRectangle(display, d, state->tile_gc, tx+2, ty+2, tw-4, th-4); } static int redraw_tile(struct app_state *state, Display *display, Drawable d, uint_fast32_t bit0, uint_fast32_t bit1, uint_fast32_t bit2, int x, int y, Dimension w, Dimension h) { uint_fast32_t pos = board_position(x, y); unsigned char tile = 0; if (bit0 & pos) tile |= 1; if (bit1 & pos) tile |= 2; if (bit2 & pos) tile |= 4; assert(tile < TILE_MAX); if (tile == TILE_EMPTY) { XClearArea(display, d, x*w, y*h, w, h, 0); } else { draw_tile(state, display, d, tile, x, y, w, h); } return tile; } static int redraw_goal_tile(struct app_state *state, Display *display, Drawable d, int x, int y, Dimension w, Dimension h) { uint_least16_t *gp = state->board.goal; return redraw_tile(state, display, d, gp[0], gp[1], gp[2], x, y, w, h); } void x11_redraw_goal(struct app_state *state, uint_fast32_t mask) { Display *display = XtDisplay(state->goal); Window window = XtWindow(state->goal); Dimension w, h; int i; XtVaGetValues(state->goal, XmNwidth, &w, XmNheight, &h, (char *)NULL); for (i = 0; i < 9; i++) { int x = i%3, y = i/3; if (mask & 1) { redraw_goal_tile(state, display, window, x, y, w/3, h/3); } /* * Goal bitmaps have a gap of 2 tiles between each row. * This funny shift will accomodate that. */ mask >>= 1|x; } } /* Render the goal area as the window's icon */ void x11_redraw_icon(struct app_state *state, Widget shell) { Display *display = XtDisplay(shell); Dimension tilesz = ICON_SIZE/3; int i, j, tile; XColor colours[(TILE_MAX-1)*COLOUR_MAX]; unsigned long *icons[ICON_MAX]; void *wm_icon = NULL; if (state->use_ewmh_icons && (wm_icon = ewmh_icon_alloc(icons))) { Colormap cmap = DefaultColormapOfScreen(XtScreen(shell)); for (i = 0; i < TILE_MAX-1; i++) { for (j = 0; j < COLOUR_MAX; j++) { XColor *c = &colours[i*COLOUR_MAX+j]; c->pixel = state->tile_colour[i][j]; } } XQueryColors(display, cmap, colours, i*j); } for (i = 0; i < 9; i++) { tile = redraw_goal_tile(state, display, state->icon_pixmap, i%3, i/3, tilesz, tilesz); if (wm_icon) { XColor *c = &colours[(tile-1)*COLOUR_MAX]; ewmh_tile16(icons[ICON_16x16], c, i%3, i/3); ewmh_tile24(icons[ICON_24x24], c, i%3, i/3); ewmh_tile32(icons[ICON_32x32], c, i%3, i/3); ewmh_tile48(icons[ICON_48x48], c, i%3, i/3); } } /* * Clear and reset XmNiconPixmap otherwise it seems dtwm will not * notice the changed icon. */ XtVaSetValues(shell, XmNiconPixmap, None, (char *)NULL); XtVaSetValues(shell, XmNiconPixmap, state->icon_pixmap, (char *)NULL); if (wm_icon) { Atom net_wm_icon = XInternAtom(display, "_NET_WM_ICON", FALSE); XChangeProperty(display, XtWindow(shell), net_wm_icon, XA_CARDINAL, 32, PropModeReplace, wm_icon, EWMH_ICON_NELEM); free(wm_icon); } } void x11_redraw_game(struct app_state *state, uint_fast32_t mask) { Display *display = XtDisplay(state->goal); Window game = XtWindow(state->game); uint_least32_t *gp = state->board.game; Dimension w, h; int i; XtVaGetValues(state->game, XmNwidth, &w, XmNheight, &h, (char *)NULL); w /= 5; h /= 5; for (i = 0; i < 25; i++) { if (mask & 1) { redraw_tile(state, display, game, gp[0], gp[1], gp[2], i%5, i/5, w, h); } mask >>= 1; } }