Debian Codegraph @

src/main-gcu.c

raw symbols SCIP
1 /**
2 * \file main-gcu.c
3 * \brief Support for "curses" systems
4 *
5 * Copyright (c) 1997 Ben Harrison, and others
6 * Copyright (c) 2009-2015 Erik Osheim
7 *
8 * This work is free software; you can redistribute it and/or modify it
9 * under the terms of either:
10 *
11 * a) the GNU General Public License as published by the Free Software
12 * Foundation, version 2, or
13 *
14 * b) the "Angband licence":
15 * This software may be copied and distributed for educational, research,
16 * and not for profit purposes provided that this copyright and statement
17 * are included in all such copies. Other copyrights may also apply.
18 */
19
20 #include "angband.h"
21 #include "buildid.h"
22 #include "cmds.h"
23 #include "cave.h"
24 #include "ui-command.h"
25 #include "ui-display.h"
26 #include "ui-prefs.h"
27
28 #ifdef USE_GCU
29 #include "main.h"
30 #ifdef _WIN32
31 /*
32 * Microsoft puts write() in io.h and marks it with a deprecation warning.
33 * Use the equivalent _write() instead.
34 */
35 #include <io.h>
36 #define PLATFORM_WRITE _write
37 #else
38 #define PLATFORM_WRITE write
39 #endif
40
41 /**
42 * Avoid 'struct term' name conflict with <curses.h> (via <term.h>) on AIX
43 */
44 #define term System_term
45
46 /**
47 * Include the proper "header" file
48 */
49 #ifdef USE_NCURSES
50 # ifdef HAVE_STDbool_H
51 # define NCURSES_ENABLE_STDbool_H 0
52 # endif
53
54 /* Mac needs _XOPEN_SOURCE_EXTENDED to expose mvwaddnstr(). */
55 # define _XOPEN_SOURCE_EXTENDED 1
56
57 # include <ncurses.h>
58 #else
59 # include <curses.h>
60 #endif
61
62 #include <term.h>
63
64 #undef term
65
66 /**
67 * Use POSIX terminal I/O
68 */
69 #define USE_TPOSIX
70
71
72 /**
73 * Hack -- Windows Console mode uses PDCURSES and cannot do any terminal stuff
74 * Hack -- Windows needs Sleep(), and I really don't want to pull in all
75 * the Win32 headers for this one function
76 */
77 #if defined(WIN32_CONSOLE_MODE)
78 # undef USE_TPOSIX
79 _stdcall void Sleep(int);
80 #define usleep(v) Sleep(v / 1000)
81 #endif
82
83 /**
84 * POSIX stuff
85 */
86 #ifdef USE_TPOSIX
87 # include <termios.h>
88 #endif
89
90 /**
91 * If you have errors relating to curs_set(), comment out the following line
92 */
93 #define USE_CURS_SET
94
95 /**
96 * If you have errors with any of the functions mentioned below, try
97 * uncommenting the line it's mentioned on.
98 */
99 /* #define cbreak() crmode() */
100 /* #define nonl() */
101 /* #define nl() */
102
103 /**
104 * Save the "normal" and "angband" terminal settings
105 */
106
107 #ifdef USE_TPOSIX
108
109 static struct termios norm_termios;
110 static struct termios game_termios;
111
112 #endif
113
114 /**
115 * The TERM environment variable; used for terminal capabilities.
116 */
117 static char *termtype;
118 static bool loaded_terminfo;
119
120 /**
121 * Simple rectangle type
122 */
123 struct rect_s
124 {
125 int x, y;
126 int cx, cy;
127 };
128 typedef struct rect_s rect_t, *rect_ptr;
129
130 /* Trivial rectangle utility to make code a bit more readable */
131 static rect_t rect(int x, int y, int cx, int cy)
132 {
133 rect_t r;
134 r.x = x;
135 r.y = y;
136 r.cx = cx;
137 r.cy = cy;
138 return r;
139 }
140
141 /**
142 * Information about a term
143 */
144 typedef struct term_data {
145 term t; /* All term info */
146 rect_t r;
147 WINDOW *win; /* Pointer to the curses window */
148 } term_data;
149
150 /* Max number of windows on screen */
151 #define MAX_TERM_DATA 6
152
153 /* Minimum main term size */
154 #define MIN_TERM0_LINES 24
155 #define MIN_TERM0_COLS 80
156
157 /* Comfortable subterm size */
158 #define COMFY_SUBTERM_LINES 5
159 #define COMFY_SUBTERM_COLS 40
160
161 /* Information about our windows */
162 static term_data data[MAX_TERM_DATA];
163
164 /* Number of initialized "term" structures */
165 static int active = 0;
166
167 #ifdef A_COLOR
168
169 /**
170 * Hack -- define "A_BRIGHT" to be "A_BOLD", because on many
171 * machines, "A_BRIGHT" produces ugly "inverse" video.
172 */
173 #ifndef A_BRIGHT
174 # define A_BRIGHT A_BOLD
175 #endif
176
177 /**
178 * Software flag -- we are allowed to use color
179 */
180 static int can_use_color = false;
181
182 /**
183 * Simple Angband to Curses color conversion table
184 */
185 static int colortable[BASIC_COLORS];
186
187 /**
188 * Same colors as in the colortable, except fg and bg are both set to the fg value.
189 * These pairs are used for drawing solid walls.
190 */
191 static int same_colortable[BASIC_COLORS];
192
193 /* Screen info: use one big Term 0, or other subwindows? */
194 static bool bold_extended = false;
195 static bool use_default_background = false;
196 static bool keep_terminal_colors = false;
197 static int term_count = 1;
198
199 /**
200 * Background color we should draw with; either BLACK or DEFAULT
201 */
202 static int bg_color = COLOR_BLACK;
203
204
205 #define PAIR_WHITE 0
206 #define PAIR_RED 1
207 #define PAIR_GREEN 2
208 #define PAIR_YELLOW 3
209 #define PAIR_BLUE 4
210 #define PAIR_MAGENTA 5
211 #define PAIR_CYAN 6
212 #define PAIR_BLACK 7
213 #define PAIR_WHITE_WHITE 8
214 #define PAIR_RED_RED 9
215 #define PAIR_GREEN_GREEN 10
216 #define PAIR_YELLOW_YELLOW 11
217 #define PAIR_BLUE_BLUE 12
218 #define PAIR_MAGENTA_MAGENTA 13
219 #define PAIR_CYAN_CYAN 14
220 #define PAIR_BLACK_BLACK 15
221
222 #endif
223
224 /**
225 * Place the "keymap" into its "normal" state
226 */
227 static void keymap_norm(void) {
228 #ifdef USE_TPOSIX
229 (void)tcsetattr(0, TCSAFLUSH, &norm_termios);
230 #endif
231 }
232
233
234 /**
235 * Place the "keymap" into the "game" state
236 */
237 static void keymap_game(void) {
238 #ifdef USE_TPOSIX
239 /* Set the game's termios settings */
240 (void)tcsetattr(0, TCSAFLUSH, &game_termios);
241 #endif
242 }
243
244
245 /**
246 * Save the normal keymap
247 */
248 static void keymap_norm_prepare(void) {
249 #ifdef USE_TPOSIX
250 /* Restore the normal termios settings */
251 tcgetattr(0, &norm_termios);
252 #endif
253 }
254
255
256 /**
257 * Save the keymaps (normal and game)
258 */
259 static void keymap_game_prepare(void) {
260 #ifdef USE_TPOSIX
261 /* Save the current termios settings */
262 tcgetattr(0, &game_termios);
263
264 /* Force "Ctrl-C" to interupt */
265 game_termios.c_cc[VINTR] = (char)3;
266
267 /* Force "Ctrl-Z" to suspend */
268 game_termios.c_cc[VSUSP] = (char)26;
269
270 #ifdef VDSUSP
271 /* Hack -- disable "Ctrl-Y" on *BSD */
272 game_termios.c_cc[VDSUSP] = (char)-1;
273 #endif
274
275 /* Disable the standard control characters */
276 game_termios.c_cc[VQUIT] = (char)-1;
277 game_termios.c_cc[VERASE] = (char)-1;
278 game_termios.c_cc[VKILL] = (char)-1;
279 game_termios.c_cc[VEOF] = (char)-1;
280 game_termios.c_cc[VEOL] = (char)-1;
281
282 /* Normally, block until a character is read */
283 game_termios.c_cc[VMIN] = 1;
284 game_termios.c_cc[VTIME] = 0;
285
286 /* Turn off flow control (enable ^S) */
287 game_termios.c_iflag &= ~IXON;
288 #endif
289 }
290
291
292 /**
293 * Suspend/Resume
294 */
295 static errr Term_xtra_gcu_alive(int v) {
296 if (!v) {
297 /* Suspend */
298 int x, y;
299
300 /* Go to normal keymap mode */
301 keymap_norm();
302
303 /* Restore modes */
304 nocbreak();
305 echo();
306 nl();
307
308 /* Hack -- make sure the cursor is visible */
309 Term_xtra(TERM_XTRA_SHAPE, 1);
310
311 /* Flush the curses buffer */
312 refresh();
313
314 /* Get current cursor position */
315 getyx(stdscr, y, x);
316
317 /* Move the cursor to bottom right corner */
318 mvcur(y, x, LINES - 1, 0);
319
320 /* Exit curses */
321 endwin();
322
323 /* Flush the output */
324 fflush(stdout);
325
326 } else {
327 /* Resume */
328
329 /* Restore the settings */
330 cbreak();
331 noecho();
332 nonl();
333
334 /* Go to angband keymap mode */
335 keymap_game();
336 }
337
338 /* Success */
339 return 0;
340 }
341
342 const char help_gcu[] = "Text mode, subopts\n"
343 " -B Use brighter bold characters\n"
344 " -D Use terminal default background color\n"
345 " -K Keep terminal's color table when changing colors\n"
346 " -nN Use N terminals (up to 6)";
347
348 /**
349 * Usage:
350 *
351 * angband -mgcu -- [-B] [-D] [-nN]
352 *
353 * -B Use brighter bold characters
354 * -D Use terminal default background color
355 * -nN Use N terminals (up to 6)
356 */
357
358 #ifdef MSYS2_ENCODING_WORKAROUND
359 /*
360 * Override the default character encoding in MSYS2 for better handling in
361 * Term_text_gcu() of characters that don't fall in the ASCII range. See
362 * init_sdl2() in main-sdl2.c for more details about the default character
363 * encoding in MSYS2.
364 */
365
366 /**
367 * Convert UTF-8 to UTF-16 with each UTF-16 in the native byte order and
368 * lossily change any code point that requires a surrogate pair to U+FFFD.
369 * Return the total number of code points that would be generated by
370 * converting the UTF-8 input.
371 *
372 * \param dest Points to the buffer in which to store the conversion. May be
373 * NULL.
374 * \param src Is a null-terminated UTF-8 sequence.
375 * \param n Is the maximum number of code points to store in dest.
376 *
377 * In case of malformed UTF-8, inserts a U+FFFD in the converted output at the
378 * point of the error.
379 */
380 static size_t Term_mbcs_gcu_msys2(wchar_t *dest, const char *src, int n)
381 {
382 size_t nout = (n > 0) ? n : 0;
383 size_t count = 0;
384
385 while (1) {
386 /*
387 * Default to U+FFFD to indicate an erroneous UTF-8 sequence
388 * that could not be decoded. Follow "best practice"
389 * recommended by the Unicode 6 standard: an erroneous
390 * sequence ends as soon as a disallowed byte is encountered.
391 */
392 uint32_t decoded = 0xfffd;
393
394 if (((unsigned int) *src & 0x80) == 0) {
395 /*
396 * Encoded as a single byte: U+0000 to U+007F ->
397 * 0xxxxxxx.
398 */
399 if (*src == 0) {
400 if (dest && count < nout) {
401 dest[count] = 0;
402 }
403 break;
404 }
405 decoded = *src;
406 ++src;
407 } else if (((unsigned int) *src & 0xe0) == 0xc0) {
408 /*
409 * Encoded as two bytes: U+0080 to U+07FF ->
410 * 110xxxxx 10xxxxxx.
411 */
412 uint32_t part = ((uint32_t) *src & 0x1f) << 6;
413
414 ++src;
415 /*
416 * Check that the first two bits of the continuation
417 * byte are valid and the encoding is not overlong.
418 */
419 if (((unsigned int) *src & 0xc0) == 0x80
420 && part > 0x40) {
421 decoded = part + ((uint32_t) *src & 0x3f);
422 ++src;
423 }
424 } else if (((unsigned int) *src & 0xf0) == 0xe0) {
425 /*
426 * Encoded as three bytes: U+0800 to U+FFFF ->
427 * 1110xxxx 10xxxxxx 10xxxxxx.
428 */
429 uint32_t part = ((uint32_t) *src & 0xf) << 12;
430
431 ++src;
432 if (((unsigned int) *src & 0xc0) == 0x80) {
433 part += ((uint32_t) *src & 0x3f) << 6;
434 ++src;
435 /*
436 * The second part of the test rejects
437 * overlong encodings. The third part
438 * rejects encodings of U+D800 to U+DFFF,
439 * reserved for surrogate pairs.
440 */
441 if (((unsigned int) *src & 0xc0) == 0x80
442 && part >= 0x800
443 && (part & 0xf800) != 0xd800) {
444 decoded = part
445 + ((uint32_t) *src & 0x3f);
446 ++src;
447 }
448 }
449 } else if (((unsigned int) *src & 0xf8) == 0xf0) {
450 /*
451 * Encoded as four bytes: U+10000 to U+1FFFFF ->
452 * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx.
453 */
454 uint32_t part = ((uint32_t) *src & 0x7) << 18;
455
456 ++src;
457 if (((unsigned int) *src & 0xc0) == 0x80) {
458 part += ((uint32_t) * src & 0x3f) << 12;
459 ++src;
460 /*
461 * The second part of the test rejects
462 * overlong encodings. The third part
463 * rejects code points beyond U+10FFFF which
464 * can't be encoded in UTF-16.
465 */
466 if (((unsigned int) *src & 0xc0) == 0x80
467 && part >= 0x10000
468 && (part & 0xff0000)
469 <= 0x100000) {
470 part += ((uint32_t) *src & 0x3f) << 6;
471 ++src;
472 if (((unsigned int) *src & 0xc0)
473 == 0x80) {
474 decoded = part
475 + ((uint32_t) *src
476 & 0x3f);
477 ++src;
478 }
479 }
480 }
481 } else {
482 /*
483 * Either an impossible byte or one that signals the
484 * start of a five byte or longer encoding.
485 */
486 ++src;
487 }
488 if (dest && count < nout) {
489 if (decoded >= 0x10000) {
490 /*
491 * Would require a surrogate pair to represent
492 * accurately. Substitute U+FFFD instead.
493 */
494 assert(decoded <= 0x10FFFF);
495 dest[count] = (wchar_t) 0xfffd;
496 } else {
497 /*
498 * By the decoding logic, the decoded value
499 * should not match part of surrogate pair.
500 */
501 assert(decoded < 0xd800 || decoded > 0xdfff);
502 dest[count] = (wchar_t) decoded;
503 }
504 }
505 ++count;
506 }
507 return count;
508 }
509
510 /**
511 * Convert a UTF-16 stored in the native byte order to UTF-8.
512 * \param s Points to the buffer where the conversion should be stored.
513 * That buffer must have at least Term_wcsz_gcu_msys2() bytes.
514 * \param wchar Is the UTF-16 value to convert.
515 * \return The returned value is the number of bytes written to s or -1
516 * if the UTF-16 value could not be converted.
517 *
518 * This is a necessary counterpart to Term_mbcs_gcu_msys2(): since we
519 * are overriding the default multibyte to wide character conversion, need
520 * to override the reverse conversion as well.
521 */
522 static int Term_wctomb_gcu_msys2(char *s, wchar_t wchar)
523 {
524 if ((unsigned int) wchar <= 0x7f) {
525 *s = wchar;
526 return 1;
527 }
528 if ((unsigned int) wchar < 0x7ff) {
529 *s++ = 0xc0 + (((unsigned int) wchar & 0x7c0) >> 6);
530 *s++ = 0x80 + ((unsigned int) wchar & 0x3f);
531 return 2;
532 }
533 /* Refuse to encode a value reserved for surrogate pairs in UTF-16. */
534 if ((unsigned int) wchar >= 0xd800 && (unsigned int) wchar <= 0xdfff) {
535 return -1;
536 }
537 *s++ = 0xe0 + (((unsigned int) wchar & 0xf000) >> 12);
538 *s++ = 0x80 + (((unsigned int) wchar & 0xfc0) >> 6);
539 *s++ = 0x80 + ((unsigned int) wchar & 0x3f);
540 return 3;
541 }
542
543 /**
544 * Return whether a UTF-16 value is printable.
545 *
546 * This is a necessary counterpart to Term_mbcs_gcu_msys2() so that
547 * screening of wide characters in the core's text_out_to_screen() is
548 * consistent with what Term_mbcs_gcu_msys2() does.
549 */
550 static int Term_iswprint_gcu_msys2(wint_t wc)
551 {
552 /*
553 * Upcast the UTF-16 value to UTF-32 since the UTF-16 value is either
554 * equal to the code point's value or is part of a surrogate pair.
555 */
556 return utf32_isprint((uint32_t) wc);
557 }
558
559 /**
560 * Return the maximum number of bytes needed for a multibyte encoding of a
561 * wchar.
562 */
563 static int Term_wcsz_gcu_msys2(void)
564 {
565 /*
566 * UTF-8 takes at most 3 bytes to encode a code point that can be
567 * encoded with a single 16-bit quantity in UTF-16.
568 */
569 return 3;
570 }
571
572 #endif /* MSYS2_ENCODING_WORKAROUND */
573
574 /**
575 * Init the "curses" system
576 */
577 static void Term_init_gcu(term *t) {
578 term_data *td = (term_data *)(t->data);
579
580 /*
581 * This is necessary to keep the first call to getch()
582 * from clearing the screen
583 */
584 wrefresh(stdscr);
585
586 /* Count init's, handle first */
587 if (active++ != 0) return;
588
589 #if defined(USE_NCURSES) && defined(KEY_MOUSE)
590 /* Turn on the mouse. */
591 mousemask(ALL_MOUSE_EVENTS, NULL);
592 #endif
593
594 /* Erase the window */
595 wclear(td->win);
596
597 /* Reset the cursor */
598 wmove(td->win, 0, 0);
599
600 /* Flush changes */
601 wrefresh(td->win);
602
603 /* Game keymap */
604 keymap_game();
605
606 #ifdef MSYS2_ENCODING_WORKAROUND
607 /*
608 * Override default character encoding on MSYS2; see main-sdl2.c for
609 * more details.
610 */
611 text_mbcs_hook = Term_mbcs_gcu_msys2;
612 text_wctomb_hook = Term_wctomb_gcu_msys2;
613 text_wcsz_hook = Term_wcsz_gcu_msys2;
614 text_iswprint_hook = Term_iswprint_gcu_msys2;
615 #endif /* MSYS2_ENCODING_WORKAROUND */
616 }
617
618
619 /**
620 * Nuke the "curses" system
621 */
622 static void Term_nuke_gcu(term *t) {
623 int x, y;
624 term_data *td = (term_data *)(t->data);
625
626 /* Delete this window */
627 delwin(td->win);
628
629 /* Count nuke's, handle last */
630 if (--active != 0) return;
631
632 /* Hack -- make sure the cursor is visible */
633 Term_xtra(TERM_XTRA_SHAPE, 1);
634
635 #ifdef A_COLOR
636 /* Reset colors to defaults */
637 start_color();
638 #endif
639
640 /* Get current cursor position */
641 getyx(stdscr, y, x);
642
643 /* Move the cursor to bottom right corner */
644 mvcur(y, x, LINES - 1, 0);
645
646 /* Flush the curses buffer */
647 refresh();
648
649 /* Exit curses */
650 endwin();
651
652 /* Flush the output */
653 fflush(stdout);
654
655 /* Normal keymap */
656 keymap_norm();
657 }
658
659 /**
660 * Helper function for get_gcu_term_size:
661 * Given inputs, populates size and start (rows and y, or cols and x)
662 * with correct values for a group (column or row) of terms.
663 * term_group_index: the placement of the group, e.g.
664 * top row is 0
665 * term_group_count: the number of groups in this dimension (2 or 3)
666 * window_size: the number of grids the window has in this dimension
667 * min_term0_size: the minimum main term size in this dimension
668 * (80 or 24), also the maximum subterm size
669 * comfy_subterm_size: in balancing among three groups, we first give the
670 * main term its minimum, and then allocate evenly between the other
671 * two subterms until they are both comfy_subterm_size, at which point
672 * we grow the outer subterm until it reaches min_term0_size. (The
673 * middle subterm then grows until min_term0_size, and any further
674 * window space goes to the main term.)
675 */
676 static void balance_dimension(int *size, int *start, int term_group_index,
677 int term_group_count, int window_size, int min_term0_size,
678 int comfy_subterm_size) {
679 /* Convenience variable for clarity.
680 * Note that it is also the number of separator rows/columns */
681 int subterm_group_count = term_group_count - 1;
682
683 if (term_group_index == 0) {
684 /* main term */
685 *size = MAX(min_term0_size, window_size - subterm_group_count*(min_term0_size + 1));
686 *start = 0;
687 } else if (term_group_index == term_group_count - 1) {
688 /* outer or only subterm */
689 if (window_size <= min_term0_size + subterm_group_count*(comfy_subterm_size + 1)) {
690 /* Not enough room for min term0 and all subterms comfy.
691 * Note that we round up here and down for the middle subterm*/
692 *size = (window_size - min_term0_size - subterm_group_count) / subterm_group_count;
693 if (window_size > min_term0_size + subterm_group_count + *size * subterm_group_count)
694 (*size)++;
695 } else {
696 *size = MIN(min_term0_size, window_size - min_term0_size - comfy_subterm_size*(subterm_group_count - 1) - subterm_group_count);
697 }
698 *start = window_size - *size;
699 } else {
700 /* middle subterm */
701 if (window_size <= subterm_group_count*(min_term0_size + 1) + comfy_subterm_size) {
702 /* Outer subterm(s) not yet full-sized, thus at most comfy */
703 *size = MIN(comfy_subterm_size, (window_size - min_term0_size - subterm_group_count) / subterm_group_count);
704 } else {
705 *size = MIN(min_term0_size, window_size - subterm_group_count*(min_term0_size + 1));
706 }
707 *start = 1 + MAX(min_term0_size, window_size - subterm_group_count*(min_term0_size + 1));
708 }
709 }
710
711 /**
712 * For a given term number (i) set the upper left corner (x, y) and the
713 * correct dimensions. Remember to leave one row and column between
714 * subterms.
715 */
716 static void get_gcu_term_size(int i, int *rows, int *cols, int *y, int *x) {
717 bool is_wide = (10 * LINES < 3 * COLS);
718 int term_rows = 1;
719 int term_cols = 1;
720 int term_row_index = 0;
721 int term_col_index = 0;
722
723 assert(i < term_count);
724
725 /* For sufficiently small windows, we can only use one term.
726 * Each additional row/column of terms requires at least two lines
727 * for the separators. If everything is as square as possible,
728 * the 3rd, 7th, 13th, etc. terms add to the short dimension, while
729 * the 2nd, 5th, 10th, etc. terms add to the long dimension.
730 * However, three terms are the special case of 1x3 or 3x1.
731 */
732 if (is_wide) {
733 while (term_rows*(term_rows + 1) < term_count) term_rows++;
734 while (term_cols*term_cols < term_count) term_cols++;
735 if (term_count == 3) {
736 term_rows = 1;
737 term_cols = 3;
738 }
739 term_col_index = i % term_cols;
740 term_row_index = (int)(i / term_cols);
741 } else { /* !is_wide */
742 while (term_rows*term_rows < term_count) term_rows++;
743 while (term_cols*(term_cols + 1) < term_count) term_cols++;
744 if (term_count == 3) {
745 term_rows = 3;
746 term_cols = 1;
747 }
748 term_col_index = (int)(i / term_rows);
749 term_row_index = i % term_rows;
750 }
751
752 if (LINES < MIN_TERM0_LINES + 2 * (term_rows - 1) ||
753 COLS < MIN_TERM0_COLS + 2 * (term_cols - 1)) {
754 term_rows = term_cols = term_count = 1;
755 if (i != 0) {
756 *rows = *cols = *y = *x = 0;
757 }
758 term_col_index = term_row_index = 0;
759 }
760
761 balance_dimension(cols, x, term_col_index, term_cols, COLS, MIN_TERM0_COLS, COMFY_SUBTERM_COLS);
762 balance_dimension(rows, y, term_row_index, term_rows, LINES, MIN_TERM0_LINES, COMFY_SUBTERM_LINES);
763 }
764
765
766 /**
767 * Query ncurses for new screen size and try to resize the GCU terms.
768 */
769 static void do_gcu_resize(void) {
770 int i, rows, cols, y, x;
771 term *old_t = Term;
772
773 for (i = 0; i < term_count; i++) {
774 /* Activate the current Term */
775 Term_activate(&data[i].t);
776
777 /* If we can resize the curses window, then resize the Term */
778 get_gcu_term_size(i, &rows, &cols, &y, &x);
779 if (wresize(data[i].win, rows, cols) == OK)
780 Term_resize(cols, rows);
781
782 /* Activate the old term */
783 Term_activate(old_t);
784 }
785 do_cmd_redraw();
786 }
787
788
789 /**
790 * Process events, with optional wait
791 */
792 static errr Term_xtra_gcu_event(int v) {
793 int i, j, k, mods=0;
794
795 if (v) {
796 /* Wait for a keypress; use halfdelay(1) so if the user takes more */
797 /* than 0.2 seconds we get a chance to do updates. */
798 halfdelay(2);
799 i = getch();
800 while (i == ERR) {
801 i = getch();
802 idle_update();
803 }
804 cbreak();
805 } else {
806 /* Do not wait for it */
807 nodelay(stdscr, true);
808
809 /* Check for keypresses */
810 i = getch();
811
812 /* Wait for it next time */
813 nodelay(stdscr, false);
814
815 /* None ready */
816 if (i == ERR) return (1);
817 if (i == EOF) return (1);
818 }
819
820 /* Not sure if this is portable to non-ncurses platforms */
821 #ifdef USE_NCURSES
822 if (i == KEY_RESIZE) {
823 /* wait until we go one second (10 deci-seconds) before actually
824 * doing the resizing. users often end up triggering multiple
825 * KEY_RESIZE events while changing window size. */
826 halfdelay(10);
827 do {
828 i = getch();
829 } while (i == KEY_RESIZE);
830 cbreak();
831 do_gcu_resize();
832 if (i == ERR) return (1);
833 }
834 #endif
835
836 #if defined(USE_NCURSES) && defined(KEY_MOUSE)
837 if (i == KEY_MOUSE) {
838 MEVENT m;
839 if (getmouse(&m) != OK) return (0);
840
841 int b = 0;
842 if (m.bstate & BUTTON1_CLICKED) b = 1;
843 else if (m.bstate & BUTTON2_CLICKED) b = 2;
844 else if (m.bstate & BUTTON3_CLICKED) b = 3;
845 else if (m.bstate & BUTTON4_CLICKED) b = 4;
846 if (m.bstate & BUTTON_SHIFT) b |= (KC_MOD_SHIFT << 4);
847 if (m.bstate & BUTTON_CTRL) b |= (KC_MOD_CONTROL << 4);
848 if (m.bstate & BUTTON_ALT) b |= (KC_MOD_ALT << 4);
849
850 if (b != 0) Term_mousepress(m.x, m.y, b);
851 return (0);
852 }
853 #endif
854
855 /* uncomment to debug keycode issues */
856 #if 0
857 printw("key %d", i);
858 wrefresh(stdscr);
859 #endif
860
861 /* This might be a bad idea, but...
862 *
863 * Here we try to second-guess ncurses. In some cases, keypad() mode will
864 * fail to translate multi-byte escape sequences into things like number-
865 * pad actions, function keys, etc. So we can hardcode a small list of some
866 * of the most common sequences here, just in case.
867 *
868 * Notice that we turn nodelay() on. This means, that we won't accidentally
869 * interpret sequences as valid unless all the bytes are immediately
870 * available; this seems like an acceptable risk to fix problems associated
871 * with various terminal emulators (I'm looking at you PuTTY).
872 */
873 if (i == 27) { /* ESC */
874 nodelay(stdscr, true);
875 j = getch();
876 switch (j) {
877 case 'O': {
878 k = getch();
879 switch (k) {
880 /* PuTTY number pad */
881 case 'q': i = '1'; break;
882 case 'r': i = '2'; break;
883 case 's': i = '3'; break;
884 case 't': i = '4'; break;
885 case 'u': i = '5'; break;
886 case 'v': i = '6'; break;
887 case 'w': i = '7'; break;
888 case 'x': i = '8'; break;
889 case 'y': i = '9'; break;
890
891 /* no match */
892 case ERR: break;
893 default: ungetch(k); ungetch(j);
894 }
895 break;
896 }
897
898 /* no match */
899 case ERR: break;
900 default: ungetch(j);
901 }
902 nodelay(stdscr, false);
903 }
904
905 #ifdef KEY_DOWN
906 /* Handle arrow keys */
907 switch (i) {
908 case KEY_DOWN: i = ARROW_DOWN; break;
909 case KEY_UP: i = ARROW_UP; break;
910 case KEY_LEFT: i = ARROW_LEFT; break;
911 case KEY_RIGHT: i = ARROW_RIGHT; break;
912 case KEY_DC: i = KC_DELETE; break;
913 case KEY_BACKSPACE: i = KC_BACKSPACE; break;
914 case KEY_ENTER: i = KC_ENTER; mods |= KC_MOD_KEYPAD; break;
915 case 9: i = KC_TAB; break;
916 case 13: i = KC_ENTER; break;
917 case 27: i = ESCAPE; break;
918
919 /* keypad keys */
920 case 0xFC: i = '0'; break;
921 case 0xFD: i = '.'; break;
922 case 0xC0: i = '\b'; break;
923 case 0xDF: i = '1'; break;
924 case 0xF5: i = '3'; break;
925 case 0xE9: i = '5'; break;
926 case 0xC1: i = '7'; break;
927 case 0xF4: i = '9'; break;
928
929 default: {
930 if (i < KEY_MIN) break;
931
932 /* Mega-Hack -- Fold, spindle, and mutilate
933 * the keys to fit in 7 bits.
934 */
935
936 if (i >= 252) i = KEY_F(63) - (i - 252);
937 if (i >= ARROW_DOWN) i += 4;
938
939 i = 128 + (i & 127);
940 break;
941 }
942 }
943 #endif
944
945 /* Enqueue the keypress */
946 Term_keypress(i, mods);
947
948 /* Success */
949 return (0);
950 }
951
952 static int scale_color(int i, int j, int scale) {
953 return (angband_color_table[i][j] * (scale - 1) + 127) / 255;
954 }
955
956 static int create_color(int i, int scale) {
957 int r = scale_color(i, 1, scale);
958 int g = scale_color(i, 2, scale);
959 int b = scale_color(i, 3, scale);
960 int rgb = 16 + scale * scale * r + scale * g + b;
961
962 /* In the case of white and black we need to use the ANSI colors */
963 if (r == g && g == b) {
964 if (b == 0) rgb = 0;
965 if (b == scale) rgb = 15;
966 }
967
968 return rgb;
969 }
970
971
972 /**
973 * Adjust the color tables if there's more than 16 available.
974 */
975 static void handle_extended_color_tables(void) {
976 #ifdef A_COLOR
977 if (COLORS == 256 || COLORS == 88) {
978 int isbold = bold_extended ? A_BRIGHT : A_NORMAL;
979 int i;
980
981 if (keep_terminal_colors) {
982 /*
983 * If we have more than 16 colors, find the best
984 * matches in the terminal's default color table.
985 * These numbers correspond to xterm/rxvt's builtin
986 * color numbers--they do not correspond to curses'
987 * constants OR with curses' color pairs.
988 *
989 * XTerm has 216 (6*6*6) RGB colors, with each RGB
990 * setting 0-5.
991 * RXVT has 64 (4*4*4) RGB colors, with each RGB
992 * setting 0-3.
993 *
994 * Both also have the basic 16 ANSI colors, plus some
995 * extra grayscale colors which we do not use.
996 */
997 int scale = COLORS == 256 ? 6 : 4;
998
999 bg_color = create_color(COLOUR_DARK, scale);
1000 for (i = 0; i < BASIC_COLORS; i++) {
1001 int fg = create_color(i, scale);
1002 init_pair(i + 1, fg, bg_color);
1003 colortable[i] = COLOR_PAIR(i + 1) | isbold;
1004 init_pair(BASIC_COLORS + i, fg, fg);
1005 same_colortable[i] =
1006 COLOR_PAIR(BASIC_COLORS + i) | isbold;
1007 }
1008 } else {
1009 bg_color = 0;
1010 for (i = 0; i < BASIC_COLORS; i++) {
1011 /*
1012 * Scale components to a range of 0 - 1000 per
1013 * init_color()'s documentation.
1014 */
1015 init_color(i,
1016 (angband_color_table[i][1] * 1001) / 256,
1017 (angband_color_table[i][2] * 1001) / 256,
1018 (angband_color_table[i][3] * 1001) / 256);
1019 init_pair(i + 1, i, bg_color);
1020 colortable[i] = COLOR_PAIR(i + 1) | isbold;
1021 init_pair(BASIC_COLORS + i, i, i);
1022 same_colortable[i] =
1023 COLOR_PAIR(BASIC_COLORS + i) | isbold;
1024 }
1025 }
1026
1027 for (i = 0; i < term_count; ++i) {
1028 if (data[i].win) {
1029 wbkgdset(data[i].win, ' ' |
1030 colortable[COLOUR_DARK]);
1031 }
1032 }
1033 if (data[0].win) {
1034 /*
1035 * Adjust the background color on the standard screen
1036 * as well so separators between the terminals have
1037 * the same background as the rest.
1038 */
1039 chtype term0_bkg = getbkgd(data[0].win);
1040
1041 if (getbkgd(stdscr) != term0_bkg) {
1042 wbkgd(stdscr, term0_bkg);
1043 wrefresh(stdscr);
1044 }
1045 }
1046 }
1047 #endif
1048 }
1049
1050
1051 /**
1052 * Handle a "special request"
1053 */
1054 static errr Term_xtra_gcu(int n, int v) {
1055 term_data *td = (term_data *)(Term->data);
1056
1057 /* Analyze the request */
1058 switch (n) {
1059 /* Clear screen */
1060 case TERM_XTRA_CLEAR: touchwin(td->win); wclear(td->win); return 0;
1061
1062 /* Make a noise */
1063 case TERM_XTRA_NOISE: PLATFORM_WRITE(1, "\007", 1); return 0;
1064
1065 /* Flush the Curses buffer */
1066 case TERM_XTRA_FRESH: wrefresh(td->win); return 0;
1067
1068 #ifdef USE_CURS_SET
1069 /* Change the cursor visibility */
1070 case TERM_XTRA_SHAPE: curs_set(v); return 0;
1071 #endif
1072
1073 /* Suspend/Resume curses */
1074 case TERM_XTRA_ALIVE: return Term_xtra_gcu_alive(v);
1075
1076 /* Process events */
1077 case TERM_XTRA_EVENT: return Term_xtra_gcu_event(v);
1078
1079 /* Flush events */
1080 case TERM_XTRA_FLUSH: while (!Term_xtra_gcu_event(false)); return 0;
1081
1082 /* Delay */
1083 case TERM_XTRA_DELAY: if (v > 0) usleep(1000 * v); return 0;
1084
1085 /* React to events */
1086 case TERM_XTRA_REACT: handle_extended_color_tables(); return 0;
1087 }
1088
1089 /* Unknown event */
1090 return 1;
1091 }
1092
1093
1094 /**
1095 * Actually MOVE the hardware cursor
1096 */
1097 static errr Term_curs_gcu(int x, int y) {
1098 term_data *td = (term_data *)(Term->data);
1099 wmove(td->win, y, x);
1100 return 0;
1101 }
1102
1103
1104 /**
1105 * Erase a grid of space
1106 * Hack -- try to be "semi-efficient".
1107 */
1108 static errr Term_wipe_gcu(int x, int y, int n) {
1109 term_data *td = (term_data *)(Term->data);
1110
1111 wmove(td->win, y, x);
1112
1113 if (x + n >= td->t.wid) {
1114 /* Clear to end of line */
1115 wclrtoeol(td->win);
1116 } else {
1117 /* Clear some characters */
1118 if (can_use_color) {
1119 wattrset(td->win, colortable[COLOUR_DARK] | A_NORMAL);
1120 }
1121 whline(td->win, ' ', n);
1122 if (can_use_color) {
1123 wattrset(td->win, A_NORMAL);
1124 }
1125 }
1126
1127 return 0;
1128 }
1129
1130
1131 /**
1132 * Place some text on the screen using an attribute
1133 */
1134 static errr Term_text_gcu(int x, int y, int n, int a, const wchar_t *s) {
1135 term_data *td = (term_data *)(Term->data);
1136
1137 #ifdef A_COLOR
1138 if (can_use_color) {
1139
1140 /* the lower 7 bits of the attribute indicate the fg/bg */
1141 int attr = a & 127;
1142
1143 /* the high bit of the attribute indicates a reversed fg/bg */
1144 bool reversed = a > 127;
1145
1146 int color;
1147
1148 /* Set bg and fg to the same color when drawing solid walls */
1149 if (a / MULT_BG == BG_SAME) {
1150 color = same_colortable[attr];
1151 } else {
1152 color = colortable[attr];
1153 }
1154
1155 /* the following check for A_BRIGHT is to avoid #1813 */
1156 int mode;
1157 if (reversed && (color & A_BRIGHT))
1158 mode = (color & ~A_BRIGHT) | A_BLINK | A_REVERSE;
1159 else if (reversed)
1160 mode = color | A_REVERSE;
1161 else
1162 mode = color | A_NORMAL;
1163
1164 wattrset(td->win, mode);
1165 mvwaddnwstr(td->win, y, x, s, n);
1166 wattrset(td->win, A_NORMAL);
1167 return 0;
1168 }
1169 #endif
1170
1171 mvwaddnwstr(td->win, y, x, s, n);
1172 return 0;
1173 }
1174
1175 /**
1176 * Create a window for the given "term_data" argument.
1177 *
1178 * Assumes legal arguments.
1179 */
1180 static errr term_data_init_gcu(term_data *td, int rows, int cols, int y, int x)
1181 {
1182 term *t = &td->t;
1183
1184 /* Create new window */
1185 td->win = newwin(rows, cols, y, x);
1186
1187 /* Check for failure */
1188 if (!td->win)
1189 quit("Failed to setup curses window.");
1190
1191 /* Initialize the term */
1192 term_init(t, cols, rows, 256);
1193
1194 /* Avoid bottom right corner */
1195 t->icky_corner = true;
1196
1197 /* Differentiate between BS/^h, Tab/^i, etc. */
1198 t->complex_input = true;
1199
1200 /* Set some hooks */
1201 t->init_hook = Term_init_gcu;
1202 t->nuke_hook = Term_nuke_gcu;
1203
1204 /* Set some more hooks */
1205 t->text_hook = Term_text_gcu;
1206 t->wipe_hook = Term_wipe_gcu;
1207 t->curs_hook = Term_curs_gcu;
1208 t->xtra_hook = Term_xtra_gcu;
1209
1210 /* Save the data */
1211 t->data = td;
1212
1213 /* Activate it */
1214 Term_activate(t);
1215
1216 /* Success */
1217 return (0);
1218 }
1219
1220 /**
1221 * Simple helper
1222 */
1223 static errr term_data_init(term_data *td)
1224 {
1225 return term_data_init_gcu(td, td->r.cy, td->r.cx, td->r.y, td->r.x);
1226 }
1227
1228
1229 /* Parse 27,15,*x30 up to the 'x'. * gets converted to a big number
1230 Parse 32,* until the end. Return count of numbers parsed */
1231 static int _parse_size_list(const char *arg, int sizes[], int max)
1232 {
1233 int i = 0;
1234 const char *start = arg;
1235 const char *stop = arg;
1236
1237 for (;;)
1238 {
1239 if (!*stop || !isdigit(*stop))
1240 {
1241 if (i >= max) break;
1242 if (*start == '*')
1243 sizes[i] = 255;
1244 else
1245 {
1246 /* rely on atoi("23,34,*") -> 23
1247 otherwise, copy [start, stop) into a new buffer first.*/
1248 sizes[i] = atoi(start);
1249 }
1250 i++;
1251 if (!*stop || *stop != ',') break;
1252
1253 stop++;
1254 start = stop;
1255 }
1256 else
1257 stop++;
1258 }
1259 return i;
1260 }
1261
1262
1263 static void hook_quit(const char *str) {
1264 int i;
1265
1266 for (i = 0; i < term_count; i++) {
1267 if (angband_term[i]) {
1268 term_nuke(angband_term[i]);
1269 }
1270 }
1271 endwin();
1272 }
1273
1274 /**
1275 * Prepare "curses" for use by the file "ui-term.c"
1276 *
1277 * Installs the "hook" functions defined above, and then activates
1278 * the main screen "term", which clears the screen and such things.
1279 *
1280 * Someone should really check the semantics of "initscr()"
1281 */
1282 errr init_gcu(int argc, char **argv) {
1283 int i;
1284
1285 /* Initialize info about terminal capabilities */
1286 termtype = getenv("TERM");
1287 loaded_terminfo = termtype && tgetent(0, termtype) == 1;
1288
1289 /* Parse args */
1290 for (i = 1; i < argc; i++) {
1291 if (prefix(argv[i], "-B")) {
1292 bold_extended = true;
1293 } else if (prefix(argv[i], "-n")) {
1294 term_count = atoi(&argv[i][2]);
1295 if (term_count > MAX_TERM_DATA) term_count = MAX_TERM_DATA;
1296 else if (term_count < 1) term_count = 1;
1297 } else if (prefix(argv[i], "-D")) {
1298 use_default_background = true;
1299 } else if (streq(argv[i], "-K")) {
1300 keep_terminal_colors = true;
1301 }
1302 }
1303
1304 /* Extract the normal keymap */
1305 keymap_norm_prepare();
1306
1307 /* We do it like this to prevent a link error with curseses that
1308 * lack ESCDELAY. */
1309 if (!getenv("ESCDELAY")) {
1310 #if _POSIX_C_SOURCE < 200112L
1311 static char escdelbuf[80] = "ESCDELAY=20";
1312 putenv(escdelbuf);
1313 #else
1314 setenv("ESCDELAY", "20", 1);
1315 #endif
1316 }
1317
1318 /* Initialize */
1319 if (initscr() == NULL) return (-1);
1320
1321 /* Activate hooks */
1322 quit_aux = hook_quit;
1323
1324 /* Require standard size screen */
1325 if (LINES < MIN_TERM0_LINES || COLS < MIN_TERM0_COLS)
1326 quit("Angband needs at least an 80x24 'curses' screen");
1327
1328 #ifdef A_COLOR
1329 /* Do we have color, and enough color, available? */
1330 can_use_color = ((start_color() != ERR) && has_colors() &&
1331 (COLORS >= 8) && (COLOR_PAIRS >= 8));
1332 if (!can_change_color()) keep_terminal_colors = true;
1333
1334 #ifdef HAVE_USE_DEFAULT_COLORS
1335 /* Should we use curses' "default color" */
1336 if (use_default_background && use_default_colors() == OK) bg_color = -1;
1337 #endif
1338
1339 /* Attempt to use colors */
1340 if (can_use_color) {
1341 /* Prepare the color pairs */
1342 /* PAIR_WHITE (pair 0) is *always* WHITE on BLACK */
1343 init_pair(PAIR_RED, COLOR_RED, bg_color);
1344 init_pair(PAIR_GREEN, COLOR_GREEN, bg_color);
1345 init_pair(PAIR_YELLOW, COLOR_YELLOW, bg_color);
1346 init_pair(PAIR_BLUE, COLOR_BLUE, bg_color);
1347 init_pair(PAIR_MAGENTA, COLOR_MAGENTA, bg_color);
1348 init_pair(PAIR_CYAN, COLOR_CYAN, bg_color);
1349 init_pair(PAIR_BLACK, COLOR_BLACK, bg_color);
1350
1351 /* These pairs are used for drawing solid walls */
1352 init_pair(PAIR_WHITE_WHITE, COLOR_WHITE, COLOR_WHITE);
1353 init_pair(PAIR_RED_RED, COLOR_RED, COLOR_RED);
1354 init_pair(PAIR_GREEN_GREEN, COLOR_GREEN, COLOR_GREEN);
1355 init_pair(PAIR_YELLOW_YELLOW, COLOR_YELLOW, COLOR_YELLOW);
1356 init_pair(PAIR_BLUE_BLUE, COLOR_BLUE, COLOR_BLUE);
1357 init_pair(PAIR_MAGENTA_MAGENTA, COLOR_MAGENTA, COLOR_MAGENTA);
1358 init_pair(PAIR_CYAN_CYAN, COLOR_CYAN, COLOR_CYAN);
1359 init_pair(PAIR_BLACK_BLACK, COLOR_BLACK, COLOR_BLACK);
1360
1361 /* Prepare the colors */
1362 colortable[COLOUR_DARK] = (COLOR_PAIR(PAIR_BLACK));
1363 colortable[COLOUR_WHITE] = (COLOR_PAIR(PAIR_WHITE) | A_BRIGHT);
1364 colortable[COLOUR_SLATE] = (COLOR_PAIR(PAIR_WHITE));
1365 colortable[COLOUR_ORANGE] = (COLOR_PAIR(PAIR_YELLOW) | A_BRIGHT);
1366 colortable[COLOUR_RED] = (COLOR_PAIR(PAIR_RED));
1367 colortable[COLOUR_GREEN] = (COLOR_PAIR(PAIR_GREEN));
1368 colortable[COLOUR_BLUE] = (COLOR_PAIR(PAIR_BLUE));
1369 colortable[COLOUR_UMBER] = (COLOR_PAIR(PAIR_YELLOW));
1370 colortable[COLOUR_L_DARK] = (COLOR_PAIR(PAIR_BLACK) | A_BRIGHT);
1371 colortable[COLOUR_L_WHITE] = (COLOR_PAIR(PAIR_WHITE));
1372 colortable[COLOUR_L_PURPLE] = (COLOR_PAIR(PAIR_MAGENTA));
1373 colortable[COLOUR_YELLOW] = (COLOR_PAIR(PAIR_YELLOW) | A_BRIGHT);
1374 colortable[COLOUR_L_RED] = (COLOR_PAIR(PAIR_MAGENTA) | A_BRIGHT);
1375 colortable[COLOUR_L_GREEN] = (COLOR_PAIR(PAIR_GREEN) | A_BRIGHT);
1376 colortable[COLOUR_L_BLUE] = (COLOR_PAIR(PAIR_BLUE) | A_BRIGHT);
1377 colortable[COLOUR_L_UMBER] = (COLOR_PAIR(PAIR_YELLOW));
1378
1379 colortable[COLOUR_PURPLE] = (COLOR_PAIR(PAIR_MAGENTA));
1380 colortable[COLOUR_VIOLET] = (COLOR_PAIR(PAIR_MAGENTA));
1381 colortable[COLOUR_TEAL] = (COLOR_PAIR(PAIR_CYAN));
1382 colortable[COLOUR_MUD] = (COLOR_PAIR(PAIR_YELLOW));
1383 colortable[COLOUR_L_YELLOW] = (COLOR_PAIR(PAIR_YELLOW | A_BRIGHT));
1384 colortable[COLOUR_MAGENTA] = (COLOR_PAIR(PAIR_MAGENTA | A_BRIGHT));
1385 colortable[COLOUR_L_TEAL] = (COLOR_PAIR(PAIR_CYAN) | A_BRIGHT);
1386 colortable[COLOUR_L_VIOLET] = (COLOR_PAIR(PAIR_MAGENTA) | A_BRIGHT);
1387 colortable[COLOUR_L_PINK] = (COLOR_PAIR(PAIR_MAGENTA) | A_BRIGHT);
1388 colortable[COLOUR_MUSTARD] = (COLOR_PAIR(PAIR_YELLOW));
1389 colortable[COLOUR_BLUE_SLATE] = (COLOR_PAIR(PAIR_BLUE));
1390 colortable[COLOUR_DEEP_L_BLUE] = (COLOR_PAIR(PAIR_BLUE));
1391
1392 same_colortable[COLOUR_DARK] = (COLOR_PAIR(PAIR_BLACK_BLACK));
1393 same_colortable[COLOUR_WHITE] = (COLOR_PAIR(PAIR_WHITE_WHITE) | A_BRIGHT);
1394 same_colortable[COLOUR_SLATE] = (COLOR_PAIR(PAIR_WHITE_WHITE));
1395 same_colortable[COLOUR_ORANGE] = (COLOR_PAIR(PAIR_YELLOW_YELLOW) | A_BRIGHT);
1396 same_colortable[COLOUR_RED] = (COLOR_PAIR(PAIR_RED_RED));
1397 same_colortable[COLOUR_GREEN] = (COLOR_PAIR(PAIR_GREEN_GREEN));
1398 same_colortable[COLOUR_BLUE] = (COLOR_PAIR(PAIR_BLUE_BLUE));
1399 same_colortable[COLOUR_UMBER] = (COLOR_PAIR(PAIR_YELLOW_YELLOW));
1400 same_colortable[COLOUR_L_DARK] = (COLOR_PAIR(PAIR_BLACK_BLACK) | A_BRIGHT);
1401 same_colortable[COLOUR_L_WHITE] = (COLOR_PAIR(PAIR_WHITE_WHITE));
1402 same_colortable[COLOUR_L_PURPLE] = (COLOR_PAIR(PAIR_MAGENTA_MAGENTA));
1403 same_colortable[COLOUR_YELLOW] = (COLOR_PAIR(PAIR_YELLOW_YELLOW) | A_BRIGHT);
1404 same_colortable[COLOUR_L_RED] = (COLOR_PAIR(PAIR_MAGENTA_MAGENTA) | A_BRIGHT);
1405 same_colortable[COLOUR_L_GREEN] = (COLOR_PAIR(PAIR_GREEN_GREEN) | A_BRIGHT);
1406 same_colortable[COLOUR_L_BLUE] = (COLOR_PAIR(PAIR_BLUE_BLUE) | A_BRIGHT);
1407 same_colortable[COLOUR_L_UMBER] = (COLOR_PAIR(PAIR_YELLOW_YELLOW));
1408
1409 same_colortable[COLOUR_PURPLE] = (COLOR_PAIR(PAIR_MAGENTA_MAGENTA));
1410 same_colortable[COLOUR_VIOLET] = (COLOR_PAIR(PAIR_MAGENTA_MAGENTA));
1411 same_colortable[COLOUR_TEAL] = (COLOR_PAIR(PAIR_CYAN_CYAN));
1412 same_colortable[COLOUR_MUD] = (COLOR_PAIR(PAIR_YELLOW_YELLOW));
1413 same_colortable[COLOUR_L_YELLOW] = (COLOR_PAIR(PAIR_YELLOW_YELLOW) | A_BRIGHT);
1414 same_colortable[COLOUR_MAGENTA] = (COLOR_PAIR(PAIR_MAGENTA_MAGENTA) | A_BRIGHT);
1415 same_colortable[COLOUR_L_TEAL] = (COLOR_PAIR(PAIR_CYAN_CYAN) | A_BRIGHT);
1416 same_colortable[COLOUR_L_VIOLET] = (COLOR_PAIR(PAIR_MAGENTA_MAGENTA) | A_BRIGHT);
1417 same_colortable[COLOUR_L_PINK] = (COLOR_PAIR(PAIR_MAGENTA_MAGENTA) | A_BRIGHT);
1418 same_colortable[COLOUR_MUSTARD] = (COLOR_PAIR(PAIR_YELLOW));
1419 same_colortable[COLOUR_BLUE_SLATE] = (COLOR_PAIR(PAIR_BLUE));
1420 same_colortable[COLOUR_DEEP_L_BLUE] = (COLOR_PAIR(PAIR_BLUE));
1421 handle_extended_color_tables();
1422 }
1423 #endif
1424
1425 /* Paranoia -- Assume no waiting */
1426 nodelay(stdscr, false);
1427
1428 /* Prepare */
1429 cbreak();
1430 noecho();
1431 nonl();
1432 raw();
1433
1434 /* Tell curses to rewrite escape sequences to KEY_UP and friends */
1435 keypad(stdscr, true);
1436
1437 /* Extract the game keymap */
1438 keymap_game_prepare();
1439
1440 /* Now prepare the term(s) */
1441 if (term_count > 1)
1442 {
1443 int rows, cols, y, x;
1444 int next_win = 0;
1445 for (i = 0; i < term_count; i++) {
1446 /* Get the terminal dimensions; if the user asked for a big screen
1447 * then we'll put the whole screen in term 0; otherwise we'll divide
1448 * it amongst the available terms */
1449 get_gcu_term_size(i, &rows, &cols, &y, &x);
1450
1451 /* Skip non-existant windows */
1452 if (rows <= 0 || cols <= 0) continue;
1453
1454 /* Create a term */
1455 term_data_init_gcu(&data[next_win], rows, cols, y, x);
1456
1457 /* Remember the term */
1458 angband_term[next_win] = &data[next_win].t;
1459
1460 /* One more window */
1461 next_win++;
1462 }
1463 }
1464 else
1465 /* Parse Args and Prepare the Terminals. Rectangles are specified
1466 as Width x Height, right? The game will allow you to have two
1467 strips of extra terminals, one on the right and one on the bottom.
1468 The map terminal will than fit in as big as possible in the remaining
1469 space.
1470
1471 Examples:
1472 angband -mgcu -- -right 30x27,* -bottom *x7 will layout as
1473
1474 Term-0: Map (COLS-30)x(LINES-7) | Term-1: 30x27
1475 --------------------------------|----------------------
1476 <----Term-3: (COLS-30)x7------->| Term-2: 30x(LINES-27)
1477
1478 composband -mgcu -- -bottom *x7 -right 30x27,* will layout as
1479
1480 Term-0: Map (COLS-30)x(LINES-7) | Term-2: 30x27
1481 |------------------------------
1482 | Term-3: 30x(LINES-27)
1483 ---------------------------------------------------------------
1484 <----------Term-1: (COLS)x7----------------------------------->
1485
1486 Notice the effect on the bottom terminal by specifying its argument
1487 second or first. Notice the sequence numbers for the various terminals
1488 as you will have to blindly configure them in the window setup screen.
1489
1490 EDIT: Added support for -left and -top.
1491 */
1492 {
1493 rect_t remaining = rect(0, 0, COLS, LINES);
1494 int spacer_cx = 1;
1495 int spacer_cy = 1;
1496 int next_term = 1;
1497 int term_ct = 1;
1498
1499 for (i = 1; i < argc; i++)
1500 {
1501 if (streq(argv[i], "-spacer"))
1502 {
1503 char *pe, *ystr;
1504 long lv;
1505
1506 i++;
1507 if (i >= argc)
1508 quit("Missing size specifier for -spacer");
1509 lv = strtol(argv[i], &pe, 10);
1510 /*
1511 * Also reject INT_MIN and INT_MAX so do not have to check
1512 * errno to detect overflow on platforms where sizeof(int) ==
1513 * sizeof(long).
1514 */
1515 if (pe == argv[i] || *pe != 'x' || lv <= INT_MIN ||
1516 lv >= INT_MAX) {
1517 quit_fmt("Invalid specification for -spacer; got %s",
1518 argv[i]);
1519 }
1520 spacer_cx = (int)lv;
1521 ystr = pe + 1;
1522 lv = strtol(ystr, &pe, 10);
1523 if (pe == ystr || !contains_only_spaces(pe) ||
1524 lv <= INT_MIN || lv >= INT_MAX) {
1525 quit_fmt("Invalid specification for -spacer; got %s",
1526 argv[i]);
1527 }
1528 spacer_cy = (int)lv;
1529 }
1530 else if (streq(argv[i], "-right") || streq(argv[i], "-left"))
1531 {
1532 const char *arg, *tmp;
1533 bool left = streq(argv[i], "-left");
1534 int cx, cys[MAX_TERM_DATA] = {0}, ct, j, x, y;
1535
1536 i++;
1537 if (i >= argc)
1538 quit(format("Missing size specifier for -%s", left ? "left" : "right"));
1539
1540 arg = argv[i];
1541 tmp = strchr(arg, 'x');
1542 if (!tmp)
1543 quit(format("Expected something like -%s 60x27,* for two %s hand terminals of 60 columns, the first 27 lines and the second whatever is left.", left ? "left" : "right", left ? "left" : "right"));
1544 cx = atoi(arg);
1545 remaining.cx -= cx;
1546 if (left)
1547 {
1548 x = remaining.x;
1549 y = remaining.y;
1550 remaining.x += cx;
1551 }
1552 else
1553 {
1554 x = remaining.x + remaining.cx;
1555 y = remaining.y;
1556 }
1557 remaining.cx -= spacer_cx;
1558 if (left)
1559 remaining.x += spacer_cx;
1560
1561 tmp++;
1562 ct = _parse_size_list(tmp, cys, MAX_TERM_DATA);
1563 for (j = 0; j < ct; j++)
1564 {
1565 int cy = cys[j];
1566 if (y + cy > remaining.y + remaining.cy)
1567 cy = remaining.y + remaining.cy - y;
1568 if (next_term >= MAX_TERM_DATA)
1569 quit(format("Too many terminals. Only %d are allowed.", MAX_TERM_DATA));
1570 if (cy <= 0)
1571 {
1572 quit(format("Out of bounds in -%s: %d is too large (%d rows max for this strip)",
1573 left ? "left" : "right", cys[j], remaining.cy));
1574 }
1575 data[next_term++].r = rect(x, y, cx, cy);
1576 y += cy + spacer_cy;
1577 term_ct++;
1578 }
1579 }
1580 else if (streq(argv[i], "-top") || streq(argv[i], "-bottom"))
1581 {
1582 const char *arg, *tmp;
1583 bool top = streq(argv[i], "-top");
1584 int cy, cxs[MAX_TERM_DATA] = {0}, ct, j, x, y;
1585
1586 i++;
1587 if (i >= argc)
1588 quit(format("Missing size specifier for -%s", top ? "top" : "bottom"));
1589
1590 arg = argv[i];
1591 tmp = strchr(arg, 'x');
1592 if (!tmp)
1593 quit(format("Expected something like -%s *x7 for a single %s terminal of 7 lines using as many columns as are available.", top ? "top" : "bottom", top ? "top" : "bottom"));
1594 tmp++;
1595 cy = atoi(tmp);
1596 ct = _parse_size_list(arg, cxs, MAX_TERM_DATA);
1597
1598 remaining.cy -= cy;
1599 if (top)
1600 {
1601 x = remaining.x;
1602 y = remaining.y;
1603 remaining.y += cy;
1604 }
1605 else
1606 {
1607 x = remaining.x;
1608 y = remaining.y + remaining.cy;
1609 }
1610 remaining.cy -= spacer_cy;
1611 if (top)
1612 remaining.y += spacer_cy;
1613
1614 tmp++;
1615 for (j = 0; j < ct; j++)
1616 {
1617 int cx = cxs[j];
1618 if (x + cx > remaining.x + remaining.cx)
1619 cx = remaining.x + remaining.cx - x;
1620 if (next_term >= MAX_TERM_DATA)
1621 quit(format("Too many terminals. Only %d are allowed.", MAX_TERM_DATA));
1622 if (cx <= 0)
1623 {
1624 quit(format("Out of bounds in -%s: %d is too large (%d cols max for this strip)",
1625 top ? "top" : "bottom", cxs[j], remaining.cx));
1626 }
1627 data[next_term++].r = rect(x, y, cx, cy);
1628 x += cx + spacer_cx;
1629 term_ct++;
1630 }
1631 }
1632 }
1633
1634 /* Map Terminal */
1635 if (remaining.cx < MIN_TERM0_COLS || remaining.cy < MIN_TERM0_LINES)
1636 quit(format("Failed: angband needs an %dx%d map screen, not %dx%d", MIN_TERM0_COLS, MIN_TERM0_LINES, remaining.cx, remaining.cy));
1637 data[0].r = remaining;
1638 term_data_init(&data[0]);
1639 angband_term[0] = Term;
1640
1641 /* Child Terminals */
1642 for (next_term = 1; next_term < term_ct; next_term++)
1643 {
1644 term_data_init(&data[next_term]);
1645 angband_term[next_term] = Term;
1646 }
1647 }
1648
1649 /* Activate the "Angband" window screen */
1650 Term_activate(&data[0].t);
1651
1652 /* Remember the active screen */
1653 term_screen = &data[0].t;
1654
1655 /* Success */
1656 return (0);
1657 }
1658
1659 #endif /* USE_GCU */