diff -urN cui115/cui.c cui116/cui.c --- cui115/cui.c Wed Apr 16 00:00:00 2014 +++ cui116/cui.c Thu Apr 17 00:00:00 2014 @@ -40,12 +40,30 @@ } int -cui_conn(char *s) +cui_conn_host_port(char *host, int port) { - int i, port, fd; - char name[1024]; struct sockaddr_in addr; struct hostent *hp; + int fd; + + if((hp = gethostbyname(host)) == NULL) return -1; + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + memcpy(&addr.sin_addr, hp->h_addr, hp->h_length); + if((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) return -1; + if(connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0){ + close(fd); + return -1; + } + return fd; +} + +int +cui_conn(char *s) +{ + int i, port; + char name[1024]; for(i=0; s[i]; i++) if(s[i] == ':') break; if(i > 0 && s[i]){ @@ -55,14 +73,7 @@ i = s[i] ? i + 1 : 0; port = strtol(s+i, NULL, 0); - if((hp = gethostbyname(name)) == NULL) ERR("gethostbyname"); - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons(port); - memcpy(&addr.sin_addr, hp->h_addr, hp->h_length); - if((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) ERR("socket"); - if(connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) ERR("connect"); - return fd; + return cui_conn_host_port(name, port); } int diff -urN cui115/cui.h cui116/cui.h --- cui115/cui.h Wed Apr 16 00:00:00 2014 +++ cui116/cui.h Thu Apr 17 00:00:00 2014 @@ -47,6 +47,7 @@ int cui_init(int ac, char **av); void cui_fini(int init_ret); +int cui_conn_host_port(char *host, int port); int cui_conn(char *s); int cui_srv_port(int port); int cui_srv_accept(int sfd); diff -urN cui115/cui_srv.c cui116/cui_srv.c --- cui115/cui_srv.c Wed Apr 16 22:00:00 2014 +++ cui116/cui_srv.c Thu Apr 17 00:00:00 2014 @@ -1,17 +1,15 @@ #include #include #include -#include -#include -#include -#include #include - #include "cui.h" #include "scpanel.h" #include "tab.h" #include "term.h" #include "handler.h" +#include "button.h" +#include "etext.h" +#include "num.h" static int arg_idx(int ac, char **av, char *targ) @@ -54,29 +52,31 @@ } static void -srv_accept(cui obj, int sfd) +client_add(cui tab, int cfd) { - cui view = obj->parent; - int y = cui_y2(obj); - cui termin; static int cnt = 0; char buf[16]; - int cfd = cui_srv_accept(sfd); - - termin = cui_termin_new(view, 0, 0, 0, 0, cfd); + cui view = tab->parent; + int y = cui_y2(tab); + cui termin = cui_termin_new(view, 0, 0, 0, 0, cfd); + cui_hide(termin); cui_xywh_set(termin, 0, y, view->w, view->h - y); sprintf(buf, "%d", cnt++); - cui_tab_add(obj, strdup(buf), termin, FALSE); + cui_tab_add(tab, strdup(buf), termin, FALSE); } static int tab_hdr(cui obj, int evt, int val, void *prm) { + int sfd, cfd; + switch(evt){ case CUI_EVT_READABLE: - srv_accept(obj, *(int *)prm); + sfd = *(int *)prm; + cfd = cui_srv_accept(sfd); + client_add(obj, cfd); break; default: return FALSE; @@ -110,6 +110,68 @@ return TRUE; } +typedef struct conn_prm{ + cui view, conn, tab; + cui dlg, host, port, ok, cancel; + int result; +} *conn_prm; + +static int +conn_hdr(cui obj, int evt, int val, void *prm) +{ + conn_prm p = (conn_prm)prm; + int cfd; + + switch(evt){ + case CUI_EVT_BUTTON: + if(obj == p->conn){ + cui_show(p->dlg); + cui_main(p->dlg, p->host); + cui_hide(p->dlg); + cui_draw(p->dlg->parent); + if(p->result){ + char *host = cui_etext_str_get(p->host); + int port = cui_num_get(p->port); + cfd = cui_conn_host_port(host, port); + client_add(p->tab, cfd); + } + }else if(obj == p->ok){ + p->result = TRUE; + cui_quit(); + }else if(obj == p->cancel){ + p->result = FALSE; + cui_quit(); + }else return FALSE; + break; + default: + return FALSE; + } + return TRUE; +} + +static void +conn_prm_new(cui conn, cui tab) +{ + cui obj = cui_alloc(sizeof(struct conn_prm)); + conn_prm p = (conn_prm)obj; + + p->conn = conn; + p->view = conn->parent; + p->tab = tab; + p->dlg = cui_panel_new(p->view, conn->x+1, cui_y2(conn), 0, 0); + cui_hide(p->dlg); + p->host = cui_etext_new(p->dlg, 1, 1, 12, "localhost"); + p->port = cui_num_new(p->dlg, p->host->x, cui_y2(p->host), 10, 12345, 0, 65535, 0); + p->ok = cui_button_new(p->dlg, p->port->x, cui_y2(p->port)+1, "OK"); + p->cancel = cui_button_new(p->dlg, cui_x2(p->ok)+1, p->ok->y, "Cancel"); + cui_wh_fit(p->dlg); + cui_wh_set(p->dlg, p->dlg->w+1, p->dlg->h+1); + + cui_bind(conn, CUI_EVT_BUTTON, conn_hdr, p); + cui_bind(p->ok, CUI_EVT_BUTTON, conn_hdr, p); + cui_bind(p->cancel, CUI_EVT_BUTTON, conn_hdr, p); +} + int main(int ac, char **av) { @@ -119,10 +181,12 @@ int init_ret = cui_init(ac, av); cui bs = cui_scpanel_new(NULL, 0, 0, w_get(ac, av), h_get(ac, av), "cui_srv"); cui view = ((cui_scpanel)bs)->view; - cui tab = cui_tab_new(view, 0, 0, 0, NULL, NULL, FALSE); + cui conn = cui_button_new(view, 0, 0, "conn"); + cui tab = cui_tab_new(view, cui_x2(conn) + 1, 0, 0, NULL, NULL, FALSE); cui_readable_add_bind(tab, sfd, tab_hdr, &sfd); cui_bind(view, CUI_EVT_DEAD | CUI_EVT_RESIZE, view_hdr, tab); + conn_prm_new(conn, tab); cui_main(bs, NULL); cui_free(bs);