+/* ttt-client.c - client handling code for tic-tac-toe game server
+ *
+ * Copyright © 2005 Carl Worth
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Author: Bryan Worth <bryan@theworths.org>
+ */
+
+
+/* TODO: see /usr/share/doc/ncurses-devel-5.3/test/view.c for example code
+ * to handle window resizing
+ *
+ * add code to read username from config file or prompt user if config
+ * file does not exist. (username is currently hardcoded and needs
+ * changed before compiling)
+ *
+ */
+
+
+#include "ttt-socket.h"
+#include <curses.h>
+#include <signal.h>
+
+static void finish(int sig);
+
+void mvprintstr(int, int, char *, chtype);
+void mvwprintstr(WINDOW *, int, int, char *, chtype);
+void wprint(WINDOW *, char *);
+
+void mvprintstr(int y, int x, char *string, chtype attrs)
+{
+ int xx = 0;
+ int l;
+ chtype cline[1000];
+ l = strlen(string);
+ while (xx < l) {
+ cline[xx] = string[xx] | attrs;
+ xx++;
+ }
+ cline[l] = '\0';
+ mvaddchstr(y, x, cline);
+}
+
+
+void mvwprintstr(WINDOW * window, int y, int x, char *string, chtype attrs)
+{
+ int xx = 0;
+ int l;
+ chtype cline[1000];
+ l = strlen(string);
+ while (xx < l) {
+ cline[xx] = string[xx] | attrs;
+ xx++;
+ }
+ cline[l] = '\0';
+ mvwaddchstr(window, y, x, cline);
+}
+
+
+void wprint(WINDOW * window, char *string)
+{
+ int xx = 0;
+ int l;
+ chtype outc;
+ l = strlen(string);
+
+ while (xx < l) {
+ outc = string[xx];
+ if (string[xx] != '\r')
+ waddch(window, outc);
+ xx++;
+ }
+}
+
+
+char *message2;
+int _socket;
+size_t maxread;
+
+
+int main(int argc, char **argv)
+{
+ char *message;
+ char buffer[1000];
+ char *host;
+ char *port;
+ chtype c;
+ ssize_t numread;
+
+ static WINDOW *mainwnd;
+ static WINDOW *dispwin;
+ static WINDOW *inpwin;
+
+ mainwnd = initscr();
+ int dlines = LINES - 2, cols = COLS;
+
+ (void) nonl();
+ noecho();
+ cbreak();
+ nodelay(mainwnd, TRUE);
+ curs_set(0);
+ refresh();
+ dispwin = newwin(dlines - 1, cols - 2, 0, 0);
+ inpwin = newwin(1, cols - 2, dlines, 0);
+ keypad(mainwnd, TRUE); // enable keyboard mapping
+ keypad(inpwin, TRUE); // enable keyboard mapping
+ nodelay(inpwin, TRUE);
+ wrefresh(mainwnd);
+ wrefresh(dispwin);
+ wrefresh(inpwin);
+ (void) scrollok(mainwnd, TRUE);
+ (void) scrollok(dispwin, TRUE);
+ (void) scrollok(inpwin, TRUE);
+ (void) idlok(mainwnd, TRUE);
+ (void) idlok(dispwin, TRUE);
+ (void) idlok(inpwin, TRUE);
+ wsetscrreg(dispwin, 0, dlines - 2);
+
+ (void) signal(SIGINT, finish); /* arrange interrupts to terminate */
+ if (has_colors()) {
+ start_color();
+
+ init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
+ init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
+ init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
+ init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
+ init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
+ init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
+ init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
+ init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
+ }
+ wbkgd(dispwin, COLOR_PAIR(COLOR_WHITE));
+ wbkgd(inpwin, COLOR_PAIR(COLOR_CYAN));
+ wrefresh(dispwin);
+ wrefresh(inpwin);
+
+ xasprintf(&host, "localhost");
+ xasprintf(&port, "5334");
+ xasprintf(&message, "HELO bryan\r\n");
+
+ ttt_socket_create_client(host, port, &_socket);
+ ttt_socket_write(_socket, message, strlen(message));
+
+ maxread = 100;
+ numread = read(_socket, buffer, maxread);
+ if (numread > 0) {
+ buffer[numread] = '\0';
+ xasprintf(&message, "%s", buffer);
+ wprint(dispwin, message);
+ wrefresh(dispwin);
+ }
+ xasprintf(&message2, "MESSAGE \"");
+ while (1) {
+ curs_set(1);
+ c = wgetch(inpwin);
+ if (c != ERR) {
+ xasprintf(&message2, "%s%c", message2, (int) c);
+ waddch(inpwin, c);
+ wrefresh(inpwin);
+ }
+
+ numread = 0;
+ maxread = 100;
+ numread = read(_socket, buffer, maxread);
+ if (numread > 0) {
+ curs_set(0);
+ buffer[numread] = '\0';
+ wprint(dispwin, buffer);
+ wrefresh(dispwin);
+ wrefresh(inpwin);
+ }
+ if ((int) c == 13) {
+ curs_set(0);
+ xasprintf(&message2, "%s\"\r\n", message2);
+ ttt_socket_write(_socket, message2, strlen(message2));
+ xasprintf(&message2, "MESSAGE \"");
+ maxread = 100;
+ numread = read(_socket, buffer, maxread);
+ if (numread > 0) {
+ buffer[numread] = '\0';
+ wprint(dispwin, buffer);
+ }
+ wrefresh(dispwin);
+ werase(inpwin);
+ wrefresh(inpwin);
+ }
+ }
+
+
+
+}
+
+
+static void finish(int sig)
+{
+ char buffer[1024];
+ ssize_t numread;
+ endwin();
+
+ /* do your non-curses wrapup here */
+ xasprintf(&message2, "QUIT\r\n");
+ ttt_socket_write(_socket, message2, strlen(message2));
+ maxread = 100;
+ numread = read(_socket, buffer, maxread);
+ buffer[numread] = '\0';
+ xasprintf(&message2, "%s", buffer);
+ exit(0);
+}