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.

308 lines
9.1 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORP., 1993-1995
  4. * TITLE: POWRPAGE.CPP
  5. * VERSION: 1.0
  6. * AUTHOR: jsenior
  7. * DATE: 10/28/1998
  8. *
  9. ********************************************************************************
  10. *
  11. * CHANGE LOG:
  12. *
  13. * DATE REV DESCRIPTION
  14. * ---------- ------- ----------------------------------------------------------
  15. * 10/28/1998 jsenior Original implementation.
  16. *
  17. *******************************************************************************/
  18. #include <assert.h>
  19. #include "PowrPage.h"
  20. #include "debug.h"
  21. #include "ItemFind.h"
  22. #include "resource.h"
  23. #include "usbutil.h"
  24. const DWORD PowerHelpIds[]=
  25. {
  26. IDC_STATIC, IDH_NOHELP, //hub information text - help not needed
  27. IDC_SELF_POWER, idh_devmgr_hub_self_power, //selfpowered or bus powered
  28. IDC_POWER_AVAIL, idh_devmgr_hub_power_avail, //total power available text
  29. IDC_DESCRIPTION, idh_devmgr_hub_devices_on_this_hub, //description of list box
  30. IDC_LIST_DEVICES, idh_devmgr_hub_list_devices, //the list box for hub devices
  31. IDC_REFRESH, idh_devmgr_hub_refresh_button,
  32. 0, 0
  33. };
  34. VOID
  35. PowerPage::Initialize()
  36. {
  37. dlgResource = IDD_POWER;
  38. hLstDevices = NULL;
  39. HelpIds = (const DWORD *) PowerHelpIds;
  40. }
  41. BOOL PowerPage::OnInitDialog()
  42. {
  43. LV_COLUMN column;
  44. RECT rect;
  45. TCHAR buf[MAX_PATH];
  46. if (preItem) {
  47. SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_CAPTION);
  48. //
  49. // Make the Refresh button go away
  50. //
  51. HWND hRefresh;
  52. if (NULL != (hRefresh =GetDlgItem(hwnd, IDC_REFRESH)) ) {
  53. SetWindowLong(hRefresh,
  54. GWL_STYLE,
  55. (GetWindowLong(hRefresh, GWL_STYLE) | WS_DISABLED)
  56. & ~WS_VISIBLE);
  57. }
  58. } else if (!deviceInfoSet && !deviceInfoData) {
  59. //
  60. // Make the Refresh button go away and make the Close button visible
  61. //
  62. HWND hButton;
  63. if (NULL != (hButton =GetDlgItem(hwnd, IDC_REFRESH)) ) {
  64. SetWindowLong(hButton,
  65. GWL_STYLE,
  66. (GetWindowLong(hButton, GWL_STYLE) | WS_DISABLED) &
  67. ~WS_VISIBLE);
  68. }
  69. if (NULL != (hButton =GetDlgItem(hwnd, IDC_POWER_CLOSE)) ) {
  70. SetWindowLong(hButton,
  71. GWL_STYLE,
  72. (GetWindowLong(hButton, GWL_STYLE) | WS_VISIBLE) &
  73. ~WS_DISABLED);
  74. }
  75. // if (!RegisterForDeviceNotification(hwnd))
  76. // MessageBox(hwnd,TEXT("Error"),TEXT("Dynamic refresh unavailable"),MB_OK);
  77. } else {
  78. // RegisterForDeviceNotification(hwnd);
  79. }
  80. hLstDevices = GetDlgItem(hwnd, IDC_LIST_DEVICES);
  81. ListView_SetImageList(hLstDevices, imageList.ImageList(), LVSIL_SMALL);
  82. ZeroMemory(&column, sizeof(LV_COLUMN));
  83. column.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
  84. column.fmt = LVCFMT_LEFT;
  85. LoadString(gHInst, IDS_DEVICE_DESCRIPTION, buf, MAX_PATH);
  86. column.pszText = buf;
  87. GetClientRect(hLstDevices, &rect);
  88. column.cx = (int)(.7*(rect.right - rect.left));
  89. // ListView_InsertColumn(hLstDevices, 0, &column);
  90. SendMessage(hLstDevices, LVM_INSERTCOLUMN, (WPARAM)0, (LPARAM)&column);
  91. ZeroMemory(&column, sizeof(LV_COLUMN));
  92. column.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
  93. column.fmt = LVCFMT_LEFT;
  94. LoadString(gHInst, IDS_POWER_CONSUMED, buf, MAX_PATH);
  95. column.pszText = buf;
  96. GetClientRect(hLstDevices, &rect);
  97. column.cx = (int) (.3*(rect.right - rect.left));
  98. // ListView_InsertColumn(hLstDevices, 1, &column);
  99. SendMessage(hLstDevices, LVM_INSERTCOLUMN, (WPARAM)1, (LPARAM)&column);
  100. Refresh();
  101. return TRUE;
  102. }
  103. BOOL
  104. PowerPage::OnCommand(INT wNotifyCode,
  105. INT wID,
  106. HWND hCtl)
  107. {
  108. if (wNotifyCode == BN_CLICKED) {
  109. if (wID == IDC_REFRESH) {
  110. Refresh();
  111. return FALSE;
  112. } else if (wID == IDC_POWER_CLOSE) {
  113. /* if (hDevNotify) {
  114. UnregisterDeviceNotification(hDevNotify);
  115. UnregisterDeviceNotification(hHubNotify);
  116. hDevNotify = NULL;
  117. }*/
  118. EndDialog(hwnd, wID);
  119. }
  120. }
  121. return TRUE;
  122. }
  123. BOOL
  124. PowerPage::OnNotify(HWND hDlg, int nID, LPNMHDR pnmh)
  125. {
  126. switch (nID) {
  127. case IDC_LIST_DEVICES:
  128. OnNotifyListDevices(hDlg, pnmh);
  129. break;
  130. }
  131. return 0;
  132. }
  133. void
  134. PowerPage::OnNotifyListDevices(HWND hDlg, LPNMHDR pnmh)
  135. {
  136. if (pnmh->code == NM_DBLCLK) {
  137. //
  138. // Display properties on this specific device on double click
  139. //
  140. DisplayPPSelectedListItem(hwnd, hLstDevices);
  141. }
  142. }
  143. void PowerPage::Refresh()
  144. {
  145. UsbItem *usbItem = 0, *hub;
  146. LVITEM item;
  147. int i=0; //, size;
  148. TCHAR buf[MAX_PATH], formatString[MAX_PATH];
  149. // CWaitCursor wait;
  150. //
  151. // Clear all UI components
  152. //
  153. ListView_DeleteAllItems(hLstDevices);
  154. if (preItem) {
  155. hub = preItem;
  156. } else {
  157. if (deviceName.empty()) {
  158. if (!GetDeviceName()) {
  159. goto PowerPageRefreshError;
  160. }
  161. }
  162. //
  163. // Recreate the rootItem
  164. //
  165. if (rootItem) {
  166. DeleteChunk(rootItem);
  167. delete rootItem;
  168. }
  169. rootItem = new UsbItem;
  170. AddChunk(rootItem);
  171. hub = rootItem;
  172. if (deviceInfoData) {
  173. if (!hub->EnumerateHub(deviceName,
  174. &imageList,
  175. deviceInfoData->DevInst,
  176. NULL,
  177. UsbItem::UsbItemType::Hub)) {
  178. goto PowerPageRefreshError;
  179. }
  180. } else {
  181. if (!hub->EnumerateHub(deviceName,
  182. &imageList,
  183. NULL,
  184. NULL,
  185. UsbItem::UsbItemType::Hub)) {
  186. goto PowerPageRefreshError;
  187. }
  188. }
  189. }
  190. ZeroMemory(&item, sizeof(LVITEM));
  191. item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_IMAGE;
  192. if (hub->child) {
  193. for (usbItem = hub->child, i = 0;
  194. usbItem;
  195. usbItem = usbItem->sibling, i++) {
  196. if (!usbItem->IsDescriptionValidDevice()) {
  197. i--;
  198. continue;
  199. }
  200. item.iItem = i;
  201. item.iImage = usbItem->imageIndex;
  202. item.lParam = (LPARAM) usbItem;
  203. assert(usbItem->configInfo != NULL);
  204. item.pszText = (LPTSTR) usbItem->configInfo->deviceDesc.c_str();
  205. ListView_InsertItem(hLstDevices, &item);
  206. if (usbItem->ComputePower()) {
  207. LoadString(gHInst, IDS_POWER_CONSUMPTION, formatString, MAX_PATH);
  208. UsbSprintf(buf, formatString, usbItem->power);
  209. } else {
  210. LoadString(gHInst, IDS_UNKNOWN, buf, MAX_PATH);
  211. }
  212. ListView_SetItemText(hLstDevices, i, 1, buf);
  213. }
  214. }
  215. if (hub->NumPorts() - hub->NumChildren() > 0) {
  216. //
  217. // Add an item indicating the number of ports available
  218. //
  219. item.iItem = i;
  220. item.iImage = -1;
  221. item.lParam = (LPARAM) NULL;
  222. LoadString(gHInst, IDS_PORTS_AVAILABLE, formatString, MAX_PATH);
  223. UsbSprintf(buf, formatString, hub->NumPorts() - hub->NumChildren());
  224. item.pszText = buf;
  225. ListView_InsertItem(hLstDevices, &item);
  226. LoadString(gHInst, IDS_POWER_CONSUMPTION, formatString, MAX_PATH);
  227. UsbSprintf(buf, formatString, 0);
  228. ListView_SetItemText(hLstDevices, i, 1, buf);
  229. }
  230. LoadString(gHInst, IDS_TOTAL_POWER, formatString, MAX_PATH);
  231. UsbSprintf(buf, formatString, hub->PortPower());
  232. SetTextItem(hwnd, IDC_POWER_AVAIL, buf);
  233. if (hub->PortPower() > 100) {
  234. SetTextItem(hwnd, IDC_SELF_POWER, IDS_POWER_SELF);
  235. } else {
  236. SetTextItem(hwnd, IDC_SELF_POWER, IDS_POWER_BUS);
  237. }
  238. return;
  239. PowerPageRefreshError:
  240. //
  241. // The hub is malfunctioning. Show the property page as disabled.
  242. //
  243. HWND hButton;
  244. if (NULL != (hButton = GetDlgItem(hwnd, IDC_REFRESH)) ) {
  245. SetWindowLong(hButton,
  246. GWL_STYLE,
  247. (GetWindowLong(hButton, GWL_STYLE) | WS_DISABLED));
  248. }
  249. if (NULL != (hButton = GetDlgItem(hwnd, IDC_LIST_DEVICES)) ) {
  250. SetWindowLong(hButton,
  251. GWL_STYLE,
  252. (GetWindowLong(hButton, GWL_STYLE) | WS_DISABLED));
  253. }
  254. if (NULL != (hButton = GetDlgItem(hwnd, IDC_DESCRIPTION)) ) {
  255. SetWindowLong(hButton,
  256. GWL_STYLE,
  257. (GetWindowLong(hButton, GWL_STYLE) | WS_DISABLED));
  258. }
  259. SetTextItem(hwnd, IDC_POWER_AVAIL, TEXT(""));
  260. SetTextItem(hwnd, IDC_SELF_POWER, IDS_POWER_MALFUNCTIONING);
  261. }
  262. HPROPSHEETPAGE PowerPage::Create()
  263. {
  264. //
  265. // Make sure that this is indeed a hub
  266. //
  267. if (deviceName.empty()) {
  268. if (!GetDeviceName()) {
  269. return NULL;
  270. }
  271. }
  272. return UsbPropertyPage::Create();
  273. }