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 alloc_console_request req;
550 struct alloc_console_reply reply;
554 req.access = GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE;
556 CLIENT_SendRequest( REQ_ALLOC_CONSOLE, -1, 1, &req, sizeof(req) );
557 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ) != ERROR_SUCCESS)
560 if (!DuplicateHandle( GetCurrentProcess(), reply.handle_out, GetCurrentProcess(), &hStderr,
561 0, TRUE, DUPLICATE_SAME_ACCESS ))
563 CloseHandle( reply.handle_in );
564 CloseHandle( reply.handle_out );
569 /* NT resets the STD_*_HANDLEs on console alloc */
570 SetStdHandle( STD_INPUT_HANDLE, reply.handle_in );
571 SetStdHandle( STD_OUTPUT_HANDLE, reply.handle_out );
572 SetStdHandle( STD_ERROR_HANDLE, hStderr );
574 SetLastError(ERROR_SUCCESS);
575 SetConsoleTitleA("Wine Console");
580 /******************************************************************************
581 * GetConsoleCP [KERNEL32.295] Returns the OEM code page for the console
586 UINT WINAPI GetConsoleCP(VOID)
592 /***********************************************************************
593 * GetConsoleOutputCP (KERNEL32.189)
595 UINT WINAPI GetConsoleOutputCP(VOID)
597 return GetConsoleCP();
600 /***********************************************************************
601 * GetConsoleMode (KERNEL32.188)
603 BOOL WINAPI GetConsoleMode(HANDLE hcon,LPDWORD mode)
605 struct get_console_mode_request req;
606 struct get_console_mode_reply reply;
609 CLIENT_SendRequest( REQ_GET_CONSOLE_MODE, -1, 1, &req, sizeof(req));
610 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
616 /******************************************************************************
617 * SetConsoleMode [KERNEL32.628] Sets input mode of console's input buffer
620 * hcon [I] Handle to console input or screen buffer
621 * mode [I] Input or output mode to set
627 BOOL WINAPI SetConsoleMode( HANDLE hcon, DWORD mode )
629 struct set_console_mode_request req;
633 CLIENT_SendRequest( REQ_SET_CONSOLE_MODE, -1, 1, &req, sizeof(req));
634 return !CLIENT_WaitReply( NULL, NULL, 0 );
638 /***********************************************************************
639 * GetConsoleTitleA (KERNEL32.191)
641 DWORD WINAPI GetConsoleTitleA(LPSTR title,DWORD size)
643 struct get_console_info_request req;
644 struct get_console_info_reply reply;
649 if ((hcon = CreateFileA( "CONOUT$", GENERIC_READ, 0, NULL,
650 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
653 CLIENT_SendRequest( REQ_GET_CONSOLE_INFO, -1, 1, &req, sizeof(req) );
654 if (!CLIENT_WaitReply( &len, NULL, 2, &reply, sizeof(reply), title, size ))
656 if (len > sizeof(reply)+size) title[size-1] = 0;
664 /******************************************************************************
665 * GetConsoleTitle32W [KERNEL32.192] Retrieves title string for console
668 * title [O] Address of buffer for title
669 * size [I] Size of buffer
672 * Success: Length of string copied
675 DWORD WINAPI GetConsoleTitleW( LPWSTR title, DWORD size )
680 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, size ))) return 0;
681 ret = GetConsoleTitleA( tmp, size );
682 lstrcpyAtoW( title, tmp );
683 HeapFree( GetProcessHeap(), 0, tmp );
688 /***********************************************************************
689 * WriteConsoleA (KERNEL32.729)
691 BOOL WINAPI WriteConsoleA( HANDLE hConsoleOutput,
693 DWORD nNumberOfCharsToWrite,
694 LPDWORD lpNumberOfCharsWritten,
697 /* FIXME: should I check if this is a console handle? */
698 return WriteFile(hConsoleOutput, lpBuffer, nNumberOfCharsToWrite,
699 lpNumberOfCharsWritten, NULL);
704 if (bufused==curbufsize-1) \
705 buffer = HeapReAlloc(GetProcessHeap(),0,buffer,(curbufsize+=100));\
707 #define SADD(s) { char *x=s;while (*x) {CADD(*x);x++;}}
709 /***********************************************************************
710 * WriteConsoleOutputA (KERNEL32.732)
712 BOOL WINAPI WriteConsoleOutputA( HANDLE hConsoleOutput,
713 LPCHAR_INFO lpBuffer,
716 LPSMALL_RECT lpWriteRegion)
718 int i,j,off=0,lastattr=-1;
719 char sbuf[20],*buffer=NULL;
720 int bufused=0,curbufsize = 100;
722 const int colormap[8] = {
726 CONSOLE_make_complex(hConsoleOutput);
727 buffer = HeapAlloc(GetProcessHeap(),0,100);;
730 TRACE("wr: top = %d, bottom=%d, left=%d,right=%d\n",
732 lpWriteRegion->Bottom,
737 for (i=lpWriteRegion->Top;i<=lpWriteRegion->Bottom;i++) {
738 sprintf(sbuf,"%c[%d;%dH",27,i+1,lpWriteRegion->Left+1);
740 for (j=lpWriteRegion->Left;j<=lpWriteRegion->Right;j++) {
741 if (lastattr!=lpBuffer[off].Attributes) {
742 lastattr = lpBuffer[off].Attributes;
743 sprintf(sbuf,"%c[0;%s3%d;4%dm",
745 (lastattr & FOREGROUND_INTENSITY)?"1;":"",
746 colormap[lastattr&7],
747 colormap[(lastattr&0x70)>>4]
749 /* FIXME: BACKGROUND_INTENSITY */
752 CADD(lpBuffer[off].Char.AsciiChar);
756 sprintf(sbuf,"%c[0m",27);SADD(sbuf);
757 WriteFile(hConsoleOutput,buffer,bufused,&res,NULL);
758 HeapFree(GetProcessHeap(),0,buffer);
762 /***********************************************************************
763 * WriteConsoleW (KERNEL32.577)
765 BOOL WINAPI WriteConsoleW( HANDLE hConsoleOutput,
767 DWORD nNumberOfCharsToWrite,
768 LPDWORD lpNumberOfCharsWritten,
772 LPSTR xstring=HeapAlloc( GetProcessHeap(), 0, nNumberOfCharsToWrite );
774 lstrcpynWtoA( xstring, lpBuffer,nNumberOfCharsToWrite);
776 /* FIXME: should I check if this is a console handle? */
777 ret= WriteFile(hConsoleOutput, xstring, nNumberOfCharsToWrite,
778 lpNumberOfCharsWritten, NULL);
779 HeapFree( GetProcessHeap(), 0, xstring );
784 /***********************************************************************
785 * ReadConsoleA (KERNEL32.419)
787 BOOL WINAPI ReadConsoleA( HANDLE hConsoleInput,
789 DWORD nNumberOfCharsToRead,
790 LPDWORD lpNumberOfCharsRead,
794 LPSTR xbuf = (LPSTR)lpBuffer;
795 struct read_console_input_request req;
796 struct read_console_input_reply reply;
799 TRACE("(%d,%p,%ld,%p,%p)\n",
800 hConsoleInput,lpBuffer,nNumberOfCharsToRead,
801 lpNumberOfCharsRead,lpReserved
804 CONSOLE_get_input(hConsoleInput,FALSE);
806 req.handle = hConsoleInput;
810 /* FIXME: should we read at least 1 char? The SDK does not say */
811 while (charsread<nNumberOfCharsToRead)
815 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
816 if (CLIENT_WaitReply( &len, NULL, 2, &reply, sizeof(reply), &ir, sizeof(ir) ))
818 len -= sizeof(reply);
819 assert( !(len % sizeof(ir)) );
821 if (!ir.Event.KeyEvent.bKeyDown)
823 if (ir.EventType != KEY_EVENT)
825 *xbuf++ = ir.Event.KeyEvent.uChar.AsciiChar;
828 if (lpNumberOfCharsRead)
829 *lpNumberOfCharsRead = charsread;
833 /***********************************************************************
834 * ReadConsoleW (KERNEL32.427)
836 BOOL WINAPI ReadConsoleW( HANDLE hConsoleInput,
838 DWORD nNumberOfCharsToRead,
839 LPDWORD lpNumberOfCharsRead,
843 LPSTR buf = (LPSTR)HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead);
848 nNumberOfCharsToRead,
853 lstrcpynAtoW(lpBuffer,buf,nNumberOfCharsToRead);
854 HeapFree( GetProcessHeap(), 0, buf );
859 /******************************************************************************
860 * ReadConsoleInput32A [KERNEL32.569] Reads data from a console
863 * hConsoleInput [I] Handle to console input buffer
864 * lpBuffer [O] Address of buffer for read data
865 * nLength [I] Number of records to read
866 * lpNumberOfEventsRead [O] Address of number of records read
872 BOOL WINAPI ReadConsoleInputA(HANDLE hConsoleInput,
873 LPINPUT_RECORD lpBuffer,
874 DWORD nLength, LPDWORD lpNumberOfEventsRead)
876 struct read_console_input_request req;
877 struct read_console_input_reply reply;
880 req.handle = hConsoleInput;
884 /* loop until we get at least one event */
887 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
888 if (CLIENT_WaitReply( &len, NULL, 2, &reply, sizeof(reply),
889 lpBuffer, nLength * sizeof(*lpBuffer) ))
891 len -= sizeof(reply);
892 assert( !(len % sizeof(INPUT_RECORD)) );
894 CONSOLE_get_input(hConsoleInput,TRUE);
895 /*WaitForSingleObject( hConsoleInput, INFINITE32 );*/
897 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = len / sizeof(INPUT_RECORD);
902 /***********************************************************************
903 * ReadConsoleInput32W (KERNEL32.570)
905 BOOL WINAPI ReadConsoleInputW( HANDLE handle, LPINPUT_RECORD buffer,
906 DWORD count, LPDWORD read )
908 /* FIXME: Fix this if we get UNICODE input. */
909 return ReadConsoleInputA( handle, buffer, count, read );
913 /***********************************************************************
914 * FlushConsoleInputBuffer (KERNEL32.132)
916 BOOL WINAPI FlushConsoleInputBuffer( HANDLE handle )
918 struct read_console_input_request req;
922 req.count = -1; /* get all records */
924 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
925 return !CLIENT_WaitReply( &len, NULL, 0 );
929 /***********************************************************************
930 * PeekConsoleInputA (KERNEL32.550)
932 * Gets 'count' first events (or less) from input queue.
934 * Does not need a complex console.
936 BOOL WINAPI PeekConsoleInputA( HANDLE handle, LPINPUT_RECORD buffer,
937 DWORD count, LPDWORD read )
939 struct read_console_input_request req;
940 struct read_console_input_reply reply;
943 CONSOLE_get_input(handle,FALSE);
948 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
949 if (CLIENT_WaitReply( &len, NULL, 2, &reply, sizeof(reply),
950 buffer, count * sizeof(*buffer) ))
952 len -= sizeof(reply);
953 assert( !(len % sizeof(INPUT_RECORD)) );
954 if (read) *read = len / sizeof(INPUT_RECORD);
959 /***********************************************************************
960 * PeekConsoleInputW (KERNEL32.551)
962 BOOL WINAPI PeekConsoleInputW(HANDLE hConsoleInput,
963 LPINPUT_RECORD pirBuffer,
967 /* FIXME: Hmm. Fix this if we get UNICODE input. */
968 return PeekConsoleInputA(hConsoleInput,pirBuffer,cInRecords,lpcRead);
972 /******************************************************************************
973 * WriteConsoleInput32A [KERNEL32.730] Write data to a console input buffer
976 BOOL WINAPI WriteConsoleInputA( HANDLE handle, INPUT_RECORD *buffer,
977 DWORD count, LPDWORD written )
979 struct write_console_input_request req;
980 struct write_console_input_reply reply;
984 CLIENT_SendRequest( REQ_WRITE_CONSOLE_INPUT, -1, 2, &req, sizeof(req),
985 buffer, count * sizeof(*buffer) );
986 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
987 if (written) *written = reply.written;
992 /***********************************************************************
993 * SetConsoleTitle32A (KERNEL32.476)
995 * Sets the console title.
997 * We do not necessarily need to create a complex console for that,
998 * but should remember the title and set it on creation of the latter.
999 * (not fixed at this time).
1001 BOOL WINAPI SetConsoleTitleA(LPCSTR title)
1003 struct set_console_info_request req;
1004 struct get_console_info_reply info;
1008 if ((hcon = CreateFileA( "CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
1009 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
1012 req.mask = SET_CONSOLE_INFO_TITLE;
1013 CLIENT_SendRequest( REQ_SET_CONSOLE_INFO, -1, 2, &req, sizeof(req),
1014 title, strlen(title)+1 );
1015 if (CLIENT_WaitReply( NULL, NULL, 0 )) goto error;
1016 if (CONSOLE_GetInfo( hcon, &info ) && info.pid)
1018 /* only set title for complex console (own xterm) */
1019 WriteFile( hcon, "\033]2;", 4, &written, NULL );
1020 WriteFile( hcon, title, strlen(title), &written, NULL );
1021 WriteFile( hcon, "\a", 1, &written, NULL );
1023 CloseHandle( hcon );
1026 CloseHandle( hcon );
1031 /******************************************************************************
1032 * SetConsoleTitle32W [KERNEL32.477] Sets title bar string for console
1035 * title [I] Address of new title
1038 * This should not be calling the A version
1044 BOOL WINAPI SetConsoleTitleW( LPCWSTR title )
1048 LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
1049 ret = SetConsoleTitleA(titleA);
1050 HeapFree( GetProcessHeap(), 0, titleA );
1054 /******************************************************************************
1055 * SetConsoleCursorPosition [KERNEL32.627]
1056 * Sets the cursor position in console
1059 * hConsoleOutput [I] Handle of console screen buffer
1060 * dwCursorPosition [I] New cursor position coordinates
1064 BOOL WINAPI SetConsoleCursorPosition( HANDLE hcon, COORD pos )
1069 /* make console complex only if we change lines, not just in the line */
1071 CONSOLE_make_complex(hcon);
1073 TRACE("%d (%dx%d)\n", hcon, pos.x , pos.y );
1074 /* x are columns, y rows */
1076 /* full screen cursor absolute positioning */
1077 sprintf(xbuf,"%c[%d;%dH", 0x1B, pos.y+1, pos.x+1);
1079 /* relative cursor positioning in line (\r to go to 0) */
1080 sprintf(xbuf,"\r%c[%dC", 0x1B, pos.x);
1081 /* FIXME: store internal if we start using own console buffers */
1082 WriteFile(hcon,xbuf,strlen(xbuf),&xlen,NULL);
1086 /***********************************************************************
1087 * GetNumberOfConsoleInputEvents (KERNEL32.246)
1089 BOOL WINAPI GetNumberOfConsoleInputEvents(HANDLE hcon,LPDWORD nrofevents)
1091 CONSOLE_get_input(hcon,FALSE);
1092 *nrofevents = 1; /* UMM */
1096 /***********************************************************************
1097 * GetNumberOfConsoleMouseButtons (KERNEL32.358)
1099 BOOL WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
1101 FIXME("(%p): stub\n", nrofbuttons);
1106 /******************************************************************************
1107 * GetConsoleCursorInfo32 [KERNEL32.296] Gets size and visibility of console
1110 * hcon [I] Handle to console screen buffer
1111 * cinfo [O] Address of cursor information
1117 BOOL WINAPI GetConsoleCursorInfo( HANDLE hcon,
1118 LPCONSOLE_CURSOR_INFO cinfo )
1120 struct get_console_info_reply reply;
1122 if (!CONSOLE_GetInfo( hcon, &reply )) return FALSE;
1125 cinfo->dwSize = reply.cursor_size;
1126 cinfo->bVisible = reply.cursor_visible;
1132 /******************************************************************************
1133 * SetConsoleCursorInfo32 [KERNEL32.626] Sets size and visibility of cursor
1139 BOOL WINAPI SetConsoleCursorInfo(
1140 HANDLE hcon, /* [in] Handle to console screen buffer */
1141 LPCONSOLE_CURSOR_INFO cinfo) /* [in] Address of cursor information */
1143 struct set_console_info_request req;
1148 CONSOLE_make_complex(hcon);
1149 sprintf(buf,"\033[?25%c",cinfo->bVisible?'h':'l');
1150 WriteFile(hcon,buf,strlen(buf),&xlen,NULL);
1152 req.cursor_size = cinfo->dwSize;
1153 req.cursor_visible = cinfo->bVisible;
1154 req.mask = SET_CONSOLE_INFO_CURSOR;
1155 CLIENT_SendRequest( REQ_SET_CONSOLE_INFO, -1, 1, &req, sizeof(req) );
1156 return !CLIENT_WaitReply( NULL, NULL, 0 );
1160 /******************************************************************************
1161 * SetConsoleWindowInfo [KERNEL32.634] Sets size and position of console
1167 BOOL WINAPI SetConsoleWindowInfo(
1168 HANDLE hcon, /* [in] Handle to console screen buffer */
1169 BOOL bAbsolute, /* [in] Coordinate type flag */
1170 LPSMALL_RECT window) /* [in] Address of new window rectangle */
1172 FIXME("(%x,%d,%p): stub\n", hcon, bAbsolute, window);
1177 /******************************************************************************
1178 * SetConsoleTextAttribute32 [KERNEL32.631] Sets colors for text
1180 * Sets the foreground and background color attributes of characters
1181 * written to the screen buffer.
1187 BOOL WINAPI SetConsoleTextAttribute(HANDLE hConsoleOutput,WORD wAttr)
1189 const int colormap[8] = {
1196 TRACE("(%d,%d)\n",hConsoleOutput,wAttr);
1197 sprintf(buffer,"%c[0;%s3%d;4%dm",
1199 (wAttr & FOREGROUND_INTENSITY)?"1;":"",
1201 colormap[(wAttr&0x70)>>4]
1203 WriteFile(hConsoleOutput,buffer,strlen(buffer),&xlen,NULL);
1208 /******************************************************************************
1209 * SetConsoleScreenBufferSize [KERNEL32.630] Changes size of console
1212 * hConsoleOutput [I] Handle to console screen buffer
1213 * dwSize [I] New size in character rows and cols
1219 BOOL WINAPI SetConsoleScreenBufferSize( HANDLE hConsoleOutput,
1222 FIXME("(%d,%dx%d): stub\n",hConsoleOutput,dwSize.x,dwSize.y);
1227 /******************************************************************************
1228 * FillConsoleOutputCharacterA [KERNEL32.242]
1231 * hConsoleOutput [I] Handle to screen buffer
1232 * cCharacter [I] Character to write
1233 * nLength [I] Number of cells to write to
1234 * dwCoord [I] Coords of first cell
1235 * lpNumCharsWritten [O] Pointer to number of cells written
1241 BOOL WINAPI FillConsoleOutputCharacterA(
1242 HANDLE hConsoleOutput,
1246 LPDWORD lpNumCharsWritten)
1251 SetConsoleCursorPosition(hConsoleOutput,dwCoord);
1252 for(count=0;count<nLength;count++)
1253 WriteFile(hConsoleOutput,&cCharacter,1,&xlen,NULL);
1254 *lpNumCharsWritten = nLength;
1259 /******************************************************************************
1260 * FillConsoleOutputCharacterW [KERNEL32.243] Writes characters to console
1263 * hConsoleOutput [I] Handle to screen buffer
1264 * cCharacter [I] Character to write
1265 * nLength [I] Number of cells to write to
1266 * dwCoord [I] Coords of first cell
1267 * lpNumCharsWritten [O] Pointer to number of cells written
1273 BOOL WINAPI FillConsoleOutputCharacterW(HANDLE hConsoleOutput,
1277 LPDWORD lpNumCharsWritten)
1282 SetConsoleCursorPosition(hConsoleOutput,dwCoord);
1283 /* FIXME: not quite correct ... but the lower part of UNICODE char comes
1286 for(count=0;count<nLength;count++)
1287 WriteFile(hConsoleOutput,&cCharacter,1,&xlen,NULL);
1288 *lpNumCharsWritten = nLength;
1293 /******************************************************************************
1294 * FillConsoleOutputAttribute [KERNEL32.241] Sets attributes for console
1297 * hConsoleOutput [I] Handle to screen buffer
1298 * wAttribute [I] Color attribute to write
1299 * nLength [I] Number of cells to write to
1300 * dwCoord [I] Coords of first cell
1301 * lpNumAttrsWritten [O] Pointer to number of cells written
1307 BOOL WINAPI FillConsoleOutputAttribute( HANDLE hConsoleOutput,
1308 WORD wAttribute, DWORD nLength, COORD dwCoord,
1309 LPDWORD lpNumAttrsWritten)
1311 FIXME("(%d,%d,%ld,%dx%d,%p): stub\n", hConsoleOutput,
1312 wAttribute,nLength,dwCoord.x,dwCoord.y,lpNumAttrsWritten);
1313 *lpNumAttrsWritten = nLength;
1317 /******************************************************************************
1318 * ReadConsoleOutputCharacter32A [KERNEL32.573]
1323 BOOL WINAPI ReadConsoleOutputCharacterA(HANDLE hConsoleOutput,
1324 LPSTR lpstr, DWORD dword, COORD coord, LPDWORD lpdword)
1326 FIXME("(%d,%p,%ld,%dx%d,%p): stub\n", hConsoleOutput,lpstr,
1327 dword,coord.x,coord.y,lpdword);
1328 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1333 /******************************************************************************
1334 * ScrollConsoleScreenBuffer [KERNEL32.612]
1339 BOOL WINAPI ScrollConsoleScreenBuffer( HANDLE hConsoleOutput,
1340 LPSMALL_RECT lpScrollRect, LPSMALL_RECT lpClipRect,
1341 COORD dwDestOrigin, LPCHAR_INFO lpFill)
1343 FIXME("(%d,%p,%p,%dx%d,%p): stub\n", hConsoleOutput,lpScrollRect,
1344 lpClipRect,dwDestOrigin.x,dwDestOrigin.y,lpFill);
1345 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);