]> git.cworth.org Git - ttt/blob - src/ttt-curses-client.c
Deleted debugging code and changed return errors to fit protocol
[ttt] / src / ttt-curses-client.c
1 /*ttt-curses-client.c - curses based tic-tac-toe game client
2  *
3  * Copyright © 2005 Bryan Worth
4  *
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)
8  * any later version.
9  *
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.
14  *
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.
18  *
19  * Author: Bryan Worth <bryan@theworths.org> 
20  */
21
22 /* TODO: see /usr/share/doc/ncurses-devel-5.3/test/view.c for example code
23  *       to handle window resizing
24  *
25  */      
26
27 #include "ttt-socket.h"
28 #include <curses.h>
29 #include <signal.h>
30
31 static void
32 finish (int sig);
33
34 void
35 mvprintstr (int     y,
36             int     x,
37             char    *string,
38             chtype  attrs);
39
40 void
41 mvwprintstr (WINDOW *window,
42              int    y,
43              int    x,
44              char   *string,
45              chtype attrs);
46
47 void
48 wprint (WINDOW *window,
49         char   *string);
50
51 /*
52  *
53  */
54
55 void
56 mvprintstr (int    y,
57             int    x,
58             char   *string,
59             chtype attrs)
60 {
61     int xx = 0;
62     int l;
63     chtype cline[1000];
64     l = strlen(string);
65     while (xx < l) {
66         cline[xx] = string[xx] | attrs;
67         xx++;
68     }
69     cline[l] = '\0';
70     mvaddchstr (y, x, cline);
71 }
72
73 void mvwprintstr (WINDOW *window,
74                   int    y,
75                   int    x,
76                   char   *string,
77                   chtype attrs)
78 {
79     int xx = 0;
80     int l;
81     chtype cline[1000];
82     l = strlen (string);
83     while (xx < l) {
84         cline[xx] = string[xx] | attrs;
85         xx++;
86     }
87     cline[l] = '\0';
88     mvwaddchstr (window, y, x, cline);
89 }
90
91 void wprint (WINDOW *window,
92              char   *string)
93 {
94     int xx = 0;
95     int l;
96     chtype outc;
97     l = strlen (string);
98
99     while (xx < l) {
100         outc = string[xx];
101         if (string[xx] != '\r') 
102             waddch (window, outc);
103         xx++;
104     }
105 }
106
107 FILE *sockin, *sockout;
108
109 int
110 main (int argc, char **argv)
111 {
112     char *host="localhost";
113     char *port="5334";
114     chtype c;
115     char *username;
116     char *envuser;
117     char *confpath;
118     char *conffile;
119     char *command_string;
120     int _socket;
121     char buffer[BUFSIZ];
122     char inplin[1024];
123     ttt_status_t status;
124
125     static WINDOW *mainwnd;
126     static WINDOW *dispwin;
127     static WINDOW *statwin;
128     static WINDOW *inpwin;
129     FILE *conf_file;
130
131     mainwnd = initscr();
132     int dlines = LINES - 2, cols = COLS;
133
134     (void) nonl ();
135     (void) nl ();
136     noecho ();
137     cbreak ();
138     nodelay (mainwnd, TRUE);
139     curs_set (0);
140     refresh ();
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);
147     wrefresh (mainwnd);
148     wrefresh (dispwin);
149     wrefresh (statwin);
150     wrefresh (inpwin);
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);
161     
162     (void) signal (SIGINT, finish);     /* arrange interrupts to terminate */
163     if (has_colors ()) {
164         start_color ();
165
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);
174     }
175     //wbkgd (dispwin, COLOR_PAIR (COLOR_WHITE));
176     //wbkgd (inpwin, COLOR_PAIR (COLOR_CYAN));
177     wrefresh (dispwin);
178     wrefresh (inpwin);
179
180     xasprintf(&confpath,"%s/.ttt/",getenv("HOME"));
181     xasprintf(&conffile,"%s/.ttt/client.conf",getenv("HOME"));
182     username="user";
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);
189         }
190         if ((conf_file = fopen(conffile,"w")) != NULL) {
191                 fprintf(conf_file,"username=%s",username);
192                 fclose(conf_file);
193         }
194     }
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]);
200             }
201             if (strncmp(buffer,"port=",5) == 0) {
202                 xasprintf (&port, "%s",&buffer[5]);
203             }
204         }
205     fclose(conf_file); 
206     }
207     status=ttt_socket_create_client (host, port, &_socket);
208     if (status) {
209         endwin();
210         printf("Unable to connect to server! Connection status: %d\n",status);
211         exit(1);
212     }
213     sockin=fdopen(_socket,"r");
214     sockout=fdopen(_socket,"w");
215
216
217
218     fprintf(sockout, "HELO %s\r\n",username);
219     fflush(sockout);
220     
221     if (fgets(buffer,BUFSIZ,sockin)) {
222         if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
223                 wprint (dispwin, buffer);
224         }
225         else {
226             if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
227         }
228         wrefresh (dispwin);
229         wrefresh (statwin);
230     }
231     
232     fprintf(sockout, "WHO \r\n");
233     fflush(sockout);
234     
235     inplin[0]='\0';
236     while (1) {
237         curs_set (1);
238         c = wgetch (inpwin);
239         if ((c != ERR) && (strlen(inplin)< 1000)) {
240             switch ((int) c) {
241                 case 13: 
242                     break;
243                 case 10:
244                     break;
245                 case 8:
246                     inplin[strlen(inplin)-1]='\0';
247                     wmove (inpwin,0,strlen(inplin));
248                     wclrtoeol( inpwin);
249                     wrefresh (inpwin);
250                     break;
251                 case 127:
252                     inplin[strlen(inplin)-1]='\0';
253                     wmove (inpwin,0,strlen(inplin));
254                     wclrtoeol (inpwin);
255                     wrefresh (inpwin);
256                     break;
257                 case 263:
258                     inplin[strlen(inplin)-1]='\0';
259                     wmove (inpwin,0,strlen(inplin));
260                     wclrtoeol (inpwin);
261                     wrefresh (inpwin);
262                     break;
263                 default: 
264                     sprintf (inplin,"%s%c",inplin,(int) c);
265                     waddch (inpwin, c);
266                     wrefresh (inpwin);
267             }
268         }
269
270         if (fgets(buffer,BUFSIZ,sockin)) {
271             curs_set (0);
272             if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
273                 wprint (dispwin, &buffer[15]);
274             }
275             else {
276                 if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
277             }
278             wrefresh (dispwin);
279             wrefresh (statwin);
280             wrefresh (inpwin);
281         }
282         if (((int) c == 13) || ((int) c == 10)) {
283             curs_set (0);
284             if (inplin[0] == '/') {
285                 fprintf(sockout,"%s\r\n",&inplin[1]);
286             }
287             else {
288                 fprintf(sockout,"MESSAGE \"%s\"\r\n",inplin);
289             }
290             fflush(sockout);
291             inplin[0] = '\0';
292             if (fgets(buffer,BUFSIZ,sockin)) {
293                 if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
294                     wprint (dispwin, buffer);
295                 }
296                 else {
297                     if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
298                 }
299             }
300             wrefresh (dispwin);
301             wrefresh (statwin);
302             werase (inpwin);
303             wrefresh (inpwin);
304         }
305     }
306 }
307
308 static void
309 finish (int sig)
310 {
311     char buffer[BUFSIZ];
312     
313     endwin();
314
315     /* do your non-curses wrapup here */
316     fprintf(sockout,"QUIT\r\n");
317     fflush(sockout);
318
319     while (fgets(buffer,BUFSIZ,sockin));
320     exit (0);
321 }