1 /* ttt-curses-client.c - curses based tic-tac-toe game client
3 * Copyright © 2005 Bryan Worth
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
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 Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Author: Bryan Worth <bryan@theworths.org>
22 /* TODO: see /usr/share/doc/ncurses-devel-5.3/test/view.c for example code
23 * to handle window resizing
25 * add code to read username from config file or prompt user if config
26 * file does not exist. (username is currently hardcoded and needs
27 * changed before compiling)
31 #include "ttt-socket.h"
45 mvwprintstr (WINDOW *window,
52 wprint (WINDOW *window,
70 cline[xx] = string[xx] | attrs;
74 mvaddchstr (y, x, cline);
77 void mvwprintstr (WINDOW *window,
88 cline[xx] = string[xx] | attrs;
92 mvwaddchstr (window, y, x, cline);
95 void wprint (WINDOW *window,
105 if (string[xx] != '\r')
106 waddch (window, outc);
116 main (int argc, char **argv)
125 static WINDOW *mainwnd;
126 static WINDOW *dispwin;
127 static WINDOW *inpwin;
130 int dlines = LINES - 2, cols = COLS;
135 nodelay (mainwnd, TRUE);
138 dispwin = newwin (dlines - 1, cols - 2, 0, 0);
139 inpwin = newwin (1, cols - 2, dlines, 0);
140 keypad (mainwnd, TRUE); // enable keyboard mapping
141 keypad (inpwin, TRUE); // enable keyboard mapping
142 nodelay (inpwin, TRUE);
146 (void) scrollok (mainwnd, TRUE);
147 (void) scrollok (dispwin, TRUE);
148 (void) scrollok (inpwin, TRUE);
149 (void) idlok (mainwnd, TRUE);
150 (void) idlok (dispwin, TRUE);
151 (void) idlok (inpwin, TRUE);
152 wsetscrreg (dispwin, 0, dlines - 2);
154 (void) signal (SIGINT, finish); /* arrange interrupts to terminate */
158 init_pair (COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
159 init_pair (COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
160 init_pair (COLOR_RED, COLOR_RED, COLOR_BLACK);
161 init_pair (COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
162 init_pair (COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
163 init_pair (COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
164 init_pair (COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
165 init_pair (COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
167 wbkgd (dispwin, COLOR_PAIR (COLOR_WHITE));
168 wbkgd (inpwin, COLOR_PAIR (COLOR_CYAN));
172 xasprintf (&host, "localhost");
173 xasprintf (&port, "5334");
174 xasprintf (&message, "HELO bryan\r\n");
176 ttt_socket_create_client (host, port, &_socket);
177 ttt_socket_write (_socket, message, strlen (message));
180 numread = read (_socket, buffer, maxread);
182 buffer[numread] = '\0';
183 xasprintf (&message, "%s", buffer);
184 wprint (dispwin, message);
187 xasprintf (&message2, "MESSAGE \"");
192 xasprintf (&message2, "%s%c", message2, (int) c);
199 numread = read (_socket, buffer, maxread);
202 buffer[numread] = '\0';
203 wprint (dispwin, buffer);
209 xasprintf (&message2, "%s\"\r\n", message2);
210 ttt_socket_write (_socket, message2, strlen (message2));
211 xasprintf (&message2, "MESSAGE \"");
213 numread = read (_socket, buffer, maxread);
215 buffer[numread] = '\0';
216 wprint (dispwin, buffer);
232 /* do your non-curses wrapup here */
233 xasprintf (&message2, "QUIT\r\n");
234 ttt_socket_write (_socket, message2, strlen (message2));
237 numread = read (_socket, buffer, maxread);
238 buffer[numread] = '\0';
239 xasprintf (&message2, "%s", buffer);