Leaked source code of windows server 2003
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.

335 lines
10 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORP., 1996
  4. *
  5. * TITLE: BATMTRCF.C
  6. *
  7. * VERSION: 2.0
  8. *
  9. * AUTHOR: ReedB
  10. *
  11. * DATE: 17 Oct, 1996
  12. *
  13. * DESCRIPTION:
  14. * Support for the battery meter configuration page of PowerCfg.Cpl.
  15. *
  16. *******************************************************************************/
  17. #include <nt.h>
  18. #include <ntrtl.h>
  19. #include <nturtl.h>
  20. #include <windows.h>
  21. #include <commctrl.h>
  22. #include <powercfp.h>
  23. #include <dbt.h>
  24. #include <objbase.h>
  25. #include <initguid.h>
  26. #include <ntpoapi.h>
  27. #include <poclass.h>
  28. #include "powercfg.h"
  29. #include "pwrresid.h"
  30. #include "PwrMn_cs.h"
  31. /*******************************************************************************
  32. *
  33. * G L O B A L D A T A
  34. *
  35. *******************************************************************************/
  36. extern UINT g_uiEnableSysTrayFlag;
  37. // A systary change requires PowerSchemeDlgProc re-init.
  38. extern BOOL g_bSystrayChange;
  39. // Persistant storage of this data is managed by POWRPROF.DLL API's.
  40. extern GLOBAL_POWER_POLICY g_gpp;
  41. // Subclass variables:
  42. WNDPROC g_fnOldPropShtProc;
  43. // BatMeter creation parameters.
  44. HWND g_hwndBatMeter;
  45. BOOL g_bShowMulti;
  46. HWND g_hwndBatMeterFrame;
  47. // Show/hide multi-bat display check box.
  48. DWORD g_dwShowMultiBatDispOpt = CONTROL_ENABLE;
  49. // Static flags:
  50. UINT g_uiEnableMultiFlag = EnableMultiBatteryDisplay;
  51. #ifdef WINNT
  52. // Used to track registration for WM_DEVICECHANGED message.
  53. HDEVNOTIFY g_hDevNotify;
  54. #endif
  55. // Battery meter policies dialog controls descriptions:
  56. #define NUM_BATMETERCFG_CONTROLS 1
  57. POWER_CONTROLS g_pcBatMeterCfg[NUM_BATMETERCFG_CONTROLS] =
  58. {// Control ID Control Type Data Address Data Size Parameter Pointer Enable/Visible State Pointer
  59. IDC_ENABLEMULTI, CHECK_BOX, &(g_gpp.user.GlobalFlags), sizeof(g_gpp.user.GlobalFlags), &g_uiEnableMultiFlag, &g_dwShowMultiBatDispOpt,
  60. };
  61. // "Battery Meter" Dialog Box (IDD_BATMETERCFG == 102) help arrays:
  62. const DWORD g_BatMeterCfgHelpIDs[]=
  63. {
  64. IDC_ENABLEMULTI, IDH_102_1204, // Battery Meter: "Show the status of all &batteries." (Button)
  65. IDC_STATIC_FRAME_BATMETER, IDH_102_1205, // Battery Meter: "Batmeter frame" (Static)
  66. IDC_POWERSTATUSGROUPBOX, IDH_102_1201, // Battery Meter: "Power status" (Button)
  67. 0, 0
  68. };
  69. #ifdef WINNT
  70. // Private function prototypes
  71. BOOL RegisterForDeviceNotification(HWND hWnd);
  72. void UnregisterForDeviceNotification(void);
  73. #endif
  74. /*******************************************************************************
  75. *
  76. * P U B L I C E N T R Y P O I N T S
  77. *
  78. *******************************************************************************/
  79. /*******************************************************************************
  80. *
  81. * PropShtSubclassProc
  82. *
  83. * DESCRIPTION:
  84. *
  85. * PARAMETERS:
  86. *
  87. *******************************************************************************/
  88. LRESULT PropShtSubclassProc(HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
  89. {
  90. LRESULT lRet;
  91. lRet = CallWindowProc(g_fnOldPropShtProc, hWnd, uiMsg, wParam, lParam);
  92. if ((uiMsg == WM_POWERBROADCAST) && (wParam == PBT_APMPOWERSTATUSCHANGE)) {
  93. UpdateBatMeter(g_hwndBatMeter, g_bShowMulti, TRUE, NULL);
  94. }
  95. return lRet;
  96. }
  97. /*******************************************************************************
  98. *
  99. * BatMeterCfgDlgProc
  100. *
  101. * DESCRIPTION:
  102. *
  103. * PARAMETERS:
  104. *
  105. *******************************************************************************/
  106. INT_PTR CALLBACK BatMeterCfgDlgProc
  107. (
  108. HWND hWnd,
  109. UINT uMsg,
  110. WPARAM wParam,
  111. LPARAM lParam
  112. )
  113. {
  114. static BOOL bDirty = FALSE;
  115. #ifdef WINNT
  116. static BOOL bRegisteredForDC = FALSE;
  117. #endif
  118. NMHDR *lpnm;
  119. switch (uMsg)
  120. {
  121. case WM_INITDIALOG:
  122. // If we can't read the global power policies
  123. // disable the controls on this page.
  124. if (!GetGlobalPwrPolicy(&g_gpp))
  125. {
  126. DisableControls(hWnd, NUM_BATMETERCFG_CONTROLS, g_pcBatMeterCfg);
  127. }
  128. else
  129. {
  130. if (g_gpp.user.GlobalFlags & EnableMultiBatteryDisplay)
  131. {
  132. g_bShowMulti = TRUE;
  133. }
  134. else
  135. {
  136. g_bShowMulti = FALSE;
  137. }
  138. // If we can't write the global power policies disable
  139. // the controls this page.
  140. if (!WriteGlobalPwrPolicyReport(hWnd, &g_gpp, FALSE))
  141. {
  142. HideControls(hWnd, NUM_BATMETERCFG_CONTROLS, g_pcBatMeterCfg);
  143. }
  144. SetControls(hWnd, NUM_BATMETERCFG_CONTROLS, g_pcBatMeterCfg);
  145. }
  146. g_hwndBatMeterFrame = GetDlgItem(hWnd, IDC_STATIC_FRAME_BATMETER);
  147. g_hwndBatMeter = CreateBatMeter(hWnd,
  148. g_hwndBatMeterFrame,
  149. g_bShowMulti,
  150. NULL);
  151. // The top level window must be subclassed to receive
  152. // the WM_POWERBROADCAST message.
  153. if (g_hwndBatMeter) {
  154. g_fnOldPropShtProc =
  155. (WNDPROC) SetWindowLongPtr(GetParent(hWnd), DWLP_DLGPROC,
  156. (LONG_PTR)PropShtSubclassProc);
  157. #ifdef WINNT
  158. // Do onetime registration for WM_DEVICECHANGED.
  159. if (!bRegisteredForDC) {
  160. bRegisteredForDC = RegisterForDeviceNotification(hWnd);
  161. }
  162. #endif
  163. }
  164. return TRUE;
  165. case WM_NOTIFY:
  166. lpnm = (NMHDR FAR *)lParam;
  167. switch(lpnm->code)
  168. {
  169. case PSN_APPLY:
  170. if (bDirty)
  171. {
  172. GetControls(hWnd, NUM_BATMETERCFG_CONTROLS, g_pcBatMeterCfg);
  173. WriteGlobalPwrPolicyReport(hWnd, &g_gpp, TRUE);
  174. bDirty = FALSE;
  175. }
  176. break;
  177. }
  178. break;
  179. case WM_COMMAND:
  180. switch (wParam) {
  181. case IDC_ENABLEMULTI:
  182. GetControls(hWnd, NUM_BATMETERCFG_CONTROLS, g_pcBatMeterCfg);
  183. if (g_gpp.user.GlobalFlags & EnableMultiBatteryDisplay) {
  184. g_bShowMulti = TRUE;
  185. }
  186. else {
  187. g_bShowMulti = FALSE;
  188. }
  189. UpdateBatMeter(g_hwndBatMeter, g_bShowMulti, TRUE, NULL);
  190. // Enable the parent dialog Apply button on change.
  191. MarkSheetDirty(hWnd, &bDirty);
  192. break;
  193. default:
  194. // Notify battery meter of enter key events.
  195. if (HIWORD(wParam) == BN_CLICKED) {
  196. SendMessage(g_hwndBatMeter, uMsg, wParam, lParam);
  197. }
  198. }
  199. break;
  200. case PCWM_NOTIFYPOWER:
  201. // Systray changed something. Get the flags and update controls.
  202. if (GetGlobalPwrPolicy(&g_gpp)) {
  203. SetControls(hWnd, NUM_BATMETERCFG_CONTROLS, g_pcBatMeterCfg);
  204. }
  205. g_bSystrayChange = TRUE;
  206. break;
  207. case WM_DEVICECHANGE:
  208. if ((wParam == DBT_DEVICEARRIVAL) ||
  209. #ifndef WINNT
  210. (wParam == DBT_DEVICEREMOVECOMPLETE) ||
  211. #endif
  212. (wParam == DBT_DEVICEQUERYREMOVEFAILED)) {
  213. if (g_hwndBatMeter) {
  214. g_hwndBatMeter = DestroyBatMeter(g_hwndBatMeter);
  215. }
  216. g_hwndBatMeter = CreateBatMeter(hWnd,
  217. g_hwndBatMeterFrame,
  218. g_bShowMulti,
  219. NULL);
  220. InvalidateRect(hWnd, NULL, TRUE);
  221. }
  222. return TRUE;
  223. #ifdef WINNT
  224. case WM_DESTROY:
  225. UnregisterForDeviceNotification();
  226. break;
  227. #endif
  228. case WM_HELP: // F1
  229. WinHelp(((LPHELPINFO)lParam)->hItemHandle, PWRMANHLP, HELP_WM_HELP, (ULONG_PTR)(LPTSTR)g_BatMeterCfgHelpIDs);
  230. return TRUE;
  231. case WM_CONTEXTMENU: // right mouse click
  232. WinHelp((HWND)wParam, PWRMANHLP, HELP_CONTEXTMENU, (ULONG_PTR)(LPTSTR)g_BatMeterCfgHelpIDs);
  233. return TRUE;
  234. }
  235. return FALSE;
  236. }
  237. /*******************************************************************************
  238. *
  239. * P R I V A T E F U N C T I O N S
  240. *
  241. *******************************************************************************/
  242. #ifdef WINNT
  243. /*******************************************************************************
  244. *
  245. * RegisterForDeviceNotification
  246. *
  247. * DESCRIPTION:
  248. * Do onetime registration for WM_DEVICECHANGED.
  249. *
  250. * PARAMETERS:
  251. *
  252. *******************************************************************************/
  253. BOOL RegisterForDeviceNotification(HWND hWnd)
  254. {
  255. DEV_BROADCAST_DEVICEINTERFACE dbc;
  256. memset(&dbc, 0, sizeof(DEV_BROADCAST_DEVICEINTERFACE));
  257. dbc.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
  258. dbc.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  259. dbc.dbcc_classguid = GUID_DEVICE_BATTERY;
  260. g_hDevNotify = RegisterDeviceNotification(hWnd,
  261. &dbc,
  262. DEVICE_NOTIFY_WINDOW_HANDLE);
  263. if (!g_hDevNotify) {
  264. MYDBGPRINT(( "RegisterForDeviceNotification failed"));
  265. return FALSE;
  266. }
  267. return TRUE;
  268. }
  269. /*******************************************************************************
  270. *
  271. * UnregisterForDeviceNotification
  272. *
  273. * DESCRIPTION:
  274. *
  275. *
  276. * PARAMETERS:
  277. *
  278. *******************************************************************************/
  279. void UnregisterForDeviceNotification(void)
  280. {
  281. if (g_hDevNotify) {
  282. UnregisterDeviceNotification(g_hDevNotify);
  283. g_hDevNotify = NULL;
  284. }
  285. }
  286. #endif