NetBSD/games/hack/hack.terminfo.c

315 lines
7.1 KiB
C
Raw Normal View History

2011-10-03 16:32:15 +04:00
/* $NetBSD: hack.terminfo.c,v 1.2 2011/10/03 12:32:28 roy Exp $ */
1997-10-19 20:56:41 +04:00
/*
* Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
* Amsterdam
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the Stichting Centrum voor Wiskunde en
* Informatica, nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
1997-10-19 20:56:41 +04:00
#include <sys/cdefs.h>
#ifndef lint
2011-10-03 16:32:15 +04:00
__RCSID("$NetBSD: hack.terminfo.c,v 1.2 2011/10/03 12:32:28 roy Exp $");
1997-10-19 20:56:41 +04:00
#endif /* not lint */
1993-03-21 12:45:37 +03:00
#include <string.h>
1995-04-29 05:08:51 +04:00
#include <termios.h>
#include <term.h>
1997-10-19 20:56:41 +04:00
#include <stdlib.h>
#include <unistd.h>
1997-10-19 20:56:41 +04:00
#include "hack.h"
#include "extern.h"
#include "def.flag.h" /* for flags.nonull */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
char *CD; /* tested in pri.c: docorner() */
int CO, LI; /* used in pri.c and whatis.c */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
void
startup(void)
1993-03-21 12:45:37 +03:00
{
/* Will exit if no suitable term found */
setupterm(NULL, 0, NULL);
CO = columns;
LI = lines;
1997-10-19 20:56:41 +04:00
if (CO < COLNO || LI < ROWNO + 2)
1993-03-21 12:45:37 +03:00
setclipped();
if (clear_screen == NULL)
error("Hack needs clear_screen.");
if (over_strike)
error("Hack can't have over_strike.");
if (cursor_address == NULL) {
printf("Playing hack without cursor_address is suspect...");
1993-03-21 12:45:37 +03:00
getret();
}
set_whole_screen();
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
void
startscreen(void)
1993-03-21 12:45:37 +03:00
{
}
1997-10-19 20:56:41 +04:00
void
endscreen(void)
1993-03-21 12:45:37 +03:00
{
}
static int
xputc(int c)
1993-03-21 12:45:37 +03:00
{
return (fputc(c, stdout));
}
1993-03-21 12:45:37 +03:00
static void
xputs(const char *s)
{
tputs(s, 1, xputc);
}
static void
cmov(int x, int y)
{
char *p;
2011-10-03 16:32:15 +04:00
p = tiparm(cursor_address, y - 1, x - 1);
if (p) {
xputs(p);
cury = y;
curx = x;
1993-03-21 12:45:37 +03:00
}
}
2009-08-12 11:28:40 +04:00
static void
nocmov(int x, int y)
1993-03-21 12:45:37 +03:00
{
if (cury > y) {
if (cursor_up) {
1993-03-21 12:45:37 +03:00
while (cury > y) { /* Go up. */
xputs(cursor_up);
1993-03-21 12:45:37 +03:00
cury--;
}
} else if (cursor_address) {
1993-03-21 12:45:37 +03:00
cmov(x, y);
} else if (cursor_home) {
1993-03-21 12:45:37 +03:00
home();
curs(x, y);
1997-10-19 20:56:41 +04:00
} /* else impossible("..."); */
1993-03-21 12:45:37 +03:00
} else if (cury < y) {
if (cursor_address) {
cmov(x, y);
#if 0
} else if (XD) {
1997-10-19 20:56:41 +04:00
while (cury < y) {
1993-03-21 12:45:37 +03:00
xputs(XD);
cury++;
}
#endif
1993-03-21 12:45:37 +03:00
} else {
1997-10-19 20:56:41 +04:00
while (cury < y) {
1993-03-21 12:45:37 +03:00
xputc('\n');
curx = 1;
cury++;
}
}
}
if (curx < x) { /* Go to the right. */
if (!cursor_right)
1997-10-19 20:56:41 +04:00
cmov(x, y);
else /* bah */
1993-03-21 12:45:37 +03:00
/* should instead print what is there already */
1997-10-19 20:56:41 +04:00
while (curx < x) {
xputs(cursor_right);
1997-10-19 20:56:41 +04:00
curx++;
}
1993-03-21 12:45:37 +03:00
} else if (curx > x) {
while (curx > x)
backsp();
1993-03-21 12:45:37 +03:00
}
}
/*
* Cursor movements
*
* x,y not xchar: perhaps xchar is unsigned and
* curx-x would be unsigned as well
*/
void
curs(int x, int y)
1993-03-21 12:45:37 +03:00
{
if (y == cury && x == curx)
return;
if (!cursor_right && (curx != x || x <= 3)) { /* Extremely primitive */
cmov(x, y); /* bunker!wtm */
return;
}
if (abs(cury - y) <= 3 && abs(curx - x) <= 3)
nocmov(x, y);
else if ((x <= 3 && abs(cury - y) <= 3) ||
(!cursor_address && x < abs(curx - x)))
{
(void) putchar('\r');
curx = 1;
nocmov(x, y);
} else if (!cursor_address) {
nocmov(x, y);
} else
cmov(x, y);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
void
2008-01-28 09:55:41 +03:00
cl_end(void)
1997-10-19 20:56:41 +04:00
{
if (clr_eol)
xputs(clr_eol);
1997-10-19 20:56:41 +04:00
else { /* no-CE fix - free after Harold Rynes */
/*
* this looks terrible, especially on a slow terminal but is
* better than nothing
*/
int cx = curx, cy = cury;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
while (curx < COLNO) {
1993-03-21 12:45:37 +03:00
xputc(' ');
curx++;
}
curs(cx, cy);
}
}
1997-10-19 20:56:41 +04:00
void
clearscreen(void)
1997-10-19 20:56:41 +04:00
{
xputs(clear_screen);
1993-03-21 12:45:37 +03:00
curx = cury = 1;
}
1997-10-19 20:56:41 +04:00
void
home(void)
1993-03-21 12:45:37 +03:00
{
char *out;
if (cursor_home)
xputs(cursor_home);
2011-10-03 16:32:15 +04:00
else if ((cursor_address) && (out = tiparm(cursor_address, 0, 0)))
xputs(out);
1993-03-21 12:45:37 +03:00
else
curs(1, 1); /* using UP ... */
curx = cury = 1;
}
1997-10-19 20:56:41 +04:00
void
standoutbeg(void)
1993-03-21 12:45:37 +03:00
{
if (enter_standout_mode && exit_standout_mode && !magic_cookie_glitch)
xputs(enter_standout_mode);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
void
standoutend(void)
1993-03-21 12:45:37 +03:00
{
if (exit_standout_mode && enter_standout_mode && !magic_cookie_glitch)
xputs(exit_standout_mode);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
void
backsp(void)
1993-03-21 12:45:37 +03:00
{
if (cursor_left)
xputs(cursor_left);
else
(void) putchar('\b');
1993-03-21 12:45:37 +03:00
curx--;
}
1997-10-19 20:56:41 +04:00
void
sound_bell(void)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
(void) putchar('\007'); /* curx does not change */
1993-03-21 12:45:37 +03:00
(void) fflush(stdout);
}
1997-10-19 20:56:41 +04:00
void
delay_output(void)
1997-10-19 20:56:41 +04:00
{
1993-03-21 12:45:37 +03:00
/* delay 50 ms - could also use a 'nap'-system call */
/* or the usleep call like this :-) */
usleep(50000);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
void
cl_eos(void)
1997-10-19 20:56:41 +04:00
{ /* free after Robert Viduya *//* must only be
* called with curx = 1 */
1993-03-21 12:45:37 +03:00
if (clr_eos)
xputs(clr_eos);
1993-03-21 12:45:37 +03:00
else {
1997-10-19 20:56:41 +04:00
int cx = curx, cy = cury;
while (cury <= LI - 2) {
1993-03-21 12:45:37 +03:00
cl_end();
xputc('\n');
curx = 1;
cury++;
}
cl_end();
curs(cx, cy);
}
}