testing display related segfaults .. :(
[my-code/ivac.git] / src / input.c
index a2886ee..b29688c 100644 (file)
@@ -9,19 +9,32 @@
 int input_init(t_input *input) {
 
   struct termios tios;
+  int size;
 
   puts("[input] initializing input system ...");
 
-  if((input->content=(char *)malloc(MAX_CONTENT))==NULL) {
+  size=((input->mode&CONTENT_BUFFER)?MAX_CONTENT:1);
+
+  if((input->content=(char *)malloc(size))==NULL) {
     perror("[input] malloc call");
     return I_ERROR;
   }
+    
+  memset(input->content,0,size);
   input->c_count=0;
 
-  tcgetattr(0,&tios);
-  /* switch off canonical mode */
-  tios.c_lflag&=(~ICANON);
-  tios.c_lflag&=(~ECHO);
+  tcgetattr(0,&(input->tios));
+  tios=input->tios;
+
+  /* general settings */
+  tios.c_iflag&=ICRNL; /* \r -> \n */
+  tios.c_cc[VTIME]=0; /* no timeout */
+  tios.c_cc[VMIN]=1; /* 1 char for non-can. mode */
+
+  /* depending on used modes */
+  if(!(input->mode&LINE_BUFFERED)) tios.c_lflag&=(~ICANON);
+  if(!(input->mode&INPUT_ECHO)) tios.c_lflag&=(~ECHO);
+
   tcsetattr(0,TCSANOW,&tios);
 
   return I_SUCCESS;
@@ -29,39 +42,34 @@ int input_init(t_input *input) {
 
 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);
-
+  tcsetattr(0,TCSANOW,&(input->tios));
   puts("[input] shutdown");
 
   return I_SUCCESS;
 }
 
-int input_get_char(t_input *input,int (*callback)(t_input *input,void *ptr),
+int input_get_event(t_input *input,int (*callback)(t_input *input,void *ptr),
                    void *ptr) {
 
-  char data[1];
+  char data[MAX_CONTENT];
+  int count;
 
-  if(read(0,data,1)==-1) {
+  /* delete char counter if not buffered */
+  if(!(input->mode&CONTENT_BUFFER)) input->c_count=0;
+
+  if((count=read(0,data,MAX_CONTENT))==-1) {
     perror("[input] read call");
     return I_ERROR;
   }
 
-  if(input->c_count==MAX_CONTENT) {
+  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++;
-
-  if(data[0]=='\n') input->c_count=0;
+  strncpy(&(input->content[input->c_count]),data,count);
+  input->c_count+=count;
 
   callback(input,ptr);