bugfixes + improvements
[physik/nlsop.git] / nlsop_gui.c
1 /*
2  * nlsop gui code
3  *
4  * author: frank zirkelbach (frank.zirkelbach@physik.uni-augsburg.de)
5  *
6  * this program tries helping to understand the amorphous depuration
7  * and recrystallization of SiCx while ion implantation at temperatures
8  * below 400 degree celsius.
9  * hopefully the program will simulate the stabilization of the
10  * selforganizing lamella structure in the observed behaviour.
11  *
12  * refs: 
13  *  - J. K. N. Lindner. Habil.Schrift, Universitaet Augsburg.
14  *  - Maik Haeberlen. Diplomarbeit, Universitaet Augsburg.
15  *
16  * Copyright (C) 2004 Frank Zirkelbach
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31  *
32  */
33
34 #define _GNU_SOURCE
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42
43 #include "nlsop.h"
44 #include "dfbapi.h"
45 #include "random.h"
46
47 #include "network.h"
48 #include "event.h"
49 #include "display.h"
50 #include "input.h"
51
52 #include "nlsop_general.h"
53
54 #define LOGFILE "~/.nlsop_logfile"
55
56 int usage(char *prog)
57 {
58  puts("usage:");
59  printf("%s -i <ip> -p <port> -l <logfile>\n",prog);
60  return 1;
61 }
62
63 /*
64  * gui internal functions
65  */
66
67 int display_new_line(t_display *display,char *text) {
68
69   int x,y;
70   int ptr;
71   int i;
72
73   unsigned char *tmp;
74
75   x=display->max_x-1;
76   y=display->max_y-1;
77  
78   tmp=(unsigned char *)malloc(display->max_x);
79   memset(tmp,0x20,display->max_x);
80
81   display_line(display,0,0,x,0,'#');
82   display_string(display,1,y-y/2-17,"nlsop gui (C) 2004 Frank Zirkelbach",35);
83   display_line(display,0,2,x,2,'#');
84   display_line(display,0,y-1,x,y-1,'#');
85   display_string(display,0,y,"prompt: ",8);
86   display_string(display,8,y,tmp,x-8);
87   display_set_cursor(display,8,y);
88
89   ptr=3*display->max_x;
90   for(i=0;i<display->max_x-6;i++) {
91     memcpy(display->screen+ptr,display->screen+ptr+display->max_x,
92            display->max_x);
93     ptr+=display->max_x;
94   }
95
96   display_draw(display);
97
98   return 1;
99 }
100
101 int send_and_wait_for_answer(t_net *net,t_display *display) {
102
103   int i,count;
104   t_job job;
105   char data;
106   char string[MAX_CONTENT];
107
108   data=GUI_INFO;
109
110   /* request information */
111   network_send_chan(net,0,&data,sizeof(unsigned char));
112
113   /* receive information */
114   network_receive_chan(net,0,&data,sizeof(unsigned char));
115   if(data!=GUI_INFO) {
116     display_new_line(display,"no gui info answer, ignored");
117     return -1;
118   }
119
120   network_receive_chan(net,0,(unsigned char *)&count,sizeof(int));
121   for(i=0;i<count;i++) {
122     network_receive_chan(net,0,(unsigned char *)&job,sizeof(t_job));
123     snprintf(string,MAX_CONTENT,
124              "channel %d %c => b=%f c=%f s=%f | d: %d %f | %d",
125              job.channel,job.status&IN_WORK?'a':'q',
126              job.info.b,job.info.c,job.info.s,
127              job.info.diff_rate,job.info.dr_ac,job.progress);
128     display_new_line(display,string);
129   }
130
131   display_new_line(display,"done");
132
133   return 1;
134 }
135
136 int send_job(t_net *net,t_input *input,t_display *display) {
137
138   char command[MAX_CONTENT];
139   int x,y,z;
140   info info;
141
142   /* default values */
143   x=_X;
144   y=_Y;
145   z=_Z;
146   info.cc=CC;
147   info.steps=STEPS;
148   info.range=RANGE;
149   info.diff_rate=DIFF_RATE;
150   info.cpi=CPI;
151   info.s_rate=S_RATE;
152   info.save_rate=RESAVE;
153   info.s=S_D;
154   info.b=B_D;
155   info.c=C_D;
156   info.dr_ac=DR_AC;
157
158   strncpy(command,input->content,MAX_CONTENT);
159
160   strtok(command," ");
161   while(strtok(NULL," ")!=NULL) {
162     switch(command[0]) {
163       case 'b':
164         info.b=atof(command+1);
165         break;
166       case 'c':
167         info.c=atof(command+1);
168         break;
169       case 's':
170         info.s=atof(command+1);
171         break;
172       case 'd':
173         info.diff_rate=atoi(command+1);
174         break;
175       case 'D':
176         info.dr_ac=atof(command+1);
177         break;
178       case 'S':
179         info.steps=atoi(command+1);
180         break;
181       case 'C':
182         info.cpi=atoi(command+1);
183         break;
184       case 'r':
185         info.range=atoi(command+1);
186         break;
187       case 'R':
188         info.s_rate=atoi(command+1);
189         break;
190       case 'x':
191         info.save_rate=atoi(command+1);
192         break;
193       case 'X':
194         x=atoi(command+1);
195         break;
196       case 'Y':
197         y=atoi(command+1);
198         break;
199       case 'Z':
200         z=atoi(command+1);
201         break;
202       default:
203         display_new_line(display,"unknown command");
204         break;
205     }
206   }
207
208   command[0]=GUI_ADDJOB;
209   network_send_chan(net,0,command,1);
210
211   network_send_chan(net,0,(unsigned char *)&x,sizeof(int));
212   network_send_chan(net,0,(unsigned char *)&y,sizeof(int));
213   network_send_chan(net,0,(unsigned char *)&z,sizeof(int));
214
215   network_send_chan(net,0,(unsigned char *)&info,sizeof(info));
216
217   return 1;
218 }
219
220 int get_command(t_event *event,void *allineed) {
221
222   t_net *net;
223   t_display *display;
224   t_input *input;
225   unsigned int addr[3];
226
227   memcpy(addr,allineed,3*sizeof(unsigned int));
228   net=(t_net *)addr[0];
229   display=(t_display *)addr[1];
230   input=(t_input *)addr[2];
231
232   switch(input->content[0]) {
233     case GUI_INFO:
234       send_and_wait_for_answer(net,display);
235       break;
236     case GUI_ADDJOB:
237       send_job(net,input,display);
238       break;
239     case GUI_QUIT:
240       /* stop everything */
241       event_math(0,event,READ,REMOVE);
242       network_shutdown(net);
243       display_shutdown(display);
244       input_shutdown(input);
245       event_stop(event);
246       return 2;
247     default:
248       display_new_line(display,"unknown gui command");
249       break;
250   }
251
252   return 1;
253 }
254
255 /*
256  * main program
257  */
258
259 int main(int argc,char **argv)
260 {
261
262   char server_ip[16];
263   char logfile[64];
264   int port;
265
266   t_net net;
267   t_event event;
268   t_display display;
269   t_input input;
270
271   unsigned char data;
272   int i;
273
274   int fd;
275
276   unsigned int addr[3];
277   void *allyouneed;
278
279   allyouneed=(void *)addr;
280
281   addr[0]=(unsigned int)&net;
282   addr[1]=(unsigned int)&display;
283   addr[2]=(unsigned int)&input;
284
285   /* default values */
286   strcpy(logfile,LOGFILE);
287   strcpy(server_ip,"");
288   port=1025;
289
290   /* parse/check argv */
291   for(i=1;i<argc;i++) {
292     if(argv[i][0]=='-') {
293       switch(argv[i][1]) {
294         case 'h':
295           usage(argv[0]);
296           return -1;
297         case 'i':
298           strncpy(server_ip,argv[++i],16);
299           break;
300         case 'p':
301           port=atoi(argv[++i]);
302           break;
303         case 'l':
304           strncpy(logfile,argv[++i],64);
305           break;
306         default:
307           usage(argv[0]);
308           return -1;
309       }
310     }
311   }
312   if(!strcmp(server_ip,"")) {
313     usage(argv[0]);
314     return -1;
315   }
316
317   if((fd=open(logfile,O_WRONLY|O_CREAT))<0) {
318     printf("unable to open file %s\n",logfile);
319     return -1;
320   }
321
322   /* input init */
323   input_init(&input,fd);
324   input.mode=CONTENT_BUFFER|LINE_BUFFERED|INPUT_ECHO;
325   input_ios_init(&input);
326
327   /* event init */
328   event_init(&event,fd);
329   event_set_timeout(&event,0,0);
330
331   /* display init */
332   display_init(&display,fd);
333
334   /* user interaction */
335   event_math(0,&event,READ,ADD);
336
337   display_new_line(&display,"welcome to nlsop gui! :)");
338
339   /* connect to server */
340   network_init(&net,fd);
341   network_set_connection_info(&net,0,server_ip,port);
342   if(network_connect(&net,0)==N_E_CONNECT) {
343     printf("unable to connect to server, aborting ...\n");
344     return -1;
345   }
346   network_select(&net,0);
347
348   display_new_line(&display,"successfully connected to server");
349
350   /* tell server: i am a client, i may work for you */
351   data=NLSOP_GUI;
352   network_send(net.connection[0].fd,&data,1);
353
354   display_new_line(&display,"send data to server, waiting for input ...");
355
356   /* wait for job */
357   event_start(&event,allyouneed,get_command,NULL);
358
359   free(allyouneed);
360   close(fd);
361
362   return 1;
363 }