2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Console Server DLL
4 * FILE: win32ss/user/winsrv/consrv/frontends/gui/guiterm.c
5 * PURPOSE: GUI Terminal Front-End
6 * PROGRAMMERS: Gé van Geldorp
9 * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
12 /* INCLUDES *******************************************************************/
22 // HACK!! Remove it when the hack in GuiWriteStream is fixed
23 #define CONGUI_UPDATE_TIME 0
24 #define CONGUI_UPDATE_TIMER 1
26 #define PM_CREATE_CONSOLE (WM_APP + 1)
27 #define PM_DESTROY_CONSOLE (WM_APP + 2)
30 /* GLOBALS ********************************************************************/
32 typedef struct _GUI_INIT_INFO
34 HANDLE GuiThreadStartupEvent
;
35 ULONG_PTR InputThreadId
;
40 BOOLEAN IsWindowVisible
;
41 GUI_CONSOLE_INFO TermInfo
;
42 } GUI_INIT_INFO
, *PGUI_INIT_INFO
;
44 static BOOL ConsInitialized
= FALSE
;
46 extern HICON ghDefaultIcon
;
47 extern HICON ghDefaultIconSm
;
48 extern HCURSOR ghDefaultCursor
;
51 SetConWndConsoleLeaderCID(IN PGUI_CONSOLE_DATA GuiData
);
53 RegisterConWndClass(IN HINSTANCE hInstance
);
55 UnRegisterConWndClass(HINSTANCE hInstance
);
57 /* FUNCTIONS ******************************************************************/
60 GetScreenBufferSizeUnits(IN PCONSOLE_SCREEN_BUFFER Buffer
,
61 IN PGUI_CONSOLE_DATA GuiData
,
65 if (Buffer
== NULL
|| GuiData
== NULL
||
66 WidthUnit
== NULL
|| HeightUnit
== NULL
)
71 if (GetType(Buffer
) == TEXTMODE_BUFFER
)
73 *WidthUnit
= GuiData
->CharWidth
;
74 *HeightUnit
= GuiData
->CharHeight
;
76 else /* if (GetType(Buffer) == GRAPHICS_BUFFER) */
84 GuiConsoleMoveWindow(PGUI_CONSOLE_DATA GuiData
)
86 /* Move the window if needed (not positioned by the system) */
87 if (!GuiData
->GuiInfo
.AutoPosition
)
89 SetWindowPos(GuiData
->hWindow
,
91 GuiData
->GuiInfo
.WindowOrigin
.x
,
92 GuiData
->GuiInfo
.WindowOrigin
.y
,
94 SWP_NOZORDER
| SWP_NOSIZE
| SWP_NOACTIVATE
);
99 SmallRectToRect(PGUI_CONSOLE_DATA GuiData
, PRECT Rect
, PSMALL_RECT SmallRect
)
101 PCONSOLE_SCREEN_BUFFER Buffer
= GuiData
->ActiveBuffer
;
102 UINT WidthUnit
, HeightUnit
;
104 GetScreenBufferSizeUnits(Buffer
, GuiData
, &WidthUnit
, &HeightUnit
);
106 Rect
->left
= (SmallRect
->Left
- Buffer
->ViewOrigin
.X
) * WidthUnit
;
107 Rect
->top
= (SmallRect
->Top
- Buffer
->ViewOrigin
.Y
) * HeightUnit
;
108 Rect
->right
= (SmallRect
->Right
+ 1 - Buffer
->ViewOrigin
.X
) * WidthUnit
;
109 Rect
->bottom
= (SmallRect
->Bottom
+ 1 - Buffer
->ViewOrigin
.Y
) * HeightUnit
;
113 DrawRegion(PGUI_CONSOLE_DATA GuiData
,
118 SmallRectToRect(GuiData
, &RegionRect
, Region
);
119 /* Do not erase the background: it speeds up redrawing and reduce flickering */
120 InvalidateRect(GuiData
->hWindow
, &RegionRect
, FALSE
);
121 /**UpdateWindow(GuiData->hWindow);**/
125 InvalidateCell(PGUI_CONSOLE_DATA GuiData
,
128 SMALL_RECT CellRect
= { x
, y
, x
, y
};
129 DrawRegion(GuiData
, &CellRect
);
133 /******************************************************************************
134 * GUI Terminal Initialization *
135 ******************************************************************************/
138 SwitchFullScreen(PGUI_CONSOLE_DATA GuiData
, BOOL FullScreen
);
140 CreateSysMenu(HWND hWnd
);
143 GuiConsoleInputThread(PVOID Param
)
146 PCSR_THREAD pcsrt
= NULL
;
147 PGUI_INIT_INFO GuiInitInfo
= (PGUI_INIT_INFO
)Param
;
148 DESKTOP_CONSOLE_THREAD DesktopConsoleThreadInfo
;
149 ULONG_PTR InputThreadId
= HandleToUlong(NtCurrentTeb()->ClientId
.UniqueThread
);
150 HANDLE hThread
= NULL
;
152 LONG WindowCount
= 0;
156 * This thread dispatches all the console notifications to the
157 * notification window. It is common for all the console windows
158 * in a given desktop in a window station.
161 /* Assign this console input thread to this desktop */
162 DesktopConsoleThreadInfo
.DesktopHandle
= GuiInitInfo
->Desktop
; // Duplicated desktop handle
163 DesktopConsoleThreadInfo
.ThreadId
= InputThreadId
;
164 Status
= NtUserConsoleControl(ConsoleCtrlDesktopConsoleThread
,
165 &DesktopConsoleThreadInfo
,
166 sizeof(DesktopConsoleThreadInfo
));
167 if (!NT_SUCCESS(Status
)) goto Quit
;
169 /* Connect this CSR thread to the USER subsystem */
171 PCSR_THREAD CurrThread
= CsrGetClientThread();
173 DPRINT1("CsrConnectToUser being called; [0x%x, 0x%x]...\n",
174 CurrThread
->ClientId
.UniqueProcess
, CurrThread
->ClientId
.UniqueThread
);
176 pcsrt
= CsrConnectToUser();
177 if (pcsrt
== NULL
) goto Quit
;
178 hThread
= pcsrt
->ThreadHandle
;
180 DPRINT1("CsrConnectToUser was successfully called; [0x%x, 0x%x] -- hThread = 0x%p, pcsrt->Process = 0x%p; pcsrt->ThreadHandle = 0x%p from [0x%x, 0x%x]\n",
181 CurrThread
->ClientId
.UniqueProcess
, CurrThread
->ClientId
.UniqueThread
,
182 hThread
, pcsrt
->Process
, pcsrt
->ThreadHandle
,
183 pcsrt
->ClientId
.UniqueProcess
, pcsrt
->ClientId
.UniqueThread
);
186 /* Assign the desktop to this thread */
187 if (!SetThreadDesktop(DesktopConsoleThreadInfo
.DesktopHandle
)) goto Quit
;
189 /* The thread has been initialized, set the event */
190 NtSetEvent(GuiInitInfo
->GuiThreadStartupEvent
, NULL
);
191 Status
= STATUS_SUCCESS
;
193 while (GetMessageW(&msg
, NULL
, 0, 0))
197 case PM_CREATE_CONSOLE
:
199 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
)msg
.lParam
;
200 PCONSRV_CONSOLE Console
= GuiData
->Console
;
204 DPRINT("PM_CREATE_CONSOLE -- creating window\n");
206 NewWindow
= CreateWindowExW(WS_EX_CLIENTEDGE
,
208 Console
->Title
.Buffer
,
209 WS_OVERLAPPEDWINDOW
| WS_HSCROLL
| WS_VSCROLL
,
214 GuiData
->IsWindowVisible
? HWND_DESKTOP
: HWND_MESSAGE
,
218 if (NewWindow
== NULL
)
220 DPRINT1("Failed to create a new console window\n");
224 ASSERT(NewWindow
== GuiData
->hWindow
);
226 InterlockedIncrement(&WindowCount
);
229 // FIXME: TODO: Move everything there into conwnd.c!OnNcCreate()
232 /* Retrieve our real position */
233 // See conwnd.c!OnMove()
234 GetWindowRect(GuiData
->hWindow
, &rcWnd
);
235 GuiData
->GuiInfo
.WindowOrigin
.x
= rcWnd
.left
;
236 GuiData
->GuiInfo
.WindowOrigin
.y
= rcWnd
.top
;
238 if (GuiData
->IsWindowVisible
)
240 /* Move and resize the window to the user's values */
241 /* CAN WE DEADLOCK ?? */
242 GuiConsoleMoveWindow(GuiData
); // FIXME: This MUST be done via the CreateWindowExW call.
243 SendMessageW(GuiData
->hWindow
, PM_RESIZE_TERMINAL
, 0, 0);
246 // FIXME: HACK: Potential HACK for CORE-8129; see revision 63595.
247 CreateSysMenu(GuiData
->hWindow
);
249 if (GuiData
->IsWindowVisible
)
251 /* Switch to full-screen mode if necessary */
252 // FIXME: Move elsewhere, it cause misdrawings of the window.
253 if (GuiData
->GuiInfo
.FullScreen
) SwitchFullScreen(GuiData
, TRUE
);
255 DPRINT("PM_CREATE_CONSOLE -- showing window\n");
256 // ShowWindow(NewWindow, (int)GuiData->GuiInfo.ShowWindow);
257 ShowWindowAsync(NewWindow
, (int)GuiData
->GuiInfo
.ShowWindow
);
258 DPRINT("Window showed\n");
262 DPRINT("PM_CREATE_CONSOLE -- hidden window\n");
263 ShowWindowAsync(NewWindow
, SW_HIDE
);
269 case PM_DESTROY_CONSOLE
:
271 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
)msg
.lParam
;
274 /* Exit the full screen mode if it was already set */
275 // LeaveFullScreen(GuiData);
278 * Window creation is done using a PostMessage(), so it's possible
279 * that the window that we want to destroy doesn't exist yet.
280 * So first empty the message queue.
283 while (PeekMessageW(&TempMsg, NULL, 0, 0, PM_REMOVE))
285 TranslateMessage(&TempMsg);
286 DispatchMessageW(&TempMsg);
288 while (PeekMessageW(&TempMsg
, NULL
, 0, 0, PM_REMOVE
)) ;
290 if (GuiData
->hWindow
== NULL
) continue;
292 DestroyWindow(GuiData
->hWindow
);
294 NtSetEvent(GuiData
->hGuiTermEvent
, NULL
);
296 if (InterlockedDecrement(&WindowCount
) == 0)
298 DPRINT("CONSRV: Going to quit the Input Thread 0x%p\n", InputThreadId
);
306 TranslateMessage(&msg
);
307 DispatchMessageW(&msg
);
311 DPRINT("CONSRV: Quit the Input Thread 0x%p, Status = 0x%08lx\n", InputThreadId
, Status
);
313 /* Remove this console input thread from this desktop */
314 // DesktopConsoleThreadInfo.DesktopHandle;
315 DesktopConsoleThreadInfo
.ThreadId
= 0;
316 NtUserConsoleControl(ConsoleCtrlDesktopConsoleThread
,
317 &DesktopConsoleThreadInfo
,
318 sizeof(DesktopConsoleThreadInfo
));
320 /* Close the duplicated desktop handle */
321 CloseDesktop(DesktopConsoleThreadInfo
.DesktopHandle
); // NtUserCloseDesktop
323 /* Cleanup CSR thread */
326 PCSR_THREAD CurrThread
= CsrGetClientThread();
328 DPRINT1("CsrDereferenceThread being called; [0x%x, 0x%x] -- hThread = 0x%p, pcsrt->Process = 0x%p; pcsrt->ThreadHandle = 0x%p from [0x%x, 0x%x]\n",
329 CurrThread
->ClientId
.UniqueProcess
, CurrThread
->ClientId
.UniqueThread
,
330 hThread
, pcsrt
->Process
, pcsrt
->ThreadHandle
,
331 pcsrt
->ClientId
.UniqueProcess
, pcsrt
->ClientId
.UniqueThread
);
333 if (hThread
!= pcsrt
->ThreadHandle
)
334 DPRINT1("WARNING!! hThread != pcsrt->ThreadHandle, you may expect crashes soon!!\n");
336 CsrDereferenceThread(pcsrt
);
339 /* Exit the thread */
340 RtlExitUserThread(Status
);
344 // FIXME: Maybe return a NTSTATUS
346 GuiInit(IN PCONSOLE_INIT_INFO ConsoleInitInfo
,
347 IN HANDLE ConsoleLeaderProcessHandle
,
348 IN OUT PGUI_INIT_INFO GuiInitInfo
)
351 UNICODE_STRING DesktopPath
;
352 DESKTOP_CONSOLE_THREAD DesktopConsoleThreadInfo
;
361 * Initialize and register the console window class, if needed.
363 if (!ConsInitialized
)
365 if (!RegisterConWndClass(ConSrvDllInstance
)) return FALSE
;
366 ConsInitialized
= TRUE
;
370 * Set-up the console input thread. We have
371 * one console input thread per desktop.
374 if (!CsrImpersonateClient(NULL
))
375 // return STATUS_BAD_IMPERSONATION_LEVEL;
378 if (ConsoleInitInfo
->DesktopLength
)
380 DesktopPath
.MaximumLength
= ConsoleInitInfo
->DesktopLength
;
381 DesktopPath
.Length
= DesktopPath
.MaximumLength
- sizeof(UNICODE_NULL
);
382 DesktopPath
.Buffer
= ConsoleInitInfo
->Desktop
;
386 RtlInitUnicodeString(&DesktopPath
, L
"Default");
389 hDesk
= NtUserResolveDesktop(ConsoleLeaderProcessHandle
,
393 DPRINT("NtUserResolveDesktop(DesktopPath = '%wZ') returned hDesk = 0x%p; hWinSta = 0x%p\n",
394 &DesktopPath
, hDesk
, hWinSta
);
398 if (hDesk
== NULL
) return FALSE
;
401 * We need to see whether we need to create a
402 * new console input thread for this desktop.
404 DesktopConsoleThreadInfo
.DesktopHandle
= hDesk
;
405 DesktopConsoleThreadInfo
.ThreadId
= (ULONG_PTR
)INVALID_HANDLE_VALUE
; // Special value to say we just want to retrieve the thread ID.
406 NtUserConsoleControl(ConsoleCtrlDesktopConsoleThread
,
407 &DesktopConsoleThreadInfo
,
408 sizeof(DesktopConsoleThreadInfo
));
409 DPRINT("NtUserConsoleControl returned ThreadId = 0x%p\n", DesktopConsoleThreadInfo
.ThreadId
);
412 * Save the opened window station and desktop handles in the initialization
413 * structure. They will be used later on, and released, by the GUI frontend.
415 GuiInitInfo
->WinSta
= hWinSta
;
416 GuiInitInfo
->Desktop
= hDesk
;
418 /* Here GuiInitInfo contains original handles */
420 /* If we already have a console input thread on this desktop... */
421 if (DesktopConsoleThreadInfo
.ThreadId
!= 0)
423 /* ... just use it... */
424 DPRINT("Using input thread InputThreadId = 0x%p\n", DesktopConsoleThreadInfo
.ThreadId
);
425 GuiInitInfo
->InputThreadId
= DesktopConsoleThreadInfo
.ThreadId
;
429 /* ... otherwise create a new one. */
431 /* Initialize a startup event for the thread to signal it */
432 Status
= NtCreateEvent(&GuiInitInfo
->GuiThreadStartupEvent
, EVENT_ALL_ACCESS
,
433 NULL
, SynchronizationEvent
, FALSE
);
434 if (!NT_SUCCESS(Status
))
441 * Duplicate the desktop handle for the console input thread internal needs.
442 * If it happens to need also a window station handle in the future, then
443 * it is there that you also need to duplicate the window station handle!
445 * Note also that we are going to temporarily overwrite the stored handles
446 * in GuiInitInfo because it happens that we use also this structure to give
447 * the duplicated handles to the input thread that is going to initialize.
448 * After the input thread finishes its initialization, we restore the handles
449 * in GuiInitInfo to their old values.
451 Status
= NtDuplicateObject(NtCurrentProcess(),
454 (PHANDLE
)&GuiInitInfo
->Desktop
,
455 0, 0, DUPLICATE_SAME_ACCESS
);
456 if (!NT_SUCCESS(Status
))
462 /* Here GuiInitInfo contains duplicated handles */
464 Status
= RtlCreateUserThread(NtCurrentProcess(),
466 TRUE
, // Start the thread in suspended state
470 (PVOID
)GuiConsoleInputThread
,
474 if (NT_SUCCESS(Status
))
476 /* Add it as a static server thread and resume it */
477 CsrAddStaticServerThread(hInputThread
, &ClientId
, 0);
478 Status
= NtResumeThread(hInputThread
, NULL
);
480 DPRINT("Thread creation hInputThread = 0x%p, InputThreadId = 0x%p, Status = 0x%08lx\n",
481 hInputThread
, ClientId
.UniqueThread
, Status
);
483 if (!NT_SUCCESS(Status
) || hInputThread
== NULL
)
485 /* Close the thread's handle */
486 if (hInputThread
) NtClose(hInputThread
);
488 /* We need to close here the duplicated desktop handle */
489 CloseDesktop(GuiInitInfo
->Desktop
); // NtUserCloseDesktop
491 /* Close the startup event and bail out */
492 NtClose(GuiInitInfo
->GuiThreadStartupEvent
);
494 DPRINT1("CONSRV: Failed to create graphics console thread.\n");
499 /* No need to close hInputThread, this is done by CSR automatically */
501 /* Wait for the thread to finish its initialization, and close the startup event */
502 NtWaitForSingleObject(GuiInitInfo
->GuiThreadStartupEvent
, FALSE
, NULL
);
503 NtClose(GuiInitInfo
->GuiThreadStartupEvent
);
506 * Save the input thread ID for later use, and restore the original handles.
507 * The copies are held by the console input thread.
509 GuiInitInfo
->InputThreadId
= (ULONG_PTR
)ClientId
.UniqueThread
;
510 GuiInitInfo
->WinSta
= hWinSta
;
511 GuiInitInfo
->Desktop
= hDesk
;
513 /* Here GuiInitInfo contains again original handles */
519 * Close the original handles. Do not use the copies in GuiInitInfo
520 * because we may have failed in the middle of the duplicate operation
521 * and the handles stored in GuiInitInfo may have changed.
523 CloseDesktop(hDesk
); // NtUserCloseDesktop
524 CloseWindowStation(hWinSta
); // NtUserCloseWindowStation
531 /******************************************************************************
532 * GUI Console Driver *
533 ******************************************************************************/
536 GuiDeinitFrontEnd(IN OUT PFRONTEND This
);
538 static NTSTATUS NTAPI
539 GuiInitFrontEnd(IN OUT PFRONTEND This
,
540 IN PCONSRV_CONSOLE Console
)
542 PGUI_INIT_INFO GuiInitInfo
;
543 PGUI_CONSOLE_DATA GuiData
;
545 if (This
== NULL
|| Console
== NULL
|| This
->Context2
== NULL
)
546 return STATUS_INVALID_PARAMETER
;
548 ASSERT(This
->Console
== Console
);
550 GuiInitInfo
= This
->Context2
;
552 /* Terminal data allocation */
553 GuiData
= ConsoleAllocHeap(HEAP_ZERO_MEMORY
, sizeof(*GuiData
));
556 DPRINT1("CONSRV: Failed to create GUI_CONSOLE_DATA\n");
557 return STATUS_UNSUCCESSFUL
;
559 /// /* HACK */ Console->FrontEndIFace.Context = (PVOID)GuiData; /* HACK */
560 GuiData
->Console
= Console
;
561 GuiData
->ActiveBuffer
= Console
->ActiveBuffer
;
562 GuiData
->hWindow
= NULL
;
563 GuiData
->IsWindowVisible
= GuiInitInfo
->IsWindowVisible
;
565 /* The console can be resized */
566 Console
->FixedSize
= FALSE
;
568 InitializeCriticalSection(&GuiData
->Lock
);
573 RtlCopyMemory(&GuiData
->GuiInfo
, &GuiInitInfo
->TermInfo
, sizeof(GuiInitInfo
->TermInfo
));
575 /* Initialize the icon handles */
576 if (GuiInitInfo
->hIcon
!= NULL
)
577 GuiData
->hIcon
= GuiInitInfo
->hIcon
;
579 GuiData
->hIcon
= ghDefaultIcon
;
581 if (GuiInitInfo
->hIconSm
!= NULL
)
582 GuiData
->hIconSm
= GuiInitInfo
->hIconSm
;
584 GuiData
->hIconSm
= ghDefaultIconSm
;
586 ASSERT(GuiData
->hIcon
&& GuiData
->hIconSm
);
588 /* Mouse is shown by default with its default cursor shape */
589 GuiData
->hCursor
= ghDefaultCursor
;
590 GuiData
->MouseCursorRefCount
= 0;
592 /* A priori don't ignore mouse signals */
593 GuiData
->IgnoreNextMouseSignal
= FALSE
;
594 /* Initialize HACK FOR CORE-8394. See conwnd.c!OnMouse for more details. */
595 GuiData
->HackCORE8394IgnoreNextMove
= FALSE
;
597 /* Close button and the corresponding system menu item are enabled by default */
598 GuiData
->IsCloseButtonEnabled
= TRUE
;
600 /* There is no user-reserved menu id range by default */
601 GuiData
->CmdIdLow
= GuiData
->CmdIdHigh
= 0;
603 /* Initialize the selection */
604 RtlZeroMemory(&GuiData
->Selection
, sizeof(GuiData
->Selection
));
605 GuiData
->Selection
.dwFlags
= CONSOLE_NO_SELECTION
;
606 RtlZeroMemory(&GuiData
->dwSelectionCursor
, sizeof(GuiData
->dwSelectionCursor
));
607 GuiData
->LineSelection
= FALSE
; // Default to block selection
608 // TODO: Retrieve the selection mode via the registry.
610 GuiData
->InputThreadId
= GuiInitInfo
->InputThreadId
;
611 GuiData
->WinSta
= GuiInitInfo
->WinSta
;
612 GuiData
->Desktop
= GuiInitInfo
->Desktop
;
614 /* Finally, finish to initialize the frontend structure */
615 This
->Context
= GuiData
;
616 ConsoleFreeHeap(This
->Context2
);
617 This
->Context2
= NULL
;
620 * We need to wait until the GUI has been fully initialized
621 * to retrieve custom settings i.e. WindowSize etc...
622 * Ideally we could use SendNotifyMessage for this but its not
625 NtCreateEvent(&GuiData
->hGuiInitEvent
, EVENT_ALL_ACCESS
,
626 NULL
, SynchronizationEvent
, FALSE
);
627 NtCreateEvent(&GuiData
->hGuiTermEvent
, EVENT_ALL_ACCESS
,
628 NULL
, SynchronizationEvent
, FALSE
);
630 DPRINT("GUI - Checkpoint\n");
632 /* Create the terminal window */
633 PostThreadMessageW(GuiData
->InputThreadId
, PM_CREATE_CONSOLE
, 0, (LPARAM
)GuiData
);
635 /* Wait until initialization has finished */
636 NtWaitForSingleObject(GuiData
->hGuiInitEvent
, FALSE
, NULL
);
637 DPRINT("OK we created the console window\n");
638 NtClose(GuiData
->hGuiInitEvent
);
639 GuiData
->hGuiInitEvent
= NULL
;
641 /* Check whether we really succeeded in initializing the terminal window */
642 if (GuiData
->hWindow
== NULL
)
644 DPRINT("GuiInitConsole - We failed at creating a new terminal window\n");
645 GuiDeinitFrontEnd(This
);
646 return STATUS_UNSUCCESSFUL
;
649 return STATUS_SUCCESS
;
653 GuiDeinitFrontEnd(IN OUT PFRONTEND This
)
655 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
657 DPRINT("Send PM_DESTROY_CONSOLE message and wait on hGuiTermEvent...\n");
658 PostThreadMessageW(GuiData
->InputThreadId
, PM_DESTROY_CONSOLE
, 0, (LPARAM
)GuiData
);
659 NtWaitForSingleObject(GuiData
->hGuiTermEvent
, FALSE
, NULL
);
660 DPRINT("hGuiTermEvent set\n");
661 NtClose(GuiData
->hGuiTermEvent
);
662 GuiData
->hGuiTermEvent
= NULL
;
664 CloseDesktop(GuiData
->Desktop
); // NtUserCloseDesktop
665 CloseWindowStation(GuiData
->WinSta
); // NtUserCloseWindowStation
667 DPRINT("Destroying icons !! - GuiData->hIcon = 0x%p ; ghDefaultIcon = 0x%p ; GuiData->hIconSm = 0x%p ; ghDefaultIconSm = 0x%p\n",
668 GuiData
->hIcon
, ghDefaultIcon
, GuiData
->hIconSm
, ghDefaultIconSm
);
669 if (GuiData
->hIcon
!= NULL
&& GuiData
->hIcon
!= ghDefaultIcon
)
671 DPRINT("Destroy hIcon\n");
672 DestroyIcon(GuiData
->hIcon
);
674 if (GuiData
->hIconSm
!= NULL
&& GuiData
->hIconSm
!= ghDefaultIconSm
)
676 DPRINT("Destroy hIconSm\n");
677 DestroyIcon(GuiData
->hIconSm
);
680 This
->Context
= NULL
;
681 DeleteCriticalSection(&GuiData
->Lock
);
682 ConsoleFreeHeap(GuiData
);
684 DPRINT("Quit GuiDeinitFrontEnd\n");
688 GuiDrawRegion(IN OUT PFRONTEND This
,
691 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
693 /* Do nothing if the window is hidden */
694 if (!GuiData
->IsWindowVisible
) return;
696 DrawRegion(GuiData
, Region
);
700 GuiWriteStream(IN OUT PFRONTEND This
,
708 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
709 PCONSOLE_SCREEN_BUFFER Buff
;
710 SHORT CursorEndX
, CursorEndY
;
713 if (NULL
== GuiData
|| NULL
== GuiData
->hWindow
) return;
715 /* Do nothing if the window is hidden */
716 if (!GuiData
->IsWindowVisible
) return;
718 Buff
= GuiData
->ActiveBuffer
;
719 if (GetType(Buff
) != TEXTMODE_BUFFER
) return;
721 if (0 != ScrolledLines
)
725 ScrollRect
.right
= Buff
->ViewSize
.X
* GuiData
->CharWidth
;
726 ScrollRect
.bottom
= Region
->Top
* GuiData
->CharHeight
;
728 ScrollWindowEx(GuiData
->hWindow
,
730 -(int)(ScrolledLines
* GuiData
->CharHeight
),
738 DrawRegion(GuiData
, Region
);
740 if (CursorStartX
< Region
->Left
|| Region
->Right
< CursorStartX
741 || CursorStartY
< Region
->Top
|| Region
->Bottom
< CursorStartY
)
743 InvalidateCell(GuiData
, CursorStartX
, CursorStartY
);
746 CursorEndX
= Buff
->CursorPosition
.X
;
747 CursorEndY
= Buff
->CursorPosition
.Y
;
748 if ((CursorEndX
< Region
->Left
|| Region
->Right
< CursorEndX
749 || CursorEndY
< Region
->Top
|| Region
->Bottom
< CursorEndY
)
750 && (CursorEndX
!= CursorStartX
|| CursorEndY
!= CursorStartY
))
752 InvalidateCell(GuiData
, CursorEndX
, CursorEndY
);
756 // Set up the update timer (very short interval) - this is a "hack" for getting the OS to
757 // repaint the window without having it just freeze up and stay on the screen permanently.
758 Buff
->CursorBlinkOn
= TRUE
;
759 SetTimer(GuiData
->hWindow
, CONGUI_UPDATE_TIMER
, CONGUI_UPDATE_TIME
, NULL
);
762 /* static */ VOID NTAPI
763 GuiRingBell(IN OUT PFRONTEND This
)
765 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
767 /* Emit an error beep sound */
768 SendNotifyMessage(GuiData
->hWindow
, PM_CONSOLE_BEEP
, 0, 0);
772 GuiSetCursorInfo(IN OUT PFRONTEND This
,
773 PCONSOLE_SCREEN_BUFFER Buff
)
775 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
777 /* Do nothing if the window is hidden */
778 if (!GuiData
->IsWindowVisible
) return TRUE
;
780 if (GuiData
->ActiveBuffer
== Buff
)
782 InvalidateCell(GuiData
, Buff
->CursorPosition
.X
, Buff
->CursorPosition
.Y
);
789 GuiSetScreenInfo(IN OUT PFRONTEND This
,
790 PCONSOLE_SCREEN_BUFFER Buff
,
794 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
796 /* Do nothing if the window is hidden */
797 if (!GuiData
->IsWindowVisible
) return TRUE
;
799 if (GuiData
->ActiveBuffer
== Buff
)
801 /* Redraw char at old position (remove cursor) */
802 InvalidateCell(GuiData
, OldCursorX
, OldCursorY
);
803 /* Redraw char at new position (show cursor) */
804 InvalidateCell(GuiData
, Buff
->CursorPosition
.X
, Buff
->CursorPosition
.Y
);
811 GuiResizeTerminal(IN OUT PFRONTEND This
)
813 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
815 /* Resize the window to the user's values */
816 PostMessageW(GuiData
->hWindow
, PM_RESIZE_TERMINAL
, 0, 0);
820 GuiSetActiveScreenBuffer(IN OUT PFRONTEND This
)
822 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
823 PCONSOLE_SCREEN_BUFFER ActiveBuffer
;
826 EnterCriticalSection(&GuiData
->Lock
);
827 GuiData
->WindowSizeLock
= TRUE
;
829 InterlockedExchangePointer((PVOID
*)&GuiData
->ActiveBuffer
,
830 ConDrvGetActiveScreenBuffer(GuiData
->Console
));
832 GuiData
->WindowSizeLock
= FALSE
;
833 LeaveCriticalSection(&GuiData
->Lock
);
835 ActiveBuffer
= GuiData
->ActiveBuffer
;
837 /* Change the current palette */
838 if (ActiveBuffer
->PaletteHandle
== NULL
)
840 hPalette
= GuiData
->hSysPalette
;
844 hPalette
= ActiveBuffer
->PaletteHandle
;
847 DPRINT("GuiSetActiveScreenBuffer using palette 0x%p\n", hPalette
);
849 /* Set the new palette for the framebuffer */
850 SelectPalette(GuiData
->hMemDC
, hPalette
, FALSE
);
852 /* Specify the use of the system palette for the framebuffer */
853 SetSystemPaletteUse(GuiData
->hMemDC
, ActiveBuffer
->PaletteUsage
);
855 /* Realize the (logical) palette */
856 RealizePalette(GuiData
->hMemDC
);
858 GuiResizeTerminal(This
);
859 // ConioDrawConsole(Console);
863 GuiReleaseScreenBuffer(IN OUT PFRONTEND This
,
864 IN PCONSOLE_SCREEN_BUFFER ScreenBuffer
)
866 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
869 * If we were notified to release a screen buffer that is not actually
870 * ours, then just ignore the notification...
872 if (ScreenBuffer
!= GuiData
->ActiveBuffer
) return;
875 * ... else, we must release our active buffer. Two cases are present:
876 * - If ScreenBuffer (== GuiData->ActiveBuffer) IS NOT the console
877 * active screen buffer, then we can safely switch to it.
878 * - If ScreenBuffer IS the console active screen buffer, we must release
882 /* Release the old active palette and set the default one */
883 if (GetCurrentObject(GuiData
->hMemDC
, OBJ_PAL
) == ScreenBuffer
->PaletteHandle
)
885 /* Set the new palette */
886 SelectPalette(GuiData
->hMemDC
, GuiData
->hSysPalette
, FALSE
);
889 /* Set the adequate active screen buffer */
890 if (ScreenBuffer
!= GuiData
->Console
->ActiveBuffer
)
892 GuiSetActiveScreenBuffer(This
);
896 EnterCriticalSection(&GuiData
->Lock
);
897 GuiData
->WindowSizeLock
= TRUE
;
899 InterlockedExchangePointer((PVOID
*)&GuiData
->ActiveBuffer
, NULL
);
901 GuiData
->WindowSizeLock
= FALSE
;
902 LeaveCriticalSection(&GuiData
->Lock
);
907 GuiSetMouseCursor(IN OUT PFRONTEND This
,
908 HCURSOR CursorHandle
);
911 GuiRefreshInternalInfo(IN OUT PFRONTEND This
)
913 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
915 /* Update the console leader information held by the window */
916 SetConWndConsoleLeaderCID(GuiData
);
920 * We reset the cursor here so that, when a console app quits, we reset
921 * the cursor to the default one. It's quite a hack since it doesn't proceed
922 * per - console process... This must be fixed.
924 * See GuiInitConsole(...) for more information.
927 /* Mouse is shown by default with its default cursor shape */
928 GuiData
->MouseCursorRefCount
= 0; // Reinitialize the reference counter
929 GuiSetMouseCursor(This
, NULL
);
933 GuiChangeTitle(IN OUT PFRONTEND This
)
935 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
936 // PostMessageW(GuiData->hWindow, PM_CONSOLE_SET_TITLE, 0, 0);
937 SetWindowTextW(GuiData
->hWindow
, GuiData
->Console
->Title
.Buffer
);
941 GuiChangeIcon(IN OUT PFRONTEND This
,
944 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
945 HICON hIcon
, hIconSm
;
947 if (IconHandle
== NULL
)
949 hIcon
= ghDefaultIcon
;
950 hIconSm
= ghDefaultIconSm
;
954 hIcon
= CopyIcon(IconHandle
);
955 hIconSm
= CopyIcon(IconHandle
);
963 if (hIcon
!= GuiData
->hIcon
)
965 if (GuiData
->hIcon
!= NULL
&& GuiData
->hIcon
!= ghDefaultIcon
)
967 DestroyIcon(GuiData
->hIcon
);
969 if (GuiData
->hIconSm
!= NULL
&& GuiData
->hIconSm
!= ghDefaultIconSm
)
971 DestroyIcon(GuiData
->hIconSm
);
974 GuiData
->hIcon
= hIcon
;
975 GuiData
->hIconSm
= hIconSm
;
977 DPRINT("Set icons in GuiChangeIcon\n");
978 PostMessageW(GuiData
->hWindow
, WM_SETICON
, ICON_BIG
, (LPARAM
)GuiData
->hIcon
);
979 PostMessageW(GuiData
->hWindow
, WM_SETICON
, ICON_SMALL
, (LPARAM
)GuiData
->hIconSm
);
986 GuiGetConsoleWindowHandle(IN OUT PFRONTEND This
)
988 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
989 return GuiData
->hWindow
;
993 GuiGetLargestConsoleWindowSize(IN OUT PFRONTEND This
,
996 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
997 PCONSOLE_SCREEN_BUFFER ActiveBuffer
;
1000 UINT WidthUnit
, HeightUnit
;
1004 if (!SystemParametersInfoW(SPI_GETWORKAREA
, 0, &WorkArea
, 0))
1006 DPRINT1("SystemParametersInfoW failed - What to do ??\n");
1010 ActiveBuffer
= GuiData
->ActiveBuffer
;
1013 GetScreenBufferSizeUnits(ActiveBuffer
, GuiData
, &WidthUnit
, &HeightUnit
);
1017 /* Default: text mode */
1018 WidthUnit
= GuiData
->CharWidth
;
1019 HeightUnit
= GuiData
->CharHeight
;
1022 width
= WorkArea
.right
;
1023 height
= WorkArea
.bottom
;
1025 width
-= (2 * (GetSystemMetrics(SM_CXFRAME
) + GetSystemMetrics(SM_CXEDGE
)));
1026 height
-= (2 * (GetSystemMetrics(SM_CYFRAME
) + GetSystemMetrics(SM_CYEDGE
)) + GetSystemMetrics(SM_CYCAPTION
));
1028 if (width
< 0) width
= 0;
1029 if (height
< 0) height
= 0;
1031 pSize
->X
= (SHORT
)(width
/ (int)WidthUnit
) /* HACK */ + 2;
1032 pSize
->Y
= (SHORT
)(height
/ (int)HeightUnit
) /* HACK */ + 1;
1036 GuiGetSelectionInfo(IN OUT PFRONTEND This
,
1037 PCONSOLE_SELECTION_INFO pSelectionInfo
)
1039 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1041 if (pSelectionInfo
== NULL
) return FALSE
;
1043 ZeroMemory(pSelectionInfo
, sizeof(*pSelectionInfo
));
1044 if (GuiData
->Selection
.dwFlags
!= CONSOLE_NO_SELECTION
)
1045 RtlCopyMemory(pSelectionInfo
, &GuiData
->Selection
, sizeof(*pSelectionInfo
));
1051 GuiSetPalette(IN OUT PFRONTEND This
,
1052 HPALETTE PaletteHandle
,
1055 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1056 HPALETTE OldPalette
;
1058 // if (GetType(GuiData->ActiveBuffer) != GRAPHICS_BUFFER) return FALSE;
1059 if (PaletteHandle
== NULL
) return FALSE
;
1061 /* Set the new palette for the framebuffer */
1062 OldPalette
= SelectPalette(GuiData
->hMemDC
, PaletteHandle
, FALSE
);
1063 if (OldPalette
== NULL
) return FALSE
;
1065 /* Specify the use of the system palette for the framebuffer */
1066 SetSystemPaletteUse(GuiData
->hMemDC
, PaletteUsage
);
1068 /* Realize the (logical) palette */
1069 RealizePalette(GuiData
->hMemDC
);
1071 /* Save the original system palette handle */
1072 if (GuiData
->hSysPalette
== NULL
) GuiData
->hSysPalette
= OldPalette
;
1078 GuiGetDisplayMode(IN OUT PFRONTEND This
)
1080 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1081 ULONG DisplayMode
= 0;
1083 if (GuiData
->GuiInfo
.FullScreen
)
1084 DisplayMode
|= CONSOLE_FULLSCREEN_HARDWARE
; // CONSOLE_FULLSCREEN
1086 DisplayMode
|= CONSOLE_WINDOWED
;
1092 GuiSetDisplayMode(IN OUT PFRONTEND This
,
1095 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1098 if (NewMode
& ~(CONSOLE_FULLSCREEN_MODE
| CONSOLE_WINDOWED_MODE
))
1101 /* Do nothing if the window is hidden */
1102 if (!GuiData
->IsWindowVisible
) return TRUE
;
1104 FullScreen
= ((NewMode
& CONSOLE_FULLSCREEN_MODE
) != 0);
1106 if (FullScreen
!= GuiData
->GuiInfo
.FullScreen
)
1108 SwitchFullScreen(GuiData
, FullScreen
);
1115 GuiShowMouseCursor(IN OUT PFRONTEND This
,
1118 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1120 if (GuiData
->IsWindowVisible
)
1122 /* Set the reference count */
1123 if (Show
) ++GuiData
->MouseCursorRefCount
;
1124 else --GuiData
->MouseCursorRefCount
;
1126 /* Effectively show (or hide) the cursor (use special values for (w|l)Param) */
1127 PostMessageW(GuiData
->hWindow
, WM_SETCURSOR
, -1, -1);
1130 return GuiData
->MouseCursorRefCount
;
1134 GuiSetMouseCursor(IN OUT PFRONTEND This
,
1135 HCURSOR CursorHandle
)
1137 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1139 /* Do nothing if the window is hidden */
1140 if (!GuiData
->IsWindowVisible
) return TRUE
;
1143 * Set the cursor's handle. If the given handle is NULL,
1144 * then restore the default cursor.
1146 GuiData
->hCursor
= (CursorHandle
? CursorHandle
: ghDefaultCursor
);
1148 /* Effectively modify the shape of the cursor (use special values for (w|l)Param) */
1149 PostMessageW(GuiData
->hWindow
, WM_SETCURSOR
, -1, -1);
1155 GuiMenuControl(IN OUT PFRONTEND This
,
1159 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1161 GuiData
->CmdIdLow
= CmdIdLow
;
1162 GuiData
->CmdIdHigh
= CmdIdHigh
;
1164 return GetSystemMenu(GuiData
->hWindow
, FALSE
);
1168 GuiSetMenuClose(IN OUT PFRONTEND This
,
1172 * NOTE: See http://www.mail-archive.com/harbour@harbour-project.org/msg27509.html
1173 * or http://harbour-devel.1590103.n2.nabble.com/Question-about-hb-gt-win-CtrlHandler-usage-td4670862i20.html
1174 * for more information.
1177 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1178 HMENU hSysMenu
= GetSystemMenu(GuiData
->hWindow
, FALSE
);
1180 if (hSysMenu
== NULL
) return FALSE
;
1182 GuiData
->IsCloseButtonEnabled
= Enable
;
1183 EnableMenuItem(hSysMenu
, SC_CLOSE
, MF_BYCOMMAND
| (Enable
? MF_ENABLED
: MF_GRAYED
));
1188 static FRONTEND_VTBL GuiVtbl
=
1198 GuiSetActiveScreenBuffer
,
1199 GuiReleaseScreenBuffer
,
1200 GuiRefreshInternalInfo
,
1203 GuiGetConsoleWindowHandle
,
1204 GuiGetLargestConsoleWindowSize
,
1205 GuiGetSelectionInfo
,
1217 GuiLoadFrontEnd(IN OUT PFRONTEND FrontEnd
,
1218 IN OUT PCONSOLE_STATE_INFO ConsoleInfo
,
1219 IN OUT PCONSOLE_INIT_INFO ConsoleInitInfo
,
1220 IN HANDLE ConsoleLeaderProcessHandle
)
1222 PCONSOLE_START_INFO ConsoleStartInfo
;
1223 PGUI_INIT_INFO GuiInitInfo
;
1225 if (FrontEnd
== NULL
|| ConsoleInfo
== NULL
|| ConsoleInitInfo
== NULL
)
1226 return STATUS_INVALID_PARAMETER
;
1228 ConsoleStartInfo
= ConsoleInitInfo
->ConsoleStartInfo
;
1231 * Initialize a private initialization info structure for later use.
1232 * It must be freed by a call to GuiUnloadFrontEnd or GuiInitFrontEnd.
1234 GuiInitInfo
= ConsoleAllocHeap(HEAP_ZERO_MEMORY
, sizeof(*GuiInitInfo
));
1235 if (GuiInitInfo
== NULL
) return STATUS_NO_MEMORY
;
1237 /* Initialize GUI terminal emulator common functionalities */
1238 if (!GuiInit(ConsoleInitInfo
, ConsoleLeaderProcessHandle
, GuiInitInfo
))
1240 ConsoleFreeHeap(GuiInitInfo
);
1241 return STATUS_UNSUCCESSFUL
;
1245 * Load terminal settings
1248 /* Impersonate the caller in order to retrieve settings in its context */
1249 // if (!CsrImpersonateClient(NULL))
1250 // return STATUS_UNSUCCESSFUL;
1251 CsrImpersonateClient(NULL
);
1253 /* 1. Load the default settings */
1254 GuiConsoleGetDefaultSettings(&GuiInitInfo
->TermInfo
);
1257 GuiInitInfo
->TermInfo
.ShowWindow
= SW_SHOWNORMAL
;
1259 if (ConsoleInitInfo
->IsWindowVisible
)
1261 /* 2. Load the remaining console settings via the registry */
1262 if ((ConsoleStartInfo
->dwStartupFlags
& STARTF_TITLEISLINKNAME
) == 0)
1265 /* Load the terminal infos from the registry */
1266 GuiConsoleReadUserSettings(&GuiInitInfo
->TermInfo
);
1270 * Now, update them with the properties the user might gave to us
1271 * via the STARTUPINFO structure before calling CreateProcess
1272 * (and which was transmitted via the ConsoleStartInfo structure).
1273 * We therefore overwrite the values read in the registry.
1275 if (ConsoleStartInfo
->dwStartupFlags
& STARTF_USESHOWWINDOW
)
1277 GuiInitInfo
->TermInfo
.ShowWindow
= ConsoleStartInfo
->wShowWindow
;
1279 if (ConsoleStartInfo
->dwStartupFlags
& STARTF_USEPOSITION
)
1281 ConsoleInfo
->AutoPosition
= FALSE
;
1282 ConsoleInfo
->WindowPosition
.x
= ConsoleStartInfo
->dwWindowOrigin
.X
;
1283 ConsoleInfo
->WindowPosition
.y
= ConsoleStartInfo
->dwWindowOrigin
.Y
;
1285 if (ConsoleStartInfo
->dwStartupFlags
& STARTF_RUNFULLSCREEN
)
1287 ConsoleInfo
->FullScreen
= TRUE
;
1293 /* Revert impersonation */
1298 wcsncpy(GuiInitInfo
->TermInfo
.FaceName
, ConsoleInfo
->FaceName
, LF_FACESIZE
);
1299 GuiInitInfo
->TermInfo
.FaceName
[LF_FACESIZE
- 1] = UNICODE_NULL
;
1300 GuiInitInfo
->TermInfo
.FontFamily
= ConsoleInfo
->FontFamily
;
1301 GuiInitInfo
->TermInfo
.FontSize
= ConsoleInfo
->FontSize
;
1302 GuiInitInfo
->TermInfo
.FontWeight
= ConsoleInfo
->FontWeight
;
1305 GuiInitInfo
->TermInfo
.FullScreen
= ConsoleInfo
->FullScreen
;
1306 // GuiInitInfo->TermInfo.ShowWindow;
1307 GuiInitInfo
->TermInfo
.AutoPosition
= ConsoleInfo
->AutoPosition
;
1308 GuiInitInfo
->TermInfo
.WindowOrigin
= ConsoleInfo
->WindowPosition
;
1310 /* Initialize the icon handles */
1311 // if (ConsoleStartInfo->hIcon != NULL)
1312 GuiInitInfo
->hIcon
= ConsoleStartInfo
->hIcon
;
1314 // GuiInitInfo->hIcon = ghDefaultIcon;
1316 // if (ConsoleStartInfo->hIconSm != NULL)
1317 GuiInitInfo
->hIconSm
= ConsoleStartInfo
->hIconSm
;
1319 // GuiInitInfo->hIconSm = ghDefaultIconSm;
1321 // ASSERT(GuiInitInfo->hIcon && GuiInitInfo->hIconSm);
1323 GuiInitInfo
->IsWindowVisible
= ConsoleInitInfo
->IsWindowVisible
;
1325 /* Finally, initialize the frontend structure */
1326 FrontEnd
->Vtbl
= &GuiVtbl
;
1327 FrontEnd
->Context
= NULL
;
1328 FrontEnd
->Context2
= GuiInitInfo
;
1330 return STATUS_SUCCESS
;
1334 GuiUnloadFrontEnd(IN OUT PFRONTEND FrontEnd
)
1336 if (FrontEnd
== NULL
) return STATUS_INVALID_PARAMETER
;
1338 if (FrontEnd
->Context
) GuiDeinitFrontEnd(FrontEnd
);
1339 if (FrontEnd
->Context2
) ConsoleFreeHeap(FrontEnd
->Context2
);
1341 return STATUS_SUCCESS
;