rainmeter-studio/Plugins/PluginProcess/PluginProcess.cpp

170 lines
4.4 KiB
C++
Raw Normal View History

2011-12-16 19:53:12 +00:00
/*
Copyright (C) 2011 Birunthan Mohanathas
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>
#include <Psapi.h>
2011-12-30 17:18:34 +00:00
#include <string>
#include <map>
2011-12-16 19:53:12 +00:00
#include "../../Library/Export.h" // Rainmeter's exported functions
2011-12-30 17:18:34 +00:00
2011-12-16 19:53:12 +00:00
#include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
2011-12-30 17:18:34 +00:00
/* 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);
2012-01-01 13:26:29 +00:00
__declspec(dllexport) double Update2(UINT id);
2011-12-30 17:18:34 +00:00
__declspec(dllexport) UINT GetPluginVersion();
__declspec(dllexport) LPCTSTR GetPluginAuthor();
}
2011-12-16 19:53:12 +00:00
struct MeasureData
{
2011-12-30 17:18:34 +00:00
std::wstring processName;
2011-12-16 19:53:12 +00:00
bool isRunning;
MeasureData() : isRunning(false) {}
};
2011-12-30 17:18:34 +00:00
static std::map<UINT, MeasureData> g_Values;
2011-12-16 19:53:12 +00:00
static UINT g_UpdateCount = 0;
void CheckProcesses();
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:
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)
2011-12-16 19:53:12 +00:00
{
2011-12-30 17:18:34 +00:00
MeasureData data;
2011-12-16 19:53:12 +00:00
2011-12-30 17:18:34 +00:00
const WCHAR* value = ReadConfigString(section, L"ProcessName", L"");
if (*value)
{
data.processName = value;
g_Values[id] = std::move(data);
}
return 1;
2011-12-16 19:53:12 +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.
*/
2012-01-01 13:26:29 +00:00
double Update2(UINT id)
2011-12-16 19:53:12 +00:00
{
2012-01-01 13:26:29 +00:00
double result = -1.0;
2011-12-16 19:53:12 +00:00
2011-12-30 17:18:34 +00:00
std::map<UINT, MeasureData>::const_iterator iter = g_Values.find(id);
if (iter != g_Values.end())
{
// Updates the measure only once per combined updates of all measures
++g_UpdateCount;
if (g_UpdateCount >= g_Values.size())
{
CheckProcesses();
g_UpdateCount = 0;
}
2012-01-01 13:26:29 +00:00
result = (*iter).second.isRunning ? 1.0 : -1.0;
2011-12-30 17:18:34 +00:00
}
return result;
2011-12-16 19:53:12 +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)
2011-12-16 19:53:12 +00:00
{
2011-12-30 17:18:34 +00:00
std::map<UINT, MeasureData>::iterator iter = g_Values.find(id);
if (iter != g_Values.end())
2011-12-16 19:53:12 +00:00
{
2011-12-30 17:18:34 +00:00
g_Values.erase(iter);
2011-12-16 19:53:12 +00:00
}
}
2011-12-30 17:18:34 +00:00
UINT GetPluginVersion()
2011-12-16 19:53:12 +00:00
{
2011-12-30 17:18:34 +00:00
return 1000;
}
2011-12-30 17:18:34 +00:00
LPCTSTR GetPluginAuthor()
{
return L"Birunthan Mohanathas (poiru.net)";
2011-12-16 19:53:12 +00:00
}
void CheckProcesses()
{
// Set everything to false
2011-12-30 17:18:34 +00:00
std::map<UINT, MeasureData>::iterator iter = g_Values.begin();
for ( ; iter != g_Values.end(); ++iter)
2011-12-16 19:53:12 +00:00
{
2011-12-30 17:18:34 +00:00
(*iter).second.isRunning = false;
2011-12-16 19:53:12 +00:00
}
int bufSize = 256;
DWORD* pids = new DWORD[bufSize];
DWORD bytesReturned = 0;
while (!EnumProcesses(pids, bufSize * sizeof(DWORD), &bytesReturned) &&
bytesReturned == bufSize * sizeof(DWORD))
{
delete [] pids;
bufSize *= 2;
pids = new DWORD[bufSize];
}
for (UINT i = 0, isize = bytesReturned / sizeof(DWORD); i < isize; ++i)
{
const DWORD flags = PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
HANDLE hProcess = OpenProcess(flags, FALSE, pids[i]);
if (hProcess)
{
WCHAR buffer[MAX_PATH];
if (GetModuleBaseName(hProcess, NULL, buffer, _countof(buffer)))
{
2011-12-30 17:18:34 +00:00
iter = g_Values.begin();
for ( ; iter != g_Values.end(); ++iter)
2011-12-16 19:53:12 +00:00
{
2011-12-30 17:18:34 +00:00
if (_wcsicmp(buffer, (*iter).second.processName.c_str()) == 0)
2011-12-16 19:53:12 +00:00
{
2011-12-30 17:18:34 +00:00
(*iter).second.isRunning = true;
2011-12-16 19:53:12 +00:00
}
}
}
CloseHandle(hProcess);
}
}
delete [] pids;
}