fixed some signed/unsigned comparison warnings with -Wsign-compare
[reactos.git] / reactos / subsys / csrss / win32csr / guiconsole.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: subsys/csrss/win32csr/guiconsole.c
6 * PURPOSE: Implementation of gui-mode consoles
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include "w32csr.h"
12
13 #define NDEBUG
14 #include <debug.h>
15
16 /* Not defined in any header file */
17 extern VOID STDCALL PrivateCsrssManualGuiCheck(LONG Check);
18
19 /* GLOBALS *******************************************************************/
20
21 typedef struct GUI_CONSOLE_DATA_TAG
22 {
23 HFONT Font;
24 unsigned CharWidth;
25 unsigned CharHeight;
26 PWCHAR LineBuffer;
27 BOOL CursorBlinkOn;
28 BOOL ForceCursorOff;
29 CRITICAL_SECTION Lock;
30 HDC MemoryDC;
31 HBITMAP MemoryBitmap;
32 RECT Selection;
33 POINT SelectionStart;
34 BOOL MouseDown;
35 } GUI_CONSOLE_DATA, *PGUI_CONSOLE_DATA;
36
37 #ifndef WM_APP
38 #define WM_APP 0x8000
39 #endif
40 #define PM_CREATE_CONSOLE (WM_APP + 1)
41 #define PM_DESTROY_CONSOLE (WM_APP + 2)
42
43 #define CURSOR_BLINK_TIME 500
44
45 static BOOL ConsInitialized = FALSE;
46 static HWND NotifyWnd;
47
48 /* FUNCTIONS *****************************************************************/
49
50 static VOID FASTCALL
51 GuiConsoleGetDataPointers(HWND hWnd, PCSRSS_CONSOLE *Console, PGUI_CONSOLE_DATA *GuiData)
52 {
53 *Console = (PCSRSS_CONSOLE) GetWindowLongPtrW(hWnd, GWL_USERDATA);
54 *GuiData = (NULL == *Console ? NULL : (*Console)->PrivateData);
55 }
56
57 static BOOL FASTCALL
58 GuiConsoleHandleNcCreate(HWND hWnd, CREATESTRUCTW *Create)
59 {
60 RECT Rect;
61 PCSRSS_CONSOLE Console = (PCSRSS_CONSOLE) Create->lpCreateParams;
62 PGUI_CONSOLE_DATA GuiData;
63 HDC Dc;
64 HFONT OldFont;
65 TEXTMETRICW Metrics;
66
67 GuiData = HeapAlloc(Win32CsrApiHeap, HEAP_ZERO_MEMORY,
68 sizeof(GUI_CONSOLE_DATA) +
69 (Console->Size.X + 1) * sizeof(WCHAR));
70 if (NULL == GuiData)
71 {
72 DPRINT1("GuiConsoleNcCreate: HeapAlloc failed\n");
73 return FALSE;
74 }
75
76 InitializeCriticalSection(&GuiData->Lock);
77
78 GuiData->LineBuffer = (PWCHAR)(GuiData + 1);
79
80 GuiData->Font = CreateFontW(12, 0, 0, TA_BASELINE, FW_NORMAL,
81 FALSE, FALSE, FALSE, OEM_CHARSET,
82 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
83 NONANTIALIASED_QUALITY, FIXED_PITCH | FF_DONTCARE,
84 L"Bitstream Vera Sans Mono");
85 if (NULL == GuiData->Font)
86 {
87 DPRINT1("GuiConsoleNcCreate: CreateFont failed\n");
88 DeleteCriticalSection(&GuiData->Lock);
89 HeapFree(Win32CsrApiHeap, 0, GuiData);
90 return FALSE;
91 }
92 Dc = GetDC(hWnd);
93 if (NULL == Dc)
94 {
95 DPRINT1("GuiConsoleNcCreate: GetDC failed\n");
96 DeleteObject(GuiData->Font);
97 DeleteCriticalSection(&GuiData->Lock);
98 HeapFree(Win32CsrApiHeap, 0, GuiData);
99 return FALSE;
100 }
101 OldFont = SelectObject(Dc, GuiData->Font);
102 if (NULL == OldFont)
103 {
104 DPRINT1("GuiConsoleNcCreate: SelectObject failed\n");
105 ReleaseDC(hWnd, Dc);
106 DeleteObject(GuiData->Font);
107 DeleteCriticalSection(&GuiData->Lock);
108 HeapFree(Win32CsrApiHeap, 0, GuiData);
109 return FALSE;
110 }
111 if (! GetTextMetricsW(Dc, &Metrics))
112 {
113 DPRINT1("GuiConsoleNcCreate: GetTextMetrics failed\n");
114 SelectObject(Dc, OldFont);
115 ReleaseDC(hWnd, Dc);
116 DeleteObject(GuiData->Font);
117 DeleteCriticalSection(&GuiData->Lock);
118 HeapFree(Win32CsrApiHeap, 0, GuiData);
119 return FALSE;
120 }
121 GuiData->CharWidth = Metrics.tmMaxCharWidth;
122 GuiData->CharHeight = Metrics.tmHeight + Metrics.tmExternalLeading;
123 SelectObject(Dc, OldFont);
124
125 GuiData->MemoryDC = CreateCompatibleDC(Dc);
126 GuiData->MemoryBitmap = CreateCompatibleBitmap(Dc,
127 Console->Size.X * GuiData->CharWidth,
128 Console->Size.Y * GuiData->CharHeight);
129 /* NOTE: Don't delete the "first bitmap", it's done in DeleteDC. */
130 SelectObject(GuiData->MemoryDC, GuiData->MemoryBitmap);
131 /* NOTE: Don't delete stock font. */
132 SelectObject(GuiData->MemoryDC, GuiData->Font);
133
134 ReleaseDC(hWnd, Dc);
135 GuiData->CursorBlinkOn = TRUE;
136 GuiData->ForceCursorOff = FALSE;
137
138 GuiData->Selection.left = -1;
139
140 Console->PrivateData = GuiData;
141 SetWindowLongPtrW(hWnd, GWL_USERDATA, (DWORD_PTR) Console);
142
143 GetWindowRect(hWnd, &Rect);
144 Rect.right = Rect.left + Console->Size.X * GuiData->CharWidth +
145 2 * GetSystemMetrics(SM_CXFIXEDFRAME);
146 Rect.bottom = Rect.top + Console->Size.Y * GuiData->CharHeight +
147 2 * GetSystemMetrics(SM_CYFIXEDFRAME) + GetSystemMetrics(SM_CYCAPTION);
148 MoveWindow(hWnd, Rect.left, Rect.top, Rect.right - Rect.left,
149 Rect.bottom - Rect.top, FALSE);
150
151 SetTimer(hWnd, 1, CURSOR_BLINK_TIME, NULL);
152
153 return (BOOL) DefWindowProcW(hWnd, WM_NCCREATE, 0, (LPARAM) Create);
154 }
155
156 static COLORREF FASTCALL
157 GuiConsoleRGBFromAttribute(BYTE Attribute)
158 {
159 int Red = (Attribute & 0x04 ? (Attribute & 0x08 ? 0xff : 0x80) : 0x00);
160 int Green = (Attribute & 0x02 ? (Attribute & 0x08 ? 0xff : 0x80) : 0x00);
161 int Blue = (Attribute & 0x01 ? (Attribute & 0x08 ? 0xff : 0x80) : 0x00);
162
163 return RGB(Red, Green, Blue);
164 }
165
166 static VOID FASTCALL
167 GuiConsoleSetTextColors(HDC Dc, BYTE Attribute)
168 {
169 SetTextColor(Dc, GuiConsoleRGBFromAttribute(Attribute & 0x0f));
170 SetBkColor(Dc, GuiConsoleRGBFromAttribute((Attribute & 0xf0) >> 4));
171 }
172
173 static VOID FASTCALL
174 GuiConsoleGetLogicalCursorPos(PCSRSS_SCREEN_BUFFER Buff, ULONG *CursorX, ULONG *CursorY)
175 {
176 *CursorX = Buff->CurrentX;
177 if (Buff->CurrentY < Buff->ShowY)
178 {
179 *CursorY = Buff->MaxY - Buff->ShowY + Buff->CurrentY;
180 }
181 else
182 {
183 *CursorY = Buff->CurrentY - Buff->ShowY;
184 }
185 }
186
187
188 static VOID FASTCALL
189 GuiConsoleUpdateSelection(HWND hWnd, PRECT rc, PGUI_CONSOLE_DATA GuiData)
190 {
191 RECT oldRect = GuiData->Selection;
192
193 if(rc != NULL)
194 {
195 RECT changeRect = *rc;
196
197 GuiData->Selection = *rc;
198
199 changeRect.left *= GuiData->CharWidth;
200 changeRect.top *= GuiData->CharHeight;
201 changeRect.right *= GuiData->CharWidth;
202 changeRect.bottom *= GuiData->CharHeight;
203
204 if(rc->left != oldRect.left ||
205 rc->top != oldRect.top ||
206 rc->right != oldRect.right ||
207 rc->bottom != oldRect.bottom)
208 {
209 if(oldRect.left != -1)
210 {
211 HRGN rgn1, rgn2;
212
213 oldRect.left *= GuiData->CharWidth;
214 oldRect.top *= GuiData->CharHeight;
215 oldRect.right *= GuiData->CharWidth;
216 oldRect.bottom *= GuiData->CharHeight;
217
218 /* calculate the region that needs to be updated */
219 if((rgn1 = CreateRectRgnIndirect(&oldRect)))
220 {
221 if((rgn2 = CreateRectRgnIndirect(&changeRect)))
222 {
223 if(CombineRgn(rgn1, rgn2, rgn1, RGN_XOR) != ERROR)
224 {
225 InvalidateRgn(hWnd, rgn1, FALSE);
226 }
227
228 DeleteObject(rgn2);
229 }
230 DeleteObject(rgn1);
231 }
232 }
233 else
234 {
235 InvalidateRect(hWnd, &changeRect, FALSE);
236 }
237 }
238 }
239 else if(oldRect.left != -1)
240 {
241 /* clear the selection */
242 GuiData->Selection.left = -1;
243 oldRect.left *= GuiData->CharWidth;
244 oldRect.top *= GuiData->CharHeight;
245 oldRect.right *= GuiData->CharWidth;
246 oldRect.bottom *= GuiData->CharHeight;
247 InvalidateRect(hWnd, &oldRect, FALSE);
248 }
249 }
250
251
252 VOID FASTCALL
253 GuiConsoleUpdateBitmap(HWND hWnd, RECT rc)
254 {
255 PCSRSS_CONSOLE Console;
256 PGUI_CONSOLE_DATA GuiData;
257 PCSRSS_SCREEN_BUFFER Buff;
258 HDC Dc;
259 ULONG TopLine, BottomLine, LeftChar, RightChar;
260 ULONG Line, Char, Start;
261 PBYTE From;
262 PWCHAR To;
263 BYTE LastAttribute, Attribute;
264 ULONG CursorX, CursorY, CursorHeight;
265 HBRUSH CursorBrush, OldBrush;
266
267 GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
268 if (NULL != Console && NULL != GuiData && NULL != Console->ActiveBuffer)
269 {
270 Buff = Console->ActiveBuffer;
271 EnterCriticalSection(&Buff->Header.Lock);
272 Dc = GetDC(hWnd);
273 if (rc.right <= rc.left || rc.bottom <= rc.top)
274 {
275 ReleaseDC(hWnd, Dc);
276 LeaveCriticalSection(&Buff->Header.Lock);
277 return;
278 }
279
280 EnterCriticalSection(&GuiData->Lock);
281
282 TopLine = rc.top / GuiData->CharHeight;
283 BottomLine = (rc.bottom + (GuiData->CharHeight - 1)) / GuiData->CharHeight - 1;
284 LeftChar = rc.left / GuiData->CharWidth;
285 RightChar = (rc.right + (GuiData->CharWidth - 1)) / GuiData->CharWidth - 1;
286 LastAttribute = Buff->Buffer[(TopLine * Buff->MaxX + LeftChar) * 2 + 1];
287 GuiConsoleSetTextColors(GuiData->MemoryDC, LastAttribute);
288
289 for (Line = TopLine; Line <= BottomLine; Line++)
290 {
291 if (Line + Buff->ShowY < Buff->MaxY)
292 {
293 From = Buff->Buffer + ((Line + Buff->ShowY) * Buff->MaxX + LeftChar) * 2;
294 }
295 else
296 {
297 From = Buff->Buffer +
298 ((Line - (Buff->MaxY - Buff->ShowY)) * Buff->MaxX + LeftChar) * 2;
299 }
300 Start = LeftChar;
301 To = GuiData->LineBuffer;
302 for (Char = LeftChar; Char <= RightChar; Char++)
303 {
304 if (*(From + 1) != LastAttribute)
305 {
306 TextOutW(GuiData->MemoryDC, Start * GuiData->CharWidth, Line * GuiData->CharHeight,
307 GuiData->LineBuffer, Char - Start);
308 Start = Char;
309 To = GuiData->LineBuffer;
310 Attribute = *(From + 1);
311 if (Attribute != LastAttribute)
312 {
313 GuiConsoleSetTextColors(GuiData->MemoryDC, Attribute);
314 LastAttribute = Attribute;
315 }
316 }
317 MultiByteToWideChar(Console->OutputCodePage, 0, (PCHAR)From, 1, To, 1);
318 To++;
319 From += 2;
320 }
321 TextOutW(GuiData->MemoryDC, Start * GuiData->CharWidth, Line * GuiData->CharHeight,
322 GuiData->LineBuffer, RightChar - Start + 1);
323 }
324
325 if (Buff->CursorInfo.bVisible && GuiData->CursorBlinkOn
326 &&! GuiData->ForceCursorOff)
327 {
328 GuiConsoleGetLogicalCursorPos(Buff, &CursorX, &CursorY);
329 if (LeftChar <= CursorX && CursorX <= RightChar &&
330 TopLine <= CursorY && CursorY <= BottomLine)
331 {
332 CursorHeight = (GuiData->CharHeight * Buff->CursorInfo.dwSize) / 100;
333 if (CursorHeight < 1)
334 {
335 CursorHeight = 1;
336 }
337 From = Buff->Buffer + (Buff->CurrentY * Buff->MaxX + Buff->CurrentX) * 2 + 1;
338 CursorBrush = CreateSolidBrush(GuiConsoleRGBFromAttribute(*From));
339 OldBrush = SelectObject(GuiData->MemoryDC, CursorBrush);
340 PatBlt(GuiData->MemoryDC, CursorX * GuiData->CharWidth,
341 CursorY * GuiData->CharHeight + (GuiData->CharHeight - CursorHeight),
342 GuiData->CharWidth, CursorHeight, PATCOPY);
343 SelectObject(GuiData->MemoryDC, OldBrush);
344 DeleteObject(CursorBrush);
345 }
346 }
347
348 LeaveCriticalSection(&GuiData->Lock);
349 ReleaseDC(hWnd, Dc);
350 LeaveCriticalSection(&Buff->Header.Lock);
351 InvalidateRect(hWnd, &rc, FALSE);
352 }
353 }
354
355 VOID FASTCALL
356 GuiConsoleHandlePaint(HWND hWnd)
357 {
358 PAINTSTRUCT Ps;
359 HDC Dc;
360 PCSRSS_CONSOLE Console;
361 PGUI_CONSOLE_DATA GuiData;
362
363 GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
364 if (NULL != Console && NULL != GuiData)
365 {
366 EnterCriticalSection(&GuiData->Lock);
367 Dc = BeginPaint (hWnd, &Ps);
368 BitBlt(Dc, Ps.rcPaint.left, Ps.rcPaint.top,
369 Ps.rcPaint.right - Ps.rcPaint.left + 1,
370 Ps.rcPaint.bottom - Ps.rcPaint.top + 1, GuiData->MemoryDC,
371 Ps.rcPaint.left, Ps.rcPaint.top, SRCCOPY);
372
373 if (GuiData->Selection.left != -1)
374 {
375 RECT rc = GuiData->Selection;
376
377 rc.left *= GuiData->CharWidth;
378 rc.top *= GuiData->CharHeight;
379 rc.right *= GuiData->CharWidth;
380 rc.bottom *= GuiData->CharHeight;
381
382 if (IntersectRect(&rc, &Ps.rcPaint, &rc))
383 {
384 PatBlt(Dc, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, DSTINVERT);
385 }
386 }
387
388 EndPaint (hWnd, &Ps);
389 LeaveCriticalSection(&GuiData->Lock);
390 }
391 else
392 {
393 Dc = BeginPaint (hWnd, &Ps);
394 EndPaint (hWnd, &Ps);
395 }
396 }
397
398 static VOID FASTCALL
399 GuiConsoleHandleKey(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
400 {
401 PCSRSS_CONSOLE Console;
402 PGUI_CONSOLE_DATA GuiData;
403 MSG Message;
404
405 GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
406 Message.hwnd = hWnd;
407 Message.message = msg;
408 Message.wParam = wParam;
409 Message.lParam = lParam;
410
411 if(msg == WM_CHAR || msg == WM_SYSKEYDOWN)
412 {
413 /* clear the selection */
414 GuiConsoleUpdateSelection(hWnd, NULL, GuiData);
415 }
416
417 ConioProcessKey(&Message, Console, FALSE);
418 }
419
420 static VOID FASTCALL
421 GuiIntDrawRegion(PGUI_CONSOLE_DATA GuiData, HWND Wnd, RECT *Region)
422 {
423 RECT RegionRect;
424
425 RegionRect.left = Region->left * GuiData->CharWidth;
426 RegionRect.top = Region->top * GuiData->CharHeight;
427 RegionRect.right = (Region->right + 1) * GuiData->CharWidth;
428 RegionRect.bottom = (Region->bottom + 1) * GuiData->CharHeight;
429
430 GuiConsoleUpdateBitmap(Wnd, RegionRect);
431 }
432
433 static VOID STDCALL
434 GuiDrawRegion(PCSRSS_CONSOLE Console, RECT *Region)
435 {
436 PGUI_CONSOLE_DATA GuiData = (PGUI_CONSOLE_DATA) Console->PrivateData;
437
438 if (NULL == Console->hWindow || NULL == GuiData)
439 {
440 return;
441 }
442
443 GuiIntDrawRegion(GuiData, Console->hWindow, Region);
444 }
445
446 static VOID FASTCALL
447 GuiInvalidateCell(PGUI_CONSOLE_DATA GuiData, HWND Wnd, UINT x, UINT y)
448 {
449 RECT CellRect;
450
451 CellRect.left = x;
452 CellRect.top = y;
453 CellRect.right = x;
454 CellRect.bottom = y;
455
456 GuiIntDrawRegion(GuiData, Wnd, &CellRect);
457 }
458
459 static VOID STDCALL
460 GuiWriteStream(PCSRSS_CONSOLE Console, RECT *Region, LONG CursorStartX, LONG CursorStartY,
461 UINT ScrolledLines, CHAR *Buffer, UINT Length)
462 {
463 PGUI_CONSOLE_DATA GuiData = (PGUI_CONSOLE_DATA) Console->PrivateData;
464 PCSRSS_SCREEN_BUFFER Buff = Console->ActiveBuffer;
465 LONG CursorEndX, CursorEndY;
466 RECT Source, Dest;
467
468 if (NULL == Console->hWindow || NULL == GuiData)
469 {
470 return;
471 }
472
473 if (0 != ScrolledLines)
474 {
475 Source.left = 0;
476 Source.top = ScrolledLines;
477 Source.right = Console->Size.X - 1;
478 Source.bottom = ScrolledLines + Region->top - 1;
479 Dest.left = 0;
480 Dest.top = 0;
481 Dest.right = Console->Size.X - 1;
482 Dest.bottom = Region->top - 1;
483
484 GuiConsoleCopyRegion(Console->hWindow, &Source, &Dest);
485 }
486
487 GuiIntDrawRegion(GuiData, Console->hWindow, Region);
488
489 if (CursorStartX < Region->left || Region->right < CursorStartX
490 || CursorStartY < Region->top || Region->bottom < CursorStartY)
491 {
492 GuiInvalidateCell(GuiData, Console->hWindow, CursorStartX, CursorStartY);
493 }
494
495 ConioPhysicalToLogical(Buff, Buff->CurrentX, Buff->CurrentY,
496 &CursorEndX, &CursorEndY);
497 if ((CursorEndX < Region->left || Region->right < CursorEndX
498 || CursorEndY < Region->top || Region->bottom < CursorEndY)
499 && (CursorEndX != CursorStartX || CursorEndY != CursorStartY))
500 {
501 GuiInvalidateCell(GuiData, Console->hWindow, CursorEndX, CursorEndY);
502 }
503 }
504
505 static BOOL STDCALL
506 GuiSetCursorInfo(PCSRSS_CONSOLE Console, PCSRSS_SCREEN_BUFFER Buff)
507 {
508 RECT UpdateRect;
509
510 if (Console->ActiveBuffer == Buff)
511 {
512 ConioPhysicalToLogical(Buff, Buff->CurrentX, Buff->CurrentY,
513 &UpdateRect.left, &UpdateRect.top);
514 UpdateRect.right = UpdateRect.left;
515 UpdateRect.bottom = UpdateRect.top;
516 ConioDrawRegion(Console, &UpdateRect);
517 }
518
519 return TRUE;
520 }
521
522 static BOOL STDCALL
523 GuiSetScreenInfo(PCSRSS_CONSOLE Console, PCSRSS_SCREEN_BUFFER Buff, UINT OldCursorX, UINT OldCursorY)
524 {
525 RECT UpdateRect;
526
527 if (Console->ActiveBuffer == Buff)
528 {
529 /* Redraw char at old position (removes cursor) */
530 UpdateRect.left = OldCursorX;
531 UpdateRect.top = OldCursorY;
532 UpdateRect.right = OldCursorX;
533 UpdateRect.bottom = OldCursorY;
534 ConioDrawRegion(Console, &UpdateRect);
535 /* Redraw char at new position (shows cursor) */
536 ConioPhysicalToLogical(Buff, Buff->CurrentX, Buff->CurrentY,
537 &(UpdateRect.left), &(UpdateRect.top));
538 UpdateRect.right = UpdateRect.left;
539 UpdateRect.bottom = UpdateRect.top;
540 ConioDrawRegion(Console, &UpdateRect);
541 }
542
543 return TRUE;
544 }
545
546 static VOID FASTCALL
547 GuiConsoleHandleTimer(HWND hWnd)
548 {
549 PCSRSS_CONSOLE Console;
550 PGUI_CONSOLE_DATA GuiData;
551 RECT CursorRect;
552 ULONG CursorX, CursorY;
553
554 GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
555 GuiData->CursorBlinkOn = ! GuiData->CursorBlinkOn;
556
557 GuiConsoleGetLogicalCursorPos(Console->ActiveBuffer, &CursorX, &CursorY);
558 CursorRect.left = CursorX;
559 CursorRect.top = CursorY;
560 CursorRect.right = CursorX;
561 CursorRect.bottom = CursorY;
562 GuiDrawRegion(Console, &CursorRect);
563 }
564
565 static VOID FASTCALL
566 GuiConsoleHandleClose(HWND hWnd)
567 {
568 PCSRSS_CONSOLE Console;
569 PGUI_CONSOLE_DATA GuiData;
570 PLIST_ENTRY current_entry;
571 PCSRSS_PROCESS_DATA current;
572
573 GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
574
575 EnterCriticalSection(&Console->Header.Lock);
576
577 current_entry = Console->ProcessList.Flink;
578 while (current_entry != &Console->ProcessList)
579 {
580 current = CONTAINING_RECORD(current_entry, CSRSS_PROCESS_DATA, ProcessEntry);
581 current_entry = current_entry->Flink;
582
583 ConioConsoleCtrlEvent(CTRL_CLOSE_EVENT, current);
584 }
585
586 LeaveCriticalSection(&Console->Header.Lock);
587 }
588
589 static VOID FASTCALL
590 GuiConsoleHandleNcDestroy(HWND hWnd)
591 {
592 PCSRSS_CONSOLE Console;
593 PGUI_CONSOLE_DATA GuiData;
594
595 GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
596 KillTimer(hWnd, 1);
597 Console->PrivateData = NULL;
598 DeleteDC(GuiData->MemoryDC);
599 DeleteCriticalSection(&GuiData->Lock);
600 HeapFree(Win32CsrApiHeap, 0, GuiData);
601 }
602
603 static VOID FASTCALL
604 GuiConsoleLeftMouseDown(HWND hWnd, LPARAM lParam)
605 {
606 PCSRSS_CONSOLE Console;
607 PGUI_CONSOLE_DATA GuiData;
608 POINTS pt;
609 RECT rc;
610
611 GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
612 if (Console == NULL || GuiData == NULL) return;
613
614 pt = MAKEPOINTS(lParam);
615
616 rc.left = pt.x / GuiData->CharWidth;
617 rc.top = pt.y / GuiData->CharHeight;
618 rc.right = rc.left + 1;
619 rc.bottom = rc.top + 1;
620
621 GuiData->SelectionStart.x = rc.left;
622 GuiData->SelectionStart.y = rc.top;
623
624 SetCapture(hWnd);
625
626 GuiData->MouseDown = TRUE;
627
628 GuiConsoleUpdateSelection(hWnd, &rc, GuiData);
629 }
630
631 static VOID FASTCALL
632 GuiConsoleLeftMouseUp(HWND hWnd, LPARAM lParam)
633 {
634 PCSRSS_CONSOLE Console;
635 PGUI_CONSOLE_DATA GuiData;
636 RECT rc;
637 POINTS pt;
638
639 GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
640 if (Console == NULL || GuiData == NULL) return;
641 if (GuiData->Selection.left == -1 || !GuiData->MouseDown) return;
642
643 pt = MAKEPOINTS(lParam);
644
645 rc.left = GuiData->SelectionStart.x;
646 rc.top = GuiData->SelectionStart.y;
647 rc.right = (pt.x >= 0 ? (pt.x / GuiData->CharWidth) + 1 : 0);
648 rc.bottom = (pt.y >= 0 ? (pt.y / GuiData->CharHeight) + 1 : 0);
649
650 /* exchange left/top with right/bottom if required */
651 if(rc.left >= rc.right)
652 {
653 LONG tmp;
654 tmp = rc.left;
655 rc.left = max(rc.right - 1, 0);
656 rc.right = tmp + 1;
657 }
658 if(rc.top >= rc.bottom)
659 {
660 LONG tmp;
661 tmp = rc.top;
662 rc.top = max(rc.bottom - 1, 0);
663 rc.bottom = tmp + 1;
664 }
665
666 GuiData->MouseDown = FALSE;
667
668 GuiConsoleUpdateSelection(hWnd, &rc, GuiData);
669
670 ReleaseCapture();
671 }
672
673 static VOID FASTCALL
674 GuiConsoleMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam)
675 {
676 PCSRSS_CONSOLE Console;
677 PGUI_CONSOLE_DATA GuiData;
678 RECT rc;
679 POINTS pt;
680
681 if (!(wParam & MK_LBUTTON)) return;
682
683 GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
684 if (Console == NULL || GuiData == NULL || !GuiData->MouseDown) return;
685
686 pt = MAKEPOINTS(lParam);
687
688 rc.left = GuiData->SelectionStart.x;
689 rc.top = GuiData->SelectionStart.y;
690 rc.right = (pt.x >= 0 ? (pt.x / GuiData->CharWidth) + 1 : 0);
691 rc.bottom = (pt.y >= 0 ? (pt.y / GuiData->CharHeight) + 1 : 0);
692
693 /* exchange left/top with right/bottom if required */
694 if(rc.left >= rc.right)
695 {
696 LONG tmp;
697 tmp = rc.left;
698 rc.left = max(rc.right - 1, 0);
699 rc.right = tmp + 1;
700 }
701 if(rc.top >= rc.bottom)
702 {
703 LONG tmp;
704 tmp = rc.top;
705 rc.top = max(rc.bottom - 1, 0);
706 rc.bottom = tmp + 1;
707 }
708
709 GuiConsoleUpdateSelection(hWnd, &rc, GuiData);
710 }
711
712 static VOID FASTCALL
713 GuiConsoleRightMouseDown(HWND hWnd)
714 {
715 PCSRSS_CONSOLE Console;
716 PGUI_CONSOLE_DATA GuiData;
717
718 GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
719 if (Console == NULL || GuiData == NULL) return;
720
721 if (GuiData->Selection.left == -1)
722 {
723 /* FIXME - paste text from clipboard */
724 }
725 else
726 {
727 /* FIXME - copy selection to clipboard */
728
729 GuiConsoleUpdateSelection(hWnd, NULL, GuiData);
730 }
731
732 }
733
734 static LRESULT CALLBACK
735 GuiConsoleWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
736 {
737 LRESULT Result = 0;
738
739 switch(msg)
740 {
741 case WM_NCCREATE:
742 Result = (LRESULT) GuiConsoleHandleNcCreate(hWnd, (CREATESTRUCTW *) lParam);
743 break;
744 case WM_PAINT:
745 GuiConsoleHandlePaint(hWnd);
746 break;
747 case WM_KEYDOWN:
748 case WM_KEYUP:
749 case WM_SYSKEYDOWN:
750 case WM_SYSKEYUP:
751 case WM_CHAR:
752 GuiConsoleHandleKey(hWnd, msg, wParam, lParam);
753 break;
754 case WM_TIMER:
755 GuiConsoleHandleTimer(hWnd);
756 break;
757 case WM_CLOSE:
758 GuiConsoleHandleClose(hWnd);
759 break;
760 case WM_NCDESTROY:
761 GuiConsoleHandleNcDestroy(hWnd);
762 break;
763 case WM_LBUTTONDOWN:
764 GuiConsoleLeftMouseDown(hWnd, lParam);
765 break;
766 case WM_LBUTTONUP:
767 GuiConsoleLeftMouseUp(hWnd, lParam);
768 break;
769 case WM_RBUTTONDOWN:
770 GuiConsoleRightMouseDown(hWnd);
771 break;
772 case WM_MOUSEMOVE:
773 GuiConsoleMouseMove(hWnd, wParam, lParam);
774 break;
775 default:
776 Result = DefWindowProcW(hWnd, msg, wParam, lParam);
777 break;
778 }
779
780 return Result;
781 }
782
783 static LRESULT CALLBACK
784 GuiConsoleNotifyWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
785 {
786 HWND NewWindow;
787 LONG WindowCount;
788 MSG Msg;
789 PCSRSS_CONSOLE Console = (PCSRSS_CONSOLE) lParam;
790
791 switch(msg)
792 {
793 case WM_CREATE:
794 SetWindowLongW(hWnd, GWL_USERDATA, 0);
795 return 0;
796 case PM_CREATE_CONSOLE:
797 NewWindow = CreateWindowW(L"ConsoleWindowClass",
798 Console->Title.Buffer,
799 WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
800 CW_USEDEFAULT,
801 CW_USEDEFAULT,
802 CW_USEDEFAULT,
803 CW_USEDEFAULT,
804 NULL,
805 NULL,
806 (HINSTANCE) GetModuleHandleW(NULL),
807 (PVOID) Console);
808 Console->hWindow = NewWindow;
809 if (NULL != NewWindow)
810 {
811 SetWindowLongW(hWnd, GWL_USERDATA, GetWindowLongW(hWnd, GWL_USERDATA) + 1);
812 ShowWindow(NewWindow, SW_SHOW);
813 }
814 return (LRESULT) NewWindow;
815 case PM_DESTROY_CONSOLE:
816 /* Window creation is done using a PostMessage(), so it's possible that the
817 * window that we want to destroy doesn't exist yet. So first empty the message
818 * queue */
819 while(PeekMessageW(&Msg, NULL, 0, 0, PM_REMOVE))
820 {
821 TranslateMessage(&Msg);
822 DispatchMessageW(&Msg);
823 }
824 DestroyWindow(Console->hWindow);
825 Console->hWindow = NULL;
826 WindowCount = GetWindowLongW(hWnd, GWL_USERDATA);
827 WindowCount--;
828 SetWindowLongW(hWnd, GWL_USERDATA, WindowCount);
829 if (0 == WindowCount)
830 {
831 NotifyWnd = NULL;
832 DestroyWindow(hWnd);
833 PrivateCsrssManualGuiCheck(-1);
834 PostQuitMessage(0);
835 }
836 return 0;
837 default:
838 return DefWindowProcW(hWnd, msg, wParam, lParam);
839 }
840 }
841
842 static DWORD STDCALL
843 GuiConsoleGuiThread(PVOID Data)
844 {
845 MSG msg;
846 PHANDLE GraphicsStartupEvent = (PHANDLE) Data;
847
848 NotifyWnd = CreateWindowW(L"Win32CsrCreateNotify",
849 L"",
850 WS_OVERLAPPEDWINDOW,
851 CW_USEDEFAULT,
852 CW_USEDEFAULT,
853 CW_USEDEFAULT,
854 CW_USEDEFAULT,
855 NULL,
856 NULL,
857 (HINSTANCE) GetModuleHandleW(NULL),
858 NULL);
859 if (NULL == NotifyWnd)
860 {
861 PrivateCsrssManualGuiCheck(-1);
862 SetEvent(*GraphicsStartupEvent);
863 return 1;
864 }
865
866 SetEvent(*GraphicsStartupEvent);
867
868 while(GetMessageW(&msg, NULL, 0, 0))
869 {
870 TranslateMessage(&msg);
871 DispatchMessageW(&msg);
872 }
873
874 return 1;
875 }
876
877 static BOOL FASTCALL
878 GuiInit(VOID)
879 {
880 WNDCLASSEXW wc;
881
882 if (NULL == NotifyWnd)
883 {
884 PrivateCsrssManualGuiCheck(+1);
885 }
886
887 wc.cbSize = sizeof(WNDCLASSEXW);
888 wc.lpszClassName = L"Win32CsrCreateNotify";
889 wc.lpfnWndProc = GuiConsoleNotifyWndProc;
890 wc.style = 0;
891 wc.hInstance = (HINSTANCE) GetModuleHandleW(NULL);
892 wc.hIcon = NULL;
893 wc.hCursor = NULL;
894 wc.hbrBackground = NULL;
895 wc.lpszMenuName = NULL;
896 wc.cbClsExtra = 0;
897 wc.cbWndExtra = 0;
898 wc.hIconSm = NULL;
899 if (RegisterClassExW(&wc) == 0)
900 {
901 DPRINT1("Failed to register notify wndproc\n");
902 return FALSE;
903 }
904
905 wc.cbSize = sizeof(WNDCLASSEXW);
906 wc.lpszClassName = L"ConsoleWindowClass";
907 wc.lpfnWndProc = GuiConsoleWndProc;
908 wc.style = 0;
909 wc.hInstance = (HINSTANCE) GetModuleHandleW(NULL);
910 wc.hIcon = LoadIconW(Win32CsrDllHandle, MAKEINTRESOURCEW(1));
911 wc.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_ARROW));
912 wc.hbrBackground = NULL;
913 wc.lpszMenuName = NULL;
914 wc.cbClsExtra = 0;
915 wc.cbWndExtra = 0;
916 wc.hIconSm = LoadImageW(Win32CsrDllHandle, MAKEINTRESOURCEW(1), IMAGE_ICON,
917 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
918 LR_SHARED);
919 if (RegisterClassExW(&wc) == 0)
920 {
921 DPRINT1("Failed to register console wndproc\n");
922 return FALSE;
923 }
924
925 return TRUE;
926 }
927
928 static VOID STDCALL
929 GuiInitScreenBuffer(PCSRSS_CONSOLE Console, PCSRSS_SCREEN_BUFFER Buffer)
930 {
931 Buffer->DefaultAttrib = 0x0f;
932 }
933
934 STATIC BOOL STDCALL
935 GuiChangeTitle(PCSRSS_CONSOLE Console)
936 {
937 SendMessageW(Console->hWindow, WM_SETTEXT, 0, (LPARAM) Console->Title.Buffer);
938
939 return TRUE;
940 }
941
942 STATIC BOOL STDCALL
943 GuiChangeIcon(PCSRSS_CONSOLE Console)
944 {
945 SendMessageW(Console->hWindow, WM_SETICON, ICON_BIG, (LPARAM)Console->hWindowIcon);
946 SendMessageW(Console->hWindow, WM_SETICON, ICON_SMALL, (LPARAM)Console->hWindowIcon);
947
948 return TRUE;
949 }
950
951 static VOID STDCALL
952 GuiCleanupConsole(PCSRSS_CONSOLE Console)
953 {
954 SendMessageW(NotifyWnd, PM_DESTROY_CONSOLE, 0, (LPARAM) Console);
955 }
956
957 static CSRSS_CONSOLE_VTBL GuiVtbl =
958 {
959 GuiInitScreenBuffer,
960 GuiWriteStream,
961 GuiDrawRegion,
962 GuiSetCursorInfo,
963 GuiSetScreenInfo,
964 GuiChangeTitle,
965 GuiCleanupConsole,
966 GuiChangeIcon
967 };
968
969 NTSTATUS FASTCALL
970 GuiInitConsole(PCSRSS_CONSOLE Console)
971 {
972 HANDLE GraphicsStartupEvent;
973 HANDLE ThreadHandle;
974
975 if (! ConsInitialized)
976 {
977 ConsInitialized = TRUE;
978 if (! GuiInit())
979 {
980 ConsInitialized = FALSE;
981 return STATUS_UNSUCCESSFUL;
982 }
983 }
984
985 Console->Vtbl = &GuiVtbl;
986 Console->Size.X = 80;
987 Console->Size.Y = 25;
988 if (NULL == NotifyWnd)
989 {
990 GraphicsStartupEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
991 if (NULL == GraphicsStartupEvent)
992 {
993 return STATUS_UNSUCCESSFUL;
994 }
995
996 ThreadHandle = CreateThread(NULL,
997 0,
998 GuiConsoleGuiThread,
999 (PVOID) &GraphicsStartupEvent,
1000 0,
1001 NULL);
1002 if (NULL == ThreadHandle)
1003 {
1004 NtClose(GraphicsStartupEvent);
1005 DPRINT1("Win32Csr: Failed to create graphics console thread. Expect problems\n");
1006 return STATUS_UNSUCCESSFUL;
1007 }
1008 SetThreadPriority(ThreadHandle, THREAD_PRIORITY_HIGHEST);
1009 CloseHandle(ThreadHandle);
1010
1011 WaitForSingleObject(GraphicsStartupEvent, INFINITE);
1012 CloseHandle(GraphicsStartupEvent);
1013
1014 if (NULL == NotifyWnd)
1015 {
1016 DPRINT1("Win32Csr: Failed to create notification window.\n");
1017 return STATUS_UNSUCCESSFUL;
1018 }
1019 }
1020
1021 PostMessageW(NotifyWnd, PM_CREATE_CONSOLE, 0, (LPARAM) Console);
1022
1023 return STATUS_SUCCESS;
1024 }
1025
1026 VOID STDCALL
1027 GuiConsoleCopyRegion(HWND hWnd,
1028 RECT *Source,
1029 RECT *Dest)
1030 {
1031 RECT ScrollRect;
1032 PGUI_CONSOLE_DATA GuiData;
1033 PCSRSS_CONSOLE Console;
1034
1035
1036 GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
1037
1038 ScrollRect.left = Dest->left * GuiData->CharWidth;
1039 ScrollRect.right = (Dest->right + 1) * GuiData->CharWidth;
1040 ScrollRect.top = Dest->top * GuiData->CharHeight;
1041 ScrollRect.bottom = (Dest->bottom + 1) * GuiData->CharHeight;
1042 EnterCriticalSection(&GuiData->Lock);
1043 BitBlt(GuiData->MemoryDC, ScrollRect.left, ScrollRect.top,
1044 ScrollRect.right - ScrollRect.left, ScrollRect.bottom - ScrollRect.top,
1045 GuiData->MemoryDC, Source->left * GuiData->CharWidth, Source->top * GuiData->CharHeight, SRCCOPY);
1046
1047 LeaveCriticalSection(&GuiData->Lock);
1048
1049 InvalidateRect(hWnd, &ScrollRect, FALSE);
1050 }
1051
1052 /* EOF */