1 /*----------------------------------------------------------------------*
2 * X from Haskell (PicoX)
6 *----------------------------------------------------------------------*/
10 #include <X11/Xatom.h>
14 /*----------------------------------------------------------------------*/
16 /* First the X Globals */
25 /* and the Haskell globals */
29 int HaskPointerX,HaskPointerY;
33 HaskGlobType HaskGlob;
35 /*----------------------------------------------------------------------*/
38 * Now the access functions into the haskell globals
41 int haskGetButtons(int n)
43 return(HaskGlob.HaskButtons[n]);
46 int haskGetPointerX(void)
48 return(HaskGlob.HaskPointerX);
51 int haskGetPointerY(void)
53 return(HaskGlob.HaskPointerY);
56 /*----------------------------------------------------------------------*/
59 *The (rather messy) initiualisation
62 haskXBegin(int x,int y,int sty)
65 * later include these via interface hacks
68 /* (int argc, char **argv) */
73 int MyWinFG, MyWinBG,tmp;
75 if ((MyDisplay = XOpenDisplay("")) == NULL) {
76 fprintf(stderr, "Cannot connect to X server '%s'\n", XDisplayName(""));
80 MyScreen = DefaultScreen(MyDisplay);
82 MyWinBG = WhitePixel(MyDisplay, MyScreen);
83 MyWinFG = BlackPixel(MyDisplay, MyScreen);
89 XHints.flags = PPosition | PSize;
94 DefaultRootWindow(MyDisplay),
101 XSetStandardProperties(
104 "XLib for Glasgow Haskell",
105 "XLib for Glasgow Haskell",
112 /* Create drawing and erasing GC */
114 DrawGC = XCreateGC(MyDisplay,MyWindow,0, 0);
115 XSetBackground(MyDisplay,DrawGC,MyWinBG);
116 XSetForeground(MyDisplay,DrawGC,MyWinFG);
118 UnDrawGC = XCreateGC(MyDisplay,MyWindow,0, 0);
119 XSetBackground(MyDisplay,UnDrawGC,MyWinFG);
120 XSetForeground(MyDisplay,UnDrawGC,MyWinBG);
122 XSetGraphicsExposures(MyDisplay,DrawGC,False);
123 XSetGraphicsExposures(MyDisplay,UnDrawGC,False);
124 XMapRaised(MyDisplay,MyWindow);
126 /* the user should be able to choose which are tested for
132 ButtonPressMask | ButtonReleaseMask | PointerMotionMask
135 /* later have more drawing styles
141 /* Andy, this used to be GXor not much use for Undrawing so I
142 changed it. (Not much use for colour either - see next
144 XSetFunction(MyDisplay,DrawGC,GXcopy);
145 XSetFunction(MyDisplay,UnDrawGC,GXcopy);
148 /* Andy, this can have totally bogus results on a colour screen */
149 XSetFunction(MyDisplay,DrawGC,GXxor);
150 XSetFunction(MyDisplay,UnDrawGC,GXxor);
153 /* Andy, is this really a good error message? */
154 printf(stderr,"Wrong Argument to XSet function\n");
157 * reset the (Haskell) globals
160 for(tmp=0;tmp<5;tmp++)
162 HaskGlob.HaskButtons[tmp] = 0;
164 HaskGlob.HaskPointerX = 0;
165 HaskGlob.HaskPointerY = 0;
166 HaskGlob.PointMoved = 0;
172 /*----------------------------------------------------------------------*/
174 /* Boring X ``Do Something'' functions
179 XFreeGC( MyDisplay, DrawGC);
180 XFreeGC( MyDisplay, UnDrawGC);
181 XDestroyWindow( MyDisplay, MyWindow);
182 XCloseDisplay( MyDisplay);
201 XDrawPoint(MyDisplay,
213 XFillRectangle(MyDisplay,
220 /*----------------------------------------------------------------------*/
222 /* This has to be called every time round the loop,
223 * it flushed the buffer and handles input from the user
229 while (XEventsQueued( MyDisplay, QueuedAfterReading) != 0) {
230 XNextEvent( MyDisplay, &MyWinEvent);
231 switch (MyWinEvent.type) {
233 switch (MyWinEvent.xbutton.button)
235 case Button1: HaskGlob.HaskButtons[0] = 1; break;
236 case Button2: HaskGlob.HaskButtons[1] = 1; break;
237 case Button3: HaskGlob.HaskButtons[2] = 1; break;
238 case Button4: HaskGlob.HaskButtons[3] = 1; break;
239 case Button5: HaskGlob.HaskButtons[4] = 1; break;
243 switch (MyWinEvent.xbutton.button)
245 case Button1: HaskGlob.HaskButtons[0] = 0; break;
246 case Button2: HaskGlob.HaskButtons[1] = 0; break;
247 case Button3: HaskGlob.HaskButtons[2] = 0; break;
248 case Button4: HaskGlob.HaskButtons[3] = 0; break;
249 case Button5: HaskGlob.HaskButtons[4] = 0; break;
253 HaskGlob.HaskPointerX = MyWinEvent.xmotion.x;
254 HaskGlob.HaskPointerY = MyWinEvent.xmotion.y;
255 HaskGlob.PointMoved = 1;
258 printf("UNKNOWN INTERUPT ???? (%d) \n",MyWinEvent.type);
266 /*----------------------------------------------------------------------*/
268 /* A function to clear the screen
273 XClearWindow(MyDisplay,MyWindow);
276 /*----------------------------------------------------------------------*/
278 /* A function to write a string
281 haskXDrawString(int x,int y,char *str)
284 /* printf("GOT HERE %s %d %d",str,x,y);
285 XDrawString(MyDisplay,MyWindow,DrawGC,x,y,str,strlen(str));
289 /*----------------------------------------------------------------------*/
291 extern int prog_argc;
292 extern char **prog_argv;
296 return(prog_argc > 1 ? atoi(prog_argv[1]) : 0);