]> pilppa.org Git - familiar-h63xx-build.git/blob - org.handhelds.familiar/packages/gnuplot/files/qtopia.trm
OE tree imported from monotone branch org.openembedded.oz354fam083 at revision 8b12e3...
[familiar-h63xx-build.git] / org.handhelds.familiar / packages / gnuplot / files / qtopia.trm
1 /*
2  * $Id: dumb.trm,v 1.16 2004/04/13 17:24:16 broeker Exp $
3  *
4  */
5
6 /* GNUPLOT - qtopia.trm */
7
8 /*[
9  * Copyright 1991 - 1993, 1998, 2004   Thomas Williams, Colin Kelley
10  *
11  * Permission to use, copy, and distribute this software and its
12  * documentation for any purpose with or without fee is hereby granted,
13  * provided that the above copyright notice appear in all copies and
14  * that both that copyright notice and this permission notice appear
15  * in supporting documentation.
16  *
17  * Permission to modify the software is granted, but not the right to
18  * distribute the complete modified source code.  Modifications are to
19  * be distributed as patches to the released version.  Permission to
20  * distribute binaries produced by compiling modified sources is granted,
21  * provided you
22  *   1. distribute the corresponding source modifications from the
23  *    released version in the form of a patch file along with the binaries,
24  *   2. add special version identification to distinguish your version
25  *    in addition to the base release version number,
26  *   3. provide your name and address as the primary contact for the
27  *    support of your modified version, and
28  *   4. retain our contact information in regard to use of the base
29  *    software.
30  * Permission to distribute the released version of the source code along
31  * with corresponding source modifications in the form of a patch file is
32  * granted with same provisions 2 through 4 for binary distributions.
33  *
34  * This software is provided "as is" without express or implied warranty
35  * to the extent permitted by applicable law.
36 ]*/
37
38 /*
39  * This file is included by ../term.c.
40  *
41  * This terminal driver supports:
42  *   qtopia terminals
43  *
44  * AUTHORS
45  *   Michael Neuroth, 2004-05-16
46  *           INTERNET: michael.neuroth@freenet.de
47  *
48  * send your comments or suggestions to (gnuplot-info@lists.sourceforge.net).
49  *
50  */
51 #include "driver.h"
52
53 #define NO_QTOPIA_ENHANCED_SUPPORT
54
55 #ifdef TERM_REGISTER
56 register_term(qtopia_driver)
57 #endif
58
59 #ifdef TERM_PROTO
60 TERM_PUBLIC void QTOPIA_options __PROTO((void));
61 TERM_PUBLIC void QTOPIA_init __PROTO((void));
62 TERM_PUBLIC void QTOPIA_graphics __PROTO((void));
63 TERM_PUBLIC void QTOPIA_text __PROTO((void));
64 TERM_PUBLIC void QTOPIA_reset __PROTO((void));
65 TERM_PUBLIC void QTOPIA_linetype __PROTO((int linetype));
66 TERM_PUBLIC void QTOPIA_move __PROTO((unsigned int x, unsigned int y));
67 TERM_PUBLIC void QTOPIA_point __PROTO((unsigned int x, unsigned int y,
68                                      int point));
69 TERM_PUBLIC void QTOPIA_vector __PROTO((unsigned int x, unsigned int y));
70 TERM_PUBLIC void QTOPIA_put_text __PROTO((unsigned int x, unsigned int y,
71                                         const char *str));
72 TERM_PUBLIC void QTOPIA_arrow __PROTO((unsigned int sx, unsigned int sy,
73                                      unsigned int ex, unsigned int ey,
74                                      int head));
75
76 #define ENHqtopia_put_text NULL
77
78
79 #define QTOPIA_XMAX 4096
80 #define QTOPIA_YMAX 4096
81
82 #ifdef ZAURUS
83 #define QTOPIA_VCHAR (QTOPIA_YMAX/25)
84 #define QTOPIA_HCHAR (QTOPIA_XMAX/40)
85 #define QTOPIA_VTIC (QTOPIA_YMAX/50)
86 #define QTOPIA_HTIC (QTOPIA_XMAX/40)
87 #else
88 #define QTOPIA_VCHAR (QTOPIA_YMAX/25)
89 #define QTOPIA_HCHAR (QTOPIA_XMAX/50)
90 #define QTOPIA_VTIC (QTOPIA_YMAX/100)
91 #define QTOPIA_HTIC (QTOPIA_XMAX/150)
92 #endif
93
94 #endif /* TERM_PROTO */
95
96 #ifdef TERM_BODY
97
98 /*#include <winsock2.h>*/
99 /* needs: ws2_32.lib */
100 #ifndef _MSC_VER
101 #include <sys/types.h>
102 #include <sys/socket.h>
103 #include <netinet/in.h>
104 #include <arpa/inet.h>
105 #endif
106
107 #define QTOPIA_MAX_BUFFER               512
108 #define QTOPIA_MAX_DELAY_COUNT  20      /* * 100 ms = 2 s */
109
110 #ifdef _MSC_VER
111 #define QTOPIA_BAD_SOCKET               INVALID_SOCKET          /* -1 */
112 #define QTOPIA_BAD_CONNECT              SOCKET_ERROR
113 #else
114 #define QTOPIA_BAD_SOCKET               -1
115 #define QTOPIA_BAD_CONNECT              -1
116 #endif
117 #define QTOPIA_PORT_NO                  5050
118
119 static int qtopia_client_socket = QTOPIA_BAD_SOCKET;
120 static unsigned short qtopia_port_no = QTOPIA_PORT_NO;
121 static char qtopia_host_name[QTOPIA_MAX_BUFFER+1] = { "localhost" };
122
123 static int OpenClient __PROTO(( int test));
124 static void CloseClient();
125
126 static void MySleep( delay )
127 int delay;
128 {
129 #ifdef _MSC_VER
130         Sleep( delay );
131 #else
132         usleep( delay );
133 #endif
134 }
135
136 static void InitSockets()
137 {
138 #ifdef _MSC_VER
139         WORD wVersionRequested;
140         WSADATA wsaData;
141         int err;
142
143         wVersionRequested = MAKEWORD( 2, 2 );
144
145         err = WSAStartup( wVersionRequested, &wsaData );
146 #endif
147 }
148
149 static void ExitSockets()
150 {
151 #ifdef _MSC_VER
152         WSACleanup();
153 #endif
154 }
155
156 static int CheckForQtplot( count )
157 int count;
158 {
159         /* test (via sockets) if qtplot is allready running */
160         if( !OpenClient( 1 ) )
161         {
162                 /* give qtplot a litle bit time to start... */
163                 if( count < QTOPIA_MAX_DELAY_COUNT )
164                 {
165                         if( count == 0 )
166                         {
167 #ifdef _MSC_VER
168                                 system( "start qtplot" );
169 #else
170                                 system( "qtplot&" );
171 #endif
172                         }
173                 
174                         MySleep(100);
175
176                         return CheckForQtplot( count+1 );
177                 }
178                 else
179                 {
180                         return 0;
181                 }
182         }
183         else
184         {
185                 CloseClient();
186
187                 return 1;
188         }
189 }
190
191 static int OpenClient( test )
192 int test;
193 {
194     int len;
195     struct sockaddr_in address;
196     int result;
197
198         /*  Create a socket for the client.  */
199
200     qtopia_client_socket = socket(AF_INET, SOCK_STREAM, 0);
201
202         if( qtopia_client_socket != QTOPIA_BAD_SOCKET )
203         {
204                 /*  Name the socket, as agreed with the server.  */
205
206                 address.sin_family = AF_INET;
207                 address.sin_addr.s_addr = inet_addr(/*qtopia_host_name*/"127.0.0.1");   /* localhost */
208                 address.sin_port = htons(qtopia_port_no);
209                 len = sizeof(address);
210
211                 /*  Now connect our socket to the server's socket.  */
212
213                 result = connect(qtopia_client_socket, (struct sockaddr *)&address, len);
214                 
215                 if( result == QTOPIA_BAD_SOCKET )
216                 {
217                         /* mark this socket as bad */
218
219                         close( qtopia_client_socket );
220
221                         qtopia_client_socket = QTOPIA_BAD_SOCKET;
222
223                         if( !test )
224                         {
225                                 fprintf( gpoutfile, "error connecting to server !\n" );
226                         }
227                         return 0;       /* something went wrong */
228                 }
229         }
230         else
231         {
232                 if( !test )
233                 {
234                         fprintf( gpoutfile, "error creating socket !\n" );
235                 }
236                 return 0;       /* something went wrong */
237         }
238
239         return 1;       /* everything ist ok ! */
240 }
241
242 static void CloseClient()
243 {
244     close( qtopia_client_socket );
245 }
246
247 static int IsClientOk()
248 {
249         return qtopia_client_socket != QTOPIA_BAD_SOCKET;
250 }
251
252 static void SendDataToSocket( sLine )
253 const char * sLine;
254 {
255         if( IsClientOk() )
256         {
257                 int send_count;
258
259                 send_count = send( qtopia_client_socket, sLine, strlen( sLine ), 0 );
260
261                 if( send_count <= 0 )
262                 {
263                         fprintf( gpoutfile, "error writing to socket str=%s!\n", sLine );
264                 }
265                 /*
266                 else
267                 {
268                         fprintf( gpoutfile, "wrote %d bytes\n", send_count );
269                 }
270                 */
271         }
272         else
273         {
274                 /* for testing... */
275                 /*fprintf( gpoutfile, sLine );*/
276         }
277 }
278
279 /* ************************************************** */
280
281 enum QTOPIA_id { QTOPIA_PORT, QTOPIA_HOST, QTOPIA_OTHER };
282
283 static struct gen_table QTOPIA_opts[] =
284 {
285     { "po$rt", QTOPIA_PORT },
286     { "ho$st", QTOPIA_HOST },
287     { NULL, QTOPIA_OTHER }
288 };
289
290 TERM_PUBLIC void
291 QTOPIA_options()
292 {
293     /* this is not for the qtopia terminal ! */
294         /*SendDataToSocket( "qtd options\n" );*/
295
296     int x, y;
297     struct value a;
298
299     while (!END_OF_COMMAND) 
300         {
301                 switch(lookup_table(&QTOPIA_opts[0],c_token)) 
302                 {
303                         case QTOPIA_PORT:
304                                 c_token++;
305                                 if (END_OF_COMMAND)
306                                         int_error(c_token, "expecting port number");
307                                 qtopia_port_no = (int) real(const_express(&a));
308                                 break;
309                         case QTOPIA_HOST:
310                                 c_token++;
311                                 if (END_OF_COMMAND)
312                                         int_error(c_token, "expecting host name");
313                                 if (isstring(c_token))
314                                         quote_str( qtopia_host_name, c_token, sizeof(qtopia_host_name) );
315                                 else
316                                         copy_str( qtopia_host_name, c_token, sizeof(qtopia_host_name) );
317                                 c_token++;
318                                 break;
319                         case QTOPIA_OTHER:
320                         default:
321                                 break;
322                 }
323     }
324
325     sprintf(term_options, "host=%s port=%d",qtopia_host_name,qtopia_port_no);
326 }
327
328
329 TERM_PUBLIC void
330 QTOPIA_init()
331 {
332         /* initialize lib (if necassary) */
333         InitSockets();
334         CheckForQtplot( 0 );
335         OpenClient( 0 ); 
336
337         SendDataToSocket( "qtd init\n" );
338 }
339
340
341 TERM_PUBLIC void
342 QTOPIA_graphics()
343 {
344         SendDataToSocket( "qtd graphics\n" );
345 }
346
347 TERM_PUBLIC void
348 QTOPIA_text()
349 {
350         SendDataToSocket( "qtd text\n" );
351 }
352
353
354 TERM_PUBLIC void
355 QTOPIA_reset()
356 {
357         SendDataToSocket( "qtd reset\n" );
358
359         /* give the qtplot a litle bit time to shutdown */
360         MySleep(100);
361
362         CloseClient();
363         ExitSockets();
364 }
365
366
367 TERM_PUBLIC void
368 QTOPIA_linetype(linetype)
369 int linetype;
370 {
371         char sBuffer[QTOPIA_MAX_BUFFER];
372
373         sprintf( sBuffer, "qtd linetype type=%d\n",linetype );
374
375         SendDataToSocket( sBuffer );
376 }
377
378
379 TERM_PUBLIC void
380 QTOPIA_move(x, y)
381 unsigned int x, y;
382 {
383         char sBuffer[QTOPIA_MAX_BUFFER];
384
385         sprintf( sBuffer, "qtd move x=%d y=%d\n",x,y );
386
387         SendDataToSocket( sBuffer );
388 }
389
390
391 TERM_PUBLIC void
392 QTOPIA_point(x, y, point)
393 unsigned int x, y;
394 int point;
395 {
396         char sBuffer[QTOPIA_MAX_BUFFER];
397
398         sprintf( sBuffer, "qtd point x=%d y=%d point=%d\n",x,y,point );
399
400         SendDataToSocket( sBuffer );
401 }
402
403
404 TERM_PUBLIC void
405 QTOPIA_vector(_x, _y)
406 unsigned int _x, _y;
407 {
408         char sBuffer[QTOPIA_MAX_BUFFER];
409
410         sprintf( sBuffer, "qtd vector x=%d y=%d\n",_x,_y );
411
412         SendDataToSocket( sBuffer );
413 }
414
415
416 TERM_PUBLIC void
417 QTOPIA_put_text(x, y, str)
418 unsigned int x, y;
419 const char *str;
420 {
421         char sBuffer[QTOPIA_MAX_BUFFER];
422
423         sprintf( sBuffer, "qtd put_text x=%d y=%d str=%s\n",x,y,str );
424
425         SendDataToSocket( sBuffer );
426 }
427
428 /* not suported yet ! */
429 TERM_PUBLIC void
430 QTOPIA_arrow(sx, sy, ex, ey, head)
431     unsigned int sx, sy, ex, ey;
432     int head;           /* ignored */
433 {
434         char sBuffer[QTOPIA_MAX_BUFFER];
435
436         sprintf( sBuffer, "qtd arrow sx=%d sy=%d ex=%d ey=%d head=%d\n",sx,sy,ex,ey,head );
437
438         SendDataToSocket( sBuffer );
439 }
440
441
442 #endif /* TERM_BODY */
443
444 #ifdef TERM_TABLE
445 TERM_TABLE_START(qtopia_driver)
446     "qtopia", "qtopia or Qt",
447     QTOPIA_XMAX, QTOPIA_YMAX, QTOPIA_VCHAR, QTOPIA_HCHAR,
448     QTOPIA_VTIC, QTOPIA_HTIC, QTOPIA_options, QTOPIA_init, QTOPIA_reset,
449     QTOPIA_text, null_scale, QTOPIA_graphics, QTOPIA_move, QTOPIA_vector,
450     QTOPIA_linetype, QTOPIA_put_text, null_text_angle,
451     null_justify_text, QTOPIA_point, /*QTOPIA_arrow*/0, set_font_null,
452     0,                          /* pointsize */
453     TERM_CAN_MULTIPLOT,
454     NULL, NULL, NULL, NULL
455 #ifdef USE_MOUSE
456     , NULL, NULL, NULL, NULL, NULL
457 #endif
458 #ifdef PM3D
459     , NULL, NULL, NULL, NULL
460 #endif
461 TERM_TABLE_END(qtopia_driver)
462
463 #undef LAST_TERM
464 #define LAST_TERM qtopia_driver
465
466 #endif /* TERM_TABLE */
467
468 #ifdef TERM_HELP
469 START_HELP(qtopia)
470 "1 qtopia",
471 "?commands set terminal qtopia",
472 "?set terminal qtopia",
473 "?set term qtopia",
474 "?terminal qtopia",
475 "?term qtopia",
476 "?qtopia",
477 " The `qtopia` terminal driver has no additional options.",
478 "",
479 " Syntax:",
480 "       set terminal qtopia",
481 ""
482 END_HELP(qtopia)
483 #endif /* TERM_HELP */