Home | History | Annotate | Line # | Download | only in huntd
terminal.c revision 1.4
      1 /*	$NetBSD: terminal.c,v 1.4 2003/06/11 12:00:23 wiz Exp $	*/
      2 /*
      3  * Copyright (c) 1983-2003, Regents of the University of California.
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions are
      8  * met:
      9  *
     10  * + Redistributions of source code must retain the above copyright
     11  *   notice, this list of conditions and the following disclaimer.
     12  * + Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  * + Neither the name of the University of California, San Francisco nor
     16  *   the names of its contributors may be used to endorse or promote
     17  *   products derived from this software without specific prior written
     18  *   permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     23  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 #ifndef lint
     35 __RCSID("$NetBSD: terminal.c,v 1.4 2003/06/11 12:00:23 wiz Exp $");
     36 #endif /* not lint */
     37 
     38 #include <stdarg.h>
     39 #include "hunt.h"
     40 #define	TERM_WIDTH	80	/* Assume terminals are 80-char wide */
     41 
     42 /*
     43  * cgoto:
     44  *	Move the cursor to the given position on the given player's
     45  *	terminal.
     46  */
     47 void
     48 cgoto(pp, y, x)
     49 	PLAYER	*pp;
     50 	int	y, x;
     51 {
     52 	if (x == pp->p_curx && y == pp->p_cury)
     53 		return;
     54 	sendcom(pp, MOVE, y, x);
     55 	pp->p_cury = y;
     56 	pp->p_curx = x;
     57 }
     58 
     59 /*
     60  * outch:
     61  *	Put out a single character.
     62  */
     63 void
     64 outch(pp, ch)
     65 	PLAYER	*pp;
     66 	char	ch;
     67 {
     68 	if (++pp->p_curx >= TERM_WIDTH) {
     69 		pp->p_curx = 0;
     70 		pp->p_cury++;
     71 	}
     72 	(void) putc(ch, pp->p_output);
     73 }
     74 
     75 /*
     76  * outstr:
     77  *	Put out a string of the given length.
     78  */
     79 void
     80 outstr(pp, str, len)
     81 	PLAYER	*pp;
     82 	char	*str;
     83 	int	len;
     84 {
     85 	pp->p_curx += len;
     86 	pp->p_cury += (pp->p_curx / TERM_WIDTH);
     87 	pp->p_curx %= TERM_WIDTH;
     88 	while (len--)
     89 		(void) putc(*str++, pp->p_output);
     90 }
     91 
     92 /*
     93  * clrscr:
     94  *	Clear the screen, and reset the current position on the screen.
     95  */
     96 void
     97 clrscr(pp)
     98 	PLAYER	*pp;
     99 {
    100 	sendcom(pp, CLEAR);
    101 	pp->p_cury = 0;
    102 	pp->p_curx = 0;
    103 }
    104 
    105 /*
    106  * ce:
    107  *	Clear to the end of the line
    108  */
    109 void
    110 ce(pp)
    111 	PLAYER	*pp;
    112 {
    113 	sendcom(pp, CLRTOEOL);
    114 }
    115 
    116 #if 0		/* XXX lukem*/
    117 /*
    118  * ref;
    119  *	Refresh the screen
    120  */
    121 void
    122 ref(pp)
    123 	PLAYER	*pp;
    124 {
    125 	sendcom(pp, REFRESH);
    126 }
    127 #endif
    128 
    129 /*
    130  * sendcom:
    131  *	Send a command to the given user
    132  */
    133 void
    134 sendcom(PLAYER *pp, int command, ...)
    135 {
    136 	va_list	ap;
    137 	int	arg1, arg2;
    138 
    139 	va_start(ap, command);
    140 	(void) putc(command, pp->p_output);
    141 	switch (command & 0377) {
    142 	case MOVE:
    143 		arg1 = va_arg(ap, int);
    144 		arg2 = va_arg(ap, int);
    145 		(void) putc(arg1, pp->p_output);
    146 		(void) putc(arg2, pp->p_output);
    147 		break;
    148 	case ADDCH:
    149 	case READY:
    150 		arg1 = va_arg(ap, int);
    151 		(void) putc(arg1, pp->p_output);
    152 		break;
    153 	}
    154 
    155 	va_end(ap);		/* No return needed for void functions. */
    156 }
    157