Added upstream from http://ftp.icm.edu.pl/pub/loglan/
[loglan.git] / sources / new-s5r4 / herc.c
1 /*     Loglan82 Compiler&Interpreter
2      Copyright (C) 1993 Institute of Informatics, University of Warsaw
3      Copyright (C)  1993, 1994 LITA, Pau
4      
5      This program is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published by
7      the Free Software Foundation; either version 2 of the License, or
8      (at your option) any later version.
9      
10      This program is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13      GNU General Public License for more details.
14      
15              You should have received a copy of the GNU General Public License
16              along with this program; if not, write to the Free Software
17              Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19  contacts:  Andrzej.Salwicki@univ-pau.fr
20
21 or             Andrzej Salwicki
22                 LITA   Departement d'Informatique
23                 Universite de Pau
24                 Avenue de l'Universite
25                 64000 Pau   FRANCE
26                  tel.  ++33 59923154    fax. ++33 59841696
27
28 =======================================================================
29 */
30
31 #include "nonstand.h"
32
33 typedef int word;
34
35 #include <X11/cursorfont.h>
36 #include <X11/keysym.h>
37
38 /*#include <X11/Xos.h>*/
39 #include <X11/Xlib.h>
40 #include <X11/Xutil.h>
41 #include <X11/Xatom.h>
42
43 #include <X11/MwmUtil.h>
44
45 #include <stdio.h>
46 #include <math.h>
47 #include <ctype.h>
48
49 XSizeHints    theHints;
50 Display       *theDisp;
51 int           theDepth, theScreen, dispcells;
52 Colormap      theCmap;
53 Window        rootW, window, father;
54 GC            theGC;
55 unsigned long fcol,bcol,white,black,style=1;
56 Font          mfont;
57 XFontStruct   *mfinfo;
58 Visual        *theVisual;
59 XImage        *theImage;
60 XClientMessageEvent toFatherEv;
61 XEvent event;
62 Cursor theCursor;
63
64 int iWIDE,iHIGH;
65 int mouse_x=0,mouse_y=0,mouse_l=0,mouse_r=0,mouse_c=0,
66                         mouse_l_prs=0,mouse_r_prs=0,mouse_c_prs=0,
67                         mouse_l_p_x=0,mouse_r_p_x=0,mouse_c_p_x=0,
68                         mouse_l_p_y=0,mouse_r_p_y=0,mouse_c_p_y=0,
69                         mouse_l_rel=0,mouse_r_rel=0,mouse_c_rel=0,
70                         mouse_l_r_x=0,mouse_r_r_x=0,mouse_c_r_x=0,
71                         mouse_l_r_y=0,mouse_r_r_y=0,mouse_c_r_y=0;
72 int tracking=0;
73 #define END_OF_TRACK -9999
74
75 Pixmap pixmap;
76
77 #define get_shrt(w)  w=(int)(*(pars++));
78 #define get_word(w)  w=((int)(pars[0])&0xffff)|((int)pars[1]<<16); pars+=2;
79
80 #define put_shrt(w)  toFatherEv.data.s[cnt++] = (short)(w);
81 #define put_word(w)  toFatherEv.data.s[cnt++] = (short)((w) & 0xffff); \
82                      toFatherEv.data.s[cnt++] = (short)(((w) >> 16) & 0xffff);
83 #define snd_father   toFatherEv.type=ClientMessage;     \
84                      toFatherEv.format = 16;            \
85                      XSendEvent(theDisp,father,False,NoEventMask,&toFatherEv); \
86                      XFlush(theDisp);
87
88 #ifndef NO_PROTOTYPES
89 void RealiseCmd(int,short *);
90 #else
91 void RealiseCmd();
92 #endif
93
94
95 static char *application_name="IIUWGRAPH";
96
97
98 #define QSIZE 256
99 static KeySym keycodes[QSIZE];
100 static int keyhead=0,keytail=0;
101
102 main(argc, argv)
103     int   argc;
104     char *argv[];
105 {
106    int w=0;
107    int i;
108    int events=0;
109
110    father = atoi(argv[1]);
111
112    for( i=2; i<argc; i++ )  argv[i-1]=argv[i];
113    argc--;
114
115    if ((theDisp = XOpenDisplay(NULL)) == NULL){
116       fprintf (stderr,"\n%s:  Can't open display\n", argv[0]);
117       exit(1);
118    }
119
120    theScreen = DefaultScreen(theDisp);
121    theDepth  = DefaultDepth(theDisp, theScreen);
122    rootW     = RootWindow(theDisp,theScreen);
123    fcol=white= WhitePixel(theDisp,theScreen);
124    bcol=black= BlackPixel(theDisp,theScreen);
125    theVisual = DefaultVisual(theDisp,theScreen);
126    theCmap   = XCreateColormap(theDisp,rootW,theVisual,AllocNone);
127    dispcells = DisplayCells(theDisp, theScreen);
128    theCursor = XCreateFontCursor(theDisp,XC_arrow);
129
130    iWIDE = 720;  iHIGH = 348;
131
132    if ((mfinfo = XLoadQueryFont(theDisp,"fixed"))==NULL){
133       fprintf (stderr,"\n%s:  Can't open 'fixed' font\n", argv[0]);
134       exit(1);
135    }
136    mfont=mfinfo->fid;
137
138    theHints.width =iWIDE;
139    theHints.height=iHIGH;
140    theHints.flags=PSize;
141    window = XCreateSimpleWindow(theDisp,rootW,10,10,iWIDE,iHIGH,3,fcol,bcol);
142
143    XSetStandardProperties(theDisp,window,"HERCULES","HERCULES",
144                           None,argv,argc,theHints);
145    XDefineCursor(theDisp,window,theCursor);
146
147    XChangeProperty(theDisp,window,XA_WM_CLASS,XA_STRING,8,PropModeReplace,
148                    application_name,strlen(application_name));
149
150    {
151       struct {
152          long   flags;
153          long   functions;
154          long   decorations;
155          int    input_mode;
156       } hints;
157       Atom a=XInternAtom(theDisp,_XA_MWM_HINTS,False);
158       hints.flags       =   MWM_HINTS_FUNCTIONS;
159       hints.functions   =   MWM_FUNC_CLOSE | MWM_FUNC_MOVE;
160       hints.decorations =   0;
161       hints.input_mode  =   0;
162       XChangeProperty(theDisp,window,a,a,32,PropModeReplace,&hints,4);
163    }
164
165    theGC = XCreateGC(theDisp,window,0,0);
166    XSetFont(theDisp,theGC,mfont);
167    XSetForeground(theDisp,theGC,fcol);
168    XSetBackground(theDisp,theGC,bcol);
169
170    XSelectInput(theDisp,window,
171                 ExposureMask |
172                 KeyPressMask |
173                 PointerMotionMask |
174                 ButtonPressMask |
175                 ButtonReleaseMask
176                );
177    XMapRaised(theDisp,window);
178
179    pixmap = XCreatePixmap(theDisp,window,iWIDE,iHIGH,theDepth);
180
181    for(;;){
182
183       XNextEvent(theDisp,&event);
184
185       switch (event.type){
186
187          case Expose:
188             {
189                int x=event.xexpose.x;
190                int y=event.xexpose.y;
191                int w=event.xexpose.width;
192                int h=event.xexpose.height;
193                int cnt=0;
194                XCopyArea(theDisp,pixmap,window,theGC,x,y,w,h,x,y);
195                if( events==0 ){
196                   put_word(window);
197                   snd_father
198                }
199                events=1;
200             }
201             break;
202
203          case ClientMessage:
204             RealiseCmd( (int)(event.xclient.message_type),event.xclient.data.s);
205             break;
206
207          case MappingNotify:
208             if( event.xmapping.request == MappingModifier  ||
209                 event.xmapping.request == MappingKeyboard )
210             XRefreshKeyboardMapping(&event);
211             break;
212
213          case KeyPress:
214             {
215                KeySym key;
216                char ch;
217                int i=XLookupString( &(event.xkey), &ch, 1, &key, NULL );
218
219                if( i>0 )
220                   if( ch == '\177' )   keycodes[ keytail++ ] = -83;
221                   else                 keycodes[ keytail++ ] = (int)ch;
222                else
223                if( key == NoSymbol )  break;
224                else
225                if( IsModifierKey( key ) )  break;
226                else
227                {
228                   int to_return = 0;
229                   switch( key ){
230                      case XK_F1    : to_return = -59; break;
231                      case XK_F2    : to_return = -60; break;
232                      case XK_F3    : to_return = -61; break;
233                      case XK_F4    : to_return = -62; break;
234                      case XK_F5    : to_return = -63; break;
235                      case XK_F6    : to_return = -64; break;
236                      case XK_F7    : to_return = -65; break;
237                      case XK_F8    : to_return = -66; break;
238                      case XK_F9    : to_return = -67; break;
239                      case XK_F10   : to_return = -68; break;
240                      case XK_Home  : to_return = -71; break;
241                      case XK_Left  : to_return = -75; break;
242                      case XK_Up    : to_return = -72; break;
243                      case XK_Right : to_return = -77; break;
244                      case XK_Down  : to_return = -80; break;
245                      case XK_End   : to_return = -79; break;
246                      case XK_Insert: to_return = -82; break;
247                      case XK_Break : to_return =  -3; break;
248                      case XK_Prior : to_return = -73; break;
249                      case XK_Next  : to_return = -81; break;
250                   }
251                   if( to_return!=0 )   keycodes[ keytail++ ] = to_return;
252                   else break;
253                }
254
255                if( keytail == QSIZE ) keytail = 0;
256                if( keytail == keyhead ){
257                   keytail -- ;
258                   if( keytail < 0 )  keytail = QSIZE ;
259                }
260             }
261             break;
262
263          case MotionNotify:
264          case ButtonPress:
265          case ButtonRelease:
266             mouse_x = event.xmotion.x;
267             mouse_y = event.xmotion.y;
268             if( event.type == ButtonPress ){
269                switch( event.xbutton.button ){
270                   case Button1 : mouse_l=1; mouse_l_prs++;
271                                  mouse_l_p_x = mouse_x;
272                                  mouse_l_p_y = mouse_y;
273                                  if( tracking ){
274                                     tracking = 0;
275                                     RealiseCmd( END_OF_TRACK, NULL );
276                                  }
277                                  break;
278                   case Button2 : mouse_c=1; mouse_c_prs++;
279                                  mouse_c_p_x = mouse_x;
280                                  mouse_c_p_y = mouse_y;
281                                  break;
282                   case Button3 : mouse_r=1; mouse_r_prs++;
283                                  mouse_r_p_x = mouse_x;
284                                  mouse_r_p_y = mouse_y;
285                                  break;
286                }
287             }
288             if( event.type == ButtonRelease ){
289                switch( event.xbutton.button ){
290                   case Button1 : mouse_l=0; mouse_l_rel++;
291                                  mouse_l_r_x = mouse_x;
292                                  mouse_l_r_y = mouse_y;
293                                  break;
294                   case Button2 : mouse_c=0; mouse_c_rel++;
295                                  mouse_c_r_x = mouse_x;
296                                  mouse_c_r_y = mouse_y;
297                                  break;
298                   case Button3 : mouse_r=0; mouse_r_rel++;
299                                  mouse_r_r_x = mouse_x;
300                                  mouse_r_r_y = mouse_y;
301                                  break;
302                }
303             }
304             break;
305
306       } /* end of switch */
307
308    } /* end of for */
309
310 }
311
312
313
314 word cmd=0,w,h,x,y,curx,cury,kolb,wwyp,p,q,r,color;
315 word alfa,beta,aspect;
316 char c;
317 XImage *image;
318 XImage *XGetImage();
319
320
321
322 void RealiseCmd( cmd, pars ) int cmd; short *pars; {
323
324    int i=1;
325    int cnt=0;
326
327    switch( cmd ){
328
329       case CLS :
330          XSetForeground(theDisp,theGC,bcol);
331          XFillRectangle(theDisp,pixmap,theGC,0,0,iWIDE,iHIGH);
332          XFillRectangle(theDisp,window,theGC,0,0,iWIDE,iHIGH);
333          XSetForeground(theDisp,theGC,fcol);
334          break;
335
336       case GROFF :
337          XFreePixmap(theDisp,pixmap);
338          XDestroyWindow(theDisp,window);
339          XCloseDisplay(theDisp);
340          exit(0);
341          break;
342
343       case INKEY :
344          {
345             int keycode=0;
346             if( keyhead != keytail ){
347                keycode  = keycodes[ keyhead++ ];
348                if( keyhead == QSIZE )  keyhead = 0;
349             }
350             put_word(keycode)
351             snd_father
352          }
353          break;
354
355       case POINT :
356          get_shrt( x );
357          get_shrt( y );
358          XDrawPoint(theDisp,pixmap,theGC,x,y);
359          XDrawPoint(theDisp,window,theGC,x,y);
360          break;
361
362       case DRAW :
363          get_shrt( curx );
364          get_shrt( cury );
365          get_shrt( x );
366          get_shrt( y );
367          XDrawLine(theDisp,pixmap,theGC,curx,cury,x,y);
368          XDrawLine(theDisp,window,theGC,curx,cury,x,y);
369          break;
370
371       case CIRB :
372          get_shrt( x );
373          get_shrt( y );
374          get_shrt( r );
375          get_shrt( aspect );
376          get_shrt( alfa );
377          get_shrt( beta );
378          XDrawArc(theDisp,pixmap,theGC,x,y,r,aspect,alfa,beta);
379          XDrawArc(theDisp,window,theGC,x,y,r,aspect,alfa,beta);
380          break;
381
382       case HFILL :
383          get_shrt( curx );
384          get_shrt( cury );
385          get_shrt( x );
386          XDrawLine(theDisp,pixmap,theGC,curx,cury,x,cury);
387          XDrawLine(theDisp,window,theGC,curx,cury,x,cury);
388          break;
389
390       case VFILL :
391          get_shrt( curx );
392          get_shrt( cury );
393          get_shrt( y );
394          XDrawLine(theDisp,pixmap,theGC,curx,cury,curx,y);
395          XDrawLine(theDisp,window,theGC,curx,cury,curx,y);
396          break;
397
398       case INPIX :
399          get_shrt( x );
400          get_shrt( y );
401          image=XGetImage(theDisp,pixmap,x,y,1,1,0x7fffffffL,ZPixmap);
402          color = XGetPixel(image,0,0);
403          XDestroyImage(image);
404          put_word(color)
405          snd_father
406          break;
407
408       case GETMAP :
409          {
410             Pixmap map;
411             get_shrt( x );
412             get_shrt( y );
413             get_shrt( w );
414             get_shrt( h );
415             if( w>iWIDE ) w=iWIDE;
416             if( h>iHIGH ) h=iHIGH;
417             map = XCreatePixmap(theDisp,window,w,h,theDepth);
418             XCopyArea(theDisp,pixmap,map,theGC,x,y,w,h,0,0);
419             put_word(map)
420             snd_father
421          }
422          break;
423
424       case PUTMAP :
425       case XORMAP :
426       case  ORMAP :
427          {
428             Pixmap map;
429             switch( cmd ){
430                case XORMAP: XSetFunction(theDisp,theGC,GXxor); break;
431                case  ORMAP: XSetFunction(theDisp,theGC,GXor ); break;
432             }
433             get_word( map );
434             get_shrt( curx );
435             get_shrt( cury );
436             get_shrt( w );
437             get_shrt( h );
438             if( w>iWIDE ) w=iWIDE;
439             if( h>iHIGH ) h=iHIGH;
440             XCopyArea(theDisp,map,pixmap,theGC,0,0,w,h,curx,cury);
441             XCopyArea(theDisp,map,window,theGC,0,0,w,h,curx,cury);
442             if( cmd != PUTMAP )  XSetFunction(theDisp,theGC,GXcopy);
443          }
444          break;
445
446       case COLOR :
447          get_shrt( color );
448          if( color == 0 )  fcol = black ;
449          else              fcol = white ;
450          XSetForeground(theDisp,theGC,fcol);
451          break;
452
453       case BORDER :
454          get_shrt( color );
455          if( color == 0 )  bcol = black ;
456          else              bcol = white ;
457          XSetBackground(theDisp,theGC,bcol);
458          break;
459
460       case STYLE :
461       {
462          static char style_2[]={ 6, 3 };
463          static char style_3[]={ 4, 6 };
464          static char style_4[]={ 2, 3 };
465          static char style_5[]={ 2, 9 };
466
467          get_shrt( style );
468          switch( style ){
469             case 0:
470                XSetForeground(theDisp,theGC,bcol);
471                XSetLineAttributes(theDisp,theGC,0,LineSolid,CapButt,JoinMiter);
472                break;
473             case 1:
474                XSetForeground(theDisp,theGC,fcol);
475                XSetLineAttributes(theDisp,theGC,0,LineSolid,CapButt,JoinMiter);
476                break;
477             case 2:
478                XSetLineAttributes(theDisp,theGC,0,LineDoubleDash,
479                                   CapButt,JoinMiter);
480                XSetDashes(theDisp,theGC,0,style_2,2);
481                break;
482             case 3:
483                XSetLineAttributes(theDisp,theGC,0,LineDoubleDash,
484                                   CapButt,JoinMiter);
485                XSetDashes(theDisp,theGC,0,style_3,2);
486                break;
487             case 4:
488                XSetLineAttributes(theDisp,theGC,0,LineDoubleDash,
489                                   CapButt,JoinMiter);
490                XSetDashes(theDisp,theGC,0,style_4,2);
491                break;
492             case 5:
493                XSetLineAttributes(theDisp,theGC,0,LineDoubleDash,
494                                   CapButt,JoinMiter);
495                XSetDashes(theDisp,theGC,0,style_5,2);
496                break;
497             }
498          }
499          break;
500
501       case HASCII :
502          get_shrt( curx );
503          get_shrt( cury );
504          get_shrt( r );
505          if( r==0 ){
506             XSetForeground(theDisp,theGC,bcol);
507             XFillRectangle(theDisp,pixmap,theGC,curx,cury,8,8);
508             XFillRectangle(theDisp,window,theGC,curx,cury,8,8);
509             XSetForeground(theDisp,theGC,fcol);
510          }else{
511             c = (char)r;
512             XDrawString(theDisp,pixmap,theGC,curx,cury+8,&c,1);
513             XDrawString(theDisp,window,theGC,curx,cury+8,&c,1);
514          }
515          break;
516
517       case STATUS :
518          put_shrt(mouse_x)
519          put_shrt(mouse_y)
520          put_shrt(mouse_l)
521          put_shrt(mouse_r)
522          put_shrt(mouse_c)
523          snd_father
524          break;
525
526       case GETPRESS :
527       case GETRELEASE :
528          {
529             int button;
530             get_shrt(button);
531             if( cmd == GETPRESS )
532                switch( button ){
533
534                   case 0:
535                           put_shrt( mouse_l_p_x );
536                           put_shrt( mouse_l_p_y );
537                           put_shrt( mouse_l_prs );
538                           mouse_l_prs=0;
539                           break;
540
541                   case 1:
542                           put_shrt( mouse_r_p_x );
543                           put_shrt( mouse_r_p_y );
544                           put_shrt( mouse_r_prs );
545                           mouse_r_prs=0;
546                           break;
547
548                   case 2:
549                           put_shrt( mouse_c_p_x );
550                           put_shrt( mouse_c_p_y );
551                           put_shrt( mouse_c_prs );
552                           mouse_c_prs=0;
553                           break;
554
555                   default:put_shrt( 0 );
556                           put_shrt( 0 );
557                           put_shrt( 0 );
558                           break;
559
560                }
561             else
562                switch( button ){
563
564                   case 0:
565                           put_shrt( mouse_l_r_x );
566                           put_shrt( mouse_l_r_y );
567                           put_shrt( mouse_l_rel );
568                           mouse_l_rel=0;
569                           break;
570
571                   case 1:
572                           put_shrt( mouse_r_r_x );
573                           put_shrt( mouse_r_r_y );
574                           put_shrt( mouse_r_rel );
575                           mouse_r_rel=0;
576                           break;
577
578                   case 2:
579                           put_shrt( mouse_c_r_x );
580                           put_shrt( mouse_c_r_y );
581                           put_shrt( mouse_c_rel );
582                           mouse_c_rel=0;
583                           break;
584
585                   default:put_shrt( 0 );
586                           put_shrt( 0 );
587                           put_shrt( 0 );
588                           break;
589
590                }
591             put_shrt(mouse_l)
592             put_shrt(mouse_r)
593             put_shrt(mouse_c)
594             snd_father
595          }
596          break;
597
598       case GETMOVEMENT :
599          {
600             static int x=0,y=0;
601             put_shrt(mouse_x-x)
602             put_shrt(mouse_y-y)
603             snd_father
604             x = mouse_x;
605             y = mouse_y;
606          }
607          break;
608
609       case TRACK:
610          {
611             int x,y;
612             get_shrt( x )   /* these parameters are not used */
613             get_shrt( y )
614             tracking = 1;
615             /* now we wait to point and press left button */
616          }
617          break;
618
619       case END_OF_TRACK:
620          put_shrt( mouse_x )
621          put_shrt( mouse_y )
622          snd_father
623          break;
624
625       default :
626          fprintf(stderr,"UKNOWN COMMAND for HERCULES emulation - %d\n",cmd);
627          fflush(stderr);
628          XFreePixmap(theDisp,pixmap);
629          XDestroyWindow(theDisp,window);
630          XCloseDisplay(theDisp);
631          exit(7);
632
633    }
634
635 }
636