Attribute VB_Name = "modPowerMan"
' ----------------------------------------------------------------------------
'  Copyright (C) 2009 Noda (Yohan Lasorsa) - http://nodadev.wordpress.com
' ----------------------------------------------------------------------------
'  This software Is provided 'as-is', without any express or implied
'  warranty. In no event will the author be held liable for any damages
'  arising from the use of this software.
'
'  Permission is granted to anyone to use this software for any purpose,
'  including commercial applications, and to alter it and redistribute it
'  freely, subject to the following restrictions:
'
'  1. The origin of this software must not be misrepresented; you must not
'     claim that you wrote the original software. If you use this software
'     in a product, an acknowledgment in the product documentation would be
'     appreciated but is not required.
'  2. Altered source versions must be plainly marked as such, and must not be
'     misrepresented as being the original software.
'  3. This notice may not be removed or altered from any source distribution.
' ----------------------------------------------------------------------------

Option Explicit

Private Declare Function GetSystemPowerStatus Lib "kernel32.dll" (lpSystemPowerStatus As SYSTEM_POWER_STATUS) As Long

Public Const BF_CHARGING = 8
Public Const BF_NOBATTERY = 128

Private Type SYSTEM_POWER_STATUS
   ACLineStatus As Byte
   BatteryFlag As Byte
   BatteryLifePercent As Byte
   Reserved1 As Byte
   BatteryLifeTime As Long
   BatteryFullLifeTime As Long
End Type

Public Enum CPUMode
    Constant = 0
    Adaptive = 1
    None = 2
End Enum

Public powerStatus As SYSTEM_POWER_STATUS
Public AC_CPUMode As CPUMode
Public DC_CPUMode As CPUMode

Public Sub InitPowerProfile()
    Shell "powercfg /c a1ctl", vbHide
    Shell "powercfg /s a1ctl", vbHide
    SetCPUModes settings.procModeAC, settings.procModeDC
End Sub

Public Sub UpdatePowerStatus()
    GetSystemPowerStatus powerStatus
End Sub

Public Sub SetCurrentCPUMode(ByVal mode As CPUMode)
    Dim acdc As String
    UpdatePowerStatus
    If (powerStatus.ACLineStatus = 0) Then
        acdc = "dc " & CPUModeStr(mode)
        DC_CPUMode = mode
        settings.procModeDC = mode
    Else
        acdc = "ac " & CPUModeStr(mode)
        AC_CPUMode = mode
        settings.procModeAC = mode
    End If
    Shell "powercfg /x a1ctl /processor-throttle-" & acdc, vbHide
    WriteSettings
End Sub

Public Function GetCurrentCPUMode() As CPUMode
    UpdatePowerStatus
    If (powerStatus.ACLineStatus = 0) Then
        GetCurrentCPUMode = DC_CPUMode
    Else
        GetCurrentCPUMode = AC_CPUMode
    End If
End Function

Public Sub SetCPUModes(ByVal ACMode As CPUMode, ByVal DCMode As CPUMode)
    Dim acdc As String
    UpdatePowerStatus
    AC_CPUMode = ACMode
    DC_CPUMode = DCMode
    settings.procModeAC = ACMode
    settings.procModeDC = DCMode
    Shell "powercfg /x a1ctl /processor-throttle-ac " & CPUModeStr(ACMode), vbHide
    Shell "powercfg /x a1ctl /processor-throttle-dc " & CPUModeStr(DCMode), vbHide
End Sub

Private Function CPUModeStr(ByVal mode As CPUMode) As String
    Select Case mode
        Case Constant
            CPUModeStr = "constant"
        Case Adaptive
            CPUModeStr = "adaptive"
        Case None
            CPUModeStr = "none"
    End Select
End Function
