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