2 * Netris -- A free networked version of T*tris
3 * Copyright (C) 1994-1996,1999 Mark H. Weaver <mhw@netris.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * $Id: curses.c,v 1.33 1999/05/16 06:56:25 mhw Exp $
23 #include <sys/types.h>
30 #ifdef NCURSES_VERSION
41 { BT_white, COLOR_WHITE },
42 { BT_blue, COLOR_BLUE },
43 { BT_magenta, COLOR_MAGENTA },
44 { BT_cyan, COLOR_CYAN },
45 { BT_yellow, COLOR_YELLOW },
46 { BT_green, COLOR_GREEN },
47 { BT_red, COLOR_RED },
52 static void PlotBlock1(int scr, int y, int x, BlockType type);
53 static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event);
55 static EventGenRec keyGen =
56 { NULL, 0, FT_read, STDIN_FILENO, KeyGenFunc, EM_key };
58 static int boardYPos[MAX_SCREENS], boardXPos[MAX_SCREENS];
59 static int statusYPos, statusXPos;
61 static int screens_dirty = 0;
63 static char *term_vi; /* String to make cursor invisible */
64 static char *term_ve; /* String to make cursor visible */
66 ExtFunc void InitScreens(void)
73 * Block signals while initializing curses. Otherwise a badly timed
74 * Ctrl-C during initialization might leave the terminal in a bad state.
76 BlockSignals(&oldMask, SIGINT, 0);
88 haveColor = colorEnable && has_colors();
94 for (i = 0; myColorTable[i].type != BT_none; ++i)
95 init_pair(myColorTable[i].type, COLOR_BLACK,
96 myColorTable[i].color);
102 AtExit(CleanupScreens);
104 RestoreSignals(NULL, &oldMask);
108 OutputTermStr(term_vi, 0);
109 AddEventGen(&keyGen);
113 addstr(version_string);
114 addstr(" (C) 1994-1996,1999 Mark H. Weaver "
115 "\"netris -h\" for more info");
120 ExtFunc void CleanupScreens(void)
123 RemoveEventGen(&keyGen);
125 OutputTermStr(term_ve, 1);
130 ExtFunc void GetTermcapInfo(void)
132 char *term, *buf, *data;
135 if (!(term = getenv("TERM")))
137 if (tgetent(scratch, term) == 1) {
139 * Make the buffer HUGE, since tgetstr is unsafe.
140 * Allocate it on the heap too.
142 data = buf = malloc(bufSize);
145 * There is no standard include file for tgetstr, no prototype
146 * definitions. I like casting better than using my own prototypes
147 * because if I guess the prototype, I might be wrong, especially
148 * with regards to "const".
150 term_vi = (char *)tgetstr("vi", &data);
151 term_ve = (char *)tgetstr("ve", &data);
153 /* Okay, so I'm paranoid; I just don't like unsafe routines */
154 if (data > buf + bufSize)
155 fatal("tgetstr overflow, you must have a very sick termcap");
157 /* Trim off the unused portion of buffer */
158 buf = realloc(buf, data - buf);
162 * If that fails, use hardcoded vt220 codes.
163 * They don't seem to do anything bad on vt100's, so
164 * we'll try them just in case they work.
166 if (!term_vi || !term_ve) {
167 static char *vts[] = {
168 "vt100", "vt101", "vt102",
169 "vt200", "vt220", "vt300",
170 "vt320", "vt400", "vt420",
171 "screen", "xterm", NULL };
174 for (i = 0; vts[i]; i++)
175 if (!strcmp(term, vts[i]))
177 term_vi = "\033[?25l";
178 term_ve = "\033[?25h";
182 if (!term_vi || !term_ve)
183 term_vi = term_ve = NULL;
186 ExtFunc void OutputTermStr(char *str, int flush)
195 ExtFunc void InitScreen(int scr)
202 boardXPos[scr] = boardXPos[scr - 1] +
203 2 * boardWidth[scr - 1] + 3;
205 if (statusXPos < boardXPos[scr] + 2 * boardWidth[scr] + 3)
206 statusXPos = boardXPos[scr] + 2 * boardWidth[scr] + 3;
207 for (y = boardVisible[scr] - 1; y >= 0; --y) {
208 move(boardYPos[scr] - y, boardXPos[scr] - 1);
210 for (x = boardWidth[scr] - 1; x >= 0; --x)
212 move(boardYPos[scr] - y, boardXPos[scr] + 2 * boardWidth[scr]);
215 for (y = boardVisible[scr]; y >= -1; y -= boardVisible[scr] + 1) {
216 move(boardYPos[scr] - y, boardXPos[scr] - 1);
218 for (x = boardWidth[scr] - 1; x >= 0; --x)
224 ExtFunc void CleanupScreen(int scr)
228 static void PlotBlock1(int scr, int y, int x, BlockType type)
230 int colorIndex = abs(type);
232 move(boardYPos[scr] - y, boardXPos[scr] + 2 * x);
242 attrset(COLOR_PAIR(colorIndex));
248 addstr(type > 0 ? "[]" : "$$");
253 ExtFunc void PlotBlock(int scr, int y, int x, BlockType type)
255 if (y >= 0 && y < boardVisible[scr] && x >= 0 && x < boardWidth[scr])
256 PlotBlock1(scr, y, x, type);
259 ExtFunc void PlotUnderline(int scr, int x, int flag)
261 move(boardYPos[scr] + 1, boardXPos[scr] + 2 * x);
262 addstr(flag ? "==" : "--");
265 ExtFunc void ShowDisplayInfo(void)
267 move(statusYPos - 3, statusXPos);
268 printw("Won: %3d", won);
269 move(statusYPos - 2, statusXPos);
270 printw("Lost: %3d", lost);
272 move(statusYPos - 1, statusXPos);
274 case STATE_WAIT_CONNECTION:
275 addstr("Waiting for opponent... ");
277 case STATE_WAIT_KEYPRESS:
278 addstr("Press the key for a new game.");
284 move(statusYPos - 9, statusXPos);
285 printw("Seed: %d", initSeed);
287 move(statusYPos - 8, statusXPos);
288 printw("Speed: %dms", speed / 1000);
291 move(statusYPos - 6, statusXPos);
293 addstr("Controlled by a fair robot");
295 addstr("Controlled by a robot");
298 if (opponentFlags & SCF_usingRobot) {
299 move(statusYPos - 5, statusXPos);
300 if (opponentFlags & SCF_fairRobot)
301 addstr("The opponent is a fair robot");
303 addstr("The opponent is a robot");
308 ExtFunc void UpdateOpponentDisplay(void)
311 printw("Playing %s@%s", opponentName, opponentHost);
315 ExtFunc void ShowPause(int pausedByMe, int pausedByThem)
317 move(statusYPos - 3, statusXPos);
319 addstr("Game paused by opponent");
322 move(statusYPos - 2, statusXPos);
324 addstr("Game paused by you");
329 ExtFunc void Message(char *s)
333 move(statusYPos - 20 + line, statusXPos);
334 addstr(s); /* XXX Should truncate long lines */
336 line = (line + 1) % 10;
337 move(statusYPos - 20 + line, statusXPos);
341 ExtFunc void RefreshScreen(void)
343 static char timeStr[2][32];
347 strftime(timeStr[0], 30, "%I:%M %p", localtime(&theTime));
348 /* Just in case the local curses library sucks */
349 if (strcmp(timeStr[0], timeStr[1]))
351 move(statusYPos, statusXPos);
353 strcpy(timeStr[1], timeStr[0]);
355 move(boardYPos[0] + 1, boardXPos[0] + 2 * boardWidth[0] + 1);
359 ExtFunc void ScheduleFullRedraw(void)
364 static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event)
366 if (MyRead(gen->fd, &event->u.key, 1))