OSDN Git Service

[Refactor] #40463 Removed unused (anymore) preprocessor in main-gcu.c
[hengband/hengband.git] / src / main-gcu.c
1 /* File: main-gcu.c */
2
3 /*
4  * Copyright (c) 1997 Ben Harrison, and others
5  *
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.
9  */
10
11 /* Purpose: Allow use of Unix "curses" with Angband -BEN- */
12
13 /*
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).
16  *
17  * Also included is Keldon Jones patch to get better colors. To switch to
18  * a term that supports this, see this posting:
19  *
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
32  * Lines: 199
33  * Path: 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
34  * Xref: xs4all rec.games.roguelike.angband:86332
35  *
36  * Greg Wooledge <wooledge@kellnet.com> wrote:
37  * > Gwidon S. Naskrent (naskrent@artemida.amu.edu.pl) wrote:
38  *
39  * > >On 30 Mar 1999 13:17:18 GMT, scarblac-spamtrap@pino.selwerd.cx (Remco
40  * > >Gerlich) wrote:
41  *
42  * > >>I recently switched to Linux, and *bands work fine. I like
43  * > >>to play them in consoles, not in X. However, colour is wrong.
44  * > >>"Slate" and "light slate" are always light blue, instead
45  * > >>of some shade of grey. Colours are fine in X.
46  *
47  * > I actually noticed the Linux console color issue a very long time ago,
48  * > but since I always play under X, I never really investigated it.
49  *
50  * > You're absolutely right, though -- the Linux console colors are not
51  * > "right" for Angband.
52  *
53  *    I've noticed this myself, so I spent the evening fixing it.
54  * Well, sorta fixing it.  It's not perfect yet, and it may not be
55  * possible to get it perfect with VGA hardware and/or the current
56  * Linux kernel.
57  *
58  * > OK, reading on in terminfo(5):
59  *
60  * >    Color Handling
61  * >        Most color terminals are either `Tektronix-like'  or  `HP-
62  * >        like'.   Tektronix-like terminals have a predefined set of
63  * >        N colors (where N usually 8), and can  set  character-cell
64  * >        foreground and background characters independently, mixing
65  * >        them into N * N color-pairs.  On  HP-like  terminals,  the
66  * >        use must set each color pair up separately (foreground and
67  * >        background are  not  independently  settable).   Up  to  M
68  * >        color-pairs  may  be  set  up  from  2*M different colors.
69  * >        ANSI-compatible terminals are Tektronix-like.
70  *
71  * > The "linux" terminfo entry is definitely in the "Tektronix-like" family.
72  * > It has the "setaf" and "setab" capabilities for setting the foreground
73  * > and background colors to one of 8 basically hard-coded values:
74  *
75  * >              Color       #define       Value       RGB
76  * >              black     COLOR_BLACK       0     0, 0, 0
77  * >              red       COLOR_RED         1     max,0,0
78  * >              green     COLOR_GREEN       2     0,max,0
79  * >              yellow    COLOR_YELLOW      3     max,max,0
80  * >              blue      COLOR_BLUE        4     0,0,max
81  * >              magenta   COLOR_MAGENTA     5     max,0,max
82  * >              cyan      COLOR_CYAN        6     0,max,max
83  * >              white     COLOR_WHITE       7     max,max,max
84  *
85  *    Well, not quite.  Using certain escape sequences, an
86  * application (or better yet, curses) can redefine the colors (at
87  * least some of them) and then those are used.  Read the
88  * curs_color manpage, and the part about "ccc" and "initc" in the
89  * terminfo manpage.  This is what the part of main-gcu inside the
90  * "if (can_fix_color)" code does.
91  *
92  * > So, what does this mean to the Angband player?  Well, it means that
93  * > either there's nothing you can do about the console colors as long as
94  * > straight curses/ncurses is used, or if there is something to be done,
95  * > I'm not clever enough to figure out how to do it.
96  *
97  *    Well, it is possible, though you have to patch main-gcu
98  * and edit a terminfo entry.  Apparently the relevant code in
99  * main-gcu was never tested (it's broken in at least one major
100  * way).  Apply the patch at the end of this message (notice that
101  * we need to define REDEFINE_COLORS at some point near the
102  * beginning of the file).
103  *    Next, write this termcap entry to a file:
104  *
105  * linux-c|linux console 1.3.6+ with private palette for each virtual console,
106  *         ccc,
107  *         colors#16, pairs#64,
108  *         initc=\E]P%x%p1%{16}%/%02x%p1%{16}%/%02x%p1%{16}%/%02x,
109  *         oc=\E]R,
110  *         use=linux,
111  *
112  * and run "tic" on it to produce a new terminfo entry called
113  * "linux-c".  Especially note the "ccc" flag which says that we
114  * can redefine colors.  The ugly "initc" string is what tells
115  * the console how to redefine a color.  Now, just set your TERM
116  * variable to "linux-c" and try Angband again.  If I've
117  * remembered to tell you everything that I've done, you should
118  * get the weird light-blue slate changed to a gray.
119  *    Now, there are still lots of problems with this.
120  * Something (I don't think it's curses, either the kernel or
121  * the hardware itself) seems to be ignoring my color changes to
122  * colors 6 and 7, which is annoying.  Also, the normal "white"
123  * color is now way too bright, but it's now necessary to
124  * distinguish it from the other grays.
125  *    The kernel seems to support 16 colors, but you can
126  * only switch to 8 of those, due to VT102 compatibility, it
127  * seems.  I think it would be possible to patch the kernel and
128  * allow all 16 colors to be used, but I haven't built up the
129  * nerve to try that yet.
130  *    Let me know if you can improve on this any.  Some of
131  * this may actually work differently on other hardware (ugh).
132  *
133  *    Keldon
134  *
135  */
136
137 /*
138  * To use this file, you must define "USE_GCU" in the Makefile.
139  *
140  * Hack -- note that "angband.h" is included AFTER the #ifdef test.
141  * This was necessary because of annoying "curses.h" silliness.
142  *
143  * Note that this file is not "intended" to support non-Unix machines,
144  * nor is it intended to support VMS or other bizarre setups.
145  *
146  * Also, this package assumes that the underlying "curses" handles both
147  * the "nonl()" and "cbreak()" commands correctly, see the "OPTION" below.
148  *
149  * This code should work with most versions of "curses" or "ncurses",
150  * and the "main-ncu.c" file (and USE_NCU define) are no longer used.
151  *
152  * See also "USE_CAP" and "main-cap.c" for code that bypasses "curses"
153  * and uses the "termcap" information directly, or even bypasses the
154  * "termcap" information and sends direct vt100 escape sequences.
155  *
156  * XXX XXX XXX Consider the use of "savetty()" and "resetty()".
157  */
158
159 #include "system/angband.h"
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 "main/sound-definitions-table.h"
165 #include "main/sound-of-music.h"
166 #include "term/gameterm.h"
167 #include "term/term-color-types.h"
168 #include "util/angband-files.h"
169 #include "view/display-map.h"
170
171 #ifdef USE_GCU
172
173 /*
174  * Hack -- play games with "bool"
175  */
176 #undef bool
177
178 /*
179  * Include the proper "header" file
180  */
181 # include <curses.h>
182
183 typedef struct term_data term_data;
184
185 struct term_data
186 {
187    term_type t;
188
189    WINDOW *win;
190 };
191
192 #define MAX_TERM_DATA 4
193
194 static term_data data[MAX_TERM_DATA];
195
196
197 /*
198  * Hack -- try to guess which systems use what commands
199  * Hack -- allow one of the "USE_Txxxxx" flags to be pre-set.
200  * Mega-Hack -- try to guess when "POSIX" is available.
201  * If the user defines two of these, we will probably crash.
202  */
203 #if !defined(USE_TCHARS)
204 # if defined(_POSIX_VERSION)
205 #  define USE_TPOSIX
206 # else
207 #  if defined(linux)
208 #   define USE_TERMIO
209 #  else
210 #   define USE_TCHARS
211 #  endif
212 # endif
213 #endif
214
215 /*
216  * Try redefining the colors at startup.
217  */
218 #define REDEFINE_COLORS
219
220 /*
221  * POSIX stuff
222  */
223 #ifdef USE_TPOSIX
224 # include <sys/ioctl.h>
225 # include <termios.h>
226 #endif
227
228 /*
229  * One version needs this file
230  */
231 #ifdef USE_TERMIO
232 # include <sys/ioctl.h>
233 # include <termio.h>
234 #endif
235
236 /*
237  * The other needs this file
238  */
239 #ifdef USE_TCHARS
240 # include <sys/ioctl.h>
241 # include <sys/resource.h>
242 # include <sys/param.h>
243 # include <sys/file.h>
244 # include <sys/types.h>
245 #endif
246
247 #include <locale.h>
248
249 /*
250  * XXX XXX Hack -- POSIX uses "O_NONBLOCK" instead of "O_NDELAY"
251  *
252  * They should both work due to the "(i != 1)" test below.
253  */
254 #ifndef O_NDELAY
255 # define O_NDELAY O_NONBLOCK
256 #endif
257
258 /*
259  * OPTION: some machines lack "cbreak()"
260  * On these machines, we use an older definition
261  */
262 /* #define cbreak() crmode() */
263
264 /*
265  * OPTION: some machines cannot handle "nonl()" and "nl()"
266  * On these machines, we can simply ignore those commands.
267  */
268 /* #define nonl() */
269 /* #define nl() */
270
271 static concptr ANGBAND_DIR_XTRA_SOUND;
272
273 /*
274  * todo 有効活用されていない疑惑
275  * Flag set once "sound" has been initialized
276  */
277 static bool can_use_sound = FALSE;
278
279 /*
280  * An array of sound file names
281  */
282 static concptr sound_file[SOUND_MAX];
283
284 /*
285  * Save the "normal" and "angband" terminal settings
286  */
287
288 #ifdef USE_TPOSIX
289
290 static struct termios  norm_termios;
291
292 static struct termios  game_termios;
293
294 #endif
295
296 #ifdef USE_TERMIO
297
298 static struct termio  norm_termio;
299
300 static struct termio  game_termio;
301
302 #endif
303
304 #ifdef USE_TCHARS
305 static struct ltchars norm_speciax_chars;
306 static struct sgttyb  norm_ttyb;
307 static struct tchars  norm_tchars;
308 static int            norm_locax_chars;
309
310 static struct ltchars game_speciax_chars;
311 static struct sgttyb  game_ttyb;
312 static struct tchars  game_tchars;
313 static int            game_locax_chars;
314 #endif
315
316 /*
317  * Hack -- Number of initialized "term" structures
318  */
319 static int active = 0;
320
321 #ifdef A_COLOR
322 /*
323  * Hack -- define "A_BRIGHT" to be "A_BOLD", because on many
324  * machines, "A_BRIGHT" produces ugly "inverse" video.
325  */
326 #ifndef A_BRIGHT
327 # define A_BRIGHT A_BOLD
328 #endif
329
330 /*
331  * Software flag -- we are allowed to use color
332  */
333 static int can_use_color = FALSE;
334
335 /*
336  * Software flag -- we are allowed to change the colors
337  */
338 static int can_fix_color = FALSE;
339
340 /*
341  * Simple Angband to Curses color conversion table
342  */
343 static int colortable[16];
344 #endif
345
346 /*
347  * Place the "keymap" into its "normal" state
348  */
349 static void keymap_norm(void)
350 {
351 #ifdef USE_TPOSIX
352    /* restore the saved values of the special chars */
353    (void)tcsetattr(0, TCSAFLUSH, &norm_termios);
354 #endif
355
356 #ifdef USE_TERMIO
357    /* restore the saved values of the special chars */
358    (void)ioctl(0, TCSETA, (char *)&norm_termio);
359 #endif
360
361 #ifdef USE_TCHARS
362    /* restore the saved values of the special chars */
363    (void)ioctl(0, TIOCSLTC, (char *)&norm_speciax_chars);
364    (void)ioctl(0, TIOCSETP, (char *)&norm_ttyb);
365    (void)ioctl(0, TIOCSETC, (char *)&norm_tchars);
366    (void)ioctl(0, TIOCLSET, (char *)&norm_locax_chars);
367 #endif
368 }
369
370
371 /*
372  * Place the "keymap" into the "game" state
373  */
374 static void keymap_game(void)
375 {
376 #ifdef USE_TPOSIX
377    /* restore the saved values of the special chars */
378    (void)tcsetattr(0, TCSAFLUSH, &game_termios);
379 #endif
380
381 #ifdef USE_TERMIO
382    /* restore the saved values of the special chars */
383    (void)ioctl(0, TCSETA, (char *)&game_termio);
384 #endif
385
386 #ifdef USE_TCHARS
387    /* restore the saved values of the special chars */
388    (void)ioctl(0, TIOCSLTC, (char *)&game_speciax_chars);
389    (void)ioctl(0, TIOCSETP, (char *)&game_ttyb);
390    (void)ioctl(0, TIOCSETC, (char *)&game_tchars);
391    (void)ioctl(0, TIOCLSET, (char *)&game_locax_chars);
392 #endif
393 }
394
395
396 /*
397  * Save the normal keymap
398  */
399 static void keymap_norm_prepare(void)
400 {
401 #ifdef USE_TPOSIX
402    /* Get the normal keymap */
403    tcgetattr(0, &norm_termios);
404 #endif
405
406 #ifdef USE_TERMIO
407    /* Get the normal keymap */
408    (void)ioctl(0, TCGETA, (char *)&norm_termio);
409 #endif
410
411 #ifdef USE_TCHARS
412    /* Get the normal keymap */
413    (void)ioctl(0, TIOCGETP, (char *)&norm_ttyb);
414    (void)ioctl(0, TIOCGLTC, (char *)&norm_speciax_chars);
415    (void)ioctl(0, TIOCGETC, (char *)&norm_tchars);
416    (void)ioctl(0, TIOCLGET, (char *)&norm_locax_chars);
417 #endif
418 }
419
420
421 /*
422  * Save the keymaps (normal and game)
423  */
424 static void keymap_game_prepare(void)
425 {
426 #ifdef USE_TPOSIX
427    /* Acquire the current mapping */
428    tcgetattr(0, &game_termios);
429
430    /* Force "Ctrl-C" to interupt */
431    game_termios.c_cc[VINTR] = (char)3;
432
433    /* Force "Ctrl-Z" to suspend */
434    game_termios.c_cc[VSUSP] = (char)26;
435
436    /* Hack -- Leave "VSTART/VSTOP" alone */
437
438    /* Disable the standard control characters */
439    game_termios.c_cc[VQUIT] = (char)-1;
440    game_termios.c_cc[VERASE] = (char)-1;
441    game_termios.c_cc[VKILL] = (char)-1;
442    game_termios.c_cc[VEOF] = (char)-1;
443    game_termios.c_cc[VEOL] = (char)-1;
444
445    /* Normally, block until a character is read */
446    game_termios.c_cc[VMIN] = 1;
447    game_termios.c_cc[VTIME] = 0;
448 #endif
449
450 #ifdef USE_TERMIO
451    /* Acquire the current mapping */
452    (void)ioctl(0, TCGETA, (char *)&game_termio);
453
454    /* Force "Ctrl-C" to interupt */
455    game_termio.c_cc[VINTR] = (char)3;
456
457    /* Force "Ctrl-Z" to suspend */
458    game_termio.c_cc[VSUSP] = (char)26;
459
460    /* Disable the standard control characters */
461    game_termio.c_cc[VQUIT] = (char)-1;
462    game_termio.c_cc[VERASE] = (char)-1;
463    game_termio.c_cc[VKILL] = (char)-1;
464    game_termio.c_cc[VEOF] = (char)-1;
465    game_termio.c_cc[VEOL] = (char)-1;
466
467    /* Normally, block until a character is read */
468    game_termio.c_cc[VMIN] = 1;
469    game_termio.c_cc[VTIME] = 0;
470 #endif
471
472 #ifdef USE_TCHARS
473    /* Get the default game characters */
474    (void)ioctl(0, TIOCGETP, (char *)&game_ttyb);
475    (void)ioctl(0, TIOCGLTC, (char *)&game_speciax_chars);
476    (void)ioctl(0, TIOCGETC, (char *)&game_tchars);
477    (void)ioctl(0, TIOCLGET, (char *)&game_locax_chars);
478
479    /* Force suspend (^Z) */
480    game_speciax_chars.t_suspc = (char)26;
481
482    /* Cancel some things */
483    game_speciax_chars.t_dsuspc = (char)-1;
484    game_speciax_chars.t_rprntc = (char)-1;
485    game_speciax_chars.t_flushc = (char)-1;
486    game_speciax_chars.t_werasc = (char)-1;
487    game_speciax_chars.t_lnextc = (char)-1;
488
489    /* Force interupt (^C) */
490    game_tchars.t_intrc = (char)3;
491
492    /* Force start/stop (^Q, ^S) */
493    game_tchars.t_startc = (char)17;
494    game_tchars.t_stopc = (char)19;
495
496    /* Cancel some things */
497    game_tchars.t_quitc = (char)-1;
498    game_tchars.t_eofc = (char)-1;
499    game_tchars.t_brkc = (char)-1;
500 #endif
501 }
502
503
504
505 /*
506  * Suspend/Resume
507  */
508 static errr Term_xtra_gcu_alive(int v)
509 {
510    if (!v)
511    {
512       /* Go to normal keymap mode */
513       keymap_norm();
514
515       /* Restore modes */
516       nocbreak();
517       echo();
518       nl();
519
520       /* Hack -- make sure the cursor is visible */
521       term_xtra(TERM_XTRA_SHAPE, 1);
522
523       /* Flush the curses buffer */
524       (void)refresh();
525
526       /* this moves curses to bottom right corner */
527       mvcur(getcury(curscr), getcurx(curscr), LINES - 1, 0);
528
529       /* Exit curses */
530       endwin();
531
532       /* Flush the output */
533       (void)fflush(stdout);
534    }
535    else
536    {
537       /* Restore the settings */
538       cbreak();
539       noecho();
540       nonl();
541
542       /* Go to angband keymap mode */
543       keymap_game();
544    }
545
546    return (0);
547 }
548
549
550 /*
551  * Check for existance of a file
552  */
553 static bool check_file(concptr s)
554 {
555    FILE *fff;
556    fff = fopen(s, "r");
557    if (!fff) return (FALSE);
558
559    fclose(fff);
560    return (TRUE);
561 }
562
563
564 /*
565  * Initialize sound
566  */
567 static bool init_sound(void)
568 {
569    /* Initialize once */
570         if (can_use_sound) return can_use_sound;
571
572         int i;
573         char wav[128];
574         char buf[1024];
575
576         /* Prepare the sounds */
577         for (i = 1; i < SOUND_MAX; i++)
578         {
579                 /* Extract name of sound file */
580                 sprintf(wav, "%s.wav", angband_sound_name[i]);
581
582                 /* Access the sound */
583                 path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_SOUND, wav);
584
585                 /* Save the sound filename, if it exists */
586                 if (check_file(buf)) sound_file[i] = string_make(buf);
587         }
588
589         /* Sound available */
590         can_use_sound = TRUE;
591         return (can_use_sound);
592 }
593
594
595 /*
596  * Init the "curses" system
597  */
598 static void Term_init_gcu(term_type *t)
599 {
600    term_data *td = (term_data *)(t->data);
601
602    /* Count init's, handle first */
603    if (active++ != 0) return;
604
605    /* Erase the screen */
606    (void)wclear(td->win);
607
608    /* Reset the cursor */
609    (void)wmove(td->win, 0, 0);
610
611    /* Flush changes */
612    (void)wrefresh(td->win);
613
614    /* Game keymap */
615    keymap_game();
616 }
617
618
619 /*
620  * Nuke the "curses" system
621  */
622 static void Term_nuke_gcu(term_type *t)
623 {
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    /* This moves curses to bottom right corner */
641    mvcur(getcury(curscr), getcurx(curscr), LINES - 1, 0);
642
643    /* Flush the curses buffer */
644    (void)refresh();
645
646    /* Exit curses */
647    endwin();
648
649    /* Flush the output */
650    (void)fflush(stdout);
651
652    /* Normal keymap */
653    keymap_norm();
654 }
655
656 #ifdef USE_GETCH
657
658 /*
659  * Process events, with optional wait
660  */
661 static errr Term_xtra_gcu_event(int v)
662 {
663    int i, k;
664
665    /* Wait */
666    if (v)
667    {
668       /* Paranoia -- Wait for it */
669       nodelay(stdscr, FALSE);
670
671       /* Get a keypress */
672       i = getch();
673
674       /* Mega-Hack -- allow graceful "suspend" */
675       for (k = 0; (k < 10) && (i == ERR); k++) i = getch();
676
677       /* Broken input is special */
678       if (i == ERR) exit_game_panic(p_ptr);
679       if (i == EOF) exit_game_panic(p_ptr);
680    }
681
682    /* Do not wait */
683    else
684    {
685       /* Do not wait for it */
686       nodelay(stdscr, TRUE);
687
688       /* Check for keypresses */
689       i = getch();
690
691       /* Wait for it next time */
692       nodelay(stdscr, FALSE);
693
694       /* None ready */
695       if (i == ERR) return (1);
696       if (i == EOF) return (1);
697    }
698
699    /* Enqueue the keypress */
700    term_key_push(i);
701
702    /* Success */
703    return (0);
704 }
705
706 #else /* USE_GETCH */
707
708 /*
709  * Process events (with optional wait)
710  */
711 static errr Term_xtra_gcu_event(int v)
712 {
713    int i, k;
714
715    char buf[2];
716
717    /* Wait */
718    if (v)
719    {
720       /* Wait for one byte */
721       i = read(0, buf, 1);
722
723       /* Hack -- Handle bizarre "errors" */
724       if ((i <= 0) && (errno != EINTR)) exit_game_panic(p_ptr);
725    }
726
727    /* Do not wait */
728    else
729    {
730       /* Get the current flags for stdin */
731       k = fcntl(0, F_GETFL, 0);
732
733       /* Oops */
734       if (k < 0) return (1);
735
736       /* Tell stdin not to block */
737       if (fcntl(0, F_SETFL, k | O_NDELAY) < 0) return (1);
738
739       /* Read one byte, if possible */
740       i = read(0, buf, 1);
741
742       /* Replace the flags for stdin */
743       if (fcntl(0, F_SETFL, k)) return (1);
744    }
745
746    /* Ignore "invalid" keys */
747    if ((i != 1) || (!buf[0])) return (1);
748
749    /* Enqueue the keypress */
750    term_key_push(buf[0]);
751
752    /* Success */
753    return (0);
754 }
755
756 #endif   /* USE_GETCH */
757
758 /*
759  * Hack -- make a sound
760  */
761 static errr Term_xtra_gcu_sound(int v)
762 {
763    char buf[1024];
764
765    /* Sound disabled */
766    if (!use_sound) return (1);
767
768    /* Illegal sound */
769    if ((v < 0) || (v >= SOUND_MAX)) return (1);
770
771    /* Unknown sound */
772    if (!sound_file[v]) return (1);
773
774    sprintf(buf,"./gcusound.sh %s\n", sound_file[v]);
775    
776    return (system(buf) < 0);
777
778    return (0);
779
780 }
781
782
783 /*
784  * React to changes
785  */
786 static errr Term_xtra_gcu_react(void)
787 {
788
789 #ifdef A_COLOR
790
791         int i;
792
793         /* Cannot handle color redefinition */
794         if (!can_fix_color) return (0);
795
796         /* Set the colors */
797         for (i = 0; i < 16; i++)
798         {
799                 /* Set one color (note scaling) */
800                 init_color(i, angband_color_table[i][1] * 1000 / 255,
801                               angband_color_table[i][2] * 1000 / 255,
802                               angband_color_table[i][3] * 1000 / 255);
803         }
804
805 #endif
806
807         /* Success */
808         return (0);
809 }
810
811
812 /*
813  * Handle a "special request"
814  */
815 static errr Term_xtra_gcu(int n, int v)
816 {
817    term_data *td = (term_data *)(Term->data);
818
819    /* Analyze the request */
820    switch (n)
821    {
822       /* Clear screen */
823       case TERM_XTRA_CLEAR:
824       touchwin(td->win);
825       (void)wclear(td->win);
826       return (0);
827
828       /* Make a noise */
829       case TERM_XTRA_NOISE:
830       return write(1, "\007", 1) != 1;
831
832       /* Make a special sound */
833       case TERM_XTRA_SOUND:
834          return (Term_xtra_gcu_sound(v));
835
836       /* Flush the Curses buffer */
837       case TERM_XTRA_FRESH:
838       (void)wrefresh(td->win);
839       return (0);
840
841 #ifdef USE_CURS_SET
842
843       /* Change the cursor visibility */
844       case TERM_XTRA_SHAPE:
845       curs_set(v);
846       return (0);
847
848 #endif
849
850       /* Suspend/Resume curses */
851       case TERM_XTRA_ALIVE:
852       return (Term_xtra_gcu_alive(v));
853
854       /* Process events */
855       case TERM_XTRA_EVENT:
856       return (Term_xtra_gcu_event(v));
857
858       /* Flush events */
859       case TERM_XTRA_FLUSH:
860       while (!Term_xtra_gcu_event(FALSE));
861       return (0);
862
863       /* Delay */
864       case TERM_XTRA_DELAY:
865       usleep(1000 * v);
866       return (0);
867
868       /* React to events */
869       case TERM_XTRA_REACT:
870       Term_xtra_gcu_react();
871       return (0);
872
873    }
874
875
876    /* Unknown */
877    return (1);
878 }
879
880
881 /*
882  * Actually MOVE the hardware cursor
883  */
884 static errr Term_curs_gcu(int x, int y)
885 {
886    term_data *td = (term_data *)(Term->data);
887
888    /* Literally move the cursor */
889    wmove(td->win, y, x);
890
891    /* Success */
892    return (0);
893 }
894
895
896 /*
897  * Erase a grid of space
898  * Hack -- try to be "semi-efficient".
899  */
900 static errr Term_wipe_gcu(int x, int y, int n)
901 {
902    term_data *td = (term_data *)(Term->data);
903
904    /* Place cursor */
905    wmove(td->win, y, x);
906
907    /* Clear to end of line */
908    if (x + n >= 80)
909    {
910       wclrtoeol(td->win);
911    }
912
913    /* Clear some characters */
914    else
915    {
916       while (n-- > 0) waddch(td->win, ' ');
917    }
918
919    /* Success */
920    return (0);
921 }
922
923 #ifdef USE_NCURSES_ACS
924 /*
925  * this function draws some ACS characters on the screen
926  * for DOS-based users: these are the graphical chars (blocks, lines etc)
927  *
928  * unix-gurus: before you start adding other attributes like A_REVERSE
929  * think hard about how map_info() in cave.c should handle the color
930  * of something that we here draw in reverse. It's not so simple, alas.
931  */
932 static void Term_acs_text_gcu(int x, int y, int n, byte a, concptr s)
933 {
934    term_data *td = (term_data *)(Term->data);
935    int i;
936
937    /* position the cursor */
938    wmove(td->win, y, x);
939
940 #ifdef A_COLOR
941    /* Set the color */
942    wattrset(td->win, colortable[a & 0x0F]);
943 #endif
944
945    for (i=0; i < n; i++)
946    {
947       /* add acs_map of a */
948       waddch(td->win, acs_map[(int)s[i]]);
949    }
950    wattrset(td->win, WA_NORMAL);
951 }
952 #endif
953
954 /*
955  * Place some text on the screen using an attribute
956  */
957 static errr Term_text_gcu(int x, int y, int n, byte a, concptr s)
958 {
959    term_data *td = (term_data *)(Term->data);
960
961    int i;
962
963    char text[81];
964
965 #ifdef USE_NCURSES_ACS
966    /* do we have colors + 16 ? */
967    /* then call special routine for drawing special characters */
968    if (a & 0x10)
969    {
970       Term_acs_text_gcu(x, y, n, a, s);
971       return(0);
972    }
973 #endif
974
975    /* Obtain a copy of the text */
976    for (i = 0; i < n; i++) text[i] = s[i];
977    text[n] = 0;
978
979    /* Move the cursor and dump the string */
980    wmove(td->win, y, x);
981
982 #ifdef A_COLOR
983    /* Set the color */
984    if (can_use_color) wattrset(td->win, colortable[a & 0x0F]);
985 #endif
986
987    /* Add the text */
988    waddstr(td->win, text);
989
990    /* Success */
991    return (0);
992 }
993
994
995
996 static errr term_data_init(term_data *td, int rows, int cols, int y, int x)
997 {
998    term_type *t = &td->t;
999
1000    /* Make sure the window has a positive size */
1001    if (rows <= 0 || cols <= 0) return (0);
1002
1003    /* Create a window */
1004    td->win = newwin(rows, cols, y, x);
1005
1006    /* Make sure we succeed */
1007    if (!td->win)
1008    {
1009       plog("Failed to setup curses window.");
1010       return (-1);
1011    }
1012
1013    /* Initialize the term */
1014    term_init(t, cols, rows, 256);
1015
1016    /* Avoid the bottom right corner */
1017    t->icky_corner = TRUE;
1018
1019    /* Erase with "white space" */
1020    t->attr_blank = TERM_WHITE;
1021    t->char_blank = ' ';
1022
1023    /* Set some hooks */
1024    t->init_hook = Term_init_gcu;
1025    t->nuke_hook = Term_nuke_gcu;
1026
1027    /* Set some more hooks */
1028    t->text_hook = Term_text_gcu;
1029    t->wipe_hook = Term_wipe_gcu;
1030    t->curs_hook = Term_curs_gcu;
1031    t->xtra_hook = Term_xtra_gcu;
1032
1033    /* Save the data */
1034    t->data = td;
1035
1036    /* Activate it */
1037    term_activate(t);
1038
1039
1040    /* Success */
1041    return (0);
1042 }
1043
1044
1045 static void hook_quit(concptr str)
1046 {
1047         /* Unused */
1048         (void)str;
1049
1050        /* Exit curses */
1051        endwin();
1052 }
1053
1054
1055 /*
1056  * Prepare "curses" for use by the file "term.c"
1057  *
1058  * Installs the "hook" functions defined above, and then activates
1059  * the main screen "term", which clears the screen and such things.
1060  *
1061  * Someone should really check the semantics of "initscr()"
1062  */
1063 errr init_gcu(int argc, char *argv[])
1064 {
1065    int i;
1066
1067    int num_term = 4, next_win = 0;
1068    char path[1024];
1069
1070    /* Unused */
1071    (void)argc;
1072    (void)argv;
1073
1074
1075    setlocale(LC_ALL, "");
1076
1077    /* Build the "sound" path */
1078    path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "sound");
1079
1080    /* Allocate the path */
1081    ANGBAND_DIR_XTRA_SOUND = string_make(path);
1082
1083    /* Extract the normal keymap */
1084    keymap_norm_prepare();
1085
1086    /* Initialize for others systems */
1087    if (initscr() == (WINDOW*)ERR) return (-1);
1088
1089    /* Activate hooks */
1090    quit_aux = hook_quit;
1091    core_aux = hook_quit;
1092
1093    /* Hack -- Require large screen, or Quit with message */
1094    i = ((LINES < 24) || (COLS < 80));
1095    if (i) quit("Angband needs an 80x24 'curses' screen");
1096
1097
1098 #ifdef A_COLOR
1099
1100    /*** Init the Color-pairs and set up a translation table ***/
1101
1102    /* Do we have color, and enough color, available? */
1103    can_use_color = ((start_color() != ERR) && has_colors() &&
1104                     (COLORS >= 8) && (COLOR_PAIRS >= 8));
1105
1106 #ifdef REDEFINE_COLORS
1107         /* Can we change colors? */
1108         can_fix_color = (can_use_color && can_change_color() &&
1109                          (COLORS >= 16) && (COLOR_PAIRS > 8));
1110 #endif
1111
1112    /* Attempt to use customized colors */
1113    if (can_fix_color)
1114    {
1115       /* Prepare the color pairs */
1116            for (i = 1; i <= 15; i++)
1117            {
1118                    if (init_pair(i, i, 0) == ERR)
1119                    {
1120                            quit("Color pair init failed");
1121                    }
1122
1123                    colortable[i] = COLOR_PAIR(i);
1124                    Term_xtra_gcu_react();
1125            }
1126    }
1127    /* Attempt to use colors */
1128    else if (can_use_color)
1129    {
1130                 /* Color-pair 0 is *always* WHITE on BLACK */
1131
1132                 /* Prepare the color pairs */
1133                 init_pair(1, COLOR_RED,     COLOR_BLACK);
1134                 init_pair(2, COLOR_GREEN,   COLOR_BLACK);
1135                 init_pair(3, COLOR_YELLOW,  COLOR_BLACK);
1136                 init_pair(4, COLOR_BLUE,    COLOR_BLACK);
1137                 init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
1138                 init_pair(6, COLOR_CYAN,    COLOR_BLACK);
1139                 init_pair(7, COLOR_BLACK,   COLOR_BLACK);
1140
1141                 /* Prepare the "Angband Colors" -- Bright white is too bright */
1142                 /* Changed in Drangband. Cyan as grey sucks -- -TM- */
1143                 colortable[0] = (COLOR_PAIR(7) | A_NORMAL);     /* Black */
1144                 colortable[1] = (COLOR_PAIR(0) | A_BRIGHT);     /* White */
1145                 colortable[2] = (COLOR_PAIR(0) | A_NORMAL);     /* Grey XXX */
1146                 colortable[3] = (COLOR_PAIR(1) | A_BRIGHT);     /* Orange XXX */
1147                 colortable[4] = (COLOR_PAIR(1) | A_NORMAL);     /* Red */
1148                 colortable[5] = (COLOR_PAIR(2) | A_NORMAL);     /* Green */
1149                 colortable[6] = (COLOR_PAIR(4) | A_BRIGHT);     /* Blue */
1150                 colortable[7] = (COLOR_PAIR(3) | A_NORMAL);     /* Umber */
1151                 colortable[8] = (COLOR_PAIR(7) | A_BRIGHT);     /* Dark-grey XXX */
1152                 colortable[9] = (COLOR_PAIR(0) | A_NORMAL);     /* Light-grey XXX */
1153                 colortable[10] = (COLOR_PAIR(5) | A_BRIGHT);    /* Purple */
1154                 colortable[11] = (COLOR_PAIR(3) | A_BRIGHT);    /* Yellow */
1155                 colortable[12] = (COLOR_PAIR(5) | A_NORMAL);    /* Light Red XXX */
1156                 colortable[13] = (COLOR_PAIR(2) | A_BRIGHT);    /* Light Green */
1157                 colortable[14] = (COLOR_PAIR(6) | A_BRIGHT);    /* Light Blue */
1158                 colortable[15] = (COLOR_PAIR(3) | A_NORMAL);    /* Light Umber XXX */
1159
1160    }
1161
1162 #endif
1163
1164    /* Handle "arg_sound" */
1165    if (use_sound != arg_sound)
1166    {
1167       /* Initialize (if needed) */
1168       if (arg_sound && !init_sound())
1169       {
1170          /* Warning */
1171          plog("Cannot initialize sound!");
1172
1173          /* Cannot enable */
1174          arg_sound = FALSE;
1175       }
1176
1177       /* Change setting */
1178       use_sound = arg_sound;
1179    }
1180
1181    /* Try graphics */
1182    if (arg_graphics)
1183    {
1184       /* if USE_NCURSES_ACS is defined, we can do something with graphics in curses! */
1185 #ifdef USE_NCURSES_ACS
1186       use_graphics = TRUE;
1187 #endif
1188    }
1189
1190    /*** Low level preparation ***/
1191
1192 #ifdef USE_GETCH
1193
1194    /* Paranoia -- Assume no waiting */
1195    nodelay(stdscr, FALSE);
1196
1197 #endif
1198
1199    /* Prepare */
1200    cbreak();
1201    noecho();
1202    nonl();
1203    raw();
1204
1205    /* Extract the game keymap */
1206    keymap_game_prepare();
1207
1208
1209    /*** Now prepare the term(s) ***/
1210    for (i = 0; i < num_term; i++)
1211    {
1212       int rows, cols;
1213       int y, x;
1214
1215       switch (i)
1216       {
1217          /* Upper left */
1218          case 0: rows = 24;
1219             cols = 80;
1220             y = x = 0;
1221             break;
1222          /* Lower left */
1223          case 1: rows = LINES - 25;
1224             cols = 80;
1225             y = 24;
1226             x = 0;
1227             break;
1228          /* Upper right */
1229          case 2: rows = 24;
1230             cols = COLS - 81;
1231             y = 0;
1232             x = 81;
1233             break;
1234          /* Lower right */
1235          case 3: rows = LINES - 25;
1236             cols = COLS - 81;
1237             y = 24;
1238             x = 81;
1239             break;
1240          /* XXX */
1241          default: rows = cols = 0;
1242              y = x = 0;
1243              break;
1244       }
1245
1246       /* No non-windows */
1247       if (rows <= 0 || cols <= 0) continue;
1248
1249       /* Initialize */
1250       term_data_init(&data[next_win], rows, cols, y, x);
1251
1252       /* Store */
1253       angband_term[next_win] = Term;
1254
1255       next_win++;
1256    }
1257
1258    /* Activate the "Angband" window screen */
1259    term_activate(&data[0].t);
1260
1261    /* Store */
1262    term_screen = &data[0].t;
1263
1264    /* Success */
1265    return (0);
1266 }
1267
1268
1269 #endif /* USE_GCU */