2009-02-10 18:37:48 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2002 Kimmo Pekkola
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <windows.h>
|
2011-12-30 17:18:34 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2011-12-30 16:24:22 +00:00
|
|
|
#include <time.h>
|
2011-12-30 17:18:34 +00:00
|
|
|
#include <Powrprof.h>
|
2011-02-03 18:09:24 +00:00
|
|
|
#include "../../Library/Export.h" // Rainmeter's exported functions
|
2011-12-30 17:18:34 +00:00
|
|
|
|
2011-02-03 18:09:24 +00:00
|
|
|
#include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
|
2011-01-30 09:31:41 +00:00
|
|
|
|
2011-03-29 19:21:57 +00:00
|
|
|
typedef struct _PROCESSOR_POWER_INFORMATION
|
2011-01-08 08:17:59 +00:00
|
|
|
{
|
|
|
|
ULONG Number;
|
|
|
|
ULONG MaxMhz;
|
|
|
|
ULONG CurrentMhz;
|
|
|
|
ULONG MhzLimit;
|
|
|
|
ULONG MaxIdleState;
|
|
|
|
ULONG CurrentIdleState;
|
2009-02-10 18:37:48 +00:00
|
|
|
} PROCESSOR_POWER_INFORMATION, *PPROCESSOR_POWER_INFORMATION;
|
|
|
|
|
2011-12-30 17:18:34 +00:00
|
|
|
typedef LONG (WINAPI *FPCALLNTPOWERINFORMATION)(POWER_INFORMATION_LEVEL, PVOID, ULONG, PVOID, ULONG);
|
|
|
|
|
|
|
|
/* The exported functions */
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
__declspec( dllexport ) UINT Initialize(HMODULE instance, LPCTSTR iniFile, LPCTSTR section, UINT id);
|
|
|
|
__declspec( dllexport ) void Finalize(HMODULE instance, UINT id);
|
|
|
|
__declspec( dllexport ) LPCTSTR GetString(UINT id, UINT flags);
|
|
|
|
__declspec( dllexport ) double Update2(UINT id);
|
|
|
|
__declspec( dllexport ) UINT GetPluginVersion();
|
|
|
|
__declspec( dllexport ) LPCTSTR GetPluginAuthor();
|
|
|
|
}
|
|
|
|
|
|
|
|
enum POWER_STATE
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
POWER_UNKNOWN,
|
|
|
|
POWER_ACLINE,
|
|
|
|
POWER_STATUS,
|
|
|
|
POWER_STATUS2,
|
|
|
|
POWER_LIFETIME,
|
|
|
|
POWER_PERCENT,
|
2011-01-08 08:17:59 +00:00
|
|
|
POWER_MHZ,
|
|
|
|
POWER_HZ
|
2009-02-10 18:37:48 +00:00
|
|
|
};
|
|
|
|
|
2011-12-30 17:18:34 +00:00
|
|
|
std::map<UINT, POWER_STATE> g_States;
|
|
|
|
std::map<UINT, std::wstring> g_Formats;
|
|
|
|
HINSTANCE hDLL = NULL;
|
|
|
|
int g_Instances, g_NumOfProcessors = 0;
|
|
|
|
FPCALLNTPOWERINFORMATION fpCallNtPowerInformation = NULL;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2011-10-12 13:34:04 +00:00
|
|
|
void NullCRTInvalidParameterHandler(const wchar_t* expression, const wchar_t* function, const wchar_t* file, unsigned int line, uintptr_t pReserved)
|
|
|
|
{
|
|
|
|
// Do nothing.
|
|
|
|
}
|
|
|
|
|
2011-12-30 17:18:34 +00:00
|
|
|
/*
|
|
|
|
This function is called when the measure is initialized.
|
|
|
|
The function must return the maximum value that can be measured.
|
|
|
|
The return value can also be 0, which means that Rainmeter will
|
|
|
|
track the maximum value automatically. The parameters for this
|
|
|
|
function are:
|
2011-12-30 16:24:22 +00:00
|
|
|
|
2011-12-30 17:18:34 +00:00
|
|
|
instance The instance of this DLL
|
|
|
|
iniFile The name of the ini-file (usually Rainmeter.ini)
|
|
|
|
section The name of the section in the ini-file for this measure
|
|
|
|
id The identifier for the measure. This is used to identify the measures that use the same plugin.
|
|
|
|
*/
|
|
|
|
UINT Initialize(HMODULE instance, LPCTSTR iniFile, LPCTSTR section, UINT id)
|
|
|
|
{
|
|
|
|
g_Instances++;
|
|
|
|
if (hDLL == NULL)
|
2011-01-08 08:17:59 +00:00
|
|
|
{
|
2011-12-30 17:18:34 +00:00
|
|
|
hDLL = LoadLibrary(L"powrprof.dll");
|
|
|
|
if (hDLL)
|
|
|
|
{
|
|
|
|
fpCallNtPowerInformation = (FPCALLNTPOWERINFORMATION)GetProcAddress(hDLL, "CallNtPowerInformation");
|
|
|
|
}
|
2011-01-08 08:17:59 +00:00
|
|
|
}
|
|
|
|
|
2011-12-30 17:18:34 +00:00
|
|
|
POWER_STATE powerState = POWER_UNKNOWN;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2011-12-30 17:18:34 +00:00
|
|
|
SYSTEM_INFO systemInfo = {0};
|
|
|
|
GetSystemInfo(&systemInfo);
|
|
|
|
g_NumOfProcessors = (int)systemInfo.dwNumberOfProcessors;
|
|
|
|
|
|
|
|
/* Read our own settings from the ini-file */
|
|
|
|
LPCTSTR type = ReadConfigString(section, L"PowerState", L"");
|
|
|
|
if (type)
|
2011-12-30 16:24:22 +00:00
|
|
|
{
|
2011-12-30 17:18:34 +00:00
|
|
|
if (_wcsicmp(L"ACLINE", type) == 0)
|
|
|
|
{
|
|
|
|
powerState = POWER_ACLINE;
|
|
|
|
}
|
|
|
|
else if (_wcsicmp(L"STATUS", type) == 0)
|
|
|
|
{
|
|
|
|
powerState = POWER_STATUS;
|
|
|
|
}
|
|
|
|
else if (_wcsicmp(L"STATUS2", type) == 0)
|
|
|
|
{
|
|
|
|
powerState = POWER_STATUS2;
|
|
|
|
}
|
|
|
|
else if (_wcsicmp(L"LIFETIME", type) == 0)
|
|
|
|
{
|
|
|
|
powerState= POWER_LIFETIME;
|
|
|
|
|
|
|
|
LPCTSTR format = ReadConfigString(section, L"Format", L"%H:%M");
|
|
|
|
if (format)
|
|
|
|
{
|
|
|
|
g_Formats[id] = format;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (_wcsicmp(L"MHZ", type) == 0)
|
|
|
|
{
|
|
|
|
powerState= POWER_MHZ;
|
|
|
|
}
|
|
|
|
else if (_wcsicmp(L"HZ", type) == 0)
|
|
|
|
{
|
|
|
|
powerState= POWER_HZ;
|
|
|
|
}
|
|
|
|
else if (_wcsicmp(L"PERCENT", type) == 0)
|
|
|
|
{
|
|
|
|
powerState = POWER_PERCENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_States[id] = powerState;
|
2011-12-30 16:24:22 +00:00
|
|
|
}
|
2011-12-30 17:18:34 +00:00
|
|
|
|
|
|
|
switch(powerState)
|
2011-12-30 16:24:22 +00:00
|
|
|
{
|
2011-12-30 17:18:34 +00:00
|
|
|
case POWER_ACLINE:
|
|
|
|
return 1;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2011-12-30 17:18:34 +00:00
|
|
|
case POWER_STATUS:
|
|
|
|
return 4;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2011-12-30 17:18:34 +00:00
|
|
|
case POWER_STATUS2:
|
|
|
|
return 255;
|
|
|
|
|
|
|
|
case POWER_LIFETIME:
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-12-30 17:18:34 +00:00
|
|
|
SYSTEM_POWER_STATUS status;
|
|
|
|
if (GetSystemPowerStatus(&status))
|
|
|
|
{
|
|
|
|
return status.BatteryFullLifeTime;
|
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2011-12-30 17:18:34 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case POWER_PERCENT:
|
|
|
|
return 100;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2011-12-30 17:18:34 +00:00
|
|
|
|
|
|
|
return 0;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
2011-12-30 17:18:34 +00:00
|
|
|
/*
|
|
|
|
This function is called when new value should be measured.
|
|
|
|
The function returns the new value.
|
|
|
|
*/
|
|
|
|
double Update2(UINT id)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-12-30 17:18:34 +00:00
|
|
|
SYSTEM_POWER_STATUS status;
|
|
|
|
if (GetSystemPowerStatus(&status))
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-12-30 17:18:34 +00:00
|
|
|
std::map<UINT, POWER_STATE>::iterator i = g_States.find(id);
|
|
|
|
if (i != g_States.end())
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-12-30 17:18:34 +00:00
|
|
|
switch ((*i).second)
|
2011-12-30 16:24:22 +00:00
|
|
|
{
|
2011-12-30 17:18:34 +00:00
|
|
|
case POWER_ACLINE:
|
|
|
|
return status.ACLineStatus == 1 ? 1 : 0;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2011-12-30 17:18:34 +00:00
|
|
|
case POWER_STATUS:
|
|
|
|
if (status.BatteryFlag & 128)
|
|
|
|
{
|
|
|
|
return 0; // No battery
|
|
|
|
}
|
|
|
|
else if (status.BatteryFlag & 8)
|
|
|
|
{
|
|
|
|
return 1; // Charging
|
|
|
|
}
|
|
|
|
else if (status.BatteryFlag & 4)
|
|
|
|
{
|
|
|
|
return 2; // Critical
|
|
|
|
}
|
|
|
|
else if (status.BatteryFlag & 2)
|
|
|
|
{
|
|
|
|
return 3; // Low
|
|
|
|
}
|
|
|
|
else if (status.BatteryFlag & 1)
|
|
|
|
{
|
|
|
|
return 4; // High
|
|
|
|
}
|
|
|
|
break;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2011-12-30 17:18:34 +00:00
|
|
|
case POWER_STATUS2:
|
|
|
|
return status.BatteryFlag;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2011-12-30 17:18:34 +00:00
|
|
|
case POWER_LIFETIME:
|
|
|
|
return status.BatteryLifeTime;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2011-12-30 17:18:34 +00:00
|
|
|
case POWER_PERCENT:
|
|
|
|
return status.BatteryLifePercent > 100 ? 100 : status.BatteryLifePercent;
|
|
|
|
|
|
|
|
case POWER_MHZ:
|
|
|
|
case POWER_HZ:
|
|
|
|
if (fpCallNtPowerInformation && g_NumOfProcessors > 0)
|
|
|
|
{
|
|
|
|
PROCESSOR_POWER_INFORMATION* ppi = new PROCESSOR_POWER_INFORMATION[g_NumOfProcessors];
|
|
|
|
memset(ppi, 0, sizeof(PROCESSOR_POWER_INFORMATION) * g_NumOfProcessors);
|
|
|
|
fpCallNtPowerInformation(ProcessorInformation, NULL, 0, ppi, sizeof(PROCESSOR_POWER_INFORMATION) * g_NumOfProcessors);
|
|
|
|
double value = ((*i).second == POWER_MHZ) ? ppi[0].CurrentMhz : ppi[0].CurrentMhz * 1000000.0;
|
|
|
|
delete [] ppi;
|
|
|
|
return value;
|
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-30 17:18:34 +00:00
|
|
|
return 0;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2011-10-12 13:34:04 +00:00
|
|
|
|
2011-12-30 17:18:34 +00:00
|
|
|
/*
|
|
|
|
This function is called when the value should be
|
|
|
|
returned as a string.
|
|
|
|
*/
|
|
|
|
LPCTSTR GetString(UINT id, UINT flags)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-10-12 13:34:04 +00:00
|
|
|
static WCHAR buffer[128];
|
2011-12-30 17:18:34 +00:00
|
|
|
std::map<UINT, POWER_STATE>::iterator i = g_States.find(id);
|
|
|
|
if (i != g_States.end())
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-12-30 17:18:34 +00:00
|
|
|
if ((*i).second == POWER_LIFETIME)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-12-30 17:18:34 +00:00
|
|
|
SYSTEM_POWER_STATUS status;
|
|
|
|
if (GetSystemPowerStatus(&status))
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-12-30 17:18:34 +00:00
|
|
|
// Change it to time string
|
|
|
|
if (status.BatteryLifeTime == -1)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-12-30 17:18:34 +00:00
|
|
|
return L"Unknown";
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2011-12-30 17:18:34 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
std::map<UINT, std::wstring>::iterator iter = g_Formats.find(id);
|
|
|
|
if (iter != g_Formats.end())
|
|
|
|
{
|
|
|
|
tm time = {0};
|
|
|
|
time.tm_sec = status.BatteryLifeTime % 60;
|
|
|
|
time.tm_min = (status.BatteryLifeTime / 60) % 60;
|
|
|
|
time.tm_hour = status.BatteryLifeTime / 60 / 60;
|
|
|
|
|
|
|
|
_invalid_parameter_handler oldHandler = _set_invalid_parameter_handler(NullCRTInvalidParameterHandler);
|
|
|
|
_CrtSetReportMode(_CRT_ASSERT, 0);
|
2011-10-12 13:34:04 +00:00
|
|
|
|
2011-12-30 17:18:34 +00:00
|
|
|
errno = 0;
|
|
|
|
wcsftime(buffer, 128, (*iter).second.c_str(), &time);
|
|
|
|
if (errno == EINVAL)
|
|
|
|
{
|
|
|
|
buffer[0] = L'\0';
|
|
|
|
}
|
2011-10-12 13:34:04 +00:00
|
|
|
|
2011-12-30 17:18:34 +00:00
|
|
|
_set_invalid_parameter_handler(oldHandler);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wsprintf(buffer, L"%i:%02i:%02i", status.BatteryLifeTime / 60 / 60, (status.BatteryLifeTime / 60) % 60, status.BatteryLifeTime % 60);
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-30 16:24:22 +00:00
|
|
|
return NULL;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
2011-12-30 17:18:34 +00:00
|
|
|
/*
|
|
|
|
If the measure needs to free resources before quitting.
|
|
|
|
The plugin can export Finalize function, which is called
|
|
|
|
when Rainmeter quits (or refreshes).
|
|
|
|
*/
|
|
|
|
void Finalize(HMODULE instance, UINT id)
|
|
|
|
{
|
|
|
|
std::map<UINT, POWER_STATE>::iterator i = g_States.find(id);
|
|
|
|
if (i != g_States.end())
|
|
|
|
{
|
|
|
|
g_States.erase(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<UINT, std::wstring>::iterator i2 = g_Formats.find(id);
|
|
|
|
if (i2 != g_Formats.end())
|
|
|
|
{
|
|
|
|
g_Formats.erase(i2);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_Instances--;
|
|
|
|
if (hDLL != NULL && g_Instances == 0)
|
|
|
|
{
|
|
|
|
FreeLibrary(hDLL);
|
|
|
|
hDLL = NULL;
|
|
|
|
fpCallNtPowerInformation = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT GetPluginVersion()
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-12-30 17:18:34 +00:00
|
|
|
return 1004;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2011-12-30 17:18:34 +00:00
|
|
|
|
|
|
|
LPCTSTR GetPluginAuthor()
|
|
|
|
{
|
|
|
|
return L"Rainy (rainy@iki.fi)";
|
|
|
|
}
|