xref: /reactos/dll/cpl/powercfg/hibernate.c (revision 8a978a17)
1 /*
2  * PROJECT:         ReactOS Power Configuration Applet
3  * LICENSE:         GPL - See COPYING in the top level directory
4  * FILE:            dll/cpl/powercfg/hibernate.c
5  * PURPOSE:         hibernate tab of applet
6  * PROGRAMMERS:     Alexander Wurzinger (Lohnegrim at gmx dot net)
7  *                  Johannes Anderwald (johannes.anderwald@reactos.org)
8  *                  Martin Rottensteiner
9  *                  Dmitry Chapyshev (lentind@yandex.ru)
10  */
11 
12 #include "powercfg.h"
13 
14 static VOID
15 Hib_InitDialog(HWND hwndDlg)
16 {
17     SYSTEM_POWER_CAPABILITIES PowerCaps;
18     MEMORYSTATUSEX msex;
19     TCHAR szTemp[MAX_PATH];
20     LPTSTR lpRoot;
21     ULARGE_INTEGER FreeBytesAvailable, TotalNumberOfBytes, TotalNumberOfFreeBytes;
22     BOOLEAN bHibernate;
23 
24     if (GetPwrCapabilities(&PowerCaps))
25     {
26         CheckDlgButton(hwndDlg,
27                    IDC_HIBERNATEFILE,
28                    PowerCaps.HiberFilePresent ? BST_CHECKED : BST_UNCHECKED);
29 
30         msex.dwLength = sizeof(msex);
31         if (!GlobalMemoryStatusEx(&msex))
32         {
33             return; // FIXME
34         }
35 
36         if (GetWindowsDirectory(szTemp,MAX_PATH))
37             lpRoot = szTemp;
38         else
39             lpRoot = NULL;
40 
41         // Get available space and size of selected volume.
42         if (!GetDiskFreeSpaceEx(lpRoot, &FreeBytesAvailable, &TotalNumberOfBytes, &TotalNumberOfFreeBytes))
43             TotalNumberOfFreeBytes.QuadPart = 0;
44 
45         // Print the free available space into selected volume.
46         StrFormatByteSize(TotalNumberOfFreeBytes.QuadPart, szTemp, _countof(szTemp));
47         SetDlgItemText(hwndDlg, IDC_FREESPACE, szTemp);
48 
49         // Print the amount of space required for hibernation.
50         StrFormatByteSize(msex.ullTotalPhys, szTemp, _countof(szTemp));
51         SetDlgItemText(hwndDlg, IDC_SPACEFORHIBERNATEFILE, szTemp);
52 
53         if (TotalNumberOfFreeBytes.QuadPart < msex.ullTotalPhys && !PowerCaps.HiberFilePresent)
54         {
55             EnableWindow(GetDlgItem(hwndDlg, IDC_HIBERNATEFILE), FALSE);
56             ShowWindow(GetDlgItem(hwndDlg, IDC_TOLESSFREESPACE), TRUE);
57         }
58         else
59         {
60             ShowWindow(GetDlgItem(hwndDlg, IDC_TOLESSFREESPACE), FALSE);
61             EnableWindow(GetDlgItem(hwndDlg, IDC_HIBERNATEFILE), TRUE);
62         }
63 
64         bHibernate = PowerCaps.HiberFilePresent ? TRUE : FALSE;
65         if (CallNtPowerInformation(SystemReserveHiberFile, &bHibernate, sizeof(bHibernate), NULL, 0) != STATUS_SUCCESS)
66         {
67             EnableWindow(GetDlgItem(hwndDlg, IDC_HIBERNATEFILE), FALSE);
68         }
69     }
70 }
71 
72 INT_PTR
73 Hib_SaveData(HWND hwndDlg)
74 {
75     BOOLEAN bHibernate;
76 
77     bHibernate = (BOOLEAN)(IsDlgButtonChecked(hwndDlg, IDC_HIBERNATEFILE) == BST_CHECKED);
78 
79     if (CallNtPowerInformation(SystemReserveHiberFile, &bHibernate, sizeof(bHibernate), NULL, 0) == STATUS_SUCCESS)
80     {
81         Hib_InitDialog(hwndDlg);
82         return TRUE;
83     }
84 
85     return FALSE;
86 }
87 
88 /* Property page dialog callback */
89 INT_PTR CALLBACK
90 HibernateDlgProc(HWND hwndDlg,
91                  UINT uMsg,
92                  WPARAM wParam,
93                  LPARAM lParam)
94 {
95     switch (uMsg)
96     {
97         case WM_INITDIALOG:
98             Hib_InitDialog(hwndDlg);
99             return TRUE;
100 
101         case WM_COMMAND:
102             switch (LOWORD(wParam))
103             {
104                 case IDC_HIBERNATEFILE:
105                     if (HIWORD(wParam) == BN_CLICKED)
106                     {
107                         PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
108                     }
109                     break;
110             }
111             break;
112 
113         case WM_NOTIFY:
114             if (((LPNMHDR)lParam)->code == (UINT)PSN_APPLY)
115             {
116                 return Hib_SaveData(hwndDlg);
117             }
118             break;
119     }
120 
121     return FALSE;
122 }
123