⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 platform.cpp

📁 此代码为WCE5.0下电源管理的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    if(dwStatus == ERROR_SUCCESS) {
        pszSubKey = _T("Interfaces");
        dwStatus = RegCreateKeyEx(hkPM, pszSubKey, 0, NULL, 0, 0, NULL, &hkSubkey,
            &dwDisposition);
        if(dwStatus == ERROR_SUCCESS) {
            if(dwDisposition == REG_CREATED_NEW_KEY) {
                LPTSTR pszName = PMCLASS_GENERIC_DEVICE;
                LPTSTR pszValue = _T("Generic power-manageable devices");
                dwStatus = RegSetValueEx(hkSubkey, pszName, 0, REG_SZ, (LPBYTE) pszValue, 
                    (_tcslen(pszValue) + 1) * sizeof(*pszValue));
                if(dwStatus == ERROR_SUCCESS) {
                    pszName = PMCLASS_BLOCK_DEVICE;
                    pszValue = _T("Power-manageable block devices");
                    dwStatus = RegSetValueEx(hkSubkey, pszName, 0, REG_SZ, (LPBYTE) pszValue, 
                        (_tcslen(pszValue) + 1) * sizeof(*pszValue));
                }
                if(dwStatus == ERROR_SUCCESS) {
                    pszName = PMCLASS_DISPLAY;
                    pszValue = _T("Power-manageable display drivers");
                    dwStatus = RegSetValueEx(hkSubkey, pszName, 0, REG_SZ, (LPBYTE) pszValue, 
                        (_tcslen(pszValue) + 1) * sizeof(*pszValue));
                }
            }
            RegCloseKey(hkSubkey);
        }

        PMLOGMSG(dwStatus != ERROR_SUCCESS && ZONE_ERROR, 
            (_T("%s: error %d while creating or writing values in '%s\\%s'\r\n"), pszFname, dwStatus,
            PWRMGR_REG_KEY, pszSubKey));
    }

    // release resources
    if(hkPM != NULL) RegCloseKey(hkPM);

    PMLOGMSG(ZONE_INIT, (_T("-%s: returning %d\r\n"), pszFname, dwStatus));

    return dwStatus;
}

// This routine performs platform-specific initialization on a device list.
// Primarily this involves determining what routines should be used to communicate
// with the class.
EXTERN_C BOOL WINAPI
PlatformDeviceListInit(PDEVICE_LIST pdl)
{
    BOOL fOk = FALSE;
    PDEVICE_INTERFACE pInterface;
    SETFNAME(_T("PlatformDeviceListInit"));

    PREFAST_DEBUGCHK(pdl != NULL);
    DEBUGCHK(pdl->pGuid != NULL);

    if(*pdl->pGuid == idPMDisplayDeviceClass) {
        PMLOGMSG(ZONE_INIT || ZONE_PLATFORM, 
            (_T("%s: using display interface to access class %08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x\r\n"), 
            pszFname, pdl->pGuid->Data1, pdl->pGuid->Data2, pdl->pGuid->Data3,
            (pdl->pGuid->Data4[0] << 8) + pdl->pGuid->Data4[1], pdl->pGuid->Data4[2], pdl->pGuid->Data4[3], 
            pdl->pGuid->Data4[4], pdl->pGuid->Data4[5], pdl->pGuid->Data4[6], pdl->pGuid->Data4[7]));
        // Use the display driver interface to get to the device.  To remove
        // display code from the link, edit or conditionally compile this code out
        // of the PM.
        extern DEVICE_INTERFACE gDisplayInterface;      // defined in the MDD
        pInterface = &gDisplayInterface;
    } else {
        // use the standard stream interface to get to the device
        PMLOGMSG(ZONE_INIT || ZONE_PLATFORM, 
            (_T("%s: using stream interface to access class %08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x\r\n"), 
            pszFname, pdl->pGuid->Data1, pdl->pGuid->Data2, pdl->pGuid->Data3,
            (pdl->pGuid->Data4[0] << 8) + pdl->pGuid->Data4[1], pdl->pGuid->Data4[2], pdl->pGuid->Data4[3], 
            pdl->pGuid->Data4[4], pdl->pGuid->Data4[5], pdl->pGuid->Data4[6], pdl->pGuid->Data4[7]));
        extern DEVICE_INTERFACE gStreamInterface;       // defined in the MDD
        pInterface = &gStreamInterface;
    }

    // try to initialize the interface
    if(pInterface != NULL) {
        if(pInterface->pfnInitInterface() == FALSE) {
            PMLOGMSG(ZONE_WARN, (_T("%s: warning: pfnInitInterface() failed for interface\r\n"), pszFname));
        } else {
            // pass back the pointer
            pdl->pInterface = pInterface;
            fOk = TRUE;
        }
    }

    return fOk;
}

