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