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.

1177 lines
41 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, MAX_PATH);
  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, size_t aBufferLength){
  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. _tcsncpy(aBuffer,lpszTaskApplicationName, (aBufferLength - 1));
  318. CoTaskMemFree(lpszTaskApplicationName);
  319. }
  320. //
  321. // release the task
  322. //
  323. shutdown_task->lpVtbl->Release(shutdown_task);
  324. shutdown_task = NULL;
  325. taskExists = TRUE;
  326. }
  327. //
  328. // Release the instance of the Task Scheduler
  329. //
  330. task_sched->lpVtbl->Release(task_sched);
  331. task_sched = NULL;
  332. }
  333. }
  334. return taskExists;
  335. }
  336. /*******************************************************************************
  337. *
  338. * EditWorkItem
  339. *
  340. * DESCRIPTION: Opens the specified task or creates a new one if the specified
  341. * name is not found in the task list.
  342. *
  343. * PARAMETERS: hWnd : handle to the parent window
  344. * pszTaskName : task to edit (create or open existing)
  345. *
  346. *******************************************************************************/
  347. void EditWorkItem_UPS(HWND hWnd)
  348. {
  349. HRESULT hr;
  350. ITask *pITask = NULL;
  351. ITaskScheduler *pISchedAgent = NULL;
  352. IPersistFile *pIPersistFile = NULL;
  353. TCHAR szTaskApplicationName[MAX_PATH] = _T("");
  354. unsigned long ulSchedAgentHandle = 0;
  355. //
  356. // if there is no task name, don't even bother
  357. //
  358. if (_tcsclen(g_szTaskName)) {
  359. //
  360. // get an instance of the scheduler
  361. //
  362. hr = CoCreateInstance( &CLSID_CSchedulingAgent,
  363. NULL,
  364. CLSCTX_INPROC_SERVER,
  365. &IID_ISchedulingAgent,
  366. (LPVOID*)&pISchedAgent);
  367. if (SUCCEEDED(hr)) {
  368. //
  369. // get an instance of the task if it exists...
  370. //
  371. hr = pISchedAgent->lpVtbl->Activate(pISchedAgent,
  372. g_szTaskName,
  373. &IID_ITask,
  374. &(IUnknown *)pITask);
  375. if (HRESULT_CODE (hr) == ERROR_FILE_NOT_FOUND){
  376. //
  377. // otherwise create a new task instance
  378. //
  379. hr = pISchedAgent->lpVtbl->NewWorkItem(
  380. pISchedAgent,
  381. g_szTaskName,
  382. &CLSID_CTask,
  383. &IID_ITask,
  384. &(IUnknown *)pITask);
  385. if (SUCCEEDED(hr)) {
  386. //
  387. // Commit new work item to disk before editing.
  388. //
  389. hr = pITask->lpVtbl->QueryInterface(pITask, &IID_IPersistFile,
  390. (void **)&pIPersistFile);
  391. if (SUCCEEDED(hr)) {
  392. hr = pIPersistFile->lpVtbl->Save(pIPersistFile, NULL, TRUE);
  393. pIPersistFile->lpVtbl->Release(pIPersistFile);
  394. }
  395. }
  396. }
  397. //
  398. // register the task scheduler agent in the ROT table
  399. //
  400. if (SUCCEEDED(hr)) {
  401. hr = RegisterActiveObject(
  402. (IUnknown *) pITask,
  403. &CLSID_CTask,
  404. ACTIVEOBJECT_WEAK,
  405. &ulSchedAgentHandle);
  406. //
  407. // allow the user to edit the task
  408. //
  409. if(SUCCEEDED(hr)) {
  410. pITask->lpVtbl->EditWorkItem(pITask, hWnd, 0);
  411. //
  412. // user is finished; remove the task scheduler agent from the ROT
  413. //
  414. if(ulSchedAgentHandle != 0){
  415. RevokeActiveObject(ulSchedAgentHandle, NULL);
  416. }
  417. }
  418. //
  419. // release the task
  420. //
  421. pITask->lpVtbl->Release(pITask);
  422. pITask = NULL;
  423. }
  424. //
  425. // release the agent
  426. //
  427. pISchedAgent->lpVtbl->Release(pISchedAgent);
  428. pISchedAgent = NULL;
  429. }
  430. //
  431. // if a task was successfully created, display the task's program name;
  432. //
  433. if (GetTaskApplicationInfo(szTaskApplicationName, MAX_PATH)){
  434. SetDlgItemText (hWnd, IDC_TASKNAMETEXT, APCFileNameOnly(szTaskApplicationName));
  435. }
  436. }
  437. }
  438. /*******************************************************************************
  439. *
  440. * OnNotifyWaitSpin
  441. *
  442. * DESCRIPTION: handles all notification messages for the Notification spin control
  443. *
  444. * PARAMETERS: lParam:
  445. *
  446. *******************************************************************************/
  447. BOOL OnNotifyWaitSpin( LPARAM lParam )
  448. {
  449. LPNMUPDOWN lpNMUpDown = (LPNMUPDOWN)lParam;
  450. UINT uNotify = lpNMUpDown->hdr.code;
  451. switch( uNotify )
  452. {
  453. case UDN_DELTAPOS:
  454. #if WAITSECONDSFIRSTVAL // Code is dead if WAITSECONDSFIRSTVAL == 0 since unsigneds cannot go < 0
  455. if ((g_ulWaitSeconds < (DWORD)WAITSECONDSFIRSTVAL) && (lpNMUpDown->iDelta > 0))
  456. {
  457. /*
  458. * user has spec'd a value less than min and wants
  459. * to scroll up, so first value should be min
  460. */
  461. g_ulWaitSeconds = WAITSECONDSFIRSTVAL;
  462. lpNMUpDown->iDelta=0; // to disallow request
  463. }
  464. else
  465. #endif
  466. if ((g_ulWaitSeconds > (DWORD)WAITSECONDSLASTVAL ) && (lpNMUpDown->iDelta < 0))
  467. {
  468. /*
  469. * user had spec'd a value greater than max and wants
  470. * to scroll down, so first value should be max
  471. */
  472. g_ulWaitSeconds = WAITSECONDSLASTVAL;
  473. lpNMUpDown->iDelta=0; // to disallow request
  474. }
  475. break;
  476. default:
  477. break;
  478. }
  479. return FALSE;
  480. }
  481. /*******************************************************************************
  482. *
  483. * OnNotifyRepeatSpin
  484. *
  485. * DESCRIPTION: handles all notification messages for the Repeat spin control
  486. *
  487. * PARAMETERS: lParam:
  488. *
  489. *******************************************************************************/
  490. BOOL OnNotifyRepeatSpin( LPARAM lParam )
  491. {
  492. LPNMUPDOWN lpNMUpDown = (LPNMUPDOWN)lParam;
  493. UINT uNotify = lpNMUpDown->hdr.code;
  494. switch( uNotify )
  495. {
  496. case UDN_DELTAPOS:
  497. if ((g_ulRepeatSeconds < (DWORD)REPEATSECONDSFIRSTVAL) && (lpNMUpDown->iDelta > 0))
  498. {
  499. /*
  500. * user has spec'd a value less than min and wants
  501. * to scroll up, so first value should be min
  502. */
  503. g_ulRepeatSeconds = REPEATSECONDSFIRSTVAL;
  504. lpNMUpDown->iDelta=0; // to disallow request
  505. }
  506. else if ((g_ulRepeatSeconds > (DWORD)REPEATSECONDSLASTVAL ) && (lpNMUpDown->iDelta < 0))
  507. {
  508. /*
  509. * user had spec'd a value greater than max and wants
  510. * to scroll down, so first value should be max
  511. */
  512. g_ulRepeatSeconds = REPEATSECONDSLASTVAL;
  513. lpNMUpDown->iDelta=0; // to disallow request
  514. }
  515. break;
  516. default:
  517. break;
  518. }
  519. return FALSE;
  520. }
  521. /*******************************************************************************
  522. *
  523. * OnNotifyTimerSpin
  524. *
  525. * DESCRIPTION: handles all notification messages for the Timer spin control
  526. *
  527. * PARAMETERS: lParam:
  528. *
  529. *******************************************************************************/
  530. BOOL OnNotifyTimerSpin( LPARAM lParam )
  531. {
  532. LPNMUPDOWN lpNMUpDown = (LPNMUPDOWN)lParam;
  533. UINT uNotify = lpNMUpDown->hdr.code;
  534. switch( uNotify )
  535. {
  536. case UDN_DELTAPOS:
  537. if ((g_ulOnBatteryMinutes < (DWORD)SHUTDOWNTIMERMINUTESFIRSTVAL) && (lpNMUpDown->iDelta > 0))
  538. {
  539. /*
  540. * user has spec'd a value less than min and wants
  541. * to scroll up, so first value should be min
  542. */
  543. g_ulOnBatteryMinutes = SHUTDOWNTIMERMINUTESFIRSTVAL;
  544. lpNMUpDown->iDelta=0; // to disallow request
  545. }
  546. else if ((g_ulOnBatteryMinutes > (DWORD)SHUTDOWNTIMERMINUTESLASTVAL ) && (lpNMUpDown->iDelta < 0))
  547. {
  548. /*
  549. * user had spec'd a value greater than max and wants
  550. * to scroll down, so first value should be max
  551. */
  552. g_ulOnBatteryMinutes = SHUTDOWNTIMERMINUTESLASTVAL;
  553. lpNMUpDown->iDelta=0; // to disallow request
  554. }
  555. break;
  556. default:
  557. break;
  558. }
  559. return FALSE;
  560. }
  561. /********************************************************************
  562. *
  563. * FUNCTION: handleSpinners
  564. *
  565. * DESCRIPTION: this function insures that if the user enters out-of-
  566. * bounds spinner values, then click on a spinner, the
  567. * next value shown is the min or max valid value.
  568. *
  569. * PARAMETERS: HWND hWnd - a handle to the main dialog window
  570. * WPARAM wParam - the WPARAM parameter to the Window's
  571. * CALLBACK function.
  572. * LPARAM lParam - the LPARAM parameter to the Window's
  573. * CALLBACK function
  574. *
  575. * RETURNS: TRUE to deny request, FALSE to allow it
  576. * (NOTE: testing suggests that this has no affect)
  577. *
  578. *********************************************************************/
  579. /*******************************************************************************
  580. *
  581. * OnNotificationCheckBox
  582. *
  583. * DESCRIPTION: Command handler for the notification check box
  584. *
  585. * PARAMETERS: hWnd:
  586. *
  587. *******************************************************************************/
  588. BOOL OnNotificationCheckBox( HWND hWnd )
  589. {
  590. g_ulNotifyEnable = IsDlgButtonChecked( hWnd, IDC_NOTIFYCHECKBOX );
  591. EnableWindow( GetDlgItem( hWnd, IDC_WAITEDITBOX ), g_ulNotifyEnable );
  592. EnableWindow( GetDlgItem( hWnd, IDC_WAITTEXT ), g_ulNotifyEnable );
  593. EnableWindow( GetDlgItem( hWnd, IDC_REPEATEDITBOX ), g_ulNotifyEnable );
  594. EnableWindow( GetDlgItem( hWnd, IDC_REPEATTEXT ), g_ulNotifyEnable );
  595. EnableWindow( GetDlgItem( hWnd, IDC_REPEATSPIN ), g_ulNotifyEnable );
  596. EnableWindow( GetDlgItem( hWnd, IDC_WAITSPIN ), g_ulNotifyEnable );
  597. return TRUE;
  598. }
  599. /*******************************************************************************
  600. *
  601. * OnShutdownTimerCheckBox
  602. *
  603. * DESCRIPTION: Command handler for the Timer check box
  604. *
  605. * PARAMETERS: hWnd:
  606. *
  607. *******************************************************************************/
  608. BOOL OnShutdownTimerCheckBox( HWND hWnd )
  609. {
  610. g_ulShutdownOnBattery = IsDlgButtonChecked( hWnd, IDC_SHUTDOWNTIMERCHECKBOX );
  611. EnableWindow( GetDlgItem( hWnd, IDC_SHUTDOWNTIMEREDITBOX ), g_ulShutdownOnBattery );
  612. EnableWindow( GetDlgItem( hWnd, IDC_TIMERSPIN ), g_ulShutdownOnBattery );
  613. return TRUE;
  614. }
  615. /*******************************************************************************
  616. *
  617. * OnRunTaskCheckBox
  618. *
  619. * DESCRIPTION: Command handler for the run task check box
  620. *
  621. * PARAMETERS: hWnd:
  622. *
  623. *******************************************************************************/
  624. BOOL OnRunTaskCheckBox( HWND hWnd )
  625. {
  626. g_ulRunTaskEnable = IsDlgButtonChecked( hWnd, IDC_RUNTASKCHECKBOX );
  627. EnableWindow( GetDlgItem( hWnd, IDC_RUNTASKCHECKBOX ), TRUE );
  628. EnableWindow( GetDlgItem( hWnd, IDC_TASKNAMETEXT ), g_ulRunTaskEnable );
  629. EnableWindow( GetDlgItem( hWnd, IDC_CONFIGURETASKBUTTON ), g_ulRunTaskEnable );
  630. return TRUE;
  631. }
  632. /*******************************************************************************
  633. *
  634. * OnTurnOffCheckBox
  635. *
  636. * DESCRIPTION: Command handler for the turn off UPS check box
  637. *
  638. * PARAMETERS: hWnd:
  639. *
  640. *******************************************************************************/
  641. BOOL OnTurnOffCheckBox( HWND hWnd )
  642. {
  643. g_ulTurnOffUPS = IsDlgButtonChecked( hWnd, IDC_TURNOFFCHECKBOX );
  644. return TRUE;
  645. }
  646. /*******************************************************************************
  647. *
  648. * OnConfigureTaskButton
  649. *
  650. * DESCRIPTION: Command handler for the configure task button
  651. *
  652. * PARAMETERS: hWnd:
  653. *
  654. *******************************************************************************/
  655. BOOL OnConfigureTaskButton( HWND hWnd )
  656. {
  657. HWND hTaskWnd;
  658. ITask *pITask = NULL;
  659. // if task scheduler window is not already active, start it
  660. if (GetActiveObject(&CLSID_CTask, NULL,&(IUnknown*)pITask) != S_OK)
  661. {
  662. EditWorkItem_UPS(hWnd);
  663. }
  664. else
  665. {
  666. // task scheduler window already active, pop to foreground
  667. hTaskWnd = FindWindow( NULL, g_szTaskName);
  668. BringWindowToTop(hTaskWnd);
  669. }
  670. return TRUE;
  671. }
  672. /*******************************************************************************
  673. *
  674. * OnPowerActionCombo
  675. *
  676. * DESCRIPTION: Command handler for the power action combobox
  677. *
  678. * PARAMETERS: hWnd:
  679. * wParam:
  680. * lParam
  681. *
  682. *******************************************************************************/
  683. BOOL OnPowerActionCombo(
  684. IN HWND hWnd,
  685. IN WPARAM wParam,
  686. IN LPARAM lParam)
  687. {
  688. BOOL bRetVal = FALSE;
  689. switch(HIWORD(wParam))
  690. {
  691. case CBN_SELCHANGE:
  692. {
  693. g_ulCriticalPowerAction = (DWORD) SendDlgItemMessage( hWnd,
  694. IDC_POWERACTIONCOMBO,
  695. CB_GETCURSEL,
  696. 0,0);
  697. // if Hibernate selected, uncheck the run task
  698. // and disable all associated controls
  699. if( UPS_SHUTDOWN_HIBERNATE == g_ulCriticalPowerAction )
  700. {
  701. g_ulRunTaskEnable = BST_UNCHECKED;
  702. CheckDlgButton( hWnd, IDC_RUNTASKCHECKBOX, (BOOL) BST_UNCHECKED );
  703. EnableWindow( GetDlgItem( hWnd, IDC_RUNTASKCHECKBOX ), FALSE );
  704. EnableWindow( GetDlgItem( hWnd, IDC_TASKNAMETEXT ), FALSE );
  705. EnableWindow( GetDlgItem( hWnd, IDC_CONFIGURETASKBUTTON ), FALSE );
  706. }
  707. else
  708. {
  709. EnableWindow( GetDlgItem( hWnd, IDC_RUNTASKCHECKBOX ), TRUE );
  710. }
  711. }
  712. bRetVal = TRUE;
  713. break;
  714. default:
  715. break;
  716. }
  717. return bRetVal;
  718. }
  719. /*******************************************************************************
  720. *
  721. * OnInitDialog
  722. *
  723. * DESCRIPTION: Handles WM_INITDIALOG message sent to UPSConfigDlgProc
  724. *
  725. * PARAMETERS:
  726. *
  727. *******************************************************************************/
  728. BOOL
  729. OnInitDialog(
  730. IN HWND hWnd,
  731. IN WPARAM wParam,
  732. IN LPARAM lParam)
  733. {
  734. #define SHORTBZ 16
  735. TCHAR szNum[SHORTBZ];
  736. UDACCEL accel;
  737. TCHAR szTaskApplicationName[MAX_PATH] = _T("");
  738. TCHAR szShutdown[SHORTBZ], szHibernate[SHORTBZ];
  739. HANDLE g_hInstance;
  740. BOOL fCallCoUninitialize;
  741. g_hInstance = GetUPSModuleHandle ();
  742. // Initialize COM
  743. fCallCoUninitialize = (S_OK == CoInitialize(NULL));
  744. SetWindowLong(hWnd, DWLP_USER, fCallCoUninitialize);
  745. // Get data from the registry
  746. GetRegistryValues();
  747. g_bPowerFailSignal = g_ulOptions & UPS_POWERFAILSIGNAL;
  748. g_bLowBatterySignal = g_ulOptions & UPS_LOWBATTERYSIGNAL;
  749. g_bShutOffSignal = g_ulOptions & UPS_SHUTOFFSIGNAL;
  750. // Set the number of valid digits in each editbox
  751. SendDlgItemMessage( hWnd,
  752. IDC_WAITEDITBOX,
  753. EM_LIMITTEXT,
  754. VALIDDIGITS, 0L );
  755. SendDlgItemMessage( hWnd,
  756. IDC_REPEATEDITBOX,
  757. EM_LIMITTEXT,
  758. VALIDDIGITS, 0L );
  759. SendDlgItemMessage( hWnd,
  760. IDC_SHUTDOWNTIMEREDITBOX,
  761. EM_LIMITTEXT,
  762. VALIDDIGITS,0L );
  763. // (reverse default behavior)
  764. // set up spinners so that uparrow increases value & downarrow decreases
  765. accel.nSec = 0;
  766. accel.nInc = -1;
  767. SendDlgItemMessage( hWnd, IDC_WAITSPIN, UDM_SETACCEL, 1, (LPARAM)&accel );
  768. SendDlgItemMessage( hWnd, IDC_REPEATSPIN, UDM_SETACCEL, 1, (LPARAM)&accel );
  769. SendDlgItemMessage( hWnd, IDC_TIMERSPIN, UDM_SETACCEL, 1, (LPARAM)&accel );
  770. // Set the range of valid integers for each spinner
  771. SendDlgItemMessage( hWnd,
  772. IDC_WAITSPIN,
  773. UDM_SETRANGE,
  774. 0L,
  775. MAKELONG(WAITSECONDSFIRSTVAL, WAITSECONDSLASTVAL) );
  776. SendDlgItemMessage( hWnd,
  777. IDC_REPEATSPIN,
  778. UDM_SETRANGE,
  779. 0L,
  780. MAKELONG(REPEATSECONDSFIRSTVAL,REPEATSECONDSLASTVAL) );
  781. SendDlgItemMessage( hWnd,
  782. IDC_TIMERSPIN,
  783. UDM_SETRANGE,
  784. 0L,
  785. MAKELONG(SHUTDOWNTIMERMINUTESFIRSTVAL,SHUTDOWNTIMERMINUTESLASTVAL) );
  786. // Set the initial editbox values
  787. _itow (g_ulWaitSeconds, szNum, 10);
  788. SetDlgItemText (hWnd, IDC_WAITEDITBOX, (LPTSTR)szNum);
  789. _itow (g_ulRepeatSeconds, szNum, 10);
  790. SetDlgItemText (hWnd, IDC_REPEATEDITBOX, (LPTSTR)szNum);
  791. _itow (g_ulOnBatteryMinutes, szNum, 10);
  792. SetDlgItemText (hWnd, IDC_SHUTDOWNTIMEREDITBOX, (LPTSTR)szNum);
  793. // Set the initial state of the notification checkbox
  794. // and enable/disable associated controls
  795. CheckDlgButton (hWnd, IDC_NOTIFYCHECKBOX, (BOOL) g_ulNotifyEnable);
  796. OnNotificationCheckBox(hWnd);
  797. // Set the initial state of the shutdown timer checkbox
  798. // and enable/disable associated controls
  799. CheckDlgButton (hWnd, IDC_SHUTDOWNTIMERCHECKBOX, (BOOL) g_ulShutdownOnBattery);
  800. OnShutdownTimerCheckBox(hWnd);
  801. // Set the initial state of the run task checkbox
  802. // and enable/disable associated controls
  803. CheckDlgButton (hWnd, IDC_RUNTASKCHECKBOX, (BOOL) g_ulRunTaskEnable);
  804. OnRunTaskCheckBox(hWnd);
  805. // Display the task's program name
  806. if (GetTaskApplicationInfo(szTaskApplicationName, MAX_PATH))
  807. {
  808. SetDlgItemText (hWnd, IDC_TASKNAMETEXT, APCFileNameOnly(szTaskApplicationName));
  809. }
  810. // Initialize the power action combo box
  811. LoadString(g_hInstance, IDS_POWEROFF, (LPTSTR) szShutdown, sizeof(szShutdown)/sizeof(TCHAR));
  812. LoadString(g_hInstance, IDS_HIBERNATE, (LPTSTR) szHibernate, sizeof(szHibernate)/sizeof(TCHAR));
  813. SendDlgItemMessage( hWnd,
  814. IDC_POWERACTIONCOMBO,
  815. CB_ADDSTRING,
  816. 0,
  817. (LPARAM) szShutdown);
  818. //
  819. // Offer Hibernate as an option if the Hiberfile is present
  820. //
  821. if(g_SysPwrCapabilities.SystemS4 && g_SysPwrCapabilities.HiberFilePresent) {
  822. SendDlgItemMessage( hWnd,
  823. IDC_POWERACTIONCOMBO,
  824. CB_ADDSTRING,
  825. 0,
  826. (LPARAM) szHibernate );
  827. }
  828. SendDlgItemMessage( hWnd,
  829. IDC_POWERACTIONCOMBO,
  830. CB_SETCURSEL,
  831. g_ulCriticalPowerAction,0);
  832. // if Hibernate selected, disable the run task
  833. if( UPS_SHUTDOWN_HIBERNATE == g_ulCriticalPowerAction )
  834. {
  835. g_ulRunTaskEnable = BST_UNCHECKED;
  836. CheckDlgButton (hWnd, IDC_RUNTASKCHECKBOX, (BOOL) g_ulRunTaskEnable);
  837. OnRunTaskCheckBox(hWnd);
  838. EnableWindow( GetDlgItem( hWnd, IDC_RUNTASKCHECKBOX ), g_ulRunTaskEnable );
  839. }
  840. // Set the initial state of the turn off UPS checkbox
  841. // and enable/disable associated controls
  842. CheckDlgButton (hWnd, IDC_TURNOFFCHECKBOX , (BOOL) g_ulTurnOffUPS);
  843. OnTurnOffCheckBox(hWnd);
  844. // Finally, hide controls that aren't supported based on options key
  845. // ShowWindow(GetDlgItem( hWnd, IDC_WAITEDITBOX ), g_bPowerFailSignal ? SW_SHOW : SW_HIDE);
  846. // ShowWindow(GetDlgItem( hWnd, IDC_WAITSPIN ), g_bPowerFailSignal ? SW_SHOW : SW_HIDE);
  847. // ShowWindow(GetDlgItem( hWnd, IDC_WAITTEXT ), g_bPowerFailSignal ? SW_SHOW : SW_HIDE);
  848. // ShowWindow(GetDlgItem( hWnd, IDC_REPEATEDITBOX ), g_bPowerFailSignal ? SW_SHOW : SW_HIDE);
  849. // ShowWindow(GetDlgItem( hWnd, IDC_REPEATSPIN ), g_bPowerFailSignal ? SW_SHOW : SW_HIDE);
  850. // ShowWindow(GetDlgItem( hWnd, IDC_REPEATTEXT ), g_bPowerFailSignal ? SW_SHOW : SW_HIDE);
  851. ShowWindow(GetDlgItem(hWnd,IDC_LOWBATTERYSHUTDOWNTEXT), g_bLowBatterySignal ? SW_SHOW : SW_HIDE);
  852. ShowWindow(GetDlgItem(hWnd,IDC_TURNOFFCHECKBOX), g_bShutOffSignal ? SW_SHOW : SW_HIDE);
  853. return TRUE;
  854. }
  855. /*******************************************************************************
  856. *
  857. * OnClose
  858. *
  859. * DESCRIPTION: Handles WM_CLOSE message sent to UPSConfigDlgProc
  860. *
  861. * PARAMETERS:
  862. *
  863. *******************************************************************************/
  864. BOOL
  865. OnClose(
  866. IN HWND hWnd,
  867. IN WPARAM wParam,
  868. IN LPARAM lParam)
  869. {
  870. HWND taskHwnd = NULL;
  871. // if task scheduler window is still up, kill it
  872. taskHwnd = FindWindow( NULL, g_szTaskName);
  873. if (taskHwnd)
  874. {
  875. DestroyWindow(taskHwnd);
  876. }
  877. if (GetWindowLong(hWnd, DWLP_USER))
  878. CoUninitialize();
  879. EndDialog(hWnd, wParam);
  880. return TRUE;
  881. }
  882. /*******************************************************************************
  883. *
  884. * OnOK
  885. *
  886. * DESCRIPTION: Handles WM_COMMAND message sent to IDOK
  887. *
  888. * PARAMETERS:
  889. *
  890. *******************************************************************************/
  891. BOOL OnOK(
  892. IN HWND hWnd,
  893. IN WPARAM wParam,
  894. IN LPARAM lParam)
  895. {
  896. if (ValidateFields(hWnd))
  897. {
  898. SetRegistryValues();
  899. AddActiveDataState(CONFIG_DATA_CHANGE);
  900. EnableApplyButton();
  901. return OnClose(hWnd, wParam, lParam);
  902. }
  903. return FALSE;
  904. }
  905. /*******************************************************************************
  906. *
  907. * OnCommand
  908. *
  909. * DESCRIPTION: Handles WM_COMMAND messages sent to UPSConfigDlgProc
  910. *
  911. * PARAMETERS:
  912. *
  913. *******************************************************************************/
  914. BOOL
  915. OnCommand(
  916. IN HWND hWnd,
  917. IN WPARAM wParam,
  918. IN LPARAM lParam
  919. )
  920. {
  921. BOOL bRetVal;
  922. WORD idCtl = LOWORD(wParam);
  923. WORD wNotify = HIWORD(wParam);
  924. //
  925. // Assume we handle the command, the default switch will catch exceptions.
  926. //
  927. bRetVal = TRUE;
  928. switch (idCtl)
  929. {
  930. case IDC_NOTIFYCHECKBOX:
  931. bRetVal = OnNotificationCheckBox(hWnd);
  932. break;
  933. case IDC_SHUTDOWNTIMERCHECKBOX:
  934. bRetVal = OnShutdownTimerCheckBox(hWnd);
  935. break;
  936. case IDC_POWERACTIONCOMBO:
  937. bRetVal = OnPowerActionCombo(hWnd, wParam, lParam);
  938. break;
  939. case IDC_RUNTASKCHECKBOX:
  940. bRetVal = OnRunTaskCheckBox(hWnd);
  941. break;
  942. case IDC_CONFIGURETASKBUTTON:
  943. bRetVal = OnConfigureTaskButton(hWnd);
  944. break;
  945. case IDOK:
  946. bRetVal = OnOK(hWnd, wParam, lParam);
  947. break;
  948. case IDCANCEL: // escape key,cancel buttion
  949. bRetVal = OnClose(hWnd, wParam, lParam);
  950. break;
  951. default:
  952. bRetVal = FALSE; // unhandled command, return FALSE
  953. }
  954. return bRetVal;
  955. }
  956. /*******************************************************************************
  957. *
  958. * OnNotify
  959. *
  960. * DESCRIPTION: Handles WM_NOTIFY messages sent to UPSConfigDlgProc
  961. *
  962. * PARAMETERS:
  963. *
  964. *******************************************************************************/
  965. BOOL
  966. OnNotify(
  967. IN HWND hWnd,
  968. IN WPARAM wParam,
  969. IN LPARAM lParam
  970. )
  971. {
  972. int idCtl = (int) wParam;
  973. switch (idCtl) {
  974. case IDC_WAITSPIN:
  975. OnNotifyWaitSpin( lParam );
  976. break;
  977. case IDC_REPEATSPIN:
  978. OnNotifyRepeatSpin( lParam );
  979. break;
  980. case IDC_TIMERSPIN:
  981. OnNotifyTimerSpin( lParam );
  982. break;
  983. default:
  984. break;
  985. }
  986. return FALSE;
  987. }
  988. /*******************************************************************************
  989. *
  990. * UPSConfigDlgProc
  991. *
  992. * DESCRIPTION:
  993. *
  994. * PARAMETERS:
  995. *
  996. *******************************************************************************/
  997. INT_PTR CALLBACK UPSConfigDlgProc(
  998. HWND hWnd,
  999. UINT uMsg,
  1000. WPARAM wParam,
  1001. LPARAM lParam
  1002. )
  1003. {
  1004. BOOL bRet = TRUE;
  1005. switch (uMsg) {
  1006. case WM_INITDIALOG:
  1007. OnInitDialog(hWnd,wParam,lParam);
  1008. break;
  1009. case WM_COMMAND:
  1010. OnCommand(hWnd,wParam,lParam);
  1011. break;
  1012. case WM_HELP: // F1
  1013. WinHelp(((LPHELPINFO)lParam)->hItemHandle,
  1014. PWRMANHLP,
  1015. HELP_WM_HELP,
  1016. (ULONG_PTR)(LPTSTR)g_UPSConfigHelpIDs);
  1017. break;
  1018. case WM_CONTEXTMENU: // right mouse click
  1019. WinHelp((HWND)wParam,
  1020. PWRMANHLP,
  1021. HELP_CONTEXTMENU,
  1022. (ULONG_PTR)(LPTSTR)g_UPSConfigHelpIDs);
  1023. break;
  1024. case WM_CLOSE:
  1025. OnClose(hWnd,wParam,lParam);
  1026. break;
  1027. case WM_NOTIFY:
  1028. OnNotify(hWnd,wParam,lParam);
  1029. break;
  1030. default:
  1031. bRet = FALSE;
  1032. break;
  1033. } // switch (uMsg)
  1034. return bRet;
  1035. }