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 #include "../include/GL/freeglut.h"
33 #include "freeglut_internal.h"
36 #if TARGET_HOST_UNIX_X11
37 #include <sys/types.h>
42 #elif TARGET_HOST_WIN32
46 #define MAX(a,b) (((a)>(b)) ? (a) : (b))
50 #define MIN(a,b) (((a)<(b)) ? (a) : (b))
55 * TODO BEFORE THE STABLE RELEASE:
57 * There are some issues concerning window redrawing under X11, and maybe
58 * some events are not handled. The Win32 version lacks some more features,
59 * but seems acceptable for not demanding purposes.
61 * Need to investigate why the X11 version breaks out with an error when
62 * closing a window (using the window manager, not glutDestroyWindow)...
65 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
68 * Handle a window configuration change. When no reshape
69 * callback is hooked, the viewport size is updated to
70 * match the new window size.
72 static void fghReshapeWindowByHandle ( SFG_WindowHandleType handle,
73 int width, int height )
75 SFG_Window *current_window = fgStructure.Window;
77 SFG_Window* window = fgWindowByHandle( handle );
78 freeglut_return_if_fail( window != NULL );
81 #if TARGET_HOST_UNIX_X11
83 XResizeWindow( fgDisplay.Display, window->Window.Handle,
85 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
87 #elif TARGET_HOST_WIN32
93 GetWindowRect( window->Window.Handle, &winRect );
97 if ( window->Parent == NULL )
100 * Adjust the size of the window to allow for the size of the
101 * frame, if we are not a menu
103 if ( ! window->IsMenu )
105 width += GetSystemMetrics( SM_CXSIZEFRAME ) * 2;
106 height += GetSystemMetrics( SM_CYSIZEFRAME ) * 2 +
107 GetSystemMetrics( SM_CYCAPTION );
112 GetWindowRect( window->Parent->Window.Handle,
114 x -= winRect.left + GetSystemMetrics( SM_CXSIZEFRAME );
115 y -= winRect.top + GetSystemMetrics( SM_CYSIZEFRAME ) +
116 GetSystemMetrics( SM_CYCAPTION );
120 window->Window.Handle,
131 if( !( FETCH_WCB( *window, Reshape ) ) )
133 fgSetWindow( window );
134 glViewport( 0, 0, width, height );
137 INVOKE_WCB( *window, Reshape, ( width, height ) );
140 * Force a window redraw. In Windows at least this is only a partial
141 * solution: if the window is increasing in size in either dimension,
142 * the already-drawn part does not get drawn again and things look funny.
143 * But without this we get this bad behaviour whenever we resize the
146 window->State.Redisplay = GL_TRUE;
149 fgSetWindow( current_window );
153 * Calls a window's redraw method. This is used when
154 * a redraw is forced by the incoming window messages.
156 static void fghRedrawWindowByHandle ( SFG_WindowHandleType handle )
158 SFG_Window* window = fgWindowByHandle( handle );
159 freeglut_return_if_fail( window );
160 freeglut_return_if_fail( FETCH_WCB ( *window, Display ) );
162 window->State.Redisplay = GL_FALSE;
164 freeglut_return_if_fail( window->State.Visible );
166 if( window->State.NeedToResize )
168 SFG_Window *current_window = fgStructure.Window;
170 fgSetWindow( window );
172 fghReshapeWindowByHandle(
173 window->Window.Handle,
178 window->State.NeedToResize = GL_FALSE;
179 fgSetWindow ( current_window );
182 INVOKE_WCB( *window, Display, ( ) );
186 * A static helper function to execute display callback for a window
188 static void fghcbDisplayWindow( SFG_Window *window,
189 SFG_Enumerator *enumerator )
191 if( window->State.Redisplay &&
192 window->State.Visible )
194 if( window->State.NeedToResize )
196 SFG_Window *current_window = fgStructure.Window;
198 fgSetWindow( window );
200 fghReshapeWindowByHandle(
201 window->Window.Handle,
206 window->State.NeedToResize = GL_FALSE;
207 fgSetWindow ( current_window );
210 window->State.Redisplay = GL_FALSE;
212 #if TARGET_HOST_UNIX_X11
214 SFG_Window *current_window = fgStructure.Window;
216 INVOKE_WCB( *window, Display, ( ) );
217 fgSetWindow( current_window );
219 #elif TARGET_HOST_WIN32
221 window->Window.Handle, NULL, NULL,
222 RDW_NOERASE | RDW_INTERNALPAINT | RDW_INVALIDATE | RDW_UPDATENOW
227 fgEnumSubWindows( window, fghcbDisplayWindow, enumerator );
231 * Make all windows perform a display call
233 static void fghDisplayAll( void )
235 SFG_Enumerator enumerator;
237 enumerator.found = GL_FALSE;
238 enumerator.data = NULL;
240 fgEnumWindows( fghcbDisplayWindow, &enumerator );
244 * Window enumerator callback to check for the joystick polling code
246 static void fghcbCheckJoystickPolls( SFG_Window *window,
247 SFG_Enumerator *enumerator )
249 long int checkTime = fgElapsedTime( );
251 if( window->State.JoystickLastPoll + window->State.JoystickPollRate <=
254 fgJoystickPollWindow( window );
255 window->State.JoystickLastPoll = checkTime;
258 fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );
262 * Check all windows for joystick polling
264 static void fghCheckJoystickPolls( void )
266 SFG_Enumerator enumerator;
268 enumerator.found = GL_FALSE;
269 enumerator.data = NULL;
271 fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );
275 * Check the global timers
277 static void fghCheckTimers( void )
279 long checkTime = fgElapsedTime( );
282 while( timer = fgState.Timers.First )
284 if( timer->TriggerTime > checkTime )
287 fgListRemove( &fgState.Timers, &timer->Node );
288 fgListAppend( &fgState.FreeTimers, &timer->Node );
290 timer->Callback( timer->ID );
297 long fgElapsedTime( void )
299 if ( fgState.Time.Set )
301 #if TARGET_HOST_UNIX_X11
305 gettimeofday( &now, NULL );
307 elapsed = (now.tv_usec - fgState.Time.Value.tv_usec) / 1000;
308 elapsed += (now.tv_sec - fgState.Time.Value.tv_sec) * 1000;
311 #elif TARGET_HOST_WIN32
312 return timeGetTime() - fgState.Time.Value;
317 #if TARGET_HOST_UNIX_X11
318 gettimeofday( &fgState.Time.Value, NULL );
319 #elif TARGET_HOST_WIN32
320 fgState.Time.Value = timeGetTime ();
322 fgState.Time.Set = GL_TRUE ;
331 void fgError( const char *fmt, ... )
337 fprintf( stderr, "freeglut ");
338 if( fgState.ProgramName )
339 fprintf (stderr, "(%s): ", fgState.ProgramName);
340 vfprintf( stderr, fmt, ap );
341 fprintf( stderr, "\n" );
345 if ( fgState.Initialised )
351 void fgWarning( const char *fmt, ... )
357 fprintf( stderr, "freeglut ");
358 if( fgState.ProgramName )
359 fprintf( stderr, "(%s): ", fgState.ProgramName );
360 vfprintf( stderr, fmt, ap );
361 fprintf( stderr, "\n" );
367 * Indicates whether Joystick events are being used by ANY window.
369 * The current mechanism is to walk all of the windows and ask if
370 * there is a joystick callback. Certainly in some cases, maybe
371 * in all cases, the joystick is attached to the system and accessed
372 * from ONE point by GLUT/freeglut, so this is not the right way,
373 * in general, to do this. However, the Joystick code is segregated
374 * in its own little world, so we can't access the information that
375 * we need in order to do that nicely.
378 * * Store Joystick data into freeglut global state.
379 * * Provide NON-static functions or data from Joystick *.c file.
381 * Basically, the RIGHT way to do this requires knowing something
382 * about the Joystick. Right now, the Joystick code is behind
386 static void fgCheckJoystickCallback( SFG_Window* w, SFG_Enumerator* e)
388 if( FETCH_WCB( *w, Joystick ) )
393 fgEnumSubWindows( w, fgCheckJoystickCallback, e );
395 static int fgHaveJoystick( void )
397 SFG_Enumerator enumerator;
398 enumerator.found = GL_FALSE;
399 enumerator.data = NULL;
400 fgEnumWindows( fgCheckJoystickCallback, &enumerator );
401 return !!enumerator.data;
403 static void fgHavePendingRedisplaysCallback( SFG_Window* w, SFG_Enumerator* e)
405 if( w->State.Redisplay )
410 fgEnumSubWindows( w, fgHavePendingRedisplaysCallback, e );
412 static int fgHavePendingRedisplays (void)
414 SFG_Enumerator enumerator;
415 enumerator.found = GL_FALSE;
416 enumerator.data = NULL;
417 fgEnumWindows( fgHavePendingRedisplaysCallback, &enumerator );
418 return !!enumerator.data;
421 * Returns the number of GLUT ticks (milliseconds) till the next timer event.
423 static long fgNextTimer( void )
428 if( (timer = fgState.Timers.First) )
429 ret = timer->TriggerTime - fgElapsedTime();
434 * Does the magic required to relinquish the CPU until something interesting
437 static void fgSleepForEvents( void )
439 #if TARGET_HOST_UNIX_X11
446 if( fgState.IdleCallback || fgHavePendingRedisplays( ) )
448 socket = ConnectionNumber( fgDisplay.Display );
450 FD_SET( socket, &fdset );
452 msec = fgNextTimer( );
453 if( fgHaveJoystick( ) )
454 msec = MIN( msec, 10 );
456 wait.tv_sec = msec / 1000;
457 wait.tv_usec = (msec % 1000) * 1000;
458 err = select( socket+1, &fdset, NULL, NULL, &wait );
461 fgWarning ( "freeglut select() error: %d\n", errno );
463 #elif TARGET_HOST_WIN32
467 #if TARGET_HOST_UNIX_X11
469 * Returns GLUT modifier mask for an XEvent.
471 int fgGetXModifiers( XEvent *event )
475 if( event->xkey.state & ( ShiftMask | LockMask ) )
476 ret |= GLUT_ACTIVE_SHIFT;
477 if( event->xkey.state & ControlMask )
478 ret |= GLUT_ACTIVE_CTRL;
479 if( event->xkey.state & Mod1Mask )
480 ret |= GLUT_ACTIVE_ALT;
487 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
490 * Executes a single iteration in the freeglut processing loop.
492 void FGAPIENTRY glutMainLoopEvent( void )
494 #if TARGET_HOST_UNIX_X11
499 * This code was repeated constantly, so here it goes into a definition:
501 #define GETWINDOW(a) \
502 window = fgWindowByHandle( event.a.window ); \
503 if( window == NULL ) \
506 #define GETMOUSE(a) \
507 window->State.MouseX = event.a.x; \
508 window->State.MouseY = event.a.y;
510 freeglut_assert_ready;
512 while( XPending( fgDisplay.Display ) )
514 XNextEvent( fgDisplay.Display, &event );
520 * Destroy the window when the WM_DELETE_WINDOW message arrives
522 if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.DeleteWindow )
524 GETWINDOW( xclient );
526 fgDestroyWindow ( window );
528 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
534 fgState.ExecState = GLUT_EXEC_STATE_STOP;
540 * CreateNotify causes a configure-event so that sub-windows are
541 * handled compatibly with GLUT. Otherwise, your sub-windows
542 * (in freeglut only) will not get an initial reshape event,
543 * which can break things.
545 * XXX NOTE that it is possible that you will more than one Reshape
546 * XXX event for your top-level window, but something like this
547 * XXX appears to be required for compatbility.
549 * GLUT presumably does this because it generally tries to treat
550 * sub-windows the same as windows.
553 case ConfigureNotify:
554 GETWINDOW( xconfigure );
555 window->State.NeedToResize = GL_TRUE ;
556 window->State.Width = event.xconfigure.width ;
557 window->State.Height = event.xconfigure.height;
562 * This is sent to confirm the XDestroyWindow call.
563 * XXX WHY is this commented out? Should we re-enable it?
565 /* fgAddToWindowDestroyList ( window ); */
570 * We are too dumb to process partial exposes...
571 * XXX Well, we could do it. However, it seems to only
572 * XXX be potentially useful for single-buffered (since
573 * XXX double-buffered does not respect viewport when we
574 * XXX do a buffer-swap).
576 if( event.xexpose.count == 0 )
577 fghRedrawWindowByHandle( event.xexpose.window );
583 * If we never do anything with this, can we just not ask to
584 * get these messages?
590 * Have the client's keyboard knowledge updated (xlib.ps,
591 * page 206, says that's a good thing to do)
593 XRefreshKeyboardMapping( (XMappingEvent *) &event );
596 case VisibilityNotify:
598 GETWINDOW( xvisibility );
599 if( ! FETCH_WCB( *window, WindowStatus ) )
601 fgSetWindow( window );
604 * Sending this event, the X server can notify us that the window
605 * has just acquired one of the three possible visibility states:
606 * VisibilityUnobscured, VisibilityPartiallyObscured or
607 * VisibilityFullyObscured
609 switch( event.xvisibility.state )
611 case VisibilityUnobscured:
612 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_RETAINED ) );
613 window->State.Visible = GL_TRUE;
616 case VisibilityPartiallyObscured:
617 INVOKE_WCB( *window, WindowStatus,
618 ( GLUT_PARTIALLY_RETAINED ) );
619 window->State.Visible = GL_TRUE;
622 case VisibilityFullyObscured:
623 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_COVERED ) );
624 window->State.Visible = GL_FALSE;
628 fgWarning( "Uknown X visibility state: %d",
629 event.xvisibility.state );
637 GETWINDOW( xcrossing );
638 GETMOUSE( xcrossing );
639 INVOKE_WCB( *window, Entry, ( ( EnterNotify == event.type ) ?
646 GETWINDOW( xmotion );
649 if( window->ActiveMenu )
651 if( window == window->ActiveMenu->ParentWindow )
653 window->ActiveMenu->Window->State.MouseX =
654 event.xmotion.x_root - window->ActiveMenu->X;
655 window->ActiveMenu->Window->State.MouseY =
656 event.xmotion.y_root - window->ActiveMenu->Y;
658 window->ActiveMenu->Window->State.Redisplay = GL_TRUE ;
659 fgSetWindow( window->ActiveMenu->ParentWindow );
665 * XXX For more than 5 buttons, just check {event.xmotion.state},
666 * XXX rather than a host of bit-masks?
668 #define BUTTON_MASK \
669 ( Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask )
670 if ( event.xmotion.state & BUTTON_MASK )
671 INVOKE_WCB( *window, Motion, ( event.xmotion.x,
674 INVOKE_WCB( *window, Passive, ( event.xmotion.x,
682 GLboolean pressed = GL_TRUE;
685 if( event.type == ButtonRelease )
689 * A mouse button has been pressed or released. Traditionally,
690 * break if the window was found within the freeglut structures.
692 GETWINDOW( xbutton );
696 * An X button (at least in XFree86) is numbered from 1.
697 * A GLUT button is numbered from 0.
698 * Old GLUT passed through buttons other than just the first
699 * three, though it only gave symbolic names and official
700 * support to the first three.
702 button = event.xbutton.button - 1;
705 * XXX This comment is replicated in the WIN32 section and
706 * XXX maybe also in the menu code. Can we move the info
707 * XXX to one central place and *reference* it from here?
709 * Do not execute the application's mouse callback if a menu
710 * is hooked to this button. In that case an appropriate
711 * private call should be generated.
712 * Near as I can tell, this is the menu behaviour:
713 * - Down-click the menu button, menu not active: activate
714 * the menu with its upper left-hand corner at the mouse
716 * - Down-click any button outside the menu, menu active:
717 * deactivate the menu
718 * - Down-click any button inside the menu, menu active:
719 * select the menu entry and deactivate the menu
720 * - Up-click the menu button, menu not active: nothing happens
721 * - Up-click the menu button outside the menu, menu active:
723 * - Up-click the menu button inside the menu, menu active:
724 * select the menu entry and deactivate the menu
726 /* Window has an active menu, it absorbs any mouse click */
727 if( window->ActiveMenu )
729 if( window == window->ActiveMenu->ParentWindow )
731 window->ActiveMenu->Window->State.MouseX =
732 event.xbutton.x_root - window->ActiveMenu->X;
733 window->ActiveMenu->Window->State.MouseY =
734 event.xbutton.y_root - window->ActiveMenu->Y;
737 /* In the menu, invoke the callback and deactivate the menu*/
738 if( fgCheckActiveMenu( window->ActiveMenu->Window,
739 window->ActiveMenu ) )
742 * Save the current window and menu and set the current
743 * window to the window whose menu this is
745 SFG_Window *save_window = fgStructure.Window;
746 SFG_Menu *save_menu = fgStructure.Menu;
747 SFG_Window *parent_window =
748 window->ActiveMenu->ParentWindow;
749 fgSetWindow( parent_window );
750 fgStructure.Menu = window->ActiveMenu;
752 /* Execute the menu callback */
753 fgExecuteMenuCallback( window->ActiveMenu );
754 fgDeactivateMenu( parent_window );
756 /* Restore the current window and menu */
757 fgSetWindow( save_window );
758 fgStructure.Menu = save_menu;
762 * Outside the menu, deactivate if it's a downclick
763 * XXX This isn't enough. A downclick outside of
764 * XXX the interior of our freeglut windows should also
765 * XXX deactivate the menu. This is more complicated.
767 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
769 window->State.Redisplay = GL_TRUE;
774 * No active menu, let's check whether we need to activate one.
776 if( ( 0 <= button ) &&
777 ( FREEGLUT_MAX_MENUS > button ) &&
778 ( window->Menu[ button ] ) &&
781 window->State.Redisplay = GL_TRUE;
782 fgSetWindow( window );
783 fgActivateMenu( window, button );
788 * Check if there is a mouse or mouse wheel callback hooked to the
791 if( ! FETCH_WCB( *window, Mouse ) &&
792 ! FETCH_WCB( *window, MouseWheel ) )
795 fgState.Modifiers = fgGetXModifiers( &event );
798 * Finally execute the mouse or mouse wheel callback
800 * XXX Use a symbolic constant, *not* "4"!
802 if( ( button < 3 ) || ( ! FETCH_WCB( *window, MouseWheel ) ) )
803 INVOKE_WCB( *window, Mouse, ( button,
804 pressed ? GLUT_DOWN : GLUT_UP,
811 * Map 4 and 5 to wheel zero; EVEN to +1, ODD to -1
812 * " 6 and 7 " " one; ...
814 * XXX This *should* be behind some variables/macros,
815 * XXX since the order and numbering isn't certain
816 * XXX See XFree86 configuration docs (even back in the
817 * XXX 3.x days, and especially with 4.x).
819 * XXX Note that {button} has already been decremeted
820 * XXX in mapping from X button numbering to GLUT.
822 int wheel_number = (button - 3) / 2;
828 INVOKE_WCB( *window, MouseWheel, ( wheel_number,
836 * Trash the modifiers state
838 fgState.Modifiers = 0xffffffff;
845 FGCBKeyboard keyboard_cb;
846 FGCBSpecial special_cb;
851 if( event.type == KeyPress )
853 keyboard_cb = FETCH_WCB( *window, Keyboard );
854 special_cb = FETCH_WCB( *window, Special );
858 keyboard_cb = FETCH_WCB( *window, KeyboardUp );
859 special_cb = FETCH_WCB( *window, SpecialUp );
863 * Is there a keyboard/special callback hooked for this window?
865 if( keyboard_cb || special_cb )
867 XComposeStatus composeStatus;
868 char asciiCode[ 32 ];
873 * Check for the ASCII/KeySym codes associated with the event:
875 len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode),
876 &keySym, &composeStatus
880 * GLUT API tells us to have two separate callbacks...
885 * ...one for the ASCII translateable keypresses...
889 fgSetWindow( window );
890 fgState.Modifiers = fgGetXModifiers( &event );
891 keyboard_cb( asciiCode[ 0 ],
892 event.xkey.x, event.xkey.y
894 fgState.Modifiers = 0xffffffff;
902 * ...and one for all the others, which need to be
903 * translated to GLUT_KEY_Xs...
907 case XK_F1: special = GLUT_KEY_F1; break;
908 case XK_F2: special = GLUT_KEY_F2; break;
909 case XK_F3: special = GLUT_KEY_F3; break;
910 case XK_F4: special = GLUT_KEY_F4; break;
911 case XK_F5: special = GLUT_KEY_F5; break;
912 case XK_F6: special = GLUT_KEY_F6; break;
913 case XK_F7: special = GLUT_KEY_F7; break;
914 case XK_F8: special = GLUT_KEY_F8; break;
915 case XK_F9: special = GLUT_KEY_F9; break;
916 case XK_F10: special = GLUT_KEY_F10; break;
917 case XK_F11: special = GLUT_KEY_F11; break;
918 case XK_F12: special = GLUT_KEY_F12; break;
920 case XK_Left: special = GLUT_KEY_LEFT; break;
921 case XK_Right: special = GLUT_KEY_RIGHT; break;
922 case XK_Up: special = GLUT_KEY_UP; break;
923 case XK_Down: special = GLUT_KEY_DOWN; break;
926 case XK_Prior: special = GLUT_KEY_PAGE_UP; break;
928 case XK_Next: special = GLUT_KEY_PAGE_DOWN; break;
930 case XK_Home: special = GLUT_KEY_HOME; break;
932 case XK_End: special = GLUT_KEY_END; break;
934 case XK_Insert: special = GLUT_KEY_INSERT; break;
938 * Execute the callback (if one has been specified),
939 * given that the special code seems to be valid...
941 if( special_cb && (special != -1) )
943 fgSetWindow( window );
944 fgState.Modifiers = fgGetXModifiers( &event );
945 special_cb( special, event.xkey.x, event.xkey.y );
946 fgState.Modifiers = 0xffffffff;
954 break; /* XXX Should disable this event */
957 fgWarning ("Unknown X event type: %d", event.type);
962 #elif TARGET_HOST_WIN32
966 while( PeekMessage( &stMsg, NULL, 0, 0, PM_NOREMOVE ) )
968 if( GetMessage( &stMsg, NULL, 0, 0 ) == 0 )
970 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
975 fgState.ExecState = GLUT_EXEC_STATE_STOP;
979 TranslateMessage( &stMsg );
980 DispatchMessage( &stMsg );
984 if( fgState.Timers.First )
986 fghCheckJoystickPolls( );
993 * Enters the freeglut processing loop.
994 * Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".
996 void FGAPIENTRY glutMainLoop( void )
998 #if TARGET_HOST_WIN32
999 SFG_Window *window = (SFG_Window *)fgStructure.Windows.First ;
1002 freeglut_assert_ready;
1004 #if TARGET_HOST_WIN32
1006 * Processing before the main loop: If there is a window which is open and
1007 * which has a visibility callback, call it. I know this is an ugly hack,
1008 * but I'm not sure what else to do about it. Ideally we should leave
1009 * something uninitialized in the create window code and initialize it in
1010 * the main loop, and have that initialization create a "WM_ACTIVATE"
1011 * message. Then we would put the visibility callback code in the
1012 * "case WM_ACTIVATE" block below. - John Fay -- 10/24/02
1016 if ( FETCH_WCB( *window, Visibility ) )
1018 SFG_Window *current_window = fgStructure.Window ;
1020 INVOKE_WCB( *window, Visibility, ( window->State.Visible ) );
1021 fgSetWindow( current_window );
1024 window = (SFG_Window *)window->Node.Next ;
1028 fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;
1029 while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )
1031 glutMainLoopEvent( );
1033 if( fgStructure.Windows.First == NULL )
1034 fgState.ExecState = GLUT_EXEC_STATE_STOP;
1037 if( fgState.IdleCallback )
1038 fgState.IdleCallback( );
1045 * When this loop terminates, destroy the display, state and structure
1046 * of a freeglut session, so that another glutInit() call can happen
1052 * Leaves the freeglut processing loop.
1054 void FGAPIENTRY glutLeaveMainLoop( void )
1056 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
1060 #if TARGET_HOST_WIN32
1062 * Determine a GLUT modifer mask based on MS-WINDOWS system info.
1064 int fgGetWin32Modifiers (void)
1067 ( ( ( GetKeyState( VK_LSHIFT ) < 0 ) ||
1068 ( GetKeyState( VK_RSHIFT ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1069 ( ( ( GetKeyState( VK_LCONTROL ) < 0 ) ||
1070 ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL : 0 ) |
1071 ( ( ( GetKeyState( VK_LMENU ) < 0 ) ||
1072 ( GetKeyState( VK_RMENU ) < 0 )) ? GLUT_ACTIVE_ALT : 0 );
1076 * The window procedure for handling Win32 events
1078 LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,
1081 SFG_Window* window = fgWindowByHandle( hWnd );
1085 if ( ( window == NULL ) && ( uMsg != WM_CREATE ) )
1086 return DefWindowProc( hWnd, uMsg, wParam, lParam );
1088 /* printf ( "Window %3d message <%04x> %12d %12d\n", window?window->ID:0,
1089 uMsg, wParam, lParam ); */
1094 * The window structure is passed as the creation structure paramter...
1096 window = (SFG_Window *) (((LPCREATESTRUCT) lParam)->lpCreateParams);
1097 assert( window != NULL );
1099 window->Window.Handle = hWnd;
1100 window->Window.Device = GetDC( hWnd );
1101 if( window->IsMenu )
1103 unsigned int current_DisplayMode = fgState.DisplayMode;
1104 fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
1105 fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
1106 fgState.DisplayMode = current_DisplayMode;
1108 if( fgStructure.MenuContext )
1109 wglMakeCurrent( window->Window.Device,
1110 fgStructure.MenuContext->Context
1114 fgStructure.MenuContext =
1115 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
1116 fgStructure.MenuContext->Context =
1117 wglCreateContext( window->Window.Device );
1120 /* window->Window.Context = wglGetCurrentContext (); */
1121 window->Window.Context = wglCreateContext( window->Window.Device );
1125 fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
1127 if( ! fgState.UseCurrentContext )
1128 window->Window.Context =
1129 wglCreateContext( window->Window.Device );
1132 window->Window.Context = wglGetCurrentContext( );
1133 if( ! window->Window.Context )
1134 window->Window.Context =
1135 wglCreateContext( window->Window.Device );
1139 window->State.NeedToResize = GL_TRUE;
1140 window->State.Width = fgState.Size.X;
1141 window->State.Height = fgState.Size.Y;
1143 ReleaseDC( window->Window.Handle, window->Window.Device );
1148 * We got resized... But check if the window has been already added...
1150 window->State.NeedToResize = GL_TRUE;
1151 window->State.Width = LOWORD(lParam);
1152 window->State.Height = HIWORD(lParam);
1156 printf("WM_SETFOCUS: %p\n", window );
1157 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1161 if (LOWORD(wParam) != WA_INACTIVE)
1163 /* glutSetCursor( fgStructure.Window->State.Cursor ); */
1164 printf("WM_ACTIVATE: glutSetCursor( %p, %d)\n", window,
1165 window->State.Cursor );
1166 glutSetCursor( window->State.Cursor );
1169 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1174 * XXX Why not re-use some common code with the glutSetCursor()
1175 * XXX function (or perhaps invoke glutSetCursor())?
1176 * XXX That is, why are we duplicating code, here, from
1177 * XXX glutSetCursor()? The WIN32 code should be able to just
1178 * XXX call glutSetCurdsor() instead of defining two macros
1179 * XXX and implementing a nested case in-line.
1182 /* Set the cursor AND change it for this window class. */
1183 #define MAP_CURSOR(a,b) \
1185 SetCursor( LoadCursor( NULL, b ) ); \
1188 /* Nuke the cursor AND change it for this window class. */
1189 #define ZAP_CURSOR(a,b) \
1191 SetCursor( NULL ); \
1194 if( LOWORD( lParam ) == HTCLIENT )
1195 switch( window->State.Cursor )
1197 MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, IDC_ARROW );
1198 MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW, IDC_ARROW );
1199 MAP_CURSOR( GLUT_CURSOR_INFO, IDC_HELP );
1200 MAP_CURSOR( GLUT_CURSOR_DESTROY, IDC_CROSS );
1201 MAP_CURSOR( GLUT_CURSOR_HELP, IDC_HELP );
1202 MAP_CURSOR( GLUT_CURSOR_CYCLE, IDC_SIZEALL );
1203 MAP_CURSOR( GLUT_CURSOR_SPRAY, IDC_CROSS );
1204 MAP_CURSOR( GLUT_CURSOR_WAIT, IDC_WAIT );
1205 MAP_CURSOR( GLUT_CURSOR_TEXT, IDC_UPARROW );
1206 MAP_CURSOR( GLUT_CURSOR_CROSSHAIR, IDC_CROSS );
1207 /* MAP_CURSOR( GLUT_CURSOR_NONE, IDC_NO ); */
1208 ZAP_CURSOR( GLUT_CURSOR_NONE, NULL );
1211 MAP_CURSOR( GLUT_CURSOR_UP_DOWN, IDC_ARROW );
1214 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1218 window->State.Visible = GL_TRUE;
1219 window->State.Redisplay = GL_TRUE;
1223 /* Turn on the visibility in case it was turned off somehow */
1224 window->State.Visible = GL_TRUE;
1225 BeginPaint( hWnd, &ps );
1226 fghRedrawWindowByHandle( hWnd );
1227 EndPaint( hWnd, &ps );
1231 fgDestroyWindow ( window );
1232 if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
1238 * The window already got destroyed, so don't bother with it.
1244 window->State.MouseX = LOWORD( lParam );
1245 window->State.MouseY = HIWORD( lParam );
1247 if ( window->ActiveMenu )
1249 window->State.Redisplay = GL_TRUE;
1250 fgSetWindow ( window->ActiveMenu->ParentWindow );
1254 fgState.Modifiers = fgGetWin32Modifiers( );
1256 if( ( wParam & MK_LBUTTON ) ||
1257 ( wParam & MK_MBUTTON ) ||
1258 ( wParam & MK_RBUTTON ) )
1259 INVOKE_WCB( *window, Motion, ( window->State.MouseX,
1260 window->State.MouseY ) );
1262 INVOKE_WCB( *window, Passive, ( window->State.MouseX,
1263 window->State.MouseY ) );
1265 fgState.Modifiers = 0xffffffff;
1269 case WM_LBUTTONDOWN:
1270 case WM_MBUTTONDOWN:
1271 case WM_RBUTTONDOWN:
1276 GLboolean pressed = GL_TRUE;
1279 window->State.MouseX = LOWORD( lParam );
1280 window->State.MouseY = HIWORD( lParam );
1284 case WM_LBUTTONDOWN:
1286 button = GLUT_LEFT_BUTTON;
1288 case WM_MBUTTONDOWN:
1290 button = GLUT_MIDDLE_BUTTON;
1292 case WM_RBUTTONDOWN:
1294 button = GLUT_RIGHT_BUTTON;
1298 button = GLUT_LEFT_BUTTON;
1302 button = GLUT_MIDDLE_BUTTON;
1306 button = GLUT_RIGHT_BUTTON;
1314 if( GetSystemMetrics( SM_SWAPBUTTON ) )
1315 if( button == GLUT_LEFT_BUTTON )
1316 button = GLUT_RIGHT_BUTTON;
1317 else if( button == GLUT_RIGHT_BUTTON )
1318 button = GLUT_LEFT_BUTTON;
1321 return DefWindowProc( hWnd, uMsg, lParam, wParam );
1324 * XXX This comment is duplicated in two other spots.
1325 * XXX Can we centralize it?
1327 * Do not execute the application's mouse callback if a
1328 * menu is hooked to this button.
1329 * In that case an appropriate private call should be generated.
1330 * Near as I can tell, this is the menu behaviour:
1331 * - Down-click the menu button, menu not active: activate
1332 * the menu with its upper left-hand corner at the mouse location.
1333 * - Down-click any button outside the menu, menu active:
1334 * deactivate the menu
1335 * - Down-click any button inside the menu, menu active:
1336 * select the menu entry and deactivate the menu
1337 * - Up-click the menu button, menu not active: nothing happens
1338 * - Up-click the menu button outside the menu, menu active:
1340 * - Up-click the menu button inside the menu, menu active:
1341 * select the menu entry and deactivate the menu
1343 /* Window has an active menu, it absorbs any mouse click */
1344 if( window->ActiveMenu )
1346 /* Outside the menu, deactivate the menu if it's a downclick */
1347 if( ! fgCheckActiveMenu( window, window->ActiveMenu ) )
1350 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
1352 else /* In menu, invoke the callback and deactivate the menu*/
1355 * Save the current window and menu and set the current
1356 * window to the window whose menu this is
1358 SFG_Window *save_window = fgStructure.Window;
1359 SFG_Menu *save_menu = fgStructure.Menu;
1360 SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
1361 fgSetWindow( parent_window );
1362 fgStructure.Menu = window->ActiveMenu;
1364 /* Execute the menu callback */
1365 fgExecuteMenuCallback( window->ActiveMenu );
1366 fgDeactivateMenu( parent_window );
1368 /* Restore the current window and menu */
1369 fgSetWindow( save_window );
1370 fgStructure.Menu = save_menu;
1374 * Let's make the window redraw as a result of the mouse
1375 * click and menu activity.
1377 if( ! window->IsMenu )
1378 window->State.Redisplay = GL_TRUE;
1383 if ( window->Menu[ button ] && pressed )
1385 window->State.Redisplay = GL_TRUE;
1386 fgSetWindow( window );
1387 fgActivateMenu( window, button );
1392 if( ! FETCH_WCB( *window, Mouse ) )
1395 fgSetWindow( window );
1396 fgState.Modifiers = fgGetWin32Modifiers( );
1401 pressed ? GLUT_DOWN : GLUT_UP,
1402 window->State.MouseX,
1403 window->State.MouseY
1407 fgState.Modifiers = 0xffffffff;
1412 /* Should be WM_MOUSEWHEEL but my compiler doesn't recognize it */
1415 * XXX THIS IS SPECULATIVE -- John Fay, 10/2/03
1416 * XXX Should use WHEEL_DELTA instead of 120
1418 int wheel_number = LOWORD( wParam );
1419 short ticks = ( short )HIWORD( wParam ) / 120;
1429 * The mouse cursor has moved. Remember the new mouse cursor's position
1431 /* window->State.MouseX = LOWORD( lParam ); */
1432 /* Need to adjust by window position, */
1433 /* window->State.MouseY = HIWORD( lParam ); */
1434 /* change "lParam" to other parameter */
1436 if( ! FETCH_WCB( *window, MouseWheel ) &&
1437 ! FETCH_WCB( *window, Mouse ) )
1440 fgSetWindow( window );
1441 fgState.Modifiers = fgGetWin32Modifiers( );
1444 if( FETCH_WCB( *window, MouseWheel ) )
1445 INVOKE_WCB( *window, MouseWheel,
1448 window->State.MouseX,
1449 window->State.MouseY
1452 else /* No mouse wheel, call the mouse button callback twice */
1455 * XXX The below assumes that you have no more than 3 mouse
1456 * XXX buttons. Sorry.
1458 int button = wheel_number*2 + 4;
1461 INVOKE_WCB( *window, Mouse,
1462 ( button, GLUT_DOWN,
1463 window->State.MouseX, window->State.MouseY )
1465 INVOKE_WCB( *window, Mouse,
1467 window->State.MouseX, window->State.MouseX )
1471 fgState.Modifiers = 0xffffffff;
1481 if( fgState.IgnoreKeyRepeat && (lParam & KF_REPEAT) )
1485 * Remember the current modifiers state. This is done here in order
1486 * to make sure the VK_DELETE keyboard callback is executed properly.
1488 fgState.Modifiers = fgGetWin32Modifiers( );
1490 GetCursorPos( &mouse_pos );
1491 ScreenToClient( window->Window.Handle, &mouse_pos );
1493 window->State.MouseX = mouse_pos.x;
1494 window->State.MouseY = mouse_pos.y;
1497 * Convert the Win32 keystroke codes to GLUTtish way
1499 # define KEY(a,b) case a: keypress = b; break;
1503 KEY( VK_F1, GLUT_KEY_F1 );
1504 KEY( VK_F2, GLUT_KEY_F2 );
1505 KEY( VK_F3, GLUT_KEY_F3 );
1506 KEY( VK_F4, GLUT_KEY_F4 );
1507 KEY( VK_F5, GLUT_KEY_F5 );
1508 KEY( VK_F6, GLUT_KEY_F6 );
1509 KEY( VK_F7, GLUT_KEY_F7 );
1510 KEY( VK_F8, GLUT_KEY_F8 );
1511 KEY( VK_F9, GLUT_KEY_F9 );
1512 KEY( VK_F10, GLUT_KEY_F10 );
1513 KEY( VK_F11, GLUT_KEY_F11 );
1514 KEY( VK_F12, GLUT_KEY_F12 );
1515 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP );
1516 KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
1517 KEY( VK_HOME, GLUT_KEY_HOME );
1518 KEY( VK_END, GLUT_KEY_END );
1519 KEY( VK_LEFT, GLUT_KEY_LEFT );
1520 KEY( VK_UP, GLUT_KEY_UP );
1521 KEY( VK_RIGHT, GLUT_KEY_RIGHT );
1522 KEY( VK_DOWN, GLUT_KEY_DOWN );
1523 KEY( VK_INSERT, GLUT_KEY_INSERT );
1527 * The delete key should be treated as an ASCII keypress:
1529 INVOKE_WCB( *window, Keyboard,
1530 ( 127, window->State.MouseX, window->State.MouseY )
1534 if( keypress != -1 )
1535 INVOKE_WCB( *window, Special,
1537 window->State.MouseX, window->State.MouseY )
1540 fgState.Modifiers = 0xffffffff;
1551 * Remember the current modifiers state. This is done here in order
1552 * to make sure the VK_DELETE keyboard callback is executed properly.
1554 fgState.Modifiers = fgGetWin32Modifiers( );
1556 GetCursorPos( &mouse_pos );
1557 ScreenToClient( window->Window.Handle, &mouse_pos );
1559 window->State.MouseX = mouse_pos.x;
1560 window->State.MouseY = mouse_pos.y;
1563 * Convert the Win32 keystroke codes to GLUTtish way.
1564 * "KEY(a,b)" was defined under "WM_KEYDOWN"
1569 KEY( VK_F1, GLUT_KEY_F1 );
1570 KEY( VK_F2, GLUT_KEY_F2 );
1571 KEY( VK_F3, GLUT_KEY_F3 );
1572 KEY( VK_F4, GLUT_KEY_F4 );
1573 KEY( VK_F5, GLUT_KEY_F5 );
1574 KEY( VK_F6, GLUT_KEY_F6 );
1575 KEY( VK_F7, GLUT_KEY_F7 );
1576 KEY( VK_F8, GLUT_KEY_F8 );
1577 KEY( VK_F9, GLUT_KEY_F9 );
1578 KEY( VK_F10, GLUT_KEY_F10 );
1579 KEY( VK_F11, GLUT_KEY_F11 );
1580 KEY( VK_F12, GLUT_KEY_F12 );
1581 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP );
1582 KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
1583 KEY( VK_HOME, GLUT_KEY_HOME );
1584 KEY( VK_END, GLUT_KEY_END );
1585 KEY( VK_LEFT, GLUT_KEY_LEFT );
1586 KEY( VK_UP, GLUT_KEY_UP );
1587 KEY( VK_RIGHT, GLUT_KEY_RIGHT );
1588 KEY( VK_DOWN, GLUT_KEY_DOWN );
1589 KEY( VK_INSERT, GLUT_KEY_INSERT );
1593 * The delete key should be treated as an ASCII keypress:
1595 INVOKE_WCB( *window, KeyboardUp,
1596 ( 127, window->State.MouseX, window->State.MouseY )
1605 GetKeyboardState( state );
1607 if( ToAscii( wParam, 0, state, code, 0 ) == 1 )
1610 INVOKE_WCB( *window, KeyboardUp,
1612 window->State.MouseX, window->State.MouseY )
1617 if( keypress != -1 )
1618 INVOKE_WCB( *window, SpecialUp,
1620 window->State.MouseX, window->State.MouseY )
1623 fgState.Modifiers = 0xffffffff;
1630 if( fgState.IgnoreKeyRepeat && (lParam & KF_REPEAT) )
1633 fgState.Modifiers = fgGetWin32Modifiers( );
1634 INVOKE_WCB( *window, Keyboard,
1636 window->State.MouseX, window->State.MouseY )
1638 fgState.Modifiers = 0xffffffff;
1642 case WM_CAPTURECHANGED:
1643 /* User has finished resizing the window, force a redraw */
1644 INVOKE_WCB( *window, Display, ( ) );
1646 /*lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ); */
1650 * Other messages that I have seen and which are not handled already
1652 case WM_SETTEXT: /* 0x000c */
1653 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1654 /* Pass it on to "DefWindowProc" to set the window text */
1657 case WM_GETTEXT: /* 0x000d */
1658 /* Ideally we would copy the title of the window into "lParam" */
1659 /* strncpy ( (char *)lParam, "Window Title", wParam );
1660 lRet = ( wParam > 12 ) ? 12 : wParam; */
1661 /* the number of characters copied */
1662 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1665 case WM_GETTEXTLENGTH: /* 0x000e */
1666 /* Ideally we would get the length of the title of the window */
1668 /* the number of characters in "Window Title\0" (see above) */
1671 case WM_ERASEBKGND: /* 0x0014 */
1672 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1675 case WM_SYNCPAINT: /* 0x0088 */
1676 /* Another window has moved, need to update this one */
1677 window->State.Redisplay = GL_TRUE;
1678 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1679 /* Help screen says this message must be passed to "DefWindowProc" */
1682 case WM_NCPAINT: /* 0x0085 */
1683 /* Need to update the border of this window */
1684 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1685 /* Pass it on to "DefWindowProc" to repaint a standard border */
1688 case WM_SYSCOMMAND : /* 0x0112 */
1691 * We have received a system command message. Try to act on it.
1692 * The commands are passed in through the "lParam" parameter:
1693 * Clicking on a corner to resize the window gives a "F004" message
1694 * but this is not defined in my header file.
1705 /* User has clicked on the "-" to minimize the window */
1706 /* Turn off the visibility */
1707 window->State.Visible = GL_FALSE ;
1714 case SC_NEXTWINDOW :
1717 case SC_PREVWINDOW :
1721 /* Followed very closely by a WM_CLOSE message */
1745 case SC_SCREENSAVE :
1753 /* We need to pass the message on to the operating system as well */
1754 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1759 * Handle unhandled messages
1761 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1769 /*** END OF FILE ***/