rainmeter-studio/Plugins/PluginProcess/PluginProcess.cpp

111 lines
2.9 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2011-12-16 19:53:12 +00:00
*/
#include <windows.h>
#include <TlHelp32.h>
2012-01-08 17:35:29 +00:00
#include <algorithm>
#include <vector>
2013-05-31 14:34:36 +00:00
#include "../../Common/RawString.h"
2011-12-16 19:53:12 +00:00
#include "../../Library/Export.h" // Rainmeter's exported functions
#include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
struct MeasureData
{
2013-05-31 14:18:52 +00:00
RawString processName;
2011-12-16 19:53:12 +00:00
bool isRunning;
MeasureData() : isRunning(false) {}
};
static UINT g_UpdateCount = 0;
2012-01-08 17:35:29 +00:00
static std::vector<MeasureData*> g_Measures;
2011-12-16 19:53:12 +00:00
void CheckProcesses();
PLUGIN_EXPORT void Initialize(void** data, void* rm)
2011-12-16 19:53:12 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = new MeasureData;
g_Measures.push_back(measure);
2011-12-16 19:53:12 +00:00
2012-01-08 17:35:29 +00:00
*data = measure;
2011-12-16 19:53:12 +00:00
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
2011-12-16 19:53:12 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
2011-12-16 19:53:12 +00:00
2012-01-08 17:35:29 +00:00
LPCWSTR value = RmReadString(rm, L"ProcessName", L"");
measure->processName = value;
2011-12-16 19:53:12 +00:00
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT double Update(void* data)
2011-12-16 19:53:12 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
// Updates the measure only once per combined updates of all measures
++g_UpdateCount;
if (g_UpdateCount >= g_Measures.size())
2011-12-16 19:53:12 +00:00
{
2012-01-08 17:35:29 +00:00
CheckProcesses();
g_UpdateCount = 0;
2011-12-16 19:53:12 +00:00
}
2012-01-08 17:35:29 +00:00
return measure->isRunning ? 1.0 : -1.0;
2011-12-30 17:18:34 +00:00
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT void Finalize(void* data)
2011-12-30 17:18:34 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
std::vector<MeasureData*>::iterator iter = std::find(g_Measures.begin(), g_Measures.end(), measure);
g_Measures.erase(iter);
delete measure;
2011-12-16 19:53:12 +00:00
}
void CheckProcesses()
{
// Set everything to false
2012-01-08 17:35:29 +00:00
std::vector<MeasureData*>::iterator iter = g_Measures.begin();
for ( ; iter != g_Measures.end(); ++iter)
2011-12-16 19:53:12 +00:00
{
2012-01-08 17:35:29 +00:00
(*iter)->isRunning = false;
2011-12-16 19:53:12 +00:00
}
HANDLE thSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (thSnapshot != INVALID_HANDLE_VALUE)
2011-12-16 19:53:12 +00:00
{
PROCESSENTRY32 processEntry = {sizeof(processEntry)};
if (Process32First(thSnapshot, &processEntry))
2011-12-16 19:53:12 +00:00
{
do
2011-12-16 19:53:12 +00:00
{
for (MeasureData* data : g_Measures)
2011-12-16 19:53:12 +00:00
{
if (_wcsicmp(processEntry.szExeFile, data->processName.c_str()) == 0)
2011-12-16 19:53:12 +00:00
{
data->isRunning = true;
2011-12-16 19:53:12 +00:00
}
}
}
while (Process32Next(thSnapshot, &processEntry));
2011-12-16 19:53:12 +00:00
}
CloseHandle(thSnapshot);
}
2011-12-16 19:53:12 +00:00
}