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.

543 lines
14 KiB

  1. /*--------------------------------------------------------------------------*
  2. *
  3. * Microsoft Windows
  4. * Copyright (C) Microsoft Corporation, 1992 - 000
  5. *
  6. * File: powertest.cpp
  7. *
  8. * Contents: Implements ACPI test snap-in
  9. *
  10. * History: 29-Feb-2000 jeffro Created
  11. *
  12. *--------------------------------------------------------------------------*/
  13. #include "stdafx.hxx"
  14. #include "powertest.h"
  15. /*+-------------------------------------------------------------------------*
  16. * ShowReturn
  17. *
  18. *
  19. *--------------------------------------------------------------------------*/
  20. static void ShowReturn (const SC& sc, LPCTSTR pszPrefix)
  21. {
  22. tstring strMessage = pszPrefix;
  23. /*
  24. * Successful HRESULTs will map to unsuccessful Win32 error codes,
  25. * so if the SC doesn't contain an error, give it S_OK so GetErrorMessage
  26. * doesn't return confusing error text.
  27. */
  28. SC scLocal = sc;
  29. if (!scLocal.IsError())
  30. scLocal = S_OK;
  31. TCHAR szErrorMessage[256];
  32. scLocal.GetErrorMessage (256, szErrorMessage);
  33. strMessage += szErrorMessage;
  34. if (!sc.IsError() && !(sc == S_OK))
  35. {
  36. strMessage += _T(" ");
  37. if (sc == S_FALSE)
  38. strMessage += _T("(S_FALSE)");
  39. else
  40. strMessage += tstring(_T("(")) + _itot(sc.GetCode(), szErrorMessage, 10) + _T(")");
  41. }
  42. MessageBox (NULL, strMessage.data(), _T("Debug message"), MB_OK);
  43. }
  44. /*+=========================================================================*
  45. * *
  46. * CPowerTestSnapinItem Implmentation *
  47. * *
  48. *==========================================================================*/
  49. #define DECLARE_SNAPIN_MENU_ITEM(id, dwGray) \
  50. { id, id, id, CCM_INSERTIONPOINTID_PRIMARY_TOP, 0, dwMenuAlwaysEnable, dwGray, dwMenuNeverChecked}
  51. SnapinMenuItem CPowerTestSnapinItem::s_rgMenuItems[] =
  52. {
  53. DECLARE_SNAPIN_MENU_ITEM (IDS_CreateConsolePower, eFlag_ConsolePowerCreated),
  54. DECLARE_SNAPIN_MENU_ITEM (IDS_ReleaseConsolePower, eFlag_ConsolePowerNotCreated),
  55. DECLARE_SNAPIN_MENU_ITEM (IDS_SetExecutionState, eFlag_ConsolePowerNotCreated),
  56. DECLARE_SNAPIN_MENU_ITEM (IDS_ResetIdleTimer, eFlag_ConsolePowerNotCreated),
  57. };
  58. /*+-------------------------------------------------------------------------*
  59. * CPowerTestSnapinItem::CPowerTestSnapinItem
  60. *
  61. *
  62. *--------------------------------------------------------------------------*/
  63. CPowerTestSnapinItem::CPowerTestSnapinItem() :
  64. m_cSystem (0),
  65. m_cDisplay (0),
  66. m_dwAdviseCookie (0)
  67. {
  68. }
  69. /*+-------------------------------------------------------------------------*
  70. * CPowerTestSnapinItem::DwFlagsMenuGray
  71. *
  72. *
  73. *--------------------------------------------------------------------------*/
  74. DWORD CPowerTestSnapinItem::DwFlagsMenuGray(void)
  75. {
  76. return ((m_spConsolePower) ? eFlag_ConsolePowerCreated : eFlag_ConsolePowerNotCreated);
  77. }
  78. /*+-------------------------------------------------------------------------*
  79. * tstring* CPowerTestSnapinItem::PstrDisplayName
  80. *
  81. *
  82. *--------------------------------------------------------------------------*/
  83. const tstring* CPowerTestSnapinItem::PstrDisplayName()
  84. {
  85. return (&Psnapin()->StrDisplayName());
  86. }
  87. /*+-------------------------------------------------------------------------*
  88. * CPowerTestSnapinItem::ScGetResultViewType
  89. *
  90. *
  91. *--------------------------------------------------------------------------*/
  92. SC CPowerTestSnapinItem::ScGetResultViewType(LPOLESTR* ppViewType, long* pViewOptions)
  93. {
  94. DECLARE_SC (sc, _T("CPowerTestSnapinItem::ScGetResultViewType"));
  95. /*
  96. * use the standard message view OCX
  97. */
  98. sc = StringFromCLSID (CLSID_MessageView, ppViewType);
  99. if (sc)
  100. return (sc);
  101. /*
  102. * use only the OCX
  103. */
  104. *pViewOptions = MMC_VIEW_OPTIONS_NOLISTVIEWS;
  105. return (sc);
  106. }
  107. /*+-------------------------------------------------------------------------*
  108. * CPowerTestSnapinItem::ScOnShow
  109. *
  110. * WM_SHOW handler for CPowerTestSnapinItem.
  111. *--------------------------------------------------------------------------*/
  112. SC CPowerTestSnapinItem::ScOnShow(CComponent *pComponent, BOOL fSelect)
  113. {
  114. DECLARE_SC (sc, _T("CPowerTestSnapinItem::ScOnShow"));
  115. if (fSelect)
  116. {
  117. if (pComponent == NULL)
  118. return (sc = E_UNEXPECTED);
  119. IConsole* pConsole = pComponent->IpConsole();
  120. if (pConsole == NULL)
  121. return (sc = E_NOINTERFACE);
  122. CComPtr<IUnknown> spResultUnk;
  123. sc = pConsole->QueryResultView (&spResultUnk);
  124. if (sc)
  125. return (sc);
  126. m_spMsgView = spResultUnk;
  127. if (m_spMsgView == NULL)
  128. return (sc = E_NOINTERFACE);
  129. UpdateMessageView();
  130. }
  131. else
  132. {
  133. m_spMsgView.Release();
  134. }
  135. return (sc);
  136. }
  137. /*+-------------------------------------------------------------------------*
  138. * CPowerTestSnapinItem::UpdateMessageView
  139. *
  140. *
  141. *--------------------------------------------------------------------------*/
  142. void CPowerTestSnapinItem::UpdateMessageView ()
  143. {
  144. if (m_spMsgView)
  145. {
  146. m_spMsgView->SetIcon (Icon_Information);
  147. m_spMsgView->SetTitleText (L"Power Test Snap-in");
  148. m_spMsgView->SetBodyText (GetMessageText().data());
  149. }
  150. }
  151. /*+-------------------------------------------------------------------------*
  152. * CPowerTestSnapinItem::GetMessageText
  153. *
  154. *
  155. *--------------------------------------------------------------------------*/
  156. std::wstring CPowerTestSnapinItem::GetMessageText()
  157. {
  158. std::wstring strMessageText;
  159. if (m_spConsolePower)
  160. {
  161. WCHAR szMessageText[256];
  162. wsprintfW (szMessageText,
  163. L"CLSID_ConsolePower created\n\n"
  164. L"Current execution state:\n"
  165. L"ES_SYSTEM_REQUIRED = %d\n"
  166. L"ES_DISPLAY_REQUIRED = %d\n",
  167. m_cSystem, m_cDisplay);
  168. strMessageText = szMessageText;
  169. }
  170. else
  171. strMessageText = L"CLSID_ConsolePower not created";
  172. return (strMessageText);
  173. }
  174. /*+-------------------------------------------------------------------------*
  175. * CPowerTestSnapinItem::Pmenuitem
  176. *
  177. *
  178. *--------------------------------------------------------------------------*/
  179. SnapinMenuItem* CPowerTestSnapinItem::Pmenuitem(void)
  180. {
  181. return (s_rgMenuItems);
  182. }
  183. /*+-------------------------------------------------------------------------*
  184. * CPowerTestSnapinItem::CMenuItem
  185. *
  186. *
  187. *--------------------------------------------------------------------------*/
  188. INT CPowerTestSnapinItem::CMenuItem(void)
  189. {
  190. return (sizeof(s_rgMenuItems) / sizeof(s_rgMenuItems[0]));
  191. }
  192. /*+-------------------------------------------------------------------------*
  193. * CPowerTestSnapinItem::ScCommand
  194. *
  195. *
  196. *--------------------------------------------------------------------------*/
  197. SC CPowerTestSnapinItem::ScCommand(long nCommandID, CComponent *pComponent)
  198. {
  199. DECLARE_SC (sc, _T("CPowerTestSnapinItem::ScCommand"));
  200. switch (nCommandID)
  201. {
  202. case IDS_CreateConsolePower:
  203. sc = ScOnCreateConsolePower (pComponent);
  204. break;
  205. case IDS_ReleaseConsolePower:
  206. sc = ScOnReleaseConsolePower (pComponent);
  207. break;
  208. case IDS_ResetIdleTimer:
  209. sc = ScOnResetIdleTimer (pComponent);
  210. break;
  211. case IDS_SetExecutionState:
  212. sc = ScOnSetExecutionState (pComponent);
  213. break;
  214. default:
  215. sc = E_UNEXPECTED;
  216. break;
  217. }
  218. UpdateMessageView();
  219. return (sc);
  220. }
  221. /*+-------------------------------------------------------------------------*
  222. * CPowerTestSnapinItem::ScOnCreateConsolePower
  223. *
  224. *
  225. *--------------------------------------------------------------------------*/
  226. SC CPowerTestSnapinItem::ScOnCreateConsolePower (CComponent *pComponent)
  227. {
  228. DECLARE_SC (sc, _T("CPowerTestSnapinItem::ScOnCreateConsolePower"));
  229. /*
  230. * create the CLSID_ConsolePower object
  231. */
  232. sc = m_spConsolePower.CoCreateInstance (CLSID_ConsolePower);
  233. if (sc)
  234. return (sc);
  235. /*
  236. * create a CPowerTestConsolePowerSinkImpl
  237. */
  238. CComObject<CPowerTestConsolePowerSinkImpl>* pPowerSinkImpl;
  239. sc = CComObject<CPowerTestConsolePowerSinkImpl>::CreateInstance (&pPowerSinkImpl);
  240. if (sc)
  241. return (ReleaseAll(), sc);
  242. m_spConsolePowerSink = pPowerSinkImpl;
  243. /*
  244. * set up the event sink
  245. */
  246. sc = AtlAdvise (m_spConsolePower, m_spConsolePowerSink, IID_IConsolePowerSink, &m_dwAdviseCookie);
  247. if (sc)
  248. return (ReleaseAll(), sc);
  249. return (sc);
  250. }
  251. /*+-------------------------------------------------------------------------*
  252. * CPowerTestSnapinItem::ScOnReleaseConsolePower
  253. *
  254. *
  255. *--------------------------------------------------------------------------*/
  256. SC CPowerTestSnapinItem::ScOnReleaseConsolePower (CComponent *pComponent)
  257. {
  258. DECLARE_SC (sc, _T("CPowerTestSnapinItem::ScOnCreateConsolePower"));
  259. AtlUnadvise (m_spConsolePower, IID_IConsolePowerSink, m_dwAdviseCookie);
  260. ReleaseAll();
  261. return (sc);
  262. }
  263. /*+-------------------------------------------------------------------------*
  264. * CPowerTestSnapinItem::ReleaseAll
  265. *
  266. *
  267. *--------------------------------------------------------------------------*/
  268. void CPowerTestSnapinItem::ReleaseAll()
  269. {
  270. m_spConsolePower.Release();
  271. m_spConsolePowerSink.Release();
  272. }
  273. /*+-------------------------------------------------------------------------*
  274. * CPowerTestSnapinItem::ScOnResetIdleTimer
  275. *
  276. *
  277. *--------------------------------------------------------------------------*/
  278. SC CPowerTestSnapinItem::ScOnResetIdleTimer (CComponent *pComponent)
  279. {
  280. DECLARE_SC (sc, _T("CPowerTestSnapinItem::ScOnCreateConsolePower"));
  281. if (!m_spConsolePower)
  282. {
  283. MMCErrorBox (sc = E_UNEXPECTED);
  284. sc.Clear();
  285. return (sc);
  286. }
  287. CPowerTestDlg dlg(true);
  288. if (dlg.DoModal() == IDOK)
  289. {
  290. sc = m_spConsolePower->ResetIdleTimer (dlg.GetAddFlags());
  291. ShowReturn (sc, _T("IConsolePower::ResetIdleTimer returned:\n\n"));
  292. }
  293. return (sc);
  294. }
  295. /*+-------------------------------------------------------------------------*
  296. * CPowerTestSnapinItem::ScOnSetExecutionState
  297. *
  298. *
  299. *--------------------------------------------------------------------------*/
  300. SC CPowerTestSnapinItem::ScOnSetExecutionState (CComponent *pComponent)
  301. {
  302. DECLARE_SC (sc, _T("CPowerTestSnapinItem::ScOnCreateConsolePower"));
  303. if (!m_spConsolePower)
  304. {
  305. MMCErrorBox (sc = E_UNEXPECTED);
  306. sc.Clear();
  307. return (sc);
  308. }
  309. CPowerTestDlg dlg;
  310. if (dlg.DoModal() == IDOK)
  311. {
  312. DWORD dwAdd = dlg.GetAddFlags();
  313. DWORD dwRemove = dlg.GetRemoveFlags();
  314. sc = m_spConsolePower->SetExecutionState (dwAdd, dwRemove);
  315. ShowReturn (sc, _T("IConsolePower::SetExecutionState returned:\n\n"));
  316. if (sc == S_OK)
  317. {
  318. if (dwAdd & ES_SYSTEM_REQUIRED) m_cSystem++;
  319. if (dwAdd & ES_DISPLAY_REQUIRED) m_cDisplay++;
  320. if (dwRemove & ES_SYSTEM_REQUIRED) m_cSystem--;
  321. if (dwRemove & ES_DISPLAY_REQUIRED) m_cDisplay--;
  322. }
  323. }
  324. return (sc);
  325. }
  326. /*+=========================================================================*
  327. * *
  328. * CPowerTestConsolePowerSinkImpl Implmentation *
  329. * *
  330. *==========================================================================*/
  331. /*+-------------------------------------------------------------------------*
  332. * CPowerTestConsolePowerSinkImpl::OnPowerBroadcast
  333. *
  334. *
  335. *--------------------------------------------------------------------------*/
  336. STDMETHODIMP CPowerTestConsolePowerSinkImpl::OnPowerBroadcast (
  337. WPARAM wParam,
  338. LPARAM lParam,
  339. LRESULT* plResult)
  340. {
  341. if (plResult == NULL)
  342. return (E_INVALIDARG);
  343. if (wParam == PBT_APMQUERYSUSPEND)
  344. *plResult = BROADCAST_QUERY_DENY;
  345. else
  346. *plResult = TRUE;
  347. return (S_OK);
  348. }
  349. /*+=========================================================================*
  350. * *
  351. * CPowerTestSnapin Implmentation *
  352. * *
  353. *==========================================================================*/
  354. SNR CPowerTestSnapin::s_rgsnr[] =
  355. {
  356. SNR(&nodetypePowerTestRoot, snrEnumSP ),
  357. };
  358. LONG CPowerTestSnapin::s_rgiconid[] = {0};
  359. LONG CPowerTestSnapin::s_iconidStatic = 0;
  360. // include members needed for every snapin.
  361. SNAPIN_DEFINE(CPowerTestSnapin);
  362. /*+-------------------------------------------------------------------------*
  363. * CPowerTestSnapin::CPowerTestSnapin
  364. *
  365. *
  366. *--------------------------------------------------------------------------*/
  367. CPowerTestSnapin::CPowerTestSnapin() :
  368. m_strDisplayName (_T("PowerTest Snap-in"))
  369. {
  370. m_pstrDisplayName = &m_strDisplayName;
  371. }
  372. /*+-------------------------------------------------------------------------*
  373. * CPowerTestSnapin::~CPowerTestSnapin
  374. *
  375. *
  376. *--------------------------------------------------------------------------*/
  377. CPowerTestSnapin::~CPowerTestSnapin()
  378. {
  379. }
  380. /*+-------------------------------------------------------------------------*
  381. * CPowerTestSnapin::ScInitBitmaps
  382. *
  383. *
  384. *--------------------------------------------------------------------------*/
  385. SC CPowerTestSnapin::ScInitBitmaps(void)
  386. {
  387. DECLARE_SC (sc, _T("CPowerTestSnapin::ScInitBitmaps"));
  388. if (BitmapSmall())
  389. return (sc);
  390. sc = CBaseSnapin::ScInitBitmaps();
  391. if (sc)
  392. return (sc);
  393. BitmapSmall().DeleteObject();
  394. sc = BitmapSmall().LoadBitmap(IDB_POWER16) ? S_OK : E_FAIL;
  395. if (sc)
  396. return sc;
  397. BitmapLarge().DeleteObject();
  398. sc = BitmapLarge().LoadBitmap(IDB_POWER32) ? S_OK : E_FAIL;
  399. if (sc)
  400. return sc;
  401. BitmapStaticSmall().DeleteObject();
  402. sc = BitmapStaticSmall().LoadBitmap(IDB_POWER16) ? S_OK : E_FAIL;
  403. if (sc)
  404. return sc;
  405. BitmapStaticSmallOpen().DeleteObject();
  406. sc = BitmapStaticSmallOpen().LoadBitmap(IDB_POWER16) ? S_OK : E_FAIL;
  407. if (sc)
  408. return sc;
  409. BitmapStaticLarge().DeleteObject();
  410. sc = BitmapStaticLarge().LoadBitmap(IDB_POWER32) ? S_OK : E_FAIL;
  411. if (sc)
  412. return sc;
  413. return sc;
  414. }