foobar2000 SDK  2015-08-03
CPowerRequest.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 
3 #ifdef _WIN32
4 
5 // win32 API declaration duplicate - not always defined on some of the Windows versions we target
6 namespace winapi_substitute {
7 
8  typedef struct _REASON_CONTEXT {
9  ULONG Version;
10  DWORD Flags;
11  union {
12  struct {
13  HMODULE LocalizedReasonModule;
14  ULONG LocalizedReasonId;
15  ULONG ReasonStringCount;
16  LPWSTR *ReasonStrings;
17 
18  } Detailed;
19 
20  LPWSTR SimpleReasonString;
21  } Reason;
23 
24  //
25  // Power Request APIs
26  //
27 
29 
30 }
31 
33  winapi_substitute::REASON_CONTEXT ctx = {POWER_REQUEST_CONTEXT_VERSION, POWER_REQUEST_CONTEXT_SIMPLE_STRING};
34  ctx.Reason.SimpleReasonString = const_cast<wchar_t*>(str);
35  return this->PowerCreateRequest(&ctx);
36 }
37 
38 CPowerRequest::CPowerRequest(const wchar_t * Reason) : m_Request(INVALID_HANDLE_VALUE), m_bSystem(), m_bDisplay() {
39  HMODULE kernel32 = GetModuleHandle(_T("kernel32.dll"));
40  if (m_API.IsValid()) {
41  winapi_substitute::REASON_CONTEXT ctx = {POWER_REQUEST_CONTEXT_VERSION, POWER_REQUEST_CONTEXT_SIMPLE_STRING};
42  ctx.Reason.SimpleReasonString = const_cast<wchar_t*>(Reason);
44  }
45 }
46 
47 void CPowerRequest::SetSystem(bool bSystem) {
48  if (bSystem == m_bSystem) return;
49  m_bSystem = bSystem;
50  if (m_Request != INVALID_HANDLE_VALUE) {
51  m_API.ToggleSystem( m_Request, bSystem );
52  } else {
53  _UpdateTES();
54  }
55 }
56 
57 void CPowerRequest::SetExecution(bool bExecution) {
58  if (bExecution == m_bSystem) return;
59  m_bSystem = bExecution;
60  if (m_Request != INVALID_HANDLE_VALUE) {
61  m_API.ToggleExecution( m_Request, bExecution );
62  } else {
63  _UpdateTES();
64  }
65 }
66 
67 void CPowerRequest::SetDisplay(bool bDisplay) {
68  if (bDisplay == m_bDisplay) return;
69  m_bDisplay = bDisplay;
70  if (m_Request != INVALID_HANDLE_VALUE) {
71  m_API.ToggleDisplay(m_Request, bDisplay);
72  } else {
73  _UpdateTES();
74  }
75 }
76 
78  if (m_Request != INVALID_HANDLE_VALUE) {
79  CloseHandle(m_Request);
80  } else {
81  if (m_bDisplay || m_bSystem) SetThreadExecutionState(ES_CONTINUOUS);
82  }
83 }
84 
86  SetThreadExecutionState(ES_CONTINUOUS | (m_bSystem ? ES_SYSTEM_REQUIRED : 0 ) | (m_bDisplay ? ES_DISPLAY_REQUIRED : 0) );
87 }
88 #endif // _WIN32
89 
void ToggleExecution(HANDLE hRequest, bool bSystem)
Definition: CPowerRequest.h:34
void SetDisplay(bool bDisplay)
void SetSystem(bool bSystem)
union winapi_substitute::_REASON_CONTEXT::@14 Reason
struct winapi_substitute::_REASON_CONTEXT * PREASON_CONTEXT
REASON_CONTEXT * PPOWER_REQUEST_CONTEXT
HANDLE PowerCreateRequestNamed(const wchar_t *str)
HANDLE m_Request
Definition: CPowerRequest.h:82
typedef HANDLE(WINAPI *pPowerCreateRequest_t)(__in void *Context)
REASON_CONTEXT POWER_REQUEST_CONTEXT
REASON_CONTEXT * LPPOWER_REQUEST_CONTEXT
pPowerCreateRequest_t PowerCreateRequest
Definition: CPowerRequest.h:63
CPowerRequestAPI m_API
Definition: CPowerRequest.h:84
CPowerRequest(const wchar_t *Reason)
void ToggleSystem(HANDLE hRequest, bool bSystem)
Definition: CPowerRequest.h:30
void ToggleDisplay(HANDLE hRequest, bool bDisplay)
Definition: CPowerRequest.h:40
struct winapi_substitute::_REASON_CONTEXT REASON_CONTEXT
void SetExecution(bool bExecution)