File: ugd.c

package info (click to toggle)
ug 3.11.0-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 15,228 kB
  • sloc: ansic: 217,704; cpp: 25,559; perl: 10,364; sh: 1,699; makefile: 722; csh: 463; yacc: 408; awk: 347; lex: 154; fortran: 58
file content (634 lines) | stat: -rw-r--r-- 15,618 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
/****************************************************************************/
/*                                                                          */
/* File:      ugd.c                                                         */
/*                                                                          */
/* Purpose:   main function and routines for ug-daemon                      */
/*                                                                          */
/* Author:    Klaus Birken                                                  */
/*            Institut fuer Computeranwendungen III                         */
/*            Universitaet Stuttgart                                        */
/*            Pfaffenwaldring 27                                            */
/*            70569 Stuttgart                                               */
/*            internet: birken@ica3.uni-stuttgart.de                        */
/*                                                                          */
/* History:   960820 kb  begin                                              */
/*                                                                          */
/* Remarks:                                                                 */
/*            there are some TODOs:                                         */
/*                                                                          */
/*            little/big endian conversion isn't implemented yet.           */
/*            the socket interface is weak, rework abstraction.             */
/*                                                                          */
/*                                                                          */
/****************************************************************************/


#ifdef RIF_SOCKETS  /* remote if must be enabled in ug.conf */

/****************************************************************************/
/*                                                                          */
/* auto include mechanism and other include files                           */
/*                                                                          */
/****************************************************************************/

#include "config.h"
#include <stdio.h>

/* low module */
#include "initlow.h"
#include "misc.h"
#include "general.h"
#include "defaults.h"
#include "ugtypes.h"
#include "ugdevices.h"
#include "sockcomm.h"

#include "debug.h"

/****************************************************************************/


#define MAXLINE 1024



/****************************************************************************/


/* RCS string */
static char RCS_ID("$Header$",UG_RCS_STRING);


static char buf[MAXLINE];

static OUTPUTDEVICE *theOutputDevice;


/****************************************************************************/
/*                                                                          */
/* subroutines                                                              */
/*                                                                          */
/****************************************************************************/


static INT InitUgDaemon (int argc, char **argv)
{
  INT err;

  /* init the low module */
  if ((err=InitLow())!=0)
  {
    fprintf(stderr, "ugd: ERROR in InitUgDaemon while InitLow (line %d): called routine line %d\n",
            (int) HiWrd(err), (int) LoWrd(err));
    printf ("aborting ugd\n");

    return (1);
  }


  /* init the devices module */
  if ((err=InitDevices(&argc,argv))!=0)
  {
    fprintf(stderr, "ugd: ERROR in InitUgDaemon while InitDevices (line %d): called routine line %d\n",
            (int) HiWrd(err), (int) LoWrd(err));
    printf ("aborting ugd\n");

    return (1);
  }

  return (0);
}



/****************************************************************************/

static void ExecInitScreen (int sockfd)
{
  INT len;
  int i, n, argc, error;
  char **argv;

  n = read(sockfd, (char *)&argc, sizeof(int));
  argv = (char **) malloc(sizeof(char *) * argc);

  for(i=0; i<argc; i++)
  {
    len  = SocketReadINT(sockfd);
    argv[i] = (char *) malloc(len+1);
    n = SocketRead(sockfd, argv[i], len);
    argv[i][len] = 0;
  }

  /* TODO. speicherverwaltung */

  InitUgDaemon(argc,argv);

  theOutputDevice = CreateOutputDevice("screen");
  if ( theOutputDevice==NULL )    {
    printf("Couldn't create screen device\n");
    exit(-1);
  }

  return;
}


static void ExecOpenOutput (int sockfd)
{
  INT len;
  INT x, y, width, height;
  INT Global_LL[2], Global_UR[2], Local_LL[2], Local_UR[2];
  INT error;
  WINDOWID winID;

  x = SocketReadINT(sockfd);
  y = SocketReadINT(sockfd);
  width = SocketReadINT(sockfd);
  height = SocketReadINT(sockfd);

  len = SocketReadINT(sockfd);
  SocketRead(sockfd, buf, len);
  buf[len] = 0;


  winID = (*theOutputDevice->OpenOutput)(buf, 0, x, y, width, height,
                                         Global_LL, Global_UR, Local_LL, Local_UR, &error);

  SocketWriteINTN(sockfd, Global_LL, 2);
  SocketWriteINTN(sockfd, Global_UR, 2);
  SocketWriteINTN(sockfd, Local_LL, 2);
  SocketWriteINTN(sockfd, Local_UR, 2);
  SocketWriteINT(sockfd, error);
  SocketWriteINT(sockfd, winID);
}



