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