OSDN Git Service

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