1 /* QWinFF - a qt4 gui frontend for ffmpeg
2 * Copyright (C) 2011-2013 Timothy Lin <lzh9102@gmail.com>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /* Windows32 Implementation of PowerManagement class */
19
20 #include "powermanagement.h"
21 #include <windows.h>
22 #include <powrprof.h>
23
24 namespace
25 {
26
adjustPrivilegeForShutdown()27 bool adjustPrivilegeForShutdown()
28 {
29 HANDLE hCurrentProc = GetCurrentProcess();
30 HANDLE hToken;
31 LUID tmpLuid;
32 TOKEN_PRIVILEGES tkp;
33 LUID_AND_ATTRIBUTES luidAttr;
34
35 if (!OpenProcessToken(hCurrentProc
36 , TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY
37 , &hToken))
38 return false;
39
40 if (!LookupPrivilegeValueA("", "SeShutdownPrivilege", &tmpLuid))
41 return false;
42
43 luidAttr.Luid = tmpLuid;
44 luidAttr.Attributes = SE_PRIVILEGE_ENABLED;
45 tkp.PrivilegeCount = 1;
46 tkp.Privileges[0] = luidAttr;
47
48 if (!AdjustTokenPrivileges(hToken, false, &tkp
49 , sizeof(TOKEN_PRIVILEGES), 0, 0))
50 return false;
51
52 return true;
53 }
54
power_suspend()55 bool power_suspend()
56 {
57 /*
58 BOOLEAN WINAPI SetSuspendState(
59 __in BOOLEAN Hibernate,
60 __in BOOLEAN ForceCritical,
61 __in BOOLEAN DisableWakeEvent
62 );
63 */
64 bool hal_works = SetSuspendState(false, true, false);
65 return hal_works;
66 }
67
power_hibernate()68 bool power_hibernate()
69 {
70 /* SetSuspendState: see power_suspend() */
71 bool hal_works = SetSuspendState(true, true, false);
72 return hal_works;
73 }
74
power_shutdown()75 bool power_shutdown()
76 {
77 bool shutdown_works = false;
78
79 if (!adjustPrivilegeForShutdown())
80 return false;
81
82 shutdown_works = ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE | EWX_POWEROFF, 0);
83
84 return shutdown_works;
85 }
86
87 } // anonymous namespace
88
sendRequest(int action)89 bool PowerManagement::sendRequest(int action)
90 {
91 switch (action) {
92 case SHUTDOWN:
93 return power_shutdown();
94 case SUSPEND:
95 return power_suspend();
96 case HIBERNATE:
97 return power_hibernate();
98 }
99 return false;
100 }
101
implemented()102 bool PowerManagement::implemented()
103 {
104 return true;
105 }
106