1 /* File: main-cap.c */
3 /* Purpose: Support for "term.c" using "termcap" calls */
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.
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.
43 # if defined(_POSIX_VERSION)
57 # include <sys/ioctl.h>
62 * One version needs these files
65 # include <sys/ioctl.h>
70 * The other needs these files
73 # include <sys/ioctl.h>
74 # include <sys/resource.h>
75 # include <sys/param.h>
76 # include <sys/file.h>
77 # include <sys/types.h>
82 * XXX XXX Hack -- POSIX uses "O_NONBLOCK" instead of "O_NDELAY"
84 * They should both work due to the "(i != 1)" test in the code
85 * which checks for the result of the "read()" command.
88 # define O_NDELAY O_NONBLOCK
97 * Termcap string information
100 static char blob[1024]; /* The "termcap" entry */
101 static char area[1024]; /* The string extraction buffer */
102 static char *next = area; /* The current "index" into "area" */
103 static char *desc; /* The terminal name */
109 * Pointers into the "area"
112 static char *cm; /* Move cursor */
113 static char *ch; /* Move cursor to horizontal location */
114 static char *cv; /* Move cursor to vertical location */
115 static char *ho; /* Move cursor to top left */
116 static char *ll; /* Move cursor to bottom left */
117 static char *cs; /* Set scroll area */
118 static char *cl; /* Clear screen */
119 static char *cd; /* Clear to end of display */
120 static char *ce; /* Clear to end of line */
121 static char *cr; /* Move to start of line */
122 static char *so; /* Turn on standout */
123 static char *se; /* Turn off standout */
124 static char *md; /* Turn on bold */
125 static char *me; /* Turn off bold */
126 static char *vi; /* Cursor - invisible */
127 static char *ve; /* Cursor - normal */
128 static char *vs; /* Cursor - bright */
135 static int rows; /* Screen size (Y) */
136 static int cols; /* Screen size (X) */
137 static int curx; /* Cursor location (X) */
138 static int cury; /* Cursor location (Y) */
139 static int curv; /* Cursor visibility */
145 extern char *getenv();
146 extern char *tgoto();
147 extern char *tgetstr();
151 * Write some chars to the terminal
153 static void ewrite(char *str)
155 int numtowrite, numwritten;
157 /* See how much work we have */
158 numtowrite = strlen(str);
160 /* Write until done */
161 while (numtowrite > 0)
163 /* Try to write the chars */
164 numwritten = write(1, str, numtowrite);
166 /* Handle FIFOs and EINTR */
167 if (numwritten < 0) numwritten = 0;
169 /* See what we completed */
170 numtowrite -= numwritten;
173 /* Hack -- sleep if not done */
174 if (numtowrite > 0) sleep(1);
182 static char write_buffer[128];
183 static char *write_buffer_ptr;
185 static void output_one(char c)
187 *write_buffer_ptr++ = c;
190 static void tp(char *s)
192 /* Dump the string into us */
193 write_buffer_ptr = write_buffer;
195 /* Write the string with padding */
196 tputs (s, 1, output_one);
198 /* Finish the string */
199 *write_buffer_ptr = '\0';
201 /* Dump the recorded buffer */
202 ewrite (write_buffer);
209 static void tp(char *s)
225 static void do_cl(void)
231 * Clear to the end of the line
233 static void do_ce(void)
240 * Set the cursor visibility (0 = invis, 1 = normal, 2 = bright)
242 static void curs_set(int vis)
265 * Restrict scrolling to within these rows
267 static void do_cs(int y1, int y2)
271 if (cs) tp(tgoto(cs, y2, y1));
276 sprintf(temp, cs, y1, y2);
285 * Go to the given screen location directly
287 static void do_cm(int x, int y)
291 if (cm) tp(tgoto(cm, x, y));
296 sprintf(temp, cm, y+1, x+1);
304 * Go to the given screen location in a "clever" manner
306 * XXX XXX XXX This function could use some work!
308 static void do_move(int x1, int y1, int x2, int y2)
310 /* Hack -- unknown start location */
311 if ((x1 == x2) && (y1 == y2)) do_cm(x2, y2);
316 if ((y2 <= 0) && ho) tp(ho);
317 else if ((y2 >= rows-1) && ll) tp(ll);
318 else if ((y2 == y1) && cr) tp(cr);
322 /* Default -- go directly there */
330 * Help initialize this file (see below)
332 errr init_cap_aux(void)
337 /* Get the terminal name (if possible) */
338 desc = getenv("TERM");
339 if (!desc) return (1);
341 /* Get the terminal info */
342 if (tgetent(blob, desc) != 1) return (2);
344 /* Get the (initial) columns and rows, or default */
345 if ((cols = tgetnum("co")) == -1) cols = 80;
346 if ((rows = tgetnum("li")) == -1) rows = 24;
348 /* Find out how to move the cursor to a given location */
349 cm = tgetstr("cm", &next);
350 if (!cm) return (10);
352 /* Find out how to move the cursor to a given position */
353 ch = tgetstr("ch", &next);
354 cv = tgetstr("cv", &next);
356 /* Find out how to "home" the screen */
357 ho = tgetstr("ho", &next);
359 /* Find out how to "last-line" the screen */
360 ll = tgetstr("ll", &next);
362 /* Find out how to do a "carriage return" */
363 cr = tgetstr("cr", &next);
366 /* Find out how to clear the screen */
367 cl = tgetstr("cl", &next);
368 if (!cl) return (11);
370 /* Find out how to clear to the end of display */
371 cd = tgetstr("cd", &next);
373 /* Find out how to clear to the end of the line */
374 ce = tgetstr("ce", &next);
376 /* Find out how to scroll (set the scroll region) */
377 cs = tgetstr("cs", &next);
379 /* Find out how to hilite */
380 so = tgetstr("so", &next);
381 se = tgetstr("se", &next);
382 if (!so || !se) so = se = NULL;
384 /* Find out how to bold */
385 md = tgetstr("md", &next);
386 me = tgetstr("me", &next);
387 if (!md || !me) md = me = NULL;
389 /* Check the cursor visibility stuff */
390 vi = tgetstr("vi", &next);
391 vs = tgetstr("vs", &next);
392 ve = tgetstr("ve", &next);
398 /* Assume some defualt information */
403 cl = "\033[2J\033[H"; /* --]--]-- */
405 /* Clear to end of line */
406 ce = "\033[K"; /* --]-- */
409 so = "\033[7m"; /* --]-- */
410 se = "\033[m"; /* --]-- */
413 cs = "\033[%d;%dr"; /* --]-- */
416 cm = "\033[%d;%dH"; /* --]-- */
431 * Save the "normal" and "angband" terminal settings
436 static struct termios norm_termios;
438 static struct termios game_termios;
444 static struct termio norm_termio;
446 static struct termio game_termio;
452 static struct sgttyb norm_ttyb;
453 static struct tchars norm_tchars;
454 static struct ltchars norm_ltchars;
455 static int norm_local_chars;
457 static struct sgttyb game_ttyb;
458 static struct tchars game_tchars;
459 static struct ltchars game_ltchars;
460 static int game_local_chars;
467 * Are we active? Not really needed.
469 static int active = FALSE;
473 * The main screen (no sub-screens)
475 static term term_screen_body;
480 * Place the "keymap" into its "normal" state
482 static void keymap_norm(void)
487 /* restore the saved values of the special chars */
488 (void)tcsetattr(0, TCSAFLUSH, &norm_termios);
494 /* restore the saved values of the special chars */
495 (void)ioctl(0, TCSETA, (char *)&norm_termio);
501 /* restore the saved values of the special chars */
502 (void)ioctl(0, TIOCSETP, (char *)&norm_ttyb);
503 (void)ioctl(0, TIOCSETC, (char *)&norm_tchars);
504 (void)ioctl(0, TIOCSLTC, (char *)&norm_ltchars);
505 (void)ioctl(0, TIOCLSET, (char *)&norm_local_chars);
513 * Place the "keymap" into the "game" state
515 static void keymap_game(void)
520 /* restore the saved values of the special chars */
521 (void)tcsetattr(0, TCSAFLUSH, &game_termios);
527 /* restore the saved values of the special chars */
528 (void)ioctl(0, TCSETA, (char *)&game_termio);
534 /* restore the saved values of the special chars */
535 (void)ioctl(0, TIOCSETP, (char *)&game_ttyb);
536 (void)ioctl(0, TIOCSETC, (char *)&game_tchars);
537 (void)ioctl(0, TIOCSLTC, (char *)&game_ltchars);
538 (void)ioctl(0, TIOCLSET, (char *)&game_local_chars);
546 * Save the normal keymap
548 static void keymap_norm_prepare(void)
553 /* Get the normal keymap */
554 tcgetattr(0, &norm_termios);
560 /* Get the normal keymap */
561 (void)ioctl(0, TCGETA, (char *)&norm_termio);
567 /* Get the normal keymap */
568 (void)ioctl(0, TIOCGETP, (char *)&norm_ttyb);
569 (void)ioctl(0, TIOCGETC, (char *)&norm_tchars);
570 (void)ioctl(0, TIOCGLTC, (char *)&norm_ltchars);
571 (void)ioctl(0, TIOCLGET, (char *)&norm_local_chars);
579 * Save the keymaps (normal and game)
581 static void keymap_game_prepare(void)
586 /* Acquire the current mapping */
587 tcgetattr(0, &game_termios);
589 /* Force "Ctrl-C" to interupt */
590 game_termios.c_cc[VINTR] = (char)3;
592 /* Force "Ctrl-Z" to suspend */
593 game_termios.c_cc[VSUSP] = (char)26;
595 /* Hack -- Leave "VSTART/VSTOP" alone */
597 /* Disable the standard control characters */
598 game_termios.c_cc[VQUIT] = (char)-1;
599 game_termios.c_cc[VERASE] = (char)-1;
600 game_termios.c_cc[VKILL] = (char)-1;
601 game_termios.c_cc[VEOF] = (char)-1;
602 game_termios.c_cc[VEOL] = (char)-1;
604 /* Normally, block until a character is read */
605 game_termios.c_cc[VMIN] = 1;
606 game_termios.c_cc[VTIME] = 0;
608 /* Hack -- Turn off "echo" and "canonical" mode */
609 game_termios.c_lflag &= ~(ECHO | ICANON);
611 /* Turn off flow control */
612 game_termios.c_iflag &= ~IXON;
618 /* Acquire the current mapping */
619 (void)ioctl(0, TCGETA, (char *)&game_termio);
621 /* Force "Ctrl-C" to interupt */
622 game_termio.c_cc[VINTR] = (char)3;
624 /* Force "Ctrl-Z" to suspend */
625 game_termio.c_cc[VSUSP] = (char)26;
627 /* Hack -- Leave "VSTART/VSTOP" alone */
629 /* Disable the standard control characters */
630 game_termio.c_cc[VQUIT] = (char)-1;
631 game_termio.c_cc[VERASE] = (char)-1;
632 game_termio.c_cc[VKILL] = (char)-1;
633 game_termio.c_cc[VEOF] = (char)-1;
634 game_termio.c_cc[VEOL] = (char)-1;
636 /* Normally, block until a character is read */
637 game_termio.c_cc[VMIN] = 1;
638 game_termio.c_cc[VTIME] = 0;
640 /* Hack -- Turn off "echo" and "canonical" mode */
641 game_termio.c_lflag &= ~(ECHO | ICANON);
643 /* Turn off flow control */
644 game_termio.c_iflag &= ~IXON;
650 /* Get the default game characters */
651 (void)ioctl(0, TIOCGETP, (char *)&game_ttyb);
652 (void)ioctl(0, TIOCGETC, (char *)&game_tchars);
653 (void)ioctl(0, TIOCGLTC, (char *)&game_ltchars);
654 (void)ioctl(0, TIOCLGET, (char *)&game_local_chars);
656 /* Force interupt (^C) */
657 game_tchars.t_intrc = (char)3;
659 /* Force start/stop (^Q, ^S) */
660 game_tchars.t_startc = (char)17;
661 game_tchars.t_stopc = (char)19;
663 /* Cancel some things */
664 game_tchars.t_quitc = (char)-1;
665 game_tchars.t_eofc = (char)-1;
666 game_tchars.t_brkc = (char)-1;
668 /* Force suspend (^Z) */
669 game_ltchars.t_suspc = (char)26;
671 /* Cancel some things */
672 game_ltchars.t_dsuspc = (char)-1;
673 game_ltchars.t_rprntc = (char)-1;
674 game_ltchars.t_flushc = (char)-1;
675 game_ltchars.t_werasc = (char)-1;
676 game_ltchars.t_lnextc = (char)-1;
678 /* XXX XXX XXX XXX Verify this before use */
679 /* Hack -- Turn off "echo" and "canonical" mode */
680 /* game_termios.c_lflag &= ~(ECHO | ICANON); */
681 game_ttyb.flag &= ~(ECHO | ICANON);
683 /* XXX XXX XXX Should maybe turn off flow control too. How? */
699 static errr Term_xtra_cap_alive(int v)
704 if (!active) return (1);
706 /* Hack -- make sure the cursor is visible */
709 /* Move to bottom right */
710 do_move(0, rows - 1, 0, rows - 1);
712 /* Go to normal keymap mode */
715 /* No longer active */
722 if (active) return (1);
724 /* Hack -- restore the cursor location */
725 do_move(curx, cury, curx, cury);
727 /* Hack -- restore the cursor visibility */
730 /* Go to angband keymap mode */
733 /* Now we are active */
746 static errr Term_xtra_cap_event(int v)
754 /* Wait for one byte */
757 /* Hack -- Handle "errors" */
758 if ((i <= 0) && (errno != EINTR)) exit_game_panic(p_ptr);
764 /* Get the current flags for stdin */
765 if ((arg = fcntl(0, F_GETFL, 0)) < 1) return (1);
767 /* Tell stdin not to block */
768 if (fcntl(0, F_SETFL, arg | O_NDELAY) < 0) return (1);
770 /* Read one byte, if possible */
773 /* Replace the flags for stdin */
774 if (fcntl(0, F_SETFL, arg)) return (1);
778 if ((i != 1) || (!buf[0])) return (1);
780 /* Enqueue the keypress */
781 Term_keypress(buf[0]);
791 * Actually move the hardware cursor
793 static errr Term_curs_cap(int x, int y)
795 /* Literally move the cursor */
796 do_move(curx, cury, x, y);
798 /* Save the cursor location */
808 * Erase a grid of space
810 * XXX XXX XXX Note that we will never be asked to clear the
811 * bottom line all the way to the bottom right edge, since we
812 * have set the "avoid the bottom right corner" flag.
814 static errr Term_wipe_cap(int x, int y, int n)
818 /* Place the cursor */
821 /* Wipe to end of line */
830 for (dx = 0; dx < n; ++dx)
843 * Place some text on the screen using an attribute
845 static errr Term_text_cap(int x, int y, int n, byte a, concptr s)
849 /* Move the cursor */
852 /* Dump the text, advance the cursor */
853 for (i = 0; s[i]; i++)
858 /* Advance cursor 'X', and wrap */
861 /* Reset cursor 'X' */
864 /* Hack -- Advance cursor 'Y', and wrap */
865 if (++cury == rows) cury = 0;
875 * Handle a "special request"
877 static errr Term_xtra_cap(int n, int v)
879 /* Analyze the request */
882 /* Clear the screen */
883 case TERM_XTRA_CLEAR:
889 case TERM_XTRA_NOISE:
890 (void)write(1, "\007", 1);
893 /* Change the cursor visibility */
894 case TERM_XTRA_SHAPE:
900 case TERM_XTRA_ALIVE:
901 return (Term_xtra_cap_alive(v));
904 case TERM_XTRA_EVENT:
905 return (Term_xtra_cap_event(v));
908 case TERM_XTRA_FLUSH:
909 while (!Term_xtra_cap_event(FALSE));
913 case TERM_XTRA_DELAY:
926 * Init a "term" for this file
928 static void Term_init_cap(term *t)
932 /* Assume cursor at top left */
936 /* Assume visible cursor */
939 /* Clear the screen */
942 /* Hack -- visible cursor */
951 * Nuke a "term" for this file
953 static void Term_nuke_cap(term *t)
957 /* Hack -- make sure the cursor is visible */
960 /* Move to bottom right */
961 do_move(0, rows - 1, 0, rows - 1);
966 /* No longer active */
980 * Prepare this file for Angband usage
984 term *t = &term_screen_body;
989 /* Initialize the screen */
990 if (init_cap_aux()) return (-1);
992 /* Hack -- Require large screen, or Quit with message */
993 if ((rows < 24) || (cols < 80)) quit("Screen too small!");
996 /*** Prepare to play ***/
998 /* Extract the normal keymap */
999 keymap_norm_prepare();
1001 /* Extract the game keymap */
1002 keymap_game_prepare();
1004 /* Hack -- activate the game keymap */
1007 /* Hack -- Do NOT buffer stdout */
1008 setbuf(stdout, NULL);
1011 /*** Now prepare the term ***/
1013 /* Initialize the term */
1014 term_init(t, 80, 24, 256);
1016 /* Avoid the bottom right corner */
1017 t->icky_corner = TRUE;
1019 /* Erase with "white space" */
1020 t->attr_blank = TERM_WHITE;
1021 t->char_blank = ' ';
1023 /* Set some hooks */
1024 t->init_hook = Term_init_cap;
1025 t->nuke_hook = Term_nuke_cap;
1027 /* Set some more hooks */
1028 t->text_hook = Term_text_cap;
1029 t->wipe_hook = Term_wipe_cap;
1030 t->curs_hook = Term_curs_cap;
1031 t->xtra_hook = Term_xtra_cap;
1037 Term_activate(term_screen);
1044 #endif /* USE_CAP */