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