]> git.cworth.org Git - ttt/blobdiff - src/ttt-curses-client.c
Add a dependency of ttt-client.c on ttt-lex.h to fix the build.
[ttt] / src / ttt-curses-client.c
index 23cd7d770f8f5696c444073dd7282bfaeed717ec..c8e42c58edee3525c6caaa5499c26ffd8fef7a58 100644 (file)
 
 /* 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)
  *
  */     
 
@@ -108,8 +104,41 @@ void wprint (WINDOW *window,
     }
 }
 
+void inpprint (WINDOW *window,char *string,int pos,int curx);
+
+void inpprint (WINDOW *window,char *string,int pos,int curx)
+{
+    int mx,my;
+    char tbuf[1024];
+    getmaxyx(window,my,mx);
+    if ( curx > 0 ) {
+       strncpy(tbuf,&string[curx], mx-1);
+       tbuf[mx-1]='\0';
+       wmove(window,0,0);
+       wprint(window,tbuf);
+       wclrtoeol(window);
+       wmove(window,0,pos - curx);
+       
+    }
+    else
+    {
+       strcpy(tbuf,string);
+       if (strlen(string) > mx-1) tbuf[mx-1]='\0';
+       wmove(window,0,0);
+       wprint(window,tbuf);
+       wclrtoeol(window);
+       wmove(window,0,pos);
+    }
+
+
+}
+
+
+
+
 FILE *sockin, *sockout;
 
+
 int
 main (int argc, char **argv)
 {
@@ -123,17 +152,21 @@ main (int argc, char **argv)
     char *command_string;
     int _socket;
     char buffer[BUFSIZ];
+    char tlin[1024];
     char inplin[1024];
-
+    ttt_status_t status;
+    int curpos = 0;
+    int curx = 0;
+    int mx, my;
 
     static WINDOW *mainwnd;
     static WINDOW *dispwin;
-    static WINDOW *statwin;
     static WINDOW *inpwin;
+    static WINDOW *statwin;
     FILE *conf_file;
 
     mainwnd = initscr();
-    int dlines = LINES - 2, cols = COLS;
+    int dlines = LINES, cols = COLS;
 
     (void) nonl ();
     (void) nl ();
@@ -142,12 +175,13 @@ main (int argc, char **argv)
     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 (1, cols - 2, dlines, 0);
+    dispwin = newwin (dlines - 6, cols , 0, 0);
+    statwin = newwin (4, cols , dlines -4, 0);
+    inpwin = newwin (1, cols , dlines -1, 0);
     keypad (mainwnd, TRUE);    // enable keyboard mapping 
     keypad (inpwin, TRUE);     // enable keyboard mapping 
     nodelay (inpwin, TRUE);
+    getmaxyx(inpwin,my,mx);
     wrefresh (mainwnd);
     wrefresh (dispwin);
     wrefresh (statwin);
@@ -176,17 +210,9 @@ main (int argc, char **argv)
        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);
 
-
-    ttt_socket_create_client (host, port, &_socket);
-    sockin=fdopen(_socket,"r");
-    sockout=fdopen(_socket,"w");
-
-
     xasprintf(&confpath,"%s/.ttt/",getenv("HOME"));
     xasprintf(&conffile,"%s/.ttt/client.conf",getenv("HOME"));
     username="user";
@@ -198,26 +224,39 @@ main (int argc, char **argv)
            system (command_string);
        }
        if ((conf_file = fopen(conffile,"w")) != NULL) {
-               fprintf(conf_file,"username=%s",username);
-               fclose(conf_file);
+           fprintf(conf_file,"username=%s",username);
+           fclose(conf_file);
        }
     }
     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]);
-               break;
+           }
+           if (strncmp(buffer,"port=",5) == 0) {
+               xasprintf (&port, "%s",&buffer[5]);
            }
        }
-    fclose(conf_file); 
+       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);
+           wprint (dispwin, buffer);
        }
        else {
            if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer);
@@ -225,26 +264,105 @@ main (int argc, char **argv)
        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);
-                   break;
-               default: 
-                   sprintf (inplin,"%s%c",inplin,(int) c);
-                   waddch (inpwin, c);
+           case 13:  // CARRIAGE RETURN
+               break;
+           case 10: // LINE FEED
+               break;
+           case 262: // HOME key
+               curpos = 0;
+               curx = 0;
+               inpprint(inpwin,inplin,curpos,curx);
+               wrefresh (inpwin);
+               break;       
+           case 360: // END key
+               curpos = strlen(inplin);
+               if (strlen(inplin) > mx -1) {
+                   curx = strlen(inplin) - mx +1;
+               }
+               else
+               {
+                   curx = 0;
+               }
+               inpprint(inpwin,inplin,curpos,curx);
+               wrefresh (inpwin);
+               break;
+           case 330: // DELETE key
+               if (strlen(inplin) > 0) {
+                   if (curpos == strlen(inplin)) {
+                       inplin[strlen(inplin)-1]='\0';
+                       curpos--;
+                   }
+                   else
+                   {
+                       strncpy(&inplin[curpos],&inplin[curpos+1],strlen(inplin)-curpos+1);
+                   }
+
+
+                   inpprint(inpwin,inplin,curpos,curx);
+                   wrefresh (inpwin);
+               }
+               break;          
+           case 263: //  }
+           case 8:   //   > Various BACKSPACE codes
+           case 127: //  }
+               if (curpos > 0) {
+                   if (curpos == strlen(inplin)) {
+                       inplin[strlen(inplin)-1]='\0';
+                   }
+                   else
+                   {
+                       strncpy(&inplin[curpos-1],&inplin[curpos],strlen(inplin)-curpos);
+                       inplin[strlen(inplin)-1]='\0';
+                   }
+                   if (curpos > 0) curpos--;
+                   if (curx > 0) curx--;
+                   inpprint(inpwin,inplin,curpos,curx);
+                   wrefresh (inpwin);
+               }
+               break;
+           case 260: // LEFT ARROW
+               if (curpos > 0) {
+                   curpos--;
+                   if (curx > curpos) curx = curpos;
+                   inpprint(inpwin,inplin,curpos,curx);
+                   wrefresh (inpwin);
+               }
+               break;
+           case 261: // RIGHT ARROW
+               if (curpos < strlen(inplin)) {
+                   curpos++;
+                   if (curpos - curx > mx-1) curx++;
+                   inpprint(inpwin,inplin,curpos,curx);
+                   wrefresh (inpwin);
+               }
+               break;
+           default: // ALL OTHER KEYS
+               if (curpos == strlen(inplin)) {
+                   sprintf (inplin,"%s%c",inplin,(int) c);
+               }
+               else
+               {
+                   
+                   strncpy(tlin,&inplin[curpos],strlen(inplin)-curpos);
+                   tlin[strlen(inplin)-curpos]='\0';
+                   strcpy(&inplin[curpos+1],tlin);
+                   inplin[curpos] = (int) c;
+               }
+               curpos++;
+               if (curpos - curx > mx-1) curx++;
+               inpprint(inpwin,inplin,curpos,curx);
+               wrefresh (inpwin);
            }
-           wrefresh (inpwin);
        }
 
        if (fgets(buffer,BUFSIZ,sockin)) {
@@ -261,7 +379,14 @@ main (int argc, char **argv)
        }
        if (((int) c == 13) || ((int) c == 10)) {
            curs_set (0);
-           fprintf(sockout,"MESSAGE \"%s\"\r\n",inplin);
+           curpos = 0;
+           curx = 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)) {