From: hackbard Date: Wed, 5 May 2004 21:48:15 +0000 (+0000) Subject: fixed input system (callback added), more ivac tests X-Git-Url: https://hackdaworld.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f7d9ad56ad9c10284c067cb2db36a7c72b33c5b3;p=my-code%2Fivac.git fixed input system (callback added), more ivac tests --- diff --git a/src/input.c b/src/input.c index 47f808b..a2886ee 100644 --- a/src/input.c +++ b/src/input.c @@ -8,30 +8,62 @@ int input_init(t_input *input) { + struct termios tios; + puts("[input] initializing input system ..."); if((input->content=(char *)malloc(MAX_CONTENT))==NULL) { perror("[input] malloc call"); - return K_ERROR; + return I_ERROR; } + input->c_count=0; + + tcgetattr(0,&tios); + /* switch off canonical mode */ + tios.c_lflag&=(~ICANON); + tios.c_lflag&=(~ECHO); + tcsetattr(0,TCSANOW,&tios); - return K_SUCCESS; + return I_SUCCESS; } int input_shutdown(t_input *input) { + struct termios tios; + free(input->content); + + tcgetattr(0,&tios); + tios.c_lflag|=ICANON; + tios.c_lflag|=ECHO; + tcsetattr(0,TCSANOW,&tios); + puts("[input] shutdown"); - return K_SUCCESS; + return I_SUCCESS; } -int input_get_char(t_input *input) { +int input_get_char(t_input *input,int (*callback)(t_input *input,void *ptr), + void *ptr) { + + char data[1]; + + if(read(0,data,1)==-1) { + perror("[input] read call"); + return I_ERROR; + } + + if(input->c_count==MAX_CONTENT) { + puts("[input] max input length reached"); + return I_ERROR; + } + + input->content[input->c_count]=data[0]; + input->c_count++; - char data[64]; + if(data[0]=='\n') input->c_count=0; - puts("a hopefully nice display for user interaction will popup soon ;)"); - read(0,data,64); + callback(input,ptr); - return K_SUCCESS; + return I_SUCCESS; } diff --git a/src/input.h b/src/input.h index 7f5796b..e31ebe2 100644 --- a/src/input.h +++ b/src/input.h @@ -8,10 +8,11 @@ #include #include #include +#include /* defines */ -#define K_SUCCESS 1 -#define K_ERROR -1 +#define I_SUCCESS 1 +#define I_ERROR -1 #define MENUE (1<<0) #define CONNECTIONS (1<<1) @@ -21,7 +22,14 @@ /* input specific variables */ typedef struct s_input { char *content; + int c_count; unsigned char mode; } t_input; +/* function prototypes */ +int input_init(t_input *input); +int input_shutdown(t_input *input); +int input_get_char(t_input *input,int (*callback)(t_input *input,void *ptr), + void *ptr); + #endif diff --git a/src/ivac.c b/src/ivac.c index c567f28..f4995a7 100644 --- a/src/ivac.c +++ b/src/ivac.c @@ -46,7 +46,10 @@ int main(int argc,char **argv) { input_init(&(ivac.input)); /* network init */ - network_init(&(ivac.net)); + if(network_init(&(ivac.net))==N_ERROR) { + printf("[ivac] use 'fuser -n tcp %d' to kill that process",ivac.net.l_port); + return ERROR; + } /* add listening port + stdin to (read) event system */ event_math(ivac.net.l_fd,&(ivac.event),READ,ADD); @@ -55,6 +58,10 @@ int main(int argc,char **argv) { /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */ event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb); + network_shutdown(&(ivac.net)); + + input_shutdown(&(ivac.input)); + return SUCCESS; } @@ -79,7 +86,8 @@ int ivac_event_cb(t_event *event,void *ptr) { receive_info(channel,&(ivac->net)); /* user interaction */ - if(FD_ISSET(0,&(event->rfds))) input_get_char(ivac); + if(FD_ISSET(0,&(event->rfds))) + input_get_char(&(ivac->input),ivac_display,ivac); return SUCCESS; } @@ -90,3 +98,14 @@ int ivac_regular_cb(t_event *event,void *ptr) { return SUCCESS; } + +int ivac_display(t_input *input,void *ptr) { + + t_ivac *ivac; + + ivac=(t_ivac *)ptr; + + if(input->content[input->c_count-1]=='q') ivac->event.status=DISABLED; + + return SUCCESS; +} diff --git a/src/ivac.h b/src/ivac.h index 76844f5..9cbced9 100644 --- a/src/ivac.h +++ b/src/ivac.h @@ -33,5 +33,6 @@ typedef struct s_ivac { /* function prototypes */ int ivac_event_cb(t_event *event,void *ptr); int ivac_regular_cb(t_event *event,void *ptr); +int ivac_display(t_input *input,void *ptr); #endif