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 GuiConsoleMoveWindow(PGUI_CONSOLE_DATA GuiData
)
62 /* Move the window if needed (not positioned by the system) */
63 if (!GuiData
->GuiInfo
.AutoPosition
)
65 SetWindowPos(GuiData
->hWindow
,
67 GuiData
->GuiInfo
.WindowOrigin
.x
,
68 GuiData
->GuiInfo
.WindowOrigin
.y
,
70 SWP_NOZORDER
| SWP_NOSIZE
| SWP_NOACTIVATE
);
75 DrawRegion(PGUI_CONSOLE_DATA GuiData
,
80 SmallRectToRect(GuiData
, &RegionRect
, Region
);
81 /* Do not erase the background: it speeds up redrawing and reduce flickering */
82 InvalidateRect(GuiData
->hWindow
, &RegionRect
, FALSE
);
83 /**UpdateWindow(GuiData->hWindow);**/
87 InvalidateCell(PGUI_CONSOLE_DATA GuiData
,
90 SMALL_RECT CellRect
= { x
, y
, x
, y
};
91 DrawRegion(GuiData
, &CellRect
);
95 /******************************************************************************
96 * GUI Terminal Initialization *
97 ******************************************************************************/
100 SwitchFullScreen(PGUI_CONSOLE_DATA GuiData
, BOOL FullScreen
);
102 CreateSysMenu(HWND hWnd
);
105 GuiConsoleInputThread(PVOID Param
)
108 PCSR_THREAD pcsrt
= NULL
;
109 PGUI_INIT_INFO GuiInitInfo
= (PGUI_INIT_INFO
)Param
;
110 DESKTOP_CONSOLE_THREAD DesktopConsoleThreadInfo
;
111 ULONG_PTR InputThreadId
= HandleToUlong(NtCurrentTeb()->ClientId
.UniqueThread
);
112 HANDLE hThread
= NULL
;
114 LONG WindowCount
= 0;
118 * This thread dispatches all the console notifications to the
119 * notification window. It is common for all the console windows
120 * in a given desktop in a window station.
123 /* Assign this console input thread to this desktop */
124 DesktopConsoleThreadInfo
.DesktopHandle
= GuiInitInfo
->Desktop
; // Duplicated desktop handle
125 DesktopConsoleThreadInfo
.ThreadId
= InputThreadId
;
126 Status
= NtUserConsoleControl(ConsoleCtrlDesktopConsoleThread
,
127 &DesktopConsoleThreadInfo
,
128 sizeof(DesktopConsoleThreadInfo
));
129 if (!NT_SUCCESS(Status
)) goto Quit
;
131 /* Connect this CSR thread to the USER subsystem */
132 pcsrt
= CsrConnectToUser();
133 if (pcsrt
== NULL
) goto Quit
;
134 hThread
= pcsrt
->ThreadHandle
;
136 /* Assign the desktop to this thread */
137 if (!SetThreadDesktop(DesktopConsoleThreadInfo
.DesktopHandle
)) goto Quit
;
139 /* The thread has been initialized, set the event */
140 NtSetEvent(GuiInitInfo
->GuiThreadStartupEvent
, NULL
);
141 Status
= STATUS_SUCCESS
;
143 while (GetMessageW(&msg
, NULL
, 0, 0))
147 case PM_CREATE_CONSOLE
:
149 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
)msg
.lParam
;
150 PCONSRV_CONSOLE Console
= GuiData
->Console
;
154 DPRINT("PM_CREATE_CONSOLE -- creating window\n");
156 NewWindow
= CreateWindowExW(WS_EX_CLIENTEDGE
,
158 Console
->Title
.Buffer
,
164 GuiData
->IsWindowVisible
? HWND_DESKTOP
: HWND_MESSAGE
,
168 if (NewWindow
== NULL
)
170 DPRINT1("Failed to create a new console window\n");
174 ASSERT(NewWindow
== GuiData
->hWindow
);
176 InterlockedIncrement(&WindowCount
);
179 // FIXME: TODO: Move everything there into conwnd.c!OnNcCreate()
182 /* Retrieve our real position */
183 // See conwnd.c!OnMove()
184 GetWindowRect(GuiData
->hWindow
, &rcWnd
);
185 GuiData
->GuiInfo
.WindowOrigin
.x
= rcWnd
.left
;
186 GuiData
->GuiInfo
.WindowOrigin
.y
= rcWnd
.top
;
188 if (GuiData
->IsWindowVisible
)
190 /* Move and resize the window to the user's values */
191 /* CAN WE DEADLOCK ?? */
192 GuiConsoleMoveWindow(GuiData
); // FIXME: This MUST be done via the CreateWindowExW call.
193 SendMessageW(GuiData
->hWindow
, PM_RESIZE_TERMINAL
, 0, 0);
196 // FIXME: HACK: Potential HACK for CORE-8129; see revision 63595.
197 CreateSysMenu(GuiData
->hWindow
);
199 if (GuiData
->IsWindowVisible
)
201 /* Switch to full-screen mode if necessary */
202 // FIXME: Move elsewhere, it cause misdrawings of the window.
203 if (GuiData
->GuiInfo
.FullScreen
) SwitchFullScreen(GuiData
, TRUE
);
205 DPRINT("PM_CREATE_CONSOLE -- showing window\n");
206 // ShowWindow(NewWindow, (int)GuiData->GuiInfo.ShowWindow);
207 ShowWindowAsync(NewWindow
, (int)GuiData
->GuiInfo
.ShowWindow
);
208 DPRINT("Window showed\n");
212 DPRINT("PM_CREATE_CONSOLE -- hidden window\n");
213 ShowWindowAsync(NewWindow
, SW_HIDE
);
219 case PM_DESTROY_CONSOLE
:
221 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
)msg
.lParam
;
224 /* Exit the full screen mode if it was already set */
225 // LeaveFullScreen(GuiData);
228 * Window creation is done using a PostMessage(), so it's possible
229 * that the window that we want to destroy doesn't exist yet.
230 * So first empty the message queue.
233 while (PeekMessageW(&TempMsg, NULL, 0, 0, PM_REMOVE))
235 TranslateMessage(&TempMsg);
236 DispatchMessageW(&TempMsg);
238 while (PeekMessageW(&TempMsg
, NULL
, 0, 0, PM_REMOVE
)) ;
240 if (GuiData
->hWindow
== NULL
) continue;
242 DestroyWindow(GuiData
->hWindow
);
244 NtSetEvent(GuiData
->hGuiTermEvent
, NULL
);
246 if (InterlockedDecrement(&WindowCount
) == 0)
248 DPRINT("CONSRV: Going to quit the Input Thread 0x%p\n", InputThreadId
);
256 TranslateMessage(&msg
);
257 DispatchMessageW(&msg
);
261 DPRINT("CONSRV: Quit the Input Thread 0x%p, Status = 0x%08lx\n", InputThreadId
, Status
);
263 /* Remove this console input thread from this desktop */
264 // DesktopConsoleThreadInfo.DesktopHandle;
265 DesktopConsoleThreadInfo
.ThreadId
= 0;
266 NtUserConsoleControl(ConsoleCtrlDesktopConsoleThread
,
267 &DesktopConsoleThreadInfo
,
268 sizeof(DesktopConsoleThreadInfo
));
270 /* Close the duplicated desktop handle */
271 CloseDesktop(DesktopConsoleThreadInfo
.DesktopHandle
); // NtUserCloseDesktop
273 /* Cleanup CSR thread */
276 if (hThread
!= pcsrt
->ThreadHandle
)
277 DPRINT1("WARNING!! hThread (0x%p) != pcsrt->ThreadHandle (0x%p), you may expect crashes soon!!\n", hThread
, pcsrt
->ThreadHandle
);
279 CsrDereferenceThread(pcsrt
);
282 /* Exit the thread */
283 RtlExitUserThread(Status
);
287 // FIXME: Maybe return a NTSTATUS
289 GuiInit(IN PCONSOLE_INIT_INFO ConsoleInitInfo
,
290 IN HANDLE ConsoleLeaderProcessHandle
,
291 IN OUT PGUI_INIT_INFO GuiInitInfo
)
294 UNICODE_STRING DesktopPath
;
295 DESKTOP_CONSOLE_THREAD DesktopConsoleThreadInfo
;
304 * Initialize and register the console window class, if needed.
306 if (!ConsInitialized
)
308 if (!RegisterConWndClass(ConSrvDllInstance
)) return FALSE
;
309 ConsInitialized
= TRUE
;
313 * Set-up the console input thread. We have
314 * one console input thread per desktop.
317 if (!CsrImpersonateClient(NULL
))
318 // return STATUS_BAD_IMPERSONATION_LEVEL;
321 if (ConsoleInitInfo
->DesktopLength
)
323 DesktopPath
.MaximumLength
= ConsoleInitInfo
->DesktopLength
;
324 DesktopPath
.Length
= DesktopPath
.MaximumLength
- sizeof(UNICODE_NULL
);
325 DesktopPath
.Buffer
= ConsoleInitInfo
->Desktop
;
329 RtlInitUnicodeString(&DesktopPath
, L
"Default");
332 hDesk
= NtUserResolveDesktop(ConsoleLeaderProcessHandle
,
336 DPRINT("NtUserResolveDesktop(DesktopPath = '%wZ') returned hDesk = 0x%p; hWinSta = 0x%p\n",
337 &DesktopPath
, hDesk
, hWinSta
);
341 if (hDesk
== NULL
) return FALSE
;
344 * We need to see whether we need to create a
345 * new console input thread for this desktop.
347 DesktopConsoleThreadInfo
.DesktopHandle
= hDesk
;
348 DesktopConsoleThreadInfo
.ThreadId
= (ULONG_PTR
)INVALID_HANDLE_VALUE
; // Special value to say we just want to retrieve the thread ID.
349 NtUserConsoleControl(ConsoleCtrlDesktopConsoleThread
,
350 &DesktopConsoleThreadInfo
,
351 sizeof(DesktopConsoleThreadInfo
));
352 DPRINT("NtUserConsoleControl returned ThreadId = 0x%p\n", DesktopConsoleThreadInfo
.ThreadId
);
355 * Save the opened window station and desktop handles in the initialization
356 * structure. They will be used later on, and released, by the GUI frontend.
358 GuiInitInfo
->WinSta
= hWinSta
;
359 GuiInitInfo
->Desktop
= hDesk
;
361 /* Here GuiInitInfo contains original handles */
363 /* If we already have a console input thread on this desktop... */
364 if (DesktopConsoleThreadInfo
.ThreadId
!= 0)
366 /* ... just use it... */
367 DPRINT("Using input thread InputThreadId = 0x%p\n", DesktopConsoleThreadInfo
.ThreadId
);
368 GuiInitInfo
->InputThreadId
= DesktopConsoleThreadInfo
.ThreadId
;
372 /* ... otherwise create a new one. */
374 /* Initialize a startup event for the thread to signal it */
375 Status
= NtCreateEvent(&GuiInitInfo
->GuiThreadStartupEvent
, EVENT_ALL_ACCESS
,
376 NULL
, SynchronizationEvent
, FALSE
);
377 if (!NT_SUCCESS(Status
))
384 * Duplicate the desktop handle for the console input thread internal needs.
385 * If it happens to need also a window station handle in the future, then
386 * it is there that you also need to duplicate the window station handle!
388 * Note also that we are going to temporarily overwrite the stored handles
389 * in GuiInitInfo because it happens that we use also this structure to give
390 * the duplicated handles to the input thread that is going to initialize.
391 * After the input thread finishes its initialization, we restore the handles
392 * in GuiInitInfo to their old values.
394 Status
= NtDuplicateObject(NtCurrentProcess(),
397 (PHANDLE
)&GuiInitInfo
->Desktop
,
398 0, 0, DUPLICATE_SAME_ACCESS
);
399 if (!NT_SUCCESS(Status
))
405 /* Here GuiInitInfo contains duplicated handles */
407 Status
= RtlCreateUserThread(NtCurrentProcess(),
409 TRUE
, // Start the thread in suspended state
413 (PVOID
)GuiConsoleInputThread
,
417 if (NT_SUCCESS(Status
))
419 /* Add it as a static server thread and resume it */
420 CsrAddStaticServerThread(hInputThread
, &ClientId
, 0);
421 Status
= NtResumeThread(hInputThread
, NULL
);
423 DPRINT("Thread creation hInputThread = 0x%p, InputThreadId = 0x%p, Status = 0x%08lx\n",
424 hInputThread
, ClientId
.UniqueThread
, Status
);
426 if (!NT_SUCCESS(Status
) || hInputThread
== NULL
)
428 /* Close the thread's handle */
429 if (hInputThread
) NtClose(hInputThread
);
431 /* We need to close here the duplicated desktop handle */
432 CloseDesktop(GuiInitInfo
->Desktop
); // NtUserCloseDesktop
434 /* Close the startup event and bail out */
435 NtClose(GuiInitInfo
->GuiThreadStartupEvent
);
437 DPRINT1("CONSRV: Failed to create graphics console thread.\n");
442 /* No need to close hInputThread, this is done by CSR automatically */
444 /* Wait for the thread to finish its initialization, and close the startup event */
445 NtWaitForSingleObject(GuiInitInfo
->GuiThreadStartupEvent
, FALSE
, NULL
);
446 NtClose(GuiInitInfo
->GuiThreadStartupEvent
);
449 * Save the input thread ID for later use, and restore the original handles.
450 * The copies are held by the console input thread.
452 GuiInitInfo
->InputThreadId
= (ULONG_PTR
)ClientId
.UniqueThread
;
453 GuiInitInfo
->WinSta
= hWinSta
;
454 GuiInitInfo
->Desktop
= hDesk
;
456 /* Here GuiInitInfo contains again original handles */
462 * Close the original handles. Do not use the copies in GuiInitInfo
463 * because we may have failed in the middle of the duplicate operation
464 * and the handles stored in GuiInitInfo may have changed.
466 CloseDesktop(hDesk
); // NtUserCloseDesktop
467 CloseWindowStation(hWinSta
); // NtUserCloseWindowStation
474 /******************************************************************************
475 * GUI Console Driver *
476 ******************************************************************************/
479 GuiDeinitFrontEnd(IN OUT PFRONTEND This
);
481 static NTSTATUS NTAPI
482 GuiInitFrontEnd(IN OUT PFRONTEND This
,
483 IN PCONSRV_CONSOLE Console
)
485 PGUI_INIT_INFO GuiInitInfo
;
486 PGUI_CONSOLE_DATA GuiData
;
488 if (This
== NULL
|| Console
== NULL
|| This
->Context2
== NULL
)
489 return STATUS_INVALID_PARAMETER
;
491 ASSERT(This
->Console
== Console
);
493 GuiInitInfo
= This
->Context2
;
495 /* Terminal data allocation */
496 GuiData
= ConsoleAllocHeap(HEAP_ZERO_MEMORY
, sizeof(*GuiData
));
499 DPRINT1("CONSRV: Failed to create GUI_CONSOLE_DATA\n");
500 return STATUS_UNSUCCESSFUL
;
502 /// /* HACK */ Console->FrontEndIFace.Context = (PVOID)GuiData; /* HACK */
503 GuiData
->Console
= Console
;
504 GuiData
->ActiveBuffer
= Console
->ActiveBuffer
;
505 GuiData
->hWindow
= NULL
;
506 GuiData
->IsWindowVisible
= GuiInitInfo
->IsWindowVisible
;
508 /* The console can be resized */
509 Console
->FixedSize
= FALSE
;
511 InitializeCriticalSection(&GuiData
->Lock
);
516 RtlCopyMemory(&GuiData
->GuiInfo
, &GuiInitInfo
->TermInfo
, sizeof(GuiInitInfo
->TermInfo
));
518 /* Initialize the icon handles */
519 if (GuiInitInfo
->hIcon
!= NULL
)
520 GuiData
->hIcon
= GuiInitInfo
->hIcon
;
522 GuiData
->hIcon
= ghDefaultIcon
;
524 if (GuiInitInfo
->hIconSm
!= NULL
)
525 GuiData
->hIconSm
= GuiInitInfo
->hIconSm
;
527 GuiData
->hIconSm
= ghDefaultIconSm
;
529 ASSERT(GuiData
->hIcon
&& GuiData
->hIconSm
);
531 /* Mouse is shown by default with its default cursor shape */
532 GuiData
->hCursor
= ghDefaultCursor
;
533 GuiData
->MouseCursorRefCount
= 0;
535 /* A priori don't ignore mouse signals */
536 GuiData
->IgnoreNextMouseSignal
= FALSE
;
537 /* Initialize HACK FOR CORE-8394. See conwnd.c!OnMouse for more details. */
538 GuiData
->HackCORE8394IgnoreNextMove
= FALSE
;
540 /* Close button and the corresponding system menu item are enabled by default */
541 GuiData
->IsCloseButtonEnabled
= TRUE
;
543 /* There is no user-reserved menu id range by default */
544 GuiData
->CmdIdLow
= GuiData
->CmdIdHigh
= 0;
546 /* Initialize the selection */
547 RtlZeroMemory(&GuiData
->Selection
, sizeof(GuiData
->Selection
));
548 GuiData
->Selection
.dwFlags
= CONSOLE_NO_SELECTION
;
549 RtlZeroMemory(&GuiData
->dwSelectionCursor
, sizeof(GuiData
->dwSelectionCursor
));
550 GuiData
->LineSelection
= FALSE
; // Default to block selection
551 // TODO: Retrieve the selection mode via the registry.
553 GuiData
->InputThreadId
= GuiInitInfo
->InputThreadId
;
554 GuiData
->WinSta
= GuiInitInfo
->WinSta
;
555 GuiData
->Desktop
= GuiInitInfo
->Desktop
;
557 /* Finally, finish to initialize the frontend structure */
558 This
->Context
= GuiData
;
559 ConsoleFreeHeap(This
->Context2
);
560 This
->Context2
= NULL
;
563 * We need to wait until the GUI has been fully initialized
564 * to retrieve custom settings i.e. WindowSize etc...
565 * Ideally we could use SendNotifyMessage for this but its not
568 NtCreateEvent(&GuiData
->hGuiInitEvent
, EVENT_ALL_ACCESS
,
569 NULL
, SynchronizationEvent
, FALSE
);
570 NtCreateEvent(&GuiData
->hGuiTermEvent
, EVENT_ALL_ACCESS
,
571 NULL
, SynchronizationEvent
, FALSE
);
573 DPRINT("GUI - Checkpoint\n");
575 /* Create the terminal window */
576 PostThreadMessageW(GuiData
->InputThreadId
, PM_CREATE_CONSOLE
, 0, (LPARAM
)GuiData
);
578 /* Wait until initialization has finished */
579 NtWaitForSingleObject(GuiData
->hGuiInitEvent
, FALSE
, NULL
);
580 DPRINT("OK we created the console window\n");
581 NtClose(GuiData
->hGuiInitEvent
);
582 GuiData
->hGuiInitEvent
= NULL
;
584 /* Check whether we really succeeded in initializing the terminal window */
585 if (GuiData
->hWindow
== NULL
)
587 DPRINT("GuiInitConsole - We failed at creating a new terminal window\n");
588 GuiDeinitFrontEnd(This
);
589 return STATUS_UNSUCCESSFUL
;
592 return STATUS_SUCCESS
;
596 GuiDeinitFrontEnd(IN OUT PFRONTEND This
)
598 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
600 DPRINT("Send PM_DESTROY_CONSOLE message and wait on hGuiTermEvent...\n");
601 PostThreadMessageW(GuiData
->InputThreadId
, PM_DESTROY_CONSOLE
, 0, (LPARAM
)GuiData
);
602 NtWaitForSingleObject(GuiData
->hGuiTermEvent
, FALSE
, NULL
);
603 DPRINT("hGuiTermEvent set\n");
604 NtClose(GuiData
->hGuiTermEvent
);
605 GuiData
->hGuiTermEvent
= NULL
;
607 CloseDesktop(GuiData
->Desktop
); // NtUserCloseDesktop
608 CloseWindowStation(GuiData
->WinSta
); // NtUserCloseWindowStation
610 DPRINT("Destroying icons !! - GuiData->hIcon = 0x%p ; ghDefaultIcon = 0x%p ; GuiData->hIconSm = 0x%p ; ghDefaultIconSm = 0x%p\n",
611 GuiData
->hIcon
, ghDefaultIcon
, GuiData
->hIconSm
, ghDefaultIconSm
);
612 if (GuiData
->hIcon
!= NULL
&& GuiData
->hIcon
!= ghDefaultIcon
)
614 DPRINT("Destroy hIcon\n");
615 DestroyIcon(GuiData
->hIcon
);
617 if (GuiData
->hIconSm
!= NULL
&& GuiData
->hIconSm
!= ghDefaultIconSm
)
619 DPRINT("Destroy hIconSm\n");
620 DestroyIcon(GuiData
->hIconSm
);
623 This
->Context
= NULL
;
624 DeleteCriticalSection(&GuiData
->Lock
);
625 ConsoleFreeHeap(GuiData
);
627 DPRINT("Quit GuiDeinitFrontEnd\n");
631 GuiDrawRegion(IN OUT PFRONTEND This
,
634 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
636 /* Do nothing if the window is hidden */
637 if (!GuiData
->IsWindowVisible
) return;
639 DrawRegion(GuiData
, Region
);
643 GuiWriteStream(IN OUT PFRONTEND This
,
651 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
652 PCONSOLE_SCREEN_BUFFER Buff
;
653 SHORT CursorEndX
, CursorEndY
;
656 if (NULL
== GuiData
|| NULL
== GuiData
->hWindow
) return;
658 /* Do nothing if the window is hidden */
659 if (!GuiData
->IsWindowVisible
) return;
661 Buff
= GuiData
->ActiveBuffer
;
662 if (GetType(Buff
) != TEXTMODE_BUFFER
) return;
664 if (0 != ScrolledLines
)
668 ScrollRect
.right
= Buff
->ViewSize
.X
* GuiData
->CharWidth
;
669 ScrollRect
.bottom
= Region
->Top
* GuiData
->CharHeight
;
671 ScrollWindowEx(GuiData
->hWindow
,
673 -(int)(ScrolledLines
* GuiData
->CharHeight
),
681 DrawRegion(GuiData
, Region
);
683 if (CursorStartX
< Region
->Left
|| Region
->Right
< CursorStartX
684 || CursorStartY
< Region
->Top
|| Region
->Bottom
< CursorStartY
)
686 InvalidateCell(GuiData
, CursorStartX
, CursorStartY
);
689 CursorEndX
= Buff
->CursorPosition
.X
;
690 CursorEndY
= Buff
->CursorPosition
.Y
;
691 if ((CursorEndX
< Region
->Left
|| Region
->Right
< CursorEndX
692 || CursorEndY
< Region
->Top
|| Region
->Bottom
< CursorEndY
)
693 && (CursorEndX
!= CursorStartX
|| CursorEndY
!= CursorStartY
))
695 InvalidateCell(GuiData
, CursorEndX
, CursorEndY
);
699 // Set up the update timer (very short interval) - this is a "hack" for getting the OS to
700 // repaint the window without having it just freeze up and stay on the screen permanently.
701 Buff
->CursorBlinkOn
= TRUE
;
702 SetTimer(GuiData
->hWindow
, CONGUI_UPDATE_TIMER
, CONGUI_UPDATE_TIME
, NULL
);
705 /* static */ VOID NTAPI
706 GuiRingBell(IN OUT PFRONTEND This
)
708 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
710 /* Emit an error beep sound */
711 SendNotifyMessage(GuiData
->hWindow
, PM_CONSOLE_BEEP
, 0, 0);
715 GuiSetCursorInfo(IN OUT PFRONTEND This
,
716 PCONSOLE_SCREEN_BUFFER Buff
)
718 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
720 /* Do nothing if the window is hidden */
721 if (!GuiData
->IsWindowVisible
) return TRUE
;
723 if (GuiData
->ActiveBuffer
== Buff
)
725 InvalidateCell(GuiData
, Buff
->CursorPosition
.X
, Buff
->CursorPosition
.Y
);
732 GuiSetScreenInfo(IN OUT PFRONTEND This
,
733 PCONSOLE_SCREEN_BUFFER Buff
,
737 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
739 /* Do nothing if the window is hidden */
740 if (!GuiData
->IsWindowVisible
) return TRUE
;
742 if (GuiData
->ActiveBuffer
== Buff
)
744 /* Redraw char at old position (remove cursor) */
745 InvalidateCell(GuiData
, OldCursorX
, OldCursorY
);
746 /* Redraw char at new position (show cursor) */
747 InvalidateCell(GuiData
, Buff
->CursorPosition
.X
, Buff
->CursorPosition
.Y
);
754 GuiResizeTerminal(IN OUT PFRONTEND This
)
756 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
758 /* Resize the window to the user's values */
759 PostMessageW(GuiData
->hWindow
, PM_RESIZE_TERMINAL
, 0, 0);
763 GuiSetActiveScreenBuffer(IN OUT PFRONTEND This
)
765 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
766 PCONSOLE_SCREEN_BUFFER ActiveBuffer
;
769 EnterCriticalSection(&GuiData
->Lock
);
770 GuiData
->WindowSizeLock
= TRUE
;
772 InterlockedExchangePointer((PVOID
*)&GuiData
->ActiveBuffer
,
773 ConDrvGetActiveScreenBuffer(GuiData
->Console
));
775 GuiData
->WindowSizeLock
= FALSE
;
776 LeaveCriticalSection(&GuiData
->Lock
);
778 ActiveBuffer
= GuiData
->ActiveBuffer
;
780 /* Change the current palette */
781 if (ActiveBuffer
->PaletteHandle
== NULL
)
782 hPalette
= GuiData
->hSysPalette
;
784 hPalette
= ActiveBuffer
->PaletteHandle
;
786 DPRINT("GuiSetActiveScreenBuffer using palette 0x%p\n", hPalette
);
788 /* Set the new palette for the framebuffer */
789 SelectPalette(GuiData
->hMemDC
, hPalette
, FALSE
);
791 /* Specify the use of the system palette for the framebuffer */
792 SetSystemPaletteUse(GuiData
->hMemDC
, ActiveBuffer
->PaletteUsage
);
794 /* Realize the (logical) palette */
795 RealizePalette(GuiData
->hMemDC
);
797 GuiResizeTerminal(This
);
798 // ConioDrawConsole(Console);
802 GuiReleaseScreenBuffer(IN OUT PFRONTEND This
,
803 IN PCONSOLE_SCREEN_BUFFER ScreenBuffer
)
805 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
808 * If we were notified to release a screen buffer that is not actually
809 * ours, then just ignore the notification...
811 if (ScreenBuffer
!= GuiData
->ActiveBuffer
) return;
814 * ... else, we must release our active buffer. Two cases are present:
815 * - If ScreenBuffer (== GuiData->ActiveBuffer) IS NOT the console
816 * active screen buffer, then we can safely switch to it.
817 * - If ScreenBuffer IS the console active screen buffer, we must release
821 /* Release the old active palette and set the default one */
822 if (GetCurrentObject(GuiData
->hMemDC
, OBJ_PAL
) == ScreenBuffer
->PaletteHandle
)
824 /* Set the new palette */
825 SelectPalette(GuiData
->hMemDC
, GuiData
->hSysPalette
, FALSE
);
828 /* Set the adequate active screen buffer */
829 if (ScreenBuffer
!= GuiData
->Console
->ActiveBuffer
)
831 GuiSetActiveScreenBuffer(This
);
835 EnterCriticalSection(&GuiData
->Lock
);
836 GuiData
->WindowSizeLock
= TRUE
;
838 InterlockedExchangePointer((PVOID
*)&GuiData
->ActiveBuffer
, NULL
);
840 GuiData
->WindowSizeLock
= FALSE
;
841 LeaveCriticalSection(&GuiData
->Lock
);
846 GuiSetMouseCursor(IN OUT PFRONTEND This
,
847 HCURSOR CursorHandle
);
850 GuiRefreshInternalInfo(IN OUT PFRONTEND This
)
852 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
854 /* Update the console leader information held by the window */
855 SetConWndConsoleLeaderCID(GuiData
);
859 * We reset the cursor here so that, when a console app quits, we reset
860 * the cursor to the default one. It's quite a hack since it doesn't proceed
861 * per - console process... This must be fixed.
863 * See GuiInitConsole(...) for more information.
866 /* Mouse is shown by default with its default cursor shape */
867 GuiData
->MouseCursorRefCount
= 0; // Reinitialize the reference counter
868 GuiSetMouseCursor(This
, NULL
);
872 GuiChangeTitle(IN OUT PFRONTEND This
)
874 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
875 // PostMessageW(GuiData->hWindow, PM_CONSOLE_SET_TITLE, 0, 0);
876 SetWindowTextW(GuiData
->hWindow
, GuiData
->Console
->Title
.Buffer
);
880 GuiChangeIcon(IN OUT PFRONTEND This
,
883 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
884 HICON hIcon
, hIconSm
;
886 if (IconHandle
== NULL
)
888 hIcon
= ghDefaultIcon
;
889 hIconSm
= ghDefaultIconSm
;
893 hIcon
= CopyIcon(IconHandle
);
894 hIconSm
= CopyIcon(IconHandle
);
900 if (hIcon
!= GuiData
->hIcon
)
902 if (GuiData
->hIcon
!= NULL
&& GuiData
->hIcon
!= ghDefaultIcon
)
904 DestroyIcon(GuiData
->hIcon
);
906 if (GuiData
->hIconSm
!= NULL
&& GuiData
->hIconSm
!= ghDefaultIconSm
)
908 DestroyIcon(GuiData
->hIconSm
);
911 GuiData
->hIcon
= hIcon
;
912 GuiData
->hIconSm
= hIconSm
;
914 DPRINT("Set icons in GuiChangeIcon\n");
915 PostMessageW(GuiData
->hWindow
, WM_SETICON
, ICON_BIG
, (LPARAM
)GuiData
->hIcon
);
916 PostMessageW(GuiData
->hWindow
, WM_SETICON
, ICON_SMALL
, (LPARAM
)GuiData
->hIconSm
);
923 GuiGetConsoleWindowHandle(IN OUT PFRONTEND This
)
925 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
926 return GuiData
->hWindow
;
930 GuiGetLargestConsoleWindowSize(IN OUT PFRONTEND This
,
933 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
934 PCONSOLE_SCREEN_BUFFER ActiveBuffer
;
936 MONITORINFO MonitorInfo
;
938 UINT WidthUnit
, HeightUnit
;
943 * Retrieve the monitor that is mostly covered by the current console window;
944 * default to primary monitor otherwise.
946 MonitorInfo
.cbSize
= sizeof(MonitorInfo
);
947 hMonitor
= MonitorFromWindow(GuiData
->hWindow
, MONITOR_DEFAULTTOPRIMARY
);
948 if (hMonitor
&& GetMonitorInfoW(hMonitor
, &MonitorInfo
))
950 /* Retrieve the width and height of the client area of this monitor */
951 Width
= MonitorInfo
.rcWork
.right
- MonitorInfo
.rcWork
.left
;
952 Height
= MonitorInfo
.rcWork
.bottom
- MonitorInfo
.rcWork
.top
;
957 * Retrieve the width and height of the client area for a full-screen
958 * window on the primary display monitor.
960 Width
= GetSystemMetrics(SM_CXFULLSCREEN
);
961 Height
= GetSystemMetrics(SM_CYFULLSCREEN
);
964 // SystemParametersInfoW(SPI_GETWORKAREA, 0, &WorkArea, 0);
965 // Width = WorkArea.right;
966 // Height = WorkArea.bottom;
969 ActiveBuffer
= GuiData
->ActiveBuffer
;
971 // NOTE: This would be surprising if we wouldn't have an associated buffer...
974 GetScreenBufferSizeUnits(ActiveBuffer
, GuiData
, &WidthUnit
, &HeightUnit
);
977 /* Default: graphics mode */
978 WidthUnit
= HeightUnit
= 1;
981 Width
-= (2 * (GetSystemMetrics(SM_CXFRAME
) + GetSystemMetrics(SM_CXEDGE
)));
982 Height
-= (2 * (GetSystemMetrics(SM_CYFRAME
) + GetSystemMetrics(SM_CYEDGE
)) + GetSystemMetrics(SM_CYCAPTION
));
984 if (Width
< 0) Width
= 0;
985 if (Height
< 0) Height
= 0;
987 pSize
->X
= (SHORT
)(Width
/ (int)WidthUnit
) /* HACK */ + 2;
988 pSize
->Y
= (SHORT
)(Height
/ (int)HeightUnit
) /* HACK */ + 1;
992 GuiGetSelectionInfo(IN OUT PFRONTEND This
,
993 PCONSOLE_SELECTION_INFO pSelectionInfo
)
995 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
997 if (pSelectionInfo
== NULL
) return FALSE
;
999 ZeroMemory(pSelectionInfo
, sizeof(*pSelectionInfo
));
1000 if (GuiData
->Selection
.dwFlags
!= CONSOLE_NO_SELECTION
)
1001 RtlCopyMemory(pSelectionInfo
, &GuiData
->Selection
, sizeof(*pSelectionInfo
));
1007 GuiSetPalette(IN OUT PFRONTEND This
,
1008 HPALETTE PaletteHandle
,
1011 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1012 HPALETTE OldPalette
;
1014 // if (GetType(GuiData->ActiveBuffer) != GRAPHICS_BUFFER) return FALSE;
1015 if (PaletteHandle
== NULL
) return FALSE
;
1017 /* Set the new palette for the framebuffer */
1018 OldPalette
= SelectPalette(GuiData
->hMemDC
, PaletteHandle
, FALSE
);
1019 if (OldPalette
== NULL
) return FALSE
;
1021 /* Specify the use of the system palette for the framebuffer */
1022 SetSystemPaletteUse(GuiData
->hMemDC
, PaletteUsage
);
1024 /* Realize the (logical) palette */
1025 RealizePalette(GuiData
->hMemDC
);
1027 /* Save the original system palette handle */
1028 if (GuiData
->hSysPalette
== NULL
) GuiData
->hSysPalette
= OldPalette
;
1034 GuiGetDisplayMode(IN OUT PFRONTEND This
)
1036 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1037 ULONG DisplayMode
= 0;
1039 if (GuiData
->GuiInfo
.FullScreen
)
1040 DisplayMode
|= CONSOLE_FULLSCREEN_HARDWARE
; // CONSOLE_FULLSCREEN
1042 DisplayMode
|= CONSOLE_WINDOWED
;
1048 GuiSetDisplayMode(IN OUT PFRONTEND This
,
1051 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1054 if (NewMode
& ~(CONSOLE_FULLSCREEN_MODE
| CONSOLE_WINDOWED_MODE
))
1057 /* Do nothing if the window is hidden */
1058 if (!GuiData
->IsWindowVisible
) return TRUE
;
1060 FullScreen
= ((NewMode
& CONSOLE_FULLSCREEN_MODE
) != 0);
1062 if (FullScreen
!= GuiData
->GuiInfo
.FullScreen
)
1064 SwitchFullScreen(GuiData
, FullScreen
);
1071 GuiShowMouseCursor(IN OUT PFRONTEND This
,
1074 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1076 if (GuiData
->IsWindowVisible
)
1078 /* Set the reference count */
1079 if (Show
) ++GuiData
->MouseCursorRefCount
;
1080 else --GuiData
->MouseCursorRefCount
;
1082 /* Effectively show (or hide) the cursor (use special values for (w|l)Param) */
1083 PostMessageW(GuiData
->hWindow
, WM_SETCURSOR
, -1, -1);
1086 return GuiData
->MouseCursorRefCount
;
1090 GuiSetMouseCursor(IN OUT PFRONTEND This
,
1091 HCURSOR CursorHandle
)
1093 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1095 /* Do nothing if the window is hidden */
1096 if (!GuiData
->IsWindowVisible
) return TRUE
;
1099 * Set the cursor's handle. If the given handle is NULL,
1100 * then restore the default cursor.
1102 GuiData
->hCursor
= (CursorHandle
? CursorHandle
: ghDefaultCursor
);
1104 /* Effectively modify the shape of the cursor (use special values for (w|l)Param) */
1105 PostMessageW(GuiData
->hWindow
, WM_SETCURSOR
, -1, -1);
1111 GuiMenuControl(IN OUT PFRONTEND This
,
1115 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1117 GuiData
->CmdIdLow
= CmdIdLow
;
1118 GuiData
->CmdIdHigh
= CmdIdHigh
;
1120 return GetSystemMenu(GuiData
->hWindow
, FALSE
);
1124 GuiSetMenuClose(IN OUT PFRONTEND This
,
1128 * NOTE: See http://www.mail-archive.com/harbour@harbour-project.org/msg27509.html
1129 * or http://harbour-devel.1590103.n2.nabble.com/Question-about-hb-gt-win-CtrlHandler-usage-td4670862i20.html
1130 * for more information.
1133 PGUI_CONSOLE_DATA GuiData
= This
->Context
;
1134 HMENU hSysMenu
= GetSystemMenu(GuiData
->hWindow
, FALSE
);
1136 if (hSysMenu
== NULL
) return FALSE
;
1138 GuiData
->IsCloseButtonEnabled
= Enable
;
1139 EnableMenuItem(hSysMenu
, SC_CLOSE
, MF_BYCOMMAND
| (Enable
? MF_ENABLED
: MF_GRAYED
));
1144 static FRONTEND_VTBL GuiVtbl
=
1154 GuiSetActiveScreenBuffer
,
1155 GuiReleaseScreenBuffer
,
1156 GuiRefreshInternalInfo
,
1159 GuiGetConsoleWindowHandle
,
1160 GuiGetLargestConsoleWindowSize
,
1161 GuiGetSelectionInfo
,
1173 GuiLoadFrontEnd(IN OUT PFRONTEND FrontEnd
,
1174 IN OUT PCONSOLE_STATE_INFO ConsoleInfo
,
1175 IN OUT PCONSOLE_INIT_INFO ConsoleInitInfo
,
1176 IN HANDLE ConsoleLeaderProcessHandle
)
1178 PCONSOLE_START_INFO ConsoleStartInfo
;
1179 PGUI_INIT_INFO GuiInitInfo
;
1181 if (FrontEnd
== NULL
|| ConsoleInfo
== NULL
|| ConsoleInitInfo
== NULL
)
1182 return STATUS_INVALID_PARAMETER
;
1184 ConsoleStartInfo
= ConsoleInitInfo
->ConsoleStartInfo
;
1187 * Initialize a private initialization info structure for later use.
1188 * It must be freed by a call to GuiUnloadFrontEnd or GuiInitFrontEnd.
1190 GuiInitInfo
= ConsoleAllocHeap(HEAP_ZERO_MEMORY
, sizeof(*GuiInitInfo
));
1191 if (GuiInitInfo
== NULL
) return STATUS_NO_MEMORY
;
1193 /* Initialize GUI terminal emulator common functionalities */
1194 if (!GuiInit(ConsoleInitInfo
, ConsoleLeaderProcessHandle
, GuiInitInfo
))
1196 ConsoleFreeHeap(GuiInitInfo
);
1197 return STATUS_UNSUCCESSFUL
;
1201 * Load terminal settings
1204 /* Impersonate the caller in order to retrieve settings in its context */
1205 // if (!CsrImpersonateClient(NULL))
1206 // return STATUS_UNSUCCESSFUL;
1207 CsrImpersonateClient(NULL
);
1209 /* 1. Load the default settings */
1210 GuiConsoleGetDefaultSettings(&GuiInitInfo
->TermInfo
);
1213 GuiInitInfo
->TermInfo
.ShowWindow
= SW_SHOWNORMAL
;
1215 if (ConsoleInitInfo
->IsWindowVisible
)
1217 /* 2. Load the remaining console settings via the registry */
1218 if ((ConsoleStartInfo
->dwStartupFlags
& STARTF_TITLEISLINKNAME
) == 0)
1221 /* Load the terminal infos from the registry */
1222 GuiConsoleReadUserSettings(&GuiInitInfo
->TermInfo
);
1226 * Now, update them with the properties the user might gave to us
1227 * via the STARTUPINFO structure before calling CreateProcess
1228 * (and which was transmitted via the ConsoleStartInfo structure).
1229 * We therefore overwrite the values read in the registry.
1231 if (ConsoleStartInfo
->dwStartupFlags
& STARTF_USESHOWWINDOW
)
1233 GuiInitInfo
->TermInfo
.ShowWindow
= ConsoleStartInfo
->wShowWindow
;
1235 if (ConsoleStartInfo
->dwStartupFlags
& STARTF_USEPOSITION
)
1237 ConsoleInfo
->AutoPosition
= FALSE
;
1238 ConsoleInfo
->WindowPosition
.x
= ConsoleStartInfo
->dwWindowOrigin
.X
;
1239 ConsoleInfo
->WindowPosition
.y
= ConsoleStartInfo
->dwWindowOrigin
.Y
;
1241 if (ConsoleStartInfo
->dwStartupFlags
& STARTF_RUNFULLSCREEN
)
1243 ConsoleInfo
->FullScreen
= TRUE
;
1249 /* Revert impersonation */
1254 StringCchCopyNW(GuiInitInfo
->TermInfo
.FaceName
, ARRAYSIZE(GuiInitInfo
->TermInfo
.FaceName
),
1255 ConsoleInfo
->FaceName
, ARRAYSIZE(ConsoleInfo
->FaceName
));
1256 GuiInitInfo
->TermInfo
.FontFamily
= ConsoleInfo
->FontFamily
;
1257 GuiInitInfo
->TermInfo
.FontSize
= ConsoleInfo
->FontSize
;
1258 GuiInitInfo
->TermInfo
.FontWeight
= ConsoleInfo
->FontWeight
;
1261 GuiInitInfo
->TermInfo
.FullScreen
= ConsoleInfo
->FullScreen
;
1262 // GuiInitInfo->TermInfo.ShowWindow;
1263 GuiInitInfo
->TermInfo
.AutoPosition
= ConsoleInfo
->AutoPosition
;
1264 GuiInitInfo
->TermInfo
.WindowOrigin
= ConsoleInfo
->WindowPosition
;
1266 /* Initialize the icon handles */
1267 // if (ConsoleStartInfo->hIcon != NULL)
1268 GuiInitInfo
->hIcon
= ConsoleStartInfo
->hIcon
;
1270 // GuiInitInfo->hIcon = ghDefaultIcon;
1272 // if (ConsoleStartInfo->hIconSm != NULL)
1273 GuiInitInfo
->hIconSm
= ConsoleStartInfo
->hIconSm
;
1275 // GuiInitInfo->hIconSm = ghDefaultIconSm;
1277 // ASSERT(GuiInitInfo->hIcon && GuiInitInfo->hIconSm);
1279 GuiInitInfo
->IsWindowVisible
= ConsoleInitInfo
->IsWindowVisible
;
1281 /* Finally, initialize the frontend structure */
1282 FrontEnd
->Vtbl
= &GuiVtbl
;
1283 FrontEnd
->Context
= NULL
;
1284 FrontEnd
->Context2
= GuiInitInfo
;
1286 return STATUS_SUCCESS
;
1290 GuiUnloadFrontEnd(IN OUT PFRONTEND FrontEnd
)
1292 if (FrontEnd
== NULL
) return STATUS_INVALID_PARAMETER
;
1294 if (FrontEnd
->Context
) GuiDeinitFrontEnd(FrontEnd
);
1295 if (FrontEnd
->Context2
) ConsoleFreeHeap(FrontEnd
->Context2
);
1297 return STATUS_SUCCESS
;