#define SKIP_NO_EVENTS   5

static void ExecGetNextUGEvent (int sockfd)
{
  EVENT ev;
  INT emask;
  int i;

  emask = SocketReadINT(sockfd);

  /* skip up to SKIP_NO_EVENTS NO_EVENTs */
  i = SKIP_NO_EVENTS;
  do
  {
    i--;
    GetNextUGEvent(&ev, emask);
  } while (i>0 && EVENT_TYPE(ev)==NO_EVENT);

  switch (EVENT_TYPE(ev))
  {
  case NO_EVENT :
    /*
       printf("event NO_EVENT\n");
     */
    SocketWriteData(sockfd, (char *)&ev, sizeof(NO_UGEVENT));
    break;

  case TERM_GOAWAY :
  case TERM_CMDKEY :
  case DOC_GOAWAY :
  case DOC_ACTIVATE :
  case DOC_DRAG :
  case DOC_GROW :
  case DOC_CHANGETOOL :
  case DOC_CONTENTCLICK :
  case DOC_UPDATE :
    /*
       printf("event %d\n", EVENT_TYPE(ev));
     */
    SocketWriteData(sockfd, (char *)&ev, sizeof(EVENT));
    break;

  case TERM_STRING :
    /*
       printf("event STRING %s\n", ev.TermString.String);
     */
    SocketWriteData(sockfd, (char *)&ev, sizeof(TERM_STRING_EVENT));
    break;

  default :
    fprintf(stderr, "ugd: unknown UGEvent type=%d!\n", ev.Type);
  }
}



/****************************************************************************/


