[CONSRV]: Move all references to VDM screenbuffers from condrv back to consrv (only...
[reactos.git] / win32ss / user / winsrv / consrv / condrv / conoutput.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Console Driver DLL
4 * FILE: win32ss/user/winsrv/consrv/condrv/conoutput.c
5 * PURPOSE: General Console Output Functions
6 * PROGRAMMERS: Jeffrey Morlan
7 * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
8 */
9
10 /* INCLUDES *******************************************************************/
11
12 #include <consrv.h>
13
14 #define NDEBUG
15 #include <debug.h>
16
17 /* PRIVATE FUNCTIONS **********************************************************/
18
19 NTSTATUS
20 TEXTMODE_BUFFER_Initialize(OUT PCONSOLE_SCREEN_BUFFER* Buffer,
21 IN PCONSOLE Console,
22 IN HANDLE ProcessHandle,
23 IN PTEXTMODE_BUFFER_INFO TextModeInfo);
24 NTSTATUS
25 GRAPHICS_BUFFER_Initialize(OUT PCONSOLE_SCREEN_BUFFER* Buffer,
26 IN PCONSOLE Console,
27 IN HANDLE ProcessHandle,
28 IN PGRAPHICS_BUFFER_INFO GraphicsInfo);
29
30 VOID
31 TEXTMODE_BUFFER_Destroy(IN OUT PCONSOLE_SCREEN_BUFFER Buffer);
32 VOID
33 GRAPHICS_BUFFER_Destroy(IN OUT PCONSOLE_SCREEN_BUFFER Buffer);
34
35
36 NTSTATUS
37 CONSOLE_SCREEN_BUFFER_Initialize(OUT PCONSOLE_SCREEN_BUFFER* Buffer,
38 IN PCONSOLE Console,
39 IN PCONSOLE_SCREEN_BUFFER_VTBL Vtbl,
40 IN SIZE_T Size)
41 {
42 if (Buffer == NULL || Console == NULL)
43 return STATUS_INVALID_PARAMETER;
44
45 *Buffer = ConsoleAllocHeap(HEAP_ZERO_MEMORY, max(sizeof(CONSOLE_SCREEN_BUFFER), Size));
46 if (*Buffer == NULL) return STATUS_INSUFFICIENT_RESOURCES;
47
48 /* Initialize the header with the default type */
49 ConSrvInitObject(&(*Buffer)->Header, SCREEN_BUFFER, Console);
50 (*Buffer)->Vtbl = Vtbl;
51 return STATUS_SUCCESS;
52 }
53
54 VOID
55 CONSOLE_SCREEN_BUFFER_Destroy(IN OUT PCONSOLE_SCREEN_BUFFER Buffer)
56 {
57 if (Buffer->Header.Type == TEXTMODE_BUFFER)
58 {
59 TEXTMODE_BUFFER_Destroy(Buffer);
60 }
61 else if (Buffer->Header.Type == GRAPHICS_BUFFER)
62 {
63 GRAPHICS_BUFFER_Destroy(Buffer);
64 }
65 else if (Buffer->Header.Type == SCREEN_BUFFER)
66 {
67 // TODO: Free Buffer->Data
68
69 /* Free the palette handle */
70 if (Buffer->PaletteHandle != NULL) DeleteObject(Buffer->PaletteHandle);
71
72 /* Free the screen buffer memory */
73 ConsoleFreeHeap(Buffer);
74 }
75 // else
76 // do_nothing;
77 }
78
79 // ConDrvCreateConsoleScreenBuffer
80 NTSTATUS
81 ConDrvCreateScreenBuffer(OUT PCONSOLE_SCREEN_BUFFER* Buffer,
82 IN PCONSOLE Console,
83 IN HANDLE ProcessHandle OPTIONAL,
84 IN ULONG BufferType,
85 IN PVOID ScreenBufferInfo)
86 {
87 NTSTATUS Status = STATUS_SUCCESS;
88
89 if ( Console == NULL || Buffer == NULL ||
90 (BufferType != CONSOLE_TEXTMODE_BUFFER && BufferType != CONSOLE_GRAPHICS_BUFFER) )
91 {
92 return STATUS_INVALID_PARAMETER;
93 }
94
95 /* Use the current process if ProcessHandle is NULL */
96 if (ProcessHandle == NULL)
97 ProcessHandle = NtCurrentProcess();
98
99 if (BufferType == CONSOLE_TEXTMODE_BUFFER)
100 {
101 Status = TEXTMODE_BUFFER_Initialize(Buffer, Console, ProcessHandle,
102 (PTEXTMODE_BUFFER_INFO)ScreenBufferInfo);
103 }
104 else if (BufferType == CONSOLE_GRAPHICS_BUFFER)
105 {
106 Status = GRAPHICS_BUFFER_Initialize(Buffer, Console, ProcessHandle,
107 (PGRAPHICS_BUFFER_INFO)ScreenBufferInfo);
108 }
109 else
110 {
111 /* Never ever go there!! */
112 ASSERT(FALSE);
113 }
114
115 /* Insert the newly created screen buffer into the list, if succeeded */
116 if (NT_SUCCESS(Status)) InsertHeadList(&Console->BufferList, &(*Buffer)->ListEntry);
117
118 return Status;
119 }
120
121 static VOID
122 ConioSetActiveScreenBuffer(PCONSOLE_SCREEN_BUFFER Buffer);
123
124 VOID NTAPI
125 ConDrvDeleteScreenBuffer(PCONSOLE_SCREEN_BUFFER Buffer)
126 {
127 PCONSOLE Console = Buffer->Header.Console;
128 PCONSOLE_SCREEN_BUFFER NewBuffer;
129
130 /*
131 * We should notify temporarily the frontend because we are susceptible
132 * to delete the screen buffer it is using (which may be different from
133 * the active screen buffer in some cases), and because, if it actually
134 * uses the active screen buffer, we are going to nullify its pointer to
135 * change it.
136 */
137 TermReleaseScreenBuffer(Console, Buffer);
138
139 RemoveEntryList(&Buffer->ListEntry);
140 if (Buffer == Console->ActiveBuffer)
141 {
142 /* Delete active buffer; switch to most recently created */
143 if (!IsListEmpty(&Console->BufferList))
144 {
145 NewBuffer = CONTAINING_RECORD(Console->BufferList.Flink,
146 CONSOLE_SCREEN_BUFFER,
147 ListEntry);
148
149 /* Tie console to new buffer and signal the change to the frontend */
150 ConioSetActiveScreenBuffer(NewBuffer);
151 }
152 else
153 {
154 Console->ActiveBuffer = NULL;
155 // InterlockedExchangePointer(&Console->ActiveBuffer, NULL);
156 }
157 }
158
159 CONSOLE_SCREEN_BUFFER_Destroy(Buffer);
160 }
161
162 VOID
163 ConioDrawConsole(PCONSOLE Console)
164 {
165 SMALL_RECT Region;
166 PCONSOLE_SCREEN_BUFFER ActiveBuffer = Console->ActiveBuffer;
167
168 if (ActiveBuffer)
169 {
170 ConioInitRect(&Region, 0, 0,
171 ActiveBuffer->ViewSize.Y - 1, ActiveBuffer->ViewSize.X - 1);
172 TermDrawRegion(Console, &Region);
173 }
174 }
175
176 static VOID
177 ConioSetActiveScreenBuffer(PCONSOLE_SCREEN_BUFFER Buffer)
178 {
179 PCONSOLE Console = Buffer->Header.Console;
180 Console->ActiveBuffer = Buffer;
181 // InterlockedExchangePointer(&Console->ActiveBuffer, Buffer);
182 TermSetActiveScreenBuffer(Console);
183 }
184
185 NTSTATUS NTAPI
186 ConDrvSetConsoleActiveScreenBuffer(IN PCONSOLE Console,
187 IN PCONSOLE_SCREEN_BUFFER Buffer)
188 {
189 if (Console == NULL || Buffer == NULL)
190 return STATUS_INVALID_PARAMETER;
191
192 /* Validity check */
193 ASSERT(Console == Buffer->Header.Console);
194
195 if (Buffer == Console->ActiveBuffer) return STATUS_SUCCESS;
196
197 /* If old buffer has no handles, it's now unreferenced */
198 if (Console->ActiveBuffer->Header.ReferenceCount == 0)
199 {
200 ConDrvDeleteScreenBuffer(Console->ActiveBuffer);
201 }
202
203 /* Tie console to new buffer and signal the change to the frontend */
204 ConioSetActiveScreenBuffer(Buffer);
205
206 return STATUS_SUCCESS;
207 }
208
209 PCONSOLE_SCREEN_BUFFER
210 ConDrvGetActiveScreenBuffer(IN PCONSOLE Console)
211 {
212 return (Console ? Console->ActiveBuffer : NULL);
213 }
214
215 /* PUBLIC DRIVER APIS *********************************************************/
216
217 NTSTATUS NTAPI
218 ConDrvInvalidateBitMapRect(IN PCONSOLE Console,
219 IN PCONSOLE_SCREEN_BUFFER Buffer,
220 IN PSMALL_RECT Region)
221 {
222 if (Console == NULL || Buffer == NULL || Region == NULL)
223 return STATUS_INVALID_PARAMETER;
224
225 /* Validity check */
226 ASSERT(Console == Buffer->Header.Console);
227
228 /* If the output buffer is the current one, redraw the correct portion of the screen */
229 if (Buffer == Console->ActiveBuffer) TermDrawRegion(Console, Region);
230
231 return STATUS_SUCCESS;
232 }
233
234 NTSTATUS NTAPI
235 ConDrvSetConsolePalette(IN PCONSOLE Console,
236 // IN PGRAPHICS_SCREEN_BUFFER Buffer,
237 IN PCONSOLE_SCREEN_BUFFER Buffer,
238 IN HPALETTE PaletteHandle,
239 IN UINT PaletteUsage)
240 {
241 BOOL Success;
242
243 /*
244 * Parameters validation
245 */
246 if (Console == NULL || Buffer == NULL)
247 return STATUS_INVALID_PARAMETER;
248
249 if ( PaletteUsage != SYSPAL_STATIC &&
250 PaletteUsage != SYSPAL_NOSTATIC &&
251 PaletteUsage != SYSPAL_NOSTATIC256 )
252 {
253 return STATUS_INVALID_PARAMETER;
254 }
255
256 /* Validity check */
257 ASSERT(Console == Buffer->Header.Console);
258
259 /* Change the palette */
260 Success = TermSetPalette(Console, PaletteHandle, PaletteUsage);
261 if (Success)
262 {
263 /* Free the old palette handle if there was already one set */
264 if ( Buffer->PaletteHandle != NULL &&
265 Buffer->PaletteHandle != PaletteHandle )
266 {
267 DeleteObject(Buffer->PaletteHandle);
268 }
269
270 /* Save the new palette in the screen buffer */
271 Buffer->PaletteHandle = PaletteHandle;
272 Buffer->PaletteUsage = PaletteUsage;
273 }
274
275 return (Success ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL);
276 }
277
278 NTSTATUS NTAPI
279 ConDrvGetConsoleCursorInfo(IN PCONSOLE Console,
280 IN PTEXTMODE_SCREEN_BUFFER Buffer,
281 OUT PCONSOLE_CURSOR_INFO CursorInfo)
282 {
283 if (Console == NULL || Buffer == NULL || CursorInfo == NULL)
284 return STATUS_INVALID_PARAMETER;
285
286 /* Validity check */
287 ASSERT(Console == Buffer->Header.Console);
288
289 *CursorInfo = Buffer->CursorInfo;
290 // CursorInfo->bVisible = Buffer->CursorInfo.bVisible;
291 // CursorInfo->dwSize = Buffer->CursorInfo.dwSize;
292
293 return STATUS_SUCCESS;
294 }
295
296 NTSTATUS NTAPI
297 ConDrvSetConsoleCursorInfo(IN PCONSOLE Console,
298 IN PTEXTMODE_SCREEN_BUFFER Buffer,
299 IN PCONSOLE_CURSOR_INFO CursorInfo)
300 {
301 ULONG Size;
302 BOOLEAN Visible, Success = TRUE;
303
304 if (Console == NULL || Buffer == NULL || CursorInfo == NULL)
305 return STATUS_INVALID_PARAMETER;
306
307 /* Validity check */
308 ASSERT(Console == Buffer->Header.Console);
309
310 Size = min(max(CursorInfo->dwSize, 1), 100);
311 Visible = CursorInfo->bVisible;
312
313 if ( (Size != Buffer->CursorInfo.dwSize) ||
314 (Visible && !Buffer->CursorInfo.bVisible) ||
315 (!Visible && Buffer->CursorInfo.bVisible) )
316 {
317 Buffer->CursorInfo.dwSize = Size;
318 Buffer->CursorInfo.bVisible = Visible;
319
320 Success = TermSetCursorInfo(Console, (PCONSOLE_SCREEN_BUFFER)Buffer);
321 }
322
323 return (Success ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL);
324 }
325
326 NTSTATUS NTAPI
327 ConDrvSetConsoleCursorPosition(IN PCONSOLE Console,
328 IN PTEXTMODE_SCREEN_BUFFER Buffer,
329 IN PCOORD Position)
330 {
331 SHORT OldCursorX, OldCursorY;
332
333 if (Console == NULL || Buffer == NULL || Position == NULL)
334 return STATUS_INVALID_PARAMETER;
335
336 /* Validity check */
337 ASSERT(Console == Buffer->Header.Console);
338
339 if ( Position->X < 0 || Position->X >= Buffer->ScreenBufferSize.X ||
340 Position->Y < 0 || Position->Y >= Buffer->ScreenBufferSize.Y )
341 {
342 return STATUS_INVALID_PARAMETER;
343 }
344
345 OldCursorX = Buffer->CursorPosition.X;
346 OldCursorY = Buffer->CursorPosition.Y;
347 Buffer->CursorPosition = *Position;
348 // Buffer->CursorPosition.X = Position->X;
349 // Buffer->CursorPosition.Y = Position->Y;
350 if ( ((PCONSOLE_SCREEN_BUFFER)Buffer == Console->ActiveBuffer) &&
351 (!TermSetScreenInfo(Console, (PCONSOLE_SCREEN_BUFFER)Buffer, OldCursorX, OldCursorY)) )
352 {
353 return STATUS_UNSUCCESSFUL;
354 }
355
356 return STATUS_SUCCESS;
357 }
358
359 /* EOF */