Debian Codegraph @

src/main-sdl2.c

raw symbols SCIP
1 /**
2 * \file main-sdl2.c
3 * \brief Angband SDL2 port
4 *
5 * Copyright (c) 1997 Ben Harrison and others
6 *
7 * This work is free software; you can redistribute it and/or modify it
8 * under the terms of either:
9 *
10 * a) the GNU General Public License as published by the Free Software
11 * Foundation, version 2, or
12 *
13 * b) the "Angband licence":
14 * This software may be copied and distributed for educational, research,
15 * and not for profit purposes provided that this copyright and statement
16 * are included in all such copies. Other copyrights may also apply.
17 */
18
19 #include "angband.h"
20
21 #ifdef USE_SDL2
22
23 #include "SDL.h"
24 #include "SDL_image.h"
25 #include "SDL_ttf.h"
26
27 #include "main.h"
28 #include "init.h"
29 #include "ui-term.h"
30 #include "buildid.h"
31 #include "ui-display.h"
32 #include "ui-command.h"
33 #include "player-calcs.h"
34 #include "ui-output.h"
35 #include "game-world.h"
36 #include "ui-input.h"
37 #include "ui-prefs.h"
38 #include "grafmode.h"
39 #include "ui-game.h"
40 #include "ui-map.h"
41 #include "parser.h"
42
43 #define MAX_SUBWINDOWS \
44 ANGBAND_TERM_MAX
45 /* that should be plenty... */
46 #define MAX_WINDOWS 4
47 #define MAX_BUTTONS 32
48 /*
49 * Since font selection goes through a menu panel with MAX_BUTTONS, there's
50 * no point to having any more than can be selected with that menu.
51 */
52 #define MAX_FONTS (MAX_BUTTONS)
53
54 #define INIT_SDL_FLAGS \
55 (SDL_INIT_VIDEO)
56 #define INIT_IMG_FLAGS \
57 (IMG_INIT_PNG)
58
59 /* this is the main term screen, where all the action takes place */
60 #define MAIN_SUBWINDOW 0
61 /* for symmetry with main subwindow */
62 #define MAIN_WINDOW 0
63 /* size of the keypress queue (term->key_queue) */
64 #define SUBWINDOW_KEYQ_SIZE(subwindow_ptr) \
65 ((subwindow_ptr)->index == MAIN_SUBWINDOW ? 1024 : 32)
66
67 #define DEFAULT_DISPLAY 0
68
69 #define DEFAULT_CONFIG_FILE "sdl2init.txt"
70 #define DEFAULT_CONFIG_FILE_DIR \
71 ANGBAND_DIR_USER
72
73 #define DEFAULT_ALPHA_FULL 0xFF
74 #define ALPHA_PERCENT(p) \
75 (DEFAULT_ALPHA_FULL * (p) / 100)
76 #define DEFAULT_ALPHA_LOW \
77 ALPHA_PERCENT(80)
78 /* for "Alpha" button; in percents */
79 #define DEFAULT_ALPHA_STEP 10
80 #define DEFAULT_ALPHA_LOWEST 0
81
82 #define DEFAULT_WALLPAPER "att-128.png"
83 #define DEFAULT_WALLPAPER_DIR \
84 ANGBAND_DIR_ICONS
85 #define DEFAULT_WINDOW_ICON "att-32.png"
86 #define DEFAULT_WINDOW_ICON_DIR \
87 ANGBAND_DIR_ICONS
88 #define DEFAULT_ABOUT_ICON "att-128.png"
89 #define DEFAULT_ABOUT_ICON_DIR \
90 ANGBAND_DIR_ICONS
91
92 #define DEFAULT_FONT_HINTING \
93 TTF_HINTING_LIGHT
94 /* border of subwindows, in pixels */
95 #define DEFAULT_BORDER 8
96 #define DEFAULT_XTRA_BORDER \
97 (DEFAULT_BORDER * 2)
98 #define DEFAULT_VISIBLE_BORDER 2
99
100 /* XXX hack: the widest character present in a font
101 * for determining font advance (width) */
102 #define GLYPH_FOR_ADVANCE 'W'
103 #define DEFAULT_VECTOR_FONT_SIZE 12
104
105 #define DEFAULT_FONT "10x20x.fon"
106 #define DEFAULT_FONT_W 10
107 #define DEFAULT_FONT_H 20
108
109 #define DEFAULT_STATUS_BAR_FONT "8x13x.fon"
110
111 #define MAX_VECTOR_FONT_SIZE 32
112 #define MIN_VECTOR_FONT_SIZE 4
113
114 #define DEFAULT_BUTTON_BORDER 8
115 #define DEFAULT_LINE_HEIGHT(h) ((h) * 150 / 100)
116 #define DEFAULT_MENU_LINE_HEIGHT(h) ((h) * 200 / 100)
117 #define DEFAULT_MENU_LINE_WIDTH(w) \
118 ((w) + DEFAULT_BUTTON_BORDER + DEFAULT_XTRA_BORDER)
119 /* update period in window delays (160 milliseconds, assuming 60 fps) */
120 #define DEFAULT_IDLE_UPDATE_PERIOD 10
121
122 #define DEFAULT_WINDOW_BG_COLOR \
123 COLOUR_L_DARK
124 #define DEFAULT_SUBWINDOW_BG_COLOR \
125 COLOUR_DARK
126 #define DEFAULT_SUBWINDOW_CURSOR_COLOR \
127 COLOUR_YELLOW
128 #define DEFAULT_STATUS_BAR_BG_COLOR \
129 COLOUR_DARK
130 #define DEFAULT_SHADE_COLOR \
131 COLOUR_SHADE
132 #define DEFAULT_SUBWINDOW_BORDER_COLOR \
133 COLOUR_SHADE
134 #define DEFAULT_STATUS_BAR_BUTTON_ACTIVE_COLOR \
135 COLOUR_WHITE
136 #define DEFAULT_STATUS_BAR_BUTTON_INACTIVE_COLOR \
137 COLOUR_L_DARK
138
139 #define DEFAULT_MENU_FG_ACTIVE_COLOR \
140 COLOUR_WHITE
141 #define DEFAULT_MENU_FG_INACTIVE_COLOR \
142 COLOUR_WHITE
143 #define DEFAULT_MENU_BG_ACTIVE_COLOR \
144 COLOUR_SHADE
145 #define DEFAULT_MENU_BG_INACTIVE_COLOR \
146 COLOUR_DARK
147
148 #define DEFAULT_MENU_TOGGLE_FG_ACTIVE_COLOR \
149 COLOUR_WHITE
150 #define DEFAULT_MENU_TOGGLE_FG_INACTIVE_COLOR \
151 COLOUR_L_DARK
152
153 #define DEFAULT_MENU_PANEL_OUTLINE_COLOR \
154 COLOUR_SHADE
155
156 #define DEFAULT_ERROR_COLOR \
157 COLOUR_RED
158
159 #define DEFAULT_ABOUT_BG_COLOR \
160 COLOUR_SHADE
161 #define DEFAULT_ABOUT_BORDER_OUTER_COLOR \
162 COLOUR_L_DARK
163 #define DEFAULT_ABOUT_BORDER_INNER_COLOR \
164 COLOUR_WHITE
165 #define DEFAULT_ABOUT_TEXT_COLOR \
166 COLOUR_WHITE
167
168 /* shockbolt's tiles are 64x64; dungeon is 198 tiles long;
169 * 64 * 198 is 12672 which is bigger than any possible texture! */
170 #define REASONABLE_MAP_TILE_WIDTH 16
171 #define REASONABLE_MAP_TILE_HEIGHT 16
172
173 /* angband needs at least 80x24 main term, else severe bugs happen */
174 #define MIN_COLS_MAIN 80
175 #define MIN_ROWS_MAIN 24
176 /* some reasonable values - we dont want the player to resize
177 * the term into nothing! */
178 #define MIN_COLS_OTHER 12
179 #define MIN_ROWS_OTHER 3
180
181 #define MIN_TILE_WIDTH 1
182 #define MAX_TILE_WIDTH 9
183 #define MIN_TILE_HEIGHT 1
184 #define MAX_TILE_HEIGHT 9
185
186 /* some random numbers */
187 #define DEFAULT_WINDOW_MINIMUM_W 198
188 #define DEFAULT_WINDOW_MINIMUM_H 66
189
190 #define DEFAULT_SNAP_RANGE \
191 DEFAULT_FONT_W
192
193 #define CHECK_BUTTON_DATA_TYPE(button, data_type) \
194 assert((button)->data.type == (data_type))
195
196 enum wallpaper_mode {
197 /* so that we won't forget to actually set wallpaper */
198 WALLPAPER_INVALID = 0,
199 WALLPAPER_DONT_SHOW,
200 WALLPAPER_TILED,
201 WALLPAPER_CENTERED,
202 WALLPAPER_SCALED
203 };
204
205 enum button_data_type {
206 BUTTON_DATA_INVALID = 0,
207 BUTTON_DATA_NONE,
208 BUTTON_DATA_INT,
209 BUTTON_DATA_UNSIGNED,
210 BUTTON_DATA_SUBWINDOW,
211 BUTTON_DATA_FONT,
212 BUTTON_DATA_TERM_FLAG,
213 BUTTON_DATA_ALPHA
214 };
215
216 enum button_movesize {
217 BUTTON_MOVESIZE_INVALID = 0,
218 BUTTON_MOVESIZE_MOVING,
219 BUTTON_MOVESIZE_SIZING
220 };
221
222 enum button_tile_scale {
223 BUTTON_TILE_SIZE_INVALID = 0,
224 BUTTON_TILE_SIZE_WIDTH,
225 BUTTON_TILE_SIZE_HEIGHT
226 };
227
228 enum button_caption_position {
229 CAPTION_POSITION_INVALID = 0,
230 CAPTION_POSITION_CENTER,
231 CAPTION_POSITION_LEFT,
232 CAPTION_POSITION_RIGHT
233 };
234
235 enum font_type {
236 FONT_TYPE_INVALID = 0,
237 FONT_TYPE_RASTER,
238 FONT_TYPE_VECTOR
239 };
240
241 struct ttf {
242 TTF_Font *handle;
243 struct { int w; int h; } glyph;
244 };
245
246 /* the array of ascii chars was generated by this perl script:
247 * my $ascii = join '', map /\p{Print}/ ? $_ : ' ', map chr, 0 .. 127;
248 * for (my $i = 0; $i < length($ascii); $i += 32) {
249 * my $s = substr $ascii, $i, 32;
250 * $s =~ s#\\#\\\\#g;
251 * $s =~ s#"#\\"#g;
252 * print qq(\t"$s"\n);
253 * }
254 * and then manually hacked to substitute the escape codes for the non-null
255 * control characters (0x01 to 0x1f) so the graphics characters at those
256 * indices in Angband's packaged .fon files remain available, if the user
257 * wants to access them (those characters are likely the ones specified by
258 * code page 437, https://en.wikipedia.org/wiki/Code_page_437 ).
259 */
260 static const char g_ascii_codepoints_for_cache[] =
261 " \x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
262 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
263 " !\"#$%&'()*+,-./0123456789:;<=>?"
264 "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
265 "`abcdefghijklmnopqrstuvwxyz{|}~ ";
266 /* Simple font cache. Only for ascii (which is like 99.99% (?) of what the game
267 * displays, anyway) */
268 #define ASCII_CACHE_SIZE \
269 (N_ELEMENTS(g_ascii_codepoints_for_cache) - 1)
270 struct font_cache {
271 SDL_Texture *texture;
272 /* it wastes some space... so what? */
273 SDL_Rect rects[ASCII_CACHE_SIZE];
274 };
275 /* 0 is also a valid codepoint, of course... that's just for finding bugs */
276 #define IS_CACHED_ASCII_CODEPOINT(c) \
277 ((c) > 0 && (c) < ASCII_CACHE_SIZE)
278
279 struct font {
280 struct ttf ttf;
281 char *name;
282 char *path;
283 int size;
284 /* index of font in g_font_info array */
285 size_t index;
286
287 struct font_cache cache;
288 };
289
290 struct subwindow_border {
291 bool visible;
292 bool error;
293 SDL_Color color;
294 int width;
295 };
296
297 struct subwindow_config {
298 char *font_name;
299 int font_size;
300 };
301
302 struct window_config {
303 int renderer_flags;
304 int renderer_index;
305 int window_flags;
306
307 char *wallpaper_path;
308 char *font_name;
309 int font_size;
310 };
311
312 /* struct subwindow is representation of angband's term */
313 struct subwindow {
314 bool inited;
315 bool loaded;
316 bool linked;
317 bool visible;
318
319 struct subwindow_config *config;
320
321 /* top in z-order */
322 bool top;
323 bool always_top;
324
325 unsigned index;
326
327 int rows;
328 int cols;
329
330 /* struct ttf also has this information; these members are
331 * just for convinience */
332 int font_width;
333 int font_height;
334
335 /* coordinates of full rect are relative to coordinates of window
336 * (basically, full rect is texture) */
337 SDL_Rect full_rect;
338 /* coordinates of inner rect are relative to that of full rect */
339 SDL_Rect inner_rect;
340 /* for use when resizing term */
341 SDL_Rect sizing_rect;
342 /* a one pixel texture, mostly for displaying something when
343 * the player is resizing term */
344 SDL_Texture *aux_texture;
345
346 /* background color */
347 SDL_Color color;
348
349 struct subwindow_border borders;
350
351 SDL_Texture *texture;
352
353 struct font *font;
354
355 struct window *window;
356 struct term *term;
357 };
358
359 struct button;
360
361 struct button_bank {
362 struct button *buttons;
363 size_t size;
364 size_t number;
365 };
366
367 struct menu_panel {
368 SDL_Rect rect;
369 struct button_bank button_bank;
370 struct menu_panel *next;
371 };
372
373 typedef void (*button_render)(const struct window *window,
374 struct button *button);
375 typedef bool (*button_event)(struct window *window,
376 struct button *button, const SDL_Event *event);
377 typedef void (*button_menu)(struct window *window,
378 struct button *button, const SDL_Event *event,
379 struct menu_panel *menu_panel);
380
381 struct font_value {
382 struct subwindow *subwindow;
383 /* index of font in g_font_info array */
384 size_t index;
385 bool size_ok;
386 };
387
388 struct term_flag_value {
389 struct subwindow *subwindow;
390 uint32_t flag;
391 };
392
393 struct alpha_value {
394 struct subwindow *subwindow;
395 int real_value;
396 int show_value;
397 };
398
399 struct button_data {
400 enum button_data_type type;
401 union {
402 int int_value;
403 unsigned unsigned_value;
404 struct subwindow *subwindow_value;
405 struct font_value font_value;
406 struct term_flag_value term_flag_value;
407 struct alpha_value alpha_value;
408 } value;
409 };
410
411 struct menu_elem {
412 const char *caption;
413 struct button_data data;
414 button_render on_render;
415 button_menu on_menu;
416 bool disabled;
417 };
418
419 struct button_callbacks {
420 /* this function should render the button;
421 * otherwise, the button will be invisible */
422 button_render on_render;
423 /* event handler for status bar buttons */
424 button_event on_event;
425 /* event handler for buttons in "Menu" */
426 button_menu on_menu;
427 };
428
429 struct button {
430 /* disabled, if true, means that the on_event or on_menu callbacks
431 * won't be invoked and the button will be drawn with an altered
432 * appearance to indicate that it currently doesn't do anything. */
433 bool disabled;
434 /* selected means the user pointed at button and
435 * pressed mouse button (but not released yet) */
436 bool selected;
437 /* highlighted means the user pointed
438 * at button but not clicking yet */
439 bool highlighted;
440 /* At least, that was the theory; buttons in "Menu" work a bit differently,
441 * some are selected when the user points at button, all are highlighted
442 * regardless of what event happened on them (click or mouse motion)... */
443
444 char *caption;
445
446 SDL_Rect full_rect;
447 SDL_Rect inner_rect;
448
449 struct button_data data;
450 struct button_callbacks callbacks;
451 };
452
453 struct status_bar {
454 struct font *font;
455
456 struct button_bank button_bank;
457 struct menu_panel *menu_panel;
458
459 struct window *window;
460
461 SDL_Rect full_rect;
462 SDL_Rect inner_rect;
463 SDL_Color color;
464 SDL_Texture *texture;
465
466 bool in_menu;
467 };
468
469 struct graphics {
470 SDL_Texture *texture;
471 int id;
472 int tile_pixel_w;
473 int tile_pixel_h;
474
475 int overdraw_row;
476 int overdraw_max;
477 };
478
479 /* thats for dragging terms */
480 struct move_state {
481 bool active;
482 bool moving;
483
484 int originx;
485 int originy;
486
487 struct subwindow *subwindow;
488 };
489
490 /* thats for resizing terms */
491 struct size_state {
492 bool active;
493 bool sizing;
494
495 int originx;
496 int originy;
497
498 bool left;
499 bool top;
500
501 struct subwindow *subwindow;
502 };
503
504 struct wallpaper {
505 int w;
506 int h;
507 SDL_Texture *texture;
508 enum wallpaper_mode mode;
509 };
510
511 struct stipple {
512 int w, h;
513 SDL_Texture *texture;
514 };
515
516 /* struct window is a real window on screen, it has one or more
517 * subwindows (terms) in it */
518 struct window {
519 bool inited;
520 bool loaded;
521
522 /* id is SDL's id, for use with events */
523 Uint32 id;
524 /* and this is our id, mostly for debugging */
525 unsigned index;
526
527 struct window_config *config;
528
529 /* does window have mouse focus? */
530 bool focus;
531 /* window has changed and must be redrawn */
532 bool dirty;
533
534 /* limiter for frames */
535 Uint32 next_redraw;
536 /* from display mode */
537 int delay;
538
539 /* as reported by SDL_GetWindowFlags() */
540 Uint32 flags;
541
542 /* position and size of window as it is on display */
543 SDL_Rect full_rect;
544 /* size of window without status bar, basically */
545 SDL_Rect inner_rect;
546
547 SDL_Color color;
548 /* for making terms transparent while moving or sizing them */
549 Uint8 alpha;
550
551 SDL_Window *window;
552 SDL_Renderer *renderer;
553
554 int pixelformat;
555
556 struct wallpaper wallpaper;
557 struct stipple stipple;
558 struct move_state move_state;
559 struct size_state size_state;
560 struct status_bar status_bar;
561 struct graphics graphics;
562
563 struct subwindow *subwindows[MAX_SUBWINDOWS];
564 };
565
566 struct font_info {
567 char *name;
568 char *path;
569 int size;
570 size_t index;
571 enum font_type type;
572 bool loaded;
573 };
574
575 const char help_sdl2[] = "SDL2 frontend";
576 static SDL_Color g_colors[MAX_COLORS];
577 static struct font_info g_font_info[MAX_FONTS];
578 /* these arrays contain windows and terms that the ui operates on */
579 static struct subwindow g_subwindows[MAX_SUBWINDOWS];
580 static struct window g_windows[MAX_WINDOWS];
581 /* True if KC_MOD_KEYPAD will be sent for numeric keypad keys at the expense
582 * of not handling some keyboard layouts properly. */
583 static int g_kp_as_mod = 1;
584
585 /* Forward declarations */
586
587 static void init_globals(void);
588 static void free_globals(void);
589 static bool read_config_file(void);
590 static void dump_config_file(void);
591 static void init_colors(void);
592 static void start_windows(void);
593 static void start_window(struct window *window);
594 static void load_font(struct font *font);
595 static bool reload_font(struct subwindow *subwindow,
596 const struct font_info *info);
597 static void free_font(struct font *font);
598 static const struct font_info *find_font_info(const char *name);
599 static void get_string_metrics(struct font *font, const char *str, int *w, int *h);
600 static struct window *get_new_window(unsigned index);
601 static void wipe_window(struct window *window, int display);
602 /* create default config for spawning a window via gui */
603 static void wipe_window_aux_config(struct window *window);
604 static void adjust_window_geometry(struct window *window);
605 static void free_window(struct window *window);
606 static struct window *get_window_by_id(Uint32 id);
607 static struct window *get_window_direct(unsigned index);
608 static bool has_visible_subwindow(const struct window *window, unsigned index);
609 static void resize_window(struct window *window, int w, int h);
610 static struct subwindow *get_new_subwindow(unsigned index);
611 static void load_subwindow(struct window *window, struct subwindow *subwindow);
612 static bool is_subwindow_loaded(unsigned index);
613 static struct subwindow *transfer_subwindow(struct window *window, unsigned index);
614 static struct subwindow *get_subwindow_by_xy(const struct window *window, int x, int y);
615 static struct subwindow *get_subwindow_by_index(const struct window *window,
616 unsigned index, bool visible);
617 static struct subwindow *get_subwindow_direct(unsigned index);
618 /* this function loads new subwindow if it's not already loaded */
619 static struct subwindow *make_subwindow(struct window *window, unsigned index);
620 static void sort_to_top(struct window *window);
621 static void bring_to_top(struct window *window, struct subwindow *subwindow);
622 static void render_borders(struct subwindow *subwindow);
623 static SDL_Texture *load_image(const struct window *window, const char *path);
624 static void reload_all_graphics(graphics_mode *mode);
625 static void free_graphics(struct graphics *graphics);
626 static void load_terms(void);
627 static void load_term(struct subwindow *subwindow);
628 static void clear_pw_flag(struct subwindow *subwindow);
629 static bool adjust_subwindow_geometry(const struct window *window,
630 struct subwindow *subwindow);
631 static bool is_ok_col_row(const struct subwindow *subwindow,
632 const SDL_Rect *rect, int cell_w, int cell_h);
633 static void resize_rect(SDL_Rect *rect,
634 int left, int top, int right, int bottom);
635 static void crop_rects(SDL_Rect *src, SDL_Rect *dst);
636 static bool is_point_in_rect(int x, int y, const SDL_Rect *rect);
637 static bool is_close_to(int a, int b, unsigned range);
638 static bool is_over_status_bar(const struct status_bar *status_bar, int x, int y);
639 static void make_button_bank(struct button_bank *bank);
640 static void free_button_bank(struct button_bank *button_bank);
641 static void free_menu_panel(struct menu_panel *menu_panel);
642 static struct menu_panel *get_menu_panel_by_xy(struct menu_panel *menu_panel,
643 int x, int y);
644 static void refresh_angband_terms(void);
645 static void handle_quit(void);
646 static void wait_anykey(void);
647
648 /* Functions */
649
650 static void render_clear(const struct window *window,
651 SDL_Texture *texture, const SDL_Color *color)
652 {
653 SDL_SetRenderTarget(window->renderer, texture);
654 SDL_SetRenderDrawColor(window->renderer,
655 color->r, color->g, color->b, color->a);
656 SDL_RenderClear(window->renderer);
657 }
658
659 static void render_wallpaper_tiled(const struct window *window)
660 {
661 SDL_SetRenderTarget(window->renderer, NULL);
662
663 SDL_Rect rect = {0, 0, window->wallpaper.w, window->wallpaper.h};
664 for (rect.y = window->inner_rect.y;
665 rect.y < window->inner_rect.h;
666 rect.y += rect.h)
667 {
668 for (rect.x = window->inner_rect.x;
669 rect.x < window->inner_rect.w;
670 rect.x += rect.w)
671 {
672 SDL_RenderCopy(window->renderer, window->wallpaper.texture, NULL, &rect);
673 }
674 }
675 }
676
677 static void render_wallpaper_scaled(const struct window *window)
678 {
679 SDL_SetRenderTarget(window->renderer, NULL);
680 SDL_RenderCopy(window->renderer, window->wallpaper.texture, NULL, NULL);
681 }
682
683 static void render_wallpaper_centered(const struct window *window)
684 {
685 SDL_Rect rect;
686
687 rect.w = window->wallpaper.w;
688 rect.h = window->wallpaper.h;
689 rect.x = window->inner_rect.x + (window->inner_rect.w - rect.w) / 2;
690 rect.y = window->inner_rect.y + (window->inner_rect.h - rect.h) / 2;
691
692 SDL_SetRenderTarget(window->renderer, NULL);
693 SDL_RenderCopy(window->renderer, window->wallpaper.texture, NULL, &rect);
694 }
695
696 static void render_background(const struct window *window)
697 {
698 render_clear(window, NULL, &window->color);
699
700 switch (window->wallpaper.mode) {
701 case WALLPAPER_DONT_SHOW:
702 return;
703 case WALLPAPER_TILED:
704 render_wallpaper_tiled(window);
705 return;
706 case WALLPAPER_CENTERED:
707 render_wallpaper_centered(window);
708 return;
709 case WALLPAPER_SCALED:
710 render_wallpaper_scaled(window);
711 return;
712 default:
713 quit_fmt("bad wallpaper mode %d in window %u",
714 window->wallpaper.mode, window->index);
715 break;
716 }
717 }
718
719 static void stipple_button(const struct window *window,
720 const struct button *button, SDL_Texture *dst_texture)
721 {
722 SDL_Rect srect = { 0, 0, 0, 0 }, drect;
723 int ylim = button->full_rect.y + button->full_rect.h;
724 int xlim = button->full_rect.x + button->full_rect.w;
725
726 if (!window->stipple.texture) {
727 return;
728 }
729 SDL_SetRenderTarget(window->renderer, dst_texture);
730 for (drect.y = button->full_rect.y; drect.y < ylim;
731 drect.y += window->stipple.h) {
732 if (drect.y + window->stipple.h > ylim) {
733 drect.h = ylim - drect.y;
734 } else {
735 drect.h = window->stipple.h;
736 }
737 srect.h = drect.h;
738 for (drect.x = button->full_rect.x; drect.x < xlim;
739 drect.x += window->stipple.w) {
740 if (drect.x + window->stipple.w > xlim) {
741 drect.w = xlim - drect.x;
742 } else {
743 drect.w = window->stipple.w;
744 }
745 srect.w = drect.w;
746 SDL_RenderCopy(window->renderer,
747 window->stipple.texture, &srect, &drect);
748 }
749 }
750 }
751
752 static void render_all(const struct window *window)
753 {
754 render_background(window);
755
756 SDL_RenderCopy(window->renderer,
757 window->status_bar.texture, NULL, &window->status_bar.full_rect);
758
759 for (size_t i = 0; i < N_ELEMENTS(window->subwindows); i++) {
760 struct subwindow *subwindow = window->subwindows[i];
761 if (subwindow != NULL && subwindow->visible) {
762 SDL_RenderCopy(window->renderer,
763 subwindow->texture,
764 NULL, &subwindow->full_rect);
765 }
766 }
767 }
768
769 static void render_status_bar(const struct window *window)
770 {
771 render_clear(window, window->status_bar.texture, &window->status_bar.color);
772
773 for (size_t i = 0; i < window->status_bar.button_bank.number; i++) {
774 struct button *button = &window->status_bar.button_bank.buttons[i];
775 if (button->callbacks.on_render != NULL) {
776 button->callbacks.on_render(window, button);
777 }
778 if (button->disabled) {
779 stipple_button(window, button,
780 window->status_bar.texture);
781 }
782 }
783 }
784
785 static void render_outline_rect(const struct window *window,
786 SDL_Texture *texture, const SDL_Rect *rect, const SDL_Color *color)
787 {
788 SDL_SetRenderTarget(window->renderer, texture);
789 SDL_SetRenderDrawColor(window->renderer,
790 color->r, color->g, color->b, color->a);
791 SDL_RenderDrawRect(window->renderer, rect);
792 }
793
794 static void render_outline_rect_width(const struct window *window,
795 SDL_Texture *texture, const SDL_Rect *rect, const SDL_Color *color, int width)
796 {
797 SDL_Rect dst = *rect;
798
799 for (int i = 0; i < width; i++) {
800 render_outline_rect(window, texture, &dst, color);
801 resize_rect(&dst, 1, 1, -1, -1);
802 }
803 }
804
805 static void render_fill_rect(const struct window *window,
806 SDL_Texture *texture, const SDL_Rect *rect, const SDL_Color *color)
807 {
808 SDL_SetRenderTarget(window->renderer, texture);
809 SDL_SetRenderDrawColor(window->renderer,
810 color->r, color->g, color->b, color->a);
811 SDL_RenderFillRect(window->renderer, rect);
812 }
813
814 static void render_window_in_menu(const struct window *window)
815 {
816 render_background(window);
817
818 SDL_SetRenderTarget(window->renderer, NULL);
819
820 for (size_t i = 0; i < N_ELEMENTS(window->subwindows); i++) {
821 struct subwindow *subwindow = window->subwindows[i];
822 if (subwindow != NULL && subwindow->visible) {
823 if (subwindow->sizing_rect.w > 0 && subwindow->sizing_rect.h > 0) {
824 SDL_SetRenderTarget(window->renderer, subwindow->aux_texture);
825 /* in case subwindow's color changed */
826 render_fill_rect(window,
827 subwindow->aux_texture, NULL, &subwindow->color);
828
829 SDL_SetRenderTarget(window->renderer, NULL);
830 SDL_RenderCopy(window->renderer,
831 subwindow->aux_texture, NULL, &subwindow->sizing_rect);
832 }
833
834 SDL_RenderCopy(window->renderer,
835 subwindow->texture,
836 NULL, &subwindow->full_rect);
837 }
838 }
839
840 /* render it last to allow the menu to draw over subwindows */
841 render_status_bar(window);
842 SDL_SetRenderTarget(window->renderer, NULL);
843 SDL_RenderCopy(window->renderer,
844 window->status_bar.texture, NULL, &window->status_bar.full_rect);
845 }
846
847 static void set_subwindow_alpha(struct subwindow *subwindow, int alpha)
848 {
849 SDL_SetTextureAlphaMod(subwindow->texture, alpha);
850 SDL_SetTextureAlphaMod(subwindow->aux_texture, alpha);
851 }
852
853 static void set_subwindows_alpha(const struct window *window, int alpha)
854 {
855 for (size_t i = 0; i < N_ELEMENTS(window->subwindows); i++) {
856 struct subwindow *subwindow = window->subwindows[i];
857 if (subwindow != NULL) {
858 set_subwindow_alpha(subwindow, alpha);
859 }
860 }
861 }
862
863 /* this function allows to perform special things that are not
864 * needed while playing the game, like moving terms */
865 static void redraw_window_in_menu(struct window *window)
866 {
867 set_subwindows_alpha(window, window->alpha);
868 render_window_in_menu(window);
869 SDL_RenderPresent(window->renderer);
870 window->next_redraw = SDL_GetTicks() + window->delay;
871 }
872
873 /* this function is mostly used while normally playing the game */
874 static void redraw_window(struct window *window)
875 {
876 if (window->status_bar.in_menu) {
877 /* we called (perhaps via refresh_angband_terms()) Term_fresh() in menu */
878 redraw_window_in_menu(window);
879 return;
880 }
881
882 /* XXX XXX dont forget to prerender status bar in loader! */
883 render_all(window);
884 SDL_RenderPresent(window->renderer);
885 window->next_redraw = SDL_GetTicks() + window->delay;
886 }
887
888 static void try_redraw_window(struct window *window)
889 {
890 if (window->next_redraw < SDL_GetTicks()) {
891 redraw_window(window);
892 }
893 }
894
895 static void redraw_all_windows(bool dirty)
896 {
897 for (unsigned i = 0; i < MAX_WINDOWS; i++) {
898 struct window *window = get_window_direct(i);
899 if (window != NULL && (dirty ? window->dirty : true)) {
900 render_status_bar(window);
901 redraw_window(window);
902 window->dirty = false;
903 }
904 }
905 }
906
907 static void render_utf8_string(const struct window *window,
908 const struct font *font, SDL_Texture *dst_texture,
909 SDL_Color fg, SDL_Rect rect, const char *utf8_string)
910 {
911 SDL_Surface *surface = TTF_RenderUTF8_Blended(font->ttf.handle, utf8_string, fg);
912 SDL_Texture *src_texture = SDL_CreateTextureFromSurface(window->renderer, surface);
913 SDL_FreeSurface(surface);
914
915 SDL_SetRenderTarget(window->renderer, dst_texture);
916 SDL_RenderCopy(window->renderer, src_texture, NULL, &rect);
917
918 SDL_DestroyTexture(src_texture);
919 }
920
921 /* this function is typically called in a loop, so for efficiency it doesn't
922 * SetRenderTarget; caller must do it (but it does SetTextureColorMod) */
923 static void render_glyph_mono(const struct window *window,
924 const struct font *font, SDL_Texture *dst_texture,
925 int x, int y, const SDL_Color *fg, uint32_t codepoint)
926 {
927 if (codepoint == ' ') {
928 return;
929 }
930
931 SDL_Rect dst = {x, y, font->ttf.glyph.w, font->ttf.glyph.h};
932
933 if (IS_CACHED_ASCII_CODEPOINT(codepoint)) {
934 SDL_Rect src = font->cache.rects[codepoint];
935
936 crop_rects(&src, &dst);
937
938 SDL_SetTextureColorMod(font->cache.texture, fg->r, fg->g, fg->b);
939
940 SDL_RenderCopy(window->renderer,
941 font->cache.texture, &font->cache.rects[codepoint], &dst);
942 } else {
943 SDL_Surface *surface = TTF_RenderGlyph_Blended(font->ttf.handle,
944 (Uint16) codepoint, *fg);
945 if (surface == NULL) {
946 return;
947 }
948
949 SDL_Texture *texture = SDL_CreateTextureFromSurface(window->renderer, surface);
950 assert(texture != NULL);
951
952 SDL_Rect src = {0, 0, surface->w, surface->h};
953
954 crop_rects(&src, &dst);
955
956 SDL_RenderCopy(window->renderer, texture, &src, &dst);
957
958 SDL_FreeSurface(surface);
959 SDL_DestroyTexture(texture);
960 }
961 }
962
963 static void render_cursor(struct subwindow *subwindow,
964 int col, int row, bool big)
965 {
966 SDL_Color color = g_colors[DEFAULT_SUBWINDOW_CURSOR_COLOR];
967 SDL_Rect rect = {
968 subwindow->inner_rect.x + subwindow->font_width * col,
969 subwindow->inner_rect.y + subwindow->font_height * row,
970 subwindow->font_width * (big ? tile_width : 1),
971 subwindow->font_height * (big ? tile_height : 1)
972 };
973
974 render_outline_rect(subwindow->window, subwindow->texture,
975 &rect, &color);
976 }
977
978 static void render_grid_cell_text(const struct subwindow *subwindow,
979 SDL_Texture *texture, int x, int y)
980 {
981 struct grid_data grid_data;
982 int a;
983 int ta;
984 wchar_t c;
985 wchar_t tc;
986
987 map_info(loc(x, y), &grid_data);
988 grid_data_as_text(&grid_data, &a, &c, &ta, &tc);
989 /* apparently either the same as a or obscured by a */
990 (void) tc;
991
992 SDL_Color fg = g_colors[a % MAX_COLORS];
993 SDL_Color bg;
994
995 switch (ta / MULT_BG) {
996 case BG_BLACK:
997 bg = subwindow->color;
998 break;
999 case BG_SAME:
1000 bg = fg;
1001 break;
1002 case BG_DARK:
1003 bg = g_colors[DEFAULT_SHADE_COLOR];
1004 break;
1005 default:
1006 /* debugging */
1007 bg = g_colors[DEFAULT_ERROR_COLOR];
1008 }
1009
1010 SDL_Rect rect = {
1011 x * subwindow->font_width,
1012 y * subwindow->font_height,
1013 subwindow->font_width,
1014 subwindow->font_height
1015 };
1016
1017 render_fill_rect(subwindow->window, texture, &rect, &bg);
1018 render_glyph_mono(subwindow->window,
1019 subwindow->font, texture, rect.x, rect.y, &fg, (uint32_t) c);
1020 }
1021
1022 /* does not SetRenderTarget */
1023 static void render_tile_rect_scaled(const struct subwindow *subwindow,
1024 int col, int row, SDL_Rect dst, int a, int c)
1025 {
1026 struct graphics *graphics = &subwindow->window->graphics;
1027
1028 SDL_Rect src = {0, 0, graphics->tile_pixel_w, graphics->tile_pixel_h};
1029
1030 int src_row = a & 0x7f;
1031 int src_col = c & 0x7f;
1032
1033 src.x = src_col * src.w;
1034 src.y = src_row * src.h;
1035
1036 if (graphics->overdraw_row != 0
1037 && src_row >= graphics->overdraw_row
1038 && src_row <= graphics->overdraw_max)
1039 {
1040 src.y -= src.h;
1041 dst.y -= dst.h;
1042 dst.h *= 2;
1043 src.h *= 2;
1044 }
1045
1046 SDL_RenderCopy(subwindow->window->renderer,
1047 graphics->texture, &src, &dst);
1048 }
1049
1050 static void render_tile_font_scaled(const struct subwindow *subwindow,
1051 int col, int row, int a, int c, bool fill, int dhrclip)
1052 {
1053 struct graphics *graphics = &subwindow->window->graphics;
1054
1055 SDL_Rect dst = {
1056 subwindow->inner_rect.x + col * subwindow->font_width,
1057 subwindow->inner_rect.y + row * subwindow->font_height,
1058 subwindow->font_width * tile_width,
1059 subwindow->font_height * tile_height
1060 };
1061
1062 if (fill) {
1063 render_fill_rect(subwindow->window, subwindow->texture, &dst, &subwindow->color);
1064 }
1065
1066 SDL_Rect src = {0, 0, graphics->tile_pixel_w, graphics->tile_pixel_h};
1067
1068 SDL_SetRenderTarget(subwindow->window->renderer, subwindow->texture);
1069
1070 int src_row = a & 0x7f;
1071 int src_col = c & 0x7f;
1072
1073 src.x = src_col * src.w;
1074 src.y = src_row * src.h;
1075
1076 if (graphics->overdraw_row != 0
1077 && row > dhrclip
1078 && src_row >= graphics->overdraw_row
1079 && src_row <= graphics->overdraw_max)
1080 {
1081 src.y -= src.h;
1082 dst.y -= dst.h;
1083 dst.h *= 2;
1084 src.h *= 2;
1085
1086 SDL_RenderCopy(subwindow->window->renderer,
1087 graphics->texture, &src, &dst);
1088 } else {
1089 SDL_RenderCopy(subwindow->window->renderer,
1090 graphics->texture, &src, &dst);
1091 }
1092 }
1093
1094 static void render_grid_cell_tile(const struct subwindow *subwindow,
1095 SDL_Texture *texture, SDL_Rect tile, int x, int y)
1096 {
1097 struct grid_data grid_data;
1098 int a;
1099 int ta;
1100 wchar_t c;
1101 wchar_t tc;
1102
1103 map_info(loc(x, y), &grid_data);
1104 grid_data_as_text(&grid_data, &a, &c, &ta, &tc);
1105
1106 SDL_SetRenderTarget(subwindow->window->renderer, texture);
1107
1108 render_tile_rect_scaled(subwindow, x, y, tile, ta, tc);
1109
1110 if (a == ta && c == tc) {
1111 return;
1112 }
1113
1114 render_tile_rect_scaled(subwindow, x, y, tile, a, c);
1115 }
1116
1117 static void clear_all_borders(struct window *window)
1118 {
1119 for (size_t i = 0; i < N_ELEMENTS(window->subwindows); i++) {
1120 struct subwindow *subwindow = window->subwindows[i];
1121 if (subwindow != NULL) {
1122 subwindow->borders.error = false;
1123 render_borders(subwindow);
1124 }
1125 }
1126 }
1127
1128 static void render_borders(struct subwindow *subwindow)
1129 {
1130 SDL_Rect rect = {0};
1131 SDL_QueryTexture(subwindow->texture, NULL, NULL, &rect.w, &rect.h);
1132
1133 SDL_Color *color;
1134 if (!subwindow->borders.error) {
1135 if (subwindow->borders.visible) {
1136 color = &subwindow->borders.color;
1137 } else {
1138 color = &subwindow->color;
1139 }
1140 } else {
1141 color = &g_colors[DEFAULT_ERROR_COLOR];
1142 }
1143
1144 render_outline_rect_width(subwindow->window,
1145 subwindow->texture, &rect, color,
1146 subwindow->borders.width);
1147 }
1148
1149 static SDL_Texture *make_subwindow_texture(const struct window *window, int w, int h)
1150 {
1151 SDL_Texture *texture = SDL_CreateTexture(window->renderer,
1152 window->pixelformat, SDL_TEXTUREACCESS_TARGET, w, h);
1153 if (texture == NULL) {
1154 quit_fmt("cannot create texture for subwindow in window %u: %s",
1155 window->index, SDL_GetError());
1156 }
1157
1158 if (SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND) != 0) {
1159 SDL_DestroyTexture(texture);
1160 quit_fmt("cannot set blend mode for texture in window %u: %s",
1161 window->index, SDL_GetError());
1162 }
1163
1164 return texture;
1165 }
1166
1167 static void render_menu_panel(const struct window *window, struct menu_panel *menu_panel)
1168 {
1169 if (menu_panel == NULL) {
1170 return;
1171 }
1172
1173 for (size_t i = 0; i < menu_panel->button_bank.number; i++) {
1174 struct button *button = &menu_panel->button_bank.buttons[i];
1175
1176 assert(button->callbacks.on_render != NULL);
1177 button->callbacks.on_render(window, button);
1178 if (button->disabled) {
1179 stipple_button(window, button, NULL);
1180 }
1181 }
1182 render_outline_rect(window,
1183 NULL, &menu_panel->rect, &g_colors[DEFAULT_MENU_PANEL_OUTLINE_COLOR]);
1184
1185 /* recurse */
1186 render_menu_panel(window, menu_panel->next);
1187 }
1188
1189 static SDL_Rect get_button_caption_rect(const struct button *button)
1190 {
1191 SDL_Rect rect = {
1192 button->full_rect.x + button->inner_rect.x,
1193 button->full_rect.y + button->inner_rect.y,
1194 button->inner_rect.w,
1195 button->inner_rect.h
1196 };
1197
1198 return rect;
1199 }
1200
1201 static void render_button_menu(const struct window *window,
1202 struct button *button, const SDL_Color *fg, const SDL_Color *bg)
1203 {
1204 SDL_Rect rect = get_button_caption_rect(button);
1205
1206 render_fill_rect(window,
1207 NULL, &button->full_rect, bg);
1208 render_utf8_string(window, window->status_bar.font, NULL,
1209 *fg, rect, button->caption);
1210 }
1211
1212 static void render_button_menu_toggle(const struct window *window,
1213 struct button *button, bool active)
1214 {
1215 SDL_Color *bg;
1216 SDL_Color *fg;
1217
1218 if (active) {
1219 fg = &g_colors[DEFAULT_MENU_TOGGLE_FG_ACTIVE_COLOR];
1220 } else {
1221 fg = &g_colors[DEFAULT_MENU_TOGGLE_FG_INACTIVE_COLOR];
1222 }
1223 if (button->highlighted) {
1224 bg = &g_colors[DEFAULT_MENU_BG_ACTIVE_COLOR];
1225 } else {
1226 bg = &g_colors[DEFAULT_MENU_BG_INACTIVE_COLOR];
1227 }
1228
1229 render_button_menu(window, button, fg, bg);
1230 }
1231
1232 static void render_button_menu_simple(const struct window *window, struct button *button)
1233 {
1234 SDL_Color *fg;
1235 SDL_Color *bg;
1236
1237 if (button->highlighted) {
1238 fg = &g_colors[DEFAULT_MENU_FG_ACTIVE_COLOR];
1239 bg = &g_colors[DEFAULT_MENU_BG_ACTIVE_COLOR];
1240 } else {
1241 fg = &g_colors[DEFAULT_MENU_FG_INACTIVE_COLOR];
1242 bg = &g_colors[DEFAULT_MENU_BG_INACTIVE_COLOR];
1243 }
1244
1245 render_button_menu(window, button, fg, bg);
1246 }
1247
1248 static void render_button_menu_pw(const struct window *window, struct button *button)
1249 {
1250 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_TERM_FLAG);
1251
1252 struct subwindow *subwindow = button->data.value.term_flag_value.subwindow;
1253 uint32_t flag = button->data.value.term_flag_value.flag;
1254
1255 assert(subwindow->index != MAIN_SUBWINDOW);
1256 assert(subwindow->index < N_ELEMENTS(window_flag));
1257
1258 render_button_menu_toggle(window,
1259 button, window_flag[subwindow->index] & flag);
1260 }
1261
1262 static void render_button_menu_terms(const struct window *window, struct button *button)
1263 {
1264 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_SUBWINDOW);
1265
1266 if (button->highlighted) {
1267 /* draw a border around subwindow, so that it would be easy to see
1268 * which subwindow corresponds to that button */
1269 struct subwindow *subwindow = button->data.value.subwindow_value;
1270 int outline_width = (subwindow->full_rect.w - subwindow->inner_rect.w) / 2
1271 - subwindow->borders.width;
1272 SDL_Rect outline_rect = subwindow->full_rect;
1273 resize_rect(&outline_rect,
1274 subwindow->borders.width, subwindow->borders.width,
1275 -subwindow->borders.width, -subwindow->borders.width);
1276 render_outline_rect_width(window,
1277 NULL,
1278 &outline_rect,
1279 &g_colors[DEFAULT_SUBWINDOW_BORDER_COLOR],
1280 outline_width);
1281 }
1282
1283 render_button_menu_simple(window, button);
1284 }
1285
1286 static void render_button_menu_borders(const struct window *window, struct button *button)
1287 {
1288 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_SUBWINDOW);
1289
1290 struct subwindow *subwindow = button->data.value.subwindow_value;
1291
1292 render_button_menu_toggle(window, button, subwindow->borders.visible);
1293 }
1294
1295 static void render_button_menu_alpha(const struct window *window, struct button *button)
1296 {
1297 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_ALPHA);
1298
1299 struct subwindow *subwindow = button->data.value.alpha_value.subwindow;
1300 int alpha = button->data.value.alpha_value.real_value;
1301
1302 SDL_Color fg;
1303 SDL_Color *bg;
1304
1305 if (is_close_to(alpha, subwindow->color.a, DEFAULT_ALPHA_STEP / 2)) {
1306 fg = g_colors[DEFAULT_MENU_TOGGLE_FG_ACTIVE_COLOR];
1307 } else {
1308 fg = g_colors[DEFAULT_MENU_TOGGLE_FG_INACTIVE_COLOR];
1309 }
1310 if (button->highlighted) {
1311 bg = &g_colors[DEFAULT_MENU_BG_ACTIVE_COLOR];
1312 } else {
1313 bg = &g_colors[DEFAULT_MENU_BG_INACTIVE_COLOR];
1314 }
1315
1316 SDL_Rect rect = get_button_caption_rect(button);
1317
1318 render_fill_rect(window,
1319 NULL, &button->full_rect, bg);
1320 render_utf8_string(window, window->status_bar.font, NULL,
1321 fg, rect, format(button->caption, button->data.value.alpha_value.show_value));
1322 }
1323
1324 static void render_button_menu_top(const struct window *window, struct button *button)
1325 {
1326 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_SUBWINDOW);
1327
1328 struct subwindow *subwindow = button->data.value.subwindow_value;
1329
1330 render_button_menu_toggle(window, button, subwindow->always_top);
1331 }
1332
1333 static void render_button_menu_tile_size(const struct window *window,
1334 struct button *button)
1335 {
1336 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_INT);
1337
1338 assert(button->data.value.int_value == BUTTON_TILE_SIZE_WIDTH
1339 || button->data.value.int_value == BUTTON_TILE_SIZE_HEIGHT);
1340
1341 SDL_Color fg;
1342 SDL_Color *bg;
1343
1344 fg = g_colors[DEFAULT_MENU_TOGGLE_FG_ACTIVE_COLOR];
1345 if (button->highlighted) {
1346 bg = &g_colors[DEFAULT_MENU_BG_ACTIVE_COLOR];
1347 } else {
1348 bg = &g_colors[DEFAULT_MENU_BG_INACTIVE_COLOR];
1349 }
1350
1351 SDL_Rect rect = get_button_caption_rect(button);
1352
1353 int scale = 0;
1354 if (button->data.value.int_value == BUTTON_TILE_SIZE_WIDTH) {
1355 scale = tile_width;
1356 } else if (button->data.value.int_value == BUTTON_TILE_SIZE_HEIGHT) {
1357 scale = tile_height;
1358 }
1359
1360 render_fill_rect(window,
1361 NULL, &button->full_rect, bg);
1362 render_utf8_string(window, window->status_bar.font, NULL,
1363 fg, rect, format(button->caption, scale));
1364 }
1365
1366 static void render_button_menu_tile_set(const struct window *window, struct button *button)
1367 {
1368 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_INT);
1369
1370 render_button_menu_toggle(window,
1371 button, button->data.value.int_value == current_graphics_mode->grafID);
1372 }
1373
1374 static void render_button_menu_font_size(const struct window *window,
1375 struct button *button)
1376 {
1377 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_FONT);
1378
1379 SDL_Color fg;
1380 SDL_Color *bg;
1381
1382 struct font_value font_value = button->data.value.font_value;
1383
1384 if (!font_value.size_ok) {
1385 fg = g_colors[DEFAULT_ERROR_COLOR];
1386 } else if (g_font_info[font_value.index].type == FONT_TYPE_VECTOR) {
1387 fg = g_colors[DEFAULT_MENU_TOGGLE_FG_ACTIVE_COLOR];
1388 } else {
1389 fg = g_colors[DEFAULT_MENU_TOGGLE_FG_INACTIVE_COLOR];
1390 }
1391 if (button->highlighted) {
1392 bg = &g_colors[DEFAULT_MENU_BG_ACTIVE_COLOR];
1393 } else {
1394 bg = &g_colors[DEFAULT_MENU_BG_INACTIVE_COLOR];
1395 }
1396
1397 SDL_Rect rect = get_button_caption_rect(button);
1398
1399 render_fill_rect(window,
1400 NULL, &button->full_rect, bg);
1401 render_utf8_string(window, window->status_bar.font, NULL,
1402 fg, rect, format(button->caption, font_value.subwindow->font->size));
1403 }
1404
1405 static void render_button_menu_font_name(const struct window *window, struct button *button)
1406 {
1407 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_FONT);
1408
1409 SDL_Color fg;
1410 SDL_Color *bg;
1411
1412 struct subwindow *subwindow = button->data.value.font_value.subwindow;
1413 size_t index = button->data.value.font_value.index;
1414
1415 if (!button->data.value.font_value.size_ok) {
1416 fg = g_colors[DEFAULT_ERROR_COLOR];
1417 } else if (subwindow->font->index == index) {
1418 fg = g_colors[DEFAULT_MENU_TOGGLE_FG_ACTIVE_COLOR];
1419 } else {
1420 fg = g_colors[DEFAULT_MENU_TOGGLE_FG_INACTIVE_COLOR];
1421 }
1422 if (button->highlighted) {
1423 bg = &g_colors[DEFAULT_MENU_BG_ACTIVE_COLOR];
1424 } else {
1425 bg = &g_colors[DEFAULT_MENU_BG_INACTIVE_COLOR];
1426 }
1427
1428 SDL_Rect rect = get_button_caption_rect(button);
1429
1430 render_fill_rect(window,
1431 NULL, &button->full_rect, bg);
1432 render_utf8_string(window, window->status_bar.font, NULL,
1433 fg, rect, button->caption);
1434 }
1435
1436 static void render_button_menu_window(const struct window *window,
1437 struct button *button)
1438 {
1439 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_UNSIGNED);
1440
1441 struct window *w = get_window_direct(button->data.value.unsigned_value);
1442
1443 SDL_Color fg;
1444 SDL_Color *bg;
1445
1446 if (w != NULL) {
1447 fg = g_colors[DEFAULT_MENU_TOGGLE_FG_ACTIVE_COLOR];
1448 } else {
1449 fg = g_colors[DEFAULT_MENU_TOGGLE_FG_INACTIVE_COLOR];
1450 }
1451 if (button->highlighted) {
1452 bg = &g_colors[DEFAULT_MENU_BG_ACTIVE_COLOR];
1453 } else {
1454 bg = &g_colors[DEFAULT_MENU_BG_INACTIVE_COLOR];
1455 }
1456
1457 SDL_Rect rect = get_button_caption_rect(button);
1458
1459 render_fill_rect(window, NULL, &button->full_rect, bg);
1460 render_utf8_string(window, window->status_bar.font, NULL,
1461 fg, rect, format(button->caption, button->data.value.unsigned_value));
1462 }
1463
1464 static void render_button_menu_fullscreen(const struct window *window,
1465 struct button *button)
1466 {
1467 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_NONE);
1468
1469 render_button_menu_toggle(window, button,
1470 window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP);
1471 }
1472
1473 static void render_button_menu_kp_mod(const struct window *window,
1474 struct button *button)
1475 {
1476 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_NONE);
1477
1478 render_button_menu_toggle(window, button, g_kp_as_mod);
1479 }
1480
1481 /* the menu proper is rendered via this callback, used by the "Menu" button */
1482 static void render_menu_button(const struct window *window, struct button *button)
1483 {
1484 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_NONE);
1485
1486 SDL_Color color;
1487 if (button->highlighted) {
1488 color = g_colors[DEFAULT_STATUS_BAR_BUTTON_ACTIVE_COLOR];
1489 } else {
1490 color = g_colors[DEFAULT_STATUS_BAR_BUTTON_INACTIVE_COLOR];
1491 }
1492
1493 SDL_Rect rect = get_button_caption_rect(button);
1494
1495 render_utf8_string(window, window->status_bar.font, window->status_bar.texture,
1496 color, rect, button->caption);
1497
1498 if (button->highlighted) {
1499 render_menu_panel(window, window->status_bar.menu_panel);
1500 }
1501 }
1502
1503 static void render_button_subwindows(const struct window *window, struct button *button)
1504 {
1505 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_UNSIGNED);
1506
1507 SDL_Color color;
1508 if (has_visible_subwindow(window, button->data.value.unsigned_value)
1509 || button->highlighted) {
1510 color = g_colors[DEFAULT_STATUS_BAR_BUTTON_ACTIVE_COLOR];
1511 } else {
1512 color = g_colors[DEFAULT_STATUS_BAR_BUTTON_INACTIVE_COLOR];
1513 }
1514
1515 SDL_Rect rect = get_button_caption_rect(button);
1516
1517 render_utf8_string(window, window->status_bar.font, window->status_bar.texture,
1518 color, rect, button->caption);
1519 }
1520
1521 static void render_button_movesize(const struct window *window, struct button *button)
1522 {
1523 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_INT);
1524
1525 bool active = false;
1526 switch (button->data.value.int_value) {
1527 case BUTTON_MOVESIZE_MOVING:
1528 active = window->move_state.active;
1529 break;
1530 case BUTTON_MOVESIZE_SIZING:
1531 active = window->size_state.active;
1532 break;
1533 default:
1534 quit_fmt("button '%s' has wrong int_value %d",
1535 button->caption, button->data.value.int_value);
1536 break;
1537 }
1538
1539 SDL_Color color;
1540 if (active || button->highlighted) {
1541 color = g_colors[DEFAULT_STATUS_BAR_BUTTON_ACTIVE_COLOR];
1542 } else {
1543 color = g_colors[DEFAULT_STATUS_BAR_BUTTON_INACTIVE_COLOR];
1544 }
1545
1546 SDL_Rect rect = get_button_caption_rect(button);
1547
1548 render_utf8_string(window, window->status_bar.font, window->status_bar.texture,
1549 color, rect, button->caption);
1550 }
1551
1552 static void show_about(const struct window *window)
1553 {
1554 const char *about_text[] = {
1555 buildid,
1556 "See http://www.rephial.org",
1557 "Visit our forum at http://angband.oook.cz/forum"
1558 };
1559
1560 struct { SDL_Rect rect; const char *text; } elems[N_ELEMENTS(about_text)];
1561 for (size_t i = 0; i < N_ELEMENTS(elems); i++) {
1562 elems[i].text = about_text[i];
1563 }
1564
1565 char path[4096];
1566 path_build(path, sizeof(path), DEFAULT_ABOUT_ICON_DIR, DEFAULT_ABOUT_ICON);
1567
1568 SDL_Texture *texture = load_image(window, path);
1569 SDL_Rect texture_rect = {0};
1570 SDL_QueryTexture(texture, NULL, NULL, &texture_rect.w, &texture_rect.h);
1571
1572 SDL_Rect total = {
1573 0, 0,
1574 2 * DEFAULT_XTRA_BORDER + texture_rect.w,
1575 /* the default icon just looks better without bottom border */
1576 DEFAULT_XTRA_BORDER + texture_rect.h
1577 };
1578
1579 for (size_t i = 0; i < N_ELEMENTS(elems); i++) {
1580 int w;
1581 int h;
1582 get_string_metrics(window->status_bar.font,
1583 elems[i].text, &w, &h);
1584
1585 elems[i].rect.h = h;
1586 elems[i].rect.w = w;
1587 elems[i].rect.y = total.h + (DEFAULT_LINE_HEIGHT(h) - h) / 2;
1588
1589 total.w = MAX(w + 2 * DEFAULT_XTRA_BORDER, total.w);
1590 total.h += DEFAULT_LINE_HEIGHT(h);
1591 }
1592 total.h += DEFAULT_XTRA_BORDER;
1593
1594 total.x = window->full_rect.w / 2 - total.w / 2;
1595 total.y = window->full_rect.h / 2 - total.h / 2;
1596
1597 render_window_in_menu(window);
1598
1599 render_fill_rect(window, NULL, &total, &g_colors[DEFAULT_ABOUT_BG_COLOR]);
1600 render_outline_rect_width(window, NULL, &total,
1601 &g_colors[DEFAULT_ABOUT_BORDER_OUTER_COLOR], DEFAULT_VISIBLE_BORDER);
1602 resize_rect(&total,
1603 DEFAULT_VISIBLE_BORDER, DEFAULT_VISIBLE_BORDER,
1604 -DEFAULT_VISIBLE_BORDER, -DEFAULT_VISIBLE_BORDER);
1605 render_outline_rect_width(window, NULL, &total,
1606 &g_colors[DEFAULT_ABOUT_BORDER_INNER_COLOR], DEFAULT_VISIBLE_BORDER);
1607
1608 for (size_t i = 0; i < N_ELEMENTS(elems); i++) {
1609 /* center the string in total rect */
1610 elems[i].rect.x = total.x + (total.w - elems[i].rect.w) / 2;
1611 /* make the y coord of string absolute (was relative to total rect) */
1612 elems[i].rect.y += total.y;
1613
1614 render_utf8_string(window, window->status_bar.font,
1615 NULL, g_colors[DEFAULT_ABOUT_TEXT_COLOR],
1616 elems[i].rect, elems[i].text);
1617 }
1618
1619 texture_rect.x = total.x + (total.w - texture_rect.w) / 2;
1620 texture_rect.y = total.y + DEFAULT_XTRA_BORDER;
1621
1622 SDL_SetRenderTarget(window->renderer, NULL);
1623 SDL_RenderCopy(window->renderer, texture, NULL, &texture_rect);
1624 SDL_RenderPresent(window->renderer);
1625
1626 wait_anykey();
1627
1628 SDL_DestroyTexture(texture);
1629 }
1630
1631 static void signal_move_state(struct window *window)
1632 {
1633 assert(!window->size_state.active);
1634
1635 bool was_active = window->move_state.active;
1636
1637 if (was_active) {
1638 window->move_state.active = false;
1639 window->move_state.moving = false;
1640 window->move_state.subwindow = NULL;
1641 } else {
1642 window->move_state.active = true;
1643 }
1644
1645 SDL_SetWindowGrab(window->window,
1646 was_active ? SDL_FALSE : SDL_TRUE);
1647 window->alpha = was_active ? DEFAULT_ALPHA_FULL : DEFAULT_ALPHA_LOW;
1648 }
1649
1650 static void signal_size_state(struct window *window)
1651 {
1652 assert(!window->move_state.active);
1653
1654 bool was_active = window->size_state.active;
1655
1656 if (was_active) {
1657 window->size_state.active = false;
1658 window->size_state.sizing = false;
1659 if (window->size_state.subwindow != NULL) {
1660 memset(&window->size_state.subwindow->sizing_rect,
1661 0, sizeof(window->size_state.subwindow->sizing_rect));
1662 window->size_state.subwindow = NULL;
1663 }
1664 } else {
1665 window->size_state.active = true;
1666 }
1667
1668 SDL_SetWindowGrab(window->window,
1669 was_active ? SDL_FALSE : SDL_TRUE);
1670 window->alpha = was_active ? DEFAULT_ALPHA_FULL : DEFAULT_ALPHA_LOW;
1671 }
1672
1673 static bool ignore_status_bar_button(struct window *window,
1674 struct button *button, const SDL_Event *event)
1675 {
1676 (void) window;
1677 (void) button;
1678 (void) event;
1679
1680 return false;
1681 }
1682
1683 static bool click_status_bar_button(struct window *window,
1684 struct button *button, const SDL_Event *event)
1685 {
1686 switch (event->type) {
1687 case SDL_MOUSEBUTTONDOWN:
1688 if (is_point_in_rect(event->button.x, event->button.y, &button->full_rect)) {
1689 button->selected = true;
1690 return false;
1691 }
1692 break;
1693 case SDL_MOUSEBUTTONUP:
1694 if (is_point_in_rect(event->button.x, event->button.y, &button->full_rect)
1695 && button->selected)
1696 {
1697 button->selected = false;
1698 return true;
1699 }
1700 break;
1701 case SDL_MOUSEMOTION:
1702 if (is_point_in_rect(event->motion.x, event->motion.y, &button->full_rect)) {
1703 button->highlighted = true;
1704 return false;
1705 }
1706 break;
1707 }
1708
1709 button->highlighted = false;
1710 button->selected = false;
1711
1712 return false;
1713 }
1714
1715 static bool handle_button_movesize(struct window *window,
1716 struct button *button, const SDL_Event *event)
1717 {
1718 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_INT);
1719
1720 if (!click_status_bar_button(window, button, event)) {
1721 return false;
1722 }
1723
1724 switch (button->data.value.int_value) {
1725 case BUTTON_MOVESIZE_MOVING:
1726 if (window->size_state.active) {
1727 /* toggle size button */
1728 signal_size_state(window);
1729 }
1730 signal_move_state(window);
1731 break;
1732 case BUTTON_MOVESIZE_SIZING:
1733 if (window->move_state.active) {
1734 /* toggle move button */
1735 signal_move_state(window);
1736 }
1737 signal_size_state(window);
1738 break;
1739 }
1740
1741 return true;
1742 }
1743
1744 static void push_button(struct button_bank *bank, struct font *font,
1745 const char *caption, bool disabled, struct button_data data, struct button_callbacks callbacks,
1746 const SDL_Rect *rect, enum button_caption_position position)
1747 {
1748 assert(bank->number < bank->size);
1749
1750 struct button *button = &bank->buttons[bank->number];
1751
1752 int w;
1753 int h;
1754 get_string_metrics(font, caption, &w, &h);
1755
1756 int x = 0;
1757 switch (position) {
1758 case CAPTION_POSITION_CENTER:
1759 x = (rect->w - w) / 2;
1760 break;
1761 case CAPTION_POSITION_LEFT:
1762 x = DEFAULT_BUTTON_BORDER;
1763 break;
1764 case CAPTION_POSITION_RIGHT:
1765 x = rect->w - DEFAULT_BUTTON_BORDER - w;
1766 break;
1767 default:
1768 quit_fmt("bad caption position %d in button '%s'",
1769 position, button->caption);
1770 break;
1771 }
1772
1773 button->inner_rect.x = x;
1774 button->inner_rect.y = (rect->h - h) / 2;
1775 button->inner_rect.w = w;
1776 button->inner_rect.h = h;
1777
1778 button->full_rect = *rect;
1779
1780 assert(button->full_rect.w >= button->inner_rect.w
1781 && button->full_rect.h >= button->inner_rect.h);
1782
1783 button->caption = string_make(caption);
1784
1785 button->callbacks = callbacks;
1786 button->data = data;
1787 button->disabled = disabled;
1788 button->highlighted = false;
1789 button->selected = false;
1790
1791 bank->number++;
1792 }
1793
1794 static struct menu_panel *new_menu_panel(void)
1795 {
1796 struct menu_panel *menu = mem_zalloc(sizeof(*menu));
1797
1798 make_button_bank(&menu->button_bank);
1799 menu->next = NULL;
1800
1801 return menu;
1802 }
1803
1804 /* if caption of some button is NULL, the button is not included in menu (skipped) */
1805 static struct menu_panel *make_menu_panel(const struct button *origin,
1806 struct font *font, size_t n_elems, struct menu_elem *elems)
1807 {
1808 int maxlen = 0;
1809
1810 for (size_t i = 0; i < n_elems; i++) {
1811 if (elems[i].caption == NULL) {
1812 continue;
1813 }
1814 int w;
1815 get_string_metrics(font, elems[i].caption, &w, NULL);
1816 maxlen = MAX(maxlen, w);
1817 }
1818
1819 struct menu_panel *menu_panel = new_menu_panel();
1820 if (menu_panel == NULL) {
1821 return NULL;
1822 }
1823
1824 SDL_Rect rect = {
1825 origin->full_rect.x + origin->full_rect.w,
1826 origin->full_rect.y,
1827 DEFAULT_MENU_LINE_WIDTH(maxlen),
1828 DEFAULT_MENU_LINE_HEIGHT(font->ttf.glyph.h)
1829 };
1830
1831 menu_panel->rect = rect;
1832 menu_panel->rect.h = 0;
1833
1834 for (size_t i = 0; i < n_elems; i++) {
1835 if (elems[i].caption == NULL) {
1836 continue;
1837 }
1838
1839 struct button_callbacks callbacks = {
1840 elems[i].on_render, NULL, elems[i].on_menu
1841 };
1842 push_button(&menu_panel->button_bank,
1843 font,
1844 elems[i].caption,
1845 elems[i].disabled,
1846 elems[i].data,
1847 callbacks,
1848 &rect,
1849 CAPTION_POSITION_LEFT);
1850
1851 rect.y += rect.h;
1852 menu_panel->rect.h += rect.h;
1853 }
1854
1855 return menu_panel;
1856 }
1857
1858 static void load_next_menu_panel(const struct window *window,
1859 struct menu_panel *menu_panel, const struct button *origin,
1860 size_t n_elems, struct menu_elem *elems)
1861 {
1862 assert(menu_panel->next == NULL);
1863 menu_panel->next = make_menu_panel(origin,
1864 window->status_bar.font, n_elems, elems);
1865 }
1866
1867 static void do_menu_cleanup(struct button *button,
1868 struct menu_panel *menu_panel, const SDL_Event *event)
1869 {
1870 switch (event->type) {
1871 case SDL_MOUSEMOTION: /* fallthru */
1872 case SDL_MOUSEBUTTONDOWN: /* fallthru */
1873 case SDL_MOUSEBUTTONUP:
1874 if (menu_panel->next != NULL) {
1875 free_menu_panel(menu_panel->next);
1876 menu_panel->next = NULL;
1877 }
1878 break;
1879 default:
1880 quit_fmt("non mouse event %d for button '%s'",
1881 event->type, button->caption);
1882 break;
1883 }
1884 }
1885
1886 static bool select_menu_button(struct button *button,
1887 struct menu_panel *menu_panel, const SDL_Event *event)
1888 {
1889 if (button->selected) {
1890 return false;
1891 } else {
1892 do_menu_cleanup(button, menu_panel, event);
1893 button->selected = true;
1894
1895 return true;
1896 }
1897 }
1898
1899 static bool click_menu_button(struct button *button,
1900 struct menu_panel *menu_panel, const SDL_Event *event)
1901 {
1902 /* any event on that button removes the existing panel submenus
1903 * (and clickable buttons should not have their own submenus) */
1904 do_menu_cleanup(button, menu_panel, event);
1905
1906 switch (event->type) {
1907 case SDL_MOUSEBUTTONDOWN:
1908 button->selected = true;
1909 return false;
1910 case SDL_MOUSEBUTTONUP:
1911 if (button->selected) {
1912 button->selected = false;
1913 /* click the button */
1914 return true;
1915 } else {
1916 return false;
1917 }
1918 default:
1919 return false;
1920 }
1921 }
1922
1923 static void handle_menu_window(struct window *window,
1924 struct button *button, const SDL_Event *event,
1925 struct menu_panel *menu_panel)
1926 {
1927 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_UNSIGNED);
1928
1929 if (!click_menu_button(button, menu_panel, event)) {
1930 return;
1931 }
1932
1933 struct window *other = get_window_direct(button->data.value.unsigned_value);
1934 if (other == NULL) {
1935 other = get_new_window(button->data.value.unsigned_value);
1936 assert(other != NULL);
1937 wipe_window_aux_config(other);
1938 start_window(other);
1939 }
1940 }
1941
1942 static void handle_menu_windows(struct window *window,
1943 struct button *button, const SDL_Event *event,
1944 struct menu_panel *menu_panel)
1945 {
1946 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_NONE);
1947
1948 if (!select_menu_button(button, menu_panel, event)) {
1949 return;
1950 }
1951
1952 struct menu_elem elems[MAX_WINDOWS];
1953
1954 for (unsigned i = 0; i < MAX_WINDOWS; i++) {
1955 elems[i].caption = "Window-%u";
1956 elems[i].data.type = BUTTON_DATA_UNSIGNED;
1957 elems[i].data.value.unsigned_value = i;
1958 elems[i].on_render = render_button_menu_window;
1959 elems[i].on_menu = handle_menu_window;
1960 elems[i].disabled = false;
1961 }
1962
1963 load_next_menu_panel(window, menu_panel, button, N_ELEMENTS(elems), elems);
1964 }
1965
1966 static void handle_menu_fullscreen(struct window *window,
1967 struct button *button, const SDL_Event *event,
1968 struct menu_panel *menu_panel)
1969 {
1970 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_NONE);
1971
1972 if (!click_menu_button(button, menu_panel, event)) {
1973 return;
1974 }
1975
1976 if (window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) {
1977 SDL_SetWindowFullscreen(window->window, 0);
1978 SDL_SetWindowMinimumSize(window->window,
1979 DEFAULT_WINDOW_MINIMUM_W, DEFAULT_WINDOW_MINIMUM_H);
1980 } else {
1981 SDL_SetWindowFullscreen(window->window, SDL_WINDOW_FULLSCREEN_DESKTOP);
1982 }
1983
1984 window->flags = SDL_GetWindowFlags(window->window);
1985 }
1986
1987 static void handle_menu_kp_mod(struct window *window,
1988 struct button *button, const SDL_Event *event,
1989 struct menu_panel *menu_panel)
1990 {
1991 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_NONE);
1992
1993 if (!click_menu_button(button, menu_panel, event)) {
1994 return;
1995 }
1996
1997 g_kp_as_mod = !g_kp_as_mod;
1998 }
1999
2000 static void handle_menu_about(struct window *window,
2001 struct button *button, const SDL_Event *event,
2002 struct menu_panel *menu_panel)
2003 {
2004 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_NONE);
2005
2006 if (!click_menu_button(button, menu_panel, event)) {
2007 return;
2008 }
2009
2010 show_about(window);
2011 }
2012
2013 static void handle_menu_quit(struct window *window,
2014 struct button *button, const SDL_Event *event,
2015 struct menu_panel *menu_panel)
2016 {
2017 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_NONE);
2018
2019 if (!click_menu_button(button, menu_panel, event)) {
2020 return;
2021 }
2022
2023 handle_quit();
2024 }
2025
2026 static void handle_menu_tile_set(struct window *window,
2027 struct button *button, const SDL_Event *event,
2028 struct menu_panel *menu_panel)
2029 {
2030 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_INT);
2031
2032 if (!click_menu_button(button, menu_panel, event)) {
2033 return;
2034 }
2035
2036 graphics_mode *mode = get_graphics_mode(button->data.value.int_value);
2037 assert(mode != NULL);
2038
2039 reload_all_graphics(mode);
2040
2041 refresh_angband_terms();
2042 }
2043
2044 static void handle_menu_tile_size(struct window *window,
2045 struct button *button, const SDL_Event *event,
2046 struct menu_panel *menu_panel)
2047 {
2048 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_INT);
2049
2050 if (!click_menu_button(button, menu_panel, event)) {
2051 return;
2052 }
2053 if (window->graphics.id == GRAPHICS_NONE) {
2054 return;
2055 }
2056
2057 int increment =
2058 (event->button.x - button->full_rect.x < button->full_rect.w / 2) ? -1 : +1;
2059
2060 if (button->data.value.int_value == BUTTON_TILE_SIZE_WIDTH) {
2061 tile_width += increment;
2062 if (tile_width < MIN_TILE_WIDTH) {
2063 tile_width = MAX_TILE_WIDTH;
2064 } else if (tile_width > MAX_TILE_WIDTH) {
2065 tile_width = MIN_TILE_WIDTH;
2066 }
2067 } else if (button->data.value.int_value == BUTTON_TILE_SIZE_HEIGHT) {
2068 tile_height += increment;
2069 if (tile_height < MIN_TILE_HEIGHT) {
2070 tile_height = MAX_TILE_HEIGHT;
2071 } else if (tile_height > MAX_TILE_HEIGHT) {
2072 tile_height = MIN_TILE_HEIGHT;
2073 }
2074 } else {
2075 quit_fmt("bad int_value in button '%s'", button->caption);
2076 }
2077
2078 refresh_angband_terms();
2079 }
2080
2081 static void handle_menu_tile_sizes(struct window *window,
2082 struct button *button, const SDL_Event *event,
2083 struct menu_panel *menu_panel)
2084 {
2085 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_SUBWINDOW);
2086
2087 if (!select_menu_button(button, menu_panel, event)) {
2088 return;
2089 }
2090
2091 /*
2092 * Disable the menu entries to change the tile multipliers if not
2093 * using tiles or if not at a command prompt in game (the latter
2094 * avoids multiplier changes causing blank screens in in-game menus
2095 * or display artifacts when the in-game menu is dismissed sometime
2096 * after the multiplier change).
2097 */
2098 struct menu_elem elems[] = {
2099 {
2100 "< Tile width %d >",
2101 {
2102 BUTTON_DATA_INT,
2103 {.int_value = BUTTON_TILE_SIZE_WIDTH},
2104 },
2105 render_button_menu_tile_size,
2106 handle_menu_tile_size,
2107 window->graphics.id == GRAPHICS_NONE
2108 || !character_generated || !inkey_flag
2109 },
2110 {
2111 "< Tile height %d >",
2112 {
2113 BUTTON_DATA_INT,
2114 {.int_value = BUTTON_TILE_SIZE_HEIGHT},
2115 },
2116 render_button_menu_tile_size,
2117 handle_menu_tile_size,
2118 window->graphics.id == GRAPHICS_NONE
2119 || !character_generated || !inkey_flag
2120 }
2121 };
2122
2123 load_next_menu_panel(window, menu_panel, button, N_ELEMENTS(elems), elems);
2124 }
2125
2126 static void handle_menu_tile_sets(struct window *window,
2127 struct button *button, const SDL_Event *event,
2128 struct menu_panel *menu_panel)
2129 {
2130 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_SUBWINDOW);
2131
2132 if (!select_menu_button(button, menu_panel, event)) {
2133 return;
2134 }
2135
2136 size_t num_elems = 0;
2137 /*
2138 * Only allow changes to the graphics mode when at a command prompt
2139 * in game. Could also allow while at the splash screen, but that
2140 * isn't possible to test for with character_generated and
2141 * character_dungeon. In other situations, the saved screens for
2142 * overlayed menus could have tile references that become outdated
2143 * when the graphics mode is changed.
2144 */
2145 bool disabled = !character_generated || !inkey_flag;
2146 struct menu_elem *elems;
2147
2148 graphics_mode *mode = graphics_modes;
2149 while (mode != NULL) {
2150 num_elems++;
2151 mode = mode->pNext;
2152 }
2153
2154 elems = mem_alloc(num_elems * sizeof(*elems));
2155
2156 mode = graphics_modes;
2157 for (size_t i = 0; i < num_elems; i++) {
2158 elems[i].caption = mode->menuname;
2159 elems[i].data.type = BUTTON_DATA_INT;
2160 elems[i].data.value.int_value = mode->grafID;
2161 elems[i].on_render = render_button_menu_tile_set;
2162 elems[i].on_menu = handle_menu_tile_set;
2163 elems[i].disabled = disabled;
2164 mode = mode->pNext;
2165 }
2166
2167 load_next_menu_panel(window, menu_panel, button, num_elems, elems);
2168
2169 mem_free(elems);
2170 }
2171
2172 static void handle_menu_tiles(struct window *window,
2173 struct button *button, const SDL_Event *event,
2174 struct menu_panel *menu_panel)
2175 {
2176 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_SUBWINDOW);
2177
2178 if (!select_menu_button(button, menu_panel, event)) {
2179 return;
2180 }
2181
2182 struct button_data data = {
2183 BUTTON_DATA_SUBWINDOW, {.subwindow_value = button->data.value.subwindow_value}
2184 };
2185
2186 struct menu_elem elems[] = {
2187 {
2188 "Set",
2189 data,
2190 render_button_menu_simple,
2191 handle_menu_tile_sets,
2192 false
2193 },
2194 {
2195 "Size",
2196 data,
2197 render_button_menu_simple,
2198 handle_menu_tile_sizes,
2199 false
2200 }
2201 };
2202
2203 load_next_menu_panel(window, menu_panel, button, N_ELEMENTS(elems), elems);
2204 }
2205
2206 static void handle_menu_pw(struct window *window,
2207 struct button *button, const SDL_Event *event,
2208 struct menu_panel *menu_panel)
2209 {
2210 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_TERM_FLAG);
2211
2212 if (!click_menu_button(button, menu_panel, event)) {
2213 return;
2214 }
2215
2216 uint32_t new_flags[N_ELEMENTS(window_flag)];
2217 assert(sizeof(new_flags) == sizeof(window_flag));
2218 memcpy(new_flags, window_flag, sizeof(new_flags));
2219
2220 struct subwindow *subwindow = button->data.value.term_flag_value.subwindow;
2221
2222 assert(subwindow->index != MAIN_SUBWINDOW);
2223 assert(subwindow->index < N_ELEMENTS(window_flag));
2224
2225 uint32_t flag = button->data.value.term_flag_value.flag;
2226
2227 new_flags[subwindow->index] = flag;
2228 subwindows_set_flags(new_flags, N_ELEMENTS(new_flags));
2229
2230 refresh_angband_terms();
2231 }
2232
2233 static void handle_menu_font_name(struct window *window,
2234 struct button *button, const SDL_Event *event,
2235 struct menu_panel *menu_panel)
2236 {
2237 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_FONT);
2238
2239 if (!click_menu_button(button, menu_panel, event)) {
2240 return;
2241 }
2242
2243 assert(button->data.value.font_value.index < N_ELEMENTS(g_font_info));
2244
2245 unsigned index = button->data.value.font_value.index;
2246 struct subwindow *subwindow = button->data.value.font_value.subwindow;
2247
2248 const struct font_info *font_info = &g_font_info[index];
2249 assert(font_info->loaded);
2250
2251 if (subwindow->font->index == index) {
2252 /* already loaded */
2253 return;
2254 }
2255
2256 if (reload_font(subwindow, font_info)) {
2257 button->data.value.font_value.size_ok = true;
2258 } else {
2259 button->data.value.font_value.size_ok = false;
2260 }
2261 }
2262
2263 static void handle_menu_font_size(struct window *window,
2264 struct button *button, const SDL_Event *event,
2265 struct menu_panel *menu_panel)
2266 {
2267 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_FONT);
2268
2269 if (!click_menu_button(button, menu_panel, event)) {
2270 return;
2271 }
2272 if (!button->data.value.font_value.size_ok) {
2273 return;
2274 }
2275
2276 unsigned index = button->data.value.font_value.index;
2277 assert(index < N_ELEMENTS(g_font_info));
2278
2279 struct font_info *info = &g_font_info[index];
2280
2281 if (info->type == FONT_TYPE_RASTER) {
2282 return;
2283 }
2284
2285 struct subwindow *subwindow = button->data.value.font_value.subwindow;
2286
2287 int size = subwindow->font->size;
2288
2289 int increment =
2290 (event->button.x - button->full_rect.x < button->full_rect.w / 2) ? -1 : +1;
2291
2292 for (size_t i = 0; i < MAX_VECTOR_FONT_SIZE - MIN_VECTOR_FONT_SIZE; i++) {
2293 size += increment;
2294 if (size > MAX_VECTOR_FONT_SIZE) {
2295 size = MIN_VECTOR_FONT_SIZE;
2296 } else if (size < MIN_VECTOR_FONT_SIZE) {
2297 size = MAX_VECTOR_FONT_SIZE;
2298 }
2299 info->size = size;
2300 if (reload_font(subwindow, info)) {
2301 return;
2302 }
2303 }
2304
2305 button->data.value.font_value.size_ok = false;
2306 }
2307
2308 static void handle_menu_font_sizes(struct window *window,
2309 struct button *button, const SDL_Event *event,
2310 struct menu_panel *menu_panel)
2311 {
2312 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_SUBWINDOW);
2313
2314 if (!select_menu_button(button, menu_panel, event)) {
2315 return;
2316 }
2317
2318 struct subwindow *subwindow = button->data.value.subwindow_value;
2319
2320 struct button_data data = {
2321 BUTTON_DATA_FONT,
2322 {.font_value =
2323 {.subwindow = subwindow, .index = subwindow->font->index, .size_ok = true}},
2324 };
2325 struct menu_elem elems[] = {
2326 {
2327 "< %2d points >",
2328 data,
2329 render_button_menu_font_size,
2330 handle_menu_font_size,
2331 false
2332 }
2333 };
2334
2335 load_next_menu_panel(window, menu_panel, button, N_ELEMENTS(elems), elems);
2336 }
2337
2338 static void handle_menu_font_names(struct window *window,
2339 struct button *button, const SDL_Event *event,
2340 struct menu_panel *menu_panel)
2341 {
2342 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_SUBWINDOW);
2343
2344 if (!select_menu_button(button, menu_panel, event)) {
2345 return;
2346 }
2347
2348 struct menu_elem elems[N_ELEMENTS(g_font_info)];
2349
2350 size_t num_elems = 0;
2351 for (size_t i = 0; i < N_ELEMENTS(g_font_info); i++) {
2352 if (g_font_info[i].loaded) {
2353 elems[num_elems].caption = g_font_info[i].name;
2354 elems[num_elems].data.type = BUTTON_DATA_FONT;
2355 elems[num_elems].data.value.font_value.subwindow = button->data.value.subwindow_value;
2356 elems[num_elems].data.value.font_value.size_ok = true;
2357 elems[num_elems].data.value.font_value.index = i;
2358 elems[num_elems].on_render = render_button_menu_font_name;
2359 elems[num_elems].on_menu = handle_menu_font_name;
2360 elems[num_elems].disabled = false;
2361 num_elems++;
2362 }
2363 }
2364
2365 load_next_menu_panel(window, menu_panel, button, num_elems, elems);
2366 }
2367
2368 static void handle_menu_purpose(struct window *window,
2369 struct button *button, const SDL_Event *event,
2370 struct menu_panel *menu_panel)
2371 {
2372 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_SUBWINDOW);
2373
2374 struct subwindow *subwindow = button->data.value.subwindow_value;
2375 assert(subwindow->index != MAIN_SUBWINDOW);
2376
2377 if (!select_menu_button(button, menu_panel, event)) {
2378 return;
2379 }
2380
2381 struct menu_elem elems[N_ELEMENTS(window_flag_desc)];
2382
2383 size_t num_elems = 0;
2384 while (num_elems < N_ELEMENTS(elems)
2385 && window_flag_desc[num_elems] != NULL)
2386 {
2387 elems[num_elems].caption = window_flag_desc[num_elems];
2388 elems[num_elems].data.value.term_flag_value.subwindow = subwindow;
2389 elems[num_elems].data.value.term_flag_value.flag =
2390 ((uint32_t) 1) << num_elems;
2391 elems[num_elems].data.type = BUTTON_DATA_TERM_FLAG;
2392 elems[num_elems].on_render = render_button_menu_pw;
2393 elems[num_elems].on_menu = handle_menu_pw;
2394 elems[num_elems].disabled = false;
2395 num_elems++;
2396 }
2397
2398 load_next_menu_panel(window, menu_panel, button, num_elems, elems);
2399 }
2400
2401 static void handle_menu_font(struct window *window,
2402 struct button *button, const SDL_Event *event,
2403 struct menu_panel *menu_panel)
2404 {
2405 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_SUBWINDOW);
2406
2407 if (!select_menu_button(button, menu_panel, event)) {
2408 return;
2409 }
2410
2411 struct button_data data = {
2412 BUTTON_DATA_SUBWINDOW, {.subwindow_value = button->data.value.subwindow_value}
2413 };
2414
2415 struct menu_elem elems[] = {
2416 {
2417 "Name",
2418 data,
2419 render_button_menu_simple,
2420 handle_menu_font_names,
2421 false
2422 },
2423 {
2424 "Size",
2425 data,
2426 render_button_menu_simple,
2427 handle_menu_font_sizes,
2428 false
2429 }
2430 };
2431
2432 load_next_menu_panel(window, menu_panel, button, N_ELEMENTS(elems), elems);
2433 }
2434
2435 static void handle_menu_borders(struct window *window,
2436 struct button *button, const SDL_Event *event,
2437 struct menu_panel *menu_panel)
2438 {
2439 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_SUBWINDOW);
2440
2441 if (!click_menu_button(button, menu_panel, event)) {
2442 return;
2443 }
2444
2445 struct subwindow *subwindow = button->data.value.subwindow_value;
2446
2447 subwindow->borders.visible = !subwindow->borders.visible;
2448 render_borders(subwindow);
2449 }
2450
2451 static void handle_menu_subwindow_alpha(struct window *window,
2452 struct button *button, const SDL_Event *event,
2453 struct menu_panel *menu_panel)
2454 {
2455 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_ALPHA);
2456
2457 if (!click_menu_button(button, menu_panel, event)) {
2458 return;
2459 }
2460
2461 struct subwindow *subwindow = button->data.value.alpha_value.subwindow;
2462 subwindow->color.a = button->data.value.alpha_value.real_value;
2463
2464 render_clear(subwindow->window, subwindow->texture, &subwindow->color);
2465 render_borders(subwindow);
2466
2467 refresh_angband_terms();
2468 }
2469
2470 static void handle_menu_alpha(struct window *window,
2471 struct button *button, const SDL_Event *event,
2472 struct menu_panel *menu_panel)
2473 {
2474 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_SUBWINDOW);
2475
2476 if (!select_menu_button(button, menu_panel, event)) {
2477 return;
2478 }
2479
2480 struct subwindow *subwindow = button->data.value.subwindow_value;
2481
2482 struct menu_elem elems[(100 - DEFAULT_ALPHA_LOWEST) / DEFAULT_ALPHA_STEP +
2483 1 + ((100 - DEFAULT_ALPHA_LOWEST) % DEFAULT_ALPHA_STEP == 0 ? 0 : 1)];
2484
2485 for (size_t i = 0; i < N_ELEMENTS(elems); i++) {
2486 int alpha = ALPHA_PERCENT(DEFAULT_ALPHA_LOWEST + i * DEFAULT_ALPHA_STEP);
2487
2488 elems[i].caption = " %3d%% ";
2489 elems[i].data.type = BUTTON_DATA_ALPHA;
2490 elems[i].data.value.alpha_value.subwindow = subwindow;
2491 elems[i].data.value.alpha_value.real_value = alpha;
2492 elems[i].data.value.alpha_value.show_value = i * DEFAULT_ALPHA_STEP;
2493 elems[i].on_render = render_button_menu_alpha;
2494 elems[i].on_menu = handle_menu_subwindow_alpha;
2495 elems[i].disabled = false;
2496 }
2497 elems[N_ELEMENTS(elems) - 1].data.value.alpha_value.real_value =
2498 DEFAULT_ALPHA_FULL;
2499
2500 load_next_menu_panel(window, menu_panel, button, N_ELEMENTS(elems), elems);
2501 }
2502
2503 static void handle_menu_top(struct window *window,
2504 struct button *button, const SDL_Event *event,
2505 struct menu_panel *menu_panel)
2506 {
2507 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_SUBWINDOW);
2508
2509 if (!click_menu_button(button, menu_panel, event)) {
2510 return;
2511 }
2512
2513 struct subwindow *subwindow = button->data.value.subwindow_value;
2514
2515 subwindow->always_top = !subwindow->always_top;
2516
2517 sort_to_top(subwindow->window);
2518 }
2519
2520 static void handle_menu_terms(struct window *window,
2521 struct button *button, const SDL_Event *event,
2522 struct menu_panel *menu_panel)
2523 {
2524 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_SUBWINDOW);
2525
2526 if (!select_menu_button(button, menu_panel, event)) {
2527 return;
2528 }
2529
2530 struct subwindow *subwindow = button->data.value.subwindow_value;
2531
2532 struct button_data data = {
2533 BUTTON_DATA_SUBWINDOW, {.subwindow_value = subwindow}
2534 };
2535
2536 struct menu_elem elems[] = {
2537 {
2538 "Font",
2539 data,
2540 render_button_menu_simple,
2541 handle_menu_font,
2542 false
2543 },
2544 {
2545 subwindow->index == MAIN_SUBWINDOW ? "Tiles" : NULL,
2546 data,
2547 render_button_menu_simple,
2548 handle_menu_tiles,
2549 false
2550 },
2551 {
2552 subwindow->index == MAIN_SUBWINDOW ? NULL: "Purpose",
2553 data,
2554 render_button_menu_simple,
2555 handle_menu_purpose,
2556 false
2557 },
2558 {
2559 subwindow->index == MAIN_SUBWINDOW ? NULL : "Alpha",
2560 data,
2561 render_button_menu_simple,
2562 handle_menu_alpha,
2563 false
2564 },
2565 {
2566 "Borders",
2567 data,
2568 render_button_menu_borders,
2569 handle_menu_borders,
2570 false
2571 },
2572 {
2573 "Top",
2574 data,
2575 render_button_menu_top,
2576 handle_menu_top,
2577 false
2578 }
2579 };
2580
2581 load_next_menu_panel(window, menu_panel, button, N_ELEMENTS(elems), elems);
2582 }
2583
2584 static void load_main_menu_panel(struct status_bar *status_bar)
2585 {
2586 assert(N_ELEMENTS(angband_term_name) == MAX_SUBWINDOWS);
2587
2588 struct menu_elem term_elems[N_ELEMENTS(angband_term_name)];
2589 size_t n_terms = 0;
2590
2591 for (size_t i = 0; i < N_ELEMENTS(angband_term_name); i++) {
2592 struct subwindow *subwindow =
2593 get_subwindow_by_index(status_bar->window, i, true);
2594 if (subwindow == NULL) {
2595 continue;
2596 }
2597
2598 term_elems[n_terms].caption = angband_term_name[i];
2599 term_elems[n_terms].data.type = BUTTON_DATA_SUBWINDOW;
2600 term_elems[n_terms].data.value.subwindow_value = subwindow;
2601 term_elems[n_terms].on_render = render_button_menu_terms;
2602 term_elems[n_terms].on_menu = handle_menu_terms;
2603 term_elems[n_terms].disabled = false;
2604 n_terms++;
2605 }
2606
2607 struct button_data data = {BUTTON_DATA_NONE, {0}};
2608 struct menu_elem other_elems[] = {
2609 {
2610 "Fullscreen",
2611 data,
2612 render_button_menu_fullscreen,
2613 handle_menu_fullscreen,
2614 false,
2615 },
2616 {
2617 status_bar->window->index == MAIN_WINDOW ?
2618 "Send Keypad Modifier" : NULL,
2619 data,
2620 render_button_menu_kp_mod,
2621 handle_menu_kp_mod,
2622 false
2623 },
2624 {
2625 status_bar->window->index == MAIN_WINDOW ?
2626 "Windows" : NULL,
2627 data,
2628 render_button_menu_simple,
2629 handle_menu_windows,
2630 false
2631 },
2632 {
2633 "About",
2634 data,
2635 render_button_menu_simple,
2636 handle_menu_about,
2637 false
2638 },
2639 {
2640 "Quit",
2641 data,
2642 render_button_menu_simple,
2643 handle_menu_quit,
2644 false
2645 }
2646 };
2647
2648 struct menu_elem elems[N_ELEMENTS(term_elems) + N_ELEMENTS(other_elems)];
2649
2650 memcpy(elems, term_elems, n_terms * sizeof(term_elems[0]));
2651 memcpy(elems + n_terms, other_elems, sizeof(other_elems));
2652
2653 struct button dummy = {0};
2654 dummy.full_rect.x = status_bar->full_rect.x;
2655 dummy.full_rect.y = status_bar->full_rect.y + status_bar->full_rect.h;
2656
2657 status_bar->menu_panel = make_menu_panel(&dummy, status_bar->font,
2658 n_terms + N_ELEMENTS(other_elems), elems);
2659 }
2660
2661 static void unselect_menu_buttons(struct menu_panel *menu_panel)
2662 {
2663 if (menu_panel == NULL) {
2664 return;
2665 }
2666
2667 for (size_t i = 0; i < menu_panel->button_bank.number; i++) {
2668 menu_panel->button_bank.buttons[i].selected = false;
2669 menu_panel->button_bank.buttons[i].highlighted = false;
2670 }
2671
2672 unselect_menu_buttons(menu_panel->next);
2673 }
2674
2675 static bool handle_menu_event(struct window *window, const SDL_Event *event)
2676 {
2677 int x = -1;
2678 int y = -1;
2679
2680 switch (event->type) {
2681 case SDL_MOUSEMOTION:
2682 x = event->motion.x;
2683 y = event->motion.y;
2684 break;
2685
2686 case SDL_MOUSEBUTTONUP: /* fallthru */
2687 case SDL_MOUSEBUTTONDOWN:
2688 x = event->button.x;
2689 y = event->button.y;
2690 break;
2691
2692 default:
2693 return false;
2694 }
2695
2696 assert(x >= 0);
2697 assert(y >= 0);
2698
2699 struct menu_panel *menu_panel =
2700 get_menu_panel_by_xy(window->status_bar.menu_panel, x, y);
2701
2702 if (menu_panel == NULL) {
2703 return false;
2704 }
2705
2706 bool handled = false;
2707
2708 for (size_t i = 0; i < menu_panel->button_bank.number; i++) {
2709 struct button *button = &menu_panel->button_bank.buttons[i];
2710 if (is_point_in_rect(x, y, &button->full_rect)) {
2711 /* note that the buttons themselves set "selected" */
2712 button->highlighted = true;
2713
2714 assert(button->callbacks.on_menu != NULL);
2715 if (!button->disabled) {
2716 button->callbacks.on_menu(window, button,
2717 event, menu_panel);
2718 handled = true;
2719 }
2720 } else {
2721 button->highlighted = false;
2722 /* but we do unset selected */
2723 button->selected = false;
2724 }
2725 }
2726
2727 unselect_menu_buttons(menu_panel->next);
2728
2729 return handled;
2730 }
2731
2732 static bool is_menu_button_mouse_click(const struct button *button,
2733 const SDL_Event *event)
2734 {
2735 if ((event->type == SDL_MOUSEBUTTONDOWN || event->type == SDL_MOUSEBUTTONUP)
2736 && is_point_in_rect(event->button.x, event->button.y, &button->full_rect))
2737 {
2738 return true;
2739 }
2740
2741 return false;
2742 }
2743
2744 static bool handle_menu_button(struct window *window,
2745 struct button *button, const SDL_Event *event)
2746 {
2747 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_NONE);
2748
2749 switch (event->type) {
2750 case SDL_MOUSEMOTION:
2751 if (is_point_in_rect(event->motion.x, event->motion.y, &button->full_rect)) {
2752 /* create menu on mouseover */
2753 if (window->status_bar.menu_panel == NULL) {
2754 load_main_menu_panel(&window->status_bar);
2755 }
2756 button->highlighted = true;
2757 return true;
2758 } else if (handle_menu_event(window, event)) {
2759 return true;
2760 } else if (button->highlighted) {
2761 /* the menu sticks around so that it is not horrible to use */
2762 return true;
2763 }
2764 return false;
2765 default:
2766 if (handle_menu_event(window, event)) {
2767 return true;
2768 } else if (is_menu_button_mouse_click(button, event)) {
2769 /* menu button just eats mouse clicks */
2770 return true;
2771 }
2772
2773 if (window->status_bar.menu_panel != NULL) {
2774 free_menu_panel(window->status_bar.menu_panel);
2775 window->status_bar.menu_panel = NULL;
2776 }
2777 button->highlighted = false;
2778 return false;
2779 }
2780 }
2781
2782 static bool is_close_to(int a, int b, unsigned range)
2783 {
2784 if (a > 0 && b > 0) {
2785 return (unsigned) ABS(a - b) < range;
2786 } else if (a < 0 && b < 0) {
2787 return (unsigned) ABS(ABS(a) - ABS(b)) < range;
2788 } else {
2789 return (unsigned) (ABS(a) + ABS(b)) < range;
2790 }
2791 }
2792
2793 static bool is_point_in_rect(int x, int y, const SDL_Rect *rect)
2794 {
2795 return x >= rect->x && x < rect->x + rect->w
2796 && y >= rect->y && y < rect->y + rect->h;
2797 }
2798
2799 static bool is_rect_in_rect(const SDL_Rect *small, const SDL_Rect *big)
2800 {
2801 return small->x >= big->x
2802 && small->x + small->w <= big->x + big->w
2803 && small->y >= big->y
2804 && small->y + small->h <= big->y + big->h;
2805 }
2806
2807 static void fit_rect_in_rect_by_hw(SDL_Rect *small, const SDL_Rect *big)
2808 {
2809 if (small->x < big->x) {
2810 small->w -= big->x - small->x;
2811 small->x = big->x;
2812 }
2813 if (small->x + small->w > big->x + big->w) {
2814 small->w = big->x + big->w - small->x;
2815 }
2816 if (small->y < big->y) {
2817 small->h -= big->y - small->y;
2818 small->y = big->y;
2819 }
2820 if (small->y + small->h > big->y + big->h) {
2821 small->h = big->y + big->h - small->y;
2822 }
2823 }
2824
2825 static void fit_rect_in_rect_by_xy(SDL_Rect *small, const SDL_Rect *big)
2826 {
2827 if (small->x < big->x) {
2828 small->x = big->x;
2829 }
2830 if (small->y < big->y) {
2831 small->y = big->y;
2832 }
2833 if (small->x + small->w > big->x + big->w) {
2834 small->x = MAX(big->x, big->x + big->w - small->w);
2835 }
2836 if (small->y + small->h > big->y + big->h) {
2837 small->y = MAX(big->y, big->y + big->h - small->h);
2838 }
2839 }
2840
2841 static void fit_rect_in_rect_proportional(SDL_Rect *small, const SDL_Rect *big)
2842 {
2843 if (small->x < big->x) {
2844 small->x = big->x;
2845 }
2846 if (small->y < big->y) {
2847 small->y = big->y;
2848 }
2849 if (small->w > big->w) {
2850 small->h = small->h * big->w / small->w;
2851 small->w = big->w;
2852 }
2853 if (small->h > big->h) {
2854 small->w = small->w * big->h / small->h;
2855 small->h = big->h;
2856 }
2857 }
2858
2859 static void resize_rect(SDL_Rect *rect,
2860 int left, int top, int right, int bottom)
2861 {
2862 if (rect->w - left + right <= 0
2863 || rect->h - top + bottom <= 0)
2864 {
2865 return;
2866 }
2867 rect->x += left;
2868 rect->w -= left;
2869
2870 rect->y += top;
2871 rect->h -= top;
2872
2873 rect->w += right;
2874 rect->h += bottom;
2875 }
2876
2877 static void crop_rects(SDL_Rect *src, SDL_Rect *dst)
2878 {
2879 if (src->w == dst->w) {
2880 /* most common case - do nothing */
2881 } else if (src->w > dst->w) {
2882 /* second most common case - font glyph is too large */
2883 src->x += (src->w - dst->w) / 2;
2884 src->w = dst->w;
2885 } else {
2886 /* uncommon case - font glyph is too small */
2887 dst->x += (dst->w - src->w) / 2;
2888 dst->w = src->w;
2889 }
2890
2891 if (src->h == dst->h) {
2892 ;
2893 } else if (src->h > dst->h) {
2894 src->y += (src->h - dst->h) / 2;
2895 src->h = dst->h;
2896 } else {
2897 dst->y += (dst->h - src->h) / 2;
2898 dst->h = src->h;
2899 }
2900 }
2901
2902 /* tries to snap to other term in such a way so that their
2903 * (visible) borders overlap */
2904 static void try_snap(struct window *window,
2905 struct subwindow *subwindow, SDL_Rect *rect)
2906 {
2907 for (size_t i = N_ELEMENTS(window->subwindows); i > 0; i--) {
2908 struct subwindow *other = window->subwindows[i - 1];
2909 if (other == NULL
2910 || !other->visible
2911 || other->index == subwindow->index)
2912 {
2913 continue;
2914 }
2915
2916 int ox = other->full_rect.x;
2917 int oy = other->full_rect.y;
2918 int ow = other->full_rect.w;
2919 int oh = other->full_rect.h;
2920
2921 if (oy < rect->y + rect->h && rect->y < oy + oh) {
2922 if (is_close_to(rect->x, ox + ow, DEFAULT_SNAP_RANGE)) {
2923 rect->x = ox + ow - DEFAULT_VISIBLE_BORDER;
2924 }
2925 if (is_close_to(rect->x + rect->w, ox, DEFAULT_SNAP_RANGE)) {
2926 rect->x = ox - rect->w + DEFAULT_VISIBLE_BORDER;
2927 }
2928 }
2929 if (ox < rect->x + rect->w && rect->x < ox + ow) {
2930 if (is_close_to(rect->y, oy + oh, DEFAULT_SNAP_RANGE)) {
2931 rect->y = oy + oh - DEFAULT_VISIBLE_BORDER;
2932 }
2933 if (is_close_to(rect->y + rect->h, oy, DEFAULT_SNAP_RANGE)) {
2934 rect->y = oy - rect->h + DEFAULT_VISIBLE_BORDER;
2935 }
2936 }
2937 }
2938 }
2939
2940 static void start_moving(struct window *window,
2941 struct subwindow *subwindow, const SDL_MouseButtonEvent *mouse)
2942 {
2943 assert(!window->size_state.active);
2944
2945 bring_to_top(window, subwindow);
2946
2947 window->move_state.originx = mouse->x;
2948 window->move_state.originy = mouse->y;
2949
2950 window->move_state.subwindow = subwindow;
2951 window->move_state.moving = true;
2952 }
2953
2954 static void start_sizing(struct window *window,
2955 struct subwindow *subwindow, const SDL_MouseButtonEvent *mouse)
2956 {
2957 assert(!window->move_state.active);
2958
2959 bring_to_top(window, subwindow);
2960
2961 subwindow->sizing_rect = subwindow->full_rect;
2962
2963 int x = mouse->x - (subwindow->full_rect.x + subwindow->full_rect.w / 2);
2964 int y = mouse->y - (subwindow->full_rect.y + subwindow->full_rect.h / 2);
2965
2966 window->size_state.left = x < 0 ? true : false;
2967 window->size_state.top = y < 0 ? true : false;
2968
2969 window->size_state.originx = mouse->x;
2970 window->size_state.originy = mouse->y;
2971
2972 window->size_state.subwindow = subwindow;
2973 window->size_state.sizing = true;
2974 }
2975
2976 static bool handle_menu_mousebuttondown(struct window *window,
2977 const SDL_MouseButtonEvent *mouse)
2978 {
2979 if (window->move_state.active || window->size_state.active) {
2980 struct subwindow *subwindow = get_subwindow_by_xy(window, mouse->x, mouse->y);
2981 if (subwindow != NULL
2982 && is_rect_in_rect(&subwindow->full_rect, &window->inner_rect))
2983 {
2984 if (window->move_state.active && !window->move_state.moving) {
2985 start_moving(window, subwindow, mouse);
2986 } else if (window->size_state.active && !window->size_state.sizing) {
2987 start_sizing(window, subwindow, mouse);
2988 }
2989 }
2990 return true;
2991 } else if (is_over_status_bar(&window->status_bar, mouse->x, mouse->y)) {
2992 return true;
2993 } else {
2994 return false;
2995 }
2996 }
2997
2998 static void handle_window_closed(const SDL_WindowEvent *event)
2999 {
3000 struct window *window = get_window_by_id(event->windowID);
3001 assert(window != NULL);
3002
3003 if (window->index == MAIN_WINDOW) {
3004 handle_quit();
3005 } else {
3006 for (size_t i = 0; i < N_ELEMENTS(window->subwindows); i++) {
3007 struct subwindow *subwindow = window->subwindows[i];
3008 if (subwindow != NULL) {
3009 clear_pw_flag(subwindow);
3010 }
3011 }
3012 free_window(window);
3013 }
3014 }
3015
3016 static void handle_window_focus(const SDL_WindowEvent *event)
3017 {
3018 assert(event->event == SDL_WINDOWEVENT_FOCUS_GAINED
3019 || event->event == SDL_WINDOWEVENT_FOCUS_LOST);
3020
3021 struct window *window = get_window_by_id(event->windowID);
3022 if (window == NULL) {
3023 /* when window is closed, it sends FOCUS_LOST event */
3024 assert(event->event == SDL_WINDOWEVENT_FOCUS_LOST);
3025 return;
3026 }
3027
3028 switch (event->event) {
3029 case SDL_WINDOWEVENT_FOCUS_GAINED:
3030 window->focus = true;
3031 break;
3032 case SDL_WINDOWEVENT_FOCUS_LOST:
3033 window->focus = false;
3034 break;
3035 }
3036 }
3037
3038 static void handle_last_resize_event(int num_events, const SDL_Event *events)
3039 {
3040 assert(num_events > 0);
3041
3042 for (int i = num_events - 1; i >= 0; i--) {
3043 if (events[i].window.event == SDL_WINDOWEVENT_RESIZED) {
3044 const SDL_WindowEvent event = events[i].window;
3045
3046 struct window *window = get_window_by_id(event.windowID);
3047 assert(window != NULL);
3048 resize_window(window, event.data1, event.data2);
3049
3050 return;
3051 }
3052 }
3053 }
3054
3055 static void handle_windowevent(const SDL_WindowEvent *event)
3056 {
3057 SDL_Event events[128];
3058 events[0].window = *event;
3059
3060 int num_events = 1 + SDL_PeepEvents(events + 1, (int) N_ELEMENTS(events) - 1,
3061 SDL_GETEVENT, SDL_WINDOWEVENT, SDL_WINDOWEVENT);
3062
3063 bool resize = false;
3064
3065 for (int i = 0; i < num_events; i++) {
3066 switch (events[i].window.event) {
3067 case SDL_WINDOWEVENT_RESIZED:
3068 /* just for efficiency */
3069 resize = true;
3070 break;
3071 case SDL_WINDOWEVENT_CLOSE:
3072 handle_window_closed(&events[i].window);
3073 break;
3074 case SDL_WINDOWEVENT_FOCUS_GAINED: /* fallthru */
3075 case SDL_WINDOWEVENT_FOCUS_LOST:
3076 handle_window_focus(&events[i].window);
3077 break;
3078 }
3079 }
3080
3081 if (resize) {
3082 handle_last_resize_event(num_events, events);
3083 }
3084
3085 redraw_all_windows(false);
3086 }
3087
3088 static void resize_subwindow(struct subwindow *subwindow)
3089 {
3090 SDL_DestroyTexture(subwindow->texture);
3091
3092 subwindow->full_rect = subwindow->sizing_rect;
3093 if (!adjust_subwindow_geometry(subwindow->window, subwindow)) {
3094 quit_fmt("bad_geometry of subwindow %u in window %u",
3095 subwindow->index, subwindow->window->index);
3096 }
3097
3098 subwindow->texture = make_subwindow_texture(subwindow->window,
3099 subwindow->full_rect.w, subwindow->full_rect.h);
3100
3101 render_clear(subwindow->window, subwindow->texture, &subwindow->color);
3102 render_borders(subwindow);
3103
3104 term *old = Term;
3105 Term_activate(subwindow->term);
3106 Term_resize(subwindow->cols, subwindow->rows);
3107 /* XXX if we don't redraw the term, resizing in birth screen is buggy */
3108 Term_redraw();
3109 Term_activate(old);
3110
3111 refresh_angband_terms();
3112 }
3113
3114 static void do_sizing(struct window *window, int x, int y)
3115 {
3116 struct size_state *size_state = &window->size_state;
3117
3118 assert(size_state->subwindow != NULL);
3119
3120 SDL_Rect rect = size_state->subwindow->sizing_rect;
3121
3122 int newx = x - size_state->originx;
3123 int newy = y - size_state->originy;
3124
3125 int left = size_state->left ? newx : 0;
3126 int top = size_state->top ? newy : 0;
3127 int right = size_state->left ? 0 : newx;
3128 int bottom = size_state->top ? 0 : newy;
3129
3130 resize_rect(&rect, left, top, right, bottom);
3131 fit_rect_in_rect_by_hw(&rect, &window->inner_rect);
3132
3133 if (is_ok_col_row(size_state->subwindow,
3134 &rect,
3135 size_state->subwindow->font_width,
3136 size_state->subwindow->font_height))
3137 {
3138 size_state->subwindow->sizing_rect = rect;
3139 }
3140
3141 size_state->originx = x;
3142 size_state->originy = y;
3143 }
3144
3145 static void do_moving(struct window *window, int x, int y)
3146 {
3147 struct move_state *move_state = &window->move_state;
3148
3149 assert(move_state->subwindow != NULL);
3150
3151 SDL_Rect *rect = &move_state->subwindow->full_rect;
3152
3153 rect->x += x - move_state->originx;
3154 rect->y += y - move_state->originy;
3155
3156 try_snap(window, move_state->subwindow, rect);
3157 fit_rect_in_rect_by_xy(rect, &window->inner_rect);
3158
3159 move_state->originx = x;
3160 move_state->originy = y;
3161 }
3162
3163 static bool handle_menu_mousebuttonup(struct window *window,
3164 const SDL_MouseButtonEvent *mouse)
3165 {
3166 if (window->move_state.active && window->move_state.moving) {
3167 window->move_state.moving = false;
3168 } else if (window->size_state.active && window->size_state.sizing) {
3169 window->size_state.sizing = false;
3170
3171 if (window->size_state.subwindow != NULL) {
3172 resize_subwindow(window->size_state.subwindow);
3173 }
3174 }
3175
3176 if (window->move_state.active
3177 || window->size_state.active
3178 || is_over_status_bar(&window->status_bar, mouse->x, mouse->y))
3179 {
3180 return true;
3181 } else {
3182 return false;
3183 }
3184 }
3185
3186 static bool handle_menu_mousemotion(struct window *window,
3187 const SDL_MouseMotionEvent *mouse)
3188 {
3189 if (window->move_state.moving) {
3190 do_moving(window, mouse->x, mouse->y);
3191 return true;
3192 } else if (window->size_state.sizing) {
3193 do_sizing(window, mouse->x, mouse->y);
3194 return true;
3195 } else if (window->move_state.active || window->size_state.active) {
3196 return true;
3197 } else if (is_over_status_bar(&window->status_bar, mouse->x, mouse->y)) {
3198 return true;
3199 }
3200
3201 return false;
3202 }
3203
3204 static bool handle_menu_keyboard(struct window *window, const SDL_Event *event)
3205 {
3206 if (window->move_state.active || window->size_state.active) {
3207 return true;
3208 }
3209
3210 SDL_Event key = *event;
3211 /* the user pressed a key; probably wants to play? */
3212 SDL_PushEvent(&key);
3213
3214 return false;
3215 }
3216
3217 static bool handle_status_bar_buttons(struct window *window,
3218 const SDL_Event *event)
3219 {
3220 bool handled = false;
3221
3222 for (size_t i = 0; i < window->status_bar.button_bank.number; i++) {
3223 struct button *button = &window->status_bar.button_bank.buttons[i];
3224 assert(button->callbacks.on_event != NULL);
3225 if (!button->disabled) {
3226 handled |= button->callbacks.on_event(window, button,
3227 event);
3228 }
3229 }
3230
3231 return handled;
3232 }
3233
3234 static void redraw_status_bar_buttons(struct window *window)
3235 {
3236 SDL_Event shutdown = {.type = SDL_USEREVENT};
3237 (void) handle_status_bar_buttons(window, &shutdown);
3238 }
3239
3240 static bool handle_menu_windowevent(struct window *window,
3241 const SDL_WindowEvent *event)
3242 {
3243 if (window->move_state.active) {
3244 signal_move_state(window);
3245 } else if (window->size_state.active) {
3246 signal_size_state(window);
3247 }
3248
3249 redraw_status_bar_buttons(window);
3250
3251 handle_windowevent(event);
3252
3253 return false;
3254 }
3255
3256 static bool is_event_windowid_ok(const struct window *window, const SDL_Event *event)
3257 {
3258 switch (event->type) {
3259 case SDL_KEYDOWN: /* fallthru */
3260 case SDL_KEYUP:
3261 return event->key.windowID == window->id;
3262 case SDL_TEXTINPUT:
3263 return event->text.windowID == window->id;
3264 case SDL_MOUSEMOTION:
3265 return event->motion.windowID == window->id;
3266 case SDL_MOUSEBUTTONDOWN: /* fallthru */
3267 case SDL_MOUSEBUTTONUP:
3268 return event->button.windowID == window->id;
3269 default:
3270 return true;
3271 }
3272 }
3273
3274 /* returns true for events that should be processed by buttons */
3275 static bool is_ok_button_event(const struct window *window, const SDL_Event *event)
3276 {
3277 switch (event->type) {
3278 case SDL_KEYDOWN: /* fallthru */
3279 case SDL_KEYUP: /* fallthru */
3280 case SDL_TEXTINPUT: /* fallthru */
3281 return is_event_windowid_ok(window, event);
3282 case SDL_MOUSEMOTION: /* fallthru */
3283 case SDL_MOUSEBUTTONDOWN: /* fallthru */
3284 case SDL_MOUSEBUTTONUP:
3285 return window->focus && is_event_windowid_ok(window, event);
3286 case SDL_USEREVENT:
3287 return true;
3288 default:
3289 return false;
3290 }
3291 }
3292
3293 static bool handle_status_bar_events(struct window *window,
3294 const SDL_Event *event)
3295 {
3296 if (!is_event_windowid_ok(window, event)) {
3297 /* just in case */
3298 if (window->move_state.active) {
3299 signal_move_state(window);
3300 } else if (window->size_state.active) {
3301 signal_size_state(window);
3302 }
3303 return false;
3304 }
3305
3306 switch (event->type) {
3307 case SDL_MOUSEMOTION:
3308 return handle_menu_mousemotion(window, &event->motion);
3309 case SDL_MOUSEBUTTONDOWN:
3310 return handle_menu_mousebuttondown(window, &event->button);
3311 case SDL_MOUSEBUTTONUP:
3312 return handle_menu_mousebuttonup(window, &event->button);
3313 case SDL_KEYDOWN: /* fallthru */
3314 case SDL_KEYUP: /* fallthru */
3315 case SDL_TEXTEDITING: /* fallthru */
3316 case SDL_TEXTINPUT:
3317 return handle_menu_keyboard(window, event);
3318 case SDL_WINDOWEVENT:
3319 return handle_menu_windowevent(window, &event->window);
3320 case SDL_QUIT:
3321 handle_quit();
3322 return false;
3323 default:
3324 return false;
3325 }
3326 }
3327
3328 static void do_status_bar_loop(struct window *window)
3329 {
3330 window->status_bar.in_menu = true;
3331
3332 bool keep_going = true;
3333 while (keep_going) {
3334 SDL_Delay(window->delay);
3335
3336 SDL_Event event;
3337 SDL_WaitEvent(&event);
3338
3339 bool handled = false;
3340 if (is_ok_button_event(window, &event)
3341 && !window->move_state.moving
3342 && !window->size_state.sizing)
3343 {
3344 handled = handle_status_bar_buttons(window, &event);
3345 }
3346
3347 if (event.type == SDL_MOUSEMOTION) {
3348 /* annoying mousemotion spam! */
3349 SDL_FlushEvent(SDL_MOUSEMOTION);
3350 }
3351
3352 if (!handled) {
3353 /* so the user didnt click on a button */
3354 keep_going = handle_status_bar_events(window, &event);
3355 }
3356
3357 redraw_window(window);
3358 }
3359
3360 window->status_bar.in_menu = false;
3361 }
3362
3363 static bool has_visible_subwindow(const struct window *window, unsigned index)
3364 {
3365 return get_subwindow_by_index(window, index, true) != NULL;
3366 }
3367
3368 static bool handle_mousemotion(const SDL_MouseMotionEvent *mouse)
3369 {
3370 struct window *window = get_window_by_id(mouse->windowID);
3371
3372 if (is_over_status_bar(&window->status_bar, mouse->x, mouse->y)) {
3373 do_status_bar_loop(window);
3374 }
3375 /* dont need other mousemotion events */
3376 SDL_FlushEvent(SDL_MOUSEMOTION);
3377
3378 return false;
3379 }
3380
3381 /* x and y are relative to window */
3382 static bool get_colrow_from_xy(const struct subwindow *subwindow,
3383 int x, int y, int *col, int *row)
3384 {
3385 SDL_Rect rect = {
3386 subwindow->full_rect.x + subwindow->inner_rect.x,
3387 subwindow->full_rect.y + subwindow->inner_rect.y,
3388 subwindow->inner_rect.w,
3389 subwindow->inner_rect.h
3390 };
3391
3392 if (!is_point_in_rect(x, y, &rect)) {
3393 return false;
3394 }
3395
3396 *col = (x - rect.x) / subwindow->font_width;
3397 *row = (y - rect.y) / subwindow->font_height;
3398
3399 return true;
3400 }
3401
3402 static uint8_t translate_key_mods(Uint16 mods)
3403 {
3404 #define TRANSLATE_K_MOD(m, k) ((m) & mods ? (k) : 0)
3405 uint8_t angband_mods =
3406 TRANSLATE_K_MOD(KMOD_SHIFT, KC_MOD_SHIFT)
3407 | TRANSLATE_K_MOD(KMOD_CTRL, KC_MOD_CONTROL)
3408 | TRANSLATE_K_MOD(KMOD_ALT, KC_MOD_ALT)
3409 | TRANSLATE_K_MOD(KMOD_GUI, KC_MOD_META);
3410 #undef TRANSLATE_K_MOD
3411 return angband_mods;
3412 }
3413
3414 static bool handle_mousebuttondown(const SDL_MouseButtonEvent *mouse)
3415 {
3416 struct window *window = get_window_by_id(mouse->windowID);
3417 assert(window != NULL);
3418
3419 struct subwindow *subwindow = get_subwindow_by_xy(window, mouse->x, mouse->y);
3420 if (subwindow == NULL) {
3421 /* not an error, the user clicked in some random place */
3422 return false;
3423 } else if (!subwindow->top) {
3424 bring_to_top(window, subwindow);
3425 redraw_window(window);
3426 return false;
3427 }
3428
3429 /* terms that are not main do not react to events, and main term
3430 * lives in main window */
3431 if (window->index != MAIN_WINDOW) {
3432 return false;
3433 }
3434
3435 /* all magic numbers are from ui-term.c and ui-context.c :) */
3436
3437 int button;
3438 switch (mouse->button) {
3439 case SDL_BUTTON_LEFT:
3440 button = 1;
3441 break;
3442 case SDL_BUTTON_RIGHT:
3443 button = 2;
3444 break;
3445 default:
3446 /* XXX other buttons? */
3447 return false;
3448 }
3449
3450 int col;
3451 int row;
3452 if (!get_colrow_from_xy(subwindow, mouse->x, mouse->y, &col, &row)) {
3453 return false;
3454 }
3455
3456 uint8_t mods = translate_key_mods(SDL_GetModState());
3457 /* apparently mouse buttons dont get this */
3458 mods &= ~KC_MOD_META;
3459
3460 button |= mods << 4;
3461
3462 term *old = Term;
3463 Term_activate(subwindow->term);
3464 Term_mousepress(col, row, button);
3465 Term_activate(old);
3466
3467 return true;
3468 }
3469
3470 static bool handle_keydown(const SDL_KeyboardEvent *key)
3471 {
3472 uint8_t mods = translate_key_mods(key->keysym.mod);
3473 keycode_t ch = 0;
3474
3475 /* SDL will give us both keydown and text input events in many cases.
3476 * Between this function and handle_text_input we need to make sure that
3477 * Term_keypress gets called exactly once for a given key press from the
3478 * user.
3479 * This function handles keys that don't produce text, and, if
3480 * g_kp_as_mod is true, the keypad and keypresses that will produce the
3481 * same characters as keypad keypresses.
3482 * Others should be handled in handle_text_input.
3483 */
3484
3485 switch (key->keysym.sym) {
3486 /* arrow keys */
3487 case SDLK_UP: ch = ARROW_UP; break;
3488 case SDLK_DOWN: ch = ARROW_DOWN; break;
3489 case SDLK_LEFT: ch = ARROW_LEFT; break;
3490 case SDLK_RIGHT: ch = ARROW_RIGHT; break;
3491 /* text editing keys */
3492 case SDLK_BACKSPACE: ch = KC_BACKSPACE; break;
3493 case SDLK_PAGEDOWN: ch = KC_PGDOWN; break;
3494 case SDLK_PAGEUP: ch = KC_PGUP; break;
3495 case SDLK_INSERT: ch = KC_INSERT; break;
3496 case SDLK_DELETE: ch = KC_DELETE; break;
3497 case SDLK_RETURN: ch = KC_ENTER; break;
3498 case SDLK_ESCAPE: ch = ESCAPE; break;
3499 case SDLK_HOME: ch = KC_HOME; break;
3500 case SDLK_END: ch = KC_END; break;
3501 case SDLK_TAB: ch = KC_TAB; break;
3502 /* function keys */
3503 case SDLK_F1: ch = KC_F1; break;
3504 case SDLK_F2: ch = KC_F2; break;
3505 case SDLK_F3: ch = KC_F3; break;
3506 case SDLK_F4: ch = KC_F4; break;
3507 case SDLK_F5: ch = KC_F5; break;
3508 case SDLK_F6: ch = KC_F6; break;
3509 case SDLK_F7: ch = KC_F7; break;
3510 case SDLK_F8: ch = KC_F8; break;
3511 case SDLK_F9: ch = KC_F9; break;
3512 case SDLK_F10: ch = KC_F10; break;
3513 case SDLK_F11: ch = KC_F11; break;
3514 case SDLK_F12: ch = KC_F12; break;
3515 case SDLK_F13: ch = KC_F13; break;
3516 case SDLK_F14: ch = KC_F14; break;
3517 case SDLK_F15: ch = KC_F15; break;
3518 }
3519
3520 if (g_kp_as_mod) {
3521 /* If numlock is set and shift is not pressed, numpad numbers
3522 * produce regular numbers and not keypad numbers */
3523 uint8_t keypad_num_mod = ((key->keysym.mod & KMOD_NUM)
3524 && !(key->keysym.mod & KMOD_SHIFT)) ?
3525 0x00 : KC_MOD_KEYPAD;
3526
3527 switch (key->keysym.sym) {
3528 /* Keypad */
3529 case SDLK_KP_0: ch = '0'; mods |= keypad_num_mod; break;
3530 case SDLK_KP_1: ch = '1'; mods |= keypad_num_mod; break;
3531 case SDLK_KP_2: ch = '2'; mods |= keypad_num_mod; break;
3532 case SDLK_KP_3: ch = '3'; mods |= keypad_num_mod; break;
3533 case SDLK_KP_4: ch = '4'; mods |= keypad_num_mod; break;
3534 case SDLK_KP_5: ch = '5'; mods |= keypad_num_mod; break;
3535 case SDLK_KP_6: ch = '6'; mods |= keypad_num_mod; break;
3536 case SDLK_KP_7: ch = '7'; mods |= keypad_num_mod; break;
3537 case SDLK_KP_8: ch = '8'; mods |= keypad_num_mod; break;
3538 case SDLK_KP_9: ch = '9'; mods |= keypad_num_mod; break;
3539
3540 case SDLK_KP_MULTIPLY:
3541 ch = '*'; mods |= KC_MOD_KEYPAD; break;
3542 case SDLK_KP_PERIOD:
3543 ch = '.'; mods |= KC_MOD_KEYPAD; break;
3544 case SDLK_KP_DIVIDE:
3545 ch = '/'; mods |= KC_MOD_KEYPAD; break;
3546 case SDLK_KP_EQUALS:
3547 ch = '='; mods |= KC_MOD_KEYPAD; break;
3548 case SDLK_KP_MINUS:
3549 ch = '-'; mods |= KC_MOD_KEYPAD; break;
3550 case SDLK_KP_PLUS:
3551 ch = '+'; mods |= KC_MOD_KEYPAD; break;
3552 case SDLK_KP_ENTER:
3553 ch = KC_ENTER; mods |= KC_MOD_KEYPAD; break;
3554
3555 /* Keys that produce the same character as keypad keys */
3556 case SDLK_ASTERISK: ch = '*'; break;
3557 case SDLK_PLUS: ch = '+'; break;
3558 }
3559 if((mods & KC_MOD_SHIFT)) {
3560 bool match = true;
3561 switch(key->keysym.sym) {
3562 /* Doesn't match every keyboard layout, unfortunately. */
3563 case SDLK_8: ch = '*'; break;
3564 case SDLK_EQUALS: ch = '+'; break;
3565 default: match = false;
3566 }
3567 if(match) {
3568 mods &= ~KC_MOD_SHIFT;
3569 }
3570 } else {
3571 switch(key->keysym.sym) {
3572 case SDLK_0: ch = '0'; break;
3573 case SDLK_1: ch = '1'; break;
3574 case SDLK_2: ch = '2'; break;
3575 case SDLK_3: ch = '3'; break;
3576 case SDLK_4: ch = '4'; break;
3577 case SDLK_5: ch = '5'; break;
3578 case SDLK_6: ch = '6'; break;
3579 case SDLK_7: ch = '7'; break;
3580 case SDLK_8: ch = '8'; break;
3581 case SDLK_9: ch = '9'; break;
3582 case SDLK_SLASH: ch = '/'; break;
3583 case SDLK_EQUALS: ch = '='; break;
3584 case SDLK_PERIOD: ch = '.'; break;
3585 case SDLK_MINUS: ch = '-'; break;
3586 }
3587 }
3588 } else if (key->keysym.sym == SDLK_KP_ENTER) {
3589 ch = KC_ENTER;
3590 }
3591
3592 /* encode control */
3593 if (mods & KC_MOD_CONTROL) {
3594 bool match = true;
3595 switch (key->keysym.sym) {
3596 case SDLK_LEFTBRACKET: ch = KTRL('['); break;
3597 case SDLK_RIGHTBRACKET: ch = KTRL(']'); break;
3598 case SDLK_BACKSLASH: ch = KTRL('\\'); break;
3599 case SDLK_a: ch = KTRL('A'); break;
3600 case SDLK_b: ch = KTRL('B'); break;
3601 case SDLK_c: ch = KTRL('C'); break;
3602 case SDLK_d: ch = KTRL('D'); break;
3603 case SDLK_e: ch = KTRL('E'); break;
3604 case SDLK_f: ch = KTRL('F'); break;
3605 case SDLK_g: ch = KTRL('G'); break;
3606 case SDLK_h: ch = KTRL('H'); break;
3607 case SDLK_i: ch = KTRL('I'); break;
3608 case SDLK_j: ch = KTRL('J'); break;
3609 case SDLK_k: ch = KTRL('K'); break;
3610 case SDLK_l: ch = KTRL('L'); break;
3611 case SDLK_m: ch = KTRL('M'); break;
3612 case SDLK_n: ch = KTRL('N'); break;
3613 case SDLK_o: ch = KTRL('O'); break;
3614 case SDLK_p: ch = KTRL('P'); break;
3615 case SDLK_q: ch = KTRL('Q'); break;
3616 case SDLK_r: ch = KTRL('R'); break;
3617 case SDLK_s: ch = KTRL('S'); break;
3618 case SDLK_t: ch = KTRL('T'); break;
3619 case SDLK_u: ch = KTRL('U'); break;
3620 case SDLK_v: ch = KTRL('V'); break;
3621 case SDLK_w: ch = KTRL('W'); break;
3622 case SDLK_x: ch = KTRL('X'); break;
3623 case SDLK_y: ch = KTRL('Y'); break;
3624 case SDLK_z: ch = KTRL('Z'); break;
3625 default: match = false;
3626 }
3627 if(match) {
3628 mods &= ~KC_MOD_CONTROL;
3629 }
3630 }
3631
3632
3633 if (ch) {
3634 Term_keypress(ch, mods);
3635 return true;
3636 } else {
3637 return false;
3638 }
3639 }
3640
3641 static keycode_t utf8_to_codepoint(const char *utf8_string)
3642 {
3643 /* hex == binary
3644 * 0x00 == 00000000
3645 * 0x80 == 10000000
3646 * 0xc0 == 11000000
3647 * 0xe0 == 11100000
3648 * 0xf0 == 11110000
3649 * 0xf8 == 11111000
3650 * 0x3f == 00111111
3651 * 0x1f == 00011111
3652 * 0x0f == 00001111
3653 * 0x07 == 00000111 */
3654
3655 keycode_t key = 0;
3656
3657 #define IS_UTF8_INFO(mask, result) (((unsigned char) utf8_string[0] & (mask)) == (result))
3658 #define EXTRACT_UTF8_INFO(pos, mask, shift) (((unsigned char) utf8_string[(pos)] & (mask)) << (shift))
3659 /* 6 is the number of information bits in a utf8 continuation byte (10xxxxxx) */
3660 if (IS_UTF8_INFO(0x80, 0)) {
3661 key = utf8_string[0];
3662 } else if (IS_UTF8_INFO(0xe0, 0xc0)) {
3663 key = EXTRACT_UTF8_INFO(0, 0x1f, 6)
3664 | EXTRACT_UTF8_INFO(1, 0x3f, 0);
3665 } else if (IS_UTF8_INFO(0xf0, 0xe0)) {
3666 key = EXTRACT_UTF8_INFO(0, 0x0f, 12)
3667 | EXTRACT_UTF8_INFO(1, 0x3f, 6)
3668 | EXTRACT_UTF8_INFO(2, 0x3f, 0);
3669 } else if (IS_UTF8_INFO(0xf8, 0xf0)) {
3670 key = EXTRACT_UTF8_INFO(0, 0x07, 18)
3671 | EXTRACT_UTF8_INFO(1, 0x3f, 12)
3672 | EXTRACT_UTF8_INFO(2, 0x3f, 6)
3673 | EXTRACT_UTF8_INFO(3, 0x3f, 0);
3674 }
3675 #undef IS_UTF8_INFO
3676 #undef EXTRACT_UTF8_INFO
3677
3678 return key;
3679 }
3680
3681 static bool handle_text_input(const SDL_TextInputEvent *input)
3682 {
3683 keycode_t ch = utf8_to_codepoint(input->text);
3684
3685 /* Don't handle any characters that can be produced by the keypad if
3686 * they were handled in handle_keydown */
3687 if (g_kp_as_mod) {
3688 switch (ch) {
3689 case '0':
3690 case '1':
3691 case '2':
3692 case '3':
3693 case '4':
3694 case '5':
3695 case '6':
3696 case '7':
3697 case '8':
3698 case '9':
3699 case '/':
3700 case '*':
3701 case '-':
3702 case '+':
3703 case '.':
3704 case '=':
3705 return false;
3706 }
3707 }
3708
3709 uint8_t mods = translate_key_mods(SDL_GetModState());
3710
3711 /* Shift is already encoded in characters we receive here */
3712 if (!MODS_INCLUDE_SHIFT(ch)) {
3713 mods &= ~KC_MOD_SHIFT;
3714 }
3715
3716 Term_keypress(ch, mods);
3717
3718 return true;
3719 }
3720
3721 static void wait_anykey(void)
3722 {
3723 SDL_Event event;
3724
3725 SDL_EventType expected = SDL_USEREVENT;
3726 while (true) {
3727 SDL_WaitEvent(&event);
3728 if (event.type == expected) {
3729 return;
3730 }
3731
3732 switch (event.type) {
3733 case SDL_KEYDOWN:
3734 expected = SDL_KEYUP;
3735 break;;
3736 case SDL_MOUSEBUTTONDOWN:
3737 expected = SDL_MOUSEBUTTONUP;
3738 break;
3739 case SDL_MOUSEMOTION:
3740 SDL_FlushEvent(SDL_MOUSEMOTION);
3741 break;
3742 case SDL_QUIT:
3743 handle_quit();
3744 break;
3745 case SDL_WINDOWEVENT:
3746 handle_windowevent(&event.window);
3747 return;
3748 }
3749 }
3750 }
3751
3752 static void handle_quit(void)
3753 {
3754 /* XXX copied from main-sdl.c */
3755 if (character_generated && inkey_flag) {
3756 /* no idea what that does :) */
3757 msg_flag = false;
3758 save_game();
3759 }
3760
3761 quit(NULL);
3762 }
3763
3764 static bool get_event(void)
3765 {
3766 SDL_Event event;
3767 if (!SDL_PollEvent(&event)) {
3768 return false;
3769 }
3770
3771 switch (event.type) {
3772 case SDL_KEYDOWN:
3773 return handle_keydown(&event.key);
3774 case SDL_TEXTINPUT:
3775 return handle_text_input(&event.text);
3776 case SDL_MOUSEMOTION:
3777 return handle_mousemotion(&event.motion);
3778 case SDL_MOUSEBUTTONDOWN:
3779 return handle_mousebuttondown(&event.button);
3780 case SDL_WINDOWEVENT:
3781 handle_windowevent(&event.window);
3782 return false;
3783 case SDL_QUIT:
3784 handle_quit();
3785 return false;
3786 default:
3787 return false;
3788 }
3789 }
3790
3791 static void refresh_angband_terms(void)
3792 {
3793 if (!character_dungeon) {
3794 return;
3795 }
3796
3797 term *old = Term;
3798 Term_activate(term_screen);
3799
3800 /* XXX XXX this is basically do_cmd_redraw(), just without EVENT_FLUSH_INPUT */
3801 {
3802 /* XXX XXX this works for refreshing monster's attrs */
3803 event_signal_point(EVENT_MAP, -1, -1);
3804
3805 Term_flush();
3806 verify_panel();
3807
3808 player->upkeep->notice |= (PN_COMBINE);
3809 player->upkeep->update |= (PU_TORCH | PU_INVEN);
3810 player->upkeep->update |= (PU_BONUS | PU_HP | PU_SPELLS);
3811 player->upkeep->update |= (PU_UPDATE_VIEW | PU_MONSTERS);
3812 player->upkeep->redraw |= (PR_BASIC | PR_EXTRA | PR_MAP | PR_INVEN |
3813 PR_EQUIP | PR_MESSAGE | PR_MONSTER |
3814 PR_OBJECT | PR_MONLIST | PR_ITEMLIST);
3815
3816 Term_clear();
3817 handle_stuff(player);
3818 move_cursor_relative(player->grid.x, player->grid.y);
3819
3820 Term_redraw_all();
3821 }
3822
3823 Term_activate(old);
3824
3825 redraw_all_windows(false);
3826 }
3827
3828 static errr term_xtra_event(int v)
3829 {
3830 struct subwindow *subwindow = Term->data;
3831 assert(subwindow != NULL);
3832
3833 redraw_all_windows(true);
3834
3835 if (v) {
3836 while (true) {
3837 for (int i = 0; i < DEFAULT_IDLE_UPDATE_PERIOD; i++) {
3838 if (get_event()) {
3839 return 0;
3840 }
3841 SDL_Delay(subwindow->window->delay);
3842 }
3843 idle_update();
3844 }
3845 } else {
3846 (void) get_event();
3847 }
3848
3849 return 0;
3850 }
3851
3852 static errr term_xtra_flush(void)
3853 {
3854 SDL_Event event;
3855
3856 while (SDL_PollEvent(&event)) {
3857 switch (event.type) {
3858 case SDL_WINDOWEVENT:
3859 handle_windowevent(&event.window);
3860 break;
3861 }
3862 }
3863
3864 return 0;
3865 }
3866
3867 static errr term_xtra_clear(void)
3868 {
3869 struct subwindow *subwindow = Term->data;
3870 assert(subwindow != NULL);
3871
3872 render_fill_rect(subwindow->window,
3873 subwindow->texture, &subwindow->inner_rect, &subwindow->color);
3874
3875 subwindow->window->dirty = true;
3876
3877 return 0;
3878 }
3879
3880 static errr term_xtra_fresh(void)
3881 {
3882 struct subwindow *subwindow = Term->data;
3883 assert(subwindow != NULL);
3884
3885 if (!subwindow->window->status_bar.in_menu) {
3886 try_redraw_window(subwindow->window);
3887 }
3888
3889 return 0;
3890 }
3891
3892 static errr term_xtra_delay(int v)
3893 {
3894 if (v > 0) {
3895 term_xtra_event(0);
3896 SDL_Delay(v);
3897 }
3898
3899 return 0;
3900 }
3901
3902 static errr term_xtra_react(void)
3903 {
3904 init_colors();
3905
3906 return 0;
3907 }
3908
3909 static errr term_xtra_hook(int n, int v)
3910 {
3911 switch (n) {
3912 case TERM_XTRA_EVENT:
3913 return term_xtra_event(v);
3914 case TERM_XTRA_DELAY:
3915 return term_xtra_delay(v);
3916 case TERM_XTRA_FLUSH:
3917 return term_xtra_flush();
3918 case TERM_XTRA_CLEAR:
3919 return term_xtra_clear();
3920 case TERM_XTRA_FRESH:
3921 return term_xtra_fresh();
3922 case TERM_XTRA_REACT:
3923 return term_xtra_react();
3924 default:
3925 return 0;
3926 }
3927 }
3928
3929 static errr term_curs_hook(int col, int row)
3930 {
3931 struct subwindow *subwindow = Term->data;
3932 assert(subwindow != NULL);
3933
3934 render_cursor(subwindow, col, row, false);
3935
3936 subwindow->window->dirty = true;
3937
3938 return 0;
3939 }
3940
3941 static errr term_bigcurs_hook(int col, int row)
3942 {
3943 struct subwindow *subwindow = Term->data;
3944 assert(subwindow != NULL);
3945
3946 render_cursor(subwindow, col, row, true);
3947
3948 subwindow->window->dirty = true;
3949
3950 return 0;
3951 }
3952
3953 static errr term_wipe_hook(int col, int row, int n)
3954 {
3955 struct subwindow *subwindow = Term->data;
3956 assert(subwindow != NULL);
3957
3958 SDL_Rect rect = {
3959 subwindow->inner_rect.x + col * subwindow->font_width,
3960 subwindow->inner_rect.y + row * subwindow->font_height,
3961 n * subwindow->font_width,
3962 subwindow->font_height
3963 };
3964
3965 render_fill_rect(subwindow->window, subwindow->texture, &rect, &subwindow->color);
3966
3967 subwindow->window->dirty = true;
3968
3969 return 0;
3970 }
3971
3972 static errr term_text_hook(int col, int row, int n, int a, const wchar_t *s)
3973 {
3974 struct subwindow *subwindow = Term->data;
3975 assert(subwindow != NULL);
3976
3977 SDL_Color fg = g_colors[a % MAX_COLORS];
3978 SDL_Color bg;
3979
3980 switch (a / MULT_BG) {
3981 case BG_BLACK:
3982 bg = subwindow->color;
3983 break;
3984 case BG_SAME:
3985 bg = fg;
3986 break;
3987 case BG_DARK:
3988 bg = g_colors[DEFAULT_SHADE_COLOR];
3989 break;
3990 default:
3991 /* debugging */
3992 bg = g_colors[DEFAULT_ERROR_COLOR];
3993 break;
3994 }
3995
3996 bg.a = subwindow->color.a;
3997
3998 SDL_Rect rect = {
3999 subwindow->inner_rect.x + col * subwindow->font_width,
4000 subwindow->inner_rect.y + row * subwindow->font_height,
4001 n * subwindow->font_width,
4002 subwindow->font_height
4003 };
4004
4005 render_fill_rect(subwindow->window, subwindow->texture, &rect, &bg);
4006
4007 rect.w = subwindow->font_width;
4008 for (int i = 0; i < n; i++) {
4009 render_glyph_mono(subwindow->window,
4010 subwindow->font, subwindow->texture,
4011 rect.x, rect.y, &fg, (uint32_t) s[i]);
4012 rect.x += subwindow->font_width;
4013 }
4014
4015 subwindow->window->dirty = true;
4016
4017 return 0;
4018 }
4019
4020 static errr term_pict_hook(int col, int row, int n,
4021 const int *ap, const wchar_t *cp, const int *tap, const wchar_t *tcp)
4022 {
4023 int dhrclip;
4024 struct subwindow *subwindow = Term->data;
4025 assert(subwindow != NULL);
4026
4027 if (!current_graphics_mode || current_graphics_mode->grafID == GRAPHICS_NONE) {
4028 /*
4029 * Do nothing unsuccessfully if asked to draw a tile while
4030 * they're not enabled. Could proceed in this function
4031 * with no apparent ill effects, but that just wastes time.
4032 */
4033 return -1;
4034 }
4035 assert(subwindow->window->graphics.texture != NULL);
4036
4037 if (subwindow->window->graphics.overdraw_row) {
4038 dhrclip = Term_get_first_tile_row(Term) + tile_height - 1;
4039 } else {
4040 /*
4041 * There's no double-height tiles so the value does not
4042 * matter.
4043 */
4044 dhrclip = 0;
4045 }
4046
4047 for (int i = 0; i < n; i++) {
4048 render_tile_font_scaled(subwindow, col + i, row, tap[i], tcp[i], true, dhrclip);
4049
4050 if (tap[i] == ap[i] && tcp[i] == cp[i]) {
4051 continue;
4052 }
4053
4054 render_tile_font_scaled(subwindow, col + i, row, ap[i], cp[i], false, dhrclip);
4055 }
4056
4057 subwindow->window->dirty = true;
4058
4059 return 0;
4060 }
4061
4062 static void term_view_map_shared(struct subwindow *subwindow,
4063 SDL_Texture *map, int w, int h)
4064 {
4065 render_all(subwindow->window);
4066
4067 SDL_Rect dst = {
4068 0, 0,
4069 w + 2 * DEFAULT_VISIBLE_BORDER,
4070 h + 2 * DEFAULT_VISIBLE_BORDER
4071 };
4072 SDL_Rect full = {
4073 0, 0,
4074 subwindow->window->full_rect.w,
4075 subwindow->window->full_rect.h
4076 };
4077 fit_rect_in_rect_proportional(&dst, &full);
4078
4079 dst.x = (subwindow->window->full_rect.w - dst.w) / 2;
4080 dst.y = (subwindow->window->full_rect.h - dst.h) / 2;
4081 resize_rect(&dst,
4082 DEFAULT_VISIBLE_BORDER, DEFAULT_VISIBLE_BORDER,
4083 -DEFAULT_VISIBLE_BORDER, -DEFAULT_VISIBLE_BORDER);
4084
4085 render_all(subwindow->window);
4086 SDL_SetRenderTarget(subwindow->window->renderer, NULL);
4087 SDL_RenderCopy(subwindow->window->renderer, map, NULL, &dst);
4088
4089 /* render borders around the whole map */
4090 resize_rect(&dst,
4091 -DEFAULT_VISIBLE_BORDER, -DEFAULT_VISIBLE_BORDER,
4092 DEFAULT_VISIBLE_BORDER, DEFAULT_VISIBLE_BORDER);
4093 render_outline_rect_width(subwindow->window,
4094 NULL, &dst, &subwindow->borders.color, DEFAULT_VISIBLE_BORDER);
4095
4096 SDL_RenderPresent(subwindow->window->renderer);
4097
4098 wait_anykey();
4099 }
4100
4101 static void term_view_map_tile(struct subwindow *subwindow)
4102 {
4103 assert(subwindow->window->graphics.id != GRAPHICS_NONE);
4104
4105 SDL_Rect tile = {
4106 0, 0, REASONABLE_MAP_TILE_WIDTH, REASONABLE_MAP_TILE_HEIGHT
4107 };
4108 SDL_Rect source = {
4109 0, 0,
4110 subwindow->window->graphics.tile_pixel_w,
4111 subwindow->window->graphics.tile_pixel_h
4112 };
4113
4114 fit_rect_in_rect_proportional(&tile, &source);
4115
4116 int w = tile.w * cave->width;
4117 int h = tile.h * cave->height;
4118
4119 SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
4120
4121 SDL_Texture *map = make_subwindow_texture(subwindow->window, w, h);
4122 assert(map != NULL);
4123
4124 render_clear(subwindow->window, map, &subwindow->color);
4125
4126 for (int y = 0; y < cave->height; y++) {
4127 tile.y = y * tile.w;
4128 for (int x = 0; x < cave->width; x++) {
4129 tile.x = x * tile.h;
4130 render_grid_cell_tile(subwindow, map, tile, x, y);
4131 }
4132 }
4133
4134 SDL_Rect cursor = {player->grid.x * tile.w, player->grid.y * tile.h, tile.w,
4135 tile.h};
4136
4137 /* render cursor around player */
4138 render_outline_rect_width(subwindow->window,
4139 map, &cursor, &g_colors[DEFAULT_SUBWINDOW_CURSOR_COLOR],
4140 /* XXX some arbitrary values that look ok at the moment */
4141 MIN(MIN(tile.w / 4, tile.h / 4),
4142 DEFAULT_VISIBLE_BORDER));
4143
4144 term_view_map_shared(subwindow, map, w, h);
4145
4146 SDL_DestroyTexture(map);
4147
4148 SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");
4149 }
4150
4151 static void term_view_map_text(struct subwindow *subwindow)
4152 {
4153 int w = subwindow->font_width * cave->width;
4154 int h = subwindow->font_height * cave->height;
4155
4156 SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
4157
4158 SDL_Texture *map = make_subwindow_texture(subwindow->window, w, h);
4159 assert(map != NULL);
4160
4161 render_clear(subwindow->window, map, &subwindow->color);
4162
4163 for (int y = 0; y < cave->height; y++) {
4164 for (int x = 0; x < cave->width; x++) {
4165 render_grid_cell_text(subwindow, map, x, y);
4166 }
4167 }
4168
4169 SDL_Rect cursor = {
4170 player->grid.x * subwindow->font_width,
4171 player->grid.y * subwindow->font_height,
4172 subwindow->font_width,
4173 subwindow->font_height
4174 };
4175
4176 /* render cursor around player */
4177 render_outline_rect_width(subwindow->window,
4178 map, &cursor, &g_colors[DEFAULT_SUBWINDOW_CURSOR_COLOR],
4179 /* XXX some arbitrary values that look reasonable at the moment */
4180 MIN(MIN(subwindow->font_width / 4,
4181 subwindow->font_height / 4),
4182 DEFAULT_VISIBLE_BORDER));
4183
4184 term_view_map_shared(subwindow, map, w, h);
4185
4186 SDL_DestroyTexture(map);
4187
4188 SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");
4189 }
4190
4191 static void term_view_map_hook(term *terminal)
4192 {
4193 struct subwindow *subwindow = terminal->data;
4194 if (subwindow->window->graphics.id == GRAPHICS_NONE) {
4195 term_view_map_text(subwindow);
4196 } else {
4197 term_view_map_tile(subwindow);
4198 }
4199 }
4200
4201 #ifdef MSYS2_ENCODING_WORKAROUND
4202 /*
4203 * Override the default character encoding in MSYS2 for better handling in
4204 * term_text_hook() of characters that don't fall in the ASCII range. See
4205 * init_sdl2() for more details about the default character encoding in MSYS2.
4206 */
4207
4208 /**
4209 * Convert UTF-8 to UTF-16 with each UTF-16 in the native byte order and
4210 * lossily change any code point that requires a surrogate pair to U+FFFD.
4211 * Return the total number of code points that would be generated by
4212 * converting the * UTF-8 input.
4213 *
4214 * \param dest Points to the buffer in which to store the conversion. May be
4215 * NULL.
4216 * \param src Is a null-terminated UTF-8 sequence.
4217 * \param n Is the maximum number of code points to store in dest.
4218 *
4219 * In case of malformed UTF-8, inserts a U+FFFD in the converted output at the
4220 * point of the error.
4221 */
4222 static size_t term_mbcs_sdl2_msys2(wchar_t *dest, const char *src, int n)
4223 {
4224 size_t nout = (n > 0) ? n : 0;
4225 size_t count = 0;
4226
4227 while (1) {
4228 /*
4229 * Default to U+FFFD to indicate an erroneous UTF-8 sequence
4230 * that could not be decoded. Follow "best practice"
4231 * recommended by the Unicode 6 standard: an erroneous
4232 * sequence ends as soon as a disallowed byte is encountered.
4233 */
4234 uint32_t decoded = 0xfffd;
4235
4236 if (((unsigned int) *src & 0x80) == 0) {
4237 /*
4238 * Encoded as a single byte: U+0000 to U+007F ->
4239 * 0xxxxxxx.
4240 */
4241 if (*src == 0) {
4242 if (dest && count < nout) {
4243 dest[count] = 0;
4244 }
4245 break;
4246 }
4247 decoded = *src;
4248 ++src;
4249 } else if (((unsigned int) *src & 0xe0) == 0xc0) {
4250 /*
4251 * Encoded as two bytes: U+0080 to U+07FF ->
4252 * 110xxxxx 10xxxxxx.
4253 */
4254 uint32_t part = ((uint32_t) *src & 0x1f) << 6;
4255
4256 ++src;
4257 /*
4258 * Check that the first two bits of the continuation
4259 * byte are valid and the encoding is not overlong.
4260 */
4261 if (((unsigned int) *src & 0xc0) == 0x80
4262 && part > 0x40) {
4263 decoded = part + ((uint32_t) *src & 0x3f);
4264 ++src;
4265 }
4266 } else if (((unsigned int) *src & 0xf0) == 0xe0) {
4267 /*
4268 * Encoded as three bytes: U+0800 to U+FFFF ->
4269 * 1110xxxx 10xxxxxx 10xxxxxx.
4270 */
4271 uint32_t part = ((uint32_t) *src & 0xf) << 12;
4272
4273 ++src;
4274 if (((unsigned int) *src & 0xc0) == 0x80) {
4275 part += ((uint32_t) *src & 0x3f) << 6;
4276 ++src;
4277 /*
4278 * The second part of the test rejects
4279 * overlong encodings. The third part
4280 * rejects encodings of U+D800 to U+DFFF,
4281 * reserved for surrogate pairs.
4282 */
4283 if (((unsigned int) *src & 0xc0) == 0x80
4284 && part >= 0x800
4285 && (part & 0xf800) != 0xd800) {
4286 decoded = part
4287 + ((uint32_t) *src & 0x3f);
4288 ++src;
4289 }
4290 }
4291 } else if (((unsigned int) *src & 0xf8) == 0xf0) {
4292 /*
4293 * Encoded as four bytes: U+10000 to U+1FFFFF ->
4294 * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx.
4295 */
4296 uint32_t part = ((uint32_t) *src & 0x7) << 18;
4297
4298 ++src;
4299 if (((unsigned int) *src & 0xc0) == 0x80) {
4300 part += ((uint32_t) * src & 0x3f) << 12;
4301 ++src;
4302 /*
4303 * The second part of the test rejects
4304 * overlong encodings. The third part
4305 * rejects code points beyond U+10FFFF which
4306 * can't be encoded in UTF-16.
4307 */
4308 if (((unsigned int) *src & 0xc0) == 0x80
4309 && part >= 0x10000
4310 && (part & 0xff0000)
4311 <= 0x100000) {
4312 part += ((uint32_t) *src & 0x3f) << 6;
4313 ++src;
4314 if (((unsigned int) *src & 0xc0)
4315 == 0x80) {
4316 decoded = part
4317 + ((uint32_t) *src
4318 & 0x3f);
4319 ++src;
4320 }
4321 }
4322 }
4323 } else {
4324 /*
4325 * Either an impossible byte or one that signals the
4326 * start of a five byte or longer encoding.
4327 */
4328 ++src;
4329 }
4330 if (dest && count < nout) {
4331 if (decoded >= 0x10000) {
4332 /*
4333 * Would require a surrogate pair to represent
4334 * accurately. Substitute U+FFFD instead.
4335 */
4336 assert(decoded <= 0x10FFFF);
4337 dest[count] = (wchar_t) 0xfffd;
4338 } else {
4339 /*
4340 * By the decoding logic, the decoded value
4341 * should not match part of surrogate pair.
4342 */
4343 assert(decoded < 0xd800 || decoded > 0xdfff);
4344 dest[count] = (wchar_t) decoded;
4345 }
4346 }
4347 ++count;
4348 }
4349 return count;
4350 }
4351
4352 /**
4353 * Convert a UTF-16 stored in the native byte order to UTF-8.
4354 * \param s Points to the buffer where the conversion should be stored.
4355 * That buffer must have at least term_wcsz_sdl2_msys2() bytes.
4356 * \param wchar Is the UTF-16 value to convert.
4357 * \return The returned value is the number of bytes written to s or -1
4358 * if the UTF-16 value could not be converted.
4359 *
4360 * This is a necessary counterpart to term_mbcs_sdl2_msys2(): since we
4361 * are overriding the default multibyte to wide character conversion, need
4362 * to override the reverse conversion as well.
4363 */
4364 static int term_wctomb_sdl2_msys2(char *s, wchar_t wchar)
4365 {
4366 if ((unsigned int) wchar <= 0x7f) {
4367 *s = wchar;
4368 return 1;
4369 }
4370 if ((unsigned int) wchar < 0x7ff) {
4371 *s++ = 0xc0 + (((unsigned int) wchar & 0x7c0) >> 6);
4372 *s++ = 0x80 + ((unsigned int) wchar & 0x3f);
4373 return 2;
4374 }
4375 /* Refuse to encode a value reserved for surrogate pairs in UTF-16. */
4376 if ((unsigned int) wchar >= 0xd800 && (unsigned int) wchar <= 0xdfff) {
4377 return -1;
4378 }
4379 *s++ = 0xe0 + (((unsigned int) wchar & 0xf000) >> 12);
4380 *s++ = 0x80 + (((unsigned int) wchar & 0xfc0) >> 6);
4381 *s++ = 0x80 + ((unsigned int) wchar & 0x3f);
4382 return 3;
4383 }
4384
4385 /**
4386 * Return whether a UTF-16 value is printable.
4387 *
4388 * This is a necessary counterpart to term_mbcs_sdl2_msys2() so that
4389 * screening of wide characters in the core's text_out_to_screen() is
4390 * consistent with what term_mbcs_sdl2_msys2() does.
4391 */
4392 static int term_iswprint_sdl2_msys2(wint_t wc)
4393 {
4394 /*
4395 * Upcast the UTF-16 value to UTF-32 since the UTF-16 value is either
4396 * equal to the code point's value or is part of a surrogate pair.
4397 */
4398 return utf32_isprint((uint32_t) wc);
4399 }
4400
4401 /**
4402 * Return the maximum number of bytes needed for a multibyte encoding of a
4403 * wchar.
4404 */
4405 static int term_wcsz_sdl2_msys2(void)
4406 {
4407 /*
4408 * UTF-8 takes at most 3 bytes to encode a code point that can be
4409 * encoded with a single 16-bit quantity in UTF-16.
4410 */
4411 return 3;
4412 }
4413
4414 #endif /* MSYS2_ENCODING_WORKAROUND */
4415
4416 static SDL_Texture *load_image(const struct window *window, const char *path)
4417 {
4418 SDL_Surface *surface = IMG_Load(path);
4419 if (surface == NULL) {
4420 quit_fmt("cannot load image '%s': %s", path, IMG_GetError());
4421 }
4422 SDL_Texture *texture = SDL_CreateTextureFromSurface(window->renderer, surface);
4423 if (texture == NULL) {
4424 quit_fmt("cannot create texture from image '%s': %s", path, SDL_GetError());
4425 }
4426 SDL_FreeSurface(surface);
4427
4428 return texture;
4429 }
4430
4431 static void load_wallpaper(struct window *window, const char *path)
4432 {
4433 if (window->wallpaper.mode == WALLPAPER_DONT_SHOW) {
4434 return;
4435 }
4436 if (window->wallpaper.mode == WALLPAPER_INVALID) {
4437 quit_fmt("invalid wallpaper mode in window %u", window->index);
4438 }
4439
4440 SDL_Texture *wallpaper = load_image(window, path);
4441 assert(wallpaper != NULL);
4442
4443 if (window->wallpaper.mode == WALLPAPER_TILED) {
4444 int w;
4445 int h;
4446 SDL_QueryTexture(wallpaper, NULL, NULL, &w, &h);
4447
4448 SDL_Rect dst = {0, 0, w, h};
4449
4450 /* make wallpaper 1/4 to 1/2 of the window's width and height
4451 * that would be 4 to 16 calls to RenderCopy in wallpaper rendering function */
4452 while (w < window->inner_rect.w / 4) {
4453 w *= 2;
4454 }
4455 while (h < window->inner_rect.h / 4) {
4456 h *= 2;
4457 }
4458 window->wallpaper.texture = make_subwindow_texture(window, w, h);
4459 window->wallpaper.w = w;
4460 window->wallpaper.h = h;
4461
4462 SDL_Color color = {0};
4463 render_clear(window, window->wallpaper.texture, &color);
4464
4465 for (dst.y = 0; dst.y < h; dst.y += dst.h) {
4466 for (dst.x = 0; dst.x < w; dst.x += dst.w) {
4467 SDL_RenderCopy(window->renderer, wallpaper, NULL, &dst);
4468 }
4469 }
4470 SDL_DestroyTexture(wallpaper);
4471 } else {
4472 SDL_QueryTexture(wallpaper, NULL, NULL,
4473 &window->wallpaper.w, &window->wallpaper.h);
4474 window->wallpaper.texture = wallpaper;
4475 }
4476 }
4477
4478 static void load_default_wallpaper(struct window *window)
4479 {
4480 if (window->wallpaper.mode == WALLPAPER_DONT_SHOW) {
4481 return;
4482 }
4483
4484 char path[4096];
4485 path_build(path, sizeof(path), DEFAULT_WALLPAPER_DIR, DEFAULT_WALLPAPER);
4486
4487 load_wallpaper(window, path);
4488 }
4489
4490 static void load_stipple(struct window *window)
4491 {
4492 SDL_Surface *s;
4493 Uint32 *pixels;
4494 Uint32 rmask, gmask, bmask, amask, on_pixel, off_pixel;
4495 int y, x;
4496
4497 /*
4498 * on_pixel is black and completely transparent. off_pixel is gray
4499 * (0x40, 0x40, 0x40) and slightly opaque.
4500 */
4501 #if SDL_BYTE_ORDER == SDL_BIGENDIAN
4502 rmask = 0xff000000;
4503 gmask = 0x00ff0000;
4504 bmask = 0x0000ff00;
4505 amask = 0x000000ff;
4506 on_pixel = 0x000000ff;
4507 off_pixel = 0x40404040;
4508 #else
4509 rmask = 0x000000ff;
4510 gmask = 0x0000ff00;
4511 bmask = 0x00ff0000;
4512 amask = 0xff000000;
4513 on_pixel = 0xff000000;
4514 off_pixel = 0x40404040;
4515 #endif
4516
4517 /*
4518 * These dimensions must be multiple of two: see the loop logic below.
4519 */
4520 window->stipple.h = 16;
4521 window->stipple.w = 16;
4522 pixels = mem_alloc(window->stipple.h * window->stipple.w *
4523 sizeof(*pixels));
4524 for (y = 0; y < window->stipple.h; y += 2) {
4525 uint32_t *row = pixels + y * window->stipple.w;
4526
4527 for (x = 0; x < window->stipple.w; x += 2) {
4528 row[x] = on_pixel;
4529 row[x + 1] = off_pixel;
4530 row[x + window->stipple.w] = off_pixel;
4531 row[x + window->stipple.w + 1] = on_pixel;
4532 }
4533 }
4534
4535 s = SDL_CreateRGBSurfaceFrom(pixels, window->stipple.w,
4536 window->stipple.h, 32, 4 * window->stipple.w, rmask, gmask,
4537 bmask, amask);
4538 window->stipple.texture = SDL_CreateTextureFromSurface(window->renderer,
4539 s);
4540 if (window->stipple.texture == NULL) {
4541 (void) fprintf(stderr, "could not create stipple texture: %s\n",
4542 SDL_GetError());
4543 }
4544
4545 SDL_FreeSurface(s);
4546 mem_free(pixels);
4547 }
4548
4549 static void load_default_window_icon(const struct window *window)
4550 {
4551 char path[4096];
4552 path_build(path, sizeof(path), DEFAULT_WINDOW_ICON_DIR, DEFAULT_WINDOW_ICON);
4553
4554 SDL_Surface *surface = IMG_Load(path);
4555 assert(surface != NULL);
4556
4557 SDL_SetWindowIcon(window->window, surface);
4558
4559 SDL_FreeSurface(surface);
4560 }
4561
4562 static void load_graphics(struct window *window, graphics_mode *mode)
4563 {
4564 assert(window->graphics.texture == NULL);
4565
4566 current_graphics_mode = mode;
4567 use_graphics = mode->grafID;
4568
4569 if (use_graphics == GRAPHICS_NONE) {
4570 tile_width = 1;
4571 tile_height = 1;
4572 } else {
4573 size_t i;
4574 char path[4096];
4575 path_build(path, sizeof(path), mode->path, mode->file);
4576 if (!file_exists(path)) {
4577 quit_fmt("cannot load graphics: file '%s' doesn't exist", path);
4578 }
4579
4580 window->graphics.texture = load_image(window, path);
4581 assert(window->graphics.texture != NULL);
4582
4583 window->graphics.tile_pixel_w = mode->cell_width;
4584 window->graphics.tile_pixel_h = mode->cell_height;
4585
4586 window->graphics.overdraw_row = mode->overdrawRow;
4587 window->graphics.overdraw_max = mode->overdrawMax;
4588
4589 for (i = 0; i < N_ELEMENTS(window->subwindows); i++) {
4590 if (window->subwindows[i] &&
4591 window->subwindows[i]->term) {
4592 window->subwindows[i]->term->dblh_hook =
4593 (window->graphics.overdraw_row) ?
4594 is_dh_tile : NULL;
4595 }
4596 }
4597 }
4598
4599 if (character_dungeon) {
4600 reset_visuals(true);
4601 }
4602
4603 window->graphics.id = mode->grafID;
4604 }
4605
4606 static void reload_all_graphics(graphics_mode *mode)
4607 {
4608 if (mode == NULL) {
4609 return;
4610 }
4611
4612 for (size_t i = 0; i < MAX_WINDOWS; i++) {
4613 struct window *window = get_window_direct(i);
4614 if (window == NULL) {
4615 continue;
4616 }
4617
4618 free_graphics(&window->graphics);
4619 memset(&window->graphics, 0, sizeof(window->graphics));
4620 window->graphics.texture = NULL;
4621
4622 load_graphics(window, mode);
4623 }
4624 }
4625
4626 static const struct font_info *find_font_info(const char *name)
4627 {
4628 for (size_t i = 0; i < N_ELEMENTS(g_font_info); i++) {
4629 if (g_font_info[i].loaded && streq(g_font_info[i].name, name)) {
4630 return &g_font_info[i];
4631 }
4632 }
4633
4634 return NULL;
4635 }
4636
4637 static void make_font_cache(const struct window *window, struct font *font)
4638 {
4639 font->cache.texture = make_subwindow_texture(window,
4640 (int) ASCII_CACHE_SIZE * font->ttf.glyph.w, font->ttf.glyph.h);
4641 assert(font->cache.texture != NULL);
4642
4643 /* fill texture with white transparent pixels */
4644 SDL_Color white = {0xFF, 0xFF, 0xFF, 0};
4645 render_clear(window, font->cache.texture, &white);
4646 /* restore the alpha; we will render glyphs in white */
4647 white.a = 0xFF;
4648
4649 const int glyph_w = font->ttf.glyph.w;
4650 const int glyph_h = font->ttf.glyph.h;
4651
4652 for (size_t i = 0; i < ASCII_CACHE_SIZE; i++) {
4653 SDL_Surface *surface = TTF_RenderGlyph_Blended(font->ttf.handle,
4654 (Uint16) g_ascii_codepoints_for_cache[i], white);
4655 if (surface == NULL) {
4656 quit_fmt("font cache rendering failed for '%c'"
4657 " (ASCII %lu) in font '%s': %s",
4658 g_ascii_codepoints_for_cache[i],
4659 (unsigned long) i, font->name, TTF_GetError());
4660 }
4661
4662 SDL_Texture *texture = SDL_CreateTextureFromSurface(window->renderer, surface);
4663 if (texture == NULL) {
4664 quit_fmt("cannot create texture for cache in font '%s': %s",
4665 font->name, SDL_GetError());
4666 }
4667
4668 SDL_Rect src = {0, 0, surface->w, surface->h};
4669 SDL_Rect dst = {glyph_w * i, 0, glyph_w, glyph_h};
4670
4671 crop_rects(&src, &dst);
4672
4673 SDL_RenderCopy(window->renderer, texture, &src, &dst);
4674
4675 font->cache.rects[i] = dst;
4676
4677 SDL_FreeSurface(surface);
4678 SDL_DestroyTexture(texture);
4679 }
4680 }
4681
4682 static struct font *make_font(const struct window *window,
4683 const char *name, int size)
4684 {
4685 const struct font_info *info = find_font_info(name);
4686 if (info == NULL) {
4687 return NULL;
4688 }
4689
4690 struct font *font = mem_zalloc(sizeof(*font));
4691
4692 font->index = info->index;
4693 font->path = string_make(info->path);
4694 font->name = string_make(info->name);
4695 font->size = size;
4696
4697 font->cache.texture = NULL;
4698
4699 load_font(font);
4700 make_font_cache(window, font);
4701
4702 return font;
4703 }
4704
4705 static bool reload_font(struct subwindow *subwindow,
4706 const struct font_info *info)
4707 {
4708 struct font *new_font = make_font(subwindow->window, info->name, info->size);
4709 if (new_font == NULL) {
4710 return false;
4711 }
4712
4713 subwindow->sizing_rect = subwindow->full_rect;
4714 if (!is_ok_col_row(subwindow,
4715 &subwindow->sizing_rect, new_font->ttf.glyph.w, new_font->ttf.glyph.h))
4716 {
4717 int min_cols = subwindow->index == MAIN_SUBWINDOW ? MIN_COLS_MAIN : MIN_COLS_OTHER;
4718 int min_rows = subwindow->index == MAIN_SUBWINDOW ? MIN_ROWS_MAIN : MIN_ROWS_OTHER;
4719
4720 subwindow->sizing_rect.w = min_cols * new_font->ttf.glyph.w + 2 * DEFAULT_BORDER;
4721 subwindow->sizing_rect.h = min_rows * new_font->ttf.glyph.h + 2 * DEFAULT_BORDER;
4722 }
4723
4724 if (subwindow->sizing_rect.w > subwindow->window->inner_rect.w
4725 || subwindow->sizing_rect.h > subwindow->window->inner_rect.h)
4726 {
4727 free_font(new_font);
4728 memset(&subwindow->sizing_rect, 0, sizeof(subwindow->sizing_rect));
4729
4730 return false;
4731 }
4732
4733 fit_rect_in_rect_by_xy(&subwindow->sizing_rect, &subwindow->window->inner_rect);
4734
4735 free_font(subwindow->font);
4736 subwindow->font = new_font;
4737
4738 resize_subwindow(subwindow);
4739
4740 return true;
4741 }
4742
4743 static void load_font(struct font *font)
4744 {
4745 assert(font != NULL);
4746 assert(font->path != NULL);
4747
4748 font->ttf.handle = TTF_OpenFont(font->path, font->size);
4749 if (font->ttf.handle == NULL) {
4750 quit_fmt("cannot open font '%s': %s", font->path, TTF_GetError());
4751 }
4752
4753 font->ttf.glyph.h = TTF_FontHeight(font->ttf.handle);
4754
4755 if (TTF_GlyphMetrics(font->ttf.handle, GLYPH_FOR_ADVANCE,
4756 NULL, NULL, NULL, NULL, &font->ttf.glyph.w) != 0)
4757 {
4758 quit_fmt("cannot query glyph metrics for font '%s': %s",
4759 font->path, TTF_GetError());
4760 }
4761
4762 TTF_SetFontHinting(font->ttf.handle, DEFAULT_FONT_HINTING);
4763 }
4764
4765 static void free_font(struct font *font)
4766 {
4767 if (font->name != NULL) {
4768 mem_free(font->name);
4769 }
4770 if (font->path != NULL) {
4771 mem_free(font->path);
4772 }
4773 if (font->ttf.handle != NULL) {
4774 TTF_CloseFont(font->ttf.handle);
4775 }
4776 if (font->cache.texture != NULL) {
4777 SDL_DestroyTexture(font->cache.texture);
4778 }
4779
4780 mem_free(font);
4781 }
4782
4783 static bool is_ok_col_row(const struct subwindow *subwindow,
4784 const SDL_Rect *rect, int cell_w, int cell_h)
4785 {
4786 const int min_col =
4787 subwindow->index == MAIN_SUBWINDOW ? MIN_COLS_MAIN : MIN_COLS_OTHER;
4788 const int min_row =
4789 subwindow->index == MAIN_SUBWINDOW ? MIN_ROWS_MAIN : MIN_ROWS_OTHER;
4790
4791 if ((rect->w - DEFAULT_BORDER * 2) / cell_w < min_col) {
4792 return false;
4793 }
4794 if ((rect->h - DEFAULT_BORDER * 2) / cell_h < min_row) {
4795 return false;
4796 }
4797
4798 return true;
4799 }
4800
4801 static bool adjust_subwindow_geometry(const struct window *window,
4802 struct subwindow *subwindow)
4803 {
4804 if (!subwindow->loaded && subwindow->config == NULL) {
4805 /* lets center it then */
4806 subwindow->full_rect.x = MAX(window->inner_rect.x,
4807 (window->inner_rect.w - subwindow->full_rect.w) / 2);
4808 subwindow->full_rect.y = MAX(window->inner_rect.y,
4809 (window->inner_rect.h - subwindow->full_rect.h) / 2);
4810 }
4811
4812 /* coordinates of inner rect are relative to that of outer rect
4813 * (really, they are relative to subwindow's texture) */
4814 subwindow->inner_rect.x = 0;
4815 subwindow->inner_rect.y = 0;
4816 subwindow->inner_rect.w = subwindow->full_rect.w;
4817 subwindow->inner_rect.h = subwindow->full_rect.h;
4818
4819 memset(&subwindow->sizing_rect, 0, sizeof(subwindow->sizing_rect));
4820
4821 resize_rect(&subwindow->inner_rect,
4822 DEFAULT_BORDER, DEFAULT_BORDER,
4823 -DEFAULT_BORDER, -DEFAULT_BORDER);
4824
4825 subwindow->borders.width = DEFAULT_VISIBLE_BORDER;
4826
4827 subwindow->font_width = subwindow->font->ttf.glyph.w;
4828 subwindow->font_height = subwindow->font->ttf.glyph.h;
4829
4830 subwindow->cols = subwindow->inner_rect.w / subwindow->font_width;
4831 subwindow->rows = subwindow->inner_rect.h / subwindow->font_height;
4832
4833 subwindow->inner_rect.w = subwindow->cols * subwindow->font_width;
4834 subwindow->inner_rect.h = subwindow->rows * subwindow->font_height;
4835
4836 subwindow->inner_rect.x =
4837 (subwindow->full_rect.w - subwindow->inner_rect.w) / 2;
4838 subwindow->inner_rect.y =
4839 (subwindow->full_rect.h - subwindow->inner_rect.h) / 2;
4840
4841 if (!is_ok_col_row(subwindow,
4842 &subwindow->full_rect, subwindow->font_width, subwindow->font_height))
4843 {
4844 return false;
4845 }
4846
4847 if (!is_rect_in_rect(&subwindow->full_rect, &window->inner_rect)) {
4848 subwindow->borders.error = true;
4849 }
4850
4851 return true;
4852 }
4853
4854 static void sort_to_top_aux(struct window *window, size_t *next_subwindow,
4855 struct subwindow **subwindows, size_t num_subwindows,
4856 bool top, bool always_top)
4857 {
4858 if (*next_subwindow == num_subwindows) {
4859 return;
4860 }
4861
4862 for (size_t i = 0; i < N_ELEMENTS(window->subwindows); i++) {
4863 if (window->subwindows[i] != NULL
4864 && window->subwindows[i]->top == top
4865 && window->subwindows[i]->always_top == always_top)
4866 {
4867 subwindows[(*next_subwindow)++] = window->subwindows[i];
4868 }
4869 }
4870
4871 assert(*next_subwindow <= num_subwindows);
4872 }
4873
4874 static void sort_to_top(struct window *window)
4875 {
4876 struct subwindow *tmp[N_ELEMENTS(window->subwindows)] = {NULL};
4877 assert(sizeof(window->subwindows) == sizeof(tmp));
4878
4879 size_t num_subwindows = 0;
4880 for (size_t i = 0; i < N_ELEMENTS(window->subwindows); i++) {
4881 if (window->subwindows[i] != NULL) {
4882 num_subwindows++;
4883 }
4884 }
4885
4886 /* and that's how we sort here :) */
4887 size_t current = 0;
4888 sort_to_top_aux(window, &current, tmp, num_subwindows, false, false);
4889 sort_to_top_aux(window, &current, tmp, num_subwindows, true, false);
4890 sort_to_top_aux(window, &current, tmp, num_subwindows, false, true);
4891 sort_to_top_aux(window, &current, tmp, num_subwindows, true, true);
4892
4893 memcpy(window->subwindows, tmp, sizeof(window->subwindows));
4894 }
4895
4896 static void bring_to_top(struct window *window, struct subwindow *subwindow)
4897 {
4898 assert(subwindow->window == window);
4899
4900 bool found_subwindow_in_window = false;
4901 for (size_t i = 0; i < N_ELEMENTS(window->subwindows); i++) {
4902 if (window->subwindows[i] != NULL) {
4903 window->subwindows[i]->top = false;
4904 if (window->subwindows[i] == subwindow) {
4905 found_subwindow_in_window = true;
4906 }
4907 }
4908 }
4909 assert(found_subwindow_in_window);
4910
4911 subwindow->top = true;
4912
4913 sort_to_top(window);
4914 }
4915
4916 static void adjust_status_bar_geometry(struct window *window)
4917 {
4918 struct status_bar *status_bar = &window->status_bar;
4919
4920 status_bar->full_rect.x = 0;
4921 status_bar->full_rect.y = 0;
4922 status_bar->full_rect.w = window->full_rect.w;
4923 status_bar->full_rect.h = DEFAULT_LINE_HEIGHT(status_bar->font->ttf.glyph.h);
4924 status_bar->inner_rect = status_bar->full_rect;
4925
4926 int border = (status_bar->full_rect.h - status_bar->font->ttf.glyph.h) / 2;
4927 resize_rect(&status_bar->inner_rect,
4928 border, border, -border, -border);
4929 }
4930
4931 static struct subwindow *get_subwindow_by_index(const struct window *window,
4932 unsigned index, bool visible)
4933 {
4934 for (size_t i = 0; i < N_ELEMENTS(window->subwindows); i++) {
4935 struct subwindow *subwindow = window->subwindows[i];
4936
4937 if (subwindow != NULL
4938 && (visible ? subwindow->visible : true)
4939 && subwindow->index == index)
4940 {
4941 return subwindow;
4942 }
4943 }
4944
4945 return NULL;
4946 }
4947
4948 static struct subwindow *get_subwindow_by_xy(const struct window *window, int x, int y)
4949 {
4950 /* checking subwindows in z order */
4951 for (size_t i = N_ELEMENTS(window->subwindows); i > 0; i--) {
4952 struct subwindow *subwindow = window->subwindows[i - 1];
4953
4954 if (subwindow != NULL && subwindow->visible) {
4955 if (is_point_in_rect(x, y, &subwindow->full_rect)) {
4956 return subwindow;
4957 }
4958 }
4959 }
4960
4961 return NULL;
4962 }
4963
4964 static struct menu_panel *get_menu_panel_by_xy(struct menu_panel *menu_panel,
4965 int x, int y)
4966 {
4967 while (menu_panel != NULL) {
4968 if (is_point_in_rect(x, y, &menu_panel->rect)) {
4969 return menu_panel;
4970 }
4971 menu_panel = menu_panel->next;
4972 }
4973
4974 return NULL;
4975 }
4976
4977 static bool is_over_status_bar(const struct status_bar *status_bar, int x, int y)
4978 {
4979 return is_point_in_rect(x, y, &status_bar->full_rect);
4980 }
4981
4982 static void make_button_bank(struct button_bank *bank)
4983 {
4984 bank->buttons = mem_zalloc(sizeof(*bank->buttons) * MAX_BUTTONS);
4985
4986 bank->size = MAX_BUTTONS;
4987 bank->number = 0;
4988 }
4989
4990 static bool handle_button_open_subwindow(struct window *window,
4991 struct button *button, const SDL_Event *event)
4992 {
4993 CHECK_BUTTON_DATA_TYPE(button, BUTTON_DATA_UNSIGNED);
4994
4995 if (!click_status_bar_button(window, button, event)) {
4996 return false;
4997 }
4998
4999 unsigned index = button->data.value.unsigned_value;
5000 struct subwindow *subwindow = NULL;
5001
5002 subwindow = get_subwindow_by_index(window, index, false);
5003 if (subwindow != NULL) {
5004 subwindow->visible = !subwindow->visible;
5005 if (subwindow->visible) {
5006 bring_to_top(window, subwindow);
5007 }
5008 } else if (is_subwindow_loaded(index)) {
5009 subwindow = transfer_subwindow(window, index);
5010 subwindow->visible = true;
5011 bring_to_top(window, subwindow);
5012 refresh_angband_terms();
5013 } else {
5014 subwindow = make_subwindow(window, index);
5015 assert(subwindow != NULL);
5016 bring_to_top(window, subwindow);
5017 refresh_angband_terms();
5018 }
5019
5020 redraw_all_windows(false);
5021
5022 return true;
5023 }
5024
5025 static void close_status_bar_menu(struct status_bar *status_bar)
5026 {
5027 if (status_bar->menu_panel != NULL) {
5028 free_menu_panel(status_bar->menu_panel);
5029 status_bar->menu_panel = NULL;
5030 }
5031 }
5032
5033 static void make_default_status_buttons(struct status_bar *status_bar)
5034 {
5035 SDL_Rect rect;
5036 struct button_data data;
5037 struct button_callbacks callbacks;
5038
5039 #define PUSH_BUTTON_LEFT_TO_RIGHT(cap) \
5040 get_string_metrics(status_bar->font, (cap), &rect.w, NULL); \
5041 rect.w += DEFAULT_BUTTON_BORDER * 2; \
5042 push_button(&status_bar->button_bank, status_bar->font, \
5043 (cap), false, data, callbacks, &rect, CAPTION_POSITION_CENTER); \
5044 rect.x += rect.w; \
5045
5046 rect.x = status_bar->full_rect.x;
5047 rect.y = status_bar->full_rect.y;
5048 rect.w = 0;
5049 rect.h = status_bar->full_rect.h;
5050
5051 callbacks.on_render = render_menu_button;
5052 callbacks.on_event = handle_menu_button;
5053 callbacks.on_menu = NULL;
5054
5055 data.type = BUTTON_DATA_NONE;
5056 PUSH_BUTTON_LEFT_TO_RIGHT("Menu");
5057
5058 callbacks.on_render = render_button_subwindows;
5059
5060 data.type = BUTTON_DATA_UNSIGNED;
5061
5062 if (status_bar->window->index == MAIN_WINDOW) {
5063 /* "A" button is not interactive, it's just for display */
5064 callbacks.on_event = ignore_status_bar_button;
5065 data.value.unsigned_value = MAIN_SUBWINDOW;
5066 /* the main term is called Angband in game options */
5067 PUSH_BUTTON_LEFT_TO_RIGHT("A");
5068 }
5069
5070 callbacks.on_event = handle_button_open_subwindow;
5071 for (unsigned i = 1; i < N_ELEMENTS(status_bar->window->subwindows); i++) {
5072 data.value.unsigned_value = i;
5073 PUSH_BUTTON_LEFT_TO_RIGHT(format("%u", i));
5074 }
5075 #undef PUSH_BUTTON_LEFT_TO_RIGHT
5076
5077 #define PUSH_BUTTON_RIGHT_TO_LEFT(cap) \
5078 get_string_metrics(status_bar->font, (cap), &rect.w, NULL); \
5079 rect.w += DEFAULT_BUTTON_BORDER * 2; \
5080 rect.x -= rect.w; \
5081 push_button(&status_bar->button_bank, status_bar->font, \
5082 (cap), false, data, callbacks, &rect, CAPTION_POSITION_CENTER); \
5083
5084 rect.x = status_bar->full_rect.x + status_bar->full_rect.w;
5085 rect.y = status_bar->full_rect.y;
5086 rect.w = 0;
5087 rect.h = status_bar->full_rect.h;
5088
5089 callbacks.on_render = render_button_movesize;
5090 callbacks.on_event = handle_button_movesize;
5091 callbacks.on_menu = NULL;
5092
5093 data.type = BUTTON_DATA_INT;
5094
5095 data.value.int_value = BUTTON_MOVESIZE_MOVING;
5096 PUSH_BUTTON_RIGHT_TO_LEFT("Move");
5097
5098 data.value.int_value = BUTTON_MOVESIZE_SIZING;
5099 PUSH_BUTTON_RIGHT_TO_LEFT("Size");
5100 #undef PUSH_BUTTON_RIGHT_TO_LEFT
5101 }
5102
5103 static void reload_status_bar(struct status_bar *status_bar)
5104 {
5105 close_status_bar_menu(status_bar);
5106
5107 SDL_DestroyTexture(status_bar->texture);
5108 status_bar->texture = make_subwindow_texture(status_bar->window,
5109 status_bar->full_rect.w, status_bar->full_rect.h);
5110 assert(status_bar->texture != NULL);
5111
5112 free_button_bank(&status_bar->button_bank);
5113 make_button_bank(&status_bar->button_bank);
5114 make_default_status_buttons(status_bar);
5115
5116 render_status_bar(status_bar->window);
5117 }
5118
5119 static void load_status_bar(struct window *window)
5120 {
5121 if (window->status_bar.font == NULL) {
5122 if (window->config != NULL) {
5123 window->status_bar.font = make_font(window,
5124 window->config->font_name,
5125 window->config->font_size);
5126 } else {
5127 window->status_bar.font = make_font(window, DEFAULT_STATUS_BAR_FONT, 0);
5128 }
5129 assert(window->status_bar.font != NULL);
5130 } else {
5131 quit_fmt("font '%s' already loaded in status bar in window %u",
5132 window->status_bar.font->name, window->index);
5133 }
5134
5135 adjust_status_bar_geometry(window);
5136
5137 window->status_bar.texture = make_subwindow_texture(window,
5138 window->status_bar.full_rect.w, window->status_bar.full_rect.h);
5139
5140 /* let's try renderer */
5141 if (SDL_SetRenderDrawColor(window->renderer,
5142 window->status_bar.color.r, window->status_bar.color.g,
5143 window->status_bar.color.b, window->status_bar.color.a) != 0) {
5144 quit_fmt("cannot set render color for status bar in window %u: %s",
5145 window->index, SDL_GetError());
5146 }
5147 /* well, renderer seems to work */
5148 if (SDL_SetRenderTarget(window->renderer, window->status_bar.texture) != 0) {
5149 quit_fmt("cannot set status bar texture as target in window %u: %s",
5150 window->index, SDL_GetError());
5151 }
5152 /* does it render? */
5153 if (SDL_RenderClear(window->renderer) != 0) {
5154 quit_fmt("cannot clear status bar texture in window %u: %s",
5155 window->index, SDL_GetError());
5156 }
5157
5158 /* well, it probably works */
5159 window->status_bar.window = window;
5160 }
5161
5162 static void fit_subwindow_in_window(const struct window *window,
5163 struct subwindow *subwindow)
5164 {
5165 fit_rect_in_rect_by_xy(&subwindow->full_rect, &window->inner_rect);
5166 if (!is_rect_in_rect(&subwindow->full_rect, &window->inner_rect)) {
5167 subwindow->borders.error = true;
5168 render_borders(subwindow);
5169 }
5170 }
5171
5172 static void resize_window(struct window *window, int w, int h)
5173 {
5174 if (window->full_rect.w == w
5175 && window->full_rect.h == h)
5176 {
5177 return;
5178 }
5179
5180 window->full_rect.w = w;
5181 window->full_rect.h = h;
5182
5183 adjust_status_bar_geometry(window);
5184 adjust_window_geometry(window);
5185
5186 clear_all_borders(window);
5187 for (size_t i = 0; i < N_ELEMENTS(window->subwindows); i++) {
5188 struct subwindow *subwindow = window->subwindows[i];
5189 if (subwindow != NULL) {
5190 fit_subwindow_in_window(window, subwindow);
5191 }
5192 }
5193
5194 reload_status_bar(&window->status_bar);
5195
5196 redraw_window(window);
5197 }
5198
5199 static void adjust_window_geometry(struct window *window)
5200 {
5201 window->inner_rect.x = 0;
5202 window->inner_rect.y = 0;
5203 window->inner_rect.w = window->full_rect.w;
5204 window->inner_rect.h = window->full_rect.h;
5205
5206 resize_rect(&window->inner_rect,
5207 0, window->status_bar.full_rect.h, 0, 0);
5208
5209 if (window->inner_rect.w <= 0
5210 || window->inner_rect.h <= 0) {
5211 quit_fmt("window %u is too small (%dx%d)",
5212 window->index, window->inner_rect.w, window->inner_rect.h);
5213 }
5214 }
5215
5216 static void set_window_delay(struct window *window)
5217 {
5218 assert(window->window != NULL);
5219
5220 int display = SDL_GetWindowDisplayIndex(window->window);
5221 if (display < 0) {
5222 quit_fmt("cannot get display of window %u: %s",
5223 window->index, SDL_GetError());
5224 }
5225
5226 SDL_DisplayMode mode;
5227 if (SDL_GetCurrentDisplayMode(display, &mode) != 0 ||
5228 mode.refresh_rate <= 0) {
5229 /* lets just guess; 60 fps is standard */
5230 mode.refresh_rate = 60;
5231 }
5232 /* delay in milliseconds; refresh rate in hz */
5233 window->delay = 1000 / mode.refresh_rate;
5234 }
5235
5236 /* initialize miscellaneous things in window */
5237 static void load_window(struct window *window)
5238 {
5239 load_status_bar(window);
5240 adjust_window_geometry(window);
5241 make_button_bank(&window->status_bar.button_bank);
5242 make_default_status_buttons(&window->status_bar);
5243 set_window_delay(window);
5244 if (window->wallpaper.mode != WALLPAPER_DONT_SHOW) {
5245 if (window->config == NULL) {
5246 load_default_wallpaper(window);
5247 } else {
5248 load_wallpaper(window, window->config->wallpaper_path);
5249 }
5250 }
5251 load_stipple(window);
5252 load_default_window_icon(window);
5253 if (window->graphics.id != GRAPHICS_NONE) {
5254 load_graphics(window, get_graphics_mode(window->graphics.id));
5255 }
5256
5257 render_clear(window, NULL, &window->color);
5258 render_status_bar(window);
5259
5260 window->loaded = true;
5261 }
5262
5263 static bool choose_pixelformat(struct window *window,
5264 const struct SDL_RendererInfo *info)
5265 {
5266 #define TRY_SET_WINDOW_PIXELFORMAT(format) \
5267 case SDL_PIXELFORMAT_ ##format: window->pixelformat = SDL_PIXELFORMAT_ ##format;
5268
5269 for (size_t i = 0; i < info->num_texture_formats; i++) {
5270 switch (info->texture_formats[i]) {
5271 TRY_SET_WINDOW_PIXELFORMAT(ARGB8888); return true;
5272 TRY_SET_WINDOW_PIXELFORMAT(RGBA8888); return true;
5273 TRY_SET_WINDOW_PIXELFORMAT(ABGR8888); return true;
5274 TRY_SET_WINDOW_PIXELFORMAT(BGRA8888); return true;
5275 }
5276 }
5277 #undef TRY_SET_WINDOW_PIXELFORMAT
5278
5279 return false;
5280 }
5281
5282 static void start_window(struct window *window)
5283 {
5284 assert(!window->loaded);
5285
5286 if (window->config == NULL) {
5287 window->window = SDL_CreateWindow(VERSION_NAME,
5288 window->full_rect.x, window->full_rect.y,
5289 window->full_rect.w, window->full_rect.h,
5290 SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_RESIZABLE);
5291 } else {
5292 window->window = SDL_CreateWindow(VERSION_NAME,
5293 window->full_rect.x, window->full_rect.y,
5294 window->full_rect.w, window->full_rect.h,
5295 window->config->window_flags);
5296 }
5297 assert(window->window != NULL);
5298
5299 if (window->config == NULL) {
5300 window->renderer = SDL_CreateRenderer(window->window,
5301 -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
5302 } else {
5303 /* this is necessary for subwindows to have their own textures */
5304 window->config->renderer_flags |= SDL_RENDERER_TARGETTEXTURE;
5305 window->renderer = SDL_CreateRenderer(window->window,
5306 -1, window->config->renderer_flags);
5307 }
5308 if (window->renderer == NULL) {
5309 quit_fmt("cannot create renderer for window %u: %s",
5310 window->index, SDL_GetError());
5311 }
5312
5313 SDL_RendererInfo info;
5314 if (SDL_GetRendererInfo(window->renderer, &info) != 0) {
5315 quit_fmt("cannot query renderer in window %u", window->index);
5316 }
5317
5318 if (!choose_pixelformat(window, &info)) {
5319 quit_fmt("cannot choose pixelformat for window %u", window->index);
5320 }
5321
5322 load_window(window);
5323
5324 for (size_t i = 0; i < N_ELEMENTS(window->subwindows); i++) {
5325 if (window->subwindows[i] != NULL) {
5326 load_subwindow(window, window->subwindows[i]);
5327 }
5328 }
5329
5330 SDL_SetWindowMinimumSize(window->window,
5331 DEFAULT_WINDOW_MINIMUM_W, DEFAULT_WINDOW_MINIMUM_H);
5332
5333 window->flags = SDL_GetWindowFlags(window->window);
5334 window->id = SDL_GetWindowID(window->window);
5335 }
5336
5337 static void wipe_window_aux_config(struct window *window)
5338 {
5339 window->config = mem_zalloc(sizeof(*window->config));
5340 assert(window->config != NULL);
5341
5342 const struct window *main_window = get_window_direct(MAIN_WINDOW);
5343 assert(main_window != NULL);
5344
5345 SDL_RendererInfo rinfo;
5346 if (SDL_GetRendererInfo(main_window->renderer, &rinfo) != 0) {
5347 quit_fmt("cannot get renderer info for main window: %s", SDL_GetError());
5348 }
5349 window->config->renderer_flags = rinfo.flags;
5350 window->config->renderer_index = -1;
5351
5352 window->config->window_flags= SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
5353
5354 if (main_window->config == NULL) {
5355 char path[4096];
5356 path_build(path, sizeof(path), DEFAULT_WALLPAPER_DIR, DEFAULT_WALLPAPER);
5357 window->config->wallpaper_path = string_make(path);
5358 window->config->font_name = string_make(DEFAULT_STATUS_BAR_FONT);
5359 } else {
5360 window->config->wallpaper_path = string_make(main_window->config->wallpaper_path);
5361 window->config->font_name = string_make(main_window->config->font_name);
5362 window->config->font_size = main_window->config->font_size;
5363 }
5364
5365 int display = SDL_GetWindowDisplayIndex(main_window->window);
5366 if (display < 0) {
5367 quit_fmt("cannot get display from main window: %s", SDL_GetError());
5368 }
5369
5370 /* center it on the screen */
5371 SDL_DisplayMode mode;
5372 SDL_GetCurrentDisplayMode(display, &mode);
5373 window->full_rect.w = mode.w / 2;
5374 window->full_rect.h = mode.h / 2;
5375 window->full_rect.x = mode.w / 4;
5376 window->full_rect.y = mode.h / 4;
5377
5378 if (current_graphics_mode != NULL) {
5379 window->graphics.id = current_graphics_mode->grafID;
5380 }
5381 }
5382
5383 /* initialize window with suitable hardcoded defaults */
5384 static void wipe_window(struct window *window, int display)
5385 {
5386 unsigned index = window->index;
5387 memset(window, 0, sizeof(*window));
5388 window->index = index;
5389
5390 for (size_t j = 0; j < N_ELEMENTS(window->subwindows); j++) {
5391 window->subwindows[j] = NULL;
5392 }
5393
5394 SDL_DisplayMode mode;
5395 if (SDL_GetCurrentDisplayMode(display, &mode) != 0) {
5396 quit_fmt("cannot get display mode for window %u: %s",
5397 window->index, SDL_GetError());
5398 }
5399
5400 window->pixelformat = SDL_PIXELFORMAT_UNKNOWN;
5401
5402 window->full_rect.w = mode.w;
5403 window->full_rect.h = mode.h;
5404
5405 window->color = g_colors[DEFAULT_WINDOW_BG_COLOR];
5406 window->alpha = DEFAULT_ALPHA_FULL;
5407
5408 window->status_bar.font = NULL;
5409
5410 window->wallpaper.texture = NULL;
5411 window->wallpaper.mode = WALLPAPER_TILED;
5412
5413 window->stipple.texture = NULL;
5414
5415 window->status_bar.font = NULL;
5416 window->status_bar.color = g_colors[DEFAULT_STATUS_BAR_BG_COLOR];
5417 window->status_bar.button_bank.buttons = NULL;
5418 window->status_bar.menu_panel = NULL;
5419 window->status_bar.in_menu = false;
5420
5421 window->graphics.texture = NULL;
5422 window->graphics.id = GRAPHICS_NONE;
5423
5424 window->dirty = true;
5425
5426 window->config = NULL;
5427 window->inited = true;
5428 }
5429
5430 static void dump_subwindow(const struct subwindow *subwindow, ang_file *config)
5431 {
5432 #define DUMP_SUBWINDOW(sym, fmt, ...) \
5433 file_putf(config, "subwindow-" sym ":%u:" fmt "\n", subwindow->index, __VA_ARGS__)
5434 DUMP_SUBWINDOW("window", "%u:%d", subwindow->window->index,
5435 (subwindow->visible) ? 1 : 0);
5436 DUMP_SUBWINDOW("full-rect", "%d:%d:%d:%d",
5437 subwindow->full_rect.x, subwindow->full_rect.y,
5438 subwindow->full_rect.w, subwindow->full_rect.h);
5439 DUMP_SUBWINDOW("font", "%d:%s",
5440 subwindow->font->size, subwindow->font->name);
5441 DUMP_SUBWINDOW("borders", "%s",
5442 subwindow->borders.visible ? "true" : "false");
5443 DUMP_SUBWINDOW("top", "%s:%s",
5444 subwindow->top ? "true" : "false",
5445 subwindow->always_top ? "true" : "false");
5446 DUMP_SUBWINDOW("alpha", "%u", subwindow->color.a);
5447 #undef DUMP_SUBWINDOW
5448 file_put(config, "\n");
5449 }
5450
5451 static void dump_window(const struct window *window, ang_file *config)
5452 {
5453 #define DUMP_WINDOW(sym, fmt, ...) \
5454 file_putf(config, "window-" sym ":%u:" fmt "\n", window->index, __VA_ARGS__)
5455 DUMP_WINDOW("display", "%d", SDL_GetWindowDisplayIndex(window->window));
5456
5457 int x;
5458 int y;
5459 SDL_GetWindowPosition(window->window, &x, &y);
5460 DUMP_WINDOW("full-rect", "%d:%d:%d:%d",
5461 x, y, window->full_rect.w, window->full_rect.h);
5462
5463 DUMP_WINDOW("fullscreen", "%s",
5464 (window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) ? "true" : "false");
5465
5466 SDL_RendererInfo rinfo;
5467 SDL_GetRendererInfo(window->renderer, &rinfo);
5468 DUMP_WINDOW("renderer", "%s",
5469 (rinfo.flags & SDL_RENDERER_ACCELERATED) ? "hardware" : "software");
5470
5471 if (window->config) {
5472 DUMP_WINDOW("wallpaper-path", "%s", window->config->wallpaper_path);
5473 } else {
5474 DUMP_WINDOW("wallpaper-path", "%s", "default");
5475 }
5476 DUMP_WINDOW("wallpaper-mode", "%s",
5477 window->wallpaper.mode == WALLPAPER_DONT_SHOW ? "none" :
5478 window->wallpaper.mode == WALLPAPER_TILED ? "tiled" :
5479 window->wallpaper.mode == WALLPAPER_CENTERED ? "centered" :
5480 window->wallpaper.mode == WALLPAPER_SCALED ? "scaled" :
5481 "ERROR");
5482 DUMP_WINDOW("status-bar-font", "%d:%s",
5483 window->status_bar.font->size, window->status_bar.font->name);
5484
5485 DUMP_WINDOW("graphics-id", "%d", window->graphics.id);
5486 DUMP_WINDOW("tile-scale", "width:%d", tile_width);
5487 DUMP_WINDOW("tile-scale", "height:%d", tile_height);
5488 #undef DUMP_WINDOW
5489 file_put(config, "\n");
5490
5491 for (size_t i = 0; i < N_ELEMENTS(window->subwindows); i++) {
5492 struct subwindow *subwindow = window->subwindows[i];
5493 if (subwindow != NULL) {
5494 dump_subwindow(subwindow, config);
5495 }
5496 }
5497 }
5498
5499 static void detach_subwindow_from_window(struct window *window,
5500 struct subwindow *subwindow)
5501 {
5502 assert(subwindow->window == window);
5503
5504 size_t i = 0;
5505 while (i < N_ELEMENTS(window->subwindows) && window->subwindows[i] != subwindow) {
5506 i++;
5507 }
5508 assert(i < N_ELEMENTS(window->subwindows));
5509
5510 window->subwindows[i] = NULL;
5511 }
5512
5513 static void attach_subwindow_to_window(struct window *window,
5514 struct subwindow *subwindow)
5515 {
5516 assert(subwindow->window == window);
5517
5518 size_t i = 0;
5519 while (i < N_ELEMENTS(window->subwindows) && window->subwindows[i] != NULL) {
5520 i++;
5521 }
5522 assert(i < N_ELEMENTS(window->subwindows));
5523
5524 window->subwindows[i] = subwindow;
5525 }
5526
5527 static struct subwindow *make_subwindow(struct window *window, unsigned index)
5528 {
5529 struct subwindow *subwindow = get_new_subwindow(index);
5530 assert(subwindow != NULL);
5531
5532 load_subwindow(window, subwindow);
5533 attach_subwindow_to_window(window, subwindow);
5534 load_term(subwindow);
5535
5536 return subwindow;
5537 }
5538
5539 static struct subwindow *transfer_subwindow(struct window *window, unsigned index)
5540 {
5541 struct subwindow *subwindow = get_subwindow_direct(index);
5542 assert(subwindow != NULL);
5543 assert(subwindow->inited);
5544 assert(subwindow->loaded);
5545
5546 detach_subwindow_from_window(subwindow->window, subwindow);
5547 subwindow->window = window;
5548 attach_subwindow_to_window(window, subwindow);
5549
5550 SDL_DestroyTexture(subwindow->texture);
5551 subwindow->texture = make_subwindow_texture(window,
5552 subwindow->full_rect.w, subwindow->full_rect.h);
5553 assert(subwindow->texture != NULL);
5554
5555 SDL_DestroyTexture(subwindow->aux_texture);
5556 subwindow->aux_texture = make_subwindow_texture(window, 1, 1);
5557 assert(subwindow->aux_texture != NULL);
5558
5559 struct font *new_font = make_font(subwindow->window,
5560 subwindow->font->name, subwindow->font->size);
5561 assert(new_font != NULL);
5562 free_font(subwindow->font);
5563 subwindow->font = new_font;
5564
5565 render_clear(window, subwindow->texture, &subwindow->color);
5566
5567 subwindow->borders.error = false;
5568 render_borders(subwindow);
5569
5570 fit_subwindow_in_window(window, subwindow);
5571
5572 return subwindow;
5573 }
5574
5575 static void load_subwindow(struct window *window, struct subwindow *subwindow)
5576 {
5577 assert(window->loaded);
5578 assert(!subwindow->loaded);
5579
5580 if (subwindow->font == NULL) {
5581 if (subwindow->config != NULL) {
5582 subwindow->font = make_font(window,
5583 subwindow->config->font_name, subwindow->config->font_size);
5584 } else {
5585 subwindow->font = make_font(window, DEFAULT_FONT, 0);
5586 }
5587 assert(subwindow->font != NULL);
5588 }
5589 if (!adjust_subwindow_geometry(window, subwindow)) {
5590 quit_fmt("cannot adjust geometry of subwindow %u in window %u",
5591 subwindow->index, window->index);
5592 }
5593 subwindow->texture = make_subwindow_texture(window,
5594 subwindow->full_rect.w, subwindow->full_rect.h);
5595 assert(subwindow->texture != NULL);
5596
5597 /* just a pixel for sizing rect */
5598 subwindow->aux_texture = make_subwindow_texture(window, 1, 1);
5599 assert(subwindow->aux_texture != NULL);
5600
5601 /* same testing sequence as for status bar */
5602 if (SDL_SetRenderDrawColor(window->renderer,
5603 subwindow->color.r, subwindow->color.g,
5604 subwindow->color.b, subwindow->color.a) != 0)
5605 {
5606 quit_fmt("cannot set draw color for subwindow %u window %u: %s",
5607 subwindow->index, window->index, SDL_GetError());
5608 }
5609 if (SDL_SetRenderTarget(window->renderer, subwindow->texture) != 0) {
5610 quit_fmt("cannot set subwindow %u as render target in window %u: %s",
5611 subwindow->index, window->index, SDL_GetError());
5612 }
5613 if (SDL_RenderClear(window->renderer) != 0) {
5614 quit_fmt("cannot clear texture in subwindow %u window %u: %s",
5615 subwindow->index, window->index, SDL_GetError());
5616 }
5617
5618 subwindow->window = window;
5619 subwindow->loaded = true;
5620
5621 render_borders(subwindow);
5622 }
5623
5624 static void clear_pw_flag(struct subwindow *subwindow)
5625 {
5626 assert(subwindow->index < N_ELEMENTS(angband_term));
5627
5628 uint32_t new_flags[N_ELEMENTS(window_flag)];
5629 assert(sizeof(new_flags) == sizeof(window_flag));
5630 memcpy(new_flags, window_flag, sizeof(new_flags));
5631
5632 assert(subwindow->index < N_ELEMENTS(new_flags));
5633 new_flags[subwindow->index] = 0;
5634
5635 subwindows_set_flags(new_flags, N_ELEMENTS(new_flags));
5636 }
5637
5638 static void unload_term(struct subwindow *subwindow)
5639 {
5640 if (!subwindow->linked) {
5641 return;
5642 }
5643
5644 term_nuke(subwindow->term);
5645 mem_free(subwindow->term);
5646 angband_term[subwindow->index] = NULL;
5647
5648 subwindow->term = NULL;
5649 subwindow->linked = false;
5650 }
5651
5652 static void link_term(struct subwindow *subwindow)
5653 {
5654 assert(subwindow->term != NULL);
5655 assert(subwindow->index < N_ELEMENTS(angband_term));
5656 assert(angband_term[subwindow->index] == NULL);
5657
5658 subwindow->term->higher_pict = true;
5659 subwindow->term->soft_cursor = true;
5660 subwindow->term->complex_input = true;
5661 subwindow->term->never_frosh = true;
5662
5663 subwindow->term->xtra_hook = term_xtra_hook;
5664 subwindow->term->curs_hook = term_curs_hook;
5665 subwindow->term->bigcurs_hook = term_bigcurs_hook;
5666 subwindow->term->wipe_hook = term_wipe_hook;
5667 subwindow->term->text_hook = term_text_hook;
5668 subwindow->term->pict_hook = term_pict_hook;
5669 subwindow->term->view_map_hook = term_view_map_hook;
5670 if (subwindow->window->graphics.overdraw_row) {
5671 subwindow->term->dblh_hook = is_dh_tile;
5672 } else {
5673 subwindow->term->dblh_hook = NULL;
5674 }
5675
5676 subwindow->term->data = subwindow;
5677 angband_term[subwindow->index] = subwindow->term;
5678 }
5679
5680 static void load_term(struct subwindow *subwindow)
5681 {
5682 assert(!subwindow->linked);
5683
5684 subwindow->term = mem_zalloc(sizeof(*subwindow->term));
5685
5686 term_init(subwindow->term,
5687 subwindow->cols, subwindow->rows,
5688 SUBWINDOW_KEYQ_SIZE(subwindow));
5689
5690 link_term(subwindow);
5691
5692 term *old = Term;
5693 Term_activate(subwindow->term);
5694 Term_redraw();
5695 Term_activate(old);
5696
5697 subwindow->linked = true;
5698 }
5699
5700 /* initialize subwindow with suitable hardcoded defaults */
5701 static bool wipe_subwindow(struct subwindow *subwindow)
5702 {
5703 unsigned index = subwindow->index;
5704 memset(subwindow, 0, sizeof(*subwindow));
5705 subwindow->index = index;
5706
5707 /* XXX 80x24 is essential for main */
5708 subwindow->full_rect.w = MIN_COLS_MAIN * DEFAULT_FONT_W + DEFAULT_BORDER * 2;
5709 subwindow->full_rect.h = MIN_ROWS_MAIN * DEFAULT_FONT_H + DEFAULT_BORDER * 2;
5710 if (subwindow->index != MAIN_SUBWINDOW) {
5711 subwindow->full_rect.w /= 2;
5712 subwindow->full_rect.h /= 2;
5713 }
5714 subwindow->cols = MIN_COLS_MAIN;
5715 subwindow->rows = MIN_ROWS_MAIN;
5716
5717 subwindow->color = g_colors[DEFAULT_SUBWINDOW_BG_COLOR];
5718 subwindow->borders.color = g_colors[DEFAULT_SUBWINDOW_BORDER_COLOR];
5719 subwindow->borders.visible = true;
5720
5721 subwindow->texture = NULL;
5722 subwindow->aux_texture = NULL;
5723 subwindow->window = NULL;
5724 subwindow->font = NULL;
5725
5726 subwindow->term = NULL;
5727
5728 subwindow->config = NULL;
5729 subwindow->inited = true;
5730 subwindow->visible = true;
5731
5732 return true;
5733 }
5734
5735 static void get_string_metrics(struct font *font, const char *str, int *w, int *h)
5736 {
5737 assert(font != NULL);
5738 assert(font->ttf.handle != NULL);
5739
5740 if (TTF_SizeUTF8(font->ttf.handle, str, w, h) != 0) {
5741 quit_fmt("cannot get string metrics for string '%s': %s", str, TTF_GetError());
5742 }
5743 }
5744
5745 static int sort_cb_font_info(const void *infoa, const void *infob)
5746 {
5747 int typea = ((struct font_info *) infoa)->type;
5748 int typeb = ((struct font_info *) infob)->type;
5749
5750 const char *namea = ((struct font_info *) infoa)->name;
5751 const char *nameb = ((struct font_info *) infob)->name;
5752
5753 if (typea != typeb) {
5754 /* raster (angband's .fon) fonts go last */
5755 return typea == FONT_TYPE_RASTER ? 1 : -1;
5756 } else if (typea == FONT_TYPE_VECTOR && typeb == FONT_TYPE_VECTOR) {
5757 /* vector (.ttf, etc) fonts go in alphabetical order */
5758 return strcmp(namea, nameb);
5759 } else {
5760 /* otherwise, we'll sort them numbers-wise (6x12x.fon before 6x13x.fon) */
5761 int wa = 0, ha = 0, wb = 0, hb = 0;
5762 char *facea = NULL, *exta = NULL, *faceb = NULL, *extb = NULL;
5763 char *ew;
5764 long lv;
5765
5766 lv = strtol(namea, &ew, 10);
5767 if (ew != namea && *ew == 'x' && lv > INT_MIN && lv < INT_MAX) {
5768 wa = (int)lv;
5769 lv = strtol(ew + 1, &facea, 10);
5770 if (facea != ew + 1 && lv > INT_MIN && lv < INT_MAX) {
5771 ha = (int)lv;
5772 exta = strchr(namea, '.');
5773 if (exta == namea) {
5774 exta = NULL;
5775 }
5776 }
5777 }
5778 lv = strtol(nameb, &ew, 10);
5779 if (ew != nameb && *ew == 'x' && lv > INT_MIN && lv < INT_MAX) {
5780 wb = (int)lv;
5781 lv = strtol(ew + 1, &faceb, 10);
5782 if (faceb != ew + 1 && lv > INT_MIN && lv < INT_MAX) {
5783 hb = (int)lv;
5784 extb = strchr(faceb, '.');
5785 if (extb == faceb) {
5786 extb = NULL;
5787 }
5788 }
5789 }
5790
5791 if (!exta) {
5792 if (!extb) {
5793 /*
5794 * Neither match the expected pattern. Sort
5795 * alphabetically.
5796 */
5797 return strcmp(namea, nameb);
5798 }
5799 /* Put b last since it matches the expected pattern. */
5800 return -1;
5801 }
5802 if (!extb) {
5803 /* Put a last since it matches the expected pattern. */
5804 return 1;
5805 }
5806 if (wa < wb) {
5807 return -1;
5808 }
5809 if (wa > wb) {
5810 return 1;
5811 }
5812 if (ha < hb) {
5813 return -1;
5814 }
5815 if (ha > hb) {
5816 return 1;
5817 }
5818 return strncmp(facea, faceb, MAX(exta - facea, extb - faceb));
5819 }
5820 }
5821
5822 static bool is_font_file(const char *path)
5823 {
5824 bool is_font = false;
5825
5826 TTF_Font *font = TTF_OpenFont(path, 1);
5827
5828 if (font != NULL) {
5829 if (TTF_FontFaceIsFixedWidth(font)) {
5830 is_font = true;
5831 }
5832 TTF_CloseFont(font);
5833 }
5834
5835 return is_font;
5836 }
5837
5838 static void free_menu_panel(struct menu_panel *menu_panel)
5839 {
5840 while (menu_panel) {
5841 struct menu_panel *next = menu_panel->next;
5842 free_button_bank(&menu_panel->button_bank);
5843 mem_free(menu_panel);
5844 menu_panel = next;
5845 }
5846 }
5847
5848 static void free_button_bank(struct button_bank *button_bank)
5849 {
5850 for (size_t i = 0; i < button_bank->number; i++) {
5851 mem_free(button_bank->buttons[i].caption);
5852 }
5853 mem_free(button_bank->buttons);
5854 button_bank->buttons = NULL;
5855 button_bank->number = 0;
5856 button_bank->size = 0;
5857 }
5858
5859 static void free_status_bar(struct status_bar *status_bar)
5860 {
5861 if (status_bar->menu_panel != NULL) {
5862 free_menu_panel(status_bar->menu_panel);
5863 status_bar->menu_panel = NULL;
5864 }
5865 if (status_bar->button_bank.buttons != NULL) {
5866 free_button_bank(&status_bar->button_bank);
5867 }
5868 if (status_bar->texture != NULL) {
5869 SDL_DestroyTexture(status_bar->texture);
5870 status_bar->texture = NULL;
5871 }
5872
5873 free_font(status_bar->font);
5874 status_bar->font = NULL;
5875 }
5876
5877 static void free_font_info(struct font_info *font_info)
5878 {
5879 if (font_info->name != NULL) {
5880 mem_free(font_info->name);
5881 font_info->name = NULL;
5882 }
5883 if (font_info->path != NULL) {
5884 mem_free(font_info->path);
5885 font_info->path = NULL;
5886 }
5887 font_info->loaded = false;
5888 }
5889
5890 static void free_window_config(struct window_config *config)
5891 {
5892 if (config->wallpaper_path != NULL) {
5893 mem_free(config->wallpaper_path);
5894 }
5895 if (config->font_name != NULL) {
5896 mem_free(config->font_name);
5897 }
5898 mem_free(config);
5899 }
5900
5901 static void free_graphics(struct graphics *graphics)
5902 {
5903 if (graphics->texture != NULL) {
5904 SDL_DestroyTexture(graphics->texture);
5905 graphics->texture = NULL;
5906 }
5907 }
5908
5909 static void free_subwindow_config(struct subwindow_config *config)
5910 {
5911 if (config->font_name != NULL) {
5912 mem_free(config->font_name);
5913 }
5914 mem_free(config);
5915 }
5916
5917 static void free_subwindow(struct subwindow *subwindow)
5918 {
5919 assert(subwindow->loaded);
5920
5921 free_font(subwindow->font);
5922 subwindow->font = NULL;
5923 if (subwindow->texture != NULL) {
5924 SDL_DestroyTexture(subwindow->texture);
5925 subwindow->texture = NULL;
5926 }
5927 if (subwindow->aux_texture != NULL) {
5928 SDL_DestroyTexture(subwindow->aux_texture);
5929 subwindow->aux_texture = NULL;
5930 }
5931 if (subwindow->term != NULL) {
5932 unload_term(subwindow);
5933 }
5934 if (subwindow->config != NULL) {
5935 free_subwindow_config(subwindow->config);
5936 subwindow->config = NULL;
5937 }
5938
5939 subwindow->window = NULL;
5940 subwindow->loaded = false;
5941 subwindow->inited = false;
5942 }
5943
5944 static void free_window(struct window *window)
5945 {
5946 assert(window->loaded);
5947
5948 for (size_t i = 0; i < N_ELEMENTS(window->subwindows); i++) {
5949 struct subwindow *subwindow = window->subwindows[i];
5950 if (subwindow != NULL) {
5951 free_subwindow(subwindow);
5952 window->subwindows[i] = NULL;
5953 }
5954 }
5955
5956 free_status_bar(&window->status_bar);
5957
5958 if (window->wallpaper.texture != NULL) {
5959 SDL_DestroyTexture(window->wallpaper.texture);
5960 window->wallpaper.texture = NULL;
5961 }
5962
5963 if (window->stipple.texture != NULL) {
5964 SDL_DestroyTexture(window->stipple.texture);
5965 window->stipple.texture = NULL;
5966 }
5967
5968 free_graphics(&window->graphics);
5969
5970 if (window->renderer != NULL) {
5971 SDL_DestroyRenderer(window->renderer);
5972 window->renderer = NULL;
5973 }
5974
5975 if (window->window != NULL) {
5976 SDL_DestroyWindow(window->window);
5977 window->window = NULL;
5978 }
5979
5980 if (window->config != NULL) {
5981 free_window_config(window->config);
5982 window->config = NULL;
5983 }
5984
5985 window->loaded = false;
5986 window->inited = false;
5987 }
5988
5989 static void init_colors(void)
5990 {
5991 assert(N_ELEMENTS(g_colors) == N_ELEMENTS(angband_color_table));
5992 size_t i;
5993
5994 for (i = 0; i < N_ELEMENTS(g_colors); i++) {
5995 g_colors[i].r = angband_color_table[i][1];
5996 g_colors[i].g = angband_color_table[i][2];
5997 g_colors[i].b = angband_color_table[i][3];
5998 g_colors[i].a = DEFAULT_ALPHA_FULL;
5999 }
6000 for (i = 0; i < N_ELEMENTS(g_windows); i++) {
6001 g_windows[i].color = g_colors[DEFAULT_WINDOW_BG_COLOR];
6002 }
6003 for (i = 0; i < N_ELEMENTS(g_subwindows); i++) {
6004 /* Retain whatever customized alpha the subwindow has. */
6005 g_subwindows[i].color.r =
6006 g_colors[DEFAULT_SUBWINDOW_BG_COLOR].r;
6007 g_subwindows[i].color.g =
6008 g_colors[DEFAULT_SUBWINDOW_BG_COLOR].g;
6009 g_subwindows[i].color.b =
6010 g_colors[DEFAULT_SUBWINDOW_BG_COLOR].b;
6011 }
6012 }
6013
6014 static void init_font_info(const char *directory)
6015 {
6016 /* well maybe it will get ability to be reinitialized at some point */
6017 for (size_t i = 0; i < N_ELEMENTS(g_font_info); i++) {
6018 g_font_info[i].name = NULL;
6019 g_font_info[i].path = NULL;
6020 g_font_info[i].loaded = false;
6021 }
6022
6023 ang_dir *dir = my_dopen(directory);
6024 assert(dir != NULL);
6025
6026 char name[1024];
6027 char path[4096];
6028
6029 size_t i = 0;
6030 while (i < N_ELEMENTS(g_font_info) && my_dread(dir, name, sizeof(name))) {
6031 path_build(path, sizeof(path), directory, name);
6032
6033 if (is_font_file(path)) {
6034 g_font_info[i].name = string_make(name);
6035 g_font_info[i].path = string_make(path);
6036 g_font_info[i].loaded = true;
6037 if (suffix_i(path, ".fon")) {
6038 g_font_info[i].type = FONT_TYPE_RASTER;
6039 g_font_info[i].size = 0;
6040 } else {
6041 g_font_info[i].type = FONT_TYPE_VECTOR;
6042 g_font_info[i].size = DEFAULT_VECTOR_FONT_SIZE;
6043 }
6044 i++;
6045 }
6046 }
6047 assert(i > 0);
6048
6049 sort(g_font_info, i, sizeof(g_font_info[0]), sort_cb_font_info);
6050
6051 for (size_t j = 0; j < i; j++) {
6052 g_font_info[j].index = j;
6053 }
6054
6055 my_dclose(dir);
6056 }
6057
6058 static void create_defaults(void)
6059 {
6060 struct window *window = get_new_window(MAIN_WINDOW);
6061 assert(window != NULL);
6062
6063 struct subwindow *subwindow = get_new_subwindow(MAIN_SUBWINDOW);
6064 assert(subwindow != NULL);
6065
6066 assert(MAIN_SUBWINDOW < N_ELEMENTS(window->subwindows));
6067 window->subwindows[MAIN_SUBWINDOW] = subwindow;
6068 }
6069
6070 static void quit_systems(void)
6071 {
6072 SDL_StopTextInput();
6073 TTF_Quit();
6074 IMG_Quit();
6075 SDL_Quit();
6076 }
6077
6078 static void quit_hook(const char *s)
6079 {
6080 dump_config_file();
6081
6082 free_globals();
6083 quit_systems();
6084 }
6085
6086 static void init_systems(void)
6087 {
6088 if (SDL_Init(INIT_SDL_FLAGS) != 0) {
6089 quit_fmt("SDL_Init: %s", SDL_GetError());
6090 }
6091 if (IMG_Init(INIT_IMG_FLAGS) != INIT_IMG_FLAGS) {
6092 quit_fmt("IMG_Init: %s", IMG_GetError());
6093 SDL_Quit();
6094 }
6095 if (TTF_Init() != 0) {
6096 quit_fmt("TTF_Init: %s", TTF_GetError());
6097 }
6098
6099 /* On (some?) Macs the touchpad sends both mouse events and touch events;
6100 * the latter interfere badly with the working of the status bar */
6101 SDL_EventState(SDL_FINGERMOTION, SDL_DISABLE);
6102 SDL_EventState(SDL_FINGERDOWN, SDL_DISABLE);
6103 SDL_EventState(SDL_FINGERUP, SDL_DISABLE);
6104 /* Ignore Keymap changed events since they are not handled */
6105 SDL_EventState(SDL_KEYMAPCHANGED, SDL_DISABLE);
6106
6107 SDL_StartTextInput();
6108 SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
6109 #ifdef SDL_HINT_POLL_SENTINEL
6110 SDL_SetHint(SDL_HINT_POLL_SENTINEL, "0");
6111 #endif
6112 }
6113
6114 errr init_sdl2(int argc, char **argv)
6115 {
6116 init_systems();
6117 init_globals();
6118
6119 if (!init_graphics_modes()) {
6120 quit_systems();
6121 return 1;
6122 }
6123
6124 if (!read_config_file()) {
6125 create_defaults();
6126 }
6127
6128 start_windows();
6129 load_terms();
6130
6131 #ifdef MSYS2_ENCODING_WORKAROUND
6132 /*
6133 * Under MSYS2, mbcstowcs() converts UTF-8 by outputting a wchar_t
6134 * (a 16-bit quantity) for every byte in the UTF-8 sequence. For most
6135 * bytes in the UTF-8 sequence, the corresponding wchar_t has the same
6136 * value as the byte but some are altered. For instance the UTF-8
6137 * sequence 0x24, 0xC2, 0xA2, 0xE2, 0x82, 0xAC, 0xF0, 0x90, 0x8D, 0x88,
6138 * 0xF0, 0x90, 0x91, and 0x99 which represents U+0024, U+00A2, U+20AC,
6139 * U+10348, and U+10459 becomes 0x0024, 0x00C2, 0x00A2, 0x00E2, 0x201A,
6140 * 0x00AC, 0x00F0, 0x0090, 0x008D, 0x02C6, 0x00F0, 0x0090, 0x2018,
6141 * and 0x2122. Override that to use a UTF-16 encoding for the
6142 * wchar_t's where any codepoint that would require a surrogate pair
6143 * is lossily converted to U+FFFD since ui-term.c only allows storage
6144 * for one wchar_t per grid location.
6145 */
6146 text_mbcs_hook = term_mbcs_sdl2_msys2;
6147 text_wctomb_hook = term_wctomb_sdl2_msys2;
6148 text_wcsz_hook = term_wcsz_sdl2_msys2;
6149 text_iswprint_hook = term_iswprint_sdl2_msys2;
6150 #endif /* MSYS2_ENCODING_WORKAROUND */
6151
6152 quit_aux = quit_hook;
6153
6154 return 0;
6155 }
6156
6157 /* the string ANGBAND_DIR_USER is freed before calling quit_hook(),
6158 * so we need to save the path to config file here */
6159 static char g_config_file[4096];
6160
6161 static void init_globals(void)
6162 {
6163 for (size_t i = 0; i < N_ELEMENTS(g_subwindows); i++) {
6164 g_subwindows[i].index = i;
6165 }
6166 for (size_t i = 0; i < N_ELEMENTS(g_windows); i++) {
6167 g_windows[i].index = i;
6168 }
6169
6170 init_font_info(ANGBAND_DIR_FONTS);
6171 init_colors();
6172
6173 path_build(g_config_file, sizeof(g_config_file),
6174 DEFAULT_CONFIG_FILE_DIR, DEFAULT_CONFIG_FILE);
6175 }
6176
6177 static bool is_subwindow_loaded(unsigned index)
6178 {
6179 const struct subwindow *subwindow = get_subwindow_direct(index);
6180 assert(subwindow != NULL);
6181
6182 return subwindow->loaded;
6183 }
6184
6185 static struct subwindow *get_subwindow_direct(unsigned index)
6186 {
6187 size_t i;
6188 if (index < N_ELEMENTS(g_subwindows)
6189 && g_subwindows[index].index == index)
6190 {
6191 i = index;
6192 } else {
6193 for (i = 0; i < N_ELEMENTS(g_subwindows); i++) {
6194 if (g_subwindows[i].index == index) {
6195 break;
6196 }
6197 }
6198 if (i == N_ELEMENTS(g_subwindows)) {
6199 return NULL;
6200 }
6201 }
6202
6203 return &g_subwindows[i];
6204 }
6205
6206 static struct subwindow *get_new_subwindow(unsigned index)
6207 {
6208 struct subwindow *subwindow = get_subwindow_direct(index);
6209 assert(subwindow != NULL);
6210 assert(!subwindow->inited);
6211 assert(!subwindow->loaded);
6212 assert(!subwindow->linked);
6213
6214 if (!wipe_subwindow(subwindow)) {
6215 return NULL;
6216 }
6217
6218 return subwindow;
6219 }
6220
6221 static struct window *get_new_window(unsigned index)
6222 {
6223 assert(index < N_ELEMENTS(g_windows));
6224 assert(g_windows[index].index == index);
6225
6226 struct window *window = &g_windows[index];
6227 assert(!window->inited);
6228 assert(!window->loaded);
6229
6230 wipe_window(window, DEFAULT_DISPLAY);
6231
6232 return window;
6233 }
6234
6235 static struct window *get_window_direct(unsigned index)
6236 {
6237 assert(index < N_ELEMENTS(g_windows));
6238
6239 if (g_windows[index].loaded) {
6240 assert(g_windows[index].index == index);
6241 return &g_windows[index];
6242 }
6243
6244 return NULL;
6245 }
6246
6247 static struct window *get_window_by_id(Uint32 id)
6248 {
6249 for (size_t i = 0; i < N_ELEMENTS(g_windows); i++) {
6250 if (g_windows[i].loaded && g_windows[i].id == id) {
6251 return &g_windows[i];
6252 }
6253 }
6254
6255 return NULL;
6256 }
6257
6258 static void free_globals(void)
6259 {
6260 for (size_t i = 0; i < N_ELEMENTS(g_font_info); i++) {
6261 free_font_info(&g_font_info[i]);
6262 }
6263 for (size_t i = 0; i < N_ELEMENTS(g_windows); i++) {
6264 if (g_windows[i].loaded) {
6265 free_window(&g_windows[i]);
6266 }
6267 }
6268 for (size_t i = 0; i < N_ELEMENTS(g_subwindows); i++) {
6269 assert(!g_subwindows[i].inited);
6270 assert(!g_subwindows[i].loaded);
6271 assert(!g_subwindows[i].linked);
6272 }
6273 }
6274
6275 static void start_windows(void)
6276 {
6277 for (size_t i = N_ELEMENTS(g_windows); i > 0; i--) {
6278 if (g_windows[i - 1].inited) {
6279 start_window(&g_windows[i - 1]);
6280 }
6281 }
6282 }
6283
6284 static void load_terms(void)
6285 {
6286 for (size_t i = 0; i < N_ELEMENTS(g_subwindows); i++) {
6287 if (g_subwindows[i].loaded) {
6288 load_term(&g_subwindows[i]);
6289 }
6290 }
6291
6292 Term_activate(term_screen);
6293 }
6294
6295 /* Config file stuff */
6296
6297 static void dump_config_file(void)
6298 {
6299 ang_file *config = file_open(g_config_file, MODE_WRITE, FTYPE_TEXT);
6300
6301 assert(config != NULL);
6302
6303 for (size_t i = 0; i < N_ELEMENTS(g_windows); i++) {
6304 if (g_windows[i].loaded) {
6305 dump_window(&g_windows[i], config);
6306 }
6307 }
6308 file_putf(config, "kp-as-modifier:%d\n", (g_kp_as_mod) ? 1 : 0);
6309
6310 file_close(config);
6311 }
6312
6313 /* XXX more bad style :) */
6314 #define GET_WINDOW_FROM_INDEX \
6315 if (parser_getuint(parser, "index") >= N_ELEMENTS(g_windows)) { \
6316 return PARSE_ERROR_OUT_OF_BOUNDS; \
6317 } \
6318 struct window *window = &g_windows[parser_getuint(parser, "index")]; \
6319
6320 #define WINDOW_INIT_OK \
6321 if (!window->inited) { \
6322 return PARSE_ERROR_MISSING_RECORD_HEADER; \
6323 }
6324
6325 #define GET_SUBWINDOW_FROM_INDEX \
6326 if (parser_getuint(parser, "index") >= N_ELEMENTS(g_subwindows)) { \
6327 return PARSE_ERROR_OUT_OF_BOUNDS; \
6328 } \
6329 struct subwindow *subwindow = &g_subwindows[parser_getuint(parser, "index")]; \
6330
6331 #define SUBWINDOW_INIT_OK \
6332 if (!subwindow->inited) { \
6333 return PARSE_ERROR_MISSING_RECORD_HEADER; \
6334 }
6335
6336 static enum parser_error config_window_display(struct parser *parser)
6337 {
6338 GET_WINDOW_FROM_INDEX;
6339
6340 int display = parser_getint(parser, "display");
6341
6342 if (display < 0 || display > SDL_GetNumVideoDisplays()) {
6343 return PARSE_ERROR_OUT_OF_BOUNDS;
6344 }
6345
6346 wipe_window(window, display);
6347
6348 window->config = mem_zalloc(sizeof(*window->config));
6349
6350 window->config->window_flags = SDL_WINDOW_RESIZABLE;
6351
6352 return PARSE_ERROR_NONE;
6353 }
6354
6355 static enum parser_error config_window_fullscreen(struct parser *parser)
6356 {
6357 GET_WINDOW_FROM_INDEX;
6358 WINDOW_INIT_OK;
6359
6360 const char *mode = parser_getsym(parser, "fullscreen");
6361 if (streq(mode, "true")) {
6362 window->config->window_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
6363 } else if (streq(mode, "false")) {
6364 ;
6365 } else {
6366 return PARSE_ERROR_INVALID_VALUE;
6367 }
6368 return PARSE_ERROR_NONE;
6369 }
6370
6371 static enum parser_error config_window_rect(struct parser *parser)
6372 {
6373 GET_WINDOW_FROM_INDEX;
6374 WINDOW_INIT_OK;
6375 window->full_rect.x = parser_getint(parser, "x");
6376 window->full_rect.y = parser_getint(parser, "y");
6377 window->full_rect.w = parser_getint(parser, "w");
6378 window->full_rect.h = parser_getint(parser, "h");
6379
6380 return PARSE_ERROR_NONE;
6381 }
6382
6383 static enum parser_error config_window_renderer(struct parser *parser)
6384 {
6385 GET_WINDOW_FROM_INDEX;
6386 WINDOW_INIT_OK;
6387 const char *type = parser_getsym(parser, "type");
6388
6389 if (streq(type, "hardware")) {
6390 window->config->renderer_flags = SDL_RENDERER_ACCELERATED;
6391 } else if (streq(type, "software")) {
6392 window->config->renderer_flags = SDL_RENDERER_SOFTWARE;
6393 } else {
6394 return PARSE_ERROR_INVALID_VALUE;
6395 }
6396
6397 return PARSE_ERROR_NONE;
6398 }
6399
6400 static enum parser_error config_window_wallpaper_path(struct parser *parser)
6401 {
6402 GET_WINDOW_FROM_INDEX;
6403 WINDOW_INIT_OK;
6404 const char *path = parser_getstr(parser, "path");
6405 if (streq(path, "default")) {
6406 char buf[4096];
6407 path_build(buf, sizeof(buf), DEFAULT_WALLPAPER_DIR, DEFAULT_WALLPAPER);
6408
6409 window->config->wallpaper_path = string_make(buf);
6410 } else {
6411 window->config->wallpaper_path = string_make(path);
6412 }
6413
6414 return PARSE_ERROR_NONE;
6415 }
6416
6417 static enum parser_error config_window_wallpaper_mode(struct parser *parser)
6418 {
6419 GET_WINDOW_FROM_INDEX;
6420 WINDOW_INIT_OK;
6421 const char *mode = parser_getstr(parser, "mode");
6422
6423 if (streq(mode, "none")) {
6424 window->wallpaper.mode = WALLPAPER_DONT_SHOW;
6425 } else if (streq(mode, "tiled")) {
6426 window->wallpaper.mode = WALLPAPER_TILED;
6427 } else if (streq(mode, "centered")) {
6428 window->wallpaper.mode = WALLPAPER_CENTERED;
6429 } else if (streq(mode, "scaled")) {
6430 window->wallpaper.mode = WALLPAPER_SCALED;
6431 } else {
6432 return PARSE_ERROR_INVALID_VALUE;
6433 }
6434
6435 return PARSE_ERROR_NONE;
6436 }
6437
6438 static enum parser_error config_window_font(struct parser *parser)
6439 {
6440 GET_WINDOW_FROM_INDEX;
6441 WINDOW_INIT_OK;
6442 const char *name = parser_getstr(parser, "name");
6443 int size = parser_getint(parser, "size");
6444
6445 if (find_font_info(name) == NULL) {
6446 return PARSE_ERROR_INVALID_VALUE;
6447 }
6448 window->config->font_name = string_make(name);
6449 window->config->font_size = size;
6450
6451 return PARSE_ERROR_NONE;
6452 }
6453
6454 static enum parser_error config_window_graphics(struct parser *parser)
6455 {
6456 GET_WINDOW_FROM_INDEX;
6457 WINDOW_INIT_OK;
6458
6459 int id = parser_getint(parser, "id");
6460
6461 if (get_graphics_mode(id) == NULL) {
6462 return PARSE_ERROR_INVALID_VALUE;
6463 }
6464
6465 window->graphics.id = id;
6466
6467 return PARSE_ERROR_NONE;
6468 }
6469
6470 static enum parser_error config_window_tile_scale(struct parser *parser)
6471 {
6472 GET_WINDOW_FROM_INDEX;
6473 WINDOW_INIT_OK;
6474
6475 if (window->graphics.id != GRAPHICS_NONE) {
6476 if (streq(parser_getsym(parser, "which"), "height")) {
6477 tile_height = parser_getint(parser, "scale");
6478 } else if (streq(parser_getsym(parser, "which"), "width")) {
6479 tile_width = parser_getint(parser, "scale");
6480 } else {
6481 return PARSE_ERROR_INVALID_OPTION;
6482 }
6483 }
6484
6485 return PARSE_ERROR_NONE;
6486 }
6487
6488 static enum parser_error config_subwindow_window(struct parser *parser)
6489 {
6490 GET_SUBWINDOW_FROM_INDEX;
6491
6492 unsigned windex = parser_getuint(parser, "windex");
6493 if (windex >= N_ELEMENTS(g_windows)) {
6494 return PARSE_ERROR_OUT_OF_BOUNDS;
6495 }
6496 struct window *window = &g_windows[windex];
6497 if (!window->inited) {
6498 return PARSE_ERROR_NON_SEQUENTIAL_RECORDS;
6499 }
6500 if (!wipe_subwindow(subwindow)) {
6501 return PARSE_ERROR_GENERIC;
6502 }
6503 subwindow->config = mem_zalloc(sizeof(*subwindow->config));
6504 /*
6505 * Old versions only wrote visible subwindows to the configuration
6506 * file and did not append the visibility status to the subwindow-window
6507 * directive.
6508 */
6509 if (parser_hasval(parser, "vis")) {
6510 subwindow->visible = (parser_getint(parser, "vis") != 0);
6511 }
6512 subwindow->window = window;
6513 attach_subwindow_to_window(window, subwindow);
6514
6515 return PARSE_ERROR_NONE;
6516 }
6517
6518 static enum parser_error config_subwindow_rect(struct parser *parser)
6519 {
6520 GET_SUBWINDOW_FROM_INDEX;
6521 SUBWINDOW_INIT_OK;
6522
6523 subwindow->full_rect.x = parser_getint(parser, "x");
6524 subwindow->full_rect.y = parser_getint(parser, "y");
6525 subwindow->full_rect.w = parser_getint(parser, "w");
6526 subwindow->full_rect.h = parser_getint(parser, "h");
6527
6528 return PARSE_ERROR_NONE;
6529 }
6530
6531 static enum parser_error config_subwindow_font(struct parser *parser)
6532 {
6533 GET_SUBWINDOW_FROM_INDEX;
6534 SUBWINDOW_INIT_OK;
6535
6536 const char *name = parser_getstr(parser, "name");
6537 int size = parser_getint(parser, "size");
6538
6539 if (find_font_info(name) == NULL) {
6540 /* TODO maybe its not really an error? the font file was
6541 * probably just deleted and now the ui wont event start... */
6542 return PARSE_ERROR_INVALID_VALUE;
6543 }
6544 subwindow->config->font_name = string_make(name);
6545 subwindow->config->font_size = size;
6546
6547 return PARSE_ERROR_NONE;
6548 }
6549
6550 static enum parser_error config_subwindow_borders(struct parser *parser)
6551 {
6552 GET_SUBWINDOW_FROM_INDEX;
6553 SUBWINDOW_INIT_OK;
6554
6555 const char *borders = parser_getsym(parser, "borders");
6556 if (streq(borders, "true")) {
6557 subwindow->borders.visible = true;
6558 } else if (streq(borders, "false")) {
6559 subwindow->borders.visible = false;
6560 } else {
6561 return PARSE_ERROR_INVALID_VALUE;
6562 }
6563
6564 return PARSE_ERROR_NONE;
6565 }
6566
6567 static enum parser_error config_subwindow_top(struct parser *parser)
6568 {
6569 GET_SUBWINDOW_FROM_INDEX;
6570 SUBWINDOW_INIT_OK;
6571
6572 const char *top = parser_getsym(parser, "top");
6573 if (streq(top, "true")) {
6574 subwindow->top = true;
6575 } else if (streq(top, "false")) {
6576 subwindow->top = false;
6577 } else {
6578 return PARSE_ERROR_INVALID_VALUE;
6579 }
6580
6581 const char *always = parser_getsym(parser, "always");
6582 if (streq(always, "true")) {
6583 subwindow->always_top = true;
6584 } else if (streq(always, "false")) {
6585 subwindow->always_top = false;
6586 } else {
6587 return PARSE_ERROR_INVALID_VALUE;
6588 }
6589
6590 return PARSE_ERROR_NONE;
6591 }
6592
6593 static enum parser_error config_subwindow_alpha(struct parser *parser)
6594 {
6595 GET_SUBWINDOW_FROM_INDEX;
6596 SUBWINDOW_INIT_OK;
6597
6598 int alpha = parser_getint(parser, "alpha");
6599
6600 if (alpha < 0 || alpha > DEFAULT_ALPHA_FULL) {
6601 return PARSE_ERROR_INVALID_VALUE;
6602 }
6603
6604 subwindow->color.a = alpha;
6605
6606 return PARSE_ERROR_NONE;
6607 }
6608 #undef GET_WINDOW_FROM_INDEX
6609 #undef WINDOW_INIT_OK
6610 #undef GET_SUBWINDOW_FROM_INDEX
6611 #undef SUBWINDOW_INIT_OK
6612
6613 static enum parser_error config_kp_as_mod(struct parser *parser)
6614 {
6615 g_kp_as_mod = parser_getint(parser, "enabled");
6616 return PARSE_ERROR_NONE;
6617 }
6618
6619 static struct parser *init_parse_config(void)
6620 {
6621 struct parser *parser = parser_new();
6622
6623 parser_reg(parser, "window-display uint index int display",
6624 config_window_display);
6625 parser_reg(parser, "window-fullscreen uint index sym fullscreen",
6626 config_window_fullscreen);
6627 parser_reg(parser, "window-full-rect uint index int x int y int w int h",
6628 config_window_rect);
6629 parser_reg(parser, "window-renderer uint index sym type",
6630 config_window_renderer);
6631 parser_reg(parser, "window-wallpaper-path uint index str path",
6632 config_window_wallpaper_path);
6633 parser_reg(parser, "window-wallpaper-mode uint index str mode",
6634 config_window_wallpaper_mode);
6635 parser_reg(parser, "window-status-bar-font uint index int size str name",
6636 config_window_font);
6637 parser_reg(parser, "window-graphics-id uint index int id",
6638 config_window_graphics);
6639 parser_reg(parser, "window-tile-scale uint index sym which int scale",
6640 config_window_tile_scale);
6641
6642 parser_reg(parser, "subwindow-window uint index uint windex ?int vis",
6643 config_subwindow_window);
6644 parser_reg(parser, "subwindow-full-rect uint index int x int y int w int h",
6645 config_subwindow_rect);
6646 parser_reg(parser, "subwindow-font uint index int size str name",
6647 config_subwindow_font);
6648 parser_reg(parser, "subwindow-borders uint index sym borders",
6649 config_subwindow_borders);
6650 parser_reg(parser, "subwindow-top uint index sym top sym always",
6651 config_subwindow_top);
6652 parser_reg(parser, "subwindow-alpha uint index int alpha",
6653 config_subwindow_alpha);
6654 parser_reg(parser, "kp-as-modifier int enabled", config_kp_as_mod);
6655
6656 return parser;
6657 }
6658
6659 static void print_error(const char *name, struct parser *parser)
6660 {
6661 struct parser_state state;
6662 parser_getstate(parser, &state);
6663
6664 fprintf(stderr, "parse error in %s line %d column %d: %s: %s\n",
6665 name,
6666 state.line,
6667 state.col,
6668 state.msg,
6669 parser_error_str[state.error]);
6670 }
6671
6672 static bool read_config_file(void)
6673 {
6674 ang_file *config = file_open(g_config_file, MODE_READ, FTYPE_TEXT);
6675 if (config == NULL) {
6676 /* not an error, its ok for a config file to not exist */
6677 return false;
6678 }
6679
6680 char line[1024];
6681 struct parser *parser = init_parse_config();
6682 errr error = 0;
6683
6684 while (file_getl(config, line, sizeof(line))) {
6685 error = parser_parse(parser, line);
6686 if (error != PARSE_ERROR_NONE) {
6687 print_error(g_config_file, parser);
6688 break;
6689 }
6690 }
6691
6692 parser_destroy(parser);
6693 file_close(config);
6694
6695 return error == PARSE_ERROR_NONE;
6696 }
6697
6698 #endif /* USE_SDL2 */