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"
49 #include "server/request.h"
52 /* The CONSOLE kernel32 Object */
53 typedef struct _CONSOLE {
57 /* FIXME: Should be in an internal header file. OK, so which one?
58 Used by CONSOLE_makecomplex. */
59 FILE *wine_openpty(int *master, int *slave, char *name,
60 struct termios *term, struct winsize *winsize);
62 /****************************************************************************
65 static BOOL CONSOLE_GetInfo( HANDLE handle, struct get_console_info_reply *reply )
67 struct get_console_info_request req;
69 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), handle,
70 K32OBJ_CONSOLE, GENERIC_READ )) == -1)
72 CLIENT_SendRequest( REQ_GET_CONSOLE_INFO, -1, 1, &req, sizeof(req) );
73 return !CLIENT_WaitSimpleReply( reply, sizeof(*reply), NULL );
76 /****************************************************************************
77 * XTERM_string_to_IR [internal]
79 * Transfers a string read from XTERM to INPUT_RECORDs and adds them to the
80 * queue. Does translation of vt100 style function keys and xterm-mouse clicks.
83 CONSOLE_string_to_IR( HANDLE hConsoleInput,unsigned char *buf,int len) {
89 unsigned char inchar = buf[j];
91 if (inchar!=27) { /* no escape -> 'normal' keyboard event */
92 ir.EventType = 1; /* Key_event */
94 ir.Event.KeyEvent.bKeyDown = 1;
95 ir.Event.KeyEvent.wRepeatCount = 0;
97 ir.Event.KeyEvent.dwControlKeyState = 0;
99 ir.Event.KeyEvent.dwControlKeyState|=LEFT_ALT_PRESSED;
102 ir.Event.KeyEvent.wVirtualKeyCode = VkKeyScan16(inchar);
103 if (ir.Event.KeyEvent.wVirtualKeyCode & 0x0100)
104 ir.Event.KeyEvent.dwControlKeyState|=SHIFT_PRESSED;
105 if (ir.Event.KeyEvent.wVirtualKeyCode & 0x0200)
106 ir.Event.KeyEvent.dwControlKeyState|=LEFT_CTRL_PRESSED;
107 if (ir.Event.KeyEvent.wVirtualKeyCode & 0x0400)
108 ir.Event.KeyEvent.dwControlKeyState|=LEFT_ALT_PRESSED;
109 ir.Event.KeyEvent.wVirtualScanCode = MapVirtualKey16(
110 ir.Event.KeyEvent.wVirtualKeyCode & 0x00ff,
111 0 /* VirtualKeyCodes to ScanCode */
113 ir.Event.KeyEvent.uChar.AsciiChar = inchar;
115 if (inchar==127) { /* backspace */
116 ir.Event.KeyEvent.uChar.AsciiChar = '\b'; /* FIXME: hmm */
117 ir.Event.KeyEvent.wVirtualScanCode = 0x0e;
118 ir.Event.KeyEvent.wVirtualKeyCode = VK_BACK;
121 ir.Event.KeyEvent.uChar.AsciiChar = '\r';
122 ir.Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
123 ir.Event.KeyEvent.wVirtualScanCode = 0x1c;
126 /* FIXME: find good values for ^X */
127 ir.Event.KeyEvent.wVirtualKeyCode = 0xdead;
128 ir.Event.KeyEvent.wVirtualScanCode = 0xbeef;
133 assert(WriteConsoleInputA( hConsoleInput, &ir, 1, &junk ));
134 ir.Event.KeyEvent.bKeyDown = 0;
135 assert(WriteConsoleInputA( hConsoleInput, &ir, 1, &junk ));
139 if ((j==len-1) || (buf[j+1]!='[')) {/* add ESCape on its own */
140 ir.EventType = 1; /* Key_event */
141 ir.Event.KeyEvent.bKeyDown = 1;
142 ir.Event.KeyEvent.wRepeatCount = 0;
144 ir.Event.KeyEvent.wVirtualKeyCode = VkKeyScan16(27);
145 ir.Event.KeyEvent.wVirtualScanCode = MapVirtualKey16(
146 ir.Event.KeyEvent.wVirtualKeyCode,0
148 ir.Event.KeyEvent.dwControlKeyState = 0;
149 ir.Event.KeyEvent.uChar.AsciiChar = 27;
150 assert(WriteConsoleInputA( hConsoleInput, &ir, 1, &junk ));
151 ir.Event.KeyEvent.bKeyDown = 0;
152 assert(WriteConsoleInputA( hConsoleInput, &ir, 1, &junk ));
155 for (k=j;k<len;k++) {
156 if (((buf[k]>='A') && (buf[k]<='Z')) ||
157 ((buf[k]>='a') && (buf[k]<='z')) ||
163 int subid,scancode=0;
165 ir.EventType = 1; /* Key_event */
166 ir.Event.KeyEvent.bKeyDown = 1;
167 ir.Event.KeyEvent.wRepeatCount = 0;
168 ir.Event.KeyEvent.dwControlKeyState = 0;
170 ir.Event.KeyEvent.wVirtualKeyCode = 0xad; /* FIXME */
171 ir.Event.KeyEvent.wVirtualScanCode = 0xad; /* FIXME */
172 ir.Event.KeyEvent.uChar.AsciiChar = 0;
176 sscanf(&buf[j+2],"%d",&subid);
178 case 2:/*INS */scancode = 0xe052;break;
179 case 3:/*DEL */scancode = 0xe053;break;
180 case 6:/*PGDW*/scancode = 0xe051;break;
181 case 5:/*PGUP*/scancode = 0xe049;break;
182 case 11:/*F1 */scancode = 0x003b;break;
183 case 12:/*F2 */scancode = 0x003c;break;
184 case 13:/*F3 */scancode = 0x003d;break;
185 case 14:/*F4 */scancode = 0x003e;break;
186 case 15:/*F5 */scancode = 0x003f;break;
187 case 17:/*F6 */scancode = 0x0040;break;
188 case 18:/*F7 */scancode = 0x0041;break;
189 case 19:/*F8 */scancode = 0x0042;break;
190 case 20:/*F9 */scancode = 0x0043;break;
191 case 21:/*F10 */scancode = 0x0044;break;
192 case 23:/*F11 */scancode = 0x00d9;break;
193 case 24:/*F12 */scancode = 0x00da;break;
194 /* FIXME: Shift-Fx */
196 FIXME(console,"parse ESC[%d~\n",subid);
200 case 'A': /* Cursor Up */scancode = 0xe048;break;
201 case 'B': /* Cursor Down */scancode = 0xe050;break;
202 case 'D': /* Cursor Left */scancode = 0xe04b;break;
203 case 'C': /* Cursor Right */scancode = 0xe04d;break;
204 case 'F': /* End */scancode = 0xe04f;break;
205 case 'H': /* Home */scancode = 0xe047;break;
207 /* Mouse Button Press (ESCM<button+'!'><x+'!'><y+'!'>) or
208 * Release (ESCM#<x+'!'><y+'!'>
211 ir.EventType = MOUSE_EVENT;
212 ir.Event.MouseEvent.dwMousePosition.x = buf[k+2]-'!';
213 ir.Event.MouseEvent.dwMousePosition.y = buf[k+3]-'!';
215 ir.Event.MouseEvent.dwButtonState = 0;
217 ir.Event.MouseEvent.dwButtonState = 1<<(buf[k+1]-' ');
218 ir.Event.MouseEvent.dwEventFlags = 0; /* FIXME */
219 assert(WriteConsoleInputA( hConsoleInput, &ir, 1, &junk));
226 ir.Event.KeyEvent.wVirtualScanCode = scancode;
227 ir.Event.KeyEvent.wVirtualKeyCode = MapVirtualKey16(scancode,1);
228 assert(WriteConsoleInputA( hConsoleInput, &ir, 1, &junk ));
229 ir.Event.KeyEvent.bKeyDown = 0;
230 assert(WriteConsoleInputA( hConsoleInput, &ir, 1, &junk ));
238 /****************************************************************************
239 * CONSOLE_get_input (internal)
241 * Reads (nonblocking) as much input events as possible and stores them
242 * in an internal queue.
245 CONSOLE_get_input( HANDLE handle, BOOL blockwait )
247 char *buf = HeapAlloc(GetProcessHeap(),0,1);
254 if (WaitForSingleObject( handle, 0 )) break;
255 if (!ReadFile( handle, &inchar, 1, &res, NULL )) break;
256 if (!res) /* res 0 but readable means EOF? Hmm. */
258 buf = HeapReAlloc(GetProcessHeap(),0,buf,len+1);
261 CONSOLE_string_to_IR(handle,buf,len);
262 HeapFree(GetProcessHeap(),0,buf);
265 /******************************************************************************
266 * SetConsoleCtrlHandler [KERNEL32.459] Adds function to calling process list
269 * func [I] Address of handler function
270 * add [I] Handler to add or remove
277 * James Sutherland (JamesSutherland@gmx.de)
278 * Added global variables console_ignore_ctrl_c and handlers[]
279 * Does not yet do any error checking, or set LastError if failed.
280 * This doesn't yet matter, since these handlers are not yet called...!
282 static unsigned int console_ignore_ctrl_c = 0;
283 static HANDLER_ROUTINE *handlers[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
284 BOOL WINAPI SetConsoleCtrlHandler( HANDLER_ROUTINE *func, BOOL add )
286 unsigned int alloc_loop = sizeof(handlers)/sizeof(HANDLER_ROUTINE *);
287 unsigned int done = 0;
288 FIXME(console, "(%p,%i) - no error checking or testing yet\n", func, add);
291 console_ignore_ctrl_c = add;
297 if (!handlers[alloc_loop] && !done)
299 handlers[alloc_loop] = func;
303 FIXME(console, "Out of space on CtrlHandler table\n");
309 if (handlers[alloc_loop] == func && !done)
311 handlers[alloc_loop] = 0;
315 WARN(console, "Attempt to remove non-installed CtrlHandler %p\n",
323 /******************************************************************************
324 * GenerateConsoleCtrlEvent [KERNEL32.275] Simulate a CTRL-C or CTRL-BREAK
327 * dwCtrlEvent [I] Type of event
328 * dwProcessGroupID [I] Process group ID to send event to
331 * Doesn't yet work...!
335 * Failure: False (and *should* [but doesn't] set LastError)
337 BOOL WINAPI GenerateConsoleCtrlEvent( DWORD dwCtrlEvent,
338 DWORD dwProcessGroupID )
340 if (dwCtrlEvent != CTRL_C_EVENT && dwCtrlEvent != CTRL_BREAK_EVENT)
342 ERR( console, "invalid event %d for PGID %ld\n",
343 (unsigned short)dwCtrlEvent, dwProcessGroupID );
346 if (dwProcessGroupID == GetCurrentProcessId() )
348 FIXME( console, "Attempt to send event %d to self - stub\n",
349 (unsigned short)dwCtrlEvent );
352 FIXME( console,"event %d to external PGID %ld - not implemented yet\n",
353 (unsigned short)dwCtrlEvent, dwProcessGroupID );
358 /******************************************************************************
359 * CreateConsoleScreenBuffer [KERNEL32.151] Creates a console screen buffer
362 * dwDesiredAccess [I] Access flag
363 * dwShareMode [I] Buffer share mode
364 * sa [I] Security attributes
365 * dwFlags [I] Type of buffer to create
366 * lpScreenBufferData [I] Reserved
369 * Should call SetLastError
372 * Success: Handle to new console screen buffer
373 * Failure: INVALID_HANDLE_VALUE
375 HANDLE WINAPI CreateConsoleScreenBuffer( DWORD dwDesiredAccess,
376 DWORD dwShareMode, LPSECURITY_ATTRIBUTES sa,
377 DWORD dwFlags, LPVOID lpScreenBufferData )
379 FIXME(console, "(%ld,%ld,%p,%ld,%p): stub\n",dwDesiredAccess,
380 dwShareMode, sa, dwFlags, lpScreenBufferData);
381 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
382 return INVALID_HANDLE_VALUE;
386 /***********************************************************************
387 * GetConsoleScreenBufferInfo (KERNEL32.190)
389 BOOL WINAPI GetConsoleScreenBufferInfo( HANDLE hConsoleOutput,
390 LPCONSOLE_SCREEN_BUFFER_INFO csbi )
394 csbi->dwCursorPosition.x = 0;
395 csbi->dwCursorPosition.y = 0;
396 csbi->wAttributes = 0;
397 csbi->srWindow.Left = 0;
398 csbi->srWindow.Right = 79;
399 csbi->srWindow.Top = 0;
400 csbi->srWindow.Bottom = 23;
401 csbi->dwMaximumWindowSize.x = 80;
402 csbi->dwMaximumWindowSize.y = 24;
407 /******************************************************************************
408 * SetConsoleActiveScreenBuffer [KERNEL32.623] Sets buffer to current console
414 BOOL WINAPI SetConsoleActiveScreenBuffer(
415 HANDLE hConsoleOutput) /* [in] Handle to console screen buffer */
417 FIXME(console, "(%x): stub\n", hConsoleOutput);
422 /***********************************************************************
423 * GetLargestConsoleWindowSize (KERNEL32.226)
425 DWORD WINAPI GetLargestConsoleWindowSize( HANDLE hConsoleOutput )
427 return (DWORD)MAKELONG(80,24);
430 /***********************************************************************
431 * FreeConsole (KERNEL32.267)
433 BOOL WINAPI FreeConsole(VOID)
436 PDB *pdb = PROCESS_Current();
441 console = (CONSOLE *)pdb->console;
443 if (console == NULL) {
444 SetLastError(ERROR_INVALID_PARAMETER);
448 CLIENT_SendRequest( REQ_FREE_CONSOLE, -1, 0 );
449 if (CLIENT_WaitReply( NULL, NULL, 0 ) != ERROR_SUCCESS)
451 K32OBJ_DecCount(&console->header);
456 HANDLE_CloseAll( pdb, &console->header );
457 K32OBJ_DecCount( &console->header );
464 /*************************************************************************
467 * Open a handle to the current process console.
469 HANDLE CONSOLE_OpenHandle( BOOL output, DWORD access, LPSECURITY_ATTRIBUTES sa )
471 struct open_console_request req;
472 struct open_console_reply reply;
478 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
479 CLIENT_SendRequest( REQ_OPEN_CONSOLE, -1, 1, &req, sizeof(req) );
480 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
481 if (reply.handle == -1) return INVALID_HANDLE_VALUE;
484 if (!(console = (CONSOLE*)HeapAlloc( SystemHeap, 0, sizeof(*console))))
489 console->header.type = K32OBJ_CONSOLE;
490 console->header.refcount = 1;
491 handle = HANDLE_Alloc( PROCESS_Current(), &console->header, req.access,
492 req.inherit, reply.handle );
494 K32OBJ_DecCount(&console->header);
499 /*************************************************************************
500 * CONSOLE_make_complex [internal]
502 * Turns a CONSOLE kernel object into a complex one.
503 * (switches from output/input using the terminal where WINE was started to
506 * This makes simple commandline tools pipeable, while complex commandline
507 * tools work without getting messed up by debugoutput.
509 * All other functions should work indedependend from this call.
511 * To test for complex console: pid == 0 -> simple, otherwise complex.
513 static BOOL CONSOLE_make_complex(HANDLE handle)
515 struct set_console_fd_request req;
516 struct get_console_info_reply info;
521 int i,xpid,master,slave;
524 if (!CONSOLE_GetInfo( handle, &info )) return FALSE;
525 if (info.pid) return TRUE; /* already complex */
527 MSG("Console: Making console complex (creating an xterm)...\n");
529 if (tcgetattr(0, &term) < 0) {
530 /* ignore failure, or we can't run from a script */
532 term.c_lflag = ~(ECHO|ICANON);
534 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), handle,
535 K32OBJ_CONSOLE, 0 )) == -1)
538 if (wine_openpty(&master, &slave, NULL, &term, NULL) < 0)
541 if ((xpid=fork()) == 0) {
542 tcsetattr(slave, TCSADRAIN, &term);
543 sprintf(buf, "-Sxx%d", master);
544 /* "-fn vga" for VGA font. Harmless if vga is not present:
545 * xterm: unable to open font "vga", trying "fixed"....
547 execlp("xterm", "xterm", buf, "-fn","vga",NULL);
548 ERR(console, "error creating AllocConsole xterm\n");
553 CLIENT_SendRequest( REQ_SET_CONSOLE_FD, dup(slave), 1, &req, sizeof(req) );
554 CLIENT_WaitReply( NULL, NULL, 0 );
556 /* most xterms like to print their window ID when used with -S;
557 * read it and continue before the user has a chance...
559 for (i=0; c!='\n'; (status=read(slave, &c, 1)), i++) {
560 if (status == -1 && c == '\0') {
561 /* wait for xterm to be created */
565 ERR(console, "can't read xterm WID\n");
570 /* enable mouseclicks */
571 sprintf(buf,"%c[?1001s%c[?1000h",27,27);
572 WriteFile(handle,buf,strlen(buf),&xlen,NULL);
574 if (GetConsoleTitleA( buf, sizeof(buf) ))
576 WriteFile(handle,"\033]2;",4,&xlen,NULL);
577 WriteFile(handle,buf,strlen(buf),&xlen,NULL);
578 WriteFile(handle,"\a",1,&xlen,NULL);
585 /***********************************************************************
586 * AllocConsole (KERNEL32.103)
588 * creates an xterm with a pty to our program
590 BOOL WINAPI AllocConsole(VOID)
592 struct open_console_request req;
593 struct open_console_reply reply;
594 PDB *pdb = PROCESS_Current();
596 HANDLE hIn, hOut, hErr;
598 SYSTEM_LOCK(); /* FIXME: really only need to lock the process */
600 console = (CONSOLE *)pdb->console;
602 /* don't create a console if we already have one */
603 if (console != NULL) {
604 SetLastError(ERROR_ACCESS_DENIED);
609 if (!(console = (CONSOLE*)HeapAlloc( SystemHeap, 0, sizeof(*console))))
615 console->header.type = K32OBJ_CONSOLE;
616 console->header.refcount = 1;
618 CLIENT_SendRequest( REQ_ALLOC_CONSOLE, -1, 0 );
619 if (CLIENT_WaitReply( NULL, NULL, 0 ) != ERROR_SUCCESS)
621 K32OBJ_DecCount(&console->header);
627 req.access = GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE;
629 CLIENT_SendRequest( REQ_OPEN_CONSOLE, -1, 1, &req, sizeof(req) );
630 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ) != ERROR_SUCCESS)
632 K32OBJ_DecCount(&console->header);
636 if ((hIn = HANDLE_Alloc(pdb,&console->header, req.access,
637 FALSE, reply.handle)) == INVALID_HANDLE_VALUE)
639 K32OBJ_DecCount(&console->header);
645 CLIENT_SendRequest( REQ_OPEN_CONSOLE, -1, 1, &req, sizeof(req) );
646 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ) != ERROR_SUCCESS)
649 K32OBJ_DecCount(&console->header);
653 if ((hOut = HANDLE_Alloc(pdb,&console->header, req.access,
654 FALSE, reply.handle)) == INVALID_HANDLE_VALUE)
657 K32OBJ_DecCount(&console->header);
662 if (!DuplicateHandle( GetCurrentProcess(), hOut,
663 GetCurrentProcess(), &hErr,
664 0, TRUE, DUPLICATE_SAME_ACCESS ))
668 K32OBJ_DecCount(&console->header);
673 if (pdb->console) K32OBJ_DecCount( pdb->console );
674 pdb->console = (K32OBJ *)console;
675 K32OBJ_IncCount( pdb->console );
677 /* NT resets the STD_*_HANDLEs on console alloc */
678 SetStdHandle(STD_INPUT_HANDLE, hIn);
679 SetStdHandle(STD_OUTPUT_HANDLE, hOut);
680 SetStdHandle(STD_ERROR_HANDLE, hErr);
682 SetLastError(ERROR_SUCCESS);
684 SetConsoleTitleA("Wine Console");
689 /******************************************************************************
690 * GetConsoleCP [KERNEL32.295] Returns the OEM code page for the console
695 UINT WINAPI GetConsoleCP(VOID)
701 /***********************************************************************
702 * GetConsoleOutputCP (KERNEL32.189)
704 UINT WINAPI GetConsoleOutputCP(VOID)
706 return GetConsoleCP();
709 /***********************************************************************
710 * GetConsoleMode (KERNEL32.188)
712 BOOL WINAPI GetConsoleMode(HANDLE hcon,LPDWORD mode)
714 struct get_console_mode_request req;
715 struct get_console_mode_reply reply;
717 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hcon,
718 K32OBJ_CONSOLE, GENERIC_READ )) == -1)
720 CLIENT_SendRequest( REQ_GET_CONSOLE_MODE, -1, 1, &req, sizeof(req));
721 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
727 /******************************************************************************
728 * SetConsoleMode [KERNEL32.628] Sets input mode of console's input buffer
731 * hcon [I] Handle to console input or screen buffer
732 * mode [I] Input or output mode to set
738 BOOL WINAPI SetConsoleMode( HANDLE hcon, DWORD mode )
740 struct set_console_mode_request req;
742 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hcon,
743 K32OBJ_CONSOLE, GENERIC_READ )) == -1)
746 CLIENT_SendRequest( REQ_SET_CONSOLE_MODE, -1, 1, &req, sizeof(req));
747 return !CLIENT_WaitReply( NULL, NULL, 0 );
751 /***********************************************************************
752 * GetConsoleTitleA (KERNEL32.191)
754 DWORD WINAPI GetConsoleTitleA(LPSTR title,DWORD size)
756 struct get_console_info_request req;
757 struct get_console_info_reply reply;
762 if ((hcon = CreateFileA( "CONOUT$", GENERIC_READ, 0, NULL,
763 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
765 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hcon,
766 K32OBJ_CONSOLE, GENERIC_READ )) == -1)
771 CLIENT_SendRequest( REQ_GET_CONSOLE_INFO, -1, 1, &req, sizeof(req) );
772 if (!CLIENT_WaitReply( &len, NULL, 2, &reply, sizeof(reply), title, size ))
774 if (len > sizeof(reply)+size) title[size-1] = 0;
782 /******************************************************************************
783 * GetConsoleTitle32W [KERNEL32.192] Retrieves title string for console
786 * title [O] Address of buffer for title
787 * size [I] Size of buffer
790 * Success: Length of string copied
793 DWORD WINAPI GetConsoleTitleW( LPWSTR title, DWORD size )
798 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, size ))) return 0;
799 ret = GetConsoleTitleA( tmp, size );
800 lstrcpyAtoW( title, tmp );
801 HeapFree( GetProcessHeap(), 0, tmp );
806 /***********************************************************************
807 * WriteConsoleA (KERNEL32.729)
809 BOOL WINAPI WriteConsoleA( HANDLE hConsoleOutput,
811 DWORD nNumberOfCharsToWrite,
812 LPDWORD lpNumberOfCharsWritten,
815 /* FIXME: should I check if this is a console handle? */
816 return WriteFile(hConsoleOutput, lpBuffer, nNumberOfCharsToWrite,
817 lpNumberOfCharsWritten, NULL);
822 if (bufused==curbufsize-1) \
823 buffer = HeapReAlloc(GetProcessHeap(),0,buffer,(curbufsize+=100));\
825 #define SADD(s) { char *x=s;while (*x) {CADD(*x);x++;}}
827 /***********************************************************************
828 * WriteConsoleOutputA (KERNEL32.732)
830 BOOL WINAPI WriteConsoleOutputA( HANDLE hConsoleOutput,
831 LPCHAR_INFO lpBuffer,
834 LPSMALL_RECT lpWriteRegion)
836 int i,j,off=0,lastattr=-1;
837 char sbuf[20],*buffer=NULL;
838 int bufused=0,curbufsize = 100;
840 const int colormap[8] = {
844 CONSOLE_make_complex(hConsoleOutput);
845 buffer = HeapAlloc(GetProcessHeap(),0,100);;
848 TRACE(console,"wr: top = %d, bottom=%d, left=%d,right=%d\n",
850 lpWriteRegion->Bottom,
855 for (i=lpWriteRegion->Top;i<=lpWriteRegion->Bottom;i++) {
856 sprintf(sbuf,"%c[%d;%dH",27,i+1,lpWriteRegion->Left+1);
858 for (j=lpWriteRegion->Left;j<=lpWriteRegion->Right;j++) {
859 if (lastattr!=lpBuffer[off].Attributes) {
860 lastattr = lpBuffer[off].Attributes;
861 sprintf(sbuf,"%c[0;%s3%d;4%dm",
863 (lastattr & FOREGROUND_INTENSITY)?"1;":"",
864 colormap[lastattr&7],
865 colormap[(lastattr&0x70)>>4]
867 /* FIXME: BACKGROUND_INTENSITY */
870 CADD(lpBuffer[off].Char.AsciiChar);
874 sprintf(sbuf,"%c[0m",27);SADD(sbuf);
875 WriteFile(hConsoleOutput,buffer,bufused,&res,NULL);
876 HeapFree(GetProcessHeap(),0,buffer);
880 /***********************************************************************
881 * WriteConsoleW (KERNEL32.577)
883 BOOL WINAPI WriteConsoleW( HANDLE hConsoleOutput,
885 DWORD nNumberOfCharsToWrite,
886 LPDWORD lpNumberOfCharsWritten,
890 LPSTR xstring=HeapAlloc( GetProcessHeap(), 0, nNumberOfCharsToWrite );
892 lstrcpynWtoA( xstring, lpBuffer,nNumberOfCharsToWrite);
894 /* FIXME: should I check if this is a console handle? */
895 ret= WriteFile(hConsoleOutput, xstring, nNumberOfCharsToWrite,
896 lpNumberOfCharsWritten, NULL);
897 HeapFree( GetProcessHeap(), 0, xstring );
902 /***********************************************************************
903 * ReadConsoleA (KERNEL32.419)
905 BOOL WINAPI ReadConsoleA( HANDLE hConsoleInput,
907 DWORD nNumberOfCharsToRead,
908 LPDWORD lpNumberOfCharsRead,
912 LPSTR xbuf = (LPSTR)lpBuffer;
913 struct read_console_input_request req;
916 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hConsoleInput,
917 K32OBJ_CONSOLE, GENERIC_READ )) == -1)
919 TRACE(console,"(%d,%p,%ld,%p,%p)\n",
920 hConsoleInput,lpBuffer,nNumberOfCharsToRead,
921 lpNumberOfCharsRead,lpReserved
924 CONSOLE_get_input(hConsoleInput,FALSE);
929 /* FIXME: should we read at least 1 char? The SDK does not say */
930 while (charsread<nNumberOfCharsToRead)
934 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
935 if (CLIENT_WaitReply( &len, NULL, 1, &ir, sizeof(ir) ))
937 assert( !(len % sizeof(ir)) );
939 if (!ir.Event.KeyEvent.bKeyDown)
941 if (ir.EventType != KEY_EVENT)
943 *xbuf++ = ir.Event.KeyEvent.uChar.AsciiChar;
946 if (lpNumberOfCharsRead)
947 *lpNumberOfCharsRead = charsread;
951 /***********************************************************************
952 * ReadConsoleW (KERNEL32.427)
954 BOOL WINAPI ReadConsoleW( HANDLE hConsoleInput,
956 DWORD nNumberOfCharsToRead,
957 LPDWORD lpNumberOfCharsRead,
961 LPSTR buf = (LPSTR)HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead);
966 nNumberOfCharsToRead,
971 lstrcpynAtoW(lpBuffer,buf,nNumberOfCharsToRead);
972 HeapFree( GetProcessHeap(), 0, buf );
977 /******************************************************************************
978 * ReadConsoleInput32A [KERNEL32.569] Reads data from a console
981 * hConsoleInput [I] Handle to console input buffer
982 * lpBuffer [O] Address of buffer for read data
983 * nLength [I] Number of records to read
984 * lpNumberOfEventsRead [O] Address of number of records read
990 BOOL WINAPI ReadConsoleInputA(HANDLE hConsoleInput,
991 LPINPUT_RECORD lpBuffer,
992 DWORD nLength, LPDWORD lpNumberOfEventsRead)
994 struct read_console_input_request req;
997 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hConsoleInput,
998 K32OBJ_CONSOLE, GENERIC_READ )) == -1)
1000 req.count = nLength;
1003 /* loop until we get at least one event */
1006 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
1007 if (CLIENT_WaitReply( &len, NULL, 1, lpBuffer, nLength * sizeof(*lpBuffer) ))
1009 assert( !(len % sizeof(INPUT_RECORD)) );
1011 CONSOLE_get_input(hConsoleInput,TRUE);
1012 /*WaitForSingleObject( hConsoleInput, INFINITE32 );*/
1014 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = len / sizeof(INPUT_RECORD);
1019 /***********************************************************************
1020 * ReadConsoleInput32W (KERNEL32.570)
1022 BOOL WINAPI ReadConsoleInputW( HANDLE handle, LPINPUT_RECORD buffer,
1023 DWORD count, LPDWORD read )
1025 /* FIXME: Fix this if we get UNICODE input. */
1026 return ReadConsoleInputA( handle, buffer, count, read );
1030 /***********************************************************************
1031 * FlushConsoleInputBuffer (KERNEL32.132)
1033 BOOL WINAPI FlushConsoleInputBuffer( HANDLE handle )
1035 struct read_console_input_request req;
1038 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), handle,
1039 K32OBJ_CONSOLE, GENERIC_READ )) == -1)
1041 req.count = -1; /* get all records */
1043 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
1044 return !CLIENT_WaitReply( &len, NULL, 0 );
1048 /***********************************************************************
1049 * PeekConsoleInputA (KERNEL32.550)
1051 * Gets 'count' first events (or less) from input queue.
1053 * Does not need a complex console.
1055 BOOL WINAPI PeekConsoleInputA( HANDLE handle, LPINPUT_RECORD buffer,
1056 DWORD count, LPDWORD read )
1058 struct read_console_input_request req;
1061 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), handle,
1062 K32OBJ_CONSOLE, GENERIC_READ )) == -1)
1065 CONSOLE_get_input(handle,FALSE);
1069 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT, -1, 1, &req, sizeof(req) );
1070 if (CLIENT_WaitReply( &len, NULL, 1, buffer, count * sizeof(*buffer) ))
1072 assert( !(len % sizeof(INPUT_RECORD)) );
1073 if (read) *read = len / sizeof(INPUT_RECORD);
1078 /***********************************************************************
1079 * PeekConsoleInputW (KERNEL32.551)
1081 BOOL WINAPI PeekConsoleInputW(HANDLE hConsoleInput,
1082 LPINPUT_RECORD pirBuffer,
1086 /* FIXME: Hmm. Fix this if we get UNICODE input. */
1087 return PeekConsoleInputA(hConsoleInput,pirBuffer,cInRecords,lpcRead);
1091 /******************************************************************************
1092 * WriteConsoleInput32A [KERNEL32.730] Write data to a console input buffer
1095 BOOL WINAPI WriteConsoleInputA( HANDLE handle, INPUT_RECORD *buffer,
1096 DWORD count, LPDWORD written )
1098 struct write_console_input_request req;
1099 struct write_console_input_reply reply;
1101 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), handle,
1102 K32OBJ_CONSOLE, GENERIC_WRITE )) == -1)
1105 CLIENT_SendRequest( REQ_WRITE_CONSOLE_INPUT, -1, 2, &req, sizeof(req),
1106 buffer, count * sizeof(*buffer) );
1107 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
1108 if (written) *written = reply.written;
1113 /***********************************************************************
1114 * SetConsoleTitle32A (KERNEL32.476)
1116 * Sets the console title.
1118 * We do not necessarily need to create a complex console for that,
1119 * but should remember the title and set it on creation of the latter.
1120 * (not fixed at this time).
1122 BOOL WINAPI SetConsoleTitleA(LPCSTR title)
1125 PDB *pdb = PROCESS_Current();
1128 char titleformat[]="\033]2;%s\a"; /*this should work for xterms*/
1132 TRACE(console,"(%s)\n",title);
1134 console = (CONSOLE *)pdb->console;
1137 if(console->title) /* Free old title, if there is one */
1138 HeapFree( SystemHeap, 0, console->title );
1139 console->title = (LPSTR)HeapAlloc(SystemHeap, 0,strlen(title)+1);
1140 if(console->title) strcpy(console->title,title);
1141 titlestring = HeapAlloc(GetProcessHeap(), 0,strlen(title)+strlen(titleformat)+1);
1143 K32OBJ_DecCount(&console->header);
1147 sprintf(titlestring,titleformat,title);
1149 /* only set title for complex console (own xterm) */
1150 if (console->pid != -1) {
1151 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),titlestring,strlen(titlestring),&written,NULL);
1152 if (written == strlen(titlestring))
1157 HeapFree( GetProcessHeap(), 0, titlestring );
1158 K32OBJ_DecCount(&console->header);
1165 struct set_console_info_request req;
1166 struct get_console_info_reply info;
1170 if ((hcon = CreateFileA( "CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
1171 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
1173 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hcon,
1174 K32OBJ_CONSOLE, GENERIC_WRITE )) == -1)
1176 req.mask = SET_CONSOLE_INFO_TITLE;
1177 CLIENT_SendRequest( REQ_SET_CONSOLE_INFO, -1, 2, &req, sizeof(req),
1178 title, strlen(title)+1 );
1179 if (CLIENT_WaitReply( NULL, NULL, 0 )) goto error;
1180 if (CONSOLE_GetInfo( hcon, &info ) && info.pid)
1182 /* only set title for complex console (own xterm) */
1183 WriteFile( hcon, "\033]2;", 4, &written, NULL );
1184 WriteFile( hcon, title, strlen(title), &written, NULL );
1185 WriteFile( hcon, "\a", 1, &written, NULL );
1189 CloseHandle( hcon );
1194 /******************************************************************************
1195 * SetConsoleTitle32W [KERNEL32.477] Sets title bar string for console
1198 * title [I] Address of new title
1201 * This should not be calling the A version
1207 BOOL WINAPI SetConsoleTitleW( LPCWSTR title )
1211 LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
1212 ret = SetConsoleTitleA(titleA);
1213 HeapFree( GetProcessHeap(), 0, titleA );
1217 /******************************************************************************
1218 * SetConsoleCursorPosition [KERNEL32.627]
1219 * Sets the cursor position in console
1222 * hConsoleOutput [I] Handle of console screen buffer
1223 * dwCursorPosition [I] New cursor position coordinates
1227 BOOL WINAPI SetConsoleCursorPosition( HANDLE hcon, COORD pos )
1232 /* make console complex only if we change lines, not just in the line */
1234 CONSOLE_make_complex(hcon);
1236 TRACE(console, "%d (%dx%d)\n", hcon, pos.x , pos.y );
1237 /* x are columns, y rows */
1239 /* full screen cursor absolute positioning */
1240 sprintf(xbuf,"%c[%d;%dH", 0x1B, pos.y+1, pos.x+1);
1242 /* relative cursor positioning in line (\r to go to 0) */
1243 sprintf(xbuf,"\r%c[%dC", 0x1B, pos.x);
1244 /* FIXME: store internal if we start using own console buffers */
1245 WriteFile(hcon,xbuf,strlen(xbuf),&xlen,NULL);
1249 /***********************************************************************
1250 * GetNumberOfConsoleInputEvents (KERNEL32.246)
1252 BOOL WINAPI GetNumberOfConsoleInputEvents(HANDLE hcon,LPDWORD nrofevents)
1254 CONSOLE_get_input(hcon,FALSE);
1255 *nrofevents = 1; /* UMM */
1259 /***********************************************************************
1260 * GetNumberOfConsoleMouseButtons (KERNEL32.358)
1262 BOOL WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
1264 FIXME(console,"(%p): stub\n", nrofbuttons);
1269 /******************************************************************************
1270 * GetConsoleCursorInfo32 [KERNEL32.296] Gets size and visibility of console
1273 * hcon [I] Handle to console screen buffer
1274 * cinfo [O] Address of cursor information
1280 BOOL WINAPI GetConsoleCursorInfo( HANDLE hcon,
1281 LPCONSOLE_CURSOR_INFO cinfo )
1283 struct get_console_info_reply reply;
1285 if (!CONSOLE_GetInfo( hcon, &reply )) return FALSE;
1288 cinfo->dwSize = reply.cursor_size;
1289 cinfo->bVisible = reply.cursor_visible;
1295 /******************************************************************************
1296 * SetConsoleCursorInfo32 [KERNEL32.626] Sets size and visibility of cursor
1302 BOOL WINAPI SetConsoleCursorInfo(
1303 HANDLE hcon, /* [in] Handle to console screen buffer */
1304 LPCONSOLE_CURSOR_INFO cinfo) /* [in] Address of cursor information */
1306 struct set_console_info_request req;
1310 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), hcon,
1311 K32OBJ_CONSOLE, GENERIC_WRITE )) == -1)
1313 CONSOLE_make_complex(hcon);
1314 sprintf(buf,"\033[?25%c",cinfo->bVisible?'h':'l');
1315 WriteFile(hcon,buf,strlen(buf),&xlen,NULL);
1317 req.cursor_size = cinfo->dwSize;
1318 req.cursor_visible = cinfo->bVisible;
1319 req.mask = SET_CONSOLE_INFO_CURSOR;
1320 CLIENT_SendRequest( REQ_SET_CONSOLE_INFO, -1, 1, &req, sizeof(req) );
1321 return !CLIENT_WaitReply( NULL, NULL, 0 );
1325 /******************************************************************************
1326 * SetConsoleWindowInfo [KERNEL32.634] Sets size and position of console
1332 BOOL WINAPI SetConsoleWindowInfo(
1333 HANDLE hcon, /* [in] Handle to console screen buffer */
1334 BOOL bAbsolute, /* [in] Coordinate type flag */
1335 LPSMALL_RECT window) /* [in] Address of new window rectangle */
1337 FIXME(console, "(%x,%d,%p): stub\n", hcon, bAbsolute, window);
1342 /******************************************************************************
1343 * SetConsoleTextAttribute32 [KERNEL32.631] Sets colors for text
1345 * Sets the foreground and background color attributes of characters
1346 * written to the screen buffer.
1352 BOOL WINAPI SetConsoleTextAttribute(HANDLE hConsoleOutput,WORD wAttr)
1354 const int colormap[8] = {
1361 TRACE(console,"(%d,%d)\n",hConsoleOutput,wAttr);
1362 sprintf(buffer,"%c[0;%s3%d;4%dm",
1364 (wAttr & FOREGROUND_INTENSITY)?"1;":"",
1366 colormap[(wAttr&0x70)>>4]
1368 WriteFile(hConsoleOutput,buffer,strlen(buffer),&xlen,NULL);
1373 /******************************************************************************
1374 * SetConsoleScreenBufferSize [KERNEL32.630] Changes size of console
1377 * hConsoleOutput [I] Handle to console screen buffer
1378 * dwSize [I] New size in character rows and cols
1384 BOOL WINAPI SetConsoleScreenBufferSize( HANDLE hConsoleOutput,
1387 FIXME(console, "(%d,%dx%d): stub\n",hConsoleOutput,dwSize.x,dwSize.y);
1392 /******************************************************************************
1393 * FillConsoleOutputCharacterA [KERNEL32.242]
1396 * hConsoleOutput [I] Handle to screen buffer
1397 * cCharacter [I] Character to write
1398 * nLength [I] Number of cells to write to
1399 * dwCoord [I] Coords of first cell
1400 * lpNumCharsWritten [O] Pointer to number of cells written
1406 BOOL WINAPI FillConsoleOutputCharacterA(
1407 HANDLE hConsoleOutput,
1411 LPDWORD lpNumCharsWritten)
1416 SetConsoleCursorPosition(hConsoleOutput,dwCoord);
1417 for(count=0;count<nLength;count++)
1418 WriteFile(hConsoleOutput,&cCharacter,1,&xlen,NULL);
1419 *lpNumCharsWritten = nLength;
1424 /******************************************************************************
1425 * FillConsoleOutputCharacterW [KERNEL32.243] Writes characters to console
1428 * hConsoleOutput [I] Handle to screen buffer
1429 * cCharacter [I] Character to write
1430 * nLength [I] Number of cells to write to
1431 * dwCoord [I] Coords of first cell
1432 * lpNumCharsWritten [O] Pointer to number of cells written
1438 BOOL WINAPI FillConsoleOutputCharacterW(HANDLE hConsoleOutput,
1442 LPDWORD lpNumCharsWritten)
1447 SetConsoleCursorPosition(hConsoleOutput,dwCoord);
1448 /* FIXME: not quite correct ... but the lower part of UNICODE char comes
1451 for(count=0;count<nLength;count++)
1452 WriteFile(hConsoleOutput,&cCharacter,1,&xlen,NULL);
1453 *lpNumCharsWritten = nLength;
1458 /******************************************************************************
1459 * FillConsoleOutputAttribute [KERNEL32.241] Sets attributes for console
1462 * hConsoleOutput [I] Handle to screen buffer
1463 * wAttribute [I] Color attribute to write
1464 * nLength [I] Number of cells to write to
1465 * dwCoord [I] Coords of first cell
1466 * lpNumAttrsWritten [O] Pointer to number of cells written
1472 BOOL WINAPI FillConsoleOutputAttribute( HANDLE hConsoleOutput,
1473 WORD wAttribute, DWORD nLength, COORD dwCoord,
1474 LPDWORD lpNumAttrsWritten)
1476 FIXME(console, "(%d,%d,%ld,%dx%d,%p): stub\n", hConsoleOutput,
1477 wAttribute,nLength,dwCoord.x,dwCoord.y,lpNumAttrsWritten);
1478 *lpNumAttrsWritten = nLength;
1482 /******************************************************************************
1483 * ReadConsoleOutputCharacter32A [KERNEL32.573]
1488 BOOL WINAPI ReadConsoleOutputCharacterA(HANDLE hConsoleOutput,
1489 LPSTR lpstr, DWORD dword, COORD coord, LPDWORD lpdword)
1491 FIXME(console, "(%d,%p,%ld,%dx%d,%p): stub\n", hConsoleOutput,lpstr,
1492 dword,coord.x,coord.y,lpdword);
1493 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1498 /******************************************************************************
1499 * ScrollConsoleScreenBuffer [KERNEL32.612]
1504 BOOL WINAPI ScrollConsoleScreenBuffer( HANDLE hConsoleOutput,
1505 LPSMALL_RECT lpScrollRect, LPSMALL_RECT lpClipRect,
1506 COORD dwDestOrigin, LPCHAR_INFO lpFill)
1508 FIXME(console, "(%d,%p,%p,%dx%d,%p): stub\n", hConsoleOutput,lpScrollRect,
1509 lpClipRect,dwDestOrigin.x,dwDestOrigin.y,lpFill);
1510 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);