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 $
24 #include <sys/types.h>
31 #ifdef NCURSES_VERSION
42 { BT_white, COLOR_WHITE },
43 { BT_blue, COLOR_BLUE },
44 { BT_magenta, COLOR_MAGENTA },
45 { BT_cyan, COLOR_CYAN },
46 { BT_yellow, COLOR_YELLOW },
47 { BT_green, COLOR_GREEN },
48 { BT_red, COLOR_RED },
53 static void PlotBlock1(int scr, int y, int x, BlockType type);
54 static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event);
56 static EventGenRec keyGen =
57 { NULL, 0, FT_read, STDIN_FILENO, KeyGenFunc, EM_key };
59 static int boardYPos[MAX_SCREENS], boardXPos[MAX_SCREENS];
60 static int statusYPos, statusXPos;
62 static int screens_dirty = 0;
64 static char *term_vi; /* String to make cursor invisible */
65 static char *term_ve; /* String to make cursor visible */
67 ExtFunc void InitScreens(void)
74 * Block signals while initializing curses. Otherwise a badly timed
75 * Ctrl-C during initialization might leave the terminal in a bad state.
77 BlockSignals(&oldMask, SIGINT, 0);
89 haveColor = colorEnable && has_colors();
95 for (i = 0; myColorTable[i].type != BT_none; ++i)
96 init_pair(myColorTable[i].type, COLOR_BLACK,
97 myColorTable[i].color);
103 AtExit(CleanupScreens);
105 RestoreSignals(NULL, &oldMask);
109 OutputTermStr(term_vi, 0);
110 AddEventGen(&keyGen);
114 addstr(version_string);
115 addstr(" (C) 1994-1996,1999 Mark H. Weaver "
116 "\"netris -h\" for more info");
121 ExtFunc void CleanupScreens(void)
124 RemoveEventGen(&keyGen);
126 OutputTermStr(term_ve, 1);
131 ExtFunc void GetTermcapInfo(void)
133 char *term, *buf, *data;
136 if (!(term = getenv("TERM")))
138 if (tgetent(scratch, term) == 1) {
140 * Make the buffer HUGE, since tgetstr is unsafe.
141 * Allocate it on the heap too.
143 data = buf = malloc(bufSize);
146 * There is no standard include file for tgetstr, no prototype
147 * definitions. I like casting better than using my own prototypes
148 * because if I guess the prototype, I might be wrong, especially
149 * with regards to "const".
151 term_vi = (char *)tgetstr("vi", &data);
152 term_ve = (char *)tgetstr("ve", &data);
154 /* Okay, so I'm paranoid; I just don't like unsafe routines */
155 if (data > buf + bufSize)
156 fatal("tgetstr overflow, you must have a very sick termcap");
158 /* Trim off the unused portion of buffer */
159 buf = realloc(buf, data - buf);
163 * If that fails, use hardcoded vt220 codes.
164 * They don't seem to do anything bad on vt100's, so
165 * we'll try them just in case they work.
167 if (!term_vi || !term_ve) {
168 static char *vts[] = {
169 "vt100", "vt101", "vt102",
170 "vt200", "vt220", "vt300",
171 "vt320", "vt400", "vt420",
172 "screen", "xterm", NULL };
175 for (i = 0; vts[i]; i++)
176 if (!strcmp(term, vts[i]))
178 term_vi = "\033[?25l";
179 term_ve = "\033[?25h";
183 if (!term_vi || !term_ve)
184 term_vi = term_ve = NULL;
187 ExtFunc void OutputTermStr(char *str, int flush)
196 ExtFunc void InitScreen(int scr)
203 boardXPos[scr] = boardXPos[scr - 1] +
204 2 * boardWidth[scr - 1] + 3;
206 if (statusXPos < boardXPos[scr] + 2 * boardWidth[scr] + 3)
207 statusXPos = boardXPos[scr] + 2 * boardWidth[scr] + 3;
208 for (y = boardVisible[scr] - 1; y >= 0; --y) {
209 move(boardYPos[scr] - y, boardXPos[scr] - 1);
211 for (x = boardWidth[scr] - 1; x >= 0; --x)
213 move(boardYPos[scr] - y, boardXPos[scr] + 2 * boardWidth[scr]);
216 for (y = boardVisible[scr]; y >= -1; y -= boardVisible[scr] + 1) {
217 move(boardYPos[scr] - y, boardXPos[scr] - 1);
219 for (x = boardWidth[scr] - 1; x >= 0; --x)
225 ExtFunc void CleanupScreen(int scr)
229 static void PlotBlock1(int scr, int y, int x, BlockType type)
231 int colorIndex = abs(type);
233 move(boardYPos[scr] - y, boardXPos[scr] + 2 * x);
243 attrset(COLOR_PAIR(colorIndex));
249 addstr(type > 0 ? "[]" : "$$");
254 ExtFunc void PlotBlock(int scr, int y, int x, BlockType type)
256 if (y >= 0 && y < boardVisible[scr] && x >= 0 && x < boardWidth[scr])
257 PlotBlock1(scr, y, x, type);
260 ExtFunc void PlotUnderline(int scr, int x, int flag)
262 move(boardYPos[scr] + 1, boardXPos[scr] + 2 * x);
263 addstr(flag ? "==" : "--");
266 ExtFunc void ShowDisplayInfo(void)
268 if (game == GT_classicTwo) {
269 move(statusYPos - 5, statusXPos);
270 printw("Enemy lines: %3d/%4d", enemyLinesCleared, enemyTotalLinesCleared);
272 move(statusYPos - 4, statusXPos);
273 printw("My lines: %3d/%4d", myLinesCleared, myTotalLinesCleared);
274 move(statusYPos - 3, statusXPos);
275 printw("Won: %3d", won);
276 move(statusYPos - 2, statusXPos);
277 printw("Lost: %3d", lost);
279 move(statusYPos - 1, statusXPos);
281 case STATE_WAIT_CONNECTION:
282 addstr("Waiting for opponent... ");
284 case STATE_WAIT_KEYPRESS:
285 addstr("Press the key for a new game.");
291 move(statusYPos - 9, statusXPos);
292 printw("Seed: %d", initSeed);
294 move(statusYPos - 8, statusXPos);
295 printw("Speed: %dms", speed / 1000);
298 move(statusYPos - 7, statusXPos);
300 addstr("Controlled by a fair robot");
302 addstr("Controlled by a robot");
305 if (opponentFlags & SCF_usingRobot) {
306 move(statusYPos - 6, statusXPos);
307 if (opponentFlags & SCF_fairRobot)
308 addstr("The opponent is a fair robot");
310 addstr("The opponent is a robot");
315 ExtFunc void UpdateOpponentDisplay(void)
318 printw("Playing %s@%s", opponentName, opponentHost);
322 ExtFunc void ShowPause(int pausedByMe, int pausedByThem)
324 move(statusYPos - 3, statusXPos);
326 addstr("Game paused by opponent");
329 move(statusYPos - 2, statusXPos);
331 addstr("Game paused by you");
336 ExtFunc void Message(char *s)
340 move(statusYPos - 20 + line, statusXPos);
341 addstr(s); /* XXX Should truncate long lines */
343 line = (line + 1) % 10;
344 move(statusYPos - 20 + line, statusXPos);
348 ExtFunc void RefreshScreen(void)
350 static char timeStr[2][32];
354 strftime(timeStr[0], 30, "%I:%M %p", localtime(&theTime));
355 /* Just in case the local curses library sucks */
356 if (strcmp(timeStr[0], timeStr[1]))
358 move(statusYPos, statusXPos);
360 strcpy(timeStr[1], timeStr[0]);
362 move(boardYPos[0] + 1, boardXPos[0] + 2 * boardWidth[0] + 1);
366 ExtFunc void ScheduleFullRedraw(void)
371 static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event)
373 if (MyRead(gen->fd, &event->u.key, 1))