static void ug_frontend (int sockfd)
{
  int n, cmd=-1;
  INT len;
  WINDOWID win;


  for (;;)
  {
                #ifdef DebugSockets
    {
      int info;
      n = read(sockfd, (char *)&info, sizeof(int));

      if (info!=MAGIC_COOKIE)
      {
        fprintf(stderr, "ugd: invalid cookie for cmd=#%d in debugmode!\n",
                cmd);
        fflush(stdout);
      }
    }
                #endif


    n = read(sockfd, (char *)&cmd, sizeof(int));
    if (n==0)
    {
      return;
    }

    else if (n<0)
    {
      fprintf(stderr, "ugd: ERROR in readline.\n");
      exit(1);
    }

    /*
       printf("cmd %s/%d\n", cmd_text[cmd], cmd);
       fflush(stdout);
     */

    switch (cmd)
    {
    case DC_InitScreen :
      ExecInitScreen(sockfd);
      break;

    case DC_WriteString :
      len = SocketReadINT(sockfd);
      n = SocketRead(sockfd, buf, len);
      buf[len] = 0;
      WriteString(buf);
      break;

    case DC_MousePosition :
    {
      INT pos[2];
      MousePosition(pos);
      SocketWriteINTN(sockfd, pos, 2);
      break;
    }

    case DC_MouseStillDown :
      SocketWriteINT(sockfd, MouseStillDown());
      break;

    case DC_GetNextUGEvent :
      ExecGetNextUGEvent(sockfd);
      break;

    case DC_InitRemotePort :
      SocketWriteLong(sockfd, theOutputDevice->black);
      SocketWriteLong(sockfd, theOutputDevice->white);
      SocketWriteLong(sockfd, theOutputDevice->red);
      SocketWriteLong(sockfd, theOutputDevice->green);
      SocketWriteLong(sockfd, theOutputDevice->blue);
      SocketWriteLong(sockfd, theOutputDevice->cyan);
      SocketWriteLong(sockfd, theOutputDevice->orange);
      SocketWriteLong(sockfd, theOutputDevice->yellow);
      SocketWriteLong(sockfd, theOutputDevice->darkyellow);
      SocketWriteLong(sockfd, theOutputDevice->magenta);
      SocketWriteINT(sockfd, theOutputDevice->hasPalette);
      SocketWriteLong(sockfd, theOutputDevice->spectrumStart);
      SocketWriteLong(sockfd, theOutputDevice->spectrumEnd);
      SocketWriteINT(sockfd, theOutputDevice->signx);
      SocketWriteINT(sockfd, theOutputDevice->signy);

      /*
                                      SocketWrite(sockfd, (char *)theOutputDevice, sizeof(OUTPUTDEVICE));
       */
      break;

    case DC_OpenOutput :
      ExecOpenOutput(sockfd);
      break;

    case DC_CloseOutput :
      win = SocketReadINT(sockfd);
      (*theOutputDevice->CloseOutput)(win);
      break;

    case DC_ActivateOutput :
      win = SocketReadINT(sockfd);
      (*theOutputDevice->ActivateOutput)(win);
      break;

    case DC_UpdateOutput :
    {
      INT tool;
      win = SocketReadINT(sockfd);
      tool = SocketReadINT(sockfd);

      (*theOutputDevice->UpdateOutput)(win,tool);
      break;
    }

    case DC_Move :
    {
      SHORT_POINT p;
      p.x = SocketReadINT(sockfd);
      p.y = SocketReadINT(sockfd);
      (*theOutputDevice->Move)(p);
      break;
    }

    case DC_Draw :
    {
      SHORT_POINT p;
      p.x = SocketReadINT(sockfd);
      p.y = SocketReadINT(sockfd);
      (*theOutputDevice->Draw)(p);
      break;
    }


    case DC_Polyline :
    {
      INT np = SocketReadINT(sockfd);
      SocketRead(sockfd, buf, sizeof(SHORT_POINT)*np);
      (*theOutputDevice->Polyline)((SHORT_POINT *)buf, np);
      break;
    }

    case DC_InversePolyline :
    {
      INT np = SocketReadINT(sockfd);
      SocketRead(sockfd, buf, sizeof(SHORT_POINT)*np);
      (*theOutputDevice->InversePolyline)((SHORT_POINT *)buf, np);
      break;
    }

    case DC_Polygon :
    {
      INT np = SocketReadINT(sockfd);
      SocketRead(sockfd, buf, sizeof(SHORT_POINT)*np);
      (*theOutputDevice->Polygon)((SHORT_POINT *)buf, np);
      break;
    }

    case DC_InversePolygon :
    {
      INT np = SocketReadINT(sockfd);
      SocketRead(sockfd, buf, sizeof(SHORT_POINT)*np);
      (*theOutputDevice->InversePolygon)((SHORT_POINT *)buf, np);
      break;
    }

    case DC_ErasePolygon :
    {
      INT np = SocketReadINT(sockfd);
      SocketRead(sockfd, buf, sizeof(SHORT_POINT)*np);
      (*theOutputDevice->ErasePolygon)((SHORT_POINT *)buf, np);
      break;
    }

    case DC_Polymark :
    {
      INT np = SocketReadINT(sockfd);
      SocketRead(sockfd, buf, sizeof(SHORT_POINT)*np);
      (*theOutputDevice->Polymark)(np, (SHORT_POINT *)buf);
      break;
    }

    case DC_InvPolymark :
    {
      INT np = SocketReadINT(sockfd);
      SocketRead(sockfd, buf, sizeof(SHORT_POINT)*np);
      (*theOutputDevice->InvPolymark)(np, (SHORT_POINT *)buf);
      break;
    }

    case DC_DrawText :
    {
      INT mode = SocketReadINT(sockfd);
      len = SocketReadINT(sockfd);
      n = SocketRead(sockfd, buf, len);
      buf[len] = 0;

      (*theOutputDevice->DrawText)(buf, mode);
      break;
    }

    case DC_CenteredText :
    {
      INT mode;
      SHORT_POINT p;
      p.x = SocketReadINT(sockfd);
      p.y = SocketReadINT(sockfd);
      mode = SocketReadINT(sockfd);

      len = SocketReadINT(sockfd);
      n = SocketRead(sockfd, buf, len);
      buf[len] = 0;

      (*theOutputDevice->CenteredText)(p, buf, mode);
      break;
    }

    case DC_ClearViewPort :
    {
      (*theOutputDevice->ClearViewPort)();
      break;
    }

    case DC_SetLineWidth :
    {
      short s = SocketReadINT(sockfd);
      (*theOutputDevice->SetLineWidth)(s);
      break;
    }

    case DC_SetTextSize :
    {
      short s = SocketReadINT(sockfd);
      (*theOutputDevice->SetTextSize)(s);
      break;
    }

    case DC_SetMarkerSize :
    {
      short s = SocketReadINT(sockfd);
      (*theOutputDevice->SetMarkerSize)(s);
      break;
    }

    case DC_SetMarker :
    {
      short s = SocketReadINT(sockfd);
      (*theOutputDevice->SetMarker)(s);
      break;
    }

    case DC_SetColor :
    {
      long v = SocketReadLong(sockfd);
      (*theOutputDevice->SetColor)(v);
      break;
    }

    case DC_SetPaletteEntry :
    {
      long index;
      short r, g, b;

      index = SocketReadLong(sockfd);
      r = SocketReadINT(sockfd);
      g = SocketReadINT(sockfd);
      b = SocketReadINT(sockfd);
      (*theOutputDevice->SetPaletteEntry)(index, r, g, b);

    }

    /* TODO andere Palette-funktionen! */


    case DC_Flush :
      (*theOutputDevice->Flush)();
      break;


    default :
      fprintf(stderr, "ugd: unknown command %s (#%d)!\n", cmd_text[cmd], cmd);
      fflush(stdout);
      break;
    }
  }
}


