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 );
161 window->State.Redisplay = GL_FALSE;
163 freeglut_return_if_fail( window->State.Visible );
165 if( window->State.NeedToResize )
167 SFG_Window *current_window = fgStructure.Window;
169 fgSetWindow( window );
171 fghReshapeWindowByHandle(
172 window->Window.Handle,
177 window->State.NeedToResize = GL_FALSE;
178 fgSetWindow ( current_window );
181 INVOKE_WCB( *window, Display, ( ) );
185 * A static helper function to execute display callback for a window
187 static void fghcbDisplayWindow( SFG_Window *window,
188 SFG_Enumerator *enumerator )
190 if( window->State.Redisplay &&
191 window->State.Visible )
193 if( window->State.NeedToResize )
195 SFG_Window *current_window = fgStructure.Window;
197 fgSetWindow( window );
199 fghReshapeWindowByHandle(
200 window->Window.Handle,
205 window->State.NeedToResize = GL_FALSE;
206 fgSetWindow ( current_window );
209 window->State.Redisplay = GL_FALSE;
211 #if TARGET_HOST_UNIX_X11
213 SFG_Window *current_window = fgStructure.Window;
215 INVOKE_WCB( *window, Display, ( ) );
216 fgSetWindow( current_window );
218 #elif TARGET_HOST_WIN32
220 window->Window.Handle, NULL, NULL,
221 RDW_NOERASE | RDW_INTERNALPAINT | RDW_INVALIDATE | RDW_UPDATENOW
226 fgEnumSubWindows( window, fghcbDisplayWindow, enumerator );
230 * Make all windows perform a display call
232 static void fghDisplayAll( void )
234 SFG_Enumerator enumerator;
236 enumerator.found = GL_FALSE;
237 enumerator.data = NULL;
239 fgEnumWindows( fghcbDisplayWindow, &enumerator );
243 * Window enumerator callback to check for the joystick polling code
245 static void fghcbCheckJoystickPolls( SFG_Window *window,
246 SFG_Enumerator *enumerator )
248 long int checkTime = fgElapsedTime( );
250 if( window->State.JoystickLastPoll + window->State.JoystickPollRate <=
253 fgJoystickPollWindow( window );
254 window->State.JoystickLastPoll = checkTime;
257 fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );
261 * Check all windows for joystick polling
263 static void fghCheckJoystickPolls( void )
265 SFG_Enumerator enumerator;
267 enumerator.found = GL_FALSE;
268 enumerator.data = NULL;
270 fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );
274 * Check the global timers
276 static void fghCheckTimers( void )
278 long checkTime = fgElapsedTime( );
279 SFG_Timer *timer, *next;
282 fgListInit(&timedOut);
284 for( timer = (SFG_Timer *)fgState.Timers.First;
286 timer = (SFG_Timer *)next )
288 next = (SFG_Timer *)timer->Node.Next;
290 if( timer->TriggerTime <= checkTime )
292 fgListRemove( &fgState.Timers, &timer->Node );
293 fgListAppend( &timedOut, &timer->Node );
298 * Now feel free to execute all the hooked and timed out timer callbacks
299 * And delete the timed out timers...
301 while ( (timer = (SFG_Timer *)timedOut.First) )
303 if( timer->Callback != NULL )
304 timer->Callback( timer->ID );
305 fgListRemove( &timedOut, &timer->Node );
314 long fgElapsedTime( void )
316 if ( fgState.Time.Set )
318 #if TARGET_HOST_UNIX_X11
322 gettimeofday( &now, NULL );
324 elapsed = (now.tv_usec - fgState.Time.Value.tv_usec) / 1000;
325 elapsed += (now.tv_sec - fgState.Time.Value.tv_sec) * 1000;
328 #elif TARGET_HOST_WIN32
329 return timeGetTime() - fgState.Time.Value;
334 #if TARGET_HOST_UNIX_X11
335 gettimeofday( &fgState.Time.Value, NULL );
336 #elif TARGET_HOST_WIN32
337 fgState.Time.Value = timeGetTime ();
339 fgState.Time.Set = GL_TRUE ;
348 void fgError( const char *fmt, ... )
354 fprintf( stderr, "freeglut ");
355 if( fgState.ProgramName )
356 fprintf (stderr, "(%s): ", fgState.ProgramName);
357 vfprintf( stderr, fmt, ap );
358 fprintf( stderr, "\n" );
362 if ( fgState.Initialised )
368 void fgWarning( const char *fmt, ... )
374 fprintf( stderr, "freeglut ");
375 if( fgState.ProgramName )
376 fprintf( stderr, "(%s): ", fgState.ProgramName );
377 vfprintf( stderr, fmt, ap );
378 fprintf( stderr, "\n" );
384 * Indicates whether Joystick events are being used by ANY window.
386 * The current mechanism is to walk all of the windows and ask if
387 * there is a joystick callback. Certainly in some cases, maybe
388 * in all cases, the joystick is attached to the system and accessed
389 * from ONE point by GLUT/freeglut, so this is not the right way,
390 * in general, to do this. However, the Joystick code is segregated
391 * in its own little world, so we can't access the information that
392 * we need in order to do that nicely.
395 * * Store Joystick data into freeglut global state.
396 * * Provide NON-static functions or data from Joystick *.c file.
398 * Basically, the RIGHT way to do this requires knowing something
399 * about the Joystick. Right now, the Joystick code is behind
403 static void fgCheckJoystickCallback( SFG_Window* w, SFG_Enumerator* e)
405 if( FETCH_WCB( *w, Joystick ) )
410 fgEnumSubWindows( w, fgCheckJoystickCallback, e );
412 static int fgHaveJoystick( void )
414 SFG_Enumerator enumerator;
415 enumerator.found = GL_FALSE;
416 enumerator.data = NULL;
417 fgEnumWindows( fgCheckJoystickCallback, &enumerator );
418 return !!enumerator.data;
420 static void fgHavePendingRedisplaysCallback( SFG_Window* w, SFG_Enumerator* e)
422 if( w->State.Redisplay )
427 fgEnumSubWindows( w, fgHavePendingRedisplaysCallback, e );
429 static int fgHavePendingRedisplays (void)
431 SFG_Enumerator enumerator;
432 enumerator.found = GL_FALSE;
433 enumerator.data = NULL;
434 fgEnumWindows( fgHavePendingRedisplaysCallback, &enumerator );
435 return !!enumerator.data;
438 * Indicates whether there are any outstanding timers.
441 static int fgHaveTimers( void )
443 return !!fgState.Timers.First;
447 * Returns the number of GLUT ticks (milliseconds) till the next timer event.
449 static long fgNextTimer( void )
451 long now = fgElapsedTime();
455 for( timer = (SFG_Timer *)fgState.Timers.First;
457 timer = (SFG_Timer *)timer->Node.Next )
458 ret = MIN( ret, MAX( 0, timer->TriggerTime - now ) );
463 * Does the magic required to relinquish the CPU until something interesting
466 static void fgSleepForEvents( void )
468 #if TARGET_HOST_UNIX_X11
475 if( fgState.IdleCallback || fgHavePendingRedisplays( ) )
477 socket = ConnectionNumber( fgDisplay.Display );
479 FD_SET( socket, &fdset );
481 msec = fgNextTimer( );
482 if( fgHaveJoystick( ) )
483 msec = MIN( msec, 10 );
485 wait.tv_sec = msec / 1000;
486 wait.tv_usec = (msec % 1000) * 1000;
487 err = select( socket+1, &fdset, NULL, NULL, &wait );
490 fgWarning ( "freeglut select() error: %d\n", errno );
492 #elif TARGET_HOST_WIN32
496 #if TARGET_HOST_UNIX_X11
498 * Returns GLUT modifier mask for an XEvent.
500 int fgGetXModifiers( XEvent *event )
504 if( event->xkey.state & ( ShiftMask | LockMask ) )
505 ret |= GLUT_ACTIVE_SHIFT;
506 if( event->xkey.state & ControlMask )
507 ret |= GLUT_ACTIVE_CTRL;
508 if( event->xkey.state & Mod1Mask )
509 ret |= GLUT_ACTIVE_ALT;
516 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
519 * Executes a single iteration in the freeglut processing loop.
521 void FGAPIENTRY glutMainLoopEvent( void )
523 #if TARGET_HOST_UNIX_X11
528 * This code was repeated constantly, so here it goes into a definition:
530 #define GETWINDOW(a) \
531 window = fgWindowByHandle( event.a.window ); \
532 if( window == NULL ) \
535 #define GETMOUSE(a) \
536 window->State.MouseX = event.a.x; \
537 window->State.MouseY = event.a.y;
539 freeglut_assert_ready;
541 while( XPending( fgDisplay.Display ) )
543 XNextEvent( fgDisplay.Display, &event );
549 * Destroy the window when the WM_DELETE_WINDOW message arrives
551 if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.DeleteWindow )
553 GETWINDOW( xclient );
555 fgCloseWindow ( window );
556 fgAddToWindowDestroyList ( window, GL_FALSE );
561 * CreateNotify causes a configure-event so that sub-windows are
562 * handled compatibly with GLUT. Otherwise, your sub-windows
563 * (in freeglut only) will not get an initial reshape event,
564 * which can break things.
566 * XXX NOTE that it is possible that you will more than one Reshape
567 * XXX event for your top-level window, but something like this
568 * XXX appears to be required for compatbility.
570 * GLUT presumably does this because it generally tries to treat
571 * sub-windows the same as windows.
574 case ConfigureNotify:
575 GETWINDOW( xconfigure );
576 window->State.NeedToResize = GL_TRUE ;
577 window->State.Width = event.xconfigure.width ;
578 window->State.Height = event.xconfigure.height;
583 * This is sent to confirm the XDestroyWindow call.
584 * XXX WHY is this commented out? Should we re-enable it?
586 /* fgAddToWindowDestroyList ( window, GL_FALSE ); */
591 * We are too dumb to process partial exposes...
592 * XXX Well, we could do it. However, it seems to only
593 * XXX be potentially useful for single-buffered (since
594 * XXX double-buffered does not respect viewport when we
595 * XXX do a buffer-swap).
597 if( event.xexpose.count == 0 )
598 fghRedrawWindowByHandle( event.xexpose.window );
604 * If we never do anything with this, can we just not ask to
605 * get these messages?
611 * Have the client's keyboard knowledge updated (xlib.ps,
612 * page 206, says that's a good thing to do)
614 XRefreshKeyboardMapping( (XMappingEvent *) &event );
617 case VisibilityNotify:
619 GETWINDOW( xvisibility );
620 if( ! FETCH_WCB( *window, WindowStatus ) )
622 fgSetWindow( window );
625 * Sending this event, the X server can notify us that the window
626 * has just acquired one of the three possible visibility states:
627 * VisibilityUnobscured, VisibilityPartiallyObscured or
628 * VisibilityFullyObscured
630 switch( event.xvisibility.state )
632 case VisibilityUnobscured:
633 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_RETAINED ) );
634 window->State.Visible = GL_TRUE;
637 case VisibilityPartiallyObscured:
638 INVOKE_WCB( *window, WindowStatus,
639 ( GLUT_PARTIALLY_RETAINED ) );
640 window->State.Visible = GL_TRUE;
643 case VisibilityFullyObscured:
644 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_COVERED ) );
645 window->State.Visible = GL_FALSE;
649 fgWarning( "Uknown X visibility state: %d",
650 event.xvisibility.state );
658 GETWINDOW( xcrossing );
659 GETMOUSE( xcrossing );
660 INVOKE_WCB( *window, Entry, ( ( EnterNotify == event.type ) ?
667 GETWINDOW( xmotion );
670 if( window->ActiveMenu )
672 if( window == window->ActiveMenu->ParentWindow )
674 window->ActiveMenu->Window->State.MouseX =
675 event.xmotion.x_root - window->ActiveMenu->X;
676 window->ActiveMenu->Window->State.MouseY =
677 event.xmotion.y_root - window->ActiveMenu->Y;
679 window->ActiveMenu->Window->State.Redisplay = GL_TRUE ;
680 fgSetWindow( window->ActiveMenu->ParentWindow );
686 * XXX For more than 5 buttons, just check {event.xmotion.state},
687 * XXX rather than a host of bit-masks?
689 #define BUTTON_MASK \
690 ( Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask )
691 if ( event.xmotion.state & BUTTON_MASK )
692 INVOKE_WCB( *window, Motion, ( event.xmotion.x,
695 INVOKE_WCB( *window, Passive, ( event.xmotion.x,
703 GLboolean pressed = GL_TRUE;
706 if( event.type == ButtonRelease )
710 * A mouse button has been pressed or released. Traditionally,
711 * break if the window was found within the freeglut structures.
713 GETWINDOW( xbutton );
717 * An X button (at least in XFree86) is numbered from 1.
718 * A GLUT button is numbered from 0.
719 * Old GLUT passed through buttons other than just the first
720 * three, though it only gave symbolic names and official
721 * support to the first three.
723 button = event.xbutton.button - 1;
726 * XXX This comment is replicated in the WIN32 section and
727 * XXX maybe also in the menu code. Can we move the info
728 * XXX to one central place and *reference* it from here?
730 * Do not execute the application's mouse callback if a menu
731 * is hooked to this button. In that case an appropriate
732 * private call should be generated.
733 * Near as I can tell, this is the menu behaviour:
734 * - Down-click the menu button, menu not active: activate
735 * the menu with its upper left-hand corner at the mouse
737 * - Down-click any button outside the menu, menu active:
738 * deactivate the menu
739 * - Down-click any button inside the menu, menu active:
740 * select the menu entry and deactivate the menu
741 * - Up-click the menu button, menu not active: nothing happens
742 * - Up-click the menu button outside the menu, menu active:
744 * - Up-click the menu button inside the menu, menu active:
745 * select the menu entry and deactivate the menu
747 /* Window has an active menu, it absorbs any mouse click */
748 if( window->ActiveMenu )
750 if( window == window->ActiveMenu->ParentWindow )
752 window->ActiveMenu->Window->State.MouseX =
753 event.xbutton.x_root - window->ActiveMenu->X;
754 window->ActiveMenu->Window->State.MouseY =
755 event.xbutton.y_root - window->ActiveMenu->Y;
758 /* In the menu, invoke the callback and deactivate the menu*/
759 if( fgCheckActiveMenu( window->ActiveMenu->Window,
760 window->ActiveMenu ) )
763 * Save the current window and menu and set the current
764 * window to the window whose menu this is
766 SFG_Window *save_window = fgStructure.Window;
767 SFG_Menu *save_menu = fgStructure.Menu;
768 SFG_Window *parent_window =
769 window->ActiveMenu->ParentWindow;
770 fgSetWindow( parent_window );
771 fgStructure.Menu = window->ActiveMenu;
773 /* Execute the menu callback */
774 fgExecuteMenuCallback( window->ActiveMenu );
775 fgDeactivateMenu( parent_window );
777 /* Restore the current window and menu */
778 fgSetWindow( save_window );
779 fgStructure.Menu = save_menu;
783 * Outside the menu, deactivate if it's a downclick
784 * XXX This isn't enough. A downclick outside of
785 * XXX the interior of our freeglut windows should also
786 * XXX deactivate the menu. This is more complicated.
788 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
790 window->State.Redisplay = GL_TRUE;
795 * No active menu, let's check whether we need to activate one.
797 if( ( 0 <= button ) &&
798 ( FREEGLUT_MAX_MENUS > button ) &&
799 ( window->Menu[ button ] ) &&
802 window->State.Redisplay = GL_TRUE;
803 fgSetWindow( window );
804 fgActivateMenu( window, button );
809 * Check if there is a mouse or mouse wheel callback hooked to the
812 if( ! FETCH_WCB( *window, Mouse ) &&
813 ! FETCH_WCB( *window, MouseWheel ) )
816 fgState.Modifiers = fgGetXModifiers( &event );
819 * Finally execute the mouse or mouse wheel callback
821 * XXX Use a symbolic constant, *not* "4"!
823 if( ( button < 3 ) || ( ! FETCH_WCB( *window, MouseWheel ) ) )
824 INVOKE_WCB( *window, Mouse, ( button,
825 pressed ? GLUT_DOWN : GLUT_UP,
832 * Map 4 and 5 to wheel zero; EVEN to +1, ODD to -1
833 * " 6 and 7 " " one; ...
835 * XXX This *should* be behind some variables/macros,
836 * XXX since the order and numbering isn't certain
837 * XXX See XFree86 configuration docs (even back in the
838 * XXX 3.x days, and especially with 4.x).
840 * XXX Note that {button} has already been decremeted
841 * XXX in mapping from X button numbering to GLUT.
843 int wheel_number = (button - 3) / 2;
849 INVOKE_WCB( *window, MouseWheel, ( wheel_number,
857 * Trash the modifiers state
859 fgState.Modifiers = 0xffffffff;
866 FGCBKeyboard keyboard_cb;
867 FGCBSpecial special_cb;
872 if( event.type == KeyPress )
874 keyboard_cb = FETCH_WCB( *window, Keyboard );
875 special_cb = FETCH_WCB( *window, Special );
879 keyboard_cb = FETCH_WCB( *window, KeyboardUp );
880 special_cb = FETCH_WCB( *window, SpecialUp );
884 * Is there a keyboard/special callback hooked for this window?
886 if( keyboard_cb || special_cb )
888 XComposeStatus composeStatus;
889 char asciiCode[ 32 ];
894 * Check for the ASCII/KeySym codes associated with the event:
896 len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode),
897 &keySym, &composeStatus
901 * GLUT API tells us to have two separate callbacks...
906 * ...one for the ASCII translateable keypresses...
910 fgSetWindow( window );
911 fgState.Modifiers = fgGetXModifiers( &event );
912 keyboard_cb( asciiCode[ 0 ],
913 event.xkey.x, event.xkey.y
915 fgState.Modifiers = 0xffffffff;
923 * ...and one for all the others, which need to be
924 * translated to GLUT_KEY_Xs...
928 case XK_F1: special = GLUT_KEY_F1; break;
929 case XK_F2: special = GLUT_KEY_F2; break;
930 case XK_F3: special = GLUT_KEY_F3; break;
931 case XK_F4: special = GLUT_KEY_F4; break;
932 case XK_F5: special = GLUT_KEY_F5; break;
933 case XK_F6: special = GLUT_KEY_F6; break;
934 case XK_F7: special = GLUT_KEY_F7; break;
935 case XK_F8: special = GLUT_KEY_F8; break;
936 case XK_F9: special = GLUT_KEY_F9; break;
937 case XK_F10: special = GLUT_KEY_F10; break;
938 case XK_F11: special = GLUT_KEY_F11; break;
939 case XK_F12: special = GLUT_KEY_F12; break;
941 case XK_Left: special = GLUT_KEY_LEFT; break;
942 case XK_Right: special = GLUT_KEY_RIGHT; break;
943 case XK_Up: special = GLUT_KEY_UP; break;
944 case XK_Down: special = GLUT_KEY_DOWN; break;
947 case XK_Prior: special = GLUT_KEY_PAGE_UP; break;
949 case XK_Next: special = GLUT_KEY_PAGE_DOWN; break;
951 case XK_Home: special = GLUT_KEY_HOME; break;
953 case XK_End: special = GLUT_KEY_END; break;
955 case XK_Insert: special = GLUT_KEY_INSERT; break;
959 * Execute the callback (if one has been specified),
960 * given that the special code seems to be valid...
962 if( special_cb && (special != -1) )
964 fgSetWindow( window );
965 fgState.Modifiers = fgGetXModifiers( &event );
966 special_cb( special, event.xkey.x, event.xkey.y );
967 fgState.Modifiers = 0xffffffff;
975 break; /* XXX Should disable this event */
978 fgWarning ("Unknown X event type: %d", event.type);
983 #elif TARGET_HOST_WIN32
987 while( PeekMessage( &stMsg, NULL, 0, 0, PM_NOREMOVE ) )
989 if( GetMessage( &stMsg, NULL, 0, 0 ) == 0 )
990 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
992 TranslateMessage( &stMsg );
993 DispatchMessage( &stMsg );
998 fghCheckJoystickPolls( );
1005 * Enters the freeglut processing loop.
1006 * Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".
1008 void FGAPIENTRY glutMainLoop( void )
1010 #if TARGET_HOST_WIN32
1011 SFG_Window *window = (SFG_Window *)fgStructure.Windows.First ;
1014 freeglut_assert_ready;
1016 #if TARGET_HOST_WIN32
1018 * Processing before the main loop: If there is a window which is open and
1019 * which has a visibility callback, call it. I know this is an ugly hack,
1020 * but I'm not sure what else to do about it. Ideally we should leave
1021 * something uninitialized in the create window code and initialize it in
1022 * the main loop, and have that initialization create a "WM_ACTIVATE"
1023 * message. Then we would put the visibility callback code in the
1024 * "case WM_ACTIVATE" block below. - John Fay -- 10/24/02
1028 if ( FETCH_WCB( *window, Visibility ) )
1030 SFG_Window *current_window = fgStructure.Window ;
1032 INVOKE_WCB( *window, Visibility, ( window->State.Visible ) );
1033 fgSetWindow( current_window );
1036 window = (SFG_Window *)window->Node.Next ;
1040 fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;
1041 while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )
1043 glutMainLoopEvent( );
1045 if( fgStructure.Windows.First == NULL )
1046 fgState.ExecState = GLUT_EXEC_STATE_STOP;
1049 if( fgState.IdleCallback )
1050 fgState.IdleCallback( );
1057 fgExecutionState execState = fgState.ExecState;
1060 * When this loop terminates, destroy the display, state and structure
1061 * of a freeglut session, so that another glutInit() call can happen
1065 if( execState == GLUT_ACTION_EXIT )
1071 * Leaves the freeglut processing loop.
1073 void FGAPIENTRY glutLeaveMainLoop( void )
1075 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
1079 #if TARGET_HOST_WIN32
1081 * Determine a GLUT modifer mask based on MS-WINDOWS system info.
1083 int fgGetWin32Modifiers (void)
1086 ( ( ( GetKeyState( VK_LSHIFT ) < 0 ) ||
1087 ( GetKeyState( VK_RSHIFT ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1088 ( ( ( GetKeyState( VK_LCONTROL ) < 0 ) ||
1089 ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL : 0 ) |
1090 ( ( ( GetKeyState( VK_LMENU ) < 0 ) ||
1091 ( GetKeyState( VK_RMENU ) < 0 )) ? GLUT_ACTIVE_ALT : 0 );
1095 * The window procedure for handling Win32 events
1097 LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,
1100 SFG_Window* window = fgWindowByHandle( hWnd );
1104 if ( ( window == NULL ) && ( uMsg != WM_CREATE ) )
1105 return DefWindowProc( hWnd, uMsg, wParam, lParam );
1107 /* printf ( "Window %3d message <%04x> %12d %12d\n", window?window->ID:0,
1108 uMsg, wParam, lParam ); */
1113 * The window structure is passed as the creation structure paramter...
1115 window = (SFG_Window *) (((LPCREATESTRUCT) lParam)->lpCreateParams);
1116 assert( window != NULL );
1118 window->Window.Handle = hWnd;
1119 window->Window.Device = GetDC( hWnd );
1120 if( window->IsMenu )
1122 unsigned int current_DisplayMode = fgState.DisplayMode;
1123 fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
1124 fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
1125 fgState.DisplayMode = current_DisplayMode;
1127 if( fgStructure.MenuContext )
1128 wglMakeCurrent( window->Window.Device,
1129 fgStructure.MenuContext->Context
1133 fgStructure.MenuContext =
1134 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
1135 fgStructure.MenuContext->Context =
1136 wglCreateContext( window->Window.Device );
1139 /* window->Window.Context = wglGetCurrentContext (); */
1140 window->Window.Context = wglCreateContext( window->Window.Device );
1144 fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
1146 if( ! fgState.UseCurrentContext )
1147 window->Window.Context =
1148 wglCreateContext( window->Window.Device );
1151 window->Window.Context = wglGetCurrentContext( );
1152 if( ! window->Window.Context )
1153 window->Window.Context =
1154 wglCreateContext( window->Window.Device );
1158 window->State.NeedToResize = GL_TRUE;
1159 window->State.Width = fgState.Size.X;
1160 window->State.Height = fgState.Size.Y;
1162 ReleaseDC( window->Window.Handle, window->Window.Device );
1167 * We got resized... But check if the window has been already added...
1169 window->State.NeedToResize = GL_TRUE;
1170 window->State.Width = LOWORD(lParam);
1171 window->State.Height = HIWORD(lParam);
1175 printf("WM_SETFOCUS: %p\n", window );
1176 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1180 if (LOWORD(wParam) != WA_INACTIVE)
1182 /* glutSetCursor( fgStructure.Window->State.Cursor ); */
1183 printf("WM_ACTIVATE: glutSetCursor( %p, %d)\n", window,
1184 window->State.Cursor );
1185 glutSetCursor( window->State.Cursor );
1188 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1193 * XXX Why not re-use some common code with the glutSetCursor()
1194 * XXX function (or perhaps invoke glutSetCursor())?
1195 * XXX That is, why are we duplicating code, here, from
1196 * XXX glutSetCursor()? The WIN32 code should be able to just
1197 * XXX call glutSetCurdsor() instead of defining two macros
1198 * XXX and implementing a nested case in-line.
1201 /* Set the cursor AND change it for this window class. */
1202 #define MAP_CURSOR(a,b) \
1204 SetCursor( LoadCursor( NULL, b ) ); \
1207 /* Nuke the cursor AND change it for this window class. */
1208 #define ZAP_CURSOR(a,b) \
1210 SetCursor( NULL ); \
1213 if( LOWORD( lParam ) == HTCLIENT )
1214 switch( window->State.Cursor )
1216 MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, IDC_ARROW );
1217 MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW, IDC_ARROW );
1218 MAP_CURSOR( GLUT_CURSOR_INFO, IDC_HELP );
1219 MAP_CURSOR( GLUT_CURSOR_DESTROY, IDC_CROSS );
1220 MAP_CURSOR( GLUT_CURSOR_HELP, IDC_HELP );
1221 MAP_CURSOR( GLUT_CURSOR_CYCLE, IDC_SIZEALL );
1222 MAP_CURSOR( GLUT_CURSOR_SPRAY, IDC_CROSS );
1223 MAP_CURSOR( GLUT_CURSOR_WAIT, IDC_WAIT );
1224 MAP_CURSOR( GLUT_CURSOR_TEXT, IDC_UPARROW );
1225 MAP_CURSOR( GLUT_CURSOR_CROSSHAIR, IDC_CROSS );
1226 /* MAP_CURSOR( GLUT_CURSOR_NONE, IDC_NO ); */
1227 ZAP_CURSOR( GLUT_CURSOR_NONE, NULL );
1230 MAP_CURSOR( GLUT_CURSOR_UP_DOWN, IDC_ARROW );
1233 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1237 window->State.Visible = GL_TRUE;
1238 window->State.Redisplay = GL_TRUE;
1242 /* Turn on the visibility in case it was turned off somehow */
1243 window->State.Visible = GL_TRUE;
1244 BeginPaint( hWnd, &ps );
1245 fghRedrawWindowByHandle( hWnd );
1246 EndPaint( hWnd, &ps );
1251 * Make sure we don't close a window with current context active
1253 if( fgStructure.Window == window )
1258 wglMakeCurrent( NULL, NULL );
1260 * Step through the list of windows. If the rendering context
1261 * is not being used by another window, then we delete it.
1263 for( iter = (SFG_Window *)fgStructure.Windows.First;
1265 iter = (SFG_Window *)iter->Node.Next )
1267 if( ( iter->Window.Context == window->Window.Context ) &&
1268 ( iter != window ) )
1273 wglDeleteContext( window->Window.Context );
1277 * Put on a linked list of windows to be removed after all the
1278 * callbacks have returned
1280 fgAddToWindowDestroyList( window, GL_FALSE );
1281 DestroyWindow( hWnd );
1286 * The window already got destroyed, so don't bother with it.
1292 window->State.MouseX = LOWORD( lParam );
1293 window->State.MouseY = HIWORD( lParam );
1295 if ( window->ActiveMenu )
1297 window->State.Redisplay = GL_TRUE;
1298 fgSetWindow ( window->ActiveMenu->ParentWindow );
1302 fgState.Modifiers = fgGetWin32Modifiers( );
1304 if( ( wParam & MK_LBUTTON ) ||
1305 ( wParam & MK_MBUTTON ) ||
1306 ( wParam & MK_RBUTTON ) )
1307 INVOKE_WCB( *window, Motion, ( window->State.MouseX,
1308 window->State.MouseY ) );
1310 INVOKE_WCB( *window, Passive, ( window->State.MouseX,
1311 window->State.MouseY ) );
1313 fgState.Modifiers = 0xffffffff;
1317 case WM_LBUTTONDOWN:
1318 case WM_MBUTTONDOWN:
1319 case WM_RBUTTONDOWN:
1324 GLboolean pressed = GL_TRUE;
1327 window->State.MouseX = LOWORD( lParam );
1328 window->State.MouseY = HIWORD( lParam );
1332 case WM_LBUTTONDOWN:
1334 button = GLUT_LEFT_BUTTON;
1336 case WM_MBUTTONDOWN:
1338 button = GLUT_MIDDLE_BUTTON;
1340 case WM_RBUTTONDOWN:
1342 button = GLUT_RIGHT_BUTTON;
1346 button = GLUT_LEFT_BUTTON;
1350 button = GLUT_MIDDLE_BUTTON;
1354 button = GLUT_RIGHT_BUTTON;
1362 if( GetSystemMetrics( SM_SWAPBUTTON ) )
1363 if( button == GLUT_LEFT_BUTTON )
1364 button = GLUT_RIGHT_BUTTON;
1365 else if( button == GLUT_RIGHT_BUTTON )
1366 button = GLUT_LEFT_BUTTON;
1369 return DefWindowProc( hWnd, uMsg, lParam, wParam );
1372 * XXX This comment is duplicated in two other spots.
1373 * XXX Can we centralize it?
1375 * Do not execute the application's mouse callback if a
1376 * menu is hooked to this button.
1377 * In that case an appropriate private call should be generated.
1378 * Near as I can tell, this is the menu behaviour:
1379 * - Down-click the menu button, menu not active: activate
1380 * the menu with its upper left-hand corner at the mouse location.
1381 * - Down-click any button outside the menu, menu active:
1382 * deactivate the menu
1383 * - Down-click any button inside the menu, menu active:
1384 * select the menu entry and deactivate the menu
1385 * - Up-click the menu button, menu not active: nothing happens
1386 * - Up-click the menu button outside the menu, menu active:
1388 * - Up-click the menu button inside the menu, menu active:
1389 * select the menu entry and deactivate the menu
1391 /* Window has an active menu, it absorbs any mouse click */
1392 if( window->ActiveMenu )
1394 /* Outside the menu, deactivate the menu if it's a downclick */
1395 if( ! fgCheckActiveMenu( window, window->ActiveMenu ) )
1398 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
1400 else /* In menu, invoke the callback and deactivate the menu*/
1403 * Save the current window and menu and set the current
1404 * window to the window whose menu this is
1406 SFG_Window *save_window = fgStructure.Window;
1407 SFG_Menu *save_menu = fgStructure.Menu;
1408 SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
1409 fgSetWindow( parent_window );
1410 fgStructure.Menu = window->ActiveMenu;
1412 /* Execute the menu callback */
1413 fgExecuteMenuCallback( window->ActiveMenu );
1414 fgDeactivateMenu( parent_window );
1416 /* Restore the current window and menu */
1417 fgSetWindow( save_window );
1418 fgStructure.Menu = save_menu;
1422 * Let's make the window redraw as a result of the mouse
1423 * click and menu activity.
1425 if( ! window->IsMenu )
1426 window->State.Redisplay = GL_TRUE;
1431 if ( window->Menu[ button ] && pressed )
1433 window->State.Redisplay = GL_TRUE;
1434 fgSetWindow( window );
1435 fgActivateMenu( window, button );
1440 if( ! FETCH_WCB( *window, Mouse ) )
1443 fgSetWindow( window );
1444 fgState.Modifiers = fgGetWin32Modifiers( );
1449 pressed ? GLUT_DOWN : GLUT_UP,
1450 window->State.MouseX,
1451 window->State.MouseY
1455 fgState.Modifiers = 0xffffffff;
1460 /* Should be WM_MOUSEWHEEL but my compiler doesn't recognize it */
1463 * XXX THIS IS SPECULATIVE -- John Fay, 10/2/03
1464 * XXX Should use WHEEL_DELTA instead of 120
1466 int wheel_number = LOWORD( wParam );
1467 short ticks = ( short )HIWORD( wParam ) / 120;
1477 * The mouse cursor has moved. Remember the new mouse cursor's position
1479 /* window->State.MouseX = LOWORD( lParam ); */
1480 /* Need to adjust by window position, */
1481 /* window->State.MouseY = HIWORD( lParam ); */
1482 /* change "lParam" to other parameter */
1484 if( ! FETCH_WCB( *window, MouseWheel ) &&
1485 ! FETCH_WCB( *window, Mouse ) )
1488 fgSetWindow( window );
1489 fgState.Modifiers = fgGetWin32Modifiers( );
1492 if( FETCH_WCB( *window, MouseWheel ) )
1493 INVOKE_WCB( *window, MouseWheel,
1496 window->State.MouseX,
1497 window->State.MouseY
1500 else /* No mouse wheel, call the mouse button callback twice */
1503 * XXX The below assumes that you have no more than 3 mouse
1504 * XXX buttons. Sorry.
1506 int button = wheel_number*2 + 4;
1509 INVOKE_WCB( *window, Mouse,
1510 ( button, GLUT_DOWN,
1511 window->State.MouseX, window->State.MouseY )
1513 INVOKE_WCB( *window, Mouse,
1515 window->State.MouseX, window->State.MouseX )
1519 fgState.Modifiers = 0xffffffff;
1529 if( fgState.IgnoreKeyRepeat && (lParam & KF_REPEAT) )
1533 * Remember the current modifiers state. This is done here in order
1534 * to make sure the VK_DELETE keyboard callback is executed properly.
1536 fgState.Modifiers = fgGetWin32Modifiers( );
1538 GetCursorPos( &mouse_pos );
1539 ScreenToClient( window->Window.Handle, &mouse_pos );
1541 window->State.MouseX = mouse_pos.x;
1542 window->State.MouseY = mouse_pos.y;
1545 * Convert the Win32 keystroke codes to GLUTtish way
1547 # define KEY(a,b) case a: keypress = b; break;
1551 KEY( VK_F1, GLUT_KEY_F1 );
1552 KEY( VK_F2, GLUT_KEY_F2 );
1553 KEY( VK_F3, GLUT_KEY_F3 );
1554 KEY( VK_F4, GLUT_KEY_F4 );
1555 KEY( VK_F5, GLUT_KEY_F5 );
1556 KEY( VK_F6, GLUT_KEY_F6 );
1557 KEY( VK_F7, GLUT_KEY_F7 );
1558 KEY( VK_F8, GLUT_KEY_F8 );
1559 KEY( VK_F9, GLUT_KEY_F9 );
1560 KEY( VK_F10, GLUT_KEY_F10 );
1561 KEY( VK_F11, GLUT_KEY_F11 );
1562 KEY( VK_F12, GLUT_KEY_F12 );
1563 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP );
1564 KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
1565 KEY( VK_HOME, GLUT_KEY_HOME );
1566 KEY( VK_END, GLUT_KEY_END );
1567 KEY( VK_LEFT, GLUT_KEY_LEFT );
1568 KEY( VK_UP, GLUT_KEY_UP );
1569 KEY( VK_RIGHT, GLUT_KEY_RIGHT );
1570 KEY( VK_DOWN, GLUT_KEY_DOWN );
1571 KEY( VK_INSERT, GLUT_KEY_INSERT );
1575 * The delete key should be treated as an ASCII keypress:
1577 INVOKE_WCB( *window, Keyboard,
1578 ( 127, window->State.MouseX, window->State.MouseY )
1582 if( keypress != -1 )
1583 INVOKE_WCB( *window, Special,
1585 window->State.MouseX, window->State.MouseY )
1588 fgState.Modifiers = 0xffffffff;
1599 * Remember the current modifiers state. This is done here in order
1600 * to make sure the VK_DELETE keyboard callback is executed properly.
1602 fgState.Modifiers = fgGetWin32Modifiers( );
1604 GetCursorPos( &mouse_pos );
1605 ScreenToClient( window->Window.Handle, &mouse_pos );
1607 window->State.MouseX = mouse_pos.x;
1608 window->State.MouseY = mouse_pos.y;
1611 * Convert the Win32 keystroke codes to GLUTtish way.
1612 * "KEY(a,b)" was defined under "WM_KEYDOWN"
1617 KEY( VK_F1, GLUT_KEY_F1 );
1618 KEY( VK_F2, GLUT_KEY_F2 );
1619 KEY( VK_F3, GLUT_KEY_F3 );
1620 KEY( VK_F4, GLUT_KEY_F4 );
1621 KEY( VK_F5, GLUT_KEY_F5 );
1622 KEY( VK_F6, GLUT_KEY_F6 );
1623 KEY( VK_F7, GLUT_KEY_F7 );
1624 KEY( VK_F8, GLUT_KEY_F8 );
1625 KEY( VK_F9, GLUT_KEY_F9 );
1626 KEY( VK_F10, GLUT_KEY_F10 );
1627 KEY( VK_F11, GLUT_KEY_F11 );
1628 KEY( VK_F12, GLUT_KEY_F12 );
1629 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP );
1630 KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
1631 KEY( VK_HOME, GLUT_KEY_HOME );
1632 KEY( VK_END, GLUT_KEY_END );
1633 KEY( VK_LEFT, GLUT_KEY_LEFT );
1634 KEY( VK_UP, GLUT_KEY_UP );
1635 KEY( VK_RIGHT, GLUT_KEY_RIGHT );
1636 KEY( VK_DOWN, GLUT_KEY_DOWN );
1637 KEY( VK_INSERT, GLUT_KEY_INSERT );
1641 * The delete key should be treated as an ASCII keypress:
1643 INVOKE_WCB( *window, KeyboardUp,
1644 ( 127, window->State.MouseX, window->State.MouseY )
1653 GetKeyboardState( state );
1655 if( ToAscii( wParam, 0, state, code, 0 ) == 1 )
1658 INVOKE_WCB( *window, KeyboardUp,
1660 window->State.MouseX, window->State.MouseY )
1665 if( keypress != -1 )
1666 INVOKE_WCB( *window, SpecialUp,
1668 window->State.MouseX, window->State.MouseY )
1671 fgState.Modifiers = 0xffffffff;
1678 if( fgState.IgnoreKeyRepeat && (lParam & KF_REPEAT) )
1681 fgState.Modifiers = fgGetWin32Modifiers( );
1682 INVOKE_WCB( *window, Keyboard,
1684 window->State.MouseX, window->State.MouseY )
1686 fgState.Modifiers = 0xffffffff;
1690 case WM_CAPTURECHANGED:
1691 /* User has finished resizing the window, force a redraw */
1692 INVOKE_WCB( *window, Display, ( ) );
1694 /*lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ); */
1698 * Other messages that I have seen and which are not handled already
1700 case WM_SETTEXT: /* 0x000c */
1701 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1702 /* Pass it on to "DefWindowProc" to set the window text */
1705 case WM_GETTEXT: /* 0x000d */
1706 /* Ideally we would copy the title of the window into "lParam" */
1707 /* strncpy ( (char *)lParam, "Window Title", wParam );
1708 lRet = ( wParam > 12 ) ? 12 : wParam; */
1709 /* the number of characters copied */
1710 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1713 case WM_GETTEXTLENGTH: /* 0x000e */
1714 /* Ideally we would get the length of the title of the window */
1716 /* the number of characters in "Window Title\0" (see above) */
1719 case WM_ERASEBKGND: /* 0x0014 */
1720 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1723 case WM_SYNCPAINT: /* 0x0088 */
1724 /* Another window has moved, need to update this one */
1725 window->State.Redisplay = GL_TRUE;
1726 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1727 /* Help screen says this message must be passed to "DefWindowProc" */
1730 case WM_NCPAINT: /* 0x0085 */
1731 /* Need to update the border of this window */
1732 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1733 /* Pass it on to "DefWindowProc" to repaint a standard border */
1736 case WM_SYSCOMMAND : /* 0x0112 */
1739 * We have received a system command message. Try to act on it.
1740 * The commands are passed in through the "lParam" parameter:
1741 * Clicking on a corner to resize the window gives a "F004" message
1742 * but this is not defined in my header file.
1753 /* User has clicked on the "-" to minimize the window */
1754 /* Turn off the visibility */
1755 window->State.Visible = GL_FALSE ;
1762 case SC_NEXTWINDOW :
1765 case SC_PREVWINDOW :
1769 /* Followed very closely by a WM_CLOSE message */
1793 case SC_SCREENSAVE :
1801 /* We need to pass the message on to the operating system as well */
1802 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1807 * Handle unhandled messages
1809 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1817 /*** END OF FILE ***/