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"
7 #include "term/z-form.h"
12 * This file is a total hack, but is often very helpful. :-)
14 * This file allows use of the terminal without requiring the
15 * "curses" routines. In fact, if "USE_HARDCODE" is defined,
16 * this file will attempt to use various hard-coded "vt100"
17 * escape sequences to also avoid the use of the "termcap"
18 * routines. I do not know if this will work on System V.
20 * This file is intended for use only on those machines which are
21 * unable, for whatever reason, to compile the "main-gcu.c" file,
22 * but which seem to be able to support the "termcap" library, or
23 * which at least seem able to support "vt100" terminals.
25 * Large portions of this file were stolen from "main-gcu.c"
27 * This file incorrectly handles output to column 80, I think.
33 #if !defined(USE_TERMCAP) && !defined(USE_HARDCODE)
38 * Hack -- try to guess which systems use what commands
39 * Hack -- allow one of the "USE_Txxxxx" flags to be pre-set.
40 * Mega-Hack -- try to guess when "POSIX" is available.
41 * If the user defines two of these, we will probably crash.
53 #include <sys/ioctl.h>
58 * One version needs these files
61 #include <sys/ioctl.h>
66 * The other needs these files
70 #include <sys/ioctl.h>
71 #include <sys/param.h>
72 #include <sys/resource.h>
73 #include <sys/types.h>
77 * XXX XXX Hack -- POSIX uses "O_NONBLOCK" instead of "O_NDELAY"
79 * They should both work due to the "(i != 1)" test in the code
80 * which checks for the result of the "read()" command.
83 #define O_NDELAY O_NONBLOCK
89 * Termcap string information
92 static char blob[1024]; /* The "termcap" entry */
93 static char area[1024]; /* The string extraction buffer */
94 static char *next = area; /* The current "index" into "area" */
95 static char *desc; /* The terminal name */
100 * Pointers into the "area"
103 static char *cm; /* Move cursor */
104 static char *ch; /* Move cursor to horizontal location */
105 static char *cv; /* Move cursor to vertical location */
106 static char *ho; /* Move cursor to top left */
107 static char *ll; /* Move cursor to bottom left */
108 static char *cs; /* Set scroll area */
109 static char *cl; /* Clear screen */
110 static char *cd; /* Clear to end of display */
111 static char *ce; /* Clear to end of line */
112 static char *cr; /* Move to start of line */
113 static char *so; /* Turn on standout */
114 static char *se; /* Turn off standout */
115 static char *md; /* Turn on bold */
116 static char *me; /* Turn off bold */
117 static char *vi; /* Cursor - invisible */
118 static char *ve; /* Cursor - normal */
119 static char *vs; /* Cursor - bright */
125 static int rows; /* Screen size (Y) */
126 static int cols; /* Screen size (X) */
127 static int curx; /* Cursor location (X) */
128 static int cury; /* Cursor location (Y) */
129 static int curv; /* Cursor visibility */
134 extern char *getenv();
135 extern char *tgoto();
136 extern char *tgetstr();
139 * Write some chars to the terminal
141 static void ewrite(char *str)
143 int numtowrite, numwritten;
145 /* See how much work we have */
146 numtowrite = strlen(str);
148 /* Write until done */
149 while (numtowrite > 0) {
150 /* Try to write the chars */
151 numwritten = write(1, str, numtowrite);
153 /* Handle FIFOs and EINTR */
154 if (numwritten < 0) {
158 /* See what we completed */
159 numtowrite -= numwritten;
162 /* Hack -- sleep if not done */
163 if (numtowrite > 0) {
171 static char write_buffer[128];
172 static char *write_buffer_ptr;
174 static void output_one(char c)
176 *write_buffer_ptr++ = c;
179 static void tp(char *s)
181 /* Dump the string into us */
182 write_buffer_ptr = write_buffer;
184 /* Write the string with padding */
185 tputs(s, 1, output_one);
187 /* Finish the string */
188 *write_buffer_ptr = '\0';
190 /* Dump the recorded buffer */
191 ewrite(write_buffer);
198 static void tp(char *s)
208 static void do_cl(void)
216 * Clear to the end of the line
218 static void do_ce(void)
226 * Set the cursor visibility (0 = invis, 1 = normal, 2 = bright)
228 static void curs_set(int vis)
234 } else if (vis > 1) {
246 * Restrict scrolling to within these rows
248 static void do_cs(int y1, int y2)
253 tp(tgoto(cs, y2, y1));
259 strnfmt(temp, sizeof(temp), cs, y1, y2);
265 * Go to the given screen location directly
267 static void do_cm(int x, int y)
278 strnfmt(temp, sizeof(temp), cm, y + 1, x + 1);
284 * Go to the given screen location in a "clever" manner
286 * XXX XXX XXX This function could use some work!
288 static void do_move(int x1, int y1, int x2, int y2)
290 /* Hack -- unknown start location */
291 if ((x1 == x2) && (y1 == y2)) {
297 if ((y2 <= 0) && ho) {
299 } else if ((y2 >= rows - 1) && ll) {
301 } else if ((y2 == y1) && cr) {
308 /* Default -- go directly there */
315 * Help initialize this file (see below)
317 errr init_cap_aux(void)
322 /* Get the terminal name (if possible) */
323 desc = getenv("TERM");
328 /* Get the terminal info */
329 if (tgetent(blob, desc) != 1) {
333 /* Get the (initial) columns and rows, or default */
334 if ((cols = tgetnum("co")) == -1) {
337 if ((rows = tgetnum("li")) == -1) {
341 /* Find out how to move the cursor to a given location */
342 cm = tgetstr("cm", &next);
347 /* Find out how to move the cursor to a given position */
348 ch = tgetstr("ch", &next);
349 cv = tgetstr("cv", &next);
351 /* Find out how to "home" the screen */
352 ho = tgetstr("ho", &next);
354 /* Find out how to "last-line" the screen */
355 ll = tgetstr("ll", &next);
357 /* Find out how to do a "carriage return" */
358 cr = tgetstr("cr", &next);
363 /* Find out how to clear the screen */
364 cl = tgetstr("cl", &next);
369 /* Find out how to clear to the end of display */
370 cd = tgetstr("cd", &next);
372 /* Find out how to clear to the end of the line */
373 ce = tgetstr("ce", &next);
375 /* Find out how to scroll (set the scroll region) */
376 cs = tgetstr("cs", &next);
378 /* Find out how to hilite */
379 so = tgetstr("so", &next);
380 se = tgetstr("se", &next);
385 /* Find out how to bold */
386 md = tgetstr("md", &next);
387 me = tgetstr("me", &next);
392 /* Check the cursor visibility stuff */
393 vi = tgetstr("vi", &next);
394 vs = tgetstr("vs", &next);
395 ve = tgetstr("ve", &next);
401 /* Assume some defualt information */
406 cl = "\033[2J\033[H"; /* --]--]-- */
408 /* Clear to end of line */
409 ce = "\033[K"; /* --]-- */
412 so = "\033[7m"; /* --]-- */
413 se = "\033[m"; /* --]-- */
416 cs = "\033[%d;%dr"; /* --]-- */
419 cm = "\033[%d;%dH"; /* --]-- */
428 * Save the "normal" and "angband" terminal settings
433 static struct termios norm_termios;
435 static struct termios game_termios;
441 static struct termio norm_termio;
443 static struct termio game_termio;
449 static struct sgttyb norm_ttyb;
450 static struct tchars norm_tchars;
451 static struct ltchars norm_ltchars;
452 static int norm_local_chars;
454 static struct sgttyb game_ttyb;
455 static struct tchars game_tchars;
456 static struct ltchars game_ltchars;
457 static int game_local_chars;
462 * Are we active? Not really needed.
464 static int active = false;
467 * The main screen (no sub-screens)
469 static term term_screen_body;
472 * Place the "keymap" into its "normal" state
474 static void keymap_norm(void)
479 /* restore the saved values of the special chars */
480 (void)tcsetattr(0, TCSAFLUSH, &norm_termios);
486 /* restore the saved values of the special chars */
487 (void)ioctl(0, TCSETA, (char *)&norm_termio);
493 /* restore the saved values of the special chars */
494 (void)ioctl(0, TIOCSETP, (char *)&norm_ttyb);
495 (void)ioctl(0, TIOCSETC, (char *)&norm_tchars);
496 (void)ioctl(0, TIOCSLTC, (char *)&norm_ltchars);
497 (void)ioctl(0, TIOCLSET, (char *)&norm_local_chars);
503 * Place the "keymap" into the "game" state
505 static void keymap_game(void)
510 /* restore the saved values of the special chars */
511 (void)tcsetattr(0, TCSAFLUSH, &game_termios);
517 /* restore the saved values of the special chars */
518 (void)ioctl(0, TCSETA, (char *)&game_termio);
524 /* restore the saved values of the special chars */
525 (void)ioctl(0, TIOCSETP, (char *)&game_ttyb);
526 (void)ioctl(0, TIOCSETC, (char *)&game_tchars);
527 (void)ioctl(0, TIOCSLTC, (char *)&game_ltchars);
528 (void)ioctl(0, TIOCLSET, (char *)&game_local_chars);
534 * Save the normal keymap
536 static void keymap_norm_prepare(void)
541 /* Get the normal keymap */
542 tcgetattr(0, &norm_termios);
548 /* Get the normal keymap */
549 (void)ioctl(0, TCGETA, (char *)&norm_termio);
555 /* Get the normal keymap */
556 (void)ioctl(0, TIOCGETP, (char *)&norm_ttyb);
557 (void)ioctl(0, TIOCGETC, (char *)&norm_tchars);
558 (void)ioctl(0, TIOCGLTC, (char *)&norm_ltchars);
559 (void)ioctl(0, TIOCLGET, (char *)&norm_local_chars);
565 * Save the keymaps (normal and game)
567 static void keymap_game_prepare(void)
572 /* Acquire the current mapping */
573 tcgetattr(0, &game_termios);
575 /* Force "Ctrl-C" to interupt */
576 game_termios.c_cc[VINTR] = (char)3;
578 /* Force "Ctrl-Z" to suspend */
579 game_termios.c_cc[VSUSP] = (char)26;
581 /* Hack -- Leave "VSTART/VSTOP" alone */
583 /* Disable the standard control characters */
584 game_termios.c_cc[VQUIT] = (char)-1;
585 game_termios.c_cc[VERASE] = (char)-1;
586 game_termios.c_cc[VKILL] = (char)-1;
587 game_termios.c_cc[VEOF] = (char)-1;
588 game_termios.c_cc[VEOL] = (char)-1;
590 /* Normally, block until a character is read */
591 game_termios.c_cc[VMIN] = 1;
592 game_termios.c_cc[VTIME] = 0;
594 /* Hack -- Turn off "echo" and "canonical" mode */
595 game_termios.c_lflag &= ~(ECHO | ICANON);
597 /* Turn off flow control */
598 game_termios.c_iflag &= ~IXON;
604 /* Acquire the current mapping */
605 (void)ioctl(0, TCGETA, (char *)&game_termio);
607 /* Force "Ctrl-C" to interupt */
608 game_termio.c_cc[VINTR] = (char)3;
610 /* Force "Ctrl-Z" to suspend */
611 game_termio.c_cc[VSUSP] = (char)26;
613 /* Hack -- Leave "VSTART/VSTOP" alone */
615 /* Disable the standard control characters */
616 game_termio.c_cc[VQUIT] = (char)-1;
617 game_termio.c_cc[VERASE] = (char)-1;
618 game_termio.c_cc[VKILL] = (char)-1;
619 game_termio.c_cc[VEOF] = (char)-1;
620 game_termio.c_cc[VEOL] = (char)-1;
622 /* Normally, block until a character is read */
623 game_termio.c_cc[VMIN] = 1;
624 game_termio.c_cc[VTIME] = 0;
626 /* Hack -- Turn off "echo" and "canonical" mode */
627 game_termio.c_lflag &= ~(ECHO | ICANON);
629 /* Turn off flow control */
630 game_termio.c_iflag &= ~IXON;
636 /* Get the default game characters */
637 (void)ioctl(0, TIOCGETP, (char *)&game_ttyb);
638 (void)ioctl(0, TIOCGETC, (char *)&game_tchars);
639 (void)ioctl(0, TIOCGLTC, (char *)&game_ltchars);
640 (void)ioctl(0, TIOCLGET, (char *)&game_local_chars);
642 /* Force interupt (^C) */
643 game_tchars.t_intrc = (char)3;
645 /* Force start/stop (^Q, ^S) */
646 game_tchars.t_startc = (char)17;
647 game_tchars.t_stopc = (char)19;
649 /* Cancel some things */
650 game_tchars.t_quitc = (char)-1;
651 game_tchars.t_eofc = (char)-1;
652 game_tchars.t_brkc = (char)-1;
654 /* Force suspend (^Z) */
655 game_ltchars.t_suspc = (char)26;
657 /* Cancel some things */
658 game_ltchars.t_dsuspc = (char)-1;
659 game_ltchars.t_rprntc = (char)-1;
660 game_ltchars.t_flushc = (char)-1;
661 game_ltchars.t_werasc = (char)-1;
662 game_ltchars.t_lnextc = (char)-1;
664 /* XXX XXX XXX XXX Verify this before use */
665 /* Hack -- Turn off "echo" and "canonical" mode */
666 /* game_termios.c_lflag &= ~(ECHO | ICANON); */
667 game_ttyb.flag &= ~(ECHO | ICANON);
669 /* XXX XXX XXX Should maybe turn off flow control too. How? */
677 static errr game_term_xtra_cap_alive(int v)
685 /* Hack -- make sure the cursor is visible */
688 /* Move to bottom right */
689 do_move(0, rows - 1, 0, rows - 1);
691 /* Go to normal keymap mode */
694 /* No longer active */
704 /* Hack -- restore the cursor location */
705 do_move(curx, cury, curx, cury);
707 /* Hack -- restore the cursor visibility */
710 /* Go to angband keymap mode */
713 /* Now we are active */
724 static errr game_term_xtra_cap_event(int v)
731 /* Wait for one byte */
734 /* Hack -- Handle "errors" */
735 if ((i <= 0) && (errno != EINTR)) {
736 exit_game_panic(p_ptr);
742 /* Get the current flags for stdin */
743 if ((arg = fcntl(0, F_GETFL, 0)) < 1) {
747 /* Tell stdin not to block */
748 if (fcntl(0, F_SETFL, arg | O_NDELAY) < 0) {
752 /* Read one byte, if possible */
755 /* Replace the flags for stdin */
756 if (fcntl(0, F_SETFL, arg)) {
762 if ((i != 1) || (!buf[0])) {
766 /* Enqueue the keypress */
767 game_term_keypress(buf[0]);
774 * Actually move the hardware cursor
776 static errr game_term_curs_cap(int x, int y)
778 /* Literally move the cursor */
779 do_move(curx, cury, x, y);
781 /* Save the cursor location */
790 * Erase a grid of space
792 * XXX XXX XXX Note that we will never be asked to clear the
793 * bottom line all the way to the bottom right edge, since we
794 * have set the "avoid the bottom right corner" flag.
796 static errr game_term_wipe_cap(int x, int y, int n)
800 /* Place the cursor */
801 game_term_curs_cap(x, y);
803 /* Wipe to end of line */
810 for (dx = 0; dx < n; ++dx) {
821 * Place some text on the screen using an attribute
823 static errr game_term_text_cap(int x, int y, int n, byte a, concptr s)
827 /* Move the cursor */
828 game_term_curs_cap(x, y);
830 /* Dump the text, advance the cursor */
831 for (i = 0; s[i]; i++) {
835 /* Advance cursor 'X', and wrap */
836 if (++curx >= cols) {
837 /* Reset cursor 'X' */
840 /* Hack -- Advance cursor 'Y', and wrap */
841 if (++cury == rows) {
852 * Handle a "special request"
854 static errr game_term_xtra_cap(int n, int v)
856 /* Analyze the request */
858 /* Clear the screen */
859 case TERM_XTRA_CLEAR:
865 case TERM_XTRA_NOISE:
866 (void)write(1, "\007", 1);
869 /* Change the cursor visibility */
870 case TERM_XTRA_SHAPE:
876 case TERM_XTRA_ALIVE:
877 return game_term_xtra_cap_alive(v);
880 case TERM_XTRA_EVENT:
881 return game_term_xtra_cap_event(v);
884 case TERM_XTRA_FLUSH:
885 while (!game_term_xtra_cap_event(false)) {
891 case TERM_XTRA_DELAY:
901 * Init a "term" for this file
903 static void game_term_init_cap(term *t)
909 /* Assume cursor at top left */
913 /* Assume visible cursor */
916 /* Clear the screen */
919 /* Hack -- visible cursor */
927 * Nuke a "term" for this file
929 static void game_term_nuke_cap(term *t)
935 /* Hack -- make sure the cursor is visible */
938 /* Move to bottom right */
939 do_move(0, rows - 1, 0, rows - 1);
944 /* No longer active */
949 * Prepare this file for Angband usage
953 term *t = &term_screen_body;
957 /* Initialize the screen */
958 if (init_cap_aux()) {
962 /* Hack -- Require large screen, or Quit with message */
963 if ((rows < 24) || (cols < 80)) {
964 quit("Screen too small!");
967 /*** Prepare to play ***/
969 /* Extract the normal keymap */
970 keymap_norm_prepare();
972 /* Extract the game keymap */
973 keymap_game_prepare();
975 /* Hack -- activate the game keymap */
978 /* Hack -- Do NOT buffer stdout */
979 setbuf(stdout, nullptr);
981 /*** Now prepare the term ***/
983 /* Initialize the term */
984 term_init(t, 80, 24, 256);
986 /* Avoid the bottom right corner */
987 t->icky_corner = true;
989 /* Erase with "white space" */
990 t->attr_blank = TERM_WHITE;
994 t->init_hook = game_term_init_cap;
995 t->nuke_hook = game_term_nuke_cap;
997 /* Set some more hooks */
998 t->text_hook = game_term_text_cap;
999 t->wipe_hook = game_term_wipe_cap;
1000 t->curs_hook = game_term_curs_cap;
1001 t->xtra_hook = game_term_xtra_cap;
1007 game_term_activate(term_screen);
1013 #endif /* USE_CAP */