4 * The windows message processing methods.
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8 * Creation date: Fri Dec 3 1999
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:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
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.
32 #define G_LOG_DOMAIN "freeglut-main"
34 #include "../include/GL/freeglut.h"
35 #include "freeglut_internal.h"
38 * TODO BEFORE THE STABLE RELEASE:
40 * There are some issues concerning window redrawing under X11, and maybe
41 * some events are not handled. The Win32 version lacks some more features,
42 * but seems acceptable for not demanding purposes.
44 * Need to investigate why the X11 version breaks out with an error when
45 * closing a window (using the window manager, not glutDestroyWindow)...
48 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
51 * Calls a window's redraw method. This is used when
52 * a redraw is forced by the incoming window messages.
55 static void fghRedrawWindowByHandle
56 #if TARGET_HOST_UNIX_X11
58 #elif TARGET_HOST_WIN32
63 * Find the window we have to redraw...
65 SFG_Window* window = fgWindowByHandle( handle );
66 freeglut_return_if_fail( window != NULL );
69 * Check if there is a display callback hooked to it
71 freeglut_return_if_fail( window->Callbacks.Display != NULL );
74 * Return if the window is not visible
76 freeglut_return_if_fail( window->State.Visible == TRUE );
79 * Set the window as the current one.
81 fgSetWindow( window );
84 * Do not exagerate with the redisplaying
86 window->State.Redisplay = FALSE;
89 * Have the callback executed now. The buffers should
90 * be swapped by the glutSwapBuffers() execution inside
91 * the callback itself.
94 window->Callbacks.Display();
98 * Handle a window configuration change. When no reshape
99 * callback is hooked, the viewport size is updated to
100 * match the new window size.
102 static void fghReshapeWindowByHandle
103 #if TARGET_HOST_UNIX_X11
104 ( Window handle, int width, int height )
105 #elif TARGET_HOST_WIN32
106 ( HWND handle, int width, int height )
110 * Find the window that received the reshape event
112 SFG_Window* window = fgWindowByHandle( handle );
113 freeglut_return_if_fail( window != NULL );
116 * Remember about setting the current window...
118 fgSetWindow( window );
121 * Check if there is a reshape callback hooked
123 if( window->Callbacks.Reshape != NULL )
126 * OKi, have it called immediately
128 window->Callbacks.Reshape( width, height );
133 * Otherwise just resize the viewport
135 glViewport( 0, 0, width, height );
139 * Force a window redraw. In Windows at least this is only a partial solution: if the
140 * window is increasing in size in either dimension, the already-drawn part does not get
141 * drawn again and things look funny. But without this we get this bad behaviour whenever
142 * we resize the window.
144 window->State.Redisplay = TRUE ;
148 * A static helper function to execute display callback for a window
150 static void fghcbDisplayWindow( SFG_Window *window, SFG_Enumerator *enumerator )
152 #if TARGET_HOST_UNIX_X11
154 * Check if there is an idle callback hooked
156 if( (window->Callbacks.Display != NULL) &&
157 (window->State.Redisplay == TRUE) &&
158 (window->State.Visible == TRUE) )
161 * OKi, this is the case: have the window set as the current one
163 fgSetWindow( window );
166 * Do not exagerate with the redisplaying
168 window->State.Redisplay = FALSE;
171 * And execute the display callback immediately after
173 window->Callbacks.Display();
176 #elif TARGET_HOST_WIN32
179 * Do we need to explicitly resize the window?
181 if( window->State.NeedToResize )
183 fgSetWindow( window );
185 fghReshapeWindowByHandle(
186 window->Window.Handle,
187 glutGet( GLUT_WINDOW_WIDTH ),
188 glutGet( GLUT_WINDOW_HEIGHT )
192 * Never ever do that again:
194 window->State.NeedToResize = FALSE;
198 * This is done in a bit different way under Windows
200 if( (window->Callbacks.Display != NULL) &&
201 (window->State.Redisplay == TRUE) &&
202 (window->State.Visible == TRUE) )
205 * Do not exagerate with the redisplaying
207 window->State.Redisplay = FALSE;
210 window->Window.Handle, NULL, NULL,
211 RDW_NOERASE | RDW_INTERNALPAINT | RDW_INVALIDATE | RDW_UPDATENOW
218 * Process this window's children (if any)
220 fgEnumSubWindows( window, fghcbDisplayWindow, enumerator );
224 * Make all windows perform a display call
226 static void fghDisplayAll( void )
228 SFG_Enumerator enumerator;
231 * Uses a method very similiar for fgWindowByHandle...
233 enumerator.found = FALSE;
234 enumerator.data = NULL;
237 * Start the enumeration now:
239 fgEnumWindows( fghcbDisplayWindow, &enumerator );
243 * Window enumerator callback to check for the joystick polling code
245 static void fghcbCheckJoystickPolls( SFG_Window *window, SFG_Enumerator *enumerator )
247 long int checkTime = fgElapsedTime();
250 * Check if actually need to do the poll for the currently enumerated window:
252 if( window->State.JoystickLastPoll + window->State.JoystickPollRate >= checkTime )
255 * Yeah, that's it. Poll the joystick...
257 fgJoystickPollWindow( window );
260 * ...and reset the polling counters:
262 window->State.JoystickLastPoll = checkTime;
266 * Process this window's children (if any)
268 fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );
272 * Check all windows for joystick polling
274 static void fghCheckJoystickPolls( void )
276 SFG_Enumerator enumerator;
279 * Uses a method very similiar for fgWindowByHandle...
281 enumerator.found = FALSE;
282 enumerator.data = NULL;
285 * Start the enumeration now:
287 fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );
291 * Check the global timers
293 static void fghCheckTimers( void )
295 long checkTime = fgElapsedTime();
296 SFG_Timer *timer, *next;
299 fgListInit(&timedOut);
302 * For every timer that is waiting for triggering
304 for( timer = (SFG_Timer *)fgState.Timers.First; timer; timer = next )
306 next = (SFG_Timer *)timer->Node.Next;
309 * Check for the timeout:
311 if( timer->TriggerTime <= checkTime )
314 * Add the timer to the timed out timers list
316 fgListRemove( &fgState.Timers, &timer->Node );
317 fgListAppend( &timedOut, &timer->Node );
322 * Now feel free to execute all the hooked and timed out timer callbacks
323 * And delete the timed out timers...
325 while ( (timer = (SFG_Timer *)timedOut.First) )
327 if( timer->Callback != NULL )
328 timer->Callback( timer->ID );
329 fgListRemove( &timedOut, &timer->Node );
338 long fgElapsedTime( void )
340 #if TARGET_HOST_UNIX_X11
344 gettimeofday( &now, NULL );
346 elapsed = (now.tv_usec - fgState.Time.Value.tv_usec) / 1000;
347 elapsed += (now.tv_sec - fgState.Time.Value.tv_sec) * 1000;
350 #elif TARGET_HOST_WIN32
351 return (timeGetTime() - fgState.Time.Value);
358 void fgError( const char *fmt, ... )
364 fprintf( stderr, "freeglut: ");
365 vfprintf( stderr, fmt, ap );
366 fprintf( stderr, "\n" );
373 void fgWarning( const char *fmt, ... )
379 fprintf( stderr, "freeglut: ");
380 vfprintf( stderr, fmt, ap );
381 fprintf( stderr, "\n" );
389 static void fgCleanUpGlutsMess( void )
395 if ( fgStructure.Windows.First != NULL )
397 SFG_Window *win = (SFG_Window *)fgStructure.Windows.First ;
401 while ( win != NULL )
403 SFG_Window *temp_win = (SFG_Window *)win->Node.Next ;
404 fgDestroyWindow ( win, FALSE ) ;
410 /* these are pointers to external handles */
412 __glutWindowListSize = 0;
413 __glutStaleWindowList = NULL;
414 __glutWindowList = NULL;
415 __glutCurrentWindow = NULL;
417 /* make sure we no longer have a GL context */
419 if ( wglGetCurrentContext() != NULL )
421 wglDeleteContext( wglGetCurrentContext() );
424 hInstance = GetModuleHandle(NULL);
425 UnregisterClass( classname, hInstance );
427 /* clean up allocated timer memory */
429 tList = __glutTimerList;
432 while ( __glutTimerList )
435 tList = __glutTimerList;
437 if ( __glutTimerList )
438 __glutTimerList = __glutTimerList->next;
447 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
450 * Executes a single iteration in the freeglut processing loop.
452 void FGAPIENTRY glutMainLoopEvent( void )
454 #if TARGET_HOST_UNIX_X11
460 * This code was repeated constantly, so here it goes into a definition:
462 # define GETWINDOW(a) window = fgWindowByHandle( event.a.window );if( window == NULL ) break;
463 # define GETMOUSE(a) window->State.MouseX = event.a.x; window->State.MouseY = event.a.y;
466 * Make sure the display has been created etc.
468 freeglut_assert_ready;
471 * Do we have any event messages pending?
473 if( XPending( fgDisplay.Display ) )
476 * Grab the next event to be processed...
478 XNextEvent( fgDisplay.Display, &event );
481 * Check the event's type
487 * The window creation confirmation
493 * This is sent to confirm the XDestroyWindow call.
496 * Call the window closure callback, remove from the structure, etc.
498 /* fgAddToWindowDestroyList ( window, FALSE ); */
504 * Destroy the window when the WM_DELETE_WINDOW message arrives
506 if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.DeleteWindow )
508 GETWINDOW( xclient );
511 * Call the XWindows functions to close the window
513 fgCloseWindow ( window ) ;
516 * Call the window closure callback, remove from the structure, etc.
518 fgAddToWindowDestroyList ( window, FALSE );
524 * A window of ours has been unmapped...
530 * We are too dumb to process partial exposes...
532 if( event.xexpose.count == 0 )
533 fghRedrawWindowByHandle( event.xexpose.window );
536 case ConfigureNotify:
538 * The window gets resized
540 fghReshapeWindowByHandle(
541 event.xconfigure.window,
542 event.xconfigure.width,
543 event.xconfigure.height
549 * Have the client's keyboard knowledge updated (xlib.ps,
550 * page 206, says that's a good thing to do)
552 XRefreshKeyboardMapping( (XMappingEvent *) &event );
555 case VisibilityNotify:
558 * The window's visiblity might have changed
560 GETWINDOW( xvisibility );
563 * Break now if no window status callback has been hooked to that window
565 if( window->Callbacks.WindowStatus == NULL )
569 * We're going to send a callback to a window. Make it current.
571 fgSetWindow( window );
574 * Sending this event, the X server can notify us that the window has just
575 * acquired one of the three possible visibility states: VisibilityUnobscured,
576 * VisibilityPartiallyObscured or VisibilityFullyObscured
578 switch( event.xvisibility.state )
580 case VisibilityUnobscured:
582 * We are fully visible...
584 window->Callbacks.WindowStatus( GLUT_FULLY_RETAINED );
585 window->State.Visible = TRUE;
588 case VisibilityPartiallyObscured:
590 * The window is partially visible
592 window->Callbacks.WindowStatus( GLUT_PARTIALLY_RETAINED );
593 window->State.Visible = TRUE;
596 case VisibilityFullyObscured:
598 * The window is totally obscured
600 window->Callbacks.WindowStatus( GLUT_FULLY_COVERED );
601 window->State.Visible = FALSE;
610 * Mouse is over one of our windows
612 GETWINDOW( xcrossing ); GETMOUSE( xcrossing );
615 * Is there an entry callback hooked to the window?
617 if( window->Callbacks.Entry != NULL )
620 * Yeah. Notify the window about having the mouse cursor over
622 window->Callbacks.Entry( GLUT_ENTERED );
630 * Mouse is no longer over one of our windows
632 GETWINDOW( xcrossing ); GETMOUSE( xcrossing );
635 * Is there an entry callback hooked to the window?
637 if( window->Callbacks.Entry != NULL )
640 * Yeah. Notify the window about having the mouse cursor over
642 window->Callbacks.Entry( GLUT_LEFT );
650 * The mouse cursor was moved...
652 GETWINDOW( xmotion ); GETMOUSE( xmotion );
655 * Fallback if there's an active menu hooked to this window
657 if( window->ActiveMenu != NULL )
660 * Let's make the window redraw as a result of the mouse motion.
662 if ( window->IsMenu ) window->State.Redisplay = TRUE ;
668 * What kind of a movement was it?
670 if( (event.xmotion.state & Button1Mask) || (event.xmotion.state & Button2Mask) ||
671 (event.xmotion.state & Button3Mask) || (event.xmotion.state & Button4Mask) ||
672 (event.xmotion.state & Button5Mask) )
675 * A mouse button was pressed during the movement...
676 * Is there a motion callback hooked to the window?
678 if( window->Callbacks.Motion != NULL )
681 * Set the current window
683 fgStructure.Window = window ;
686 * Yup. Have it executed immediately
688 window->Callbacks.Motion( event.xmotion.x, event.xmotion.y );
694 * Otherwise it was a passive movement...
696 if( window->Callbacks.Passive != NULL )
699 * Set the current window
701 fgStructure.Window = window ;
704 * That's right, and there is a passive callback, too.
706 window->Callbacks.Passive( event.xmotion.x, event.xmotion.y );
715 GLboolean pressed = TRUE ;
718 if ( event.type == ButtonRelease ) pressed = FALSE ;
721 * A mouse button has been pressed or released. Traditionally,
722 * break if the window was found within the freeglut structures.
724 GETWINDOW( xbutton ); GETMOUSE( xbutton );
727 * GLUT API assumes that you can't have more than three mouse buttons, so:
729 switch( event.xbutton.button )
732 * WARNING: this might be wrong, if we only have two mouse buttons,
733 * Button2 might mean the right button, isn't that right?
735 case Button1: button = GLUT_LEFT_BUTTON; break;
736 case Button2: button = GLUT_MIDDLE_BUTTON; break;
737 case Button3: button = GLUT_RIGHT_BUTTON; break;
738 default: button = -1; break;
742 * Skip the unwanted mouse buttons...
748 * Do not execute the application's mouse callback if a menu is hooked to this button.
749 * In that case an appropriate private call should be generated.
750 * Near as I can tell, this is the menu behaviour:
751 * - Down-click the menu button, menu not active: activate the menu with its upper left-hand corner at the mouse location.
752 * - Down-click any button outside the menu, menu active: deactivate the menu
753 * - Down-click any button inside the menu, menu active: select the menu entry and deactivate the menu
754 * - Up-click the menu button, menu not active: nothing happens
755 * - Up-click the menu button outside the menu, menu active: nothing happens
756 * - Up-click the menu button inside the menu, menu active: select the menu entry and deactivate the menu
758 if ( window->ActiveMenu != NULL ) /* Window has an active menu, it absorbs any mouse click */
760 if ( fgCheckActiveMenu ( window, window->ActiveMenu ) == TRUE ) /* Inside the menu, invoke the callback and deactivate the menu*/
762 /* Save the current window and menu and set the current window to the window whose menu this is */
763 SFG_Window *save_window = fgStructure.Window ;
764 SFG_Menu *save_menu = fgStructure.Menu ;
765 fgSetWindow ( window ) ;
766 fgStructure.Menu = window->ActiveMenu ;
768 /* Execute the menu callback */
769 fgExecuteMenuCallback ( window->ActiveMenu ) ;
770 fgDeactivateMenu ( window->ActiveMenu->ParentWindow ) ;
772 /* Restore the current window and menu */
773 fgSetWindow ( save_window ) ;
774 fgStructure.Menu = save_menu ;
776 else /* Outside the menu, deactivate the menu if it's a downclick */
778 if ( pressed == TRUE ) fgDeactivateMenu ( window->ActiveMenu->ParentWindow ) ;
785 * No active menu, let's check whether we need to activate one.
787 if ( ( window->Menu[ button ] != NULL ) && ( pressed == TRUE ) )
790 * Set the current window
792 fgSetWindow( window );
795 * Activate the appropriate menu structure...
797 fgActivateMenu( window, button );
803 * Check if there is a mouse callback hooked to the window
805 if( fgStructure.Window->Callbacks.Mouse == NULL )
809 * Set the current window
811 fgSetWindow( window );
814 * Remember the current modifiers state
817 if (event.xbutton.state & (ShiftMask|LockMask))
818 modifiers |= GLUT_ACTIVE_SHIFT;
819 if (event.xbutton.state & ControlMask)
820 modifiers |= GLUT_ACTIVE_CTRL;
821 if (event.xbutton.state & Mod1Mask)
822 modifiers |= GLUT_ACTIVE_ALT;
823 fgStructure.Window->State.Modifiers = modifiers;
826 * Finally execute the mouse callback
828 fgStructure.Window->Callbacks.Mouse(
830 event.type == ButtonPress ? GLUT_DOWN : GLUT_UP,
836 * Trash the modifiers state
838 fgStructure.Window->State.Modifiers = 0xffffffff;
845 FGCBkeyboard keyboard_cb;
846 FGCBspecial special_cb;
849 * A key has been pressed, find the window that had the focus:
851 GETWINDOW( xkey ); GETMOUSE( xkey );
853 if( event.type == KeyPress )
855 keyboard_cb = window->Callbacks.Keyboard;
856 special_cb = window->Callbacks.Special;
860 keyboard_cb = window->Callbacks.KeyboardUp;
861 special_cb = window->Callbacks.SpecialUp;
865 * Is there a keyboard/special callback hooked for this window?
867 if( (keyboard_cb != NULL) || (special_cb != NULL) )
869 XComposeStatus composeStatus;
870 char asciiCode[ 32 ];
875 * Check for the ASCII/KeySym codes associated with the event:
877 len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode), &keySym, &composeStatus );
880 * Get ready to calling the keyboard/special callbacks
882 fgSetWindow( window );
885 * GLUT API tells us to have two separate callbacks...
890 * ...one for the ASCII translateable keypresses...
892 if( keyboard_cb != NULL )
895 * Remember the current modifiers state
898 if (event.xkey.state & (ShiftMask|LockMask))
899 modifiers |= GLUT_ACTIVE_SHIFT;
900 if (event.xkey.state & ControlMask)
901 modifiers |= GLUT_ACTIVE_CTRL;
902 if (event.xkey.state & Mod1Mask)
903 modifiers |= GLUT_ACTIVE_ALT;
904 window->State.Modifiers = modifiers;
907 * Execute the callback
909 keyboard_cb( asciiCode[ 0 ], event.xkey.x, event.xkey.y );
912 * Trash the modifiers state
914 window->State.Modifiers = 0xffffffff;
922 * ...and one for all the others, which need to be translated to GLUT_KEY_Xs...
927 * First the function keys come:
929 case XK_F1: special = GLUT_KEY_F1; break;
930 case XK_F2: special = GLUT_KEY_F2; break;
931 case XK_F3: special = GLUT_KEY_F3; break;
932 case XK_F4: special = GLUT_KEY_F4; break;
933 case XK_F5: special = GLUT_KEY_F5; break;
934 case XK_F6: special = GLUT_KEY_F6; break;
935 case XK_F7: special = GLUT_KEY_F7; break;
936 case XK_F8: special = GLUT_KEY_F8; break;
937 case XK_F9: special = GLUT_KEY_F9; break;
938 case XK_F10: special = GLUT_KEY_F10; break;
939 case XK_F11: special = GLUT_KEY_F11; break;
940 case XK_F12: special = GLUT_KEY_F12; break;
943 * Then the arrows and stuff:
945 case XK_Left: special = GLUT_KEY_LEFT; break;
946 case XK_Right: special = GLUT_KEY_RIGHT; break;
947 case XK_Up: special = GLUT_KEY_UP; break;
948 case XK_Down: special = GLUT_KEY_DOWN; break;
951 case XK_Prior: special = GLUT_KEY_PAGE_UP; break;
953 case XK_Next: special = GLUT_KEY_PAGE_DOWN; break;
955 case XK_Home: special = GLUT_KEY_HOME; break;
957 case XK_End: special = GLUT_KEY_END; break;
959 case XK_Insert: special = GLUT_KEY_INSERT; break;
963 * Execute the callback (if one has been specified),
964 * given that the special code seems to be valid...
966 if( (special_cb != NULL) && (special != -1) )
969 * Remember the current modifiers state
972 if (event.xkey.state & (ShiftMask|LockMask))
973 modifiers |= GLUT_ACTIVE_SHIFT;
974 if (event.xkey.state & ControlMask)
975 modifiers |= GLUT_ACTIVE_CTRL;
976 if (event.xkey.state & Mod1Mask)
977 modifiers |= GLUT_ACTIVE_ALT;
978 window->State.Modifiers = modifiers;
980 special_cb( special, event.xkey.x, event.xkey.y );
983 * Trash the modifiers state
985 window->State.Modifiers = 0xffffffff;
996 * Have all the timers checked.
1001 * Poll the joystick and notify all windows that want to be notified...
1003 fghCheckJoystickPolls();
1006 * No messages in the queue, which means we are idling...
1007 * Don't call this if there is a menu active
1009 if( ( fgState.IdleCallback != NULL ) && ( fgState.ActiveMenus == 0 ) )
1010 fgState.IdleCallback();
1013 * Remember about displaying all the windows that have
1014 * been marked for a redisplay (possibly in the idle call):
1019 #elif TARGET_HOST_WIN32
1024 * The windows processing is considerably smaller
1026 if( PeekMessage( &stMsg, NULL, 0, 0, PM_NOREMOVE ) )
1029 * Grab the message now, checking for WM_QUIT
1031 if( GetMessage( &stMsg, NULL, 0, 0 ) == 0 )
1032 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
1035 * Translate virtual-key messages and send them to the window...
1037 TranslateMessage( &stMsg );
1038 DispatchMessage( &stMsg );
1043 * Have all the timers checked.
1048 * Poll the joystick and notify all windows that want to be notified...
1050 fghCheckJoystickPolls();
1053 * No messages in the queue, which means we are idling...
1054 * Don't call this if there is a menu active
1056 if( ( fgState.IdleCallback != NULL ) && ( fgState.ActiveMenus == 0 ) )
1057 fgState.IdleCallback();
1060 * Remember about displaying all the windows that have
1061 * been marked for a redisplay (possibly in the idle call):
1068 * If an event caused a window to be closed, do the actual closing here
1074 * Enters the freeglut processing loop. Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".
1076 void FGAPIENTRY glutMainLoop( void )
1078 #if TARGET_HOST_WIN32
1079 SFG_Window *window = (SFG_Window *)fgStructure.Windows.First ;
1083 * Make sure the display has been created etc.
1085 freeglut_assert_ready;
1087 #if TARGET_HOST_WIN32
1089 * Processing before the main loop: If there is a window which is open and which
1090 * has a visibility callback, call it. I know this is an ugly hack, but I'm not sure
1091 * what else to do about it. Ideally we should leave something uninitialized in the
1092 * create window code and initialize it in the main loop, and have that initialization
1093 * create a "WM_ACTIVATE" message. Then we would put the visibility callback code in
1094 * the "case WM_ACTIVATE" block below. - John Fay -- 10/24/02
1096 while ( window != NULL )
1098 if ( window->Callbacks.Visibility != NULL )
1099 window->Callbacks.Visibility ( window->State.Visible ) ;
1101 window = (SFG_Window *)window->Node.Next ;
1106 * Set freeglut to be running
1108 fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;
1111 * Enter the main loop itself. Inside the loop, process events and check for loop exit.
1113 while ( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )
1115 glutMainLoopEvent () ;
1118 * If there are no more windows open, stop execution
1120 if ( fgStructure.Windows.First == NULL )
1121 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
1126 * If we got here by the user closing a window or by the application closing down, there may still be windows open.
1128 fgCleanUpGlutsMess () ;
1131 * Check whether we return to the calling program or simply exit
1133 if ( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
1137 * When this loop terminates, destroy the display, state and structure
1138 * of a freeglut session, so that another glutInit() call can happen
1144 * Leaves the freeglut processing loop.
1146 void FGAPIENTRY glutLeaveMainLoop( void )
1148 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
1152 * The window procedure for handling Win32 events
1154 #if TARGET_HOST_WIN32
1155 LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
1157 SFG_Window* window = fgWindowByHandle( hWnd );
1161 if ( ( window == NULL ) && ( uMsg != WM_CREATE ) )
1162 return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
1164 /* printf ( "Window %3d message <%04x> %12d %12d\n", window?window->ID:0, uMsg, wParam, lParam ) ; */
1166 * Check what type of message are we receiving
1172 * The window structure is passed as the creation structure paramter...
1174 window = (SFG_Window *) (((LPCREATESTRUCT) lParam)->lpCreateParams);
1175 assert( window != NULL );
1178 * We can safely store the window's handle now:
1180 window->Window.Handle = hWnd;
1183 * Get the window's device context
1185 window->Window.Device = GetDC( hWnd );
1188 * Create or get the OpenGL rendering context now
1190 if ( fgState.BuildingAMenu )
1193 * Setup the pixel format of our window
1195 unsigned int current_DisplayMode = fgState.DisplayMode ;
1196 fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH ;
1197 fgSetupPixelFormat( window, FALSE, PFD_MAIN_PLANE );
1198 fgState.DisplayMode = current_DisplayMode ;
1201 * If there isn't already an OpenGL rendering context for menu windows, make one
1203 if ( !fgStructure.MenuContext )
1205 fgStructure.MenuContext = (SFG_MenuContext *)malloc ( sizeof(SFG_MenuContext) ) ;
1206 fgStructure.MenuContext->Context = wglCreateContext( window->Window.Device );
1209 wglMakeCurrent ( window->Window.Device, fgStructure.MenuContext->Context ) ;
1211 /* window->Window.Context = wglGetCurrentContext () ; */
1212 window->Window.Context = wglCreateContext( window->Window.Device );
1217 * Setup the pixel format of our window
1219 fgSetupPixelFormat( window, FALSE, PFD_MAIN_PLANE );
1221 if ( fgState.UseCurrentContext == TRUE )
1223 window->Window.Context = wglGetCurrentContext();
1224 if ( ! window->Window.Context )
1225 window->Window.Context = wglCreateContext( window->Window.Device );
1228 window->Window.Context = wglCreateContext( window->Window.Device );
1232 * Still, we'll be needing to explicitly resize the window
1234 window->State.NeedToResize = TRUE;
1237 * Finally, have the window's device context released
1239 ReleaseDC( window->Window.Handle, window->Window.Device );
1244 * We got resized... But check if the window has been already added...
1246 fghReshapeWindowByHandle( hWnd, LOWORD(lParam), HIWORD(lParam) );
1250 printf("WM_SETFOCUS: %p\n", window );
1251 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1255 if (LOWORD(wParam) != WA_INACTIVE)
1257 /* glutSetCursor( fgStructure.Window->State.Cursor ); */
1258 printf("WM_ACTIVATE: glutSetCursor( %p, %d)\n", window, window->State.Cursor );
1260 glutSetCursor( window->State.Cursor );
1263 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1269 * Windows seems to need reminding to erase the cursor for NONE.
1272 if ((LOWORD(lParam) == HTCLIENT) &&
1273 (fgStructure.Window->State.Cursor == GLUT_CURSOR_NONE))
1276 /* Set the cursor AND change it for this window class. */
1277 # define MAP_CURSOR(a,b) case a: SetCursor( LoadCursor( NULL, b ) ); \
1279 /* Nuke the cursor AND change it for this window class. */
1280 # define ZAP_CURSOR(a,b) case a: SetCursor( NULL ); \
1283 if (LOWORD(lParam) == HTCLIENT)
1284 switch( window->State.Cursor )
1286 MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, IDC_ARROW );
1287 MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW, IDC_ARROW );
1288 MAP_CURSOR( GLUT_CURSOR_INFO, IDC_HELP );
1289 MAP_CURSOR( GLUT_CURSOR_DESTROY, IDC_CROSS );
1290 MAP_CURSOR( GLUT_CURSOR_HELP, IDC_HELP );
1291 MAP_CURSOR( GLUT_CURSOR_CYCLE, IDC_SIZEALL );
1292 MAP_CURSOR( GLUT_CURSOR_SPRAY, IDC_CROSS );
1293 MAP_CURSOR( GLUT_CURSOR_WAIT, IDC_WAIT );
1294 MAP_CURSOR( GLUT_CURSOR_TEXT, IDC_UPARROW );
1295 MAP_CURSOR( GLUT_CURSOR_CROSSHAIR, IDC_CROSS );
1296 /* MAP_CURSOR( GLUT_CURSOR_NONE, IDC_NO ); */
1297 ZAP_CURSOR( GLUT_CURSOR_NONE, NULL );
1300 MAP_CURSOR( GLUT_CURSOR_UP_DOWN, IDC_ARROW );
1304 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1309 * We are now Visible!
1311 window->State.Visible = TRUE;
1312 window->State.Redisplay = TRUE;
1317 * Start the painting job
1320 BeginPaint( hWnd, &ps );
1323 * Call the engine's main frame drawing method
1325 fghRedrawWindowByHandle( hWnd );
1328 * End the painting job, release the device context
1330 EndPaint( hWnd, &ps );
1335 * Make sure we don't close a window with current context active
1337 if( fgStructure.Window == window )
1342 wglMakeCurrent( NULL, NULL );
1343 /* Step through the list of windows. If the rendering context is notbeing used
1344 * by another window, then we delete it.
1346 for ( iter = (SFG_Window *)fgStructure.Windows.First; iter; iter = (SFG_Window *)iter->Node.Next )
1348 if ( ( iter->Window.Context == window->Window.Context ) && ( iter != window ) )
1352 if ( used == FALSE ) wglDeleteContext( window->Window.Context );
1356 * Put on a linked list of windows to be removed after all the callbacks have returned
1358 fgAddToWindowDestroyList ( window, FALSE ) ;
1361 * Proceed with the window destruction
1363 DestroyWindow( hWnd );
1368 * The window already got destroyed, so don't bother with it.
1375 * The mouse cursor has moved. Remember the new mouse cursor's position
1377 window->State.MouseX = LOWORD( lParam );
1378 window->State.MouseY = HIWORD( lParam );
1381 * Fallback if there's an active menu hooked to this window
1383 if ( window->ActiveMenu != NULL )
1386 * Let's make the window redraw as a result of the mouse motion.
1388 if ( window->IsMenu ) window->State.Redisplay = TRUE ;
1394 * Remember the current modifiers state.
1396 window->State.Modifiers =
1397 ( ( (GetKeyState( VK_LSHIFT ) < 0 ) || ( GetKeyState( VK_RSHIFT ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1398 ( ( (GetKeyState( VK_LCONTROL ) < 0 ) || ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL : 0 ) |
1399 ( ( (GetKeyState( VK_LMENU ) < 0 ) || ( GetKeyState( VK_RMENU ) < 0 )) ? GLUT_ACTIVE_ALT : 0 );
1402 * Check if any of the mouse buttons is pressed...
1404 if( (wParam & MK_LBUTTON) || (wParam & MK_MBUTTON) || (wParam & MK_RBUTTON) )
1407 * Yeah, indeed. We need to use the motion callback then:
1409 if( window->Callbacks.Motion != NULL )
1412 * Make sure the current window is set...
1414 fgSetWindow( window );
1417 * Execute the active mouse motion callback now
1419 window->Callbacks.Motion( window->State.MouseX, window->State.MouseY );
1425 * All mouse buttons are up, execute the passive mouse motion callback
1427 if( window->Callbacks.Passive != NULL )
1430 * Make sure the current window is set
1432 fgSetWindow( window );
1435 * Execute the passive mouse motion callback
1437 window->Callbacks.Passive( window->State.MouseX, window->State.MouseY );
1442 * Thrash the current modifiers state now
1444 window->State.Modifiers = 0xffffffff;
1448 case WM_LBUTTONDOWN:
1449 case WM_MBUTTONDOWN:
1450 case WM_RBUTTONDOWN:
1455 GLboolean pressed = TRUE;
1459 * The mouse cursor has moved. Remember the new mouse cursor's position
1461 window->State.MouseX = LOWORD( lParam );
1462 window->State.MouseY = HIWORD( lParam );
1465 * We're curious about the GLUT API button name...
1469 case WM_LBUTTONDOWN: pressed = TRUE; button = GLUT_LEFT_BUTTON; break;
1470 case WM_MBUTTONDOWN: pressed = TRUE; button = GLUT_MIDDLE_BUTTON; break;
1471 case WM_RBUTTONDOWN: pressed = TRUE; button = GLUT_RIGHT_BUTTON; break;
1472 case WM_LBUTTONUP: pressed = FALSE; button = GLUT_LEFT_BUTTON; break;
1473 case WM_MBUTTONUP: pressed = FALSE; button = GLUT_MIDDLE_BUTTON; break;
1474 case WM_RBUTTONUP: pressed = FALSE; button = GLUT_RIGHT_BUTTON; break;
1475 default: pressed = FALSE; button = -1; break;
1479 * The left and right mouse buttons might have been swapped...
1481 if( GetSystemMetrics( SM_SWAPBUTTON ) )
1482 if( button == GLUT_LEFT_BUTTON ) button = GLUT_RIGHT_BUTTON;
1483 else if( button == GLUT_RIGHT_BUTTON ) button = GLUT_LEFT_BUTTON;
1486 * Hey, what's up with you?
1489 return( DefWindowProc( hWnd, uMsg, lParam, wParam ) );
1492 * Do not execute the application's mouse callback if a menu is hooked to this button.
1493 * In that case an appropriate private call should be generated.
1494 * Near as I can tell, this is the menu behaviour:
1495 * - Down-click the menu button, menu not active: activate the menu with its upper left-hand corner at the mouse location.
1496 * - Down-click any button outside the menu, menu active: deactivate the menu
1497 * - Down-click any button inside the menu, menu active: select the menu entry and deactivate the menu
1498 * - Up-click the menu button, menu not active: nothing happens
1499 * - Up-click the menu button outside the menu, menu active: nothing happens
1500 * - Up-click the menu button inside the menu, menu active: select the menu entry and deactivate the menu
1502 if ( window->ActiveMenu != NULL ) /* Window has an active menu, it absorbs any mouse click */
1504 if ( fgCheckActiveMenu ( window, window->ActiveMenu ) == TRUE ) /* Inside the menu, invoke the callback and deactivate the menu*/
1506 /* Save the current window and menu and set the current window to the window whose menu this is */
1507 SFG_Window *save_window = fgStructure.Window ;
1508 SFG_Menu *save_menu = fgStructure.Menu ;
1509 fgSetWindow ( window ) ;
1510 fgStructure.Menu = window->ActiveMenu ;
1512 /* Execute the menu callback */
1513 fgExecuteMenuCallback ( window->ActiveMenu ) ;
1514 fgDeactivateMenu ( window->ActiveMenu->ParentWindow ) ;
1516 /* Restore the current window and menu */
1517 fgSetWindow ( save_window ) ;
1518 fgStructure.Menu = save_menu ;
1520 else /* Outside the menu, deactivate the menu if it's a downclick */
1522 if ( pressed == TRUE ) fgDeactivateMenu ( window->ActiveMenu->ParentWindow ) ;
1529 * No active menu, let's check whether we need to activate one.
1531 if ( ( window->Menu[ button ] != NULL ) && ( pressed == TRUE ) )
1534 * Set the current window
1536 fgSetWindow( window );
1539 * Activate the appropriate menu structure...
1541 fgActivateMenu( window, button );
1547 * Check if there is a mouse callback hooked to the window
1549 if( fgStructure.Window->Callbacks.Mouse == NULL )
1553 * Set the current window
1555 fgSetWindow( window );
1558 * Remember the current modifiers state.
1560 fgStructure.Window->State.Modifiers =
1561 ( ( (GetKeyState( VK_LSHIFT ) < 0 ) || ( GetKeyState( VK_RSHIFT ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1562 ( ( (GetKeyState( VK_LCONTROL ) < 0 ) || ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL : 0 ) |
1563 ( ( (GetKeyState( VK_LMENU ) < 0 ) || ( GetKeyState( VK_RMENU ) < 0 )) ? GLUT_ACTIVE_ALT : 0 );
1566 * Finally execute the mouse callback
1568 fgStructure.Window->Callbacks.Mouse(
1570 pressed == TRUE ? GLUT_DOWN : GLUT_UP,
1571 window->State.MouseX,
1572 window->State.MouseY
1576 * Trash the modifiers state
1578 fgStructure.Window->State.Modifiers = 0xffffffff;
1589 * Ignore the automatic key repetition if needed:
1591 if( fgState.IgnoreKeyRepeat && (lParam & KF_REPEAT) )
1595 * Set the current window
1597 fgSetWindow( window );
1600 * Remember the current modifiers state. This is done here in order
1601 * to make sure the VK_DELETE keyboard callback is executed properly.
1603 window->State.Modifiers =
1604 ( ( (GetKeyState( VK_LSHIFT ) < 0 ) || ( GetKeyState( VK_RSHIFT ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1605 ( ( (GetKeyState( VK_LCONTROL ) < 0 ) || ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL : 0 ) |
1606 ( ( (GetKeyState( VK_LMENU ) < 0 ) || ( GetKeyState( VK_RMENU ) < 0 )) ? GLUT_ACTIVE_ALT : 0 );
1609 * Set the mouse position
1611 GetCursorPos ( &mouse_pos ) ;
1612 ScreenToClient ( window->Window.Handle, &mouse_pos ) ;
1614 window->State.MouseX = mouse_pos.x ;
1615 window->State.MouseY = mouse_pos.y ;
1618 * Convert the Win32 keystroke codes to GLUTtish way
1620 # define KEY(a,b) case a: keypress = b; break;
1625 * Most of the special characters can be handled automagically...
1627 KEY( VK_F1, GLUT_KEY_F1 ); KEY( VK_F2, GLUT_KEY_F2 );
1628 KEY( VK_F3, GLUT_KEY_F3 ); KEY( VK_F4, GLUT_KEY_F4 );
1629 KEY( VK_F5, GLUT_KEY_F5 ); KEY( VK_F6, GLUT_KEY_F6 );
1630 KEY( VK_F7, GLUT_KEY_F7 ); KEY( VK_F8, GLUT_KEY_F8 );
1631 KEY( VK_F9, GLUT_KEY_F9 ); KEY( VK_F10, GLUT_KEY_F10 );
1632 KEY( VK_F11, GLUT_KEY_F11 ); KEY( VK_F12, GLUT_KEY_F12 );
1633 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP ); KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
1634 KEY( VK_HOME, GLUT_KEY_HOME ); KEY( VK_END, GLUT_KEY_END );
1635 KEY( VK_LEFT, GLUT_KEY_LEFT ); KEY( VK_UP, GLUT_KEY_UP );
1636 KEY( VK_RIGHT, GLUT_KEY_RIGHT ); KEY( VK_DOWN, GLUT_KEY_DOWN );
1637 KEY( VK_INSERT, GLUT_KEY_INSERT );
1640 * ...yet there is a small exception we need to have handled...
1644 * The delete key should be treated as an ASCII keypress:
1646 if( window->Callbacks.Keyboard != NULL )
1647 window->Callbacks.Keyboard( 127, window->State.MouseX, window->State.MouseY );
1651 * Execute the special callback, if present, given the conversion was a success:
1653 if( (keypress != -1) && (window->Callbacks.Special != NULL) )
1656 * Have the special callback executed:
1658 window->Callbacks.Special( keypress, window->State.MouseX, window->State.MouseY );
1662 * Thrash the modifiers register now
1664 window->State.Modifiers = 0xffffffff;
1675 * Set the current window
1677 fgSetWindow( window );
1680 * Remember the current modifiers state. This is done here in order
1681 * to make sure the VK_DELETE keyboard callback is executed properly.
1683 window->State.Modifiers =
1684 ( ( (GetKeyState( VK_LSHIFT ) < 0 ) || ( GetKeyState( VK_RSHIFT ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1685 ( ( (GetKeyState( VK_LCONTROL ) < 0 ) || ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL : 0 ) |
1686 ( ( (GetKeyState( VK_LMENU ) < 0 ) || ( GetKeyState( VK_RMENU ) < 0 )) ? GLUT_ACTIVE_ALT : 0 );
1689 * Set the mouse position
1691 GetCursorPos ( &mouse_pos ) ;
1692 ScreenToClient ( window->Window.Handle, &mouse_pos ) ;
1694 window->State.MouseX = mouse_pos.x ;
1695 window->State.MouseY = mouse_pos.y ;
1698 * Convert the Win32 keystroke codes to GLUTtish way. "KEY(a,b)" was defined under "WM_KEYDOWN"
1704 * Most of the special characters can be handled automagically...
1706 KEY( VK_F1, GLUT_KEY_F1 ); KEY( VK_F2, GLUT_KEY_F2 );
1707 KEY( VK_F3, GLUT_KEY_F3 ); KEY( VK_F4, GLUT_KEY_F4 );
1708 KEY( VK_F5, GLUT_KEY_F5 ); KEY( VK_F6, GLUT_KEY_F6 );
1709 KEY( VK_F7, GLUT_KEY_F7 ); KEY( VK_F8, GLUT_KEY_F8 );
1710 KEY( VK_F9, GLUT_KEY_F9 ); KEY( VK_F10, GLUT_KEY_F10 );
1711 KEY( VK_F11, GLUT_KEY_F11 ); KEY( VK_F12, GLUT_KEY_F12 );
1712 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP ); KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
1713 KEY( VK_HOME, GLUT_KEY_HOME ); KEY( VK_END, GLUT_KEY_END );
1714 KEY( VK_LEFT, GLUT_KEY_LEFT ); KEY( VK_UP, GLUT_KEY_UP );
1715 KEY( VK_RIGHT, GLUT_KEY_RIGHT ); KEY( VK_DOWN, GLUT_KEY_DOWN );
1716 KEY( VK_INSERT, GLUT_KEY_INSERT );
1719 * ...yet there is a small exception we need to have handled...
1723 * The delete key should be treated as an ASCII keypress:
1725 if( window->Callbacks.KeyboardUp != NULL )
1726 window->Callbacks.KeyboardUp( 127, window->State.MouseX, window->State.MouseY );
1732 * Call the KeyboardUp callback for a regular character if there is one.
1737 GetKeyboardState(state);
1739 if ( ToAscii( wParam, 0, state, code, 0 ) == 1 )
1742 if( window->Callbacks.KeyboardUp != NULL )
1743 window->Callbacks.KeyboardUp( (char)wParam, window->State.MouseX, window->State.MouseY );
1748 * Execute the special callback, if present, given the conversion was a success:
1750 if( (keypress != -1) && (window->Callbacks.SpecialUp != NULL) )
1753 * Have the special callback executed:
1755 window->Callbacks.SpecialUp( keypress, window->State.MouseX, window->State.MouseY );
1759 * Thrash the modifiers register now
1761 window->State.Modifiers = 0xffffffff;
1769 * Ignore the automatic key repetition if needed:
1771 if( fgState.IgnoreKeyRepeat && (lParam & KF_REPEAT) )
1775 * Clear to go with the keyboard callback, if registered:
1777 if( window->Callbacks.Keyboard != NULL )
1780 * Remember the current modifiers state
1782 window->State.Modifiers =
1783 ( ( (GetKeyState( VK_LSHIFT ) < 0 ) || ( GetKeyState( VK_RSHIFT ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1784 ( ( (GetKeyState( VK_LCONTROL ) < 0 ) || ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL : 0 ) |
1785 ( ( (GetKeyState( VK_LMENU ) < 0 ) || ( GetKeyState( VK_RMENU ) < 0 )) ? GLUT_ACTIVE_ALT : 0 );
1788 * Have the special callback executed:
1790 window->Callbacks.Keyboard( (char)wParam, window->State.MouseX, window->State.MouseY );
1793 * Thrash the modifiers register now
1795 window->State.Modifiers = 0xffffffff;
1800 case WM_CAPTURECHANGED : /* User has finished resizing the window, force a redraw */
1801 if ( window->Callbacks.Display )
1802 window->Callbacks.Display () ;
1804 /* lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ) ; */
1808 * Other messages that I have seen and which are not handled already
1810 case WM_SETTEXT : /* 0x000c */
1811 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ); /* Pass it on to "DefWindowProc" to set the window text */
1814 case WM_GETTEXT : /* 0x000d */
1815 /* Ideally we would copy the title of the window into "lParam" */
1816 /* strncpy ( (char *)lParam, "Window Title", wParam ) ;
1817 lRet = ( wParam > 12 ) ? 12 : wParam ; */ /* the number of characters copied */
1818 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1821 case WM_GETTEXTLENGTH : /* 0x000e */
1822 /* Ideally we would get the length of the title of the window */
1823 lRet = 12 ; /* the number of characters in "Window Title\0" (see above) */
1826 case WM_ERASEBKGND : /* 0x0014 */
1827 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1830 case WM_SYNCPAINT : /* 0x0088 */
1831 /* Another window has moved, need to update this one */
1832 window->State.Redisplay = TRUE ;
1833 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ); /* Help screen says this message must be passed to "DefWindowProc" */
1836 case WM_NCPAINT : /* 0x0085 */
1837 /* Need to update the border of this window */
1838 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ); /* Pass it on to "DefWindowProc" to repaint a standard border */
1843 * Handle unhandled messages
1845 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1853 /*** END OF FILE ***/