2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis and Cameron Heide
5 * Copyright 1997 Karl Garrison
6 * Copyright 1998 John Richardson
7 * Copyright 1998 Marcus Meissner
11 * - Completely lacks SCREENBUFFER interface.
12 * - No abstraction for something other than xterm.
13 * - Key input translation shouldn't use VkKeyScan and MapVirtualKey, since
14 * they are window (USER) driver dependend.
15 * - Output sometimes is buffered (We switched off buffering by ~ICANON ?)
17 /* Reference applications:
18 * - IDA (interactive disassembler) full version 3.75. Works.
19 * - LYNX/W32. Works mostly, some keys crash it.
28 #include <sys/ioctl.h>
29 #include <sys/types.h>
34 #ifdef HAVE_SYS_ERRNO_H
35 #include <sys/errno.h>
41 #include "wine/winuser16.h"
42 #include "wine/keyboard16.h"
50 #include "debugtools.h"
52 DEFAULT_DEBUG_CHANNEL(console
)
55 /* FIXME: Should be in an internal header file. OK, so which one?
56 Used by CONSOLE_makecomplex. */
57 int wine_openpty(int *master
, int *slave
, char *name
,
58 struct termios
*term
, struct winsize
*winsize
);
60 /****************************************************************************
63 static int CONSOLE_GetPid( HANDLE handle
)
65 struct get_console_info_request
*req
= get_req_buffer();
67 if (server_call( REQ_GET_CONSOLE_INFO
)) return 0;
71 /****************************************************************************
72 * XTERM_string_to_IR [internal]
74 * Transfers a string read from XTERM to INPUT_RECORDs and adds them to the
75 * queue. Does translation of vt100 style function keys and xterm-mouse clicks.
78 CONSOLE_string_to_IR( HANDLE hConsoleInput
,unsigned char *buf
,int len
) {
84 unsigned char inchar
= buf
[j
];
86 if (inchar
!=27) { /* no escape -> 'normal' keyboard event */
87 ir
.EventType
= 1; /* Key_event */
89 ir
.Event
.KeyEvent
.bKeyDown
= 1;
90 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
92 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
94 ir
.Event
.KeyEvent
.dwControlKeyState
|=LEFT_ALT_PRESSED
;
97 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VkKeyScan16(inchar
);
98 if (ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x0100)
99 ir
.Event
.KeyEvent
.dwControlKeyState
|=SHIFT_PRESSED
;
100 if (ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x0200)
101 ir
.Event
.KeyEvent
.dwControlKeyState
|=LEFT_CTRL_PRESSED
;
102 if (ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x0400)
103 ir
.Event
.KeyEvent
.dwControlKeyState
|=LEFT_ALT_PRESSED
;
104 ir
.Event
.KeyEvent
.wVirtualScanCode
= MapVirtualKey16(
105 ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x00ff,
106 0 /* VirtualKeyCodes to ScanCode */
108 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= inchar
;
110 if (inchar
==127) { /* backspace */
111 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= '\b'; /* FIXME: hmm */
112 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0x0e;
113 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VK_BACK
;
115 if ((inchar
=='\n')||(inchar
=='\r')) {
116 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= '\r';
117 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VK_RETURN
;
118 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0x1c;
119 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
122 /* FIXME: find good values for ^X */
123 ir
.Event
.KeyEvent
.wVirtualKeyCode
= 0xdead;
124 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0xbeef;
129 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
130 ir
.Event
.KeyEvent
.bKeyDown
= 0;
131 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
135 if ((j
==len
-1) || (buf
[j
+1]!='[')) {/* add ESCape on its own */
136 ir
.EventType
= 1; /* Key_event */
137 ir
.Event
.KeyEvent
.bKeyDown
= 1;
138 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
140 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VkKeyScan16(27);
141 ir
.Event
.KeyEvent
.wVirtualScanCode
= MapVirtualKey16(
142 ir
.Event
.KeyEvent
.wVirtualKeyCode
,0
144 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
145 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= 27;
146 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
147 ir
.Event
.KeyEvent
.bKeyDown
= 0;
148 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
151 for (k
=j
;k
<len
;k
++) {
152 if (((buf
[k
]>='A') && (buf
[k
]<='Z')) ||
153 ((buf
[k
]>='a') && (buf
[k
]<='z')) ||
159 int subid
,scancode
=0;
161 ir
.EventType
= 1; /* Key_event */
162 ir
.Event
.KeyEvent
.bKeyDown
= 1;
163 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
164 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
166 ir
.Event
.KeyEvent
.wVirtualKeyCode
= 0xad; /* FIXME */
167 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0xad; /* FIXME */
168 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= 0;
172 sscanf(&buf
[j
+2],"%d",&subid
);
174 case 2:/*INS */scancode
= 0xe052;break;
175 case 3:/*DEL */scancode
= 0xe053;break;
176 case 6:/*PGDW*/scancode
= 0xe051;break;
177 case 5:/*PGUP*/scancode
= 0xe049;break;
178 case 11:/*F1 */scancode
= 0x003b;break;
179 case 12:/*F2 */scancode
= 0x003c;break;
180 case 13:/*F3 */scancode
= 0x003d;break;
181 case 14:/*F4 */scancode
= 0x003e;break;
182 case 15:/*F5 */scancode
= 0x003f;break;
183 case 17:/*F6 */scancode
= 0x0040;break;
184 case 18:/*F7 */scancode
= 0x0041;break;
185 case 19:/*F8 */scancode
= 0x0042;break;
186 case 20:/*F9 */scancode
= 0x0043;break;
187 case 21:/*F10 */scancode
= 0x0044;break;
188 case 23:/*F11 */scancode
= 0x00d9;break;
189 case 24:/*F12 */scancode
= 0x00da;break;
190 /* FIXME: Shift-Fx */
192 FIXME("parse ESC[%d~\n",subid
);
196 case 'A': /* Cursor Up */scancode
= 0xe048;break;
197 case 'B': /* Cursor Down */scancode
= 0xe050;break;
198 case 'D': /* Cursor Left */scancode
= 0xe04b;break;
199 case 'C': /* Cursor Right */scancode
= 0xe04d;break;
200 case 'F': /* End */scancode
= 0xe04f;break;
201 case 'H': /* Home */scancode
= 0xe047;break;
203 /* Mouse Button Press (ESCM<button+'!'><x+'!'><y+'!'>) or
204 * Release (ESCM#<x+'!'><y+'!'>
207 ir
.EventType
= MOUSE_EVENT
;
208 ir
.Event
.MouseEvent
.dwMousePosition
.x
= buf
[k
+2]-'!';
209 ir
.Event
.MouseEvent
.dwMousePosition
.y
= buf
[k
+3]-'!';
211 ir
.Event
.MouseEvent
.dwButtonState
= 0;
213 ir
.Event
.MouseEvent
.dwButtonState
= 1<<(buf
[k
+1]-' ');
214 ir
.Event
.MouseEvent
.dwEventFlags
= 0; /* FIXME */
215 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
224 ir
.Event
.KeyEvent
.wVirtualScanCode
= scancode
;
225 ir
.Event
.KeyEvent
.wVirtualKeyCode
= MapVirtualKey16(scancode
,1);
226 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
227 ir
.Event
.KeyEvent
.bKeyDown
= 0;
228 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
236 /****************************************************************************
237 * CONSOLE_get_input (internal)
239 * Reads (nonblocking) as much input events as possible and stores them
240 * in an internal queue.
243 CONSOLE_get_input( HANDLE handle
, BOOL blockwait
)
245 char *buf
= HeapAlloc(GetProcessHeap(),0,1);
252 if (WaitForSingleObject( handle
, 0 )) break;
253 if (!ReadFile( handle
, &inchar
, 1, &res
, NULL
)) break;
254 if (!res
) /* res 0 but readable means EOF? Hmm. */
256 buf
= HeapReAlloc(GetProcessHeap(),0,buf
,len
+1);
259 CONSOLE_string_to_IR(handle
,buf
,len
);
260 HeapFree(GetProcessHeap(),0,buf
);
263 /******************************************************************************
264 * SetConsoleCtrlHandler [KERNEL32.459] Adds function to calling process list
267 * func [I] Address of handler function
268 * add [I] Handler to add or remove
275 * James Sutherland (JamesSutherland@gmx.de)
276 * Added global variables console_ignore_ctrl_c and handlers[]
277 * Does not yet do any error checking, or set LastError if failed.
278 * This doesn't yet matter, since these handlers are not yet called...!
280 static unsigned int console_ignore_ctrl_c
= 0;
281 static HANDLER_ROUTINE
*handlers
[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
282 BOOL WINAPI
SetConsoleCtrlHandler( HANDLER_ROUTINE
*func
, BOOL add
)
284 unsigned int alloc_loop
= sizeof(handlers
)/sizeof(HANDLER_ROUTINE
*);
285 unsigned int done
= 0;
286 FIXME("(%p,%i) - no error checking or testing yet\n", func
, add
);
289 console_ignore_ctrl_c
= add
;
295 if (!handlers
[alloc_loop
] && !done
)
297 handlers
[alloc_loop
] = func
;
301 FIXME("Out of space on CtrlHandler table\n");
307 if (handlers
[alloc_loop
] == func
&& !done
)
309 handlers
[alloc_loop
] = 0;
313 WARN("Attempt to remove non-installed CtrlHandler %p\n",
321 /******************************************************************************
322 * GenerateConsoleCtrlEvent [KERNEL32.275] Simulate a CTRL-C or CTRL-BREAK
325 * dwCtrlEvent [I] Type of event
326 * dwProcessGroupID [I] Process group ID to send event to
329 * Doesn't yet work...!
333 * Failure: False (and *should* [but doesn't] set LastError)
335 BOOL WINAPI
GenerateConsoleCtrlEvent( DWORD dwCtrlEvent
,
336 DWORD dwProcessGroupID
)
338 if (dwCtrlEvent
!= CTRL_C_EVENT
&& dwCtrlEvent
!= CTRL_BREAK_EVENT
)
340 ERR("invalid event %d for PGID %ld\n",
341 (unsigned short)dwCtrlEvent
, dwProcessGroupID
);
344 if (dwProcessGroupID
== GetCurrentProcessId() )
346 FIXME("Attempt to send event %d to self - stub\n",
347 (unsigned short)dwCtrlEvent
);
350 FIXME("event %d to external PGID %ld - not implemented yet\n",
351 (unsigned short)dwCtrlEvent
, dwProcessGroupID
);
356 /******************************************************************************
357 * CreateConsoleScreenBuffer [KERNEL32.151] Creates a console screen buffer
360 * dwDesiredAccess [I] Access flag
361 * dwShareMode [I] Buffer share mode
362 * sa [I] Security attributes
363 * dwFlags [I] Type of buffer to create
364 * lpScreenBufferData [I] Reserved
367 * Should call SetLastError
370 * Success: Handle to new console screen buffer
371 * Failure: INVALID_HANDLE_VALUE
373 HANDLE WINAPI
CreateConsoleScreenBuffer( DWORD dwDesiredAccess
,
374 DWORD dwShareMode
, LPSECURITY_ATTRIBUTES sa
,
375 DWORD dwFlags
, LPVOID lpScreenBufferData
)
377 FIXME("(%ld,%ld,%p,%ld,%p): stub\n",dwDesiredAccess
,
378 dwShareMode
, sa
, dwFlags
, lpScreenBufferData
);
379 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
380 return INVALID_HANDLE_VALUE
;
384 /***********************************************************************
385 * GetConsoleScreenBufferInfo (KERNEL32.190)
387 BOOL WINAPI
GetConsoleScreenBufferInfo( HANDLE hConsoleOutput
,
388 LPCONSOLE_SCREEN_BUFFER_INFO csbi
)
392 csbi
->dwCursorPosition
.x
= 0;
393 csbi
->dwCursorPosition
.y
= 0;
394 csbi
->wAttributes
= 0;
395 csbi
->srWindow
.Left
= 0;
396 csbi
->srWindow
.Right
= 79;
397 csbi
->srWindow
.Top
= 0;
398 csbi
->srWindow
.Bottom
= 23;
399 csbi
->dwMaximumWindowSize
.x
= 80;
400 csbi
->dwMaximumWindowSize
.y
= 24;
405 /******************************************************************************
406 * SetConsoleActiveScreenBuffer [KERNEL32.623] Sets buffer to current console
412 BOOL WINAPI
SetConsoleActiveScreenBuffer(
413 HANDLE hConsoleOutput
) /* [in] Handle to console screen buffer */
415 FIXME("(%x): stub\n", hConsoleOutput
);
420 /***********************************************************************
421 * GetLargestConsoleWindowSize (KERNEL32.226)
423 DWORD WINAPI
GetLargestConsoleWindowSize( HANDLE hConsoleOutput
)
425 return (DWORD
)MAKELONG(80,24);
428 /***********************************************************************
429 * FreeConsole (KERNEL32.267)
431 BOOL WINAPI
FreeConsole(VOID
)
433 return !server_call( REQ_FREE_CONSOLE
);
437 /*************************************************************************
440 * Open a handle to the current process console.
442 HANDLE
CONSOLE_OpenHandle( BOOL output
, DWORD access
, LPSECURITY_ATTRIBUTES sa
)
445 struct open_console_request
*req
= get_req_buffer();
447 req
->output
= output
;
448 req
->access
= access
;
449 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
451 if (!server_call( REQ_OPEN_CONSOLE
)) ret
= req
->handle
;
456 /*************************************************************************
457 * CONSOLE_make_complex [internal]
459 * Turns a CONSOLE kernel object into a complex one.
460 * (switches from output/input using the terminal where WINE was started to
463 * This makes simple commandline tools pipeable, while complex commandline
464 * tools work without getting messed up by debugoutput.
466 * All other functions should work indedependend from this call.
468 * To test for complex console: pid == 0 -> simple, otherwise complex.
470 static BOOL
CONSOLE_make_complex(HANDLE handle
)
472 struct set_console_fd_request
*req
= get_req_buffer();
476 int i
,xpid
,master
,slave
,pty_handle
;
478 if (CONSOLE_GetPid( handle
)) return TRUE
; /* already complex */
480 MESSAGE("Console: Making console complex (creating an xterm)...\n");
482 if (tcgetattr(0, &term
) < 0) {
483 /* ignore failure, or we can't run from a script */
485 term
.c_lflag
= ~(ECHO
|ICANON
);
487 if (wine_openpty(&master
, &slave
, NULL
, &term
, NULL
) < 0)
490 if ((xpid
=fork()) == 0) {
491 tcsetattr(slave
, TCSADRAIN
, &term
);
493 sprintf(buf
, "-Sxx%d", master
);
494 /* "-fn vga" for VGA font. Harmless if vga is not present:
495 * xterm: unable to open font "vga", trying "fixed"....
497 execlp("xterm", "xterm", buf
, "-fn","vga",NULL
);
498 ERR("error creating AllocConsole xterm\n");
501 pty_handle
= FILE_DupUnixHandle( slave
, GENERIC_READ
| GENERIC_WRITE
);
504 if (pty_handle
== -1) return FALSE
;
506 /* most xterms like to print their window ID when used with -S;
507 * read it and continue before the user has a chance...
509 for (i
= 0; i
< 10000; i
++)
511 BOOL ok
= ReadFile( pty_handle
, &c
, 1, NULL
, NULL
);
512 if (!ok
&& !c
) usleep(100); /* wait for xterm to be created */
513 else if (c
== '\n') break;
517 ERR("can't read xterm WID\n");
518 CloseHandle( pty_handle
);
521 req
->handle
= handle
;
522 req
->file_handle
= pty_handle
;
524 server_call( REQ_SET_CONSOLE_FD
);
525 CloseHandle( pty_handle
);
527 /* enable mouseclicks */
528 strcpy( buf
, "\033[?1001s\033[?1000h" );
529 WriteFile(handle
,buf
,strlen(buf
),NULL
,NULL
);
531 strcpy( buf
, "\033]2;" );
532 if (GetConsoleTitleA( buf
+ 4, sizeof(buf
) - 5 ))
535 WriteFile(handle
,buf
,strlen(buf
),NULL
,NULL
);
542 /***********************************************************************
543 * AllocConsole (KERNEL32.103)
545 * creates an xterm with a pty to our program
547 BOOL WINAPI
AllocConsole(VOID
)
549 struct alloc_console_request
*req
= get_req_buffer();
551 int handle_in
, handle_out
;
554 req
->access
= GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
;
555 req
->inherit
= FALSE
;
556 if (server_call( REQ_ALLOC_CONSOLE
)) return FALSE
;
557 handle_in
= req
->handle_in
;
558 handle_out
= req
->handle_out
;
560 if (!DuplicateHandle( GetCurrentProcess(), req
->handle_out
, GetCurrentProcess(), &hStderr
,
561 0, TRUE
, DUPLICATE_SAME_ACCESS
))
563 CloseHandle( handle_in
);
564 CloseHandle( handle_out
);
569 /* NT resets the STD_*_HANDLEs on console alloc */
570 SetStdHandle( STD_INPUT_HANDLE
, handle_in
);
571 SetStdHandle( STD_OUTPUT_HANDLE
, handle_out
);
572 SetStdHandle( STD_ERROR_HANDLE
, hStderr
);
574 SetLastError(ERROR_SUCCESS
);
575 SetConsoleTitleA("Wine Console");
580 /******************************************************************************
581 * GetConsoleCP [KERNEL32.295] Returns the OEM code page for the console
586 UINT WINAPI
GetConsoleCP(VOID
)
592 /***********************************************************************
593 * GetConsoleOutputCP (KERNEL32.189)
595 UINT WINAPI
GetConsoleOutputCP(VOID
)
597 return GetConsoleCP();
600 /***********************************************************************
601 * GetConsoleMode (KERNEL32.188)
603 BOOL WINAPI
GetConsoleMode(HANDLE hcon
,LPDWORD mode
)
606 struct get_console_mode_request
*req
= get_req_buffer();
608 if (!server_call( REQ_GET_CONSOLE_MODE
))
610 if (mode
) *mode
= req
->mode
;
617 /******************************************************************************
618 * SetConsoleMode [KERNEL32.628] Sets input mode of console's input buffer
621 * hcon [I] Handle to console input or screen buffer
622 * mode [I] Input or output mode to set
628 BOOL WINAPI
SetConsoleMode( HANDLE hcon
, DWORD mode
)
630 struct set_console_mode_request
*req
= get_req_buffer();
633 return !server_call( REQ_SET_CONSOLE_MODE
);
637 /***********************************************************************
638 * GetConsoleTitleA (KERNEL32.191)
640 DWORD WINAPI
GetConsoleTitleA(LPSTR title
,DWORD size
)
642 struct get_console_info_request
*req
= get_req_buffer();
646 if ((hcon
= CreateFileA( "CONOUT$", GENERIC_READ
, 0, NULL
,
647 OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
650 if (!server_call( REQ_GET_CONSOLE_INFO
))
652 lstrcpynA( title
, req
->title
, size
);
653 ret
= strlen(req
->title
);
660 /******************************************************************************
661 * GetConsoleTitle32W [KERNEL32.192] Retrieves title string for console
664 * title [O] Address of buffer for title
665 * size [I] Size of buffer
668 * Success: Length of string copied
671 DWORD WINAPI
GetConsoleTitleW( LPWSTR title
, DWORD size
)
676 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, size
))) return 0;
677 ret
= GetConsoleTitleA( tmp
, size
);
678 lstrcpyAtoW( title
, tmp
);
679 HeapFree( GetProcessHeap(), 0, tmp
);
684 /***********************************************************************
685 * WriteConsoleA (KERNEL32.729)
687 BOOL WINAPI
WriteConsoleA( HANDLE hConsoleOutput
,
689 DWORD nNumberOfCharsToWrite
,
690 LPDWORD lpNumberOfCharsWritten
,
693 /* FIXME: should I check if this is a console handle? */
694 return WriteFile(hConsoleOutput
, lpBuffer
, nNumberOfCharsToWrite
,
695 lpNumberOfCharsWritten
, NULL
);
700 if (bufused==curbufsize-1) \
701 buffer = HeapReAlloc(GetProcessHeap(),0,buffer,(curbufsize+=100));\
703 #define SADD(s) { char *x=s;while (*x) {CADD(*x);x++;}}
705 /***********************************************************************
706 * WriteConsoleOutputA (KERNEL32.732)
708 BOOL WINAPI
WriteConsoleOutputA( HANDLE hConsoleOutput
,
709 LPCHAR_INFO lpBuffer
,
712 LPSMALL_RECT lpWriteRegion
)
714 int i
,j
,off
=0,lastattr
=-1;
716 char sbuf
[20],*buffer
=NULL
;
717 int bufused
=0,curbufsize
= 100;
719 CONSOLE_SCREEN_BUFFER_INFO csbi
;
720 const int colormap
[8] = {
724 CONSOLE_make_complex(hConsoleOutput
);
725 buffer
= HeapAlloc(GetProcessHeap(),0,curbufsize
);
726 offbase
= (dwBufferCoord
.y
- 1) * dwBufferSize
.x
+
727 (dwBufferCoord
.x
- lpWriteRegion
->Left
);
729 TRACE("orig rect top = %d, bottom=%d, left=%d, right=%d\n",
731 lpWriteRegion
->Bottom
,
736 GetConsoleScreenBufferInfo(hConsoleOutput
, &csbi
);
738 /* Step 1. Make (Bottom,Right) offset of intersection with
740 lpWriteRegion
->Bottom
= min(lpWriteRegion
->Bottom
, csbi
.dwSize
.y
-1) -
742 lpWriteRegion
->Right
= min(lpWriteRegion
->Right
, csbi
.dwSize
.x
-1) -
745 /* Step 2. If either offset is negative, then no action
746 should be performed. (Implies that requested rectangle is
747 outside the current screen buffer rectangle.) */
748 if ((lpWriteRegion
->Bottom
< 0) ||
749 (lpWriteRegion
->Right
< 0)) {
750 /* readjust (Bottom Right) for rectangle */
751 lpWriteRegion
->Bottom
+= lpWriteRegion
->Top
;
752 lpWriteRegion
->Right
+= lpWriteRegion
->Left
;
754 TRACE("invisible rect top = %d, bottom=%d, left=%d, right=%d\n",
756 lpWriteRegion
->Bottom
,
761 HeapFree(GetProcessHeap(),0,buffer
);
765 /* Step 3. Intersect with source rectangle */
766 lpWriteRegion
->Bottom
= lpWriteRegion
->Top
- dwBufferCoord
.y
+
767 min(lpWriteRegion
->Bottom
+ dwBufferCoord
.y
, dwBufferSize
.y
-1);
768 lpWriteRegion
->Right
= lpWriteRegion
->Left
- dwBufferCoord
.x
+
769 min(lpWriteRegion
->Right
+ dwBufferCoord
.x
, dwBufferSize
.x
-1);
771 TRACE("clipped rect top = %d, bottom=%d, left=%d,right=%d\n",
773 lpWriteRegion
->Bottom
,
778 /* Validate above computations made sense, if not then issue
779 error and fudge to single character rectangle */
780 if ((lpWriteRegion
->Bottom
< lpWriteRegion
->Top
) ||
781 (lpWriteRegion
->Right
< lpWriteRegion
->Left
)) {
782 ERR("Invalid clipped rectangle top = %d, bottom=%d, left=%d,right=%d\n",
784 lpWriteRegion
->Bottom
,
788 lpWriteRegion
->Bottom
= lpWriteRegion
->Top
;
789 lpWriteRegion
->Right
= lpWriteRegion
->Left
;
792 /* Now do the real processing and move the characters */
793 for (i
=lpWriteRegion
->Top
;i
<=lpWriteRegion
->Bottom
;i
++) {
794 offbase
+= dwBufferSize
.x
;
795 sprintf(sbuf
,"%c[%d;%dH",27,i
+1,lpWriteRegion
->Left
+1);
797 for (j
=lpWriteRegion
->Left
;j
<=lpWriteRegion
->Right
;j
++) {
799 if (lastattr
!=lpBuffer
[off
].Attributes
) {
800 lastattr
= lpBuffer
[off
].Attributes
;
801 sprintf(sbuf
,"%c[0;%s3%d;4%dm",
803 (lastattr
& FOREGROUND_INTENSITY
)?"1;":"",
804 colormap
[lastattr
&7],
805 colormap
[(lastattr
&0x70)>>4]
807 /* FIXME: BACKGROUND_INTENSITY */
810 CADD(lpBuffer
[off
].Char
.AsciiChar
);
813 sprintf(sbuf
,"%c[0m",27);SADD(sbuf
);
814 WriteFile(hConsoleOutput
,buffer
,bufused
,&res
,NULL
);
815 HeapFree(GetProcessHeap(),0,buffer
);
819 /***********************************************************************
820 * WriteConsoleW (KERNEL32.577)
822 BOOL WINAPI
WriteConsoleW( HANDLE hConsoleOutput
,
824 DWORD nNumberOfCharsToWrite
,
825 LPDWORD lpNumberOfCharsWritten
,
829 LPSTR xstring
=HeapAlloc( GetProcessHeap(), 0, nNumberOfCharsToWrite
);
831 lstrcpynWtoA( xstring
, lpBuffer
,nNumberOfCharsToWrite
);
833 /* FIXME: should I check if this is a console handle? */
834 ret
= WriteFile(hConsoleOutput
, xstring
, nNumberOfCharsToWrite
,
835 lpNumberOfCharsWritten
, NULL
);
836 HeapFree( GetProcessHeap(), 0, xstring
);
841 /***********************************************************************
842 * ReadConsoleA (KERNEL32.419)
844 BOOL WINAPI
ReadConsoleA( HANDLE hConsoleInput
,
846 DWORD nNumberOfCharsToRead
,
847 LPDWORD lpNumberOfCharsRead
,
851 LPSTR xbuf
= (LPSTR
)lpBuffer
;
854 TRACE("(%d,%p,%ld,%p,%p)\n",
855 hConsoleInput
,lpBuffer
,nNumberOfCharsToRead
,
856 lpNumberOfCharsRead
,lpReserved
859 CONSOLE_get_input(hConsoleInput
,FALSE
);
861 /* FIXME: should we read at least 1 char? The SDK does not say */
862 while (charsread
<nNumberOfCharsToRead
)
864 struct read_console_input_request
*req
= get_req_buffer();
865 req
->handle
= hConsoleInput
;
868 if (server_call( REQ_READ_CONSOLE_INPUT
)) return FALSE
;
869 if (!req
->read
) break;
870 ir
= (LPINPUT_RECORD
)(req
+1);
871 if (!ir
->Event
.KeyEvent
.bKeyDown
)
873 if (ir
->EventType
!= KEY_EVENT
)
875 *xbuf
++ = ir
->Event
.KeyEvent
.uChar
.AsciiChar
;
878 if (lpNumberOfCharsRead
)
879 *lpNumberOfCharsRead
= charsread
;
883 /***********************************************************************
884 * ReadConsoleW (KERNEL32.427)
886 BOOL WINAPI
ReadConsoleW( HANDLE hConsoleInput
,
888 DWORD nNumberOfCharsToRead
,
889 LPDWORD lpNumberOfCharsRead
,
893 LPSTR buf
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead
);
898 nNumberOfCharsToRead
,
903 lstrcpynAtoW(lpBuffer
,buf
,nNumberOfCharsToRead
);
904 HeapFree( GetProcessHeap(), 0, buf
);
909 /******************************************************************************
910 * ReadConsoleInput32A [KERNEL32.569] Reads data from a console
913 * hConsoleInput [I] Handle to console input buffer
914 * lpBuffer [O] Address of buffer for read data
915 * nLength [I] Number of records to read
916 * lpNumberOfEventsRead [O] Address of number of records read
922 BOOL WINAPI
ReadConsoleInputA(HANDLE hConsoleInput
, LPINPUT_RECORD lpBuffer
,
923 DWORD nLength
, LPDWORD lpNumberOfEventsRead
)
925 struct read_console_input_request
*req
= get_req_buffer();
927 /* loop until we get at least one event */
930 req
->handle
= hConsoleInput
;
931 req
->count
= nLength
;
933 if (server_call( REQ_READ_CONSOLE_INPUT
)) return FALSE
;
936 memcpy( lpBuffer
, req
+ 1, req
->read
* sizeof(*lpBuffer
) );
937 if (lpNumberOfEventsRead
) *lpNumberOfEventsRead
= req
->read
;
940 CONSOLE_get_input(hConsoleInput
,TRUE
);
941 /*WaitForSingleObject( hConsoleInput, INFINITE32 );*/
946 /***********************************************************************
947 * ReadConsoleInput32W (KERNEL32.570)
949 BOOL WINAPI
ReadConsoleInputW( HANDLE handle
, LPINPUT_RECORD buffer
,
950 DWORD count
, LPDWORD read
)
952 /* FIXME: Fix this if we get UNICODE input. */
953 return ReadConsoleInputA( handle
, buffer
, count
, read
);
957 /***********************************************************************
958 * FlushConsoleInputBuffer (KERNEL32.132)
960 BOOL WINAPI
FlushConsoleInputBuffer( HANDLE handle
)
962 struct read_console_input_request
*req
= get_req_buffer();
963 req
->handle
= handle
;
964 req
->count
= -1; /* get all records */
966 return !server_call( REQ_READ_CONSOLE_INPUT
);
970 /***********************************************************************
971 * PeekConsoleInputA (KERNEL32.550)
973 * Gets 'count' first events (or less) from input queue.
975 * Does not need a complex console.
977 BOOL WINAPI
PeekConsoleInputA( HANDLE handle
, LPINPUT_RECORD buffer
,
978 DWORD count
, LPDWORD read
)
980 struct read_console_input_request
*req
= get_req_buffer();
982 CONSOLE_get_input(handle
,FALSE
);
984 req
->handle
= handle
;
987 if (server_call( REQ_READ_CONSOLE_INPUT
)) return FALSE
;
988 if (req
->read
) memcpy( buffer
, req
+ 1, req
->read
* sizeof(*buffer
) );
989 if (read
) *read
= req
->read
;
994 /***********************************************************************
995 * PeekConsoleInputW (KERNEL32.551)
997 BOOL WINAPI
PeekConsoleInputW(HANDLE hConsoleInput
,
998 LPINPUT_RECORD pirBuffer
,
1002 /* FIXME: Hmm. Fix this if we get UNICODE input. */
1003 return PeekConsoleInputA(hConsoleInput
,pirBuffer
,cInRecords
,lpcRead
);
1007 /******************************************************************************
1008 * WriteConsoleInput32A [KERNEL32.730] Write data to a console input buffer
1011 BOOL WINAPI
WriteConsoleInputA( HANDLE handle
, INPUT_RECORD
*buffer
,
1012 DWORD count
, LPDWORD written
)
1014 struct write_console_input_request
*req
= get_req_buffer();
1015 const DWORD max
= server_remaining( req
+ 1 ) / sizeof(INPUT_RECORD
);
1017 if (written
) *written
= 0;
1020 DWORD len
= count
< max
? count
: max
;
1022 req
->handle
= handle
;
1023 memcpy( req
+ 1, buffer
, len
* sizeof(*buffer
) );
1024 if (server_call( REQ_WRITE_CONSOLE_INPUT
)) return FALSE
;
1025 if (written
) *written
+= req
->written
;
1033 /***********************************************************************
1034 * SetConsoleTitle32A (KERNEL32.476)
1036 * Sets the console title.
1038 * We do not necessarily need to create a complex console for that,
1039 * but should remember the title and set it on creation of the latter.
1040 * (not fixed at this time).
1042 BOOL WINAPI
SetConsoleTitleA(LPCSTR title
)
1044 struct set_console_info_request
*req
= get_req_buffer();
1048 if ((hcon
= CreateFileA( "CONOUT$", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1049 OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
1052 req
->mask
= SET_CONSOLE_INFO_TITLE
;
1053 lstrcpynA( req
->title
, title
, server_remaining(req
->title
) );
1054 if (server_call( REQ_SET_CONSOLE_INFO
)) goto error
;
1055 if (CONSOLE_GetPid( hcon
))
1057 /* only set title for complex console (own xterm) */
1058 WriteFile( hcon
, "\033]2;", 4, &written
, NULL
);
1059 WriteFile( hcon
, title
, strlen(title
), &written
, NULL
);
1060 WriteFile( hcon
, "\a", 1, &written
, NULL
);
1062 CloseHandle( hcon
);
1065 CloseHandle( hcon
);
1070 /******************************************************************************
1071 * SetConsoleTitle32W [KERNEL32.477] Sets title bar string for console
1074 * title [I] Address of new title
1077 * This should not be calling the A version
1083 BOOL WINAPI
SetConsoleTitleW( LPCWSTR title
)
1087 LPSTR titleA
= HEAP_strdupWtoA( GetProcessHeap(), 0, title
);
1088 ret
= SetConsoleTitleA(titleA
);
1089 HeapFree( GetProcessHeap(), 0, titleA
);
1093 /******************************************************************************
1094 * SetConsoleCursorPosition [KERNEL32.627]
1095 * Sets the cursor position in console
1098 * hConsoleOutput [I] Handle of console screen buffer
1099 * dwCursorPosition [I] New cursor position coordinates
1103 BOOL WINAPI
SetConsoleCursorPosition( HANDLE hcon
, COORD pos
)
1108 /* make console complex only if we change lines, not just in the line */
1110 CONSOLE_make_complex(hcon
);
1112 TRACE("%d (%dx%d)\n", hcon
, pos
.x
, pos
.y
);
1113 /* x are columns, y rows */
1115 /* full screen cursor absolute positioning */
1116 sprintf(xbuf
,"%c[%d;%dH", 0x1B, pos
.y
+1, pos
.x
+1);
1118 /* relative cursor positioning in line (\r to go to 0) */
1119 sprintf(xbuf
,"\r%c[%dC", 0x1B, pos
.x
);
1120 /* FIXME: store internal if we start using own console buffers */
1121 WriteFile(hcon
,xbuf
,strlen(xbuf
),&xlen
,NULL
);
1125 /***********************************************************************
1126 * GetNumberOfConsoleInputEvents (KERNEL32.246)
1128 BOOL WINAPI
GetNumberOfConsoleInputEvents(HANDLE hcon
,LPDWORD nrofevents
)
1130 struct read_console_input_request
*req
= get_req_buffer();
1132 CONSOLE_get_input(hcon
,FALSE
);
1137 if (server_call( REQ_READ_CONSOLE_INPUT
)) return FALSE
;
1138 if (nrofevents
) *nrofevents
= req
->read
;
1142 /***********************************************************************
1143 * GetNumberOfConsoleMouseButtons (KERNEL32.358)
1145 BOOL WINAPI
GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons
)
1147 FIXME("(%p): stub\n", nrofbuttons
);
1152 /******************************************************************************
1153 * GetConsoleCursorInfo32 [KERNEL32.296] Gets size and visibility of console
1156 * hcon [I] Handle to console screen buffer
1157 * cinfo [O] Address of cursor information
1163 BOOL WINAPI
GetConsoleCursorInfo( HANDLE hcon
, LPCONSOLE_CURSOR_INFO cinfo
)
1165 struct get_console_info_request
*req
= get_req_buffer();
1167 if (server_call( REQ_GET_CONSOLE_INFO
)) return FALSE
;
1170 cinfo
->dwSize
= req
->cursor_size
;
1171 cinfo
->bVisible
= req
->cursor_visible
;
1177 /******************************************************************************
1178 * SetConsoleCursorInfo32 [KERNEL32.626] Sets size and visibility of cursor
1184 BOOL WINAPI
SetConsoleCursorInfo(
1185 HANDLE hcon
, /* [in] Handle to console screen buffer */
1186 LPCONSOLE_CURSOR_INFO cinfo
) /* [in] Address of cursor information */
1188 struct set_console_info_request
*req
= get_req_buffer();
1192 CONSOLE_make_complex(hcon
);
1193 sprintf(buf
,"\033[?25%c",cinfo
->bVisible
?'h':'l');
1194 WriteFile(hcon
,buf
,strlen(buf
),&xlen
,NULL
);
1197 req
->cursor_size
= cinfo
->dwSize
;
1198 req
->cursor_visible
= cinfo
->bVisible
;
1199 req
->mask
= SET_CONSOLE_INFO_CURSOR
;
1200 return !server_call( REQ_SET_CONSOLE_INFO
);
1204 /******************************************************************************
1205 * SetConsoleWindowInfo [KERNEL32.634] Sets size and position of console
1211 BOOL WINAPI
SetConsoleWindowInfo(
1212 HANDLE hcon
, /* [in] Handle to console screen buffer */
1213 BOOL bAbsolute
, /* [in] Coordinate type flag */
1214 LPSMALL_RECT window
) /* [in] Address of new window rectangle */
1216 FIXME("(%x,%d,%p): stub\n", hcon
, bAbsolute
, window
);
1221 /******************************************************************************
1222 * SetConsoleTextAttribute32 [KERNEL32.631] Sets colors for text
1224 * Sets the foreground and background color attributes of characters
1225 * written to the screen buffer.
1231 BOOL WINAPI
SetConsoleTextAttribute(HANDLE hConsoleOutput
,WORD wAttr
)
1233 const int colormap
[8] = {
1240 TRACE("(%d,%d)\n",hConsoleOutput
,wAttr
);
1241 sprintf(buffer
,"%c[0;%s3%d;4%dm",
1243 (wAttr
& FOREGROUND_INTENSITY
)?"1;":"",
1245 colormap
[(wAttr
&0x70)>>4]
1247 WriteFile(hConsoleOutput
,buffer
,strlen(buffer
),&xlen
,NULL
);
1252 /******************************************************************************
1253 * SetConsoleScreenBufferSize [KERNEL32.630] Changes size of console
1256 * hConsoleOutput [I] Handle to console screen buffer
1257 * dwSize [I] New size in character rows and cols
1263 BOOL WINAPI
SetConsoleScreenBufferSize( HANDLE hConsoleOutput
,
1266 FIXME("(%d,%dx%d): stub\n",hConsoleOutput
,dwSize
.x
,dwSize
.y
);
1271 /******************************************************************************
1272 * FillConsoleOutputCharacterA [KERNEL32.242]
1275 * hConsoleOutput [I] Handle to screen buffer
1276 * cCharacter [I] Character to write
1277 * nLength [I] Number of cells to write to
1278 * dwCoord [I] Coords of first cell
1279 * lpNumCharsWritten [O] Pointer to number of cells written
1285 BOOL WINAPI
FillConsoleOutputCharacterA(
1286 HANDLE hConsoleOutput
,
1290 LPDWORD lpNumCharsWritten
)
1295 SetConsoleCursorPosition(hConsoleOutput
,dwCoord
);
1296 for(count
=0;count
<nLength
;count
++)
1297 WriteFile(hConsoleOutput
,&cCharacter
,1,&xlen
,NULL
);
1298 *lpNumCharsWritten
= nLength
;
1303 /******************************************************************************
1304 * FillConsoleOutputCharacterW [KERNEL32.243] Writes characters to console
1307 * hConsoleOutput [I] Handle to screen buffer
1308 * cCharacter [I] Character to write
1309 * nLength [I] Number of cells to write to
1310 * dwCoord [I] Coords of first cell
1311 * lpNumCharsWritten [O] Pointer to number of cells written
1317 BOOL WINAPI
FillConsoleOutputCharacterW(HANDLE hConsoleOutput
,
1321 LPDWORD lpNumCharsWritten
)
1326 SetConsoleCursorPosition(hConsoleOutput
,dwCoord
);
1327 /* FIXME: not quite correct ... but the lower part of UNICODE char comes
1330 for(count
=0;count
<nLength
;count
++)
1331 WriteFile(hConsoleOutput
,&cCharacter
,1,&xlen
,NULL
);
1332 *lpNumCharsWritten
= nLength
;
1337 /******************************************************************************
1338 * FillConsoleOutputAttribute [KERNEL32.241] Sets attributes for console
1341 * hConsoleOutput [I] Handle to screen buffer
1342 * wAttribute [I] Color attribute to write
1343 * nLength [I] Number of cells to write to
1344 * dwCoord [I] Coords of first cell
1345 * lpNumAttrsWritten [O] Pointer to number of cells written
1351 BOOL WINAPI
FillConsoleOutputAttribute( HANDLE hConsoleOutput
,
1352 WORD wAttribute
, DWORD nLength
, COORD dwCoord
,
1353 LPDWORD lpNumAttrsWritten
)
1355 FIXME("(%d,%d,%ld,%dx%d,%p): stub\n", hConsoleOutput
,
1356 wAttribute
,nLength
,dwCoord
.x
,dwCoord
.y
,lpNumAttrsWritten
);
1357 *lpNumAttrsWritten
= nLength
;
1361 /******************************************************************************
1362 * ReadConsoleOutputCharacter32A [KERNEL32.573]
1367 BOOL WINAPI
ReadConsoleOutputCharacterA(HANDLE hConsoleOutput
,
1368 LPSTR lpstr
, DWORD dword
, COORD coord
, LPDWORD lpdword
)
1370 FIXME("(%d,%p,%ld,%dx%d,%p): stub\n", hConsoleOutput
,lpstr
,
1371 dword
,coord
.x
,coord
.y
,lpdword
);
1372 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1377 /******************************************************************************
1378 * ScrollConsoleScreenBuffer [KERNEL32.612]
1383 BOOL WINAPI
ScrollConsoleScreenBuffer( HANDLE hConsoleOutput
,
1384 LPSMALL_RECT lpScrollRect
, LPSMALL_RECT lpClipRect
,
1385 COORD dwDestOrigin
, LPCHAR_INFO lpFill
)
1387 FIXME("(%d,%p,%p,%dx%d,%p): stub\n", hConsoleOutput
,lpScrollRect
,
1388 lpClipRect
,dwDestOrigin
.x
,dwDestOrigin
.y
,lpFill
);
1389 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);