//------------------------------------------------------------------
// write changed power policies
//
// return codes:
//   -1: power policy writing failure
//   -2: cpu policy writing failure
//   0: success
//------------------------------------------------------------------
int writePolicies( BOOL power, BOOL cpu )
{
  if( power && !WritePwrScheme(&internalProfile,L"Speedswitch Control",L"\0\0",&internalPolicy) )
  {
    ReadPwrScheme( internalProfile, &internalPolicy );
    return -1;
  }

  if( cpu && !WriteProcessorPwrScheme(internalProfile,&mach) )
  {
    ReadProcessorPwrScheme( internalProfile, &mach );

    if( power )
      SetActivePwrScheme( internalProfile, NULL, NULL );

    return -2;
  }

  SetActivePwrScheme( internalProfile, NULL, NULL );
  return 0;
}
//------------------------------------------------------------------
// create new profile from current one
//
// return codes:
//   -1: active profile detection failed
//   -2: reading active profile policy failed
//   -3: writing our own profile failed
//   -4: reading active cpu policy failed
//   -5: writing our own cpu policy failed
//   -6: new profile activation failed
//   0: profile creation successful
//------------------------------------------------------------------
int createProfile()
{
  // get the # of the currently active profile
  UINT activeProfile;
  if( !GetActivePwrScheme(&activeProfile) )
    return -1;  

  // read the profile policy settings
  if( !ReadPwrScheme(activeProfile,&toBeCopied) )
    return -2;

  // copy this to the one we use later
  internalPolicy = toBeCopied;

  // create the # of our own profile
  lastChecked++;

  // disable throttling in this new profile
  internalPolicy.user.ForcedThrottleAc = 100;
  internalPolicy.user.ForcedThrottleDc = 100;

  // write our own profile policy settings to the registry
  if( !WritePwrScheme(&lastChecked,L"Speedswitch Control",L"\0\0",&internalPolicy) )
    return -3;

  // this is our profile #
  internalProfile = lastChecked;

  // read the CPU policy from the active profile
  if( !ReadProcessorPwrScheme(activeProfile,&mach) )
    return -4;

  // use this CPU policy for our own profile
  if( !WriteProcessorPwrScheme(internalProfile,&mach) )
    return -5;

  if( !SetActivePwrScheme(internalProfile,NULL,NULL) )
    return -6;

  updateStates();

  return 0;
}
//------------------------------------------------------------------
// Detect our own profile no
//
// return codes:
//   -1: profile enumeration failed
//   -2: profile not found (profile creation necessary)
//   -3: CPU policy reading from our own profile failed
//   -4: new profile activation failed
//   >=0: profile no (success)
//------------------------------------------------------------------
int detectProfile()
{
  internalProfile = 0xffffffff;
  lastChecked = 0xffffffff;

  if( !EnumPwrSchemes(&powerSchemeCallback,0) )
    return -1;      // enumeration failed

  if( internalProfile == 0xffffffff )
    return -2;      // profile not found

  // read the current CPU policy from our profile
  if( !ReadProcessorPwrScheme(internalProfile,&mach) )
    return -3;      // CPU policy reading failed

  if( !SetActivePwrScheme(internalProfile,NULL,NULL) )
    return -4;  

  updateStates();

  return internalProfile;   // profile found, return profile no
}
//------------------------------------------------------------------
// set current ac/dc throttle and activate that
//------------------------------------------------------------------
BOOL setState( BOOL ac, BYTE policy )
{
  checkProfile( 1 );

  if( ac )
  {
    acThrottle = policy;
    mach.ProcessorPolicyAc.DynamicThrottle = policy;
  }
  else
  {
    dcThrottle = policy;
    mach.ProcessorPolicyDc.DynamicThrottle = policy;
  }

  if( !WriteProcessorPwrScheme(internalProfile,&mach) )
    return FALSE;

  if( !SetActivePwrScheme(internalProfile,NULL,NULL) )
    return FALSE;  

  return TRUE;
}
Exemple #5
0
BOOL PowerSchemes::ApplyPowerSchemeXP(UINT & uiID)
{
	BOOL bResult = FALSE;
	if(GetActivePwrScheme(&uiID))
	{
		//printf("GetActivePwrScheme OK\n");

		//PROCESSOR_POWER_POLICY  ppp; 
		MACHINE_PROCESSOR_POWER_POLICY  mppp;
		/*
		ReadProcessorPwrScheme is available for use in the operating systems specified in the Requirements section. 
		Requirements: Windows XP [desktop apps only] | Windows Server 2003 [desktop apps only]  
		*/
		if(ReadProcessorPwrScheme(uiID, &mppp))
		{
			printf("ReadProcessorPwrScheme OK\n");

			mppp.ProcessorPolicyAc.Policy[0].AllowPromotion = 1; // C1
			mppp.ProcessorPolicyAc.Policy[1].AllowPromotion = 1; // C2
			mppp.ProcessorPolicyAc.Policy[2].AllowPromotion = 1; // C3
			if (WriteProcessorPwrScheme(uiID, &mppp))
				SetActivePwrScheme(uiID, 0, 0);

			bResult = TRUE;
		}
		else
		{
			//printf("ReadProcessorPwrScheme FAIL\n");
			bResult = FALSE;
		}
	}
	else {
		printf("GetActivePwrScheme FAIL\n"); 
		bResult = FALSE;;
	} 
	return bResult;
}
Exemple #6
0
static
VOID
ShowPowerSchemesPopupMenu(
    CSysTray *pSysTray)
{
    PWRSCHEMECONTEXT PowerSchemeContext = {NULL, 0, 0};
    UINT uiActiveScheme;
    DWORD id;
    POINT pt;
    PowerSchemeContext.hPopup = CreatePopupMenu();
    EnumPwrSchemes(PowerSchemesEnumProc, (LPARAM)&PowerSchemeContext);

    if (GetActivePwrScheme(&uiActiveScheme))
    {
        CheckMenuRadioItem(PowerSchemeContext.hPopup,
                           PowerSchemeContext.uiFirst,
                           PowerSchemeContext.uiLast,
                           uiActiveScheme + 1,
                           MF_BYCOMMAND);
    }

    SetForegroundWindow(pSysTray->GetHWnd());
    GetCursorPos(&pt);
    
    id = TrackPopupMenuEx(PowerSchemeContext.hPopup,
                          TPM_RETURNCMD | TPM_NONOTIFY | TPM_RIGHTALIGN | TPM_BOTTOMALIGN,
                          pt.x,
                          pt.y,
                          pSysTray->GetHWnd(),
                          NULL);

    DestroyMenu(PowerSchemeContext.hPopup);

    if (id != 0)
        SetActivePwrScheme(id - 1, NULL, NULL);
}
Exemple #7
0
static VOID
Pos_SaveData(HWND hwndDlg)
{
	INT iCurSel=0,tmp=0;

	iCurSel = (INT) SendDlgItemMessage(hwndDlg, IDC_ENERGYLIST,
		CB_GETCURSEL,
		0,
		0);
	if (iCurSel == CB_ERR)
		return;

    tmp = (INT) SendDlgItemMessage(hwndDlg, IDC_MONITORDCLIST,
				   CB_GETCURSEL,
				   0,
				   (LPARAM)0);
	if (tmp > 0 && tmp < 16)
	{
		gPP[iCurSel].user.VideoTimeoutAc = Sec[tmp];
	}
    tmp = (INT) SendDlgItemMessage(hwndDlg, IDC_MONITORDCLIST,
				   CB_GETCURSEL,
				   0,
				   (LPARAM)0);
	if (tmp > 0 && tmp < 16)
	{
		gPP[iCurSel].user.VideoTimeoutDc = Sec[tmp];
	}
    tmp = (INT) SendDlgItemMessage(hwndDlg, IDC_DISKACLIST,
				   CB_GETCURSEL,
				   0,
				   (LPARAM)0);
	if (tmp > 0 && tmp < 16)
	{
		gPP[iCurSel].user.SpindownTimeoutAc = Sec[tmp+2];
	}
    tmp = (INT) SendDlgItemMessage(hwndDlg, IDC_DISKDCLIST,
				   CB_GETCURSEL,
				   0,
				   (LPARAM)0);
	if (tmp > 0 && tmp < 16)
	{
		gPP[iCurSel].user.SpindownTimeoutDc = Sec[tmp+2];
	}
    tmp = (INT) SendDlgItemMessage(hwndDlg, IDC_STANDBYACLIST,
				   CB_GETCURSEL,
				   0,
				   (LPARAM)0);
	if (tmp > 0 && tmp < 16)
	{
		gPP[iCurSel].user.IdleTimeoutAc = Sec[tmp];
	}
    tmp = (INT) SendDlgItemMessage(hwndDlg, IDC_STANDBYDCLIST,
				   CB_GETCURSEL,
				   0,
				   (LPARAM)0);
	if (tmp > 0 && tmp < 16)
	{
		gPP[iCurSel].user.IdleTimeoutDc = Sec[tmp];
	}
    tmp = (INT) SendDlgItemMessage(hwndDlg, IDC_HYBERNATEACLIST,
				   CB_GETCURSEL,
				   0,
				   (LPARAM)0);
	if (tmp > 0 && tmp < 16)
	{
		gPP[iCurSel].mach.DozeS4TimeoutAc = Sec[tmp];
	}
    tmp = (INT) SendDlgItemMessage(hwndDlg, IDC_HYBERNATEDCLIST,
				   CB_GETCURSEL,
				   0,
				   (LPARAM)0);
	if (tmp > 0 && tmp < 16)
	{
		gPP[iCurSel].mach.DozeS4TimeoutDc = Sec[tmp];
	}

    SetActivePwrScheme(iCurSel,NULL,&gPP[iCurSel]);
	LoadConfig(hwndDlg);
}
//------------------------------------------------------------------
// check power scheme integrity
//
// check: 0=no consistency check
//        1=check power scheme only
//        2=check processor scheme only
//        3=check both power and processor scheme
//------------------------------------------------------------------
BOOL checkProfile( int check )
{
  log( _T("Checking power scheme integrity ...") );
  
  CString err;
  err.LoadStringW( IDS_MAINERR2 );

  POWER_POLICY policyTemp;
  MACHINE_PROCESSOR_POWER_POLICY machTemp;
  static TCHAR msg[1024];   // für diverse Fehlertexte
  BOOL forceActivate = FALSE;

  if( !ReadPwrScheme(internalProfile,&policyTemp) )
  {
    log( _T("Recreating power scheme ...") );
    UINT x = internalProfile;

    if( !WritePwrScheme(&x,L"Speedswitch Control",L"\0\0",&internalPolicy) )
    {
      CString s1;
      s1.LoadStringW( IDS_SW1 );
      wsprintf( msg, s1, GetLastError() );
      MessageBox( NULL, msg, err, MB_ICONEXCLAMATION|MB_OK );
      return FALSE;
    }

    forceActivate = TRUE;
  }
  else if( check&1 )
  {
    if( !comparePowerProfile(policyTemp,internalPolicy) )
    {
      log( _T("Restoring power scheme data ...") );
      UINT x = internalProfile;

      if( !WritePwrScheme(&x,L"Speedswitch Control",L"\0\0",&internalPolicy) )
      {
        CString s1;
        s1.LoadStringW( IDS_SW2 );
        wsprintf( msg, s1, GetLastError() );
        MessageBox( NULL, msg, err, MB_ICONEXCLAMATION|MB_OK );
        return FALSE;
      }

      forceActivate = TRUE;
    }
  }

  if( !ReadProcessorPwrScheme(internalProfile,&machTemp) )
  {
    log( _T("Recreating processor scheme ...") );
    if( !WriteProcessorPwrScheme(internalProfile,&mach) )
    {
      CString s1;
      s1.LoadStringW( IDS_SW3 );
      wsprintf( msg, s1, GetLastError() );
      MessageBox( NULL, msg, err, MB_ICONEXCLAMATION|MB_OK );
      return FALSE;
    }   

    forceActivate = TRUE;
  }
  else if( check&2 )
  {
    if( !compareCPUScheme(machTemp,mach) )
    {
      log( _T("Restoring processor scheme data ...") );
      if( !WriteProcessorPwrScheme(internalProfile,&mach) )
      {
        CString s1;
        s1.LoadStringW( IDS_SW4 );
        wsprintf( msg, s1, GetLastError() );
        MessageBox( NULL, msg, err, MB_ICONEXCLAMATION|MB_OK );
        return FALSE;
      }  

      forceActivate = TRUE;
    }
  }

  UINT profile;

  if( !GetActivePwrScheme(&profile) )
  {
    CString s1;
    s1.LoadStringW( IDS_SW5 );
    wsprintf( msg, s1, GetLastError() );
    MessageBox( NULL, msg, err, MB_OK|MB_ICONEXCLAMATION );
    return FALSE;
  }

  if( forceActivate || profile!=internalProfile )
  {
    log( _T("Reactivating power scheme (%d, %d) ..."), profile, internalProfile );
    if( !SetActivePwrScheme(internalProfile,NULL,NULL) )
    {
      CString s1;
      s1.LoadStringW( IDS_SW6 );
      wsprintf( msg, s1, GetLastError() );
      MessageBox( NULL, msg, err, MB_ICONEXCLAMATION|MB_OK );
      return FALSE;
    }
  }

  return TRUE;
}
Exemple #9
0
////////////////////////////////////////////////////////////
///
/// @brief 设置当前Windows下的电源使用方案
///
/// @param __in ULONG nStandby = PWR_NEVER               系统挂起时间,默认值为0
/// @param __in ULONG nHibernate = PWR_NEVER             系统待机时间,默认值为0
/// @param __in ULONG& nSpindownTime = PWR_NEVER         关闭硬盘时间,默认值为0
/// @param __in ULONG nVedioOffTime = PWR_NEVER          关闭监视器时间,默认值为0
///
/// @return HRESULT
///
/// @retval S_OK 正常返回
/// @retval 通过GetLastError()返回
///
////////////////////////////////////////////////////////////
HRESULT WindowsPowerLW::UpdateCurrentPowerScheme(
    __in ULONG nStandby,
    __in ULONG nHibernate,
    __in ULONG nSpindowTime,
    __in ULONG nVideoOffTime
    )
{
    DEBUG_XCOVER_MARK_LINE;
    /// 分为直流电源和交流电源
    if (SystemPowerStatus.ACLineStatus)                              /// 交流电源
    {
        DEBUG_XCOVER_MARK_LINE;
        if (!nStandby && !nHibernate)
        {
            DEBUG_XCOVER_MARK_LINE;
            pwrPolicy.user.IdleTimeoutAc   = 0;
            pwrPolicy.mach.DozeS4TimeoutAc = 0;
            pwrPolicy.user.IdleAc.Action   = PowerActionNone;
        }
        else if (!nStandby && nHibernate)
        {
            DEBUG_XCOVER_MARK_LINE;
            pwrPolicy.user.IdleTimeoutAc   = nHibernate;
            pwrPolicy.mach.DozeS4TimeoutAc = 0;
            pwrPolicy.user.IdleAc.Action   = PowerActionHibernate;
        }
        else
        {
            DEBUG_XCOVER_MARK_LINE;
            pwrPolicy.user.IdleTimeoutAc   = nStandby;
            pwrPolicy.mach.DozeS4TimeoutAc = 0;
            if (nHibernate > nStandby)
            {
                DEBUG_XCOVER_MARK_LINE;
                pwrPolicy.mach.DozeS4TimeoutAc = nHibernate - nStandby;
            }
            pwrPolicy.user.IdleAc.Action   = PowerActionSleep;
        }
        pwrPolicy.user.SpindownTimeoutAc = nSpindowTime;
        pwrPolicy.user.VideoTimeoutAc    = nVideoOffTime;
    }
    else                                                           /// 直流电源
    {
        DEBUG_XCOVER_MARK_LINE;
        if (!nStandby && !nHibernate)
        {
            DEBUG_XCOVER_MARK_LINE;
            pwrPolicy.user.IdleTimeoutDc   = 0;
            pwrPolicy.mach.DozeS4TimeoutDc = 0;
            pwrPolicy.user.IdleDc.Action   = PowerActionNone;
        }
        else if (!nStandby && nHibernate)
        {
            DEBUG_XCOVER_MARK_LINE;
            pwrPolicy.user.IdleTimeoutDc   = nHibernate;
            pwrPolicy.mach.DozeS4TimeoutDc = 0;
            pwrPolicy.user.IdleDc.Action   = PowerActionHibernate;
        }
        else
        {
            DEBUG_XCOVER_MARK_LINE;
            pwrPolicy.user.IdleTimeoutDc   = nStandby;
            pwrPolicy.mach.DozeS4TimeoutDc = 0;
            if (nHibernate > nStandby)
            {
                DEBUG_XCOVER_MARK_LINE;
                pwrPolicy.mach.DozeS4TimeoutDc = nHibernate - nStandby;
            }
            pwrPolicy.user.IdleDc.Action   = PowerActionSleep;
        }
        pwrPolicy.user.SpindownTimeoutDc   = nSpindowTime;
        pwrPolicy.user.VideoTimeoutDc      = nVideoOffTime;
    }
    DEBUG_XCOVER_MARK_LINE;
    if (!SetActivePwrScheme(nIndex,NULL,&pwrPolicy))
    {
        return HRESULT_FROM_WIN32(GetLastError());
    }
    DEBUG_XCOVER_MARK_LINE;
    return S_OK;
}