]> git.cworth.org Git - ttt/blob - src/ttt-curses-client.c
2005-12-05 Richard D. Worth <richard@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 char *message2;
112 int _socket;
113 size_t maxread;
114
115 int
116 main (int argc, char **argv)
117 {
118     char *message;
119     char buffer[1000];
120     char *host;
121     char *port;
122     chtype c;
123     ssize_t numread;
124
125     static WINDOW *mainwnd;
126     static WINDOW *dispwin;
127     static WINDOW *inpwin;
128
129     mainwnd = initscr();
130     int dlines = LINES - 2, cols = COLS;
131
132     (void) nonl ();
133     noecho ();
134     cbreak ();
135     nodelay (mainwnd, TRUE);
136     curs_set (0);
137     refresh ();
138     dispwin = newwin (dlines - 1, cols - 2, 0, 0);
139     inpwin = newwin (1, cols - 2, dlines, 0);
140     keypad (mainwnd, TRUE);     // enable keyboard mapping 
141     keypad (inpwin, TRUE);      // enable keyboard mapping 
142     nodelay (inpwin, TRUE);
143     wrefresh (mainwnd);
144     wrefresh (dispwin);
145     wrefresh (inpwin);
146     (void) scrollok (mainwnd, TRUE);
147     (void) scrollok (dispwin, TRUE);
148     (void) scrollok (inpwin, TRUE);
149     (void) idlok (mainwnd, TRUE);
150     (void) idlok (dispwin, TRUE);
151     (void) idlok (inpwin, TRUE);
152     wsetscrreg (dispwin, 0, dlines - 2);
153     
154     (void) signal (SIGINT, finish);     /* arrange interrupts to terminate */
155     if (has_colors ()) {
156         start_color ();
157
158         init_pair (COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
159         init_pair (COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
160         init_pair (COLOR_RED, COLOR_RED, COLOR_BLACK);
161         init_pair (COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
162         init_pair (COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
163         init_pair (COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
164         init_pair (COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
165         init_pair (COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
166     }
167     wbkgd (dispwin, COLOR_PAIR (COLOR_WHITE));
168     wbkgd (inpwin, COLOR_PAIR (COLOR_CYAN));
169     wrefresh (dispwin);
170     wrefresh (inpwin);
171
172     xasprintf (&host, "localhost");
173     xasprintf (&port, "5334");
174     xasprintf (&message, "HELO bryan\r\n");
175
176     ttt_socket_create_client (host, port, &_socket);
177     ttt_socket_write (_socket, message, strlen (message));
178
179     maxread = 100;
180     numread = read (_socket, buffer, maxread);
181     if (numread > 0) {
182         buffer[numread] = '\0';
183         xasprintf (&message, "%s", buffer);
184         wprint (dispwin, message);
185         wrefresh (dispwin);
186     }
187     xasprintf (&message2, "MESSAGE \"");
188     while (1) {
189         curs_set (1);
190         c = wgetch (inpwin);
191         if (c != ERR) {
192             xasprintf (&message2, "%s%c", message2, (int) c);
193             waddch (inpwin, c);
194             wrefresh (inpwin);
195         }
196
197         numread = 0;
198         maxread = 100;
199         numread = read (_socket, buffer, maxread);
200         if (numread > 0) {
201             curs_set (0);
202             buffer[numread] = '\0';
203             wprint (dispwin, buffer);
204             wrefresh (dispwin);
205             wrefresh (inpwin);
206         }
207         if ((int) c == 13) {
208             curs_set (0);
209             xasprintf (&message2, "%s\"\r\n", message2);
210             ttt_socket_write (_socket, message2, strlen (message2));
211             xasprintf (&message2, "MESSAGE \"");
212             maxread = 100;
213             numread = read (_socket, buffer, maxread);
214             if (numread > 0) {
215                 buffer[numread] = '\0';
216                 wprint (dispwin, buffer);
217             }
218             wrefresh (dispwin);
219             werase (inpwin);
220             wrefresh (inpwin);
221         }
222     }
223 }
224
225 static void
226 finish (int sig)
227 {
228     char buffer[1024];
229     ssize_t numread;
230     endwin();
231
232     /* do your non-curses wrapup here */
233     xasprintf (&message2, "QUIT\r\n");
234     ttt_socket_write (_socket, message2, strlen (message2));
235
236     maxread = 100;
237     numread = read (_socket, buffer, maxread);
238     buffer[numread] = '\0';
239     xasprintf (&message2, "%s", buffer);
240
241     exit (0);
242 }