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 (2, 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));
180 xasprintf(&confpath,"%s/.ttt/",getenv("HOME"));
181 xasprintf(&conffile,"%s/.ttt/client.conf",getenv("HOME"));
183 if (access (conffile, F_OK) != 0 ) {
184 envuser=getenv("USER");
185 if (envuser != NULL) username=strdup(envuser);
186 if (access (confpath, F_OK) != 0 ) {
187 xasprintf(&command_string,"mkdir %s",confpath);
188 system (command_string);
190 if ((conf_file = fopen(conffile,"w")) != NULL) {
191 fprintf(conf_file,"username=%s",username);
195 if ((conf_file = fopen(conffile,"r")) != NULL) {
196 while (fgets(buffer,BUFSIZ,conf_file)) {
197 if (buffer[strlen(buffer)-1] == 10) buffer[strlen(buffer)-1] = '\0';
198 if (strncmp(buffer,"username=",9) == 0) {
199 xasprintf (&username, "%s", &buffer[9]);
201 if (strncmp(buffer,"port=",5) == 0) {
202 xasprintf (&port, "%s",&buffer[5]);
207 status=ttt_socket_create_client (host, port, &_socket);
210 printf("Unable to connect to server! Connection status: %d\n",status);
213 sockin=fdopen(_socket,"r");
214 sockout=fdopen(_socket,"w");
218 fprintf(sockout, "HELO %s\r\n",username);
221 if (fgets(buffer,BUFSIZ,sockin)) {
222 if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
223 wprint (dispwin, buffer);
226 if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
232 fprintf(sockout, "WHO \r\n");
239 if ((c != ERR) && (strlen(inplin)< 1000)) {
246 inplin[strlen(inplin)-1]='\0';
247 wmove (inpwin,0,strlen(inplin));
252 inplin[strlen(inplin)-1]='\0';
253 wmove (inpwin,0,strlen(inplin));
258 inplin[strlen(inplin)-1]='\0';
259 wmove (inpwin,0,strlen(inplin));
264 sprintf (inplin,"%s%c",inplin,(int) c);
270 if (fgets(buffer,BUFSIZ,sockin)) {
272 if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
273 wprint (dispwin, &buffer[15]);
276 if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
282 if (((int) c == 13) || ((int) c == 10)) {
284 if (inplin[0] == '/') {
285 fprintf(sockout,"%s\r\n",&inplin[1]);
288 fprintf(sockout,"MESSAGE \"%s\"\r\n",inplin);
292 if (fgets(buffer,BUFSIZ,sockin)) {
293 if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
294 wprint (dispwin, buffer);
297 if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
315 /* do your non-curses wrapup here */
316 fprintf(sockout,"QUIT\r\n");
319 while (fgets(buffer,BUFSIZ,sockin));