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.

198 lines
5.3 KiB

  1. #include <windows.h>
  2. #include <commctrl.h>
  3. #include "usermsg.h"
  4. // #define SEND_TEST_MESSAGES 1
  5. #define RETURN_SUCCESS 0
  6. #define RETURN_FAILURE 1
  7. #define RETURN_HANG 2
  8. /*******************************************************************************
  9. *
  10. * G L O B A L D A T A
  11. *
  12. *******************************************************************************/
  13. HINSTANCE g_hInstance; // Global instance handle of this DLL.
  14. HWND g_hwndParent; // Parent of the battery meter.
  15. LPTSTR szDebugPBT[] =
  16. {
  17. TEXT("WM_POWERBROADCAST, PBT_APMQUERYSUSPEND"),
  18. TEXT("WM_POWERBROADCAST, PBT_APMQUERYSTANDBY"),
  19. TEXT("WM_POWERBROADCAST, PBT_APMQUERYSUSPENDFAILED"),
  20. TEXT("WM_POWERBROADCAST, PBT_APMQUERYSTANDBYFAILED"),
  21. TEXT("WM_POWERBROADCAST, PBT_APMSUSPEND"),
  22. TEXT("WM_POWERBROADCAST, PBT_APMSTANDBY"),
  23. TEXT("WM_POWERBROADCAST, PBT_APMRESUMECRITICAL"),
  24. TEXT("WM_POWERBROADCAST, PBT_APMRESUMESUSPEND"),
  25. TEXT("WM_POWERBROADCAST, PBT_APMRESUMESTANDBY"),
  26. TEXT("WM_POWERBROADCAST, PBT_APMBATTERYLOW"),
  27. TEXT("WM_POWERBROADCAST, PBT_APMPOWERSTATUSCHANGE"),
  28. TEXT("WM_POWERBROADCAST, PBT_APMOEMEVENT"),
  29. TEXT("WM_POWERBROADCAST, unknown"),
  30. TEXT("WM_POWERBROADCAST, unknown"),
  31. TEXT("WM_POWERBROADCAST, unknown"),
  32. TEXT("WM_POWERBROADCAST, unknown"),
  33. TEXT("WM_POWERBROADCAST, unknown"),
  34. TEXT("WM_POWERBROADCAST, unknown"),
  35. TEXT("WM_POWERBROADCAST, PBT_APMRESUMEAUTOMATIC")
  36. };
  37. #ifdef SEND_TEST_MESSAGES
  38. HANDLE g_hNotifyMsg;
  39. #define STACKSIZE 4096
  40. /*******************************************************************************
  41. *
  42. * NotifyMsgThread
  43. *
  44. * DESCRIPTION:
  45. *
  46. * PARAMETERS:
  47. *
  48. *******************************************************************************/
  49. DWORD NotifyMsgThread(DWORD dwParameter)
  50. {
  51. static INT wParam;
  52. SetThreadPriority(g_hNotifyMsg, THREAD_PRIORITY_LOWEST);
  53. SendMessage(g_hwndParent, WM_POWERBROADCAST, PBT_APMPOWERSTATUSCHANGE, 0);
  54. for (; ;) {
  55. Sleep(3000);
  56. SendMessage(g_hwndParent, WM_POWERBROADCAST, wParam, 0);
  57. if (++wParam > 0xC) {
  58. wParam = 0;
  59. }
  60. }
  61. return 0;
  62. }
  63. #endif
  64. /*******************************************************************************
  65. *
  66. * DlgProc
  67. *
  68. * DESCRIPTION:
  69. *
  70. * PARAMETERS:
  71. *
  72. *******************************************************************************/
  73. BOOL CALLBACK DlgProc(
  74. HWND hWnd,
  75. UINT uMsg,
  76. WPARAM wParam,
  77. LPARAM lParam
  78. )
  79. {
  80. static UINT uiReturnAction;
  81. UINT uiCount;
  82. switch (uMsg) {
  83. case WM_INITDIALOG:
  84. g_hwndParent = hWnd;
  85. CheckDlgButton(hWnd, IDC_PASSPWRMSG, BST_CHECKED);
  86. return TRUE;
  87. case WM_COMMAND:
  88. switch (LOWORD(wParam)) {
  89. case IDC_PASSPWRMSG:
  90. uiReturnAction = RETURN_SUCCESS;
  91. break;
  92. case IDC_FAILPWRMSG:
  93. uiReturnAction = RETURN_FAILURE;
  94. break;
  95. case IDC_HANGPWRMSG:
  96. uiReturnAction = RETURN_HANG;
  97. break;
  98. case IDCANCEL:
  99. case IDOK:
  100. EndDialog(hWnd, wParam);
  101. break;
  102. }
  103. break;
  104. case WM_POWERBROADCAST:
  105. if (//(wParam >= PBT_APMQUERYSUSPEND) && // compiler warning
  106. (wParam <= PBT_APMRESUMEAUTOMATIC)) {
  107. SendDlgItemMessage(hWnd, IDC_STATUSLIST, LB_ADDSTRING,
  108. 0, (LPARAM) szDebugPBT[wParam]);
  109. }
  110. else {
  111. SendDlgItemMessage(hWnd, IDC_STATUSLIST, LB_ADDSTRING,
  112. 0, (LPARAM) TEXT("WM_POWERBROADCAST, unknown wParam"));
  113. }
  114. uiCount = SendDlgItemMessage(hWnd, IDC_STATUSLIST, LB_GETCOUNT, 0, 0);
  115. if ((uiCount != LB_ERR) && (uiCount)) {
  116. SendDlgItemMessage(hWnd, IDC_STATUSLIST, LB_SETCURSEL, uiCount-1, 0);
  117. }
  118. switch (uiReturnAction) {
  119. case RETURN_HANG:
  120. while (uiReturnAction == RETURN_HANG);
  121. case RETURN_SUCCESS:
  122. SetWindowLong(hWnd, DWL_MSGRESULT, TRUE);
  123. return TRUE;
  124. case RETURN_FAILURE:
  125. SetWindowLong(hWnd, DWL_MSGRESULT, FALSE);
  126. return TRUE;
  127. }
  128. break;
  129. case WM_DEVICECHANGE:
  130. SendDlgItemMessage(hWnd, IDC_STATUSLIST, LB_ADDSTRING,
  131. 0, (LPARAM) TEXT("WM_DEVICECHANGE"));
  132. break;
  133. }
  134. return FALSE;
  135. }
  136. /*******************************************************************************
  137. *
  138. * WinMain
  139. *
  140. * DESCRIPTION:
  141. *
  142. * PARAMETERS:
  143. *
  144. *******************************************************************************/
  145. INT WINAPI WinMain(
  146. HINSTANCE hInstance,
  147. HINSTANCE hPrevInstance,
  148. LPSTR lpAnsiCmdLine,
  149. INT cmdShow)
  150. {
  151. DWORD dwThreadId;
  152. #ifdef SEND_TEST_MESSAGES
  153. // Spin a thread to send update messages
  154. g_hNotifyMsg = CreateThread(NULL, STACKSIZE,
  155. (LPTHREAD_START_ROUTINE) NotifyMsgThread,
  156. NULL, 0, &dwThreadId);
  157. #endif
  158. DialogBox(hInstance,
  159. MAKEINTRESOURCE(IDD_STATUSDLG),
  160. NULL,
  161. DlgProc);
  162. return 0;
  163. }