fixed input system (callback added), more ivac tests
[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,&tios);
22   /* switch off canonical mode */
23   tios.c_lflag&=(~ICANON);
24   tios.c_lflag&=(~ECHO);
25   tcsetattr(0,TCSANOW,&tios);
26
27   return I_SUCCESS;
28 }
29
30 int input_shutdown(t_input *input) {
31
32   struct termios tios;
33
34   free(input->content);
35
36   tcgetattr(0,&tios);
37   tios.c_lflag|=ICANON;
38   tios.c_lflag|=ECHO;
39   tcsetattr(0,TCSANOW,&tios);
40
41   puts("[input] shutdown");
42
43   return I_SUCCESS;
44 }
45
46 int input_get_char(t_input *input,int (*callback)(t_input *input,void *ptr),
47                    void *ptr) {
48
49   char data[1];
50
51   if(read(0,data,1)==-1) {
52     perror("[input] read call");
53     return I_ERROR;
54   }
55
56   if(input->c_count==MAX_CONTENT) {
57     puts("[input] max input length reached");
58     return I_ERROR;
59   }
60
61   input->content[input->c_count]=data[0];
62   input->c_count++;
63
64   if(data[0]=='\n') input->c_count=0;
65
66   callback(input,ptr);
67
68   return I_SUCCESS;
69 }