9438888ec6bc3f696581ac2349d3adeec46f7b79
[freeglut] / include / GL / 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 /*
32  * Freeglut is meant to be available under all Unix/X11 and Win32 platforms.
33  */
34 #if !defined(_WIN32)
35 #   define  TARGET_HOST_UNIX_X11    1
36 #   define  TARGET_HOST_WIN32       0
37 #else
38 #   define  TARGET_HOST_UNIX_X11    0
39 #   define  TARGET_HOST_WIN32       1
40 #endif
41
42 #define  FREEGLUT_MAX_MENUS         3
43 #define  FREEGLUT_DEBUG             1
44
45 #if FREEGLUT_DEBUG
46     #undef   G_DISABLE_ASSERT
47     #undef   G_DISABLE_CHECKS
48 #else
49     #define  G_DISABLE_ASSERT
50     #define  G_DISABLE_CHECKS
51 #endif
52
53 /*
54  * Somehow all Win32 include headers depend on this one:
55  */
56 #if TARGET_HOST_WIN32
57     #include <windows.h>
58         #include <windowsx.h>
59 #endif
60
61 /*
62  * Those files should be available on every platform. GLib can be
63  * downloaded from ftp.gtk.org. Ports are available for most Unix
64  * systems and Win32 (for both native and posix-emulation modes).
65  */
66 #include <GL/gl.h>
67 #include <GL/glu.h>
68 #include <stdio.h>
69 #include <string.h>
70 #include <math.h>
71 #include <glib.h>
72
73 /*
74  * The system-dependant include files should go here:
75  */
76 #if TARGET_HOST_UNIX_X11
77     #include <GL/glx.h>
78     #include <X11/Xlib.h>
79     #include <X11/Xatom.h>
80     #include <X11/keysym.h>
81
82     /*
83      * This will generate errors, but I don't have any idea how to fix it (will autoconf help?)
84      */
85     #include <X11/extensions/xf86vmode.h>
86 #endif
87
88 /*
89  * Microsoft VisualC++ 5.0's <math.h> does not define the PI
90  */
91 #ifndef M_PI
92 #       define  M_PI  3.14159265358979323846
93 #endif
94
95
96 /* -- GLOBAL TYPE DEFINITIONS ---------------------------------------------- */
97
98 /*
99  * Freeglut callbacks type definitions
100  */
101 typedef void (* FGCBdisplay       )( void );
102 typedef void (* FGCBreshape       )( int, int );
103 typedef void (* FGCBvisibility    )( int );
104 typedef void (* FGCBkeyboard      )( unsigned char, int, int );
105 typedef void (* FGCBspecial       )( int, int, int );
106 typedef void (* FGCBmouse         )( int, int, int, int );
107 typedef void (* FGCBmotion        )( int, int );
108 typedef void (* FGCBpassive       )( int, int );
109 typedef void (* FGCBentry         )( int );
110 typedef void (* FGCBwindowStatus  )( int );
111 typedef void (* FGCBmenuState     )( int );
112 typedef void (* FGCBmenuStatus    )( int, int, int );
113 typedef void (* FGCBselect        )( int, int, int );
114 typedef void (* FGCBjoystick      )( unsigned int, int, int, int );
115 typedef void (* FGCBkeyboardUp    )( unsigned char, int, int );
116 typedef void (* FGCBspecialUp     )( int, int, int );
117 typedef void (* FGCBoverlayDisplay)( void );
118 typedef void (* FGCBspaceMotion   )( int, int, int );
119 typedef void (* FGCBspaceRotate   )( int, int, int );
120 typedef void (* FGCBspaceButton   )( int, int );
121 typedef void (* FGCBdials         )( int, int );
122 typedef void (* FGCBbuttonBox     )( int, int );
123 typedef void (* FGCBtabletMotion  )( int, int );
124 typedef void (* FGCBtabletButton  )( int, int, int, int );
125
126 /*
127  * The global callbacks type definitions
128  */
129 typedef void (* FGCBidle          )( void );
130 typedef void (* FGCBtimer         )( int );
131
132 /*
133  * The callback used when creating/using menus
134  */
135 typedef void (* FGCBmenu          )( int );
136
137 /*
138  * A helper structure holding two ints and a boolean
139  */
140 typedef struct tagSFG_XYUse SFG_XYUse;
141 struct tagSFG_XYUse
142 {
143     gint            X, Y;               /* The two integers...               */
144     gboolean        Use;                /* ...and a single boolean.          */
145 };
146
147 /*
148  * This structure holds different freeglut settings
149  */
150 typedef struct tagSFG_State SFG_State;
151 struct tagSFG_State
152 {
153     SFG_XYUse       Position;           /* The default windows' position     */
154     SFG_XYUse       Size;               /* The default windows' size         */
155     guint           DisplayMode;        /* The display mode for new windows  */
156
157     gboolean        ForceDirectContext; /* Should we force direct contexts?  */
158     gboolean        TryDirectContext;   /* What about giving a try to?       */
159
160     gboolean        ForceIconic;        /* All new top windows are iconified */
161
162     gboolean        GLDebugSwitch;      /* OpenGL state debugging switch     */
163     gboolean        XSyncSwitch;        /* X11 sync protocol switch          */
164
165         gboolean                IgnoreKeyRepeat;        /* Whether to ignore key repeat...       */
166
167     GTimer*         Timer;              /* This timer is started on glutInit */
168     GList*          Timers;             /* The freeglut timer hooks          */
169
170     FGCBidle        IdleCallback;       /* The global idle callback          */
171
172     SFG_XYUse       GameModeSize;       /* The game mode screen's dimensions */
173     gint            GameModeDepth;      /* The pixel depth for game mode     */
174     gint            GameModeRefresh;    /* The refresh rate for game mode    */
175 };
176
177 /*
178  * The structure used by display initialization in freeglut_init.c
179  */
180 typedef struct tagSFG_Display SFG_Display;
181 struct tagSFG_Display
182 {
183 #if TARGET_HOST_UNIX_X11
184     Display*        Display;            /* The display we are being run in.  */
185     gint            Screen;             /* The screen we are about to use.   */
186     Window          RootWindow;         /* The screen's root window.         */
187     gint            Connection;         /* The display's connection number   */
188     Atom            DeleteWindow;       /* The window deletion atom          */
189
190 #ifdef X_XF86VidModeGetModeLine
191     XF86VidModeModeLine DisplayMode;    /* Current screen's display settings */
192     gint            DisplayModeClock;   /* The display mode's refresh rate   */
193 #endif
194
195 #elif TARGET_HOST_WIN32
196         HINSTANCE               Instance;                       /* The application's instance            */
197     DEVMODE         DisplayMode;        /* Desktop's display settings        */
198
199 #endif
200
201     gint            ScreenWidth;        /* The screen's width in pixels      */
202     gint            ScreenHeight;       /* The screen's height in pixels     */
203     gint            ScreenWidthMM;      /* The screen's width in milimeters  */
204     gint            ScreenHeightMM;     /* The screen's height in milimeters */
205 };
206
207
208 /*
209  * The user can create any number of timer hooks
210  */
211 typedef struct tagSFG_Timer SFG_Timer;
212 struct tagSFG_Timer
213 {
214     gint32          ID;                 /* The timer ID integer              */
215     FGCBtimer       Callback;           /* The timer callback                */
216     double          TriggerTime;        /* The timer trigger time            */
217 };
218
219 /*
220  * A window and it's OpenGL context. The contents of this structure
221  * are highly dependant on the target operating system we aim at...
222  */
223 typedef struct tagSFG_Context SFG_Context;
224 struct tagSFG_Context
225 {
226 #if TARGET_HOST_UNIX_X11
227     Window          Handle;             /* The window's handle               */
228     GLXContext      Context;            /* The OpenGL context                */
229     XVisualInfo*    VisualInfo;         /* The window's visual information   */
230
231 #elif TARGET_HOST_WIN32
232     HWND            Handle;             /* The window's handle               */
233         HDC                             Device;                         /* The window's device context           */
234         HGLRC                   Context;                        /* The window's WGL context                      */
235
236 #endif
237 };
238
239 /*
240  * Window's state description. This structure should be kept portable.
241  */
242 typedef struct tagSFG_WindowState SFG_WindowState;
243 struct tagSFG_WindowState
244 {
245     gint            Width;              /* Window's width in pixels          */
246     gint            Height;             /* The same about the height         */
247
248     gboolean        Redisplay;          /* Do we have to redisplay?          */
249     gboolean        Visible;            /* Is the window visible now         */
250
251     gint            Cursor;             /* The currently selected cursor     */
252     guint32         Modifiers;          /* The current ALT/SHIFT/CTRL state  */
253
254     double          JoystickPollRate;   /* The joystick polling rate         */
255     double          JoystickLastPoll;   /* When the last poll has happened   */
256
257     gint            MouseX, MouseY;     /* The most recent mouse position    */
258
259     gboolean        IsGameMode;         /* Is this the game mode window?     */
260
261 #if TARGET_HOST_WIN32
262         gboolean                NeedToResize;           /* Do we need to explicitly resize?  */
263 #endif
264 };
265
266 /*
267  * The window callbacks the user can supply us with. Should be kept portable.
268  */
269 typedef struct tagSFG_WindowCallbacks SFG_WindowCallbacks;
270 struct tagSFG_WindowCallbacks
271 {
272     /*
273      * Following callbacks are fully supported right now
274      * and are ready to be tested for GLUT conformance:
275      */
276     FGCBdisplay         Display;
277     FGCBreshape         Reshape;
278     FGCBkeyboard        Keyboard;
279     FGCBspecial         Special;
280     FGCBmouse           Mouse;
281     FGCBmotion          Motion;
282     FGCBpassive         Passive;
283     FGCBentry           Entry;
284     FGCBvisibility      Visibility;
285     FGCBwindowStatus    WindowStatus;
286
287     /*
288      * Those callbacks are required for the initial version
289      */
290     FGCBmenuState       MenuState;
291     FGCBmenuStatus      MenuStatus;
292     FGCBselect          Select;
293     FGCBjoystick        Joystick;
294     FGCBkeyboardUp      KeyboardUp;
295     FGCBspecialUp       SpecialUp;
296
297     /*
298      * Those callbacks are being ignored for the moment
299      */
300     FGCBoverlayDisplay  OverlayDisplay;
301     FGCBspaceMotion     SpaceMotion;
302     FGCBspaceRotate     SpaceRotation;
303     FGCBspaceButton     SpaceButton;
304     FGCBdials           Dials;
305     FGCBbuttonBox       ButtonBox;
306     FGCBtabletMotion    TabletMotion;
307     FGCBtabletButton    TabletButton;
308 };
309
310 /*
311  * This structure describes a menu
312  */
313 typedef struct tagSFG_Menu SFG_Menu;
314 struct tagSFG_Menu
315 {
316     gint                ID;                     /* The global menu ID        */
317     GList*              Entries;                /* The menu entries list     */
318     FGCBmenu            Callback;               /* The menu callback         */
319     gboolean            IsActive;               /* Is the menu selected?     */
320     gint                Width;                  /* Menu box width in pixels  */
321     gint                Height;                 /* Menu box height in pixels */
322     gint                X, Y;                   /* Menu box raster position  */
323 };
324
325 /*
326  * This is a menu entry
327  */
328 typedef struct tagSFG_MenuEntry SFG_MenuEntry;
329 struct tagSFG_MenuEntry
330 {
331     gint                ID;                     /* The menu entry ID (local) */
332     gint                Ordinal;                /* The menu's ordinal number */
333     GString*            Text;                   /* The text to be displayed  */
334     SFG_Menu*           SubMenu;                /* Optional sub-menu tree    */
335     gboolean            IsActive;               /* Is the entry highlighted? */
336     gint                Width;                  /* Label's width in pixels   */
337 };
338
339 /*
340  * A window, making part of FreeGLUT windows hierarchy. Should be kept portable.
341  */
342 typedef struct tagSFG_Window SFG_Window;
343 struct tagSFG_Window
344 {
345     gint                ID;                     /* Window's ID number        */
346
347     SFG_Context         Window;                 /* Window and OpenGL context */
348     SFG_WindowState     State;                  /* The window state          */
349     SFG_WindowCallbacks Callbacks;              /* The window callbacks      */
350
351     SFG_Menu*       Menu[ FREEGLUT_MAX_MENUS ]; /* Menus appended to window  */
352     gboolean  MenuActive[ FREEGLUT_MAX_MENUS ]; /* The menus activity flags  */
353
354     SFG_Window*         Parent;                 /* The parent to this window */
355     GList*              Children;               /* The subwindows d.l. list  */
356 };
357
358 /*
359  * This holds information about all the windows, menus etc.
360  */
361 typedef struct tagSFG_Structure SFG_Structure;
362 struct tagSFG_Structure
363 {
364     GList*              Windows;                /* The global windows list   */
365     GList*              Menus;                  /* The global menus list     */
366
367     SFG_Window*         Window;                 /* The currently active win. */
368     SFG_Menu*           Menu;                   /* Same, but menu...         */
369
370     SFG_Window*         GameMode;               /* The game mode window      */
371
372     gint                WindowID;               /* The new current window ID */
373     gint                MenuID;                 /* The new current menu ID   */
374 };
375
376 /*
377  * This structure is used for the enumeration purposes.
378  * You can easily extend it's functionalities by declaring
379  * a structure containing enumerator's contents and custom
380  * data, then casting it's pointer to (SFG_Enumerator *).
381  */
382 typedef struct tagSFG_Enumerator SFG_Enumerator;
383 struct tagSFG_Enumerator
384 {
385     gboolean    found;                          /* Used to terminate search  */
386     gpointer    data;                           /* Custom data pointer       */
387 };
388
389 /*
390  * The bitmap font structure
391  */
392 typedef struct tagSFG_Font SFG_Font;
393 struct tagSFG_Font
394 {
395     gchar*          Name;                       /* The source font name      */
396     gint            Quantity;                   /* Number of chars in font   */
397     gint            Height;                     /* Height of the characters  */
398     const guchar**  Characters;                 /* The characters mapping    */
399 };
400
401
402 /* -- GLOBAL VARIABLES EXPORTS --------------------------------------------- */
403
404 /*
405  * Freeglut display related stuff (initialized once per session)
406  */
407 extern SFG_Display fgDisplay;
408
409 /*
410  * Freeglut internal structure
411  */
412 extern SFG_Structure fgStructure;
413
414 /*
415  * The current freeglut settings
416  */
417 extern SFG_State fgState;
418
419
420 /* -- PRIVATE FUNCTION DECLARATIONS ---------------------------------------- */
421
422 /*
423  * A call to this function makes us sure that the Display and Structure
424  * subsystems have been properly initialized and are ready to be used
425  */
426 #define  freeglut_assert_ready                      g_assert( fgState.Timer != NULL );
427
428 /*
429  * Following definitions are somewhat similiar to GLib's,
430  * but do not generate any log messages:
431  */
432 #define  freeglut_return_if_fail( expr )            if( !(expr) ) return;
433 #define  freeglut_return_val_if_fail( expr, val )   if( !(expr) ) return( val );
434
435 /*
436  * A call to those macros assures us that there is a current
437  * window and menu set, respectively:
438  */
439 #define  freeglut_assert_window                     g_assert( fgStructure.Window != NULL );
440 #define  freeglut_assert_menu                       g_assert( fgStructure.Menu != NULL );
441
442 /*
443  * The initialize and deinitialize functions get called on glutInit()
444  * and glutMainLoop() end respectively. They should create/clean up
445  * everything inside of the freeglut
446  */
447 void fgInitialize( const gchar* displayName );
448 void fgDeinitialize( void );
449
450 /*
451  * Those two functions are used to create/destroy the freeglut internal
452  * structures. This actually happens when calling glutInit() and when
453  * quitting the glutMainLoop() (which actually happens, when all windows
454  * have been closed).
455  */
456 void fgCreateStructure( void );
457 void fgDestroyStructure( void );
458
459 /*
460  * A helper function to check if a display mode is possible to use
461  */
462 #if TARGET_HOST_UNIX_X11
463 XVisualInfo* fgChooseVisual( void );
464 #endif
465
466 /*
467  * The window procedure for Win32 events handling
468  */
469 #if TARGET_HOST_WIN32
470 LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
471 gboolean fgSetupPixelFormat( SFG_Window* window, gboolean checkOnly );
472 #endif
473
474 /*
475  * Window creation, opening, closing and destruction.
476  * Defined in freeglut_structure.c, freeglut_window.c.
477  */
478 SFG_Window* fgCreateWindow( SFG_Window* parent, const gchar* title, gint x, gint y, gint w, gint h, gboolean gameMode );
479 void        fgOpenWindow( SFG_Window* window, const gchar* title, gint x, gint y, gint w, gint h, gboolean gameMode );
480 void        fgCloseWindow( SFG_Window* window );
481 void            fgDestroyWindow( SFG_Window* window, gboolean needToClose );
482
483 /*
484  * Menu creation and destruction. Defined in freeglut_structure.c
485  */
486 SFG_Menu*   fgCreateMenu( FGCBmenu menuCallback );
487 void        fgDestroyMenu( SFG_Menu* menu );
488
489 /*
490  * Joystick device management functions, defined in freeglut_joystick.c
491  */
492 void        fgJoystickInit( gint ident );
493 void        fgJoystickClose( void );
494 void        fgJoystickPollWindow( SFG_Window* window );
495
496 /*
497  * Helper function to enumerate through all registered windows
498  * and one to enumerate all of a window's subwindows...
499  *
500  * The GFunc callback for those functions will be defined as:
501  *
502  *      void enumCallback( gpointer window, gpointer enumerator );
503  *
504  * where window is the enumerated (sub)window pointer (SFG_Window *),
505  * and userData is the a custom user-supplied pointer. Functions
506  * are defined and exported from freeglut_structure.c file.
507  */
508 void fgEnumWindows( GFunc enumCallback, SFG_Enumerator* enumerator );
509 void fgEnumSubWindows( SFG_Window* window, GFunc enumCallback, SFG_Enumerator* enumerator );
510
511 /*
512  * fgWindowByHandle returns a (SFG_Window *) value pointing to the
513  * first window in the queue matching the specified window handle.
514  * The function is defined in freeglut_structure.c file.
515  */
516 #if TARGET_HOST_UNIX_X11
517     SFG_Window* fgWindowByHandle( Window hWindow );
518 #elif TARGET_HOST_WIN32
519
520     SFG_Window* fgWindowByHandle( HWND hWindow );
521 #endif
522
523 /*
524  * This function is similiar to the previous one, except it is
525  * looking for a specified (sub)window identifier. The function
526  * is defined in freeglut_structure.c file.
527  */
528 SFG_Window* fgWindowByID( gint windowID );
529
530 /*
531  * Looks up a menu given it's ID. This is easier that fgWindowByXXX
532  * as all menus are placed in a single doubly linked list...
533  */
534 SFG_Menu* fgMenuByID( gint menuID );
535
536 /*
537  * The menu activation and deactivation the code. This is the meat
538  * of the menu user interface handling code...
539  */
540 void fgActivateMenu( gint button );
541 void fgDeactivateMenu( gint button );
542
543 /*
544  * This function gets called just before the buffers swap, so that
545  * freeglut can display the pull-down menus via OpenGL. The function
546  * is defined in freeglut_menu.c file.
547  */
548 void fgDisplayMenu( void );
549
550 /*
551  * Display the mouse cursor using OpenGL calls. The function
552  * is defined in freeglut_cursor.c file.
553  */
554 void fgDisplayCursor( void );
555
556 #endif /* FREEGLUT_INTERNAL_H */
557
558 /*** END OF FILE ***/
559
560
561
562