VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Begin VB.Form frmMain 
   Caption         =   "Main"
   ClientHeight    =   870
   ClientLeft      =   165
   ClientTop       =   735
   ClientWidth     =   2625
   Icon            =   "frmMenu.frx":0000
   LinkTopic       =   "Form1"
   ScaleHeight     =   870
   ScaleWidth      =   2625
   StartUpPosition =   3  'Windows Default
   Begin VB.PictureBox icoBatt 
      Height          =   255
      Left            =   720
      Picture         =   "frmMenu.frx":058A
      ScaleHeight     =   195
      ScaleWidth      =   195
      TabIndex        =   2
      Top             =   480
      Width           =   255
   End
   Begin VB.PictureBox imgBattIcon 
      AutoRedraw      =   -1  'True
      AutoSize        =   -1  'True
      BackColor       =   &H00FF00FF&
      BeginProperty Font 
         Name            =   "Small Fonts"
         Size            =   6.75
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   300
      Left            =   0
      ScaleHeight     =   16
      ScaleMode       =   3  'Pixel
      ScaleWidth      =   16
      TabIndex        =   1
      Top             =   360
      Width           =   300
   End
   Begin MSComctlLib.ImageList ilsIcons 
      Left            =   1560
      Top             =   240
      _ExtentX        =   1005
      _ExtentY        =   1005
      BackColor       =   -2147483643
      MaskColor       =   16711935
      _Version        =   393216
   End
   Begin VB.PictureBox imgMainIcon 
      AutoRedraw      =   -1  'True
      AutoSize        =   -1  'True
      BackColor       =   &H00FF00FF&
      BeginProperty Font 
         Name            =   "Small Fonts"
         Size            =   6
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00FFFFFF&
      Height          =   300
      Left            =   360
      Picture         =   "frmMenu.frx":0B14
      ScaleHeight     =   16
      ScaleMode       =   3  'Pixel
      ScaleWidth      =   16
      TabIndex        =   0
      Top             =   360
      Width           =   300
   End
   Begin VB.Timer TimerMon 
      Enabled         =   0   'False
      Left            =   1080
      Top             =   360
   End
   Begin VB.Menu mnuMenu 
      Caption         =   "Menu"
      Begin VB.Menu mnuAbout 
         Caption         =   "&About"
      End
      Begin VB.Menu mnuConfigure 
         Caption         =   "&Configure"
      End
      Begin VB.Menu sep3 
         Caption         =   "-"
      End
      Begin VB.Menu mnuDevices 
         Caption         =   "&Devices"
         Begin VB.Menu mnuLAN 
            Caption         =   "LAN"
         End
         Begin VB.Menu mnuWifi 
            Caption         =   "Wi-Fi"
         End
         Begin VB.Menu mnuWebcam 
            Caption         =   "Webcam"
         End
      End
      Begin VB.Menu mnuFan 
         Caption         =   "&Fan"
         Begin VB.Menu mnuOff 
            Caption         =   "Off"
         End
         Begin VB.Menu mnuSlow 
            Caption         =   "Slow"
         End
         Begin VB.Menu mnuAuto 
            Caption         =   "Auto"
         End
         Begin VB.Menu mnuCustom 
            Caption         =   "Custom"
         End
      End
      Begin VB.Menu mnuScreen 
         Caption         =   "&Screen"
         Begin VB.Menu mnuDownscaling 
            Caption         =   "Downscaling"
         End
         Begin VB.Menu mnu1024x768 
            Caption         =   "Custom"
         End
         Begin VB.Menu mnu1024x600 
            Caption         =   "1024x600"
         End
         Begin VB.Menu mnu800x600 
            Caption         =   "800x600"
         End
      End
      Begin VB.Menu sep2 
         Caption         =   "-"
      End
      Begin VB.Menu mnu800Mhz 
         Caption         =   "800 Mhz"
      End
      Begin VB.Menu mnuDynamic 
         Caption         =   "Dynamic"
      End
      Begin VB.Menu sep1 
         Caption         =   "-"
      End
      Begin VB.Menu mnuExit 
         Caption         =   "&Exit"
      End
   End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' ----------------------------------------------------------------------------
