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