xref: /reactos/dll/cpl/powercfg/powercfg.c (revision e5ebbc8a)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  * PROJECT:         ReactOS Power Configuration Applet
3c2c66affSColin Finck  * LICENSE:         GPL - See COPYING in the top level directory
4c2c66affSColin Finck  * FILE:            dll/cpl/powercfg/powercfg.c
5c2c66affSColin Finck  * PURPOSE:         initialization of applet
6c2c66affSColin Finck  * PROGRAMMERS:     Alexander Wurzinger (Lohnegrim at gmx dot net)
7c2c66affSColin Finck  *                  Johannes Anderwald (johannes.anderwald@reactos.org)
8c2c66affSColin Finck  *                  Martin Rottensteiner
9c2c66affSColin Finck  *                  Dmitry Chapyshev (lentind@yandex.ru)
10c2c66affSColin Finck  */
11c2c66affSColin Finck 
12c2c66affSColin Finck #include "powercfg.h"
13c2c66affSColin Finck 
14c2c66affSColin Finck #include <winreg.h>
15c2c66affSColin Finck #include <regstr.h>
16c2c66affSColin Finck 
17c2c66affSColin Finck #define NUM_APPLETS (1)
18c2c66affSColin Finck 
19c2c66affSColin Finck static LONG APIENTRY Applet1(HWND hwnd, UINT uMsg, LPARAM wParam, LPARAM lParam);
20c2c66affSColin Finck 
21c2c66affSColin Finck 
22c2c66affSColin Finck HINSTANCE hApplet = 0;
23c2c66affSColin Finck GLOBAL_POWER_POLICY gGPP;
24c2c66affSColin Finck TCHAR langSel[255];
25c2c66affSColin Finck 
26c2c66affSColin Finck /* Applets */
27c2c66affSColin Finck APPLET Applets[NUM_APPLETS] =
28c2c66affSColin Finck {
29c2c66affSColin Finck     {IDC_CPLICON_1, IDS_CPLNAME_1, IDS_CPLDESCRIPTION_1, Applet1}
30c2c66affSColin Finck };
31c2c66affSColin Finck 
32c2c66affSColin Finck static BOOL CALLBACK
PropSheetAddPage(HPROPSHEETPAGE hpage,LPARAM lParam)33c2c66affSColin Finck PropSheetAddPage(HPROPSHEETPAGE hpage, LPARAM lParam)
34c2c66affSColin Finck {
35c2c66affSColin Finck     PROPSHEETHEADER *ppsh = (PROPSHEETHEADER *)lParam;
36c2c66affSColin Finck     if (ppsh != NULL && ppsh->nPages < MAX_POWER_PAGES)
37c2c66affSColin Finck     {
38c2c66affSColin Finck         ppsh->phpage[ppsh->nPages++] = hpage;
39c2c66affSColin Finck         return TRUE;
40c2c66affSColin Finck     }
41c2c66affSColin Finck 
42c2c66affSColin Finck     return FALSE;
43c2c66affSColin Finck }
44c2c66affSColin Finck 
45c2c66affSColin Finck static BOOL
InitPropSheetPage(PROPSHEETHEADER * ppsh,WORD idDlg,DLGPROC DlgProc)46c2c66affSColin Finck InitPropSheetPage(PROPSHEETHEADER *ppsh, WORD idDlg, DLGPROC DlgProc)
47c2c66affSColin Finck {
48c2c66affSColin Finck     HPROPSHEETPAGE hPage;
49c2c66affSColin Finck     PROPSHEETPAGE psp;
50c2c66affSColin Finck 
51c2c66affSColin Finck     if (ppsh->nPages < MAX_POWER_PAGES)
52c2c66affSColin Finck     {
53c2c66affSColin Finck         ZeroMemory(&psp, sizeof(psp));
54c2c66affSColin Finck         psp.dwSize = sizeof(psp);
55c2c66affSColin Finck         psp.dwFlags = PSP_DEFAULT;
56c2c66affSColin Finck         psp.hInstance = hApplet;
57c2c66affSColin Finck         psp.pszTemplate = MAKEINTRESOURCE(idDlg);
58c2c66affSColin Finck         psp.pfnDlgProc = DlgProc;
59c2c66affSColin Finck 
60c2c66affSColin Finck         hPage = CreatePropertySheetPage(&psp);
61c2c66affSColin Finck         if (hPage != NULL)
62c2c66affSColin Finck         {
63c2c66affSColin Finck             return PropSheetAddPage(hPage, (LPARAM)ppsh);
64c2c66affSColin Finck         }
65c2c66affSColin Finck     }
66c2c66affSColin Finck 
67c2c66affSColin Finck     return FALSE;
68c2c66affSColin Finck }
69c2c66affSColin Finck 
70bd55105cSKatayama Hirofumi MZ static int CALLBACK
PropSheetProc(HWND hwndDlg,UINT uMsg,LPARAM lParam)71bd55105cSKatayama Hirofumi MZ PropSheetProc(HWND hwndDlg, UINT uMsg, LPARAM lParam)
72bd55105cSKatayama Hirofumi MZ {
73bd55105cSKatayama Hirofumi MZ     // NOTE: This callback is needed to set large icon correctly.
74bd55105cSKatayama Hirofumi MZ     HICON hIcon;
752872cfc3SEric Kohl 
76bd55105cSKatayama Hirofumi MZ     switch (uMsg)
77bd55105cSKatayama Hirofumi MZ     {
78bd55105cSKatayama Hirofumi MZ         case PSCB_INITIALIZED:
79bd55105cSKatayama Hirofumi MZ         {
80bd55105cSKatayama Hirofumi MZ             hIcon = LoadIconW(hApplet, MAKEINTRESOURCEW(IDC_CPLICON_1));
81bd55105cSKatayama Hirofumi MZ             SendMessageW(hwndDlg, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
82bd55105cSKatayama Hirofumi MZ             break;
83bd55105cSKatayama Hirofumi MZ         }
84bd55105cSKatayama Hirofumi MZ     }
85bd55105cSKatayama Hirofumi MZ     return 0;
86bd55105cSKatayama Hirofumi MZ }
87c2c66affSColin Finck 
88c2c66affSColin Finck /* First Applet */
89c2c66affSColin Finck static LONG APIENTRY
Applet1(HWND hwnd,UINT uMsg,LPARAM wParam,LPARAM lParam)90c2c66affSColin Finck Applet1(HWND hwnd, UINT uMsg, LPARAM wParam, LPARAM lParam)
91c2c66affSColin Finck {
92c2c66affSColin Finck     HPROPSHEETPAGE hpsp[MAX_POWER_PAGES];
93c2c66affSColin Finck     PROPSHEETHEADER psh;
94c2c66affSColin Finck     HPSXA hpsxa = NULL;
95c2c66affSColin Finck     SYSTEM_POWER_CAPABILITIES spc;
96c2c66affSColin Finck     LONG ret;
97c2c66affSColin Finck 
98c2c66affSColin Finck     UNREFERENCED_PARAMETER(uMsg);
99c2c66affSColin Finck     UNREFERENCED_PARAMETER(wParam);
100c2c66affSColin Finck     UNREFERENCED_PARAMETER(lParam);
101c2c66affSColin Finck 
102c2c66affSColin Finck     ZeroMemory(&psh, sizeof(PROPSHEETHEADER));
103c2c66affSColin Finck     psh.dwSize = sizeof(PROPSHEETHEADER);
104bd55105cSKatayama Hirofumi MZ     psh.dwFlags = PSH_PROPTITLE | PSH_USEICONID | PSH_USECALLBACK;
105c2c66affSColin Finck     psh.hwndParent = hwnd;
106c2c66affSColin Finck     psh.hInstance = hApplet;
107bd55105cSKatayama Hirofumi MZ     psh.pszIcon = MAKEINTRESOURCEW(IDC_CPLICON_1);
1080c324e35SThamatip Chitpong     psh.pszCaption = MAKEINTRESOURCEW(IDS_CPLNAME_1);
109c2c66affSColin Finck     psh.nPages = 0;
110c2c66affSColin Finck     psh.nStartPage = 0;
111c2c66affSColin Finck     psh.phpage = hpsp;
112bd55105cSKatayama Hirofumi MZ     psh.pfnCallback = PropSheetProc;
113c2c66affSColin Finck 
114c58a2163SEric Kohl     if (!GetPwrCapabilities(&spc))
115c2c66affSColin Finck     {
116c58a2163SEric Kohl         return GetLastError();
117c58a2163SEric Kohl     }
118c58a2163SEric Kohl 
119c2c66affSColin Finck     if (spc.SystemBatteriesPresent)
120c2c66affSColin Finck     {
121c58a2163SEric Kohl         InitPropSheetPage(&psh, IDD_POWERSCHEMESPAGE_ACDC, PowerSchemesDlgProc);
122bcbe0dd8SThomas Faber         InitPropSheetPage(&psh, IDD_PROPPAGEALARMS, AlarmsDlgProc);
1236977833eSEric Kohl 
1246977833eSEric Kohl         /* FIXME: Add battery page */
125c2c66affSColin Finck     }
126c58a2163SEric Kohl     else
127c58a2163SEric Kohl     {
128c58a2163SEric Kohl         InitPropSheetPage(&psh, IDD_POWERSCHEMESPAGE_AC, PowerSchemesDlgProc);
129c2c66affSColin Finck     }
130bcbe0dd8SThomas Faber     InitPropSheetPage(&psh, IDD_PROPPAGEADVANCED, AdvancedDlgProc);
131a65a2609SEric Kohl     if (spc.SystemS4)
132a65a2609SEric Kohl     {
133bcbe0dd8SThomas Faber         InitPropSheetPage(&psh, IDD_PROPPAGEHIBERNATE, HibernateDlgProc);
134a65a2609SEric Kohl     }
135c2c66affSColin Finck 
1366977833eSEric Kohl     /* FIXME: Add UPS page */
1376977833eSEric Kohl 
138c2c66affSColin Finck     /* Load additional pages provided by shell extensions */
139c2c66affSColin Finck     hpsxa = SHCreatePropSheetExtArray(HKEY_LOCAL_MACHINE, REGSTR_PATH_CONTROLSFOLDER TEXT("\\Power"), MAX_POWER_PAGES - psh.nPages);
140c2c66affSColin Finck     if (hpsxa != NULL)
141c2c66affSColin Finck         SHAddFromPropSheetExtArray(hpsxa, PropSheetAddPage, (LPARAM)&psh);
142c2c66affSColin Finck 
143c2c66affSColin Finck     ret = (LONG)(PropertySheet(&psh) != -1);
144c2c66affSColin Finck 
145c2c66affSColin Finck     if (hpsxa != NULL)
146c2c66affSColin Finck         SHDestroyPropSheetExtArray(hpsxa);
147c2c66affSColin Finck 
148c2c66affSColin Finck     return ret;
149c2c66affSColin Finck }
150c2c66affSColin Finck 
151c2c66affSColin Finck /* Control Panel Callback */
152c2c66affSColin Finck LONG CALLBACK
CPlApplet(HWND hwndCPl,UINT uMsg,LPARAM lParam1,LPARAM lParam2)153c2c66affSColin Finck CPlApplet(HWND hwndCPl,
154c2c66affSColin Finck           UINT uMsg,
155c2c66affSColin Finck           LPARAM lParam1,
156c2c66affSColin Finck           LPARAM lParam2)
157c2c66affSColin Finck {
158*e5ebbc8aSKatayama Hirofumi MZ     UINT i = (UINT)lParam1;
159c2c66affSColin Finck 
160c2c66affSColin Finck     switch (uMsg)
161c2c66affSColin Finck     {
162c2c66affSColin Finck         case CPL_INIT:
163c2c66affSColin Finck         {
164c2c66affSColin Finck             return TRUE;
165c2c66affSColin Finck         }
1662872cfc3SEric Kohl 
167c2c66affSColin Finck         case CPL_GETCOUNT:
168c2c66affSColin Finck         {
169c2c66affSColin Finck             return NUM_APPLETS;
170c2c66affSColin Finck         }
1712872cfc3SEric Kohl 
172c2c66affSColin Finck         case CPL_INQUIRE:
173*e5ebbc8aSKatayama Hirofumi MZ             if (i < NUM_APPLETS)
174c2c66affSColin Finck             {
175c2c66affSColin Finck                 CPLINFO *CPlInfo = (CPLINFO*)lParam2;
176c2c66affSColin Finck                 CPlInfo->lData = 0;
177c2c66affSColin Finck                 CPlInfo->idIcon = Applets[i].idIcon;
178c2c66affSColin Finck                 CPlInfo->idName = Applets[i].idName;
179c2c66affSColin Finck                 CPlInfo->idInfo = Applets[i].idDescription;
180c2c66affSColin Finck             }
181*e5ebbc8aSKatayama Hirofumi MZ             else
182*e5ebbc8aSKatayama Hirofumi MZ             {
183*e5ebbc8aSKatayama Hirofumi MZ                 return TRUE;
184*e5ebbc8aSKatayama Hirofumi MZ             }
185*e5ebbc8aSKatayama Hirofumi MZ             break;
1862872cfc3SEric Kohl 
187c2c66affSColin Finck         case CPL_DBLCLK:
188*e5ebbc8aSKatayama Hirofumi MZ             if (i < NUM_APPLETS)
189c2c66affSColin Finck                 Applets[i].AppletProc(hwndCPl, uMsg, lParam1, lParam2);
190*e5ebbc8aSKatayama Hirofumi MZ             else
191*e5ebbc8aSKatayama Hirofumi MZ                 return TRUE;
192c2c66affSColin Finck             break;
193c2c66affSColin Finck     }
1942872cfc3SEric Kohl 
195c2c66affSColin Finck     return FALSE;
196c2c66affSColin Finck }
197c2c66affSColin Finck 
198c2c66affSColin Finck 
199c2c66affSColin Finck BOOLEAN WINAPI
DllMain(HINSTANCE hinstDLL,DWORD dwReason,LPVOID lpvReserved)200c2c66affSColin Finck DllMain(HINSTANCE hinstDLL,
201c2c66affSColin Finck         DWORD dwReason,
202c2c66affSColin Finck         LPVOID lpvReserved)
203c2c66affSColin Finck {
204c2c66affSColin Finck     UNREFERENCED_PARAMETER(lpvReserved);
2052872cfc3SEric Kohl 
206c2c66affSColin Finck     switch (dwReason)
207c2c66affSColin Finck     {
208c2c66affSColin Finck         case DLL_PROCESS_ATTACH:
209c58a2163SEric Kohl             DisableThreadLibraryCalls(hinstDLL);
210c2c66affSColin Finck             hApplet = hinstDLL;
211c2c66affSColin Finck             break;
212c2c66affSColin Finck     }
2132872cfc3SEric Kohl 
214c2c66affSColin Finck     return TRUE;
215c2c66affSColin Finck }
216