'  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 Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, _
    pnid As NOTIFYICONDATA) As Boolean
    
Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) As Integer

Private Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2
Private Const NIF_TIP = &H4
Private Const WM_MOUSEMOVE = &H200
Private Const WM_RBUTTONUP = &H205
Private Const WM_LBUTTONUP = &H202

Private Type NOTIFYICONDATA
    cbSize As Long
    hwnd As Long
    uID As Long
    uFlags As Long
    uCallbackMessage As Long
    hIcon As Long
    szTip As String * 64
End Type

Private trayIconData As NOTIFYICONDATA
Private battIconData As NOTIFYICONDATA
Private battIconVisible As Boolean
Private switchIcons As Boolean

Private Sub Form_Load()
    Dim i As Integer
    
    ' save window process address
    oldProcAddress = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf VB_WindowProc)
    
    ' check if another app instance already exists
    If App.PrevInstance Then
        MsgBox App.Title & " is already running!"
        End
    End If

    ' read settings
    ReadSettings
    
    ' check for ACPIEC patch on first launch
    If (Not ConfigFileExists) Then
        If (Not CheckACPIECPatched And Not settings.vistaOS And Not settings.useAltDriver) Then
            If (MsgBox("Your ACPIEC driver is not patched :" & vbNewLine & vbNewLine & _
                    "It is recommanded to patch it to prevent constant log writing, especially if you have a SSD." & vbNewLine & _
                    "This is completely safe, and can be undone later in the configuration dialog." & vbNewLine & vbNewLine & _
                    "Do you want to patch it now?", vbYesNo, "Warning") = VbMsgBoxResult.vbYes) Then
                    
                PatchACPIEC
            End If
        End If
        WriteSettings
    End If
    
    ' if OS is Vista, remove resolution & CPU frequency settings from menu
    If (settings.vistaOS) Then
        mnuScreen.Visible = False
        mnu800Mhz.Visible = False
        mnuDynamic.Visible = False
        sep2.Visible = False
    Else
        ' init intel registry hack
        InitVideoRegHack
        mnuDownscaling.Checked = settings.downscaling
    
        UpdateResMenu
        
        ' set custom resolution entry
        mnu1024x768.Caption = CStr(settings.resolutionWidth) & "x" & CStr(settings.resolutionHeight)
    End If
    
    ' check autostart
    If (HasStartupShortcut(App.EXEName)) Then
        settings.autostart = True
    Else
        settings.autostart = False
    End If

    ' check devices status
    DetectWebcamModel
    CheckDevicesStatus
    
    ' set power profile
    InitPowerProfile
    UpdateProcModeMenu
    
    ' init IO access
    If (Not settings.useAltDriver) Then
        i = 0
        While (i < 10 And Not InitIO)
            i = i + 1
            Sleep 100
        Wend
        If (i = 10) Then
            MsgBox "Error, can't initialize IO access."
            End
        End If
    End If
    
    ' init app
    Me.Visible = False
    UpdateFanMenu
    UpdateMainIcon NIM_ADD
    battIconVisible = False
    fanStatus = Unknown
    HDAPM = 128
    switchIcons = False
    imgBattIcon.ForeColor = settings.batteryTextColor
    TimerMon_Timer

    ' setup the timer
    TimerMon.Interval = settings.timerInterval
    TimerMon.Enabled = True
End Sub

Private Sub Form_Unload(Cancel As Integer)
    ' restore window process address
    Call SetWindowLong(Me.hwnd, GWL_WNDPROC, oldProcAddress)
    
    WriteSettings
    UpdateMainIcon NIM_DELETE
    UpdateBattIcon NIM_DELETE
    StartFanAuto
    
    If (Not settings.useAltDriver) Then
        CloseIO
    End If
