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"
46 #include "debugtools.h"
48 #include "server/request.h"
51 DEFAULT_DEBUG_CHANNEL(console
)
54 /* FIXME: Should be in an internal header file. OK, so which one?
55 Used by CONSOLE_makecomplex. */
56 FILE *wine_openpty(int *master
, int *slave
, char *name
,
57 struct termios
*term
, struct winsize
*winsize
);
59 /****************************************************************************
62 static BOOL
CONSOLE_GetInfo( HANDLE handle
, struct get_console_info_reply
*reply
)
64 struct get_console_info_request req
;
67 CLIENT_SendRequest( REQ_GET_CONSOLE_INFO
, -1, 1, &req
, sizeof(req
) );
68 return !CLIENT_WaitSimpleReply( reply
, sizeof(*reply
), NULL
);
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
;
116 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= '\r';
117 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VK_RETURN
;
118 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0x1c;
121 /* FIXME: find good values for ^X */
122 ir
.Event
.KeyEvent
.wVirtualKeyCode
= 0xdead;
123 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0xbeef;
128 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
129 ir
.Event
.KeyEvent
.bKeyDown
= 0;
130 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
134 if ((j
==len
-1) || (buf
[j
+1]!='[')) {/* add ESCape on its own */
135 ir
.EventType
= 1; /* Key_event */
136 ir
.Event
.KeyEvent
.bKeyDown
= 1;
137 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
139 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VkKeyScan16(27);
140 ir
.Event
.KeyEvent
.wVirtualScanCode
= MapVirtualKey16(
141 ir
.Event
.KeyEvent
.wVirtualKeyCode
,0
143 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
144 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= 27;
145 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
146 ir
.Event
.KeyEvent
.bKeyDown
= 0;
147 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
150 for (k
=j
;k
<len
;k
++) {
151 if (((buf
[k
]>='A') && (buf
[k
]<='Z')) ||
152 ((buf
[k
]>='a') && (buf
[k
]<='z')) ||
158 int subid
,scancode
=0;
160 ir
.EventType
= 1; /* Key_event */
161 ir
.Event
.KeyEvent
.bKeyDown
= 1;
162 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
163 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
165 ir
.Event
.KeyEvent
.wVirtualKeyCode
= 0xad; /* FIXME */
166 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0xad; /* FIXME */
167 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= 0;
171 sscanf(&buf
[j
+2],"%d",&subid
);
173 case 2:/*INS */scancode
= 0xe052;break;
174 case 3:/*DEL */scancode
= 0xe053;break;
175 case 6:/*PGDW*/scancode
= 0xe051;break;
176 case 5:/*PGUP*/scancode
= 0xe049;break;
177 case 11:/*F1 */scancode
= 0x003b;break;
178 case 12:/*F2 */scancode
= 0x003c;break;
179 case 13:/*F3 */scancode
= 0x003d;break;
180 case 14:/*F4 */scancode
= 0x003e;break;
181 case 15:/*F5 */scancode
= 0x003f;break;
182 case 17:/*F6 */scancode
= 0x0040;break;
183 case 18:/*F7 */scancode
= 0x0041;break;
184 case 19:/*F8 */scancode
= 0x0042;break;
185 case 20:/*F9 */scancode
= 0x0043;break;
186 case 21:/*F10 */scancode
= 0x0044;break;
187 case 23:/*F11 */scancode
= 0x00d9;break;
188 case 24:/*F12 */scancode
= 0x00da;break;
189 /* FIXME: Shift-Fx */
191 FIXME("parse ESC[%d~\n",subid
);
195 case 'A': /* Cursor Up */scancode
= 0xe048;break;
196 case 'B': /* Cursor Down */scancode
= 0xe050;break;
197 case 'D': /* Cursor Left */scancode
= 0xe04b;break;
198 case 'C': /* Cursor Right */scancode
= 0xe04d;break;
199 case 'F': /* End */scancode
= 0xe04f;break;
200 case 'H': /* Home */scancode
= 0xe047;break;
202 /* Mouse Button Press (ESCM<button+'!'><x+'!'><y+'!'>) or
203 * Release (ESCM#<x+'!'><y+'!'>
206 ir
.EventType
= MOUSE_EVENT
;
207 ir
.Event
.MouseEvent
.dwMousePosition
.x
= buf
[k
+2]-'!';
208 ir
.Event
.MouseEvent
.dwMousePosition
.y
= buf
[k
+3]-'!';
210 ir
.Event
.MouseEvent
.dwButtonState
= 0;
212 ir
.Event
.MouseEvent
.dwButtonState
= 1<<(buf
[k
+1]-' ');
213 ir
.Event
.MouseEvent
.dwEventFlags
= 0; /* FIXME */
214 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
223 ir
.Event
.KeyEvent
.wVirtualScanCode
= scancode
;
224 ir
.Event
.KeyEvent
.wVirtualKeyCode
= MapVirtualKey16(scancode
,1);
225 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
226 ir
.Event
.KeyEvent
.bKeyDown
= 0;
227 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
235 /****************************************************************************
236 * CONSOLE_get_input (internal)
238 * Reads (nonblocking) as much input events as possible and stores them
239 * in an internal queue.
242 CONSOLE_get_input( HANDLE handle
, BOOL blockwait
)
244 char *buf
= HeapAlloc(GetProcessHeap(),0,1);
251 if (WaitForSingleObject( handle
, 0 )) break;
252 if (!ReadFile( handle
, &inchar
, 1, &res
, NULL
)) break;
253 if (!res
) /* res 0 but readable means EOF? Hmm. */
255 buf
= HeapReAlloc(GetProcessHeap(),0,buf
,len
+1);
258 CONSOLE_string_to_IR(handle
,buf
,len
);
259 HeapFree(GetProcessHeap(),0,buf
);
262 /******************************************************************************
263 * SetConsoleCtrlHandler [KERNEL32.459] Adds function to calling process list
266 * func [I] Address of handler function
267 * add [I] Handler to add or remove
274 * James Sutherland (JamesSutherland@gmx.de)
275 * Added global variables console_ignore_ctrl_c and handlers[]
276 * Does not yet do any error checking, or set LastError if failed.
277 * This doesn't yet matter, since these handlers are not yet called...!
279 static unsigned int console_ignore_ctrl_c
= 0;
280 static HANDLER_ROUTINE
*handlers
[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
281 BOOL WINAPI
SetConsoleCtrlHandler( HANDLER_ROUTINE
*func
, BOOL add
)
283 unsigned int alloc_loop
= sizeof(handlers
)/sizeof(HANDLER_ROUTINE
*);
284 unsigned int done
= 0;
285 FIXME("(%p,%i) - no error checking or testing yet\n", func
, add
);
288 console_ignore_ctrl_c
= add
;
294 if (!handlers
[alloc_loop
] && !done
)
296 handlers
[alloc_loop
] = func
;
300 FIXME("Out of space on CtrlHandler table\n");
306 if (handlers
[alloc_loop
] == func
&& !done
)
308 handlers
[alloc_loop
] = 0;
312 WARN("Attempt to remove non-installed CtrlHandler %p\n",
320 /******************************************************************************
321 * GenerateConsoleCtrlEvent [KERNEL32.275] Simulate a CTRL-C or CTRL-BREAK
324 * dwCtrlEvent [I] Type of event
325 * dwProcessGroupID [I] Process group ID to send event to
328 * Doesn't yet work...!
332 * Failure: False (and *should* [but doesn't] set LastError)
334 BOOL WINAPI
GenerateConsoleCtrlEvent( DWORD dwCtrlEvent
,
335 DWORD dwProcessGroupID
)
337 if (dwCtrlEvent
!= CTRL_C_EVENT
&& dwCtrlEvent
!= CTRL_BREAK_EVENT
)
339 ERR("invalid event %d for PGID %ld\n",
340 (unsigned short)dwCtrlEvent
, dwProcessGroupID
);
343 if (dwProcessGroupID
== GetCurrentProcessId() )
345 FIXME("Attempt to send event %d to self - stub\n",
346 (unsigned short)dwCtrlEvent
);
349 FIXME("event %d to external PGID %ld - not implemented yet\n",
350 (unsigned short)dwCtrlEvent
, dwProcessGroupID
);
355 /******************************************************************************
356 * CreateConsoleScreenBuffer [KERNEL32.151] Creates a console screen buffer
359 * dwDesiredAccess [I] Access flag
360 * dwShareMode [I] Buffer share mode
361 * sa [I] Security attributes
362 * dwFlags [I] Type of buffer to create
363 * lpScreenBufferData [I] Reserved
366 * Should call SetLastError
369 * Success: Handle to new console screen buffer
370 * Failure: INVALID_HANDLE_VALUE
372 HANDLE WINAPI
CreateConsoleScreenBuffer( DWORD dwDesiredAccess
,
373 DWORD dwShareMode
, LPSECURITY_ATTRIBUTES sa
,
374 DWORD dwFlags
, LPVOID lpScreenBufferData
)
376 FIXME("(%ld,%ld,%p,%ld,%p): stub\n",dwDesiredAccess
,
377 dwShareMode
, sa
, dwFlags
, lpScreenBufferData
);
378 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
379 return INVALID_HANDLE_VALUE
;
383 /***********************************************************************
384 * GetConsoleScreenBufferInfo (KERNEL32.190)
386 BOOL WINAPI
GetConsoleScreenBufferInfo( HANDLE hConsoleOutput
,
387 LPCONSOLE_SCREEN_BUFFER_INFO csbi
)
391 csbi
->dwCursorPosition
.x
= 0;
392 csbi
->dwCursorPosition
.y
= 0;
393 csbi
->wAttributes
= 0;
394 csbi
->srWindow
.Left
= 0;
395 csbi
->srWindow
.Right
= 79;
396 csbi
->srWindow
.Top
= 0;
397 csbi
->srWindow
.Bottom
= 23;
398 csbi
->dwMaximumWindowSize
.x
= 80;
399 csbi
->dwMaximumWindowSize
.y
= 24;
404 /******************************************************************************
405 * SetConsoleActiveScreenBuffer [KERNEL32.623] Sets buffer to current console
411 BOOL WINAPI
SetConsoleActiveScreenBuffer(
412 HANDLE hConsoleOutput
) /* [in] Handle to console screen buffer */
414 FIXME("(%x): stub\n", hConsoleOutput
);
419 /***********************************************************************
420 * GetLargestConsoleWindowSize (KERNEL32.226)
422 DWORD WINAPI
GetLargestConsoleWindowSize( HANDLE hConsoleOutput
)
424 return (DWORD
)MAKELONG(80,24);
427 /***********************************************************************
428 * FreeConsole (KERNEL32.267)
430 BOOL WINAPI
FreeConsole(VOID
)
432 struct free_console_request req
;
433 CLIENT_SendRequest( REQ_FREE_CONSOLE
, -1, 1, &req
, sizeof(req
) );
434 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
438 /*************************************************************************
441 * Open a handle to the current process console.
443 HANDLE
CONSOLE_OpenHandle( BOOL output
, DWORD access
, LPSECURITY_ATTRIBUTES sa
)
445 struct open_console_request req
;
446 struct open_console_reply reply
;
450 req
.inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
451 CLIENT_SendRequest( REQ_OPEN_CONSOLE
, -1, 1, &req
, sizeof(req
) );
453 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
458 /*************************************************************************
459 * CONSOLE_make_complex [internal]
461 * Turns a CONSOLE kernel object into a complex one.
462 * (switches from output/input using the terminal where WINE was started to
465 * This makes simple commandline tools pipeable, while complex commandline
466 * tools work without getting messed up by debugoutput.
468 * All other functions should work indedependend from this call.
470 * To test for complex console: pid == 0 -> simple, otherwise complex.
472 static BOOL
CONSOLE_make_complex(HANDLE handle
)
474 struct set_console_fd_request req
;
475 struct get_console_info_reply info
;
480 int i
,xpid
,master
,slave
;
483 if (!CONSOLE_GetInfo( handle
, &info
)) return FALSE
;
484 if (info
.pid
) return TRUE
; /* already complex */
486 MESSAGE("Console: Making console complex (creating an xterm)...\n");
488 if (tcgetattr(0, &term
) < 0) {
489 /* ignore failure, or we can't run from a script */
491 term
.c_lflag
= ~(ECHO
|ICANON
);
493 if (wine_openpty(&master
, &slave
, NULL
, &term
, NULL
) < 0)
496 if ((xpid
=fork()) == 0) {
497 tcsetattr(slave
, TCSADRAIN
, &term
);
498 sprintf(buf
, "-Sxx%d", master
);
499 /* "-fn vga" for VGA font. Harmless if vga is not present:
500 * xterm: unable to open font "vga", trying "fixed"....
502 execlp("xterm", "xterm", buf
, "-fn","vga",NULL
);
503 ERR("error creating AllocConsole xterm\n");
509 CLIENT_SendRequest( REQ_SET_CONSOLE_FD
, dup(slave
), 1, &req
, sizeof(req
) );
510 CLIENT_WaitReply( NULL
, NULL
, 0 );
512 /* most xterms like to print their window ID when used with -S;
513 * read it and continue before the user has a chance...
515 for (i
=0; c
!='\n'; (status
=read(slave
, &c
, 1)), i
++) {
516 if (status
== -1 && c
== '\0') {
517 /* wait for xterm to be created */
521 ERR("can't read xterm WID\n");
526 /* enable mouseclicks */
527 sprintf(buf
,"%c[?1001s%c[?1000h",27,27);
528 WriteFile(handle
,buf
,strlen(buf
),&xlen
,NULL
);
530 if (GetConsoleTitleA( buf
, sizeof(buf
) ))
532 WriteFile(handle
,"\033]2;",4,&xlen
,NULL
);
533 WriteFile(handle
,buf
,strlen(buf
),&xlen
,NULL
);
534 WriteFile(handle
,"\a",1,&xlen
,NULL
);
541 /***********************************************************************
542 * AllocConsole (KERNEL32.103)
544 * creates an xterm with a pty to our program
546 BOOL WINAPI
AllocConsole(VOID
)
548 struct open_console_request req
;
549 struct open_console_reply reply
;
550 HANDLE hIn
, hOut
, hErr
;
554 CLIENT_SendRequest( REQ_ALLOC_CONSOLE
, -1, 1, &req
, sizeof(req
) );
555 ret
= CLIENT_WaitReply( NULL
, NULL
, 0 );
556 if (ret
!= ERROR_SUCCESS
) {
557 /* Hmm, error returned by server when we already have an
558 * opened console. however, we might have inherited it(?)
559 * and our handles are wrong? puzzling -MM 990330
561 if (ret
!=ERROR_ACCESS_DENIED
) {
562 ERR(" failed to allocate console: %ld\n",ret
);
568 req
.access
= GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
;
570 CLIENT_SendRequest( REQ_OPEN_CONSOLE
, -1, 1, &req
, sizeof(req
) );
571 ret
=CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
572 if (ret
!= ERROR_SUCCESS
)
574 /* FIXME: free console */
575 ERR(" open console error %ld\n",ret
);
581 CLIENT_SendRequest( REQ_OPEN_CONSOLE
, -1, 1, &req
, sizeof(req
) );
582 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
) != ERROR_SUCCESS
)
585 /* FIXME: free console */
590 if (!DuplicateHandle( GetCurrentProcess(), hOut
,
591 GetCurrentProcess(), &hErr
,
592 0, TRUE
, DUPLICATE_SAME_ACCESS
))
599 /* NT resets the STD_*_HANDLEs on console alloc */
600 SetStdHandle(STD_INPUT_HANDLE
, hIn
);
601 SetStdHandle(STD_OUTPUT_HANDLE
, hOut
);
602 SetStdHandle(STD_ERROR_HANDLE
, hErr
);
604 SetLastError(ERROR_SUCCESS
);
605 SetConsoleTitleA("Wine Console");
610 /******************************************************************************
611 * GetConsoleCP [KERNEL32.295] Returns the OEM code page for the console
616 UINT WINAPI
GetConsoleCP(VOID
)
622 /***********************************************************************
623 * GetConsoleOutputCP (KERNEL32.189)
625 UINT WINAPI
GetConsoleOutputCP(VOID
)
627 return GetConsoleCP();
630 /***********************************************************************
631 * GetConsoleMode (KERNEL32.188)
633 BOOL WINAPI
GetConsoleMode(HANDLE hcon
,LPDWORD mode
)
635 struct get_console_mode_request req
;
636 struct get_console_mode_reply reply
;
639 CLIENT_SendRequest( REQ_GET_CONSOLE_MODE
, -1, 1, &req
, sizeof(req
));
640 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
)) return FALSE
;
646 /******************************************************************************
647 * SetConsoleMode [KERNEL32.628] Sets input mode of console's input buffer
650 * hcon [I] Handle to console input or screen buffer
651 * mode [I] Input or output mode to set
657 BOOL WINAPI
SetConsoleMode( HANDLE hcon
, DWORD mode
)
659 struct set_console_mode_request req
;
663 CLIENT_SendRequest( REQ_SET_CONSOLE_MODE
, -1, 1, &req
, sizeof(req
));
664 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
668 /***********************************************************************
669 * GetConsoleTitleA (KERNEL32.191)
671 DWORD WINAPI
GetConsoleTitleA(LPSTR title
,DWORD size
)
673 struct get_console_info_request req
;
674 struct get_console_info_reply reply
;
679 if ((hcon
= CreateFileA( "CONOUT$", GENERIC_READ
, 0, NULL
,
680 OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
683 CLIENT_SendRequest( REQ_GET_CONSOLE_INFO
, -1, 1, &req
, sizeof(req
) );
684 if (!CLIENT_WaitReply( &len
, NULL
, 2, &reply
, sizeof(reply
), title
, size
))
686 if (len
> sizeof(reply
)+size
) title
[size
-1] = 0;
694 /******************************************************************************
695 * GetConsoleTitle32W [KERNEL32.192] Retrieves title string for console
698 * title [O] Address of buffer for title
699 * size [I] Size of buffer
702 * Success: Length of string copied
705 DWORD WINAPI
GetConsoleTitleW( LPWSTR title
, DWORD size
)
710 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, size
))) return 0;
711 ret
= GetConsoleTitleA( tmp
, size
);
712 lstrcpyAtoW( title
, tmp
);
713 HeapFree( GetProcessHeap(), 0, tmp
);
718 /***********************************************************************
719 * WriteConsoleA (KERNEL32.729)
721 BOOL WINAPI
WriteConsoleA( HANDLE hConsoleOutput
,
723 DWORD nNumberOfCharsToWrite
,
724 LPDWORD lpNumberOfCharsWritten
,
727 /* FIXME: should I check if this is a console handle? */
728 return WriteFile(hConsoleOutput
, lpBuffer
, nNumberOfCharsToWrite
,
729 lpNumberOfCharsWritten
, NULL
);
734 if (bufused==curbufsize-1) \
735 buffer = HeapReAlloc(GetProcessHeap(),0,buffer,(curbufsize+=100));\
737 #define SADD(s) { char *x=s;while (*x) {CADD(*x);x++;}}
739 /***********************************************************************
740 * WriteConsoleOutputA (KERNEL32.732)
742 BOOL WINAPI
WriteConsoleOutputA( HANDLE hConsoleOutput
,
743 LPCHAR_INFO lpBuffer
,
746 LPSMALL_RECT lpWriteRegion
)
748 int i
,j
,off
=0,lastattr
=-1;
749 char sbuf
[20],*buffer
=NULL
;
750 int bufused
=0,curbufsize
= 100;
752 const int colormap
[8] = {
756 CONSOLE_make_complex(hConsoleOutput
);
757 buffer
= HeapAlloc(GetProcessHeap(),0,100);;
760 TRACE("wr: top = %d, bottom=%d, left=%d,right=%d\n",
762 lpWriteRegion
->Bottom
,
767 for (i
=lpWriteRegion
->Top
;i
<=lpWriteRegion
->Bottom
;i
++) {
768 sprintf(sbuf
,"%c[%d;%dH",27,i
+1,lpWriteRegion
->Left
+1);
770 for (j
=lpWriteRegion
->Left
;j
<=lpWriteRegion
->Right
;j
++) {
771 if (lastattr
!=lpBuffer
[off
].Attributes
) {
772 lastattr
= lpBuffer
[off
].Attributes
;
773 sprintf(sbuf
,"%c[0;%s3%d;4%dm",
775 (lastattr
& FOREGROUND_INTENSITY
)?"1;":"",
776 colormap
[lastattr
&7],
777 colormap
[(lastattr
&0x70)>>4]
779 /* FIXME: BACKGROUND_INTENSITY */
782 CADD(lpBuffer
[off
].Char
.AsciiChar
);
786 sprintf(sbuf
,"%c[0m",27);SADD(sbuf
);
787 WriteFile(hConsoleOutput
,buffer
,bufused
,&res
,NULL
);
788 HeapFree(GetProcessHeap(),0,buffer
);
792 /***********************************************************************
793 * WriteConsoleW (KERNEL32.577)
795 BOOL WINAPI
WriteConsoleW( HANDLE hConsoleOutput
,
797 DWORD nNumberOfCharsToWrite
,
798 LPDWORD lpNumberOfCharsWritten
,
802 LPSTR xstring
=HeapAlloc( GetProcessHeap(), 0, nNumberOfCharsToWrite
);
804 lstrcpynWtoA( xstring
, lpBuffer
,nNumberOfCharsToWrite
);
806 /* FIXME: should I check if this is a console handle? */
807 ret
= WriteFile(hConsoleOutput
, xstring
, nNumberOfCharsToWrite
,
808 lpNumberOfCharsWritten
, NULL
);
809 HeapFree( GetProcessHeap(), 0, xstring
);
814 /***********************************************************************
815 * ReadConsoleA (KERNEL32.419)
817 BOOL WINAPI
ReadConsoleA( HANDLE hConsoleInput
,
819 DWORD nNumberOfCharsToRead
,
820 LPDWORD lpNumberOfCharsRead
,
824 LPSTR xbuf
= (LPSTR
)lpBuffer
;
825 struct read_console_input_request req
;
828 TRACE("(%d,%p,%ld,%p,%p)\n",
829 hConsoleInput
,lpBuffer
,nNumberOfCharsToRead
,
830 lpNumberOfCharsRead
,lpReserved
833 CONSOLE_get_input(hConsoleInput
,FALSE
);
835 req
.handle
= hConsoleInput
;
839 /* FIXME: should we read at least 1 char? The SDK does not say */
840 while (charsread
<nNumberOfCharsToRead
)
844 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT
, -1, 1, &req
, sizeof(req
) );
845 if (CLIENT_WaitReply( &len
, NULL
, 1, &ir
, sizeof(ir
) ))
847 assert( !(len
% sizeof(ir
)) );
849 if (!ir
.Event
.KeyEvent
.bKeyDown
)
851 if (ir
.EventType
!= KEY_EVENT
)
853 *xbuf
++ = ir
.Event
.KeyEvent
.uChar
.AsciiChar
;
856 if (lpNumberOfCharsRead
)
857 *lpNumberOfCharsRead
= charsread
;
861 /***********************************************************************
862 * ReadConsoleW (KERNEL32.427)
864 BOOL WINAPI
ReadConsoleW( HANDLE hConsoleInput
,
866 DWORD nNumberOfCharsToRead
,
867 LPDWORD lpNumberOfCharsRead
,
871 LPSTR buf
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead
);
876 nNumberOfCharsToRead
,
881 lstrcpynAtoW(lpBuffer
,buf
,nNumberOfCharsToRead
);
882 HeapFree( GetProcessHeap(), 0, buf
);
887 /******************************************************************************
888 * ReadConsoleInput32A [KERNEL32.569] Reads data from a console
891 * hConsoleInput [I] Handle to console input buffer
892 * lpBuffer [O] Address of buffer for read data
893 * nLength [I] Number of records to read
894 * lpNumberOfEventsRead [O] Address of number of records read
900 BOOL WINAPI
ReadConsoleInputA(HANDLE hConsoleInput
,
901 LPINPUT_RECORD lpBuffer
,
902 DWORD nLength
, LPDWORD lpNumberOfEventsRead
)
904 struct read_console_input_request req
;
907 req
.handle
= hConsoleInput
;
911 /* loop until we get at least one event */
914 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT
, -1, 1, &req
, sizeof(req
) );
915 if (CLIENT_WaitReply( &len
, NULL
, 1, lpBuffer
, nLength
* sizeof(*lpBuffer
) ))
917 assert( !(len
% sizeof(INPUT_RECORD
)) );
919 CONSOLE_get_input(hConsoleInput
,TRUE
);
920 /*WaitForSingleObject( hConsoleInput, INFINITE32 );*/
922 if (lpNumberOfEventsRead
) *lpNumberOfEventsRead
= len
/ sizeof(INPUT_RECORD
);
927 /***********************************************************************
928 * ReadConsoleInput32W (KERNEL32.570)
930 BOOL WINAPI
ReadConsoleInputW( HANDLE handle
, LPINPUT_RECORD buffer
,
931 DWORD count
, LPDWORD read
)
933 /* FIXME: Fix this if we get UNICODE input. */
934 return ReadConsoleInputA( handle
, buffer
, count
, read
);
938 /***********************************************************************
939 * FlushConsoleInputBuffer (KERNEL32.132)
941 BOOL WINAPI
FlushConsoleInputBuffer( HANDLE handle
)
943 struct read_console_input_request req
;
947 req
.count
= -1; /* get all records */
949 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT
, -1, 1, &req
, sizeof(req
) );
950 return !CLIENT_WaitReply( &len
, NULL
, 0 );
954 /***********************************************************************
955 * PeekConsoleInputA (KERNEL32.550)
957 * Gets 'count' first events (or less) from input queue.
959 * Does not need a complex console.
961 BOOL WINAPI
PeekConsoleInputA( HANDLE handle
, LPINPUT_RECORD buffer
,
962 DWORD count
, LPDWORD read
)
964 struct read_console_input_request req
;
967 CONSOLE_get_input(handle
,FALSE
);
972 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT
, -1, 1, &req
, sizeof(req
) );
973 if (CLIENT_WaitReply( &len
, NULL
, 1, buffer
, count
* sizeof(*buffer
) ))
975 assert( !(len
% sizeof(INPUT_RECORD
)) );
976 if (read
) *read
= len
/ sizeof(INPUT_RECORD
);
981 /***********************************************************************
982 * PeekConsoleInputW (KERNEL32.551)
984 BOOL WINAPI
PeekConsoleInputW(HANDLE hConsoleInput
,
985 LPINPUT_RECORD pirBuffer
,
989 /* FIXME: Hmm. Fix this if we get UNICODE input. */
990 return PeekConsoleInputA(hConsoleInput
,pirBuffer
,cInRecords
,lpcRead
);
994 /******************************************************************************
995 * WriteConsoleInput32A [KERNEL32.730] Write data to a console input buffer
998 BOOL WINAPI
WriteConsoleInputA( HANDLE handle
, INPUT_RECORD
*buffer
,
999 DWORD count
, LPDWORD written
)
1001 struct write_console_input_request req
;
1002 struct write_console_input_reply reply
;
1004 req
.handle
= handle
;
1006 CLIENT_SendRequest( REQ_WRITE_CONSOLE_INPUT
, -1, 2, &req
, sizeof(req
),
1007 buffer
, count
* sizeof(*buffer
) );
1008 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
)) return FALSE
;
1009 if (written
) *written
= reply
.written
;
1014 /***********************************************************************
1015 * SetConsoleTitle32A (KERNEL32.476)
1017 * Sets the console title.
1019 * We do not necessarily need to create a complex console for that,
1020 * but should remember the title and set it on creation of the latter.
1021 * (not fixed at this time).
1023 BOOL WINAPI
SetConsoleTitleA(LPCSTR title
)
1025 struct set_console_info_request req
;
1026 struct get_console_info_reply info
;
1030 if ((hcon
= CreateFileA( "CONOUT$", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1031 OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
1034 req
.mask
= SET_CONSOLE_INFO_TITLE
;
1035 CLIENT_SendRequest( REQ_SET_CONSOLE_INFO
, -1, 2, &req
, sizeof(req
),
1036 title
, strlen(title
)+1 );
1037 if (CLIENT_WaitReply( NULL
, NULL
, 0 )) goto error
;
1038 if (CONSOLE_GetInfo( hcon
, &info
) && info
.pid
)
1040 /* only set title for complex console (own xterm) */
1041 WriteFile( hcon
, "\033]2;", 4, &written
, NULL
);
1042 WriteFile( hcon
, title
, strlen(title
), &written
, NULL
);
1043 WriteFile( hcon
, "\a", 1, &written
, NULL
);
1045 CloseHandle( hcon
);
1048 CloseHandle( hcon
);
1053 /******************************************************************************
1054 * SetConsoleTitle32W [KERNEL32.477] Sets title bar string for console
1057 * title [I] Address of new title
1060 * This should not be calling the A version
1066 BOOL WINAPI
SetConsoleTitleW( LPCWSTR title
)
1070 LPSTR titleA
= HEAP_strdupWtoA( GetProcessHeap(), 0, title
);
1071 ret
= SetConsoleTitleA(titleA
);
1072 HeapFree( GetProcessHeap(), 0, titleA
);
1076 /******************************************************************************
1077 * SetConsoleCursorPosition [KERNEL32.627]
1078 * Sets the cursor position in console
1081 * hConsoleOutput [I] Handle of console screen buffer
1082 * dwCursorPosition [I] New cursor position coordinates
1086 BOOL WINAPI
SetConsoleCursorPosition( HANDLE hcon
, COORD pos
)
1091 /* make console complex only if we change lines, not just in the line */
1093 CONSOLE_make_complex(hcon
);
1095 TRACE("%d (%dx%d)\n", hcon
, pos
.x
, pos
.y
);
1096 /* x are columns, y rows */
1098 /* full screen cursor absolute positioning */
1099 sprintf(xbuf
,"%c[%d;%dH", 0x1B, pos
.y
+1, pos
.x
+1);
1101 /* relative cursor positioning in line (\r to go to 0) */
1102 sprintf(xbuf
,"\r%c[%dC", 0x1B, pos
.x
);
1103 /* FIXME: store internal if we start using own console buffers */
1104 WriteFile(hcon
,xbuf
,strlen(xbuf
),&xlen
,NULL
);
1108 /***********************************************************************
1109 * GetNumberOfConsoleInputEvents (KERNEL32.246)
1111 BOOL WINAPI
GetNumberOfConsoleInputEvents(HANDLE hcon
,LPDWORD nrofevents
)
1113 CONSOLE_get_input(hcon
,FALSE
);
1114 *nrofevents
= 1; /* UMM */
1118 /***********************************************************************
1119 * GetNumberOfConsoleMouseButtons (KERNEL32.358)
1121 BOOL WINAPI
GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons
)
1123 FIXME("(%p): stub\n", nrofbuttons
);
1128 /******************************************************************************
1129 * GetConsoleCursorInfo32 [KERNEL32.296] Gets size and visibility of console
1132 * hcon [I] Handle to console screen buffer
1133 * cinfo [O] Address of cursor information
1139 BOOL WINAPI
GetConsoleCursorInfo( HANDLE hcon
,
1140 LPCONSOLE_CURSOR_INFO cinfo
)
1142 struct get_console_info_reply reply
;
1144 if (!CONSOLE_GetInfo( hcon
, &reply
)) return FALSE
;
1147 cinfo
->dwSize
= reply
.cursor_size
;
1148 cinfo
->bVisible
= reply
.cursor_visible
;
1154 /******************************************************************************
1155 * SetConsoleCursorInfo32 [KERNEL32.626] Sets size and visibility of cursor
1161 BOOL WINAPI
SetConsoleCursorInfo(
1162 HANDLE hcon
, /* [in] Handle to console screen buffer */
1163 LPCONSOLE_CURSOR_INFO cinfo
) /* [in] Address of cursor information */
1165 struct set_console_info_request req
;
1170 CONSOLE_make_complex(hcon
);
1171 sprintf(buf
,"\033[?25%c",cinfo
->bVisible
?'h':'l');
1172 WriteFile(hcon
,buf
,strlen(buf
),&xlen
,NULL
);
1174 req
.cursor_size
= cinfo
->dwSize
;
1175 req
.cursor_visible
= cinfo
->bVisible
;
1176 req
.mask
= SET_CONSOLE_INFO_CURSOR
;
1177 CLIENT_SendRequest( REQ_SET_CONSOLE_INFO
, -1, 1, &req
, sizeof(req
) );
1178 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
1182 /******************************************************************************
1183 * SetConsoleWindowInfo [KERNEL32.634] Sets size and position of console
1189 BOOL WINAPI
SetConsoleWindowInfo(
1190 HANDLE hcon
, /* [in] Handle to console screen buffer */
1191 BOOL bAbsolute
, /* [in] Coordinate type flag */
1192 LPSMALL_RECT window
) /* [in] Address of new window rectangle */
1194 FIXME("(%x,%d,%p): stub\n", hcon
, bAbsolute
, window
);
1199 /******************************************************************************
1200 * SetConsoleTextAttribute32 [KERNEL32.631] Sets colors for text
1202 * Sets the foreground and background color attributes of characters
1203 * written to the screen buffer.
1209 BOOL WINAPI
SetConsoleTextAttribute(HANDLE hConsoleOutput
,WORD wAttr
)
1211 const int colormap
[8] = {
1218 TRACE("(%d,%d)\n",hConsoleOutput
,wAttr
);
1219 sprintf(buffer
,"%c[0;%s3%d;4%dm",
1221 (wAttr
& FOREGROUND_INTENSITY
)?"1;":"",
1223 colormap
[(wAttr
&0x70)>>4]
1225 WriteFile(hConsoleOutput
,buffer
,strlen(buffer
),&xlen
,NULL
);
1230 /******************************************************************************
1231 * SetConsoleScreenBufferSize [KERNEL32.630] Changes size of console
1234 * hConsoleOutput [I] Handle to console screen buffer
1235 * dwSize [I] New size in character rows and cols
1241 BOOL WINAPI
SetConsoleScreenBufferSize( HANDLE hConsoleOutput
,
1244 FIXME("(%d,%dx%d): stub\n",hConsoleOutput
,dwSize
.x
,dwSize
.y
);
1249 /******************************************************************************
1250 * FillConsoleOutputCharacterA [KERNEL32.242]
1253 * hConsoleOutput [I] Handle to screen buffer
1254 * cCharacter [I] Character to write
1255 * nLength [I] Number of cells to write to
1256 * dwCoord [I] Coords of first cell
1257 * lpNumCharsWritten [O] Pointer to number of cells written
1263 BOOL WINAPI
FillConsoleOutputCharacterA(
1264 HANDLE hConsoleOutput
,
1268 LPDWORD lpNumCharsWritten
)
1273 SetConsoleCursorPosition(hConsoleOutput
,dwCoord
);
1274 for(count
=0;count
<nLength
;count
++)
1275 WriteFile(hConsoleOutput
,&cCharacter
,1,&xlen
,NULL
);
1276 *lpNumCharsWritten
= nLength
;
1281 /******************************************************************************
1282 * FillConsoleOutputCharacterW [KERNEL32.243] Writes characters to console
1285 * hConsoleOutput [I] Handle to screen buffer
1286 * cCharacter [I] Character to write
1287 * nLength [I] Number of cells to write to
1288 * dwCoord [I] Coords of first cell
1289 * lpNumCharsWritten [O] Pointer to number of cells written
1295 BOOL WINAPI
FillConsoleOutputCharacterW(HANDLE hConsoleOutput
,
1299 LPDWORD lpNumCharsWritten
)
1304 SetConsoleCursorPosition(hConsoleOutput
,dwCoord
);
1305 /* FIXME: not quite correct ... but the lower part of UNICODE char comes
1308 for(count
=0;count
<nLength
;count
++)
1309 WriteFile(hConsoleOutput
,&cCharacter
,1,&xlen
,NULL
);
1310 *lpNumCharsWritten
= nLength
;
1315 /******************************************************************************
1316 * FillConsoleOutputAttribute [KERNEL32.241] Sets attributes for console
1319 * hConsoleOutput [I] Handle to screen buffer
1320 * wAttribute [I] Color attribute to write
1321 * nLength [I] Number of cells to write to
1322 * dwCoord [I] Coords of first cell
1323 * lpNumAttrsWritten [O] Pointer to number of cells written
1329 BOOL WINAPI
FillConsoleOutputAttribute( HANDLE hConsoleOutput
,
1330 WORD wAttribute
, DWORD nLength
, COORD dwCoord
,
1331 LPDWORD lpNumAttrsWritten
)
1333 FIXME("(%d,%d,%ld,%dx%d,%p): stub\n", hConsoleOutput
,
1334 wAttribute
,nLength
,dwCoord
.x
,dwCoord
.y
,lpNumAttrsWritten
);
1335 *lpNumAttrsWritten
= nLength
;
1339 /******************************************************************************
1340 * ReadConsoleOutputCharacter32A [KERNEL32.573]
1345 BOOL WINAPI
ReadConsoleOutputCharacterA(HANDLE hConsoleOutput
,
1346 LPSTR lpstr
, DWORD dword
, COORD coord
, LPDWORD lpdword
)
1348 FIXME("(%d,%p,%ld,%dx%d,%p): stub\n", hConsoleOutput
,lpstr
,
1349 dword
,coord
.x
,coord
.y
,lpdword
);
1350 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1355 /******************************************************************************
1356 * ScrollConsoleScreenBuffer [KERNEL32.612]
1361 BOOL WINAPI
ScrollConsoleScreenBuffer( HANDLE hConsoleOutput
,
1362 LPSMALL_RECT lpScrollRect
, LPSMALL_RECT lpClipRect
,
1363 COORD dwDestOrigin
, LPCHAR_INFO lpFill
)
1365 FIXME("(%d,%p,%p,%dx%d,%p): stub\n", hConsoleOutput
,lpScrollRect
,
1366 lpClipRect
,dwDestOrigin
.x
,dwDestOrigin
.y
,lpFill
);
1367 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);