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 ******************************************************************/
59 /* NOTE: Defined in conwnd.c */
61 GetScreenBufferSizeUnits(IN PCONSOLE_SCREEN_BUFFER Buffer
,
62 IN PGUI_CONSOLE_DATA GuiData
,
64 OUT PUINT HeightUnit
);
67 GuiConsoleMoveWindow(PGUI_CONSOLE_DATA GuiData
)
69 /* Move the window if needed (not positioned by the system) */
70 if (!GuiData
->GuiInfo
.AutoPosition
)
72 SetWindowPos(GuiData
->hWindow
,
74 GuiData
->GuiInfo
.WindowOrigin
.x
,
75 GuiData
->GuiInfo
.WindowOrigin
.y
,
77 SWP_NOZORDER
| SWP_NOSIZE
| SWP_NOACTIVATE
);
82 SmallRectToRect(PGUI_CONSOLE_DATA GuiData
, PRECT Rect
, PSMALL_RECT SmallRect
)
84 PCONSOLE_SCREEN_BUFFER Buffer
= GuiData
->ActiveBuffer
;
85 UINT WidthUnit
, HeightUnit
;
87 GetScreenBufferSizeUnits(Buffer
, GuiData
, &WidthUnit
, &HeightUnit
);
89 Rect
->left
= (SmallRect
->Left
- Buffer
->ViewOrigin
.X
) * WidthUnit
;
90 Rect
->top
= (SmallRect
->Top
- Buffer
->ViewOrigin
.Y
) * HeightUnit
;
91 Rect
->right
= (SmallRect
->Right
+ 1 - Buffer
->ViewOrigin
.X
) * WidthUnit
;
92 Rect
->bottom
= (SmallRect
->Bottom
+ 1 - Buffer
->ViewOrigin
.Y
) * HeightUnit
;
96 DrawRegion(PGUI_CONSOLE_DATA GuiData
,
101 SmallRectToRect(GuiData
, &RegionRect
, Region
);
102 /* Do not erase the background: it speeds up redrawing and reduce flickering */
103 InvalidateRect(GuiData
->hWindow
, &RegionRect
, FALSE
);
104 /**UpdateWindow(GuiData->hWindow);**/
108 InvalidateCell(PGUI_CONSOLE_DATA GuiData
,
111 SMALL_RECT CellRect
= { x
, y
, x
, y
};
112 DrawRegion(GuiData
, &CellRect
);
116 /******************************************************************************
117 * GUI Terminal Initialization *
118 ******************************************************************************/
121 SwitchFullScreen(PGUI_CONSOLE_DATA GuiData
, BOOL FullScreen
);
123 CreateSysMenu(HWND hWnd
);
126 GuiConsoleInputThread(PVOID Param
)
129 PCSR_THREAD pcsrt
= NULL
;
130 PGUI_INIT_INFO GuiInitInfo
= (PGUI_INIT_INFO
)Param
;
131 DESKTOP_CONSOLE_THREAD DesktopConsoleThreadInfo
;
132 ULONG_PTR InputThreadId
= HandleToUlong(NtCurrentTeb()->ClientId
.UniqueThread
);
133 HANDLE hThread
= NULL
;
135 LONG WindowCount
= 0;
139 * This thread dispatches all the console notifications to the
140 * notification window. It is common for all the console windows
141 * in a given desktop in a window station.
144 /* Assign this console input thread to this desktop */
145 DesktopConsoleThreadInfo
.DesktopHandle
= GuiInitInfo
->Desktop
; // Duplicated desktop handle
146 DesktopConsoleThreadInfo
.ThreadId
= InputThreadId
;
147 Status
= NtUserConsoleControl(ConsoleCtrlDesktopConsoleThread
,
148 &DesktopConsoleThreadInfo
,
149 sizeof(DesktopConsoleThreadInfo
));
150 if (!NT_SUCCESS(Status
)) goto Quit
;
152 /* Connect this CSR thread to the USER subsystem */
153 pcsrt
= CsrConnectToUser();
154 if (pcsrt
== NULL
) goto Quit
;
155 hThread
= pcsrt
->ThreadHandle
;
157 /* Assign the desktop to this thread */
158 if (!SetThreadDesktop(DesktopConsoleThreadInfo
.DesktopHandle
)) goto Quit
;
160 /* The thread has been initialized, set the event */
161 NtSetEvent(GuiInitInfo
->GuiThreadStartupEvent
, NULL
);
162 Status
= STATUS_SUCCESS
;
164 while (GetMessageW(&msg
, NULL
, 0, 0))
168 case PM_CREATE_CONSOLE
:
170 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
)msg
.lParam
;
171 PCONSRV_CONSOLE Console
= GuiData
->Console
;
175 DPRINT("PM_CREATE_CONSOLE -- creating window\n");
177 NewWindow
= CreateWindowExW(WS_EX_CLIENTEDGE
,
179 Console
->Title
.Buffer
,
185 GuiData
->IsWindowVisible
? HWND_DESKTOP
: HWND_MESSAGE
,
189 if (NewWindow
== NULL
)
191 DPRINT1("Failed to create a new console window\n");
195 ASSERT(NewWindow
== GuiData
->hWindow
);
197 InterlockedIncrement(&WindowCount
);
200 // FIXME: TODO: Move everything there into conwnd.c!OnNcCreate()
203 /* Retrieve our real position */
204 // See conwnd.c!OnMove()
205 GetWindowRect(GuiData
->hWindow
, &rcWnd
);
206 GuiData
->GuiInfo
.WindowOrigin
.x
= rcWnd
.left
;
207 GuiData
->GuiInfo
.WindowOrigin
.y
= rcWnd
.top
;
209 if (GuiData
->IsWindowVisible
)
211 /* Move and resize the window to the user's values */
212 /* CAN WE DEADLOCK ?? */
213 GuiConsoleMoveWindow(GuiData
); // FIXME: This MUST be done via the CreateWindowExW call.
214 SendMessageW(GuiData
->hWindow
, PM_RESIZE_TERMINAL
, 0, 0);
217 // FIXME: HACK: Potential HACK for CORE-8129; see revision 63595.
218 CreateSysMenu(GuiData
->hWindow
);
220 if (GuiData
->IsWindowVisible
)
222 /* Switch to full-screen mode if necessary */
223 // FIXME: Move elsewhere, it cause misdrawings of the window.
224 if (GuiData
->GuiInfo
.FullScreen
) SwitchFullScreen(GuiData
, TRUE
);
226 DPRINT("PM_CREATE_CONSOLE -- showing window\n");
227 // ShowWindow(NewWindow, (int)GuiData->GuiInfo.ShowWindow);
228 ShowWindowAsync(NewWindow
, (int)GuiData
->GuiInfo
.ShowWindow
);
229 DPRINT("Window showed\n");
233 DPRINT("PM_CREATE_CONSOLE -- hidden window\n");
234 ShowWindowAsync(NewWindow
, SW_HIDE
);
240 case PM_DESTROY_CONSOLE
:
242 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
)msg
.lParam
;
245 /* Exit the full screen mode if it was already set */
246 // LeaveFullScreen(GuiData);
249 * Window creation is done using a PostMessage(), so it's possible
250 * that the window that we want to destroy doesn't exist yet.
251 * So first empty the message queue.
254 while (PeekMessageW(&TempMsg, NULL, 0, 0, PM_REMOVE))
256 TranslateMessage(&TempMsg);
257 DispatchMessageW(&TempMsg);
259 while (PeekMessageW(&TempMsg
, NULL
, 0, 0, PM_REMOVE
)) ;
261 if (GuiData
->hWindow
== NULL
) continue;
263 DestroyWindow(GuiData
->hWindow
);
265 NtSetEvent(GuiData
->hGuiTermEvent
, NULL
);
267 if (InterlockedDecrement(&WindowCount
) == 0)
269 DPRINT("CONSRV: Going to quit the Input Thread 0x%p\n", InputThreadId
);
277 TranslateMessage(&msg
);
278 DispatchMessageW(&msg
);
282 DPRINT("CONSRV: Quit the Input Thread 0x%p, Status = 0x%08lx\n", InputThreadId
, Status
);
284 /* Remove this console input thread from this desktop */
285 // DesktopConsoleThreadInfo.DesktopHandle;
286 DesktopConsoleThreadInfo
.ThreadId
= 0;
287 NtUserConsoleControl(ConsoleCtrlDesktopConsoleThread
,
288 &DesktopConsoleThreadInfo
,
289 sizeof(DesktopConsoleThreadInfo
));
291 /* Close the duplicated desktop handle */
292 CloseDesktop(DesktopConsoleThreadInfo
.DesktopHandle
); // NtUserCloseDesktop
294 /* Cleanup CSR thread */
297 if (hThread
!= pcsrt
->ThreadHandle
)
298 DPRINT1("WARNING!! hThread (0x%p) != pcsrt->ThreadHandle (0x%p), you may expect crashes soon!!\n", hThread
, pcsrt
->ThreadHandle
);
300 CsrDereferenceThread(pcsrt
);
303 /* Exit the thread */
304 RtlExitUserThread(Status
);
308 // FIXME: Maybe return a NTSTATUS
310 GuiInit(IN PCONSOLE_INIT_INFO ConsoleInitInfo
,
311 IN HANDLE ConsoleLeaderProcessHandle
,
312 IN OUT PGUI_INIT_INFO GuiInitInfo
)
315 UNICODE_STRING DesktopPath
;
316 DESKTOP_CONSOLE_THREAD DesktopConsoleThreadInfo
;
325 * Initialize and register the console window class, if needed.
327 if (!ConsInitialized
)
329 if (!RegisterConWndClass(ConSrvDllInstance
)) return FALSE
;
330 ConsInitialized
= TRUE
;
334 * Set-up the console input thread. We have
335 * one console input thread per desktop.
338 if (!CsrImpersonateClient(NULL
))
339 // return STATUS_BAD_IMPERSONATION_LEVEL;
342 if (ConsoleInitInfo
->DesktopLength
)
344 DesktopPath
.MaximumLength
= ConsoleInitInfo
->DesktopLength
;
345 DesktopPath
.Length
= DesktopPath
.MaximumLength
- sizeof(UNICODE_NULL
);
346 DesktopPath
.Buffer
= ConsoleInitInfo
->Desktop
;
350 RtlInitUnicodeString(&DesktopPath
, L
"Default");
353 hDesk
= NtUserResolveDesktop(ConsoleLeaderProcessHandle
,
357 DPRINT("NtUserResolveDesktop(DesktopPath = '%wZ') returned hDesk = 0x%p; hWinSta = 0x%p\n",
358 &DesktopPath
, hDesk
, hWinSta
);
362 if (hDesk
== NULL
) return FALSE
;
365 * We need to see whether we need to create a
366 * new console input thread for this desktop.
368 DesktopConsoleThreadInfo
.DesktopHandle
= hDesk
;
369 DesktopConsoleThreadInfo
.ThreadId
= (ULONG_PTR
)INVALID_HANDLE_VALUE
; // Special value to say we just want to retrieve the thread ID.
370 NtUserConsoleControl(ConsoleCtrlDesktopConsoleThread
,
371 &DesktopConsoleThreadInfo
,
372 sizeof(DesktopConsoleThreadInfo
));
373 DPRINT("NtUserConsoleControl returned ThreadId = 0x%p\n", DesktopConsoleThreadInfo
.ThreadId
);
376 * Save the opened window station and desktop handles in the initialization
377 * structure. They will be used later on, and released, by the GUI frontend.
379 GuiInitInfo
->WinSta
= hWinSta
;
380 GuiInitInfo
->Desktop
= hDesk
;
382 /* Here GuiInitInfo contains original handles */
384 /* If we already have a console input thread on this desktop... */
385 if (DesktopConsoleThreadInfo
.ThreadId
!= 0)
387 /* ... just use it... */
388 DPRINT("Using input thread InputThreadId = 0x%p\n", DesktopConsoleThreadInfo
.ThreadId
);
389 GuiInitInfo
->InputThreadId
= DesktopConsoleThreadInfo
.ThreadId
;
393 /* ... otherwise create a new one. */
395 /* Initialize a startup event for the thread to signal it */
396 Status
= NtCreateEvent(&GuiInitInfo
->GuiThreadStartupEvent
, EVENT_ALL_ACCESS
,
397 NULL
, SynchronizationEvent
, FALSE
);
398 if (!NT_SUCCESS(Status
))
405 * Duplicate the desktop handle for the console input thread internal needs.
406 * If it happens to need also a window station handle in the future, then
407 * it is there that you also need to duplicate the window station handle!
409 * Note also that we are going to temporarily overwrite the stored handles
410 * in GuiInitInfo because it happens that we use also this structure to give
411 * the duplicated handles to the input thread that is going to initialize.
412 * After the input thread finishes its initialization, we restore the handles
413 * in GuiInitInfo to their old values.
415 Status
= NtDuplicateObject(NtCurrentProcess(),
418 (PHANDLE
)&GuiInitInfo
->Desktop
,
419 0, 0, DUPLICATE_SAME_ACCESS
);
420 if (!NT_SUCCESS(Status
))
426 /* Here GuiInitInfo contains duplicated handles */
428 Status
= RtlCreateUserThread(NtCurrentProcess(),
430 TRUE
, // Start the thread in suspended state
434 (PVOID
)GuiConsoleInputThread
,
438 if (NT_SUCCESS(Status
))
440 /* Add it as a static server thread and resume it */
441 CsrAddStaticServerThread(hInputThread
, &ClientId
, 0);
442 Status
= NtResumeThread(hInputThread
, NULL
);
444 DPRINT("Thread creation hInputThread = 0x%p, InputThreadId = 0x%p, Status = 0x%08lx\n",
445 hInputThread
, ClientId
.UniqueThread
, Status
);
447 if (!NT_SUCCESS(Status
) || hInputThread
== NULL
)
449 /* Close the thread's handle */
450 if (hInputThread
) NtClose(hInputThread
);
452 /* We need to close here the duplicated desktop handle */
453 CloseDesktop(GuiInitInfo
->Desktop
); // NtUserCloseDesktop
455 /* Close the startup event and bail out */
456 NtClose(GuiInitInfo
->GuiThreadStartupEvent
);
458 DPRINT1("CONSRV: Failed to create graphics console thread.\n");
463 /* No need to close hInputThread, this is done by CSR automatically */
465 /* Wait for the thread to finish its initialization, and close the startup event */
466 NtWaitForSingleObject(GuiInitInfo
->GuiThreadStartupEvent
, FALSE
, NULL
);
467 NtClose(GuiInitInfo
->GuiThreadStartupEvent
);
470 * Save the input thread ID for later use, and restore the original handles.
471 * The copies are held by the console input thread.
473 GuiInitInfo
->InputThreadId
= (ULONG_PTR
)ClientId
.UniqueThread
;
474 GuiInitInfo
->WinSta
= hWinSta
;
475 GuiInitInfo
->Desktop
= hDesk
;
477 /* Here GuiInitInfo contains again original handles */
483 * Close the original handles. Do not use the copies in GuiInitInfo
484 * because we may have failed in the middle of the duplicate operation
485 * and the handles stored in GuiInitInfo may have changed.
487 CloseDesktop(hDesk
); // NtUserCloseDesktop
488 CloseWindowStation(hWinSta
); // NtUserCloseWindowStation
495 /******************************************************************************
496 * GUI Console Driver *
497 ******************************************************************************/
500 GuiDeinitFrontEnd(IN OUT PFRONTEND This
);
502 static NTSTATUS NTAPI
503 GuiInitFrontEnd(IN OUT PFRONTEND This
,
504 IN PCONSRV_CONSOLE Console
)
506 PGUI_INIT_INFO GuiInitInfo
;
507 PGUI_CONSOLE_DATA GuiData
;
509 if (This
== NULL
|| Console
== NULL
|| This
->Context2
== NULL
)
510 return STATUS_INVALID_PARAMETER
;
512 ASSERT(This
->Console
== Console
);
514 GuiInitInfo
= This
->Context2
;
516 /* Terminal data allocation */
517 GuiData
= ConsoleAllocHeap(HEAP_ZERO_MEMORY
, sizeof(*GuiData
));
520 DPRINT1("CONSRV: Failed to create GUI_CONSOLE_DATA\n");
521 return STATUS_UNSUCCESSFUL
;
523 /// /* HACK */ Console->FrontEndIFace.Context = (PVOID)GuiData; /* HACK */
524 GuiData
->Console
= Console
;
525 GuiData
->ActiveBuffer
= Console
->ActiveBuffer
;
526 GuiData
->hWindow
= NULL
;
527 GuiData
->IsWindowVisible
= GuiInitInfo
->IsWindowVisible
;
529 /* The console can be resized */
530 Console
->FixedSize
= FALSE
;
532 InitializeCriticalSection(&GuiData
->Lock
);
537 RtlCopyMemory(&GuiData
->GuiInfo
, &GuiInitInfo
->TermInfo
, sizeof(GuiInitInfo
->TermInfo
));
539 /* Initialize the icon handles */
540 if (GuiInitInfo
->hIcon
!= NULL
)
541 GuiData
->hIcon
= GuiInitInfo
->hIcon
;
543 GuiData
->hIcon
= ghDefaultIcon
;
545 if (GuiInitInfo
->hIconSm
!= NULL
)
546 GuiData
->hIconSm
= GuiInitInfo
->hIconSm
;
548 GuiData
->hIconSm
= ghDefaultIconSm
;
550 ASSERT(GuiData
->hIcon
&& GuiData
->hIconSm
);
552 /* Mouse is shown by default with its default cursor shape */
553 GuiData
->hCursor
= ghDefaultCursor
;
554 GuiData
->MouseCursorRefCount
= 0;
556 /* A priori don't ignore mouse signals */
557 GuiData
->IgnoreNextMouseSignal
= FALSE
;
558 /* Initialize HACK FOR CORE-8394. See conwnd.c!OnMouse for more details. */
559 GuiData
->HackCORE8394IgnoreNextMove
= FALSE
;
561 /* Close button and the corresponding system menu item are enabled by default */
562 GuiData
->IsCloseButtonEnabled
= TRUE
;
564 /* There is no user-reserved menu id range by default */
565 GuiData
->CmdIdLow
= GuiData
->CmdIdHigh
= 0;
567 /* Initialize the selection */
568 RtlZeroMemory(&GuiData
->Selection
, sizeof(GuiData
->Selection
));
569 GuiData
->Selection
.dwFlags
= CONSOLE_NO_SELECTION
;
570 RtlZeroMemory(&GuiData
->dwSelectionCursor
, sizeof(GuiData
->dwSelectionCursor
));
571 GuiData
->LineSelection
= FALSE
; // Default to block selection
572 // TODO: Retrieve the selection mode via the registry.
574 GuiData
->InputThreadId
= GuiInitInfo
->InputThreadId
;
575 GuiData
->WinSta
= GuiInitInfo
->WinSta
;
576 GuiData
->Desktop
= GuiInitInfo
->Desktop
;
578 /* Finally, finish to initialize the frontend structure */
579 This
->Context
= GuiData
;
580 ConsoleFreeHeap(This
->Context2
);
581 This
->Context2
= NULL
;
584 * We need to wait until the GUI has been fully initialized
585 * to retrieve custom settings i.e. WindowSize etc...
586 * Ideally we could use SendNotifyMessage for this but its not
589 NtCreateEvent(&GuiData
->hGuiInitEvent
, EVENT_ALL_ACCESS
,
590 NULL
, SynchronizationEvent
, FALSE
);
591 NtCreateEvent(&GuiData
->hGuiTermEvent
, EVENT_ALL_ACCESS
,
592 NULL
, SynchronizationEvent
, FALSE
);
594 DPRINT("GUI - Checkpoint\n");
596 /* Create the terminal window */
597 PostThreadMessageW(GuiData
->InputThreadId
, PM_CREATE_CONSOLE
, 0, (LPARAM
)GuiData
);
599 /* Wait until initialization has finished */
600 NtWaitForSingleObject(GuiData
->hGuiInitEvent
, FALSE
, NULL
);
601 DPRINT("OK we created the console window\n");
602 NtClose(GuiData
->hGuiInitEvent
);
603 GuiData
->hGuiInitEvent
= NULL
;
605 /* Check whether we really succeeded in initializing the terminal window */
606 if (GuiData
->hWindow
== NULL
)
608 DPRINT("GuiInitConsole - We failed at creating a new terminal window\n");
609 GuiDeinitFrontEnd(This
);
610 return STATUS_UNSUCCESSFUL
;
613 return STATUS_SUCCESS
;
617 GuiDeinitFrontEnd(IN OUT PFRONTEND This
)
619 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
621 DPRINT("Send PM_DESTROY_CONSOLE message and wait on hGuiTermEvent...\n");
622 PostThreadMessageW(GuiData
->InputThreadId
, PM_DESTROY_CONSOLE
, 0, (LPARAM
)GuiData
);
623 NtWaitForSingleObject(GuiData
->hGuiTermEvent
, FALSE
, NULL
);
624 DPRINT("hGuiTermEvent set\n");
625 NtClose(GuiData
->hGuiTermEvent
);
626 GuiData
->hGuiTermEvent
= NULL
;
628 CloseDesktop(GuiData
->Desktop
); // NtUserCloseDesktop
629 CloseWindowStation(GuiData
->WinSta
); // NtUserCloseWindowStation
631 DPRINT("Destroying icons !! - GuiData->hIcon = 0x%p ; ghDefaultIcon = 0x%p ; GuiData->hIconSm = 0x%p ; ghDefaultIconSm = 0x%p\n",
632 GuiData
->hIcon
, ghDefaultIcon
, GuiData
->hIconSm
, ghDefaultIconSm
);
633 if (GuiData
->hIcon
!= NULL
&& GuiData
->hIcon
!= ghDefaultIcon
)
635 DPRINT("Destroy hIcon\n");
636 DestroyIcon(GuiData
->hIcon
);
638 if (GuiData
->hIconSm
!= NULL
&& GuiData
->hIconSm
!= ghDefaultIconSm
)
640 DPRINT("Destroy hIconSm\n");
641 DestroyIcon(GuiData
->hIconSm
);
644 This
->Context
= NULL
;
645 DeleteCriticalSection(&GuiData
->Lock
);
646 ConsoleFreeHeap(GuiData
);
648 DPRINT("Quit GuiDeinitFrontEnd\n");
652 GuiDrawRegion(IN OUT PFRONTEND This
,
655 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
657 /* Do nothing if the window is hidden */
658 if (!GuiData
->IsWindowVisible
) return;
660 DrawRegion(GuiData
, Region
);
664 GuiWriteStream(IN OUT PFRONTEND This
,
672 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
673 PCONSOLE_SCREEN_BUFFER Buff
;
674 SHORT CursorEndX
, CursorEndY
;
677 if (NULL
== GuiData
|| NULL
== GuiData
->hWindow
) return;
679 /* Do nothing if the window is hidden */
680 if (!GuiData
->IsWindowVisible
) return;
682 Buff
= GuiData
->ActiveBuffer
;
683 if (GetType(Buff
) != TEXTMODE_BUFFER
) return;
685 if (0 != ScrolledLines
)
689 ScrollRect
.right
= Buff
->ViewSize
.X
* GuiData
->CharWidth
;
690 ScrollRect
.bottom
= Region
->Top
* GuiData
->CharHeight
;
692 ScrollWindowEx(GuiData
->hWindow
,
694 -(int)(ScrolledLines
* GuiData
->CharHeight
),
702 DrawRegion(GuiData
, Region
);
704 if (CursorStartX
< Region
->Left
|| Region
->Right
< CursorStartX
705 || CursorStartY
< Region
->Top
|| Region
->Bottom
< CursorStartY
)
707 InvalidateCell(GuiData
, CursorStartX
, CursorStartY
);
710 CursorEndX
= Buff
->CursorPosition
.X
;
711 CursorEndY
= Buff
->CursorPosition
.Y
;
712 if ((CursorEndX
< Region
->Left
|| Region
->Right
< CursorEndX
713 || CursorEndY
< Region
->Top
|| Region
->Bottom
< CursorEndY
)
714 && (CursorEndX
!= CursorStartX
|| CursorEndY
!= CursorStartY
))
716 InvalidateCell(GuiData
, CursorEndX
, CursorEndY
);
720 // Set up the update timer (very short interval) - this is a "hack" for getting the OS to
721 // repaint the window without having it just freeze up and stay on the screen permanently.
722 Buff
->CursorBlinkOn
= TRUE
;
723 SetTimer(GuiData
->hWindow
, CONGUI_UPDATE_TIMER
, CONGUI_UPDATE_TIME
, NULL
);
726 /* static */ VOID NTAPI
727 GuiRingBell(IN OUT PFRONTEND This
)
729 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
731 /* Emit an error beep sound */
732 SendNotifyMessage(GuiData
->hWindow
, PM_CONSOLE_BEEP
, 0, 0);
736 GuiSetCursorInfo(IN OUT PFRONTEND This
,
737 PCONSOLE_SCREEN_BUFFER Buff
)
739 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
741 /* Do nothing if the window is hidden */
742 if (!GuiData
->IsWindowVisible
) return TRUE
;
744 if (GuiData
->ActiveBuffer
== Buff
)
746 InvalidateCell(GuiData
, Buff
->CursorPosition
.X
, Buff
->CursorPosition
.Y
);
753 GuiSetScreenInfo(IN OUT PFRONTEND This
,
754 PCONSOLE_SCREEN_BUFFER Buff
,
758 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
760 /* Do nothing if the window is hidden */
761 if (!GuiData
->IsWindowVisible
) return TRUE
;
763 if (GuiData
->ActiveBuffer
== Buff
)
765 /* Redraw char at old position (remove cursor) */
766 InvalidateCell(GuiData
, OldCursorX
, OldCursorY
);
767 /* Redraw char at new position (show cursor) */
768 InvalidateCell(GuiData
, Buff
->CursorPosition
.X
, Buff
->CursorPosition
.Y
);
775 GuiResizeTerminal(IN OUT PFRONTEND This
)
777 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
779 /* Resize the window to the user's values */
780 PostMessageW(GuiData
->hWindow
, PM_RESIZE_TERMINAL
, 0, 0);
784 GuiSetActiveScreenBuffer(IN OUT PFRONTEND This
)
786 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
787 PCONSOLE_SCREEN_BUFFER ActiveBuffer
;
790 EnterCriticalSection(&GuiData
->Lock
);
791 GuiData
->WindowSizeLock
= TRUE
;
793 InterlockedExchangePointer((PVOID
*)&GuiData
->ActiveBuffer
,
794 ConDrvGetActiveScreenBuffer(GuiData
->Console
));
796 GuiData
->WindowSizeLock
= FALSE
;
797 LeaveCriticalSection(&GuiData
->Lock
);
799 ActiveBuffer
= GuiData
->ActiveBuffer
;
801 /* Change the current palette */
802 if (ActiveBuffer
->PaletteHandle
== NULL
)
803 hPalette
= GuiData
->hSysPalette
;
805 hPalette
= ActiveBuffer
->PaletteHandle
;
807 DPRINT("GuiSetActiveScreenBuffer using palette 0x%p\n", hPalette
);
809 /* Set the new palette for the framebuffer */
810 SelectPalette(GuiData
->hMemDC
, hPalette
, FALSE
);
812 /* Specify the use of the system palette for the framebuffer */
813 SetSystemPaletteUse(GuiData
->hMemDC
, ActiveBuffer
->PaletteUsage
);
815 /* Realize the (logical) palette */
816 RealizePalette(GuiData
->hMemDC
);
818 GuiResizeTerminal(This
);
819 // ConioDrawConsole(Console);
823 GuiReleaseScreenBuffer(IN OUT PFRONTEND This
,
824 IN PCONSOLE_SCREEN_BUFFER ScreenBuffer
)
826 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
829 * If we were notified to release a screen buffer that is not actually
830 * ours, then just ignore the notification...
832 if (ScreenBuffer
!= GuiData
->ActiveBuffer
) return;
835 * ... else, we must release our active buffer. Two cases are present:
836 * - If ScreenBuffer (== GuiData->ActiveBuffer) IS NOT the console
837 * active screen buffer, then we can safely switch to it.
838 * - If ScreenBuffer IS the console active screen buffer, we must release
842 /* Release the old active palette and set the default one */
843 if (GetCurrentObject(GuiData
->hMemDC
, OBJ_PAL
) == ScreenBuffer
->PaletteHandle
)
845 /* Set the new palette */
846 SelectPalette(GuiData
->hMemDC
, GuiData
->hSysPalette
, FALSE
);
849 /* Set the adequate active screen buffer */
850 if (ScreenBuffer
!= GuiData
->Console
->ActiveBuffer
)
852 GuiSetActiveScreenBuffer(This
);
856 EnterCriticalSection(&GuiData
->Lock
);
857 GuiData
->WindowSizeLock
= TRUE
;
859 InterlockedExchangePointer((PVOID
*)&GuiData
->ActiveBuffer
, NULL
);
861 GuiData
->WindowSizeLock
= FALSE
;
862 LeaveCriticalSection(&GuiData
->Lock
);
867 GuiSetMouseCursor(IN OUT PFRONTEND This
,
868 HCURSOR CursorHandle
);
871 GuiRefreshInternalInfo(IN OUT PFRONTEND This
)
873 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
875 /* Update the console leader information held by the window */
876 SetConWndConsoleLeaderCID(GuiData
);
880 * We reset the cursor here so that, when a console app quits, we reset
881 * the cursor to the default one. It's quite a hack since it doesn't proceed
882 * per - console process... This must be fixed.
884 * See GuiInitConsole(...) for more information.
887 /* Mouse is shown by default with its default cursor shape */
888 GuiData
->MouseCursorRefCount
= 0; // Reinitialize the reference counter
889 GuiSetMouseCursor(This
, NULL
);
893 GuiChangeTitle(IN OUT PFRONTEND This
)
895 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
896 // PostMessageW(GuiData->hWindow, PM_CONSOLE_SET_TITLE, 0, 0);
897 SetWindowTextW(GuiData
->hWindow
, GuiData
->Console
->Title
.Buffer
);
901 GuiChangeIcon(IN OUT PFRONTEND This
,
904 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
905 HICON hIcon
, hIconSm
;
907 if (IconHandle
== NULL
)
909 hIcon
= ghDefaultIcon
;
910 hIconSm
= ghDefaultIconSm
;
914 hIcon
= CopyIcon(IconHandle
);
915 hIconSm
= CopyIcon(IconHandle
);
921 if (hIcon
!= GuiData
->hIcon
)
923 if (GuiData
->hIcon
!= NULL
&& GuiData
->hIcon
!= ghDefaultIcon
)
925 DestroyIcon(GuiData
->hIcon
);
927 if (GuiData
->hIconSm
!= NULL
&& GuiData
->hIconSm
!= ghDefaultIconSm
)
929 DestroyIcon(GuiData
->hIconSm
);
932 GuiData
->hIcon
= hIcon
;
933 GuiData
->hIconSm
= hIconSm
;
935 DPRINT("Set icons in GuiChangeIcon\n");
936 PostMessageW(GuiData
->hWindow
, WM_SETICON
, ICON_BIG
, (LPARAM
)GuiData
->hIcon
);
937 PostMessageW(GuiData
->hWindow
, WM_SETICON
, ICON_SMALL
, (LPARAM
)GuiData
->hIconSm
);
944 GuiGetConsoleWindowHandle(IN OUT PFRONTEND This
)
946 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
947 return GuiData
->hWindow
;
951 GuiGetLargestConsoleWindowSize(IN OUT PFRONTEND This
,
954 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
955 PCONSOLE_SCREEN_BUFFER ActiveBuffer
;
957 MONITORINFO MonitorInfo
;
959 UINT WidthUnit
, HeightUnit
;
964 * Retrieve the monitor that is mostly covered by the current console window;
965 * default to primary monitor otherwise.
967 MonitorInfo
.cbSize
= sizeof(MonitorInfo
);
968 hMonitor
= MonitorFromWindow(GuiData
->hWindow
, MONITOR_DEFAULTTOPRIMARY
);
969 if (hMonitor
&& GetMonitorInfoW(hMonitor
, &MonitorInfo
))
971 /* Retrieve the width and height of the client area of this monitor */
972 Width
= MonitorInfo
.rcWork
.right
- MonitorInfo
.rcWork
.left
;
973 Height
= MonitorInfo
.rcWork
.bottom
- MonitorInfo
.rcWork
.top
;
978 * Retrieve the width and height of the client area for a full-screen
979 * window on the primary display monitor.
981 Width
= GetSystemMetrics(SM_CXFULLSCREEN
);
982 Height
= GetSystemMetrics(SM_CYFULLSCREEN
);
985 // SystemParametersInfoW(SPI_GETWORKAREA, 0, &WorkArea, 0);
986 // Width = WorkArea.right;
987 // Height = WorkArea.bottom;
990 ActiveBuffer
= GuiData
->ActiveBuffer
;
992 // NOTE: This would be surprising if we wouldn't have an associated buffer...
995 GetScreenBufferSizeUnits(ActiveBuffer
, GuiData
, &WidthUnit
, &HeightUnit
);
998 /* Default: graphics mode */
999 WidthUnit
= HeightUnit
= 1;
1002 Width
-= (2 * (GetSystemMetrics(SM_CXFRAME
) + GetSystemMetrics(SM_CXEDGE
)));
1003 Height
-= (2 * (GetSystemMetrics(SM_CYFRAME
) + GetSystemMetrics(SM_CYEDGE
)) + GetSystemMetrics(SM_CYCAPTION
));
1005 if (Width
< 0) Width
= 0;
1006 if (Height
< 0) Height
= 0;
1008 pSize
->X
= (SHORT
)(Width
/ (int)WidthUnit
) /* HACK */ + 2;
1009 pSize
->Y
= (SHORT
)(Height
/ (int)HeightUnit
) /* HACK */ + 1;
1013 GuiGetSelectionInfo(IN OUT PFRONTEND This
,
1014 PCONSOLE_SELECTION_INFO pSelectionInfo
)
1016 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1018 if (pSelectionInfo
== NULL
) return FALSE
;
1020 ZeroMemory(pSelectionInfo
, sizeof(*pSelectionInfo
));
1021 if (GuiData
->Selection
.dwFlags
!= CONSOLE_NO_SELECTION
)
1022 RtlCopyMemory(pSelectionInfo
, &GuiData
->Selection
, sizeof(*pSelectionInfo
));
1028 GuiSetPalette(IN OUT PFRONTEND This
,
1029 HPALETTE PaletteHandle
,
1032 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1033 HPALETTE OldPalette
;
1035 // if (GetType(GuiData->ActiveBuffer) != GRAPHICS_BUFFER) return FALSE;
1036 if (PaletteHandle
== NULL
) return FALSE
;
1038 /* Set the new palette for the framebuffer */
1039 OldPalette
= SelectPalette(GuiData
->hMemDC
, PaletteHandle
, FALSE
);
1040 if (OldPalette
== NULL
) return FALSE
;
1042 /* Specify the use of the system palette for the framebuffer */
1043 SetSystemPaletteUse(GuiData
->hMemDC
, PaletteUsage
);
1045 /* Realize the (logical) palette */
1046 RealizePalette(GuiData
->hMemDC
);
1048 /* Save the original system palette handle */
1049 if (GuiData
->hSysPalette
== NULL
) GuiData
->hSysPalette
= OldPalette
;
1055 GuiGetDisplayMode(IN OUT PFRONTEND This
)
1057 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1058 ULONG DisplayMode
= 0;
1060 if (GuiData
->GuiInfo
.FullScreen
)
1061 DisplayMode
|= CONSOLE_FULLSCREEN_HARDWARE
; // CONSOLE_FULLSCREEN
1063 DisplayMode
|= CONSOLE_WINDOWED
;
1069 GuiSetDisplayMode(IN OUT PFRONTEND This
,
1072 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1075 if (NewMode
& ~(CONSOLE_FULLSCREEN_MODE
| CONSOLE_WINDOWED_MODE
))
1078 /* Do nothing if the window is hidden */
1079 if (!GuiData
->IsWindowVisible
) return TRUE
;
1081 FullScreen
= ((NewMode
& CONSOLE_FULLSCREEN_MODE
) != 0);
1083 if (FullScreen
!= GuiData
->GuiInfo
.FullScreen
)
1085 SwitchFullScreen(GuiData
, FullScreen
);
1092 GuiShowMouseCursor(IN OUT PFRONTEND This
,
1095 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1097 if (GuiData
->IsWindowVisible
)
1099 /* Set the reference count */
1100 if (Show
) ++GuiData
->MouseCursorRefCount
;
1101 else --GuiData
->MouseCursorRefCount
;
1103 /* Effectively show (or hide) the cursor (use special values for (w|l)Param) */
1104 PostMessageW(GuiData
->hWindow
, WM_SETCURSOR
, -1, -1);
1107 return GuiData
->MouseCursorRefCount
;
1111 GuiSetMouseCursor(IN OUT PFRONTEND This
,
1112 HCURSOR CursorHandle
)
1114 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1116 /* Do nothing if the window is hidden */
1117 if (!GuiData
->IsWindowVisible
) return TRUE
;
1120 * Set the cursor's handle. If the given handle is NULL,
1121 * then restore the default cursor.
1123 GuiData
->hCursor
= (CursorHandle
? CursorHandle
: ghDefaultCursor
);
1125 /* Effectively modify the shape of the cursor (use special values for (w|l)Param) */
1126 PostMessageW(GuiData
->hWindow
, WM_SETCURSOR
, -1, -1);
1132 GuiMenuControl(IN OUT PFRONTEND This
,
1136 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1138 GuiData
->CmdIdLow
= CmdIdLow
;
1139 GuiData
->CmdIdHigh
= CmdIdHigh
;
1141 return GetSystemMenu(GuiData
->hWindow
, FALSE
);
1145 GuiSetMenuClose(IN OUT PFRONTEND This
,
1149 * NOTE: See http://www.mail-archive.com/harbour@harbour-project.org/msg27509.html
1150 * or http://harbour-devel.1590103.n2.nabble.com/Question-about-hb-gt-win-CtrlHandler-usage-td4670862i20.html
1151 * for more information.
1154 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1155 HMENU hSysMenu
= GetSystemMenu(GuiData
->hWindow
, FALSE
);
1157 if (hSysMenu
== NULL
) return FALSE
;
1159 GuiData
->IsCloseButtonEnabled
= Enable
;
1160 EnableMenuItem(hSysMenu
, SC_CLOSE
, MF_BYCOMMAND
| (Enable
? MF_ENABLED
: MF_GRAYED
));
1165 static FRONTEND_VTBL GuiVtbl
=
1175 GuiSetActiveScreenBuffer
,
1176 GuiReleaseScreenBuffer
,
1177 GuiRefreshInternalInfo
,
1180 GuiGetConsoleWindowHandle
,
1181 GuiGetLargestConsoleWindowSize
,
1182 GuiGetSelectionInfo
,
1194 GuiLoadFrontEnd(IN OUT PFRONTEND FrontEnd
,
1195 IN OUT PCONSOLE_STATE_INFO ConsoleInfo
,
1196 IN OUT PCONSOLE_INIT_INFO ConsoleInitInfo
,
1197 IN HANDLE ConsoleLeaderProcessHandle
)
1199 PCONSOLE_START_INFO ConsoleStartInfo
;
1200 PGUI_INIT_INFO GuiInitInfo
;
1202 if (FrontEnd
== NULL
|| ConsoleInfo
== NULL
|| ConsoleInitInfo
== NULL
)
1203 return STATUS_INVALID_PARAMETER
;
1205 ConsoleStartInfo
= ConsoleInitInfo
->ConsoleStartInfo
;
1208 * Initialize a private initialization info structure for later use.
1209 * It must be freed by a call to GuiUnloadFrontEnd or GuiInitFrontEnd.
1211 GuiInitInfo
= ConsoleAllocHeap(HEAP_ZERO_MEMORY
, sizeof(*GuiInitInfo
));
1212 if (GuiInitInfo
== NULL
) return STATUS_NO_MEMORY
;
1214 /* Initialize GUI terminal emulator common functionalities */
1215 if (!GuiInit(ConsoleInitInfo
, ConsoleLeaderProcessHandle
, GuiInitInfo
))
1217 ConsoleFreeHeap(GuiInitInfo
);
1218 return STATUS_UNSUCCESSFUL
;
1222 * Load terminal settings
1225 /* Impersonate the caller in order to retrieve settings in its context */
1226 // if (!CsrImpersonateClient(NULL))
1227 // return STATUS_UNSUCCESSFUL;
1228 CsrImpersonateClient(NULL
);
1230 /* 1. Load the default settings */
1231 GuiConsoleGetDefaultSettings(&GuiInitInfo
->TermInfo
);
1234 GuiInitInfo
->TermInfo
.ShowWindow
= SW_SHOWNORMAL
;
1236 if (ConsoleInitInfo
->IsWindowVisible
)
1238 /* 2. Load the remaining console settings via the registry */
1239 if ((ConsoleStartInfo
->dwStartupFlags
& STARTF_TITLEISLINKNAME
) == 0)
1242 /* Load the terminal infos from the registry */
1243 GuiConsoleReadUserSettings(&GuiInitInfo
->TermInfo
);
1247 * Now, update them with the properties the user might gave to us
1248 * via the STARTUPINFO structure before calling CreateProcess
1249 * (and which was transmitted via the ConsoleStartInfo structure).
1250 * We therefore overwrite the values read in the registry.
1252 if (ConsoleStartInfo
->dwStartupFlags
& STARTF_USESHOWWINDOW
)
1254 GuiInitInfo
->TermInfo
.ShowWindow
= ConsoleStartInfo
->wShowWindow
;
1256 if (ConsoleStartInfo
->dwStartupFlags
& STARTF_USEPOSITION
)
1258 ConsoleInfo
->AutoPosition
= FALSE
;
1259 ConsoleInfo
->WindowPosition
.x
= ConsoleStartInfo
->dwWindowOrigin
.X
;
1260 ConsoleInfo
->WindowPosition
.y
= ConsoleStartInfo
->dwWindowOrigin
.Y
;
1262 if (ConsoleStartInfo
->dwStartupFlags
& STARTF_RUNFULLSCREEN
)
1264 ConsoleInfo
->FullScreen
= TRUE
;
1270 /* Revert impersonation */
1275 wcsncpy(GuiInitInfo
->TermInfo
.FaceName
, ConsoleInfo
->FaceName
, LF_FACESIZE
);
1276 GuiInitInfo
->TermInfo
.FaceName
[LF_FACESIZE
- 1] = UNICODE_NULL
;
1277 GuiInitInfo
->TermInfo
.FontFamily
= ConsoleInfo
->FontFamily
;
1278 GuiInitInfo
->TermInfo
.FontSize
= ConsoleInfo
->FontSize
;
1279 GuiInitInfo
->TermInfo
.FontWeight
= ConsoleInfo
->FontWeight
;
1282 GuiInitInfo
->TermInfo
.FullScreen
= ConsoleInfo
->FullScreen
;
1283 // GuiInitInfo->TermInfo.ShowWindow;
1284 GuiInitInfo
->TermInfo
.AutoPosition
= ConsoleInfo
->AutoPosition
;
1285 GuiInitInfo
->TermInfo
.WindowOrigin
= ConsoleInfo
->WindowPosition
;
1287 /* Initialize the icon handles */
1288 // if (ConsoleStartInfo->hIcon != NULL)
1289 GuiInitInfo
->hIcon
= ConsoleStartInfo
->hIcon
;
1291 // GuiInitInfo->hIcon = ghDefaultIcon;
1293 // if (ConsoleStartInfo->hIconSm != NULL)
1294 GuiInitInfo
->hIconSm
= ConsoleStartInfo
->hIconSm
;
1296 // GuiInitInfo->hIconSm = ghDefaultIconSm;
1298 // ASSERT(GuiInitInfo->hIcon && GuiInitInfo->hIconSm);
1300 GuiInitInfo
->IsWindowVisible
= ConsoleInitInfo
->IsWindowVisible
;
1302 /* Finally, initialize the frontend structure */
1303 FrontEnd
->Vtbl
= &GuiVtbl
;
1304 FrontEnd
->Context
= NULL
;
1305 FrontEnd
->Context2
= GuiInitInfo
;
1307 return STATUS_SUCCESS
;
1311 GuiUnloadFrontEnd(IN OUT PFRONTEND FrontEnd
)
1313 if (FrontEnd
== NULL
) return STATUS_INVALID_PARAMETER
;
1315 if (FrontEnd
->Context
) GuiDeinitFrontEnd(FrontEnd
);
1316 if (FrontEnd
->Context2
) ConsoleFreeHeap(FrontEnd
->Context2
);
1318 return STATUS_SUCCESS
;