Initial release of Maemo 5 port of gnuplot
[gnuplot] / term / multitos.trm
1 /* Hello, Emacs, this is -*-C-*-
2  * $Id: multitos.trm,v 1.15 2006/07/21 02:35:47 sfeam Exp $
3  */
4
5 /* GNUPLOT - multitos.trm */
6
7 /*[
8  * Copyright 1994, 1998, 2004
9  *
10  * Permission to use, copy, and distribute this software and its
11  * documentation for any purpose with or without fee is hereby granted,
12  * provided that the above copyright notice appear in all copies and
13  * that both that copyright notice and this permission notice appear
14  * in supporting documentation.
15  *
16  * Permission to modify the software is granted, but not the right to
17  * distribute the complete modified source code.  Modifications are to
18  * be distributed as patches to the released version.  Permission to
19  * distribute binaries produced by compiling modified sources is granted,
20  * provided you
21  *   1. distribute the corresponding source modifications from the
22  *    released version in the form of a patch file along with the binaries,
23  *   2. add special version identification to distinguish your version
24  *    in addition to the base release version number,
25  *   3. provide your name and address as the primary contact for the
26  *    support of your modified version, and
27  *   4. retain our contact information in regard to use of the base
28  *    software.
29  * Permission to distribute the released version of the source code along
30  * with corresponding source modifications in the form of a patch file is
31  * granted with same provisions 2 through 4 for binary distributions.
32  *
33  * This software is provided "as is" without express or implied warranty
34  * to the extent permitted by applicable law.
35 ]*/
36
37 /*
38  * This file is included by ../term.c.
39  *
40  * This terminal driver supports:
41  *   mtos : ATARI Systems MiNT/MULTITOS/MAGIC with external client
42  *
43  * AUTHOR
44  *  Dirk Stadler (email: dirk_stadler@n.maus.de, dirk@lstm.uni-erlangen.de)
45  *
46  * send your comments or suggestions to (gnuplot-info@lists.sourceforge.net).
47  *
48  */
49
50 #include "driver.h"
51
52 #ifdef TERM_REGISTER
53 register_term(mtos)
54 #endif
55
56 #ifdef TERM_PROTO
57
58 /* function-prototypes */
59 TERM_PUBLIC void MTOS_options(void);
60 TERM_PUBLIC void MTOS_init(void);
61 TERM_PUBLIC void MTOS_reset(void);
62 TERM_PUBLIC void MTOS_graphics(void);
63 TERM_PUBLIC void MTOS_text(void);
64 TERM_PUBLIC void MTOS_move(unsigned int x, unsigned int y);
65 TERM_PUBLIC void MTOS_vector(unsigned int x, unsigned int y);
66 TERM_PUBLIC void MTOS_linetype(int lt);
67 TERM_PUBLIC int MTOS_text_angle(int ang);
68 TERM_PUBLIC void MTOS_put_text(unsigned int x, unsigned int y, const char *str);
69 TERM_PUBLIC int MTOS_justify_text(enum JUSTIFY mode);
70 TERM_PUBLIC void MTOS_point(unsigned int x, unsigned int y, int number);
71
72 /* default to ST high resolution */
73 #define MTOS_XMAX  640
74 #define MTOS_YMAX  400
75 #define MTOS_VCHAR 18
76 #define MTOS_HCHAR 12
77 #define MTOS_HTIC  5
78 #define MTOS_VTIC  MTOS_HTIC
79
80 #define GOT_MTOS_PROTO
81 #endif /* TERM_PROTO */
82
83 #ifndef TERM_PROTO_ONLY
84 #ifdef TERM_BODY
85
86 #include <unistd.h>
87 #include <fcntl.h>
88 #include <param.h>
89 #include <process.h>
90 #include <signal.h>
91 #include <support.h>
92 #include <sys/stat.h>
93 #include <time.h>
94 #include <errno.h>
95 #include <aesbind.h>
96 #include <osbind.h>
97 #include <mintbind.h>
98
99 /* plot.c */
100 extern const char *user_gnuplotpath;
101
102 int MTOS_pause(char *str);
103 static void MTOS_quit(void);
104 void MTOS_open_pipe(void);
105 static void write_pid(void);
106 static void init_exit(void);
107
108 /* commands for GPCLIENT */
109 #define SET_GRAPHICS    'G'
110 #define SET_TEXT        'E'
111 #define SET_LINE        'L'
112 #define SET_ANGLE       'A'
113 #define SET_JUSTIFY     'J'
114 #define SET_POINTMODE   'D'
115 #define GR_MOVE         'M'
116 #define GR_DRAW         'V'
117 #define GR_RESET        'R'
118 #define GR_TEXT         'T'
119
120 #define PAUSE           'P'
121 #define PID             'I'
122 #define QUIT            'Q'
123
124 /* for creating pipes */
125 #define PIPE1           "u:\\pipe\\GPLT%d"
126 #define PIPE2           "u:\\pipe\\gpclient.tmp"
127 #define NAMESIZE        25
128 #define SIZE            ((unsigned)sizeof(short))
129
130 /* default name for client */
131 #define GPCLIENT1       "gpclient.prg"
132 #define GPCLIENT2       "gpclient"
133
134 /* environment */
135 #define PATH           "PATH"
136
137 /* some global variables */
138 static char MTOS_mode = 0;
139 static int handle = -1;
140 static int pid = -1;
141
142 TERM_PUBLIC void
143 MTOS_options()
144 {
145     term_options[0] = NUL;
146 }
147
148 TERM_PUBLIC void
149 MTOS_init()
150 {
151     char pipe[NAMESIZE];
152     char *file, cmd[MAXPATHLEN+1];
153     char const *const ext[] = { "prg", "app", NULL };
154
155     if (handle < 0) {
156         if (aesid < 0) {
157             if ((aesid = appl_init()) < 0)
158                 int_error(NO_CARET, "APPL_INIT failed !");
159         }
160         file = findfile(GPCLIENT2, user_gnuplotpath, ext);
161         if (!file)
162             safe_strncpy(cmd, GPCLIENT1, sizeof(cmd));
163         else if (file && !strchr(file, '\\') && !strchr(file, '/'))
164             safe_strncpy(cmd, file, sizeof(cmd));
165         else
166             unx2dos(file, cmd);
167         if (!shel_find(cmd))
168             int_error(NO_CARET, "Cannot find GPCLIENT !");
169
170         sprintf(&pipe[1], PIPE1, aesid);
171
172         if ((handle = open(&pipe[1], O_RDWR | O_CREAT)) < 0) {
173             if ((handle = (int) Fcreate(&pipe[1], 0)) < 0)
174                 int_error(NO_CARET, "Cannot open PIPE to GPCLIENT !");
175         }
176         pipe[0] = (char) (strlen(&pipe[1]) + 1);
177
178         if (!shel_write(1, 1, 100, cmd, pipe)) {
179             close(handle);
180             handle = -1;
181             int_error(NO_CARET, "Cannot spawn GPCLIENT !");
182         }
183         init_exit();
184     }
185     if (aesid > -1)
186         menu_register(aesid, "  Terminal: mtos");
187 }
188
189 TERM_PUBLIC void
190 MTOS_reset()
191 {
192     short buff;
193
194     buff = (short) GR_RESET;
195     write(handle, &buff, SIZE);
196 }
197
198 TERM_PUBLIC void
199 MTOS_text()
200 {
201     short buff;
202
203     buff = (short) SET_TEXT;
204     if (MTOS_mode != SET_TEXT)
205         write(handle, &buff, SIZE);
206     MTOS_mode = SET_TEXT;
207 }
208
209 TERM_PUBLIC void
210 MTOS_graphics()
211 {
212     short buff;
213
214     buff = (short) SET_GRAPHICS;
215     write(handle, &buff, SIZE);
216     MTOS_mode = SET_GRAPHICS;
217 }
218
219 TERM_PUBLIC void
220 MTOS_move(unsigned int x, unsigned int y)
221 {
222     short x_1, y_1, buff;
223
224     x_1 = (short) x;
225     y_1 = (short) y;
226     buff = (short) GR_MOVE;
227     write(handle, &buff, SIZE);
228     write(handle, &x_1, SIZE);
229     write(handle, &y_1, SIZE);
230 }
231
232 TERM_PUBLIC void
233 MTOS_vector(unsigned int x, unsigned int y)
234 {
235     short x_1, y_1, buff;
236
237     x_1 = (short) x;
238     y_1 = (short) y;
239     buff = (short) GR_DRAW;
240     write(handle, &buff, SIZE);
241     write(handle, &x_1, SIZE);
242     write(handle, &y_1, SIZE);
243 }
244
245 TERM_PUBLIC void
246 MTOS_linetype(int lt)
247 {
248     short lt_1, buff;
249
250     lt_1 = (short) lt;
251     buff = (short) SET_LINE;
252     write(handle, &buff, SIZE);
253     write(handle, &lt_1, SIZE);
254 }
255
256 TERM_PUBLIC int
257 MTOS_text_angle(int ang)
258 {
259     short ta_1, buff;
260
261     ta_1 = (short) (ang ? 1 : 0);
262     buff = (short) SET_ANGLE;
263     write(handle, &buff, SIZE);
264     write(handle, &ta_1, SIZE);
265     return (TRUE);
266 }
267
268 TERM_PUBLIC void
269 MTOS_put_text(unsigned int x, unsigned int y, const char *str)
270 {
271     short x_1, y_1, len, buff;
272
273     x_1 = (short) x;
274     y_1 = (short) y;
275     len = (short) strlen(str) + 1;
276     buff = (short) GR_TEXT;
277     write(handle, &buff, SIZE);
278     write(handle, &x_1, SIZE);
279     write(handle, &y_1, SIZE);
280     write(handle, &len, SIZE);
281     write(handle, str, (unsigned) len);
282 }
283
284 TERM_PUBLIC int
285 MTOS_justify_text(enum JUSTIFY mode)
286 {
287     short j_mode, buff;
288
289     j_mode = (short) mode;
290     buff = (short) SET_JUSTIFY;
291     write(handle, &buff, SIZE);
292     write(handle, &j_mode, SIZE);
293     return (TRUE);
294 }
295
296 TERM_PUBLIC void
297 MTOS_point(unsigned int x, unsigned int y, int number)
298 {
299     short mode, buff;
300
301     buff = (short) SET_POINTMODE;
302     mode = 1;
303     write(handle, &buff, SIZE);
304     write(handle, &mode, SIZE);
305     do_point(x, y, number);
306     mode = 0;
307     write(handle, &buff, SIZE);
308     write(handle, &mode, SIZE);
309 }
310
311 int
312 MTOS_pause(char *str)
313 {
314     short len, buff;
315
316     len = (short) strlen(str) + 1;
317     buff = (short) PAUSE;
318     write(handle, &buff, SIZE);
319     write(handle, &len, SIZE);
320     write(handle, str, (unsigned) len);
321     read(handle, &len, SIZE);
322     return ((int) len);
323 }
324
325 static void
326 MTOS_quit()
327 {
328     short buff;
329
330     if (pid > -1)
331         kill(pid, SIGTERM);
332     if (handle > -1) {
333         buff = (short) QUIT;
334         write(handle, &buff, SIZE);
335         close(handle);
336     }
337 }
338
339 void
340 MTOS_open_pipe()
341 {
342     char pipe[NAMESIZE];
343     short len;
344
345     if (handle < 0) {
346         if ((handle = open(PIPE2, O_RDWR)) < 0)
347             return;
348         read(handle, &len, SIZE);
349         if (len > 0) {
350             read(handle, pipe, (unsigned) len);
351             close(handle);
352             if ((handle = open(pipe, O_RDWR)) < 0) {
353                 fprintf(stderr,
354                         "\n\n\33p Can't open Pipe: (%s) Error: (%s) !\33q\n\n",
355                         pipe,sys_errlist[errno]);
356                 fflush(stderr);
357                 return;
358             }
359         }
360         init_exit();
361     }
362 }
363
364 static void
365 write_pid()
366 {
367     short buff, mypid, gpclpid;
368
369     mypid = (short) getpid();
370     buff = (short) PID;
371     while (write(handle, &buff, SIZE) <= 0)
372         Fselect(100, 0L, 0L, 0L);
373     while (read(handle, &gpclpid, SIZE) <= 0)
374         Fselect(100, 0L, 0L, 0L);
375     write(handle, &mypid, SIZE);
376     pid = (int) gpclpid;
377 }
378
379 static void
380 init_exit()
381 {
382     Cconout(7);
383     write_pid();
384     atexit(MTOS_quit);
385 }
386
387 #endif /* TERM_BODY */
388
389 #ifdef TERM_TABLE
390
391 TERM_TABLE_START(mtos_driver)
392     "mtos", "Atari MiNT/MULTITOS/Magic Terminal",
393     MTOS_XMAX, MTOS_YMAX, MTOS_VCHAR, MTOS_HCHAR,
394     MTOS_VTIC, MTOS_HTIC, MTOS_options, MTOS_init, MTOS_reset,
395     MTOS_text, null_scale, MTOS_graphics, MTOS_move, MTOS_vector,
396     MTOS_linetype, MTOS_put_text, MTOS_text_angle,
397     MTOS_justify_text, MTOS_point, do_arrow, set_font_null,
398     0, TERM_CAN_MULTIPLOT, 0, 0
399 TERM_TABLE_END(mtos_driver)
400
401 #undef LAST_TERM
402 #define LAST_TERM mtos_driver
403
404 #endif /* TERM_TABLE */
405
406 #endif /* TERM_PROTO_ONLY */
407
408 #ifdef TERM_HELP
409 START_HELP(mtos)
410 "1 mtos",
411 "?commands set terminal mtos",
412 "?set terminal mtos",
413 "?set term mtos",
414 "?terminal mtos",
415 "?term mtos",
416 "?mtos",
417 " The `mtos` terminal has no options.  It sends data via a pipe to an external",
418 " program called GPCLIENT.  It runs under MULTITOS, Magic 3.x, MagicMAC. and",
419 " MiNT.  If you cannot find GPCLIENT, than mail to dirk@lstm.uni-erlangen.de."
420 END_HELP(mtos)
421 #endif /* TERM_HELP */