1 /* ttt-client.c - client handling code for tic-tac-toe game server
3 * Copyright © 2005 Carl 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>
23 /* TODO: see /usr/share/doc/ncurses-devel-5.3/test/view.c for example code
24 * to handle window resizing
26 * add code to read username from config file or prompt user if config
27 * file does not exist. (username is currently hardcoded and needs
28 * changed before compiling)
33 #include "ttt-socket.h"
37 static void finish(int sig);
39 void mvprintstr(int, int, char *, chtype);
40 void mvwprintstr(WINDOW *, int, int, char *, chtype);
41 void wprint(WINDOW *, char *);
43 void mvprintstr(int y, int x, char *string, chtype attrs)
50 cline[xx] = string[xx] | attrs;
54 mvaddchstr(y, x, cline);
58 void mvwprintstr(WINDOW * window, int y, int x, char *string, chtype attrs)
65 cline[xx] = string[xx] | attrs;
69 mvwaddchstr(window, y, x, cline);
73 void wprint(WINDOW * window, char *string)
82 if (string[xx] != '\r')
94 int main(int argc, char **argv)
103 static WINDOW *mainwnd;
104 static WINDOW *dispwin;
105 static WINDOW *inpwin;
108 int dlines = LINES - 2, cols = COLS;
113 nodelay(mainwnd, TRUE);
116 dispwin = newwin(dlines - 1, cols - 2, 0, 0);
117 inpwin = newwin(1, cols - 2, dlines, 0);
118 keypad(mainwnd, TRUE); // enable keyboard mapping
119 keypad(inpwin, TRUE); // enable keyboard mapping
120 nodelay(inpwin, TRUE);
124 (void) scrollok(mainwnd, TRUE);
125 (void) scrollok(dispwin, TRUE);
126 (void) scrollok(inpwin, TRUE);
127 (void) idlok(mainwnd, TRUE);
128 (void) idlok(dispwin, TRUE);
129 (void) idlok(inpwin, TRUE);
130 wsetscrreg(dispwin, 0, dlines - 2);
132 (void) signal(SIGINT, finish); /* arrange interrupts to terminate */
136 init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
137 init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
138 init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
139 init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
140 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
141 init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
142 init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
143 init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
145 wbkgd(dispwin, COLOR_PAIR(COLOR_WHITE));
146 wbkgd(inpwin, COLOR_PAIR(COLOR_CYAN));
150 xasprintf(&host, "localhost");
151 xasprintf(&port, "5334");
152 xasprintf(&message, "HELO bryan\r\n");
154 ttt_socket_create_client(host, port, &_socket);
155 ttt_socket_write(_socket, message, strlen(message));
158 numread = read(_socket, buffer, maxread);
160 buffer[numread] = '\0';
161 xasprintf(&message, "%s", buffer);
162 wprint(dispwin, message);
165 xasprintf(&message2, "MESSAGE \"");
170 xasprintf(&message2, "%s%c", message2, (int) c);
177 numread = read(_socket, buffer, maxread);
180 buffer[numread] = '\0';
181 wprint(dispwin, buffer);
187 xasprintf(&message2, "%s\"\r\n", message2);
188 ttt_socket_write(_socket, message2, strlen(message2));
189 xasprintf(&message2, "MESSAGE \"");
191 numread = read(_socket, buffer, maxread);
193 buffer[numread] = '\0';
194 wprint(dispwin, buffer);
207 static void finish(int sig)
213 /* do your non-curses wrapup here */
214 xasprintf(&message2, "QUIT\r\n");
215 ttt_socket_write(_socket, message2, strlen(message2));
217 numread = read(_socket, buffer, maxread);
218 buffer[numread] = '\0';
219 xasprintf(&message2, "%s", buffer);