Fixed Win95 crash when getenv("DISPLAY") returned NULL ptr.
[freeglut] / freeglut-1.3 / freeglut_structure.c
1 /*
2  * freeglut_structure.c
3  *
4  * Windows and menus need tree structure
5  *
6  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7  * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8  * Creation date: Sat Dec 18 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 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #define  G_LOG_DOMAIN  "freeglut-structure"
33
34 #include "../include/GL/freeglut.h"
35 #include "../include/GL/freeglut_internal.h"
36
37
38 /* -- GLOBAL EXPORTS ------------------------------------------------------- */
39
40 /*
41  * The SFG_Structure container holds information about windows and menus
42  * created between glutInit() and glutMainLoop() return.
43  */
44 SFG_Structure fgStructure;
45
46
47 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
48
49 /*
50  * This private function creates, opens and adds to the hierarchy
51  * a freeglut window complete with OpenGL context and stuff...
52  *
53  * If parent is set to NULL, the window created will be a topmost one.
54  */
55 SFG_Window* fgCreateWindow( SFG_Window* parent, const char* title, int x, int y, int w, int h, GLboolean gameMode )
56 {
57     /*
58      * Have the window object created
59      */
60     SFG_Window* window = calloc( sizeof(SFG_Window), 1 );
61     int fakeArgc = 0;
62
63     /*
64      * If the freeglut internals haven't been initialized yet,
65      * do it now. Hack's idea courtesy of Chris Purnell...
66      */
67     if( !fgState.Time.Set )
68         glutInit( &fakeArgc, NULL );
69
70     /*
71      * Initialize the object properties
72      */
73     window->ID = ++fgStructure.WindowID;
74
75     /*
76      * Initialize the children list
77      */
78     fgListInit( &window->Children );
79
80     /*
81      * Does this window have a parent?
82      */
83     if( parent != NULL )
84     {
85         /*
86          * That's quite right, attach this windows as a child window
87          */
88         fgListAppend( &parent->Children, &window->Node );
89         window->Parent = parent;
90     }
91     else
92     {
93         /*
94          * Otherwise add the newly created window to the topmost windows list
95          */
96         fgListAppend( &fgStructure.Windows, &window->Node );
97     }
98
99     /*
100      * Set the default mouse cursor and reset the modifiers value
101      */
102     window->State.Cursor    = GLUT_CURSOR_INHERIT;
103     window->State.Modifiers = 0xffffffff;
104
105     /*
106      * Open the window now. The fgOpenWindow() function is system
107      * dependant, and resides in freeglut_window.c. Uses fgState.
108      */
109     fgOpenWindow( window, title, x, y, w, h, gameMode, (parent != NULL) ? TRUE : FALSE );
110
111     /*
112      * Return a pointer to the newly created window
113      */
114     return( window );
115 }
116
117 /*
118  * This private function creates a menu and adds it to the menus list
119  */
120 SFG_Menu* fgCreateMenu( FGCBmenu menuCallback )
121 {
122     /*
123      * Have the menu object created
124      */
125     SFG_Menu* menu = calloc( sizeof(SFG_Menu), 1 );
126     int fakeArgc = 0;
127
128     /*
129      * If the freeglut internals haven't been initialized yet,
130      * do it now. Hack's idea courtesy of Chris Purnell...
131      */
132     if( !fgState.Time.Set )
133         glutInit( &fakeArgc, NULL );
134
135     /*
136      * Initialize the object properties:
137      */
138     menu->ID       = ++fgStructure.MenuID;
139     menu->Callback = menuCallback;
140
141     /*
142      * Initialize the entries list
143      */
144     fgListInit( &menu->Entries );
145
146     /*
147      * Add it to the menu structure hierarchy
148      */
149     fgListAppend( &fgStructure.Menus, &menu->Node );
150
151     /*
152      * Newly created menus implicitly become current ones
153      */
154     fgStructure.Menu = menu;
155
156     /*
157      * Return the result to the caller
158      */
159     return( menu );
160 }
161
162 /*
163  * Linked list of windows to destroy ... this is so we don't destroy a window from the middle of
164  * its callback.  Some C compilers take an extremely dim view of this.
165  */
166
167 static SFG_WindowList* WindowsToDestroy = (SFG_WindowList*)NULL ;
168
169 /*
170  * Function to add a window to the linked list of windows to destroy.  Subwindows are automatically
171  * added because they hang from the window structure.
172  */
173 void fgAddToWindowDestroyList ( SFG_Window* window, GLboolean needToClose )
174 {
175   SFG_WindowList *new_list_entry = (SFG_WindowList*)malloc ( sizeof(SFG_WindowList) ) ;
176   new_list_entry->window = window ;
177   new_list_entry->needToClose = needToClose ;
178   new_list_entry->next = WindowsToDestroy ;
179   WindowsToDestroy = new_list_entry ;
180
181   /*
182    * Check the execution state.  If this has been called from "glutDestroyWindow",
183    * a statement in that function will reset the "ExecState" after this function returns.
184    */
185   if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
186   {
187     /*
188      * Set the execution state flag to drop out of the main loop.
189      */
190     if ( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
191       fgState.ExecState = GLUT_EXEC_STATE_STOP ;
192   }
193 }
194
195 /*
196  * Function to close down all the windows in the "WindowsToDestroy" list
197  */
198 void fgCloseWindows ()
199 {
200   fgExecutionState ExecState = fgState.ExecState ;
201
202   SFG_WindowList *window_ptr = WindowsToDestroy ;
203   WindowsToDestroy = (SFG_WindowList*)NULL ;  // In case the destroy callbacks cause more windows to be closed
204
205   while ( window_ptr )
206   {
207     SFG_WindowList *next = window_ptr->next ;
208     fgDestroyWindow ( window_ptr->window, window_ptr->needToClose ) ;
209     free ( window_ptr ) ;
210     window_ptr = next ;
211
212     if ( !window_ptr ) window_ptr = WindowsToDestroy ;
213   }
214
215   /*
216    * Since the "fgDestroyWindow" function could easily have set the "ExecState" to stop,
217    * let's set it back to what it was.
218    */
219   fgState.ExecState = ExecState ;
220 }
221
222 /*
223  * This function destroys a window and all of its subwindows. Actually,
224  * another function, defined in freeglut_window.c is called, but this is
225  * a whole different story...
226  */
227 void fgDestroyWindow( SFG_Window* window, GLboolean needToClose )
228 {
229     SFG_Window* subWindow;
230
231     assert( window != NULL );
232     freeglut_assert_ready;
233
234     /*
235      * Does this window have any subwindows?
236      */
237     while ( (subWindow = window->Children.First) != NULL )
238     {
239         /*
240          * Destroy the first window in the list (possibly destroying
241          * its subwindows too). This is not very effective, but works
242          */
243         fgDestroyWindow( subWindow, needToClose );
244     }
245
246     /*
247      * If the programmer defined a destroy callback, call it
248      */
249     if ( window->Callbacks.Destroy != NULL )
250       window->Callbacks.Destroy () ;
251
252     /*
253      * Now we should remove the reference to this window from its parent
254      */
255     if ( window->Parent != NULL )
256         fgListRemove( &window->Parent->Children, &window->Node );
257     else
258         fgListRemove( &fgStructure.Windows, &window->Node );
259
260     /*
261      * OK, this window seems disconnected from the structure enough
262      * in order to be closed without any bigger risks...
263      */
264     if( needToClose == TRUE )
265         fgCloseWindow( window );
266
267     /*
268      * Check if the window is the current one...
269      */
270     if( fgStructure.Window == window )
271         fgStructure.Window = NULL;
272
273     /*
274      * Finally, we can delete the window's object. It hopefully does
275      * have everything inside it freed and we do not have to care...
276      */
277     free( window );
278 }
279
280 /*
281  * This is a helper static function that removes a menu (given its pointer)
282  * from any windows that can be accessed from a given parent...
283  */
284 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
285 {
286     SFG_Window *subWindow;
287     int i;
288
289     /*
290      * Check if the menu is attached to the current window,
291      * if so, have it detached (by overwriting with a NULL):
292      */
293     for( i=0; i<3; i++ )
294     {
295         if( window->Menu[ i ] == menu )
296             window->Menu[ i ] = NULL;
297     }
298
299     /*
300      * Call this function for all of the window's children recursively:
301      */
302     for( subWindow = window->Children.First; subWindow;
303          subWindow = subWindow->Node.Next)
304     {
305         fghRemoveMenuFromWindow( subWindow, menu );
306     }
307 }
308
309 /*
310  * This is a static helper function that removes menu references
311  * from another menu, given two pointers to them...
312  */
313 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
314 {
315   SFG_MenuEntry *entry;
316
317   for( entry = from->Entries.First; entry; entry = entry->Node.Next )
318   {
319     if (entry->SubMenu == menu)
320     {
321       entry->SubMenu = NULL;
322     }
323   }
324 }
325
326 /*
327  * This function destroys a menu specified by the parameter. All menus
328  * and windows are updated to make sure no ill pointers hang around.
329  */
330 void fgDestroyMenu( SFG_Menu* menu )
331 {
332   SFG_Window *window;
333   SFG_Menu *from;
334   SFG_MenuEntry *entry;
335
336   assert( menu != NULL );
337   freeglut_assert_ready;
338
339   /*
340    * First of all, have all references to this menu removed from all windows:
341    */
342   for( window = fgStructure.Windows.First; window; window = window->Node.Next )
343   {
344     fghRemoveMenuFromWindow( window, menu );
345   }
346
347   /*
348    * Now proceed with removing menu entries that lead to this menu
349    */
350   for( from = fgStructure.Menus.First; from; from = from->Node.Next )
351   {
352     fghRemoveMenuFromMenu( from, menu );
353   }
354
355   /*
356    * Now we are pretty sure the menu is not used anywhere
357    * and that we can remove all of its entries
358    */
359   while( (entry = menu->Entries.First) != NULL )
360   {
361     fgListRemove(&menu->Entries, &entry->Node);
362
363     /*
364      * There might be a string allocated, have it freed:
365      */
366     free( entry->Text );
367
368     /*
369      * Deallocate the entry itself:
370      */
371     free( entry );
372   }
373
374   /*
375    * Remove the menu from the menus list
376    */
377   fgListRemove( &fgStructure.Menus, &menu->Node );
378
379   /*
380    * If that menu was the current one...
381    */
382   if( fgStructure.Menu == menu )
383     fgStructure.Menu = NULL;
384
385   /*
386    * Have the menu structure freed
387    */
388   free( menu );
389 }
390
391 /*
392  * This function should be called on glutInit(). It will prepare the internal
393  * structure of freeglut to be used in the application. The structure will be
394  * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
395  * case further use of freeglut should be preceeded with a glutInit() call.
396  */
397 void fgCreateStructure( void )
398 {
399   /*
400    * We will be needing two lists: the first containing windows,
401    * and the second containing the user-defined menus.
402    * Also, no current window/menu is set, as none has been created yet.
403    */
404
405   fgListInit(&fgStructure.Windows);
406   fgListInit(&fgStructure.Menus);
407 }
408
409 /*
410  * This function is automatically called on glutMainLoop() return. It should deallocate 
411  * and destroy all remnants of previous glutInit()-enforced structure initialization...
412  */
413 void fgDestroyStructure( void )
414 {
415   SFG_Window *window;
416   SFG_Menu *menu;
417
418   /*
419    * Just make sure we are not called in vain...
420    */
421   freeglut_assert_ready;
422
423   /*
424    * Make sure all windows and menus have been deallocated
425    */
426   while( (window = fgStructure.Windows.First) != NULL )
427     fgDestroyWindow( window, TRUE );
428
429   while( (menu = fgStructure.Menus.First) != NULL )
430     fgDestroyMenu( menu );
431 }
432
433 /*
434  * Helper function to enumerate through all registered top-level windows
435  */
436 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
437 {
438   SFG_Window *window;
439
440   assert( (enumCallback != NULL) && (enumerator != NULL) );
441   freeglut_assert_ready;
442
443   /*
444    * Check every of the top-level windows
445    */
446   for( window = fgStructure.Windows.First; window;
447        window = window->Node.Next )
448   {
449     /*
450      * Execute the callback...
451      */
452     enumCallback( window, enumerator );
453
454     /*
455      * If it has been marked as 'found', stop searching
456      */
457     if( enumerator->found == TRUE )
458       return;
459   }
460 }
461
462 /*
463  * Helper function to enumerate through all a window's subwindows (single level descent)
464  */
465 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
466 {
467   SFG_Window *child;
468
469   assert( (enumCallback != NULL) && (enumerator != NULL) );
470   freeglut_assert_ready;
471
472   /*
473    * Check every of the window's children:
474    */
475   for( child = window->Children.First; child; child = child->Node.Next )
476   {
477     /*
478      * Execute the callback...
479      */
480     enumCallback( child, enumerator );
481
482     /*
483      * If it has been marked as 'found', stop searching
484      */
485     if( enumerator->found == TRUE )
486       return;
487   }
488 }
489
490 /*
491  * A static helper function to look for a window given its handle
492  */
493 static void fghcbWindowByHandle( SFG_Window *window, SFG_Enumerator *enumerator )
494 {
495     /*
496      * Make sure we do not overwrite our precious results...
497      */
498     freeglut_return_if_fail( enumerator->found == FALSE );
499
500 #if TARGET_HOST_UNIX_X11
501     #define WBHANDLE (Window)
502 #elif TARGET_HOST_WIN32
503     #define WBHANDLE (HWND)
504 #endif
505
506     /*
507      * Check the window's handle. Hope this works. Looks ugly. That's for sure.
508      */
509     if( window->Window.Handle == WBHANDLE (enumerator->data) )
510     {
511         enumerator->found = TRUE;
512         enumerator->data = window;
513
514         return;
515     }
516
517     /*
518      * Otherwise, check this window's children
519      */
520     fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
521
522 #undef WBHANDLE
523 }
524
525 /*
526  * fgWindowByHandle returns a (SFG_Window *) value pointing to the
527  * first window in the queue matching the specified window handle.
528  * The function is defined in freeglut_structure.c file.
529  */
530 SFG_Window* fgWindowByHandle
531 #if TARGET_HOST_UNIX_X11
532 ( Window hWindow )
533 #elif TARGET_HOST_WIN32
534 ( HWND hWindow )
535 #endif
536 {
537     SFG_Enumerator enumerator;
538
539     /*
540      * This is easy and makes use of the windows enumeration defined above
541      */
542     enumerator.found = FALSE;
543     enumerator.data = (void *)hWindow;
544
545     /*
546      * Start the enumeration now:
547      */
548     fgEnumWindows( fghcbWindowByHandle, &enumerator );
549
550     /*
551      * Check if the window has been found or not:
552      */
553     if( enumerator.found == TRUE )
554         return( (SFG_Window *) enumerator.data );
555
556     /*
557      * Otherwise return NULL to mark the failure
558      */
559     return( NULL );
560 }
561
562 /*
563  * A static helper function to look for a window given its ID
564  */
565 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
566 {
567     /*
568      * Make sure we do not overwrite our precious results...
569      */
570     freeglut_return_if_fail( enumerator->found == FALSE );
571
572     /*
573      * Check the window's handle. Hope this works. Looks ugly. That's for sure.
574      */
575     if( window->ID == (int) (enumerator->data) )
576     {
577         enumerator->found = TRUE;
578         enumerator->data = window;
579
580         return;
581     }
582
583     /*
584      * Otherwise, check this window's children
585      */
586     fgEnumSubWindows( window, fghcbWindowByID, enumerator );
587 }
588
589 /*
590  * This function is similiar to the previous one, except it is
591  * looking for a specified (sub)window identifier. The function
592  * is defined in freeglut_structure.c file.
593  */
594 SFG_Window* fgWindowByID( int windowID )
595 {
596     SFG_Enumerator enumerator;
597
598     /*
599      * Uses a method very similiar for fgWindowByHandle...
600      */
601     enumerator.found = FALSE;
602     enumerator.data = (void *) windowID;
603
604     /*
605      * Start the enumeration now:
606      */
607     fgEnumWindows( fghcbWindowByID, &enumerator );
608
609     /*
610      * Check if the window has been found or not:
611      */
612     if( enumerator.found == TRUE )
613         return( (SFG_Window *) enumerator.data );
614
615     /*
616      * Otherwise return NULL to mark the failure
617      */
618     return( NULL );
619 }
620
621 /*
622  * Looks up a menu given its ID. This is easier that fgWindowByXXX
623  * as all menus are placed in a single doubly linked list...
624  */
625 SFG_Menu* fgMenuByID( int menuID )
626 {
627     SFG_Menu *menu = NULL;
628
629     freeglut_assert_ready;
630
631     /*
632      * It's enough to check all entries in fgStructure.Menus...
633      */
634     for( menu = fgStructure.Menus.First; menu; menu = menu->Node.Next )
635     {
636         /*
637          * Does the ID number match?
638          */
639         if( menu->ID == menuID )
640             return( menu );
641     }
642
643     /*
644      * We have not found the requested menu ID
645      */
646     return( NULL );
647 }
648
649 /*
650  * List functions...
651  */
652 void fgListInit(SFG_List *list)
653 {
654     list->First = NULL;
655     list->Last = NULL;
656 }
657
658 void fgListAppend(SFG_List *list, SFG_Node *node)
659 {
660     SFG_Node *ln;
661
662     if ( (ln = list->Last) != NULL )
663     {
664         ln->Next = node;
665         node->Prev = ln;
666     }
667     else
668     {
669         node->Prev = NULL;
670         list->First = node;
671     }
672
673     node->Next = NULL;
674     list->Last = node;
675 }
676
677 void fgListRemove(SFG_List *list, SFG_Node *node)
678 {
679     SFG_Node *ln;
680
681     if ( (ln = node->Next) != NULL )
682         ln->Prev = node->Prev;
683     if ( (ln = node->Prev) != NULL )
684         ln->Next = node->Next;
685     if ( (ln = list->First) == node )
686         list->First = node->Next;
687     if ( (ln = list->Last) == node )
688         list->Last = node->Prev;
689 }
690
691 int fgListLength(SFG_List *list)
692 {
693     SFG_Node *node;
694     int length = 0;
695
696     for( node = list->First; node; node = node->Next )
697         ++length;
698
699     return( length );
700 }
701
702 /*** END OF FILE ***/