[CONSOLE.DLL]
[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 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 = (COORD){80, 300};
101 pConInfo->ci.ConsoleSize = (COORD){80, 25 };
102 pConInfo->ci.CursorBlinkOn = TRUE;
103 pConInfo->ci.ForceCursorOff = FALSE;
104 pConInfo->ci.CursorSize = CSR_DEFAULT_CURSOR_SIZE;
105 pConInfo->ci.ScreenAttrib = DEFAULT_SCREEN_ATTRIB;
106 pConInfo->ci.PopupAttrib = DEFAULT_POPUP_ATTRIB;
107 pConInfo->ci.CodePage = 0;
108 pConInfo->ci.ConsoleTitle[0] = L'\0';
109
110 /* Adapted for holding GUI terminal information */
111 pConInfo->TerminalInfo.Size = sizeof(GUI_CONSOLE_INFO);
112 GuiInfo = pConInfo->TerminalInfo.TermInfo = (PGUI_CONSOLE_INFO)(pConInfo + 1);
113 wcsncpy(GuiInfo->FaceName, L"Fixedsys", LF_FACESIZE); // HACK: !!
114 // GuiInfo->FaceName[0] = L'\0';
115 GuiInfo->FontFamily = FF_DONTCARE;
116 GuiInfo->FontSize = 0;
117 GuiInfo->FontWeight = FW_DONTCARE;
118 GuiInfo->UseRasterFonts = TRUE;
119
120 GuiInfo->AutoPosition = TRUE;
121 GuiInfo->WindowOrigin = (POINT){0, 0};
122
123 memcpy(pConInfo->ci.Colors, s_Colors, sizeof(s_Colors));
124 }
125
126 INT_PTR
127 CALLBACK
128 ApplyProc(HWND hwndDlg,
129 UINT uMsg,
130 WPARAM wParam,
131 LPARAM lParam)
132 {
133 HWND hDlgCtrl;
134
135 UNREFERENCED_PARAMETER(lParam);
136
137 switch (uMsg)
138 {
139 case WM_INITDIALOG:
140 {
141 hDlgCtrl = GetDlgItem(hwndDlg, IDC_RADIO_APPLY_CURRENT);
142 SendMessage(hDlgCtrl, BM_SETCHECK, BST_CHECKED, 0);
143 return TRUE;
144 }
145 case WM_COMMAND:
146 {
147 if (LOWORD(wParam) == IDOK)
148 {
149 hDlgCtrl = GetDlgItem(hwndDlg, IDC_RADIO_APPLY_CURRENT);
150 if (SendMessage(hDlgCtrl, BM_GETCHECK, 0, 0) == BST_CHECKED)
151 EndDialog(hwndDlg, IDC_RADIO_APPLY_CURRENT);
152 else
153 EndDialog(hwndDlg, IDC_RADIO_APPLY_ALL);
154 }
155 else if (LOWORD(wParam) == IDCANCEL)
156 {
157 EndDialog(hwndDlg, IDCANCEL);
158 }
159 break;
160 }
161 default:
162 break;
163 }
164
165 return FALSE;
166 }
167
168 BOOL
169 ApplyConsoleInfo(HWND hwndDlg,
170 PCONSOLE_PROPS pConInfo)
171 {
172 BOOL SetParams = FALSE;
173 BOOL SaveParams = FALSE;
174
175 /*
176 * If we are setting the default parameters, just save them,
177 * otherwise display the save-confirmation dialog.
178 */
179 if (pConInfo->ShowDefaultParams)
180 {
181 SetParams = TRUE;
182 SaveParams = TRUE;
183 }
184 else
185 {
186 INT_PTR res = DialogBox(hApplet, MAKEINTRESOURCE(IDD_APPLYOPTIONS), hwndDlg, ApplyProc);
187
188 SetParams = (res != IDCANCEL);
189 SaveParams = (res == IDC_RADIO_APPLY_ALL);
190
191 if (SetParams == FALSE)
192 {
193 /* Don't destroy when user presses cancel */
194 SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE);
195 // return TRUE;
196 }
197 }
198
199 // if (res == IDC_RADIO_APPLY_ALL || res == IDC_RADIO_APPLY_CURRENT)
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 = CreateFileMapping(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 BOOL GuiTermInfo = FALSE;
265 PCONSOLE_PROPS pSharedInfo;
266 PCONSOLE_PROPS pConInfo;
267 WCHAR szTitle[MAX_PATH + 1];
268 PROPSHEETPAGE psp[4];
269 PROPSHEETHEADER psh;
270 INT i = 0;
271
272 UNREFERENCED_PARAMETER(uMsg);
273
274 /*
275 * CONSOLE.DLL shares information with CONSRV with wParam:
276 * wParam is a handle to a shared section holding a CONSOLE_PROPS struct.
277 *
278 * NOTE: lParam is not used.
279 */
280
281 /* Allocate a local buffer to hold console information */
282 pConInfo = AllocConsoleInfo();
283 if (!pConInfo) return 0;
284
285 /* Map the shared section */
286 pSharedInfo = MapViewOfFile(hSection, FILE_MAP_READ, 0, 0, 0);
287 if (pSharedInfo == NULL)
288 {
289 HeapFree(GetProcessHeap(), 0, pConInfo);
290 return 0;
291 }
292
293 // if (IsBadReadPtr((PVOID)pSharedInfo, sizeof(CONSOLE_PROPS)))
294 // {
295 // }
296
297 /* Find the console window and whether we must use default parameters */
298 pConInfo->hConsoleWindow = pSharedInfo->hConsoleWindow;
299 pConInfo->ShowDefaultParams = pSharedInfo->ShowDefaultParams;
300
301 /* Check that we are going to modify GUI terminal information */
302 GuiTermInfo = ( pSharedInfo->TerminalInfo.Size == sizeof(GUI_CONSOLE_INFO) &&
303 pSharedInfo->TerminalInfo.TermInfo != 0 );
304
305 if (pConInfo->ShowDefaultParams || !GuiTermInfo)
306 {
307 /* Use defaults */
308 InitConsoleDefaults(pConInfo);
309 }
310 else
311 {
312 /*
313 * Copy the shared data into our allocated buffer, and
314 * de-offsetize the address of terminal-specific information.
315 */
316 RtlCopyMemory(pConInfo, pSharedInfo, sizeof(CONSOLE_PROPS) + sizeof(GUI_CONSOLE_INFO));
317 pConInfo->TerminalInfo.TermInfo = (PVOID)((ULONG_PTR)pConInfo + (ULONG_PTR)pConInfo->TerminalInfo.TermInfo);
318 }
319
320 /* Close the section */
321 UnmapViewOfFile(pSharedInfo);
322 CloseHandle(hSection);
323
324 /* Initialize the property sheet structure */
325 ZeroMemory(&psh, sizeof(PROPSHEETHEADER));
326 psh.dwSize = sizeof(PROPSHEETHEADER);
327 psh.dwFlags = PSH_PROPSHEETPAGE | PSH_PROPTITLE | /* PSH_USEHICON */ PSH_USEICONID | PSH_NOAPPLYNOW;
328
329 if (pConInfo->ci.ConsoleTitle[0] != L'\0')
330 {
331 wcsncpy(szTitle, L"\"", sizeof(szTitle) / sizeof(szTitle[0]));
332 wcsncat(szTitle, pConInfo->ci.ConsoleTitle, sizeof(szTitle) / sizeof(szTitle[0]));
333 wcsncat(szTitle, L"\"", sizeof(szTitle) / sizeof(szTitle[0]));
334 }
335 else
336 {
337 wcscpy(szTitle, L"ReactOS Console");
338 }
339 psh.pszCaption = szTitle;
340
341 psh.hwndParent = pConInfo->hConsoleWindow;
342 psh.hInstance = hApplet;
343 // psh.hIcon = LoadIcon(hApplet, MAKEINTRESOURCE(IDC_CPLICON));
344 psh.pszIcon = MAKEINTRESOURCE(IDC_CPLICON);
345 psh.nPages = 4;
346 psh.nStartPage = 0;
347 psh.ppsp = psp;
348
349 InitPropSheetPage(&psp[i++], IDD_PROPPAGEOPTIONS, (DLGPROC) OptionsProc, (LPARAM)pConInfo);
350 InitPropSheetPage(&psp[i++], IDD_PROPPAGEFONT, (DLGPROC) FontProc, (LPARAM)pConInfo);
351 InitPropSheetPage(&psp[i++], IDD_PROPPAGELAYOUT, (DLGPROC) LayoutProc, (LPARAM)pConInfo);
352 InitPropSheetPage(&psp[i++], IDD_PROPPAGECOLORS, (DLGPROC) ColorsProc, (LPARAM)pConInfo);
353
354 return (PropertySheet(&psh) != -1);
355 }
356
357 /* Control Panel Callback */
358 LONG CALLBACK
359 CPlApplet(HWND hwndCPl,
360 UINT uMsg,
361 LPARAM lParam1,
362 LPARAM lParam2)
363 {
364 switch (uMsg)
365 {
366 case CPL_INIT:
367 return TRUE;
368
369 case CPL_EXIT:
370 // TODO: Free allocated memory
371 break;
372
373 case CPL_GETCOUNT:
374 return NUM_APPLETS;
375
376 case CPL_INQUIRE:
377 {
378 CPLINFO *CPlInfo = (CPLINFO*)lParam2;
379 CPlInfo->idIcon = Applets[0].idIcon;
380 CPlInfo->idName = Applets[0].idName;
381 CPlInfo->idInfo = Applets[0].idDescription;
382 break;
383 }
384
385 case CPL_DBLCLK:
386 InitApplet(hwndCPl, uMsg, lParam1, lParam2);
387 break;
388 }
389
390 return FALSE;
391 }
392
393
394 INT
395 WINAPI
396 DllMain(HINSTANCE hinstDLL,
397 DWORD dwReason,
398 LPVOID lpvReserved)
399 {
400 UNREFERENCED_PARAMETER(lpvReserved);
401
402 switch (dwReason)
403 {
404 case DLL_PROCESS_ATTACH:
405 case DLL_THREAD_ATTACH:
406 hApplet = hinstDLL;
407 break;
408 }
409
410 return TRUE;
411 }