more abstraction in network.* and input.*, first display stuff in ivac.*
[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   if(!(input->mode&LINE_BUFFERED)) {
22     tcgetattr(0,&tios);
23     /* switch off canonical mode */
24     tios.c_lflag&=(~ICANON);
25     tios.c_lflag&=(~ECHO);
26     tcsetattr(0,TCSANOW,&tios);
27   }
28
29   return I_SUCCESS;
30 }
31
32 int input_shutdown(t_input *input) {
33
34   struct termios tios;
35
36   free(input->content);
37
38   tcgetattr(0,&tios);
39   tios.c_lflag|=ICANON;
40   tios.c_lflag|=ECHO;
41   tcsetattr(0,TCSANOW,&tios);
42
43   puts("[input] shutdown");
44
45   return I_SUCCESS;
46 }
47
48 int input_get_event(t_input *input,int (*callback)(t_input *input,void *ptr),
49                    void *ptr) {
50
51   char data[MAX_CONTENT];
52   int count;
53
54   /* delete char counter if not buffered */
55   if(!(input->mode&CONTENT_BUFFER)) input->c_count=0;
56
57   if((count=read(0,data,MAX_CONTENT))==-1) {
58     perror("[input] read call");
59     return I_ERROR;
60   }
61
62   if(input->c_count>=MAX_CONTENT) {
63     puts("[input] max input length reached");
64     return I_ERROR;
65   }
66
67   strncpy(&(input->content[input->c_count]),data,count);
68   input->c_count+=count;
69
70   callback(input,ptr);
71
72   return I_SUCCESS;
73 }