1 /* File: main-cap.c */
3 /* Purpose: Support for "term.c" using "termcap" calls */
5 #include "io/exit-panic.h"
6 #include "system/angband.h"
11 * This file is a total hack, but is often very helpful. :-)
13 * This file allows use of the terminal without requiring the
14 * "curses" routines. In fact, if "USE_HARDCODE" is defined,
15 * this file will attempt to use various hard-coded "vt100"
16 * escape sequences to also avoid the use of the "termcap"
17 * routines. I do not know if this will work on System V.
19 * This file is intended for use only on those machines which are
20 * unable, for whatever reason, to compile the "main-gcu.c" file,
21 * but which seem to be able to support the "termcap" library, or
22 * which at least seem able to support "vt100" terminals.
24 * Large portions of this file were stolen from "main-gcu.c"
26 * This file incorrectly handles output to column 80, I think.
32 #if !defined(USE_TERMCAP) && !defined(USE_HARDCODE)
37 * Hack -- try to guess which systems use what commands
38 * Hack -- allow one of the "USE_Txxxxx" flags to be pre-set.
39 * Mega-Hack -- try to guess when "POSIX" is available.
40 * If the user defines two of these, we will probably crash.
52 #include <sys/ioctl.h>
57 * One version needs these files
60 #include <sys/ioctl.h>
65 * The other needs these files
69 #include <sys/ioctl.h>
70 #include <sys/param.h>
71 #include <sys/resource.h>
72 #include <sys/types.h>
76 * XXX XXX Hack -- POSIX uses "O_NONBLOCK" instead of "O_NDELAY"
78 * They should both work due to the "(i != 1)" test in the code
79 * which checks for the result of the "read()" command.
82 #define O_NDELAY O_NONBLOCK
88 * Termcap string information
91 static char blob[1024]; /* The "termcap" entry */
92 static char area[1024]; /* The string extraction buffer */
93 static char *next = area; /* The current "index" into "area" */
94 static char *desc; /* The terminal name */
99 * Pointers into the "area"
102 static char *cm; /* Move cursor */
103 static char *ch; /* Move cursor to horizontal location */
104 static char *cv; /* Move cursor to vertical location */
105 static char *ho; /* Move cursor to top left */
106 static char *ll; /* Move cursor to bottom left */
107 static char *cs; /* Set scroll area */
108 static char *cl; /* Clear screen */
109 static char *cd; /* Clear to end of display */
110 static char *ce; /* Clear to end of line */
111 static char *cr; /* Move to start of line */
112 static char *so; /* Turn on standout */
113 static char *se; /* Turn off standout */
114 static char *md; /* Turn on bold */
115 static char *me; /* Turn off bold */
116 static char *vi; /* Cursor - invisible */
117 static char *ve; /* Cursor - normal */
118 static char *vs; /* Cursor - bright */
124 static int rows; /* Screen size (Y) */
125 static int cols; /* Screen size (X) */
126 static int curx; /* Cursor location (X) */
127 static int cury; /* Cursor location (Y) */
128 static int curv; /* Cursor visibility */
133 extern char *getenv();
134 extern char *tgoto();
135 extern char *tgetstr();
138 * Write some chars to the terminal
140 static void ewrite(char *str)
142 int numtowrite, numwritten;
144 /* See how much work we have */
145 numtowrite = strlen(str);
147 /* Write until done */
148 while (numtowrite > 0) {
149 /* Try to write the chars */
150 numwritten = write(1, str, numtowrite);
152 /* Handle FIFOs and EINTR */
153 if (numwritten < 0) {
157 /* See what we completed */
158 numtowrite -= numwritten;
161 /* Hack -- sleep if not done */
162 if (numtowrite > 0) {
170 static char write_buffer[128];
171 static char *write_buffer_ptr;
173 static void output_one(char c)
175 *write_buffer_ptr++ = c;
178 static void tp(char *s)
180 /* Dump the string into us */
181 write_buffer_ptr = write_buffer;
183 /* Write the string with padding */
184 tputs(s, 1, output_one);
186 /* Finish the string */
187 *write_buffer_ptr = '\0';
189 /* Dump the recorded buffer */
190 ewrite(write_buffer);
197 static void tp(char *s)
207 static void do_cl(void)
215 * Clear to the end of the line
217 static void do_ce(void)
225 * Set the cursor visibility (0 = invis, 1 = normal, 2 = bright)
227 static void curs_set(int vis)
233 } else if (vis > 1) {
245 * Restrict scrolling to within these rows
247 static void do_cs(int y1, int y2)
252 tp(tgoto(cs, y2, y1));
258 sprintf(temp, cs, y1, y2);
264 * Go to the given screen location directly
266 static void do_cm(int x, int y)
277 sprintf(temp, cm, y + 1, x + 1);
283 * Go to the given screen location in a "clever" manner
285 * XXX XXX XXX This function could use some work!
287 static void do_move(int x1, int y1, int x2, int y2)
289 /* Hack -- unknown start location */
290 if ((x1 == x2) && (y1 == y2)) {
296 if ((y2 <= 0) && ho) {
298 } else if ((y2 >= rows - 1) && ll) {
300 } else if ((y2 == y1) && cr) {
307 /* Default -- go directly there */
314 * Help initialize this file (see below)
316 errr init_cap_aux(void)
321 /* Get the terminal name (if possible) */
322 desc = getenv("TERM");
327 /* Get the terminal info */
328 if (tgetent(blob, desc) != 1) {
332 /* Get the (initial) columns and rows, or default */
333 if ((cols = tgetnum("co")) == -1) {
336 if ((rows = tgetnum("li")) == -1) {
340 /* Find out how to move the cursor to a given location */
341 cm = tgetstr("cm", &next);
346 /* Find out how to move the cursor to a given position */
347 ch = tgetstr("ch", &next);
348 cv = tgetstr("cv", &next);
350 /* Find out how to "home" the screen */
351 ho = tgetstr("ho", &next);
353 /* Find out how to "last-line" the screen */
354 ll = tgetstr("ll", &next);
356 /* Find out how to do a "carriage return" */
357 cr = tgetstr("cr", &next);
362 /* Find out how to clear the screen */
363 cl = tgetstr("cl", &next);
368 /* Find out how to clear to the end of display */
369 cd = tgetstr("cd", &next);
371 /* Find out how to clear to the end of the line */
372 ce = tgetstr("ce", &next);
374 /* Find out how to scroll (set the scroll region) */
375 cs = tgetstr("cs", &next);
377 /* Find out how to hilite */
378 so = tgetstr("so", &next);
379 se = tgetstr("se", &next);
384 /* Find out how to bold */
385 md = tgetstr("md", &next);
386 me = tgetstr("me", &next);
391 /* Check the cursor visibility stuff */
392 vi = tgetstr("vi", &next);
393 vs = tgetstr("vs", &next);
394 ve = tgetstr("ve", &next);
400 /* Assume some defualt information */
405 cl = "\033[2J\033[H"; /* --]--]-- */
407 /* Clear to end of line */
408 ce = "\033[K"; /* --]-- */
411 so = "\033[7m"; /* --]-- */
412 se = "\033[m"; /* --]-- */
415 cs = "\033[%d;%dr"; /* --]-- */
418 cm = "\033[%d;%dH"; /* --]-- */
427 * Save the "normal" and "angband" terminal settings
432 static struct termios norm_termios;
434 static struct termios game_termios;
440 static struct termio norm_termio;
442 static struct termio game_termio;
448 static struct sgttyb norm_ttyb;
449 static struct tchars norm_tchars;
450 static struct ltchars norm_ltchars;
451 static int norm_local_chars;
453 static struct sgttyb game_ttyb;
454 static struct tchars game_tchars;
455 static struct ltchars game_ltchars;
456 static int game_local_chars;
461 * Are we active? Not really needed.
463 static int active = false;
466 * The main screen (no sub-screens)
468 static term term_screen_body;
471 * Place the "keymap" into its "normal" state
473 static void keymap_norm(void)
478 /* restore the saved values of the special chars */
479 (void)tcsetattr(0, TCSAFLUSH, &norm_termios);
485 /* restore the saved values of the special chars */
486 (void)ioctl(0, TCSETA, (char *)&norm_termio);
492 /* restore the saved values of the special chars */
493 (void)ioctl(0, TIOCSETP, (char *)&norm_ttyb);
494 (void)ioctl(0, TIOCSETC, (char *)&norm_tchars);
495 (void)ioctl(0, TIOCSLTC, (char *)&norm_ltchars);
496 (void)ioctl(0, TIOCLSET, (char *)&norm_local_chars);
502 * Place the "keymap" into the "game" state
504 static void keymap_game(void)
509 /* restore the saved values of the special chars */
510 (void)tcsetattr(0, TCSAFLUSH, &game_termios);
516 /* restore the saved values of the special chars */
517 (void)ioctl(0, TCSETA, (char *)&game_termio);
523 /* restore the saved values of the special chars */
524 (void)ioctl(0, TIOCSETP, (char *)&game_ttyb);
525 (void)ioctl(0, TIOCSETC, (char *)&game_tchars);
526 (void)ioctl(0, TIOCSLTC, (char *)&game_ltchars);
527 (void)ioctl(0, TIOCLSET, (char *)&game_local_chars);
533 * Save the normal keymap
535 static void keymap_norm_prepare(void)
540 /* Get the normal keymap */
541 tcgetattr(0, &norm_termios);
547 /* Get the normal keymap */
548 (void)ioctl(0, TCGETA, (char *)&norm_termio);
554 /* Get the normal keymap */
555 (void)ioctl(0, TIOCGETP, (char *)&norm_ttyb);
556 (void)ioctl(0, TIOCGETC, (char *)&norm_tchars);
557 (void)ioctl(0, TIOCGLTC, (char *)&norm_ltchars);
558 (void)ioctl(0, TIOCLGET, (char *)&norm_local_chars);
564 * Save the keymaps (normal and game)
566 static void keymap_game_prepare(void)
571 /* Acquire the current mapping */
572 tcgetattr(0, &game_termios);
574 /* Force "Ctrl-C" to interupt */
575 game_termios.c_cc[VINTR] = (char)3;
577 /* Force "Ctrl-Z" to suspend */
578 game_termios.c_cc[VSUSP] = (char)26;
580 /* Hack -- Leave "VSTART/VSTOP" alone */
582 /* Disable the standard control characters */
583 game_termios.c_cc[VQUIT] = (char)-1;
584 game_termios.c_cc[VERASE] = (char)-1;
585 game_termios.c_cc[VKILL] = (char)-1;
586 game_termios.c_cc[VEOF] = (char)-1;
587 game_termios.c_cc[VEOL] = (char)-1;
589 /* Normally, block until a character is read */
590 game_termios.c_cc[VMIN] = 1;
591 game_termios.c_cc[VTIME] = 0;
593 /* Hack -- Turn off "echo" and "canonical" mode */
594 game_termios.c_lflag &= ~(ECHO | ICANON);
596 /* Turn off flow control */
597 game_termios.c_iflag &= ~IXON;
603 /* Acquire the current mapping */
604 (void)ioctl(0, TCGETA, (char *)&game_termio);
606 /* Force "Ctrl-C" to interupt */
607 game_termio.c_cc[VINTR] = (char)3;
609 /* Force "Ctrl-Z" to suspend */
610 game_termio.c_cc[VSUSP] = (char)26;
612 /* Hack -- Leave "VSTART/VSTOP" alone */
614 /* Disable the standard control characters */
615 game_termio.c_cc[VQUIT] = (char)-1;
616 game_termio.c_cc[VERASE] = (char)-1;
617 game_termio.c_cc[VKILL] = (char)-1;
618 game_termio.c_cc[VEOF] = (char)-1;
619 game_termio.c_cc[VEOL] = (char)-1;
621 /* Normally, block until a character is read */
622 game_termio.c_cc[VMIN] = 1;
623 game_termio.c_cc[VTIME] = 0;
625 /* Hack -- Turn off "echo" and "canonical" mode */
626 game_termio.c_lflag &= ~(ECHO | ICANON);
628 /* Turn off flow control */
629 game_termio.c_iflag &= ~IXON;
635 /* Get the default game characters */
636 (void)ioctl(0, TIOCGETP, (char *)&game_ttyb);
637 (void)ioctl(0, TIOCGETC, (char *)&game_tchars);
638 (void)ioctl(0, TIOCGLTC, (char *)&game_ltchars);
639 (void)ioctl(0, TIOCLGET, (char *)&game_local_chars);
641 /* Force interupt (^C) */
642 game_tchars.t_intrc = (char)3;
644 /* Force start/stop (^Q, ^S) */
645 game_tchars.t_startc = (char)17;
646 game_tchars.t_stopc = (char)19;
648 /* Cancel some things */
649 game_tchars.t_quitc = (char)-1;
650 game_tchars.t_eofc = (char)-1;
651 game_tchars.t_brkc = (char)-1;
653 /* Force suspend (^Z) */
654 game_ltchars.t_suspc = (char)26;
656 /* Cancel some things */
657 game_ltchars.t_dsuspc = (char)-1;
658 game_ltchars.t_rprntc = (char)-1;
659 game_ltchars.t_flushc = (char)-1;
660 game_ltchars.t_werasc = (char)-1;
661 game_ltchars.t_lnextc = (char)-1;
663 /* XXX XXX XXX XXX Verify this before use */
664 /* Hack -- Turn off "echo" and "canonical" mode */
665 /* game_termios.c_lflag &= ~(ECHO | ICANON); */
666 game_ttyb.flag &= ~(ECHO | ICANON);
668 /* XXX XXX XXX Should maybe turn off flow control too. How? */
676 static errr game_term_xtra_cap_alive(int v)
684 /* Hack -- make sure the cursor is visible */
687 /* Move to bottom right */
688 do_move(0, rows - 1, 0, rows - 1);
690 /* Go to normal keymap mode */
693 /* No longer active */
703 /* Hack -- restore the cursor location */
704 do_move(curx, cury, curx, cury);
706 /* Hack -- restore the cursor visibility */
709 /* Go to angband keymap mode */
712 /* Now we are active */
723 static errr game_term_xtra_cap_event(int v)
730 /* Wait for one byte */
733 /* Hack -- Handle "errors" */
734 if ((i <= 0) && (errno != EINTR)) {
735 exit_game_panic(p_ptr);
741 /* Get the current flags for stdin */
742 if ((arg = fcntl(0, F_GETFL, 0)) < 1) {
746 /* Tell stdin not to block */
747 if (fcntl(0, F_SETFL, arg | O_NDELAY) < 0) {
751 /* Read one byte, if possible */
754 /* Replace the flags for stdin */
755 if (fcntl(0, F_SETFL, arg)) {
761 if ((i != 1) || (!buf[0])) {
765 /* Enqueue the keypress */
766 game_term_keypress(buf[0]);
773 * Actually move the hardware cursor
775 static errr game_term_curs_cap(int x, int y)
777 /* Literally move the cursor */
778 do_move(curx, cury, x, y);
780 /* Save the cursor location */
789 * Erase a grid of space
791 * XXX XXX XXX Note that we will never be asked to clear the
792 * bottom line all the way to the bottom right edge, since we
793 * have set the "avoid the bottom right corner" flag.
795 static errr game_term_wipe_cap(int x, int y, int n)
799 /* Place the cursor */
800 game_term_curs_cap(x, y);
802 /* Wipe to end of line */
809 for (dx = 0; dx < n; ++dx) {
820 * Place some text on the screen using an attribute
822 static errr game_term_text_cap(int x, int y, int n, byte a, concptr s)
826 /* Move the cursor */
827 game_term_curs_cap(x, y);
829 /* Dump the text, advance the cursor */
830 for (i = 0; s[i]; i++) {
834 /* Advance cursor 'X', and wrap */
835 if (++curx >= cols) {
836 /* Reset cursor 'X' */
839 /* Hack -- Advance cursor 'Y', and wrap */
840 if (++cury == rows) {
851 * Handle a "special request"
853 static errr game_term_xtra_cap(int n, int v)
855 /* Analyze the request */
857 /* Clear the screen */
858 case TERM_XTRA_CLEAR:
864 case TERM_XTRA_NOISE:
865 (void)write(1, "\007", 1);
868 /* Change the cursor visibility */
869 case TERM_XTRA_SHAPE:
875 case TERM_XTRA_ALIVE:
876 return game_term_xtra_cap_alive(v);
879 case TERM_XTRA_EVENT:
880 return game_term_xtra_cap_event(v);
883 case TERM_XTRA_FLUSH:
884 while (!game_term_xtra_cap_event(false)) {
890 case TERM_XTRA_DELAY:
900 * Init a "term" for this file
902 static void game_term_init_cap(term *t)
908 /* Assume cursor at top left */
912 /* Assume visible cursor */
915 /* Clear the screen */
918 /* Hack -- visible cursor */
926 * Nuke a "term" for this file
928 static void game_term_nuke_cap(term *t)
934 /* Hack -- make sure the cursor is visible */
937 /* Move to bottom right */
938 do_move(0, rows - 1, 0, rows - 1);
943 /* No longer active */
948 * Prepare this file for Angband usage
952 term *t = &term_screen_body;
956 /* Initialize the screen */
957 if (init_cap_aux()) {
961 /* Hack -- Require large screen, or Quit with message */
962 if ((rows < 24) || (cols < 80)) {
963 quit("Screen too small!");
966 /*** Prepare to play ***/
968 /* Extract the normal keymap */
969 keymap_norm_prepare();
971 /* Extract the game keymap */
972 keymap_game_prepare();
974 /* Hack -- activate the game keymap */
977 /* Hack -- Do NOT buffer stdout */
978 setbuf(stdout, nullptr);
980 /*** Now prepare the term ***/
982 /* Initialize the term */
983 term_init(t, 80, 24, 256);
985 /* Avoid the bottom right corner */
986 t->icky_corner = true;
988 /* Erase with "white space" */
989 t->attr_blank = TERM_WHITE;
993 t->init_hook = game_term_init_cap;
994 t->nuke_hook = game_term_nuke_cap;
996 /* Set some more hooks */
997 t->text_hook = game_term_text_cap;
998 t->wipe_hook = game_term_wipe_cap;
999 t->curs_hook = game_term_curs_cap;
1000 t->xtra_hook = game_term_xtra_cap;
1006 game_term_activate(term_screen);
1012 #endif /* USE_CAP */