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);
111 FILE *sockin, *sockout;
114 main (int argc, char **argv)
116 char *host="localhost";
123 char *command_string;
129 static WINDOW *mainwnd;
130 static WINDOW *dispwin;
131 static WINDOW *statwin;
132 static WINDOW *inpwin;
136 int dlines = LINES - 2, cols = COLS;
142 nodelay (mainwnd, TRUE);
145 dispwin = newwin (dlines - 5, cols - 2, 0, 0);
146 statwin = newwin (4, cols - 2, dlines -3, 0);
147 inpwin = newwin (1, cols - 2, dlines, 0);
148 keypad (mainwnd, TRUE); // enable keyboard mapping
149 keypad (inpwin, TRUE); // enable keyboard mapping
150 nodelay (inpwin, TRUE);
155 (void) scrollok (mainwnd, TRUE);
156 (void) scrollok (dispwin, TRUE);
157 (void) scrollok (statwin, TRUE);
158 (void) scrollok (inpwin, TRUE);
159 (void) idlok (mainwnd, TRUE);
160 (void) idlok (dispwin, TRUE);
161 (void) idlok (statwin, TRUE);
162 (void) idlok (inpwin, TRUE);
163 wsetscrreg (dispwin, 0, dlines - 2);
164 wsetscrreg (statwin, 0, 4);
166 (void) signal (SIGINT, finish); /* arrange interrupts to terminate */
170 init_pair (COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
171 init_pair (COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
172 init_pair (COLOR_RED, COLOR_RED, COLOR_BLACK);
173 init_pair (COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
174 init_pair (COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
175 init_pair (COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
176 init_pair (COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
177 init_pair (COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
179 //wbkgd (dispwin, COLOR_PAIR (COLOR_WHITE));
180 //wbkgd (inpwin, COLOR_PAIR (COLOR_CYAN));
185 ttt_socket_create_client (host, port, &_socket);
186 sockin=fdopen(_socket,"r");
187 sockout=fdopen(_socket,"w");
190 xasprintf(&confpath,"%s/.ttt/",getenv("HOME"));
191 xasprintf(&conffile,"%s/.ttt/client.conf",getenv("HOME"));
193 if (access (conffile, F_OK) != 0 ) {
194 envuser=getenv("USER");
195 if (envuser != NULL) username=strdup(envuser);
196 if (access (confpath, F_OK) != 0 ) {
197 xasprintf(&command_string,"mkdir %s",confpath);
198 system (command_string);
200 if ((conf_file = fopen(conffile,"w")) != NULL) {
201 fprintf(conf_file,"username=%s",username);
205 if ((conf_file = fopen(conffile,"r")) != NULL) {
206 while (fgets(buffer,BUFSIZ,conf_file)) {
207 if (strncmp(buffer,"username=",9) == 0) {
208 xasprintf (&username, "%s", &buffer[9]);
215 fprintf(sockout, "HELO %s\r\n",username);
218 if (fgets(buffer,BUFSIZ,sockin)) {
219 if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
220 wprint (dispwin, buffer);
223 if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
232 if ((c != ERR) && (strlen(inplin)< 1000)) {
239 inplin[strlen(inplin)-1]='\0';
240 wmove(inpwin,0,strlen(inplin));
244 sprintf (inplin,"%s%c",inplin,(int) c);
250 if (fgets(buffer,BUFSIZ,sockin)) {
252 if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
253 wprint (dispwin, &buffer[15]);
256 if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
262 if (((int) c == 13) || ((int) c == 10)) {
264 fprintf(sockout,"MESSAGE \"%s\"\r\n",inplin);
267 if (fgets(buffer,BUFSIZ,sockin)) {
268 if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
269 wprint (dispwin, buffer);
272 if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
290 /* do your non-curses wrapup here */
291 fprintf(sockout,"QUIT\r\n");
294 while (fgets(buffer,BUFSIZ,sockin));