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
);
151 LONG WindowCount
= 0;
155 * This thread dispatches all the console notifications to the
156 * notification window. It is common for all the console windows
157 * in a given desktop in a window station.
160 /* Assign this console input thread to this desktop */
161 DesktopConsoleThreadInfo
.DesktopHandle
= GuiInitInfo
->Desktop
; // Duplicated desktop handle
162 DesktopConsoleThreadInfo
.ThreadId
= InputThreadId
;
163 Status
= NtUserConsoleControl(ConsoleCtrlDesktopConsoleThread
,
164 &DesktopConsoleThreadInfo
,
165 sizeof(DesktopConsoleThreadInfo
));
166 if (!NT_SUCCESS(Status
)) goto Quit
;
168 /* Connect this CSR thread to the USER subsystem */
169 pcsrt
= CsrConnectToUser();
170 if (pcsrt
== NULL
) goto Quit
;
172 /* Assign the desktop to this thread */
173 if (!SetThreadDesktop(DesktopConsoleThreadInfo
.DesktopHandle
)) goto Quit
;
175 /* The thread has been initialized, set the event */
176 NtSetEvent(GuiInitInfo
->GuiThreadStartupEvent
, NULL
);
177 Status
= STATUS_SUCCESS
;
179 while (GetMessageW(&msg
, NULL
, 0, 0))
183 case PM_CREATE_CONSOLE
:
185 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
)msg
.lParam
;
186 PCONSRV_CONSOLE Console
= GuiData
->Console
;
190 DPRINT("PM_CREATE_CONSOLE -- creating window\n");
192 NewWindow
= CreateWindowExW(WS_EX_CLIENTEDGE
,
194 Console
->Title
.Buffer
,
195 WS_OVERLAPPEDWINDOW
| WS_HSCROLL
| WS_VSCROLL
,
200 GuiData
->IsWindowVisible
? HWND_DESKTOP
: HWND_MESSAGE
,
204 if (NewWindow
== NULL
)
206 DPRINT1("Failed to create a new console window\n");
210 ASSERT(NewWindow
== GuiData
->hWindow
);
212 InterlockedIncrement(&WindowCount
);
215 // FIXME: TODO: Move everything there into conwnd.c!OnNcCreate()
218 /* Retrieve our real position */
219 // See conwnd.c!OnMove()
220 GetWindowRect(GuiData
->hWindow
, &rcWnd
);
221 GuiData
->GuiInfo
.WindowOrigin
.x
= rcWnd
.left
;
222 GuiData
->GuiInfo
.WindowOrigin
.y
= rcWnd
.top
;
224 if (GuiData
->IsWindowVisible
)
226 /* Move and resize the window to the user's values */
227 /* CAN WE DEADLOCK ?? */
228 GuiConsoleMoveWindow(GuiData
); // FIXME: This MUST be done via the CreateWindowExW call.
229 SendMessageW(GuiData
->hWindow
, PM_RESIZE_TERMINAL
, 0, 0);
232 // FIXME: HACK: Potential HACK for CORE-8129; see revision 63595.
233 CreateSysMenu(GuiData
->hWindow
);
235 if (GuiData
->IsWindowVisible
)
237 /* Switch to full-screen mode if necessary */
238 // FIXME: Move elsewhere, it cause misdrawings of the window.
239 if (GuiData
->GuiInfo
.FullScreen
) SwitchFullScreen(GuiData
, TRUE
);
241 DPRINT("PM_CREATE_CONSOLE -- showing window\n");
242 // ShowWindow(NewWindow, (int)GuiData->GuiInfo.ShowWindow);
243 ShowWindowAsync(NewWindow
, (int)GuiData
->GuiInfo
.ShowWindow
);
244 DPRINT("Window showed\n");
248 DPRINT("PM_CREATE_CONSOLE -- hidden window\n");
249 ShowWindowAsync(NewWindow
, SW_HIDE
);
255 case PM_DESTROY_CONSOLE
:
257 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
)msg
.lParam
;
260 /* Exit the full screen mode if it was already set */
261 // LeaveFullScreen(GuiData);
264 * Window creation is done using a PostMessage(), so it's possible
265 * that the window that we want to destroy doesn't exist yet.
266 * So first empty the message queue.
269 while (PeekMessageW(&TempMsg, NULL, 0, 0, PM_REMOVE))
271 TranslateMessage(&TempMsg);
272 DispatchMessageW(&TempMsg);
274 while (PeekMessageW(&TempMsg
, NULL
, 0, 0, PM_REMOVE
)) ;
276 if (GuiData
->hWindow
== NULL
) continue;
278 DestroyWindow(GuiData
->hWindow
);
280 NtSetEvent(GuiData
->hGuiTermEvent
, NULL
);
282 if (InterlockedDecrement(&WindowCount
) == 0)
284 DPRINT("CONSRV: Going to quit the Input Thread 0x%p\n", InputThreadId
);
292 TranslateMessage(&msg
);
293 DispatchMessageW(&msg
);
297 DPRINT("CONSRV: Quit the Input Thread 0x%p, Status = 0x%08lx\n", InputThreadId
, Status
);
299 /* Remove this console input thread from this desktop */
300 // DesktopConsoleThreadInfo.DesktopHandle;
301 DesktopConsoleThreadInfo
.ThreadId
= 0;
302 NtUserConsoleControl(ConsoleCtrlDesktopConsoleThread
,
303 &DesktopConsoleThreadInfo
,
304 sizeof(DesktopConsoleThreadInfo
));
306 /* Close the duplicated desktop handle */
307 CloseDesktop(DesktopConsoleThreadInfo
.DesktopHandle
); // NtUserCloseDesktop
309 /* Cleanup CSR thread */
310 if (pcsrt
) CsrDereferenceThread(pcsrt
);
312 /* Exit the thread */
313 RtlExitUserThread(Status
);
317 // FIXME: Maybe return a NTSTATUS
319 GuiInit(IN PCONSOLE_INIT_INFO ConsoleInitInfo
,
320 IN HANDLE ConsoleLeaderProcessHandle
,
321 IN OUT PGUI_INIT_INFO GuiInitInfo
)
324 UNICODE_STRING DesktopPath
;
325 DESKTOP_CONSOLE_THREAD DesktopConsoleThreadInfo
;
334 * Initialize and register the console window class, if needed.
336 if (!ConsInitialized
)
338 if (!RegisterConWndClass(ConSrvDllInstance
)) return FALSE
;
339 ConsInitialized
= TRUE
;
343 * Set-up the console input thread. We have
344 * one console input thread per desktop.
347 if (!CsrImpersonateClient(NULL
))
348 // return STATUS_BAD_IMPERSONATION_LEVEL;
351 if (ConsoleInitInfo
->DesktopLength
)
353 DesktopPath
.MaximumLength
= ConsoleInitInfo
->DesktopLength
;
354 DesktopPath
.Length
= DesktopPath
.MaximumLength
- sizeof(UNICODE_NULL
);
355 DesktopPath
.Buffer
= ConsoleInitInfo
->Desktop
;
359 RtlInitUnicodeString(&DesktopPath
, L
"Default");
362 hDesk
= NtUserResolveDesktop(ConsoleLeaderProcessHandle
,
366 DPRINT("NtUserResolveDesktop(DesktopPath = '%wZ') returned hDesk = 0x%p; hWinSta = 0x%p\n",
367 &DesktopPath
, hDesk
, hWinSta
);
371 if (hDesk
== NULL
) return FALSE
;
374 * We need to see whether we need to create a
375 * new console input thread for this desktop.
377 DesktopConsoleThreadInfo
.DesktopHandle
= hDesk
;
378 DesktopConsoleThreadInfo
.ThreadId
= (ULONG_PTR
)INVALID_HANDLE_VALUE
; // Special value to say we just want to retrieve the thread ID.
379 NtUserConsoleControl(ConsoleCtrlDesktopConsoleThread
,
380 &DesktopConsoleThreadInfo
,
381 sizeof(DesktopConsoleThreadInfo
));
382 DPRINT("NtUserConsoleControl returned ThreadId = 0x%p\n", DesktopConsoleThreadInfo
.ThreadId
);
385 * Save the opened window station and desktop handles in the initialization
386 * structure. They will be used later on, and released, by the GUI frontend.
388 GuiInitInfo
->WinSta
= hWinSta
;
389 GuiInitInfo
->Desktop
= hDesk
;
391 /* Here GuiInitInfo contains original handles */
393 /* If we already have a console input thread on this desktop... */
394 if (DesktopConsoleThreadInfo
.ThreadId
!= 0)
396 /* ... just use it... */
397 DPRINT("Using input thread InputThreadId = 0x%p\n", DesktopConsoleThreadInfo
.ThreadId
);
398 GuiInitInfo
->InputThreadId
= DesktopConsoleThreadInfo
.ThreadId
;
402 /* ... otherwise create a new one. */
404 /* Initialize a startup event for the thread to signal it */
405 Status
= NtCreateEvent(&GuiInitInfo
->GuiThreadStartupEvent
, EVENT_ALL_ACCESS
,
406 NULL
, SynchronizationEvent
, FALSE
);
407 if (!NT_SUCCESS(Status
))
414 * Duplicate the desktop handle for the console input thread internal needs.
415 * If it happens to need also a window station handle in the future, then
416 * it is there that you also need to duplicate the window station handle!
418 * Note also that we are going to temporarily overwrite the stored handles
419 * in GuiInitInfo because it happens that we use also this structure to give
420 * the duplicated handles to the input thread that is going to initialize.
421 * After the input thread finishes its initialization, we restore the handles
422 * in GuiInitInfo to their old values.
424 Status
= NtDuplicateObject(NtCurrentProcess(),
427 (PHANDLE
)&GuiInitInfo
->Desktop
,
428 0, 0, DUPLICATE_SAME_ACCESS
);
429 if (!NT_SUCCESS(Status
))
435 /* Here GuiInitInfo contains duplicated handles */
437 Status
= RtlCreateUserThread(NtCurrentProcess(),
439 TRUE
, // Start the thread in suspended state
443 (PVOID
)GuiConsoleInputThread
,
447 if (NT_SUCCESS(Status
))
449 /* Add it as a static server thread and resume it */
450 CsrAddStaticServerThread(hInputThread
, &ClientId
, 0);
451 Status
= NtResumeThread(hInputThread
, NULL
);
453 DPRINT("Thread creation hInputThread = 0x%p, InputThreadId = 0x%p, Status = 0x%08lx\n",
454 hInputThread
, ClientId
.UniqueThread
, Status
);
456 if (!NT_SUCCESS(Status
) || hInputThread
== NULL
)
458 /* Close the thread's handle */
459 if (hInputThread
) NtClose(hInputThread
);
461 /* We need to close here the duplicated desktop handle */
462 CloseDesktop(GuiInitInfo
->Desktop
); // NtUserCloseDesktop
464 /* Close the startup event and bail out */
465 NtClose(GuiInitInfo
->GuiThreadStartupEvent
);
467 DPRINT1("CONSRV: Failed to create graphics console thread.\n");
472 /* No need to close hInputThread, this is done by CSR automatically */
474 /* Wait for the thread to finish its initialization, and close the startup event */
475 NtWaitForSingleObject(GuiInitInfo
->GuiThreadStartupEvent
, FALSE
, NULL
);
476 NtClose(GuiInitInfo
->GuiThreadStartupEvent
);
479 * Save the input thread ID for later use, and restore the original handles.
480 * The copies are held by the console input thread.
482 GuiInitInfo
->InputThreadId
= (ULONG_PTR
)ClientId
.UniqueThread
;
483 GuiInitInfo
->WinSta
= hWinSta
;
484 GuiInitInfo
->Desktop
= hDesk
;
486 /* Here GuiInitInfo contains again original handles */
492 * Close the original handles. Do not use the copies in GuiInitInfo
493 * because we may have failed in the middle of the duplicate operation
494 * and the handles stored in GuiInitInfo may have changed.
496 CloseDesktop(hDesk
); // NtUserCloseDesktop
497 CloseWindowStation(hWinSta
); // NtUserCloseWindowStation
504 /******************************************************************************
505 * GUI Console Driver *
506 ******************************************************************************/
509 GuiDeinitFrontEnd(IN OUT PFRONTEND This
);
511 static NTSTATUS NTAPI
512 GuiInitFrontEnd(IN OUT PFRONTEND This
,
513 IN PCONSRV_CONSOLE Console
)
515 PGUI_INIT_INFO GuiInitInfo
;
516 PGUI_CONSOLE_DATA GuiData
;
518 if (This
== NULL
|| Console
== NULL
|| This
->Context2
== NULL
)
519 return STATUS_INVALID_PARAMETER
;
521 ASSERT(This
->Console
== Console
);
523 GuiInitInfo
= This
->Context2
;
525 /* Terminal data allocation */
526 GuiData
= ConsoleAllocHeap(HEAP_ZERO_MEMORY
, sizeof(*GuiData
));
529 DPRINT1("CONSRV: Failed to create GUI_CONSOLE_DATA\n");
530 return STATUS_UNSUCCESSFUL
;
532 /// /* HACK */ Console->FrontEndIFace.Context = (PVOID)GuiData; /* HACK */
533 GuiData
->Console
= Console
;
534 GuiData
->ActiveBuffer
= Console
->ActiveBuffer
;
535 GuiData
->hWindow
= NULL
;
536 GuiData
->IsWindowVisible
= GuiInitInfo
->IsWindowVisible
;
538 /* The console can be resized */
539 Console
->FixedSize
= FALSE
;
541 InitializeCriticalSection(&GuiData
->Lock
);
546 RtlCopyMemory(&GuiData
->GuiInfo
, &GuiInitInfo
->TermInfo
, sizeof(GuiInitInfo
->TermInfo
));
548 /* Initialize the icon handles */
549 if (GuiInitInfo
->hIcon
!= NULL
)
550 GuiData
->hIcon
= GuiInitInfo
->hIcon
;
552 GuiData
->hIcon
= ghDefaultIcon
;
554 if (GuiInitInfo
->hIconSm
!= NULL
)
555 GuiData
->hIconSm
= GuiInitInfo
->hIconSm
;
557 GuiData
->hIconSm
= ghDefaultIconSm
;
559 ASSERT(GuiData
->hIcon
&& GuiData
->hIconSm
);
561 /* Mouse is shown by default with its default cursor shape */
562 GuiData
->hCursor
= ghDefaultCursor
;
563 GuiData
->MouseCursorRefCount
= 0;
565 /* A priori don't ignore mouse signals */
566 GuiData
->IgnoreNextMouseSignal
= FALSE
;
567 /* Initialize HACK FOR CORE-8394. See conwnd.c!OnMouse for more details. */
568 GuiData
->HackCORE8394IgnoreNextMove
= FALSE
;
570 /* Close button and the corresponding system menu item are enabled by default */
571 GuiData
->IsCloseButtonEnabled
= TRUE
;
573 /* There is no user-reserved menu id range by default */
574 GuiData
->CmdIdLow
= GuiData
->CmdIdHigh
= 0;
576 /* Initialize the selection */
577 RtlZeroMemory(&GuiData
->Selection
, sizeof(GuiData
->Selection
));
578 GuiData
->Selection
.dwFlags
= CONSOLE_NO_SELECTION
;
579 RtlZeroMemory(&GuiData
->dwSelectionCursor
, sizeof(GuiData
->dwSelectionCursor
));
580 GuiData
->LineSelection
= FALSE
; // Default to block selection
581 // TODO: Retrieve the selection mode via the registry.
583 GuiData
->InputThreadId
= GuiInitInfo
->InputThreadId
;
584 GuiData
->WinSta
= GuiInitInfo
->WinSta
;
585 GuiData
->Desktop
= GuiInitInfo
->Desktop
;
587 /* Finally, finish to initialize the frontend structure */
588 This
->Context
= GuiData
;
589 ConsoleFreeHeap(This
->Context2
);
590 This
->Context2
= NULL
;
593 * We need to wait until the GUI has been fully initialized
594 * to retrieve custom settings i.e. WindowSize etc...
595 * Ideally we could use SendNotifyMessage for this but its not
598 NtCreateEvent(&GuiData
->hGuiInitEvent
, EVENT_ALL_ACCESS
,
599 NULL
, SynchronizationEvent
, FALSE
);
600 NtCreateEvent(&GuiData
->hGuiTermEvent
, EVENT_ALL_ACCESS
,
601 NULL
, SynchronizationEvent
, FALSE
);
603 DPRINT("GUI - Checkpoint\n");
605 /* Create the terminal window */
606 PostThreadMessageW(GuiData
->InputThreadId
, PM_CREATE_CONSOLE
, 0, (LPARAM
)GuiData
);
608 /* Wait until initialization has finished */
609 NtWaitForSingleObject(GuiData
->hGuiInitEvent
, FALSE
, NULL
);
610 DPRINT("OK we created the console window\n");
611 NtClose(GuiData
->hGuiInitEvent
);
612 GuiData
->hGuiInitEvent
= NULL
;
614 /* Check whether we really succeeded in initializing the terminal window */
615 if (GuiData
->hWindow
== NULL
)
617 DPRINT("GuiInitConsole - We failed at creating a new terminal window\n");
618 GuiDeinitFrontEnd(This
);
619 return STATUS_UNSUCCESSFUL
;
622 return STATUS_SUCCESS
;
626 GuiDeinitFrontEnd(IN OUT PFRONTEND This
)
628 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
630 DPRINT("Send PM_DESTROY_CONSOLE message and wait on hGuiTermEvent...\n");
631 PostThreadMessageW(GuiData
->InputThreadId
, PM_DESTROY_CONSOLE
, 0, (LPARAM
)GuiData
);
632 NtWaitForSingleObject(GuiData
->hGuiTermEvent
, FALSE
, NULL
);
633 DPRINT("hGuiTermEvent set\n");
634 NtClose(GuiData
->hGuiTermEvent
);
635 GuiData
->hGuiTermEvent
= NULL
;
637 CloseDesktop(GuiData
->Desktop
); // NtUserCloseDesktop
638 CloseWindowStation(GuiData
->WinSta
); // NtUserCloseWindowStation
640 DPRINT("Destroying icons !! - GuiData->hIcon = 0x%p ; ghDefaultIcon = 0x%p ; GuiData->hIconSm = 0x%p ; ghDefaultIconSm = 0x%p\n",
641 GuiData
->hIcon
, ghDefaultIcon
, GuiData
->hIconSm
, ghDefaultIconSm
);
642 if (GuiData
->hIcon
!= NULL
&& GuiData
->hIcon
!= ghDefaultIcon
)
644 DPRINT("Destroy hIcon\n");
645 DestroyIcon(GuiData
->hIcon
);
647 if (GuiData
->hIconSm
!= NULL
&& GuiData
->hIconSm
!= ghDefaultIconSm
)
649 DPRINT("Destroy hIconSm\n");
650 DestroyIcon(GuiData
->hIconSm
);
653 This
->Context
= NULL
;
654 DeleteCriticalSection(&GuiData
->Lock
);
655 ConsoleFreeHeap(GuiData
);
657 DPRINT("Quit GuiDeinitFrontEnd\n");
661 GuiDrawRegion(IN OUT PFRONTEND This
,
664 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
666 /* Do nothing if the window is hidden */
667 if (!GuiData
->IsWindowVisible
) return;
669 DrawRegion(GuiData
, Region
);
673 GuiWriteStream(IN OUT PFRONTEND This
,
681 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
682 PCONSOLE_SCREEN_BUFFER Buff
;
683 SHORT CursorEndX
, CursorEndY
;
686 if (NULL
== GuiData
|| NULL
== GuiData
->hWindow
) return;
688 /* Do nothing if the window is hidden */
689 if (!GuiData
->IsWindowVisible
) return;
691 Buff
= GuiData
->ActiveBuffer
;
692 if (GetType(Buff
) != TEXTMODE_BUFFER
) return;
694 if (0 != ScrolledLines
)
698 ScrollRect
.right
= Buff
->ViewSize
.X
* GuiData
->CharWidth
;
699 ScrollRect
.bottom
= Region
->Top
* GuiData
->CharHeight
;
701 ScrollWindowEx(GuiData
->hWindow
,
703 -(int)(ScrolledLines
* GuiData
->CharHeight
),
711 DrawRegion(GuiData
, Region
);
713 if (CursorStartX
< Region
->Left
|| Region
->Right
< CursorStartX
714 || CursorStartY
< Region
->Top
|| Region
->Bottom
< CursorStartY
)
716 InvalidateCell(GuiData
, CursorStartX
, CursorStartY
);
719 CursorEndX
= Buff
->CursorPosition
.X
;
720 CursorEndY
= Buff
->CursorPosition
.Y
;
721 if ((CursorEndX
< Region
->Left
|| Region
->Right
< CursorEndX
722 || CursorEndY
< Region
->Top
|| Region
->Bottom
< CursorEndY
)
723 && (CursorEndX
!= CursorStartX
|| CursorEndY
!= CursorStartY
))
725 InvalidateCell(GuiData
, CursorEndX
, CursorEndY
);
729 // Set up the update timer (very short interval) - this is a "hack" for getting the OS to
730 // repaint the window without having it just freeze up and stay on the screen permanently.
731 Buff
->CursorBlinkOn
= TRUE
;
732 SetTimer(GuiData
->hWindow
, CONGUI_UPDATE_TIMER
, CONGUI_UPDATE_TIME
, NULL
);
735 /* static */ VOID NTAPI
736 GuiRingBell(IN OUT PFRONTEND This
)
738 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
740 /* Emit an error beep sound */
741 SendNotifyMessage(GuiData
->hWindow
, PM_CONSOLE_BEEP
, 0, 0);
745 GuiSetCursorInfo(IN OUT PFRONTEND This
,
746 PCONSOLE_SCREEN_BUFFER Buff
)
748 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
750 /* Do nothing if the window is hidden */
751 if (!GuiData
->IsWindowVisible
) return TRUE
;
753 if (GuiData
->ActiveBuffer
== Buff
)
755 InvalidateCell(GuiData
, Buff
->CursorPosition
.X
, Buff
->CursorPosition
.Y
);
762 GuiSetScreenInfo(IN OUT PFRONTEND This
,
763 PCONSOLE_SCREEN_BUFFER Buff
,
767 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
769 /* Do nothing if the window is hidden */
770 if (!GuiData
->IsWindowVisible
) return TRUE
;
772 if (GuiData
->ActiveBuffer
== Buff
)
774 /* Redraw char at old position (remove cursor) */
775 InvalidateCell(GuiData
, OldCursorX
, OldCursorY
);
776 /* Redraw char at new position (show cursor) */
777 InvalidateCell(GuiData
, Buff
->CursorPosition
.X
, Buff
->CursorPosition
.Y
);
784 GuiResizeTerminal(IN OUT PFRONTEND This
)
786 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
788 /* Resize the window to the user's values */
789 PostMessageW(GuiData
->hWindow
, PM_RESIZE_TERMINAL
, 0, 0);
793 GuiSetActiveScreenBuffer(IN OUT PFRONTEND This
)
795 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
796 PCONSOLE_SCREEN_BUFFER ActiveBuffer
;
799 EnterCriticalSection(&GuiData
->Lock
);
800 GuiData
->WindowSizeLock
= TRUE
;
802 InterlockedExchangePointer((PVOID
*)&GuiData
->ActiveBuffer
,
803 ConDrvGetActiveScreenBuffer(GuiData
->Console
));
805 GuiData
->WindowSizeLock
= FALSE
;
806 LeaveCriticalSection(&GuiData
->Lock
);
808 ActiveBuffer
= GuiData
->ActiveBuffer
;
810 /* Change the current palette */
811 if (ActiveBuffer
->PaletteHandle
== NULL
)
813 hPalette
= GuiData
->hSysPalette
;
817 hPalette
= ActiveBuffer
->PaletteHandle
;
820 DPRINT("GuiSetActiveScreenBuffer using palette 0x%p\n", hPalette
);
822 /* Set the new palette for the framebuffer */
823 SelectPalette(GuiData
->hMemDC
, hPalette
, FALSE
);
825 /* Specify the use of the system palette for the framebuffer */
826 SetSystemPaletteUse(GuiData
->hMemDC
, ActiveBuffer
->PaletteUsage
);
828 /* Realize the (logical) palette */
829 RealizePalette(GuiData
->hMemDC
);
831 GuiResizeTerminal(This
);
832 // ConioDrawConsole(Console);
836 GuiReleaseScreenBuffer(IN OUT PFRONTEND This
,
837 IN PCONSOLE_SCREEN_BUFFER ScreenBuffer
)
839 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
842 * If we were notified to release a screen buffer that is not actually
843 * ours, then just ignore the notification...
845 if (ScreenBuffer
!= GuiData
->ActiveBuffer
) return;
848 * ... else, we must release our active buffer. Two cases are present:
849 * - If ScreenBuffer (== GuiData->ActiveBuffer) IS NOT the console
850 * active screen buffer, then we can safely switch to it.
851 * - If ScreenBuffer IS the console active screen buffer, we must release
855 /* Release the old active palette and set the default one */
856 if (GetCurrentObject(GuiData
->hMemDC
, OBJ_PAL
) == ScreenBuffer
->PaletteHandle
)
858 /* Set the new palette */
859 SelectPalette(GuiData
->hMemDC
, GuiData
->hSysPalette
, FALSE
);
862 /* Set the adequate active screen buffer */
863 if (ScreenBuffer
!= GuiData
->Console
->ActiveBuffer
)
865 GuiSetActiveScreenBuffer(This
);
869 EnterCriticalSection(&GuiData
->Lock
);
870 GuiData
->WindowSizeLock
= TRUE
;
872 InterlockedExchangePointer((PVOID
*)&GuiData
->ActiveBuffer
, NULL
);
874 GuiData
->WindowSizeLock
= FALSE
;
875 LeaveCriticalSection(&GuiData
->Lock
);
880 GuiSetMouseCursor(IN OUT PFRONTEND This
,
881 HCURSOR CursorHandle
);
884 GuiRefreshInternalInfo(IN OUT PFRONTEND This
)
886 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
888 /* Update the console leader information held by the window */
889 SetConWndConsoleLeaderCID(GuiData
);
893 * We reset the cursor here so that, when a console app quits, we reset
894 * the cursor to the default one. It's quite a hack since it doesn't proceed
895 * per - console process... This must be fixed.
897 * See GuiInitConsole(...) for more information.
900 /* Mouse is shown by default with its default cursor shape */
901 GuiData
->MouseCursorRefCount
= 0; // Reinitialize the reference counter
902 GuiSetMouseCursor(This
, NULL
);
906 GuiChangeTitle(IN OUT PFRONTEND This
)
908 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
909 // PostMessageW(GuiData->hWindow, PM_CONSOLE_SET_TITLE, 0, 0);
910 SetWindowTextW(GuiData
->hWindow
, GuiData
->Console
->Title
.Buffer
);
914 GuiChangeIcon(IN OUT PFRONTEND This
,
917 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
918 HICON hIcon
, hIconSm
;
920 if (IconHandle
== NULL
)
922 hIcon
= ghDefaultIcon
;
923 hIconSm
= ghDefaultIconSm
;
927 hIcon
= CopyIcon(IconHandle
);
928 hIconSm
= CopyIcon(IconHandle
);
936 if (hIcon
!= GuiData
->hIcon
)
938 if (GuiData
->hIcon
!= NULL
&& GuiData
->hIcon
!= ghDefaultIcon
)
940 DestroyIcon(GuiData
->hIcon
);
942 if (GuiData
->hIconSm
!= NULL
&& GuiData
->hIconSm
!= ghDefaultIconSm
)
944 DestroyIcon(GuiData
->hIconSm
);
947 GuiData
->hIcon
= hIcon
;
948 GuiData
->hIconSm
= hIconSm
;
950 DPRINT("Set icons in GuiChangeIcon\n");
951 PostMessageW(GuiData
->hWindow
, WM_SETICON
, ICON_BIG
, (LPARAM
)GuiData
->hIcon
);
952 PostMessageW(GuiData
->hWindow
, WM_SETICON
, ICON_SMALL
, (LPARAM
)GuiData
->hIconSm
);
959 GuiGetConsoleWindowHandle(IN OUT PFRONTEND This
)
961 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
962 return GuiData
->hWindow
;
966 GuiGetLargestConsoleWindowSize(IN OUT PFRONTEND This
,
969 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
970 PCONSOLE_SCREEN_BUFFER ActiveBuffer
;
973 UINT WidthUnit
, HeightUnit
;
977 if (!SystemParametersInfoW(SPI_GETWORKAREA
, 0, &WorkArea
, 0))
979 DPRINT1("SystemParametersInfoW failed - What to do ??\n");
983 ActiveBuffer
= GuiData
->ActiveBuffer
;
986 GetScreenBufferSizeUnits(ActiveBuffer
, GuiData
, &WidthUnit
, &HeightUnit
);
990 /* Default: text mode */
991 WidthUnit
= GuiData
->CharWidth
;
992 HeightUnit
= GuiData
->CharHeight
;
995 width
= WorkArea
.right
;
996 height
= WorkArea
.bottom
;
998 width
-= (2 * (GetSystemMetrics(SM_CXFRAME
) + GetSystemMetrics(SM_CXEDGE
)));
999 height
-= (2 * (GetSystemMetrics(SM_CYFRAME
) + GetSystemMetrics(SM_CYEDGE
)) + GetSystemMetrics(SM_CYCAPTION
));
1001 if (width
< 0) width
= 0;
1002 if (height
< 0) height
= 0;
1004 pSize
->X
= (SHORT
)(width
/ (int)WidthUnit
) /* HACK */ + 2;
1005 pSize
->Y
= (SHORT
)(height
/ (int)HeightUnit
) /* HACK */ + 1;
1009 GuiGetSelectionInfo(IN OUT PFRONTEND This
,
1010 PCONSOLE_SELECTION_INFO pSelectionInfo
)
1012 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1014 if (pSelectionInfo
== NULL
) return FALSE
;
1016 ZeroMemory(pSelectionInfo
, sizeof(*pSelectionInfo
));
1017 if (GuiData
->Selection
.dwFlags
!= CONSOLE_NO_SELECTION
)
1018 RtlCopyMemory(pSelectionInfo
, &GuiData
->Selection
, sizeof(*pSelectionInfo
));
1024 GuiSetPalette(IN OUT PFRONTEND This
,
1025 HPALETTE PaletteHandle
,
1028 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1029 HPALETTE OldPalette
;
1031 // if (GetType(GuiData->ActiveBuffer) != GRAPHICS_BUFFER) return FALSE;
1032 if (PaletteHandle
== NULL
) return FALSE
;
1034 /* Set the new palette for the framebuffer */
1035 OldPalette
= SelectPalette(GuiData
->hMemDC
, PaletteHandle
, FALSE
);
1036 if (OldPalette
== NULL
) return FALSE
;
1038 /* Specify the use of the system palette for the framebuffer */
1039 SetSystemPaletteUse(GuiData
->hMemDC
, PaletteUsage
);
1041 /* Realize the (logical) palette */
1042 RealizePalette(GuiData
->hMemDC
);
1044 /* Save the original system palette handle */
1045 if (GuiData
->hSysPalette
== NULL
) GuiData
->hSysPalette
= OldPalette
;
1051 GuiGetDisplayMode(IN OUT PFRONTEND This
)
1053 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1054 ULONG DisplayMode
= 0;
1056 if (GuiData
->GuiInfo
.FullScreen
)
1057 DisplayMode
|= CONSOLE_FULLSCREEN_HARDWARE
; // CONSOLE_FULLSCREEN
1059 DisplayMode
|= CONSOLE_WINDOWED
;
1065 GuiSetDisplayMode(IN OUT PFRONTEND This
,
1068 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1071 if (NewMode
& ~(CONSOLE_FULLSCREEN_MODE
| CONSOLE_WINDOWED_MODE
))
1074 /* Do nothing if the window is hidden */
1075 if (!GuiData
->IsWindowVisible
) return TRUE
;
1077 FullScreen
= ((NewMode
& CONSOLE_FULLSCREEN_MODE
) != 0);
1079 if (FullScreen
!= GuiData
->GuiInfo
.FullScreen
)
1081 SwitchFullScreen(GuiData
, FullScreen
);
1088 GuiShowMouseCursor(IN OUT PFRONTEND This
,
1091 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1093 if (GuiData
->IsWindowVisible
)
1095 /* Set the reference count */
1096 if (Show
) ++GuiData
->MouseCursorRefCount
;
1097 else --GuiData
->MouseCursorRefCount
;
1099 /* Effectively show (or hide) the cursor (use special values for (w|l)Param) */
1100 PostMessageW(GuiData
->hWindow
, WM_SETCURSOR
, -1, -1);
1103 return GuiData
->MouseCursorRefCount
;
1107 GuiSetMouseCursor(IN OUT PFRONTEND This
,
1108 HCURSOR CursorHandle
)
1110 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1112 /* Do nothing if the window is hidden */
1113 if (!GuiData
->IsWindowVisible
) return TRUE
;
1116 * Set the cursor's handle. If the given handle is NULL,
1117 * then restore the default cursor.
1119 GuiData
->hCursor
= (CursorHandle
? CursorHandle
: ghDefaultCursor
);
1121 /* Effectively modify the shape of the cursor (use special values for (w|l)Param) */
1122 PostMessageW(GuiData
->hWindow
, WM_SETCURSOR
, -1, -1);
1128 GuiMenuControl(IN OUT PFRONTEND This
,
1132 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1134 GuiData
->CmdIdLow
= CmdIdLow
;
1135 GuiData
->CmdIdHigh
= CmdIdHigh
;
1137 return GetSystemMenu(GuiData
->hWindow
, FALSE
);
1141 GuiSetMenuClose(IN OUT PFRONTEND This
,
1145 * NOTE: See http://www.mail-archive.com/harbour@harbour-project.org/msg27509.html
1146 * or http://harbour-devel.1590103.n2.nabble.com/Question-about-hb-gt-win-CtrlHandler-usage-td4670862i20.html
1147 * for more information.
1150 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1151 HMENU hSysMenu
= GetSystemMenu(GuiData
->hWindow
, FALSE
);
1153 if (hSysMenu
== NULL
) return FALSE
;
1155 GuiData
->IsCloseButtonEnabled
= Enable
;
1156 EnableMenuItem(hSysMenu
, SC_CLOSE
, MF_BYCOMMAND
| (Enable
? MF_ENABLED
: MF_GRAYED
));
1161 static FRONTEND_VTBL GuiVtbl
=
1171 GuiSetActiveScreenBuffer
,
1172 GuiReleaseScreenBuffer
,
1173 GuiRefreshInternalInfo
,
1176 GuiGetConsoleWindowHandle
,
1177 GuiGetLargestConsoleWindowSize
,
1178 GuiGetSelectionInfo
,
1190 GuiLoadFrontEnd(IN OUT PFRONTEND FrontEnd
,
1191 IN OUT PCONSOLE_STATE_INFO ConsoleInfo
,
1192 IN OUT PCONSOLE_INIT_INFO ConsoleInitInfo
,
1193 IN HANDLE ConsoleLeaderProcessHandle
)
1195 PCONSOLE_START_INFO ConsoleStartInfo
;
1196 PGUI_INIT_INFO GuiInitInfo
;
1198 if (FrontEnd
== NULL
|| ConsoleInfo
== NULL
|| ConsoleInitInfo
== NULL
)
1199 return STATUS_INVALID_PARAMETER
;
1201 ConsoleStartInfo
= ConsoleInitInfo
->ConsoleStartInfo
;
1204 * Initialize a private initialization info structure for later use.
1205 * It must be freed by a call to GuiUnloadFrontEnd or GuiInitFrontEnd.
1207 GuiInitInfo
= ConsoleAllocHeap(HEAP_ZERO_MEMORY
, sizeof(*GuiInitInfo
));
1208 if (GuiInitInfo
== NULL
) return STATUS_NO_MEMORY
;
1210 /* Initialize GUI terminal emulator common functionalities */
1211 if (!GuiInit(ConsoleInitInfo
, ConsoleLeaderProcessHandle
, GuiInitInfo
))
1213 ConsoleFreeHeap(GuiInitInfo
);
1214 return STATUS_UNSUCCESSFUL
;
1218 * Load terminal settings
1221 /* Impersonate the caller in order to retrieve settings in its context */
1222 // if (!CsrImpersonateClient(NULL))
1223 // return STATUS_UNSUCCESSFUL;
1224 CsrImpersonateClient(NULL
);
1226 /* 1. Load the default settings */
1227 GuiConsoleGetDefaultSettings(&GuiInitInfo
->TermInfo
);
1230 GuiInitInfo
->TermInfo
.ShowWindow
= SW_SHOWNORMAL
;
1232 if (ConsoleInitInfo
->IsWindowVisible
)
1234 /* 2. Load the remaining console settings via the registry */
1235 if ((ConsoleStartInfo
->dwStartupFlags
& STARTF_TITLEISLINKNAME
) == 0)
1238 /* Load the terminal infos from the registry */
1239 GuiConsoleReadUserSettings(&GuiInitInfo
->TermInfo
);
1243 * Now, update them with the properties the user might gave to us
1244 * via the STARTUPINFO structure before calling CreateProcess
1245 * (and which was transmitted via the ConsoleStartInfo structure).
1246 * We therefore overwrite the values read in the registry.
1248 if (ConsoleStartInfo
->dwStartupFlags
& STARTF_USESHOWWINDOW
)
1250 GuiInitInfo
->TermInfo
.ShowWindow
= ConsoleStartInfo
->wShowWindow
;
1252 if (ConsoleStartInfo
->dwStartupFlags
& STARTF_USEPOSITION
)
1254 ConsoleInfo
->AutoPosition
= FALSE
;
1255 ConsoleInfo
->WindowPosition
.x
= ConsoleStartInfo
->dwWindowOrigin
.X
;
1256 ConsoleInfo
->WindowPosition
.y
= ConsoleStartInfo
->dwWindowOrigin
.Y
;
1258 if (ConsoleStartInfo
->dwStartupFlags
& STARTF_RUNFULLSCREEN
)
1260 ConsoleInfo
->FullScreen
= TRUE
;
1266 /* Revert impersonation */
1271 wcsncpy(GuiInitInfo
->TermInfo
.FaceName
, ConsoleInfo
->FaceName
, LF_FACESIZE
);
1272 GuiInitInfo
->TermInfo
.FaceName
[LF_FACESIZE
- 1] = UNICODE_NULL
;
1273 GuiInitInfo
->TermInfo
.FontFamily
= ConsoleInfo
->FontFamily
;
1274 GuiInitInfo
->TermInfo
.FontSize
= ConsoleInfo
->FontSize
;
1275 GuiInitInfo
->TermInfo
.FontWeight
= ConsoleInfo
->FontWeight
;
1278 GuiInitInfo
->TermInfo
.FullScreen
= ConsoleInfo
->FullScreen
;
1279 // GuiInitInfo->TermInfo.ShowWindow;
1280 GuiInitInfo
->TermInfo
.AutoPosition
= ConsoleInfo
->AutoPosition
;
1281 GuiInitInfo
->TermInfo
.WindowOrigin
= ConsoleInfo
->WindowPosition
;
1283 /* Initialize the icon handles */
1284 // if (ConsoleStartInfo->hIcon != NULL)
1285 GuiInitInfo
->hIcon
= ConsoleStartInfo
->hIcon
;
1287 // GuiInitInfo->hIcon = ghDefaultIcon;
1289 // if (ConsoleStartInfo->hIconSm != NULL)
1290 GuiInitInfo
->hIconSm
= ConsoleStartInfo
->hIconSm
;
1292 // GuiInitInfo->hIconSm = ghDefaultIconSm;
1294 // ASSERT(GuiInitInfo->hIcon && GuiInitInfo->hIconSm);
1296 GuiInitInfo
->IsWindowVisible
= ConsoleInitInfo
->IsWindowVisible
;
1298 /* Finally, initialize the frontend structure */
1299 FrontEnd
->Vtbl
= &GuiVtbl
;
1300 FrontEnd
->Context
= NULL
;
1301 FrontEnd
->Context2
= GuiInitInfo
;
1303 return STATUS_SUCCESS
;
1307 GuiUnloadFrontEnd(IN OUT PFRONTEND FrontEnd
)
1309 if (FrontEnd
== NULL
) return STATUS_INVALID_PARAMETER
;
1311 if (FrontEnd
->Context
) GuiDeinitFrontEnd(FrontEnd
);
1312 if (FrontEnd
->Context2
) ConsoleFreeHeap(FrontEnd
->Context2
);
1314 return STATUS_SUCCESS
;