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>
29 #ifdef NCURSES_VERSION
40 { BT_white, COLOR_WHITE },
41 { BT_blue, COLOR_BLUE },
42 { BT_magenta, COLOR_MAGENTA },
43 { BT_cyan, COLOR_CYAN },
44 { BT_yellow, COLOR_YELLOW },
45 { BT_green, COLOR_GREEN },
46 { BT_red, COLOR_RED },
51 static void PlotBlock1(int scr, int y, int x, BlockType type);
52 static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event);
54 static EventGenRec keyGen =
55 { NULL, 0, FT_read, STDIN_FILENO, KeyGenFunc, EM_key };
57 static int boardYPos[MAX_SCREENS], boardXPos[MAX_SCREENS];
58 static int statusYPos, statusXPos;
61 static char *term_vi; /* String to make cursor invisible */
62 static char *term_ve; /* String to make cursor visible */
64 ExtFunc void InitScreens(void)
71 * Block signals while initializing curses. Otherwise a badly timed
72 * Ctrl-C during initialization might leave the terminal in a bad state.
74 BlockSignals(&oldMask, SIGINT, 0);
86 haveColor = colorEnable && has_colors();
92 for (i = 0; myColorTable[i].type != BT_none; ++i)
93 init_pair(myColorTable[i].type, COLOR_BLACK,
94 myColorTable[i].color);
100 AtExit(CleanupScreens);
101 RestoreSignals(NULL, &oldMask);
105 OutputTermStr(term_vi, 0);
106 AddEventGen(&keyGen);
110 addstr(version_string);
111 addstr(" (C) 1994-1996,1999 Mark H. Weaver "
112 "\"netris -h\" for more info");
117 ExtFunc void CleanupScreens(void)
119 RemoveEventGen(&keyGen);
121 OutputTermStr(term_ve, 1);
124 ExtFunc void GetTermcapInfo(void)
126 char *term, *buf, *data;
129 if (!(term = getenv("TERM")))
131 if (tgetent(scratch, term) == 1) {
133 * Make the buffer HUGE, since tgetstr is unsafe.
134 * Allocate it on the heap too.
136 data = buf = malloc(bufSize);
139 * There is no standard include file for tgetstr, no prototype
140 * definitions. I like casting better than using my own prototypes
141 * because if I guess the prototype, I might be wrong, especially
142 * with regards to "const".
144 term_vi = (char *)tgetstr("vi", &data);
145 term_ve = (char *)tgetstr("ve", &data);
147 /* Okay, so I'm paranoid; I just don't like unsafe routines */
148 if (data > buf + bufSize)
149 fatal("tgetstr overflow, you must have a very sick termcap");
151 /* Trim off the unused portion of buffer */
152 buf = realloc(buf, data - buf);
156 * If that fails, use hardcoded vt220 codes.
157 * They don't seem to do anything bad on vt100's, so
158 * we'll try them just in case they work.
160 if (!term_vi || !term_ve) {
161 static char *vts[] = {
162 "vt100", "vt101", "vt102",
163 "vt200", "vt220", "vt300",
164 "vt320", "vt400", "vt420",
165 "screen", "xterm", NULL };
168 for (i = 0; vts[i]; i++)
169 if (!strcmp(term, vts[i]))
171 term_vi = "\033[?25l";
172 term_ve = "\033[?25h";
176 if (!term_vi || !term_ve)
177 term_vi = term_ve = NULL;
180 ExtFunc void OutputTermStr(char *str, int flush)
189 ExtFunc void InitScreen(int scr)
196 boardXPos[scr] = boardXPos[scr - 1] +
197 2 * boardWidth[scr - 1] + 3;
199 if (statusXPos < boardXPos[scr] + 2 * boardWidth[scr] + 3)
200 statusXPos = boardXPos[scr] + 2 * boardWidth[scr] + 3;
201 for (y = boardVisible[scr] - 1; y >= 0; --y) {
202 move(boardYPos[scr] - y, boardXPos[scr] - 1);
204 move(boardYPos[scr] - y, boardXPos[scr] + 2 * boardWidth[scr]);
207 for (y = boardVisible[scr]; y >= -1; y -= boardVisible[scr] + 1) {
208 move(boardYPos[scr] - y, boardXPos[scr] - 1);
210 for (x = boardWidth[scr] - 1; x >= 0; --x)
216 ExtFunc void CleanupScreen(int scr)
220 static void PlotBlock1(int scr, int y, int x, BlockType type)
222 int colorIndex = abs(type);
224 move(boardYPos[scr] - y, boardXPos[scr] + 2 * x);
234 attrset(COLOR_PAIR(colorIndex));
240 addstr(type > 0 ? "[]" : "$$");
245 ExtFunc void PlotBlock(int scr, int y, int x, BlockType type)
247 if (y >= 0 && y < boardVisible[scr] && x >= 0 && x < boardWidth[scr])
248 PlotBlock1(scr, y, x, type);
251 ExtFunc void PlotUnderline(int scr, int x, int flag)
253 move(boardYPos[scr] + 1, boardXPos[scr] + 2 * x);
254 addstr(flag ? "==" : "--");
257 ExtFunc void ShowDisplayInfo(void)
259 move(statusYPos - 9, statusXPos);
260 printw("Seed: %d", initSeed);
262 move(statusYPos - 8, statusXPos);
263 printw("Speed: %dms", speed / 1000);
266 move(statusYPos - 6, statusXPos);
268 addstr("Controlled by a fair robot");
270 addstr("Controlled by a robot");
273 if (opponentFlags & SCF_usingRobot) {
274 move(statusYPos - 5, statusXPos);
275 if (opponentFlags & SCF_fairRobot)
276 addstr("The opponent is a fair robot");
278 addstr("The opponent is a robot");
283 ExtFunc void UpdateOpponentDisplay(void)
286 printw("Playing %s@%s", opponentName, opponentHost);
290 ExtFunc void ShowPause(int pausedByMe, int pausedByThem)
292 move(statusYPos - 3, statusXPos);
294 addstr("Game paused by opponent");
297 move(statusYPos - 2, statusXPos);
299 addstr("Game paused by you");
304 ExtFunc void Message(char *s)
308 move(statusYPos - 20 + line, statusXPos);
309 addstr(s); /* XXX Should truncate long lines */
311 line = (line + 1) % 10;
312 move(statusYPos - 20 + line, statusXPos);
316 ExtFunc void RefreshScreen(void)
318 static char timeStr[2][32];
322 strftime(timeStr[0], 30, "%I:%M %p", localtime(&theTime));
323 /* Just in case the local curses library sucks */
324 if (strcmp(timeStr[0], timeStr[1]))
326 move(statusYPos, statusXPos);
328 strcpy(timeStr[1], timeStr[0]);
330 move(boardYPos[0] + 1, boardXPos[0] + 2 * boardWidth[0] + 1);
334 ExtFunc void ScheduleFullRedraw(void)
339 static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event)
341 if (MyRead(gen->fd, &event->u.key, 1))