2
/****************************************************************************
5
* AUTHOR(S): Glynn Clements <glynn gclements.plus.com> (original contributor)
6
* Jachym Cepicky <jachym les-ejk.cz>
8
* COPYRIGHT: (C) 2006-2007 by the GRASS Development Team
10
* This program is free software under the GNU General Public
11
* License (>=v2). Read the file COPYING that comes with GRASS
14
*****************************************************************************/
23
#include <sys/types.h>
25
#include <grass/gis.h>
26
#include <grass/graphics.h>
27
#include "driverlib.h"
30
#include <grass/glocale.h>
32
#define REC(a,b) if ((ateof=rec((a),(b)))) break
33
#define RECTEXT(x,s) if ((ateof=rectext(&x,&s))) break
34
#define SEND(a,b) _send((a),(b))
35
#define SENDTEXT(x) sendtext((x))
37
static int rec(void *, int);
38
static int rectext(char **, int *);
39
static int _send(const void *, int);
40
static int sendtext(const char *);
41
static int RESULT(int);
46
static char inbuf[4096];
50
static char current_command;
54
static PAD *curpad; /* current selected pad */
56
static unsigned char *blua;
57
static unsigned char *grna;
58
static unsigned char *reda;
59
static unsigned char *nula;
72
static void *xalloc(void *buf, int *cur, int new, int len)
75
buf = G_realloc(buf, (size_t) new * len);
82
void command_init(int rfd, int wfd)
93
int LIB_command_get_input(void)
98
static void send_fonts(void (*func) (char ***, int *))
104
(*func) (&fonts, &num_fonts);
105
SEND(&num_fonts, sizeof num_fonts);
106
for (i = 0; i < num_fonts; i++)
108
free_font_list(fonts, num_fonts);
111
int process_command(int c)
114
static int name_size;
116
static int text_size;
120
unsigned char red, grn, blu;
129
int src[2][2], dst[2][2];
134
for (index = -10; index < BEGIN_SYNC_COUNT; index++)
144
COM_Number_of_colors(&index);
145
SEND(&index, sizeof index);
148
REC(&index, sizeof index);
149
COM_Standard_color(index);
152
REC(&red, sizeof red);
153
REC(&grn, sizeof grn);
154
REC(&blu, sizeof blu);
155
COM_Color_RGB(red, grn, blu);
158
REC(&number, sizeof number);
159
COM_Line_width(number);
176
COM_Box_abs(l, t, r, b);
186
case GET_LOCATION_WITH_BOX:
191
COM_Get_location_with_box(t, b, &x, &y, &button);
194
SEND(&button, sizeof button);
196
case GET_LOCATION_WITH_LINE:
201
COM_Get_location_with_line(t, b, &x, &y, &button);
204
SEND(&button, sizeof button);
206
case GET_LOCATION_WITH_POINTER:
209
REC(&button, sizeof button);
210
COM_Get_location_with_pointer(&x, &y, &button);
213
SEND(&button, sizeof button);
231
REC(&index, sizeof index);
233
(unsigned char *)xalloc(blua, &blu_alloc, x * y, sizeof(*blua));
234
REC(blua, x * y * sizeof(char));
235
COM_Bitmap(x, y, index, blua);
237
case BEGIN_SCALED_RASTER:
238
REC(&index, sizeof(int));
239
REC(&src[0][0], 4 * sizeof(int));
240
REC(&dst[0][0], 4 * sizeof(int));
241
COM_begin_scaled_raster(index, src, dst);
246
reda = (unsigned char *)xalloc(reda, &red_alloc, x, sizeof(*reda));
247
grna = (unsigned char *)xalloc(grna, &grn_alloc, x, sizeof(*grna));
248
blua = (unsigned char *)xalloc(blua, &blu_alloc, x, sizeof(*blua));
249
nula = (unsigned char *)xalloc(nula, &nul_alloc, x, sizeof(*nula));
250
REC(reda, x * sizeof(char));
251
REC(grna, x * sizeof(char));
252
REC(blua, x * sizeof(char));
253
REC(nula, x * sizeof(char));
255
ret = COM_scaled_raster(x, y, reda, grna, blua, t ? nula : NULL);
256
SEND(&ret, sizeof ret);
258
case END_SCALED_RASTER:
259
COM_end_scaled_raster();
262
REC(&number, sizeof number);
263
xarray = (int *)xalloc(xarray, &n_xarray, number, sizeof(*xarray));
264
yarray = (int *)xalloc(yarray, &n_yarray, number, sizeof(*yarray));
265
REC(xarray, number * sizeof(xarray[0]));
266
REC(yarray, number * sizeof(yarray[0]));
267
COM_Polygon_abs(xarray, yarray, number);
270
REC(&number, sizeof number);
271
xarray = (int *)xalloc(xarray, &n_xarray, number, sizeof(*xarray));
272
yarray = (int *)xalloc(yarray, &n_yarray, number, sizeof(*yarray));
273
REC(xarray, number * sizeof(xarray[0]));
274
REC(yarray, number * sizeof(yarray[0]));
275
COM_Polygon_rel(xarray, yarray, number);
278
REC(&number, sizeof number);
279
xarray = (int *)xalloc(xarray, &n_xarray, number, sizeof(*xarray));
280
yarray = (int *)xalloc(yarray, &n_yarray, number, sizeof(*yarray));
281
REC(xarray, number * sizeof(xarray[0]));
282
REC(yarray, number * sizeof(yarray[0]));
283
COM_Polyline_abs(xarray, yarray, number);
286
REC(&number, sizeof number);
287
xarray = (int *)xalloc(xarray, &n_xarray, number, sizeof(*xarray));
288
yarray = (int *)xalloc(yarray, &n_yarray, number, sizeof(*yarray));
289
REC(xarray, number * sizeof(xarray[0]));
290
REC(yarray, number * sizeof(yarray[0]));
291
COM_Polyline_rel(xarray, yarray, number);
294
REC(&number, sizeof number);
295
xarray = (int *)xalloc(xarray, &n_xarray, number, sizeof(*xarray));
296
yarray = (int *)xalloc(yarray, &n_yarray, number, sizeof(*yarray));
297
REC(xarray, number * sizeof(xarray[0]));
298
REC(yarray, number * sizeof(yarray[0]));
299
COM_Polydots_abs(xarray, yarray, number);
302
REC(&number, sizeof number);
303
xarray = (int *)xalloc(xarray, &n_xarray, number, sizeof(*xarray));
304
yarray = (int *)xalloc(yarray, &n_yarray, number, sizeof(*yarray));
305
REC(xarray, number * sizeof(xarray[0]));
306
REC(yarray, number * sizeof(yarray[0]));
307
COM_Polydots_rel(xarray, yarray, number);
310
COM_Screen_left(&index);
311
SEND(&index, sizeof index);
314
COM_Screen_rite(&index);
315
SEND(&index, sizeof index);
318
COM_Screen_bot(&index);
319
SEND(&index, sizeof index);
322
COM_Screen_top(&index);
323
SEND(&index, sizeof index);
330
COM_Set_window(t, b, l, r);
333
RECTEXT(text, text_size);
334
COM_Get_text_box(text, &t, &b, &l, &r);
341
RECTEXT(text, text_size);
345
RECTEXT(text, text_size);
346
COM_Font_init_charset(text);
349
send_fonts(COM_Font_list);
352
send_fonts(COM_Font_info);
355
RECTEXT(text, text_size);
365
COM_Text_rotation(wx);
368
RECTEXT(text, text_size);
373
COM_Panel_save(text, t, b, l, r);
376
RECTEXT(text, text_size);
377
COM_Panel_restore(text);
380
RECTEXT(text, text_size);
381
COM_Panel_delete(text);
384
RECTEXT(text, text_size);
385
if (*text == 0) /* this is scratch pad */
387
else if (find_pad(text) != NULL)
388
RESULT(DUPLICATE); /* duplicate pad */
389
else if (create_pad(text))
396
if (curpad == NULL) {
402
SENDTEXT(curpad->name);
409
else if (*curpad->name == 0)
424
for (pad = pad_list(); pad != NULL; pad = pad->next)
431
RECTEXT(text, text_size); /* pad name */
432
curpad = find_pad(text);
440
RECTEXT(text, text_size); /* item name */
441
if (curpad == NULL) {
445
item = find_item(curpad, text);
451
for (list = item->list; list != NULL; list = list->next)
453
SENDTEXT(list->value);
458
RECTEXT(name, name_size); /* item name */
459
RECTEXT(text, text_size); /* item value */
460
if (curpad == NULL) {
464
delete_item(curpad, name);
465
if (append_item(curpad, name, text, 0))
471
case PAD_APPEND_ITEM:
472
RECTEXT(name, name_size); /* item name */
473
RECTEXT(text, text_size); /* item value */
474
REC(&index, sizeof index); /* replace flag */
475
if (curpad == NULL) {
479
if (append_item(curpad, name, text, index))
485
case PAD_DELETE_ITEM:
486
RECTEXT(text, text_size); /* item name */
487
if (curpad == NULL) {
491
delete_item(curpad, text);
496
if (curpad == NULL) {
501
for (item = curpad->items; item != NULL; item = item->next)
503
SENDTEXT(item->name);
508
G_warning(_("Unknown command: %d last: %d"), c, lc);
516
static int read1(char *c)
518
if (atbuf == n_read) {
520
n_read = read(_rfd, inbuf, sizeof inbuf);
522
perror("Monitor: read1: Error reading input");
530
int get_command(char *c)
532
/* is there a command char pending? */
533
if ((*c = current_command)) {
539
* look for 1 (or more) COMMAND_ESC chars
540
* followed by a non-zero comamnd token char
542
while (read1(c) == 0) { /* while !EOF */
543
if (*c != COMMAND_ESC)
545
while (*c == COMMAND_ESC)
547
G_warning(_("Monitor: get_command: Premature EOF"));
551
return 0; /* got the command token */
556
static int get1(char *c)
560
if (*c != COMMAND_ESC)
565
current_command = *c;
566
return -1; /* Got command within data */
568
*c = COMMAND_ESC; /* sequence COMMAND_ESC,0 becomes data COMMAND_ESC */
572
static int rec(void *buf, int n)
578
if ((stat = get1(cbuf++)) != 0)
579
return stat; /* EOF or COMMAND_ESC */
584
static int rectext(char **buff_p, int *size_p)
586
char *buff = *buff_p;
595
return stat; /* EOF or COMMAND_ESC */
598
*size_p = size = size ? size * 2 : 1000;
599
*buff_p = buff = G_realloc(buff, size);
609
static int _send(const void *buf, int n)
611
int r = write(_wfd, buf, n);
614
perror("Monitor: _send: write");
618
G_warning("Monitor: _send: write returned short count: %d of %d",
625
static int sendtext(const char *s)
627
SEND(s, strlen(s) + 1);
631
static int RESULT(int n)