foobar2000 SDK  2015-08-03
CPowerRequest.h
Go to the documentation of this file.
1 #ifdef _WIN32
2 
3 typedef HANDLE (WINAPI * pPowerCreateRequest_t) (
4  __in void* Context
5  );
6 
7 typedef BOOL (WINAPI * pPowerSetRequest_t) (
8  __in HANDLE PowerRequest,
9  __in POWER_REQUEST_TYPE RequestType
10  );
11 
12 typedef BOOL (WINAPI * pPowerClearRequest_t) (
13  __in HANDLE PowerRequest,
14  __in POWER_REQUEST_TYPE RequestType
15  );
16 
18 public:
20  Bind();
21  }
22  bool Bind() {
23  HMODULE kernel32 = GetModuleHandle(_T("kernel32.dll"));
24  return Bind(PowerCreateRequest, kernel32, "PowerCreateRequest")
25  && Bind(PowerSetRequest, kernel32, "PowerSetRequest")
26  && Bind(PowerClearRequest, kernel32, "PowerClearRequest") ;
27  }
28  bool IsValid() {return PowerCreateRequest != NULL;}
29 
30  void ToggleSystem(HANDLE hRequest, bool bSystem) {
31  Toggle(hRequest, bSystem, PowerRequestSystemRequired);
32  }
33 
34  void ToggleExecution(HANDLE hRequest, bool bSystem) {
35  const POWER_REQUEST_TYPE _PowerRequestExecutionRequired = (POWER_REQUEST_TYPE)3;
36  const POWER_REQUEST_TYPE RequestType = IsWin8() ? _PowerRequestExecutionRequired : PowerRequestSystemRequired;
37  Toggle(hRequest, bSystem, RequestType);
38  }
39 
40  void ToggleDisplay(HANDLE hRequest, bool bDisplay) {
41  Toggle(hRequest, bDisplay, PowerRequestDisplayRequired);
42  }
43 
44  void Toggle(HANDLE hRequest, bool bToggle, POWER_REQUEST_TYPE what) {
45  if (bToggle) {
46  PowerSetRequest(hRequest, what);
47  } else {
48  PowerClearRequest(hRequest, what);
49  }
50 
51  }
52  HANDLE PowerCreateRequestNamed( const wchar_t * str );
53 
54  static bool IsWin8() {
55  auto ver = myGetOSVersion();
56  return ver >= 0x602;
57  }
58  static WORD myGetOSVersion() {
59  const DWORD ver = GetVersion();
60  return (WORD)HIBYTE(LOWORD(ver)) | ((WORD)LOBYTE(LOWORD(ver)) << 8);
61  }
62 
63  pPowerCreateRequest_t PowerCreateRequest;
64  pPowerSetRequest_t PowerSetRequest;
65  pPowerClearRequest_t PowerClearRequest;
66 private:
67  template<typename func_t> static bool Bind(func_t & f, HMODULE dll, const char * name) {
68  f = reinterpret_cast<func_t>(GetProcAddress(dll, name));
69  return f != NULL;
70  }
71 };
72 
74 public:
75  CPowerRequest(const wchar_t * Reason);
76  void SetSystem(bool bSystem);
77  void SetExecution(bool bExecution);
78  void SetDisplay(bool bDisplay);
79  ~CPowerRequest();
80 private:
81  void _UpdateTES();
83  bool m_bSystem, m_bDisplay;
86  void operator=(const CPowerRequest&);
87 };
88 #else
89 
90 class CPowerRequest {
91 public:
92  CPowerRequest(const wchar_t * Reason) {}
93  void SetSystem(bool bSystem) {}
94  void SetExecution(bool bExecution) {}
95  void SetDisplay(bool bDisplay) {}
96 private:
98  void operator=(const CPowerRequest&);
99 };
100 
101 #endif
void ToggleExecution(HANDLE hRequest, bool bSystem)
Definition: CPowerRequest.h:34
void SetDisplay(bool bDisplay)
Definition: CPowerRequest.h:95
void SetSystem(bool bSystem)
Definition: CPowerRequest.h:93
static WORD myGetOSVersion()
Definition: CPowerRequest.h:58
typedef BOOL(WINAPI *pPowerSetRequest_t)(__in HANDLE PowerRequest
HANDLE PowerCreateRequestNamed(const wchar_t *str)
static bool Bind(func_t &f, HMODULE dll, const char *name)
Definition: CPowerRequest.h:67
HANDLE m_Request
Definition: CPowerRequest.h:82
void Toggle(HANDLE hRequest, bool bToggle, POWER_REQUEST_TYPE what)
Definition: CPowerRequest.h:44
typedef HANDLE(WINAPI *pPowerCreateRequest_t)(__in void *Context)
pPowerClearRequest_t PowerClearRequest
Definition: CPowerRequest.h:65
__in POWER_REQUEST_TYPE RequestType
Definition: CPowerRequest.h:10
pPowerCreateRequest_t PowerCreateRequest
Definition: CPowerRequest.h:63
CPowerRequestAPI m_API
Definition: CPowerRequest.h:84
static bool IsWin8()
Definition: CPowerRequest.h:54
CPowerRequest(const wchar_t *Reason)
Definition: CPowerRequest.h:92
void ToggleSystem(HANDLE hRequest, bool bSystem)
Definition: CPowerRequest.h:30
void ToggleDisplay(HANDLE hRequest, bool bDisplay)
Definition: CPowerRequest.h:40
pPowerSetRequest_t PowerSetRequest
Definition: CPowerRequest.h:64
void SetExecution(bool bExecution)
Definition: CPowerRequest.h:94