]> git.cworth.org Git - ttt/blob - src/ttt-curses-client.c
b6e5981194b90d19380432198511da990eae243a
[ttt] / src / ttt-curses-client.c
1 /* ttt-client.c - client handling code for tic-tac-toe game server
2  *
3  * Copyright © 2005 Carl 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
23 /* TODO: see /usr/share/doc/ncurses-devel-5.3/test/view.c for example code
24  *       to handle window resizing
25  *       
26  *       add code to read username from config file or prompt user if config
27  *       file does not exist. (username is currently hardcoded and needs
28  *       changed before compiling)
29  *
30  */      
31
32
33 #include "ttt-socket.h"
34 #include <curses.h>
35 #include <signal.h>
36
37 static void finish(int sig);
38
39 void mvprintstr(int, int, char *, chtype);
40 void mvwprintstr(WINDOW *, int, int, char *, chtype);
41 void wprint(WINDOW *, char *);
42
43 void mvprintstr(int y, int x, char *string, chtype attrs)
44 {
45     int xx = 0;
46     int l;
47     chtype cline[1000];
48     l = strlen(string);
49     while (xx < l) {
50         cline[xx] = string[xx] | attrs;
51         xx++;
52     }
53     cline[l] = '\0';
54     mvaddchstr(y, x, cline);
55 }
56
57
58 void mvwprintstr(WINDOW * window, int y, int x, char *string, chtype attrs)
59 {
60     int xx = 0;
61     int l;
62     chtype cline[1000];
63     l = strlen(string);
64     while (xx < l) {
65         cline[xx] = string[xx] | attrs;
66         xx++;
67     }
68     cline[l] = '\0';
69     mvwaddchstr(window, y, x, cline);
70 }
71
72
73 void wprint(WINDOW * window, char *string)
74 {
75     int xx = 0;
76     int l;
77     chtype outc;
78     l = strlen(string);
79
80     while (xx < l) {
81         outc = string[xx];
82         if (string[xx] != '\r')
83             waddch(window, outc);
84         xx++;
85     }
86 }
87
88
89 char *message2;
90 int _socket;
91 size_t maxread;
92
93
94 int main(int argc, char **argv)
95 {
96     char *message;
97     char buffer[1000];
98     char *host;
99     char *port;
100     chtype c;
101     ssize_t numread;
102
103     static WINDOW *mainwnd;
104     static WINDOW *dispwin;
105     static WINDOW *inpwin;
106
107     mainwnd = initscr();
108     int dlines = LINES - 2, cols = COLS;
109
110     (void) nonl();
111     noecho();
112     cbreak();
113     nodelay(mainwnd, TRUE);
114     curs_set(0);
115     refresh();
116     dispwin = newwin(dlines - 1, cols - 2, 0, 0);
117     inpwin = newwin(1, cols - 2, dlines, 0);
118     keypad(mainwnd, TRUE);      // enable keyboard mapping 
119     keypad(inpwin, TRUE);       // enable keyboard mapping 
120     nodelay(inpwin, TRUE);
121     wrefresh(mainwnd);
122     wrefresh(dispwin);
123     wrefresh(inpwin);
124     (void) scrollok(mainwnd, TRUE);
125     (void) scrollok(dispwin, TRUE);
126     (void) scrollok(inpwin, TRUE);
127     (void) idlok(mainwnd, TRUE);
128     (void) idlok(dispwin, TRUE);
129     (void) idlok(inpwin, TRUE);
130     wsetscrreg(dispwin, 0, dlines - 2);
131     
132     (void) signal(SIGINT, finish);      /* arrange interrupts to terminate */
133     if (has_colors()) {
134         start_color();
135
136         init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
137         init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
138         init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
139         init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
140         init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
141         init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
142         init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
143         init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
144     }
145     wbkgd(dispwin, COLOR_PAIR(COLOR_WHITE));
146     wbkgd(inpwin, COLOR_PAIR(COLOR_CYAN));
147     wrefresh(dispwin);
148     wrefresh(inpwin);
149
150     xasprintf(&host, "localhost");
151     xasprintf(&port, "5334");
152     xasprintf(&message, "HELO bryan\r\n");
153
154     ttt_socket_create_client(host, port, &_socket);
155     ttt_socket_write(_socket, message, strlen(message));
156
157     maxread = 100;
158     numread = read(_socket, buffer, maxread);
159     if (numread > 0) {
160         buffer[numread] = '\0';
161         xasprintf(&message, "%s", buffer);
162         wprint(dispwin, message);
163         wrefresh(dispwin);
164     }
165     xasprintf(&message2, "MESSAGE \"");
166     while (1) {
167         curs_set(1);
168         c = wgetch(inpwin);
169         if (c != ERR) {
170             xasprintf(&message2, "%s%c", message2, (int) c);
171             waddch(inpwin, c);
172             wrefresh(inpwin);
173         }
174
175         numread = 0;
176         maxread = 100;
177         numread = read(_socket, buffer, maxread);
178         if (numread > 0) {
179             curs_set(0);
180             buffer[numread] = '\0';
181             wprint(dispwin, buffer);
182             wrefresh(dispwin);
183             wrefresh(inpwin);
184         }
185         if ((int) c == 13) {
186             curs_set(0);
187             xasprintf(&message2, "%s\"\r\n", message2);
188             ttt_socket_write(_socket, message2, strlen(message2));
189             xasprintf(&message2, "MESSAGE \"");
190             maxread = 100;
191             numread = read(_socket, buffer, maxread);
192             if (numread > 0) {
193                 buffer[numread] = '\0';
194                 wprint(dispwin, buffer);
195             }
196             wrefresh(dispwin);
197             werase(inpwin);
198             wrefresh(inpwin);
199         }
200     }
201
202
203
204 }
205
206
207 static void finish(int sig)
208 {
209     char buffer[1024];
210     ssize_t numread;
211     endwin();
212
213     /* do your non-curses wrapup here */
214     xasprintf(&message2, "QUIT\r\n");
215     ttt_socket_write(_socket, message2, strlen(message2));
216     maxread = 100;
217     numread = read(_socket, buffer, maxread);
218     buffer[numread] = '\0';
219     xasprintf(&message2, "%s", buffer);
220     exit(0);
221 }