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;
}
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
+#include <termios.h>
/* 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)
/* 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
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);
/* 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;
}
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;
}
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;
+}
/* 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