canoncial mode fixes
[my-code/ivac.git] / src / input.c
1 /* input.c -- input management stuff
2  *
3  * author: hackbard@hackdaworld.dyndns.org
4  *
5  */
6
7 #include "input.h"
8
9 int input_init(t_input *input) {
10
11   struct termios tios;
12
13   puts("[input] initializing input system ...");
14
15   if((input->content=(char *)malloc(MAX_CONTENT))==NULL) {
16     perror("[input] malloc call");
17     return I_ERROR;
18   }
19   input->c_count=0;
20
21   tcgetattr(0,&(input->tios));
22   tios=input->tios;
23
24   if(!(input->mode&LINE_BUFFERED)) {
25     tios.c_lflag&=(~ICANON);
26     tios.c_lflag&=(~ECHO);
27     tios.c_cc[VTIME]=0;
28     tios.c_cc[VMIN]=1;
29     tcsetattr(0,TCSANOW,&tios);
30   }
31
32   return I_SUCCESS;
33 }
34
35 int input_shutdown(t_input *input) {
36
37   free(input->content);
38   tcsetattr(0,TCSANOW,&(input->tios));
39   puts("[input] shutdown");
40
41   return I_SUCCESS;
42 }
43
44 int input_get_event(t_input *input,int (*callback)(t_input *input,void *ptr),
45                    void *ptr) {
46
47   char data[MAX_CONTENT];
48   int count;
49
50   /* delete char counter if not buffered */
51   if(!(input->mode&CONTENT_BUFFER)) input->c_count=0;
52
53   if((count=read(0,data,MAX_CONTENT))==-1) {
54     perror("[input] read call");
55     return I_ERROR;
56   }
57
58   if(input->c_count>=MAX_CONTENT) {
59     puts("[input] max input length reached");
60     return I_ERROR;
61   }
62
63   strncpy(&(input->content[input->c_count]),data,count);
64   input->c_count+=count;
65
66   callback(input,ptr);
67
68   return I_SUCCESS;
69 }