OSDN Git Service

Merge remote-tracking branch 'remotes/origin/For2.2.2-Fix-Hourier' into For2.2.2...
[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 "term/gameterm.h"
166 #include "term/term-color-types.h"
167
168 #ifdef USE_GCU
169
170 /*
171  * Hack -- play games with "bool"
172  */
173 #undef bool
174
175 /*
176  * Include the proper "header" file
177  */
178 # include <curses.h>
179
180 typedef struct term_data term_data;
181
182 struct term_data
183 {
184    term t;
185
186    WINDOW *win;
187 };
188
189 #define MAX_TERM_DATA 4
190
191 static term_data data[MAX_TERM_DATA];
192
193
194 /*
195  * Hack -- try to guess which systems use what commands
196  * Hack -- allow one of the "USE_Txxxxx" flags to be pre-set.
197  * Mega-Hack -- try to guess when "POSIX" is available.
198  * If the user defines two of these, we will probably crash.
199  */
200 #if !defined(USE_TERMIO) && !defined(USE_TCHARS)
201 # if defined(_POSIX_VERSION)
202 #  define USE_TPOSIX
203 # else
204 #  if defined(linux)
205 #   define USE_TERMIO
206 #  else
207 #   define USE_TCHARS
208 #  endif
209 # endif
210 #endif
211
212 /*
213  * Try redefining the colors at startup.
214  */
215 #define REDEFINE_COLORS
216
217 /*
218  * POSIX stuff
219  */
220 #ifdef USE_TPOSIX
221 # include <sys/ioctl.h>
222 # include <termios.h>
223 #endif
224
225 /*
226  * One version needs this file
227  */
228 #ifdef USE_TERMIO
229 # include <sys/ioctl.h>
230 # include <termio.h>
231 #endif
232
233 /*
234  * The other needs this file
235  */
236 #ifdef USE_TCHARS
237 # include <sys/ioctl.h>
238 # include <sys/resource.h>
239 # include <sys/param.h>
240 # include <sys/file.h>
241 # include <sys/types.h>
242 #endif
243
244 #include <locale.h>
245
246 /*
247  * XXX XXX Hack -- POSIX uses "O_NONBLOCK" instead of "O_NDELAY"
248  *
249  * They should both work due to the "(i != 1)" test below.
250  */
251 #ifndef O_NDELAY
252 # define O_NDELAY O_NONBLOCK
253 #endif
254
255 /*
256  * OPTION: some machines lack "cbreak()"
257  * On these machines, we use an older definition
258  */
259 /* #define cbreak() crmode() */
260
261 /*
262  * OPTION: some machines cannot handle "nonl()" and "nl()"
263  * On these machines, we can simply ignore those commands.
264  */
265 /* #define nonl() */
266 /* #define nl() */
267
268 static concptr ANGBAND_DIR_XTRA_SOUND;
269
270 /*
271  * todo 有効活用されていない疑惑
272  * Flag set once "sound" has been initialized
273  */
274 static bool can_use_sound = FALSE;
275
276 /*
277  * An array of sound file names
278  */
279 static concptr sound_file[SOUND_MAX];
280
281 /*
282  * Save the "normal" and "angband" terminal settings
283  */
284
285 #ifdef USE_TPOSIX
286
287 static struct termios  norm_termios;
288
289 static struct termios  game_termios;
290
291 #endif
292
293 #ifdef USE_TERMIO
294
295 static struct termio  norm_termio;
296
297 static struct termio  game_termio;
298
299 #endif
300
301 #ifdef USE_TCHARS
302 static struct ltchars norm_speciax_chars;
303 static struct sgttyb  norm_ttyb;
304 static struct tchars  norm_tchars;
305 static int            norm_locax_chars;
306
307 static struct ltchars game_speciax_chars;
308 static struct sgttyb  game_ttyb;
309 static struct tchars  game_tchars;
310 static int            game_locax_chars;
311 #endif
312
313 /*
314  * Hack -- Number of initialized "term" structures
315  */
316 static int active = 0;
317
318 #ifdef A_COLOR
319 /*
320  * Hack -- define "A_BRIGHT" to be "A_BOLD", because on many
321  * machines, "A_BRIGHT" produces ugly "inverse" video.
322  */
323 #ifndef A_BRIGHT
324 # define A_BRIGHT A_BOLD
325 #endif
326
327 /*
328  * Software flag -- we are allowed to use color
329  */
330 static int can_use_color = FALSE;
331
332 /*
333  * Software flag -- we are allowed to change the colors
334  */
335 static int can_fix_color = FALSE;
336
337 /*
338  * Simple Angband to Curses color conversion table
339  */
340 static int colortable[16];
341 #endif
342
343 /*
344  * Place the "keymap" into its "normal" state
345  */
346 static void keymap_norm(void)
347 {
348 #ifdef USE_TPOSIX
349    /* restore the saved values of the special chars */
350    (void)tcsetattr(0, TCSAFLUSH, &norm_termios);
351 #endif
352
353 #ifdef USE_TERMIO
354    /* restore the saved values of the special chars */
355    (void)ioctl(0, TCSETA, (char *)&norm_termio);
356 #endif
357
358 #ifdef USE_TCHARS
359    /* restore the saved values of the special chars */
360    (void)ioctl(0, TIOCSLTC, (char *)&norm_speciax_chars);
361    (void)ioctl(0, TIOCSETP, (char *)&norm_ttyb);
362    (void)ioctl(0, TIOCSETC, (char *)&norm_tchars);
363    (void)ioctl(0, TIOCLSET, (char *)&norm_locax_chars);
364 #endif
365 }
366
367
368 /*
369  * Place the "keymap" into the "game" state
370  */
371 static void keymap_game(void)
372 {
373 #ifdef USE_TPOSIX
374    /* restore the saved values of the special chars */
375    (void)tcsetattr(0, TCSAFLUSH, &game_termios);
376 #endif
377
378 #ifdef USE_TERMIO
379    /* restore the saved values of the special chars */
380    (void)ioctl(0, TCSETA, (char *)&game_termio);
381 #endif
382
383 #ifdef USE_TCHARS
384    /* restore the saved values of the special chars */
385    (void)ioctl(0, TIOCSLTC, (char *)&game_speciax_chars);
386    (void)ioctl(0, TIOCSETP, (char *)&game_ttyb);
387    (void)ioctl(0, TIOCSETC, (char *)&game_tchars);
388    (void)ioctl(0, TIOCLSET, (char *)&game_locax_chars);
389 #endif
390 }
391
392
393 /*
394  * Save the normal keymap
395  */
396 static void keymap_norm_prepare(void)
397 {
398 #ifdef USE_TPOSIX
399    /* Get the normal keymap */
400    tcgetattr(0, &norm_termios);
401 #endif
402
403 #ifdef USE_TERMIO
404    /* Get the normal keymap */
405    (void)ioctl(0, TCGETA, (char *)&norm_termio);
406 #endif
407
408 #ifdef USE_TCHARS
409    /* Get the normal keymap */
410    (void)ioctl(0, TIOCGETP, (char *)&norm_ttyb);
411    (void)ioctl(0, TIOCGLTC, (char *)&norm_speciax_chars);
412    (void)ioctl(0, TIOCGETC, (char *)&norm_tchars);
413    (void)ioctl(0, TIOCLGET, (char *)&norm_locax_chars);
414 #endif
415 }
416
417
418 /*
419  * Save the keymaps (normal and game)
420  */
421 static void keymap_game_prepare(void)
422 {
423 #ifdef USE_TPOSIX
424    /* Acquire the current mapping */
425    tcgetattr(0, &game_termios);
426
427    /* Force "Ctrl-C" to interupt */
428    game_termios.c_cc[VINTR] = (char)3;
429
430    /* Force "Ctrl-Z" to suspend */
431    game_termios.c_cc[VSUSP] = (char)26;
432
433    /* Hack -- Leave "VSTART/VSTOP" alone */
434
435    /* Disable the standard control characters */
436    game_termios.c_cc[VQUIT] = (char)-1;
437    game_termios.c_cc[VERASE] = (char)-1;
438    game_termios.c_cc[VKILL] = (char)-1;
439    game_termios.c_cc[VEOF] = (char)-1;
440    game_termios.c_cc[VEOL] = (char)-1;
441
442    /* Normally, block until a character is read */
443    game_termios.c_cc[VMIN] = 1;
444    game_termios.c_cc[VTIME] = 0;
445 #endif
446
447 #ifdef USE_TERMIO
448    /* Acquire the current mapping */
449    (void)ioctl(0, TCGETA, (char *)&game_termio);
450
451    /* Force "Ctrl-C" to interupt */
452    game_termio.c_cc[VINTR] = (char)3;
453
454    /* Force "Ctrl-Z" to suspend */
455    game_termio.c_cc[VSUSP] = (char)26;
456
457    /* Disable the standard control characters */
458    game_termio.c_cc[VQUIT] = (char)-1;
459    game_termio.c_cc[VERASE] = (char)-1;
460    game_termio.c_cc[VKILL] = (char)-1;
461    game_termio.c_cc[VEOF] = (char)-1;
462    game_termio.c_cc[VEOL] = (char)-1;
463
464    /* Normally, block until a character is read */
465    game_termio.c_cc[VMIN] = 1;
466    game_termio.c_cc[VTIME] = 0;
467 #endif
468
469 #ifdef USE_TCHARS
470    /* Get the default game characters */
471    (void)ioctl(0, TIOCGETP, (char *)&game_ttyb);
472    (void)ioctl(0, TIOCGLTC, (char *)&game_speciax_chars);
473    (void)ioctl(0, TIOCGETC, (char *)&game_tchars);
474    (void)ioctl(0, TIOCLGET, (char *)&game_locax_chars);
475
476    /* Force suspend (^Z) */
477    game_speciax_chars.t_suspc = (char)26;
478
479    /* Cancel some things */
480    game_speciax_chars.t_dsuspc = (char)-1;
481    game_speciax_chars.t_rprntc = (char)-1;
482    game_speciax_chars.t_flushc = (char)-1;
483    game_speciax_chars.t_werasc = (char)-1;
484    game_speciax_chars.t_lnextc = (char)-1;
485
486    /* Force interupt (^C) */
487    game_tchars.t_intrc = (char)3;
488
489    /* Force start/stop (^Q, ^S) */
490    game_tchars.t_startc = (char)17;
491    game_tchars.t_stopc = (char)19;
492
493    /* Cancel some things */
494    game_tchars.t_quitc = (char)-1;
495    game_tchars.t_eofc = (char)-1;
496    game_tchars.t_brkc = (char)-1;
497 #endif
498 }
499
500
501
502 /*
503  * Suspend/Resume
504  */
505 static errr Term_xtra_gcu_alive(int v)
506 {
507    if (!v)
508    {
509       /* Go to normal keymap mode */
510       keymap_norm();
511
512       /* Restore modes */
513       nocbreak();
514       echo();
515       nl();
516
517       /* Hack -- make sure the cursor is visible */
518       Term_xtra(TERM_XTRA_SHAPE, 1);
519
520       /* Flush the curses buffer */
521       (void)refresh();
522
523       /* this moves curses to bottom right corner */
524       mvcur(getcury(curscr), getcurx(curscr), LINES - 1, 0);
525
526       /* Exit curses */
527       endwin();
528
529       /* Flush the output */
530       (void)fflush(stdout);
531    }
532    else
533    {
534       /* Restore the settings */
535       cbreak();
536       noecho();
537       nonl();
538
539       /* Go to angband keymap mode */
540       keymap_game();
541    }
542
543    return (0);
544 }
545
546
547 /*
548  * Check for existance of a file
549  */
550 static bool check_file(concptr s)
551 {
552    FILE *fff;
553    fff = fopen(s, "r");
554    if (!fff) return (FALSE);
555
556    fclose(fff);
557    return (TRUE);
558 }
559
560
561 /*
562  * Initialize sound
563  */
564 static bool init_sound(void)
565 {
566    /* Initialize once */
567         if (can_use_sound) return can_use_sound;
568
569         int i;
570         char wav[128];
571         char buf[1024];
572
573         /* Prepare the sounds */
574         for (i = 1; i < SOUND_MAX; i++)
575         {
576                 /* Extract name of sound file */
577                 sprintf(wav, "%s.wav", angband_sound_name[i]);
578
579                 /* Access the sound */
580                 path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_SOUND, wav);
581
582                 /* Save the sound filename, if it exists */
583                 if (check_file(buf)) sound_file[i] = string_make(buf);
584         }
585
586         /* Sound available */
587         can_use_sound = TRUE;
588         return (can_use_sound);
589 }
590
591
592 /*
593  * Init the "curses" system
594  */
595 static void Term_init_gcu(term *t)
596 {
597    term_data *td = (term_data *)(t->data);
598
599    /* Count init's, handle first */
600    if (active++ != 0) return;
601
602    /* Erase the screen */
603    (void)wclear(td->win);
604
605    /* Reset the cursor */
606    (void)wmove(td->win, 0, 0);
607
608    /* Flush changes */
609    (void)wrefresh(td->win);
610
611    /* Game keymap */
612    keymap_game();
613 }
614
615
616 /*
617  * Nuke the "curses" system
618  */
619 static void Term_nuke_gcu(term *t)
620 {
621    term_data *td = (term_data *)(t->data);
622
623    /* Delete this window */
624    delwin(td->win);
625
626    /* Count nuke's, handle last */
627    if (--active != 0) return;
628
629    /* Hack -- make sure the cursor is visible */
630    Term_xtra(TERM_XTRA_SHAPE, 1);
631
632 #ifdef A_COLOR
633   /* Reset colors to defaults */
634   start_color();
635 #endif
636
637    /* This moves curses to bottom right corner */
638    mvcur(getcury(curscr), getcurx(curscr), LINES - 1, 0);
639
640    /* Flush the curses buffer */
641    (void)refresh();
642
643    /* Exit curses */
644    endwin();
645
646    /* Flush the output */
647    (void)fflush(stdout);
648
649    /* Normal keymap */
650    keymap_norm();
651 }
652
653 #ifdef USE_GETCH
654
655 /*
656  * Process events, with optional wait
657  */
658 static errr Term_xtra_gcu_event(int v)
659 {
660    int i, k;
661
662    /* Wait */
663    if (v)
664    {
665       /* Paranoia -- Wait for it */
666       nodelay(stdscr, FALSE);
667
668       /* Get a keypress */
669       i = getch();
670
671       /* Mega-Hack -- allow graceful "suspend" */
672       for (k = 0; (k < 10) && (i == ERR); k++) i = getch();
673
674       /* Broken input is special */
675       if (i == ERR) exit_game_panic(p_ptr);
676       if (i == EOF) exit_game_panic(p_ptr);
677    }
678
679    /* Do not wait */
680    else
681    {
682       /* Do not wait for it */
683       nodelay(stdscr, TRUE);
684
685       /* Check for keypresses */
686       i = getch();
687
688       /* Wait for it next time */
689       nodelay(stdscr, FALSE);
690
691       /* None ready */
692       if (i == ERR) return (1);
693       if (i == EOF) return (1);
694    }
695
696    /* Enqueue the keypress */
697    Term_keypress(i);
698
699    /* Success */
700    return (0);
701 }
702
703 #else /* USE_GETCH */
704
705 /*
706  * Process events (with optional wait)
707  */
708 static errr Term_xtra_gcu_event(int v)
709 {
710    int i, k;
711
712    char buf[2];
713
714    /* Wait */
715    if (v)
716    {
717       /* Wait for one byte */
718       i = read(0, buf, 1);
719
720       /* Hack -- Handle bizarre "errors" */
721       if ((i <= 0) && (errno != EINTR)) exit_game_panic(p_ptr);
722    }
723
724    /* Do not wait */
725    else
726    {
727       /* Get the current flags for stdin */
728       k = fcntl(0, F_GETFL, 0);
729
730       /* Oops */
731       if (k < 0) return (1);
732
733       /* Tell stdin not to block */
734       if (fcntl(0, F_SETFL, k | O_NDELAY) < 0) return (1);
735
736       /* Read one byte, if possible */
737       i = read(0, buf, 1);
738
739       /* Replace the flags for stdin */
740       if (fcntl(0, F_SETFL, k)) return (1);
741    }
742
743    /* Ignore "invalid" keys */
744    if ((i != 1) || (!buf[0])) return (1);
745
746    /* Enqueue the keypress */
747    Term_keypress(buf[0]);
748
749    /* Success */
750    return (0);
751 }
752
753 #endif   /* USE_GETCH */
754
755 /*
756  * Hack -- make a sound
757  */
758 static errr Term_xtra_gcu_sound(int v)
759 {
760    char buf[1024];
761
762    /* Sound disabled */
763    if (!use_sound) return (1);
764
765    /* Illegal sound */
766    if ((v < 0) || (v >= SOUND_MAX)) return (1);
767
768    /* Unknown sound */
769    if (!sound_file[v]) return (1);
770
771    sprintf(buf,"./gcusound.sh %s\n", sound_file[v]);
772    
773    return (system(buf) < 0);
774
775    return (0);
776
777 }
778
779
780 /*
781  * React to changes
782  */
783 static errr Term_xtra_gcu_react(void)
784 {
785
786 #ifdef A_COLOR
787
788         int i;
789
790         /* Cannot handle color redefinition */
791         if (!can_fix_color) return (0);
792
793         /* Set the colors */
794         for (i = 0; i < 16; i++)
795         {
796                 /* Set one color (note scaling) */
797                 init_color(i, angband_color_table[i][1] * 1000 / 255,
798                               angband_color_table[i][2] * 1000 / 255,
799                               angband_color_table[i][3] * 1000 / 255);
800         }
801
802 #endif
803
804         /* Success */
805         return (0);
806 }
807
808
809 /*
810  * Handle a "special request"
811  */
812 static errr Term_xtra_gcu(int n, int v)
813 {
814    term_data *td = (term_data *)(Term->data);
815
816    /* Analyze the request */
817    switch (n)
818    {
819       /* Clear screen */
820       case TERM_XTRA_CLEAR:
821       touchwin(td->win);
822       (void)wclear(td->win);
823       return (0);
824
825       /* Make a noise */
826       case TERM_XTRA_NOISE:
827       return write(1, "\007", 1) != 1;
828
829       /* Make a special sound */
830       case TERM_XTRA_SOUND:
831          return (Term_xtra_gcu_sound(v));
832
833       /* Flush the Curses buffer */
834       case TERM_XTRA_FRESH:
835       (void)wrefresh(td->win);
836       return (0);
837
838 #ifdef USE_CURS_SET
839
840       /* Change the cursor visibility */
841       case TERM_XTRA_SHAPE:
842       curs_set(v);
843       return (0);
844
845 #endif
846
847       /* Suspend/Resume curses */
848       case TERM_XTRA_ALIVE:
849       return (Term_xtra_gcu_alive(v));
850
851       /* Process events */
852       case TERM_XTRA_EVENT:
853       return (Term_xtra_gcu_event(v));
854
855       /* Flush events */
856       case TERM_XTRA_FLUSH:
857       while (!Term_xtra_gcu_event(FALSE));
858       return (0);
859
860       /* Delay */
861       case TERM_XTRA_DELAY:
862       usleep(1000 * v);
863       return (0);
864
865       /* React to events */
866       case TERM_XTRA_REACT:
867       Term_xtra_gcu_react();
868       return (0);
869
870    }
871
872
873    /* Unknown */
874    return (1);
875 }
876
877
878 /*
879  * Actually MOVE the hardware cursor
880  */
881 static errr Term_curs_gcu(int x, int y)
882 {
883    term_data *td = (term_data *)(Term->data);
884
885    /* Literally move the cursor */
886    wmove(td->win, y, x);
887
888    /* Success */
889    return (0);
890 }
891
892
893 /*
894  * Erase a grid of space
895  * Hack -- try to be "semi-efficient".
896  */
897 static errr Term_wipe_gcu(int x, int y, int n)
898 {
899    term_data *td = (term_data *)(Term->data);
900
901    /* Place cursor */
902    wmove(td->win, y, x);
903
904    /* Clear to end of line */
905    if (x + n >= 80)
906    {
907       wclrtoeol(td->win);
908    }
909
910    /* Clear some characters */
911    else
912    {
913       while (n-- > 0) waddch(td->win, ' ');
914    }
915
916    /* Success */
917    return (0);
918 }
919
920 #ifdef USE_NCURSES_ACS
921 /*
922  * this function draws some ACS characters on the screen
923  * for DOS-based users: these are the graphical chars (blocks, lines etc)
924  *
925  * unix-gurus: before you start adding other attributes like A_REVERSE
926  * think hard about how map_info() in cave.c should handle the color
927  * of something that we here draw in reverse. It's not so simple, alas.
928  */
929 static void Term_acs_text_gcu(int x, int y, int n, byte a, concptr s)
930 {
931    term_data *td = (term_data *)(Term->data);
932    int i;
933
934    /* position the cursor */
935    wmove(td->win, y, x);
936
937 #ifdef A_COLOR
938    /* Set the color */
939    wattrset(td->win, colortable[a & 0x0F]);
940 #endif
941
942    for (i=0; i < n; i++)
943    {
944       /* add acs_map of a */
945       waddch(td->win, acs_map[(int)s[i]]);
946    }
947    wattrset(td->win, WA_NORMAL);
948 }
949 #endif
950
951 /*
952  * Place some text on the screen using an attribute
953  */
954 static errr Term_text_gcu(int x, int y, int n, byte a, concptr s)
955 {
956    term_data *td = (term_data *)(Term->data);
957
958    int i;
959
960    char text[81];
961
962 #ifdef USE_NCURSES_ACS
963    /* do we have colors + 16 ? */
964    /* then call special routine for drawing special characters */
965    if (a & 0x10)
966    {
967       Term_acs_text_gcu(x, y, n, a, s);
968       return(0);
969    }
970 #endif
971
972    /* Obtain a copy of the text */
973    for (i = 0; i < n; i++) text[i] = s[i];
974    text[n] = 0;
975
976    /* Move the cursor and dump the string */
977    wmove(td->win, y, x);
978
979 #ifdef A_COLOR
980    /* Set the color */
981    if (can_use_color) wattrset(td->win, colortable[a & 0x0F]);
982 #endif
983
984    /* Add the text */
985    waddstr(td->win, text);
986
987    /* Success */
988    return (0);
989 }
990
991
992
993 static errr term_data_init(term_data *td, int rows, int cols, int y, int x)
994 {
995    term *t = &td->t;
996
997    /* Make sure the window has a positive size */
998    if (rows <= 0 || cols <= 0) return (0);
999
1000    /* Create a window */
1001    td->win = newwin(rows, cols, y, x);
1002
1003    /* Make sure we succeed */
1004    if (!td->win)
1005    {
1006       plog("Failed to setup curses window.");
1007       return (-1);
1008    }
1009
1010    /* Initialize the term */
1011    term_init(t, cols, rows, 256);
1012
1013    /* Avoid the bottom right corner */
1014    t->icky_corner = TRUE;
1015
1016    /* Erase with "white space" */
1017    t->attr_blank = TERM_WHITE;
1018    t->char_blank = ' ';
1019
1020    /* Set some hooks */
1021    t->init_hook = Term_init_gcu;
1022    t->nuke_hook = Term_nuke_gcu;
1023
1024    /* Set some more hooks */
1025    t->text_hook = Term_text_gcu;
1026    t->wipe_hook = Term_wipe_gcu;
1027    t->curs_hook = Term_curs_gcu;
1028    t->xtra_hook = Term_xtra_gcu;
1029
1030    /* Save the data */
1031    t->data = td;
1032
1033    /* Activate it */
1034    Term_activate(t);
1035
1036
1037    /* Success */
1038    return (0);
1039 }
1040
1041
1042 static void hook_quit(concptr str)
1043 {
1044         /* Unused */
1045         (void)str;
1046
1047        /* Exit curses */
1048        endwin();
1049 }
1050
1051
1052 /*
1053  * Prepare "curses" for use by the file "term.c"
1054  *
1055  * Installs the "hook" functions defined above, and then activates
1056  * the main screen "term", which clears the screen and such things.
1057  *
1058  * Someone should really check the semantics of "initscr()"
1059  */
1060 errr init_gcu(int argc, char *argv[])
1061 {
1062    int i;
1063
1064    int num_term = 4, next_win = 0;
1065    char path[1024];
1066
1067    /* Unused */
1068    (void)argc;
1069    (void)argv;
1070
1071
1072    setlocale(LC_ALL, "");
1073
1074    /* Build the "sound" path */
1075    path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "sound");
1076
1077    /* Allocate the path */
1078    ANGBAND_DIR_XTRA_SOUND = string_make(path);
1079
1080    /* Extract the normal keymap */
1081    keymap_norm_prepare();
1082
1083    /* Initialize for others systems */
1084    if (initscr() == (WINDOW*)ERR) return (-1);
1085
1086    /* Activate hooks */
1087    quit_aux = hook_quit;
1088    core_aux = hook_quit;
1089
1090    /* Hack -- Require large screen, or Quit with message */
1091    i = ((LINES < 24) || (COLS < 80));
1092    if (i) quit("Angband needs an 80x24 'curses' screen");
1093
1094
1095 #ifdef A_COLOR
1096
1097    /*** Init the Color-pairs and set up a translation table ***/
1098
1099    /* Do we have color, and enough color, available? */
1100    can_use_color = ((start_color() != ERR) && has_colors() &&
1101                     (COLORS >= 8) && (COLOR_PAIRS >= 8));
1102
1103 #ifdef REDEFINE_COLORS
1104         /* Can we change colors? */
1105         can_fix_color = (can_use_color && can_change_color() &&
1106                          (COLORS >= 16) && (COLOR_PAIRS > 8));
1107 #endif
1108
1109    /* Attempt to use customized colors */
1110    if (can_fix_color)
1111    {
1112       /* Prepare the color pairs */
1113            for (i = 1; i <= 15; i++)
1114            {
1115                    if (init_pair(i, i, 0) == ERR)
1116                    {
1117                            quit("Color pair init failed");
1118                    }
1119
1120                    colortable[i] = COLOR_PAIR(i);
1121                    Term_xtra_gcu_react();
1122            }
1123    }
1124    /* Attempt to use colors */
1125    else if (can_use_color)
1126    {
1127                 /* Color-pair 0 is *always* WHITE on BLACK */
1128
1129                 /* Prepare the color pairs */
1130                 init_pair(1, COLOR_RED,     COLOR_BLACK);
1131                 init_pair(2, COLOR_GREEN,   COLOR_BLACK);
1132                 init_pair(3, COLOR_YELLOW,  COLOR_BLACK);
1133                 init_pair(4, COLOR_BLUE,    COLOR_BLACK);
1134                 init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
1135                 init_pair(6, COLOR_CYAN,    COLOR_BLACK);
1136                 init_pair(7, COLOR_BLACK,   COLOR_BLACK);
1137
1138                 /* Prepare the "Angband Colors" -- Bright white is too bright */
1139                 /* Changed in Drangband. Cyan as grey sucks -- -TM- */
1140                 colortable[0] = (COLOR_PAIR(7) | A_NORMAL);     /* Black */
1141                 colortable[1] = (COLOR_PAIR(0) | A_BRIGHT);     /* White */
1142                 colortable[2] = (COLOR_PAIR(0) | A_NORMAL);     /* Grey XXX */
1143                 colortable[3] = (COLOR_PAIR(1) | A_BRIGHT);     /* Orange XXX */
1144                 colortable[4] = (COLOR_PAIR(1) | A_NORMAL);     /* Red */
1145                 colortable[5] = (COLOR_PAIR(2) | A_NORMAL);     /* Green */
1146                 colortable[6] = (COLOR_PAIR(4) | A_BRIGHT);     /* Blue */
1147                 colortable[7] = (COLOR_PAIR(3) | A_NORMAL);     /* Umber */
1148                 colortable[8] = (COLOR_PAIR(7) | A_BRIGHT);     /* Dark-grey XXX */
1149                 colortable[9] = (COLOR_PAIR(0) | A_NORMAL);     /* Light-grey XXX */
1150                 colortable[10] = (COLOR_PAIR(5) | A_BRIGHT);    /* Purple */
1151                 colortable[11] = (COLOR_PAIR(3) | A_BRIGHT);    /* Yellow */
1152                 colortable[12] = (COLOR_PAIR(5) | A_NORMAL);    /* Light Red XXX */
1153                 colortable[13] = (COLOR_PAIR(2) | A_BRIGHT);    /* Light Green */
1154                 colortable[14] = (COLOR_PAIR(6) | A_BRIGHT);    /* Light Blue */
1155                 colortable[15] = (COLOR_PAIR(3) | A_NORMAL);    /* Light Umber XXX */
1156
1157    }
1158
1159 #endif
1160
1161    /* Handle "arg_sound" */
1162    if (use_sound != arg_sound)
1163    {
1164       /* Initialize (if needed) */
1165       if (arg_sound && !init_sound())
1166       {
1167          /* Warning */
1168          plog("Cannot initialize sound!");
1169
1170          /* Cannot enable */
1171          arg_sound = FALSE;
1172       }
1173
1174       /* Change setting */
1175       use_sound = arg_sound;
1176    }
1177
1178    /* Try graphics */
1179    if (arg_graphics)
1180    {
1181       /* if USE_NCURSES_ACS is defined, we can do something with graphics in curses! */
1182 #ifdef USE_NCURSES_ACS
1183       use_graphics = TRUE;
1184 #endif
1185    }
1186
1187    /*** Low level preparation ***/
1188
1189 #ifdef USE_GETCH
1190
1191    /* Paranoia -- Assume no waiting */
1192    nodelay(stdscr, FALSE);
1193
1194 #endif
1195
1196    /* Prepare */
1197    cbreak();
1198    noecho();
1199    nonl();
1200    raw();
1201
1202    /* Extract the game keymap */
1203    keymap_game_prepare();
1204
1205
1206    /*** Now prepare the term(s) ***/
1207    for (i = 0; i < num_term; i++)
1208    {
1209       int rows, cols;
1210       int y, x;
1211
1212       switch (i)
1213       {
1214          /* Upper left */
1215          case 0: rows = 24;
1216             cols = 80;
1217             y = x = 0;
1218             break;
1219          /* Lower left */
1220          case 1: rows = LINES - 25;
1221             cols = 80;
1222             y = 24;
1223             x = 0;
1224             break;
1225          /* Upper right */
1226          case 2: rows = 24;
1227             cols = COLS - 81;
1228             y = 0;
1229             x = 81;
1230             break;
1231          /* Lower right */
1232          case 3: rows = LINES - 25;
1233             cols = COLS - 81;
1234             y = 24;
1235             x = 81;
1236             break;
1237          /* XXX */
1238          default: rows = cols = 0;
1239              y = x = 0;
1240              break;
1241       }
1242
1243       /* No non-windows */
1244       if (rows <= 0 || cols <= 0) continue;
1245
1246       /* Initialize */
1247       term_data_init(&data[next_win], rows, cols, y, x);
1248
1249       /* Store */
1250       angband_term[next_win] = Term;
1251
1252       next_win++;
1253    }
1254
1255    /* Activate the "Angband" window screen */
1256    Term_activate(&data[0].t);
1257
1258    /* Store */
1259    term_screen = &data[0].t;
1260
1261    /* Success */
1262    return (0);
1263 }
1264
1265
1266 #endif /* USE_GCU */