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;
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 struct free_console_request req;
434 CLIENT_SendRequest( REQ_FREE_CONSOLE, -1, 1, &req, sizeof(req) );
435 return !CLIENT_WaitReply( NULL, NULL, 0 );
439 /*************************************************************************
442 * Open a handle to the current process console.
444 HANDLE CONSOLE_OpenHandle( BOOL output, DWORD access, LPSECURITY_ATTRIBUTES sa )
446 struct open_console_request req;
447 struct open_console_reply reply;
451 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
452 CLIENT_SendRequest( REQ_OPEN_CONSOLE, -1, 1, &req, sizeof(req) );
454 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
459 /*************************************************************************
460 * CONSOLE_make_complex [internal]
462 * Turns a CONSOLE kernel object into a complex one.
463 * (switches from output/input using the terminal where WINE was started to
466 * This makes simple commandline tools pipeable, while complex commandline
467 * tools work without getting messed up by debugoutput.
469 * All other functions should work indedependend from this call.
471 * To test for complex console: pid == 0 -> simple, otherwise complex.
473 static BOOL CONSOLE_make_complex(HANDLE handle)
475 struct set_console_fd_request req;
476 struct get_console_info_reply info;
481 int i,xpid,master,slave;
484 if (!CONSOLE_GetInfo( handle, &info )) return FALSE;
485 if (info.pid) return TRUE; /* already complex */
487 MESSAGE("Console: Making console complex (creating an xterm)...\n");
489 if (tcgetattr(0, &term) < 0) {
490 /* ignore failure, or we can't run from a script */
492 term.c_lflag = ~(ECHO|ICANON);
494 if (wine_openpty(&master, &slave, NULL, &term, NULL) < 0)
497 if ((xpid=fork()) == 0) {
498 tcsetattr(slave, TCSADRAIN, &term);
499 sprintf(buf, "-Sxx%d", master);
500 /* "-fn vga" for VGA font. Harmless if vga is not present:
501 * xterm: unable to open font "vga", trying "fixed"....
503 execlp("xterm", "xterm", buf, "-fn","vga",NULL);
504 ERR("error creating AllocConsole xterm\n");
510 CLIENT_SendRequest( REQ_SET_CONSOLE_FD, dup(slave), 1, &req, sizeof(req) );
511 CLIENT_WaitReply( NULL, NULL, 0 );
513 /* most xterms like to print their window ID when used with -S;
514 * read it and continue before the user has a chance...
516 for (i=0; c!='\n'; (status=read(slave, &c, 1)), i++) {
517 if (status == -1 && c == '\0') {
518 /* wait for xterm to be created */
522 ERR("can't read xterm WID\n");
527 /* enable mouseclicks */
528 sprintf(buf,"%c[?1001s%c[?1000h",27,27);
529 WriteFile(handle,buf,strlen(buf),&xlen,NULL);
531 if (GetConsoleTitleA( buf, sizeof(buf) ))
533 WriteFile(handle,"\033]2;",4,&xlen,NULL);
534 WriteFile(handle,buf,strlen(buf),&xlen,NULL);
535 WriteFile(handle,"\a",1,&xlen,NULL);
542 /***********************************************************************
543 * AllocConsole (KERNEL32.103)
545 * creates an xterm with a pty to our program
547 BOOL WINAPI AllocConsole(VOID)
549 struct open_console_request req;
550 struct open_console_reply reply;
551 HANDLE hIn, hOut, hErr;
555 CLIENT_SendRequest( REQ_ALLOC_CONSOLE, -1, 1, &req, sizeof(req) );
556 ret = CLIENT_WaitReply( NULL, NULL, 0 );
557 if (ret != ERROR_SUCCESS) {
558 /* Hmm, error returned by server when we already have an
559 * opened console. however, we might have inherited it(?)
560 * and our handles are wrong? puzzling -MM 990330
562 if (ret!=ERROR_ACCESS_DENIED) {
563 ERR(" failed to allocate console: %ld\n",ret);
569 req.access = GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE;
571 CLIENT_SendRequest( REQ_OPEN_CONSOLE, -1, 1, &req, sizeof(req) );
572 ret =CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
573 if (ret != ERROR_SUCCESS)
575 /* FIXME: free console */
576 ERR(" open console error %ld\n",ret);
582 CLIENT_SendRequest( REQ_OPEN_CONSOLE, -1, 1, &req, sizeof(req) );
583 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ) != ERROR_SUCCESS)
586 /* FIXME: free console */
591 if (!DuplicateHandle( GetCurrentProcess(), hOut,
592 GetCurrentProcess(), &hErr,
593 0, TRUE, DUPLICATE_SAME_ACCESS ))
600 /* NT resets the STD_*_HANDLEs on console alloc */
601 SetStdHandle(STD_INPUT_HANDLE, hIn);
602 SetStdHandle(STD_OUTPUT_HANDLE, hOut);
603 SetStdHandle(STD_ERROR_HANDLE, hErr);
605 SetLastError(ERROR_SUCCESS);
606 SetConsoleTitleA("Wine Console");
611 /******************************************************************************
612 * GetConsoleCP [KERNEL32.295] Returns the OEM code page for the console
617 UINT WINAPI GetConsoleCP(VOID)
623 /***********************************************************************
624 * GetConsoleOutputCP (KERNEL32.189)
626 UINT WINAPI GetConsoleOutputCP(VOID)
628 return GetConsoleCP();
631 /***********************************************************************
632 * GetConsoleMode (KERNEL32.188)
634 BOOL WINAPI GetConsoleMode(HANDLE hcon,LPDWORD mode)
636 struct get_console_mode_request req;
637 struct get_console_mode_reply reply;
640 CLIENT_SendRequest( REQ_GET_CONSOLE_MODE, -1, 1, &req, sizeof(req));
641 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
647 /******************************************************************************
648 * SetConsoleMode [KERNEL32.628] Sets input mode of console's input buffer
651 * hcon [I] Handle to console input or screen buffer
652 * mode [I] Input or output mode to set
658 BOOL WINAPI SetConsoleMode( HANDLE hcon, DWORD mode )
660 struct set_console_mode_request req;
664 CLIENT_SendRequest( REQ_SET_CONSOLE_MODE, -1, 1, &req, sizeof(req));
665 return !CLIENT_WaitReply( NULL, NULL, 0 );
669 /***********************************************************************
670 * GetConsoleTitleA (KERNEL32.191)
672 DWORD WINAPI GetConsoleTitleA(LPSTR title,DWORD size)
674 struct get_console_info_request req;
675 struct get_console_info_reply reply;
680 if ((hcon = CreateFileA( "CONOUT$", GENERIC_READ, 0, NULL,
681 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
684 CLIENT_SendRequest( REQ_GET_CONSOLE_INFO, -1, 1, &req, sizeof(req) );
685 if (!CLIENT_WaitReply( &len, NULL, 2, &reply, sizeof(reply), title, size ))
687 if (len > sizeof(reply)+size) title[size-1] = 0;
695 /******************************************************************************
696 * GetConsoleTitle32W [KERNEL32.192] Retrieves title string for console
699 * title [O] Address of buffer for title
700 * size [I] Size of buffer
703 * Success: Length of string copied
706 DWORD WINAPI GetConsoleTitleW( LPWSTR title, DWORD size )
711 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, size ))) return 0;
712 ret = GetConsoleTitleA( tmp, size );
713 lstrcpyAtoW( title, tmp );
714 HeapFree( GetProcessHeap(), 0, tmp );
719 /***********************************************************************
720 * WriteConsoleA (KERNEL32.729)
722 BOOL WINAPI WriteConsoleA( HANDLE hConsoleOutput,
724 DWORD nNumberOfCharsToWrite,
725 LPDWORD lpNumberOfCharsWritten,
728 /* FIXME: should I check if this is a console handle? */
729 return WriteFile(hConsoleOutput, lpBuffer, nNumberOfCharsToWrite,
730 lpNumberOfCharsWritten, NULL);
735 if (bufused==curbufsize-1) \
736 buffer = HeapReAlloc(GetProcessHeap(),0,buffer,(curbufsize+=100));\
738 #define SADD(s) { char *x=s;while (*x) {CADD(*x);x++;}}
740 /***********************************************************************
741 * WriteConsoleOutputA (KERNEL32.732)
743 BOOL WINAPI WriteConsoleOutputA( HANDLE hConsoleOutput,
744 LPCHAR_INFO lpBuffer,
747 LPSMALL_RECT lpWriteRegion)
749 int i,j,off=0,lastattr=-1;
750 char sbuf[20],*buffer=NULL;
751 int bufused=0,curbufsize = 100;
753 const int colormap[8] = {
757 CONSOLE_make_complex(hConsoleOutput);
758 buffer = HeapAlloc(GetProcessHeap(),0,100);;
761 TRACE("wr: top = %d, bottom=%d, left=%d,right=%d\n",
763 lpWriteRegion->Bottom,
768 for (i=lpWriteRegion->Top;i<=lpWriteRegion->Bottom;i++) {
769 sprintf(sbuf,"%c[%d;%dH",27,i+1,lpWriteRegion->Left+1);
771 for (j=lpWriteRegion->Left;j<=lpWriteRegion->Right;j++) {
772 if (lastattr!=lpBuffer[off].Attributes) {
773 lastattr = lpBuffer[off].Attributes;
774 sprintf(sbuf,"%c[0;%s3%d;4%dm",
776 (lastattr & FOREGROUND_INTENSITY)?"1;":"",
777 colormap[lastattr&7],
778 colormap[(lastattr&0x70)>>4]
780 /* FIXME: BACKGROUND_INTENSITY */
783 CADD(lpBuffer[off].Char.AsciiChar);
787 sprintf(sbuf,"%c[0m",27);SADD(sbuf);
788 WriteFile(hConsoleOutput,buffer,bufused,&res,NULL);
789 HeapFree(GetProcessHeap(),0,buffer);
793 /***********************************************************************
794 * WriteConsoleW (KERNEL32.577)
796 BOOL WINAPI WriteConsoleW( HANDLE hConsoleOutput,
798 DWORD nNumberOfCharsToWrite,
799 LPDWORD lpNumberOfCharsWritten,
803 LPSTR xstring=HeapAlloc( GetProcessHeap(), 0, nNumberOfCharsToWrite );
805 lstrcpynWtoA( xstring, lpBuffer,nNumberOfCharsToWrite);
807 /* FIXME: should I check if this is a console handle? */
808 ret= WriteFile(hConsoleOutput, xstring, nNumberOfCharsToWrite,
809 lpNumberOfCharsWritten, NULL);
810 HeapFree( GetProcessHeap(), 0, xstring );
815 /***********************************************************************
816 * ReadConsoleA (KERNEL32.419)
818 BOOL WINAPI ReadConsoleA( HANDLE hConsoleInput,
820 DWORD nNumberOfCharsToRead,
821 LPDWORD lpNumberOfCharsRead,
825 LPSTR xbuf = (LPSTR)lpBuffer;
826 struct read_console_input_request req;
829 TRACE("(%d,%p,%ld,%p,%p)\n",
830 hConsoleInput,lpBuffer,nNumberOfCharsToRead,
831 lpNumberOfCharsRead,lpReserved
834 CONSOLE_get_input(hConsoleInput,FALSE);
836 req.handle = hConsoleInput;
840 /* FIXME: should we read at least 1 char? The SDK does not say */
841 while (charsread<nNumberOfCharsToRead)
845 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
846 if (CLIENT_WaitReply( &len, NULL, 1, &ir, sizeof(ir) ))
848 assert( !(len % sizeof(ir)) );
850 if (!ir.Event.KeyEvent.bKeyDown)
852 if (ir.EventType != KEY_EVENT)
854 *xbuf++ = ir.Event.KeyEvent.uChar.AsciiChar;
857 if (lpNumberOfCharsRead)
858 *lpNumberOfCharsRead = charsread;
862 /***********************************************************************
863 * ReadConsoleW (KERNEL32.427)
865 BOOL WINAPI ReadConsoleW( HANDLE hConsoleInput,
867 DWORD nNumberOfCharsToRead,
868 LPDWORD lpNumberOfCharsRead,
872 LPSTR buf = (LPSTR)HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead);
877 nNumberOfCharsToRead,
882 lstrcpynAtoW(lpBuffer,buf,nNumberOfCharsToRead);
883 HeapFree( GetProcessHeap(), 0, buf );
888 /******************************************************************************
889 * ReadConsoleInput32A [KERNEL32.569] Reads data from a console
892 * hConsoleInput [I] Handle to console input buffer
893 * lpBuffer [O] Address of buffer for read data
894 * nLength [I] Number of records to read
895 * lpNumberOfEventsRead [O] Address of number of records read
901 BOOL WINAPI ReadConsoleInputA(HANDLE hConsoleInput,
902 LPINPUT_RECORD lpBuffer,
903 DWORD nLength, LPDWORD lpNumberOfEventsRead)
905 struct read_console_input_request req;
908 req.handle = hConsoleInput;
912 /* loop until we get at least one event */
915 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
916 if (CLIENT_WaitReply( &len, NULL, 1, lpBuffer, nLength * sizeof(*lpBuffer) ))
918 assert( !(len % sizeof(INPUT_RECORD)) );
920 CONSOLE_get_input(hConsoleInput,TRUE);
921 /*WaitForSingleObject( hConsoleInput, INFINITE32 );*/
923 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = len / sizeof(INPUT_RECORD);
928 /***********************************************************************
929 * ReadConsoleInput32W (KERNEL32.570)
931 BOOL WINAPI ReadConsoleInputW( HANDLE handle, LPINPUT_RECORD buffer,
932 DWORD count, LPDWORD read )
934 /* FIXME: Fix this if we get UNICODE input. */
935 return ReadConsoleInputA( handle, buffer, count, read );
939 /***********************************************************************
940 * FlushConsoleInputBuffer (KERNEL32.132)
942 BOOL WINAPI FlushConsoleInputBuffer( HANDLE handle )
944 struct read_console_input_request req;
948 req.count = -1; /* get all records */
950 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
951 return !CLIENT_WaitReply( &len, NULL, 0 );
955 /***********************************************************************
956 * PeekConsoleInputA (KERNEL32.550)
958 * Gets 'count' first events (or less) from input queue.
960 * Does not need a complex console.
962 BOOL WINAPI PeekConsoleInputA( HANDLE handle, LPINPUT_RECORD buffer,
963 DWORD count, LPDWORD read )
965 struct read_console_input_request req;
968 CONSOLE_get_input(handle,FALSE);
973 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
974 if (CLIENT_WaitReply( &len, NULL, 1, buffer, count * sizeof(*buffer) ))
976 assert( !(len % sizeof(INPUT_RECORD)) );
977 if (read) *read = len / sizeof(INPUT_RECORD);
982 /***********************************************************************
983 * PeekConsoleInputW (KERNEL32.551)
985 BOOL WINAPI PeekConsoleInputW(HANDLE hConsoleInput,
986 LPINPUT_RECORD pirBuffer,
990 /* FIXME: Hmm. Fix this if we get UNICODE input. */
991 return PeekConsoleInputA(hConsoleInput,pirBuffer,cInRecords,lpcRead);
995 /******************************************************************************
996 * WriteConsoleInput32A [KERNEL32.730] Write data to a console input buffer
999 BOOL WINAPI WriteConsoleInputA( HANDLE handle, INPUT_RECORD *buffer,
1000 DWORD count, LPDWORD written )
1002 struct write_console_input_request req;
1003 struct write_console_input_reply reply;
1005 req.handle = handle;
1007 CLIENT_SendRequest( REQ_WRITE_CONSOLE_INPUT, -1, 2, &req, sizeof(req),
1008 buffer, count * sizeof(*buffer) );
1009 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
1010 if (written) *written = reply.written;
1015 /***********************************************************************
1016 * SetConsoleTitle32A (KERNEL32.476)
1018 * Sets the console title.
1020 * We do not necessarily need to create a complex console for that,
1021 * but should remember the title and set it on creation of the latter.
1022 * (not fixed at this time).
1024 BOOL WINAPI SetConsoleTitleA(LPCSTR title)
1026 struct set_console_info_request req;
1027 struct get_console_info_reply info;
1031 if ((hcon = CreateFileA( "CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
1032 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
1035 req.mask = SET_CONSOLE_INFO_TITLE;
1036 CLIENT_SendRequest( REQ_SET_CONSOLE_INFO, -1, 2, &req, sizeof(req),
1037 title, strlen(title)+1 );
1038 if (CLIENT_WaitReply( NULL, NULL, 0 )) goto error;
1039 if (CONSOLE_GetInfo( hcon, &info ) && info.pid)
1041 /* only set title for complex console (own xterm) */
1042 WriteFile( hcon, "\033]2;", 4, &written, NULL );
1043 WriteFile( hcon, title, strlen(title), &written, NULL );
1044 WriteFile( hcon, "\a", 1, &written, NULL );
1046 CloseHandle( hcon );
1049 CloseHandle( hcon );
1054 /******************************************************************************
1055 * SetConsoleTitle32W [KERNEL32.477] Sets title bar string for console
1058 * title [I] Address of new title
1061 * This should not be calling the A version
1067 BOOL WINAPI SetConsoleTitleW( LPCWSTR title )
1071 LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
1072 ret = SetConsoleTitleA(titleA);
1073 HeapFree( GetProcessHeap(), 0, titleA );
1077 /******************************************************************************
1078 * SetConsoleCursorPosition [KERNEL32.627]
1079 * Sets the cursor position in console
1082 * hConsoleOutput [I] Handle of console screen buffer
1083 * dwCursorPosition [I] New cursor position coordinates
1087 BOOL WINAPI SetConsoleCursorPosition( HANDLE hcon, COORD pos )
1092 /* make console complex only if we change lines, not just in the line */
1094 CONSOLE_make_complex(hcon);
1096 TRACE("%d (%dx%d)\n", hcon, pos.x , pos.y );
1097 /* x are columns, y rows */
1099 /* full screen cursor absolute positioning */
1100 sprintf(xbuf,"%c[%d;%dH", 0x1B, pos.y+1, pos.x+1);
1102 /* relative cursor positioning in line (\r to go to 0) */
1103 sprintf(xbuf,"\r%c[%dC", 0x1B, pos.x);
1104 /* FIXME: store internal if we start using own console buffers */
1105 WriteFile(hcon,xbuf,strlen(xbuf),&xlen,NULL);
1109 /***********************************************************************
1110 * GetNumberOfConsoleInputEvents (KERNEL32.246)
1112 BOOL WINAPI GetNumberOfConsoleInputEvents(HANDLE hcon,LPDWORD nrofevents)
1114 CONSOLE_get_input(hcon,FALSE);
1115 *nrofevents = 1; /* UMM */
1119 /***********************************************************************
1120 * GetNumberOfConsoleMouseButtons (KERNEL32.358)
1122 BOOL WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
1124 FIXME("(%p): stub\n", nrofbuttons);
1129 /******************************************************************************
1130 * GetConsoleCursorInfo32 [KERNEL32.296] Gets size and visibility of console
1133 * hcon [I] Handle to console screen buffer
1134 * cinfo [O] Address of cursor information
1140 BOOL WINAPI GetConsoleCursorInfo( HANDLE hcon,
1141 LPCONSOLE_CURSOR_INFO cinfo )
1143 struct get_console_info_reply reply;
1145 if (!CONSOLE_GetInfo( hcon, &reply )) return FALSE;
1148 cinfo->dwSize = reply.cursor_size;
1149 cinfo->bVisible = reply.cursor_visible;
1155 /******************************************************************************
1156 * SetConsoleCursorInfo32 [KERNEL32.626] Sets size and visibility of cursor
1162 BOOL WINAPI SetConsoleCursorInfo(
1163 HANDLE hcon, /* [in] Handle to console screen buffer */
1164 LPCONSOLE_CURSOR_INFO cinfo) /* [in] Address of cursor information */
1166 struct set_console_info_request req;
1171 CONSOLE_make_complex(hcon);
1172 sprintf(buf,"\033[?25%c",cinfo->bVisible?'h':'l');
1173 WriteFile(hcon,buf,strlen(buf),&xlen,NULL);
1175 req.cursor_size = cinfo->dwSize;
1176 req.cursor_visible = cinfo->bVisible;
1177 req.mask = SET_CONSOLE_INFO_CURSOR;
1178 CLIENT_SendRequest( REQ_SET_CONSOLE_INFO, -1, 1, &req, sizeof(req) );
1179 return !CLIENT_WaitReply( NULL, NULL, 0 );
1183 /******************************************************************************
1184 * SetConsoleWindowInfo [KERNEL32.634] Sets size and position of console
1190 BOOL WINAPI SetConsoleWindowInfo(
1191 HANDLE hcon, /* [in] Handle to console screen buffer */
1192 BOOL bAbsolute, /* [in] Coordinate type flag */
1193 LPSMALL_RECT window) /* [in] Address of new window rectangle */
1195 FIXME("(%x,%d,%p): stub\n", hcon, bAbsolute, window);
1200 /******************************************************************************
1201 * SetConsoleTextAttribute32 [KERNEL32.631] Sets colors for text
1203 * Sets the foreground and background color attributes of characters
1204 * written to the screen buffer.
1210 BOOL WINAPI SetConsoleTextAttribute(HANDLE hConsoleOutput,WORD wAttr)
1212 const int colormap[8] = {
1219 TRACE("(%d,%d)\n",hConsoleOutput,wAttr);
1220 sprintf(buffer,"%c[0;%s3%d;4%dm",
1222 (wAttr & FOREGROUND_INTENSITY)?"1;":"",
1224 colormap[(wAttr&0x70)>>4]
1226 WriteFile(hConsoleOutput,buffer,strlen(buffer),&xlen,NULL);
1231 /******************************************************************************
1232 * SetConsoleScreenBufferSize [KERNEL32.630] Changes size of console
1235 * hConsoleOutput [I] Handle to console screen buffer
1236 * dwSize [I] New size in character rows and cols
1242 BOOL WINAPI SetConsoleScreenBufferSize( HANDLE hConsoleOutput,
1245 FIXME("(%d,%dx%d): stub\n",hConsoleOutput,dwSize.x,dwSize.y);
1250 /******************************************************************************
1251 * FillConsoleOutputCharacterA [KERNEL32.242]
1254 * hConsoleOutput [I] Handle to screen buffer
1255 * cCharacter [I] Character to write
1256 * nLength [I] Number of cells to write to
1257 * dwCoord [I] Coords of first cell
1258 * lpNumCharsWritten [O] Pointer to number of cells written
1264 BOOL WINAPI FillConsoleOutputCharacterA(
1265 HANDLE hConsoleOutput,
1269 LPDWORD lpNumCharsWritten)
1274 SetConsoleCursorPosition(hConsoleOutput,dwCoord);
1275 for(count=0;count<nLength;count++)
1276 WriteFile(hConsoleOutput,&cCharacter,1,&xlen,NULL);
1277 *lpNumCharsWritten = nLength;
1282 /******************************************************************************
1283 * FillConsoleOutputCharacterW [KERNEL32.243] Writes characters to console
1286 * hConsoleOutput [I] Handle to screen buffer
1287 * cCharacter [I] Character to write
1288 * nLength [I] Number of cells to write to
1289 * dwCoord [I] Coords of first cell
1290 * lpNumCharsWritten [O] Pointer to number of cells written
1296 BOOL WINAPI FillConsoleOutputCharacterW(HANDLE hConsoleOutput,
1300 LPDWORD lpNumCharsWritten)
1305 SetConsoleCursorPosition(hConsoleOutput,dwCoord);
1306 /* FIXME: not quite correct ... but the lower part of UNICODE char comes
1309 for(count=0;count<nLength;count++)
1310 WriteFile(hConsoleOutput,&cCharacter,1,&xlen,NULL);
1311 *lpNumCharsWritten = nLength;
1316 /******************************************************************************
1317 * FillConsoleOutputAttribute [KERNEL32.241] Sets attributes for console
1320 * hConsoleOutput [I] Handle to screen buffer
1321 * wAttribute [I] Color attribute to write
1322 * nLength [I] Number of cells to write to
1323 * dwCoord [I] Coords of first cell
1324 * lpNumAttrsWritten [O] Pointer to number of cells written
1330 BOOL WINAPI FillConsoleOutputAttribute( HANDLE hConsoleOutput,
1331 WORD wAttribute, DWORD nLength, COORD dwCoord,
1332 LPDWORD lpNumAttrsWritten)
1334 FIXME("(%d,%d,%ld,%dx%d,%p): stub\n", hConsoleOutput,
1335 wAttribute,nLength,dwCoord.x,dwCoord.y,lpNumAttrsWritten);
1336 *lpNumAttrsWritten = nLength;
1340 /******************************************************************************
1341 * ReadConsoleOutputCharacter32A [KERNEL32.573]
1346 BOOL WINAPI ReadConsoleOutputCharacterA(HANDLE hConsoleOutput,
1347 LPSTR lpstr, DWORD dword, COORD coord, LPDWORD lpdword)
1349 FIXME("(%d,%p,%ld,%dx%d,%p): stub\n", hConsoleOutput,lpstr,
1350 dword,coord.x,coord.y,lpdword);
1351 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1356 /******************************************************************************
1357 * ScrollConsoleScreenBuffer [KERNEL32.612]
1362 BOOL WINAPI ScrollConsoleScreenBuffer( HANDLE hConsoleOutput,
1363 LPSMALL_RECT lpScrollRect, LPSMALL_RECT lpClipRect,
1364 COORD dwDestOrigin, LPCHAR_INFO lpFill)
1366 FIXME("(%d,%p,%p,%dx%d,%p): stub\n", hConsoleOutput,lpScrollRect,
1367 lpClipRect,dwDestOrigin.x,dwDestOrigin.y,lpFill);
1368 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);