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.
28 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
35 # define VFPRINTF(s,f,a) vfprintf((s),(f),(a))
37 # define VFPRINTF(s,f,a) _doprnt((f),(a),(s))
39 # define VFPRINTF(s,f,a)
44 typedef struct GXDisplayProperties GXDisplayProperties;
45 typedef struct GXKeyList GXKeyList;
48 typedef struct GXKeyList (*GXGETDEFAULTKEYS)(int);
49 typedef int (*GXOPENINPUT)();
51 GXGETDEFAULTKEYS GXGetDefaultKeys_ = NULL;
52 GXOPENINPUT GXOpenInput_ = NULL;
54 struct GXKeyList gxKeyList;
56 #endif /* _WIN32_WCE */
59 * Try to get the maximum value allowed for ints, falling back to the minimum
60 * guaranteed by ISO C99 if there is no suitable header.
66 # define INT_MAX 32767
70 # define MIN(a,b) (((a)<(b)) ? (a) : (b))
75 * TODO BEFORE THE STABLE RELEASE:
77 * There are some issues concerning window redrawing under X11, and maybe
78 * some events are not handled. The Win32 version lacks some more features,
79 * but seems acceptable for not demanding purposes.
81 * Need to investigate why the X11 version breaks out with an error when
82 * closing a window (using the window manager, not glutDestroyWindow)...
85 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
88 * Handle a window configuration change. When no reshape
89 * callback is hooked, the viewport size is updated to
90 * match the new window size.
92 static void fghReshapeWindow ( SFG_Window *window, int width, int height )
94 SFG_Window *current_window = fgStructure.CurrentWindow;
96 freeglut_return_if_fail( window != NULL );
99 #if TARGET_HOST_POSIX_X11
101 XResizeWindow( fgDisplay.Display, window->Window.Handle,
103 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
105 #elif TARGET_HOST_MS_WINDOWS && !defined(_WIN32_WCE)
111 * For windowed mode, get the current position of the
112 * window and resize taking the size of the frame
113 * decorations into account.
116 /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
117 GetWindowRect( window->Window.Handle, &winRect );
123 if ( window->Parent == NULL )
125 if ( ! window->IsMenu && (window != fgStructure.GameModeWindow) )
127 w += GetSystemMetrics( SM_CXSIZEFRAME ) * 2;
128 h += GetSystemMetrics( SM_CYSIZEFRAME ) * 2 +
129 GetSystemMetrics( SM_CYCAPTION );
135 GetWindowRect( window->Parent->Window.Handle, &parentRect );
136 x -= parentRect.left + GetSystemMetrics( SM_CXSIZEFRAME ) * 2;
137 y -= parentRect.top + GetSystemMetrics( SM_CYSIZEFRAME ) * 2 +
138 GetSystemMetrics( SM_CYCAPTION );
142 * SWP_NOACTIVATE Do not activate the window
143 * SWP_NOOWNERZORDER Do not change position in z-order
144 * SWP_NOSENDCHANGING Supress WM_WINDOWPOSCHANGING message
145 * SWP_NOZORDER Retains the current Z order (ignore 2nd param)
148 SetWindowPos( window->Window.Handle,
151 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
158 * XXX Should update {window->State.OldWidth, window->State.OldHeight}
159 * XXX to keep in lockstep with POSIX_X11 code.
161 if( FETCH_WCB( *window, Reshape ) )
162 INVOKE_WCB( *window, Reshape, ( width, height ) );
165 fgSetWindow( window );
166 glViewport( 0, 0, width, height );
170 * Force a window redraw. In Windows at least this is only a partial
171 * solution: if the window is increasing in size in either dimension,
172 * the already-drawn part does not get drawn again and things look funny.
173 * But without this we get this bad behaviour whenever we resize the
176 window->State.Redisplay = GL_TRUE;
179 fgSetWindow( current_window );
183 * Calls a window's redraw method. This is used when
184 * a redraw is forced by the incoming window messages.
186 static void fghRedrawWindow ( SFG_Window *window )
188 SFG_Window *current_window = fgStructure.CurrentWindow;
190 freeglut_return_if_fail( window );
191 freeglut_return_if_fail( FETCH_WCB ( *window, Display ) );
193 window->State.Redisplay = GL_FALSE;
195 freeglut_return_if_fail( window->State.Visible );
197 fgSetWindow( window );
199 if( window->State.NeedToResize )
207 window->State.NeedToResize = GL_FALSE;
210 INVOKE_WCB( *window, Display, ( ) );
212 fgSetWindow( current_window );
216 * A static helper function to execute display callback for a window
218 static void fghcbDisplayWindow( SFG_Window *window,
219 SFG_Enumerator *enumerator )
221 if( window->State.Redisplay &&
222 window->State.Visible )
224 window->State.Redisplay = GL_FALSE;
226 #if TARGET_HOST_POSIX_X11
227 fghRedrawWindow ( window ) ;
228 #elif TARGET_HOST_MS_WINDOWS
230 window->Window.Handle, NULL, NULL,
231 RDW_NOERASE | RDW_INTERNALPAINT | RDW_INVALIDATE | RDW_UPDATENOW
236 fgEnumSubWindows( window, fghcbDisplayWindow, enumerator );
240 * Make all windows perform a display call
242 static void fghDisplayAll( void )
244 SFG_Enumerator enumerator;
246 enumerator.found = GL_FALSE;
247 enumerator.data = NULL;
249 fgEnumWindows( fghcbDisplayWindow, &enumerator );
253 * Window enumerator callback to check for the joystick polling code
255 static void fghcbCheckJoystickPolls( SFG_Window *window,
256 SFG_Enumerator *enumerator )
258 long int checkTime = fgElapsedTime( );
260 if( window->State.JoystickLastPoll + window->State.JoystickPollRate <=
263 #if !defined(_WIN32_WCE)
264 fgJoystickPollWindow( window );
265 #endif /* !defined(_WIN32_WCE) */
266 window->State.JoystickLastPoll = checkTime;
269 fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );
273 * Check all windows for joystick polling
275 static void fghCheckJoystickPolls( void )
277 SFG_Enumerator enumerator;
279 enumerator.found = GL_FALSE;
280 enumerator.data = NULL;
282 fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );
286 * Check the global timers
288 static void fghCheckTimers( void )
290 long checkTime = fgElapsedTime( );
292 while( fgState.Timers.First )
294 SFG_Timer *timer = fgState.Timers.First;
296 if( timer->TriggerTime > checkTime )
299 fgListRemove( &fgState.Timers, &timer->Node );
300 fgListAppend( &fgState.FreeTimers, &timer->Node );
302 timer->Callback( timer->ID );
307 /* Platform-dependent time in milliseconds, as an unsigned 32-bit integer.
308 * This value wraps every 49.7 days, but integer overflows cancel
309 * when subtracting an initial start time, unless the total time exceeds
310 * 32-bit, where the GLUT API return value is also overflowed.
312 unsigned long fgSystemTime(void) {
313 #if TARGET_HOST_SOLARIS || HAVE_GETTIMEOFDAY
315 gettimeofday( &now, NULL );
316 return now.tv_usec/1000 + now.tv_sec*1000;
317 #elif TARGET_HOST_MS_WINDOWS
318 # if defined(_WIN32_WCE)
319 return GetTickCount();
321 return timeGetTime();
329 long fgElapsedTime( void )
331 return (long) (fgSystemTime() - fgState.Time);
337 void fgError( const char *fmt, ... )
343 fprintf( stderr, "freeglut ");
344 if( fgState.ProgramName )
345 fprintf( stderr, "(%s): ", fgState.ProgramName );
346 VFPRINTF( stderr, fmt, ap );
347 fprintf( stderr, "\n" );
351 if ( fgState.Initialised )
357 void fgWarning( const char *fmt, ... )
363 fprintf( stderr, "freeglut ");
364 if( fgState.ProgramName )
365 fprintf( stderr, "(%s): ", fgState.ProgramName );
366 VFPRINTF( stderr, fmt, ap );
367 fprintf( stderr, "\n" );
373 * Indicates whether Joystick events are being used by ANY window.
375 * The current mechanism is to walk all of the windows and ask if
376 * there is a joystick callback. We have a short-circuit early
377 * return if we find any joystick handler registered.
379 * The real way to do this is to make use of the glutTimer() API
380 * to more cleanly re-implement the joystick API. Then, this code
381 * and all other "joystick timer" code can be yanked.
384 static void fghCheckJoystickCallback( SFG_Window* w, SFG_Enumerator* e)
386 if( FETCH_WCB( *w, Joystick ) )
391 fgEnumSubWindows( w, fghCheckJoystickCallback, e );
393 static int fghHaveJoystick( void )
395 SFG_Enumerator enumerator;
397 enumerator.found = GL_FALSE;
398 enumerator.data = NULL;
399 fgEnumWindows( fghCheckJoystickCallback, &enumerator );
400 return !!enumerator.data;
402 static void fghHavePendingRedisplaysCallback( SFG_Window* w, SFG_Enumerator* e)
404 if( w->State.Redisplay && w->State.Visible )
409 fgEnumSubWindows( w, fghHavePendingRedisplaysCallback, e );
411 static int fghHavePendingRedisplays (void)
413 SFG_Enumerator enumerator;
415 enumerator.found = GL_FALSE;
416 enumerator.data = NULL;
417 fgEnumWindows( fghHavePendingRedisplaysCallback, &enumerator );
418 return !!enumerator.data;
421 * Returns the number of GLUT ticks (milliseconds) till the next timer event.
423 static long fghNextTimer( void )
426 SFG_Timer *timer = fgState.Timers.First;
429 ret = timer->TriggerTime - fgElapsedTime();
436 * Does the magic required to relinquish the CPU until something interesting
439 static void fghSleepForEvents( void )
443 if( fgState.IdleCallback || fghHavePendingRedisplays( ) )
446 msec = fghNextTimer( );
447 /* XXX Use GLUT timers for joysticks... */
448 /* XXX Dumb; forces granularity to .01sec */
449 if( fghHaveJoystick( ) && ( msec > 10 ) )
452 #if TARGET_HOST_POSIX_X11
454 * Possibly due to aggressive use of XFlush() and friends,
455 * it is possible to have our socket drained but still have
456 * unprocessed events. (Or, this may just be normal with
457 * X, anyway?) We do non-trivial processing of X events
458 * after the event-reading loop, in any case, so we
459 * need to allow that we may have an empty socket but non-
462 if( ! XPending( fgDisplay.Display ) )
469 socket = ConnectionNumber( fgDisplay.Display );
471 FD_SET( socket, &fdset );
472 wait.tv_sec = msec / 1000;
473 wait.tv_usec = (msec % 1000) * 1000;
474 err = select( socket+1, &fdset, NULL, NULL, &wait );
477 if( ( -1 == err ) && ( errno != EINTR ) )
478 fgWarning ( "freeglut select() error: %d", errno );
481 #elif TARGET_HOST_MS_WINDOWS
482 MsgWaitForMultipleObjects( 0, NULL, FALSE, msec, QS_ALLINPUT );
486 #if TARGET_HOST_POSIX_X11
488 * Returns GLUT modifier mask for the state field of an X11 event.
490 static int fghGetXModifiers( int state )
494 if( state & ( ShiftMask | LockMask ) )
495 ret |= GLUT_ACTIVE_SHIFT;
496 if( state & ControlMask )
497 ret |= GLUT_ACTIVE_CTRL;
498 if( state & Mod1Mask )
499 ret |= GLUT_ACTIVE_ALT;
506 #if TARGET_HOST_POSIX_X11 && _DEBUG
508 static const char* fghTypeToString( int type )
511 case KeyPress: return "KeyPress";
512 case KeyRelease: return "KeyRelease";
513 case ButtonPress: return "ButtonPress";
514 case ButtonRelease: return "ButtonRelease";
515 case MotionNotify: return "MotionNotify";
516 case EnterNotify: return "EnterNotify";
517 case LeaveNotify: return "LeaveNotify";
518 case FocusIn: return "FocusIn";
519 case FocusOut: return "FocusOut";
520 case KeymapNotify: return "KeymapNotify";
521 case Expose: return "Expose";
522 case GraphicsExpose: return "GraphicsExpose";
523 case NoExpose: return "NoExpose";
524 case VisibilityNotify: return "VisibilityNotify";
525 case CreateNotify: return "CreateNotify";
526 case DestroyNotify: return "DestroyNotify";
527 case UnmapNotify: return "UnmapNotify";
528 case MapNotify: return "MapNotify";
529 case MapRequest: return "MapRequest";
530 case ReparentNotify: return "ReparentNotify";
531 case ConfigureNotify: return "ConfigureNotify";
532 case ConfigureRequest: return "ConfigureRequest";
533 case GravityNotify: return "GravityNotify";
534 case ResizeRequest: return "ResizeRequest";
535 case CirculateNotify: return "CirculateNotify";
536 case CirculateRequest: return "CirculateRequest";
537 case PropertyNotify: return "PropertyNotify";
538 case SelectionClear: return "SelectionClear";
539 case SelectionRequest: return "SelectionRequest";
540 case SelectionNotify: return "SelectionNotify";
541 case ColormapNotify: return "ColormapNotify";
542 case ClientMessage: return "ClientMessage";
543 case MappingNotify: return "MappingNotify";
544 default: return "UNKNOWN";
548 static const char* fghBoolToString( Bool b )
550 return b == False ? "False" : "True";
553 static const char* fghNotifyHintToString( char is_hint )
556 case NotifyNormal: return "NotifyNormal";
557 case NotifyHint: return "NotifyHint";
558 default: return "UNKNOWN";
562 static const char* fghNotifyModeToString( int mode )
565 case NotifyNormal: return "NotifyNormal";
566 case NotifyGrab: return "NotifyGrab";
567 case NotifyUngrab: return "NotifyUngrab";
568 case NotifyWhileGrabbed: return "NotifyWhileGrabbed";
569 default: return "UNKNOWN";
573 static const char* fghNotifyDetailToString( int detail )
576 case NotifyAncestor: return "NotifyAncestor";
577 case NotifyVirtual: return "NotifyVirtual";
578 case NotifyInferior: return "NotifyInferior";
579 case NotifyNonlinear: return "NotifyNonlinear";
580 case NotifyNonlinearVirtual: return "NotifyNonlinearVirtual";
581 case NotifyPointer: return "NotifyPointer";
582 case NotifyPointerRoot: return "NotifyPointerRoot";
583 case NotifyDetailNone: return "NotifyDetailNone";
584 default: return "UNKNOWN";
588 static const char* fghVisibilityToString( int state ) {
590 case VisibilityUnobscured: return "VisibilityUnobscured";
591 case VisibilityPartiallyObscured: return "VisibilityPartiallyObscured";
592 case VisibilityFullyObscured: return "VisibilityFullyObscured";
593 default: return "UNKNOWN";
597 static const char* fghConfigureDetailToString( int detail )
600 case Above: return "Above";
601 case Below: return "Below";
602 case TopIf: return "TopIf";
603 case BottomIf: return "BottomIf";
604 case Opposite: return "Opposite";
605 default: return "UNKNOWN";
609 static const char* fghPlaceToString( int place )
612 case PlaceOnTop: return "PlaceOnTop";
613 case PlaceOnBottom: return "PlaceOnBottom";
614 default: return "UNKNOWN";
618 static const char* fghMappingRequestToString( int request )
621 case MappingModifier: return "MappingModifier";
622 case MappingKeyboard: return "MappingKeyboard";
623 case MappingPointer: return "MappingPointer";
624 default: return "UNKNOWN";
628 static const char* fghPropertyStateToString( int state )
631 case PropertyNewValue: return "PropertyNewValue";
632 case PropertyDelete: return "PropertyDelete";
633 default: return "UNKNOWN";
637 static const char* fghColormapStateToString( int state )
640 case ColormapUninstalled: return "ColormapUninstalled";
641 case ColormapInstalled: return "ColormapInstalled";
642 default: return "UNKNOWN";
646 static void fghPrintEvent( XEvent *event )
648 switch( event->type ) {
652 XKeyEvent *e = &event->xkey;
653 fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
654 "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "
655 "keycode=%u, same_screen=%s", fghTypeToString( e->type ),
656 e->window, e->root, e->subwindow, (unsigned long)e->time,
657 e->x, e->y, e->x_root, e->y_root, e->state, e->keycode,
658 fghBoolToString( e->same_screen ) );
663 case ButtonRelease: {
664 XButtonEvent *e = &event->xbutton;
665 fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
666 "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "
667 "button=%u, same_screen=%d", fghTypeToString( e->type ),
668 e->window, e->root, e->subwindow, (unsigned long)e->time,
669 e->x, e->y, e->x_root, e->y_root, e->state, e->button,
670 fghBoolToString( e->same_screen ) );
675 XMotionEvent *e = &event->xmotion;
676 fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
677 "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "
678 "is_hint=%s, same_screen=%d", fghTypeToString( e->type ),
679 e->window, e->root, e->subwindow, (unsigned long)e->time,
680 e->x, e->y, e->x_root, e->y_root, e->state,
681 fghNotifyHintToString( e->is_hint ),
682 fghBoolToString( e->same_screen ) );
688 XCrossingEvent *e = &event->xcrossing;
689 fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
690 "(x,y)=(%d,%d), mode=%s, detail=%s, same_screen=%d, "
691 "focus=%d, state=0x%x", fghTypeToString( e->type ),
692 e->window, e->root, e->subwindow, (unsigned long)e->time,
693 e->x, e->y, fghNotifyModeToString( e->mode ),
694 fghNotifyDetailToString( e->detail ), (int)e->same_screen,
695 (int)e->focus, e->state );
701 XFocusChangeEvent *e = &event->xfocus;
702 fgWarning( "%s: window=0x%x, mode=%s, detail=%s",
703 fghTypeToString( e->type ), e->window,
704 fghNotifyModeToString( e->mode ),
705 fghNotifyDetailToString( e->detail ) );
710 XKeymapEvent *e = &event->xkeymap;
711 char buf[32 * 2 + 1];
713 for ( i = 0; i < 32; i++ ) {
714 snprintf( &buf[ i * 2 ], sizeof( buf ) - i * 2,
715 "%02x", e->key_vector[ i ] );
718 fgWarning( "%s: window=0x%x, %s", fghTypeToString( e->type ), e->window,
724 XExposeEvent *e = &event->xexpose;
725 fgWarning( "%s: window=0x%x, (x,y)=(%d,%d), (width,height)=(%d,%d), "
726 "count=%d", fghTypeToString( e->type ), e->window, e->x,
727 e->y, e->width, e->height, e->count );
731 case GraphicsExpose: {
732 XGraphicsExposeEvent *e = &event->xgraphicsexpose;
733 fgWarning( "%s: drawable=0x%x, (x,y)=(%d,%d), (width,height)=(%d,%d), "
734 "count=%d, (major_code,minor_code)=(%d,%d)",
735 fghTypeToString( e->type ), e->drawable, e->x, e->y,
736 e->width, e->height, e->count, e->major_code,
742 XNoExposeEvent *e = &event->xnoexpose;
743 fgWarning( "%s: drawable=0x%x, (major_code,minor_code)=(%d,%d)",
744 fghTypeToString( e->type ), e->drawable, e->major_code,
749 case VisibilityNotify: {
750 XVisibilityEvent *e = &event->xvisibility;
751 fgWarning( "%s: window=0x%x, state=%s", fghTypeToString( e->type ),
752 e->window, fghVisibilityToString( e->state) );
757 XCreateWindowEvent *e = &event->xcreatewindow;
758 fgWarning( "%s: (x,y)=(%d,%d), (width,height)=(%d,%d), border_width=%d, "
759 "window=0x%x, override_redirect=%s",
760 fghTypeToString( e->type ), e->x, e->y, e->width, e->height,
761 e->border_width, e->window,
762 fghBoolToString( e->override_redirect ) );
766 case DestroyNotify: {
767 XDestroyWindowEvent *e = &event->xdestroywindow;
768 fgWarning( "%s: event=0x%x, window=0x%x",
769 fghTypeToString( e->type ), e->event, e->window );
774 XUnmapEvent *e = &event->xunmap;
775 fgWarning( "%s: event=0x%x, window=0x%x, from_configure=%s",
776 fghTypeToString( e->type ), e->event, e->window,
777 fghBoolToString( e->from_configure ) );
782 XMapEvent *e = &event->xmap;
783 fgWarning( "%s: event=0x%x, window=0x%x, override_redirect=%s",
784 fghTypeToString( e->type ), e->event, e->window,
785 fghBoolToString( e->override_redirect ) );
790 XMapRequestEvent *e = &event->xmaprequest;
791 fgWarning( "%s: parent=0x%x, window=0x%x",
792 fghTypeToString( event->type ), e->parent, e->window );
796 case ReparentNotify: {
797 XReparentEvent *e = &event->xreparent;
798 fgWarning( "%s: event=0x%x, window=0x%x, parent=0x%x, (x,y)=(%d,%d), "
799 "override_redirect=%s", fghTypeToString( e->type ),
800 e->event, e->window, e->parent, e->x, e->y,
801 fghBoolToString( e->override_redirect ) );
805 case ConfigureNotify: {
806 XConfigureEvent *e = &event->xconfigure;
807 fgWarning( "%s: event=0x%x, window=0x%x, (x,y)=(%d,%d), "
808 "(width,height)=(%d,%d), border_width=%d, above=0x%x, "
809 "override_redirect=%s", fghTypeToString( e->type ), e->event,
810 e->window, e->x, e->y, e->width, e->height, e->border_width,
811 e->above, fghBoolToString( e->override_redirect ) );
815 case ConfigureRequest: {
816 XConfigureRequestEvent *e = &event->xconfigurerequest;
817 fgWarning( "%s: parent=0x%x, window=0x%x, (x,y)=(%d,%d), "
818 "(width,height)=(%d,%d), border_width=%d, above=0x%x, "
819 "detail=%s, value_mask=%lx", fghTypeToString( e->type ),
820 e->parent, e->window, e->x, e->y, e->width, e->height,
821 e->border_width, e->above,
822 fghConfigureDetailToString( e->detail ), e->value_mask );
826 case GravityNotify: {
827 XGravityEvent *e = &event->xgravity;
828 fgWarning( "%s: event=0x%x, window=0x%x, (x,y)=(%d,%d)",
829 fghTypeToString( e->type ), e->event, e->window, e->x, e->y );
833 case ResizeRequest: {
834 XResizeRequestEvent *e = &event->xresizerequest;
835 fgWarning( "%s: window=0x%x, (width,height)=(%d,%d)",
836 fghTypeToString( e->type ), e->window, e->width, e->height );
840 case CirculateNotify: {
841 XCirculateEvent *e = &event->xcirculate;
842 fgWarning( "%s: event=0x%x, window=0x%x, place=%s",
843 fghTypeToString( e->type ), e->event, e->window,
844 fghPlaceToString( e->place ) );
848 case CirculateRequest: {
849 XCirculateRequestEvent *e = &event->xcirculaterequest;
850 fgWarning( "%s: parent=0x%x, window=0x%x, place=%s",
851 fghTypeToString( e->type ), e->parent, e->window,
852 fghPlaceToString( e->place ) );
856 case PropertyNotify: {
857 XPropertyEvent *e = &event->xproperty;
858 fgWarning( "%s: window=0x%x, atom=%lu, time=%lu, state=%s",
859 fghTypeToString( e->type ), e->window,
860 (unsigned long)e->atom, (unsigned long)e->time,
861 fghPropertyStateToString( e->state ) );
865 case SelectionClear: {
866 XSelectionClearEvent *e = &event->xselectionclear;
867 fgWarning( "%s: window=0x%x, selection=%lu, time=%lu",
868 fghTypeToString( e->type ), e->window,
869 (unsigned long)e->selection, (unsigned long)e->time );
873 case SelectionRequest: {
874 XSelectionRequestEvent *e = &event->xselectionrequest;
875 fgWarning( "%s: owner=0x%x, requestor=0x%x, selection=0x%x, "
876 "target=0x%x, property=%lu, time=%lu",
877 fghTypeToString( e->type ), e->owner, e->requestor,
878 (unsigned long)e->selection, (unsigned long)e->target,
879 (unsigned long)e->property, (unsigned long)e->time );
883 case SelectionNotify: {
884 XSelectionEvent *e = &event->xselection;
885 fgWarning( "%s: requestor=0x%x, selection=0x%x, target=0x%x, "
886 "property=%lu, time=%lu", fghTypeToString( e->type ),
887 e->requestor, (unsigned long)e->selection,
888 (unsigned long)e->target, (unsigned long)e->property,
889 (unsigned long)e->time );
893 case ColormapNotify: {
894 XColormapEvent *e = &event->xcolormap;
895 fgWarning( "%s: window=0x%x, colormap=%lu, new=%s, state=%s",
896 fghTypeToString( e->type ), e->window,
897 (unsigned long)e->colormap, fghBoolToString( e->new ),
898 fghColormapStateToString( e->state ) );
902 case ClientMessage: {
903 XClientMessageEvent *e = &event->xclient;
906 char* end = buf + sizeof( buf );
908 switch( e->format ) {
910 for ( i = 0; i < 20; i++, p += 3 ) {
911 snprintf( p, end - p, " %02x", e->data.b[ i ] );
915 for ( i = 0; i < 10; i++, p += 5 ) {
916 snprintf( p, end - p, " %04x", e->data.s[ i ] );
920 for ( i = 0; i < 5; i++, p += 9 ) {
921 snprintf( p, end - p, " %08lx", e->data.l[ i ] );
926 fgWarning( "%s: window=0x%x, message_type=%lu, format=%d, data=(%s )",
927 fghTypeToString( e->type ), e->window,
928 (unsigned long)e->message_type, e->format, buf );
932 case MappingNotify: {
933 XMappingEvent *e = &event->xmapping;
934 fgWarning( "%s: window=0x%x, request=%s, first_keycode=%d, count=%d",
935 fghTypeToString( e->type ), e->window,
936 fghMappingRequestToString( e->request ), e->first_keycode,
942 fgWarning( "%s", fghTypeToString( event->type ) );
950 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
953 * Executes a single iteration in the freeglut processing loop.
955 void FGAPIENTRY glutMainLoopEvent( void )
957 #if TARGET_HOST_POSIX_X11
961 /* This code was repeated constantly, so here it goes into a definition: */
962 #define GETWINDOW(a) \
963 window = fgWindowByHandle( event.a.window ); \
964 if( window == NULL ) \
967 #define GETMOUSE(a) \
968 window->State.MouseX = event.a.x; \
969 window->State.MouseY = event.a.y;
971 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" );
973 while( XPending( fgDisplay.Display ) )
975 XNextEvent( fgDisplay.Display, &event );
977 fghPrintEvent( &event );
983 if(fgIsSpaceballXEvent(&event)) {
984 fgSpaceballHandleXEvent(&event);
987 /* Destroy the window when the WM_DELETE_WINDOW message arrives */
988 if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.DeleteWindow )
990 GETWINDOW( xclient );
992 fgDestroyWindow ( window );
994 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
999 else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )
1000 fgState.ExecState = GLUT_EXEC_STATE_STOP;
1007 * CreateNotify causes a configure-event so that sub-windows are
1008 * handled compatibly with GLUT. Otherwise, your sub-windows
1009 * (in freeglut only) will not get an initial reshape event,
1010 * which can break things.
1012 * GLUT presumably does this because it generally tries to treat
1013 * sub-windows the same as windows.
1016 case ConfigureNotify:
1019 if( event.type == CreateNotify ) {
1020 GETWINDOW( xcreatewindow );
1021 width = event.xcreatewindow.width;
1022 height = event.xcreatewindow.height;
1024 GETWINDOW( xconfigure );
1025 width = event.xconfigure.width;
1026 height = event.xconfigure.height;
1029 if( ( width != window->State.OldWidth ) ||
1030 ( height != window->State.OldHeight ) )
1032 SFG_Window *current_window = fgStructure.CurrentWindow;
1034 window->State.OldWidth = width;
1035 window->State.OldHeight = height;
1036 if( FETCH_WCB( *window, Reshape ) )
1037 INVOKE_WCB( *window, Reshape, ( width, height ) );
1040 fgSetWindow( window );
1041 glViewport( 0, 0, width, height );
1043 glutPostRedisplay( );
1044 if( window->IsMenu )
1045 fgSetWindow( current_window );
1052 * This is sent to confirm the XDestroyWindow call.
1054 * XXX WHY is this commented out? Should we re-enable it?
1056 /* fgAddToWindowDestroyList ( window ); */
1061 * We are too dumb to process partial exposes...
1063 * XXX Well, we could do it. However, it seems to only
1064 * XXX be potentially useful for single-buffered (since
1065 * XXX double-buffered does not respect viewport when we
1066 * XXX do a buffer-swap).
1069 if( event.xexpose.count == 0 )
1071 GETWINDOW( xexpose );
1072 window->State.Redisplay = GL_TRUE;
1080 /* We get this when iconifying a window. */
1081 GETWINDOW( xunmap );
1082 INVOKE_WCB( *window, WindowStatus, ( GLUT_HIDDEN ) );
1083 window->State.Visible = GL_FALSE;
1088 * Have the client's keyboard knowledge updated (xlib.ps,
1089 * page 206, says that's a good thing to do)
1091 XRefreshKeyboardMapping( (XMappingEvent *) &event );
1094 case VisibilityNotify:
1097 * Sending this event, the X server can notify us that the window
1098 * has just acquired one of the three possible visibility states:
1099 * VisibilityUnobscured, VisibilityPartiallyObscured or
1100 * VisibilityFullyObscured. Note that we DO NOT receive a
1101 * VisibilityNotify event when iconifying a window, we only get an
1104 GETWINDOW( xvisibility );
1105 switch( event.xvisibility.state )
1107 case VisibilityUnobscured:
1108 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_RETAINED ) );
1109 window->State.Visible = GL_TRUE;
1112 case VisibilityPartiallyObscured:
1113 INVOKE_WCB( *window, WindowStatus,
1114 ( GLUT_PARTIALLY_RETAINED ) );
1115 window->State.Visible = GL_TRUE;
1118 case VisibilityFullyObscured:
1119 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_COVERED ) );
1120 window->State.Visible = GL_FALSE;
1124 fgWarning( "Unknown X visibility state: %d",
1125 event.xvisibility.state );
1133 GETWINDOW( xcrossing );
1134 GETMOUSE( xcrossing );
1135 if( ( event.type == LeaveNotify ) && window->IsMenu &&
1136 window->ActiveMenu && window->ActiveMenu->IsActive )
1137 fgUpdateMenuHighlight( window->ActiveMenu );
1139 INVOKE_WCB( *window, Entry, ( ( EnterNotify == event.type ) ?
1146 GETWINDOW( xmotion );
1147 GETMOUSE( xmotion );
1149 if( window->ActiveMenu )
1151 if( window == window->ActiveMenu->ParentWindow )
1153 window->ActiveMenu->Window->State.MouseX =
1154 event.xmotion.x_root - window->ActiveMenu->X;
1155 window->ActiveMenu->Window->State.MouseY =
1156 event.xmotion.y_root - window->ActiveMenu->Y;
1159 fgUpdateMenuHighlight( window->ActiveMenu );
1165 * XXX For more than 5 buttons, just check {event.xmotion.state},
1166 * XXX rather than a host of bit-masks? Or maybe we need to
1167 * XXX track ButtonPress/ButtonRelease events in our own
1170 fgState.Modifiers = fghGetXModifiers( event.xmotion.state );
1171 if ( event.xmotion.state & ( Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask ) ) {
1172 INVOKE_WCB( *window, Motion, ( event.xmotion.x,
1173 event.xmotion.y ) );
1175 INVOKE_WCB( *window, Passive, ( event.xmotion.x,
1176 event.xmotion.y ) );
1178 fgState.Modifiers = INVALID_MODIFIERS;
1185 GLboolean pressed = GL_TRUE;
1188 if( event.type == ButtonRelease )
1189 pressed = GL_FALSE ;
1192 * A mouse button has been pressed or released. Traditionally,
1193 * break if the window was found within the freeglut structures.
1195 GETWINDOW( xbutton );
1196 GETMOUSE( xbutton );
1199 * An X button (at least in XFree86) is numbered from 1.
1200 * A GLUT button is numbered from 0.
1201 * Old GLUT passed through buttons other than just the first
1202 * three, though it only gave symbolic names and official
1203 * support to the first three.
1205 button = event.xbutton.button - 1;
1208 * Do not execute the application's mouse callback if a menu
1209 * is hooked to this button. In that case an appropriate
1210 * private call should be generated.
1212 if( fgCheckActiveMenu( window, button, pressed,
1213 event.xbutton.x_root, event.xbutton.y_root ) )
1217 * Check if there is a mouse or mouse wheel callback hooked to the
1220 if( ! FETCH_WCB( *window, Mouse ) &&
1221 ! FETCH_WCB( *window, MouseWheel ) )
1224 fgState.Modifiers = fghGetXModifiers( event.xbutton.state );
1226 /* Finally execute the mouse or mouse wheel callback */
1227 if( ( button < glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS ) ) || ( ! FETCH_WCB( *window, MouseWheel ) ) )
1228 INVOKE_WCB( *window, Mouse, ( button,
1229 pressed ? GLUT_DOWN : GLUT_UP,
1236 * Map 4 and 5 to wheel zero; EVEN to +1, ODD to -1
1237 * " 6 and 7 " " one; ...
1239 * XXX This *should* be behind some variables/macros,
1240 * XXX since the order and numbering isn't certain
1241 * XXX See XFree86 configuration docs (even back in the
1242 * XXX 3.x days, and especially with 4.x).
1244 * XXX Note that {button} has already been decremeted
1245 * XXX in mapping from X button numbering to GLUT.
1247 int wheel_number = (button - glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS )) / 2;
1253 INVOKE_WCB( *window, MouseWheel, ( wheel_number,
1259 fgState.Modifiers = INVALID_MODIFIERS;
1266 FGCBKeyboard keyboard_cb;
1267 FGCBSpecial special_cb;
1272 /* Detect auto repeated keys, if configured globally or per-window */
1274 if ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE )
1276 if (event.type==KeyRelease)
1279 * Look at X11 keystate to detect repeat mode.
1280 * While X11 says the key is actually held down, we'll ignore KeyRelease/KeyPress pairs.
1284 XQueryKeymap( fgDisplay.Display, keys ); /* Look at X11 keystate to detect repeat mode */
1286 if ( event.xkey.keycode<256 ) /* XQueryKeymap is limited to 256 keycodes */
1288 if ( keys[event.xkey.keycode>>3] & (1<<(event.xkey.keycode%8)) )
1289 window->State.KeyRepeating = GL_TRUE;
1291 window->State.KeyRepeating = GL_FALSE;
1296 window->State.KeyRepeating = GL_FALSE;
1298 /* Cease processing this event if it is auto repeated */
1300 if (window->State.KeyRepeating)
1302 if (event.type == KeyPress) window->State.KeyRepeating = GL_FALSE;
1306 if( event.type == KeyPress )
1308 keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, Keyboard ));
1309 special_cb = (FGCBSpecial) ( FETCH_WCB( *window, Special ));
1313 keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, KeyboardUp ));
1314 special_cb = (FGCBSpecial) ( FETCH_WCB( *window, SpecialUp ));
1317 /* Is there a keyboard/special callback hooked for this window? */
1318 if( keyboard_cb || special_cb )
1320 XComposeStatus composeStatus;
1321 char asciiCode[ 32 ];
1325 /* Check for the ASCII/KeySym codes associated with the event: */
1326 len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode),
1327 &keySym, &composeStatus
1330 /* GLUT API tells us to have two separate callbacks... */
1333 /* ...one for the ASCII translateable keypresses... */
1336 fgSetWindow( window );
1337 fgState.Modifiers = fghGetXModifiers( event.xkey.state );
1338 keyboard_cb( asciiCode[ 0 ],
1339 event.xkey.x, event.xkey.y
1341 fgState.Modifiers = INVALID_MODIFIERS;
1349 * ...and one for all the others, which need to be
1350 * translated to GLUT_KEY_Xs...
1354 case XK_F1: special = GLUT_KEY_F1; break;
1355 case XK_F2: special = GLUT_KEY_F2; break;
1356 case XK_F3: special = GLUT_KEY_F3; break;
1357 case XK_F4: special = GLUT_KEY_F4; break;
1358 case XK_F5: special = GLUT_KEY_F5; break;
1359 case XK_F6: special = GLUT_KEY_F6; break;
1360 case XK_F7: special = GLUT_KEY_F7; break;
1361 case XK_F8: special = GLUT_KEY_F8; break;
1362 case XK_F9: special = GLUT_KEY_F9; break;
1363 case XK_F10: special = GLUT_KEY_F10; break;
1364 case XK_F11: special = GLUT_KEY_F11; break;
1365 case XK_F12: special = GLUT_KEY_F12; break;
1368 case XK_Left: special = GLUT_KEY_LEFT; break;
1370 case XK_Right: special = GLUT_KEY_RIGHT; break;
1372 case XK_Up: special = GLUT_KEY_UP; break;
1374 case XK_Down: special = GLUT_KEY_DOWN; break;
1377 case XK_Prior: special = GLUT_KEY_PAGE_UP; break;
1379 case XK_Next: special = GLUT_KEY_PAGE_DOWN; break;
1381 case XK_Home: special = GLUT_KEY_HOME; break;
1383 case XK_End: special = GLUT_KEY_END; break;
1385 case XK_Insert: special = GLUT_KEY_INSERT; break;
1387 case XK_Num_Lock : special = GLUT_KEY_NUM_LOCK; break;
1388 case XK_KP_Begin : special = GLUT_KEY_BEGIN; break;
1389 case XK_KP_Delete: special = GLUT_KEY_DELETE; break;
1393 * Execute the callback (if one has been specified),
1394 * given that the special code seems to be valid...
1396 if( special_cb && (special != -1) )
1398 fgSetWindow( window );
1399 fgState.Modifiers = fghGetXModifiers( event.xkey.state );
1400 special_cb( special, event.xkey.x, event.xkey.y );
1401 fgState.Modifiers = INVALID_MODIFIERS;
1408 case ReparentNotify:
1409 break; /* XXX Should disable this event */
1416 fgWarning ("Unknown X event type: %d\n", event.type);
1421 #elif TARGET_HOST_MS_WINDOWS
1425 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" );
1427 while( PeekMessage( &stMsg, NULL, 0, 0, PM_NOREMOVE ) )
1429 if( GetMessage( &stMsg, NULL, 0, 0 ) == 0 )
1431 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
1436 else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )
1437 fgState.ExecState = GLUT_EXEC_STATE_STOP;
1442 TranslateMessage( &stMsg );
1443 DispatchMessage( &stMsg );
1447 if( fgState.Timers.First )
1449 fghCheckJoystickPolls( );
1456 * Enters the freeglut processing loop.
1457 * Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".
1459 void FGAPIENTRY glutMainLoop( void )
1463 #if TARGET_HOST_MS_WINDOWS
1464 SFG_Window *window = (SFG_Window *)fgStructure.Windows.First ;
1467 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoop" );
1469 #if TARGET_HOST_MS_WINDOWS
1471 * Processing before the main loop: If there is a window which is open and
1472 * which has a visibility callback, call it. I know this is an ugly hack,
1473 * but I'm not sure what else to do about it. Ideally we should leave
1474 * something uninitialized in the create window code and initialize it in
1475 * the main loop, and have that initialization create a "WM_ACTIVATE"
1476 * message. Then we would put the visibility callback code in the
1477 * "case WM_ACTIVATE" block below. - John Fay -- 10/24/02
1481 if ( FETCH_WCB( *window, Visibility ) )
1483 SFG_Window *current_window = fgStructure.CurrentWindow ;
1485 INVOKE_WCB( *window, Visibility, ( window->State.Visible ) );
1486 fgSetWindow( current_window );
1489 window = (SFG_Window *)window->Node.Next ;
1493 fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;
1494 while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )
1498 glutMainLoopEvent( );
1500 * Step through the list of windows, seeing if there are any
1501 * that are not menus
1503 for( window = ( SFG_Window * )fgStructure.Windows.First;
1505 window = ( SFG_Window * )window->Node.Next )
1506 if ( ! ( window->IsMenu ) )
1510 fgState.ExecState = GLUT_EXEC_STATE_STOP;
1513 if( fgState.IdleCallback )
1515 if( fgStructure.CurrentWindow &&
1516 fgStructure.CurrentWindow->IsMenu )
1518 fgSetWindow( window );
1519 fgState.IdleCallback( );
1522 fghSleepForEvents( );
1527 * When this loop terminates, destroy the display, state and structure
1528 * of a freeglut session, so that another glutInit() call can happen
1530 * Save the "ActionOnWindowClose" because "fgDeinitialize" resets it.
1532 action = fgState.ActionOnWindowClose;
1534 if( action == GLUT_ACTION_EXIT )
1539 * Leaves the freeglut processing loop.
1541 void FGAPIENTRY glutLeaveMainLoop( void )
1543 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveMainLoop" );
1544 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
1548 #if TARGET_HOST_MS_WINDOWS
1550 * Determine a GLUT modifer mask based on MS-WINDOWS system info.
1552 static int fghGetWin32Modifiers (void)
1555 ( ( ( GetKeyState( VK_LSHIFT ) < 0 ) ||
1556 ( GetKeyState( VK_RSHIFT ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1557 ( ( ( GetKeyState( VK_LCONTROL ) < 0 ) ||
1558 ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL : 0 ) |
1559 ( ( ( GetKeyState( VK_LMENU ) < 0 ) ||
1560 ( GetKeyState( VK_RMENU ) < 0 )) ? GLUT_ACTIVE_ALT : 0 );
1564 * The window procedure for handling Win32 events
1566 LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,
1573 FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Event Handler" ) ;
1575 window = fgWindowByHandle( hWnd );
1577 if ( ( window == NULL ) && ( uMsg != WM_CREATE ) )
1578 return DefWindowProc( hWnd, uMsg, wParam, lParam );
1580 /* printf ( "Window %3d message <%04x> %12d %12d\n", window?window->ID:0,
1581 uMsg, wParam, lParam ); */
1585 /* The window structure is passed as the creation structure paramter... */
1586 window = (SFG_Window *) (((LPCREATESTRUCT) lParam)->lpCreateParams);
1587 FREEGLUT_INTERNAL_ERROR_EXIT ( ( window != NULL ), "Cannot create window",
1590 window->Window.Handle = hWnd;
1591 window->Window.Device = GetDC( hWnd );
1592 if( window->IsMenu )
1594 unsigned int current_DisplayMode = fgState.DisplayMode;
1595 fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
1596 #if !defined(_WIN32_WCE)
1597 fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
1599 fgState.DisplayMode = current_DisplayMode;
1601 if( fgStructure.MenuContext )
1602 wglMakeCurrent( window->Window.Device,
1603 fgStructure.MenuContext->MContext
1607 fgStructure.MenuContext =
1608 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
1609 fgStructure.MenuContext->MContext =
1610 wglCreateContext( window->Window.Device );
1613 /* window->Window.Context = wglGetCurrentContext (); */
1614 window->Window.Context = wglCreateContext( window->Window.Device );
1618 #if !defined(_WIN32_WCE)
1619 fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
1622 if( ! fgState.UseCurrentContext )
1623 window->Window.Context =
1624 wglCreateContext( window->Window.Device );
1627 window->Window.Context = wglGetCurrentContext( );
1628 if( ! window->Window.Context )
1629 window->Window.Context =
1630 wglCreateContext( window->Window.Device );
1633 #if !defined(_WIN32_WCE)
1634 fgNewWGLCreateContext( window );
1638 window->State.NeedToResize = GL_TRUE;
1639 if( ( window->State.Width < 0 ) || ( window->State.Height < 0 ) )
1641 SFG_Window *current_window = fgStructure.CurrentWindow;
1643 fgSetWindow( window );
1644 window->State.Width = glutGet( GLUT_WINDOW_WIDTH );
1645 window->State.Height = glutGet( GLUT_WINDOW_HEIGHT );
1646 fgSetWindow( current_window );
1649 ReleaseDC( window->Window.Handle, window->Window.Device );
1651 #if defined(_WIN32_WCE)
1652 /* Take over button handling */
1654 HINSTANCE dxDllLib=LoadLibrary(_T("gx.dll"));
1657 GXGetDefaultKeys_=(GXGETDEFAULTKEYS)GetProcAddress(dxDllLib, _T("?GXGetDefaultKeys@@YA?AUGXKeyList@@H@Z"));
1658 GXOpenInput_=(GXOPENINPUT)GetProcAddress(dxDllLib, _T("?GXOpenInput@@YAHXZ"));
1663 if(GXGetDefaultKeys_)
1664 gxKeyList = (*GXGetDefaultKeys_)(GX_LANDSCAPEKEYS);
1667 #endif /* defined(_WIN32_WCE) */
1672 * If the window is visible, then it is the user manually resizing it.
1673 * If it is not, then it is the system sending us a dummy resize with
1674 * zero dimensions on a "glutIconifyWindow" call.
1676 if( window->State.Visible )
1678 window->State.NeedToResize = GL_TRUE;
1679 #if defined(_WIN32_WCE)
1680 window->State.Width = HIWORD(lParam);
1681 window->State.Height = LOWORD(lParam);
1683 window->State.Width = LOWORD(lParam);
1684 window->State.Height = HIWORD(lParam);
1685 #endif /* defined(_WIN32_WCE) */
1691 /* printf("WM_SETFOCUS: %p\n", window ); */
1692 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1693 INVOKE_WCB( *window, Entry, ( GLUT_ENTERED ) );
1697 /* printf("WM_KILLFOCUS: %p\n", window ); */
1698 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1699 INVOKE_WCB( *window, Entry, ( GLUT_LEFT ) );
1701 if( window->IsMenu &&
1702 window->ActiveMenu && window->ActiveMenu->IsActive )
1703 fgUpdateMenuHighlight( window->ActiveMenu );
1709 if (LOWORD(wParam) != WA_INACTIVE)
1711 /* printf("WM_ACTIVATE: fgSetCursor( %p, %d)\n", window,
1712 window->State.Cursor ); */
1713 fgSetCursor( window, window->State.Cursor );
1716 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1721 /* printf ( "Cursor event %x %x %x %x\n", window, window->State.Cursor, lParam, wParam ) ; */
1722 if( LOWORD( lParam ) == HTCLIENT )
1723 fgSetCursor ( window, window->State.Cursor ) ;
1725 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1729 window->State.Visible = GL_TRUE;
1730 window->State.Redisplay = GL_TRUE;
1734 /* Turn on the visibility in case it was turned off somehow */
1735 window->State.Visible = GL_TRUE;
1736 BeginPaint( hWnd, &ps );
1737 fghRedrawWindow( window );
1738 EndPaint( hWnd, &ps );
1742 fgDestroyWindow ( window );
1743 if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
1749 * The window already got destroyed, so don't bother with it.
1755 #if defined(_WIN32_WCE)
1756 window->State.MouseX = 320-HIWORD( lParam );
1757 window->State.MouseY = LOWORD( lParam );
1759 window->State.MouseX = LOWORD( lParam );
1760 window->State.MouseY = HIWORD( lParam );
1761 #endif /* defined(_WIN32_WCE) */
1762 /* Restrict to [-32768, 32767] to match X11 behaviour */
1763 /* See comment in "freeglut_developer" mailing list 10/4/04 */
1764 if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;
1765 if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;
1767 if ( window->ActiveMenu )
1769 fgUpdateMenuHighlight( window->ActiveMenu );
1773 fgState.Modifiers = fghGetWin32Modifiers( );
1775 if( ( wParam & MK_LBUTTON ) ||
1776 ( wParam & MK_MBUTTON ) ||
1777 ( wParam & MK_RBUTTON ) )
1778 INVOKE_WCB( *window, Motion, ( window->State.MouseX,
1779 window->State.MouseY ) );
1781 INVOKE_WCB( *window, Passive, ( window->State.MouseX,
1782 window->State.MouseY ) );
1784 fgState.Modifiers = INVALID_MODIFIERS;
1788 case WM_LBUTTONDOWN:
1789 case WM_MBUTTONDOWN:
1790 case WM_RBUTTONDOWN:
1795 GLboolean pressed = GL_TRUE;
1798 #if defined(_WIN32_WCE)
1799 window->State.MouseX = 320-HIWORD( lParam );
1800 window->State.MouseY = LOWORD( lParam );
1802 window->State.MouseX = LOWORD( lParam );
1803 window->State.MouseY = HIWORD( lParam );
1804 #endif /* defined(_WIN32_WCE) */
1806 /* Restrict to [-32768, 32767] to match X11 behaviour */
1807 /* See comment in "freeglut_developer" mailing list 10/4/04 */
1808 if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;
1809 if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;
1813 case WM_LBUTTONDOWN:
1815 button = GLUT_LEFT_BUTTON;
1817 case WM_MBUTTONDOWN:
1819 button = GLUT_MIDDLE_BUTTON;
1821 case WM_RBUTTONDOWN:
1823 button = GLUT_RIGHT_BUTTON;
1827 button = GLUT_LEFT_BUTTON;
1831 button = GLUT_MIDDLE_BUTTON;
1835 button = GLUT_RIGHT_BUTTON;
1843 #if !defined(_WIN32_WCE)
1844 if( GetSystemMetrics( SM_SWAPBUTTON ) )
1846 if( button == GLUT_LEFT_BUTTON )
1847 button = GLUT_RIGHT_BUTTON;
1849 if( button == GLUT_RIGHT_BUTTON )
1850 button = GLUT_LEFT_BUTTON;
1852 #endif /* !defined(_WIN32_WCE) */
1855 return DefWindowProc( hWnd, uMsg, lParam, wParam );
1858 * Do not execute the application's mouse callback if a menu
1859 * is hooked to this button. In that case an appropriate
1860 * private call should be generated.
1862 if( fgCheckActiveMenu( window, button, pressed,
1863 window->State.MouseX, window->State.MouseY ) )
1866 /* Set capture so that the window captures all the mouse messages */
1868 * XXX - Multiple button support: Under X11, the mouse is not released
1869 * XXX - from the window until all buttons have been released, even if the
1870 * XXX - user presses a button in another window. This will take more
1871 * XXX - code changes than I am up to at the moment (10/5/04). The present
1872 * XXX - is a 90 percent solution.
1874 if ( pressed == GL_TRUE )
1875 SetCapture ( window->Window.Handle ) ;
1879 if( ! FETCH_WCB( *window, Mouse ) )
1882 fgSetWindow( window );
1883 fgState.Modifiers = fghGetWin32Modifiers( );
1888 pressed ? GLUT_DOWN : GLUT_UP,
1889 window->State.MouseX,
1890 window->State.MouseY
1894 fgState.Modifiers = INVALID_MODIFIERS;
1899 /* Should be WM_MOUSEWHEEL but my compiler doesn't recognize it */
1902 * XXX THIS IS SPECULATIVE -- John Fay, 10/2/03
1903 * XXX Should use WHEEL_DELTA instead of 120
1905 int wheel_number = LOWORD( wParam );
1906 short ticks = ( short )HIWORD( wParam ) / 120;
1916 * The mouse cursor has moved. Remember the new mouse cursor's position
1918 /* window->State.MouseX = LOWORD( lParam ); */
1919 /* Need to adjust by window position, */
1920 /* window->State.MouseY = HIWORD( lParam ); */
1921 /* change "lParam" to other parameter */
1923 if( ! FETCH_WCB( *window, MouseWheel ) &&
1924 ! FETCH_WCB( *window, Mouse ) )
1927 fgSetWindow( window );
1928 fgState.Modifiers = fghGetWin32Modifiers( );
1931 if( FETCH_WCB( *window, MouseWheel ) )
1932 INVOKE_WCB( *window, MouseWheel,
1935 window->State.MouseX,
1936 window->State.MouseY
1939 else /* No mouse wheel, call the mouse button callback twice */
1942 * Map wheel zero to button 3 and 4; +1 to 3, -1 to 4
1943 * " " one +1 to 5, -1 to 6, ...
1945 * XXX The below assumes that you have no more than 3 mouse
1946 * XXX buttons. Sorry.
1948 int button = wheel_number * 2 + 3;
1951 INVOKE_WCB( *window, Mouse,
1952 ( button, GLUT_DOWN,
1953 window->State.MouseX, window->State.MouseY )
1955 INVOKE_WCB( *window, Mouse,
1957 window->State.MouseX, window->State.MouseY )
1961 fgState.Modifiers = INVALID_MODIFIERS;
1971 if( ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE ) && (HIWORD(lParam) & KF_REPEAT) )
1975 * Remember the current modifiers state. This is done here in order
1976 * to make sure the VK_DELETE keyboard callback is executed properly.
1978 fgState.Modifiers = fghGetWin32Modifiers( );
1980 GetCursorPos( &mouse_pos );
1981 ScreenToClient( window->Window.Handle, &mouse_pos );
1983 window->State.MouseX = mouse_pos.x;
1984 window->State.MouseY = mouse_pos.y;
1986 /* Convert the Win32 keystroke codes to GLUTtish way */
1987 # define KEY(a,b) case a: keypress = b; break;
1991 KEY( VK_F1, GLUT_KEY_F1 );
1992 KEY( VK_F2, GLUT_KEY_F2 );
1993 KEY( VK_F3, GLUT_KEY_F3 );
1994 KEY( VK_F4, GLUT_KEY_F4 );
1995 KEY( VK_F5, GLUT_KEY_F5 );
1996 KEY( VK_F6, GLUT_KEY_F6 );
1997 KEY( VK_F7, GLUT_KEY_F7 );
1998 KEY( VK_F8, GLUT_KEY_F8 );
1999 KEY( VK_F9, GLUT_KEY_F9 );
2000 KEY( VK_F10, GLUT_KEY_F10 );
2001 KEY( VK_F11, GLUT_KEY_F11 );
2002 KEY( VK_F12, GLUT_KEY_F12 );
2003 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP );
2004 KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
2005 KEY( VK_HOME, GLUT_KEY_HOME );
2006 KEY( VK_END, GLUT_KEY_END );
2007 KEY( VK_LEFT, GLUT_KEY_LEFT );
2008 KEY( VK_UP, GLUT_KEY_UP );
2009 KEY( VK_RIGHT, GLUT_KEY_RIGHT );
2010 KEY( VK_DOWN, GLUT_KEY_DOWN );
2011 KEY( VK_INSERT, GLUT_KEY_INSERT );
2014 /* The delete key should be treated as an ASCII keypress: */
2015 INVOKE_WCB( *window, Keyboard,
2016 ( 127, window->State.MouseX, window->State.MouseY )
2020 #if defined(_WIN32_WCE)
2021 if(!(lParam & 0x40000000)) /* Prevent auto-repeat */
2023 if(wParam==(unsigned)gxKeyList.vkRight)
2024 keypress = GLUT_KEY_RIGHT;
2025 else if(wParam==(unsigned)gxKeyList.vkLeft)
2026 keypress = GLUT_KEY_LEFT;
2027 else if(wParam==(unsigned)gxKeyList.vkUp)
2028 keypress = GLUT_KEY_UP;
2029 else if(wParam==(unsigned)gxKeyList.vkDown)
2030 keypress = GLUT_KEY_DOWN;
2031 else if(wParam==(unsigned)gxKeyList.vkA)
2032 keypress = GLUT_KEY_F1;
2033 else if(wParam==(unsigned)gxKeyList.vkB)
2034 keypress = GLUT_KEY_F2;
2035 else if(wParam==(unsigned)gxKeyList.vkC)
2036 keypress = GLUT_KEY_F3;
2037 else if(wParam==(unsigned)gxKeyList.vkStart)
2038 keypress = GLUT_KEY_F4;
2042 if( keypress != -1 )
2043 INVOKE_WCB( *window, Special,
2045 window->State.MouseX, window->State.MouseY )
2048 fgState.Modifiers = INVALID_MODIFIERS;
2059 * Remember the current modifiers state. This is done here in order
2060 * to make sure the VK_DELETE keyboard callback is executed properly.
2062 fgState.Modifiers = fghGetWin32Modifiers( );
2064 GetCursorPos( &mouse_pos );
2065 ScreenToClient( window->Window.Handle, &mouse_pos );
2067 window->State.MouseX = mouse_pos.x;
2068 window->State.MouseY = mouse_pos.y;
2071 * Convert the Win32 keystroke codes to GLUTtish way.
2072 * "KEY(a,b)" was defined under "WM_KEYDOWN"
2077 KEY( VK_F1, GLUT_KEY_F1 );
2078 KEY( VK_F2, GLUT_KEY_F2 );
2079 KEY( VK_F3, GLUT_KEY_F3 );
2080 KEY( VK_F4, GLUT_KEY_F4 );
2081 KEY( VK_F5, GLUT_KEY_F5 );
2082 KEY( VK_F6, GLUT_KEY_F6 );
2083 KEY( VK_F7, GLUT_KEY_F7 );
2084 KEY( VK_F8, GLUT_KEY_F8 );
2085 KEY( VK_F9, GLUT_KEY_F9 );
2086 KEY( VK_F10, GLUT_KEY_F10 );
2087 KEY( VK_F11, GLUT_KEY_F11 );
2088 KEY( VK_F12, GLUT_KEY_F12 );
2089 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP );
2090 KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
2091 KEY( VK_HOME, GLUT_KEY_HOME );
2092 KEY( VK_END, GLUT_KEY_END );
2093 KEY( VK_LEFT, GLUT_KEY_LEFT );
2094 KEY( VK_UP, GLUT_KEY_UP );
2095 KEY( VK_RIGHT, GLUT_KEY_RIGHT );
2096 KEY( VK_DOWN, GLUT_KEY_DOWN );
2097 KEY( VK_INSERT, GLUT_KEY_INSERT );
2100 /* The delete key should be treated as an ASCII keypress: */
2101 INVOKE_WCB( *window, KeyboardUp,
2102 ( 127, window->State.MouseX, window->State.MouseY )
2108 #if !defined(_WIN32_WCE)
2112 GetKeyboardState( state );
2114 if( ToAscii( (UINT)wParam, 0, state, code, 0 ) == 1 )
2117 INVOKE_WCB( *window, KeyboardUp,
2119 window->State.MouseX, window->State.MouseY )
2121 #endif /* !defined(_WIN32_WCE) */
2125 if( keypress != -1 )
2126 INVOKE_WCB( *window, SpecialUp,
2128 window->State.MouseX, window->State.MouseY )
2131 fgState.Modifiers = INVALID_MODIFIERS;
2138 if( (fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE) && (HIWORD(lParam) & KF_REPEAT) )
2141 fgState.Modifiers = fghGetWin32Modifiers( );
2142 INVOKE_WCB( *window, Keyboard,
2144 window->State.MouseX, window->State.MouseY )
2146 fgState.Modifiers = INVALID_MODIFIERS;
2150 case WM_CAPTURECHANGED:
2151 /* User has finished resizing the window, force a redraw */
2152 INVOKE_WCB( *window, Display, ( ) );
2154 /*lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ); */
2157 /* Other messages that I have seen and which are not handled already */
2158 case WM_SETTEXT: /* 0x000c */
2159 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2160 /* Pass it on to "DefWindowProc" to set the window text */
2163 case WM_GETTEXT: /* 0x000d */
2164 /* Ideally we would copy the title of the window into "lParam" */
2165 /* strncpy ( (char *)lParam, "Window Title", wParam );
2166 lRet = ( wParam > 12 ) ? 12 : wParam; */
2167 /* the number of characters copied */
2168 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2171 case WM_GETTEXTLENGTH: /* 0x000e */
2172 /* Ideally we would get the length of the title of the window */
2174 /* the number of characters in "Window Title\0" (see above) */
2177 case WM_ERASEBKGND: /* 0x0014 */
2178 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2181 #if !defined(_WIN32_WCE)
2182 case WM_SYNCPAINT: /* 0x0088 */
2183 /* Another window has moved, need to update this one */
2184 window->State.Redisplay = GL_TRUE;
2185 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2186 /* Help screen says this message must be passed to "DefWindowProc" */
2189 case WM_NCPAINT: /* 0x0085 */
2190 /* Need to update the border of this window */
2191 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2192 /* Pass it on to "DefWindowProc" to repaint a standard border */
2195 case WM_SYSCOMMAND : /* 0x0112 */
2198 * We have received a system command message. Try to act on it.
2199 * The commands are passed in through the "wParam" parameter:
2200 * The least significant digit seems to be which edge of the window
2201 * is being used for a resize event:
2205 * Congratulations and thanks to Richard Rauch for figuring this out..
2207 switch ( wParam & 0xfff0 )
2216 /* User has clicked on the "-" to minimize the window */
2217 /* Turn off the visibility */
2218 window->State.Visible = GL_FALSE ;
2225 case SC_NEXTWINDOW :
2228 case SC_PREVWINDOW :
2232 /* Followed very closely by a WM_CLOSE message */
2256 case SC_SCREENSAVE :
2262 #if(WINVER >= 0x0400)
2266 case SC_MONITORPOWER :
2269 case SC_CONTEXTHELP :
2271 #endif /* WINVER >= 0x0400 */
2275 fgWarning( "Unknown wParam type 0x%x", wParam );
2280 #endif /* !defined(_WIN32_WCE) */
2282 /* We need to pass the message on to the operating system as well */
2283 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2287 /* Handle unhandled messages */
2288 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
2296 /*** END OF FILE ***/