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.

255 lines
9.0 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORP., 1993-1995
  4. * TITLE: POWER.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 "UsbPopup.h"
  19. #include "itemfind.h"
  20. #include "debug.h"
  21. #include "usbutil.h"
  22. BOOL
  23. UsbPowerPopup::Refresh()
  24. {
  25. TV_INSERTSTRUCT item;
  26. int i=0; //, size;
  27. String hubName;
  28. int stage;
  29. TCHAR buf[MAX_PATH];
  30. TCHAR formatString[MAX_PATH];
  31. PUSB_ACQUIRE_INFO acquireInfo = 0;
  32. LPCTSTR deviceName = deviceItem.configInfo->deviceDesc.c_str();
  33. UsbItem *realItem;
  34. //
  35. // Clear all UI components, and then recreate the rootItem
  36. //
  37. UsbTreeView_DeleteAllItems(hTreeDevices);
  38. if (deviceState == DeviceReattached) {
  39. //
  40. // Set the notification using the name of the offending device
  41. //
  42. LoadString(gHInst,
  43. IDS_POWER_SOLVED,
  44. formatString,
  45. MAX_PATH);
  46. LoadString(gHInst,
  47. IDS_POWER_EXCEEDED,
  48. buf,
  49. MAX_PATH);
  50. MessageBox(hWnd, formatString, buf, MB_OK);
  51. EndDialog(hWnd, 0);
  52. return TRUE;
  53. }
  54. //
  55. // Set the notification using the name of the offending device
  56. //
  57. LoadString(gHInst,
  58. IDS_POWER_NOTIFICATION,
  59. formatString,
  60. MAX_PATH);
  61. UsbSprintf(buf, formatString, deviceName);
  62. if (!SetTextItem(hWnd, IDC_POWER_NOTIFICATION, buf)) {
  63. goto PowerRefreshError;
  64. }
  65. for (stage=0; stage < 2; stage++) {
  66. //
  67. // Recreate the rootItem for each enumeration attempt
  68. //
  69. if (rootItem) {
  70. DeleteChunk(rootItem);
  71. delete rootItem;
  72. }
  73. realItem = rootItem = new UsbItem;
  74. if (!realItem) {
  75. USBERROR((_T("Out of memory!\n")));
  76. goto PowerRefreshError;
  77. }
  78. AddChunk(rootItem);
  79. if (stage == 0) {
  80. acquireInfo = GetControllerName(WmiHandle,
  81. InstanceName.c_str());
  82. if (!acquireInfo) {
  83. goto PowerRefreshError;
  84. }
  85. if (!rootItem->EnumerateController(0,
  86. acquireInfo->Buffer,
  87. &ImageList,
  88. 0)) {
  89. goto PowerRefreshError;
  90. }
  91. //
  92. // Usability: Rename the "Root Hub" to "My Computer" and change the
  93. // USB "shovel" icon to a computer icon.
  94. //
  95. LoadString(gHInst,
  96. IDS_MY_COMPUTER,
  97. buf,
  98. MAX_PATH);
  99. rootItem->child->configInfo->deviceDesc = buf;
  100. wsprintf(buf, _T(" (%d ports)"), rootItem->child->NumPorts());
  101. rootItem->child->configInfo->deviceDesc += buf;
  102. ImageList.GetClassImageIndex(MyComputerClass,
  103. &rootItem->child->imageIndex);
  104. acquireInfo = (PUSB_ACQUIRE_INFO) LocalFree(acquireInfo);
  105. } else {
  106. if (!rootItem->EnumerateAll(&ImageList)) {
  107. goto PowerRefreshError;
  108. }
  109. if (rootItem->NumChildren() == 1) {
  110. realItem = rootItem->child;
  111. break;
  112. }
  113. }
  114. if (rootItem->child) {
  115. //
  116. // Find all unused ports on self powered hubs
  117. //
  118. UsbItemActionFindSelfPoweredHubsWithFreePorts find1(rootItem);
  119. rootItem->Walk(find1);
  120. UsbItemList& devices1 = find1.GetHubs();
  121. if (!devices1.empty()) {
  122. return AssembleDialog(rootItem->child,
  123. &item,
  124. deviceName,
  125. IDS_POWER_PORTS,
  126. IDS_POWER_RECOMMENDATION_PORTS,
  127. TrueAlways,
  128. UsbItemActionFindFreePortsOnSelfPoweredHubs::IsValid,
  129. UsbItemActionFindFreePortsOnSelfPoweredHubs::IsExpanded);
  130. }
  131. //
  132. // Find all self powered hubs that have devices attached requiring less
  133. // than or equal to 100 mA. These devices can be switched with the
  134. // offending device.
  135. //
  136. UsbItemActionFindLowPoweredDevicesOnSelfPoweredHubs find2(rootItem);
  137. rootItem->Walk(find2);
  138. UsbItemList& devices2 = find2.GetDevices();
  139. if (!devices2.empty()) {
  140. return AssembleDialog(rootItem->child,
  141. &item,
  142. deviceName,
  143. IDS_POWER_DEVICE,
  144. IDS_POWER_RECOMMENDATION_DEVICE,
  145. TrueAlways,
  146. UsbItemActionFindLowPoweredDevicesOnSelfPoweredHubs::IsValid,
  147. UsbItemActionFindLowPoweredDevicesOnSelfPoweredHubs::IsExpanded);
  148. }
  149. }
  150. }
  151. {
  152. //
  153. // Find all self powered hubs that have devices attached requiring less
  154. // than or equal to 100 mA. These devices can be switched with the
  155. // offending device.
  156. //
  157. UsbItemActionFindUnknownPoweredDevicesOnSelfPoweredHubs find2(realItem);
  158. realItem->Walk(find2);
  159. UsbItemList& devices = find2.GetDevices();
  160. if (!devices.empty()) {
  161. return AssembleDialog(realItem->child,
  162. &item,
  163. deviceName,
  164. IDS_POWER_HIGHDEVICE,
  165. IDS_POWER_RECHIGHDEVICE,
  166. TrueAlways,
  167. UsbItemActionFindUnknownPoweredDevicesOnSelfPoweredHubs::IsValid,
  168. UsbItemActionFindUnknownPoweredDevicesOnSelfPoweredHubs::IsExpanded);
  169. }
  170. }
  171. //
  172. // Last resort here. Highlight high-powered devices on self-powered hubs
  173. // and tell the user to put it there if they want the device to work.
  174. //
  175. if (realItem->child) {
  176. return AssembleDialog(realItem->child,
  177. &item,
  178. deviceName,
  179. IDS_POWER_HIGHDEVICE,
  180. IDS_POWER_RECHIGHDEVICE,
  181. TrueAlways,
  182. UsbItemActionFindHighPoweredDevicesOnSelfPoweredHubs::IsValid,
  183. UsbItemActionFindHighPoweredDevicesOnSelfPoweredHubs::IsExpanded);
  184. }
  185. return TRUE;
  186. PowerRefreshError:
  187. if (acquireInfo) {
  188. LocalFree(acquireInfo);
  189. }
  190. return FALSE;
  191. }
  192. BOOL
  193. UsbPowerPopup::AssembleDialog(UsbItem* RootItem,
  194. LPTV_INSERTSTRUCT LvItem,
  195. LPCTSTR DeviceName,
  196. UINT Explanation,
  197. UINT Recommendation,
  198. PUsbItemActionIsValid IsValid,
  199. PUsbItemActionIsValid IsBold,
  200. PUsbItemActionIsValid IsExpanded)
  201. {
  202. HTREEITEM hDevice;
  203. TCHAR buf[MAX_PATH], formatString[MAX_PATH];
  204. LoadString(gHInst,
  205. Recommendation,
  206. formatString,
  207. MAX_PATH);
  208. UsbSprintf(buf, formatString, DeviceName);
  209. if (!SetTextItem(hWnd, IDC_POWER_RECOMMENDATION, buf) ||
  210. !SetTextItem(hWnd, IDC_POWER_EXPLANATION, Explanation)) {
  211. return FALSE;
  212. }
  213. if (!InsertTreeItem(hTreeDevices,
  214. RootItem,
  215. NULL,
  216. LvItem,
  217. IsValid,
  218. IsBold,
  219. IsExpanded)) {
  220. return FALSE;
  221. }
  222. if (NULL != (hDevice = TreeView_FindItem(hTreeDevices,
  223. DeviceName))) {
  224. return TreeView_SelectItem (hTreeDevices, hDevice);
  225. }
  226. return TRUE;
  227. }
  228. USBINT_PTR
  229. UsbPowerPopup::OnTimer()
  230. {
  231. if (deviceState == DeviceAttachedError) {
  232. if (S_FALSE == QueryContinue()) {
  233. // Update the device state
  234. deviceState = DeviceDetachedError;
  235. }
  236. }
  237. return 0;
  238. }