End Sub

Private Sub CheckDevicesStatus()
    GetDevicesStatus
    mnuLAN.Checked = LANActive
    mnuWifi.Checked = WifiActive
    mnuWebcam.Checked = WebcamActive
End Sub

Private Sub UpdateProcModeMenu()
    Dim mode As Integer
    UpdatePowerStatus
    mode = GetCurrentCPUMode
    mnu800Mhz.Checked = False
    mnuDynamic.Checked = False
    Select Case mode
        Case CPUMode.Constant
            mnu800Mhz.Checked = True
        Case CPUMode.Adaptive
            mnuDynamic.Checked = True
    End Select
End Sub

Private Sub UpdateFanMenu()
    mnuOff.Checked = False
    mnuSlow.Checked = False
    mnuAuto.Checked = False
    mnuCustom.Checked = False
    Select Case settings.fanControl
        Case Off
            mnuOff.Checked = True
        Case Slow
            mnuSlow.Checked = True
        Case Auto
            mnuAuto.Checked = True
        Case Custom
            mnuCustom.Checked = True
    End Select
End Sub

Private Sub UpdateResMenu()
    mnu1024x768.Checked = False
    mnu1024x600.Checked = False
    mnu800x600.Checked = False
    
    ' check current screen resolution
    modeCustom = False
    If (GetScreenResX = 800 And GetScreenResY = 600) Then
        mnu800x600.Checked = True
    ElseIf (GetScreenResX = 1024 And GetScreenResY = 600) Then
        mnu1024x600.Checked = True
    ElseIf (GetScreenResX = settings.resolutionWidth And GetScreenResY = settings.resolutionHeight) Then
        mnu1024x768.Checked = True
        modeCustom = True
            
        If (settings.downscaling) Then
            SetDownscaling True
        End If
    End If
End Sub

Private Sub mnu1024x600_Click()
    Call ChangeScreenRes(1024, 600)
    mnu1024x600.Checked = True
    mnu1024x768.Checked = False
    mnu800x600.Checked = False
End Sub

Private Sub mnu1024x768_Click()
    Call ChangeScreenRes(settings.resolutionWidth, settings.resolutionHeight)
    mnu1024x600.Checked = False
    mnu1024x768.Checked = True
    mnu800x600.Checked = False
End Sub

Private Sub mnu800x600_Click()
    Call ChangeScreenRes(800, 600)
    mnu1024x600.Checked = False
    mnu1024x768.Checked = False
    mnu800x600.Checked = True
End Sub

Private Sub mnu800Mhz_Click()
    SetCurrentCPUMode Constant
    mnu800Mhz.Checked = True
    mnuDynamic.Checked = False
End Sub

Private Sub mnuDownscaling_Click()
    SetDownscaling (Not settings.downscaling)
    mnuDownscaling.Checked = settings.downscaling
    WriteSettings
End Sub

Private Sub mnuDynamic_Click()
    SetCurrentCPUMode Adaptive
    mnu800Mhz.Checked = False
    mnuDynamic.Checked = True
End Sub

Private Sub mnuOff_Click()
    settings.fanControl = Off
    WriteSettings
    UpdateFanMenu
End Sub

Private Sub mnuSlow_Click()
    settings.fanControl = Slow
    WriteSettings
    UpdateFanMenu
End Sub

Private Sub mnuAuto_Click()
    settings.fanControl = Auto
    WriteSettings
    UpdateFanMenu
End Sub

Private Sub mnuCustom_Click()
    settings.fanControl = Custom
    WriteSettings
    UpdateFanMenu
End Sub

Private Sub mnuAbout_Click()
    frmAbout.Show 1
End Sub

Private Sub mnuConfigure_Click()
    frmConfig.Show 1
End Sub

