added ncurses based display system (minimal and still full of bugs) :o
[my-code/ivac.git] / src / ivac.c
1 /* ivac.c -- main ivac file
2  *
3  * author: hackbard@hackdaworld.dyndns.org
4  *         frank.zirkelbach@physik.uni-augsburg.de
5  *
6  * Copyright (C) 2004 Frank Zirkelbach
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include "ivac.h"
25
26 int main(int argc,char **argv) {
27
28   /* TESTING BY NOW */
29
30   t_ivac ivac;
31
32   /* set username (futur: read from config or entered later) */
33   strcpy(ivac.username,"hackbard");
34   memset(&ivac.console[0][0],0,IVAC_CONSOLE_LEN*IVAC_CONSOLE_STRING_LEN);
35
36   /* set capabilities (futur: set by check routines) */
37   ivac.g_cap=NETWORK;
38   ivac.av_cap=AUDIO|VIDEO|DUPLEX;
39
40   /* set event timeout */
41   ivac.event.timeout.tv_sec=IVAC_S_SEC;
42   ivac.event.timeout.tv_usec=IVAC_S_USEC;
43
44   /* set listen port (futur: read from config or entered later) */
45   network_set_listen_port(&(ivac.net),IVAC_LISTEN_PORT);
46
47   /* event init */
48   event_init(&(ivac.event));
49
50   /* input init */
51   ivac.input.mode=CONTENT_BUFFER;
52   input_init(&(ivac.input));
53
54   /* network init */
55   if(network_init(&(ivac.net))==N_ERROR) {
56     printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
57            ivac.net.l_port);
58     ivac_shutdown(&ivac);
59     return ERROR;
60   }
61
62   /* add listening port + stdin to (read) event system */
63   event_math(ivac.net.l_fd,&(ivac.event),READ,ADD);
64   event_math(0,&(ivac.event),READ,ADD);
65
66   /* display init */
67   display_init(&(ivac.display));
68
69   /* display */
70   ivac_display(&(ivac.display));
71
72   /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
73   event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
74
75   ivac_shutdown(&ivac);
76
77   return SUCCESS;
78 }
79
80 int ivac_shutdown(t_ivac *ivac) {
81
82   network_shutdown(&(ivac->net));
83   input_shutdown(&(ivac->input));
84   event_stop(&(ivac->event));
85   display_shutdown(&(ivac->display));
86
87   return SUCCESS;
88 }
89
90 int ivac_send_info(int channel,t_ivac *ivac) {
91
92   char data[SEND_N_MAX];
93   int size;
94
95   size=strlen(ivac->username);
96
97   data[0]=IVAC_SEND_NAME;
98   data[1]=size;
99   strncpy(data+2,ivac->username,size);
100   size+=2;
101
102   data[size]=IVAC_SEND_G_CAP;
103   data[size+1]=1;
104   data[size+2]=ivac->g_cap;
105   size+=3;
106
107   data[size]=IVAC_SEND_AV_CAP;
108   data[size+1]=2;
109   data[size+2]=(ivac->av_cap)>>8;
110   data[size+3]=(ivac->av_cap)&0xff;
111   size+=4;
112
113   if(network_send(ivac->net.connection[channel].fd,data,size)==N_ERROR) {
114     puts("[ivac] ivac_send_info failed");
115     return ERROR;
116   }
117
118   return SUCCESS;
119 }
120
121 int ivac_receive_info(int channel,t_ivac *ivac) {
122
123   char data[SEND_N_MAX];
124   int count,length;
125
126   count=0;
127
128   if((length=network_receive(ivac->net.connection[channel].fd,
129                              data,SEND_N_MAX))==N_ERROR) {
130     puts("[ivac] ivac_receive_info failed");
131     return ERROR;
132   }
133
134   while(length-count) {
135     switch(data[count]) {
136       case IVAC_SEND_NAME:
137         strncpy(ivac->challenger[channel].name,data+count+2,data[count+1]);
138         ivac->challenger[channel].name[data[count+1]]='\0';
139         count+=(data[count+1]+2);
140         break;
141       case IVAC_SEND_G_CAP:
142         ivac->challenger[channel].g_cap=data[count+2];
143         count+=3;
144         break;
145       case IVAC_SEND_AV_CAP:
146         ivac->challenger[channel].av_cap=data[count+2]<<8;
147         ivac->challenger[channel].av_cap|=data[count+3];
148         count+=4;
149         break;
150       default:
151         puts("[ivac] ivac_receive_info, unknown character");
152         return ERROR;
153         break;
154     }
155   }
156
157   return SUCCESS;
158 }
159
160 int ivac_event_cb(t_event *event,void *ptr) {
161  
162   t_ivac *ivac;
163   int channel;
164
165   ivac=(t_ivac *)ptr;
166
167   if(FD_ISSET(ivac->net.l_fd,&(event->rfds))) {
168     /* manage incoming + send info */
169     channel=network_manage_incoming(&(ivac->net));
170     event_math(ivac->net.connection[channel].fd,event,READ,ADD);
171     ivac_send_info(channel,ivac);
172   }
173
174   /* receive info */
175   for(channel=0;channel<MAX_CONNECTIONS;channel++)
176     if(ivac->net.connection[channel].status&C_ESTABL)
177       if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds)))
178         ivac_receive_info(channel,ivac);
179
180   /* user interaction */
181   if(FD_ISSET(0,&(event->rfds)))
182     input_get_event(&(ivac->input),ivac_parse_command,ivac);
183
184   /* display ivac gui */
185   ivac_display_content(ivac);
186     
187   return SUCCESS;
188 }
189
190 int ivac_regular_cb(t_event *event,void *ptr) {
191
192   /* usual jobs like audio & video transmit ... */
193
194   return SUCCESS;
195 }
196
197 int ivac_parse_command(t_input *input,void *ptr) {
198
199   t_ivac *ivac;
200   int channel,i;
201   int len;
202   char *data,valid=0;
203   char c_str[IVAC_CONSOLE_STRING_LEN];
204
205   ivac=(t_ivac *)ptr;
206   data=input->content;
207   i=input->c_count-1;
208   memset(c_str,0,IVAC_CONSOLE_STRING_LEN);
209
210   /* parse command routines */
211
212   if((data[i]=='\r') || (data[i]=='\n')) {
213
214     /* parse commands */
215     switch(data[0]) {
216       case '/':
217         len=strlen(data+1);
218         if(len==4) {
219           /* len 4 commands */
220           if(!strncmp(data+1,"quit",4)) {
221             valid=1;
222             sprintf(c_str,"ivac shutdown\n");
223             ivac_add_to_monitor(ivac,c_str);
224             ivac_shutdown(ivac);
225           }
226         }
227         break;
228       default:
229         break;
230     }
231
232     if(!valid) {
233       snprintf(c_str,IVAC_CONSOLE_STRING_LEN-1,"unknown command: '%s...'\n",
234                data);
235       ivac_add_to_monitor(ivac,c_str);
236     }
237
238     /* delete content buffer + reset counter */
239     memset(input->content,0,input->c_count);
240     input->c_count=0;
241
242   }
243
244   return SUCCESS;
245 }
246
247 int ivac_display_head(t_display *display) {
248
249   int x,y;
250
251   move(0,0);
252   for(x=0;x<display->max_x;x++) addch('#');
253   mvaddstr(1,0,"##");
254   mvaddstr(1,(display->max_x-4)/2-4,"- ivac -");
255   mvaddstr(1,(display->max_x-2),"##");
256   move(2,0);
257   for(x=0;x<display->max_x;x++) addch('#');
258   refresh();
259
260   return SUCCESS;
261 }
262
263 int ivac_display_box(t_display *display) {
264   
265   int x,y;
266
267   for(y=IVAC_PROMPT_LEN;y<display->max_y-IVAC_PROMPT_LEN;y++) {
268     mvaddch(y,0,'#');
269     mvaddch(y,display->max_x-1,'#');
270   }
271
272   return SUCCESS;
273 }
274
275 int ivac_display_box_content(t_ivac *ivac) {
276
277
278   return SUCCESS;
279 }
280
281 int ivac_display_console(t_display *display) {
282                                              
283   int x,y;
284          
285   move(display->max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN-1,0);
286   for(x=0;x<display->max_x;x++) addch('#');                         
287                                           
288   return SUCCESS;
289 }
290
291 int ivac_display_console_content(t_ivac *ivac) {
292
293   int x,y,len;
294   for(y=0;y<IVAC_CONSOLE_LEN;y++) {
295     len=strlen(ivac->console[y]);
296     move(ivac->display.max_y-IVAC_CONSOLE_LEN-IVAC_PROMPT_LEN+y,2);
297     for(x=0;x<IVAC_CONSOLE_STRING_LEN;x++) addch(ivac->console[y][x]);
298   }
299   refresh();
300
301   return SUCCESS;
302 }
303
304 int ivac_display_prompt(t_display *display) {
305
306   int x,y;
307
308   move(display->max_y-3,0);
309   for(x=0;x<display->max_x;x++) addch('#');
310   mvaddstr(display->max_y-2,0,"## command: ");
311   mvaddstr(display->max_y-2,display->max_x-2,"##");
312   move(display->max_y-1,0);
313   for(x=0;x<display->max_x;x++) addch('#');
314   refresh();
315
316   return SUCCESS;
317 }
318
319 int ivac_display_prompt_content(t_ivac *ivac) {
320
321   int x,y;
322
323   /* delete old command */
324   if(ivac->input.c_count==0) {
325     move(ivac->display.max_y-2,12);
326     for(x=12;x<ivac->display.max_x-1;x++) addch(' ');
327   }
328
329   for(x=0;x<ivac->input.c_count;x++)
330     mvaddch(ivac->display.max_y-2,x+12,ivac->input.content[x]);
331   refresh();
332
333   return SUCCESS;
334 }
335
336 int ivac_display(t_display *display) {
337
338   int x,y;
339
340   /* display head */
341   ivac_display_head(display);
342
343   /* display box */
344   ivac_display_box(display);
345
346   /* display console */
347   ivac_display_console(display);
348
349   /* display command prompt */
350   ivac_display_prompt(display);
351
352   return SUCCESS;
353 }
354
355 int ivac_display_content(t_ivac *ivac) {
356
357   /* display box content */
358   ivac_display_box_content(ivac);
359
360   /* display console content */
361   ivac_display_console_content(ivac);
362
363   /* display prompt content */
364   ivac_display_prompt_content(ivac);
365
366   return SUCCESS;
367 }
368
369 int ivac_add_to_monitor(t_ivac *ivac,char *msg) {
370
371   int i;
372
373   for(i=0;i<IVAC_CONSOLE_LEN-1;i++)
374     memcpy(ivac->console[i],ivac->console[i+1],IVAC_CONSOLE_STRING_LEN);
375   memcpy(ivac->console[IVAC_CONSOLE_LEN-1],msg,IVAC_CONSOLE_STRING_LEN);
376   ivac_display_console_content(ivac);
377
378   return SUCCESS;
379 }