Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

355 lines
13 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORP., 1996
  4. *
  5. * TITLE: MERGE.C
  6. *
  7. * VERSION: 2.0
  8. *
  9. * AUTHOR: ReedB
  10. *
  11. * DATE: 17 Oct, 1996
  12. *
  13. * DESCRIPTION:
  14. * Helper for merging/splitting and dumping various power profile structures.
  15. *
  16. *******************************************************************************/
  17. #include <nt.h>
  18. #include <ntrtl.h>
  19. #include <nturtl.h>
  20. #include <windows.h>
  21. #include <ntpoapi.h>
  22. #include "powrprofp.h"
  23. /*******************************************************************************
  24. *
  25. * G L O B A L D A T A
  26. *
  27. *******************************************************************************/
  28. /*******************************************************************************
  29. *
  30. * MergeSplitPolicies
  31. *
  32. * DESCRIPTION:
  33. *
  34. * PARAMETERS:
  35. *
  36. *******************************************************************************/
  37. BOOLEAN MergePolicies(
  38. PUSER_POWER_POLICY pupp,
  39. PMACHINE_POWER_POLICY pmpp,
  40. PPOWER_POLICY ppp
  41. )
  42. {
  43. if ((pupp->Revision == CURRENT_REVISION) &&
  44. (pmpp->Revision == CURRENT_REVISION))
  45. {
  46. memcpy(&(ppp->user), pupp, sizeof(USER_POWER_POLICY));
  47. memcpy(&(ppp->mach), pmpp, sizeof(MACHINE_POWER_POLICY));
  48. return TRUE;
  49. }
  50. SetLastError(ERROR_REVISION_MISMATCH);
  51. DebugPrint("MergePolicies, failed, LastError: 0x%08X", ERROR_REVISION_MISMATCH);
  52. return FALSE;
  53. }
  54. /*******************************************************************************
  55. *
  56. * SplitPolicies
  57. *
  58. * DESCRIPTION:
  59. *
  60. * PARAMETERS:
  61. *
  62. *******************************************************************************/
  63. BOOLEAN SplitPolicies(
  64. PPOWER_POLICY ppp,
  65. PUSER_POWER_POLICY pupp,
  66. PMACHINE_POWER_POLICY pmpp
  67. )
  68. {
  69. if ((ppp->user.Revision == CURRENT_REVISION) &&
  70. (ppp->mach.Revision == CURRENT_REVISION)) {
  71. memcpy(pupp, &(ppp->user), sizeof(USER_POWER_POLICY));
  72. memcpy(pmpp, &(ppp->mach), sizeof(MACHINE_POWER_POLICY));
  73. return TRUE;
  74. }
  75. SetLastError(ERROR_REVISION_MISMATCH);
  76. DebugPrint("SplitPolicies, failed, LastError: 0x%08X", ERROR_REVISION_MISMATCH);
  77. return FALSE;
  78. }
  79. /*******************************************************************************
  80. *
  81. * MergeGlobalPolicies
  82. *
  83. * DESCRIPTION:
  84. *
  85. * PARAMETERS:
  86. *
  87. *******************************************************************************/
  88. BOOLEAN MergeGlobalPolicies(
  89. PGLOBAL_USER_POWER_POLICY pgupp,
  90. PGLOBAL_MACHINE_POWER_POLICY pgmpp,
  91. PGLOBAL_POWER_POLICY pgpp
  92. )
  93. {
  94. if ((pgupp->Revision == CURRENT_REVISION) &&
  95. (pgmpp->Revision == CURRENT_REVISION)) {
  96. memcpy(&(pgpp->user), pgupp, sizeof(GLOBAL_USER_POWER_POLICY));
  97. memcpy(&(pgpp->mach), pgmpp, sizeof(GLOBAL_MACHINE_POWER_POLICY));
  98. return TRUE;
  99. }
  100. SetLastError(ERROR_REVISION_MISMATCH);
  101. DebugPrint("MergeGlobalPolicies, failed, LastError: 0x%08X", ERROR_REVISION_MISMATCH);
  102. return FALSE;
  103. }
  104. /*******************************************************************************
  105. *
  106. * SplitGlobalPolicies
  107. *
  108. * DESCRIPTION:
  109. *
  110. * PARAMETERS:
  111. *
  112. *******************************************************************************/
  113. BOOLEAN SplitGlobalPolicies(
  114. PGLOBAL_POWER_POLICY pgpp,
  115. PGLOBAL_USER_POWER_POLICY pgupp,
  116. PGLOBAL_MACHINE_POWER_POLICY pgmpp
  117. )
  118. {
  119. if ((pgpp->user.Revision == CURRENT_REVISION) &&
  120. (pgpp->mach.Revision == CURRENT_REVISION)) {
  121. memcpy(pgupp, &(pgpp->user), sizeof(GLOBAL_USER_POWER_POLICY));
  122. memcpy(pgmpp, &(pgpp->mach), sizeof(GLOBAL_MACHINE_POWER_POLICY));
  123. return TRUE;
  124. }
  125. SetLastError(ERROR_REVISION_MISMATCH);
  126. DebugPrint("SplitGlobalPolicies, failed, LastError: 0x%08X", ERROR_REVISION_MISMATCH);
  127. return FALSE;
  128. }
  129. /*******************************************************************************
  130. *
  131. * MergeToSystemPowerPolicies
  132. *
  133. * DESCRIPTION:
  134. *
  135. * PARAMETERS:
  136. *
  137. *******************************************************************************/
  138. BOOLEAN MergeToSystemPowerPolicies(
  139. PGLOBAL_POWER_POLICY pgpp,
  140. PPOWER_POLICY ppp,
  141. PSYSTEM_POWER_POLICY psppAc,
  142. PSYSTEM_POWER_POLICY psppDc
  143. )
  144. {
  145. UINT i;
  146. if ((pgpp->user.Revision != CURRENT_REVISION) ||
  147. (pgpp->mach.Revision != CURRENT_REVISION) ||
  148. (ppp->user.Revision != CURRENT_REVISION) ||
  149. (ppp->mach.Revision != CURRENT_REVISION))
  150. {
  151. DebugPrint("MergeToSystemPowerPolicies, failed, LastError: 0x%08X", ERROR_REVISION_MISMATCH);
  152. SetLastError(ERROR_REVISION_MISMATCH);
  153. return FALSE;
  154. }
  155. psppAc->Revision = ppp->user.Revision;
  156. psppAc->Idle = ppp->user.IdleAc;
  157. psppAc->IdleTimeout = ppp->user.IdleTimeoutAc;
  158. psppAc->IdleSensitivity = ppp->user.IdleSensitivityAc;
  159. psppAc->DynamicThrottle = ppp->user.ThrottlePolicyAc;
  160. psppAc->MinSleep = ppp->mach.MinSleepAc;
  161. psppAc->MaxSleep = ppp->user.MaxSleepAc;
  162. psppAc->ReducedLatencySleep = ppp->mach.ReducedLatencySleepAc;
  163. psppAc->DozeS4Timeout = ppp->mach.DozeS4TimeoutAc;
  164. psppAc->VideoTimeout = ppp->user.VideoTimeoutAc;
  165. psppAc->SpindownTimeout = ppp->user.SpindownTimeoutAc;
  166. psppAc->OptimizeForPower = ppp->user.OptimizeForPowerAc;
  167. psppAc->FanThrottleTolerance = ppp->user.FanThrottleToleranceAc;
  168. psppAc->ForcedThrottle = ppp->user.ForcedThrottleAc;
  169. psppAc->MinThrottle = ppp->mach.MinThrottleAc;
  170. psppAc->OverThrottled = ppp->mach.OverThrottledAc;
  171. psppAc->PowerButton = pgpp->user.PowerButtonAc;
  172. psppAc->SleepButton = pgpp->user.SleepButtonAc;
  173. psppAc->LidClose = pgpp->user.LidCloseAc;
  174. psppAc->LidOpenWake = pgpp->mach.LidOpenWakeAc;
  175. psppAc->BroadcastCapacityResolution = pgpp->mach.BroadcastCapacityResolution;
  176. psppDc->Revision = ppp->user.Revision;
  177. psppDc->Idle = ppp->user.IdleDc;
  178. psppDc->IdleTimeout = ppp->user.IdleTimeoutDc;
  179. psppDc->IdleSensitivity = ppp->user.IdleSensitivityDc;
  180. psppDc->DynamicThrottle = ppp->user.ThrottlePolicyDc;
  181. psppDc->MinSleep = ppp->mach.MinSleepDc;
  182. psppDc->MaxSleep = ppp->user.MaxSleepDc;
  183. psppDc->ReducedLatencySleep = ppp->mach.ReducedLatencySleepDc;
  184. psppDc->DozeS4Timeout = ppp->mach.DozeS4TimeoutDc;
  185. psppDc->VideoTimeout = ppp->user.VideoTimeoutDc;
  186. psppDc->SpindownTimeout = ppp->user.SpindownTimeoutDc;
  187. psppDc->OptimizeForPower = ppp->user.OptimizeForPowerDc;
  188. psppDc->FanThrottleTolerance = ppp->user.FanThrottleToleranceDc;
  189. psppDc->ForcedThrottle = ppp->user.ForcedThrottleDc;
  190. psppDc->MinThrottle = ppp->mach.MinThrottleDc;
  191. psppDc->OverThrottled = ppp->mach.OverThrottledDc;
  192. psppDc->PowerButton = pgpp->user.PowerButtonDc;
  193. psppDc->SleepButton = pgpp->user.SleepButtonDc;
  194. psppDc->LidClose = pgpp->user.LidCloseDc;
  195. psppDc->LidOpenWake = pgpp->mach.LidOpenWakeDc;
  196. psppDc->BroadcastCapacityResolution = pgpp->mach.BroadcastCapacityResolution;
  197. if (pgpp->user.GlobalFlags & EnablePasswordLogon)
  198. {
  199. psppAc->WinLogonFlags = psppDc->WinLogonFlags = WINLOGON_LOCK_ON_SLEEP;
  200. }
  201. else
  202. {
  203. psppAc->WinLogonFlags = psppDc->WinLogonFlags = 0;
  204. }
  205. if (pgpp->user.GlobalFlags & EnableVideoDimDisplay)
  206. {
  207. psppDc->VideoDimDisplay = TRUE;
  208. }
  209. else
  210. {
  211. psppDc->VideoDimDisplay = FALSE;
  212. }
  213. for (i = 0; i < NUM_DISCHARGE_POLICIES; i++)
  214. {
  215. psppDc->DischargePolicy[i] = pgpp->user.DischargePolicy[i];
  216. psppAc->DischargePolicy[i] = pgpp->user.DischargePolicy[i];
  217. // HIWORD of EventCode contains index.
  218. psppDc->DischargePolicy[i].PowerPolicy.EventCode |= (i << 16);
  219. psppAc->DischargePolicy[i].PowerPolicy.EventCode |= (i << 16);
  220. }
  221. return TRUE;
  222. }
  223. /*******************************************************************************
  224. *
  225. * SplitFromSystemPowerPolicies
  226. *
  227. * DESCRIPTION:
  228. *
  229. * PARAMETERS:
  230. *
  231. *******************************************************************************/
  232. BOOLEAN SplitFromSystemPowerPolicies(
  233. PSYSTEM_POWER_POLICY psppAc,
  234. PSYSTEM_POWER_POLICY psppDc,
  235. PGLOBAL_POWER_POLICY pgpp,
  236. PPOWER_POLICY ppp
  237. )
  238. {
  239. UINT i;
  240. if ((psppAc->Revision != CURRENT_REVISION) ||
  241. (psppDc->Revision != CURRENT_REVISION)) {
  242. SetLastError(ERROR_REVISION_MISMATCH);
  243. DebugPrint("SplitFromSystemPowerPolicies, failed, LastError: 0x%08X", ERROR_REVISION_MISMATCH);
  244. return FALSE;
  245. }
  246. if (ppp) {
  247. ppp->user.Revision = ppp->mach.Revision = CURRENT_REVISION;
  248. ppp->user.IdleAc = psppAc->Idle;
  249. ppp->user.IdleTimeoutAc = psppAc->IdleTimeout;
  250. ppp->user.IdleSensitivityAc = psppAc->IdleSensitivity;
  251. ppp->user.ThrottlePolicyAc = psppAc->DynamicThrottle;
  252. ppp->mach.MinSleepAc = psppAc->MinSleep;
  253. ppp->user.MaxSleepAc = psppAc->MaxSleep;
  254. ppp->mach.ReducedLatencySleepAc = psppAc->ReducedLatencySleep;
  255. ppp->mach.DozeTimeoutAc = 0;
  256. ppp->mach.DozeS4TimeoutAc = psppAc->DozeS4Timeout;
  257. ppp->user.VideoTimeoutAc = psppAc->VideoTimeout;
  258. ppp->user.SpindownTimeoutAc = psppAc->SpindownTimeout;
  259. ppp->user.OptimizeForPowerAc = psppAc->OptimizeForPower;
  260. ppp->user.FanThrottleToleranceAc = psppAc->FanThrottleTolerance;
  261. ppp->user.ForcedThrottleAc = psppAc->ForcedThrottle;
  262. ppp->mach.MinThrottleAc = psppAc->MinThrottle;
  263. ppp->mach.OverThrottledAc = psppAc->OverThrottled;
  264. ppp->user.IdleDc = psppDc->Idle;
  265. ppp->user.IdleTimeoutDc = psppDc->IdleTimeout;
  266. ppp->user.IdleSensitivityDc = psppDc->IdleSensitivity;
  267. ppp->user.ThrottlePolicyDc = psppDc->DynamicThrottle;
  268. ppp->mach.MinSleepDc = psppDc->MinSleep;
  269. ppp->user.MaxSleepDc = psppDc->MaxSleep;
  270. ppp->mach.ReducedLatencySleepDc = psppDc->ReducedLatencySleep;
  271. ppp->mach.DozeTimeoutDc = 0;
  272. ppp->mach.DozeS4TimeoutDc = psppDc->DozeS4Timeout;
  273. ppp->user.VideoTimeoutDc = psppDc->VideoTimeout;
  274. ppp->user.SpindownTimeoutDc = psppDc->SpindownTimeout;
  275. ppp->user.OptimizeForPowerDc = psppDc->OptimizeForPower;
  276. ppp->user.FanThrottleToleranceDc = psppDc->FanThrottleTolerance;
  277. ppp->user.ForcedThrottleDc = psppDc->ForcedThrottle;
  278. ppp->mach.MinThrottleDc = psppDc->MinThrottle;
  279. ppp->mach.OverThrottledDc = psppDc->OverThrottled;
  280. }
  281. if (pgpp) {
  282. pgpp->user.Revision = pgpp->mach.Revision = CURRENT_REVISION;
  283. pgpp->user.PowerButtonAc = psppAc->PowerButton;
  284. pgpp->user.SleepButtonAc = psppAc->SleepButton;
  285. pgpp->user.LidCloseAc = psppAc->LidClose;
  286. pgpp->mach.LidOpenWakeAc = psppAc->LidOpenWake;
  287. pgpp->mach.BroadcastCapacityResolution = psppAc->BroadcastCapacityResolution;
  288. pgpp->user.PowerButtonDc = psppDc->PowerButton;
  289. pgpp->user.SleepButtonDc = psppDc->SleepButton;
  290. pgpp->user.LidCloseDc = psppDc->LidClose;
  291. pgpp->mach.LidOpenWakeDc = psppDc->LidOpenWake;
  292. pgpp->mach.BroadcastCapacityResolution = psppDc->BroadcastCapacityResolution;
  293. if ((psppDc->WinLogonFlags & WINLOGON_LOCK_ON_SLEEP) ||
  294. (psppAc->WinLogonFlags & WINLOGON_LOCK_ON_SLEEP)) {
  295. pgpp->user.GlobalFlags |= EnablePasswordLogon;
  296. }
  297. else {
  298. pgpp->user.GlobalFlags &= ~EnablePasswordLogon;
  299. }
  300. if (psppDc->VideoDimDisplay) {
  301. pgpp->user.GlobalFlags |= EnableVideoDimDisplay;
  302. }
  303. else {
  304. pgpp->user.GlobalFlags &= ~EnableVideoDimDisplay;
  305. }
  306. for (i = 0; i < NUM_DISCHARGE_POLICIES; i++) {
  307. pgpp->user.DischargePolicy[i] = psppDc->DischargePolicy[i];
  308. }
  309. }
  310. return TRUE;
  311. }