/****************************************************************************/

INT main (int argc, char **argv)
{
  int sockfd, newsockfd, clilen, childpid;
  struct sockaddr_in cli_addr, serv_addr;
  int port;
  int nodelay_flag, optlen=sizeof(nodelay_flag);


  /* open internet TCP stream socket */
  if ((sockfd=socket(AF_INET, SOCK_STREAM, 0)) < 0)
  {
    fprintf(stderr, "ugd: can't open TCP socket.\n");
    exit(1);
  }
  getsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&nodelay_flag,&optlen);
  printf("TCP_NODELAY=%d\n");


  /* optional first argument is local port number */
  if (argc>1)
  {
    port = atoi(argv[1]);
  }
  else
  {
    port = SERV_TCP_PORT_DEFAULT;
  }

  if (port<SERV_TCP_PORT_MIN || port>SERV_TCP_PORT_MAX)
  {
    fprintf(stderr,"%s: port number must be between %d and %d!\n",
            argv[0], SERV_TCP_PORT_MIN, SERV_TCP_PORT_MAX);
    exit(-1);
  }



  /* bind local address */
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family  = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port        = htons(port);

  if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
  {
    perror("ugd");
    exit(1);
  }

  listen(sockfd, 5);

  fprintf(stdout, "%s: listening to port %d.\n", argv[0], port);


  /*
          now clients can connect, enter main loop.

          this is a concurrent server, i.e., every time
          a socket connection is established, the server
          forks a child process and connects it to this
          socket. the child process is the ug-frontend.
   */

  for (;;)
  {
    clilen = sizeof(cli_addr);
    newsockfd = accept(sockfd,
                       (struct sockaddr *) &cli_addr,
                       &clilen);



    if (newsockfd<0)
    {
      fprintf(stderr, "ugd: error during accept.\n");
      exit(1);
    }

    if ((childpid=fork()) < 0)
    {
      fprintf(stderr, "ugd: error during fork.\n");
      exit(1);
    }

    else if (childpid==0)
    {
      /* this is the child process */

      /* close original socket */
      close(sockfd);

      /* child process: do work and suicide afterwards. */
      ug_frontend(newsockfd);
      printf("ugd: client has closed connection.\n");
      exit(0);
    }
    else
    {
      /* this is the server (ugd) process */
      printf("ugd: starting ug-frontend with pid=%d for client %s\n",
             childpid,
             InternetAddr((struct in_addr *) &cli_addr.sin_addr));
    }

    close(newsockfd);
  }

  /* we will never reach this line */
  return(0);
}


/****************************************************************************/

#endif