testing display related segfaults .. :(
[my-code/ivac.git] / src / input.c
index 47f808b..b29688c 100644 (file)
@@ -8,30 +8,70 @@
 
 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 K_ERROR;
+    return I_ERROR;
   }
+    
+  memset(input->content,0,size);
+  input->c_count=0;
+
+  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 K_SUCCESS;
+  return I_SUCCESS;
 }
 
 int input_shutdown(t_input *input) {
 
   free(input->content);
+  tcsetattr(0,TCSANOW,&(input->tios));
   puts("[input] shutdown");
 
-  return K_SUCCESS;
+  return I_SUCCESS;
 }
 
-int input_get_char(t_input *input) {
+int input_get_event(t_input *input,int (*callback)(t_input *input,void *ptr),
+                   void *ptr) {
+
+  char data[MAX_CONTENT];
+  int count;
+
+  /* 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) {
+    puts("[input] max input length reached");
+    return I_ERROR;
+  }
 
-  char data[64];
+  strncpy(&(input->content[input->c_count]),data,count);
+  input->c_count+=count;
 
-  puts("a hopefully nice display for user interaction will popup soon ;)");
-  read(0,data,64);
+  callback(input,ptr);
 
-  return K_SUCCESS;
+  return I_SUCCESS;
 }