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,
129 if( FETCH_WCB( *window, Reshape ) )
130 INVOKE_WCB( *window, Reshape, ( width, height ) );
133 fgSetWindow( window );
134 glViewport( 0, 0, 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 )
195 * XXX Resizing should *not* depend upon whether there
196 * XXX is a pending redisplay flag, as far as I can tell.
198 * XXX Note, too, that the {NeedToResize} flag is a little
199 * XXX fuzzy in its meaning, since for WIN32, this also
200 * XXX means "we need to tell the application that the window has
201 * XXX changed size", while in X11, it only means "we need
202 * XXX to ask the window system to resize the window.
203 * XXX Splitting the flag's meaning might be desirable, but
204 * XXX that could complicate the code more. (On X11, the
205 * XXX user callback is called as soon as the event is
206 * XXX discovered, but resizing the window is postponed
207 * XXX until after other events.)
209 if( window->State.NeedToResize )
211 SFG_Window *current_window = fgStructure.Window;
213 fgSetWindow( window );
215 fghReshapeWindowByHandle(
216 window->Window.Handle,
221 window->State.NeedToResize = GL_FALSE;
222 fgSetWindow ( current_window );
225 window->State.Redisplay = GL_FALSE;
227 #if TARGET_HOST_UNIX_X11
229 SFG_Window *current_window = fgStructure.Window;
231 INVOKE_WCB( *window, Display, ( ) );
232 fgSetWindow( current_window );
234 #elif TARGET_HOST_WIN32
236 window->Window.Handle, NULL, NULL,
237 RDW_NOERASE | RDW_INTERNALPAINT | RDW_INVALIDATE | RDW_UPDATENOW
242 fgEnumSubWindows( window, fghcbDisplayWindow, enumerator );
246 * Make all windows perform a display call
248 static void fghDisplayAll( void )
250 SFG_Enumerator enumerator;
252 enumerator.found = GL_FALSE;
253 enumerator.data = NULL;
255 fgEnumWindows( fghcbDisplayWindow, &enumerator );
259 * Window enumerator callback to check for the joystick polling code
261 static void fghcbCheckJoystickPolls( SFG_Window *window,
262 SFG_Enumerator *enumerator )
264 long int checkTime = fgElapsedTime( );
266 if( window->State.JoystickLastPoll + window->State.JoystickPollRate <=
269 fgJoystickPollWindow( window );
270 window->State.JoystickLastPoll = checkTime;
273 fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );
277 * Check all windows for joystick polling
279 static void fghCheckJoystickPolls( void )
281 SFG_Enumerator enumerator;
283 enumerator.found = GL_FALSE;
284 enumerator.data = NULL;
286 fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );
290 * Check the global timers
292 static void fghCheckTimers( void )
294 long checkTime = fgElapsedTime( );
297 while( timer = fgState.Timers.First )
299 if( timer->TriggerTime > checkTime )
302 fgListRemove( &fgState.Timers, &timer->Node );
303 fgListAppend( &fgState.FreeTimers, &timer->Node );
305 timer->Callback( timer->ID );
312 long fgElapsedTime( void )
314 if ( fgState.Time.Set )
316 #if TARGET_HOST_UNIX_X11
320 gettimeofday( &now, NULL );
322 elapsed = (now.tv_usec - fgState.Time.Value.tv_usec) / 1000;
323 elapsed += (now.tv_sec - fgState.Time.Value.tv_sec) * 1000;
326 #elif TARGET_HOST_WIN32
327 return timeGetTime() - fgState.Time.Value;
332 #if TARGET_HOST_UNIX_X11
333 gettimeofday( &fgState.Time.Value, NULL );
334 #elif TARGET_HOST_WIN32
335 fgState.Time.Value = timeGetTime ();
337 fgState.Time.Set = GL_TRUE ;
346 void fgError( const char *fmt, ... )
352 fprintf( stderr, "freeglut ");
353 if( fgState.ProgramName )
354 fprintf (stderr, "(%s): ", fgState.ProgramName);
355 vfprintf( stderr, fmt, ap );
356 fprintf( stderr, "\n" );
360 if ( fgState.Initialised )
366 void fgWarning( const char *fmt, ... )
372 fprintf( stderr, "freeglut ");
373 if( fgState.ProgramName )
374 fprintf( stderr, "(%s): ", fgState.ProgramName );
375 vfprintf( stderr, fmt, ap );
376 fprintf( stderr, "\n" );
382 * Indicates whether Joystick events are being used by ANY window.
384 * The current mechanism is to walk all of the windows and ask if
385 * there is a joystick callback. Certainly in some cases, maybe
386 * in all cases, the joystick is attached to the system and accessed
387 * from ONE point by GLUT/freeglut, so this is not the right way,
388 * in general, to do this. However, the Joystick code is segregated
389 * in its own little world, so we can't access the information that
390 * we need in order to do that nicely.
393 * * Store Joystick data into freeglut global state.
394 * * Provide NON-static functions or data from Joystick *.c file.
396 * Basically, the RIGHT way to do this requires knowing something
397 * about the Joystick. Right now, the Joystick code is behind
401 static void fgCheckJoystickCallback( SFG_Window* w, SFG_Enumerator* e)
403 if( FETCH_WCB( *w, Joystick ) )
408 fgEnumSubWindows( w, fgCheckJoystickCallback, e );
410 static int fgHaveJoystick( void )
412 SFG_Enumerator enumerator;
413 enumerator.found = GL_FALSE;
414 enumerator.data = NULL;
415 fgEnumWindows( fgCheckJoystickCallback, &enumerator );
416 return !!enumerator.data;
418 static void fgHavePendingRedisplaysCallback( SFG_Window* w, SFG_Enumerator* e)
420 if( w->State.Redisplay )
425 fgEnumSubWindows( w, fgHavePendingRedisplaysCallback, e );
427 static int fgHavePendingRedisplays (void)
429 SFG_Enumerator enumerator;
430 enumerator.found = GL_FALSE;
431 enumerator.data = NULL;
432 fgEnumWindows( fgHavePendingRedisplaysCallback, &enumerator );
433 return !!enumerator.data;
436 * Returns the number of GLUT ticks (milliseconds) till the next timer event.
438 static long fgNextTimer( void )
441 SFG_Timer *timer = fgState.Timers.First;
444 ret = timer->TriggerTime - fgElapsedTime();
451 * Does the magic required to relinquish the CPU until something interesting
454 static void fgSleepForEvents( void )
456 #if TARGET_HOST_UNIX_X11
463 if( fgState.IdleCallback || fgHavePendingRedisplays( ) )
465 socket = ConnectionNumber( fgDisplay.Display );
467 FD_SET( socket, &fdset );
469 msec = fgNextTimer( );
470 if( fgHaveJoystick( ) )
471 msec = MIN( msec, 10 );
473 wait.tv_sec = msec / 1000;
474 wait.tv_usec = (msec % 1000) * 1000;
475 err = select( socket+1, &fdset, NULL, NULL, &wait );
478 fgWarning ( "freeglut select() error: %d\n", errno );
480 #elif TARGET_HOST_WIN32
484 #if TARGET_HOST_UNIX_X11
486 * Returns GLUT modifier mask for an XEvent.
488 int fgGetXModifiers( XEvent *event )
492 if( event->xkey.state & ( ShiftMask | LockMask ) )
493 ret |= GLUT_ACTIVE_SHIFT;
494 if( event->xkey.state & ControlMask )
495 ret |= GLUT_ACTIVE_CTRL;
496 if( event->xkey.state & Mod1Mask )
497 ret |= GLUT_ACTIVE_ALT;
504 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
507 * Executes a single iteration in the freeglut processing loop.
509 void FGAPIENTRY glutMainLoopEvent( void )
511 #if TARGET_HOST_UNIX_X11
516 * This code was repeated constantly, so here it goes into a definition:
518 #define GETWINDOW(a) \
519 window = fgWindowByHandle( event.a.window ); \
520 if( window == NULL ) \
523 #define GETMOUSE(a) \
524 window->State.MouseX = event.a.x; \
525 window->State.MouseY = event.a.y;
527 freeglut_assert_ready;
529 while( XPending( fgDisplay.Display ) )
531 XNextEvent( fgDisplay.Display, &event );
537 * Destroy the window when the WM_DELETE_WINDOW message arrives
539 if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.DeleteWindow )
541 GETWINDOW( xclient );
543 fgDestroyWindow ( window );
545 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
551 fgState.ExecState = GLUT_EXEC_STATE_STOP;
557 * CreateNotify causes a configure-event so that sub-windows are
558 * handled compatibly with GLUT. Otherwise, your sub-windows
559 * (in freeglut only) will not get an initial reshape event,
560 * which can break things.
562 * GLUT presumably does this because it generally tries to treat
563 * sub-windows the same as windows.
566 case ConfigureNotify:
567 GETWINDOW( xconfigure );
569 int width = event.xconfigure.width;
570 int height = event.xconfigure.height;
572 if( ( width != window->State.OldWidth ) ||
573 ( height != window->State.OldHeight ) )
575 window->State.OldWidth = width;
576 window->State.OldHeight = height;
577 if( FETCH_WCB( *window, Reshape ) )
578 INVOKE_WCB( *window, Reshape, ( width, height ) );
581 fgSetWindow( window );
582 glViewport( 0, 0, width, height );
584 glutPostRedisplay( );
591 * This is sent to confirm the XDestroyWindow call.
592 * XXX WHY is this commented out? Should we re-enable it?
594 /* fgAddToWindowDestroyList ( window ); */
599 * We are too dumb to process partial exposes...
600 * XXX Well, we could do it. However, it seems to only
601 * XXX be potentially useful for single-buffered (since
602 * XXX double-buffered does not respect viewport when we
603 * XXX do a buffer-swap).
605 if( event.xexpose.count == 0 )
606 fghRedrawWindowByHandle( event.xexpose.window );
612 * If we never do anything with this, can we just not ask to
613 * get these messages?
619 * Have the client's keyboard knowledge updated (xlib.ps,
620 * page 206, says that's a good thing to do)
622 XRefreshKeyboardMapping( (XMappingEvent *) &event );
625 case VisibilityNotify:
627 GETWINDOW( xvisibility );
628 if( ! FETCH_WCB( *window, WindowStatus ) )
630 fgSetWindow( window );
633 * Sending this event, the X server can notify us that the window
634 * has just acquired one of the three possible visibility states:
635 * VisibilityUnobscured, VisibilityPartiallyObscured or
636 * VisibilityFullyObscured
638 switch( event.xvisibility.state )
640 case VisibilityUnobscured:
641 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_RETAINED ) );
642 window->State.Visible = GL_TRUE;
645 case VisibilityPartiallyObscured:
646 INVOKE_WCB( *window, WindowStatus,
647 ( GLUT_PARTIALLY_RETAINED ) );
648 window->State.Visible = GL_TRUE;
651 case VisibilityFullyObscured:
652 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_COVERED ) );
653 window->State.Visible = GL_FALSE;
657 fgWarning( "Uknown X visibility state: %d",
658 event.xvisibility.state );
666 GETWINDOW( xcrossing );
667 GETMOUSE( xcrossing );
668 INVOKE_WCB( *window, Entry, ( ( EnterNotify == event.type ) ?
675 GETWINDOW( xmotion );
678 if( window->ActiveMenu )
680 if( window == window->ActiveMenu->ParentWindow )
682 window->ActiveMenu->Window->State.MouseX =
683 event.xmotion.x_root - window->ActiveMenu->X;
684 window->ActiveMenu->Window->State.MouseY =
685 event.xmotion.y_root - window->ActiveMenu->Y;
687 window->ActiveMenu->Window->State.Redisplay = GL_TRUE ;
688 fgSetWindow( window->ActiveMenu->ParentWindow );
694 * XXX For more than 5 buttons, just check {event.xmotion.state},
695 * XXX rather than a host of bit-masks?
697 #define BUTTON_MASK \
698 ( Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask )
699 if ( event.xmotion.state & BUTTON_MASK )
700 INVOKE_WCB( *window, Motion, ( event.xmotion.x,
703 INVOKE_WCB( *window, Passive, ( event.xmotion.x,
711 GLboolean pressed = GL_TRUE;
714 if( event.type == ButtonRelease )
718 * A mouse button has been pressed or released. Traditionally,
719 * break if the window was found within the freeglut structures.
721 GETWINDOW( xbutton );
725 * An X button (at least in XFree86) is numbered from 1.
726 * A GLUT button is numbered from 0.
727 * Old GLUT passed through buttons other than just the first
728 * three, though it only gave symbolic names and official
729 * support to the first three.
731 button = event.xbutton.button - 1;
734 * XXX This comment is replicated in the WIN32 section and
735 * XXX maybe also in the menu code. Can we move the info
736 * XXX to one central place and *reference* it from here?
738 * Do not execute the application's mouse callback if a menu
739 * is hooked to this button. In that case an appropriate
740 * private call should be generated.
741 * Near as I can tell, this is the menu behaviour:
742 * - Down-click the menu button, menu not active: activate
743 * the menu with its upper left-hand corner at the mouse
745 * - Down-click any button outside the menu, menu active:
746 * deactivate the menu
747 * - Down-click any button inside the menu, menu active:
748 * select the menu entry and deactivate the menu
749 * - Up-click the menu button, menu not active: nothing happens
750 * - Up-click the menu button outside the menu, menu active:
752 * - Up-click the menu button inside the menu, menu active:
753 * select the menu entry and deactivate the menu
755 /* Window has an active menu, it absorbs any mouse click */
756 if( window->ActiveMenu )
758 if( window == window->ActiveMenu->ParentWindow )
760 window->ActiveMenu->Window->State.MouseX =
761 event.xbutton.x_root - window->ActiveMenu->X;
762 window->ActiveMenu->Window->State.MouseY =
763 event.xbutton.y_root - window->ActiveMenu->Y;
766 /* In the menu, invoke the callback and deactivate the menu*/
767 if( fgCheckActiveMenu( window->ActiveMenu->Window,
768 window->ActiveMenu ) )
771 * Save the current window and menu and set the current
772 * window to the window whose menu this is
774 SFG_Window *save_window = fgStructure.Window;
775 SFG_Menu *save_menu = fgStructure.Menu;
776 SFG_Window *parent_window =
777 window->ActiveMenu->ParentWindow;
778 fgSetWindow( parent_window );
779 fgStructure.Menu = window->ActiveMenu;
781 /* Execute the menu callback */
782 fgExecuteMenuCallback( window->ActiveMenu );
783 fgDeactivateMenu( parent_window );
785 /* Restore the current window and menu */
786 fgSetWindow( save_window );
787 fgStructure.Menu = save_menu;
791 * Outside the menu, deactivate if it's a downclick
792 * XXX This isn't enough. A downclick outside of
793 * XXX the interior of our freeglut windows should also
794 * XXX deactivate the menu. This is more complicated.
796 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
798 window->State.Redisplay = GL_TRUE;
803 * No active menu, let's check whether we need to activate one.
805 if( ( 0 <= button ) &&
806 ( FREEGLUT_MAX_MENUS > button ) &&
807 ( window->Menu[ button ] ) &&
810 window->State.Redisplay = GL_TRUE;
811 fgSetWindow( window );
812 fgActivateMenu( window, button );
817 * Check if there is a mouse or mouse wheel callback hooked to the
820 if( ! FETCH_WCB( *window, Mouse ) &&
821 ! FETCH_WCB( *window, MouseWheel ) )
824 fgState.Modifiers = fgGetXModifiers( &event );
827 * Finally execute the mouse or mouse wheel callback
829 * XXX Use a symbolic constant, *not* "4"!
831 if( ( button < 3 ) || ( ! FETCH_WCB( *window, MouseWheel ) ) )
832 INVOKE_WCB( *window, Mouse, ( button,
833 pressed ? GLUT_DOWN : GLUT_UP,
840 * Map 4 and 5 to wheel zero; EVEN to +1, ODD to -1
841 * " 6 and 7 " " one; ...
843 * XXX This *should* be behind some variables/macros,
844 * XXX since the order and numbering isn't certain
845 * XXX See XFree86 configuration docs (even back in the
846 * XXX 3.x days, and especially with 4.x).
848 * XXX Note that {button} has already been decremeted
849 * XXX in mapping from X button numbering to GLUT.
851 int wheel_number = (button - 3) / 2;
857 INVOKE_WCB( *window, MouseWheel, ( wheel_number,
865 * Trash the modifiers state
867 fgState.Modifiers = 0xffffffff;
874 FGCBKeyboard keyboard_cb;
875 FGCBSpecial special_cb;
880 if( event.type == KeyPress )
882 keyboard_cb = FETCH_WCB( *window, Keyboard );
883 special_cb = FETCH_WCB( *window, Special );
887 keyboard_cb = FETCH_WCB( *window, KeyboardUp );
888 special_cb = FETCH_WCB( *window, SpecialUp );
892 * Is there a keyboard/special callback hooked for this window?
894 if( keyboard_cb || special_cb )
896 XComposeStatus composeStatus;
897 char asciiCode[ 32 ];
902 * Check for the ASCII/KeySym codes associated with the event:
904 len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode),
905 &keySym, &composeStatus
909 * GLUT API tells us to have two separate callbacks...
914 * ...one for the ASCII translateable keypresses...
918 fgSetWindow( window );
919 fgState.Modifiers = fgGetXModifiers( &event );
920 keyboard_cb( asciiCode[ 0 ],
921 event.xkey.x, event.xkey.y
923 fgState.Modifiers = 0xffffffff;
931 * ...and one for all the others, which need to be
932 * translated to GLUT_KEY_Xs...
936 case XK_F1: special = GLUT_KEY_F1; break;
937 case XK_F2: special = GLUT_KEY_F2; break;
938 case XK_F3: special = GLUT_KEY_F3; break;
939 case XK_F4: special = GLUT_KEY_F4; break;
940 case XK_F5: special = GLUT_KEY_F5; break;
941 case XK_F6: special = GLUT_KEY_F6; break;
942 case XK_F7: special = GLUT_KEY_F7; break;
943 case XK_F8: special = GLUT_KEY_F8; break;
944 case XK_F9: special = GLUT_KEY_F9; break;
945 case XK_F10: special = GLUT_KEY_F10; break;
946 case XK_F11: special = GLUT_KEY_F11; break;
947 case XK_F12: special = GLUT_KEY_F12; break;
949 case XK_Left: special = GLUT_KEY_LEFT; break;
950 case XK_Right: special = GLUT_KEY_RIGHT; break;
951 case XK_Up: special = GLUT_KEY_UP; break;
952 case XK_Down: special = GLUT_KEY_DOWN; break;
955 case XK_Prior: special = GLUT_KEY_PAGE_UP; break;
957 case XK_Next: special = GLUT_KEY_PAGE_DOWN; break;
959 case XK_Home: special = GLUT_KEY_HOME; break;
961 case XK_End: special = GLUT_KEY_END; break;
963 case XK_Insert: special = GLUT_KEY_INSERT; break;
967 * Execute the callback (if one has been specified),
968 * given that the special code seems to be valid...
970 if( special_cb && (special != -1) )
972 fgSetWindow( window );
973 fgState.Modifiers = fgGetXModifiers( &event );
974 special_cb( special, event.xkey.x, event.xkey.y );
975 fgState.Modifiers = 0xffffffff;
983 break; /* XXX Should disable this event */
986 fgWarning ("Unknown X event type: %d", event.type);
991 #elif TARGET_HOST_WIN32
995 while( PeekMessage( &stMsg, NULL, 0, 0, PM_NOREMOVE ) )
997 if( GetMessage( &stMsg, NULL, 0, 0 ) == 0 )
999 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
1004 fgState.ExecState = GLUT_EXEC_STATE_STOP;
1008 TranslateMessage( &stMsg );
1009 DispatchMessage( &stMsg );
1013 if( fgState.Timers.First )
1015 fghCheckJoystickPolls( );
1022 * Enters the freeglut processing loop.
1023 * Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".
1025 void FGAPIENTRY glutMainLoop( void )
1027 #if TARGET_HOST_WIN32
1028 SFG_Window *window = (SFG_Window *)fgStructure.Windows.First ;
1031 freeglut_assert_ready;
1033 #if TARGET_HOST_WIN32
1035 * Processing before the main loop: If there is a window which is open and
1036 * which has a visibility callback, call it. I know this is an ugly hack,
1037 * but I'm not sure what else to do about it. Ideally we should leave
1038 * something uninitialized in the create window code and initialize it in
1039 * the main loop, and have that initialization create a "WM_ACTIVATE"
1040 * message. Then we would put the visibility callback code in the
1041 * "case WM_ACTIVATE" block below. - John Fay -- 10/24/02
1045 if ( FETCH_WCB( *window, Visibility ) )
1047 SFG_Window *current_window = fgStructure.Window ;
1049 INVOKE_WCB( *window, Visibility, ( window->State.Visible ) );
1050 fgSetWindow( current_window );
1053 window = (SFG_Window *)window->Node.Next ;
1057 fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;
1058 while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )
1060 glutMainLoopEvent( );
1062 if( fgStructure.Windows.First == NULL )
1063 fgState.ExecState = GLUT_EXEC_STATE_STOP;
1066 if( fgState.IdleCallback )
1067 fgState.IdleCallback( );
1074 * When this loop terminates, destroy the display, state and structure
1075 * of a freeglut session, so that another glutInit() call can happen
1081 * Leaves the freeglut processing loop.
1083 void FGAPIENTRY glutLeaveMainLoop( void )
1085 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
1089 #if TARGET_HOST_WIN32
1091 * Determine a GLUT modifer mask based on MS-WINDOWS system info.
1093 int fgGetWin32Modifiers (void)
1096 ( ( ( GetKeyState( VK_LSHIFT ) < 0 ) ||
1097 ( GetKeyState( VK_RSHIFT ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1098 ( ( ( GetKeyState( VK_LCONTROL ) < 0 ) ||
1099 ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL : 0 ) |
1100 ( ( ( GetKeyState( VK_LMENU ) < 0 ) ||
1101 ( GetKeyState( VK_RMENU ) < 0 )) ? GLUT_ACTIVE_ALT : 0 );
1105 * The window procedure for handling Win32 events
1107 LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,
1110 SFG_Window* window = fgWindowByHandle( hWnd );
1114 if ( ( window == NULL ) && ( uMsg != WM_CREATE ) )
1115 return DefWindowProc( hWnd, uMsg, wParam, lParam );
1117 /* printf ( "Window %3d message <%04x> %12d %12d\n", window?window->ID:0,
1118 uMsg, wParam, lParam ); */
1123 * The window structure is passed as the creation structure paramter...
1125 window = (SFG_Window *) (((LPCREATESTRUCT) lParam)->lpCreateParams);
1126 assert( window != NULL );
1128 window->Window.Handle = hWnd;
1129 window->Window.Device = GetDC( hWnd );
1130 if( window->IsMenu )
1132 unsigned int current_DisplayMode = fgState.DisplayMode;
1133 fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
1134 fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
1135 fgState.DisplayMode = current_DisplayMode;
1137 if( fgStructure.MenuContext )
1138 wglMakeCurrent( window->Window.Device,
1139 fgStructure.MenuContext->Context
1143 fgStructure.MenuContext =
1144 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
1145 fgStructure.MenuContext->Context =
1146 wglCreateContext( window->Window.Device );
1149 /* window->Window.Context = wglGetCurrentContext (); */
1150 window->Window.Context = wglCreateContext( window->Window.Device );
1154 fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
1156 if( ! fgState.UseCurrentContext )
1157 window->Window.Context =
1158 wglCreateContext( window->Window.Device );
1161 window->Window.Context = wglGetCurrentContext( );
1162 if( ! window->Window.Context )
1163 window->Window.Context =
1164 wglCreateContext( window->Window.Device );
1168 window->State.NeedToResize = GL_TRUE;
1169 window->State.Width = fgState.Size.X;
1170 window->State.Height = fgState.Size.Y;
1172 ReleaseDC( window->Window.Handle, window->Window.Device );
1177 * We got resized... But check if the window has been already added...
1179 window->State.NeedToResize = GL_TRUE;
1180 window->State.Width = LOWORD(lParam);
1181 window->State.Height = HIWORD(lParam);
1185 printf("WM_SETFOCUS: %p\n", window );
1186 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1190 if (LOWORD(wParam) != WA_INACTIVE)
1192 /* glutSetCursor( fgStructure.Window->State.Cursor ); */
1193 printf("WM_ACTIVATE: glutSetCursor( %p, %d)\n", window,
1194 window->State.Cursor );
1195 glutSetCursor( window->State.Cursor );
1198 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1203 * XXX Why not re-use some common code with the glutSetCursor()
1204 * XXX function (or perhaps invoke glutSetCursor())?
1205 * XXX That is, why are we duplicating code, here, from
1206 * XXX glutSetCursor()? The WIN32 code should be able to just
1207 * XXX call glutSetCurdsor() instead of defining two macros
1208 * XXX and implementing a nested case in-line.
1211 /* Set the cursor AND change it for this window class. */
1212 #define MAP_CURSOR(a,b) \
1214 SetCursor( LoadCursor( NULL, b ) ); \
1217 /* Nuke the cursor AND change it for this window class. */
1218 #define ZAP_CURSOR(a,b) \
1220 SetCursor( NULL ); \
1223 if( LOWORD( lParam ) == HTCLIENT )
1224 switch( window->State.Cursor )
1226 MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, IDC_ARROW );
1227 MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW, IDC_ARROW );
1228 MAP_CURSOR( GLUT_CURSOR_INFO, IDC_HELP );
1229 MAP_CURSOR( GLUT_CURSOR_DESTROY, IDC_CROSS );
1230 MAP_CURSOR( GLUT_CURSOR_HELP, IDC_HELP );
1231 MAP_CURSOR( GLUT_CURSOR_CYCLE, IDC_SIZEALL );
1232 MAP_CURSOR( GLUT_CURSOR_SPRAY, IDC_CROSS );
1233 MAP_CURSOR( GLUT_CURSOR_WAIT, IDC_WAIT );
1234 MAP_CURSOR( GLUT_CURSOR_TEXT, IDC_UPARROW );
1235 MAP_CURSOR( GLUT_CURSOR_CROSSHAIR, IDC_CROSS );
1236 /* MAP_CURSOR( GLUT_CURSOR_NONE, IDC_NO ); */
1237 ZAP_CURSOR( GLUT_CURSOR_NONE, NULL );
1240 MAP_CURSOR( GLUT_CURSOR_UP_DOWN, IDC_ARROW );
1243 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1247 window->State.Visible = GL_TRUE;
1248 window->State.Redisplay = GL_TRUE;
1252 /* Turn on the visibility in case it was turned off somehow */
1253 window->State.Visible = GL_TRUE;
1254 BeginPaint( hWnd, &ps );
1255 fghRedrawWindowByHandle( hWnd );
1256 EndPaint( hWnd, &ps );
1260 fgDestroyWindow ( window );
1261 if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
1267 * The window already got destroyed, so don't bother with it.
1273 window->State.MouseX = LOWORD( lParam );
1274 window->State.MouseY = HIWORD( lParam );
1276 if ( window->ActiveMenu )
1278 window->State.Redisplay = GL_TRUE;
1279 fgSetWindow ( window->ActiveMenu->ParentWindow );
1283 fgState.Modifiers = fgGetWin32Modifiers( );
1285 if( ( wParam & MK_LBUTTON ) ||
1286 ( wParam & MK_MBUTTON ) ||
1287 ( wParam & MK_RBUTTON ) )
1288 INVOKE_WCB( *window, Motion, ( window->State.MouseX,
1289 window->State.MouseY ) );
1291 INVOKE_WCB( *window, Passive, ( window->State.MouseX,
1292 window->State.MouseY ) );
1294 fgState.Modifiers = 0xffffffff;
1298 case WM_LBUTTONDOWN:
1299 case WM_MBUTTONDOWN:
1300 case WM_RBUTTONDOWN:
1305 GLboolean pressed = GL_TRUE;
1308 window->State.MouseX = LOWORD( lParam );
1309 window->State.MouseY = HIWORD( lParam );
1313 case WM_LBUTTONDOWN:
1315 button = GLUT_LEFT_BUTTON;
1317 case WM_MBUTTONDOWN:
1319 button = GLUT_MIDDLE_BUTTON;
1321 case WM_RBUTTONDOWN:
1323 button = GLUT_RIGHT_BUTTON;
1327 button = GLUT_LEFT_BUTTON;
1331 button = GLUT_MIDDLE_BUTTON;
1335 button = GLUT_RIGHT_BUTTON;
1343 if( GetSystemMetrics( SM_SWAPBUTTON ) )
1344 if( button == GLUT_LEFT_BUTTON )
1345 button = GLUT_RIGHT_BUTTON;
1346 else if( button == GLUT_RIGHT_BUTTON )
1347 button = GLUT_LEFT_BUTTON;
1350 return DefWindowProc( hWnd, uMsg, lParam, wParam );
1353 * XXX This comment is duplicated in two other spots.
1354 * XXX Can we centralize it?
1356 * Do not execute the application's mouse callback if a
1357 * menu is hooked to this button.
1358 * In that case an appropriate private call should be generated.
1359 * Near as I can tell, this is the menu behaviour:
1360 * - Down-click the menu button, menu not active: activate
1361 * the menu with its upper left-hand corner at the mouse location.
1362 * - Down-click any button outside the menu, menu active:
1363 * deactivate the menu
1364 * - Down-click any button inside the menu, menu active:
1365 * select the menu entry and deactivate the menu
1366 * - Up-click the menu button, menu not active: nothing happens
1367 * - Up-click the menu button outside the menu, menu active:
1369 * - Up-click the menu button inside the menu, menu active:
1370 * select the menu entry and deactivate the menu
1372 /* Window has an active menu, it absorbs any mouse click */
1373 if( window->ActiveMenu )
1375 /* Outside the menu, deactivate the menu if it's a downclick */
1376 if( ! fgCheckActiveMenu( window, window->ActiveMenu ) )
1379 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
1381 else /* In menu, invoke the callback and deactivate the menu*/
1384 * Save the current window and menu and set the current
1385 * window to the window whose menu this is
1387 SFG_Window *save_window = fgStructure.Window;
1388 SFG_Menu *save_menu = fgStructure.Menu;
1389 SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
1390 fgSetWindow( parent_window );
1391 fgStructure.Menu = window->ActiveMenu;
1393 /* Execute the menu callback */
1394 fgExecuteMenuCallback( window->ActiveMenu );
1395 fgDeactivateMenu( parent_window );
1397 /* Restore the current window and menu */
1398 fgSetWindow( save_window );
1399 fgStructure.Menu = save_menu;
1403 * Let's make the window redraw as a result of the mouse
1404 * click and menu activity.
1406 if( ! window->IsMenu )
1407 window->State.Redisplay = GL_TRUE;
1412 if ( window->Menu[ button ] && pressed )
1414 window->State.Redisplay = GL_TRUE;
1415 fgSetWindow( window );
1416 fgActivateMenu( window, button );
1421 if( ! FETCH_WCB( *window, Mouse ) )
1424 fgSetWindow( window );
1425 fgState.Modifiers = fgGetWin32Modifiers( );
1430 pressed ? GLUT_DOWN : GLUT_UP,
1431 window->State.MouseX,
1432 window->State.MouseY
1436 fgState.Modifiers = 0xffffffff;
1441 /* Should be WM_MOUSEWHEEL but my compiler doesn't recognize it */
1444 * XXX THIS IS SPECULATIVE -- John Fay, 10/2/03
1445 * XXX Should use WHEEL_DELTA instead of 120
1447 int wheel_number = LOWORD( wParam );
1448 short ticks = ( short )HIWORD( wParam ) / 120;
1458 * The mouse cursor has moved. Remember the new mouse cursor's position
1460 /* window->State.MouseX = LOWORD( lParam ); */
1461 /* Need to adjust by window position, */
1462 /* window->State.MouseY = HIWORD( lParam ); */
1463 /* change "lParam" to other parameter */
1465 if( ! FETCH_WCB( *window, MouseWheel ) &&
1466 ! FETCH_WCB( *window, Mouse ) )
1469 fgSetWindow( window );
1470 fgState.Modifiers = fgGetWin32Modifiers( );
1473 if( FETCH_WCB( *window, MouseWheel ) )
1474 INVOKE_WCB( *window, MouseWheel,
1477 window->State.MouseX,
1478 window->State.MouseY
1481 else /* No mouse wheel, call the mouse button callback twice */
1484 * XXX The below assumes that you have no more than 3 mouse
1485 * XXX buttons. Sorry.
1487 int button = wheel_number*2 + 4;
1490 INVOKE_WCB( *window, Mouse,
1491 ( button, GLUT_DOWN,
1492 window->State.MouseX, window->State.MouseY )
1494 INVOKE_WCB( *window, Mouse,
1496 window->State.MouseX, window->State.MouseX )
1500 fgState.Modifiers = 0xffffffff;
1510 if( fgState.IgnoreKeyRepeat && (lParam & KF_REPEAT) )
1514 * Remember the current modifiers state. This is done here in order
1515 * to make sure the VK_DELETE keyboard callback is executed properly.
1517 fgState.Modifiers = fgGetWin32Modifiers( );
1519 GetCursorPos( &mouse_pos );
1520 ScreenToClient( window->Window.Handle, &mouse_pos );
1522 window->State.MouseX = mouse_pos.x;
1523 window->State.MouseY = mouse_pos.y;
1526 * Convert the Win32 keystroke codes to GLUTtish way
1528 # define KEY(a,b) case a: keypress = b; break;
1532 KEY( VK_F1, GLUT_KEY_F1 );
1533 KEY( VK_F2, GLUT_KEY_F2 );
1534 KEY( VK_F3, GLUT_KEY_F3 );
1535 KEY( VK_F4, GLUT_KEY_F4 );
1536 KEY( VK_F5, GLUT_KEY_F5 );
1537 KEY( VK_F6, GLUT_KEY_F6 );
1538 KEY( VK_F7, GLUT_KEY_F7 );
1539 KEY( VK_F8, GLUT_KEY_F8 );
1540 KEY( VK_F9, GLUT_KEY_F9 );
1541 KEY( VK_F10, GLUT_KEY_F10 );
1542 KEY( VK_F11, GLUT_KEY_F11 );
1543 KEY( VK_F12, GLUT_KEY_F12 );
1544 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP );
1545 KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
1546 KEY( VK_HOME, GLUT_KEY_HOME );
1547 KEY( VK_END, GLUT_KEY_END );
1548 KEY( VK_LEFT, GLUT_KEY_LEFT );
1549 KEY( VK_UP, GLUT_KEY_UP );
1550 KEY( VK_RIGHT, GLUT_KEY_RIGHT );
1551 KEY( VK_DOWN, GLUT_KEY_DOWN );
1552 KEY( VK_INSERT, GLUT_KEY_INSERT );
1556 * The delete key should be treated as an ASCII keypress:
1558 INVOKE_WCB( *window, Keyboard,
1559 ( 127, window->State.MouseX, window->State.MouseY )
1563 if( keypress != -1 )
1564 INVOKE_WCB( *window, Special,
1566 window->State.MouseX, window->State.MouseY )
1569 fgState.Modifiers = 0xffffffff;
1580 * Remember the current modifiers state. This is done here in order
1581 * to make sure the VK_DELETE keyboard callback is executed properly.
1583 fgState.Modifiers = fgGetWin32Modifiers( );
1585 GetCursorPos( &mouse_pos );
1586 ScreenToClient( window->Window.Handle, &mouse_pos );
1588 window->State.MouseX = mouse_pos.x;
1589 window->State.MouseY = mouse_pos.y;
1592 * Convert the Win32 keystroke codes to GLUTtish way.
1593 * "KEY(a,b)" was defined under "WM_KEYDOWN"
1598 KEY( VK_F1, GLUT_KEY_F1 );
1599 KEY( VK_F2, GLUT_KEY_F2 );
1600 KEY( VK_F3, GLUT_KEY_F3 );
1601 KEY( VK_F4, GLUT_KEY_F4 );
1602 KEY( VK_F5, GLUT_KEY_F5 );
1603 KEY( VK_F6, GLUT_KEY_F6 );
1604 KEY( VK_F7, GLUT_KEY_F7 );
1605 KEY( VK_F8, GLUT_KEY_F8 );
1606 KEY( VK_F9, GLUT_KEY_F9 );
1607 KEY( VK_F10, GLUT_KEY_F10 );
1608 KEY( VK_F11, GLUT_KEY_F11 );
1609 KEY( VK_F12, GLUT_KEY_F12 );
1610 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP );
1611 KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
1612 KEY( VK_HOME, GLUT_KEY_HOME );
1613 KEY( VK_END, GLUT_KEY_END );
1614 KEY( VK_LEFT, GLUT_KEY_LEFT );
1615 KEY( VK_UP, GLUT_KEY_UP );
1616 KEY( VK_RIGHT, GLUT_KEY_RIGHT );
1617 KEY( VK_DOWN, GLUT_KEY_DOWN );
1618 KEY( VK_INSERT, GLUT_KEY_INSERT );
1622 * The delete key should be treated as an ASCII keypress:
1624 INVOKE_WCB( *window, KeyboardUp,
1625 ( 127, window->State.MouseX, window->State.MouseY )
1634 GetKeyboardState( state );
1636 if( ToAscii( wParam, 0, state, code, 0 ) == 1 )
1639 INVOKE_WCB( *window, KeyboardUp,
1641 window->State.MouseX, window->State.MouseY )
1646 if( keypress != -1 )
1647 INVOKE_WCB( *window, SpecialUp,
1649 window->State.MouseX, window->State.MouseY )
1652 fgState.Modifiers = 0xffffffff;
1659 if( fgState.IgnoreKeyRepeat && (lParam & KF_REPEAT) )
1662 fgState.Modifiers = fgGetWin32Modifiers( );
1663 INVOKE_WCB( *window, Keyboard,
1665 window->State.MouseX, window->State.MouseY )
1667 fgState.Modifiers = 0xffffffff;
1671 case WM_CAPTURECHANGED:
1672 /* User has finished resizing the window, force a redraw */
1673 INVOKE_WCB( *window, Display, ( ) );
1675 /*lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ); */
1679 * Other messages that I have seen and which are not handled already
1681 case WM_SETTEXT: /* 0x000c */
1682 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1683 /* Pass it on to "DefWindowProc" to set the window text */
1686 case WM_GETTEXT: /* 0x000d */
1687 /* Ideally we would copy the title of the window into "lParam" */
1688 /* strncpy ( (char *)lParam, "Window Title", wParam );
1689 lRet = ( wParam > 12 ) ? 12 : wParam; */
1690 /* the number of characters copied */
1691 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1694 case WM_GETTEXTLENGTH: /* 0x000e */
1695 /* Ideally we would get the length of the title of the window */
1697 /* the number of characters in "Window Title\0" (see above) */
1700 case WM_ERASEBKGND: /* 0x0014 */
1701 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1704 case WM_SYNCPAINT: /* 0x0088 */
1705 /* Another window has moved, need to update this one */
1706 window->State.Redisplay = GL_TRUE;
1707 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1708 /* Help screen says this message must be passed to "DefWindowProc" */
1711 case WM_NCPAINT: /* 0x0085 */
1712 /* Need to update the border of this window */
1713 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1714 /* Pass it on to "DefWindowProc" to repaint a standard border */
1717 case WM_SYSCOMMAND : /* 0x0112 */
1720 * We have received a system command message. Try to act on it.
1721 * The commands are passed in through the "lParam" parameter:
1722 * Clicking on a corner to resize the window gives a "F004" message
1723 * but this is not defined in my header file.
1734 /* User has clicked on the "-" to minimize the window */
1735 /* Turn off the visibility */
1736 window->State.Visible = GL_FALSE ;
1743 case SC_NEXTWINDOW :
1746 case SC_PREVWINDOW :
1750 /* Followed very closely by a WM_CLOSE message */
1774 case SC_SCREENSAVE :
1782 /* We need to pass the message on to the operating system as well */
1783 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1788 * Handle unhandled messages
1790 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1798 /*** END OF FILE ***/