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