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
|
/*----------------------------------------------------------------------*
* X from Haskell (PicoX)
*
* (c) 1993 Andy Gill
*
*----------------------------------------------------------------------*/
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include <strings.h>
/*----------------------------------------------------------------------*/
/* First the X Globals */
Display *MyDisplay;
int MyScreen;
Window MyWindow;
XEvent MyWinEvent;
GC DrawGC;
GC UnDrawGC;
/* and the Haskell globals */
typedef struct {
int HaskButtons[5];
int HaskPointerX,HaskPointerY;
int PointMoved;
} HaskGlobType;
HaskGlobType HaskGlob;
/*----------------------------------------------------------------------*/
/*
* Now the access functions into the haskell globals
*/
int haskGetButtons(int n)
{
return(HaskGlob.HaskButtons[n]);
}
int haskGetPointerX(void)
{
return(HaskGlob.HaskPointerX);
}
int haskGetPointerY(void)
{
return(HaskGlob.HaskPointerY);
}
/*----------------------------------------------------------------------*/
/*
*The (rather messy) initiualisation
*/
haskXBegin(int x,int y,int sty)
{
/*
* later include these via interface hacks
*/
/* (int argc, char **argv) */
int argc = 0;
char **argv = 0;
XSizeHints XHints;
int MyWinFG, MyWinBG,tmp;
if ((MyDisplay = XOpenDisplay("")) == NULL) {
fprintf(stderr, "Cannot connect to X server '%s'\n", XDisplayName(""));
exit(1);
}
MyScreen = DefaultScreen(MyDisplay);
MyWinBG = WhitePixel(MyDisplay, MyScreen);
MyWinFG = BlackPixel(MyDisplay, MyScreen);
XHints.x = x;
XHints.y = y;
XHints.width = x;
XHints.height = y;
XHints.flags = PPosition | PSize;
MyWindow =
XCreateSimpleWindow(
MyDisplay,
DefaultRootWindow(MyDisplay),
x,y, x, y,
5,
MyWinFG,
MyWinBG
);
XSetStandardProperties(
MyDisplay,
MyWindow,
"XLib for Glasgow Haskell",
"XLib for Glasgow Haskell",
None,
argv,
argc,
&XHints
);
/* Create drawing and erasing GC */
DrawGC = XCreateGC(MyDisplay,MyWindow,0, 0);
XSetBackground(MyDisplay,DrawGC,MyWinBG);
XSetForeground(MyDisplay,DrawGC,MyWinFG);
UnDrawGC = XCreateGC(MyDisplay,MyWindow,0, 0);
XSetBackground(MyDisplay,UnDrawGC,MyWinFG);
XSetForeground(MyDisplay,UnDrawGC,MyWinBG);
XSetGraphicsExposures(MyDisplay,DrawGC,False);
XSetGraphicsExposures(MyDisplay,UnDrawGC,False);
XMapRaised(MyDisplay,MyWindow);
/* the user should be able to choose which are tested for
*/
XSelectInput(
MyDisplay,
MyWindow,
ButtonPressMask | ButtonReleaseMask | PointerMotionMask
);
/* later have more drawing styles
*/
switch (sty)
{
case 0:
/* Andy, this used to be GXor not much use for Undrawing so I
changed it. (Not much use for colour either - see next
comment */
XSetFunction(MyDisplay,DrawGC,GXcopy);
XSetFunction(MyDisplay,UnDrawGC,GXcopy);
break;
case 1:
/* Andy, this can have totally bogus results on a colour screen */
XSetFunction(MyDisplay,DrawGC,GXxor);
XSetFunction(MyDisplay,UnDrawGC,GXxor);
break;
default:
/* Andy, is this really a good error message? */
printf(stderr,"Wrong Argument to XSet function\n");
}
/*
* reset the (Haskell) globals
*/
for(tmp=0;tmp<5;tmp++)
{
HaskGlob.HaskButtons[tmp] = 0;
}
HaskGlob.HaskPointerX = 0;
HaskGlob.HaskPointerY = 0;
HaskGlob.PointMoved = 0;
XFlush(MyDisplay);
}
/*----------------------------------------------------------------------*/
/* Boring X ``Do Something'' functions
*/
haskXClose(void)
{
XFreeGC( MyDisplay, DrawGC);
XFreeGC( MyDisplay, UnDrawGC);
XDestroyWindow( MyDisplay, MyWindow);
XCloseDisplay( MyDisplay);
return(0);
}
haskXDraw(x,y,x1,y1)
int x,y,x1,y1;
{
XDrawLine(MyDisplay,
MyWindow,
DrawGC,
x,y,x1,y1);
return(0);
}
haskXPlot(c,x,y)
int c;
int x,y;
{
XDrawPoint(MyDisplay,
MyWindow,
(c?DrawGC:UnDrawGC),
x,y);
return(0);
}
haskXFill(c,x,y,w,h)
int c;
int x, y;
int w, h;
{
XFillRectangle(MyDisplay,
MyWindow,
(c?DrawGC:UnDrawGC),
x, y, w, h);
return(0);
}
/*----------------------------------------------------------------------*/
/* This has to be called every time round the loop,
* it flushed the buffer and handles input from the user
*/
haskHandleEvent()
{
XFlush( MyDisplay);
while (XEventsQueued( MyDisplay, QueuedAfterReading) != 0) {
XNextEvent( MyDisplay, &MyWinEvent);
switch (MyWinEvent.type) {
case ButtonPress:
switch (MyWinEvent.xbutton.button)
{
case Button1: HaskGlob.HaskButtons[0] = 1; break;
case Button2: HaskGlob.HaskButtons[1] = 1; break;
case Button3: HaskGlob.HaskButtons[2] = 1; break;
case Button4: HaskGlob.HaskButtons[3] = 1; break;
case Button5: HaskGlob.HaskButtons[4] = 1; break;
}
break;
case ButtonRelease:
switch (MyWinEvent.xbutton.button)
{
case Button1: HaskGlob.HaskButtons[0] = 0; break;
case Button2: HaskGlob.HaskButtons[1] = 0; break;
case Button3: HaskGlob.HaskButtons[2] = 0; break;
case Button4: HaskGlob.HaskButtons[3] = 0; break;
case Button5: HaskGlob.HaskButtons[4] = 0; break;
}
break;
case MotionNotify:
HaskGlob.HaskPointerX = MyWinEvent.xmotion.x;
HaskGlob.HaskPointerY = MyWinEvent.xmotion.y;
HaskGlob.PointMoved = 1;
break;
default:
printf("UNKNOWN INTERUPT ???? (%d) \n",MyWinEvent.type);
break;
} /*switch*/
} /*if*/
return(0);
}
/*----------------------------------------------------------------------*/
/* A function to clear the screen
*/
haskXCls(void)
{
XClearWindow(MyDisplay,MyWindow);
}
/*----------------------------------------------------------------------*/
/* A function to write a string
*/
haskXDrawString(int x,int y,char *str)
{
return(0);
/* printf("GOT HERE %s %d %d",str,x,y);
XDrawString(MyDisplay,MyWindow,DrawGC,x,y,str,strlen(str));
*/
}
/*----------------------------------------------------------------------*/
extern int prog_argc;
extern char **prog_argv;
haskArgs()
{
return(prog_argc > 1 ? atoi(prog_argv[1]) : 0);
}
|