1 /* File: main-gcu.c */
4 * Copyright (c) 1997 Ben Harrison, and others
6 * This software may be copied and distributed for educational, research,
7 * and not for profit purposes provided that this copyright and statement
8 * are included in all such copies.
11 /* Purpose: Allow use of Unix "curses" with Angband -BEN- */
14 * This file has been modified to use multiple text windows if your screen
15 * is larger than 80x25. By Keldon Jones (keldon@umr.edu).
17 * Also included is Keldon Jones patch to get better colors. To switch to
18 * a term that supports this, see this posting:
20 * From keldon@umr.edu Thu Apr 01 05:40:14 1999
21 * Sender: KELDON JONES <keldon@saucer.cc.umr.edu>
22 * From: Keldon Jones <keldon@umr.edu>
23 * Subject: Re: Linux colour prob (Or: question for Greg)
24 * Newsgroups: rec.games.roguelike.angband
25 * References: <slrn7g1jlp.gj9.scarblac-spamtrap@flits104-37.flits.rug.nl> <3700f96b.1593384@news.polsl.gliwice.pl> <slrn7g36er.fm4.wooledge@jekyll.local>
26 * X-Newsreader: TIN [UNIX 1.3 unoff BETA 970625; 9000/780 HP-UX B.10.20]
27 * NNTP-Posting-Host: saucer.cc.umr.edu
28 * X-NNTP-Posting-Host: saucer.cc.umr.edu
29 * Message-ID: <370306be.0@news.cc.umr.edu>
30 * Date: 1 Apr 99 05:40:14 GMT
31 * Organization: University of Missouri - Rolla
34 * xs4all!xs4all!newsfeed.wirehub.nl!news-peer.gip.net!news.gsl.net!gip.net!news.he.net!mercury.cts.com!alpha.sky.net!news.missouri.edu!news.cc.umr.edu!not-for-mail
35 * Xref: xs4all rec.games.roguelike.angband:86332
37 * Greg Wooledge <wooledge@kellnet.com> wrote:
38 * > Gwidon S. Naskrent (naskrent@artemida.amu.edu.pl) wrote:
40 * > >On 30 Mar 1999 13:17:18 GMT, scarblac-spamtrap@pino.selwerd.cx (Remco
43 * > >>I recently switched to Linux, and *bands work fine. I like
44 * > >>to play them in consoles, not in X. However, colour is wrong.
45 * > >>"Slate" and "light slate" are always light blue, instead
46 * > >>of some shade of grey. Colours are fine in X.
48 * > I actually noticed the Linux console color issue a very long time ago,
49 * > but since I always play under X, I never really investigated it.
51 * > You're absolutely right, though -- the Linux console colors are not
52 * > "right" for Angband.
54 * I've noticed this myself, so I spent the evening fixing it.
55 * Well, sorta fixing it. It's not perfect yet, and it may not be
56 * possible to get it perfect with VGA hardware and/or the current
59 * > OK, reading on in terminfo(5):
62 * > Most color terminals are either `Tektronix-like' or `HP-
63 * > like'. Tektronix-like terminals have a predefined set of
64 * > N colors (where N usually 8), and can set character-cell
65 * > foreground and background characters independently, mixing
66 * > them into N * N color-pairs. On HP-like terminals, the
67 * > use must set each color pair up separately (foreground and
68 * > background are not independently settable). Up to M
69 * > color-pairs may be set up from 2*M different colors.
70 * > ANSI-compatible terminals are Tektronix-like.
72 * > The "linux" terminfo entry is definitely in the "Tektronix-like" family.
73 * > It has the "setaf" and "setab" capabilities for setting the foreground
74 * > and background colors to one of 8 basically hard-coded values:
76 * > Color #define Value RGB
77 * > black COLOR_BLACK 0 0, 0, 0
78 * > red COLOR_RED 1 max,0,0
79 * > green COLOR_GREEN 2 0,max,0
80 * > yellow COLOR_YELLOW 3 max,max,0
81 * > blue COLOR_BLUE 4 0,0,max
82 * > magenta COLOR_MAGENTA 5 max,0,max
83 * > cyan COLOR_CYAN 6 0,max,max
84 * > white COLOR_WHITE 7 max,max,max
86 * Well, not quite. Using certain escape sequences, an
87 * application (or better yet, curses) can redefine the colors (at
88 * least some of them) and then those are used. Read the
89 * curs_color manpage, and the part about "ccc" and "initc" in the
90 * terminfo manpage. This is what the part of main-gcu inside the
91 * "if (can_fix_color)" code does.
93 * > So, what does this mean to the Angband player? Well, it means that
94 * > either there's nothing you can do about the console colors as long as
95 * > straight curses/ncurses is used, or if there is something to be done,
96 * > I'm not clever enough to figure out how to do it.
98 * Well, it is possible, though you have to patch main-gcu
99 * and edit a terminfo entry. Apparently the relevant code in
100 * main-gcu was never tested (it's broken in at least one major
101 * way). Apply the patch at the end of this message (notice that
102 * we need to define REDEFINE_COLORS at some point near the
103 * beginning of the file).
104 * Next, write this termcap entry to a file:
106 * linux-c|linux console 1.3.6+ with private palette for each virtual console,
108 * colors#16, pairs#64,
109 * initc=\E]P%x%p1%{16}%/%02x%p1%{16}%/%02x%p1%{16}%/%02x,
113 * and run "tic" on it to produce a new terminfo entry called
114 * "linux-c". Especially note the "ccc" flag which says that we
115 * can redefine colors. The ugly "initc" string is what tells
116 * the console how to redefine a color. Now, just set your TERM
117 * variable to "linux-c" and try Angband again. If I've
118 * remembered to tell you everything that I've done, you should
119 * get the weird light-blue slate changed to a gray.
120 * Now, there are still lots of problems with this.
121 * Something (I don't think it's curses, either the kernel or
122 * the hardware itself) seems to be ignoring my color changes to
123 * colors 6 and 7, which is annoying. Also, the normal "white"
124 * color is now way too bright, but it's now necessary to
125 * distinguish it from the other grays.
126 * The kernel seems to support 16 colors, but you can
127 * only switch to 8 of those, due to VT102 compatibility, it
128 * seems. I think it would be possible to patch the kernel and
129 * allow all 16 colors to be used, but I haven't built up the
130 * nerve to try that yet.
131 * Let me know if you can improve on this any. Some of
132 * this may actually work differently on other hardware (ugh).
139 * To use this file, you must define "USE_GCU" in the Makefile.
141 * Hack -- note that "angband.h" is included AFTER the #ifdef test.
142 * This was necessary because of annoying "curses.h" silliness.
144 * Note that this file is not "intended" to support non-Unix machines,
145 * nor is it intended to support VMS or other bizarre setups.
147 * Also, this package assumes that the underlying "curses" handles both
148 * the "nonl()" and "cbreak()" commands correctly, see the "OPTION" below.
150 * This code should work with most versions of "curses" or "ncurses",
151 * and the "main-ncu.c" file (and USE_NCU define) are no longer used.
153 * See also "USE_CAP" and "main-cap.c" for code that bypasses "curses"
154 * and uses the "termcap" information directly, or even bypasses the
155 * "termcap" information and sends direct vt100 escape sequences.
157 * XXX XXX XXX Consider the use of "savetty()" and "resetty()".
160 #include "game-option/runtime-arguments.h"
161 #include "game-option/special-options.h"
162 #include "io/exit-panic.h"
163 #include "io/files-util.h"
164 #include "locale/japanese.h"
165 #include "main/sound-definitions-table.h"
166 #include "main/sound-of-music.h"
167 #include "system/angband-version.h"
168 #include "system/angband.h"
169 #include "system/player-type-definition.h"
170 #include "term/gameterm.h"
171 #include "term/term-color-types.h"
172 #include "term/z-form.h"
173 #include "util/angband-files.h"
174 #include "view/display-map.h"
179 * Hack -- play games with "bool"
181 #if __STDC_VERSION__ < 199901L
186 * Include the proper "header" file
191 * Simple rectangle type
198 /* Trivial rectangle utility to make code a bit more readable */
199 static rect_t rect(int x, int y, int cx, int cy)
210 * Information about a term
218 /* Max number of windows on screen */
219 #define MAX_TERM_DATA 8
221 /* Minimum main term size */
222 #define MIN_TERM0_LINES 24
223 #define MIN_TERM0_COLS 80
225 /* Information about our windows */
226 static term_data data[MAX_TERM_DATA];
229 * Hack -- try to guess which systems use what commands
230 * Hack -- allow one of the "USE_Txxxxx" flags to be pre-set.
231 * Mega-Hack -- try to guess when "POSIX" is available.
232 * If the user defines two of these, we will probably crash.
234 #if !defined(USE_TCHARS)
235 #if defined(_POSIX_VERSION)
243 * Try redefining the colors at startup.
245 #define REDEFINE_COLORS
251 #include <sys/ioctl.h>
256 * One version needs this file
259 #include <sys/ioctl.h>
264 * The other needs this file
267 #include <sys/file.h>
268 #include <sys/ioctl.h>
269 #include <sys/param.h>
270 #include <sys/resource.h>
271 #include <sys/types.h>
277 * XXX XXX Hack -- POSIX uses "O_NONBLOCK" instead of "O_NDELAY"
279 * They should both work due to the "(i != 1)" test below.
282 #define O_NDELAY O_NONBLOCK
286 * OPTION: some machines lack "cbreak()"
287 * On these machines, we use an older definition
289 /* #define cbreak() crmode() */
292 * OPTION: some machines cannot handle "nonl()" and "nl()"
293 * On these machines, we can simply ignore those commands.
298 static concptr ANGBAND_DIR_XTRA_SOUND;
302 * Flag set once "sound" has been initialized
304 static bool can_use_sound = false;
307 * An array of sound file names
309 static concptr sound_file[SOUND_MAX];
312 * Save the "normal" and "angband" terminal settings
317 static struct termios norm_termios;
319 static struct termios game_termios;
325 static struct termio norm_termio;
327 static struct termio game_termio;
332 static struct ltchars norm_speciax_chars;
333 static struct sgttyb norm_ttyb;
334 static struct tchars norm_tchars;
335 static int norm_locax_chars;
337 static struct ltchars game_speciax_chars;
338 static struct sgttyb game_ttyb;
339 static struct tchars game_tchars;
340 static int game_locax_chars;
344 * Hack -- Number of initialized "term" structures
346 static int active = 0;
350 * Hack -- define "A_BRIGHT" to be "A_BOLD", because on many
351 * machines, "A_BRIGHT" produces ugly "inverse" video.
354 #define A_BRIGHT A_BOLD
358 * Software flag -- we are allowed to use color
360 static int can_use_color = false;
363 * Software flag -- we are allowed to change the colors
365 static int can_fix_color = false;
368 * Simple Angband to Curses color conversion table
370 static int colortable[16];
373 * Background color we should draw with; either BLACK or DEFAULT
375 static int bg_color = COLOR_BLACK;
380 * Place the "keymap" into its "normal" state
382 static void keymap_norm(void)
385 /* restore the saved values of the special chars */
386 (void)tcsetattr(0, TCSAFLUSH, &norm_termios);
390 /* restore the saved values of the special chars */
391 (void)ioctl(0, TCSETA, (char *)&norm_termio);
395 /* restore the saved values of the special chars */
396 (void)ioctl(0, TIOCSLTC, (char *)&norm_speciax_chars);
397 (void)ioctl(0, TIOCSETP, (char *)&norm_ttyb);
398 (void)ioctl(0, TIOCSETC, (char *)&norm_tchars);
399 (void)ioctl(0, TIOCLSET, (char *)&norm_locax_chars);
404 * Place the "keymap" into the "game" state
406 static void keymap_game(void)
409 /* restore the saved values of the special chars */
410 (void)tcsetattr(0, TCSAFLUSH, &game_termios);
414 /* restore the saved values of the special chars */
415 (void)ioctl(0, TCSETA, (char *)&game_termio);
419 /* restore the saved values of the special chars */
420 (void)ioctl(0, TIOCSLTC, (char *)&game_speciax_chars);
421 (void)ioctl(0, TIOCSETP, (char *)&game_ttyb);
422 (void)ioctl(0, TIOCSETC, (char *)&game_tchars);
423 (void)ioctl(0, TIOCLSET, (char *)&game_locax_chars);
428 * Save the normal keymap
430 static void keymap_norm_prepare(void)
433 /* Get the normal keymap */
434 tcgetattr(0, &norm_termios);
438 /* Get the normal keymap */
439 (void)ioctl(0, TCGETA, (char *)&norm_termio);
443 /* Get the normal keymap */
444 (void)ioctl(0, TIOCGETP, (char *)&norm_ttyb);
445 (void)ioctl(0, TIOCGLTC, (char *)&norm_speciax_chars);
446 (void)ioctl(0, TIOCGETC, (char *)&norm_tchars);
447 (void)ioctl(0, TIOCLGET, (char *)&norm_locax_chars);
452 * Save the keymaps (normal and game)
454 static void keymap_game_prepare(void)
457 /* Acquire the current mapping */
458 tcgetattr(0, &game_termios);
460 /* Force "Ctrl-C" to interupt */
461 game_termios.c_cc[VINTR] = (char)3;
463 /* Force "Ctrl-Z" to suspend */
464 game_termios.c_cc[VSUSP] = (char)26;
466 /* Hack -- Leave "VSTART/VSTOP" alone */
468 /* Disable the standard control characters */
469 game_termios.c_cc[VQUIT] = (char)-1;
470 game_termios.c_cc[VERASE] = (char)-1;
471 game_termios.c_cc[VKILL] = (char)-1;
472 game_termios.c_cc[VEOF] = (char)-1;
473 game_termios.c_cc[VEOL] = (char)-1;
475 /* Normally, block until a character is read */
476 game_termios.c_cc[VMIN] = 1;
477 game_termios.c_cc[VTIME] = 0;
481 /* Acquire the current mapping */
482 (void)ioctl(0, TCGETA, (char *)&game_termio);
484 /* Force "Ctrl-C" to interupt */
485 game_termio.c_cc[VINTR] = (char)3;
487 /* Force "Ctrl-Z" to suspend */
488 game_termio.c_cc[VSUSP] = (char)26;
490 /* Disable the standard control characters */
491 game_termio.c_cc[VQUIT] = (char)-1;
492 game_termio.c_cc[VERASE] = (char)-1;
493 game_termio.c_cc[VKILL] = (char)-1;
494 game_termio.c_cc[VEOF] = (char)-1;
495 game_termio.c_cc[VEOL] = (char)-1;
497 /* Normally, block until a character is read */
498 game_termio.c_cc[VMIN] = 1;
499 game_termio.c_cc[VTIME] = 0;
503 /* Get the default game characters */
504 (void)ioctl(0, TIOCGETP, (char *)&game_ttyb);
505 (void)ioctl(0, TIOCGLTC, (char *)&game_speciax_chars);
506 (void)ioctl(0, TIOCGETC, (char *)&game_tchars);
507 (void)ioctl(0, TIOCLGET, (char *)&game_locax_chars);
509 /* Force suspend (^Z) */
510 game_speciax_chars.t_suspc = (char)26;
512 /* Cancel some things */
513 game_speciax_chars.t_dsuspc = (char)-1;
514 game_speciax_chars.t_rprntc = (char)-1;
515 game_speciax_chars.t_flushc = (char)-1;
516 game_speciax_chars.t_werasc = (char)-1;
517 game_speciax_chars.t_lnextc = (char)-1;
519 /* Force interupt (^C) */
520 game_tchars.t_intrc = (char)3;
522 /* Force start/stop (^Q, ^S) */
523 game_tchars.t_startc = (char)17;
524 game_tchars.t_stopc = (char)19;
526 /* Cancel some things */
527 game_tchars.t_quitc = (char)-1;
528 game_tchars.t_eofc = (char)-1;
529 game_tchars.t_brkc = (char)-1;
536 static errr game_term_xtra_gcu_alive(int v)
539 /* Go to normal keymap mode */
547 /* Hack -- make sure the cursor is visible */
548 term_xtra(TERM_XTRA_SHAPE, 1);
550 /* Flush the curses buffer */
553 /* this moves curses to bottom right corner */
554 mvcur(getcury(curscr), getcurx(curscr), LINES - 1, 0);
559 /* Flush the output */
560 (void)fflush(stdout);
562 /* Restore the settings */
567 /* Go to angband keymap mode */
575 * Check for existance of a file
577 static bool check_file(concptr s)
592 static bool init_sound(void)
594 /* Initialize once */
596 return can_use_sound;
602 /* Prepare the sounds */
603 for (i = 1; i < SOUND_MAX; i++) {
604 /* Extract name of sound file */
605 std::string wav = angband_sound_name[i];
608 /* Access the sound */
609 path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_SOUND, wav.data());
611 /* Save the sound filename, if it exists */
612 if (check_file(buf)) {
613 sound_file[i] = string_make(buf);
617 /* Sound available */
618 can_use_sound = true;
619 return can_use_sound;
623 * Init the "curses" system
625 static void game_term_init_gcu(term_type *t)
627 term_data *td = (term_data *)(t->data);
629 /* Count init's, handle first */
634 /* Erase the screen */
635 (void)wclear(td->win);
637 /* Reset the cursor */
638 (void)wmove(td->win, 0, 0);
641 (void)wrefresh(td->win);
648 * Nuke the "curses" system
650 static void game_term_nuke_gcu(term_type *t)
652 term_data *td = (term_data *)(t->data);
654 /* Delete this window */
657 /* Count nuke's, handle last */
662 /* Hack -- make sure the cursor is visible */
663 term_xtra(TERM_XTRA_SHAPE, 1);
666 /* Reset colors to defaults */
670 /* This moves curses to bottom right corner */
671 mvcur(getcury(curscr), getcurx(curscr), LINES - 1, 0);
673 /* Flush the curses buffer */
679 /* Flush the output */
680 (void)fflush(stdout);
687 * Push multiple keys reversal
689 static void term_string_push(char *buf)
691 int i, l = strlen(buf);
692 for (i = l; i >= 0; i--) {
693 term_key_push(buf[i]);
700 * Process events, with optional wait
702 static errr game_term_xtra_gcu_event(int v)
711 /* Paranoia -- Wait for it */
712 nodelay(stdscr, false);
717 /* Broken input is special */
719 exit_game_panic(p_ptr);
722 exit_game_panic(p_ptr);
727 /* Do not wait for it */
728 nodelay(stdscr, true);
730 while ((i = getch()) != EOF) {
732 exit_game_panic(p_ptr);
735 if (bp == &buf[255]) {
740 /* Wait for it next time */
741 nodelay(stdscr, false);
745 char eucbuf[sizeof(buf)];
746 /* strlen + 1 を渡して文字列終端('\0')を含めて変換する */
747 if (utf8_to_euc(buf, strlen(buf) + 1, eucbuf, sizeof(eucbuf)) < 0) {
751 term_string_push(_(eucbuf, buf));
756 /* Do not wait for it */
757 nodelay(stdscr, true);
759 /* Check for keypresses */
762 /* Wait for it next time */
763 nodelay(stdscr, false);
773 /* Enqueue the keypress */
781 #else /* USE_GETCH */
784 * Process events (with optional wait)
786 static errr game_term_xtra_gcu_event(int v)
796 /* Wait for one byte */
797 i = read(0, bp++, 1);
799 /* Hack -- Handle bizarre "errors" */
800 if ((i <= 0) && (errno != EINTR)) {
801 exit_game_panic(p_ptr);
804 /* Get the current flags for stdin */
805 k = fcntl(0, F_GETFL, 0);
812 /* Tell stdin not to block */
813 if (fcntl(0, F_SETFL, k | O_NDELAY) >= 0) {
814 if ((i = read(0, bp, 254)) > 0) {
818 /* Replace the flags for stdin */
819 if (fcntl(0, F_SETFL, k)) {
826 char eucbuf[sizeof(buf)];
827 /* strlen + 1 を渡して文字列終端('\0')を含めて変換する */
828 if (utf8_to_euc(buf, strlen(buf) + 1, eucbuf, sizeof(eucbuf)) < 0) {
832 term_string_push(_(eucbuf, buf));
837 /* Get the current flags for stdin */
838 k = fcntl(0, F_GETFL, 0);
845 /* Tell stdin not to block */
846 if (fcntl(0, F_SETFL, k | O_NDELAY) < 0) {
850 /* Read one byte, if possible */
853 /* Replace the flags for stdin */
854 if (fcntl(0, F_SETFL, k)) {
858 /* Ignore "invalid" keys */
859 if ((i != 1) || (!buf[0])) {
863 /* Enqueue the keypress */
864 term_key_push(buf[0]);
871 #endif /* USE_GETCH */
874 * Hack -- make a sound
876 static errr game_term_xtra_gcu_sound(int v)
884 if ((v < 0) || (v >= SOUND_MAX)) {
889 if (!sound_file[v]) {
893 std::string buf = "./gcusound.sh ";
894 buf.append(sound_file[v]).append("\n");
895 return system(buf.data()) < 0;
898 static int scale_color(int i, int j, int scale)
900 return (angband_color_table[i][j] * (scale - 1) + 127) / 255;
903 static int create_color(int i, int scale)
905 int r = scale_color(i, 1, scale);
906 int g = scale_color(i, 2, scale);
907 int b = scale_color(i, 3, scale);
908 int rgb = 16 + scale * scale * r + scale * g + b;
909 /* In the case of white and black we need to use the ANSI colors */
910 if (r == g && g == b) {
924 static errr game_term_xtra_gcu_react(void)
929 if (!can_change_color()) {
930 if (COLORS == 256 || COLORS == 88) {
931 /* If we have more than 16 colors, find the best matches. These numbers
932 * correspond to xterm/rxvt's builtin color numbers--they do not
933 * correspond to curses' constants OR with curses' color pairs.
935 * XTerm has 216 (6*6*6) RGB colors, with each RGB setting 0-5.
936 * RXVT has 64 (4*4*4) RGB colors, with each RGB setting 0-3.
938 * Both also have the basic 16 ANSI colors, plus some extra grayscale
939 * colors which we do not use.
941 int scale = COLORS == 256 ? 6 : 4;
942 for (int i = 0; i < 16; i++) {
943 int fg = create_color(i, scale);
944 init_pair(i + 1, fg, bg_color);
945 colortable[i] = COLOR_PAIR(i + 1) | A_NORMAL;
949 for (int i = 0; i < 16; ++i) {
951 (angband_color_table[i][1] * 1000) / 255,
952 (angband_color_table[i][2] * 1000) / 255,
953 (angband_color_table[i][3] * 1000) / 255);
964 * Handle a "special request"
966 static errr game_term_xtra_gcu(int n, int v)
968 term_data *td = (term_data *)(game_term->data);
970 /* Analyze the request */
973 case TERM_XTRA_CLEAR:
975 (void)werase(td->win);
979 case TERM_XTRA_NOISE:
980 return write(1, "\007", 1) != 1;
982 /* Make a special sound */
983 case TERM_XTRA_SOUND:
984 return game_term_xtra_gcu_sound(v);
986 /* Flush the Curses buffer */
987 case TERM_XTRA_FRESH:
988 (void)wrefresh(td->win);
991 /* Change the cursor visibility */
992 case TERM_XTRA_SHAPE:
996 /* Suspend/Resume curses */
997 case TERM_XTRA_ALIVE:
998 return game_term_xtra_gcu_alive(v);
1000 /* Process events */
1001 case TERM_XTRA_EVENT:
1002 return game_term_xtra_gcu_event(v);
1005 case TERM_XTRA_FLUSH:
1006 while (!game_term_xtra_gcu_event(false)) {
1012 case TERM_XTRA_DELAY:
1016 /* React to events */
1017 case TERM_XTRA_REACT:
1018 game_term_xtra_gcu_react();
1027 * Actually MOVE the hardware cursor
1029 static errr game_term_curs_gcu(int x, int y)
1031 term_data *td = (term_data *)(game_term->data);
1033 /* Literally move the cursor */
1034 wmove(td->win, y, x);
1041 * Erase a grid of space
1042 * Hack -- try to be "semi-efficient".
1044 static errr game_term_wipe_gcu(int x, int y, int n)
1046 term_data *td = (term_data *)(game_term->data);
1049 wmove(td->win, y, x);
1051 /* Clear to end of line */
1052 if (x + n >= td->t.wid) {
1056 /* Clear some characters */
1059 waddch(td->win, ' ');
1067 #ifdef USE_NCURSES_ACS
1069 * this function draws some ACS characters on the screen
1070 * for DOS-based users: these are the graphical chars (blocks, lines etc)
1072 * unix-gurus: before you start adding other attributes like A_REVERSE
1073 * think hard about how map_info() in cave.c should handle the color
1074 * of something that we here draw in reverse. It's not so simple, alas.
1076 static void game_term_acs_text_gcu(int x, int y, int n, byte a, concptr s)
1078 term_data *td = (term_data *)(game_term->data);
1081 /* position the cursor */
1082 wmove(td->win, y, x);
1086 wattrset(td->win, colortable[a & 0x0F]);
1089 for (i = 0; i < n; i++) {
1090 /* add acs_map of a */
1091 waddch(td->win, acs_map[(int)s[i]]);
1093 wattrset(td->win, WA_NORMAL);
1098 * Place some text on the screen using an attribute
1100 static errr game_term_text_gcu(int x, int y, int n, byte a, concptr s)
1102 term_data *td = (term_data *)(game_term->data);
1104 #ifdef USE_NCURSES_ACS
1105 /* do we have colors + 16 ? */
1106 /* then call special routine for drawing special characters */
1108 game_term_acs_text_gcu(x, y, n, a, s);
1113 /* Move the cursor and dump the string */
1114 wmove(td->win, y, x);
1118 if (can_use_color) {
1119 wattrset(td->win, colortable[a & 0x0F]);
1125 int text_len = euc_to_utf8(s, n, text, sizeof(text));
1131 waddnstr(td->win, _(text, s), _(text_len, n));
1138 * Create a window for the given "term_data" argument.
1140 * Assumes legal arguments.
1142 static errr term_data_init_gcu(term_data *td, int rows, int cols, int y, int x)
1144 term_type *t = &td->t;
1146 /* Make sure the window has a positive size */
1147 if (rows <= 0 || cols <= 0) {
1151 /* Create a window */
1152 td->win = newwin(rows, cols, y, x);
1154 /* Make sure we succeed */
1156 plog("Failed to setup curses window.");
1160 /* Initialize the term */
1161 term_init(t, cols, rows, 256);
1163 /* Avoid the bottom right corner */
1164 t->icky_corner = true;
1166 /* Erase with "white space" */
1167 t->attr_blank = TERM_WHITE;
1168 t->char_blank = ' ';
1170 /* Set some hooks */
1171 t->init_hook = game_term_init_gcu;
1172 t->nuke_hook = game_term_nuke_gcu;
1174 /* Set some more hooks */
1175 t->text_hook = game_term_text_gcu;
1176 t->wipe_hook = game_term_wipe_gcu;
1177 t->curs_hook = game_term_curs_gcu;
1178 t->xtra_hook = game_term_xtra_gcu;
1193 static errr term_data_init(term_data *td)
1195 return term_data_init_gcu(td, td->r.cy, td->r.cx, td->r.y, td->r.x);
1198 /* Parse 27,15,*x30 up to the 'x'. * gets converted to a big number
1199 Parse 32,* until the end. Return count of numbers parsed */
1200 static int _parse_size_list(const char *arg, int sizes[], int max)
1203 const char *start = arg;
1204 const char *stop = arg;
1207 if (!*stop || !isdigit(*stop)) {
1211 if (*start == '*') {
1214 /* rely on atoi("23,34,*") -> 23
1215 otherwise, copy [start, stop) into a new buffer first.*/
1216 sizes[i] = atoi(start);
1219 if (!*stop || *stop != ',') {
1232 static void hook_quit(concptr str)
1242 * Prepare "curses" for use by the file "term.c"
1244 * Installs the "hook" functions defined above, and then activates
1245 * the main screen "term", which clears the screen and such things.
1247 * Someone should really check the semantics of "initscr()"
1249 errr init_gcu(int argc, char *argv[])
1253 int num_term = 4, next_win = 0;
1260 setlocale(LC_ALL, "");
1262 /* Build the "sound" path */
1263 path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "sound");
1265 /* Allocate the path */
1266 ANGBAND_DIR_XTRA_SOUND = string_make(path);
1268 /* Extract the normal keymap */
1269 keymap_norm_prepare();
1271 bool nobigscreen = false;
1274 for (i = 1; i < argc; i++) {
1275 if (prefix(argv[i], "-o")) {
1280 /* Initialize for others systems */
1281 if (initscr() == (WINDOW *)ERR) {
1285 /* Activate hooks */
1286 quit_aux = hook_quit;
1287 core_aux = hook_quit;
1289 /* Hack -- Require large screen, or Quit with message */
1290 i = ((LINES < 24) || (COLS < 80));
1292 quit_fmt("%s needs an 80x24 'curses' screen", std::string(VARIANT_NAME).data());
1297 /*** Init the Color-pairs and set up a translation table ***/
1299 /* Do we have color, and enough color, available? */
1300 can_use_color = ((start_color() != ERR) && has_colors() && (COLORS >= 8) && (COLOR_PAIRS >= 8));
1302 #ifdef REDEFINE_COLORS
1303 /* Can we change colors? */
1304 can_fix_color = (can_use_color && can_change_color() && (COLORS >= 16) && (COLOR_PAIRS > 8));
1307 /* Attempt to use customized colors */
1308 if (can_fix_color) {
1309 /* Prepare the color pairs */
1310 for (i = 1; i <= 15; i++) {
1311 if (init_pair(i, i, 0) == ERR) {
1312 quit("Color pair init failed");
1315 colortable[i] = COLOR_PAIR(i);
1316 game_term_xtra_gcu_react();
1319 /* Attempt to use colors */
1320 else if (can_use_color) {
1321 /* Color-pair 0 is *always* WHITE on BLACK */
1323 /* Prepare the color pairs */
1324 init_pair(1, COLOR_RED, COLOR_BLACK);
1325 init_pair(2, COLOR_GREEN, COLOR_BLACK);
1326 init_pair(3, COLOR_YELLOW, COLOR_BLACK);
1327 init_pair(4, COLOR_BLUE, COLOR_BLACK);
1328 init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
1329 init_pair(6, COLOR_CYAN, COLOR_BLACK);
1330 init_pair(7, COLOR_BLACK, COLOR_BLACK);
1332 /* Prepare the "Angband Colors" -- Bright white is too bright */
1333 /* Changed in Drangband. Cyan as grey sucks -- -TM- */
1334 colortable[0] = (COLOR_PAIR(7) | A_NORMAL); /* Black */
1335 colortable[1] = (COLOR_PAIR(0) | A_BRIGHT); /* White */
1336 colortable[2] = (COLOR_PAIR(0) | A_NORMAL); /* Grey XXX */
1337 colortable[3] = (COLOR_PAIR(1) | A_BRIGHT); /* Orange XXX */
1338 colortable[4] = (COLOR_PAIR(1) | A_NORMAL); /* Red */
1339 colortable[5] = (COLOR_PAIR(2) | A_NORMAL); /* Green */
1340 colortable[6] = (COLOR_PAIR(4) | A_BRIGHT); /* Blue */
1341 colortable[7] = (COLOR_PAIR(3) | A_NORMAL); /* Umber */
1342 colortable[8] = (COLOR_PAIR(7) | A_BRIGHT); /* Dark-grey XXX */
1343 colortable[9] = (COLOR_PAIR(0) | A_NORMAL); /* Light-grey XXX */
1344 colortable[10] = (COLOR_PAIR(5) | A_BRIGHT); /* Purple */
1345 colortable[11] = (COLOR_PAIR(3) | A_BRIGHT); /* Yellow */
1346 colortable[12] = (COLOR_PAIR(5) | A_NORMAL); /* Light Red XXX */
1347 colortable[13] = (COLOR_PAIR(2) | A_BRIGHT); /* Light Green */
1348 colortable[14] = (COLOR_PAIR(6) | A_BRIGHT); /* Light Blue */
1349 colortable[15] = (COLOR_PAIR(3) | A_NORMAL); /* Light Umber XXX */
1354 /* Handle "arg_sound" */
1355 if (use_sound != arg_sound) {
1356 /* Initialize (if needed) */
1357 if (arg_sound && !init_sound()) {
1359 plog("Cannot initialize sound!");
1365 /* Change setting */
1366 use_sound = arg_sound;
1371 /* if USE_NCURSES_ACS is defined, we can do something with graphics in curses! */
1372 #ifdef USE_NCURSES_ACS
1373 use_graphics = true;
1377 /*** Low level preparation ***/
1381 /* Paranoia -- Assume no waiting */
1382 nodelay(stdscr, false);
1392 /* Extract the game keymap */
1393 keymap_game_prepare();
1395 /*** Now prepare the term(s) ***/
1397 /* Create several terms */
1398 for (i = 0; i < num_term; i++) {
1399 int rows, cols, y, x;
1401 /* Decide on size and position */
1440 rows = cols = y = x = 0;
1445 /* Skip non-existant windows */
1446 if (rows <= 0 || cols <= 0) {
1451 term_data_init_gcu(&data[next_win], rows, cols, y, x);
1453 /* Remember the term */
1454 angband_terms[next_win] = &data[next_win].t;
1456 /* One more window */
1460 /* Parse Args and Prepare the Terminals. Rectangles are specified
1461 as Width x Height, right? The game will allow you to have two
1462 strips of extra terminals, one on the right and one on the bottom.
1463 The map terminal will than fit in as big as possible in the remaining
1466 angband -mgcu -- -right 30x27,* -bottom *x7 will layout as
1467 Term-0: Map (COLS-30)x(LINES-7) | Term-1: 30x27
1468 --------------------------------|----------------------
1469 <----Term-3: (COLS-30)x7------->| Term-2: 30x(LINES-27)
1470 composband -mgcu -- -bottom *x7 -right 30x27,* will layout as
1471 Term-0: Map (COLS-30)x(LINES-7) | Term-2: 30x27
1472 |------------------------------
1473 | Term-3: 30x(LINES-27)
1474 ---------------------------------------------------------------
1475 <----------Term-1: (COLS)x7----------------------------------->
1476 Notice the effect on the bottom terminal by specifying its argument
1477 second or first. Notice the sequence numbers for the various terminals
1478 as you will have to blindly configure them in the window setup screen.
1479 EDIT: Added support for -left and -top.
1482 rect_t remaining = rect(0, 0, COLS, LINES);
1488 for (i = 1; i < argc; i++) {
1489 if (streq(argv[i], "-spacer")) {
1492 quit("Missing size specifier for -spacer");
1494 sscanf(argv[i], "%dx%d", &spacer_cx, &spacer_cy);
1495 } else if (streq(argv[i], "-right") || streq(argv[i], "-left")) {
1496 const char *arg, *tmp;
1497 bool left = streq(argv[i], "-left");
1498 int cx, cys[MAX_TERM_DATA] = { 0 }, ct, j, x, y;
1502 quit_fmt("Missing size specifier for -%s", left ? "left" : "right");
1506 tmp = strchr(arg, 'x');
1508 quit_fmt("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");
1517 x = remaining.x + remaining.cx;
1520 remaining.cx -= spacer_cx;
1522 remaining.x += spacer_cx;
1526 ct = _parse_size_list(tmp, cys, MAX_TERM_DATA);
1527 for (j = 0; j < ct; j++) {
1529 if (y + cy > remaining.y + remaining.cy) {
1530 cy = remaining.y + remaining.cy - y;
1532 if (next_term >= MAX_TERM_DATA) {
1533 quit_fmt("Too many terminals. Only %d are allowed.", MAX_TERM_DATA);
1536 quit_fmt("Out of bounds in -%s: %d is too large (%d rows max for this strip)",
1537 left ? "left" : "right", cys[j], remaining.cy);
1539 data[next_term++].r = rect(x, y, cx, cy);
1540 y += cy + spacer_cy;
1543 } else if (streq(argv[i], "-top") || streq(argv[i], "-bottom")) {
1544 const char *arg, *tmp;
1545 bool top = streq(argv[i], "-top");
1546 int cy, cxs[MAX_TERM_DATA] = { 0 }, ct, j, x, y;
1550 quit_fmt("Missing size specifier for -%s", top ? "top" : "bottom");
1554 tmp = strchr(arg, 'x');
1556 quit_fmt("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");
1560 ct = _parse_size_list(arg, cxs, MAX_TERM_DATA);
1569 y = remaining.y + remaining.cy;
1571 remaining.cy -= spacer_cy;
1573 remaining.y += spacer_cy;
1577 for (j = 0; j < ct; j++) {
1579 if (x + cx > remaining.x + remaining.cx) {
1580 cx = remaining.x + remaining.cx - x;
1582 if (next_term >= MAX_TERM_DATA) {
1583 quit_fmt("Too many terminals. Only %d are allowed.", MAX_TERM_DATA);
1586 quit_fmt("Out of bounds in -%s: %d is too large (%d cols max for this strip)",
1587 top ? "top" : "bottom", cxs[j], remaining.cx);
1589 data[next_term++].r = rect(x, y, cx, cy);
1590 x += cx + spacer_cx;
1597 if (remaining.cx < MIN_TERM0_COLS || remaining.cy < MIN_TERM0_LINES) {
1598 quit_fmt("Failed: %s needs an %dx%d map screen, not %dx%d", std::string(VARIANT_NAME).data(), MIN_TERM0_COLS, MIN_TERM0_LINES, remaining.cx, remaining.cy);
1600 data[0].r = remaining;
1601 term_data_init(&data[0]);
1602 angband_terms[0] = game_term;
1604 /* Child Terminals */
1605 for (next_term = 1; next_term < term_ct; next_term++) {
1606 term_data_init(&data[next_term]);
1607 angband_terms[next_term] = game_term;
1611 /* Activate the "Angband" window screen */
1612 term_activate(&data[0].t);
1615 term_screen = &data[0].t;
1621 #endif /* USE_GCU */