void fgPlatformProcessSingleEvent ( void ) { MSG stMsg; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" ); while( PeekMessage( &stMsg, NULL, 0, 0, PM_NOREMOVE ) ) { if( GetMessage( &stMsg, NULL, 0, 0 ) == 0 ) { if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT ) { fgDeinitialize( ); exit( 0 ); } else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS ) fgState.ExecState = GLUT_EXEC_STATE_STOP; return; } TranslateMessage( &stMsg ); DispatchMessage( &stMsg ); } }
/* * Error Messages. */ void fgError( const char *fmt, ... ) { va_list ap; if (fgState.ErrorFunc) { va_start( ap, fmt ); /* call user set error handler here */ fgState.ErrorFunc(fmt, ap); va_end( ap ); } else { #ifdef FREEGLUT_PRINT_ERRORS va_start( ap, fmt ); fprintf( stderr, "freeglut "); if( fgState.ProgramName ) fprintf( stderr, "(%s): ", fgState.ProgramName ); VFPRINTF( stderr, fmt, ap ); fprintf( stderr, "\n" ); va_end( ap ); #endif if ( fgState.Initialised ) fgDeinitialize (); exit( 1 ); } }
/* * Undoes all the "glutInit" stuff */ void FGAPIENTRY glutExit ( void ) { fgDeinitialize (); }
void fgPlatformProcessSingleEvent ( void ) { SFG_Window* window; XEvent event; /* This code was repeated constantly, so here it goes into a definition: */ #define GETWINDOW(a) \ window = fgWindowByHandle( event.a.window ); \ if( window == NULL ) \ break; #define GETMOUSE(a) \ window->State.MouseX = event.a.x; \ window->State.MouseY = event.a.y; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" ); while( XPending( fgDisplay.pDisplay.Display ) ) { XNextEvent( fgDisplay.pDisplay.Display, &event ); #if _DEBUG fghPrintEvent( &event ); #endif switch( event.type ) { case ClientMessage: if (fgStructure.CurrentWindow) if(fgIsSpaceballXEvent(&event)) { fgSpaceballHandleXEvent(&event); break; } /* Destroy the window when the WM_DELETE_WINDOW message arrives */ if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.pDisplay.DeleteWindow ) { GETWINDOW( xclient ); fgDestroyWindow ( window ); if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT ) { fgDeinitialize( ); exit( 0 ); } else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS ) fgState.ExecState = GLUT_EXEC_STATE_STOP; return; } break; /* * CreateNotify causes a configure-event so that sub-windows are * handled compatibly with GLUT. Otherwise, your sub-windows * (in freeglut only) will not get an initial reshape event, * which can break things. * * GLUT presumably does this because it generally tries to treat * sub-windows the same as windows. */ case CreateNotify: case ConfigureNotify: { int width, height, x, y; if( event.type == CreateNotify ) { GETWINDOW( xcreatewindow ); width = event.xcreatewindow.width; height = event.xcreatewindow.height; x = event.xcreatewindow.x; y = event.xcreatewindow.y; } else { GETWINDOW( xconfigure ); width = event.xconfigure.width; height = event.xconfigure.height; x = event.xconfigure.x; y = event.xconfigure.y; } /* Update state and call callback, if there was a change */ fghOnPositionNotify(window, x, y, GL_FALSE); /* Update state and call callback, if there was a change */ fghOnReshapeNotify(window, width, height, GL_FALSE); } break; case DestroyNotify: /* * This is sent to confirm the XDestroyWindow call. * * XXX WHY is this commented out? Should we re-enable it? */ /* fgAddToWindowDestroyList ( window ); */ break; case Expose: /* * We are too dumb to process partial exposes... * * XXX Well, we could do it. However, it seems to only * XXX be potentially useful for single-buffered (since * XXX double-buffered does not respect viewport when we * XXX do a buffer-swap). * */ if( event.xexpose.count == 0 ) { GETWINDOW( xexpose ); window->State.WorkMask |= GLUT_DISPLAY_WORK; } break; case MapNotify: break; case UnmapNotify: /* We get this when iconifying a window. */ GETWINDOW( xunmap ); INVOKE_WCB( *window, WindowStatus, ( GLUT_HIDDEN ) ); window->State.Visible = GL_FALSE; break; case MappingNotify: /* * Have the client's keyboard knowledge updated (xlib.ps, * page 206, says that's a good thing to do) */ XRefreshKeyboardMapping( (XMappingEvent *) &event ); break; case VisibilityNotify: { /* * Sending this event, the X server can notify us that the window * has just acquired one of the three possible visibility states: * VisibilityUnobscured, VisibilityPartiallyObscured or * VisibilityFullyObscured. Note that we DO NOT receive a * VisibilityNotify event when iconifying a window, we only get an * UnmapNotify then. */ GETWINDOW( xvisibility ); switch( event.xvisibility.state ) { case VisibilityUnobscured: INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_RETAINED ) ); window->State.Visible = GL_TRUE; break; case VisibilityPartiallyObscured: INVOKE_WCB( *window, WindowStatus, ( GLUT_PARTIALLY_RETAINED ) ); window->State.Visible = GL_TRUE; break; case VisibilityFullyObscured: INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_COVERED ) ); window->State.Visible = GL_FALSE; break; default: fgWarning( "Unknown X visibility state: %d", event.xvisibility.state ); break; } } break; case EnterNotify: case LeaveNotify: GETWINDOW( xcrossing ); GETMOUSE( xcrossing ); if( ( event.type == LeaveNotify ) && window->IsMenu && window->ActiveMenu && window->ActiveMenu->IsActive ) fgUpdateMenuHighlight( window->ActiveMenu ); INVOKE_WCB( *window, Entry, ( ( EnterNotify == event.type ) ? GLUT_ENTERED : GLUT_LEFT ) ); break; case MotionNotify: { /* if GLUT_SKIP_STALE_MOTION_EVENTS is true, then discard all but * the last motion event from the queue */ if(fgState.SkipStaleMotion) { while(XCheckIfEvent(fgDisplay.pDisplay.Display, &event, match_motion, 0)); } GETWINDOW( xmotion ); GETMOUSE( xmotion ); if( window->ActiveMenu ) { if( window == window->ActiveMenu->ParentWindow ) { window->ActiveMenu->Window->State.MouseX = event.xmotion.x_root - window->ActiveMenu->X; window->ActiveMenu->Window->State.MouseY = event.xmotion.y_root - window->ActiveMenu->Y; } fgUpdateMenuHighlight( window->ActiveMenu ); break; } /* * XXX For more than 5 buttons, just check {event.xmotion.state}, * XXX rather than a host of bit-masks? Or maybe we need to * XXX track ButtonPress/ButtonRelease events in our own * XXX bit-mask? */ fgState.Modifiers = fgPlatformGetModifiers( event.xmotion.state ); if ( event.xmotion.state & ( Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask ) ) { INVOKE_WCB( *window, Motion, ( event.xmotion.x, event.xmotion.y ) ); } else { INVOKE_WCB( *window, Passive, ( event.xmotion.x, event.xmotion.y ) ); } fgState.Modifiers = INVALID_MODIFIERS; } break; case ButtonRelease: case ButtonPress: { GLboolean pressed = GL_TRUE; int button; if( event.type == ButtonRelease ) pressed = GL_FALSE ; /* * A mouse button has been pressed or released. Traditionally, * break if the window was found within the freeglut structures. */ GETWINDOW( xbutton ); GETMOUSE( xbutton ); /* * An X button (at least in XFree86) is numbered from 1. * A GLUT button is numbered from 0. * Old GLUT passed through buttons other than just the first * three, though it only gave symbolic names and official * support to the first three. */ button = event.xbutton.button - 1; /* * Do not execute the application's mouse callback if a menu * is hooked to this button. In that case an appropriate * private call should be generated. */ if( fgCheckActiveMenu( window, button, pressed, event.xbutton.x, event.xbutton.y ) ) break; /* * Check if there is a mouse or mouse wheel callback hooked to the * window */ if( ! FETCH_WCB( *window, Mouse ) && ! FETCH_WCB( *window, MouseWheel ) ) break; fgState.Modifiers = fgPlatformGetModifiers( event.xbutton.state ); /* Finally execute the mouse or mouse wheel callback */ if( ( button < glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS ) ) || ( ! FETCH_WCB( *window, MouseWheel ) ) ) INVOKE_WCB( *window, Mouse, ( button, pressed ? GLUT_DOWN : GLUT_UP, event.xbutton.x, event.xbutton.y ) ); else { /* * Map 4 and 5 to wheel zero; EVEN to +1, ODD to -1 * " 6 and 7 " " one; ... * * XXX This *should* be behind some variables/macros, * XXX since the order and numbering isn't certain * XXX See XFree86 configuration docs (even back in the * XXX 3.x days, and especially with 4.x). * * XXX Note that {button} has already been decremented * XXX in mapping from X button numbering to GLUT. * * XXX Should add support for partial wheel turns as Windows does -- 5/27/11 */ int wheel_number = (button - glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS )) / 2; int direction = -1; if( button % 2 ) direction = 1; if( pressed ) INVOKE_WCB( *window, MouseWheel, ( wheel_number, direction, event.xbutton.x, event.xbutton.y ) ); } fgState.Modifiers = INVALID_MODIFIERS; } break; case KeyRelease: case KeyPress: { FGCBKeyboard keyboard_cb; FGCBSpecial special_cb; GETWINDOW( xkey ); GETMOUSE( xkey ); /* Detect auto repeated keys, if configured globally or per-window */ if ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE ) { if (event.type==KeyRelease) { /* * Look at X11 keystate to detect repeat mode. * While X11 says the key is actually held down, we'll ignore KeyRelease/KeyPress pairs. */ char keys[32]; XQueryKeymap( fgDisplay.pDisplay.Display, keys ); /* Look at X11 keystate to detect repeat mode */ if ( event.xkey.keycode<256 ) /* XQueryKeymap is limited to 256 keycodes */ { if ( keys[event.xkey.keycode>>3] & (1<<(event.xkey.keycode%8)) ) window->State.pWState.KeyRepeating = GL_TRUE; else window->State.pWState.KeyRepeating = GL_FALSE; } } } else window->State.pWState.KeyRepeating = GL_FALSE; /* Cease processing this event if it is auto repeated */ if (window->State.pWState.KeyRepeating) { if (event.type == KeyPress) window->State.pWState.KeyRepeating = GL_FALSE; break; } if( event.type == KeyPress ) { keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, Keyboard )); special_cb = (FGCBSpecial) ( FETCH_WCB( *window, Special )); } else { keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, KeyboardUp )); special_cb = (FGCBSpecial) ( FETCH_WCB( *window, SpecialUp )); } /* Is there a keyboard/special callback hooked for this window? */ if( keyboard_cb || special_cb ) { XComposeStatus composeStatus; char asciiCode[ 32 ]; KeySym keySym; int len; /* Check for the ASCII/KeySym codes associated with the event: */ len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode), &keySym, &composeStatus ); /* GLUT API tells us to have two separate callbacks... */ if( len > 0 ) { /* ...one for the ASCII translateable keypresses... */ if( keyboard_cb ) { fgSetWindow( window ); fgState.Modifiers = fgPlatformGetModifiers( event.xkey.state ); keyboard_cb( asciiCode[ 0 ], event.xkey.x, event.xkey.y ); fgState.Modifiers = INVALID_MODIFIERS; } } else { int special = -1; /* * ...and one for all the others, which need to be * translated to GLUT_KEY_Xs... */ switch( keySym ) { case XK_F1: special = GLUT_KEY_F1; break; case XK_F2: special = GLUT_KEY_F2; break; case XK_F3: special = GLUT_KEY_F3; break; case XK_F4: special = GLUT_KEY_F4; break; case XK_F5: special = GLUT_KEY_F5; break; case XK_F6: special = GLUT_KEY_F6; break; case XK_F7: special = GLUT_KEY_F7; break; case XK_F8: special = GLUT_KEY_F8; break; case XK_F9: special = GLUT_KEY_F9; break; case XK_F10: special = GLUT_KEY_F10; break; case XK_F11: special = GLUT_KEY_F11; break; case XK_F12: special = GLUT_KEY_F12; break; case XK_KP_Left: case XK_Left: special = GLUT_KEY_LEFT; break; case XK_KP_Right: case XK_Right: special = GLUT_KEY_RIGHT; break; case XK_KP_Up: case XK_Up: special = GLUT_KEY_UP; break; case XK_KP_Down: case XK_Down: special = GLUT_KEY_DOWN; break; case XK_KP_Prior: case XK_Prior: special = GLUT_KEY_PAGE_UP; break; case XK_KP_Next: case XK_Next: special = GLUT_KEY_PAGE_DOWN; break; case XK_KP_Home: case XK_Home: special = GLUT_KEY_HOME; break; case XK_KP_End: case XK_End: special = GLUT_KEY_END; break; case XK_KP_Insert: case XK_Insert: special = GLUT_KEY_INSERT; break; case XK_Num_Lock : special = GLUT_KEY_NUM_LOCK; break; case XK_KP_Begin : special = GLUT_KEY_BEGIN; break; case XK_KP_Delete: special = GLUT_KEY_DELETE; break; case XK_Shift_L: special = GLUT_KEY_SHIFT_L; break; case XK_Shift_R: special = GLUT_KEY_SHIFT_R; break; case XK_Control_L: special = GLUT_KEY_CTRL_L; break; case XK_Control_R: special = GLUT_KEY_CTRL_R; break; case XK_Alt_L: special = GLUT_KEY_ALT_L; break; case XK_Alt_R: special = GLUT_KEY_ALT_R; break; } /* * Execute the callback (if one has been specified), * given that the special code seems to be valid... */ if( special_cb && (special != -1) ) { fgSetWindow( window ); fgState.Modifiers = fgPlatformGetModifiers( event.xkey.state ); special_cb( special, event.xkey.x, event.xkey.y ); fgState.Modifiers = INVALID_MODIFIERS; } } } }
/* * Enters the freeglut processing loop. * Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP". */ void FGAPIENTRY glutMainLoopMT( void ) { //int action; //MT #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE SFG_Window *window = (SFG_Window *)fgStructure.Windows.First ; #endif FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoop" ); #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE /* * Processing before the main loop: If there is a window which is open and * which has a visibility callback, call it. I know this is an ugly hack, * but I'm not sure what else to do about it. Ideally we should leave * something uninitialized in the create window code and initialize it in * the main loop, and have that initialization create a "WM_ACTIVATE" * message. Then we would put the visibility callback code in the * "case WM_ACTIVATE" block below. - John Fay -- 10/24/02 */ while( window ) { if ( FETCH_WCB( *window, Visibility ) ) { SFG_Window *current_window = fgStructure.CurrentWindow ; INVOKE_WCB( *window, Visibility, ( window->State.Visible ) ); fgSetWindow( current_window ); } window = (SFG_Window *)window->Node.Next ; } #endif fgState.ExecState = GLUT_EXEC_STATE_RUNNING ; while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING ) { SFG_Window *window; glutMainLoopEvent( ); /* * Step through the list of windows, seeing if there are any * that are not menus */ for( window = ( SFG_Window * )fgStructure.Windows.First; window; window = ( SFG_Window * )window->Node.Next ) if ( ! ( window->IsMenu ) ) break; if( ! window ) fgState.ExecState = GLUT_EXEC_STATE_STOP; else { if( fgState.IdleCallback ) { if( fgStructure.CurrentWindow && fgStructure.CurrentWindow->IsMenu ) /* fail safe */ fgSetWindow( window ); fgState.IdleCallback( ); } fghSleepForEvents( ); } } #if 0 //Marc Toussaint /* * When this loop terminates, destroy the display, state and structure * of a freeglut session, so that another glutInit() call can happen * * Save the "ActionOnWindowClose" because "fgDeinitialize" resets it. */ action = fgState.ActionOnWindowClose; fgDeinitialize( ); if( action == GLUT_ACTION_EXIT ) exit( 0 ); #endif }