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.

1174 lines
40 KiB

  1. /*******************************************************************************
  2. *
  3. * Copyright 1999 American Power Conversion, All Rights Reserved
  4. *
  5. * TITLE: UPSCONFIG.C
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: TedC
  10. *
  11. * DATE: 07 June, 1999
  12. *
  13. * DESCRIPTION: Dialog for configuring UPS service behavior:
  14. * - display popup messages on power failure
  15. * - wait X seconds before displaying warnings
  16. * - repeat warning messages every X seconds
  17. * - shutdown X minutes after power fails
  18. * - ALWAYS shutdown when low battery
  19. * - execute a task before shutdown
  20. * - turn off the UPS after shutdown
  21. ******************************************************************************/
  22. /********************* Header Files ************************/
  23. #include <nt.h>
  24. #include <ntrtl.h>
  25. #include <nturtl.h>
  26. #include "upstab.h"
  27. #include "..\powercfg.h"
  28. #include "..\pwrresid.h"
  29. #include "..\PwrMn_cs.h"
  30. #define VALIDDIGITS 3 // number of digits allowed in each edit box
  31. //
  32. // This structure is filled in by the Power Policy Manager at CPL_INIT time.
  33. //
  34. extern SYSTEM_POWER_CAPABILITIES g_SysPwrCapabilities;
  35. // local copy of UPS Configuration settings
  36. static DWORD g_ulWaitSeconds = 0;
  37. static DWORD g_ulRepeatSeconds = 0;
  38. static DWORD g_ulOnBatteryMinutes = 0;
  39. static DWORD g_ulNotifyEnable = 0;
  40. static DWORD g_ulShutdownOnBattery = 0;
  41. static DWORD g_ulCriticalPowerAction = 0;
  42. static DWORD g_ulRunTaskEnable = 0;
  43. static DWORD g_ulTurnOffUPS = 0;
  44. static TCHAR g_szTaskName[MAX_PATH] = _T("");
  45. static DWORD g_ulOptions = 0;
  46. static BOOL g_bPowerFailSignal = FALSE;
  47. static BOOL g_bLowBatterySignal = FALSE;
  48. static BOOL g_bShutOffSignal = FALSE;
  49. // context-sensitive help table
  50. const DWORD g_UPSConfigHelpIDs[]=
  51. {
  52. IDC_NOTIFYCHECKBOX,idh_enable_notification,
  53. IDC_WAITTEXT,idh_first_warning_delay,
  54. IDC_WAITEDITBOX,idh_first_warning_delay,
  55. IDC_WAITSPIN,idh_first_warning_delay,
  56. IDC_REPEATTEXT,idh_warning_message_interval,
  57. IDC_REPEATEDITBOX,idh_warning_message_interval,
  58. IDC_REPEATSPIN,idh_warning_message_interval,
  59. IDC_SHUTDOWNTIMERCHECKBOX,idh_time_before_critical_action,
  60. IDC_SHUTDOWNTIMEREDITBOX,idh_time_before_critical_action,
  61. IDC_TIMERSPIN,idh_time_before_critical_action,
  62. IDC_SHUTDOWNTEXT,idh_time_before_critical_action,
  63. IDC_LOWBATTERYSHUTDOWNTEXT,idh_low_battery,
  64. IDC_POWERACTIONTEXT,idh_shutdown_or_hibernate,
  65. IDC_POWERACTIONCOMBO,idh_shutdown_or_hibernate,
  66. IDC_RUNTASKCHECKBOX,idh_run_program,
  67. IDC_TASKNAMETEXT,idh_run_program,
  68. IDC_CONFIGURETASKBUTTON,idh_configure_program,
  69. IDC_TURNOFFCHECKBOX,idh_ups_turn_off,
  70. IDC_STATIC, NO_HELP,
  71. IDC_SHUTDOWNGROUPBOX, NO_HELP,
  72. 0, 0
  73. };
  74. /*******************************************************************************
  75. *
  76. * GetRegistryValues
  77. *
  78. * DESCRIPTION: Initialize UPS configuration settings from the registry
  79. *
  80. * PARAMETERS:
  81. *
  82. *******************************************************************************/
  83. void GetRegistryValues()
  84. {
  85. GetUPSConfigFirstMessageDelay(&g_ulWaitSeconds);
  86. GetUPSConfigNotifyEnable(&g_ulNotifyEnable);
  87. GetUPSConfigMessageInterval(&g_ulRepeatSeconds);
  88. GetUPSConfigShutdownOnBatteryEnable(&g_ulShutdownOnBattery);
  89. GetUPSConfigShutdownOnBatteryWait(&g_ulOnBatteryMinutes);
  90. GetUPSConfigCriticalPowerAction(&g_ulCriticalPowerAction);
  91. GetUPSConfigRunTaskEnable(&g_ulRunTaskEnable);
  92. GetUPSConfigTaskName(g_szTaskName);
  93. if (!_tcsclen(g_szTaskName)) {
  94. // The taskname in the registry is NULL so
  95. // get the default taskname from the resource file.
  96. LoadString(GetUPSModuleHandle(),
  97. IDS_SHUTDOWN_TASKNAME,
  98. (LPTSTR) g_szTaskName,
  99. sizeof(g_szTaskName)/sizeof(TCHAR));
  100. }
  101. GetUPSConfigTurnOffEnable(&g_ulTurnOffUPS);
  102. GetUPSConfigOptions(&g_ulOptions);
  103. }
  104. /*******************************************************************************
  105. *
  106. * SetRegistryValues
  107. *
  108. * DESCRIPTION: Flush UPS configuration settings to the registry
  109. *
  110. * PARAMETERS:
  111. *
  112. *******************************************************************************/
  113. void SetRegistryValues()
  114. {
  115. SetUPSConfigFirstMessageDelay(g_ulWaitSeconds);
  116. SetUPSConfigNotifyEnable(g_ulNotifyEnable);
  117. SetUPSConfigMessageInterval(g_ulRepeatSeconds);
  118. SetUPSConfigShutdownOnBatteryEnable(g_ulShutdownOnBattery);
  119. SetUPSConfigShutdownOnBatteryWait(g_ulOnBatteryMinutes);
  120. SetUPSConfigCriticalPowerAction(g_ulCriticalPowerAction);
  121. SetUPSConfigRunTaskEnable(g_ulRunTaskEnable);
  122. SetUPSConfigTaskName(g_szTaskName);
  123. SetUPSConfigTurnOffEnable(g_ulTurnOffUPS);
  124. }
  125. /*******************************************************************************
  126. *
  127. * ErrorBox
  128. *
  129. * DESCRIPTION: used to display error messagebox when data is out of range
  130. *
  131. * PARAMETERS: hWnd
  132. * wText
  133. * wCaption
  134. * wType
  135. *
  136. *******************************************************************************/
  137. void ErrorBox (HWND hWnd, DWORD wText, DWORD wCaption, DWORD wType)
  138. {
  139. TCHAR szText[256+MAX_PATH], szCaption[256];
  140. HANDLE hModule;
  141. hModule = GetUPSModuleHandle ();
  142. LoadString(hModule, wCaption, szCaption, sizeof (szCaption)/sizeof(TCHAR));
  143. LoadString(hModule, wText, szText, sizeof (szText)/sizeof(TCHAR));
  144. MessageBox(hWnd, szText, szCaption, MB_OK|MB_ICONSTOP);
  145. }
  146. /*******************************************************************************
  147. *
  148. * ValidWaitSeconds
  149. *
  150. * DESCRIPTION: check if user's data is in range
  151. *
  152. * PARAMETERS: hDlg
  153. *
  154. *******************************************************************************/
  155. BOOL ValidWaitSeconds(HWND hDlg)
  156. {
  157. // Popup warning if g_ulWaitSeconds is not in the valid range:
  158. if ( g_ulWaitSeconds > (DWORD)WAITSECONDSLASTVAL )
  159. {
  160. ErrorBox(hDlg, IDS_OUTOFWAITRANGE, IDS_NOTIFYCAPTION, MB_OK|MB_ICONSTOP);
  161. SetFocus (GetDlgItem (hDlg, IDC_WAITEDITBOX));
  162. SendMessage(GetDlgItem (hDlg, IDC_WAITEDITBOX),EM_SETSEL, 0, -1L);
  163. return FALSE;
  164. }
  165. return TRUE;
  166. }
  167. /*******************************************************************************
  168. *
  169. * ValidRepeatSeconds
  170. *
  171. * DESCRIPTION: check if user's data is in range
  172. *
  173. * PARAMETERS: hDlg
  174. *
  175. *******************************************************************************/
  176. BOOL ValidRepeatSeconds(HWND hDlg)
  177. {
  178. // Popup warning if g_ulWaitSeconds is not in the valid range:
  179. if ((g_ulRepeatSeconds < (DWORD)REPEATSECONDSFIRSTVAL) ||
  180. (g_ulRepeatSeconds > (DWORD)REPEATSECONDSLASTVAL ))
  181. {
  182. ErrorBox(hDlg, IDS_OUTOFREPEATRANGE, IDS_NOTIFYCAPTION, MB_OK|MB_ICONSTOP);
  183. SetFocus (GetDlgItem (hDlg, IDC_REPEATEDITBOX));
  184. SendMessage(GetDlgItem (hDlg, IDC_REPEATEDITBOX),EM_SETSEL, 0, -1L);
  185. return FALSE;
  186. }
  187. return TRUE;
  188. }
  189. /*******************************************************************************
  190. *
  191. * ValidShutdownDelay
  192. *
  193. * DESCRIPTION: check if user's data is in range
  194. *
  195. * PARAMETERS: hDlg
  196. *
  197. *******************************************************************************/
  198. BOOL ValidShutdownDelay(HWND hDlg)
  199. {
  200. // Popup warning if shutdown delay is not in the valid range:
  201. if ((g_ulOnBatteryMinutes< (DWORD)SHUTDOWNTIMERMINUTESFIRSTVAL) ||
  202. (g_ulOnBatteryMinutes > (DWORD)SHUTDOWNTIMERMINUTESLASTVAL ))
  203. {
  204. ErrorBox(hDlg, IDS_OUTOFSHUTDELAYRANGE, IDS_SHUTDOWNCAPTION, MB_OK|MB_ICONSTOP);
  205. SetFocus (GetDlgItem (hDlg,IDC_SHUTDOWNTIMEREDITBOX));
  206. SendMessage(GetDlgItem (hDlg,IDC_SHUTDOWNTIMEREDITBOX),EM_SETSEL, 0, -1L);
  207. return FALSE;
  208. }
  209. return TRUE;
  210. }
  211. /*******************************************************************************
  212. *
  213. * ValidateFields
  214. *
  215. * DESCRIPTION: Validate all the user's data before we save the values
  216. *
  217. * PARAMETERS: hDlg
  218. *
  219. *******************************************************************************/
  220. BOOL ValidateFields(HWND hDlg)
  221. {
  222. BOOL bOK;
  223. // get the Notification configuration
  224. if (g_ulNotifyEnable = IsDlgButtonChecked (hDlg, IDC_NOTIFYCHECKBOX))
  225. {
  226. //g_ulNotifyEnable = 1;
  227. g_ulWaitSeconds = (DWORD) GetDlgItemInt (hDlg,IDC_WAITEDITBOX, &bOK, FALSE);
  228. g_ulRepeatSeconds = (DWORD) GetDlgItemInt (hDlg,IDC_REPEATEDITBOX, &bOK, FALSE);
  229. // check that the delay & interval are in valid range
  230. if ((!ValidWaitSeconds(hDlg)) || (!ValidRepeatSeconds(hDlg)))
  231. return FALSE;
  232. }
  233. // get the Shutdown configuration
  234. if (g_ulShutdownOnBattery = IsDlgButtonChecked (hDlg, IDC_SHUTDOWNTIMERCHECKBOX))
  235. {
  236. //g_ulShutdownOnBattery = 1;
  237. g_ulOnBatteryMinutes = (DWORD) GetDlgItemInt (hDlg,IDC_SHUTDOWNTIMEREDITBOX, &bOK, FALSE);
  238. // check that the shutdown delay is in valid range
  239. if (!ValidShutdownDelay(hDlg))
  240. return FALSE;
  241. }
  242. // get the Shutdown Actions configuration
  243. g_ulRunTaskEnable = IsDlgButtonChecked (hDlg, IDC_RUNTASKCHECKBOX);
  244. g_ulTurnOffUPS = IsDlgButtonChecked (hDlg, IDC_TURNOFFCHECKBOX);
  245. // all configuration data is captured & validated
  246. return TRUE;
  247. }
  248. /*******************************************************************************
  249. *
  250. * APCFileNameOnly
  251. *
  252. * DESCRIPTION: Returns a pointer to the first character after the last
  253. * backslash in a string
  254. *
  255. * PARAMETERS: sz: string from which to strip the path
  256. *
  257. *******************************************************************************/
  258. LPTSTR APCFileNameOnly(LPTSTR sz)
  259. {
  260. LPTSTR next = sz;
  261. LPTSTR prev;
  262. LPTSTR begin = next;
  263. if (next == NULL) {
  264. return NULL;
  265. }
  266. while ( *next ) {
  267. prev = next;
  268. next++;
  269. if ( (*prev == TEXT('\\')) || (*prev == TEXT(':')) ) {
  270. begin = next;
  271. }
  272. }
  273. return begin;
  274. }
  275. /*******************************************************************************
  276. *
  277. * GetTaskApplicationInfo
  278. *
  279. * DESCRIPTION: if the UPS System Shutdown task exists get the application name
  280. *
  281. * PARAMETERS: aBuffer:
  282. *
  283. *******************************************************************************/
  284. BOOL GetTaskApplicationInfo(LPTSTR aBuffer){
  285. HRESULT hr;
  286. ITaskScheduler *task_sched = NULL;
  287. ITask *shutdown_task = NULL;
  288. BOOL taskExists = FALSE;
  289. //
  290. // if there is no task name, don't bother
  291. //
  292. if (_tcsclen(g_szTaskName)) {
  293. //
  294. // Get a handle to the ITaskScheduler COM Object
  295. //
  296. hr = CoCreateInstance( &CLSID_CSchedulingAgent,
  297. NULL,
  298. CLSCTX_INPROC_SERVER,
  299. &IID_ISchedulingAgent,
  300. (LPVOID *)&task_sched);
  301. if (SUCCEEDED(hr)) {
  302. //
  303. // Get an instance of the Task if it already exists
  304. //
  305. hr = task_sched->lpVtbl->Activate( task_sched,
  306. g_szTaskName,
  307. &IID_ITask,
  308. (IUnknown**)&shutdown_task);
  309. if (SUCCEEDED(hr)) {
  310. LPTSTR lpszTaskApplicationName;
  311. if (aBuffer != NULL) {
  312. //
  313. // get the app name
  314. //
  315. shutdown_task->lpVtbl->GetApplicationName( shutdown_task,
  316. &lpszTaskApplicationName);
  317. _tcscpy(aBuffer,lpszTaskApplicationName);
  318. }
  319. //
  320. // release the task
  321. //
  322. shutdown_task->lpVtbl->Release(shutdown_task);
  323. shutdown_task = NULL;
  324. taskExists = TRUE;
  325. }
  326. //
  327. // Release the instance of the Task Scheduler
  328. //
  329. task_sched->lpVtbl->Release(task_sched);
  330. task_sched = NULL;
  331. }
  332. }
  333. return taskExists;
  334. }
  335. /*******************************************************************************
  336. *
  337. * EditWorkItem
  338. *
  339. * DESCRIPTION: Opens the specified task or creates a new one if the specified
  340. * name is not found in the task list.
  341. *
  342. * PARAMETERS: hWnd : handle to the parent window
  343. * pszTaskName : task to edit (create or open existing)
  344. *
  345. *******************************************************************************/
  346. void EditWorkItem_UPS(HWND hWnd)
  347. {
  348. HRESULT hr;
  349. ITask *pITask = NULL;
  350. ITaskScheduler *pISchedAgent = NULL;
  351. IPersistFile *pIPersistFile = NULL;
  352. TCHAR szTaskApplicationName[MAX_PATH] = _T("");
  353. unsigned long ulSchedAgentHandle = 0;
  354. //
  355. // if there is no task name, don't even bother
  356. //
  357. if (_tcsclen(g_szTaskName)) {
  358. //
  359. // get an instance of the scheduler
  360. //
  361. hr = CoCreateInstance( &CLSID_CSchedulingAgent,
  362. NULL,
  363. CLSCTX_INPROC_SERVER,
  364. &IID_ISchedulingAgent,
  365. (LPVOID*)&pISchedAgent);
  366. if (SUCCEEDED(hr)) {
  367. //
  368. // get an instance of the task if it exists...
  369. //
  370. hr = pISchedAgent->lpVtbl->Activate(pISchedAgent,
  371. g_szTaskName,
  372. &IID_ITask,
  373. &(IUnknown *)pITask);
  374. if (HRESULT_CODE (hr) == ERROR_FILE_NOT_FOUND){
  375. //
  376. // otherwise create a new task instance
  377. //
  378. hr = pISchedAgent->lpVtbl->NewWorkItem(
  379. pISchedAgent,
  380. g_szTaskName,
  381. &CLSID_CTask,
  382. &IID_ITask,
  383. &(IUnknown *)pITask);
  384. if (SUCCEEDED(hr)) {
  385. //
  386. // Commit new work item to disk before editing.
  387. //
  388. hr = pITask->lpVtbl->QueryInterface(pITask, &IID_IPersistFile,
  389. (void **)&pIPersistFile);
  390. if (SUCCEEDED(hr)) {
  391. hr = pIPersistFile->lpVtbl->Save(pIPersistFile, NULL, TRUE);
  392. pIPersistFile->lpVtbl->Release(pIPersistFile);
  393. }
  394. }
  395. }
  396. //
  397. // register the task scheduler agent in the ROT table
  398. //
  399. if (SUCCEEDED(hr)) {
  400. hr = RegisterActiveObject(
  401. (IUnknown *) pITask,
  402. &CLSID_CTask,
  403. ACTIVEOBJECT_WEAK,
  404. &ulSchedAgentHandle);
  405. //
  406. // allow the user to edit the task
  407. //
  408. if(SUCCEEDED(hr)) {
  409. pITask->lpVtbl->EditWorkItem(pITask, hWnd, 0);
  410. //
  411. // user is finished; remove the task scheduler agent from the ROT
  412. //
  413. if(ulSchedAgentHandle != 0){
  414. RevokeActiveObject(ulSchedAgentHandle, NULL);
  415. }
  416. }
  417. //
  418. // release the task
  419. //
  420. pITask->lpVtbl->Release(pITask);
  421. pITask = NULL;
  422. }
  423. //
  424. // release the agent
  425. //
  426. pISchedAgent->lpVtbl->Release(pISchedAgent);
  427. pISchedAgent = NULL;
  428. }
  429. //
  430. // if a task was successfully created, display the task's program name;
  431. //
  432. if (GetTaskApplicationInfo(szTaskApplicationName)){
  433. SetDlgItemText (hWnd, IDC_TASKNAMETEXT, APCFileNameOnly(szTaskApplicationName));
  434. }
  435. }
  436. }
  437. /*******************************************************************************
  438. *
  439. * OnNotifyWaitSpin
  440. *
  441. * DESCRIPTION: handles all notification messages for the Notification spin control
  442. *
  443. * PARAMETERS: lParam:
  444. *
  445. *******************************************************************************/
  446. BOOL OnNotifyWaitSpin( LPARAM lParam )
  447. {
  448. LPNMUPDOWN lpNMUpDown = (LPNMUPDOWN)lParam;
  449. UINT uNotify = lpNMUpDown->hdr.code;
  450. switch( uNotify )
  451. {
  452. case UDN_DELTAPOS:
  453. #if WAITSECONDSFIRSTVAL // Code is dead if WAITSECONDSFIRSTVAL == 0 since unsigneds cannot go < 0
  454. if ((g_ulWaitSeconds < (DWORD)WAITSECONDSFIRSTVAL) && (lpNMUpDown->iDelta > 0))
  455. {
  456. /*
  457. * user has spec'd a value less than min and wants
  458. * to scroll up, so first value should be min
  459. */
  460. g_ulWaitSeconds = WAITSECONDSFIRSTVAL;
  461. lpNMUpDown->iDelta=0; // to disallow request
  462. }
  463. else
  464. #endif
  465. if ((g_ulWaitSeconds > (DWORD)WAITSECONDSLASTVAL ) && (lpNMUpDown->iDelta < 0))
  466. {
  467. /*
  468. * user had spec'd a value greater than max and wants
  469. * to scroll down, so first value should be max
  470. */
  471. g_ulWaitSeconds = WAITSECONDSLASTVAL;
  472. lpNMUpDown->iDelta=0; // to disallow request
  473. }
  474. break;
  475. default:
  476. break;
  477. }
  478. return FALSE;
  479. }
  480. /*******************************************************************************
  481. *
  482. * OnNotifyRepeatSpin
  483. *
  484. * DESCRIPTION: handles all notification messages for the Repeat spin control
  485. *
  486. * PARAMETERS: lParam:
  487. *
  488. *******************************************************************************/
  489. BOOL OnNotifyRepeatSpin( LPARAM lParam )
  490. {
  491. LPNMUPDOWN lpNMUpDown = (LPNMUPDOWN)lParam;
  492. UINT uNotify = lpNMUpDown->hdr.code;
  493. switch( uNotify )
  494. {
  495. case UDN_DELTAPOS:
  496. if ((g_ulRepeatSeconds < (DWORD)REPEATSECONDSFIRSTVAL) && (lpNMUpDown->iDelta > 0))
  497. {
  498. /*
  499. * user has spec'd a value less than min and wants
  500. * to scroll up, so first value should be min
  501. */
  502. g_ulRepeatSeconds = REPEATSECONDSFIRSTVAL;
  503. lpNMUpDown->iDelta=0; // to disallow request
  504. }
  505. else if ((g_ulRepeatSeconds > (DWORD)REPEATSECONDSLASTVAL ) && (lpNMUpDown->iDelta < 0))
  506. {
  507. /*
  508. * user had spec'd a value greater than max and wants
  509. * to scroll down, so first value should be max
  510. */
  511. g_ulRepeatSeconds = REPEATSECONDSLASTVAL;
  512. lpNMUpDown->iDelta=0; // to disallow request
  513. }
  514. break;
  515. default:
  516. break;
  517. }
  518. return FALSE;
  519. }
  520. /*******************************************************************************
  521. *
  522. * OnNotifyTimerSpin
  523. *
  524. * DESCRIPTION: handles all notification messages for the Timer spin control
  525. *
  526. * PARAMETERS: lParam:
  527. *
  528. *******************************************************************************/
  529. BOOL OnNotifyTimerSpin( LPARAM lParam )
  530. {
  531. LPNMUPDOWN lpNMUpDown = (LPNMUPDOWN)lParam;
  532. UINT uNotify = lpNMUpDown->hdr.code;
  533. switch( uNotify )
  534. {
  535. case UDN_DELTAPOS:
  536. if ((g_ulOnBatteryMinutes < (DWORD)SHUTDOWNTIMERMINUTESFIRSTVAL) && (lpNMUpDown->iDelta > 0))
  537. {
  538. /*
  539. * user has spec'd a value less than min and wants
  540. * to scroll up, so first value should be min
  541. */
  542. g_ulOnBatteryMinutes = SHUTDOWNTIMERMINUTESFIRSTVAL;
  543. lpNMUpDown->iDelta=0; // to disallow request
  544. }
  545. else if ((g_ulOnBatteryMinutes > (DWORD)SHUTDOWNTIMERMINUTESLASTVAL ) && (lpNMUpDown->iDelta < 0))
  546. {
  547. /*
  548. * user had spec'd a value greater than max and wants
  549. * to scroll down, so first value should be max
  550. */
  551. g_ulOnBatteryMinutes = SHUTDOWNTIMERMINUTESLASTVAL;
  552. lpNMUpDown->iDelta=0; // to disallow request
  553. }
  554. break;
  555. default:
  556. break;
  557. }
  558. return FALSE;
  559. }
  560. /********************************************************************
  561. *
  562. * FUNCTION: handleSpinners
  563. *
  564. * DESCRIPTION: this function insures that if the user enters out-of-
  565. * bounds spinner values, then click on a spinner, the
  566. * next value shown is the min or max valid value.
  567. *
  568. * PARAMETERS: HWND hWnd - a handle to the main dialog window
  569. * WPARAM wParam - the WPARAM parameter to the Window's
  570. * CALLBACK function.
  571. * LPARAM lParam - the LPARAM parameter to the Window's
  572. * CALLBACK function
  573. *
  574. * RETURNS: TRUE to deny request, FALSE to allow it
  575. * (NOTE: testing suggests that this has no affect)
  576. *
  577. *********************************************************************/
  578. /*******************************************************************************
  579. *
  580. * OnNotificationCheckBox
  581. *
  582. * DESCRIPTION: Command handler for the notification check box
  583. *
  584. * PARAMETERS: hWnd:
  585. *
  586. *******************************************************************************/
  587. BOOL OnNotificationCheckBox( HWND hWnd )
  588. {
  589. g_ulNotifyEnable = IsDlgButtonChecked( hWnd, IDC_NOTIFYCHECKBOX );
  590. EnableWindow( GetDlgItem( hWnd, IDC_WAITEDITBOX ), g_ulNotifyEnable );
  591. EnableWindow( GetDlgItem( hWnd, IDC_WAITTEXT ), g_ulNotifyEnable );
  592. EnableWindow( GetDlgItem( hWnd, IDC_REPEATEDITBOX ), g_ulNotifyEnable );
  593. EnableWindow( GetDlgItem( hWnd, IDC_REPEATTEXT ), g_ulNotifyEnable );
  594. EnableWindow( GetDlgItem( hWnd, IDC_REPEATSPIN ), g_ulNotifyEnable );
  595. EnableWindow( GetDlgItem( hWnd, IDC_WAITSPIN ), g_ulNotifyEnable );
  596. return TRUE;
  597. }
  598. /*******************************************************************************
  599. *
  600. * OnShutdownTimerCheckBox
  601. *
  602. * DESCRIPTION: Command handler for the Timer check box
  603. *
  604. * PARAMETERS: hWnd:
  605. *
  606. *******************************************************************************/
  607. BOOL OnShutdownTimerCheckBox( HWND hWnd )
  608. {
  609. g_ulShutdownOnBattery = IsDlgButtonChecked( hWnd, IDC_SHUTDOWNTIMERCHECKBOX );
  610. EnableWindow( GetDlgItem( hWnd, IDC_SHUTDOWNTIMEREDITBOX ), g_ulShutdownOnBattery );
  611. EnableWindow( GetDlgItem( hWnd, IDC_TIMERSPIN ), g_ulShutdownOnBattery );
  612. return TRUE;
  613. }
  614. /*******************************************************************************
  615. *
  616. * OnRunTaskCheckBox
  617. *
  618. * DESCRIPTION: Command handler for the run task check box
  619. *
  620. * PARAMETERS: hWnd:
  621. *
  622. *******************************************************************************/
  623. BOOL OnRunTaskCheckBox( HWND hWnd )
  624. {
  625. g_ulRunTaskEnable = IsDlgButtonChecked( hWnd, IDC_RUNTASKCHECKBOX );
  626. EnableWindow( GetDlgItem( hWnd, IDC_RUNTASKCHECKBOX ), TRUE );
  627. EnableWindow( GetDlgItem( hWnd, IDC_TASKNAMETEXT ), g_ulRunTaskEnable );
  628. EnableWindow( GetDlgItem( hWnd, IDC_CONFIGURETASKBUTTON ), g_ulRunTaskEnable );
  629. return TRUE;
  630. }
  631. /*******************************************************************************
  632. *
  633. * OnTurnOffCheckBox
  634. *
  635. * DESCRIPTION: Command handler for the turn off UPS check box
  636. *
  637. * PARAMETERS: hWnd:
  638. *
  639. *******************************************************************************/
  640. BOOL OnTurnOffCheckBox( HWND hWnd )
  641. {
  642. g_ulTurnOffUPS = IsDlgButtonChecked( hWnd, IDC_TURNOFFCHECKBOX );
  643. return TRUE;
  644. }
  645. /*******************************************************************************
  646. *
  647. * OnConfigureTaskButton
  648. *
  649. * DESCRIPTION: Command handler for the configure task button
  650. *
  651. * PARAMETERS: hWnd:
  652. *
  653. *******************************************************************************/
  654. BOOL OnConfigureTaskButton( HWND hWnd )
  655. {
  656. HWND hTaskWnd;
  657. ITask *pITask = NULL;
  658. // if task scheduler window is not already active, start it
  659. if (GetActiveObject(&CLSID_CTask, NULL,&(IUnknown*)pITask) != S_OK)
  660. {
  661. EditWorkItem_UPS(hWnd);
  662. }
  663. else
  664. {
  665. // task scheduler window already active, pop to foreground
  666. hTaskWnd = FindWindow( NULL, g_szTaskName);
  667. BringWindowToTop(hTaskWnd);
  668. }
  669. return TRUE;
  670. }
  671. /*******************************************************************************
  672. *
  673. * OnPowerActionCombo
  674. *
  675. * DESCRIPTION: Command handler for the power action combobox
  676. *
  677. * PARAMETERS: hWnd:
  678. * wParam:
  679. * lParam
  680. *
  681. *******************************************************************************/
  682. BOOL OnPowerActionCombo(
  683. IN HWND hWnd,
  684. IN WPARAM wParam,
  685. IN LPARAM lParam)
  686. {
  687. BOOL bRetVal = FALSE;
  688. switch(HIWORD(wParam))
  689. {
  690. case CBN_SELCHANGE:
  691. {
  692. g_ulCriticalPowerAction = (DWORD) SendDlgItemMessage( hWnd,
  693. IDC_POWERACTIONCOMBO,
  694. CB_GETCURSEL,
  695. 0,0);
  696. // if Hibernate selected, uncheck the run task
  697. // and disable all associated controls
  698. if( UPS_SHUTDOWN_HIBERNATE == g_ulCriticalPowerAction )
  699. {
  700. g_ulRunTaskEnable = BST_UNCHECKED;
  701. CheckDlgButton( hWnd, IDC_RUNTASKCHECKBOX, (BOOL) BST_UNCHECKED );
  702. EnableWindow( GetDlgItem( hWnd, IDC_RUNTASKCHECKBOX ), FALSE );
  703. EnableWindow( GetDlgItem( hWnd, IDC_TASKNAMETEXT ), FALSE );
  704. EnableWindow( GetDlgItem( hWnd, IDC_CONFIGURETASKBUTTON ), FALSE );
  705. }
  706. else
  707. {
  708. EnableWindow( GetDlgItem( hWnd, IDC_RUNTASKCHECKBOX ), TRUE );
  709. }
  710. }
  711. bRetVal = TRUE;
  712. break;
  713. default:
  714. break;
  715. }
  716. return bRetVal;
  717. }
  718. /*******************************************************************************
  719. *
  720. * OnInitDialog
  721. *
  722. * DESCRIPTION: Handles WM_INITDIALOG message sent to UPSConfigDlgProc
  723. *
  724. * PARAMETERS:
  725. *
  726. *******************************************************************************/
  727. BOOL
  728. OnInitDialog(
  729. IN HWND hWnd,
  730. IN WPARAM wParam,
  731. IN LPARAM lParam)
  732. {
  733. #define SHORTBZ 16
  734. TCHAR szNum[SHORTBZ];
  735. UDACCEL accel;
  736. TCHAR szTaskApplicationName[MAX_PATH] = _T("");
  737. TCHAR szShutdown[SHORTBZ], szHibernate[SHORTBZ];
  738. HANDLE g_hInstance;
  739. BOOL fCallCoUninitialize;
  740. g_hInstance = GetUPSModuleHandle ();
  741. // Initialize COM
  742. fCallCoUninitialize = (S_OK == CoInitialize(NULL));
  743. SetWindowLong(hWnd, DWLP_USER, fCallCoUninitialize);
  744. // Get data from the registry
  745. GetRegistryValues();
  746. g_bPowerFailSignal = g_ulOptions & UPS_POWERFAILSIGNAL;
  747. g_bLowBatterySignal = g_ulOptions & UPS_LOWBATTERYSIGNAL;
  748. g_bShutOffSignal = g_ulOptions & UPS_SHUTOFFSIGNAL;
  749. // Set the number of valid digits in each editbox
  750. SendDlgItemMessage( hWnd,
  751. IDC_WAITEDITBOX,
  752. EM_LIMITTEXT,
  753. VALIDDIGITS, 0L );
  754. SendDlgItemMessage( hWnd,
  755. IDC_REPEATEDITBOX,
  756. EM_LIMITTEXT,
  757. VALIDDIGITS, 0L );
  758. SendDlgItemMessage( hWnd,
  759. IDC_SHUTDOWNTIMEREDITBOX,
  760. EM_LIMITTEXT,
  761. VALIDDIGITS,0L );
  762. // (reverse default behavior)
  763. // set up spinners so that uparrow increases value & downarrow decreases
  764. accel.nSec = 0;
  765. accel.nInc = -1;
  766. SendDlgItemMessage( hWnd, IDC_WAITSPIN, UDM_SETACCEL, 1, (LPARAM)&accel );
  767. SendDlgItemMessage( hWnd, IDC_REPEATSPIN, UDM_SETACCEL, 1, (LPARAM)&accel );
  768. SendDlgItemMessage( hWnd, IDC_TIMERSPIN, UDM_SETACCEL, 1, (LPARAM)&accel );
  769. // Set the range of valid integers for each spinner
  770. SendDlgItemMessage( hWnd,
  771. IDC_WAITSPIN,
  772. UDM_SETRANGE,
  773. 0L,
  774. MAKELONG(WAITSECONDSFIRSTVAL, WAITSECONDSLASTVAL) );
  775. SendDlgItemMessage( hWnd,
  776. IDC_REPEATSPIN,
  777. UDM_SETRANGE,
  778. 0L,
  779. MAKELONG(REPEATSECONDSFIRSTVAL,REPEATSECONDSLASTVAL) );
  780. SendDlgItemMessage( hWnd,
  781. IDC_TIMERSPIN,
  782. UDM_SETRANGE,
  783. 0L,
  784. MAKELONG(SHUTDOWNTIMERMINUTESFIRSTVAL,SHUTDOWNTIMERMINUTESLASTVAL) );
  785. // Set the initial editbox values
  786. _itow (g_ulWaitSeconds, szNum, 10);
  787. SetDlgItemText (hWnd, IDC_WAITEDITBOX, (LPTSTR)szNum);
  788. _itow (g_ulRepeatSeconds, szNum, 10);
  789. SetDlgItemText (hWnd, IDC_REPEATEDITBOX, (LPTSTR)szNum);
  790. _itow (g_ulOnBatteryMinutes, szNum, 10);
  791. SetDlgItemText (hWnd, IDC_SHUTDOWNTIMEREDITBOX, (LPTSTR)szNum);
  792. // Set the initial state of the notification checkbox
  793. // and enable/disable associated controls
  794. CheckDlgButton (hWnd, IDC_NOTIFYCHECKBOX, (BOOL) g_ulNotifyEnable);
  795. OnNotificationCheckBox(hWnd);
  796. // Set the initial state of the shutdown timer checkbox
  797. // and enable/disable associated controls
  798. CheckDlgButton (hWnd, IDC_SHUTDOWNTIMERCHECKBOX, (BOOL) g_ulShutdownOnBattery);
  799. OnShutdownTimerCheckBox(hWnd);
  800. // Set the initial state of the run task checkbox
  801. // and enable/disable associated controls
  802. CheckDlgButton (hWnd, IDC_RUNTASKCHECKBOX, (BOOL) g_ulRunTaskEnable);
  803. OnRunTaskCheckBox(hWnd);
  804. // Display the task's program name
  805. if (GetTaskApplicationInfo(szTaskApplicationName))
  806. {
  807. SetDlgItemText (hWnd, IDC_TASKNAMETEXT, APCFileNameOnly(szTaskApplicationName));
  808. }
  809. // Initialize the power action combo box
  810. LoadString(g_hInstance, IDS_POWEROFF, (LPTSTR) szShutdown, sizeof(szShutdown)/sizeof(TCHAR));
  811. LoadString(g_hInstance, IDS_HIBERNATE, (LPTSTR) szHibernate, sizeof(szHibernate)/sizeof(TCHAR));
  812. SendDlgItemMessage( hWnd,
  813. IDC_POWERACTIONCOMBO,
  814. CB_ADDSTRING,
  815. 0,
  816. (LPARAM) szShutdown);
  817. //
  818. // Offer Hibernate as an option if the Hiberfile is present
  819. //
  820. if(g_SysPwrCapabilities.SystemS4 && g_SysPwrCapabilities.HiberFilePresent) {
  821. SendDlgItemMessage( hWnd,
  822. IDC_POWERACTIONCOMBO,
  823. CB_ADDSTRING,
  824. 0,
  825. (LPARAM) szHibernate );
  826. }
  827. SendDlgItemMessage( hWnd,
  828. IDC_POWERACTIONCOMBO,
  829. CB_SETCURSEL,
  830. g_ulCriticalPowerAction,0);
  831. // if Hibernate selected, disable the run task
  832. if( UPS_SHUTDOWN_HIBERNATE == g_ulCriticalPowerAction )
  833. {
  834. g_ulRunTaskEnable = BST_UNCHECKED;
  835. CheckDlgButton (hWnd, IDC_RUNTASKCHECKBOX, (BOOL) g_ulRunTaskEnable);
  836. OnRunTaskCheckBox(hWnd);
  837. EnableWindow( GetDlgItem( hWnd, IDC_RUNTASKCHECKBOX ), g_ulRunTaskEnable );
  838. }
  839. // Set the initial state of the turn off UPS checkbox
  840. // and enable/disable associated controls
  841. CheckDlgButton (hWnd, IDC_TURNOFFCHECKBOX , (BOOL) g_ulTurnOffUPS);
  842. OnTurnOffCheckBox(hWnd);
  843. // Finally, hide controls that aren't supported based on options key
  844. // ShowWindow(GetDlgItem( hWnd, IDC_WAITEDITBOX ), g_bPowerFailSignal ? SW_SHOW : SW_HIDE);
  845. // ShowWindow(GetDlgItem( hWnd, IDC_WAITSPIN ), g_bPowerFailSignal ? SW_SHOW : SW_HIDE);
  846. // ShowWindow(GetDlgItem( hWnd, IDC_WAITTEXT ), g_bPowerFailSignal ? SW_SHOW : SW_HIDE);
  847. // ShowWindow(GetDlgItem( hWnd, IDC_REPEATEDITBOX ), g_bPowerFailSignal ? SW_SHOW : SW_HIDE);
  848. // ShowWindow(GetDlgItem( hWnd, IDC_REPEATSPIN ), g_bPowerFailSignal ? SW_SHOW : SW_HIDE);
  849. // ShowWindow(GetDlgItem( hWnd, IDC_REPEATTEXT ), g_bPowerFailSignal ? SW_SHOW : SW_HIDE);
  850. ShowWindow(GetDlgItem(hWnd,IDC_LOWBATTERYSHUTDOWNTEXT), g_bLowBatterySignal ? SW_SHOW : SW_HIDE);
  851. ShowWindow(GetDlgItem(hWnd,IDC_TURNOFFCHECKBOX), g_bShutOffSignal ? SW_SHOW : SW_HIDE);
  852. return TRUE;
  853. }
  854. /*******************************************************************************
  855. *
  856. * OnClose
  857. *
  858. * DESCRIPTION: Handles WM_CLOSE message sent to UPSConfigDlgProc
  859. *
  860. * PARAMETERS:
  861. *
  862. *******************************************************************************/
  863. BOOL
  864. OnClose(
  865. IN HWND hWnd,
  866. IN WPARAM wParam,
  867. IN LPARAM lParam)
  868. {
  869. HWND taskHwnd = NULL;
  870. // if task scheduler window is still up, kill it
  871. taskHwnd = FindWindow( NULL, g_szTaskName);
  872. if (taskHwnd)
  873. {
  874. DestroyWindow(taskHwnd);
  875. }
  876. if (GetWindowLong(hWnd, DWLP_USER))
  877. CoUninitialize();
  878. EndDialog(hWnd, wParam);
  879. return TRUE;
  880. }
  881. /*******************************************************************************
  882. *
  883. * OnOK
  884. *
  885. * DESCRIPTION: Handles WM_COMMAND message sent to IDOK
  886. *
  887. * PARAMETERS:
  888. *
  889. *******************************************************************************/
  890. BOOL OnOK(
  891. IN HWND hWnd,
  892. IN WPARAM wParam,
  893. IN LPARAM lParam)
  894. {
  895. if (ValidateFields(hWnd))
  896. {
  897. SetRegistryValues();
  898. AddActiveDataState(CONFIG_DATA_CHANGE);
  899. EnableApplyButton();
  900. return OnClose(hWnd, wParam, lParam);
  901. }
  902. return FALSE;
  903. }
  904. /*******************************************************************************
  905. *
  906. * OnCommand
  907. *
  908. * DESCRIPTION: Handles WM_COMMAND messages sent to UPSConfigDlgProc
  909. *
  910. * PARAMETERS:
  911. *
  912. *******************************************************************************/
  913. BOOL
  914. OnCommand(
  915. IN HWND hWnd,
  916. IN WPARAM wParam,
  917. IN LPARAM lParam
  918. )
  919. {
  920. BOOL bRetVal;
  921. WORD idCtl = LOWORD(wParam);
  922. WORD wNotify = HIWORD(wParam);
  923. //
  924. // Assume we handle the command, the default switch will catch exceptions.
  925. //
  926. bRetVal = TRUE;
  927. switch (idCtl)
  928. {
  929. case IDC_NOTIFYCHECKBOX:
  930. bRetVal = OnNotificationCheckBox(hWnd);
  931. break;
  932. case IDC_SHUTDOWNTIMERCHECKBOX:
  933. bRetVal = OnShutdownTimerCheckBox(hWnd);
  934. break;
  935. case IDC_POWERACTIONCOMBO:
  936. bRetVal = OnPowerActionCombo(hWnd, wParam, lParam);
  937. break;
  938. case IDC_RUNTASKCHECKBOX:
  939. bRetVal = OnRunTaskCheckBox(hWnd);
  940. break;
  941. case IDC_CONFIGURETASKBUTTON:
  942. bRetVal = OnConfigureTaskButton(hWnd);
  943. break;
  944. case IDOK:
  945. bRetVal = OnOK(hWnd, wParam, lParam);
  946. break;
  947. case IDCANCEL: // escape key,cancel buttion
  948. bRetVal = OnClose(hWnd, wParam, lParam);
  949. break;
  950. default:
  951. bRetVal = FALSE; // unhandled command, return FALSE
  952. }
  953. return bRetVal;
  954. }
  955. /*******************************************************************************
  956. *
  957. * OnNotify
  958. *
  959. * DESCRIPTION: Handles WM_NOTIFY messages sent to UPSConfigDlgProc
  960. *
  961. * PARAMETERS:
  962. *
  963. *******************************************************************************/
  964. BOOL
  965. OnNotify(
  966. IN HWND hWnd,
  967. IN WPARAM wParam,
  968. IN LPARAM lParam
  969. )
  970. {
  971. int idCtl = (int) wParam;
  972. switch (idCtl) {
  973. case IDC_WAITSPIN:
  974. OnNotifyWaitSpin( lParam );
  975. break;
  976. case IDC_REPEATSPIN:
  977. OnNotifyRepeatSpin( lParam );
  978. break;
  979. case IDC_TIMERSPIN:
  980. OnNotifyTimerSpin( lParam );
  981. break;
  982. default:
  983. break;
  984. }
  985. return FALSE;
  986. }
  987. /*******************************************************************************
  988. *
  989. * UPSConfigDlgProc
  990. *
  991. * DESCRIPTION:
  992. *
  993. * PARAMETERS:
  994. *
  995. *******************************************************************************/
  996. INT_PTR CALLBACK UPSConfigDlgProc(
  997. HWND hWnd,
  998. UINT uMsg,
  999. WPARAM wParam,
  1000. LPARAM lParam
  1001. )
  1002. {
  1003. BOOL bRet = TRUE;
  1004. switch (uMsg) {
  1005. case WM_INITDIALOG:
  1006. OnInitDialog(hWnd,wParam,lParam);
  1007. break;
  1008. case WM_COMMAND:
  1009. OnCommand(hWnd,wParam,lParam);
  1010. break;
  1011. case WM_HELP: // F1
  1012. WinHelp(((LPHELPINFO)lParam)->hItemHandle,
  1013. PWRMANHLP,
  1014. HELP_WM_HELP,
  1015. (ULONG_PTR)(LPTSTR)g_UPSConfigHelpIDs);
  1016. break;
  1017. case WM_CONTEXTMENU: // right mouse click
  1018. WinHelp((HWND)wParam,
  1019. PWRMANHLP,
  1020. HELP_CONTEXTMENU,
  1021. (ULONG_PTR)(LPTSTR)g_UPSConfigHelpIDs);
  1022. break;
  1023. case WM_CLOSE:
  1024. OnClose(hWnd,wParam,lParam);
  1025. break;
  1026. case WM_NOTIFY:
  1027. OnNotify(hWnd,wParam,lParam);
  1028. break;
  1029. default:
  1030. bRet = FALSE;
  1031. break;
  1032. } // switch (uMsg)
  1033. return bRet;
  1034. }