- moving to a new way of handling window changes (position, size, visibility)
[freeglut] / src / fg_internal.h
1 /*
2  * fg_internal.h
3  *
4  * The freeglut library private include file.
5  *
6  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7  * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8  * Creation date: Thu Dec 2 1999
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27
28 #ifndef  FREEGLUT_INTERNAL_H
29 #define  FREEGLUT_INTERNAL_H
30
31 #ifdef HAVE_CONFIG_H
32 #    include "config.h"
33 #endif
34
35 /* Freeglut is intended to function under all Unix/X11 and Win32 platforms. */
36 /* XXX: Don't all MS-Windows compilers (except Cygwin) have _WIN32 defined?
37  * XXX: If so, remove the first set of defined()'s below.
38  */
39 #if !defined(TARGET_HOST_POSIX_X11) && !defined(TARGET_HOST_MS_WINDOWS) && !defined(TARGET_HOST_MAC_OSX) && !defined(TARGET_HOST_SOLARIS)
40 #if defined(_MSC_VER) || defined(__WATCOMC__) || defined(__MINGW32__) \
41     || defined(_WIN32) || defined(_WIN32_WCE) \
42     || ( defined(__CYGWIN__) && defined(X_DISPLAY_MISSING) )
43 #   define  TARGET_HOST_MS_WINDOWS 1
44
45 #elif defined (__ANDROID__)
46 #   define  TARGET_HOST_ANDROID  1
47
48 #elif defined(__posix__) || defined(__unix__) || defined(__linux__) || defined(__sun)
49 #   define  TARGET_HOST_POSIX_X11  1
50
51 #elif defined(__APPLE__)
52 /* This is a placeholder until we get native OSX support ironed out -- JFF 11/18/09 */
53 #   define  TARGET_HOST_POSIX_X11  1
54 /* #   define  TARGET_HOST_MAC_OSX    1 */
55
56 #else
57 #   error "Unrecognized target host!"
58
59 #endif
60 #endif
61
62 /* Detect both SunPro and gcc compilers on Sun Solaris */
63 #if defined (__SVR4) && defined (__sun)
64 #   define TARGET_HOST_SOLARIS 1
65 #endif
66
67 #ifndef TARGET_HOST_MS_WINDOWS
68 #   define  TARGET_HOST_MS_WINDOWS 0
69 #endif
70
71 #ifndef  TARGET_HOST_POSIX_X11
72 #   define  TARGET_HOST_POSIX_X11  0
73 #endif
74
75 #ifndef  TARGET_HOST_MAC_OSX
76 #   define  TARGET_HOST_MAC_OSX    0
77 #endif
78
79 #ifndef  TARGET_HOST_SOLARIS
80 #   define  TARGET_HOST_SOLARIS    0
81 #endif
82
83 /* -- FIXED CONFIGURATION LIMITS ------------------------------------------- */
84
85 #define  FREEGLUT_MAX_MENUS         3
86
87 /* These files should be available on every platform. */
88 #include <stdio.h>
89 #include <string.h>
90 #include <math.h>
91 #include <stdlib.h>
92 #include <stdarg.h>
93
94 /* These are included based on autoconf directives. */
95 #ifdef HAVE_SYS_TYPES_H
96 #    include <sys/types.h>
97 #endif
98 #ifdef HAVE_UNISTD_H
99 #    include <unistd.h>
100 #endif
101 #ifdef TIME_WITH_SYS_TIME
102 #    include <sys/time.h>
103 #    include <time.h>
104 #elif defined(HAVE_SYS_TIME_H)
105 #    include <sys/time.h>
106 #else
107 #    include <time.h>
108 #endif
109
110 /* -- AUTOCONF HACKS --------------------------------------------------------*/
111
112 /* XXX: Update autoconf to avoid these.
113  * XXX: Are non-POSIX platforms intended not to use autoconf?
114  * If so, perhaps there should be a config_guess.h for them. Alternatively,
115  * config guesses could be placed above, just after the config.h exclusion.
116  */
117 #if defined(__FreeBSD__) || defined(__NetBSD__)
118 #    define HAVE_USB_JS 1
119 #    if defined(__NetBSD__) || ( defined(__FreeBSD__) && __FreeBSD_version >= 500000)
120 #        define HAVE_USBHID_H 1
121 #    endif
122 #endif
123
124 #if defined(_MSC_VER) || defined(__WATCOMC__)
125 /* strdup() is non-standard, for all but POSIX-2001 */
126 #define strdup   _strdup
127 #endif
128
129 /* M_PI is non-standard (defined by BSD, not ISO-C) */
130 #ifndef M_PI
131 #    define  M_PI  3.14159265358979323846
132 #endif
133
134 #ifdef HAVE_STDBOOL_H
135 #    include <stdbool.h>
136 #    ifndef TRUE
137 #        define TRUE true
138 #    endif
139 #    ifndef FALSE
140 #        define FALSE false
141 #    endif
142 #else
143 #    ifndef TRUE
144 #        define  TRUE  1
145 #    endif
146 #    ifndef FALSE
147 #        define  FALSE  0
148 #    endif
149 #endif
150
151 /* General defines */
152 #define INVALID_MODIFIERS 0xffffffff
153
154 /* FreeGLUT internal time type */
155 #if defined(HAVE_STDINT_H)
156 #   include <stdint.h>
157     typedef uint64_t fg_time_t;
158 #elif defined(HAVE_INTTYPES_H)
159 #   include <inttypes.h>
160     typedef uint64_t fg_time_t;
161 #elif defined(HAVE_U__INT64)
162     typedef unsigned __int64 fg_time_t;
163 #elif defined(HAVE_ULONG_LONG)
164     typedef unsigned long long fg_time_t;
165 #else
166     typedef unsigned long fg_time_t;
167 #endif
168
169 #ifndef __fg_unused
170 # ifdef __GNUC__
171 #  define __fg_unused __attribute__((unused))
172 # else
173 #  define __fg_unused
174 # endif
175 #endif
176
177 /* Platform-specific includes */
178 #if TARGET_HOST_POSIX_X11
179 #include "x11/fg_internal_x11.h"
180 #endif
181 #if TARGET_HOST_MS_WINDOWS
182 #include "mswin/fg_internal_mswin.h"
183 #endif
184 #if TARGET_HOST_ANDROID
185 #include "android/fg_internal_android.h"
186 #endif
187
188
189 /* -- GLOBAL TYPE DEFINITIONS ---------------------------------------------- */
190
191 /* Freeglut callbacks type definitions */
192 typedef void (* FGCBDisplay       )( void );
193 typedef void (* FGCBReshape       )( int, int );
194 typedef void (* FGCBPosition      )( int, int );
195 typedef void (* FGCBVisibility    )( int );
196 typedef void (* FGCBKeyboard      )( unsigned char, int, int );
197 typedef void (* FGCBKeyboardUp    )( unsigned char, int, int );
198 typedef void (* FGCBSpecial       )( int, int, int );
199 typedef void (* FGCBSpecialUp     )( int, int, int );
200 typedef void (* FGCBMouse         )( int, int, int, int );
201 typedef void (* FGCBMouseWheel    )( int, int, int, int );
202 typedef void (* FGCBMotion        )( int, int );
203 typedef void (* FGCBPassive       )( int, int );
204 typedef void (* FGCBEntry         )( int );
205 typedef void (* FGCBWindowStatus  )( int );
206 typedef void (* FGCBJoystick      )( unsigned int, int, int, int );
207 typedef void (* FGCBOverlayDisplay)( void );
208 typedef void (* FGCBSpaceMotion   )( int, int, int );
209 typedef void (* FGCBSpaceRotation )( int, int, int );
210 typedef void (* FGCBSpaceButton   )( int, int );
211 typedef void (* FGCBDials         )( int, int );
212 typedef void (* FGCBButtonBox     )( int, int );
213 typedef void (* FGCBTabletMotion  )( int, int );
214 typedef void (* FGCBTabletButton  )( int, int, int, int );
215 typedef void (* FGCBDestroy       )( void );    /* Used for both window and menu destroy callbacks */
216
217 typedef void (* FGCBMultiEntry   )( int, int );
218 typedef void (* FGCBMultiButton  )( int, int, int, int, int );
219 typedef void (* FGCBMultiMotion  )( int, int, int );
220 typedef void (* FGCBMultiPassive )( int, int, int );
221
222 typedef void (* FGCBInitContext)();
223 typedef void (* FGCBPause)();
224 typedef void (* FGCBResume)();
225
226 /* The global callbacks type definitions */
227 typedef void (* FGCBIdle          )( void );
228 typedef void (* FGCBTimer         )( int );
229 typedef void (* FGCBMenuState     )( int );
230 typedef void (* FGCBMenuStatus    )( int, int, int );
231
232 /* The callback used when creating/using menus */
233 typedef void (* FGCBMenu          )( int );
234
235 /* The FreeGLUT error/warning handler type definition */
236 typedef void (* FGError           ) ( const char *fmt, va_list ap);
237 typedef void (* FGWarning         ) ( const char *fmt, va_list ap);
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     fg_time_t        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     int              NumActiveJoysticks;    /* Number of active joysticks (callback defined and positive pollrate) -- if zero, don't poll joysticks */
320     GLboolean        InputDevsInitialised;  /* Only initialize if application calls for them */
321
322         int              MouseWheelTicks;      /* Number of ticks the mouse wheel has turned */
323
324     int              AuxiliaryBufferNumber;  /* Number of auxiliary buffers */
325     int              SampleNumber;         /*  Number of samples per pixel  */
326
327     GLboolean        SkipStaleMotion;      /* skip stale motion events */
328
329     int              MajorVersion;         /* Major OpenGL context version  */
330     int              MinorVersion;         /* Minor OpenGL context version  */
331     int              ContextFlags;         /* OpenGL context flags          */
332     int              ContextProfile;       /* OpenGL context profile        */
333     int              HasOpenGL20;          /* fgInitGL2 could find all OpenGL 2.0 functions */
334     FGError          ErrorFunc;            /* User defined error handler    */
335     FGWarning        WarningFunc;          /* User defined warning handler  */
336 };
337
338 /* The structure used by display initialization in freeglut_init.c */
339 typedef struct tagSFG_Display SFG_Display;
340 struct tagSFG_Display
341 {
342         SFG_PlatformDisplay pDisplay;
343
344     int             ScreenWidth;        /* The screen's width in pixels      */
345     int             ScreenHeight;       /* The screen's height in pixels     */
346     int             ScreenWidthMM;      /* The screen's width in milimeters  */
347     int             ScreenHeightMM;     /* The screen's height in milimeters */
348 };
349
350
351 /* The user can create any number of timer hooks */
352 typedef struct tagSFG_Timer SFG_Timer;
353 struct tagSFG_Timer
354 {
355     SFG_Node        Node;
356     int             ID;                 /* The timer ID integer              */
357     FGCBTimer       Callback;           /* The timer callback                */
358     fg_time_t       TriggerTime;        /* The timer trigger time            */
359 };
360
361 /*
362  * A window and its OpenGL context. The contents of this structure
363  * are highly dependant on the target operating system we aim at...
364  */
365 typedef struct tagSFG_Context SFG_Context;
366 struct tagSFG_Context
367 {
368     SFG_WindowHandleType  Handle;    /* The window's handle                 */
369     SFG_WindowContextType Context;   /* The window's OpenGL/WGL context     */
370
371         SFG_PlatformContext pContext;    /* The window's FBConfig (X11) or device context (Windows) */
372
373     int             DoubleBuffered;  /* Treat the window as double-buffered */
374
375     /* When drawing geometry to vertex attribute buffers, user specifies 
376      * the attribute indices for vertices, normals and/or texture coords
377      * to freeglut. Those are stored here
378      */
379     GLint           attribute_v_coord;
380     GLint           attribute_v_normal;
381     GLint           attribute_v_texture;
382 };
383
384
385 /*
386  * Bitmasks indicating the different kinds of
387  * actions that can be scheduled for a window.
388  */
389 #define GLUT_INIT_WORK        (1<<0)
390 #define GLUT_VISIBILITY_WORK  (1<<1)
391 #define GLUT_POSITION_WORK    (1<<2)
392 #define GLUT_SIZE_WORK        (1<<3)
393 #define GLUT_ZORDER_WORK      (1<<4)
394 #define GLUT_FULL_SCREEN_WORK (1<<5)
395
396 /*
397  * An enumeration containing the state of the GLUT execution:
398  * initializing, running, or stopping
399  */
400 typedef enum
401 {
402   DesireHiddenState,
403   DesireIconicState,
404   DesireNormalState
405 } fgDesiredVisibility ;
406
407 /*
408  *  There is considerable confusion about the "right thing to
409  *  do" concerning window  size and position.  GLUT itself is
410  *  not consistent between Windows and UNIX/X11; since
411  *  platform independence is a virtue for "freeglut", we
412  *  decided to break with GLUT's behaviour.
413  *
414  *  Under UNIX/X11, it is apparently not possible to get the
415  *  window border sizes in order to subtract them off the
416  *  window's initial position until some time after the window
417  *  has been created.  Therefore we decided on the following
418  *  behaviour, both under Windows and under UNIX/X11:
419  *  - When you create a window with position (x,y) and size
420  *    (w,h), the upper left hand corner of the outside of the
421  *    window is at (x,y) and the size of the drawable area is
422  *    (w,h).
423  *  - When you query the size and position of the window--as
424  *    is happening here for Windows--"freeglut" will return
425  *    the size of the drawable area--the (w,h) that you
426  *    specified when you created the window--and the coordinates
427  *    of the upper left hand corner of the drawable area, i.e.
428  *    of the client rect--which is NOT the (x,y) you specified.
429  */
430 typedef struct tagSFG_WindowState SFG_WindowState;
431 struct tagSFG_WindowState   /* as per notes above, sizes always refer to the client area (thus without the window decorations) */
432 {
433     /* window state - size, position, look */
434     int             Xpos;               /* Window's top-left of client area, X-coordinate */
435     int             Ypos;               /* Window's top-left of client area, Y-coordinate */
436     int             Width;              /* Window's width in pixels          */
437     int             Height;             /* The same about the height         */
438     GLboolean       Visible;            /* Is the window visible now? Not using fgVisibilityState as we only care if visible or not */
439     int             Cursor;             /* The currently selected cursor style */
440     GLboolean       IsFullscreen;       /* is the window fullscreen?         */
441
442     /* FreeGLUT operations are deferred, that is, window moving, resizing,
443      * Z-order changing, making full screen or not do not happen immediately
444      * upon the user's request, but only in the next iteration of the main
445      * loop, before the display callback is called. This allows multiple
446      * reshape, position, etc requests to be combined into one and is
447      * compatible with the way GLUT does things. Callbacks get triggered
448      * based on the feedback/messages/notifications from the window manager.
449      * Below here we define what work should be done, as well as the relevant
450      * parameters for this work.
451      */
452     unsigned int    WorkMask;           /* work (resize, etc) to be done on the window */
453     int             DesiredXpos;        /* desired X location */
454     int             DesiredYpos;        /* desired Y location */
455     int             DesiredWidth;       /* desired window width */
456     int             DesiredHeight;      /* desired window height */
457     int             DesiredZOrder;      /* desired window Z Order position */
458     fgDesiredVisibility DesiredVisibility;/* desired visibility (hidden, iconic, shown/normal) */
459
460
461         SFG_PlatformWindowState pWState;    /* Window width/height (X11) or rectangle/style (Windows) from before a resize, and other stuff only needed on specific platforms */
462
463     GLboolean       Redisplay;          /* Do we have to redisplay?          */
464
465
466     long            JoystickPollRate;   /* The joystick polling rate         */
467     fg_time_t       JoystickLastPoll;   /* When the last poll happened       */
468
469     int             MouseX, MouseY;     /* The most recent mouse position    */
470
471     GLboolean       IgnoreKeyRepeat;    /* Whether to ignore key repeat.     */
472
473     GLboolean       VisualizeNormals;   /* When drawing objects, draw vectors representing the normals as well? */
474 };
475
476
477 /*
478  * A generic function pointer.  We should really use the GLUTproc type
479  * defined in freeglut_ext.h, but if we include that header in this file
480  * a bunch of other stuff (font-related) blows up!
481  */
482 typedef void (*SFG_Proc)();
483
484
485 /*
486  * SET_WCB() is used as:
487  *
488  *     SET_WCB( window, cbname, func );
489  *
490  * ...where {window} is the freeglut window to set the callback,
491  *          {cbname} is the window-specific callback to set,
492  *          {func} is a function-pointer.
493  *
494  * Originally, {FETCH_WCB( ... ) = func} was rather sloppily used,
495  * but this can cause warnings because the FETCH_WCB() macro type-
496  * casts its result, and a type-cast value shouldn't be an lvalue.
497  *
498  * The {if( FETCH_WCB( ... ) != func )} test is to do type-checking
499  * and for no other reason.  Since it's hidden in the macro, the
500  * ugliness is felt to be rather benign.
501  */
502 #define SET_WCB(window,cbname,func)                            \
503 do                                                             \
504 {                                                              \
505     if( FETCH_WCB( window, cbname ) != (SFG_Proc)(func) )      \
506         (((window).CallBacks[WCB_ ## cbname]) = (SFG_Proc)(func)); \
507 } while( 0 )
508
509 /*
510  * FETCH_WCB() is used as:
511  *
512  *     FETCH_WCB( window, cbname );
513  *
514  * ...where {window} is the freeglut window to fetch the callback from,
515  *          {cbname} is the window-specific callback to fetch.
516  *
517  * The result is correctly type-cast to the callback function pointer
518  * type.
519  */
520 #define FETCH_WCB(window,cbname) \
521     ((window).CallBacks[WCB_ ## cbname])
522
523 /*
524  * INVOKE_WCB() is used as:
525  *
526  *     INVOKE_WCB( window, cbname, ( arg_list ) );
527  *
528  * ...where {window} is the freeglut window,
529  *          {cbname} is the window-specific callback to be invoked,
530  *          {(arg_list)} is the parameter list.
531  *
532  * The callback is invoked as:
533  *
534  *    callback( arg_list );
535  *
536  * ...so the parentheses are REQUIRED in the {arg_list}.
537  *
538  * NOTE that it does a sanity-check and also sets the
539  * current window.
540  *
541  */
542 #if TARGET_HOST_MS_WINDOWS && !defined(_WIN32_WCE) /* FIXME: also WinCE? */
543 #define INVOKE_WCB(window,cbname,arg_list)    \
544 do                                            \
545 {                                             \
546     if( FETCH_WCB( window, cbname ) )         \
547     {                                         \
548         FGCB ## cbname func = (FGCB ## cbname)(FETCH_WCB( window, cbname )); \
549         fgSetWindow( &window );               \
550         func arg_list;                        \
551     }                                         \
552 } while( 0 )
553 #else
554 #define INVOKE_WCB(window,cbname,arg_list)    \
555 do                                            \
556 {                                             \
557     if( FETCH_WCB( window, cbname ) )         \
558     {                                         \
559         fgSetWindow( &window );               \
560         ((FGCB ## cbname)FETCH_WCB( window, cbname )) arg_list; \
561     }                                         \
562 } while( 0 )
563 #endif
564
565 /*
566  * The window callbacks the user can supply us with. Should be kept portable.
567  *
568  * This enumeration provides the freeglut CallBack numbers.
569  * The symbolic constants are indices into a window's array of
570  * function callbacks.  The names are formed by splicing a common
571  * prefix onto the callback's base name.  (This was originally
572  * done so that an early stage of development could live side-by-
573  * side with the old callback code.  The old callback code used
574  * the bare callback's name as a structure member, so I used a
575  * prefix for the array index name.)
576  */
577 enum
578 {
579     WCB_Display,
580     WCB_Reshape,
581     WCB_Position,
582     WCB_Keyboard,
583     WCB_KeyboardUp,
584     WCB_Special,
585     WCB_SpecialUp,
586     WCB_Mouse,
587     WCB_MouseWheel,
588     WCB_Motion,
589     WCB_Passive,
590     WCB_Entry,
591     WCB_Visibility,
592     WCB_WindowStatus,
593     WCB_Joystick,
594     WCB_Destroy,
595
596     /* Multi-Pointer X and touch related */
597     WCB_MultiEntry,
598     WCB_MultiButton,
599     WCB_MultiMotion,
600     WCB_MultiPassive,
601
602     /* Mobile platforms LifeCycle */
603     WCB_InitContext,
604     WCB_Pause,
605     WCB_Resume,
606
607     /* Presently ignored */
608     WCB_Select,
609     WCB_OverlayDisplay,
610     WCB_SpaceMotion,     /* presently implemented only on UNIX/X11 */
611     WCB_SpaceRotation,   /* presently implemented only on UNIX/X11 */
612     WCB_SpaceButton,     /* presently implemented only on UNIX/X11 */
613     WCB_Dials,
614     WCB_ButtonBox,
615     WCB_TabletMotion,
616     WCB_TabletButton,
617
618     /* Always make this the LAST one */
619     TOTAL_CALLBACKS
620 };
621
622
623 /* This structure holds the OpenGL rendering context for all the menu windows */
624 typedef struct tagSFG_MenuContext SFG_MenuContext;
625 struct tagSFG_MenuContext
626 {
627     SFG_WindowContextType MContext;       /* The menu window's WGL context   */
628 };
629
630 /* This structure describes a menu */
631 typedef struct tagSFG_Window SFG_Window;
632 typedef struct tagSFG_MenuEntry SFG_MenuEntry;
633 typedef struct tagSFG_Menu SFG_Menu;
634 struct tagSFG_Menu
635 {
636     SFG_Node            Node;
637     void               *UserData;     /* User data passed back at callback   */
638     int                 ID;           /* The global menu ID                  */
639     SFG_List            Entries;      /* The menu entries list               */
640     FGCBMenu            Callback;     /* The menu callback                   */
641     FGCBDestroy         Destroy;      /* Destruction callback                */
642     GLboolean           IsActive;     /* Is the menu selected?               */
643     int                 Width;        /* Menu box width in pixels            */
644     int                 Height;       /* Menu box height in pixels           */
645     int                 X, Y;         /* Menu box raster position            */
646
647     SFG_MenuEntry      *ActiveEntry;  /* Currently active entry in the menu  */
648     SFG_Window         *Window;       /* Window for menu                     */
649     SFG_Window         *ParentWindow; /* Window in which the menu is invoked */
650 };
651
652 /* This is a menu entry */
653 struct tagSFG_MenuEntry
654 {
655     SFG_Node            Node;
656     int                 ID;                     /* The menu entry ID (local) */
657     int                 Ordinal;                /* The menu's ordinal number */
658     char*               Text;                   /* The text to be displayed  */
659     SFG_Menu*           SubMenu;                /* Optional sub-menu tree    */
660     GLboolean           IsActive;               /* Is the entry highlighted? */
661     int                 Width;                  /* Label's width in pixels   */
662 };
663
664 /*
665  * A window, making part of freeglut windows hierarchy.
666  * Should be kept portable.
667  *
668  * NOTE that ActiveMenu is set to menu itself if the window is a menu.
669  */
670 struct tagSFG_Window
671 {
672     SFG_Node            Node;
673     int                 ID;                     /* Window's ID number        */
674
675     SFG_Context         Window;                 /* Window and OpenGL context */
676     SFG_WindowState     State;                  /* The window state          */
677     SFG_Proc            CallBacks[ TOTAL_CALLBACKS ]; /* Array of window callbacks */
678     void               *UserData ;              /* For use by user           */
679
680     SFG_Menu*       Menu[ FREEGLUT_MAX_MENUS ]; /* Menus appended to window  */
681     SFG_Menu*       ActiveMenu;                 /* The window's active menu  */
682
683     SFG_Window*         Parent;                 /* The parent to this window */
684     SFG_List            Children;               /* The subwindows d.l. list  */
685
686     GLboolean           IsMenu;                 /* Set to 1 if we are a menu */
687 };
688
689
690 /* A linked list structure of windows */
691 typedef struct tagSFG_WindowList SFG_WindowList ;
692 struct tagSFG_WindowList
693 {
694     SFG_Node node;
695     SFG_Window *window ;
696 };
697
698 /* This holds information about all the windows, menus etc. */
699 typedef struct tagSFG_Structure SFG_Structure;
700 struct tagSFG_Structure
701 {
702     SFG_List        Windows;         /* The global windows list            */
703     SFG_List        Menus;           /* The global menus list              */
704     SFG_List        WindowsToDestroy;
705
706     SFG_Window*     CurrentWindow;   /* The currently set window          */
707     SFG_Menu*       CurrentMenu;     /* Same, but menu...                 */
708
709     SFG_MenuContext* MenuContext;    /* OpenGL rendering context for menus */
710
711     SFG_Window*      GameModeWindow; /* The game mode window               */
712
713     int              WindowID;       /* The window ID for the next window to be created */
714     int              MenuID;         /* The menu ID for the next menu to be created */
715 };
716
717 /*
718  * This structure is used for the enumeration purposes.
719  * You can easily extend its functionalities by declaring
720  * a structure containing enumerator's contents and custom
721  * data, then casting its pointer to (SFG_Enumerator *).
722  */
723 typedef struct tagSFG_Enumerator SFG_Enumerator;
724 struct tagSFG_Enumerator
725 {
726     GLboolean   found;                          /* Used to terminate search  */
727     void*       data;                           /* Custom data pointer       */
728 };
729 typedef void (* FGCBWindowEnumerator  )( SFG_Window *, SFG_Enumerator * );
730 typedef void (* FGCBMenuEnumerator  )( SFG_Menu *, SFG_Enumerator * );
731
732 /* The bitmap font structure */
733 typedef struct tagSFG_Font SFG_Font;
734 struct tagSFG_Font
735 {
736     char*           Name;         /* The source font name             */
737     int             Quantity;     /* Number of chars in font          */
738     int             Height;       /* Height of the characters         */
739     const GLubyte** Characters;   /* The characters mapping           */
740
741     float           xorig, yorig; /* Relative origin of the character */
742 };
743
744 /* The stroke font structures */
745
746 typedef struct tagSFG_StrokeVertex SFG_StrokeVertex;
747 struct tagSFG_StrokeVertex
748 {
749     GLfloat         X, Y;
750 };
751
752 typedef struct tagSFG_StrokeStrip SFG_StrokeStrip;
753 struct tagSFG_StrokeStrip
754 {
755     int             Number;
756     const SFG_StrokeVertex* Vertices;
757 };
758
759 typedef struct tagSFG_StrokeChar SFG_StrokeChar;
760 struct tagSFG_StrokeChar
761 {
762     GLfloat         Right;
763     int             Number;
764     const SFG_StrokeStrip* Strips;
765 };
766
767 typedef struct tagSFG_StrokeFont SFG_StrokeFont;
768 struct tagSFG_StrokeFont
769 {
770     char*           Name;                       /* The source font name      */
771     int             Quantity;                   /* Number of chars in font   */
772     GLfloat         Height;                     /* Height of the characters  */
773     const SFG_StrokeChar** Characters;          /* The characters mapping    */
774 };
775
776
777 /* -- JOYSTICK-SPECIFIC STRUCTURES AND TYPES ------------------------------- */
778 /*
779  * Initial defines from "js.h" starting around line 33 with the existing "freeglut_joystick.c"
780  * interspersed
781  */
782
783 #if TARGET_HOST_MACINTOSH
784 #    include <InputSprocket.h>
785 #endif
786
787 #if TARGET_HOST_MAC_OSX
788 #    include <mach/mach.h>
789 #    include <IOKit/IOkitLib.h>
790 #    include <IOKit/hid/IOHIDLib.h>
791 #endif
792
793 /* XXX It might be better to poll the operating system for the numbers of buttons and
794  * XXX axes and then dynamically allocate the arrays.
795  */
796 #define _JS_MAX_BUTTONS 32
797
798 #if TARGET_HOST_MACINTOSH
799 #    define _JS_MAX_AXES  9
800 typedef struct tagSFG_PlatformJoystick SFG_PlatformJoystick;
801 struct tagSFG_PlatformJoystick
802 {
803 #define  ISP_NUM_AXIS    9
804 #define  ISP_NUM_NEEDS  41
805     ISpElementReference isp_elem  [ ISP_NUM_NEEDS ];
806     ISpNeed             isp_needs [ ISP_NUM_NEEDS ];
807 };
808 #endif
809
810 #if TARGET_HOST_MAC_OSX
811 #    define _JS_MAX_AXES 16
812 typedef struct tagSFG_PlatformJoystick SFG_PlatformJoystick;
813 struct tagSFG_PlatformJoystick
814 {
815     IOHIDDeviceInterface ** hidDev;
816     IOHIDElementCookie buttonCookies[41];
817     IOHIDElementCookie axisCookies[_JS_MAX_AXES];
818 /* The next two variables are not used anywhere */
819 /*    long minReport[_JS_MAX_AXES],
820  *         maxReport[_JS_MAX_AXES];
821  */
822 };
823 #endif
824
825
826 /*
827  * Definition of "SFG_Joystick" structure -- based on JS's "jsJoystick" object class.
828  * See "js.h" lines 80-178.
829  */
830 typedef struct tagSFG_Joystick SFG_Joystick;
831 struct tagSFG_Joystick
832 {
833         SFG_PlatformJoystick pJoystick;
834
835     int          id;
836     GLboolean    error;
837     char         name [ 128 ];
838     int          num_axes;
839     int          num_buttons;
840
841     float dead_band[ _JS_MAX_AXES ];
842     float saturate [ _JS_MAX_AXES ];
843     float center   [ _JS_MAX_AXES ];
844     float max      [ _JS_MAX_AXES ];
845     float min      [ _JS_MAX_AXES ];
846 };
847
848
849
850 /* -- GLOBAL VARIABLES EXPORTS --------------------------------------------- */
851
852 /* Freeglut display related stuff (initialized once per session) */
853 extern SFG_Display fgDisplay;
854
855 /* Freeglut internal structure */
856 extern SFG_Structure fgStructure;
857
858 /* The current freeglut settings */
859 extern SFG_State fgState;
860
861
862 /* -- PRIVATE FUNCTION DECLARATIONS ---------------------------------------- */
863
864 /*
865  * A call to this function makes us sure that the Display and Structure
866  * subsystems have been properly initialized and are ready to be used
867  */
868 #define  FREEGLUT_EXIT_IF_NOT_INITIALISED( string )               \
869   if ( ! fgState.Initialised )                                    \
870   {                                                               \
871     fgError ( " ERROR:  Function <%s> called"                     \
872               " without first calling 'glutInit'.", (string) ) ;  \
873   }
874
875 #define  FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED( string )  \
876   if ( ! fgState.Initialised )                                      \
877   {                                                                 \
878     fgError ( " ERROR:  Internal <%s> function called"              \
879               " without first calling 'glutInit'.", (string) ) ;    \
880   }
881
882 #define  FREEGLUT_INTERNAL_ERROR_EXIT( cond, string, function )  \
883   if ( ! ( cond ) )                                              \
884   {                                                              \
885     fgError ( " ERROR:  Internal error <%s> in function %s",     \
886               (string), (function) ) ;                           \
887   }
888
889 /*
890  * Following definitions are somewhat similiar to GLib's,
891  * but do not generate any log messages:
892  */
893 #define  freeglut_return_if_fail( expr ) \
894     if( !(expr) )                        \
895         return;
896 #define  freeglut_return_val_if_fail( expr, val ) \
897     if( !(expr) )                                 \
898         return val ;
899
900 /*
901  * A call to those macros assures us that there is a current
902  * window set, respectively:
903  */
904 #define  FREEGLUT_EXIT_IF_NO_WINDOW( string )                               \
905   if ( ! fgStructure.CurrentWindow &&                                       \
906        ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION ) )  \
907   {                                                                         \
908     fgError ( " ERROR:  Function <%s> called"                               \
909               " with no current window defined.", (string) ) ;              \
910   }
911
912 /*
913  * The deinitialize function gets called on glutMainLoop() end. It should clean up
914  * everything inside of the freeglut
915  */
916 void fgDeinitialize( void );
917
918 /*
919  * Those two functions are used to create/destroy the freeglut internal
920  * structures. This actually happens when calling glutInit() and when
921  * quitting the glutMainLoop() (which actually happens, when all windows
922  * have been closed).
923  */
924 void fgCreateStructure( void );
925 void fgDestroyStructure( void );
926
927 /*
928  * Window creation, opening, closing and destruction.
929  * Also CallBack clearing/initialization.
930  * Defined in freeglut_structure.c, freeglut_window.c.
931  */
932 SFG_Window* fgCreateWindow( SFG_Window* parent, const char* title,
933                             GLboolean positionUse, int x, int y,
934                             GLboolean sizeUse, int w, int h,
935                             GLboolean gameMode, GLboolean isMenu );
936 void        fgSetWindow ( SFG_Window *window );
937 void        fgOpenWindow( SFG_Window* window, const char* title,
938                           GLboolean positionUse, int x, int y,
939                           GLboolean sizeUse, int w, int h,
940                           GLboolean gameMode, GLboolean isSubWindow );
941 void        fgCloseWindow( SFG_Window* window );
942 void        fgAddToWindowDestroyList ( SFG_Window* window );
943 void        fgCloseWindows ();
944 void        fgDestroyWindow( SFG_Window* window );
945
946 /* Menu creation and destruction. Defined in freeglut_structure.c */
947 SFG_Menu*   fgCreateMenu( FGCBMenu menuCallback );
948 void        fgDestroyMenu( SFG_Menu* menu );
949
950 /* Joystick device management functions, defined in freeglut_joystick.c */
951 int         fgJoystickDetect( void );
952 void        fgInitialiseJoysticks( void );
953 void        fgJoystickClose( void );
954 void        fgJoystickPollWindow( SFG_Window* window );
955
956 /* InputDevice Initialisation and Closure */
957 int         fgInputDeviceDetect( void );
958 void        fgInitialiseInputDevices( void );
959 void        fgInputDeviceClose( void );
960
961 /* spaceball device functions, defined in freeglut_spaceball.c */
962 void        fgInitialiseSpaceball( void );
963 void        fgSpaceballClose( void );
964 void        fgSpaceballSetWindow( SFG_Window *window );
965
966 int         fgHasSpaceball( void );
967 int         fgSpaceballNumButtons( void );
968
969 /* Setting the cursor for a given window */
970 void fgSetCursor ( SFG_Window *window, int cursorID );
971
972 /*
973  * Helper function to enumerate through all registered windows
974  * and one to enumerate all of a window's subwindows...
975  *
976  * The GFunc callback for those functions will be defined as:
977  *
978  *      void enumCallback( gpointer window, gpointer enumerator );
979  *
980  * where window is the enumerated (sub)window pointer (SFG_Window *),
981  * and userData is the a custom user-supplied pointer. Functions
982  * are defined and exported from freeglut_structure.c file.
983  */
984 void fgEnumWindows( FGCBWindowEnumerator enumCallback, SFG_Enumerator* enumerator );
985 void fgEnumSubWindows( SFG_Window* window, FGCBWindowEnumerator enumCallback,
986                        SFG_Enumerator* enumerator );
987
988 /*
989  * fgWindowByHandle returns a (SFG_Window *) value pointing to the
990  * first window in the queue matching the specified window handle.
991  * The function is defined in freeglut_structure.c file.
992  */
993 SFG_Window* fgWindowByHandle( SFG_WindowHandleType hWindow );
994
995 /*
996  * This function is similiar to the previous one, except it is
997  * looking for a specified (sub)window identifier. The function
998  * is defined in freeglut_structure.c file.
999  */
1000 SFG_Window* fgWindowByID( int windowID );
1001
1002 /*
1003  * Looks up a menu given its ID. This is easier than fgWindowByXXX
1004  * as all menus are placed in a single doubly linked list...
1005  */
1006 SFG_Menu* fgMenuByID( int menuID );
1007
1008 /*
1009  * Returns active menu, if any. Assumption: only one menu active throughout application at any one time.
1010  * This is easier than fgWindowByXXX as all menus are placed in one doubly linked list...
1011  */
1012 SFG_Menu* fgGetActiveMenu( );
1013
1014 /*
1015  * The menu activation and deactivation the code. This is the meat
1016  * of the menu user interface handling code...
1017  */
1018 void fgUpdateMenuHighlight ( SFG_Menu *menu );
1019 GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,
1020                               int mouse_x, int mouse_y );
1021 void fgDeactivateMenu( SFG_Window *window );
1022
1023 /*
1024  * This function gets called just before the buffers swap, so that
1025  * freeglut can display the pull-down menus via OpenGL. The function
1026  * is defined in freeglut_menu.c file.
1027  */
1028 void fgDisplayMenu( void );
1029
1030 /* Elapsed time as per glutGet(GLUT_ELAPSED_TIME). */
1031 fg_time_t fgElapsedTime( void );
1032
1033 /* System time in milliseconds */
1034 fg_time_t fgSystemTime(void);
1035
1036 /* List functions */
1037 void fgListInit(SFG_List *list);
1038 void fgListAppend(SFG_List *list, SFG_Node *node);
1039 void fgListRemove(SFG_List *list, SFG_Node *node);
1040 int fgListLength(SFG_List *list);
1041 void fgListInsert(SFG_List *list, SFG_Node *next, SFG_Node *node);
1042
1043 /* Error Message functions */
1044 void fgError( const char *fmt, ... );
1045 void fgWarning( const char *fmt, ... );
1046
1047 SFG_Proc fgPlatformGetProcAddress( const char *procName );
1048
1049 /* pushing attribute/value pairs into an array */
1050 #define ATTRIB(a) attributes[where++]=(a)
1051 #define ATTRIB_VAL(a,v) {ATTRIB(a); ATTRIB(v);}
1052
1053 int fghMapBit( int mask, int from, int to );
1054 int fghIsLegacyContextRequested( void );
1055 void fghContextCreationError( void );
1056 int fghNumberOfAuxBuffersRequested( void );
1057
1058 #endif /* FREEGLUT_INTERNAL_H */
1059
1060 /*** END OF FILE ***/