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.
26 #include <sys/ioctl.h>
27 #include <sys/types.h>
32 #include <sys/errno.h>
37 #include "wine/winuser16.h"
38 #include "wine/keyboard16.h"
47 #include "debugtools.h"
49 DEFAULT_DEBUG_CHANNEL(console
)
52 /* FIXME: Should be in an internal header file. OK, so which one?
53 Used by CONSOLE_makecomplex. */
54 FILE *wine_openpty(int *master
, int *slave
, char *name
,
55 struct termios
*term
, struct winsize
*winsize
);
57 /****************************************************************************
60 static int CONSOLE_GetPid( HANDLE handle
)
62 struct get_console_info_request
*req
= get_req_buffer();
64 if (server_call( REQ_GET_CONSOLE_INFO
)) return 0;
68 /****************************************************************************
69 * XTERM_string_to_IR [internal]
71 * Transfers a string read from XTERM to INPUT_RECORDs and adds them to the
72 * queue. Does translation of vt100 style function keys and xterm-mouse clicks.
75 CONSOLE_string_to_IR( HANDLE hConsoleInput
,unsigned char *buf
,int len
) {
81 unsigned char inchar
= buf
[j
];
83 if (inchar
!=27) { /* no escape -> 'normal' keyboard event */
84 ir
.EventType
= 1; /* Key_event */
86 ir
.Event
.KeyEvent
.bKeyDown
= 1;
87 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
89 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
91 ir
.Event
.KeyEvent
.dwControlKeyState
|=LEFT_ALT_PRESSED
;
94 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VkKeyScan16(inchar
);
95 if (ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x0100)
96 ir
.Event
.KeyEvent
.dwControlKeyState
|=SHIFT_PRESSED
;
97 if (ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x0200)
98 ir
.Event
.KeyEvent
.dwControlKeyState
|=LEFT_CTRL_PRESSED
;
99 if (ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x0400)
100 ir
.Event
.KeyEvent
.dwControlKeyState
|=LEFT_ALT_PRESSED
;
101 ir
.Event
.KeyEvent
.wVirtualScanCode
= MapVirtualKey16(
102 ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x00ff,
103 0 /* VirtualKeyCodes to ScanCode */
105 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= inchar
;
107 if (inchar
==127) { /* backspace */
108 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= '\b'; /* FIXME: hmm */
109 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0x0e;
110 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VK_BACK
;
112 if ((inchar
=='\n')||(inchar
=='\r')) {
113 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= '\r';
114 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VK_RETURN
;
115 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0x1c;
116 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
119 /* FIXME: find good values for ^X */
120 ir
.Event
.KeyEvent
.wVirtualKeyCode
= 0xdead;
121 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0xbeef;
126 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
127 ir
.Event
.KeyEvent
.bKeyDown
= 0;
128 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
132 if ((j
==len
-1) || (buf
[j
+1]!='[')) {/* add ESCape on its own */
133 ir
.EventType
= 1; /* Key_event */
134 ir
.Event
.KeyEvent
.bKeyDown
= 1;
135 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
137 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VkKeyScan16(27);
138 ir
.Event
.KeyEvent
.wVirtualScanCode
= MapVirtualKey16(
139 ir
.Event
.KeyEvent
.wVirtualKeyCode
,0
141 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
142 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= 27;
143 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
144 ir
.Event
.KeyEvent
.bKeyDown
= 0;
145 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
148 for (k
=j
;k
<len
;k
++) {
149 if (((buf
[k
]>='A') && (buf
[k
]<='Z')) ||
150 ((buf
[k
]>='a') && (buf
[k
]<='z')) ||
156 int subid
,scancode
=0;
158 ir
.EventType
= 1; /* Key_event */
159 ir
.Event
.KeyEvent
.bKeyDown
= 1;
160 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
161 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
163 ir
.Event
.KeyEvent
.wVirtualKeyCode
= 0xad; /* FIXME */
164 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0xad; /* FIXME */
165 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= 0;
169 sscanf(&buf
[j
+2],"%d",&subid
);
171 case 2:/*INS */scancode
= 0xe052;break;
172 case 3:/*DEL */scancode
= 0xe053;break;
173 case 6:/*PGDW*/scancode
= 0xe051;break;
174 case 5:/*PGUP*/scancode
= 0xe049;break;
175 case 11:/*F1 */scancode
= 0x003b;break;
176 case 12:/*F2 */scancode
= 0x003c;break;
177 case 13:/*F3 */scancode
= 0x003d;break;
178 case 14:/*F4 */scancode
= 0x003e;break;
179 case 15:/*F5 */scancode
= 0x003f;break;
180 case 17:/*F6 */scancode
= 0x0040;break;
181 case 18:/*F7 */scancode
= 0x0041;break;
182 case 19:/*F8 */scancode
= 0x0042;break;
183 case 20:/*F9 */scancode
= 0x0043;break;
184 case 21:/*F10 */scancode
= 0x0044;break;
185 case 23:/*F11 */scancode
= 0x00d9;break;
186 case 24:/*F12 */scancode
= 0x00da;break;
187 /* FIXME: Shift-Fx */
189 FIXME("parse ESC[%d~\n",subid
);
193 case 'A': /* Cursor Up */scancode
= 0xe048;break;
194 case 'B': /* Cursor Down */scancode
= 0xe050;break;
195 case 'D': /* Cursor Left */scancode
= 0xe04b;break;
196 case 'C': /* Cursor Right */scancode
= 0xe04d;break;
197 case 'F': /* End */scancode
= 0xe04f;break;
198 case 'H': /* Home */scancode
= 0xe047;break;
200 /* Mouse Button Press (ESCM<button+'!'><x+'!'><y+'!'>) or
201 * Release (ESCM#<x+'!'><y+'!'>
204 ir
.EventType
= MOUSE_EVENT
;
205 ir
.Event
.MouseEvent
.dwMousePosition
.x
= buf
[k
+2]-'!';
206 ir
.Event
.MouseEvent
.dwMousePosition
.y
= buf
[k
+3]-'!';
208 ir
.Event
.MouseEvent
.dwButtonState
= 0;
210 ir
.Event
.MouseEvent
.dwButtonState
= 1<<(buf
[k
+1]-' ');
211 ir
.Event
.MouseEvent
.dwEventFlags
= 0; /* FIXME */
212 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
221 ir
.Event
.KeyEvent
.wVirtualScanCode
= scancode
;
222 ir
.Event
.KeyEvent
.wVirtualKeyCode
= MapVirtualKey16(scancode
,1);
223 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
224 ir
.Event
.KeyEvent
.bKeyDown
= 0;
225 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
233 /****************************************************************************
234 * CONSOLE_get_input (internal)
236 * Reads (nonblocking) as much input events as possible and stores them
237 * in an internal queue.
240 CONSOLE_get_input( HANDLE handle
, BOOL blockwait
)
242 char *buf
= HeapAlloc(GetProcessHeap(),0,1);
249 if (WaitForSingleObject( handle
, 0 )) break;
250 if (!ReadFile( handle
, &inchar
, 1, &res
, NULL
)) break;
251 if (!res
) /* res 0 but readable means EOF? Hmm. */
253 buf
= HeapReAlloc(GetProcessHeap(),0,buf
,len
+1);
256 CONSOLE_string_to_IR(handle
,buf
,len
);
257 HeapFree(GetProcessHeap(),0,buf
);
260 /******************************************************************************
261 * SetConsoleCtrlHandler [KERNEL32.459] Adds function to calling process list
264 * func [I] Address of handler function
265 * add [I] Handler to add or remove
272 * James Sutherland (JamesSutherland@gmx.de)
273 * Added global variables console_ignore_ctrl_c and handlers[]
274 * Does not yet do any error checking, or set LastError if failed.
275 * This doesn't yet matter, since these handlers are not yet called...!
277 static unsigned int console_ignore_ctrl_c
= 0;
278 static HANDLER_ROUTINE
*handlers
[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
279 BOOL WINAPI
SetConsoleCtrlHandler( HANDLER_ROUTINE
*func
, BOOL add
)
281 unsigned int alloc_loop
= sizeof(handlers
)/sizeof(HANDLER_ROUTINE
*);
282 unsigned int done
= 0;
283 FIXME("(%p,%i) - no error checking or testing yet\n", func
, add
);
286 console_ignore_ctrl_c
= add
;
292 if (!handlers
[alloc_loop
] && !done
)
294 handlers
[alloc_loop
] = func
;
298 FIXME("Out of space on CtrlHandler table\n");
304 if (handlers
[alloc_loop
] == func
&& !done
)
306 handlers
[alloc_loop
] = 0;
310 WARN("Attempt to remove non-installed CtrlHandler %p\n",
318 /******************************************************************************
319 * GenerateConsoleCtrlEvent [KERNEL32.275] Simulate a CTRL-C or CTRL-BREAK
322 * dwCtrlEvent [I] Type of event
323 * dwProcessGroupID [I] Process group ID to send event to
326 * Doesn't yet work...!
330 * Failure: False (and *should* [but doesn't] set LastError)
332 BOOL WINAPI
GenerateConsoleCtrlEvent( DWORD dwCtrlEvent
,
333 DWORD dwProcessGroupID
)
335 if (dwCtrlEvent
!= CTRL_C_EVENT
&& dwCtrlEvent
!= CTRL_BREAK_EVENT
)
337 ERR("invalid event %d for PGID %ld\n",
338 (unsigned short)dwCtrlEvent
, dwProcessGroupID
);
341 if (dwProcessGroupID
== GetCurrentProcessId() )
343 FIXME("Attempt to send event %d to self - stub\n",
344 (unsigned short)dwCtrlEvent
);
347 FIXME("event %d to external PGID %ld - not implemented yet\n",
348 (unsigned short)dwCtrlEvent
, dwProcessGroupID
);
353 /******************************************************************************
354 * CreateConsoleScreenBuffer [KERNEL32.151] Creates a console screen buffer
357 * dwDesiredAccess [I] Access flag
358 * dwShareMode [I] Buffer share mode
359 * sa [I] Security attributes
360 * dwFlags [I] Type of buffer to create
361 * lpScreenBufferData [I] Reserved
364 * Should call SetLastError
367 * Success: Handle to new console screen buffer
368 * Failure: INVALID_HANDLE_VALUE
370 HANDLE WINAPI
CreateConsoleScreenBuffer( DWORD dwDesiredAccess
,
371 DWORD dwShareMode
, LPSECURITY_ATTRIBUTES sa
,
372 DWORD dwFlags
, LPVOID lpScreenBufferData
)
374 FIXME("(%ld,%ld,%p,%ld,%p): stub\n",dwDesiredAccess
,
375 dwShareMode
, sa
, dwFlags
, lpScreenBufferData
);
376 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
377 return INVALID_HANDLE_VALUE
;
381 /***********************************************************************
382 * GetConsoleScreenBufferInfo (KERNEL32.190)
384 BOOL WINAPI
GetConsoleScreenBufferInfo( HANDLE hConsoleOutput
,
385 LPCONSOLE_SCREEN_BUFFER_INFO csbi
)
389 csbi
->dwCursorPosition
.x
= 0;
390 csbi
->dwCursorPosition
.y
= 0;
391 csbi
->wAttributes
= 0;
392 csbi
->srWindow
.Left
= 0;
393 csbi
->srWindow
.Right
= 79;
394 csbi
->srWindow
.Top
= 0;
395 csbi
->srWindow
.Bottom
= 23;
396 csbi
->dwMaximumWindowSize
.x
= 80;
397 csbi
->dwMaximumWindowSize
.y
= 24;
402 /******************************************************************************
403 * SetConsoleActiveScreenBuffer [KERNEL32.623] Sets buffer to current console
409 BOOL WINAPI
SetConsoleActiveScreenBuffer(
410 HANDLE hConsoleOutput
) /* [in] Handle to console screen buffer */
412 FIXME("(%x): stub\n", hConsoleOutput
);
417 /***********************************************************************
418 * GetLargestConsoleWindowSize (KERNEL32.226)
420 DWORD WINAPI
GetLargestConsoleWindowSize( HANDLE hConsoleOutput
)
422 return (DWORD
)MAKELONG(80,24);
425 /***********************************************************************
426 * FreeConsole (KERNEL32.267)
428 BOOL WINAPI
FreeConsole(VOID
)
430 return !server_call( REQ_FREE_CONSOLE
);
434 /*************************************************************************
437 * Open a handle to the current process console.
439 HANDLE
CONSOLE_OpenHandle( BOOL output
, DWORD access
, LPSECURITY_ATTRIBUTES sa
)
442 struct open_console_request
*req
= get_req_buffer();
444 req
->output
= output
;
445 req
->access
= access
;
446 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
448 if (!server_call( REQ_OPEN_CONSOLE
)) ret
= req
->handle
;
453 /*************************************************************************
454 * CONSOLE_make_complex [internal]
456 * Turns a CONSOLE kernel object into a complex one.
457 * (switches from output/input using the terminal where WINE was started to
460 * This makes simple commandline tools pipeable, while complex commandline
461 * tools work without getting messed up by debugoutput.
463 * All other functions should work indedependend from this call.
465 * To test for complex console: pid == 0 -> simple, otherwise complex.
467 static BOOL
CONSOLE_make_complex(HANDLE handle
)
469 struct set_console_fd_request
*req
= get_req_buffer();
473 int i
,xpid
,master
,slave
,pty_handle
;
475 if (CONSOLE_GetPid( handle
)) return TRUE
; /* already complex */
477 MESSAGE("Console: Making console complex (creating an xterm)...\n");
479 if (tcgetattr(0, &term
) < 0) {
480 /* ignore failure, or we can't run from a script */
482 term
.c_lflag
= ~(ECHO
|ICANON
);
484 if (wine_openpty(&master
, &slave
, NULL
, &term
, NULL
) < 0)
487 if ((xpid
=fork()) == 0) {
488 tcsetattr(slave
, TCSADRAIN
, &term
);
490 sprintf(buf
, "-Sxx%d", master
);
491 /* "-fn vga" for VGA font. Harmless if vga is not present:
492 * xterm: unable to open font "vga", trying "fixed"....
494 execlp("xterm", "xterm", buf
, "-fn","vga",NULL
);
495 ERR("error creating AllocConsole xterm\n");
498 pty_handle
= FILE_DupUnixHandle( slave
, GENERIC_READ
| GENERIC_WRITE
);
501 if (pty_handle
== -1) return FALSE
;
503 /* most xterms like to print their window ID when used with -S;
504 * read it and continue before the user has a chance...
506 for (i
= 0; i
< 10000; i
++)
508 BOOL ok
= ReadFile( pty_handle
, &c
, 1, NULL
, NULL
);
509 if (!ok
&& !c
) usleep(100); /* wait for xterm to be created */
510 else if (c
== '\n') break;
514 ERR("can't read xterm WID\n");
515 CloseHandle( pty_handle
);
518 req
->handle
= handle
;
519 req
->file_handle
= pty_handle
;
521 server_call( REQ_SET_CONSOLE_FD
);
522 CloseHandle( pty_handle
);
524 /* enable mouseclicks */
525 strcpy( buf
, "\033[?1001s\033[?1000h" );
526 WriteFile(handle
,buf
,strlen(buf
),NULL
,NULL
);
528 strcpy( buf
, "\033]2;" );
529 if (GetConsoleTitleA( buf
+ 4, sizeof(buf
) - 5 ))
532 WriteFile(handle
,buf
,strlen(buf
),NULL
,NULL
);
539 /***********************************************************************
540 * AllocConsole (KERNEL32.103)
542 * creates an xterm with a pty to our program
544 BOOL WINAPI
AllocConsole(VOID
)
546 struct alloc_console_request
*req
= get_req_buffer();
548 int handle_in
, handle_out
;
551 req
->access
= GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
;
552 req
->inherit
= FALSE
;
553 if (server_call( REQ_ALLOC_CONSOLE
)) return FALSE
;
554 handle_in
= req
->handle_in
;
555 handle_out
= req
->handle_out
;
557 if (!DuplicateHandle( GetCurrentProcess(), req
->handle_out
, GetCurrentProcess(), &hStderr
,
558 0, TRUE
, DUPLICATE_SAME_ACCESS
))
560 CloseHandle( handle_in
);
561 CloseHandle( handle_out
);
566 /* NT resets the STD_*_HANDLEs on console alloc */
567 SetStdHandle( STD_INPUT_HANDLE
, handle_in
);
568 SetStdHandle( STD_OUTPUT_HANDLE
, handle_out
);
569 SetStdHandle( STD_ERROR_HANDLE
, hStderr
);
571 SetLastError(ERROR_SUCCESS
);
572 SetConsoleTitleA("Wine Console");
577 /******************************************************************************
578 * GetConsoleCP [KERNEL32.295] Returns the OEM code page for the console
583 UINT WINAPI
GetConsoleCP(VOID
)
589 /***********************************************************************
590 * GetConsoleOutputCP (KERNEL32.189)
592 UINT WINAPI
GetConsoleOutputCP(VOID
)
594 return GetConsoleCP();
597 /***********************************************************************
598 * GetConsoleMode (KERNEL32.188)
600 BOOL WINAPI
GetConsoleMode(HANDLE hcon
,LPDWORD mode
)
603 struct get_console_mode_request
*req
= get_req_buffer();
605 if (!server_call( REQ_GET_CONSOLE_MODE
))
607 if (mode
) *mode
= req
->mode
;
614 /******************************************************************************
615 * SetConsoleMode [KERNEL32.628] Sets input mode of console's input buffer
618 * hcon [I] Handle to console input or screen buffer
619 * mode [I] Input or output mode to set
625 BOOL WINAPI
SetConsoleMode( HANDLE hcon
, DWORD mode
)
627 struct set_console_mode_request
*req
= get_req_buffer();
630 return !server_call( REQ_SET_CONSOLE_MODE
);
634 /***********************************************************************
635 * GetConsoleTitleA (KERNEL32.191)
637 DWORD WINAPI
GetConsoleTitleA(LPSTR title
,DWORD size
)
639 struct get_console_info_request
*req
= get_req_buffer();
643 if ((hcon
= CreateFileA( "CONOUT$", GENERIC_READ
, 0, NULL
,
644 OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
647 if (!server_call( REQ_GET_CONSOLE_INFO
))
649 lstrcpynA( title
, req
->title
, size
);
650 ret
= strlen(req
->title
);
657 /******************************************************************************
658 * GetConsoleTitle32W [KERNEL32.192] Retrieves title string for console
661 * title [O] Address of buffer for title
662 * size [I] Size of buffer
665 * Success: Length of string copied
668 DWORD WINAPI
GetConsoleTitleW( LPWSTR title
, DWORD size
)
673 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, size
))) return 0;
674 ret
= GetConsoleTitleA( tmp
, size
);
675 lstrcpyAtoW( title
, tmp
);
676 HeapFree( GetProcessHeap(), 0, tmp
);
681 /***********************************************************************
682 * WriteConsoleA (KERNEL32.729)
684 BOOL WINAPI
WriteConsoleA( HANDLE hConsoleOutput
,
686 DWORD nNumberOfCharsToWrite
,
687 LPDWORD lpNumberOfCharsWritten
,
690 /* FIXME: should I check if this is a console handle? */
691 return WriteFile(hConsoleOutput
, lpBuffer
, nNumberOfCharsToWrite
,
692 lpNumberOfCharsWritten
, NULL
);
697 if (bufused==curbufsize-1) \
698 buffer = HeapReAlloc(GetProcessHeap(),0,buffer,(curbufsize+=100));\
700 #define SADD(s) { char *x=s;while (*x) {CADD(*x);x++;}}
702 /***********************************************************************
703 * WriteConsoleOutputA (KERNEL32.732)
705 BOOL WINAPI
WriteConsoleOutputA( HANDLE hConsoleOutput
,
706 LPCHAR_INFO lpBuffer
,
709 LPSMALL_RECT lpWriteRegion
)
711 int i
,j
,off
=0,lastattr
=-1;
712 char sbuf
[20],*buffer
=NULL
;
713 int bufused
=0,curbufsize
= 100;
715 const int colormap
[8] = {
719 CONSOLE_make_complex(hConsoleOutput
);
720 buffer
= HeapAlloc(GetProcessHeap(),0,100);
723 TRACE("wr: top = %d, bottom=%d, left=%d,right=%d\n",
725 lpWriteRegion
->Bottom
,
730 for (i
=lpWriteRegion
->Top
;i
<=lpWriteRegion
->Bottom
;i
++) {
731 sprintf(sbuf
,"%c[%d;%dH",27,i
+1,lpWriteRegion
->Left
+1);
733 for (j
=lpWriteRegion
->Left
;j
<=lpWriteRegion
->Right
;j
++) {
734 if (lastattr
!=lpBuffer
[off
].Attributes
) {
735 lastattr
= lpBuffer
[off
].Attributes
;
736 sprintf(sbuf
,"%c[0;%s3%d;4%dm",
738 (lastattr
& FOREGROUND_INTENSITY
)?"1;":"",
739 colormap
[lastattr
&7],
740 colormap
[(lastattr
&0x70)>>4]
742 /* FIXME: BACKGROUND_INTENSITY */
745 CADD(lpBuffer
[off
].Char
.AsciiChar
);
749 sprintf(sbuf
,"%c[0m",27);SADD(sbuf
);
750 WriteFile(hConsoleOutput
,buffer
,bufused
,&res
,NULL
);
751 HeapFree(GetProcessHeap(),0,buffer
);
755 /***********************************************************************
756 * WriteConsoleW (KERNEL32.577)
758 BOOL WINAPI
WriteConsoleW( HANDLE hConsoleOutput
,
760 DWORD nNumberOfCharsToWrite
,
761 LPDWORD lpNumberOfCharsWritten
,
765 LPSTR xstring
=HeapAlloc( GetProcessHeap(), 0, nNumberOfCharsToWrite
);
767 lstrcpynWtoA( xstring
, lpBuffer
,nNumberOfCharsToWrite
);
769 /* FIXME: should I check if this is a console handle? */
770 ret
= WriteFile(hConsoleOutput
, xstring
, nNumberOfCharsToWrite
,
771 lpNumberOfCharsWritten
, NULL
);
772 HeapFree( GetProcessHeap(), 0, xstring
);
777 /***********************************************************************
778 * ReadConsoleA (KERNEL32.419)
780 BOOL WINAPI
ReadConsoleA( HANDLE hConsoleInput
,
782 DWORD nNumberOfCharsToRead
,
783 LPDWORD lpNumberOfCharsRead
,
787 LPSTR xbuf
= (LPSTR
)lpBuffer
;
790 TRACE("(%d,%p,%ld,%p,%p)\n",
791 hConsoleInput
,lpBuffer
,nNumberOfCharsToRead
,
792 lpNumberOfCharsRead
,lpReserved
795 CONSOLE_get_input(hConsoleInput
,FALSE
);
797 /* FIXME: should we read at least 1 char? The SDK does not say */
798 while (charsread
<nNumberOfCharsToRead
)
800 struct read_console_input_request
*req
= get_req_buffer();
801 req
->handle
= hConsoleInput
;
804 if (server_call( REQ_READ_CONSOLE_INPUT
)) return FALSE
;
805 if (!req
->read
) break;
806 if (!ir
.Event
.KeyEvent
.bKeyDown
)
808 if (ir
.EventType
!= KEY_EVENT
)
810 *xbuf
++ = ir
.Event
.KeyEvent
.uChar
.AsciiChar
;
813 if (lpNumberOfCharsRead
)
814 *lpNumberOfCharsRead
= charsread
;
818 /***********************************************************************
819 * ReadConsoleW (KERNEL32.427)
821 BOOL WINAPI
ReadConsoleW( HANDLE hConsoleInput
,
823 DWORD nNumberOfCharsToRead
,
824 LPDWORD lpNumberOfCharsRead
,
828 LPSTR buf
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead
);
833 nNumberOfCharsToRead
,
838 lstrcpynAtoW(lpBuffer
,buf
,nNumberOfCharsToRead
);
839 HeapFree( GetProcessHeap(), 0, buf
);
844 /******************************************************************************
845 * ReadConsoleInput32A [KERNEL32.569] Reads data from a console
848 * hConsoleInput [I] Handle to console input buffer
849 * lpBuffer [O] Address of buffer for read data
850 * nLength [I] Number of records to read
851 * lpNumberOfEventsRead [O] Address of number of records read
857 BOOL WINAPI
ReadConsoleInputA(HANDLE hConsoleInput
, LPINPUT_RECORD lpBuffer
,
858 DWORD nLength
, LPDWORD lpNumberOfEventsRead
)
860 struct read_console_input_request
*req
= get_req_buffer();
862 /* loop until we get at least one event */
865 req
->handle
= hConsoleInput
;
866 req
->count
= nLength
;
868 if (server_call( REQ_READ_CONSOLE_INPUT
)) return FALSE
;
871 memcpy( lpBuffer
, req
+ 1, req
->read
* sizeof(*lpBuffer
) );
872 if (lpNumberOfEventsRead
) *lpNumberOfEventsRead
= req
->read
;
875 CONSOLE_get_input(hConsoleInput
,TRUE
);
876 /*WaitForSingleObject( hConsoleInput, INFINITE32 );*/
881 /***********************************************************************
882 * ReadConsoleInput32W (KERNEL32.570)
884 BOOL WINAPI
ReadConsoleInputW( HANDLE handle
, LPINPUT_RECORD buffer
,
885 DWORD count
, LPDWORD read
)
887 /* FIXME: Fix this if we get UNICODE input. */
888 return ReadConsoleInputA( handle
, buffer
, count
, read
);
892 /***********************************************************************
893 * FlushConsoleInputBuffer (KERNEL32.132)
895 BOOL WINAPI
FlushConsoleInputBuffer( HANDLE handle
)
897 struct read_console_input_request
*req
= get_req_buffer();
898 req
->handle
= handle
;
899 req
->count
= -1; /* get all records */
901 return !server_call( REQ_READ_CONSOLE_INPUT
);
905 /***********************************************************************
906 * PeekConsoleInputA (KERNEL32.550)
908 * Gets 'count' first events (or less) from input queue.
910 * Does not need a complex console.
912 BOOL WINAPI
PeekConsoleInputA( HANDLE handle
, LPINPUT_RECORD buffer
,
913 DWORD count
, LPDWORD read
)
915 struct read_console_input_request
*req
= get_req_buffer();
917 CONSOLE_get_input(handle
,FALSE
);
919 req
->handle
= handle
;
922 if (server_call( REQ_READ_CONSOLE_INPUT
)) return FALSE
;
923 if (req
->read
) memcpy( buffer
, req
+ 1, req
->read
* sizeof(*buffer
) );
924 if (read
) *read
= req
->read
;
929 /***********************************************************************
930 * PeekConsoleInputW (KERNEL32.551)
932 BOOL WINAPI
PeekConsoleInputW(HANDLE hConsoleInput
,
933 LPINPUT_RECORD pirBuffer
,
937 /* FIXME: Hmm. Fix this if we get UNICODE input. */
938 return PeekConsoleInputA(hConsoleInput
,pirBuffer
,cInRecords
,lpcRead
);
942 /******************************************************************************
943 * WriteConsoleInput32A [KERNEL32.730] Write data to a console input buffer
946 BOOL WINAPI
WriteConsoleInputA( HANDLE handle
, INPUT_RECORD
*buffer
,
947 DWORD count
, LPDWORD written
)
949 struct write_console_input_request
*req
= get_req_buffer();
950 const DWORD max
= server_remaining( req
+ 1 ) / sizeof(INPUT_RECORD
);
952 if (written
) *written
= 0;
955 DWORD len
= count
< max
? count
: max
;
957 req
->handle
= handle
;
958 memcpy( req
+ 1, buffer
, len
* sizeof(*buffer
) );
959 if (server_call( REQ_WRITE_CONSOLE_INPUT
)) return FALSE
;
960 if (written
) *written
+= req
->written
;
968 /***********************************************************************
969 * SetConsoleTitle32A (KERNEL32.476)
971 * Sets the console title.
973 * We do not necessarily need to create a complex console for that,
974 * but should remember the title and set it on creation of the latter.
975 * (not fixed at this time).
977 BOOL WINAPI
SetConsoleTitleA(LPCSTR title
)
979 struct set_console_info_request
*req
= get_req_buffer();
983 if ((hcon
= CreateFileA( "CONOUT$", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
984 OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
987 req
->mask
= SET_CONSOLE_INFO_TITLE
;
988 lstrcpynA( req
->title
, title
, server_remaining(req
->title
) );
989 if (server_call( REQ_SET_CONSOLE_INFO
)) goto error
;
990 if (CONSOLE_GetPid( hcon
))
992 /* only set title for complex console (own xterm) */
993 WriteFile( hcon
, "\033]2;", 4, &written
, NULL
);
994 WriteFile( hcon
, title
, strlen(title
), &written
, NULL
);
995 WriteFile( hcon
, "\a", 1, &written
, NULL
);
1000 CloseHandle( hcon
);
1005 /******************************************************************************
1006 * SetConsoleTitle32W [KERNEL32.477] Sets title bar string for console
1009 * title [I] Address of new title
1012 * This should not be calling the A version
1018 BOOL WINAPI
SetConsoleTitleW( LPCWSTR title
)
1022 LPSTR titleA
= HEAP_strdupWtoA( GetProcessHeap(), 0, title
);
1023 ret
= SetConsoleTitleA(titleA
);
1024 HeapFree( GetProcessHeap(), 0, titleA
);
1028 /******************************************************************************
1029 * SetConsoleCursorPosition [KERNEL32.627]
1030 * Sets the cursor position in console
1033 * hConsoleOutput [I] Handle of console screen buffer
1034 * dwCursorPosition [I] New cursor position coordinates
1038 BOOL WINAPI
SetConsoleCursorPosition( HANDLE hcon
, COORD pos
)
1043 /* make console complex only if we change lines, not just in the line */
1045 CONSOLE_make_complex(hcon
);
1047 TRACE("%d (%dx%d)\n", hcon
, pos
.x
, pos
.y
);
1048 /* x are columns, y rows */
1050 /* full screen cursor absolute positioning */
1051 sprintf(xbuf
,"%c[%d;%dH", 0x1B, pos
.y
+1, pos
.x
+1);
1053 /* relative cursor positioning in line (\r to go to 0) */
1054 sprintf(xbuf
,"\r%c[%dC", 0x1B, pos
.x
);
1055 /* FIXME: store internal if we start using own console buffers */
1056 WriteFile(hcon
,xbuf
,strlen(xbuf
),&xlen
,NULL
);
1060 /***********************************************************************
1061 * GetNumberOfConsoleInputEvents (KERNEL32.246)
1063 BOOL WINAPI
GetNumberOfConsoleInputEvents(HANDLE hcon
,LPDWORD nrofevents
)
1065 CONSOLE_get_input(hcon
,FALSE
);
1066 *nrofevents
= 1; /* UMM */
1070 /***********************************************************************
1071 * GetNumberOfConsoleMouseButtons (KERNEL32.358)
1073 BOOL WINAPI
GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons
)
1075 FIXME("(%p): stub\n", nrofbuttons
);
1080 /******************************************************************************
1081 * GetConsoleCursorInfo32 [KERNEL32.296] Gets size and visibility of console
1084 * hcon [I] Handle to console screen buffer
1085 * cinfo [O] Address of cursor information
1091 BOOL WINAPI
GetConsoleCursorInfo( HANDLE hcon
, LPCONSOLE_CURSOR_INFO cinfo
)
1093 struct get_console_info_request
*req
= get_req_buffer();
1095 if (server_call( REQ_GET_CONSOLE_INFO
)) return FALSE
;
1098 cinfo
->dwSize
= req
->cursor_size
;
1099 cinfo
->bVisible
= req
->cursor_visible
;
1105 /******************************************************************************
1106 * SetConsoleCursorInfo32 [KERNEL32.626] Sets size and visibility of cursor
1112 BOOL WINAPI
SetConsoleCursorInfo(
1113 HANDLE hcon
, /* [in] Handle to console screen buffer */
1114 LPCONSOLE_CURSOR_INFO cinfo
) /* [in] Address of cursor information */
1116 struct set_console_info_request
*req
= get_req_buffer();
1120 CONSOLE_make_complex(hcon
);
1121 sprintf(buf
,"\033[?25%c",cinfo
->bVisible
?'h':'l');
1122 WriteFile(hcon
,buf
,strlen(buf
),&xlen
,NULL
);
1125 req
->cursor_size
= cinfo
->dwSize
;
1126 req
->cursor_visible
= cinfo
->bVisible
;
1127 req
->mask
= SET_CONSOLE_INFO_CURSOR
;
1128 return !server_call( REQ_SET_CONSOLE_INFO
);
1132 /******************************************************************************
1133 * SetConsoleWindowInfo [KERNEL32.634] Sets size and position of console
1139 BOOL WINAPI
SetConsoleWindowInfo(
1140 HANDLE hcon
, /* [in] Handle to console screen buffer */
1141 BOOL bAbsolute
, /* [in] Coordinate type flag */
1142 LPSMALL_RECT window
) /* [in] Address of new window rectangle */
1144 FIXME("(%x,%d,%p): stub\n", hcon
, bAbsolute
, window
);
1149 /******************************************************************************
1150 * SetConsoleTextAttribute32 [KERNEL32.631] Sets colors for text
1152 * Sets the foreground and background color attributes of characters
1153 * written to the screen buffer.
1159 BOOL WINAPI
SetConsoleTextAttribute(HANDLE hConsoleOutput
,WORD wAttr
)
1161 const int colormap
[8] = {
1168 TRACE("(%d,%d)\n",hConsoleOutput
,wAttr
);
1169 sprintf(buffer
,"%c[0;%s3%d;4%dm",
1171 (wAttr
& FOREGROUND_INTENSITY
)?"1;":"",
1173 colormap
[(wAttr
&0x70)>>4]
1175 WriteFile(hConsoleOutput
,buffer
,strlen(buffer
),&xlen
,NULL
);
1180 /******************************************************************************
1181 * SetConsoleScreenBufferSize [KERNEL32.630] Changes size of console
1184 * hConsoleOutput [I] Handle to console screen buffer
1185 * dwSize [I] New size in character rows and cols
1191 BOOL WINAPI
SetConsoleScreenBufferSize( HANDLE hConsoleOutput
,
1194 FIXME("(%d,%dx%d): stub\n",hConsoleOutput
,dwSize
.x
,dwSize
.y
);
1199 /******************************************************************************
1200 * FillConsoleOutputCharacterA [KERNEL32.242]
1203 * hConsoleOutput [I] Handle to screen buffer
1204 * cCharacter [I] Character to write
1205 * nLength [I] Number of cells to write to
1206 * dwCoord [I] Coords of first cell
1207 * lpNumCharsWritten [O] Pointer to number of cells written
1213 BOOL WINAPI
FillConsoleOutputCharacterA(
1214 HANDLE hConsoleOutput
,
1218 LPDWORD lpNumCharsWritten
)
1223 SetConsoleCursorPosition(hConsoleOutput
,dwCoord
);
1224 for(count
=0;count
<nLength
;count
++)
1225 WriteFile(hConsoleOutput
,&cCharacter
,1,&xlen
,NULL
);
1226 *lpNumCharsWritten
= nLength
;
1231 /******************************************************************************
1232 * FillConsoleOutputCharacterW [KERNEL32.243] Writes characters to console
1235 * hConsoleOutput [I] Handle to screen buffer
1236 * cCharacter [I] Character to write
1237 * nLength [I] Number of cells to write to
1238 * dwCoord [I] Coords of first cell
1239 * lpNumCharsWritten [O] Pointer to number of cells written
1245 BOOL WINAPI
FillConsoleOutputCharacterW(HANDLE hConsoleOutput
,
1249 LPDWORD lpNumCharsWritten
)
1254 SetConsoleCursorPosition(hConsoleOutput
,dwCoord
);
1255 /* FIXME: not quite correct ... but the lower part of UNICODE char comes
1258 for(count
=0;count
<nLength
;count
++)
1259 WriteFile(hConsoleOutput
,&cCharacter
,1,&xlen
,NULL
);
1260 *lpNumCharsWritten
= nLength
;
1265 /******************************************************************************
1266 * FillConsoleOutputAttribute [KERNEL32.241] Sets attributes for console
1269 * hConsoleOutput [I] Handle to screen buffer
1270 * wAttribute [I] Color attribute to write
1271 * nLength [I] Number of cells to write to
1272 * dwCoord [I] Coords of first cell
1273 * lpNumAttrsWritten [O] Pointer to number of cells written
1279 BOOL WINAPI
FillConsoleOutputAttribute( HANDLE hConsoleOutput
,
1280 WORD wAttribute
, DWORD nLength
, COORD dwCoord
,
1281 LPDWORD lpNumAttrsWritten
)
1283 FIXME("(%d,%d,%ld,%dx%d,%p): stub\n", hConsoleOutput
,
1284 wAttribute
,nLength
,dwCoord
.x
,dwCoord
.y
,lpNumAttrsWritten
);
1285 *lpNumAttrsWritten
= nLength
;
1289 /******************************************************************************
1290 * ReadConsoleOutputCharacter32A [KERNEL32.573]
1295 BOOL WINAPI
ReadConsoleOutputCharacterA(HANDLE hConsoleOutput
,
1296 LPSTR lpstr
, DWORD dword
, COORD coord
, LPDWORD lpdword
)
1298 FIXME("(%d,%p,%ld,%dx%d,%p): stub\n", hConsoleOutput
,lpstr
,
1299 dword
,coord
.x
,coord
.y
,lpdword
);
1300 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1305 /******************************************************************************
1306 * ScrollConsoleScreenBuffer [KERNEL32.612]
1311 BOOL WINAPI
ScrollConsoleScreenBuffer( HANDLE hConsoleOutput
,
1312 LPSMALL_RECT lpScrollRect
, LPSMALL_RECT lpClipRect
,
1313 COORD dwDestOrigin
, LPCHAR_INFO lpFill
)
1315 FIXME("(%d,%p,%p,%dx%d,%p): stub\n", hConsoleOutput
,lpScrollRect
,
1316 lpClipRect
,dwDestOrigin
.x
,dwDestOrigin
.y
,lpFill
);
1317 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);