Remove some unnecessary inclusions of <sys/cdefs.h> across the tree.
[dragonfly.git] / games / hunt / hunt / display.c
CommitLineData
6693db17 1/*-
82a5c12e
MD
2 * Display abstraction.
3 * David Leonard <d@openbsd.org>, 1999. Public domain.
4 *
5 * $OpenBSD: display.c,v 1.4 2002/02/19 19:39:36 millert Exp $
82a5c12e
MD
6 */
7
8#define USE_CURSES
9
82a5c12e
MD
10#include "display.h"
11
12#if !defined(USE_CURSES)
13
14#include <stdlib.h>
15#include <unistd.h>
16#include <signal.h>
17#include <stdio.h>
6693db17 18#include <termios.h>
82a5c12e
MD
19#define _USE_OLD_CURSES_
20#include <curses.h>
21#include <err.h>
22#include "hunt.h"
23
24static struct termios saved_tty;
25
26char screen[SCREEN_HEIGHT][SCREEN_WIDTH2];
27char blanks[SCREEN_WIDTH];
28int cur_row, cur_col;
29
30/*
31 * tstp:
32 * Handle stop and start signals
33 */
34static void
6beb426b 35tstp(int dummy)
82a5c12e
MD
36{
37 int y, x;
38
39 y = cur_row;
40 x = cur_col;
41 mvcur(cur_row, cur_col, HEIGHT, 0);
42 cur_row = HEIGHT;
43 cur_col = 0;
44 _puts(VE);
45 _puts(TE);
46 (void) fflush(stdout);
47 tcsetattr(0, TCSADRAIN, &__orig_termios);
48 (void) kill(getpid(), SIGSTOP);
49 (void) signal(SIGTSTP, tstp);
50 tcsetattr(0, TCSADRAIN, &saved_tty);
51 _puts(TI);
52 _puts(VS);
53 cur_row = y;
54 cur_col = x;
55 _puts(tgoto(CM, cur_row, cur_col));
56 display_redraw_screen();
57 (void) fflush(stdout);
58}
59
60/*
61 * display_open:
62 * open the display
63 */
64void
6beb426b 65display_open(void)
82a5c12e
MD
66{
67 char *term;
68
69 if (!isatty(0) || (term = getenv("TERM")) == NULL)
70 errx(1, "no terminal type");
71
72 gettmode();
73 (void) setterm(term);
6693db17 74 (void) noecho();
82a5c12e
MD
75 (void) cbreak();
76 tcgetattr(0, &saved_tty);
77 _puts(TI);
78 _puts(VS);
79#ifdef SIGTSTP
80 (void) signal(SIGTSTP, tstp);
81#endif
82}
83
84/*
85 * display_beep:
86 * beep
87 */
88void
6beb426b 89display_beep(void)
82a5c12e
MD
90{
91 (void) putchar('\a');
92}
93
94/*
95 * display_refresh:
96 * sync the display
97 */
98void
6beb426b 99display_refresh(void)
82a5c12e
MD
100{
101 (void) fflush(stdout);
102}
103
104/*
105 * display_clear_eol:
106 * clear to end of line, without moving cursor
107 */
108void
6beb426b 109display_clear_eol(void)
82a5c12e
MD
110{
111 if (CE != NULL)
112 tputs(CE, 1, __cputchar);
113 else {
114 fwrite(blanks, sizeof (char), SCREEN_WIDTH - cur_col, stdout);
115 if (COLS != SCREEN_WIDTH)
116 mvcur(cur_row, SCREEN_WIDTH, cur_row, cur_col);
117 else if (AM)
118 mvcur(cur_row + 1, 0, cur_row, cur_col);
119 else
120 mvcur(cur_row, SCREEN_WIDTH - 1, cur_row, cur_col);
121 }
122 memcpy(&screen[cur_row][cur_col], blanks, SCREEN_WIDTH - cur_col);
123}
124
125/*
126 * display_putchar:
127 * put one character on the screen, move the cursor right one,
128 * with wraparound
129 */
130void
6beb426b 131display_put_ch(char ch)
82a5c12e
MD
132{
133 if (!isprint(ch)) {
134 fprintf(stderr, "r,c,ch: %d,%d,%d", cur_row, cur_col, ch);
135 return;
136 }
137 screen[cur_row][cur_col] = ch;
138 putchar(ch);
139 if (++cur_col >= COLS) {
6693db17 140 if (!AM || XN)
82a5c12e
MD
141 putchar('\n');
142 cur_col = 0;
143 if (++cur_row >= LINES)
144 cur_row = LINES;
145 }
146}
147
148/*
149 * display_put_str:
150 * put a string of characters on the screen
151 */
152void
153display_put_str(const char *s)
154{
155 for( ; *s; s++)
156 display_put_ch(*s);
157}
158
159/*
160 * display_clear_the_screen:
161 * clear the screen; move cursor to top left
162 */
163void
6beb426b 164display_clear_the_screen(void)
82a5c12e
MD
165{
166 int i;
167
6693db17 168 if (blanks[0] == '\0')
82a5c12e
MD
169 for (i = 0; i < SCREEN_WIDTH; i++)
170 blanks[i] = ' ';
6693db17 171
82a5c12e
MD
172 if (CL != NULL) {
173 tputs(CL, LINES, __cputchar);
174 for (i = 0; i < SCREEN_HEIGHT; i++)
175 memcpy(screen[i], blanks, SCREEN_WIDTH);
176 } else {
177 for (i = 0; i < SCREEN_HEIGHT; i++) {
178 mvcur(cur_row, cur_col, i, 0);
179 cur_row = i;
180 cur_col = 0;
181 display_clear_eol();
182 }
183 mvcur(cur_row, cur_col, 0, 0);
184 }
185 cur_row = cur_col = 0;
186}
187
188/*
189 * display_move:
190 * move the cursor
191 */
192void
6beb426b 193display_move(int y, int x)
82a5c12e
MD
194{
195 mvcur(cur_row, cur_col, y, x);
196 cur_row = y;
197 cur_col = x;
198}
199
200/*
201 * display_getyx:
202 * locate the cursor
203 */
204void
6beb426b 205display_getyx(int *yp, int *xp)
82a5c12e
MD
206{
207 *xp = cur_col;
208 *yp = cur_row;
209}
210
211/*
212 * display_end:
213 * close the display
214 */
215void
6beb426b 216display_end(void)
82a5c12e
MD
217{
218 tcsetattr(0, TCSADRAIN, &__orig_termios);
219 _puts(VE);
220 _puts(TE);
221}
222
223/*
224 * display_atyx:
225 * return a character from the screen
226 */
227char
6beb426b 228display_atyx(int y, int x)
82a5c12e
MD
229{
230 return screen[y][x];
231}
232
233/*
234 * display_redraw_screen:
235 * redraw the screen
236 */
237void
6beb426b 238display_redraw_screen(void)
82a5c12e
MD
239{
240 int i;
241
242 mvcur(cur_row, cur_col, 0, 0);
243 for (i = 0; i < SCREEN_HEIGHT - 1; i++) {
244 fwrite(screen[i], sizeof (char), SCREEN_WIDTH, stdout);
245 if (COLS > SCREEN_WIDTH || (COLS == SCREEN_WIDTH && !AM))
246 putchar('\n');
247 }
248 fwrite(screen[SCREEN_HEIGHT - 1], sizeof (char), SCREEN_WIDTH - 1,
249 stdout);
250 mvcur(SCREEN_HEIGHT - 1, SCREEN_WIDTH - 1, cur_row, cur_col);
251}
252
253#else /* CURSES */ /* --------------------------------------------------- */
254
255#include <curses.h>
256#include "hunt.h"
257
258void
6beb426b 259display_open(void)
82a5c12e
MD
260{
261 initscr();
6693db17
SW
262 (void) noecho();
263 (void) cbreak();
82a5c12e
MD
264}
265
266void
6beb426b 267display_beep(void)
82a5c12e
MD
268{
269 beep();
270}
271
272void
6beb426b 273display_refresh(void)
82a5c12e
MD
274{
275 refresh();
276}
277
278void
6beb426b 279display_clear_eol(void)
82a5c12e
MD
280{
281 clrtoeol();
282}
283
284void
6beb426b 285display_put_ch(char c)
82a5c12e
MD
286{
287 addch(c);
288}
289
290void
291display_put_str(const char *s)
292{
293 addstr(s);
294}
295
296void
6beb426b 297display_clear_the_screen(void)
82a5c12e
MD
298{
299 clear();
300 move(0, 0);
301 display_refresh();
302}
303
304void
6beb426b 305display_move(int y, int x)
82a5c12e
MD
306{
307 move(y, x);
308}
309
310void
6beb426b 311display_getyx(int *yp, int *xp)
82a5c12e
MD
312{
313 getyx(stdscr, *yp, *xp);
314}
315
316void
6beb426b 317display_end(void)
82a5c12e
MD
318{
319 endwin();
320}
321
322char
6beb426b 323display_atyx(int y, int x)
82a5c12e
MD
324{
325 int oy, ox;
326 char c;
327
328 display_getyx(&oy, &ox);
329 c = mvwinch(stdscr, y, x) & 0x7f;
330 display_move(oy, ox);
331 return (c);
332}
333
334void
6beb426b 335display_redraw_screen(void)
82a5c12e
MD
336{
337 clearok(stdscr, TRUE);
338 touchwin(stdscr);
339}
340
341int
6beb426b 342display_iserasechar(char ch)
82a5c12e
MD
343{
344 return ch == erasechar();
345}
346
347int
6beb426b 348display_iskillchar(char ch)
82a5c12e
MD
349{
350 return ch == killchar();
351}
352
353#endif