Example #1
0
OSStatus safe_enter_critical_region(MPCriticalRegionID pCriticalRegionID, Duration lDuration, MPCriticalRegionID pCriticalRegionCriticalRegionID/* = kInvalidID*/)
{
    if(pCriticalRegionCriticalRegionID != kInvalidID)
    {
        if(at_mp())
        {
        // enter the critical region's critical region
            OSStatus lStatus = noErr;
            AbsoluteTime sExpiration;
            if(lDuration != kDurationImmediate && lDuration != kDurationForever)
            {
                sExpiration = AddDurationToAbsolute(lDuration, UpTime());
            }
            lStatus = MPEnterCriticalRegion(pCriticalRegionCriticalRegionID, lDuration);
            assert(lStatus == noErr || lStatus == kMPTimeoutErr);
            if(lStatus == noErr)
            {
            // calculate a new duration
                if(lDuration != kDurationImmediate && lDuration != kDurationForever)
                {
                // check if we have any time left
                    AbsoluteTime sUpTime(UpTime());
                    if(force_cast<uint64_t>(sExpiration) > force_cast<uint64_t>(sUpTime))
                    {
                    // reset our duration to our remaining time
                        lDuration = AbsoluteDeltaToDuration(sExpiration, sUpTime);
                    }
                    else
                    {
                    // no time left
                        lDuration = kDurationImmediate;
                    }
                }
            // if we entered the critical region, exit it again
                lStatus = MPExitCriticalRegion(pCriticalRegionCriticalRegionID);
                assert(lStatus == noErr);
            }
            else
            {
            // otherwise, give up
                return(lStatus);
            }
        }
        else
        {
        // if we're at system task time, try to enter the critical region's critical
        //    region until we succeed.  MP tasks will block on this until we let it go.
            OSStatus lStatus;
            do
            {
                lStatus = MPEnterCriticalRegion(pCriticalRegionCriticalRegionID, kDurationImmediate);
            } while(lStatus == kMPTimeoutErr);
            assert(lStatus == noErr);
        }
    }

// try to enter the critical region
    function<OSStatus, Duration> oEnterCriticalRegion;
    oEnterCriticalRegion = bind(MPEnterCriticalRegion, pCriticalRegionID, _1);
    OSStatus lStatus = safe_wait(oEnterCriticalRegion, lDuration);

// if we entered the critical region's critical region to get the critical region,
//    exit the critical region's critical region.
    if(pCriticalRegionCriticalRegionID != kInvalidID && at_mp() == false)
    {
        lStatus = MPExitCriticalRegion(pCriticalRegionCriticalRegionID);
        assert(lStatus == noErr);
    }
    return(lStatus);
}
Example #2
0
pascal void HIDTimer (EventLoopTimerRef inTimer, void* )
{
    #pragma unused (inTimer)
	static AbsoluteTime time = {0, 0};
	long				deadZone = 25;
   pRecContext			pContextInfo = NULL;
//	float				rotation[4] = {0.0f, 0.0f, 0.0f, 0.0f};

    AbsoluteTime currTime = UpTime ();
    float deltaTime = (float) AbsoluteDeltaToDuration (currTime, time);
	time = currTime;	// reset for next time interval
    if (0 > deltaTime)	// if negative microseconds
		deltaTime /= -1000000.0;
    else				// else milliseconds
		deltaTime /= 1000.0;
    if (deltaTime > 10.0) // skip pauses
        return;

    // get input values
    GetInput ();
	// apply to front window
	pContextInfo = GetCurrentContextInfo(FrontWindow()); // call back to main to get the current context info record
	if (!pContextInfo)
	{
		return; // not an application window so do not process
	}
	// apply input
	double panX=0, panY=0;
	if (gActionArray [kActionXAxis].pDevice && gActionArray [kActionXAxis].pElement && (abs (gActionArray [kActionXAxis].value) > deadZone)) {
		// pan
		panX = gActionArray [kActionXAxis].value;
		//panX = abs (gActionArray [kActionXAxis].value) * gActionArray [kActionXAxis].value * 0.002f;
		//panX = abs (gActionArray [kActionXAxis].value) * gActionArray [kActionXAxis].value * 0.002f / (1500.0f / -pContextInfo->camera.viewPos.z);
		//pContextInfo->camera.viewPos.x += panX;
	}
	if (gActionArray [kActionYAxis].pDevice && gActionArray [kActionYAxis].pElement && (abs (gActionArray [kActionYAxis].value) > deadZone)) {
		// pan
		panY = gActionArray [kActionYAxis].value;
		//panY = abs (gActionArray [kActionYAxis].value) * gActionArray [kActionYAxis].value * deltaTime * 0.002f;
		//panY = abs (gActionArray [kActionYAxis].value) * gActionArray [kActionYAxis].value * deltaTime * 0.002f / (1500.0f / -pContextInfo->camera.viewPos.z);
		//pContextInfo->camera.viewPos.y -= panY;
	}
	handleJoystickMovement(pContextInfo, panX, panY);

	//	doMouseDelta(panX, panY);
//	if (gActionArray [kActionZAxis].pDevice && gActionArray [kActionZAxis].pElement && (abs (gActionArray [kActionZAxis].value) > deadZone)) {
//		// dolly
//		GLfloat dolly = abs (gActionArray [kActionZAxis].value) * gActionArray [kActionZAxis].value * deltaTime * 0.002f * -pContextInfo->camera.viewPos.z / 500.0f;
//		pContextInfo->camera.viewPos.z += dolly;
//		if (pContextInfo->camera.viewPos.z == 0.0) // do not let z = 0.0
//			pContextInfo->camera.viewPos.z = 0.0001;
//	}
	// handle rotations about each respective axis
//	if (gActionArray [kActionXRot].pDevice && gActionArray [kActionXRot].pElement && (abs (gActionArray [kActionXRot].value) > deadZone)) {
//		rotation[0] = abs (gActionArray [kActionXRot].value) * -gActionArray [kActionXRot].value * deltaTime * 0.0003f;
//		rotation[1] = 1.0f;
//		rotation[2] = 0.0f;
//		rotation[3] = 0.0f;
//		addToRotationTrackball (rotation, pContextInfo->worldRotation);
//	}
//	if (gActionArray [kActionYRot].pDevice && gActionArray [kActionYRot].pElement && (abs (gActionArray [kActionYRot].value) > deadZone)) {
//		rotation[0] = abs (gActionArray [kActionYRot].value) * gActionArray [kActionYRot].value * deltaTime * 0.0003f;
//		rotation[1] = 0.0f;
//		rotation[2] = 1.0f;
//		rotation[3] = 0.0f;
//		addToRotationTrackball (rotation, pContextInfo->worldRotation);
//	}
//	if (gActionArray [kActionZRot].pDevice && gActionArray [kActionZRot].pElement && (abs (gActionArray [kActionZRot].value) > deadZone)) {
//		rotation[0] = abs (gActionArray [kActionZRot].value) * -gActionArray [kActionZRot].value * deltaTime * 0.0003f;
//		rotation[1] = 0.0f;
//		rotation[2] = 0.0f;
//		rotation[3] = 1.0f;
//		addToRotationTrackball (rotation, pContextInfo->worldRotation);
//	}
	// need to force draw here...
	{
		Rect rectPort;
		WindowRef window = FrontWindow ();
		GetWindowPortBounds (window, &rectPort);
		InvalWindowRect (window, &rectPort);
	}
}