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