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
27 #include "ttt-socket.h"
41 mvwprintstr (WINDOW *window,
48 wprint (WINDOW *window,
66 cline[xx] = string[xx] | attrs;
70 mvaddchstr (y, x, cline);
73 void mvwprintstr (WINDOW *window,
84 cline[xx] = string[xx] | attrs;
88 mvwaddchstr (window, y, x, cline);
91 void wprint (WINDOW *window,
101 if (string[xx] != '\r')
102 waddch (window, outc);
107 FILE *sockin, *sockout;
110 main (int argc, char **argv)
112 char *host="localhost";
119 char *command_string;
125 static WINDOW *mainwnd;
126 static WINDOW *dispwin;
127 static WINDOW *statwin;
128 static WINDOW *inpwin;
132 int dlines = LINES - 2, cols = COLS;
138 nodelay (mainwnd, TRUE);
141 dispwin = newwin (dlines - 5, cols - 2, 0, 0);
142 statwin = newwin (4, cols - 2, dlines -3, 0);
143 inpwin = newwin (1, cols - 2, dlines, 0);
144 keypad (mainwnd, TRUE); // enable keyboard mapping
145 keypad (inpwin, TRUE); // enable keyboard mapping
146 nodelay (inpwin, TRUE);
151 (void) scrollok (mainwnd, TRUE);
152 (void) scrollok (dispwin, TRUE);
153 (void) scrollok (statwin, TRUE);
154 (void) scrollok (inpwin, TRUE);
155 (void) idlok (mainwnd, TRUE);
156 (void) idlok (dispwin, TRUE);
157 (void) idlok (statwin, TRUE);
158 (void) idlok (inpwin, TRUE);
159 wsetscrreg (dispwin, 0, dlines - 2);
160 wsetscrreg (statwin, 0, 4);
162 (void) signal (SIGINT, finish); /* arrange interrupts to terminate */
166 init_pair (COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
167 init_pair (COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
168 init_pair (COLOR_RED, COLOR_RED, COLOR_BLACK);
169 init_pair (COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
170 init_pair (COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
171 init_pair (COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
172 init_pair (COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
173 init_pair (COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
175 //wbkgd (dispwin, COLOR_PAIR (COLOR_WHITE));
176 //wbkgd (inpwin, COLOR_PAIR (COLOR_CYAN));
181 ttt_socket_create_client (host, port, &_socket);
182 sockin=fdopen(_socket,"r");
183 sockout=fdopen(_socket,"w");
186 xasprintf(&confpath,"%s/.ttt/",getenv("HOME"));
187 xasprintf(&conffile,"%s/.ttt/client.conf",getenv("HOME"));
189 if (access (conffile, F_OK) != 0 ) {
190 envuser=getenv("USER");
191 if (envuser != NULL) username=strdup(envuser);
192 if (access (confpath, F_OK) != 0 ) {
193 xasprintf(&command_string,"mkdir %s",confpath);
194 system (command_string);
196 if ((conf_file = fopen(conffile,"w")) != NULL) {
197 fprintf(conf_file,"username=%s",username);
201 if ((conf_file = fopen(conffile,"r")) != NULL) {
202 while (fgets(buffer,BUFSIZ,conf_file)) {
203 if (strncmp(buffer,"username=",9) == 0) {
204 xasprintf (&username, "%s", &buffer[9]);
211 fprintf(sockout, "HELO %s\r\n",username);
214 if (fgets(buffer,BUFSIZ,sockin)) {
215 if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
216 wprint (dispwin, buffer);
219 if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
228 if ((c != ERR) && (strlen(inplin)< 1000)) {
235 inplin[strlen(inplin)-1]='\0';
236 wmove (inpwin,0,strlen(inplin));
241 inplin[strlen(inplin)-1]='\0';
242 wmove (inpwin,0,strlen(inplin));
247 inplin[strlen(inplin)-1]='\0';
248 wmove (inpwin,0,strlen(inplin));
253 sprintf (inplin,"%s%c",inplin,(int) c);
259 if (fgets(buffer,BUFSIZ,sockin)) {
261 if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
262 wprint (dispwin, &buffer[15]);
265 if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
271 if (((int) c == 13) || ((int) c == 10)) {
273 fprintf(sockout,"MESSAGE \"%s\"\r\n",inplin);
276 if (fgets(buffer,BUFSIZ,sockin)) {
277 if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
278 wprint (dispwin, buffer);
281 if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
299 /* do your non-curses wrapup here */
300 fprintf(sockout,"QUIT\r\n");
303 while (fgets(buffer,BUFSIZ,sockin));