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