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.
33 #include <GL/freeglut.h>
34 #include "freeglut_internal.h"
37 #if TARGET_HOST_UNIX_X11
38 #include <sys/types.h>
43 #elif TARGET_HOST_WIN32
44 #elif TARGET_HOST_WINCE
46 typedef struct GXDisplayProperties GXDisplayProperties;
47 typedef struct GXKeyList GXKeyList;
50 typedef struct GXKeyList (*GXGETDEFAULTKEYS)(int);
51 typedef int (*GXOPENINPUT)();
53 GXGETDEFAULTKEYS GXGetDefaultKeys_ = NULL;
54 GXOPENINPUT GXOpenInput_ = NULL;
56 struct GXKeyList gxKeyList;
61 #define MAX(a,b) (((a)>(b)) ? (a) : (b))
65 #define MIN(a,b) (((a)<(b)) ? (a) : (b))
70 * TODO BEFORE THE STABLE RELEASE:
72 * There are some issues concerning window redrawing under X11, and maybe
73 * some events are not handled. The Win32 version lacks some more features,
74 * but seems acceptable for not demanding purposes.
76 * Need to investigate why the X11 version breaks out with an error when
77 * closing a window (using the window manager, not glutDestroyWindow)...
80 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
83 * Handle a window configuration change. When no reshape
84 * callback is hooked, the viewport size is updated to
85 * match the new window size.
87 static void fghReshapeWindow ( SFG_Window *window, int width, int height )
89 SFG_Window *current_window = fgStructure.Window;
91 freeglut_return_if_fail( window != NULL );
94 #if TARGET_HOST_UNIX_X11
96 XResizeWindow( fgDisplay.Display, window->Window.Handle,
98 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
100 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
102 #if !TARGET_HOST_WINCE
108 * For windowed mode, get the current position of the
109 * window and resize taking the size of the frame
110 * decorations into account.
113 /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
114 GetWindowRect( window->Window.Handle, &winRect );
120 if ( window->Parent == NULL )
122 if ( ! window->IsMenu && !window->State.IsGameMode )
124 w += GetSystemMetrics( SM_CXSIZEFRAME ) * 2;
125 h += GetSystemMetrics( SM_CYSIZEFRAME ) * 2 +
126 GetSystemMetrics( SM_CYCAPTION );
132 GetWindowRect( window->Parent->Window.Handle, &parentRect );
133 x -= parentRect.left + GetSystemMetrics( SM_CXSIZEFRAME ) * 2;
134 y -= parentRect.top + GetSystemMetrics( SM_CYSIZEFRAME ) * 2 +
135 GetSystemMetrics( SM_CYCAPTION );
139 * SWP_NOACTIVATE Do not activate the window
140 * SWP_NOOWNERZORDER Do not change position in z-order
141 * SWP_NOSENDCHANGING Supress WM_WINDOWPOSCHANGING message
142 * SWP_NOZORDER Retains the current Z order (ignore 2nd param)
145 SetWindowPos( window->Window.Handle,
148 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
152 #endif /* TARGET_HOST_WINCE */
155 * XXX Should update {window->State.OldWidth, window->State.OldHeight}
156 * XXX to keep in lockstep with UNIX_X11 code.
158 if( FETCH_WCB( *window, Reshape ) )
159 INVOKE_WCB( *window, Reshape, ( width, height ) );
162 fgSetWindow( window );
163 glViewport( 0, 0, width, height );
169 * Force a window redraw. In Windows at least this is only a partial
170 * solution: if the window is increasing in size in either dimension,
171 * the already-drawn part does not get drawn again and things look funny.
172 * But without this we get this bad behaviour whenever we resize the
175 window->State.Redisplay = GL_TRUE;
178 fgSetWindow( current_window );
182 * Calls a window's redraw method. This is used when
183 * a redraw is forced by the incoming window messages.
185 static void fghRedrawWindow ( SFG_Window *window )
187 SFG_Window *current_window = fgStructure.Window;
189 freeglut_return_if_fail( window );
190 freeglut_return_if_fail( FETCH_WCB ( *window, Display ) );
192 window->State.Redisplay = GL_FALSE;
194 freeglut_return_if_fail( window->State.Visible );
196 fgSetWindow( window );
198 if( window->State.NeedToResize )
206 window->State.NeedToResize = GL_FALSE;
209 INVOKE_WCB( *window, Display, ( ) );
211 fgSetWindow( current_window );
215 * A static helper function to execute display callback for a window
217 static void fghcbDisplayWindow( SFG_Window *window,
218 SFG_Enumerator *enumerator )
220 if( window->State.Redisplay &&
221 window->State.Visible )
223 window->State.Redisplay = GL_FALSE;
225 #if TARGET_HOST_UNIX_X11
226 fghRedrawWindow ( window ) ;
227 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
229 window->Window.Handle, NULL, NULL,
230 RDW_NOERASE | RDW_INTERNALPAINT | RDW_INVALIDATE | RDW_UPDATENOW
235 fgEnumSubWindows( window, fghcbDisplayWindow, enumerator );
239 * Make all windows perform a display call
241 static void fghDisplayAll( void )
243 SFG_Enumerator enumerator;
245 enumerator.found = GL_FALSE;
246 enumerator.data = NULL;
248 fgEnumWindows( fghcbDisplayWindow, &enumerator );
252 * Window enumerator callback to check for the joystick polling code
254 static void fghcbCheckJoystickPolls( SFG_Window *window,
255 SFG_Enumerator *enumerator )
257 long int checkTime = fgElapsedTime( );
259 if( window->State.JoystickLastPoll + window->State.JoystickPollRate <=
262 #if !TARGET_HOST_WINCE
263 fgJoystickPollWindow( window );
264 #endif /* !TARGET_HOST_WINCE */
265 window->State.JoystickLastPoll = checkTime;
268 fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );
272 * Check all windows for joystick polling
274 static void fghCheckJoystickPolls( void )
276 SFG_Enumerator enumerator;
278 enumerator.found = GL_FALSE;
279 enumerator.data = NULL;
281 fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );
285 * Check the global timers
287 static void fghCheckTimers( void )
289 long checkTime = fgElapsedTime( );
291 while( fgState.Timers.First )
293 SFG_Timer *timer = fgState.Timers.First;
295 if( timer->TriggerTime > checkTime )
298 fgListRemove( &fgState.Timers, &timer->Node );
299 fgListAppend( &fgState.FreeTimers, &timer->Node );
301 timer->Callback( timer->ID );
308 long fgElapsedTime( void )
310 if ( fgState.Time.Set )
312 #if TARGET_HOST_UNIX_X11
316 gettimeofday( &now, NULL );
318 elapsed = (now.tv_usec - fgState.Time.Value.tv_usec) / 1000;
319 elapsed += (now.tv_sec - fgState.Time.Value.tv_sec) * 1000;
322 #elif TARGET_HOST_WIN32
323 return timeGetTime() - fgState.Time.Value;
324 #elif TARGET_HOST_WINCE
325 return GetTickCount() - fgState.Time.Value;
330 #if TARGET_HOST_UNIX_X11
331 gettimeofday( &fgState.Time.Value, NULL );
332 #elif TARGET_HOST_WIN32
333 fgState.Time.Value = timeGetTime ();
334 #elif TARGET_HOST_WINCE
335 fgState.Time.Value = GetTickCount();
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. We have a short-circuit early
386 * return if we find any joystick handler registered.
388 * The real way to do this is to make use of the glutTimer() API
389 * to more cleanly re-implement the joystick API. Then, this code
390 * and all other "joystick timer" code can be yanked.
393 static void fghCheckJoystickCallback( SFG_Window* w, SFG_Enumerator* e)
395 if( FETCH_WCB( *w, Joystick ) )
400 fgEnumSubWindows( w, fghCheckJoystickCallback, e );
402 static int fghHaveJoystick( void )
404 SFG_Enumerator enumerator;
406 enumerator.found = GL_FALSE;
407 enumerator.data = NULL;
408 fgEnumWindows( fghCheckJoystickCallback, &enumerator );
409 return !!enumerator.data;
411 static void fghHavePendingRedisplaysCallback( SFG_Window* w, SFG_Enumerator* e)
413 if( w->State.Redisplay )
418 fgEnumSubWindows( w, fghHavePendingRedisplaysCallback, e );
420 static int fghHavePendingRedisplays (void)
422 SFG_Enumerator enumerator;
424 enumerator.found = GL_FALSE;
425 enumerator.data = NULL;
426 fgEnumWindows( fghHavePendingRedisplaysCallback, &enumerator );
427 return !!enumerator.data;
430 * Returns the number of GLUT ticks (milliseconds) till the next timer event.
432 static long fghNextTimer( void )
435 SFG_Timer *timer = fgState.Timers.First;
438 ret = timer->TriggerTime - fgElapsedTime();
445 * Does the magic required to relinquish the CPU until something interesting
448 static void fghSleepForEvents( void )
452 if( fgState.IdleCallback || fghHavePendingRedisplays( ) )
455 msec = fghNextTimer( );
456 if( fghHaveJoystick( ) ) /* XXX Use GLUT timers for joysticks... */
457 msec = MIN( msec, 10 ); /* XXX Dumb; forces granularity to .01sec */
459 #if TARGET_HOST_UNIX_X11
461 * Possibly due to aggressive use of XFlush() and friends,
462 * it is possible to have our socket drained but still have
463 * unprocessed events. (Or, this may just be normal with
464 * X, anyway?) We do non-trivial processing of X events
465 * after the event-reading loop, in any case, so we
466 * need to allow that we may have an empty socket but non-
469 if( ! XPending( fgDisplay.Display ) )
476 socket = ConnectionNumber( fgDisplay.Display );
478 FD_SET( socket, &fdset );
479 wait.tv_sec = msec / 1000;
480 wait.tv_usec = (msec % 1000) * 1000;
481 err = select( socket+1, &fdset, NULL, NULL, &wait );
484 fgWarning ( "freeglut select() error: %d", errno );
486 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
487 MsgWaitForMultipleObjects( 0, NULL, FALSE, msec, QS_ALLEVENTS );
491 #if TARGET_HOST_UNIX_X11
493 * Returns GLUT modifier mask for an XEvent.
495 static int fghGetXModifiers( XEvent *event )
499 if( event->xkey.state & ( ShiftMask | LockMask ) )
500 ret |= GLUT_ACTIVE_SHIFT;
501 if( event->xkey.state & ControlMask )
502 ret |= GLUT_ACTIVE_CTRL;
503 if( event->xkey.state & Mod1Mask )
504 ret |= GLUT_ACTIVE_ALT;
511 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
514 * Executes a single iteration in the freeglut processing loop.
516 void FGAPIENTRY glutMainLoopEvent( void )
518 #if TARGET_HOST_UNIX_X11
522 /* This code was repeated constantly, so here it goes into a definition: */
523 #define GETWINDOW(a) \
524 window = fgWindowByHandle( event.a.window ); \
525 if( window == NULL ) \
528 #define GETMOUSE(a) \
529 window->State.MouseX = event.a.x; \
530 window->State.MouseY = event.a.y;
532 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" );
534 while( XPending( fgDisplay.Display ) )
536 XNextEvent( fgDisplay.Display, &event );
541 /* Destroy the window when the WM_DELETE_WINDOW message arrives */
542 if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.DeleteWindow )
544 GETWINDOW( xclient );
546 fgDestroyWindow ( window );
548 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
553 else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )
554 fgState.ExecState = GLUT_EXEC_STATE_STOP;
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 * GLUT presumably does this because it generally tries to treat
567 * sub-windows the same as windows.
569 * XXX Technically, GETWINDOW( xconfigure ) and
570 * XXX {event.xconfigure} may not be legit ways to get at
571 * XXX data for CreateNotify events. In practice, the data
572 * XXX is in a union which is laid out much the same either
573 * XXX way. But if you want to split hairs, this isn't legit,
574 * XXX and we should instead duplicate some code.
577 case ConfigureNotify:
578 GETWINDOW( xconfigure );
580 int width = event.xconfigure.width;
581 int height = event.xconfigure.height;
583 if( ( width != window->State.OldWidth ) ||
584 ( height != window->State.OldHeight ) )
586 window->State.OldWidth = width;
587 window->State.OldHeight = height;
588 if( FETCH_WCB( *window, Reshape ) )
589 INVOKE_WCB( *window, Reshape, ( width, height ) );
592 fgSetWindow( window );
593 glViewport( 0, 0, width, height );
595 glutPostRedisplay( );
602 * This is sent to confirm the XDestroyWindow call.
604 * XXX WHY is this commented out? Should we re-enable it?
606 /* fgAddToWindowDestroyList ( window ); */
611 * We are too dumb to process partial exposes...
613 * XXX Well, we could do it. However, it seems to only
614 * XXX be potentially useful for single-buffered (since
615 * XXX double-buffered does not respect viewport when we
616 * XXX do a buffer-swap).
619 if( event.xexpose.count == 0 )
621 GETWINDOW( xexpose );
622 fgSetWindow( window );
623 glutPostRedisplay( );
630 * If we never do anything with this, can we just not ask to
631 * get these messages?
637 * Have the client's keyboard knowledge updated (xlib.ps,
638 * page 206, says that's a good thing to do)
640 XRefreshKeyboardMapping( (XMappingEvent *) &event );
643 case VisibilityNotify:
645 GETWINDOW( xvisibility );
647 * XXX INVOKE_WCB() does this check for us.
649 if( ! FETCH_WCB( *window, WindowStatus ) )
651 fgSetWindow( window );
654 * Sending this event, the X server can notify us that the window
655 * has just acquired one of the three possible visibility states:
656 * VisibilityUnobscured, VisibilityPartiallyObscured or
657 * VisibilityFullyObscured
659 switch( event.xvisibility.state )
661 case VisibilityUnobscured:
662 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_RETAINED ) );
663 window->State.Visible = GL_TRUE;
666 case VisibilityPartiallyObscured:
667 INVOKE_WCB( *window, WindowStatus,
668 ( GLUT_PARTIALLY_RETAINED ) );
669 window->State.Visible = GL_TRUE;
672 case VisibilityFullyObscured:
673 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_COVERED ) );
674 window->State.Visible = GL_FALSE;
678 fgWarning( "Unknown X visibility state: %d",
679 event.xvisibility.state );
687 GETWINDOW( xcrossing );
688 GETMOUSE( xcrossing );
689 INVOKE_WCB( *window, Entry, ( ( EnterNotify == event.type ) ?
696 GETWINDOW( xmotion );
699 if( window->ActiveMenu )
701 if( window == window->ActiveMenu->ParentWindow )
703 window->ActiveMenu->Window->State.MouseX =
704 event.xmotion.x_root - window->ActiveMenu->X;
705 window->ActiveMenu->Window->State.MouseY =
706 event.xmotion.y_root - window->ActiveMenu->Y;
708 window->ActiveMenu->Window->State.Redisplay = GL_TRUE ;
709 fgSetWindow( window->ActiveMenu->ParentWindow );
715 * XXX For more than 5 buttons, just check {event.xmotion.state},
716 * XXX rather than a host of bit-masks? Or maybe we need to
717 * XXX track ButtonPress/ButtonRelease events in our own
720 #define BUTTON_MASK \
721 ( Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask )
722 if ( event.xmotion.state & BUTTON_MASK )
723 INVOKE_WCB( *window, Motion, ( event.xmotion.x,
726 INVOKE_WCB( *window, Passive, ( event.xmotion.x,
734 GLboolean pressed = GL_TRUE;
737 if( event.type == ButtonRelease )
741 * A mouse button has been pressed or released. Traditionally,
742 * break if the window was found within the freeglut structures.
744 GETWINDOW( xbutton );
748 * An X button (at least in XFree86) is numbered from 1.
749 * A GLUT button is numbered from 0.
750 * Old GLUT passed through buttons other than just the first
751 * three, though it only gave symbolic names and official
752 * support to the first three.
754 button = event.xbutton.button - 1;
757 * XXX This comment is replicated in the WIN32 section and
758 * XXX maybe also in the menu code. Can we move the info
759 * XXX to one central place and *reference* it from here?
761 * Do not execute the application's mouse callback if a menu
762 * is hooked to this button. In that case an appropriate
763 * private call should be generated.
764 * Near as I can tell, this is the menu behaviour:
765 * - Down-click the menu button, menu not active: activate
766 * the menu with its upper left-hand corner at the mouse
768 * - Down-click any button outside the menu, menu active:
769 * deactivate the menu
770 * - Down-click any button inside the menu, menu active:
771 * select the menu entry and deactivate the menu
772 * - Up-click the menu button, menu not active: nothing happens
773 * - Up-click the menu button outside the menu, menu active:
775 * - Up-click the menu button inside the menu, menu active:
776 * select the menu entry and deactivate the menu
778 /* Window has an active menu, it absorbs any mouse click */
779 if( window->ActiveMenu )
781 if( window == window->ActiveMenu->ParentWindow )
783 window->ActiveMenu->Window->State.MouseX =
784 event.xbutton.x_root - window->ActiveMenu->X;
785 window->ActiveMenu->Window->State.MouseY =
786 event.xbutton.y_root - window->ActiveMenu->Y;
789 /* In the menu, invoke the callback and deactivate the menu*/
790 if( fgCheckActiveMenu( window->ActiveMenu->Window,
791 window->ActiveMenu ) )
794 * Save the current window and menu and set the current
795 * window to the window whose menu this is
797 SFG_Window *save_window = fgStructure.Window;
798 SFG_Menu *save_menu = fgStructure.Menu;
799 SFG_Window *parent_window =
800 window->ActiveMenu->ParentWindow;
801 fgSetWindow( parent_window );
802 fgStructure.Menu = window->ActiveMenu;
804 /* Execute the menu callback */
805 fgExecuteMenuCallback( window->ActiveMenu );
806 fgDeactivateMenu( parent_window );
808 /* Restore the current window and menu */
809 fgSetWindow( save_window );
810 fgStructure.Menu = save_menu;
814 * Outside the menu, deactivate if it's a downclick
816 * XXX This isn't enough. A downclick outside of
817 * XXX the interior of our freeglut windows should also
818 * XXX deactivate the menu. This is more complicated.
820 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
823 * XXX Why does an active menu require a redisplay at
824 * XXX this point? If this can come out cleanly, then
825 * XXX it probably should do so; if not, a comment should
828 window->State.Redisplay = GL_TRUE;
832 /* No active menu, let's check whether we need to activate one. */
833 if( ( 0 <= button ) &&
834 ( FREEGLUT_MAX_MENUS > button ) &&
835 ( window->Menu[ button ] ) &&
838 /* XXX Posting a requisite Redisplay seems bogus. */
839 window->State.Redisplay = GL_TRUE;
840 fgSetWindow( window );
841 fgActivateMenu( window, button );
846 * Check if there is a mouse or mouse wheel callback hooked to the
849 if( ! FETCH_WCB( *window, Mouse ) &&
850 ! FETCH_WCB( *window, MouseWheel ) )
853 fgState.Modifiers = fghGetXModifiers( &event );
856 * Finally execute the mouse or mouse wheel callback
858 * XXX Use a symbolic constant, *not* "4"! ("3, sire!")
860 if( ( button < 3 ) || ( ! FETCH_WCB( *window, MouseWheel ) ) )
861 INVOKE_WCB( *window, Mouse, ( button,
862 pressed ? GLUT_DOWN : GLUT_UP,
869 * Map 4 and 5 to wheel zero; EVEN to +1, ODD to -1
870 * " 6 and 7 " " one; ...
872 * XXX This *should* be behind some variables/macros,
873 * XXX since the order and numbering isn't certain
874 * XXX See XFree86 configuration docs (even back in the
875 * XXX 3.x days, and especially with 4.x).
877 * XXX Note that {button} has already been decremeted
878 * XXX in mapping from X button numbering to GLUT.
880 int wheel_number = (button - 3) / 2;
886 INVOKE_WCB( *window, MouseWheel, ( wheel_number,
893 /* Trash the modifiers state */
894 fgState.Modifiers = 0xffffffff;
901 FGCBKeyboard keyboard_cb;
902 FGCBSpecial special_cb;
907 /* Detect auto repeated keys, if configured globally or per-window */
909 if ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE )
911 if (event.type==KeyRelease)
914 * Look at X11 keystate to detect repeat mode.
915 * While X11 says the key is actually held down, we'll ignore KeyRelease/KeyPress pairs.
919 XQueryKeymap( fgDisplay.Display, keys ); /* Look at X11 keystate to detect repeat mode */
921 if ( event.xkey.keycode<256 ) /* XQueryKeymap is limited to 256 keycodes */
923 if ( keys[event.xkey.keycode>>3] & (1<<(event.xkey.keycode%8)) )
924 window->State.KeyRepeating = GL_TRUE;
926 window->State.KeyRepeating = GL_FALSE;
931 window->State.KeyRepeating = GL_FALSE;
933 /* Cease processing this event if it is auto repeated */
935 if (window->State.KeyRepeating)
938 if( event.type == KeyPress )
940 keyboard_cb = FETCH_WCB( *window, Keyboard );
941 special_cb = FETCH_WCB( *window, Special );
945 keyboard_cb = FETCH_WCB( *window, KeyboardUp );
946 special_cb = FETCH_WCB( *window, SpecialUp );
949 /* Is there a keyboard/special callback hooked for this window? */
950 if( keyboard_cb || special_cb )
952 XComposeStatus composeStatus;
953 char asciiCode[ 32 ];
957 /* Check for the ASCII/KeySym codes associated with the event: */
958 len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode),
959 &keySym, &composeStatus
962 /* GLUT API tells us to have two separate callbacks... */
965 /* ...one for the ASCII translateable keypresses... */
968 fgSetWindow( window );
969 fgState.Modifiers = fghGetXModifiers( &event );
970 keyboard_cb( asciiCode[ 0 ],
971 event.xkey.x, event.xkey.y
973 fgState.Modifiers = 0xffffffff;
981 * ...and one for all the others, which need to be
982 * translated to GLUT_KEY_Xs...
986 case XK_F1: special = GLUT_KEY_F1; break;
987 case XK_F2: special = GLUT_KEY_F2; break;
988 case XK_F3: special = GLUT_KEY_F3; break;
989 case XK_F4: special = GLUT_KEY_F4; break;
990 case XK_F5: special = GLUT_KEY_F5; break;
991 case XK_F6: special = GLUT_KEY_F6; break;
992 case XK_F7: special = GLUT_KEY_F7; break;
993 case XK_F8: special = GLUT_KEY_F8; break;
994 case XK_F9: special = GLUT_KEY_F9; break;
995 case XK_F10: special = GLUT_KEY_F10; break;
996 case XK_F11: special = GLUT_KEY_F11; break;
997 case XK_F12: special = GLUT_KEY_F12; break;
999 case XK_Left: special = GLUT_KEY_LEFT; break;
1000 case XK_Right: special = GLUT_KEY_RIGHT; break;
1001 case XK_Up: special = GLUT_KEY_UP; break;
1002 case XK_Down: special = GLUT_KEY_DOWN; break;
1005 case XK_Prior: special = GLUT_KEY_PAGE_UP; break;
1007 case XK_Next: special = GLUT_KEY_PAGE_DOWN; break;
1009 case XK_Home: special = GLUT_KEY_HOME; break;
1011 case XK_End: special = GLUT_KEY_END; break;
1013 case XK_Insert: special = GLUT_KEY_INSERT; break;
1017 * Execute the callback (if one has been specified),
1018 * given that the special code seems to be valid...
1020 if( special_cb && (special != -1) )
1022 fgSetWindow( window );
1023 fgState.Modifiers = fghGetXModifiers( &event );
1024 special_cb( special, event.xkey.x, event.xkey.y );
1025 fgState.Modifiers = 0xffffffff;
1032 case ReparentNotify:
1033 break; /* XXX Should disable this event */
1036 fgWarning ("Unknown X event type: %d", event.type);
1041 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
1045 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" );
1047 while( PeekMessage( &stMsg, NULL, 0, 0, PM_NOREMOVE ) )
1049 if( GetMessage( &stMsg, NULL, 0, 0 ) == 0 )
1051 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
1056 else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )
1057 fgState.ExecState = GLUT_EXEC_STATE_STOP;
1062 TranslateMessage( &stMsg );
1063 DispatchMessage( &stMsg );
1067 if( fgState.Timers.First )
1069 fghCheckJoystickPolls( );
1076 * Enters the freeglut processing loop.
1077 * Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".
1079 void FGAPIENTRY glutMainLoop( void )
1083 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
1084 SFG_Window *window = (SFG_Window *)fgStructure.Windows.First ;
1087 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoop" );
1089 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
1091 * Processing before the main loop: If there is a window which is open and
1092 * which has a visibility callback, call it. I know this is an ugly hack,
1093 * but I'm not sure what else to do about it. Ideally we should leave
1094 * something uninitialized in the create window code and initialize it in
1095 * the main loop, and have that initialization create a "WM_ACTIVATE"
1096 * message. Then we would put the visibility callback code in the
1097 * "case WM_ACTIVATE" block below. - John Fay -- 10/24/02
1101 if ( FETCH_WCB( *window, Visibility ) )
1103 SFG_Window *current_window = fgStructure.Window ;
1105 INVOKE_WCB( *window, Visibility, ( window->State.Visible ) );
1106 fgSetWindow( current_window );
1109 window = (SFG_Window *)window->Node.Next ;
1113 fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;
1114 while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )
1118 glutMainLoopEvent( );
1120 * Step through the list of windows, seeing if there are any
1121 * that are not menus
1123 for( window = ( SFG_Window * )fgStructure.Windows.First;
1125 window = ( SFG_Window * )window->Node.Next )
1126 if ( ! ( window->IsMenu ) )
1130 fgState.ExecState = GLUT_EXEC_STATE_STOP;
1133 if( fgState.IdleCallback )
1134 fgState.IdleCallback( );
1136 fghSleepForEvents( );
1141 * When this loop terminates, destroy the display, state and structure
1142 * of a freeglut session, so that another glutInit() call can happen
1144 * Save the "ActionOnWindowClose" because "fgDeinitialize" resets it.
1146 action = fgState.ActionOnWindowClose;
1148 if( action == GLUT_ACTION_EXIT )
1153 * Leaves the freeglut processing loop.
1155 void FGAPIENTRY glutLeaveMainLoop( void )
1157 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveMainLoop" );
1158 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
1162 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
1164 * Determine a GLUT modifer mask based on MS-WINDOWS system info.
1166 static int fghGetWin32Modifiers (void)
1169 ( ( ( GetKeyState( VK_LSHIFT ) < 0 ) ||
1170 ( GetKeyState( VK_RSHIFT ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1171 ( ( ( GetKeyState( VK_LCONTROL ) < 0 ) ||
1172 ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL : 0 ) |
1173 ( ( ( GetKeyState( VK_LMENU ) < 0 ) ||
1174 ( GetKeyState( VK_RMENU ) < 0 )) ? GLUT_ACTIVE_ALT : 0 );
1178 * The window procedure for handling Win32 events
1180 LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,
1187 FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Event Handler" ) ;
1189 window = fgWindowByHandle( hWnd );
1191 if ( ( window == NULL ) && ( uMsg != WM_CREATE ) )
1192 return DefWindowProc( hWnd, uMsg, wParam, lParam );
1194 /* printf ( "Window %3d message <%04x> %12d %12d\n", window?window->ID:0,
1195 uMsg, wParam, lParam ); */
1199 /* The window structure is passed as the creation structure paramter... */
1200 window = (SFG_Window *) (((LPCREATESTRUCT) lParam)->lpCreateParams);
1201 FREEGLUT_INTERNAL_ERROR_EXIT ( ( window != NULL ), "Cannot create window",
1204 window->Window.Handle = hWnd;
1205 window->Window.Device = GetDC( hWnd );
1206 if( window->IsMenu )
1208 unsigned int current_DisplayMode = fgState.DisplayMode;
1209 fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
1210 #if !TARGET_HOST_WINCE
1211 fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
1213 fgState.DisplayMode = current_DisplayMode;
1215 if( fgStructure.MenuContext )
1216 wglMakeCurrent( window->Window.Device,
1217 fgStructure.MenuContext->Context
1221 fgStructure.MenuContext =
1222 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
1223 fgStructure.MenuContext->Context =
1224 wglCreateContext( window->Window.Device );
1227 /* window->Window.Context = wglGetCurrentContext (); */
1228 window->Window.Context = wglCreateContext( window->Window.Device );
1232 #if !TARGET_HOST_WINCE
1233 fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
1236 if( ! fgState.UseCurrentContext )
1237 window->Window.Context =
1238 wglCreateContext( window->Window.Device );
1241 window->Window.Context = wglGetCurrentContext( );
1242 if( ! window->Window.Context )
1243 window->Window.Context =
1244 wglCreateContext( window->Window.Device );
1248 window->State.NeedToResize = GL_TRUE;
1249 window->State.Width = fgState.Size.X;
1250 window->State.Height = fgState.Size.Y;
1252 ReleaseDC( window->Window.Handle, window->Window.Device );
1254 #if TARGET_HOST_WINCE
1255 /* Take over button handling */
1257 HINSTANCE dxDllLib=LoadLibrary(_T("gx.dll"));
1260 GXGetDefaultKeys_=(GXGETDEFAULTKEYS)GetProcAddress(dxDllLib, _T("?GXGetDefaultKeys@@YA?AUGXKeyList@@H@Z"));
1261 GXOpenInput_=(GXOPENINPUT)GetProcAddress(dxDllLib, _T("?GXOpenInput@@YAHXZ"));
1266 if(GXGetDefaultKeys_)
1267 gxKeyList = (*GXGetDefaultKeys_)(GX_LANDSCAPEKEYS);
1270 #endif /* TARGET_HOST_WINCE */
1275 * If the window is visible, then it is the user manually resizing it.
1276 * If it is not, then it is the system sending us a dummy resize with
1277 * zero dimensions on a "glutIconifyWindow" call.
1279 if( window->State.Visible )
1281 window->State.NeedToResize = GL_TRUE;
1282 #if TARGET_HOST_WINCE
1283 window->State.Width = HIWORD(lParam);
1284 window->State.Height = LOWORD(lParam);
1286 window->State.Width = LOWORD(lParam);
1287 window->State.Height = HIWORD(lParam);
1288 #endif /* TARGET_HOST_WINCE */
1294 /* printf("WM_SETFOCUS: %p\n", window ); */
1295 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1299 if (LOWORD(wParam) != WA_INACTIVE)
1301 /* glutSetCursor( fgStructure.Window->State.Cursor ); */
1302 /* printf("WM_ACTIVATE: glutSetCursor( %p, %d)\n", window,
1303 window->State.Cursor ); */
1304 glutSetCursor( window->State.Cursor );
1307 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1312 * XXX Why not re-use some common code with the glutSetCursor()
1313 * XXX function (or perhaps invoke glutSetCursor())?
1314 * XXX That is, why are we duplicating code, here, from
1315 * XXX glutSetCursor()? The WIN32 code should be able to just
1316 * XXX call glutSetCursor() instead of defining two macros
1317 * XXX and implementing a nested case in-line.
1320 /* Set the cursor AND change it for this window class. */
1321 #define MAP_CURSOR(a,b) \
1323 SetCursor( LoadCursor( NULL, b ) ); \
1326 /* Nuke the cursor AND change it for this window class. */
1327 #define ZAP_CURSOR(a,b) \
1329 SetCursor( NULL ); \
1332 if( LOWORD( lParam ) == HTCLIENT )
1333 switch( window->State.Cursor )
1335 MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, IDC_ARROW );
1336 MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW, IDC_ARROW );
1337 MAP_CURSOR( GLUT_CURSOR_INFO, IDC_HELP );
1338 MAP_CURSOR( GLUT_CURSOR_DESTROY, IDC_CROSS );
1339 MAP_CURSOR( GLUT_CURSOR_HELP, IDC_HELP );
1340 MAP_CURSOR( GLUT_CURSOR_CYCLE, IDC_SIZEALL );
1341 MAP_CURSOR( GLUT_CURSOR_SPRAY, IDC_CROSS );
1342 MAP_CURSOR( GLUT_CURSOR_WAIT, IDC_WAIT );
1343 MAP_CURSOR( GLUT_CURSOR_TEXT, IDC_UPARROW );
1344 MAP_CURSOR( GLUT_CURSOR_CROSSHAIR, IDC_CROSS );
1345 /* MAP_CURSOR( GLUT_CURSOR_NONE, IDC_NO ); */
1346 ZAP_CURSOR( GLUT_CURSOR_NONE, NULL );
1349 MAP_CURSOR( GLUT_CURSOR_UP_DOWN, IDC_ARROW );
1352 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1356 window->State.Visible = GL_TRUE;
1357 window->State.Redisplay = GL_TRUE;
1361 /* Turn on the visibility in case it was turned off somehow */
1362 window->State.Visible = GL_TRUE;
1363 BeginPaint( hWnd, &ps );
1364 fghRedrawWindow( window );
1365 EndPaint( hWnd, &ps );
1369 fgDestroyWindow ( window );
1370 if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
1376 * The window already got destroyed, so don't bother with it.
1382 #if TARGET_HOST_WINCE
1383 window->State.MouseX = 320-HIWORD( lParam );
1384 window->State.MouseY = LOWORD( lParam );
1386 window->State.MouseX = LOWORD( lParam );
1387 window->State.MouseY = HIWORD( lParam );
1388 #endif /* TARGET_HOST_WINCE */
1389 /* Restrict to [-32768, 32767] to match X11 behaviour */
1390 /* See comment in "freeglut_developer" mailing list 10/4/04 */
1391 if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;
1392 if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;
1394 if ( window->ActiveMenu )
1396 window->State.Redisplay = GL_TRUE;
1397 fgSetWindow ( window->ActiveMenu->ParentWindow );
1401 fgState.Modifiers = fghGetWin32Modifiers( );
1403 if( ( wParam & MK_LBUTTON ) ||
1404 ( wParam & MK_MBUTTON ) ||
1405 ( wParam & MK_RBUTTON ) )
1406 INVOKE_WCB( *window, Motion, ( window->State.MouseX,
1407 window->State.MouseY ) );
1409 INVOKE_WCB( *window, Passive, ( window->State.MouseX,
1410 window->State.MouseY ) );
1412 fgState.Modifiers = 0xffffffff;
1416 case WM_LBUTTONDOWN:
1417 case WM_MBUTTONDOWN:
1418 case WM_RBUTTONDOWN:
1423 GLboolean pressed = GL_TRUE;
1426 #if TARGET_HOST_WINCE
1427 window->State.MouseX = 320-HIWORD( lParam );
1428 window->State.MouseY = LOWORD( lParam );
1430 window->State.MouseX = LOWORD( lParam );
1431 window->State.MouseY = HIWORD( lParam );
1432 #endif /* TARGET_HOST_WINCE */
1434 /* Restrict to [-32768, 32767] to match X11 behaviour */
1435 /* See comment in "freeglut_developer" mailing list 10/4/04 */
1436 if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;
1437 if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;
1441 case WM_LBUTTONDOWN:
1443 button = GLUT_LEFT_BUTTON;
1445 case WM_MBUTTONDOWN:
1447 button = GLUT_MIDDLE_BUTTON;
1449 case WM_RBUTTONDOWN:
1451 button = GLUT_RIGHT_BUTTON;
1455 button = GLUT_LEFT_BUTTON;
1459 button = GLUT_MIDDLE_BUTTON;
1463 button = GLUT_RIGHT_BUTTON;
1471 #if !TARGET_HOST_WINCE
1472 if( GetSystemMetrics( SM_SWAPBUTTON ) )
1474 if( button == GLUT_LEFT_BUTTON )
1475 button = GLUT_RIGHT_BUTTON;
1477 if( button == GLUT_RIGHT_BUTTON )
1478 button = GLUT_LEFT_BUTTON;
1480 #endif /* !TARGET_HOST_WINCE */
1483 return DefWindowProc( hWnd, uMsg, lParam, wParam );
1486 * XXX This comment is duplicated in two other spots.
1487 * XXX Can we centralize it?
1489 * Do not execute the application's mouse callback if a
1490 * menu is hooked to this button.
1491 * In that case an appropriate private call should be generated.
1492 * Near as I can tell, this is the menu behaviour:
1493 * - Down-click the menu button, menu not active: activate
1494 * the menu with its upper left-hand corner at the mouse location.
1495 * - Down-click any button outside the menu, menu active:
1496 * deactivate the menu
1497 * - Down-click any button inside the menu, menu active:
1498 * select the menu entry and deactivate the menu
1499 * - Up-click the menu button, menu not active: nothing happens
1500 * - Up-click the menu button outside the menu, menu active:
1502 * - Up-click the menu button inside the menu, menu active:
1503 * select the menu entry and deactivate the menu
1505 /* Window has an active menu, it absorbs any mouse click */
1506 if( window->ActiveMenu )
1508 /* Outside the menu, deactivate the menu if it's a downclick */
1509 if( ! fgCheckActiveMenu( window, window->ActiveMenu ) )
1512 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
1514 else /* In menu, invoke the callback and deactivate the menu*/
1517 * Save the current window and menu and set the current
1518 * window to the window whose menu this is
1520 SFG_Window *save_window = fgStructure.Window;
1521 SFG_Menu *save_menu = fgStructure.Menu;
1522 SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
1523 fgSetWindow( parent_window );
1524 fgStructure.Menu = window->ActiveMenu;
1526 /* Execute the menu callback */
1527 fgExecuteMenuCallback( window->ActiveMenu );
1528 fgDeactivateMenu( parent_window );
1530 /* Restore the current window and menu */
1531 fgSetWindow( save_window );
1532 fgStructure.Menu = save_menu;
1536 * Let's make the window redraw as a result of the mouse
1537 * click and menu activity.
1539 if( ! window->IsMenu )
1540 window->State.Redisplay = GL_TRUE;
1545 if( window->Menu[ button ] && pressed )
1547 window->State.Redisplay = GL_TRUE;
1548 fgSetWindow( window );
1549 fgActivateMenu( window, button );
1554 /* Set capture so that the window captures all the mouse messages */
1556 * XXX - Multiple button support: Under X11, the mouse is not released
1557 * XXX - from the window until all buttons have been released, even if the
1558 * XXX - user presses a button in another window. This will take more
1559 * XXX - code changes than I am up to at the moment (10/5/04). The present
1560 * XXX - is a 90 percent solution.
1562 if ( pressed == GL_TRUE )
1563 SetCapture ( window->Window.Handle ) ;
1567 if( ! FETCH_WCB( *window, Mouse ) )
1570 fgSetWindow( window );
1571 fgState.Modifiers = fghGetWin32Modifiers( );
1576 pressed ? GLUT_DOWN : GLUT_UP,
1577 window->State.MouseX,
1578 window->State.MouseY
1582 fgState.Modifiers = 0xffffffff;
1587 /* Should be WM_MOUSEWHEEL but my compiler doesn't recognize it */
1590 * XXX THIS IS SPECULATIVE -- John Fay, 10/2/03
1591 * XXX Should use WHEEL_DELTA instead of 120
1593 int wheel_number = LOWORD( wParam );
1594 short ticks = ( short )HIWORD( wParam ) / 120;
1604 * The mouse cursor has moved. Remember the new mouse cursor's position
1606 /* window->State.MouseX = LOWORD( lParam ); */
1607 /* Need to adjust by window position, */
1608 /* window->State.MouseY = HIWORD( lParam ); */
1609 /* change "lParam" to other parameter */
1611 if( ! FETCH_WCB( *window, MouseWheel ) &&
1612 ! FETCH_WCB( *window, Mouse ) )
1615 fgSetWindow( window );
1616 fgState.Modifiers = fghGetWin32Modifiers( );
1619 if( FETCH_WCB( *window, MouseWheel ) )
1620 INVOKE_WCB( *window, MouseWheel,
1623 window->State.MouseX,
1624 window->State.MouseY
1627 else /* No mouse wheel, call the mouse button callback twice */
1630 * XXX The below assumes that you have no more than 3 mouse
1631 * XXX buttons. Sorry.
1633 int button = wheel_number*2 + 4;
1636 INVOKE_WCB( *window, Mouse,
1637 ( button, GLUT_DOWN,
1638 window->State.MouseX, window->State.MouseY )
1640 INVOKE_WCB( *window, Mouse,
1642 window->State.MouseX, window->State.MouseX )
1646 fgState.Modifiers = 0xffffffff;
1656 if( ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE ) && (HIWORD(lParam) & KF_REPEAT) )
1660 * Remember the current modifiers state. This is done here in order
1661 * to make sure the VK_DELETE keyboard callback is executed properly.
1663 fgState.Modifiers = fghGetWin32Modifiers( );
1665 GetCursorPos( &mouse_pos );
1666 ScreenToClient( window->Window.Handle, &mouse_pos );
1668 window->State.MouseX = mouse_pos.x;
1669 window->State.MouseY = mouse_pos.y;
1671 /* Convert the Win32 keystroke codes to GLUTtish way */
1672 # define KEY(a,b) case a: keypress = b; break;
1676 KEY( VK_F1, GLUT_KEY_F1 );
1677 KEY( VK_F2, GLUT_KEY_F2 );
1678 KEY( VK_F3, GLUT_KEY_F3 );
1679 KEY( VK_F4, GLUT_KEY_F4 );
1680 KEY( VK_F5, GLUT_KEY_F5 );
1681 KEY( VK_F6, GLUT_KEY_F6 );
1682 KEY( VK_F7, GLUT_KEY_F7 );
1683 KEY( VK_F8, GLUT_KEY_F8 );
1684 KEY( VK_F9, GLUT_KEY_F9 );
1685 KEY( VK_F10, GLUT_KEY_F10 );
1686 KEY( VK_F11, GLUT_KEY_F11 );
1687 KEY( VK_F12, GLUT_KEY_F12 );
1688 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP );
1689 KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
1690 KEY( VK_HOME, GLUT_KEY_HOME );
1691 KEY( VK_END, GLUT_KEY_END );
1692 KEY( VK_LEFT, GLUT_KEY_LEFT );
1693 KEY( VK_UP, GLUT_KEY_UP );
1694 KEY( VK_RIGHT, GLUT_KEY_RIGHT );
1695 KEY( VK_DOWN, GLUT_KEY_DOWN );
1696 KEY( VK_INSERT, GLUT_KEY_INSERT );
1699 /* The delete key should be treated as an ASCII keypress: */
1700 INVOKE_WCB( *window, Keyboard,
1701 ( 127, window->State.MouseX, window->State.MouseY )
1705 #if TARGET_HOST_WINCE
1706 if(!(lParam & 0x40000000)) /* Prevent auto-repeat */
1708 if(wParam==(unsigned)gxKeyList.vkRight)
1709 keypress = GLUT_KEY_RIGHT;
1710 else if(wParam==(unsigned)gxKeyList.vkLeft)
1711 keypress = GLUT_KEY_LEFT;
1712 else if(wParam==(unsigned)gxKeyList.vkUp)
1713 keypress = GLUT_KEY_UP;
1714 else if(wParam==(unsigned)gxKeyList.vkDown)
1715 keypress = GLUT_KEY_DOWN;
1716 else if(wParam==(unsigned)gxKeyList.vkA)
1717 keypress = GLUT_KEY_F1;
1718 else if(wParam==(unsigned)gxKeyList.vkB)
1719 keypress = GLUT_KEY_F2;
1720 else if(wParam==(unsigned)gxKeyList.vkC)
1721 keypress = GLUT_KEY_F3;
1722 else if(wParam==(unsigned)gxKeyList.vkStart)
1723 keypress = GLUT_KEY_F4;
1727 if( keypress != -1 )
1728 INVOKE_WCB( *window, Special,
1730 window->State.MouseX, window->State.MouseY )
1733 fgState.Modifiers = 0xffffffff;
1744 * Remember the current modifiers state. This is done here in order
1745 * to make sure the VK_DELETE keyboard callback is executed properly.
1747 fgState.Modifiers = fghGetWin32Modifiers( );
1749 GetCursorPos( &mouse_pos );
1750 ScreenToClient( window->Window.Handle, &mouse_pos );
1752 window->State.MouseX = mouse_pos.x;
1753 window->State.MouseY = mouse_pos.y;
1756 * Convert the Win32 keystroke codes to GLUTtish way.
1757 * "KEY(a,b)" was defined under "WM_KEYDOWN"
1762 KEY( VK_F1, GLUT_KEY_F1 );
1763 KEY( VK_F2, GLUT_KEY_F2 );
1764 KEY( VK_F3, GLUT_KEY_F3 );
1765 KEY( VK_F4, GLUT_KEY_F4 );
1766 KEY( VK_F5, GLUT_KEY_F5 );
1767 KEY( VK_F6, GLUT_KEY_F6 );
1768 KEY( VK_F7, GLUT_KEY_F7 );
1769 KEY( VK_F8, GLUT_KEY_F8 );
1770 KEY( VK_F9, GLUT_KEY_F9 );
1771 KEY( VK_F10, GLUT_KEY_F10 );
1772 KEY( VK_F11, GLUT_KEY_F11 );
1773 KEY( VK_F12, GLUT_KEY_F12 );
1774 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP );
1775 KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
1776 KEY( VK_HOME, GLUT_KEY_HOME );
1777 KEY( VK_END, GLUT_KEY_END );
1778 KEY( VK_LEFT, GLUT_KEY_LEFT );
1779 KEY( VK_UP, GLUT_KEY_UP );
1780 KEY( VK_RIGHT, GLUT_KEY_RIGHT );
1781 KEY( VK_DOWN, GLUT_KEY_DOWN );
1782 KEY( VK_INSERT, GLUT_KEY_INSERT );
1785 /* The delete key should be treated as an ASCII keypress: */
1786 INVOKE_WCB( *window, KeyboardUp,
1787 ( 127, window->State.MouseX, window->State.MouseY )
1793 #if !TARGET_HOST_WINCE
1797 GetKeyboardState( state );
1799 if( ToAscii( wParam, 0, state, code, 0 ) == 1 )
1802 INVOKE_WCB( *window, KeyboardUp,
1804 window->State.MouseX, window->State.MouseY )
1806 #endif /* !TARGET_HOST_WINCE */
1810 if( keypress != -1 )
1811 INVOKE_WCB( *window, SpecialUp,
1813 window->State.MouseX, window->State.MouseY )
1816 fgState.Modifiers = 0xffffffff;
1823 if( (fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE) && (HIWORD(lParam) & KF_REPEAT) )
1826 fgState.Modifiers = fghGetWin32Modifiers( );
1827 INVOKE_WCB( *window, Keyboard,
1829 window->State.MouseX, window->State.MouseY )
1831 fgState.Modifiers = 0xffffffff;
1835 case WM_CAPTURECHANGED:
1836 /* User has finished resizing the window, force a redraw */
1837 INVOKE_WCB( *window, Display, ( ) );
1839 /*lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ); */
1842 /* Other messages that I have seen and which are not handled already */
1843 case WM_SETTEXT: /* 0x000c */
1844 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1845 /* Pass it on to "DefWindowProc" to set the window text */
1848 case WM_GETTEXT: /* 0x000d */
1849 /* Ideally we would copy the title of the window into "lParam" */
1850 /* strncpy ( (char *)lParam, "Window Title", wParam );
1851 lRet = ( wParam > 12 ) ? 12 : wParam; */
1852 /* the number of characters copied */
1853 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1856 case WM_GETTEXTLENGTH: /* 0x000e */
1857 /* Ideally we would get the length of the title of the window */
1859 /* the number of characters in "Window Title\0" (see above) */
1862 case WM_ERASEBKGND: /* 0x0014 */
1863 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1866 #if !TARGET_HOST_WINCE
1867 case WM_SYNCPAINT: /* 0x0088 */
1868 /* Another window has moved, need to update this one */
1869 window->State.Redisplay = GL_TRUE;
1870 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1871 /* Help screen says this message must be passed to "DefWindowProc" */
1874 case WM_NCPAINT: /* 0x0085 */
1875 /* Need to update the border of this window */
1876 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1877 /* Pass it on to "DefWindowProc" to repaint a standard border */
1880 case WM_SYSCOMMAND : /* 0x0112 */
1883 * We have received a system command message. Try to act on it.
1884 * The commands are passed in through the "wParam" parameter:
1885 * The least significant digit seems to be which edge of the window
1886 * is being used for a resize event:
1890 * Congratulations and thanks to Richard Rauch for figuring this out..
1892 switch ( wParam & 0xfff0 )
1901 /* User has clicked on the "-" to minimize the window */
1902 /* Turn off the visibility */
1903 window->State.Visible = GL_FALSE ;
1910 case SC_NEXTWINDOW :
1913 case SC_PREVWINDOW :
1917 /* Followed very closely by a WM_CLOSE message */
1941 case SC_SCREENSAVE :
1949 fgWarning( "Unknown wParam type 0x%x", wParam );
1954 #endif /* !TARGET_HOST_WINCE */
1956 /* We need to pass the message on to the operating system as well */
1957 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1961 /* Handle unhandled messages */
1962 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1970 /*** END OF FILE ***/