2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Console Server DLL
4 * FILE: win32ss/user/winsrv/consrv/frontends/gui/guisettings.c
5 * PURPOSE: GUI Terminal Front-End Settings Management
6 * PROGRAMMERS: Johannes Anderwald
7 * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
10 /* INCLUDES *******************************************************************/
13 #include "include/conio.h"
14 #include "include/settings.h"
15 #include "guisettings.h"
21 VOID
GuiConsoleMoveWindow(PGUI_CONSOLE_DATA GuiData
);
23 /* FUNCTIONS ******************************************************************/
26 GuiConsoleReadUserSettings(IN OUT PGUI_CONSOLE_INFO TermInfo
,
27 IN LPCWSTR ConsoleTitle
,
30 /*****************************************************
31 * Adapted from ConSrvReadUserSettings in settings.c *
32 *****************************************************/
36 DWORD dwNumSubKeys
= 0;
39 WCHAR szValueName
[MAX_PATH
];
41 WCHAR szValue
[LF_FACESIZE
] = L
"\0";
45 if (!ConSrvOpenUserSettings(ProcessId
,
50 DPRINT("ConSrvOpenUserSettings failed\n");
54 if (RegQueryInfoKey(hKey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
55 &dwNumSubKeys
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
57 DPRINT("GuiConsoleReadUserSettings: RegQueryInfoKey failed\n");
62 DPRINT("GuiConsoleReadUserSettings entered dwNumSubKeys %d\n", dwNumSubKeys
);
64 for (dwIndex
= 0; dwIndex
< dwNumSubKeys
; dwIndex
++)
66 dwValue
= sizeof(Value
);
67 dwValueName
= MAX_PATH
; // sizeof(szValueName)/sizeof(szValueName[0])
69 if (RegEnumValueW(hKey
, dwIndex
, szValueName
, &dwValueName
, NULL
, &dwType
, (BYTE
*)&Value
, &dwValue
) != ERROR_SUCCESS
)
74 * Retry in case of string value
76 dwValue
= sizeof(szValue
);
77 dwValueName
= MAX_PATH
; // sizeof(szValueName)/sizeof(szValueName[0])
78 if (RegEnumValueW(hKey
, dwIndex
, szValueName
, &dwValueName
, NULL
, NULL
, (BYTE
*)szValue
, &dwValue
) != ERROR_SUCCESS
)
87 if (!wcscmp(szValueName
, L
"FaceName"))
89 SIZE_T Length
= min(wcslen(szValue
) + 1, LF_FACESIZE
); // wcsnlen
90 wcsncpy(TermInfo
->FaceName
, szValue
, LF_FACESIZE
);
91 TermInfo
->FaceName
[Length
] = L
'\0';
94 else if (!wcscmp(szValueName
, L
"FontFamily"))
96 TermInfo
->FontFamily
= Value
;
99 else if (!wcscmp(szValueName
, L
"FontSize"))
101 TermInfo
->FontSize
= Value
;
104 else if (!wcscmp(szValueName
, L
"FontWeight"))
106 TermInfo
->FontWeight
= Value
;
109 else if (!wcscmp(szValueName
, L
"FullScreen"))
111 TermInfo
->FullScreen
= Value
;
114 else if (!wcscmp(szValueName
, L
"WindowPosition"))
116 TermInfo
->AutoPosition
= FALSE
;
117 TermInfo
->WindowOrigin
.x
= LOWORD(Value
);
118 TermInfo
->WindowOrigin
.y
= HIWORD(Value
);
128 GuiConsoleWriteUserSettings(IN OUT PGUI_CONSOLE_INFO TermInfo
,
129 IN LPCWSTR ConsoleTitle
,
132 /******************************************************
133 * Adapted from ConSrvWriteUserSettings in settings.c *
134 ******************************************************/
136 BOOL GlobalSettings
= (ConsoleTitle
[0] == L
'\0');
140 #define SetConsoleSetting(SettingName, SettingType, SettingSize, Setting, DefaultValue) \
142 if (GlobalSettings || (!GlobalSettings && (*(Setting) != (DefaultValue)))) \
144 RegSetValueExW(hKey, (SettingName), 0, (SettingType), (PBYTE)(Setting), (SettingSize)); \
148 RegDeleteValue(hKey, (SettingName)); \
152 if (!ConSrvOpenUserSettings(ProcessId
,
160 SetConsoleSetting(L
"FaceName", REG_SZ
, (wcslen(TermInfo
->FaceName
) + 1) * sizeof(WCHAR
), TermInfo
->FaceName
, L
'\0'); // wcsnlen
161 SetConsoleSetting(L
"FontFamily", REG_DWORD
, sizeof(DWORD
), &TermInfo
->FontFamily
, FF_DONTCARE
);
162 SetConsoleSetting(L
"FontSize", REG_DWORD
, sizeof(DWORD
), &TermInfo
->FontSize
, 0);
163 SetConsoleSetting(L
"FontWeight", REG_DWORD
, sizeof(DWORD
), &TermInfo
->FontWeight
, FW_DONTCARE
);
165 Storage
= TermInfo
->FullScreen
;
166 SetConsoleSetting(L
"FullScreen", REG_DWORD
, sizeof(DWORD
), &Storage
, FALSE
);
168 if (TermInfo
->AutoPosition
== FALSE
)
170 Storage
= MAKELONG(TermInfo
->WindowOrigin
.x
, TermInfo
->WindowOrigin
.y
);
171 RegSetValueExW(hKey
, L
"WindowPosition", 0, REG_DWORD
, (PBYTE
)&Storage
, sizeof(DWORD
));
175 RegDeleteValue(hKey
, L
"WindowPosition");
183 GuiConsoleGetDefaultSettings(IN OUT PGUI_CONSOLE_INFO TermInfo
,
186 /*******************************************************
187 * Adapted from ConSrvGetDefaultSettings in settings.c *
188 *******************************************************/
190 if (TermInfo
== NULL
) return;
193 * 1. Load the default values
195 // wcsncpy(TermInfo->FaceName, L"DejaVu Sans Mono", LF_FACESIZE);
196 // TermInfo->FontSize = MAKELONG(12, 8); // 0x0008000C; // font is 8x12
197 // TermInfo->FontSize = MAKELONG(16, 16); // font is 16x16
198 // TermInfo->FontWeight = FW_NORMAL;
200 wcsncpy(TermInfo
->FaceName
, L
"Fixedsys", LF_FACESIZE
); // HACK: !!
201 // TermInfo->FaceName[0] = L'\0';
202 TermInfo
->FontFamily
= FF_DONTCARE
;
203 TermInfo
->FontSize
= 0;
204 TermInfo
->FontWeight
= FW_DONTCARE
;
205 TermInfo
->UseRasterFonts
= TRUE
;
207 TermInfo
->FullScreen
= FALSE
;
208 TermInfo
->ShowWindow
= SW_SHOWNORMAL
;
209 TermInfo
->AutoPosition
= TRUE
;
210 TermInfo
->WindowOrigin
.x
= 0;
211 TermInfo
->WindowOrigin
.y
= 0;
214 * 2. Overwrite them with the ones stored in HKCU\Console.
215 * If the HKCU\Console key doesn't exist, create it
216 * and store the default values inside.
218 if (!GuiConsoleReadUserSettings(TermInfo
, L
"", ProcessId
))
220 GuiConsoleWriteUserSettings(TermInfo
, L
"", ProcessId
);
225 GuiConsoleShowConsoleProperties(PGUI_CONSOLE_DATA GuiData
,
229 PCONSOLE Console
= GuiData
->Console
;
230 PCONSOLE_SCREEN_BUFFER ActiveBuffer
= GuiData
->ActiveBuffer
;
231 PCONSOLE_PROCESS_DATA ProcessData
;
232 HANDLE hSection
= NULL
, hClientSection
= NULL
;
233 LARGE_INTEGER SectionSize
;
236 PCONSOLE_PROPS pSharedInfo
= NULL
;
237 PGUI_CONSOLE_INFO GuiInfo
= NULL
;
239 DPRINT("GuiConsoleShowConsoleProperties entered\n");
242 * Create a memory section to share with the applet, and map it.
244 /* Holds data for console.dll + console info + terminal-specific info */
245 SectionSize
.QuadPart
= sizeof(CONSOLE_PROPS
) + sizeof(GUI_CONSOLE_INFO
);
246 Status
= NtCreateSection(&hSection
,
253 if (!NT_SUCCESS(Status
))
255 DPRINT1("Error: Impossible to create a shared section ; Status = %lu\n", Status
);
259 Status
= NtMapViewOfSection(hSection
,
261 (PVOID
*)&pSharedInfo
,
269 if (!NT_SUCCESS(Status
))
271 DPRINT1("Error: Impossible to map the shared section ; Status = %lu\n", Status
);
278 * Setup the shared console properties structure.
282 pSharedInfo
->hConsoleWindow
= GuiData
->hWindow
;
283 pSharedInfo
->ShowDefaultParams
= Defaults
;
286 * We fill-in the fields only if we display
287 * our properties, not the default ones.
291 /* Console information */
292 pSharedInfo
->ci
.HistoryBufferSize
= Console
->HistoryBufferSize
;
293 pSharedInfo
->ci
.NumberOfHistoryBuffers
= Console
->NumberOfHistoryBuffers
;
294 pSharedInfo
->ci
.HistoryNoDup
= Console
->HistoryNoDup
;
295 pSharedInfo
->ci
.QuickEdit
= Console
->QuickEdit
;
296 pSharedInfo
->ci
.InsertMode
= Console
->InsertMode
;
297 pSharedInfo
->ci
.InputBufferSize
= 0;
298 pSharedInfo
->ci
.ScreenBufferSize
= ActiveBuffer
->ScreenBufferSize
;
299 pSharedInfo
->ci
.ConsoleSize
= ActiveBuffer
->ViewSize
;
300 pSharedInfo
->ci
.CursorBlinkOn
;
301 pSharedInfo
->ci
.ForceCursorOff
;
302 pSharedInfo
->ci
.CursorSize
= ActiveBuffer
->CursorInfo
.dwSize
;
303 if (GetType(ActiveBuffer
) == TEXTMODE_BUFFER
)
305 PTEXTMODE_SCREEN_BUFFER Buffer
= (PTEXTMODE_SCREEN_BUFFER
)ActiveBuffer
;
307 pSharedInfo
->ci
.ScreenAttrib
= Buffer
->ScreenDefaultAttrib
;
308 pSharedInfo
->ci
.PopupAttrib
= Buffer
->PopupDefaultAttrib
;
310 else // if (GetType(ActiveBuffer) == GRAPHICS_BUFFER)
312 // PGRAPHICS_SCREEN_BUFFER Buffer = (PGRAPHICS_SCREEN_BUFFER)ActiveBuffer;
313 DPRINT1("GuiConsoleShowConsoleProperties - Graphics buffer\n");
315 // FIXME: Gather defaults from the registry ?
316 pSharedInfo
->ci
.ScreenAttrib
= DEFAULT_SCREEN_ATTRIB
;
317 pSharedInfo
->ci
.PopupAttrib
= DEFAULT_POPUP_ATTRIB
;
319 pSharedInfo
->ci
.CodePage
;
321 /* GUI Information */
322 pSharedInfo
->TerminalInfo
.Size
= sizeof(GUI_CONSOLE_INFO
);
323 GuiInfo
= pSharedInfo
->TerminalInfo
.TermInfo
= (PGUI_CONSOLE_INFO
)(pSharedInfo
+ 1);
324 Length
= min(wcslen(GuiData
->GuiInfo
.FaceName
) + 1, LF_FACESIZE
); // wcsnlen
325 wcsncpy(GuiInfo
->FaceName
, GuiData
->GuiInfo
.FaceName
, LF_FACESIZE
);
326 GuiInfo
->FaceName
[Length
] = L
'\0';
327 GuiInfo
->FontFamily
= GuiData
->GuiInfo
.FontFamily
;
328 GuiInfo
->FontSize
= GuiData
->GuiInfo
.FontSize
;
329 GuiInfo
->FontWeight
= GuiData
->GuiInfo
.FontWeight
;
330 GuiInfo
->UseRasterFonts
= GuiData
->GuiInfo
.UseRasterFonts
;
331 GuiInfo
->FullScreen
= GuiData
->GuiInfo
.FullScreen
;
332 /// GuiInfo->WindowPosition = GuiData->GuiInfo.WindowPosition;
333 GuiInfo
->AutoPosition
= GuiData
->GuiInfo
.AutoPosition
;
334 GuiInfo
->WindowOrigin
= GuiData
->GuiInfo
.WindowOrigin
;
336 pSharedInfo
->TerminalInfo
.TermInfo
= (PVOID
)((ULONG_PTR
)GuiInfo
- (ULONG_PTR
)pSharedInfo
);
339 memcpy(pSharedInfo
->ci
.Colors
, Console
->Colors
, sizeof(Console
->Colors
));
341 /* Title of the console, original one corresponding to the one set by the console leader */
342 Length
= min(sizeof(pSharedInfo
->ci
.ConsoleTitle
) / sizeof(pSharedInfo
->ci
.ConsoleTitle
[0]) - 1,
343 Console
->OriginalTitle
.Length
/ sizeof(WCHAR
));
344 wcsncpy(pSharedInfo
->ci
.ConsoleTitle
, Console
->OriginalTitle
.Buffer
, Length
);
349 // FIXME: Load the default parameters from the registry.
352 /* Null-terminate the title */
353 pSharedInfo
->ci
.ConsoleTitle
[Length
] = L
'\0';
357 NtUnmapViewOfSection(NtCurrentProcess(), pSharedInfo
);
359 /* Get the console leader process, our client */
360 ProcessData
= CONTAINING_RECORD(Console
->ProcessList
.Blink
,
361 CONSOLE_PROCESS_DATA
,
364 /* Duplicate the section handle for the client */
365 Status
= NtDuplicateObject(NtCurrentProcess(),
367 ProcessData
->Process
->ProcessHandle
,
369 0, 0, DUPLICATE_SAME_ACCESS
);
370 if (!NT_SUCCESS(Status
))
372 DPRINT1("Error: Impossible to duplicate section handle for client ; Status = %lu\n", Status
);
376 /* Start the properties dialog */
377 if (ProcessData
->PropDispatcher
)
381 HANDLE Thread
= NULL
;
385 Thread
= CreateRemoteThread(ProcessData
->Process
->ProcessHandle
, NULL
, 0,
386 ProcessData
->PropDispatcher
,
387 (PVOID
)hClientSection
, 0, NULL
);
390 DPRINT1("Failed thread creation (Error: 0x%x)\n", GetLastError());
394 DPRINT("ProcessData->PropDispatcher remote thread creation succeeded, ProcessId = %x, Process = 0x%p\n", ProcessData
->Process
->ClientId
.UniqueProcess
, ProcessData
->Process
);
395 /// WaitForSingleObject(Thread, INFINITE);
404 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
406 Status
= _SEH2_GetExceptionCode();
407 DPRINT1("GuiConsoleShowConsoleProperties - Caught an exception, Status = %08X\n", Status
);
413 /* We have finished, close the section handle */
419 GuiApplyUserSettings(PGUI_CONSOLE_DATA GuiData
,
420 HANDLE hClientSection
,
423 NTSTATUS Status
= STATUS_SUCCESS
;
424 PCONSOLE Console
= GuiData
->Console
;
425 PCONSOLE_PROCESS_DATA ProcessData
;
426 HANDLE hSection
= NULL
;
428 PCONSOLE_PROPS pConInfo
= NULL
;
429 PCONSOLE_INFO ConInfo
= NULL
;
430 PTERMINAL_INFO TermInfo
= NULL
;
431 PGUI_CONSOLE_INFO GuiInfo
= NULL
;
433 /* Get the console leader process, our client */
434 ProcessData
= CONTAINING_RECORD(Console
->ProcessList
.Blink
,
435 CONSOLE_PROCESS_DATA
,
438 /* Duplicate the section handle for ourselves */
439 Status
= NtDuplicateObject(ProcessData
->Process
->ProcessHandle
,
443 0, 0, DUPLICATE_SAME_ACCESS
);
444 if (!NT_SUCCESS(Status
))
446 DPRINT1("Error when mapping client handle, Status = %lu\n", Status
);
450 /* Get a view of the shared section */
451 Status
= NtMapViewOfSection(hSection
,
461 if (!NT_SUCCESS(Status
))
463 DPRINT1("Error when mapping view of file, Status = %lu\n", Status
);
470 /* Check that the section is well-sized */
471 if ( (ViewSize
< sizeof(CONSOLE_PROPS
)) ||
472 (pConInfo
->TerminalInfo
.Size
!= sizeof(GUI_CONSOLE_INFO
)) ||
473 (ViewSize
< sizeof(CONSOLE_PROPS
) + pConInfo
->TerminalInfo
.Size
) )
475 DPRINT1("Error: section bad-sized: sizeof(Section) < sizeof(CONSOLE_PROPS) + sizeof(Terminal_specific_info)\n");
476 Status
= STATUS_INVALID_VIEW_SIZE
;
477 _SEH2_YIELD(goto Quit
);
480 // TODO: Check that GuiData->hWindow == pConInfo->hConsoleWindow
482 /* Retrieve terminal informations */
483 ConInfo
= &pConInfo
->ci
;
484 TermInfo
= &pConInfo
->TerminalInfo
;
485 GuiInfo
= TermInfo
->TermInfo
= (PVOID
)((ULONG_PTR
)pConInfo
+ (ULONG_PTR
)TermInfo
->TermInfo
);
488 * If we don't set the default parameters,
489 * apply them, otherwise just save them.
491 if (pConInfo
->ShowDefaultParams
== FALSE
)
493 /* Set the console informations */
494 ConSrvApplyUserSettings(Console
, ConInfo
);
496 /* Set the terminal informations */
498 // memcpy(&GuiData->GuiInfo, GuiInfo, sizeof(GUI_CONSOLE_INFO));
500 /* Move the window to the user's values */
501 GuiData
->GuiInfo
.AutoPosition
= GuiInfo
->AutoPosition
;
502 GuiData
->GuiInfo
.WindowOrigin
= GuiInfo
->WindowOrigin
;
503 GuiConsoleMoveWindow(GuiData
);
505 InvalidateRect(GuiData
->hWindow
, NULL
, TRUE
);
508 * Apply full-screen mode.
510 GuiData
->GuiInfo
.FullScreen
= GuiInfo
->FullScreen
;
511 // TODO: Apply it really
515 * Save settings if needed
517 // FIXME: Do it in the console properties applet ??
520 DWORD ProcessId
= HandleToUlong(ProcessData
->Process
->ClientId
.UniqueProcess
);
521 ConSrvWriteUserSettings(ConInfo
, ProcessId
);
522 GuiConsoleWriteUserSettings(GuiInfo
, ConInfo
->ConsoleTitle
, ProcessId
);
525 Status
= STATUS_SUCCESS
;
527 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
529 Status
= _SEH2_GetExceptionCode();
530 DPRINT1("GuiApplyUserSettings - Caught an exception, Status = %08X\n", Status
);
535 /* Finally, close the section and return */
536 NtUnmapViewOfSection(NtCurrentProcess(), pConInfo
);