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