// This routine is responsible for mapping system power state hint
// values to known system power states that we maintain in the registry.
EXTERN_C DWORD WINAPI
PlatformMapPowerStateHint(DWORD dwHint, LPTSTR pszBuf, DWORD dwBufChars)
{
    DWORD dwStatus = ERROR_SUCCESS;
    LPTSTR pszMappedStateName = NULL;
    SETFNAME(_T("PlatformMapPowerStateHint"));

    // mask off unused bits
    dwHint &= (POWER_STATE_ON | POWER_STATE_IDLE | POWER_STATE_SUSPEND | POWER_STATE_OFF | POWER_STATE_RESET | POWER_STATE_CRITICAL);

    // Try to map the hint value.  Note that only one bit at a time should be set.
    switch(dwHint) {
    case POWER_STATE_ON:
        pszMappedStateName = _T("on");
        break;

    case POWER_STATE_IDLE:
        pszMappedStateName = _T("screenoff");
        break;

    case POWER_STATE_SUSPEND:
        // If we're honoring them, do we have outstanding unattended mode 
        // requests?  Otherwise just suspend.
        if(!gfSuspendToUnattendedMode || gdwUnattendedModeRequests == 0) {
            // suspend the system normally
            pszMappedStateName = _T("suspend");
        } else {
            // yes, go to unattended mode
            pszMappedStateName = _T("unattended");
        }
        break;
        
    case POWER_STATE_OFF:           // power off, cold boot on resume
    case POWER_STATE_CRITICAL:      // catastrophic power loss, shut down immediately
        pszMappedStateName = _T("suspend");
        break;
        
    case POWER_STATE_RESET:         // flush files and reboot
        pszMappedStateName = _T("reboot");
        break;

    default:
        PMLOGMSG(ZONE_PLATFORM | ZONE_WARN, 
            (_T("%s: bad hint value 0x%x\r\n"), pszFname, dwHint));
        dwStatus = ERROR_FILE_NOT_FOUND;
        break;
    }

    DEBUGCHK(dwStatus == ERROR_SUCCESS);

    // if we have one, copy the name back to the caller
    if(pszMappedStateName != NULL) {
        if(dwBufChars < (_tcslen(pszMappedStateName) + 1)) {
            dwStatus = ERROR_INSUFFICIENT_BUFFER;
        } else {
            _tcscpy(pszBuf, pszMappedStateName);
        }
    }

    PMLOGMSG(dwStatus != ERROR_SUCCESS && ZONE_WARN,
        (_T("%s: returning %d\r\n"), pszFname, dwStatus));
    return dwStatus;
}

// This routine converts the PM's internal state machine states into
// system power states.  It returns NULL if no match exists.
LPCTSTR
ActivityStateToSystemState(PLATFORM_ACTIVITY_STATE eActivityState)
{
    LPCTSTR pszNewState;

    switch(eActivityState) {
    case On:
        pszNewState = _T("on");
        break;
    case ScreenOff:
        pszNewState = _T("screenoff");
        break;
    case Unattended:
        pszNewState = _T("unattended");
        break;
    case Resuming:
        pszNewState = _T("Resuming");
        break;
    case Suspend:
        pszNewState = _T("suspend");
        break;
    default:
        pszNewState = NULL;
        break;
    }

    return pszNewState;
}

