📄 pmsysstate.cpp
字号:
// read the default power state and the flags -- both values
// must be present
dwSize = sizeof(dwDefaultDx);
dwStatus = keyHandle->RegFindValue(_T("Default"), &dwDefaultDx, &dwSize, &dwType);
if(dwStatus != ERROR_SUCCESS) {
PMLOGMSG(ZONE_WARN,
(_T("%s: can't read default Dx from '%s', status is %d\r\n"),
pszFname, szPath, dwStatus));
dwRetStatus = dwStatus;
}
if(dwDefaultDx < D0 || dwDefaultDx > D4) {
PMLOGMSG(ZONE_WARN,
(_T("%s: invalid Dx value for '%s': %d\r\n"), pszFname,
pszName, dwDefaultDx));
dwRetStatus = ERROR_INVALID_DATA;
}
if(dwRetStatus == ERROR_SUCCESS) {
dwSize = sizeof(dwFlags);
dwStatus =keyHandle->RegFindValue( _T("Flags"), &dwFlags, &dwSize, &dwType);
if(dwStatus != ERROR_SUCCESS) {
PMLOGMSG(ZONE_WARN,
(_T("%s: can't read flags from '%s', status is %d\r\n"),
pszFname, szPath, dwStatus));
dwRetStatus = dwStatus;
}
}
PMLOGMSG(dwRetStatus == ERROR_SUCCESS && ZONE_REGISTRY,
(_T("%s: state '%s' devices max is D%d, flags 0x%08x\r\n"),
pszFname, pszName, dwDefaultDx, dwFlags));
if(dwRetStatus == ERROR_SUCCESS && ppsps != NULL) {
// allocate and initialize a system power state
PSYSTEM_POWER_STATE psps = SystemPowerStateCreate(pszName);
if(psps == NULL) {
dwRetStatus = ERROR_OUTOFMEMORY;
} else {
// initialize the structure
psps->defaultCeilingDx = (CEDEVICE_POWER_STATE) dwDefaultDx;
psps->dwFlags = dwFlags;
*ppsps = psps;
}
}
}
// read device power modifiers for all associated devices
if(dwRetStatus == ERROR_SUCCESS && ppdpr != NULL) {
PDEVICE_POWER_RESTRICTION pdpr = RegReadDeviceRestrictions(keyHandle);
*ppdpr = pdpr;
}
g_pSysRegistryAccess->LeaveLock();
return dwRetStatus;
}
// This routine allows applications to determine what the current system
// power state name is. It also passes back flag bits which provide some
// information about the state.
EXTERN_C DWORD WINAPI
PmGetSystemPowerState(LPWSTR pBuffer, DWORD dwBufChars, PDWORD pdwFlags)
{
DWORD dwStatus = ERROR_INVALID_PARAMETER;
SETFNAME(_T("PmGetSystemPowerState"));
PMLOGMSG(ZONE_API, (_T("+%s: buf 0x%08x, size %d, pflags 0x%08x\r\n"),
pszFname, pBuffer, dwBufChars, pdwFlags));
PMLOCK();
__try {
if(dwBufChars < (_tcslen(gpSystemPowerState->pszName) + 1)) {
dwStatus = ERROR_INSUFFICIENT_BUFFER;
} else {
_tcscpy(pBuffer, gpSystemPowerState->pszName);
*pdwFlags = gpSystemPowerState->dwFlags;
dwStatus = ERROR_SUCCESS;
}
}
__except(EXCEPTION_EXECUTE_HANDLER) {
dwStatus = ERROR_INVALID_PARAMETER;
}
PMUNLOCK();
PMLOGMSG(ZONE_API, (_T("-%s: returning %d\r\n"), pszFname, dwStatus));
return dwStatus;
}
// This routine wraps system power state transitions. It can be called
// from within the PM or (indirectly) by applications. The fInternal
// flag indicates whether the invocation is from the PM or an application.
// If apps don't know the name of the state in which they're interested,
// the PM will find a mapping for them. This routine serializes system power
// state changes using a critical section. Drivers should not change system
// power states while handling device power state transitions. If they do,
// they may deadlock the power manager.
DWORD
PmSetSystemPowerState_I(LPCWSTR pwsState, DWORD dwStateHint, DWORD dwOptions,
BOOL fInternal)
{
TCHAR szStateName[MAX_PATH];
DWORD dwStatus = ERROR_SUCCESS;
SETFNAME(_T("PmSetSystemPowerState_I"));
PMLOGMSG(ZONE_API, (_T("+%s: name %s, hint 0x%08x, options 0x%08x, fInternal %d\r\n"),
pszFname, pwsState != NULL ? pwsState : _T("<NULL>"), dwStateHint, dwOptions,
fInternal));
PMENTERUPDATE();
// if the user passes a null state name, use the hints flag to try
// to find a match.
if(pwsState != NULL) {
// copy the parameter but watch out for bad pointers
__try {
_tcsncpy(szStateName, pwsState, dim(szStateName) - 1);
szStateName[dim(szStateName) - 1] = 0;
}
__except(EXCEPTION_EXECUTE_HANDLER) {
PMLOGMSG(ZONE_WARN, (_T("%s: exception copying state name\r\n"),
pszFname));
dwStatus = ERROR_INVALID_PARAMETER;
}
} else {
// try to match the hint flag to a system state
dwStatus = PlatformMapPowerStateHint(dwStateHint, szStateName, dim(szStateName));
}
// go ahead and do the update?
if(dwStatus == ERROR_SUCCESS) {
BOOL fForce;
if((dwOptions & POWER_FORCE) != 0) {
fForce = TRUE;
} else {
fForce = FALSE;
}
dwStatus = PlatformSetSystemPowerState(szStateName, fForce, fInternal);
}
PMLEAVEUPDATE();
PMLOGMSG(ZONE_API, (_T("-%s: returning dwStatus %d\r\n"), pszFname, dwStatus));
return dwStatus;
}
// Applications can call this routine to modify the system power state.
// If they don't know the name of the state in which they're interested,
// the PM will find a mapping for them. Drivers should not change system
// power states while handling device power state transitions. If they do,
// they may deadlock the power manager.
EXTERN_C DWORD WINAPI
PmSetSystemPowerState(LPCWSTR pwsState, DWORD dwStateHint, DWORD dwOptions)
{
TCHAR szState[MAX_PATH];
LPTSTR pszStateName = NULL;
DWORD dwStatus = ERROR_SUCCESS;
SETFNAME(_T("PmSetSystemPowerState"));
PMLOGMSG(ZONE_API, (_T("+%s: name %s, hint 0x%08x, options 0x%08x\r\n"),
pszFname, pwsState != NULL ? pwsState : _T("<NULL>"), dwStateHint, dwOptions));
// if the user passes a null state name, use the hints flag to try
// to find a match.
if(pwsState != NULL) {
// copy the parameter but watch out for bad pointers
__try {
DWORD dwIndex;
for(dwIndex = 0; pwsState[dwIndex] != 0 && dwIndex < (dim(szState) - 1); dwIndex++) {
szState[dwIndex] = _totlower(pwsState[dwIndex]);
}
szState[dwIndex] = 0;
pszStateName = szState;
// don't pass on truncated system power state names
if(pwsState[dwIndex] != 0) {
PMLOGMSG(ZONE_WARN,
(_T("%s: system power state name '%s' exceeds %d characters\r\n"),
pszFname, pwsState, dim(szState) - 1));
dwStatus = ERROR_INVALID_PARAMETER;
}
}
__except(EXCEPTION_EXECUTE_HANDLER) {
PMLOGMSG(ZONE_WARN, (_T("%s: exception copying state name\r\n"),
pszFname));
dwStatus = ERROR_INVALID_PARAMETER;
}
}
// carry out the power state change
if(dwStatus == ERROR_SUCCESS) {
dwStatus = PmSetSystemPowerState_I(pszStateName, dwStateHint, dwOptions, FALSE);
}
PMLOGMSG(ZONE_API, (_T("-%s: returning dwStatus %d\r\n"), pszFname, dwStatus));
return dwStatus;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -