more abstraction in network.* and input.*, first display stuff in ivac.*
[my-code/ivac.git] / src / input.c
index 47f808b..1403017 100644 (file)
@@ -8,30 +8,66 @@
 
 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;
+
+  if(!(input->mode&LINE_BUFFERED)) {
+    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_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;
 }