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