More explicit argument list for INVOKE_WCB callbacks
[freeglut] / src / fg_internal.h
1 /*
2  * fg_internal.h
3  *
4  * The freeglut library private include file.
5  *
6  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7  * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8  * Creation date: Thu Dec 2 1999
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27
28 #ifndef  FREEGLUT_INTERNAL_H
29 #define  FREEGLUT_INTERNAL_H
30
31 #ifdef HAVE_CONFIG_H
32 #    include "config.h"
33 #endif
34
35 #include "fg_version.h"
36
37 /* Freeglut is intended to function under all Unix/X11 and Win32 platforms. */
38 /* XXX: Don't all MS-Windows compilers (except Cygwin) have _WIN32 defined?
39  * XXX: If so, remove the first set of defined()'s below.
40  */
41 #if !defined(TARGET_HOST_POSIX_X11) && !defined(TARGET_HOST_MS_WINDOWS) && !defined(TARGET_HOST_MAC_OSX) && !defined(TARGET_HOST_SOLARIS)
42 #if defined(_MSC_VER) || defined(__WATCOMC__) || defined(__MINGW32__) \
43     || defined(_WIN32) || defined(_WIN32_WCE) \
44     || ( defined(__CYGWIN__) && defined(X_DISPLAY_MISSING) )
45 #   define  TARGET_HOST_MS_WINDOWS 1
46
47 #elif defined (__ANDROID__)
48 #   define  TARGET_HOST_ANDROID  1
49
50 #elif defined (__QNXNTO__) || defined (__PLAYBOOK__)
51 #   define  TARGET_HOST_BLACKBERRY  1
52
53 #elif defined(__posix__) || defined(__unix__) || defined(__linux__) || defined(__sun)
54 #   if defined(FREEGLUT_WAYLAND)
55 #      define  TARGET_HOST_POSIX_WAYLAND  1
56 #   else
57 #      define  TARGET_HOST_POSIX_X11  1
58 #   endif
59
60 #elif defined(__APPLE__)
61 /* This is a placeholder until we get native OSX support ironed out -- JFF 11/18/09 */
62 #   define  TARGET_HOST_POSIX_X11  1
63 /* #   define  TARGET_HOST_MAC_OSX    1 */
64
65 #else
66 #   error "Unrecognized target host!"
67
68 #endif
69 #endif
70
71 /* Detect both SunPro and gcc compilers on Sun Solaris */
72 #if defined (__SVR4) && defined (__sun)
73 #   define TARGET_HOST_SOLARIS 1
74 #endif
75
76 #ifndef TARGET_HOST_MS_WINDOWS
77 #   define  TARGET_HOST_MS_WINDOWS     0
78 #endif
79
80 #ifndef TARGET_HOST_ANDROID
81 #   define  TARGET_HOST_ANDROID        0
82 #endif
83
84 #ifndef TARGET_HOST_BLACKBERRY
85 #   define  TARGET_HOST_BLACKBERRY     0
86 #endif
87
88 #ifndef  TARGET_HOST_POSIX_WAYLAND
89 #   define  TARGET_HOST_POSIX_WAYLAND  0
90 #endif
91
92 #ifndef  TARGET_HOST_POSIX_X11
93 #   define  TARGET_HOST_POSIX_X11      0
94 #endif
95
96 #ifndef  TARGET_HOST_MAC_OSX
97 #   define  TARGET_HOST_MAC_OSX        0
98 #endif
99
100 #ifndef  TARGET_HOST_SOLARIS
101 #   define  TARGET_HOST_SOLARIS        0
102 #endif
103
104 /* -- FIXED CONFIGURATION LIMITS ------------------------------------------- */
105
106 #define  FREEGLUT_MAX_MENUS            3
107
108 /* These files should be available on every platform. */
109 #include <stdio.h>
110 #include <string.h>
111 #include <math.h>
112 #include <stdlib.h>
113 #include <stdarg.h>
114
115 /* These are included based on autoconf directives. */
116 #ifdef HAVE_SYS_TYPES_H
117 #    include <sys/types.h>
118 #endif
119 #ifdef HAVE_UNISTD_H
120 #    include <unistd.h>
121 #endif
122 #ifdef TIME_WITH_SYS_TIME
123 #    include <sys/time.h>
124 #    include <time.h>
125 #elif defined(HAVE_SYS_TIME_H)
126 #    include <sys/time.h>
127 #else
128 #    include <time.h>
129 #endif
130
131 /* -- AUTOCONF HACKS --------------------------------------------------------*/
132
133 /* XXX: Update autoconf to avoid these.
134  * XXX: Are non-POSIX platforms intended not to use autoconf?
135  * If so, perhaps there should be a config_guess.h for them. Alternatively,
136  * config guesses could be placed above, just after the config.h exclusion.
137  */
138 #if defined(__FreeBSD__) || defined(__NetBSD__)
139 #    define HAVE_USB_JS 1
140 #    if defined(__NetBSD__) || ( defined(__FreeBSD__) && __FreeBSD_version >= 500000)
141 #        define HAVE_USBHID_H 1
142 #    endif
143 #endif
144
145 #if defined(_MSC_VER) || defined(__WATCOMC__)
146 /* strdup() is non-standard, for all but POSIX-2001 */
147 #define strdup   _strdup
148 #endif
149
150 /* M_PI is non-standard (defined by BSD, not ISO-C) */
151 #ifndef M_PI
152 #    define  M_PI  3.14159265358979323846
153 #endif
154
155 #ifdef HAVE_STDBOOL_H
156 #    include <stdbool.h>
157 #    ifndef TRUE
158 #        define TRUE true
159 #    endif
160 #    ifndef FALSE
161 #        define FALSE false
162 #    endif
163 #else
164 #    ifndef TRUE
165 #        define  TRUE  1
166 #    endif
167 #    ifndef FALSE
168 #        define  FALSE  0
169 #    endif
170 #endif
171
172 /* General defines */
173 #define INVALID_MODIFIERS 0xffffffff
174
175 /* FreeGLUT internal time type */
176 #if defined(HAVE_STDINT_H)
177 #   include <stdint.h>
178     typedef uint64_t fg_time_t;
179 #elif defined(HAVE_INTTYPES_H)
180 #   include <inttypes.h>
181     typedef uint64_t fg_time_t;
182 #elif defined(HAVE_U__INT64)
183     typedef unsigned __int64 fg_time_t;
184 #elif defined(HAVE_ULONG_LONG)
185     typedef unsigned long long fg_time_t;
186 #else
187     typedef unsigned long fg_time_t;
188 #endif
189
190 #ifndef __fg_unused
191 # ifdef __GNUC__
192 #  define __fg_unused __attribute__((unused))
193 # else
194 #  define __fg_unused
195 # endif
196 #endif
197
198 /* Platform-specific includes */
199 #if TARGET_HOST_POSIX_WAYLAND
200 #include "wayland/fg_internal_wl.h"
201 #endif
202 #if TARGET_HOST_POSIX_X11
203 #include "x11/fg_internal_x11.h"
204 #endif
205 #if TARGET_HOST_MS_WINDOWS
206 #include "mswin/fg_internal_mswin.h"
207 #endif
208 #if TARGET_HOST_ANDROID
209 #include "android/fg_internal_android.h"
210 #endif
211 #if TARGET_HOST_BLACKBERRY
212 #include "blackberry/fg_internal_blackberry.h"
213 #endif
214
215
216 /* -- GLOBAL TYPE DEFINITIONS ---------------------------------------------- */
217
218 /* Freeglut callbacks type definitions */
219 typedef void* FGCBUserData;
220
221 typedef void (* FGCBDisplay         )( void );
222 typedef void (* FGCBDisplayUC       )( FGCBUserData );
223 #define EXPAND_WCB_SUB_Display(args, userData) EXPAND_WCB_ZERO(args, userData)
224 typedef void (* FGCBReshape         )( int, int );
225 typedef void (* FGCBReshapeUC       )( int, int, FGCBUserData );
226 #define EXPAND_WCB_SUB_Reshape(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
227 typedef void (* FGCBPosition        )( int, int );
228 typedef void (* FGCBPositionUC      )( int, int, FGCBUserData );
229 #define EXPAND_WCB_SUB_Position(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
230 typedef void (* FGCBVisibility      )( int );
231 typedef void (* FGCBVisibilityUC    )( int, FGCBUserData );
232 #define EXPAND_WCB_SUB_Visibility(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
233 typedef void (* FGCBKeyboard        )( unsigned char, int, int );
234 typedef void (* FGCBKeyboardUC      )( unsigned char, int, int, FGCBUserData );
235 #define EXPAND_WCB_SUB_Keyboard(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
236 typedef void (* FGCBKeyboardUp      )( unsigned char, int, int );
237 typedef void (* FGCBKeyboardUpUC    )( unsigned char, int, int, FGCBUserData );
238 #define EXPAND_WCB_SUB_KeyboardUp(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
239 typedef void (* FGCBSpecial         )( int, int, int );
240 typedef void (* FGCBSpecialUC       )( int, int, int, FGCBUserData );
241 #define EXPAND_WCB_SUB_Special(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
242 typedef void (* FGCBSpecialUp       )( int, int, int );
243 typedef void (* FGCBSpecialUpUC     )( int, int, int, FGCBUserData );
244 #define EXPAND_WCB_SUB_SpecialUp(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
245 typedef void (* FGCBMouse           )( int, int, int, int );
246 typedef void (* FGCBMouseUC         )( int, int, int, int, FGCBUserData );
247 #define EXPAND_WCB_SUB_Mouse(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
248 typedef void (* FGCBMouseWheel      )( int, int, int, int );
249 typedef void (* FGCBMouseWheelUC    )( int, int, int, int, FGCBUserData );
250 #define EXPAND_WCB_SUB_MouseWheel(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
251 typedef void (* FGCBMotion          )( int, int );
252 typedef void (* FGCBMotionUC        )( int, int, FGCBUserData );
253 #define EXPAND_WCB_SUB_Motion(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
254 typedef void (* FGCBPassive         )( int, int );
255 typedef void (* FGCBPassiveUC       )( int, int, FGCBUserData );
256 #define EXPAND_WCB_SUB_Passive(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
257 typedef void (* FGCBEntry           )( int );
258 typedef void (* FGCBEntryUC         )( int, FGCBUserData );
259 #define EXPAND_WCB_SUB_Entry(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
260 typedef void (* FGCBWindowStatus    )( int );
261 typedef void (* FGCBWindowStatusUC  )( int, FGCBUserData );
262 #define EXPAND_WCB_SUB_WindowStatus(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
263 typedef void (* FGCBJoystick        )( unsigned int, int, int, int );
264 typedef void (* FGCBJoystickUC      )( unsigned int, int, int, int, FGCBUserData );
265 #define EXPAND_WCB_SUB_Joystick(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
266 typedef void (* FGCBOverlayDisplay  )( void );
267 typedef void (* FGCBOverlayDisplayUC)( FGCBUserData );
268 #define EXPAND_WCB_SUB_OverlayDisplay(args, userData) EXPAND_WCB_ZERO(args, userData)
269 typedef void (* FGCBSpaceMotion     )( int, int, int );
270 typedef void (* FGCBSpaceMotionUC   )( int, int, int, FGCBUserData );
271 #define EXPAND_WCB_SUB_SpaceMotion(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
272 typedef void (* FGCBSpaceRotation   )( int, int, int );
273 typedef void (* FGCBSpaceRotationUC )( int, int, int, FGCBUserData );
274 #define EXPAND_WCB_SUB_SpaceRotation(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
275 typedef void (* FGCBSpaceButton     )( int, int );
276 typedef void (* FGCBSpaceButtonUC   )( int, int, FGCBUserData );
277 #define EXPAND_WCB_SUB_SpaceButton(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
278 typedef void (* FGCBDials           )( int, int );
279 typedef void (* FGCBDialsUC         )( int, int, FGCBUserData );
280 #define EXPAND_WCB_SUB_Dials(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
281 typedef void (* FGCBButtonBox       )( int, int );
282 typedef void (* FGCBButtonBoxUC     )( int, int, FGCBUserData );
283 #define EXPAND_WCB_SUB_ButtonBox(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
284 typedef void (* FGCBTabletMotion    )( int, int );
285 typedef void (* FGCBTabletMotionUC  )( int, int, FGCBUserData );
286 #define EXPAND_WCB_SUB_TabletMotion(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
287 typedef void (* FGCBTabletButton    )( int, int, int, int );
288 typedef void (* FGCBTabletButtonUC  )( int, int, int, int, FGCBUserData );
289 #define EXPAND_WCB_SUB_TabletButton(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
290 typedef void (* FGCBDestroy         )( void );    /* Used for both window and menu destroy callbacks */
291 typedef void (* FGCBDestroyUC       )( FGCBUserData );
292 #define EXPAND_WCB_SUB_Destroy(args, userData) EXPAND_WCB_ZERO(args, userData)
293
294 typedef void (* FGCBMultiEntry      )( int, int );
295 typedef void (* FGCBMultiEntryUC    )( int, int, FGCBUserData );
296 #define EXPAND_WCB_SUB_MultiEntry(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
297 typedef void (* FGCBMultiButton     )( int, int, int, int, int );
298 typedef void (* FGCBMultiButtonUC   )( int, int, int, int, int, FGCBUserData );
299 #define EXPAND_WCB_SUB_MultiButton(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
300 typedef void (* FGCBMultiMotion     )( int, int, int );
301 typedef void (* FGCBMultiMotionUC   )( int, int, int, FGCBUserData );
302 #define EXPAND_WCB_SUB_MultiMotion(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
303 typedef void (* FGCBMultiPassive    )( int, int, int );
304 typedef void (* FGCBMultiPassiveUC  )( int, int, int, FGCBUserData );
305 #define EXPAND_WCB_SUB_MultiPassive(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
306
307 typedef void (* FGCBInitContext     )( void );
308 typedef void (* FGCBInitContextUC   )( FGCBUserData );
309 #define EXPAND_WCB_SUB_InitContext(args, userData) EXPAND_WCB_ZERO(args, userData)
310 typedef void (* FGCBAppStatus       )( int );
311 typedef void (* FGCBAppStatusUC     )( int, FGCBUserData );
312 #define EXPAND_WCB_SUB_AppStatus(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
313
314 /* The global callbacks type definitions */
315 typedef void (* FGCBIdle            )( void ); \
316 typedef void (* FGCBIdleUC          )( FGCBUserData );
317 typedef void (* FGCBTimer           )( int );
318 typedef void (* FGCBTimerUC         )( int, FGCBUserData );
319 typedef void (* FGCBMenuState       )( int );
320 typedef void (* FGCBMenuStatus      )( int, int, int );
321 typedef void (* FGCBMenuStatusUC    )( int, int, int, FGCBUserData );
322
323 /* The callback used when creating/using menus */
324 typedef void (* FGCBMenu            )( int );
325 typedef void (* FGCBMenuUC          )( int, FGCBUserData );
326
327 /* The FreeGLUT error/warning handler type definition */
328 typedef void (* FGError             )( const char *fmt, va_list ap );
329 typedef void (* FGErrorUC           )( const char *fmt, va_list ap, FGCBUserData userData );
330 typedef void (* FGWarning           )( const char *fmt, va_list ap );
331 typedef void (* FGWarningUC         )( const char *fmt, va_list ap, FGCBUserData userData );
332
333
334 /* A list structure */
335 typedef struct tagSFG_List SFG_List;
336 struct tagSFG_List
337 {
338     void *First;
339     void *Last;
340 };
341
342 /* A list node structure */
343 typedef struct tagSFG_Node SFG_Node;
344 struct tagSFG_Node
345 {
346     void *Next;
347     void *Prev;
348 };
349
350 /* A helper structure holding two ints and a boolean */
351 typedef struct tagSFG_XYUse SFG_XYUse;
352 struct tagSFG_XYUse
353 {
354     GLint           X, Y;               /* The two integers...               */
355     GLboolean       Use;                /* ...and a single boolean.          */
356 };
357
358 /*
359  * An enumeration containing the state of the GLUT execution:
360  * initializing, running, or stopping
361  */
362 typedef enum
363 {
364   GLUT_EXEC_STATE_INIT,
365   GLUT_EXEC_STATE_RUNNING,
366   GLUT_EXEC_STATE_STOP
367 } fgExecutionState ;
368
369 /* This structure holds different freeglut settings */
370 typedef struct tagSFG_State SFG_State;
371 struct tagSFG_State
372 {
373     SFG_XYUse        Position;             /* The default windows' position  */
374     SFG_XYUse        Size;                 /* The default windows' size      */
375     unsigned int     DisplayMode;          /* Display mode for new windows   */
376
377     GLboolean        Initialised;          /* freeglut has been initialised  */
378
379     int              DirectContext;        /* Direct rendering state         */
380
381     GLboolean        ForceIconic;          /* New top windows are iconified  */
382     GLboolean        UseCurrentContext;    /* New windows share with current */
383
384     GLboolean        GLDebugSwitch;        /* OpenGL state debugging switch  */
385     GLboolean        XSyncSwitch;          /* X11 sync protocol switch       */
386
387     int              KeyRepeat;            /* Global key repeat mode.        */
388     int              Modifiers;            /* Current ALT/SHIFT/CTRL state   */
389
390     GLuint           FPSInterval;          /* Interval between FPS printfs   */
391     GLuint           SwapCount;            /* Count of glutSwapBuffer calls  */
392     GLuint           SwapTime;             /* Time of last SwapBuffers       */
393
394     fg_time_t        Time;                 /* Time that glutInit was called  */
395     SFG_List         Timers;               /* The freeglut timer hooks       */
396     SFG_List         FreeTimers;           /* The unused timer hooks         */
397
398     FGCBIdleUC       IdleCallback;         /* The global idle callback       */
399     FGCBUserData     IdleCallbackData;     /* The global idle callback data  */
400
401     int              ActiveMenus;          /* Num. of currently active menus */
402     FGCBMenuState    MenuStateCallback;    /* Menu callbacks are global      */
403     FGCBMenuStatusUC MenuStatusCallback;
404     FGCBUserData     MenuStatusCallbackData;
405     void*            MenuFont;             /* Font to be used for newly created menus */
406
407     SFG_XYUse        GameModeSize;         /* Game mode screen's dimensions  */
408     int              GameModeDepth;        /* The pixel depth for game mode  */
409     int              GameModeRefresh;      /* The refresh rate for game mode */
410
411     int              ActionOnWindowClose;  /* Action when user closes window  */
412
413     fgExecutionState ExecState;            /* Used for GLUT termination       */
414     char            *ProgramName;          /* Name of the invoking program    */
415     GLboolean        JoysticksInitialised; /* Only initialize if application calls for them */
416     int              NumActiveJoysticks;   /* Number of active joysticks (callback defined and positive pollrate) -- if zero, don't poll joysticks */
417     GLboolean        InputDevsInitialised; /* Only initialize if application calls for them */
418
419         int              MouseWheelTicks;      /* Number of ticks the mouse wheel has turned */
420
421     int              AuxiliaryBufferNumber;/* Number of auxiliary buffers */
422     int              SampleNumber;         /*  Number of samples per pixel  */
423
424     GLboolean        SkipStaleMotion;      /* skip stale motion events */
425
426     GLboolean        StrokeFontDrawJoinDots;/* Draw dots between line segments of stroke fonts? */
427     GLboolean        AllowNegativeWindowPosition; /* GLUT, by default, doesn't allow negative window positions. Enable it? */
428
429     int              MajorVersion;         /* Major OpenGL context version  */
430     int              MinorVersion;         /* Minor OpenGL context version  */
431     int              ContextFlags;         /* OpenGL context flags          */
432     int              ContextProfile;       /* OpenGL context profile        */
433     int              HasOpenGL20;          /* fgInitGL2 could find all OpenGL 2.0 functions */
434     FGErrorUC        ErrorFunc;            /* User defined error handler    */
435     FGCBUserData     ErrorFuncData;        /* User defined error handler user data */
436     FGWarningUC      WarningFunc;          /* User defined warning handler  */
437     FGCBUserData     WarningFuncData;      /* User defined warning handler user data */
438 };
439
440 /* The structure used by display initialization in fg_init.c */
441 typedef struct tagSFG_Display SFG_Display;
442 struct tagSFG_Display
443 {
444         SFG_PlatformDisplay pDisplay;
445
446     int             ScreenWidth;        /* The screen's width in pixels      */
447     int             ScreenHeight;       /* The screen's height in pixels     */
448     int             ScreenWidthMM;      /* The screen's width in milimeters  */
449     int             ScreenHeightMM;     /* The screen's height in milimeters */
450 };
451
452
453 /* The user can create any number of timer hooks */
454 typedef struct tagSFG_Timer SFG_Timer;
455 struct tagSFG_Timer
456 {
457     SFG_Node        Node;
458     int             ID;                 /* The timer ID integer              */
459     FGCBTimerUC     Callback;           /* The timer callback                */
460     FGCBUserData    CallbackData;       /* The timer callback user data      */
461     fg_time_t       TriggerTime;        /* The timer trigger time            */
462 };
463
464 /*
465  * A window and its OpenGL context. The contents of this structure
466  * are highly dependent on the target operating system we aim at...
467  */
468 typedef struct tagSFG_Context SFG_Context;
469 struct tagSFG_Context
470 {
471     SFG_WindowHandleType  Handle;    /* The window's handle                 */
472     SFG_WindowContextType Context;   /* The window's OpenGL/WGL context     */
473
474         SFG_PlatformContext pContext;    /* The window's FBConfig (X11) or device context (Windows) */
475
476     int             DoubleBuffered;  /* Treat the window as double-buffered */
477
478     /* When drawing geometry to vertex attribute buffers, user specifies
479      * the attribute indices for vertices, normals and/or texture coords
480      * to freeglut. Those are stored here
481      */
482     GLint           attribute_v_coord;
483     GLint           attribute_v_normal;
484     GLint           attribute_v_texture;
485 };
486
487
488 /*
489  * Bitmasks indicating the different kinds of
490  * actions that can be scheduled for a window.
491  */
492 #define GLUT_INIT_WORK        (1<<0)
493 #define GLUT_VISIBILITY_WORK  (1<<1)
494 #define GLUT_POSITION_WORK    (1<<2)
495 #define GLUT_SIZE_WORK        (1<<3)
496 #define GLUT_ZORDER_WORK      (1<<4)
497 #define GLUT_FULL_SCREEN_WORK (1<<5)
498 #define GLUT_DISPLAY_WORK     (1<<6)
499
500 /*
501  * An enumeration containing the desired mapping state of a window
502  */
503 typedef enum
504 {
505   DesireHiddenState,
506   DesireIconicState,
507   DesireNormalState
508 } fgDesiredVisibility ;
509
510 /*
511  *  There is considerable confusion about the "right thing to
512  *  do" concerning window  size and position.  GLUT itself is
513  *  not consistent between Windows and UNIX/X11; since
514  *  platform independence is a virtue for "freeglut", we
515  *  decided to break with GLUT's behaviour.
516  *
517  *  Under UNIX/X11, it is apparently not possible to get the
518  *  window border sizes in order to subtract them off the
519  *  window's initial position until some time after the window
520  *  has been created.  Therefore we decided on the following
521  *  behaviour, both under Windows and under UNIX/X11:
522  *  - When you create a window with position (x,y) and size
523  *    (w,h), the upper left hand corner of the outside of the
524  *    window is at (x,y) and the size of the drawable area is
525  *    (w,h).
526  *  - When you query the size and position of the window--as
527  *    is happening here for Windows--"freeglut" will return
528  *    the size of the drawable area--the (w,h) that you
529  *    specified when you created the window--and the coordinates
530  *    of the upper left hand corner of the drawable area, i.e.
531  *    of the client rect--which is NOT the (x,y) you specified.
532  */
533 typedef struct tagSFG_WindowState SFG_WindowState;
534 struct tagSFG_WindowState   /* as per notes above, sizes always refer to the client area (thus without the window decorations) */
535 {
536     /* window state - size, position, look */
537     int             Xpos;               /* Window's top-left of client area, X-coordinate */
538     int             Ypos;               /* Window's top-left of client area, Y-coordinate */
539     int             Width;              /* Window's width in pixels          */
540     int             Height;             /* The same about the height         */
541     GLboolean       Visible;            /* Is the window visible now? Not using fgVisibilityState as we only care if visible or not */
542     int             Cursor;             /* The currently selected cursor style */
543     GLboolean       IsFullscreen;       /* is the window fullscreen?         */
544
545     /* FreeGLUT operations are deferred, that is, window moving, resizing,
546      * Z-order changing, making full screen or not do not happen immediately
547      * upon the user's request, but only in the next iteration of the main
548      * loop, before the display callback is called. This allows multiple
549      * reshape, position, etc requests to be combined into one and is
550      * compatible with the way GLUT does things. Callbacks get triggered
551      * based on the feedback/messages/notifications from the window manager.
552      * Below here we define what work should be done, as well as the relevant
553      * parameters for this work.
554      */
555     unsigned int    WorkMask;           /* work (resize, etc) to be done on the window */
556     int             DesiredXpos;        /* desired X location */
557     int             DesiredYpos;        /* desired Y location */
558     int             DesiredWidth;       /* desired window width */
559     int             DesiredHeight;      /* desired window height */
560     int             DesiredZOrder;      /* desired window Z Order position */
561     fgDesiredVisibility DesiredVisibility;/* desired visibility (hidden, iconic, shown/normal) */
562
563         SFG_PlatformWindowState pWState;    /* Window width/height (X11) or rectangle/style (Windows) from before a resize, and other stuff only needed on specific platforms */
564
565     long            JoystickPollRate;   /* The joystick polling rate         */
566     fg_time_t       JoystickLastPoll;   /* When the last poll happened       */
567
568     int             MouseX, MouseY;     /* The most recent mouse position    */
569
570     GLboolean       IgnoreKeyRepeat;    /* Whether to ignore key repeat.     */
571
572     GLboolean       VisualizeNormals;   /* When drawing objects, draw vectors representing the normals as well? */
573 };
574
575
576 /*
577  * A generic function pointer.  We should really use the GLUTproc type
578  * defined in freeglut_ext.h, but if we include that header in this file
579  * a bunch of other stuff (font-related) blows up!
580  */
581 typedef void (*SFG_Proc)();
582
583
584 /*
585  * SET_WCB() is used as:
586  *
587  *     SET_WCB( window, cbname, func, udata );
588  *
589  * ...where {window} is the freeglut window to set the callback,
590  *          {cbname} is the window-specific callback to set,
591  *          {func} is a function-pointer,
592  *          {udata} is a void* pointer for user data.
593  *
594  * Originally, {FETCH_WCB( ... ) = func} was rather sloppily used,
595  * but this can cause warnings because the FETCH_WCB() macro type-
596  * casts its result, and a type-cast value shouldn't be an lvalue.
597  *
598  * The {if( FETCH_WCB( ... ) != func )} test is to do type-checking
599  * and for no other reason.  Since it's hidden in the macro, the
600  * ugliness is felt to be rather benign.
601  */
602 #define SET_WCB(window,cbname,func,udata)                      \
603 do                                                             \
604 {                                                              \
605     if( FETCH_WCB( window, cbname ) != (SFG_Proc)(func) )      \
606     {                                                          \
607         (((window).CallBacks[WCB_ ## cbname]) = (SFG_Proc)(func)); \
608         (((window).CallbackDatas[WCB_ ## cbname]) = (udata));  \
609     }                                                          \
610 } while( 0 )
611
612 /*
613  * FETCH_WCB() is used as:
614  *
615  *     FETCH_WCB( window, cbname );
616  *
617  * ...where {window} is the freeglut window to fetch the callback from,
618  *          {cbname} is the window-specific callback to fetch.
619  *
620  * The result is correctly type-cast to the callback function pointer
621  * type.
622  */
623 #define FETCH_WCB(window,cbname) \
624     ((window).CallBacks[WCB_ ## cbname])
625
626 /*
627  * FETCH_USER_DATA_WCB() is used as:
628  * 
629  *     FETCH_USER_DATA_WCB( window, cbname );
630  *
631  * ...where {window} is the freeglut window,
632  *          {cbname} is the window-specific callback to be invoked,
633  *
634  * This expects a variable named "window" of type tagSFG_Window to exist.
635  */
636 /*
637  * FETCH_USER_DATA_WCB() is used as:
638  *
639  *     FETCH_USER_DATA_WCB( window, cbname );
640  *
641  * ...where {window} is the freeglut window to fetch the callback data from,
642  *          {cbname} is the window-specific callback data to fetch.
643  *
644  * The result is the callback data pointer.
645  */
646 #define FETCH_USER_DATA_WCB(window,cbname) \
647     ((window).CallbackDatas[WCB_ ## cbname])
648
649 #if 0
650 /*
651  * EXPAND_WCB() is used as:
652  * 
653  *     EXPAND_WCB arg_list
654  * 
655  * ... where {(arg_list)} is the parameter list.
656  *
657  * This will take the arg_list and extend it by one argument, adding
658  * the argument "userData" to the end of the list.
659  * 
660  * All additional args are to get around trailing ',', argument counts,
661  * and not needing a GCC extension to make this work.
662  *
663  * Minor modification of:
664  * http://stackoverflow.com/questions/5355241/generating-function-declaration-using-a-macro-iteration/5355946#5355946
665  *
666  * Supports up to five arguments
667  */
668
669 /* GCC-specific design that doesn't require per-callback defines */
670
671 #define PP_HAS_ARGS_IMPL2(_0, _1, _2, _3, _4, _5, N, ...) N
672 #define PP_HAS_ARGS_SOURCE() \
673     ONE_OR_MORE, ONE_OR_MORE, ONE_OR_MORE, ONE_OR_MORE, ONE_OR_MORE, ZERO
674
675 #define PP_HAS_ARGS_IMPL(...) \
676     PP_HAS_ARGS_IMPL2(__VA_ARGS__)
677 #define PP_HAS_ARGS(...) \
678     PP_HAS_ARGS_IMPL(NOT_EXIST, ##__VA_ARGS__, PP_HAS_ARGS_SOURCE())
679
680 #define EXPAND_WCB_ZERO(x) \
681     (userData)
682 #define EXPAND_WCB_ONE_OR_MORE(...) \
683     (__VA_ARGS__, userData)
684
685 #define EXPAND_WCB_DISAMBIGUATE2(has_args, ...) \
686     EXPAND_WCB_ ## has_args (__VA_ARGS__)
687 #define EXPAND_WCB_DISAMBIGUATE(has_args, ...) \
688     EXPAND_WCB_DISAMBIGUATE2(has_args, __VA_ARGS__)
689 #define EXPAND_WCB(...) \
690     EXPAND_WCB_DISAMBIGUATE(PP_HAS_ARGS(__VA_ARGS__), __VA_ARGS__)
691
692 #else
693 /*
694  * EXPAND_WCB() is used as:
695  * 
696  *     EXPAND_WCB( cbname )( arg_list, userData )
697  * 
698  * ... where {(arg_list)} is the parameter list and userData is user
699  * provided data.
700  *
701  * This will take the arg_list and extend it by one argument, adding
702  * the argument "userData" to the end of the list.
703  *
704  * In order for this to work, each callback must have a define that
705  * properly handles the arguments as needed by the callback.
706  * This callback is in the format of EXPAND_WCB_SUB_<cbname>.
707  * Two helper defines exist: EXPAND_WCB_ZERO and EXPAND_WCB_ONE_OR_MORE,
708  * each to handle callbacks that take zero-params (but take userData) and
709  * to take one or more params (along with userData).
710  * 
711  * An example for the "Entry" callback, where "Entry" is the cbname:
712  * typedef void (* FGCBEntry  )( int );
713  * typedef void (* FGCBEntryUC)( int, FGCBUserData );
714  * #define EXPAND_WCB_SUB_Entry(args, userData) EXPAND_WCB_ONE_OR_MORE(args, userData)
715  */
716 #define FG_COMPILER_SUPPORTS_VA_ARGS
717 #ifdef FG_COMPILER_SUPPORTS_VA_ARGS
718 #define EXPAND_WCB_UNPARAN(...) __VA_ARGS__
719 #else
720 #error "Compiler does not support varadic argument macros"
721 #endif
722
723 #define EXPAND_WCB_ZERO(args, userData) ( userData )
724 #define EXPAND_WCB_ONE_OR_MORE(args, userData) ( EXPAND_WCB_UNPARAN args, userData )
725
726 #define EXPAND_WCB(cbname) EXPAND_WCB_SUB_ ## cbname
727 #endif
728
729 /*
730  * INVOKE_WCB() is used as:
731  *
732  *     INVOKE_WCB( window, cbname, ( arg_list ) );
733  *
734  * ...where {window} is the freeglut window,
735  *          {cbname} is the window-specific callback to be invoked,
736  *          {(arg_list)} is the parameter list.
737  *
738  * The callback is invoked as:
739  *
740  *    callback( arg_list, userData );
741  *
742  * ...where userData is added to the arg_list, but the parentheses 
743  * are REQUIRED in the {arg_list}.
744  *
745  * NOTE that it does a sanity-check and also sets the
746  * current window.
747  *
748  */
749 #if TARGET_HOST_MS_WINDOWS && !defined(_WIN32_WCE) /* FIXME: also WinCE? */
750 #define INVOKE_WCB(window,cbname,arg_list)    \
751 do                                            \
752 {                                             \
753     if( FETCH_WCB( window, cbname ) )         \
754     {                                         \
755         FGCB ## cbname ## UC func = (FGCB ## cbname ## UC)(FETCH_WCB( window, cbname )); \
756         FGCBUserData userData = FETCH_USER_DATA_WCB( window, cbname ); \
757         fgSetWindow( &window );               \
758                 func EXPAND_WCB( cbname )( arg_list, userData ); \
759     }                                         \
760 } while( 0 )
761 #else
762 #define INVOKE_WCB(window,cbname,arg_list)    \
763 do                                            \
764 {                                             \
765     if( FETCH_WCB( window, cbname ) )         \
766     {                                         \
767         fgSetWindow( &window );               \
768         FGCBUserData userData = FETCH_USER_DATA_WCB( window, cbname ); \
769                 ((FGCB ## cbname ## UC)FETCH_WCB( window, cbname )) EXPAND_WCB( cbname )( arg_list, userData ); \
770     }                                         \
771 } while( 0 )
772 #endif
773
774 /*
775  * The window callbacks the user can supply us with. Should be kept portable.
776  *
777  * This enumeration provides the freeglut CallBack numbers.
778  * The symbolic constants are indices into a window's array of
779  * function callbacks.  The names are formed by splicing a common
780  * prefix onto the callback's base name.  (This was originally
781  * done so that an early stage of development could live side-by-
782  * side with the old callback code.  The old callback code used
783  * the bare callback's name as a structure member, so I used a
784  * prefix for the array index name.)
785  */
786 enum
787 {
788     WCB_Display,
789     WCB_Reshape,
790     WCB_Position,
791     WCB_Keyboard,
792     WCB_KeyboardUp,
793     WCB_Special,
794     WCB_SpecialUp,
795     WCB_Mouse,
796     WCB_MouseWheel,
797     WCB_Motion,
798     WCB_Passive,
799     WCB_Entry,
800     WCB_Visibility,
801     WCB_WindowStatus,
802     WCB_Joystick,
803     WCB_Destroy,
804
805     /* Multi-Pointer X and touch related */
806     WCB_MultiEntry,
807     WCB_MultiButton,
808     WCB_MultiMotion,
809     WCB_MultiPassive,
810
811     /* Mobile platforms LifeCycle */
812     WCB_InitContext,
813     WCB_AppStatus,
814
815     /* Presently ignored */
816     WCB_Select,
817     WCB_OverlayDisplay,
818     WCB_SpaceMotion,     /* presently implemented only on UNIX/X11 and Windows */
819     WCB_SpaceRotation,   /* presently implemented only on UNIX/X11 and Windows */
820     WCB_SpaceButton,     /* presently implemented only on UNIX/X11 and Windows */
821     WCB_Dials,
822     WCB_ButtonBox,
823     WCB_TabletMotion,
824     WCB_TabletButton,
825
826     /* Always make this the LAST one */
827     TOTAL_CALLBACKS
828 };
829
830
831 /* This structure holds the OpenGL rendering context for all the menu windows */
832 typedef struct tagSFG_MenuContext SFG_MenuContext;
833 struct tagSFG_MenuContext
834 {
835     SFG_WindowContextType MContext;       /* The menu window's WGL context   */
836 };
837
838 /* This structure describes a menu */
839 typedef struct tagSFG_Window SFG_Window;
840 typedef struct tagSFG_MenuEntry SFG_MenuEntry;
841 typedef struct tagSFG_Menu SFG_Menu;
842 struct tagSFG_Menu
843 {
844     SFG_Node            Node;
845     void               *UserData;     /* User data passed back at callback   */
846     int                 ID;           /* The global menu ID                  */
847     SFG_List            Entries;      /* The menu entries list               */
848     FGCBMenuUC          Callback;     /* The menu callback                   */
849     FGCBUserData        CallbackData; /* The menu callback user data         */
850     FGCBDestroyUC       Destroy;      /* Destruction callback                */
851     FGCBUserData        DestroyData;  /* Destruction callback user data      */
852     GLboolean           IsActive;     /* Is the menu selected?               */
853     void*               Font;         /* Font to be used for displaying this menu */
854     int                 Width;        /* Menu box width in pixels            */
855     int                 Height;       /* Menu box height in pixels           */
856     int                 X, Y;         /* Menu box raster position            */
857
858     SFG_MenuEntry      *ActiveEntry;  /* Currently active entry in the menu  */
859     SFG_Window         *Window;       /* Window for menu                     */
860     SFG_Window         *ParentWindow; /* Window in which the menu is invoked */
861 };
862
863 /* This is a menu entry */
864 struct tagSFG_MenuEntry
865 {
866     SFG_Node            Node;
867     int                 ID;                     /* The menu entry ID (local) */
868     int                 Ordinal;                /* The menu's ordinal number */
869     char*               Text;                   /* The text to be displayed  */
870     SFG_Menu*           SubMenu;                /* Optional sub-menu tree    */
871     GLboolean           IsActive;               /* Is the entry highlighted? */
872     int                 Width;                  /* Label's width in pixels   */
873 };
874
875 /*
876  * A window, making part of freeglut windows hierarchy.
877  * Should be kept portable.
878  *
879  * NOTE that ActiveMenu is set to menu itself if the window is a menu.
880  */
881 struct tagSFG_Window
882 {
883     SFG_Node            Node;
884     int                 ID;                     /* Window's ID number        */
885
886     SFG_Context         Window;                 /* Window and OpenGL context */
887     SFG_WindowState     State;                  /* The window state          */
888     SFG_Proc            CallBacks[ TOTAL_CALLBACKS ]; /* Array of window callbacks */
889     FGCBUserData        CallbackDatas[ TOTAL_CALLBACKS ]; /* Array of window callback datas */
890     void               *UserData ;              /* For use by user           */
891
892     SFG_Menu*           Menu[ FREEGLUT_MAX_MENUS ]; /* Menus appended to window  */
893     SFG_Menu*           ActiveMenu;             /* The window's active menu  */
894
895     SFG_Window*         Parent;                 /* The parent to this window */
896     SFG_List            Children;               /* The subwindows d.l. list  */
897
898     GLboolean           IsMenu;                 /* Set to 1 if we are a menu */
899 };
900
901
902 /* A linked list structure of windows */
903 typedef struct tagSFG_WindowList SFG_WindowList ;
904 struct tagSFG_WindowList
905 {
906     SFG_Node node;
907     SFG_Window *window ;
908 };
909
910 /* This holds information about all the windows, menus etc. */
911 typedef struct tagSFG_Structure SFG_Structure;
912 struct tagSFG_Structure
913 {
914     SFG_List        Windows;         /* The global windows list            */
915     SFG_List        Menus;           /* The global menus list              */
916     SFG_List        WindowsToDestroy;
917
918     SFG_Window*     CurrentWindow;   /* The currently set window          */
919     SFG_Menu*       CurrentMenu;     /* Same, but menu...                 */
920
921     SFG_MenuContext* MenuContext;    /* OpenGL rendering context for menus */
922
923     SFG_Window*      GameModeWindow; /* The game mode window               */
924
925     int              WindowID;       /* The window ID for the next window to be created */
926     int              MenuID;         /* The menu ID for the next menu to be created */
927 };
928
929 /*
930  * This structure is used for the enumeration purposes.
931  * You can easily extend its functionalities by declaring
932  * a structure containing enumerator's contents and custom
933  * data, then casting its pointer to (SFG_Enumerator *).
934  */
935 typedef struct tagSFG_Enumerator SFG_Enumerator;
936 struct tagSFG_Enumerator
937 {
938     GLboolean   found;                          /* Used to terminate search  */
939     void*       data;                           /* Custom data pointer       */
940 };
941 typedef void (* FGCBWindowEnumerator  )( SFG_Window *, SFG_Enumerator * );
942 typedef void (* FGCBMenuEnumerator  )( SFG_Menu *, SFG_Enumerator * );
943
944 /* The bitmap font structure */
945 typedef struct tagSFG_Font SFG_Font;
946 struct tagSFG_Font
947 {
948     char*           Name;         /* The source font name             */
949     int             Quantity;     /* Number of chars in font          */
950     int             Height;       /* Height of the characters         */
951     const GLubyte** Characters;   /* The characters mapping           */
952
953     float           xorig, yorig; /* Relative origin of the character */
954 };
955
956 /* The stroke font structures */
957
958 typedef struct tagSFG_StrokeVertex SFG_StrokeVertex;
959 struct tagSFG_StrokeVertex
960 {
961     GLfloat         X, Y;
962 };
963
964 typedef struct tagSFG_StrokeStrip SFG_StrokeStrip;
965 struct tagSFG_StrokeStrip
966 {
967     int             Number;
968     const SFG_StrokeVertex* Vertices;
969 };
970
971 typedef struct tagSFG_StrokeChar SFG_StrokeChar;
972 struct tagSFG_StrokeChar
973 {
974     GLfloat         Right;
975     int             Number;
976     const SFG_StrokeStrip* Strips;
977 };
978
979 typedef struct tagSFG_StrokeFont SFG_StrokeFont;
980 struct tagSFG_StrokeFont
981 {
982     char*           Name;                       /* The source font name      */
983     int             Quantity;                   /* Number of chars in font   */
984     GLfloat         Height;                     /* Height of the characters  */
985     const SFG_StrokeChar** Characters;          /* The characters mapping    */
986 };
987
988
989 /* -- JOYSTICK-SPECIFIC STRUCTURES AND TYPES ------------------------------- */
990 /*
991  * Initial defines from "js.h" starting around line 33 with the existing "fg_joystick.c"
992  * interspersed
993  */
994
995 #if TARGET_HOST_MACINTOSH
996 #    include <InputSprocket.h>
997 #endif
998
999 #if TARGET_HOST_MAC_OSX
1000 #    include <mach/mach.h>
1001 #    include <IOKit/IOkitLib.h>
1002 #    include <IOKit/hid/IOHIDLib.h>
1003 #endif
1004
1005 /* XXX It might be better to poll the operating system for the numbers of buttons and
1006  * XXX axes and then dynamically allocate the arrays.
1007  */
1008 #define _JS_MAX_BUTTONS 32
1009
1010 #if TARGET_HOST_MACINTOSH
1011 #    define _JS_MAX_AXES  9
1012 typedef struct tagSFG_PlatformJoystick SFG_PlatformJoystick;
1013 struct tagSFG_PlatformJoystick
1014 {
1015 #define  ISP_NUM_AXIS    9
1016 #define  ISP_NUM_NEEDS  41
1017     ISpElementReference isp_elem  [ ISP_NUM_NEEDS ];
1018     ISpNeed             isp_needs [ ISP_NUM_NEEDS ];
1019 };
1020 #endif
1021
1022 #if TARGET_HOST_MAC_OSX
1023 #    define _JS_MAX_AXES 16
1024 typedef struct tagSFG_PlatformJoystick SFG_PlatformJoystick;
1025 struct tagSFG_PlatformJoystick
1026 {
1027     IOHIDDeviceInterface ** hidDev;
1028     IOHIDElementCookie buttonCookies[41];
1029     IOHIDElementCookie axisCookies[_JS_MAX_AXES];
1030 /* The next two variables are not used anywhere */
1031 /*    long minReport[_JS_MAX_AXES],
1032  *         maxReport[_JS_MAX_AXES];
1033  */
1034 };
1035 #endif
1036
1037
1038 /*
1039  * Definition of "SFG_Joystick" structure -- based on JS's "jsJoystick" object class.
1040  * See "js.h" lines 80-178.
1041  */
1042 typedef struct tagSFG_Joystick SFG_Joystick;
1043 struct tagSFG_Joystick
1044 {
1045         SFG_PlatformJoystick pJoystick;
1046
1047     int          id;
1048     GLboolean    error;
1049     char         name [ 128 ];
1050     int          num_axes;
1051     int          num_buttons;
1052
1053     float dead_band[ _JS_MAX_AXES ];
1054     float saturate [ _JS_MAX_AXES ];
1055     float center   [ _JS_MAX_AXES ];
1056     float max      [ _JS_MAX_AXES ];
1057     float min      [ _JS_MAX_AXES ];
1058 };
1059
1060
1061
1062 /* -- GLOBAL VARIABLES EXPORTS --------------------------------------------- */
1063
1064 /* Freeglut display related stuff (initialized once per session) */
1065 extern SFG_Display fgDisplay;
1066
1067 /* Freeglut internal structure */
1068 extern SFG_Structure fgStructure;
1069
1070 /* The current freeglut settings */
1071 extern SFG_State fgState;
1072
1073
1074 /* -- PRIVATE FUNCTION DECLARATIONS ---------------------------------------- */
1075
1076 /*
1077  * A call to this function makes us sure that the Display and Structure
1078  * subsystems have been properly initialized and are ready to be used
1079  */
1080 #define  FREEGLUT_EXIT_IF_NOT_INITIALISED( string )               \
1081   if ( ! fgState.Initialised )                                    \
1082   {                                                               \
1083     fgError ( " ERROR:  Function <%s> called"                     \
1084               " without first calling 'glutInit'.", (string) ) ;  \
1085   }
1086
1087 #define  FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED( string )  \
1088   if ( ! fgState.Initialised )                                      \
1089   {                                                                 \
1090     fgError ( " ERROR:  Internal <%s> function called"              \
1091               " without first calling 'glutInit'.", (string) ) ;    \
1092   }
1093
1094 #define  FREEGLUT_INTERNAL_ERROR_EXIT( cond, string, function )  \
1095   if ( ! ( cond ) )                                              \
1096   {                                                              \
1097     fgError ( " ERROR:  Internal error <%s> in function %s",     \
1098               (string), (function) ) ;                           \
1099   }
1100
1101 /*
1102  * Following definitions are somewhat similar to GLib's,
1103  * but do not generate any log messages:
1104  */
1105 #define  freeglut_return_if_fail( expr ) \
1106     if( !(expr) )                        \
1107         return;
1108 #define  freeglut_return_val_if_fail( expr, val ) \
1109     if( !(expr) )                                 \
1110         return val ;
1111
1112 /*
1113  * A call to those macros assures us that there is a current
1114  * window set, respectively:
1115  */
1116 #define  FREEGLUT_EXIT_IF_NO_WINDOW( string )                               \
1117   if ( ! fgStructure.CurrentWindow &&                                       \
1118        ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION ) )  \
1119   {                                                                         \
1120     fgError ( " ERROR:  Function <%s> called"                               \
1121               " with no current window defined.", (string) ) ;              \
1122   }
1123
1124 /*
1125  * The deinitialize function gets called on glutMainLoop() end. It should clean up
1126  * everything inside of the freeglut
1127  */
1128 void fgDeinitialize( void );
1129
1130 /*
1131  * Those two functions are used to create/destroy the freeglut internal
1132  * structures. This actually happens when calling glutInit() and when
1133  * quitting the glutMainLoop() (which actually happens, when all windows
1134  * have been closed).
1135  */
1136 void fgCreateStructure( void );
1137 void fgDestroyStructure( void );
1138
1139 /*
1140  * Window creation, opening, closing and destruction.
1141  * Also CallBack clearing/initialization.
1142  * Defined in fg_structure.c, fg_window.c.
1143  */
1144 SFG_Window* fgCreateWindow( SFG_Window* parent, const char* title,
1145                             GLboolean positionUse, int x, int y,
1146                             GLboolean sizeUse, int w, int h,
1147                             GLboolean gameMode, GLboolean isMenu );
1148 void        fgSetWindow ( SFG_Window *window );
1149 void        fgOpenWindow( SFG_Window* window, const char* title,
1150                           GLboolean positionUse, int x, int y,
1151                           GLboolean sizeUse, int w, int h,
1152                           GLboolean gameMode, GLboolean isSubWindow );
1153 void        fgCloseWindow( SFG_Window* window );
1154 void        fgAddToWindowDestroyList ( SFG_Window* window );
1155 void        fgCloseWindows ();
1156 void        fgDestroyWindow( SFG_Window* window );
1157
1158 /* Menu creation and destruction. Defined in fg_structure.c */
1159 SFG_Menu*   fgCreateMenu( FGCBMenuUC menuCallback, FGCBUserData userData );
1160 void        fgDestroyMenu( SFG_Menu* menu );
1161
1162 /* Joystick device management functions, defined in fg_joystick.c */
1163 int         fgJoystickDetect( void );
1164 void        fgInitialiseJoysticks( void );
1165 void        fgJoystickClose( void );
1166 void        fgJoystickPollWindow( SFG_Window* window );
1167
1168 /* InputDevice Initialisation and Closure */
1169 int         fgInputDeviceDetect( void );
1170 void        fgInitialiseInputDevices( void );
1171 void        fgInputDeviceClose( void );
1172
1173 /* spaceball device functions, defined in fg_spaceball.c */
1174 void        fgInitialiseSpaceball( void );
1175 void        fgSpaceballClose( void );
1176 void        fgSpaceballSetWindow( SFG_Window *window );
1177
1178 int         fgHasSpaceball( void );
1179 int         fgSpaceballNumButtons( void );
1180
1181 /* Setting the cursor for a given window */
1182 void fgSetCursor ( SFG_Window *window, int cursorID );
1183
1184 /*
1185  * Helper function to enumerate through all registered windows
1186  * and one to enumerate all of a window's subwindows...
1187  *
1188  * The GFunc callback for those functions will be defined as:
1189  *
1190  *      void enumCallback( gpointer window, gpointer enumerator );
1191  *
1192  * where window is the enumerated (sub)window pointer (SFG_Window *),
1193  * and userData is the a custom user-supplied pointer. Functions
1194  * are defined and exported from fg_structure.c file.
1195  */
1196 void fgEnumWindows( FGCBWindowEnumerator enumCallback, SFG_Enumerator* enumerator );
1197 void fgEnumSubWindows( SFG_Window* window, FGCBWindowEnumerator enumCallback,
1198                        SFG_Enumerator* enumerator );
1199
1200 /*
1201  * fgWindowByHandle returns a (SFG_Window *) value pointing to the
1202  * first window in the queue matching the specified window handle.
1203  * The function is defined in fg_structure.c file.
1204  */
1205 SFG_Window* fgWindowByHandle( SFG_WindowHandleType hWindow );
1206
1207 /*
1208  * This function is similar to the previous one, except it is
1209  * looking for a specified (sub)window identifier. The function
1210  * is defined in fg_structure.c file.
1211  */
1212 SFG_Window* fgWindowByID( int windowID );
1213
1214 /*
1215  * Looks up a menu given its ID. This is easier than fgWindowByXXX
1216  * as all menus are placed in a single doubly linked list...
1217  */
1218 SFG_Menu* fgMenuByID( int menuID );
1219
1220 /*
1221  * Returns active menu, if any. Assumption: only one menu active throughout application at any one time.
1222  * This is easier than fgWindowByXXX as all menus are placed in one doubly linked list...
1223  */
1224 SFG_Menu* fgGetActiveMenu( );
1225
1226 /*
1227  * The menu activation and deactivation the code. This is the meat
1228  * of the menu user interface handling code...
1229  */
1230 void fgUpdateMenuHighlight ( SFG_Menu *menu );
1231 GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,
1232                               int mouse_x, int mouse_y );
1233 void fgDeactivateMenu( SFG_Window *window );
1234
1235 /*
1236  * This function gets called just before the buffers swap, so that
1237  * freeglut can display the pull-down menus via OpenGL. The function
1238  * is defined in fg_menu.c file.
1239  */
1240 void fgDisplayMenu( void );
1241
1242 /* Elapsed time as per glutGet(GLUT_ELAPSED_TIME). */
1243 fg_time_t fgElapsedTime( void );
1244
1245 /* System time in milliseconds */
1246 fg_time_t fgSystemTime(void);
1247
1248 /* List functions */
1249 void fgListInit(SFG_List *list);
1250 void fgListAppend(SFG_List *list, SFG_Node *node);
1251 void fgListRemove(SFG_List *list, SFG_Node *node);
1252 int fgListLength(SFG_List *list);
1253 void fgListInsert(SFG_List *list, SFG_Node *next, SFG_Node *node);
1254
1255 /* Error Message functions */
1256 void fgError( const char *fmt, ... );
1257 void fgWarning( const char *fmt, ... );
1258
1259 SFG_Proc fgPlatformGetProcAddress( const char *procName );
1260
1261 /* pushing attribute/value pairs into an array */
1262 #define ATTRIB(a) attributes[where++]=(a)
1263 #define ATTRIB_VAL(a,v) {ATTRIB(a); ATTRIB(v);}
1264
1265 int fghMapBit( int mask, int from, int to );
1266 int fghIsLegacyContextRequested( void );
1267 void fghContextCreationError( void );
1268 int fghNumberOfAuxBuffersRequested( void );
1269
1270 #endif /* FREEGLUT_INTERNAL_H */
1271
1272 /*** END OF FILE ***/