Private Sub mnuLAN_Click()
    SetLANStatus (Not LANActive)
    mnuLAN.Checked = LANActive
End Sub

Private Sub mnuWifi_Click()
    SetWifiStatus (Not WifiActive)
    mnuWifi.Checked = WifiActive
End Sub

Private Sub mnuWebcam_Click()
    SetWebcamStatus (Not WebcamActive)
    mnuWebcam.Checked = WebcamActive
End Sub

Private Sub mnuExit_Click()
    Unload Me
End Sub

Private Function SetTooltip(value As String)
    trayIconData.szTip = value & vbNullChar
End Function

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    ' the Form_MouseMove is intercepted to give systray mouse events
    Dim Msg As Integer
    Msg = IIf(Me.ScaleMode = vbPixels, x, x / Screen.TwipsPerPixelX)
    Select Case Msg
        Case WM_RBUTTONUP
            UpdateProcModeMenu
'            UpdateResMenu
            SetForegroundWindow hwnd
            PopupMenu mnuMenu
        Case WM_LBUTTONUP
            UpdateProcModeMenu
'            UpdateResMenu
            SetForegroundWindow hwnd
            PopupMenu mnuMenu
    End Select
End Sub

Private Sub UpdateMainIcon(value As Long)
    ' used to add, modify and delete icon
    With trayIconData
        .cbSize = Len(trayIconData)
        .hwnd = Me.hwnd
        .uID = 0
        .uFlags = NIM_DELETE Or NIF_TIP Or NIM_MODIFY
        .uCallbackMessage = WM_MOUSEMOVE
        .hIcon = imgMainIcon.Picture
    End With
    Shell_NotifyIcon value, trayIconData
End Sub

Private Sub UpdateBattIcon(value As Long)
    If (Not ((Not battIconVisible) And (value = NIM_DELETE))) Then
        ' used to add, modify and delete icon
        With battIconData
            .cbSize = Len(battIconData)
            .hwnd = Me.hwnd
            .uID = 1
            .uFlags = NIM_DELETE Or NIF_TIP Or NIM_MODIFY
'            .uCallbackMessage = WM_MOUSEMOVE
            .hIcon = imgBattIcon.Picture
        End With
        If ((Not battIconVisible) And (value = NIM_MODIFY)) Then
            Shell_NotifyIcon NIM_ADD, battIconData
        Else
            Shell_NotifyIcon value, battIconData
        End If
    End If
    battIconVisible = True
    If (value = NIM_DELETE) Then
        battIconVisible = False
    End If
End Sub

Private Sub UpdateTempIconText(temp As Byte)
    If (settings.fullSizeTempIcon) Then
        imgMainIcon.Picture = LoadPicture
        imgMainIcon.CurrentX = 0
        imgMainIcon.CurrentY = 2.25
        imgMainIcon.FontName = "MS Sans Serif"
        imgMainIcon.FontSize = 9
    Else
        imgMainIcon.Picture = Me.Icon
        imgMainIcon.CurrentX = 2
        imgMainIcon.CurrentY = 3
    End If
    
    If (GetCurrentCPUMode = Constant) Then
        imgMainIcon.ForeColor = settings.temp2TextColor
    Else
        imgMainIcon.ForeColor = settings.tempTextColor
    End If
    imgMainIcon.Print (Format(temp, "00") & "°")
    imgMainIcon.Picture = imgMainIcon.Image
    ilsIcons.ListImages.Add , "temp", imgMainIcon.Picture
    imgMainIcon.Picture = ilsIcons.ListImages("temp").ExtractIcon
    ilsIcons.ListImages.Clear
    UpdateMainIcon NIM_MODIFY
End Sub

