-
Notifications
You must be signed in to change notification settings - Fork 10
/
curses.c
382 lines (335 loc) · 8.37 KB
/
curses.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* Netris -- A free networked version of T*tris
* Copyright (C) 1994-1996,1999 Mark H. Weaver <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id: curses.c,v 1.33 1999/05/16 06:56:25 mhw Exp $
*/
#include "netris.h"
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <term.h>
#include <curses.h>
#include <string.h>
#include <stdlib.h>
#ifdef NCURSES_VERSION
# define HAVE_NCURSES
#endif
#ifdef HAVE_NCURSES
static struct
{
BlockType type;
short color;
} myColorTable[] =
{
{ BT_white, COLOR_WHITE },
{ BT_blue, COLOR_BLUE },
{ BT_magenta, COLOR_MAGENTA },
{ BT_cyan, COLOR_CYAN },
{ BT_yellow, COLOR_YELLOW },
{ BT_green, COLOR_GREEN },
{ BT_red, COLOR_RED },
{ BT_none, 0 }
};
#endif
static void PlotBlock1(int scr, int y, int x, BlockType type);
static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event);
static EventGenRec keyGen =
{ NULL, 0, FT_read, STDIN_FILENO, KeyGenFunc, EM_key };
static int boardYPos[MAX_SCREENS], boardXPos[MAX_SCREENS];
static int statusYPos, statusXPos;
static int haveColor;
static int screens_dirty = 0;
static char *term_vi; /* String to make cursor invisible */
static char *term_ve; /* String to make cursor visible */
ExtFunc void InitScreens(void)
{
MySigSet oldMask;
GetTermcapInfo();
/*
* Block signals while initializing curses. Otherwise a badly timed
* Ctrl-C during initialization might leave the terminal in a bad state.
*/
BlockSignals(&oldMask, SIGINT, 0);
initscr();
#ifdef CURSES_HACK
{
extern char *CS;
CS = 0;
}
#endif
#ifdef HAVE_NCURSES
haveColor = colorEnable && has_colors();
if (haveColor)
{
int i = 0;
start_color();
for (i = 0; myColorTable[i].type != BT_none; ++i)
init_pair(myColorTable[i].type, COLOR_BLACK,
myColorTable[i].color);
}
#else
haveColor = 0;
#endif
AtExit(CleanupScreens);
screens_dirty = 1;
RestoreSignals(NULL, &oldMask);
cbreak();
noecho();
OutputTermStr(term_vi, 0);
AddEventGen(&keyGen);
move(0, 0);
addstr("Netris ");
addstr(version_string);
addstr(" (C) 1994-1996,1999 Mark H. Weaver "
"\"netris -h\" for more info");
statusYPos = 22;
statusXPos = 0;
}
ExtFunc void CleanupScreens(void)
{
if (screens_dirty) {
RemoveEventGen(&keyGen);
endwin();
OutputTermStr(term_ve, 1);
screens_dirty = 0;
}
}
ExtFunc void GetTermcapInfo(void)
{
char *term, *buf, *data;
int bufSize = 10240;
if (!(term = getenv("TERM")))
return;
if (tgetent(scratch, term) == 1) {
/*
* Make the buffer HUGE, since tgetstr is unsafe.
* Allocate it on the heap too.
*/
data = buf = malloc(bufSize);
/*
* There is no standard include file for tgetstr, no prototype
* definitions. I like casting better than using my own prototypes
* because if I guess the prototype, I might be wrong, especially
* with regards to "const".
*/
term_vi = (char *)tgetstr("vi", &data);
term_ve = (char *)tgetstr("ve", &data);
/* Okay, so I'm paranoid; I just don't like unsafe routines */
if (data > buf + bufSize)
fatal("tgetstr overflow, you must have a very sick termcap");
/* Trim off the unused portion of buffer */
buf = realloc(buf, data - buf);
}
/*
* If that fails, use hardcoded vt220 codes.
* They don't seem to do anything bad on vt100's, so
* we'll try them just in case they work.
*/
if (!term_vi || !term_ve) {
static char *vts[] = {
"vt100", "vt101", "vt102",
"vt200", "vt220", "vt300",
"vt320", "vt400", "vt420",
"screen", "xterm", NULL };
int i;
for (i = 0; vts[i]; i++)
if (!strcmp(term, vts[i]))
{
term_vi = "\033[?25l";
term_ve = "\033[?25h";
break;
}
}
if (!term_vi || !term_ve)
term_vi = term_ve = NULL;
}
ExtFunc void OutputTermStr(char *str, int flush)
{
if (str) {
fputs(str, stdout);
if (flush)
fflush(stdout);
}
}
ExtFunc void InitScreen(int scr)
{
int y, x;
if (scr == 0)
boardXPos[scr] = 1;
else
boardXPos[scr] = boardXPos[scr - 1] +
2 * boardWidth[scr - 1] + 3;
boardYPos[scr] = 22;
if (statusXPos < boardXPos[scr] + 2 * boardWidth[scr] + 3)
statusXPos = boardXPos[scr] + 2 * boardWidth[scr] + 3;
for (y = boardVisible[scr] - 1; y >= 0; --y) {
move(boardYPos[scr] - y, boardXPos[scr] - 1);
addch('|');
for (x = boardWidth[scr] - 1; x >= 0; --x)
addstr(" ");
move(boardYPos[scr] - y, boardXPos[scr] + 2 * boardWidth[scr]);
addch('|');
}
for (y = boardVisible[scr]; y >= -1; y -= boardVisible[scr] + 1) {
move(boardYPos[scr] - y, boardXPos[scr] - 1);
addch('+');
for (x = boardWidth[scr] - 1; x >= 0; --x)
addstr("--");
addch('+');
}
}
ExtFunc void CleanupScreen(int scr)
{
}
static void PlotBlock1(int scr, int y, int x, BlockType type)
{
int colorIndex = abs(type);
move(boardYPos[scr] - y, boardXPos[scr] + 2 * x);
if (type == BT_none)
addstr(" ");
else
{
if (standoutEnable)
{
#ifdef HAVE_NCURSES
if (haveColor)
attrset(COLOR_PAIR(colorIndex));
else
#endif
standout();
}
addstr(type > 0 ? "[]" : "$$");
standend();
}
}
ExtFunc void PlotBlock(int scr, int y, int x, BlockType type)
{
if (y >= 0 && y < boardVisible[scr] && x >= 0 && x < boardWidth[scr])
PlotBlock1(scr, y, x, type);
}
ExtFunc void PlotUnderline(int scr, int x, int flag)
{
move(boardYPos[scr] + 1, boardXPos[scr] + 2 * x);
addstr(flag ? "==" : "--");
}
ExtFunc void ShowDisplayInfo(void)
{
if (game == GT_classicTwo) {
move(statusYPos - 5, statusXPos);
printw("Enemy lines: %3d/%4d", enemyLinesCleared, enemyTotalLinesCleared);
}
move(statusYPos - 4, statusXPos);
printw("My lines: %3d/%4d", myLinesCleared, myTotalLinesCleared);
move(statusYPos - 3, statusXPos);
printw("Won: %3d", won);
move(statusYPos - 2, statusXPos);
printw("Lost: %3d", lost);
move(statusYPos - 1, statusXPos);
switch(gameState) {
case STATE_WAIT_CONNECTION:
addstr("Waiting for opponent... ");
break;
case STATE_WAIT_KEYPRESS:
addstr("Press the key for a new game.");
break;
default:
addstr(" ");
}
move(statusYPos - 9, statusXPos);
printw("Seed: %d", initSeed);
clrtoeol();
move(statusYPos - 8, statusXPos);
printw("Speed: %dms", speed / 1000);
clrtoeol();
if (robotEnable) {
move(statusYPos - 7, statusXPos);
if (fairRobot)
addstr("Controlled by a fair robot");
else
addstr("Controlled by a robot");
clrtoeol();
}
if (opponentFlags & SCF_usingRobot) {
move(statusYPos - 6, statusXPos);
if (opponentFlags & SCF_fairRobot)
addstr("The opponent is a fair robot");
else
addstr("The opponent is a robot");
clrtoeol();
}
}
ExtFunc void UpdateOpponentDisplay(void)
{
move(1, 0);
printw("Playing %s@%s", opponentName, opponentHost);
clrtoeol();
}
ExtFunc void ShowPause(int pausedByMe, int pausedByThem)
{
move(statusYPos - 3, statusXPos);
if (pausedByThem)
addstr("Game paused by opponent");
else
clrtoeol();
move(statusYPos - 2, statusXPos);
if (pausedByMe)
addstr("Game paused by you");
else
clrtoeol();
}
ExtFunc void Message(char *s)
{
static int line = 0;
move(statusYPos - 20 + line, statusXPos);
addstr(s); /* XXX Should truncate long lines */
clrtoeol();
line = (line + 1) % 10;
move(statusYPos - 20 + line, statusXPos);
clrtoeol();
}
ExtFunc void RefreshScreen(void)
{
static char timeStr[2][32];
time_t theTime;
time(&theTime);
strftime(timeStr[0], 30, "%I:%M %p", localtime(&theTime));
/* Just in case the local curses library sucks */
if (strcmp(timeStr[0], timeStr[1]))
{
move(statusYPos, statusXPos);
addstr(timeStr[0]);
strcpy(timeStr[1], timeStr[0]);
}
move(boardYPos[0] + 1, boardXPos[0] + 2 * boardWidth[0] + 1);
refresh();
}
ExtFunc void ScheduleFullRedraw(void)
{
touchwin(stdscr);
}
static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event)
{
if (MyRead(gen->fd, &event->u.key, 1))
return E_key;
else
return E_none;
}
/*
* vi: ts=4 ai
* vim: noai si
*/