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.

385 lines
11 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORP., 1996
  4. *
  5. * TITLE: DETAILS.C
  6. *
  7. * VERSION: 2.0
  8. *
  9. * AUTHOR: ReedB
  10. *
  11. * DATE: 17 Oct, 1996
  12. *
  13. * DESCRIPTION:
  14. * Implements the Detailed Batery Information dialog.
  15. *
  16. *******************************************************************************/
  17. #include <nt.h>
  18. #include <ntrtl.h>
  19. #include <nturtl.h>
  20. #include <windows.h>
  21. #include <commctrl.h>
  22. #include <devioctl.h>
  23. #include <ntpoapi.h>
  24. #include <poclass.h>
  25. #include "batmeter.h"
  26. #include "bmresid.h"
  27. /*******************************************************************************
  28. *
  29. * G L O B A L D A T A
  30. *
  31. *******************************************************************************/
  32. extern HINSTANCE g_hInstance; // Global instance handle of this DLL.
  33. extern const DWORD g_ContextMenuHelpIDs[]; //Help ID's.
  34. /*******************************************************************************
  35. *
  36. * AppendStrID
  37. *
  38. * DESCRIPTION:
  39. *
  40. * PARAMETERS:
  41. *
  42. *******************************************************************************/
  43. BOOL
  44. AppendStrID(
  45. LPTSTR lpszDest,
  46. DWORD DestBufferSizeInChars,
  47. UINT uiID,
  48. BOOLEAN bUseComma)
  49. {
  50. LPTSTR lpsz;
  51. DWORD len;
  52. BOOL RetVal = FALSE;
  53. if (DestBufferSizeInChars < 1) {
  54. return(FALSE);
  55. }
  56. lpszDest[0] = TEXT('\0');
  57. if (lpszDest) {
  58. lpsz = LoadDynamicString(uiID);
  59. if (lpsz) {
  60. len = lstrlen(lpsz);
  61. if (bUseComma) {
  62. len += sizeof(TEXT(", ")); // includes null terminator.
  63. if (len <= DestBufferSizeInChars) {
  64. lstrcat(lpszDest, TEXT(", "));
  65. }
  66. } else {
  67. len +=1; // null terminator
  68. }
  69. if (len <= DestBufferSizeInChars) {
  70. lstrcat(lpszDest, lpsz);
  71. RetVal = TRUE;
  72. }
  73. LocalFree(lpsz);
  74. }
  75. }
  76. return(RetVal);
  77. }
  78. /*******************************************************************************
  79. *
  80. * GetBatStatusDetails
  81. *
  82. * DESCRIPTION:
  83. *
  84. * PARAMETERS:
  85. *
  86. *******************************************************************************/
  87. BOOL GetBatStatusDetails(HWND hWnd, PBATTERY_STATE pbs)
  88. {
  89. BATTERY_STATUS bs;
  90. BATTERY_WAIT_STATUS bws;
  91. DWORD dwByteCount;
  92. BATTERY_INFORMATION bi;
  93. BATTERY_QUERY_INFORMATION bqi;
  94. TCHAR szChem[5], szStatus[128];
  95. CHAR szaChem[5];
  96. LPTSTR lpsz;
  97. UINT uiIDS;
  98. BOOLEAN bUseComma;
  99. bqi.BatteryTag = pbs->ulTag;
  100. bqi.InformationLevel = BatteryInformation;
  101. bqi.AtRate = 0;
  102. if (DeviceIoControl(pbs->hDevice, IOCTL_BATTERY_QUERY_INFORMATION,
  103. &bqi, sizeof(bqi),
  104. &bi, sizeof(bi),
  105. &dwByteCount, NULL)) {
  106. // Set chemistry.
  107. memcpy(szaChem, bi.Chemistry, 4);
  108. szaChem[4] = 0;
  109. if (szaChem[0]) {
  110. #ifdef UNICODE
  111. MultiByteToWideChar(CP_ACP, 0, szaChem, -1, szChem, 5);
  112. SetDlgItemText(hWnd, IDC_CHEM, szChem);
  113. #else
  114. SetDlgItemText(hWnd, IDC_CHEM, szaChem);
  115. #endif
  116. }
  117. else {
  118. ShowWindow(GetDlgItem(hWnd, IDC_CHEM), SW_HIDE);
  119. ShowWindow(GetDlgItem(hWnd, IDC_CHEMISTRY), SW_HIDE);
  120. }
  121. // Set up BATTERY_WAIT_STATUS for immediate return.
  122. memset(&bws, 0, sizeof(BATTERY_WAIT_STATUS));
  123. bws.BatteryTag = pbs->ulTag;
  124. if (DeviceIoControl(pbs->hDevice, IOCTL_BATTERY_QUERY_STATUS,
  125. &bws, sizeof(BATTERY_WAIT_STATUS),
  126. &bs, sizeof(BATTERY_STATUS),
  127. &dwByteCount, NULL)) {
  128. szStatus[0] = '\0';
  129. bUseComma = FALSE;
  130. if (bs.PowerState & BATTERY_POWER_ON_LINE) {
  131. AppendStrID(
  132. szStatus,
  133. ARRAYSIZE(szStatus),
  134. IDS_BATTERY_POWER_ON_LINE,
  135. bUseComma);
  136. bUseComma = TRUE;
  137. }
  138. if (bs.PowerState & BATTERY_DISCHARGING) {
  139. AppendStrID(
  140. szStatus,
  141. ARRAYSIZE(szStatus),
  142. IDS_BATTERY_DISCHARGING,
  143. bUseComma);
  144. bUseComma = TRUE;
  145. }
  146. if (bs.PowerState & BATTERY_CHARGING) {
  147. AppendStrID(
  148. szStatus,
  149. ARRAYSIZE(szStatus),
  150. IDS_BATTERY_CHARGING,
  151. bUseComma);
  152. bUseComma = TRUE;
  153. }
  154. if (bs.PowerState & BATTERY_CRITICAL) {
  155. AppendStrID(
  156. szStatus,
  157. ARRAYSIZE(szStatus),
  158. IDS_BATTERY_CRITICAL,
  159. bUseComma);
  160. bUseComma = TRUE;
  161. }
  162. SetDlgItemText(hWnd, IDC_STATE, szStatus);
  163. return TRUE;
  164. }
  165. }
  166. return FALSE;
  167. }
  168. /*******************************************************************************
  169. *
  170. * GetBatQueryInfo
  171. *
  172. * DESCRIPTION:
  173. *
  174. * PARAMETERS:
  175. *
  176. *******************************************************************************/
  177. BOOL GetBatQueryInfo(
  178. PBATTERY_STATE pbs,
  179. PBATTERY_QUERY_INFORMATION pbqi,
  180. PVOID pData,
  181. ULONG ulSize
  182. )
  183. {
  184. DWORD dwByteCount;
  185. if (DeviceIoControl(pbs->hDevice, IOCTL_BATTERY_QUERY_INFORMATION,
  186. pbqi, sizeof(BATTERY_QUERY_INFORMATION),
  187. pData, ulSize,
  188. &dwByteCount, NULL)) {
  189. return TRUE;
  190. }
  191. return FALSE;
  192. }
  193. /*******************************************************************************
  194. *
  195. * GetAndSetBatQueryInfoText
  196. *
  197. * DESCRIPTION:
  198. *
  199. * PARAMETERS:
  200. *
  201. *******************************************************************************/
  202. BOOL GetAndSetBatQueryInfoText(
  203. HWND hWnd,
  204. PBATTERY_STATE pbs,
  205. PBATTERY_QUERY_INFORMATION pbqi,
  206. UINT uiIDS,
  207. UINT uiLabelID
  208. )
  209. {
  210. WCHAR szBatStr[MAX_BATTERY_STRING_SIZE];
  211. memset(szBatStr, 0, sizeof(szBatStr));
  212. if (GetBatQueryInfo(pbs, pbqi, (PVOID)szBatStr, sizeof(szBatStr))) {
  213. #ifdef UNICODE
  214. if (lstrcmp(szBatStr, TEXT(""))) {
  215. SetDlgItemText(hWnd, uiIDS, szBatStr);
  216. return TRUE;
  217. }
  218. #else
  219. CHAR szaBatStr[MAX_BATTERY_STRING_SIZE];
  220. szaBatStr[0] = '\0';
  221. WideCharToMultiByte(CP_ACP, 0, szBatStr, -1,
  222. szaBatStr, MAX_BATTERY_STRING_SIZE, NULL, NULL);
  223. if (szaBatStr[0]) {
  224. SetDlgItemText(hWnd, uiIDS, szaBatStr);
  225. return TRUE;
  226. }
  227. #endif
  228. }
  229. ShowWindow(GetDlgItem(hWnd, uiIDS), SW_HIDE);
  230. ShowWindow(GetDlgItem(hWnd, uiLabelID), SW_HIDE);
  231. return FALSE;
  232. }
  233. /*******************************************************************************
  234. *
  235. * GetBatOptionalDetails
  236. *
  237. * DESCRIPTION:
  238. * Get optional battery data and set the dialog control.
  239. *
  240. * PARAMETERS:
  241. *
  242. *******************************************************************************/
  243. BOOL GetBatOptionalDetails(HWND hWnd, PBATTERY_STATE pbs)
  244. {
  245. BATTERY_QUERY_INFORMATION bqi;
  246. ULONG ulData;
  247. LPTSTR lpsz = NULL;
  248. BATTERY_MANUFACTURE_DATE bmd;
  249. TCHAR szDateBuf[128];
  250. SYSTEMTIME stDate;
  251. bqi.BatteryTag = pbs->ulTag;
  252. bqi.InformationLevel = BatteryManufactureDate;
  253. bqi.AtRate = 0;
  254. if (GetBatQueryInfo(pbs, &bqi, (PULONG)&bmd,
  255. sizeof(BATTERY_MANUFACTURE_DATE))) {
  256. memset(&stDate, 0, sizeof(SYSTEMTIME));
  257. stDate.wYear = (WORD) bmd.Year;
  258. stDate.wMonth = (WORD) bmd.Month;
  259. stDate.wDay = (WORD) bmd.Day;
  260. GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE,
  261. &stDate, NULL, szDateBuf, 128);
  262. SetDlgItemText(hWnd, IDC_BATMANDATE, szDateBuf);
  263. }
  264. else {
  265. ShowWindow(GetDlgItem(hWnd, IDC_BATMANDATE), SW_HIDE);
  266. ShowWindow(GetDlgItem(hWnd, IDC_DATEMANUFACTURED), SW_HIDE);
  267. }
  268. bqi.InformationLevel = BatteryDeviceName;
  269. GetAndSetBatQueryInfoText(hWnd, pbs, &bqi, IDC_DEVNAME, IDC_BATTERYNAME);
  270. bqi.InformationLevel = BatteryManufactureName;
  271. GetAndSetBatQueryInfoText(hWnd, pbs, &bqi, IDC_BATMANNAME, IDC_MANUFACTURE);
  272. bqi.InformationLevel = BatteryUniqueID;
  273. GetAndSetBatQueryInfoText(hWnd, pbs, &bqi, IDC_BATID, IDC_UNIQUEID);
  274. return TRUE;
  275. }
  276. /*******************************************************************************
  277. *
  278. * InitBatDetailDlg
  279. *
  280. * DESCRIPTION:
  281. *
  282. * PARAMETERS:
  283. *
  284. *******************************************************************************/
  285. BOOL InitBatDetailDialogs(HWND hWnd, PBATTERY_STATE pbs)
  286. {
  287. LPTSTR lpsz;
  288. DWORD dwByteCount;
  289. lpsz = LoadDynamicString(IDS_BATTERYNUMDETAILS, pbs->ulBatNum);
  290. if (lpsz) {
  291. SetWindowText(hWnd, lpsz);
  292. LocalFree(lpsz);
  293. }
  294. if (GetBatOptionalDetails(hWnd, pbs)) {
  295. return GetBatStatusDetails(hWnd, pbs);
  296. }
  297. return FALSE;
  298. }
  299. /*******************************************************************************
  300. *
  301. * BatDetailDlgProc
  302. *
  303. * DESCRIPTION:
  304. * DialogProc for the Detailed Battery Information dialog.
  305. *
  306. * PARAMETERS:
  307. *
  308. *******************************************************************************/
  309. LRESULT CALLBACK BatDetailDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  310. {
  311. UINT uiBatNum;
  312. static PBATTERY_STATE pbs;
  313. switch (uMsg) {
  314. case WM_INITDIALOG:
  315. pbs = (PBATTERY_STATE) lParam;
  316. return InitBatDetailDialogs(hWnd, pbs);
  317. case WM_COMMAND:
  318. switch (wParam) {
  319. case IDC_REFRESH:
  320. GetBatStatusDetails(hWnd, pbs);
  321. break;
  322. case IDCANCEL:
  323. case IDOK:
  324. EndDialog(hWnd, wParam);
  325. break;
  326. }
  327. break;
  328. case WM_HELP: // F1
  329. WinHelp(((LPHELPINFO)lParam)->hItemHandle, PWRMANHLP, HELP_WM_HELP, (ULONG_PTR)(LPTSTR)g_ContextMenuHelpIDs);
  330. break;
  331. case WM_CONTEXTMENU: // right mouse click
  332. WinHelp((HWND)wParam, PWRMANHLP, HELP_CONTEXTMENU, (ULONG_PTR)(LPTSTR)g_ContextMenuHelpIDs);
  333. break;
  334. }
  335. return FALSE;
  336. }