1 /* File: main-cap.c */
3 /* Purpose: Support for "term.c" using "termcap" calls */
6 #include "io/exit-panic.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.
34 #if !defined(USE_TERMCAP) && !defined(USE_HARDCODE)
39 * Hack -- try to guess which systems use what commands
40 * Hack -- allow one of the "USE_Txxxxx" flags to be pre-set.
41 * Mega-Hack -- try to guess when "POSIX" is available.
42 * If the user defines two of these, we will probably crash.
44 # if defined(_POSIX_VERSION)
58 # include <sys/ioctl.h>
63 * One version needs these files
66 # include <sys/ioctl.h>
71 * The other needs these files
74 # include <sys/ioctl.h>
75 # include <sys/resource.h>
76 # include <sys/param.h>
77 # include <sys/file.h>
78 # include <sys/types.h>
83 * XXX XXX Hack -- POSIX uses "O_NONBLOCK" instead of "O_NDELAY"
85 * They should both work due to the "(i != 1)" test in the code
86 * which checks for the result of the "read()" command.
89 # define O_NDELAY O_NONBLOCK
98 * Termcap string information
101 static char blob[1024]; /* The "termcap" entry */
102 static char area[1024]; /* The string extraction buffer */
103 static char *next = area; /* The current "index" into "area" */
104 static char *desc; /* The terminal name */
110 * Pointers into the "area"
113 static char *cm; /* Move cursor */
114 static char *ch; /* Move cursor to horizontal location */
115 static char *cv; /* Move cursor to vertical location */
116 static char *ho; /* Move cursor to top left */
117 static char *ll; /* Move cursor to bottom left */
118 static char *cs; /* Set scroll area */
119 static char *cl; /* Clear screen */
120 static char *cd; /* Clear to end of display */
121 static char *ce; /* Clear to end of line */
122 static char *cr; /* Move to start of line */
123 static char *so; /* Turn on standout */
124 static char *se; /* Turn off standout */
125 static char *md; /* Turn on bold */
126 static char *me; /* Turn off bold */
127 static char *vi; /* Cursor - invisible */
128 static char *ve; /* Cursor - normal */
129 static char *vs; /* Cursor - bright */
136 static int rows; /* Screen size (Y) */
137 static int cols; /* Screen size (X) */
138 static int curx; /* Cursor location (X) */
139 static int cury; /* Cursor location (Y) */
140 static int curv; /* Cursor visibility */
146 extern char *getenv();
147 extern char *tgoto();
148 extern char *tgetstr();
152 * Write some chars to the terminal
154 static void ewrite(char *str)
156 int numtowrite, numwritten;
158 /* See how much work we have */
159 numtowrite = strlen(str);
161 /* Write until done */
162 while (numtowrite > 0)
164 /* Try to write the chars */
165 numwritten = write(1, str, numtowrite);
167 /* Handle FIFOs and EINTR */
168 if (numwritten < 0) numwritten = 0;
170 /* See what we completed */
171 numtowrite -= numwritten;
174 /* Hack -- sleep if not done */
175 if (numtowrite > 0) sleep(1);
183 static char write_buffer[128];
184 static char *write_buffer_ptr;
186 static void output_one(char c)
188 *write_buffer_ptr++ = c;
191 static void tp(char *s)
193 /* Dump the string into us */
194 write_buffer_ptr = write_buffer;
196 /* Write the string with padding */
197 tputs (s, 1, output_one);
199 /* Finish the string */
200 *write_buffer_ptr = '\0';
202 /* Dump the recorded buffer */
203 ewrite (write_buffer);
210 static void tp(char *s)
226 static void do_cl(void)
232 * Clear to the end of the line
234 static void do_ce(void)
241 * Set the cursor visibility (0 = invis, 1 = normal, 2 = bright)
243 static void curs_set(int vis)
266 * Restrict scrolling to within these rows
268 static void do_cs(int y1, int y2)
272 if (cs) tp(tgoto(cs, y2, y1));
277 sprintf(temp, cs, y1, y2);
286 * Go to the given screen location directly
288 static void do_cm(int x, int y)
292 if (cm) tp(tgoto(cm, x, y));
297 sprintf(temp, cm, y+1, x+1);
305 * Go to the given screen location in a "clever" manner
307 * XXX XXX XXX This function could use some work!
309 static void do_move(int x1, int y1, int x2, int y2)
311 /* Hack -- unknown start location */
312 if ((x1 == x2) && (y1 == y2)) do_cm(x2, y2);
317 if ((y2 <= 0) && ho) tp(ho);
318 else if ((y2 >= rows-1) && ll) tp(ll);
319 else if ((y2 == y1) && cr) tp(cr);
323 /* Default -- go directly there */
331 * Help initialize this file (see below)
333 errr init_cap_aux(void)
338 /* Get the terminal name (if possible) */
339 desc = getenv("TERM");
340 if (!desc) return (1);
342 /* Get the terminal info */
343 if (tgetent(blob, desc) != 1) return (2);
345 /* Get the (initial) columns and rows, or default */
346 if ((cols = tgetnum("co")) == -1) cols = 80;
347 if ((rows = tgetnum("li")) == -1) rows = 24;
349 /* Find out how to move the cursor to a given location */
350 cm = tgetstr("cm", &next);
351 if (!cm) return (10);
353 /* Find out how to move the cursor to a given position */
354 ch = tgetstr("ch", &next);
355 cv = tgetstr("cv", &next);
357 /* Find out how to "home" the screen */
358 ho = tgetstr("ho", &next);
360 /* Find out how to "last-line" the screen */
361 ll = tgetstr("ll", &next);
363 /* Find out how to do a "carriage return" */
364 cr = tgetstr("cr", &next);
367 /* Find out how to clear the screen */
368 cl = tgetstr("cl", &next);
369 if (!cl) return (11);
371 /* Find out how to clear to the end of display */
372 cd = tgetstr("cd", &next);
374 /* Find out how to clear to the end of the line */
375 ce = tgetstr("ce", &next);
377 /* Find out how to scroll (set the scroll region) */
378 cs = tgetstr("cs", &next);
380 /* Find out how to hilite */
381 so = tgetstr("so", &next);
382 se = tgetstr("se", &next);
383 if (!so || !se) so = se = NULL;
385 /* Find out how to bold */
386 md = tgetstr("md", &next);
387 me = tgetstr("me", &next);
388 if (!md || !me) md = me = NULL;
390 /* Check the cursor visibility stuff */
391 vi = tgetstr("vi", &next);
392 vs = tgetstr("vs", &next);
393 ve = tgetstr("ve", &next);
399 /* Assume some defualt information */
404 cl = "\033[2J\033[H"; /* --]--]-- */
406 /* Clear to end of line */
407 ce = "\033[K"; /* --]-- */
410 so = "\033[7m"; /* --]-- */
411 se = "\033[m"; /* --]-- */
414 cs = "\033[%d;%dr"; /* --]-- */
417 cm = "\033[%d;%dH"; /* --]-- */
432 * Save the "normal" and "angband" terminal settings
437 static struct termios norm_termios;
439 static struct termios game_termios;
445 static struct termio norm_termio;
447 static struct termio game_termio;
453 static struct sgttyb norm_ttyb;
454 static struct tchars norm_tchars;
455 static struct ltchars norm_ltchars;
456 static int norm_local_chars;
458 static struct sgttyb game_ttyb;
459 static struct tchars game_tchars;
460 static struct ltchars game_ltchars;
461 static int game_local_chars;
468 * Are we active? Not really needed.
470 static int active = FALSE;
474 * The main screen (no sub-screens)
476 static term term_screen_body;
481 * Place the "keymap" into its "normal" state
483 static void keymap_norm(void)
488 /* restore the saved values of the special chars */
489 (void)tcsetattr(0, TCSAFLUSH, &norm_termios);
495 /* restore the saved values of the special chars */
496 (void)ioctl(0, TCSETA, (char *)&norm_termio);
502 /* restore the saved values of the special chars */
503 (void)ioctl(0, TIOCSETP, (char *)&norm_ttyb);
504 (void)ioctl(0, TIOCSETC, (char *)&norm_tchars);
505 (void)ioctl(0, TIOCSLTC, (char *)&norm_ltchars);
506 (void)ioctl(0, TIOCLSET, (char *)&norm_local_chars);
514 * Place the "keymap" into the "game" state
516 static void keymap_game(void)
521 /* restore the saved values of the special chars */
522 (void)tcsetattr(0, TCSAFLUSH, &game_termios);
528 /* restore the saved values of the special chars */
529 (void)ioctl(0, TCSETA, (char *)&game_termio);
535 /* restore the saved values of the special chars */
536 (void)ioctl(0, TIOCSETP, (char *)&game_ttyb);
537 (void)ioctl(0, TIOCSETC, (char *)&game_tchars);
538 (void)ioctl(0, TIOCSLTC, (char *)&game_ltchars);
539 (void)ioctl(0, TIOCLSET, (char *)&game_local_chars);
547 * Save the normal keymap
549 static void keymap_norm_prepare(void)
554 /* Get the normal keymap */
555 tcgetattr(0, &norm_termios);
561 /* Get the normal keymap */
562 (void)ioctl(0, TCGETA, (char *)&norm_termio);
568 /* Get the normal keymap */
569 (void)ioctl(0, TIOCGETP, (char *)&norm_ttyb);
570 (void)ioctl(0, TIOCGETC, (char *)&norm_tchars);
571 (void)ioctl(0, TIOCGLTC, (char *)&norm_ltchars);
572 (void)ioctl(0, TIOCLGET, (char *)&norm_local_chars);
580 * Save the keymaps (normal and game)
582 static void keymap_game_prepare(void)
587 /* Acquire the current mapping */
588 tcgetattr(0, &game_termios);
590 /* Force "Ctrl-C" to interupt */
591 game_termios.c_cc[VINTR] = (char)3;
593 /* Force "Ctrl-Z" to suspend */
594 game_termios.c_cc[VSUSP] = (char)26;
596 /* Hack -- Leave "VSTART/VSTOP" alone */
598 /* Disable the standard control characters */
599 game_termios.c_cc[VQUIT] = (char)-1;
600 game_termios.c_cc[VERASE] = (char)-1;
601 game_termios.c_cc[VKILL] = (char)-1;
602 game_termios.c_cc[VEOF] = (char)-1;
603 game_termios.c_cc[VEOL] = (char)-1;
605 /* Normally, block until a character is read */
606 game_termios.c_cc[VMIN] = 1;
607 game_termios.c_cc[VTIME] = 0;
609 /* Hack -- Turn off "echo" and "canonical" mode */
610 game_termios.c_lflag &= ~(ECHO | ICANON);
612 /* Turn off flow control */
613 game_termios.c_iflag &= ~IXON;
619 /* Acquire the current mapping */
620 (void)ioctl(0, TCGETA, (char *)&game_termio);
622 /* Force "Ctrl-C" to interupt */
623 game_termio.c_cc[VINTR] = (char)3;
625 /* Force "Ctrl-Z" to suspend */
626 game_termio.c_cc[VSUSP] = (char)26;
628 /* Hack -- Leave "VSTART/VSTOP" alone */
630 /* Disable the standard control characters */
631 game_termio.c_cc[VQUIT] = (char)-1;
632 game_termio.c_cc[VERASE] = (char)-1;
633 game_termio.c_cc[VKILL] = (char)-1;
634 game_termio.c_cc[VEOF] = (char)-1;
635 game_termio.c_cc[VEOL] = (char)-1;
637 /* Normally, block until a character is read */
638 game_termio.c_cc[VMIN] = 1;
639 game_termio.c_cc[VTIME] = 0;
641 /* Hack -- Turn off "echo" and "canonical" mode */
642 game_termio.c_lflag &= ~(ECHO | ICANON);
644 /* Turn off flow control */
645 game_termio.c_iflag &= ~IXON;
651 /* Get the default game characters */
652 (void)ioctl(0, TIOCGETP, (char *)&game_ttyb);
653 (void)ioctl(0, TIOCGETC, (char *)&game_tchars);
654 (void)ioctl(0, TIOCGLTC, (char *)&game_ltchars);
655 (void)ioctl(0, TIOCLGET, (char *)&game_local_chars);
657 /* Force interupt (^C) */
658 game_tchars.t_intrc = (char)3;
660 /* Force start/stop (^Q, ^S) */
661 game_tchars.t_startc = (char)17;
662 game_tchars.t_stopc = (char)19;
664 /* Cancel some things */
665 game_tchars.t_quitc = (char)-1;
666 game_tchars.t_eofc = (char)-1;
667 game_tchars.t_brkc = (char)-1;
669 /* Force suspend (^Z) */
670 game_ltchars.t_suspc = (char)26;
672 /* Cancel some things */
673 game_ltchars.t_dsuspc = (char)-1;
674 game_ltchars.t_rprntc = (char)-1;
675 game_ltchars.t_flushc = (char)-1;
676 game_ltchars.t_werasc = (char)-1;
677 game_ltchars.t_lnextc = (char)-1;
679 /* XXX XXX XXX XXX Verify this before use */
680 /* Hack -- Turn off "echo" and "canonical" mode */
681 /* game_termios.c_lflag &= ~(ECHO | ICANON); */
682 game_ttyb.flag &= ~(ECHO | ICANON);
684 /* XXX XXX XXX Should maybe turn off flow control too. How? */
700 static errr Term_xtra_cap_alive(int v)
705 if (!active) return (1);
707 /* Hack -- make sure the cursor is visible */
710 /* Move to bottom right */
711 do_move(0, rows - 1, 0, rows - 1);
713 /* Go to normal keymap mode */
716 /* No longer active */
723 if (active) return (1);
725 /* Hack -- restore the cursor location */
726 do_move(curx, cury, curx, cury);
728 /* Hack -- restore the cursor visibility */
731 /* Go to angband keymap mode */
734 /* Now we are active */
747 static errr Term_xtra_cap_event(int v)
755 /* Wait for one byte */
758 /* Hack -- Handle "errors" */
759 if ((i <= 0) && (errno != EINTR)) exit_game_panic(p_ptr);
765 /* Get the current flags for stdin */
766 if ((arg = fcntl(0, F_GETFL, 0)) < 1) return (1);
768 /* Tell stdin not to block */
769 if (fcntl(0, F_SETFL, arg | O_NDELAY) < 0) return (1);
771 /* Read one byte, if possible */
774 /* Replace the flags for stdin */
775 if (fcntl(0, F_SETFL, arg)) return (1);
779 if ((i != 1) || (!buf[0])) return (1);
781 /* Enqueue the keypress */
782 Term_keypress(buf[0]);
792 * Actually move the hardware cursor
794 static errr Term_curs_cap(int x, int y)
796 /* Literally move the cursor */
797 do_move(curx, cury, x, y);
799 /* Save the cursor location */
809 * Erase a grid of space
811 * XXX XXX XXX Note that we will never be asked to clear the
812 * bottom line all the way to the bottom right edge, since we
813 * have set the "avoid the bottom right corner" flag.
815 static errr Term_wipe_cap(int x, int y, int n)
819 /* Place the cursor */
822 /* Wipe to end of line */
831 for (dx = 0; dx < n; ++dx)
844 * Place some text on the screen using an attribute
846 static errr Term_text_cap(int x, int y, int n, byte a, concptr s)
850 /* Move the cursor */
853 /* Dump the text, advance the cursor */
854 for (i = 0; s[i]; i++)
859 /* Advance cursor 'X', and wrap */
862 /* Reset cursor 'X' */
865 /* Hack -- Advance cursor 'Y', and wrap */
866 if (++cury == rows) cury = 0;
876 * Handle a "special request"
878 static errr Term_xtra_cap(int n, int v)
880 /* Analyze the request */
883 /* Clear the screen */
884 case TERM_XTRA_CLEAR:
890 case TERM_XTRA_NOISE:
891 (void)write(1, "\007", 1);
894 /* Change the cursor visibility */
895 case TERM_XTRA_SHAPE:
901 case TERM_XTRA_ALIVE:
902 return (Term_xtra_cap_alive(v));
905 case TERM_XTRA_EVENT:
906 return (Term_xtra_cap_event(v));
909 case TERM_XTRA_FLUSH:
910 while (!Term_xtra_cap_event(FALSE));
914 case TERM_XTRA_DELAY:
927 * Init a "term" for this file
929 static void Term_init_cap(term *t)
933 /* Assume cursor at top left */
937 /* Assume visible cursor */
940 /* Clear the screen */
943 /* Hack -- visible cursor */
952 * Nuke a "term" for this file
954 static void Term_nuke_cap(term *t)
958 /* Hack -- make sure the cursor is visible */
961 /* Move to bottom right */
962 do_move(0, rows - 1, 0, rows - 1);
967 /* No longer active */
981 * Prepare this file for Angband usage
985 term *t = &term_screen_body;
990 /* Initialize the screen */
991 if (init_cap_aux()) return (-1);
993 /* Hack -- Require large screen, or Quit with message */
994 if ((rows < 24) || (cols < 80)) quit("Screen too small!");
997 /*** Prepare to play ***/
999 /* Extract the normal keymap */
1000 keymap_norm_prepare();
1002 /* Extract the game keymap */
1003 keymap_game_prepare();
1005 /* Hack -- activate the game keymap */
1008 /* Hack -- Do NOT buffer stdout */
1009 setbuf(stdout, NULL);
1012 /*** Now prepare the term ***/
1014 /* Initialize the term */
1015 term_init(t, 80, 24, 256);
1017 /* Avoid the bottom right corner */
1018 t->icky_corner = TRUE;
1020 /* Erase with "white space" */
1021 t->attr_blank = TERM_WHITE;
1022 t->char_blank = ' ';
1024 /* Set some hooks */
1025 t->init_hook = Term_init_cap;
1026 t->nuke_hook = Term_nuke_cap;
1028 /* Set some more hooks */
1029 t->text_hook = Term_text_cap;
1030 t->wipe_hook = Term_wipe_cap;
1031 t->curs_hook = Term_curs_cap;
1032 t->xtra_hook = Term_xtra_cap;
1038 Term_activate(term_screen);
1045 #endif /* USE_CAP */