e0c4978ca7d35bdb50af84b6e185a188c90f6b65
[reactos.git] / dll / cpl / console / console.c
1 /*
2 * PROJECT: ReactOS Console Configuration DLL
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/console/console.c
5 * PURPOSE: Initialization
6 * PROGRAMMERS: Johannes Anderwald (johannes.anderwald@reactos.org)
7 * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
8 */
9
10 #include "console.h"
11
12 #define NDEBUG
13 #include <debug.h>
14
15 #define NUM_APPLETS 1
16
17 LONG APIENTRY InitApplet(HWND hwnd, UINT uMsg, LPARAM wParam, LPARAM lParam);
18 INT_PTR CALLBACK OptionsProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
19 INT_PTR CALLBACK FontProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
20 INT_PTR CALLBACK LayoutProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
21 INT_PTR CALLBACK ColorsProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
22
23 HINSTANCE hApplet = 0;
24
25 /* Applets */
26 APPLET Applets[NUM_APPLETS] =
27 {
28 {IDC_CPLICON, IDS_CPLNAME, IDS_CPLDESCRIPTION, InitApplet}
29 };
30
31 /*
32 * Default 16-color palette for foreground and background
33 * (corresponding flags in comments).
34 */
35 const COLORREF s_Colors[16] =
36 {
37 RGB(0, 0, 0), // (Black)
38 RGB(0, 0, 128), // BLUE
39 RGB(0, 128, 0), // GREEN
40 RGB(0, 128, 128), // BLUE | GREEN
41 RGB(128, 0, 0), // RED
42 RGB(128, 0, 128), // BLUE | RED
43 RGB(128, 128, 0), // GREEN | RED
44 RGB(192, 192, 192), // BLUE | GREEN | RED
45
46 RGB(128, 128, 128), // (Grey) INTENSITY
47 RGB(0, 0, 255), // BLUE | INTENSITY
48 RGB(0, 255, 0), // GREEN | INTENSITY
49 RGB(0, 255, 255), // BLUE | GREEN | INTENSITY
50 RGB(255, 0, 0), // RED | INTENSITY
51 RGB(255, 0, 255), // BLUE | RED | INTENSITY
52 RGB(255, 255, 0), // GREEN | RED | INTENSITY
53 RGB(255, 255, 255) // BLUE | GREEN | RED | INTENSITY
54 };
55 /* Default attributes */
56 #define DEFAULT_SCREEN_ATTRIB (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED)
57 #define DEFAULT_POPUP_ATTRIB (FOREGROUND_BLUE | FOREGROUND_RED | \
58 BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY)
59 /* Cursor size */
60 #define CSR_DEFAULT_CURSOR_SIZE 25
61
62 static VOID
63 InitPropSheetPage(PROPSHEETPAGEW *psp,
64 WORD idDlg,
65 DLGPROC DlgProc,
66 LPARAM lParam)
67 {
68 ZeroMemory(psp, sizeof(PROPSHEETPAGEW));
69 psp->dwSize = sizeof(PROPSHEETPAGEW);
70 psp->dwFlags = PSP_DEFAULT;
71 psp->hInstance = hApplet;
72 psp->pszTemplate = MAKEINTRESOURCEW(idDlg);
73 psp->pfnDlgProc = DlgProc;
74 psp->lParam = lParam;
75 }
76
77 PCONSOLE_PROPS
78 AllocConsoleInfo()
79 {
80 /* Adapted for holding GUI terminal information */
81 return HeapAlloc(GetProcessHeap(),
82 HEAP_ZERO_MEMORY,
83 sizeof(CONSOLE_PROPS) + sizeof(GUI_CONSOLE_INFO));
84 }
85
86 VOID
87 InitConsoleDefaults(PCONSOLE_PROPS pConInfo)
88 {
89 PGUI_CONSOLE_INFO GuiInfo = NULL;
90
91 /* FIXME: Get also the defaults from the registry */
92
93 /* Initialize the default properties */
94 pConInfo->ci.HistoryBufferSize = 50;
95 pConInfo->ci.NumberOfHistoryBuffers = 4;
96 pConInfo->ci.HistoryNoDup = FALSE;
97 pConInfo->ci.QuickEdit = FALSE;
98 pConInfo->ci.InsertMode = TRUE;
99 // pConInfo->ci.InputBufferSize;
100 pConInfo->ci.ScreenBufferSize.X = 80;
101 pConInfo->ci.ScreenBufferSize.Y = 300;
102 pConInfo->ci.ConsoleSize.X = 80;
103 pConInfo->ci.ConsoleSize.Y = 25;
104 pConInfo->ci.CursorBlinkOn = TRUE;
105 pConInfo->ci.ForceCursorOff = FALSE;
106 pConInfo->ci.CursorSize = CSR_DEFAULT_CURSOR_SIZE;
107 pConInfo->ci.ScreenAttrib = DEFAULT_SCREEN_ATTRIB;
108 pConInfo->ci.PopupAttrib = DEFAULT_POPUP_ATTRIB;
109 pConInfo->ci.CodePage = 0;
110 pConInfo->ci.ConsoleTitle[0] = L'\0';
111
112 /* Adapted for holding GUI terminal information */
113 pConInfo->TerminalInfo.Size = sizeof(GUI_CONSOLE_INFO);
114 GuiInfo = pConInfo->TerminalInfo.TermInfo = (PGUI_CONSOLE_INFO)(pConInfo + 1);
115 wcsncpy(GuiInfo->FaceName, L"VGA", LF_FACESIZE); // HACK: !!
116 // GuiInfo->FaceName[0] = L'\0';
117 GuiInfo->FontFamily = FF_DONTCARE;
118 GuiInfo->FontSize = 0;
119 GuiInfo->FontWeight = FW_DONTCARE;
120 GuiInfo->UseRasterFonts = TRUE;
121
122 GuiInfo->FullScreen = FALSE;
123 GuiInfo->ShowWindow = SW_SHOWNORMAL;
124 GuiInfo->AutoPosition = TRUE;
125 GuiInfo->WindowOrigin.x = 0;
126 GuiInfo->WindowOrigin.y = 0;
127
128 memcpy(pConInfo->ci.Colors, s_Colors, sizeof(s_Colors));
129 }
130
131 INT_PTR
132 CALLBACK
133 ApplyProc(HWND hwndDlg,
134 UINT uMsg,
135 WPARAM wParam,
136 LPARAM lParam)
137 {
138 UNREFERENCED_PARAMETER(lParam);
139
140 switch (uMsg)
141 {
142 case WM_INITDIALOG:
143 {
144 CheckDlgButton(hwndDlg, IDC_RADIO_APPLY_CURRENT, BST_CHECKED);
145 return TRUE;
146 }
147 case WM_COMMAND:
148 {
149 if (LOWORD(wParam) == IDOK)
150 {
151 if (IsDlgButtonChecked(hwndDlg, IDC_RADIO_APPLY_CURRENT) == BST_CHECKED)
152 EndDialog(hwndDlg, IDC_RADIO_APPLY_CURRENT);
153 else
154 EndDialog(hwndDlg, IDC_RADIO_APPLY_ALL);
155 }
156 else if (LOWORD(wParam) == IDCANCEL)
157 {
158 EndDialog(hwndDlg, IDCANCEL);
159 }
160 break;
161 }
162 default:
163 break;
164 }
165
166 return FALSE;
167 }
168
169 BOOL
170 ApplyConsoleInfo(HWND hwndDlg,
171 PCONSOLE_PROPS pConInfo)
172 {
173 BOOL SetParams = FALSE;
174 BOOL SaveParams = FALSE;
175
176 /*
177 * If we are setting the default parameters, just save them,
178 * otherwise display the save-confirmation dialog.
179 */
180 if (pConInfo->ShowDefaultParams)
181 {
182 SetParams = TRUE;
183 SaveParams = TRUE;
184 }
185 else
186 {
187 INT_PTR res = DialogBoxW(hApplet, MAKEINTRESOURCEW(IDD_APPLYOPTIONS), hwndDlg, ApplyProc);
188
189 SetParams = (res != IDCANCEL);
190 SaveParams = (res == IDC_RADIO_APPLY_ALL);
191
192 if (SetParams == FALSE)
193 {
194 /* Don't destroy when user presses cancel */
195 SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE);
196 // return TRUE;
197 }
198 }
199
200 if (SetParams)
201 {
202 HANDLE hSection;
203 PCONSOLE_PROPS pSharedInfo;
204
205 /*
206 * Create a memory section to share with the server, and map it.
207 */
208 /* Holds data for console.dll + console info + terminal-specific info */
209 hSection = CreateFileMappingW(INVALID_HANDLE_VALUE,
210 NULL,
211 PAGE_READWRITE,
212 0,
213 sizeof(CONSOLE_PROPS) + sizeof(GUI_CONSOLE_INFO),
214 NULL);
215 if (!hSection)
216 {
217 DPRINT1("Error when creating file mapping, error = %d\n", GetLastError());
218 return FALSE;
219 }
220
221 pSharedInfo = MapViewOfFile(hSection, FILE_MAP_ALL_ACCESS, 0, 0, 0);
222 if (!pSharedInfo)
223 {
224 DPRINT1("Error when mapping view of file, error = %d\n", GetLastError());
225 CloseHandle(hSection);
226 return FALSE;
227 }
228
229 /* We are applying the chosen configuration */
230 pConInfo->AppliedConfig = TRUE;
231
232 /*
233 * Copy the console information into the section and
234 * offsetize the address of terminal-specific information.
235 * Do not perform the offsetization in pConInfo as it is
236 * likely to be reused later on. Instead, do it in pSharedInfo
237 * after having copied all the data.
238 */
239 RtlCopyMemory(pSharedInfo, pConInfo, sizeof(CONSOLE_PROPS) + sizeof(GUI_CONSOLE_INFO));
240 pSharedInfo->TerminalInfo.TermInfo = (PVOID)((ULONG_PTR)pConInfo->TerminalInfo.TermInfo - (ULONG_PTR)pConInfo);
241
242 /* Unmap it */
243 UnmapViewOfFile(pSharedInfo);
244
245 /* Signal to the console server that it can apply the new configuration */
246 SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
247 SendMessage(pConInfo->hConsoleWindow,
248 PM_APPLY_CONSOLE_INFO,
249 (WPARAM)hSection,
250 (LPARAM)SaveParams);
251
252 /* Close the section and return */
253 CloseHandle(hSection);
254 }
255
256 return TRUE;
257 }
258
259 /* First Applet */
260 LONG APIENTRY
261 InitApplet(HWND hWnd, UINT uMsg, LPARAM wParam, LPARAM lParam)
262 {
263 HANDLE hSection = (HANDLE)wParam;
264 PCONSOLE_PROPS pSharedInfo = NULL;
265 PCONSOLE_PROPS pConInfo;
266 WCHAR szTitle[MAX_PATH + 1];
267 PROPSHEETPAGEW psp[4];
268 PROPSHEETHEADERW psh;
269 INT i = 0;
270
271 UNREFERENCED_PARAMETER(uMsg);
272
273 /*
274 * CONSOLE.DLL shares information with CONSRV with wParam:
275 * wParam is a handle to a shared section holding a CONSOLE_PROPS struct.
276 *
277 * NOTE: lParam is not used.
278 */
279
280 /* Allocate a local buffer to hold console information */
281 pConInfo = AllocConsoleInfo();
282 if (!pConInfo) return 0;
283
284 /* Check whether we were launched from the terminal... */
285 if (hSection != NULL)
286 {
287 /* ... yes, map the shared section */
288 pSharedInfo = MapViewOfFile(hSection, FILE_MAP_READ, 0, 0, 0);
289 if (pSharedInfo == NULL)
290 {
291 /* Cleanup */
292 HeapFree(GetProcessHeap(), 0, pConInfo);
293
294 /* Close the section */
295 CloseHandle(hSection);
296
297 return 0;
298 }
299
300 /* Find the console window and whether we set the default parameters */
301 pConInfo->hConsoleWindow = pSharedInfo->hConsoleWindow;
302 pConInfo->ShowDefaultParams = pSharedInfo->ShowDefaultParams;
303 }
304 else
305 {
306 /* ... no, we were launched as a CPL. Display the default settings. */
307 pConInfo->ShowDefaultParams = TRUE;
308 }
309
310 if (pConInfo->ShowDefaultParams)
311 {
312 /* Use defaults */
313 InitConsoleDefaults(pConInfo);
314 }
315 else if (hSection && pSharedInfo)
316 {
317 /*
318 * Copy the shared data into our allocated buffer, and
319 * de-offsetize the address of terminal-specific information.
320 */
321
322 /* Check that we are really going to modify GUI terminal information */
323 // FIXME: Do something clever, for example copy the UI-independent part
324 // and init the UI-dependent part to some default values...
325 ASSERT(pSharedInfo->TerminalInfo.Size == sizeof(GUI_CONSOLE_INFO));
326 ASSERT(pSharedInfo->TerminalInfo.TermInfo);
327
328 RtlCopyMemory(pConInfo, pSharedInfo, sizeof(CONSOLE_PROPS) + sizeof(GUI_CONSOLE_INFO));
329 pConInfo->TerminalInfo.TermInfo = (PVOID)((ULONG_PTR)pConInfo + (ULONG_PTR)pConInfo->TerminalInfo.TermInfo);
330 }
331
332 if (hSection && pSharedInfo)
333 {
334 /* Close the section */
335 UnmapViewOfFile(pSharedInfo);
336 CloseHandle(hSection);
337 }
338
339 /* Initialize the property sheet structure */
340 ZeroMemory(&psh, sizeof(PROPSHEETHEADERW));
341 psh.dwSize = sizeof(PROPSHEETHEADERW);
342 psh.dwFlags = PSH_PROPSHEETPAGE | PSH_PROPTITLE | /* PSH_USEHICON */ PSH_USEICONID | PSH_NOAPPLYNOW;
343
344 if (pConInfo->ci.ConsoleTitle[0] != L'\0')
345 {
346 wcsncpy(szTitle, L"\"", MAX_PATH);
347 wcsncat(szTitle, pConInfo->ci.ConsoleTitle, MAX_PATH - wcslen(szTitle));
348 wcsncat(szTitle, L"\"", MAX_PATH - wcslen(szTitle));
349 }
350 else
351 {
352 wcscpy(szTitle, L"ReactOS Console");
353 }
354 psh.pszCaption = szTitle;
355
356 psh.hwndParent = pConInfo->hConsoleWindow;
357 psh.hInstance = hApplet;
358 // psh.hIcon = LoadIcon(hApplet, MAKEINTRESOURCEW(IDC_CPLICON));
359 psh.pszIcon = MAKEINTRESOURCEW(IDC_CPLICON);
360 psh.nPages = 4;
361 psh.nStartPage = 0;
362 psh.ppsp = psp;
363
364 InitPropSheetPage(&psp[i++], IDD_PROPPAGEOPTIONS, (DLGPROC) OptionsProc, (LPARAM)pConInfo);
365 InitPropSheetPage(&psp[i++], IDD_PROPPAGEFONT , (DLGPROC) FontProc , (LPARAM)pConInfo);
366 InitPropSheetPage(&psp[i++], IDD_PROPPAGELAYOUT , (DLGPROC) LayoutProc , (LPARAM)pConInfo);
367 InitPropSheetPage(&psp[i++], IDD_PROPPAGECOLORS , (DLGPROC) ColorsProc , (LPARAM)pConInfo);
368
369 return (PropertySheetW(&psh) != -1);
370 }
371
372 /* Control Panel Callback */
373 LONG CALLBACK
374 CPlApplet(HWND hwndCPl,
375 UINT uMsg,
376 LPARAM lParam1,
377 LPARAM lParam2)
378 {
379 switch (uMsg)
380 {
381 case CPL_INIT:
382 return TRUE;
383
384 case CPL_EXIT:
385 // TODO: Free allocated memory
386 break;
387
388 case CPL_GETCOUNT:
389 return NUM_APPLETS;
390
391 case CPL_INQUIRE:
392 {
393 CPLINFO *CPlInfo = (CPLINFO*)lParam2;
394 CPlInfo->idIcon = Applets[0].idIcon;
395 CPlInfo->idName = Applets[0].idName;
396 CPlInfo->idInfo = Applets[0].idDescription;
397 break;
398 }
399
400 case CPL_DBLCLK:
401 InitApplet(hwndCPl, uMsg, lParam1, lParam2);
402 break;
403 }
404
405 return FALSE;
406 }
407
408
409 INT
410 WINAPI
411 DllMain(HINSTANCE hinstDLL,
412 DWORD dwReason,
413 LPVOID lpvReserved)
414 {
415 UNREFERENCED_PARAMETER(lpvReserved);
416
417 switch (dwReason)
418 {
419 case DLL_PROCESS_ATTACH:
420 case DLL_THREAD_ATTACH:
421 hApplet = hinstDLL;
422 break;
423 }
424
425 return TRUE;
426 }