]> git.cworth.org Git - ttt/blob - src/ttt-curses-client.c
2005-12-08 Bryan Worth <bryan@theworths.org>
[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  *       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)
28  *
29  */      
30
31 #include "ttt-socket.h"
32 #include <curses.h>
33 #include <signal.h>
34
35 static void
36 finish (int sig);
37
38 void
39 mvprintstr (int     y,
40             int     x,
41             char    *string,
42             chtype  attrs);
43
44 void
45 mvwprintstr (WINDOW *window,
46              int    y,
47              int    x,
48              char   *string,
49              chtype attrs);
50
51 void
52 wprint (WINDOW *window,
53         char   *string);
54
55 /*
56  *
57  */
58
59 void
60 mvprintstr (int    y,
61             int    x,
62             char   *string,
63             chtype attrs)
64 {
65     int xx = 0;
66     int l;
67     chtype cline[1000];
68     l = strlen(string);
69     while (xx < l) {
70         cline[xx] = string[xx] | attrs;
71         xx++;
72     }
73     cline[l] = '\0';
74     mvaddchstr (y, x, cline);
75 }
76
77 void mvwprintstr (WINDOW *window,
78                   int    y,
79                   int    x,
80                   char   *string,
81                   chtype attrs)
82 {
83     int xx = 0;
84     int l;
85     chtype cline[1000];
86     l = strlen (string);
87     while (xx < l) {
88         cline[xx] = string[xx] | attrs;
89         xx++;
90     }
91     cline[l] = '\0';
92     mvwaddchstr (window, y, x, cline);
93 }
94
95 void wprint (WINDOW *window,
96              char   *string)
97 {
98     int xx = 0;
99     int l;
100     chtype outc;
101     l = strlen (string);
102
103     while (xx < l) {
104         outc = string[xx];
105         if (string[xx] != '\r') 
106             waddch (window, outc);
107         xx++;
108     }
109 }
110
111 FILE *sockin, *sockout;
112
113 int
114 main (int argc, char **argv)
115 {
116     char *host="localhost";
117     char *port="5334";
118     chtype c;
119     char *username;
120     char *envuser;
121     char *confpath;
122     char *conffile;
123     char *command_string;
124     int _socket;
125     char buffer[BUFSIZ];
126     char inplin[1024];
127
128
129     static WINDOW *mainwnd;
130     static WINDOW *dispwin;
131     static WINDOW *statwin;
132     static WINDOW *inpwin;
133     FILE *conf_file;
134
135     mainwnd = initscr();
136     int dlines = LINES - 2, cols = COLS;
137
138     (void) nonl ();
139     (void) nl ();
140     noecho ();
141     cbreak ();
142     nodelay (mainwnd, TRUE);
143     curs_set (0);
144     refresh ();
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);
151     wrefresh (mainwnd);
152     wrefresh (dispwin);
153     wrefresh (statwin);
154     wrefresh (inpwin);
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);
165     
166     (void) signal (SIGINT, finish);     /* arrange interrupts to terminate */
167     if (has_colors ()) {
168         start_color ();
169
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);
178     }
179     //wbkgd (dispwin, COLOR_PAIR (COLOR_WHITE));
180     //wbkgd (inpwin, COLOR_PAIR (COLOR_CYAN));
181     wrefresh (dispwin);
182     wrefresh (inpwin);
183
184
185     ttt_socket_create_client (host, port, &_socket);
186     sockin=fdopen(_socket,"r");
187     sockout=fdopen(_socket,"w");
188
189
190     xasprintf(&confpath,"%s/.ttt/",getenv("HOME"));
191     xasprintf(&conffile,"%s/.ttt/client.conf",getenv("HOME"));
192     username="user";
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);
199         }
200         if ((conf_file = fopen(conffile,"w")) != NULL) {
201                 fprintf(conf_file,"username=%s",username);
202                 fclose(conf_file);
203         }
204     }
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]);
209                 break;
210             }
211         }
212     fclose(conf_file); 
213     }
214
215     fprintf(sockout, "HELO %s\r\n",username);
216     fflush(sockout);
217
218     if (fgets(buffer,BUFSIZ,sockin)) {
219         if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
220                 wprint (dispwin, buffer);
221         }
222         else {
223             if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
224         }
225         wrefresh (dispwin);
226         wrefresh (statwin);
227     }
228     inplin[0]='\0';
229     while (1) {
230         curs_set (1);
231         c = wgetch (inpwin);
232         if ((c != ERR) && (strlen(inplin)< 1000)) {
233             switch ((int) c) {
234                 case 13: 
235                     break;
236                 case 10:
237                     break;
238                 case 8:
239                     inplin[strlen(inplin)-1]='\0';
240                     wmove(inpwin,0,strlen(inplin));
241                     wclrtoeol(inpwin);
242                     break;
243                 default: 
244                     sprintf (inplin,"%s%c",inplin,(int) c);
245                     waddch (inpwin, c);
246             }
247             wrefresh (inpwin);
248         }
249
250         if (fgets(buffer,BUFSIZ,sockin)) {
251             curs_set (0);
252             if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
253                 wprint (dispwin, &buffer[15]);
254             }
255             else {
256                 if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
257             }
258             wrefresh (dispwin);
259             wrefresh (statwin);
260             wrefresh (inpwin);
261         }
262         if (((int) c == 13) || ((int) c == 10)) {
263             curs_set (0);
264             fprintf(sockout,"MESSAGE \"%s\"\r\n",inplin);
265             fflush(sockout);
266             inplin[0] = '\0';
267             if (fgets(buffer,BUFSIZ,sockin)) {
268                 if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) {
269                     wprint (dispwin, buffer);
270                 }
271                 else {
272                     if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
273                 }
274             }
275             wrefresh (dispwin);
276             wrefresh (statwin);
277             werase (inpwin);
278             wrefresh (inpwin);
279         }
280     }
281 }
282
283 static void
284 finish (int sig)
285 {
286     char buffer[BUFSIZ];
287     
288     endwin();
289
290     /* do your non-curses wrapup here */
291     fprintf(sockout,"QUIT\r\n");
292     fflush(sockout);
293
294     while (fgets(buffer,BUFSIZ,sockin));
295     exit (0);
296 }