Private Sub UpdateBattIconText()
    Dim min As Long
    min = powerStatus.BatteryLifeTime / 60
    imgBattIcon.Picture = icoBatt.Picture
    imgBattIcon.CurrentX = -1
    imgBattIcon.CurrentY = 7
    imgBattIcon.Print (Fix(min / 60) & ":" & Format(min Mod 60, "00"))
    imgBattIcon.Picture = imgBattIcon.Image
    ilsIcons.ListImages.Add , "batt", imgBattIcon.Picture
    If (settings.singleIconMode) Then
        UpdateBattIcon NIM_DELETE
        If (switchIcons) Then
            imgMainIcon.Picture = ilsIcons.ListImages("batt").ExtractIcon
            UpdateMainIcon NIM_MODIFY
        End If
    Else
        imgBattIcon.Picture = ilsIcons.ListImages("batt").ExtractIcon
        UpdateBattIcon NIM_MODIFY
    End If
    ilsIcons.ListImages.Clear
    switchIcons = Not switchIcons
End Sub

Public Sub TimerMon_Timer()
    Dim min As Integer
    Dim temp As Byte
    temp = GetCpuTemp
    
    If (temp > 0 And temp < 100) Then
        ' fan management
        If (settings.fanControl = Custom) Then
            If ((settings.fanStop > 0) Or (settings.fanSlow > 0)) Then
                If (((settings.fanStop > 0) And (fanStatus <> Stopped)) And _
                    (temp < settings.fanStop) Or _
                    ((settings.fanSlow = 0) And (settings.fanAuto = 0))) Then
                    StopFan
                ElseIf ((settings.fanAuto > 0) And (fanStatus <> AutoMode) And (temp > settings.fanAuto)) Then
                    StartFanAuto
                ElseIf (((settings.fanSlow > 0) And (fanStatus <> SlowMode)) And _
                    ((((fanStatus = AutoMode) Or (fanStatus = Unknown)) And (temp < settings.fanSlow)) Or _
                    ((fanStatus = Stopped) And ((settings.fanStop = 0) Or (temp > settings.fanSlow))) Or _
                    ((settings.fanStop = 0) And (settings.fanAuto = 0)))) Then
                    If (temp < settings.fanSlowThreshold) Then
                        If (fanStatus <> Stopped) Then
                            StopFan
                        End If
                    Else
                        StartFanSlow
                    End If
                End If
            ElseIf (fanStatus <> AutoMode) Then
                StartFanAuto
            End If
        ElseIf ((settings.fanControl = Off) And (fanStatus <> Stopped)) Then
            StopFan
        ElseIf ((settings.fanControl = Slow) And (fanStatus <> SlowMode)) Then
            StartFanSlow
        ElseIf ((settings.fanControl = Auto) And (fanStatus <> AutoMode)) Then
            StartFanAuto
        End If
        
        ' update tray icons
        UpdatePowerStatus
        UpdateTempIconText temp
        If (powerStatus.BatteryFlag = BF_CHARGING) Then
            SetTooltip "Charging: " & powerStatus.BatteryLifePercent & "%"
            UpdateBattIcon NIM_DELETE
        ElseIf (powerStatus.BatteryFlag = BF_NOBATTERY) Then
            SetTooltip "No battery"
            UpdateBattIcon NIM_DELETE
        ElseIf ((powerStatus.ACLineStatus = 0) And (powerStatus.BatteryLifeTime > 0)) Then
            min = powerStatus.BatteryLifeTime / 60
            SetTooltip Fix(min / 60) & ":" & Format(min Mod 60, "00") & " (" & powerStatus.BatteryLifePercent & "%)"
            If (settings.showTime) Then
                UpdateBattIconText
            Else
                UpdateBattIcon NIM_DELETE
            End If
        Else
            SetTooltip "Battery: " & powerStatus.BatteryLifePercent & "%"
            UpdateBattIcon NIM_DELETE
        End If
    End If
    
    ' HD management
    If (settings.noHDClicks And (HDAPM <> 254)) Then
        SetHDAPM 254
    ElseIf (Not settings.noHDClicks And (HDAPM = 254)) Then
        SetHDAPM 128
    End If
End Sub
