mirror of
https://github.com/debauchee/barrier.git
synced 2026-11-25 13:23:02 +08:00
Added win32 support for power management.
This commit is contained in:
@@ -15,11 +15,24 @@
|
||||
#include "CArchMiscWindows.h"
|
||||
#include "CArchDaemonWindows.h"
|
||||
|
||||
#ifndef ES_SYSTEM_REQUIRED
|
||||
#define ES_SYSTEM_REQUIRED ((DWORD)0x00000001)
|
||||
#endif
|
||||
#ifndef ES_DISPLAY_REQUIRED
|
||||
#define ES_DISPLAY_REQUIRED ((DWORD)0x00000002)
|
||||
#endif
|
||||
#ifndef ES_CONTINUOUS
|
||||
#define ES_CONTINUOUS ((DWORD)0x80000000)
|
||||
#endif
|
||||
typedef DWORD EXECUTION_STATE;
|
||||
|
||||
//
|
||||
// CArchMiscWindows
|
||||
//
|
||||
|
||||
CArchMiscWindows::CDialogs* CArchMiscWindows::s_dialogs = NULL;
|
||||
CArchMiscWindows::CDialogs* CArchMiscWindows::s_dialogs = NULL;
|
||||
DWORD CArchMiscWindows::s_busyState = 0;
|
||||
CArchMiscWindows::STES_t CArchMiscWindows::s_stes = NULL;
|
||||
|
||||
void
|
||||
CArchMiscWindows::init()
|
||||
@@ -143,6 +156,29 @@ CArchMiscWindows::hasValue(HKEY key, const TCHAR* name)
|
||||
(type == REG_DWORD || type == REG_SZ));
|
||||
}
|
||||
|
||||
CArchMiscWindows::EValueType
|
||||
CArchMiscWindows::typeOfValue(HKEY key, const TCHAR* name)
|
||||
{
|
||||
DWORD type;
|
||||
LONG result = RegQueryValueEx(key, name, 0, &type, NULL, NULL);
|
||||
if (result != ERROR_SUCCESS) {
|
||||
return kNO_VALUE;
|
||||
}
|
||||
switch (type) {
|
||||
case REG_DWORD:
|
||||
return kUINT;
|
||||
|
||||
case REG_SZ:
|
||||
return kSTRING;
|
||||
|
||||
case REG_BINARY:
|
||||
return kBINARY;
|
||||
|
||||
default:
|
||||
return kUNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CArchMiscWindows::setValue(HKEY key,
|
||||
const TCHAR* name, const std::string& value)
|
||||
@@ -164,14 +200,25 @@ CArchMiscWindows::setValue(HKEY key, const TCHAR* name, DWORD value)
|
||||
sizeof(DWORD));
|
||||
}
|
||||
|
||||
void
|
||||
CArchMiscWindows::setValueBinary(HKEY key,
|
||||
const TCHAR* name, const std::string& value)
|
||||
{
|
||||
assert(key != NULL);
|
||||
assert(name != NULL);
|
||||
RegSetValueEx(key, name, 0, REG_BINARY,
|
||||
reinterpret_cast<const BYTE*>(value.data()),
|
||||
value.size());
|
||||
}
|
||||
|
||||
std::string
|
||||
CArchMiscWindows::readValueString(HKEY key, const TCHAR* name)
|
||||
CArchMiscWindows::readBinaryOrString(HKEY key, const TCHAR* name, DWORD type)
|
||||
{
|
||||
// get the size of the string
|
||||
DWORD type;
|
||||
DWORD actualType;
|
||||
DWORD size = 0;
|
||||
LONG result = RegQueryValueEx(key, name, 0, &type, NULL, &size);
|
||||
if (result != ERROR_SUCCESS || type != REG_SZ) {
|
||||
LONG result = RegQueryValueEx(key, name, 0, &actualType, NULL, &size);
|
||||
if (result != ERROR_SUCCESS || actualType != type) {
|
||||
return std::string();
|
||||
}
|
||||
|
||||
@@ -179,19 +226,31 @@ CArchMiscWindows::readValueString(HKEY key, const TCHAR* name)
|
||||
char* buffer = new char[size];
|
||||
|
||||
// read it
|
||||
result = RegQueryValueEx(key, name, 0, &type,
|
||||
result = RegQueryValueEx(key, name, 0, &actualType,
|
||||
reinterpret_cast<BYTE*>(buffer), &size);
|
||||
if (result != ERROR_SUCCESS || type != REG_SZ) {
|
||||
if (result != ERROR_SUCCESS || actualType != type) {
|
||||
delete[] buffer;
|
||||
return std::string();
|
||||
}
|
||||
|
||||
// clean up and return value
|
||||
std::string value(buffer);
|
||||
std::string value(buffer, size);
|
||||
delete[] buffer;
|
||||
return value;
|
||||
}
|
||||
|
||||
std::string
|
||||
CArchMiscWindows::readValueString(HKEY key, const TCHAR* name)
|
||||
{
|
||||
return readBinaryOrString(key, name, REG_SZ);
|
||||
}
|
||||
|
||||
std::string
|
||||
CArchMiscWindows::readValueBinary(HKEY key, const TCHAR* name)
|
||||
{
|
||||
return readBinaryOrString(key, name, REG_BINARY);
|
||||
}
|
||||
|
||||
DWORD
|
||||
CArchMiscWindows::readValueInt(HKEY key, const TCHAR* name)
|
||||
{
|
||||
@@ -229,3 +288,55 @@ CArchMiscWindows::processDialog(MSG* msg)
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void
|
||||
CArchMiscWindows::addBusyState(DWORD busyModes)
|
||||
{
|
||||
s_busyState |= busyModes;
|
||||
setThreadExecutionState(s_busyState);
|
||||
}
|
||||
|
||||
void
|
||||
CArchMiscWindows::removeBusyState(DWORD busyModes)
|
||||
{
|
||||
s_busyState &= ~busyModes;
|
||||
setThreadExecutionState(s_busyState);
|
||||
}
|
||||
|
||||
void
|
||||
CArchMiscWindows::setThreadExecutionState(DWORD busyModes)
|
||||
{
|
||||
// look up function dynamically so we work on older systems
|
||||
if (s_stes == NULL) {
|
||||
HINSTANCE kernel = LoadLibrary("kernel32.dll");
|
||||
if (kernel != NULL) {
|
||||
s_stes = reinterpret_cast<STES_t>(GetProcAddress(kernel,
|
||||
"SetThreadExecutionState"));
|
||||
}
|
||||
if (s_stes == NULL) {
|
||||
s_stes = &CArchMiscWindows::dummySetThreadExecutionState;
|
||||
}
|
||||
}
|
||||
|
||||
// convert to STES form
|
||||
EXECUTION_STATE state = 0;
|
||||
if ((busyModes & kSYSTEM) != 0) {
|
||||
state |= ES_SYSTEM_REQUIRED;
|
||||
}
|
||||
if ((busyModes & kDISPLAY) != 0) {
|
||||
state |= ES_DISPLAY_REQUIRED;
|
||||
}
|
||||
if (state != 0) {
|
||||
state |= ES_CONTINUOUS;
|
||||
}
|
||||
|
||||
// do it
|
||||
s_stes(state);
|
||||
}
|
||||
|
||||
DWORD
|
||||
CArchMiscWindows::dummySetThreadExecutionState(DWORD)
|
||||
{
|
||||
// do nothing
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user