// This routine converts a system power state into an activity state.  It passes
// back the activity state by side effect and returns TRUE if successful, FALSE
// otherwise.
BOOL
SystemStateToActivityState(PSYSTEM_POWER_STATE pSystemPowerState, PPLATFORM_ACTIVITY_STATE peActivityState)
{
    BOOL fOk = TRUE;

    DEBUGCHK(pSystemPowerState != NULL);
    PREFAST_DEBUGCHK(peActivityState != NULL);
    DEBUGCHK(pSystemPowerState->pszName != NULL);

    if(pSystemPowerState->dwFlags & POWER_STATE_ON) {
        *peActivityState = On;
    }
    else if(pSystemPowerState->dwFlags & POWER_STATE_IDLE) {
        *peActivityState = ScreenOff;
    }
    else if(pSystemPowerState->dwFlags & (POWER_STATE_SUSPEND | POWER_STATE_OFF | POWER_STATE_CRITICAL)) {
        *peActivityState = Suspend;
    }
    else if(_tcscmp(pSystemPowerState->pszName, _T("unattended")) == 0) {
        *peActivityState = Unattended;
    } 
    else if(_tcscmp(pSystemPowerState->pszName, _T("resuming")) == 0) {
        *peActivityState = Resuming;
    }
    else {
        fOk = FALSE;
    }

    DEBUGCHK(fOk);
    return fOk;
}

// This routine figures out what the next power state for the platform should be
// based on activity, battery state, elapsed time, and so forth.  If necessary
// it will update the system power state.  The eActivityEvent parameter indicates what
// has changed that may affect the system power state.  The dwElapsedTime
// value indicates how many ticks have occurred since the last update.
DWORD
PlatformUpdateSystemPowerState(PLATFORM_ACTIVITY_EVENT eActivityEvent, DWORD dwElapsedTime)
{
    PLATFORM_ACTIVITY_STATE asNew;
    BOOL fChangingActivityStates = FALSE;
    LPCTSTR pszNewState;
    BOOL fOnACPower;
    DWORD dwSuspendTimeout, dwResumingSuspendTimeout;
    static fUserActive = FALSE;
    SETFNAME(_T("PlatformUpdateSystemPowerState"));

    PMLOGMSG(ZONE_PLATFORM, (_T("%s: activity '%s' in state '%s', elapsed time %d, urc %d\r\n"), pszFname, 
        eActivityEvent == NoActivity ? _T("NoActivity") :
        eActivityEvent == UserActivity ? _T("UserActivity") :
        eActivityEvent == UserInactivity ? _T("UserInactivity") :
        eActivityEvent == SystemIdleTimerWasReset ? _T("SystemIdleTimerWasReset") :
        eActivityEvent == EnterUnattendedModeRequest ? _T("EnterUnattendedModeRequest") :
        eActivityEvent == LeaveUnattendedModeRequest ? _T("LeaveUnattendedModeRequest") :
        eActivityEvent == RestartTimeouts ? _T("RestartTimeouts") :
        eActivityEvent == Timeout ? _T("Timeout") :
        eActivityEvent == PowerSourceChange ? _T("PowerSourceChange") :
        eActivityEvent == SystemPowerStateChange ? _T("SystemPowerStateChange") :
        eActivityEvent == Resume ? _T("Resume") : 
            _T("<UNKNOWN>"),
        gActivityState == On ? _T("On") :
        gActivityState == ScreenOff ? _T("ScreenOff") :
        gActivityState == Unattended ? _T("Unattended") :
        gActivityState == Resuming ? _T("Resuming") :
        gActivityState == Suspend ? _T("Suspend") :
            _T("<UNKNOWN>"),
        dwElapsedTime, gdwUnattendedModeRequests));

    PMLOCK();

    // assume no state change
    asNew = gActivityState;

    // are we on battery?
    if(gSystemPowerStatus.bACLineStatus == AC_LINE_OFFLINE) {
        // we're on battery power
        fOnACPower = FALSE;
        dwSuspendTimeout = gdwBattSuspendTimeout;
        dwResumingSuspendTimeout = gdwBattResumingSuspendTimeout;
    } else {
        // no, assume AC power
        fOnACPower = TRUE;
        dwSuspendTimeout = gdwACSuspendTimeout;
        dwResumingSuspendTimeout = gdwACResumingSuspendTimeout;
    }

    // determine what action, if any, to take based on the current system state
    // and the event with which we're being invoked.
    switch(eActivityEvent) {
    case Resume:
        // reset our count of unattended mode requests
        gdwUnattendedModeRequests = 0;

        // always transition to resuming when we detect a resume event
        asNew = Resuming;

        // assume no user activity
        fUserActive = FALSE;

        // restart timeouts
        dwElapsedTime = 0;
        break;
    case Timeout:
        // suspend if the state timer has really expired.  
        DEBUGCHK(gdwStateTimeLeft != INFINITE);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -