X-Git-Url: https://git.cworth.org/git?p=ttt;a=blobdiff_plain;f=src%2Fttt-curses-client.c;h=25d724acf428a8680b9ad0af55629474ced6a076;hp=b6e5981194b90d19380432198511da990eae243a;hb=122d6b8810d67c601fe260e25ec323bb001698af;hpb=8fbec78bf02e67469ffaea254d49c291f9987105 diff --git a/src/ttt-curses-client.c b/src/ttt-curses-client.c index b6e5981..25d724a 100644 --- a/src/ttt-curses-client.c +++ b/src/ttt-curses-client.c @@ -1,6 +1,6 @@ -/* 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 @@ -19,28 +19,44 @@ * Author: Bryan Worth */ - /* 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 #include -static void finish(int sig); +static void +finish (int sig); + +void +mvprintstr (int y, + int x, + char *string, + chtype attrs); -void mvprintstr(int, int, char *, chtype); -void mvwprintstr(WINDOW *, int, int, char *, chtype); -void wprint(WINDOW *, char *); +void +mvwprintstr (WINDOW *window, + int y, + int x, + char *string, + chtype attrs); -void mvprintstr(int y, int x, char *string, chtype attrs) +void +wprint (WINDOW *window, + char *string); + +/* + * + */ + +void +mvprintstr (int y, + int x, + char *string, + chtype attrs) { int xx = 0; int l; @@ -51,171 +67,255 @@ void mvprintstr(int y, int x, char *string, chtype attrs) 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); + if (string[xx] != '\r') + waddch (window, outc); xx++; } } +FILE *sockin, *sockout; -char *message2; -int _socket; -size_t maxread; - - -int main(int argc, char **argv) +int +main (int argc, char **argv) { - char *message; - char buffer[1000]; - char *host; - char *port; + char *host="localhost"; + char *port="5334"; chtype c; - ssize_t numread; + char *username; + char *envuser; + char *confpath; + char *conffile; + char *command_string; + int _socket; + char buffer[BUFSIZ]; + char inplin[1024]; + ttt_status_t status; static WINDOW *mainwnd; static WINDOW *dispwin; + static WINDOW *statwin; static WINDOW *inpwin; + FILE *conf_file; 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 (); + (void) nl (); + noecho (); + cbreak (); + nodelay (mainwnd, TRUE); + curs_set (0); + refresh (); + dispwin = newwin (dlines - 5, cols - 2, 0, 0); + statwin = newwin (4, cols - 2, dlines -3, 0); + inpwin = newwin (2, cols - 2, dlines, 0); + keypad (mainwnd, TRUE); // enable keyboard mapping + keypad (inpwin, TRUE); // enable keyboard mapping + nodelay (inpwin, TRUE); + wrefresh (mainwnd); + wrefresh (dispwin); + wrefresh (statwin); + wrefresh (inpwin); + (void) scrollok (mainwnd, TRUE); + (void) scrollok (dispwin, TRUE); + (void) scrollok (statwin, TRUE); + (void) scrollok (inpwin, TRUE); + (void) idlok (mainwnd, TRUE); + (void) idlok (dispwin, TRUE); + (void) idlok (statwin, TRUE); + (void) idlok (inpwin, TRUE); + wsetscrreg (dispwin, 0, dlines - 2); + wsetscrreg (statwin, 0, 4); - (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); + (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); } - 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); - } + //wbkgd (dispwin, COLOR_PAIR (COLOR_WHITE)); + //wbkgd (inpwin, COLOR_PAIR (COLOR_CYAN)); + wrefresh (dispwin); + 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); + xasprintf(&confpath,"%s/.ttt/",getenv("HOME")); + xasprintf(&conffile,"%s/.ttt/client.conf",getenv("HOME")); + username="user"; + if (access (conffile, F_OK) != 0 ) { + envuser=getenv("USER"); + if (envuser != NULL) username=strdup(envuser); + if (access (confpath, F_OK) != 0 ) { + xasprintf(&command_string,"mkdir %s",confpath); + system (command_string); + } + if ((conf_file = fopen(conffile,"w")) != NULL) { + fprintf(conf_file,"username=%s",username); + fclose(conf_file); } - 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); + } + if ((conf_file = fopen(conffile,"r")) != NULL) { + while (fgets(buffer,BUFSIZ,conf_file)) { + if (buffer[strlen(buffer)-1] == 10) buffer[strlen(buffer)-1] = '\0'; + if (strncmp(buffer,"username=",9) == 0) { + xasprintf (&username, "%s", &buffer[9]); + } + if (strncmp(buffer,"port=",5) == 0) { + xasprintf (&port, "%s",&buffer[5]); } - wrefresh(dispwin); - werase(inpwin); - wrefresh(inpwin); } + fclose(conf_file); + } + status=ttt_socket_create_client (host, port, &_socket); + if (status) { + endwin(); + printf("Unable to connect to server! Connection status: %d\n",status); + exit(1); } + sockin=fdopen(_socket,"r"); + sockout=fdopen(_socket,"w"); -} + fprintf(sockout, "HELO %s\r\n",username); + fflush(sockout); + + if (fgets(buffer,BUFSIZ,sockin)) { + if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) { + wprint (dispwin, buffer); + } + else { + if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer); + } + wrefresh (dispwin); + wrefresh (statwin); + } + + fprintf(sockout, "WHO \r\n"); + fflush(sockout); + + inplin[0]='\0'; + while (1) { + curs_set (1); + c = wgetch (inpwin); + if ((c != ERR) && (strlen(inplin)< 1000)) { + switch ((int) c) { + case 13: + break; + case 10: + break; + case 8: + inplin[strlen(inplin)-1]='\0'; + wmove (inpwin,0,strlen(inplin)); + wclrtoeol( inpwin); + wrefresh (inpwin); + break; + case 127: + inplin[strlen(inplin)-1]='\0'; + wmove (inpwin,0,strlen(inplin)); + wclrtoeol (inpwin); + wrefresh (inpwin); + break; + case 263: + inplin[strlen(inplin)-1]='\0'; + wmove (inpwin,0,strlen(inplin)); + wclrtoeol (inpwin); + wrefresh (inpwin); + break; + default: + sprintf (inplin,"%s%c",inplin,(int) c); + waddch (inpwin, c); + wrefresh (inpwin); + } + } + if (fgets(buffer,BUFSIZ,sockin)) { + curs_set (0); + if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) { + wprint (dispwin, &buffer[15]); + } + else { + if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer); + } + wrefresh (dispwin); + wrefresh (statwin); + wrefresh (inpwin); + } + if (((int) c == 13) || ((int) c == 10)) { + curs_set (0); + if (inplin[0] == '/') { + fprintf(sockout,"%s\r\n",&inplin[1]); + } + else { + fprintf(sockout,"MESSAGE \"%s\"\r\n",inplin); + } + fflush(sockout); + inplin[0] = '\0'; + if (fgets(buffer,BUFSIZ,sockin)) { + if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) { + wprint (dispwin, buffer); + } + else { + if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer); + } + } + wrefresh (dispwin); + wrefresh (statwin); + werase (inpwin); + wrefresh (inpwin); + } + } +} -static void finish(int sig) +static void +finish (int sig) { - char buffer[1024]; - ssize_t numread; + char buffer[BUFSIZ]; + 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); + fprintf(sockout,"QUIT\r\n"); + fflush(sockout); + + while (fgets(buffer,BUFSIZ,sockin)); + exit (0); }