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