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