1 /* aux.c */
2 
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 
8 #if !defined(AMIGA) && !defined(__WIN32__)
9 #include <X11/Xlib.h>
10 #include <X11/Xutil.h>
11 #include <X11/keysym.h>
12 #endif
13 
14 #include <GL/gl.h>
15 
16 #include "../tk/tk.h"
17 #include "glaux.h"
18 
19 #if defined(__cplusplus) || defined(c_plusplus)
20 #define class c_class
21 #endif
22 
23 
24 static struct {
25     int keyField;
26     void (*KeyFunc)(void);
27 } keyTable[200];
28 
29 static struct {
30     int mouseField;
31     void (*MouseFunc)(AUX_EVENTREC *);
32 } mouseDownTable[20], mouseUpTable[20], mouseLocTable[20];
33 
34 static int keyTableCount = 0;
35 static int mouseDownTableCount = 0;
36 static int mouseUpTableCount = 0;
37 static int mouseLocTableCount = 0;
38 static GLenum displayModeType = 0;
39 static GLenum displayModePolicy = 0; /* AUX_MINIMUM_CRITERIA; */
40 static int displayModeID = 0;
41 
42 static int animate = 0;
43 
44 
45 #ifdef __WIN32__
46 #define NCOLORS 17
47 float auxRGBMap[NCOLORS][3] = {
48     {0,0,0},
49     {0,0,0},
50     {0,0,0},
51     {0,0,0},
52     {0,0,0},
53     {0,0,0},
54     {0,0,0},
55     {0,0,0},
56     {0,0,0},
57     {0,0,0},
58     {1,0,0},
59     {0,1,0},
60     {1,1,0},
61     {0,0,1},
62     {1,0,1},
63     {0,1,1},
64     {1,1,1}
65 };
66 #endif
67 
68 
DefaultHandleReshape(int w,int h)69 static void DefaultHandleReshape(int w, int h)
70 {
71     glViewport(0, 0, w, h);
72     glMatrixMode(GL_PROJECTION);
73     glLoadIdentity();
74     glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0);
75     glMatrixMode(GL_MODELVIEW);
76     glLoadIdentity();
77 }
78 
DefaultHandleExpose(int w,int h)79 static void DefaultHandleExpose(int w, int h)
80 {
81 }
82 
MouseLoc(int x,int y,GLenum button)83 static GLenum MouseLoc(int x, int y, GLenum button)
84 {
85     AUX_EVENTREC info;
86     GLenum flag;
87     int i;
88 
89     flag = GL_FALSE;
90     for (i = 0; i < mouseLocTableCount; i++) {
91 	if ((button & AUX_LEFTBUTTON) == mouseLocTable[i].mouseField) {
92 	    info.event = AUX_MOUSELOC;
93 	    info.data[AUX_MOUSEX] = x;
94 	    info.data[AUX_MOUSEY] = y;
95 	    info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
96 	    (*mouseLocTable[i].MouseFunc)(&info);
97 	    flag |= GL_TRUE;
98 	}
99 	if ((button & AUX_RIGHTBUTTON) == mouseLocTable[i].mouseField) {
100 	    info.event = AUX_MOUSELOC;
101 	    info.data[AUX_MOUSEX] = x;
102 	    info.data[AUX_MOUSEY] = y;
103 	    info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
104 	    (*mouseLocTable[i].MouseFunc)(&info);
105 	    flag |= GL_TRUE;
106 	}
107 	if ((button & AUX_MIDDLEBUTTON) == mouseLocTable[i].mouseField) {
108 	    info.event = AUX_MOUSELOC;
109 	    info.data[AUX_MOUSEX] = x;
110 	    info.data[AUX_MOUSEY] = y;
111 	    info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
112 	    (*mouseLocTable[i].MouseFunc)(&info);
113 	    flag |= GL_TRUE;
114 	}
115     }
116     return flag;
117 }
118 
MouseUp(int x,int y,GLenum button)119 static GLenum MouseUp(int x, int y, GLenum button)
120 {
121     AUX_EVENTREC info;
122     GLenum flag;
123     int i;
124 
125     flag = GL_FALSE;
126     for (i = 0; i < mouseUpTableCount; i++) {
127 	if ((button & AUX_LEFTBUTTON) == mouseUpTable[i].mouseField) {
128 	    info.event = AUX_MOUSEUP;
129 	    info.data[AUX_MOUSEX] = x;
130 	    info.data[AUX_MOUSEY] = y;
131 	    info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
132 	    (*mouseUpTable[i].MouseFunc)(&info);
133 	    flag |= GL_TRUE;
134 	}
135 	if ((button & AUX_RIGHTBUTTON) == mouseUpTable[i].mouseField) {
136 	    info.event = AUX_MOUSEUP;
137 	    info.data[AUX_MOUSEX] = x;
138 	    info.data[AUX_MOUSEY] = y;
139 	    info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
140 	    (*mouseUpTable[i].MouseFunc)(&info);
141 	    flag |= GL_TRUE;
142 	}
143 	if ((button & AUX_MIDDLEBUTTON) == mouseUpTable[i].mouseField) {
144 	    info.event = AUX_MOUSEUP;
145 	    info.data[AUX_MOUSEX] = x;
146 	    info.data[AUX_MOUSEY] = y;
147 	    info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
148 	    (*mouseUpTable[i].MouseFunc)(&info);
149 	    flag |= GL_TRUE;
150 	}
151     }
152     return flag;
153 }
154 
MouseDown(int x,int y,GLenum button)155 static GLenum MouseDown(int x, int y, GLenum button)
156 {
157     AUX_EVENTREC info;
158     GLenum flag;
159     int i;
160 
161     flag = GL_FALSE;
162     for (i = 0; i < mouseDownTableCount; i++) {
163 	if ((button & AUX_LEFTBUTTON) == mouseDownTable[i].mouseField) {
164 	    info.event = AUX_MOUSEDOWN;
165 	    info.data[AUX_MOUSEX] = x;
166 	    info.data[AUX_MOUSEY] = y;
167 	    info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
168 	    (*mouseDownTable[i].MouseFunc)(&info);
169 	    flag |= GL_TRUE;
170 	}
171 	if ((button & AUX_RIGHTBUTTON) == mouseDownTable[i].mouseField) {
172 	    info.event = AUX_MOUSEDOWN;
173 	    info.data[AUX_MOUSEX] = x;
174 	    info.data[AUX_MOUSEY] = y;
175 	    info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
176 	    (*mouseDownTable[i].MouseFunc)(&info);
177 	    flag |= GL_TRUE;
178 	}
179 	if ((button & AUX_MIDDLEBUTTON) == mouseDownTable[i].mouseField) {
180 	    info.event = AUX_MOUSEDOWN;
181 	    info.data[AUX_MOUSEX] = x;
182 	    info.data[AUX_MOUSEY] = y;
183 	    info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
184 	    (*mouseDownTable[i].MouseFunc)(&info);
185 	    flag |= GL_TRUE;
186 	}
187     }
188     return flag;
189 }
190 
KeyDown(int key,GLenum status)191 static GLenum KeyDown(int key, GLenum status)
192 {
193     GLenum flag;
194     int i;
195 
196     flag = GL_FALSE;
197     if (keyTableCount) {
198 	for (i = 0; i < keyTableCount; i++) {
199 	    if (key == keyTable[i].keyField) {
200 		(*keyTable[i].KeyFunc)();
201 		flag |= GL_TRUE;
202 	    }
203 	}
204     }
205     return flag;
206 }
207 
auxExposeFunc(void (* Func)(int,int))208 void auxExposeFunc(void (*Func)(int, int))
209 {
210     tkExposeFunc(Func);
211 }
212 
auxReshapeFunc(void (* Func)(int,int))213 void auxReshapeFunc(void (*Func)(int, int))
214 {
215     tkExposeFunc(Func);
216     tkReshapeFunc(Func);
217 }
218 
auxIdleFunc(void (* Func)(void))219 void auxIdleFunc(void (*Func)(void))
220 {
221     tkIdleFunc(Func);
222 }
223 
auxKeyFunc(int key,void (* Func)(void))224 void auxKeyFunc(int key, void (*Func)(void))
225 {
226     keyTable[keyTableCount].keyField = key;
227     keyTable[keyTableCount++].KeyFunc = Func;
228 }
229 
auxMouseFunc(int mouse,int mode,void (* Func)(AUX_EVENTREC *))230 void auxMouseFunc(int mouse, int mode, void (*Func)(AUX_EVENTREC *))
231 {
232     if (mode == AUX_MOUSEDOWN) {
233 	mouseDownTable[mouseDownTableCount].mouseField = mouse;
234 	mouseDownTable[mouseDownTableCount++].MouseFunc = Func;
235     } else if (mode == AUX_MOUSEUP) {
236 	mouseUpTable[mouseUpTableCount].mouseField = mouse;
237 	mouseUpTable[mouseUpTableCount++].MouseFunc = Func;
238     } else if (mode == AUX_MOUSELOC) {
239 	mouseLocTable[mouseLocTableCount].mouseField = mouse;
240 	mouseLocTable[mouseLocTableCount++].MouseFunc = Func;
241     }
242 }
243 
244 
auxDeleteMouseFunc(int mouse,int mode,void (* Func)(AUX_EVENTREC *))245 void auxDeleteMouseFunc( int mouse, int mode, void (*Func)(AUX_EVENTREC *))
246 {
247    int i, j;
248 
249    for (i=0;i<mouseLocTableCount;i++) {
250       if (mouseLocTable[i].MouseFunc == Func) {
251          /* delete this one */
252          for (j=i+1;j<mouseLocTableCount;j++) {
253             mouseLocTable[j-1].MouseFunc = mouseLocTable[j].MouseFunc;
254             mouseLocTable[j-1].mouseField = mouseLocTable[j].mouseField;
255          }
256          mouseLocTableCount--;
257          break;
258       }
259    }
260 
261 }
262 
263 
idle(void)264 static void idle(void)
265 {
266    /* do nothing */
267 }
268 
auxMainLoop(void (* Func)(void))269 void auxMainLoop(void (*Func)(void))
270 {
271    if (animate) {
272       auxIdleFunc( idle );
273    }
274 
275     tkDisplayFunc(Func);
276     tkExec(1);
277 }
278 
auxInitPosition(int x,int y,int width,int height)279 void auxInitPosition(int x, int y, int width, int height)
280 {
281     tkInitPosition(x, y, width, height);
282 }
283 
auxInitDisplayMode(GLenum type)284 void auxInitDisplayMode(GLenum type)
285 {
286     displayModeType = type;
287     tkInitDisplayMode(type);
288 }
289 
auxInitDisplayModePolicy(GLenum type)290 void auxInitDisplayModePolicy(GLenum type)
291 {
292 
293     displayModePolicy = type;
294 /*    tkInitDisplayModePolicy(type);*/
295 }
296 
auxInitDisplayModeID(GLint id)297 GLenum auxInitDisplayModeID(GLint id)
298 {
299 
300     displayModeID = id;
301 /*    tkInitDisplayModeID(id);*/
302 }
303 
auxInitWindow(char * title)304 GLenum auxInitWindow(char *title)
305 {
306     int useDoubleAsSingle = 0;
307 
308     if (tkInitWindow(title) == GL_FALSE) {
309 	if (AUX_WIND_IS_SINGLE(displayModeType)) {
310 	    tkInitDisplayMode(displayModeType|AUX_DOUBLE);
311 	    if (tkInitWindow(title) == GL_FALSE) {
312 		return GL_FALSE;
313 	    }
314 	    fprintf(stderr, "Can't initialize a single buffer visual.\n");
315 	    fprintf(stderr, "Will use a double buffer visual instead,");
316 	    fprintf(stderr, "only drawing into the front buffer.\n");
317 	    displayModeType = displayModeType | AUX_DOUBLE;
318 	    useDoubleAsSingle = 1;
319 	}
320     }
321     tkReshapeFunc(DefaultHandleReshape);
322     tkExposeFunc(DefaultHandleExpose);
323     tkMouseUpFunc(MouseUp);
324     tkMouseDownFunc(MouseDown);
325     tkMouseMoveFunc(MouseLoc);
326     tkKeyDownFunc(KeyDown);
327     auxKeyFunc(AUX_ESCAPE, auxQuit);
328     glClearColor(0.0, 0.0, 0.0, 1.0);
329     glClearIndex(0);
330     glLoadIdentity();
331     if (useDoubleAsSingle) {
332         glReadBuffer(GL_FRONT);
333 	glDrawBuffer(GL_FRONT);
334     }
335     return GL_TRUE;
336 }
337 
auxCloseWindow(void)338 void auxCloseWindow(void)
339 {
340     tkCloseWindow();
341     keyTableCount = 0;
342     mouseDownTableCount = 0;
343     mouseUpTableCount = 0;
344     mouseLocTableCount = 0;
345 }
346 
auxQuit(void)347 void auxQuit(void)
348 {
349     tkQuit();
350 }
351 
auxSwapBuffers(void)352 void auxSwapBuffers(void)
353 {
354     tkSwapBuffers();
355 }
356 
357 #if !defined(AMIGA) && !defined(__WIN32__)
358 /* for systems with X only... */
auxXDisplay(void)359 Display *auxXDisplay(void)
360 {
361     Display *ptr;
362 
363     tkGetSystem(TK_X_DISPLAY, (void *)&ptr);
364     return ptr;
365 }
366 
auxXWindow(void)367 Window auxXWindow(void)
368 {
369     Window ptr;
370 
371     tkGetSystem(TK_X_WINDOW, (void *)&ptr);
372     return ptr;
373 }
374 #endif
375 
auxGetDisplayModePolicy(void)376 GLenum auxGetDisplayModePolicy(void)
377 {
378    return displayModePolicy;
379 /*    return tkGetDisplayModePolicy();*/
380 }
381 
auxGetDisplayModeID(void)382 GLint auxGetDisplayModeID(void)
383 {
384 /*    return tkGetDisplayModeID();*/
385    return displayModeID;
386 }
387 
auxGetDisplayMode(void)388 GLenum auxGetDisplayMode(void)
389 {
390 /*    return tkGetDisplayMode();*/
391    return displayModeType;
392 }
393 
auxSetOneColor(int index,float r,float g,float b)394 void auxSetOneColor(int index, float r, float g, float b)
395 {
396     tkSetOneColor(index, r, g, b);
397 }
398 
auxSetFogRamp(int density,int startIndex)399 void auxSetFogRamp(int density, int startIndex)
400 {
401     tkSetFogRamp(density, startIndex);
402 }
403 
auxSetGreyRamp(void)404 void auxSetGreyRamp(void)
405 {
406     tkSetGreyRamp();
407 }
408 
auxSetRGBMap(int size,float * rgb)409 void auxSetRGBMap(int size, float *rgb)
410 {
411     tkSetRGBMap(size, rgb);
412 }
413 
auxGetColorMapSize(void)414 int auxGetColorMapSize(void)
415 {
416 
417     return tkGetColorMapSize();;
418 }
419 
auxGetMouseLoc(int * x,int * y)420 void auxGetMouseLoc(int *x, int *y)
421 {
422     tkGetMouseLoc(x, y);
423 }
424 
auxGetScreenSize(GLint * width,GLint * height)425 void auxGetScreenSize( GLint *width, GLint *height )
426 {
427    /* This is a kludge! */
428    *width = 1280;
429    *height = 1024;
430 }
431 
432 
auxAnimation(GLint state)433 void auxAnimation( GLint state )
434 {
435    animate = state;
436 }
437 
438