-/* ttt-client.c - client handling code for tic-tac-toe game server
+/* ttt-curses-client.c - curses based tic-tac-toe game client
*
- * Copyright © 2005 Carl Worth
+ * Copyright © 2005 Bryan 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
* 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
*
*
*/
-
#include "ttt-socket.h"
#include <curses.h>
#include <signal.h>
-static void finish(int sig);
+static void
+finish (int sig);
+
+void
+mvprintstr (int y,
+ int x,
+ char *string,
+ chtype attrs);
+
+void
+mvwprintstr (WINDOW *window,
+ int y,
+ int x,
+ char *string,
+ chtype attrs);
+
+void
+wprint (WINDOW *window,
+ char *string);
-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)
+void
+mvprintstr (int y,
+ int x,
+ char *string,
+ chtype attrs)
{
int xx = 0;
int l;
xx++;
}
cline[l] = '\0';
- mvaddchstr(y, x, cline);
+ mvaddchstr (y, x, cline);
}
-
-void mvwprintstr(WINDOW * window, int y, int x, char *string, chtype attrs)
+void mvwprintstr (WINDOW *window,
+ int y,
+ int x,
+ char *string,
+ chtype attrs)
{
int xx = 0;
int l;
chtype cline[1000];
- l = strlen(string);
+ l = strlen (string);
while (xx < l) {
cline[xx] = string[xx] | attrs;
xx++;
}
cline[l] = '\0';
- mvwaddchstr(window, y, x, cline);
+ mvwaddchstr (window, y, x, cline);
}
-
-void wprint(WINDOW * window, char *string)
+void wprint (WINDOW *window,
+ char *string)
{
int xx = 0;
int l;
chtype outc;
- l = strlen(string);
+ l = strlen (string);
while (xx < l) {
outc = string[xx];
if (string[xx] != '\r')
- waddch(window, outc);
+ waddch (window, outc);
xx++;
}
}
-
char *message2;
int _socket;
size_t maxread;
-
-int main(int argc, char **argv)
+int
+main (int argc, char **argv)
{
char *message;
char buffer[1000];
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) 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);
+ (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);
+ 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");
+ 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));
+ ttt_socket_create_client (host, port, &_socket);
+ ttt_socket_write (_socket, message, strlen (message));
maxread = 100;
- numread = read(_socket, buffer, maxread);
+ numread = read (_socket, buffer, maxread);
if (numread > 0) {
buffer[numread] = '\0';
- xasprintf(&message, "%s", buffer);
- wprint(dispwin, message);
- wrefresh(dispwin);
+ xasprintf (&message, "%s", buffer);
+ wprint (dispwin, message);
+ wrefresh (dispwin);
}
- xasprintf(&message2, "MESSAGE \"");
+ xasprintf (&message2, "MESSAGE \"");
while (1) {
- curs_set(1);
- c = wgetch(inpwin);
+ curs_set (1);
+ c = wgetch (inpwin);
if (c != ERR) {
- xasprintf(&message2, "%s%c", message2, (int) c);
- waddch(inpwin, c);
- wrefresh(inpwin);
+ xasprintf (&message2, "%s%c", message2, (int) c);
+ waddch (inpwin, c);
+ wrefresh (inpwin);
}
numread = 0;
maxread = 100;
- numread = read(_socket, buffer, maxread);
+ numread = read (_socket, buffer, maxread);
if (numread > 0) {
- curs_set(0);
+ curs_set (0);
buffer[numread] = '\0';
- wprint(dispwin, buffer);
- wrefresh(dispwin);
- wrefresh(inpwin);
+ 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 \"");
+ 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);
+ numread = read (_socket, buffer, maxread);
if (numread > 0) {
buffer[numread] = '\0';
- wprint(dispwin, buffer);
+ wprint (dispwin, buffer);
}
- wrefresh(dispwin);
- werase(inpwin);
- wrefresh(inpwin);
+ wrefresh (dispwin);
+ werase (inpwin);
+ wrefresh (inpwin);
}
}
-
-
-
}
-
-static void finish(int sig)
+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));
+ xasprintf (&message2, "QUIT\r\n");
+ ttt_socket_write (_socket, message2, strlen (message2));
+
maxread = 100;
- numread = read(_socket, buffer, maxread);
+ numread = read (_socket, buffer, maxread);
buffer[numread] = '\0';
- xasprintf(&message2, "%s", buffer);
- exit(0);
+ xasprintf (&message2, "%s", buffer);
+
+ exit (0);
}