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"
48 #include "server/request.h"
52 /* FIXME: Should be in an internal header file. OK, so which one?
53 Used by CONSOLE_makecomplex. */
54 FILE *wine_openpty(int *master
, int *slave
, char *name
,
55 struct termios
*term
, struct winsize
*winsize
);
57 /****************************************************************************
60 static BOOL
CONSOLE_GetInfo( HANDLE handle
, struct get_console_info_reply
*reply
)
62 struct get_console_info_request req
;
65 CLIENT_SendRequest( REQ_GET_CONSOLE_INFO
, -1, 1, &req
, sizeof(req
) );
66 return !CLIENT_WaitSimpleReply( reply
, sizeof(*reply
), NULL
);
69 /****************************************************************************
70 * XTERM_string_to_IR [internal]
72 * Transfers a string read from XTERM to INPUT_RECORDs and adds them to the
73 * queue. Does translation of vt100 style function keys and xterm-mouse clicks.
76 CONSOLE_string_to_IR( HANDLE hConsoleInput
,unsigned char *buf
,int len
) {
82 unsigned char inchar
= buf
[j
];
84 if (inchar
!=27) { /* no escape -> 'normal' keyboard event */
85 ir
.EventType
= 1; /* Key_event */
87 ir
.Event
.KeyEvent
.bKeyDown
= 1;
88 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
90 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
92 ir
.Event
.KeyEvent
.dwControlKeyState
|=LEFT_ALT_PRESSED
;
95 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VkKeyScan16(inchar
);
96 if (ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x0100)
97 ir
.Event
.KeyEvent
.dwControlKeyState
|=SHIFT_PRESSED
;
98 if (ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x0200)
99 ir
.Event
.KeyEvent
.dwControlKeyState
|=LEFT_CTRL_PRESSED
;
100 if (ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x0400)
101 ir
.Event
.KeyEvent
.dwControlKeyState
|=LEFT_ALT_PRESSED
;
102 ir
.Event
.KeyEvent
.wVirtualScanCode
= MapVirtualKey16(
103 ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x00ff,
104 0 /* VirtualKeyCodes to ScanCode */
106 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= inchar
;
108 if (inchar
==127) { /* backspace */
109 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= '\b'; /* FIXME: hmm */
110 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0x0e;
111 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VK_BACK
;
114 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= '\r';
115 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VK_RETURN
;
116 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0x1c;
119 /* FIXME: find good values for ^X */
120 ir
.Event
.KeyEvent
.wVirtualKeyCode
= 0xdead;
121 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0xbeef;
126 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
127 ir
.Event
.KeyEvent
.bKeyDown
= 0;
128 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
132 if ((j
==len
-1) || (buf
[j
+1]!='[')) {/* add ESCape on its own */
133 ir
.EventType
= 1; /* Key_event */
134 ir
.Event
.KeyEvent
.bKeyDown
= 1;
135 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
137 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VkKeyScan16(27);
138 ir
.Event
.KeyEvent
.wVirtualScanCode
= MapVirtualKey16(
139 ir
.Event
.KeyEvent
.wVirtualKeyCode
,0
141 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
142 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= 27;
143 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
144 ir
.Event
.KeyEvent
.bKeyDown
= 0;
145 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
148 for (k
=j
;k
<len
;k
++) {
149 if (((buf
[k
]>='A') && (buf
[k
]<='Z')) ||
150 ((buf
[k
]>='a') && (buf
[k
]<='z')) ||
156 int subid
,scancode
=0;
158 ir
.EventType
= 1; /* Key_event */
159 ir
.Event
.KeyEvent
.bKeyDown
= 1;
160 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
161 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
163 ir
.Event
.KeyEvent
.wVirtualKeyCode
= 0xad; /* FIXME */
164 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0xad; /* FIXME */
165 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= 0;
169 sscanf(&buf
[j
+2],"%d",&subid
);
171 case 2:/*INS */scancode
= 0xe052;break;
172 case 3:/*DEL */scancode
= 0xe053;break;
173 case 6:/*PGDW*/scancode
= 0xe051;break;
174 case 5:/*PGUP*/scancode
= 0xe049;break;
175 case 11:/*F1 */scancode
= 0x003b;break;
176 case 12:/*F2 */scancode
= 0x003c;break;
177 case 13:/*F3 */scancode
= 0x003d;break;
178 case 14:/*F4 */scancode
= 0x003e;break;
179 case 15:/*F5 */scancode
= 0x003f;break;
180 case 17:/*F6 */scancode
= 0x0040;break;
181 case 18:/*F7 */scancode
= 0x0041;break;
182 case 19:/*F8 */scancode
= 0x0042;break;
183 case 20:/*F9 */scancode
= 0x0043;break;
184 case 21:/*F10 */scancode
= 0x0044;break;
185 case 23:/*F11 */scancode
= 0x00d9;break;
186 case 24:/*F12 */scancode
= 0x00da;break;
187 /* FIXME: Shift-Fx */
189 FIXME(console
,"parse ESC[%d~\n",subid
);
193 case 'A': /* Cursor Up */scancode
= 0xe048;break;
194 case 'B': /* Cursor Down */scancode
= 0xe050;break;
195 case 'D': /* Cursor Left */scancode
= 0xe04b;break;
196 case 'C': /* Cursor Right */scancode
= 0xe04d;break;
197 case 'F': /* End */scancode
= 0xe04f;break;
198 case 'H': /* Home */scancode
= 0xe047;break;
200 /* Mouse Button Press (ESCM<button+'!'><x+'!'><y+'!'>) or
201 * Release (ESCM#<x+'!'><y+'!'>
204 ir
.EventType
= MOUSE_EVENT
;
205 ir
.Event
.MouseEvent
.dwMousePosition
.x
= buf
[k
+2]-'!';
206 ir
.Event
.MouseEvent
.dwMousePosition
.y
= buf
[k
+3]-'!';
208 ir
.Event
.MouseEvent
.dwButtonState
= 0;
210 ir
.Event
.MouseEvent
.dwButtonState
= 1<<(buf
[k
+1]-' ');
211 ir
.Event
.MouseEvent
.dwEventFlags
= 0; /* FIXME */
212 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
219 ir
.Event
.KeyEvent
.wVirtualScanCode
= scancode
;
220 ir
.Event
.KeyEvent
.wVirtualKeyCode
= MapVirtualKey16(scancode
,1);
221 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
222 ir
.Event
.KeyEvent
.bKeyDown
= 0;
223 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
231 /****************************************************************************
232 * CONSOLE_get_input (internal)
234 * Reads (nonblocking) as much input events as possible and stores them
235 * in an internal queue.
238 CONSOLE_get_input( HANDLE handle
, BOOL blockwait
)
240 char *buf
= HeapAlloc(GetProcessHeap(),0,1);
247 if (WaitForSingleObject( handle
, 0 )) break;
248 if (!ReadFile( handle
, &inchar
, 1, &res
, NULL
)) break;
249 if (!res
) /* res 0 but readable means EOF? Hmm. */
251 buf
= HeapReAlloc(GetProcessHeap(),0,buf
,len
+1);
254 CONSOLE_string_to_IR(handle
,buf
,len
);
255 HeapFree(GetProcessHeap(),0,buf
);
258 /******************************************************************************
259 * SetConsoleCtrlHandler [KERNEL32.459] Adds function to calling process list
262 * func [I] Address of handler function
263 * add [I] Handler to add or remove
270 * James Sutherland (JamesSutherland@gmx.de)
271 * Added global variables console_ignore_ctrl_c and handlers[]
272 * Does not yet do any error checking, or set LastError if failed.
273 * This doesn't yet matter, since these handlers are not yet called...!
275 static unsigned int console_ignore_ctrl_c
= 0;
276 static HANDLER_ROUTINE
*handlers
[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
277 BOOL WINAPI
SetConsoleCtrlHandler( HANDLER_ROUTINE
*func
, BOOL add
)
279 unsigned int alloc_loop
= sizeof(handlers
)/sizeof(HANDLER_ROUTINE
*);
280 unsigned int done
= 0;
281 FIXME(console
, "(%p,%i) - no error checking or testing yet\n", func
, add
);
284 console_ignore_ctrl_c
= add
;
290 if (!handlers
[alloc_loop
] && !done
)
292 handlers
[alloc_loop
] = func
;
296 FIXME(console
, "Out of space on CtrlHandler table\n");
302 if (handlers
[alloc_loop
] == func
&& !done
)
304 handlers
[alloc_loop
] = 0;
308 WARN(console
, "Attempt to remove non-installed CtrlHandler %p\n",
316 /******************************************************************************
317 * GenerateConsoleCtrlEvent [KERNEL32.275] Simulate a CTRL-C or CTRL-BREAK
320 * dwCtrlEvent [I] Type of event
321 * dwProcessGroupID [I] Process group ID to send event to
324 * Doesn't yet work...!
328 * Failure: False (and *should* [but doesn't] set LastError)
330 BOOL WINAPI
GenerateConsoleCtrlEvent( DWORD dwCtrlEvent
,
331 DWORD dwProcessGroupID
)
333 if (dwCtrlEvent
!= CTRL_C_EVENT
&& dwCtrlEvent
!= CTRL_BREAK_EVENT
)
335 ERR( console
, "invalid event %d for PGID %ld\n",
336 (unsigned short)dwCtrlEvent
, dwProcessGroupID
);
339 if (dwProcessGroupID
== GetCurrentProcessId() )
341 FIXME( console
, "Attempt to send event %d to self - stub\n",
342 (unsigned short)dwCtrlEvent
);
345 FIXME( console
,"event %d to external PGID %ld - not implemented yet\n",
346 (unsigned short)dwCtrlEvent
, dwProcessGroupID
);
351 /******************************************************************************
352 * CreateConsoleScreenBuffer [KERNEL32.151] Creates a console screen buffer
355 * dwDesiredAccess [I] Access flag
356 * dwShareMode [I] Buffer share mode
357 * sa [I] Security attributes
358 * dwFlags [I] Type of buffer to create
359 * lpScreenBufferData [I] Reserved
362 * Should call SetLastError
365 * Success: Handle to new console screen buffer
366 * Failure: INVALID_HANDLE_VALUE
368 HANDLE WINAPI
CreateConsoleScreenBuffer( DWORD dwDesiredAccess
,
369 DWORD dwShareMode
, LPSECURITY_ATTRIBUTES sa
,
370 DWORD dwFlags
, LPVOID lpScreenBufferData
)
372 FIXME(console
, "(%ld,%ld,%p,%ld,%p): stub\n",dwDesiredAccess
,
373 dwShareMode
, sa
, dwFlags
, lpScreenBufferData
);
374 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
375 return INVALID_HANDLE_VALUE
;
379 /***********************************************************************
380 * GetConsoleScreenBufferInfo (KERNEL32.190)
382 BOOL WINAPI
GetConsoleScreenBufferInfo( HANDLE hConsoleOutput
,
383 LPCONSOLE_SCREEN_BUFFER_INFO csbi
)
387 csbi
->dwCursorPosition
.x
= 0;
388 csbi
->dwCursorPosition
.y
= 0;
389 csbi
->wAttributes
= 0;
390 csbi
->srWindow
.Left
= 0;
391 csbi
->srWindow
.Right
= 79;
392 csbi
->srWindow
.Top
= 0;
393 csbi
->srWindow
.Bottom
= 23;
394 csbi
->dwMaximumWindowSize
.x
= 80;
395 csbi
->dwMaximumWindowSize
.y
= 24;
400 /******************************************************************************
401 * SetConsoleActiveScreenBuffer [KERNEL32.623] Sets buffer to current console
407 BOOL WINAPI
SetConsoleActiveScreenBuffer(
408 HANDLE hConsoleOutput
) /* [in] Handle to console screen buffer */
410 FIXME(console
, "(%x): stub\n", hConsoleOutput
);
415 /***********************************************************************
416 * GetLargestConsoleWindowSize (KERNEL32.226)
418 DWORD WINAPI
GetLargestConsoleWindowSize( HANDLE hConsoleOutput
)
420 return (DWORD
)MAKELONG(80,24);
423 /***********************************************************************
424 * FreeConsole (KERNEL32.267)
426 BOOL WINAPI
FreeConsole(VOID
)
428 CLIENT_SendRequest( REQ_FREE_CONSOLE
, -1, 0 );
429 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
433 /*************************************************************************
436 * Open a handle to the current process console.
438 HANDLE
CONSOLE_OpenHandle( BOOL output
, DWORD access
, LPSECURITY_ATTRIBUTES sa
)
440 struct open_console_request req
;
441 struct open_console_reply reply
;
445 req
.inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
446 CLIENT_SendRequest( REQ_OPEN_CONSOLE
, -1, 1, &req
, sizeof(req
) );
448 CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
);
453 /*************************************************************************
454 * CONSOLE_make_complex [internal]
456 * Turns a CONSOLE kernel object into a complex one.
457 * (switches from output/input using the terminal where WINE was started to
460 * This makes simple commandline tools pipeable, while complex commandline
461 * tools work without getting messed up by debugoutput.
463 * All other functions should work indedependend from this call.
465 * To test for complex console: pid == 0 -> simple, otherwise complex.
467 static BOOL
CONSOLE_make_complex(HANDLE handle
)
469 struct set_console_fd_request req
;
470 struct get_console_info_reply info
;
475 int i
,xpid
,master
,slave
;
478 if (!CONSOLE_GetInfo( handle
, &info
)) return FALSE
;
479 if (info
.pid
) return TRUE
; /* already complex */
481 MSG("Console: Making console complex (creating an xterm)...\n");
483 if (tcgetattr(0, &term
) < 0) {
484 /* ignore failure, or we can't run from a script */
486 term
.c_lflag
= ~(ECHO
|ICANON
);
488 if (wine_openpty(&master
, &slave
, NULL
, &term
, NULL
) < 0)
491 if ((xpid
=fork()) == 0) {
492 tcsetattr(slave
, TCSADRAIN
, &term
);
493 sprintf(buf
, "-Sxx%d", master
);
494 /* "-fn vga" for VGA font. Harmless if vga is not present:
495 * xterm: unable to open font "vga", trying "fixed"....
497 execlp("xterm", "xterm", buf
, "-fn","vga",NULL
);
498 ERR(console
, "error creating AllocConsole xterm\n");
504 CLIENT_SendRequest( REQ_SET_CONSOLE_FD
, dup(slave
), 1, &req
, sizeof(req
) );
505 CLIENT_WaitReply( NULL
, NULL
, 0 );
507 /* most xterms like to print their window ID when used with -S;
508 * read it and continue before the user has a chance...
510 for (i
=0; c
!='\n'; (status
=read(slave
, &c
, 1)), i
++) {
511 if (status
== -1 && c
== '\0') {
512 /* wait for xterm to be created */
516 ERR(console
, "can't read xterm WID\n");
521 /* enable mouseclicks */
522 sprintf(buf
,"%c[?1001s%c[?1000h",27,27);
523 WriteFile(handle
,buf
,strlen(buf
),&xlen
,NULL
);
525 if (GetConsoleTitleA( buf
, sizeof(buf
) ))
527 WriteFile(handle
,"\033]2;",4,&xlen
,NULL
);
528 WriteFile(handle
,buf
,strlen(buf
),&xlen
,NULL
);
529 WriteFile(handle
,"\a",1,&xlen
,NULL
);
536 /***********************************************************************
537 * AllocConsole (KERNEL32.103)
539 * creates an xterm with a pty to our program
541 BOOL WINAPI
AllocConsole(VOID
)
543 struct open_console_request req
;
544 struct open_console_reply reply
;
545 HANDLE hIn
, hOut
, hErr
;
547 CLIENT_SendRequest( REQ_ALLOC_CONSOLE
, -1, 0 );
548 if (CLIENT_WaitReply( NULL
, NULL
, 0 ) != ERROR_SUCCESS
) return FALSE
;
551 req
.access
= GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
;
553 CLIENT_SendRequest( REQ_OPEN_CONSOLE
, -1, 1, &req
, sizeof(req
) );
554 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
) != ERROR_SUCCESS
)
556 /* FIXME: free console */
562 CLIENT_SendRequest( REQ_OPEN_CONSOLE
, -1, 1, &req
, sizeof(req
) );
563 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
) != ERROR_SUCCESS
)
566 /* FIXME: free console */
571 if (!DuplicateHandle( GetCurrentProcess(), hOut
,
572 GetCurrentProcess(), &hErr
,
573 0, TRUE
, DUPLICATE_SAME_ACCESS
))
580 /* NT resets the STD_*_HANDLEs on console alloc */
581 SetStdHandle(STD_INPUT_HANDLE
, hIn
);
582 SetStdHandle(STD_OUTPUT_HANDLE
, hOut
);
583 SetStdHandle(STD_ERROR_HANDLE
, hErr
);
585 SetLastError(ERROR_SUCCESS
);
586 SetConsoleTitleA("Wine Console");
591 /******************************************************************************
592 * GetConsoleCP [KERNEL32.295] Returns the OEM code page for the console
597 UINT WINAPI
GetConsoleCP(VOID
)
603 /***********************************************************************
604 * GetConsoleOutputCP (KERNEL32.189)
606 UINT WINAPI
GetConsoleOutputCP(VOID
)
608 return GetConsoleCP();
611 /***********************************************************************
612 * GetConsoleMode (KERNEL32.188)
614 BOOL WINAPI
GetConsoleMode(HANDLE hcon
,LPDWORD mode
)
616 struct get_console_mode_request req
;
617 struct get_console_mode_reply reply
;
620 CLIENT_SendRequest( REQ_GET_CONSOLE_MODE
, -1, 1, &req
, sizeof(req
));
621 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
)) return FALSE
;
627 /******************************************************************************
628 * SetConsoleMode [KERNEL32.628] Sets input mode of console's input buffer
631 * hcon [I] Handle to console input or screen buffer
632 * mode [I] Input or output mode to set
638 BOOL WINAPI
SetConsoleMode( HANDLE hcon
, DWORD mode
)
640 struct set_console_mode_request req
;
644 CLIENT_SendRequest( REQ_SET_CONSOLE_MODE
, -1, 1, &req
, sizeof(req
));
645 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
649 /***********************************************************************
650 * GetConsoleTitleA (KERNEL32.191)
652 DWORD WINAPI
GetConsoleTitleA(LPSTR title
,DWORD size
)
654 struct get_console_info_request req
;
655 struct get_console_info_reply reply
;
660 if ((hcon
= CreateFileA( "CONOUT$", GENERIC_READ
, 0, NULL
,
661 OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
664 CLIENT_SendRequest( REQ_GET_CONSOLE_INFO
, -1, 1, &req
, sizeof(req
) );
665 if (!CLIENT_WaitReply( &len
, NULL
, 2, &reply
, sizeof(reply
), title
, size
))
667 if (len
> sizeof(reply
)+size
) title
[size
-1] = 0;
675 /******************************************************************************
676 * GetConsoleTitle32W [KERNEL32.192] Retrieves title string for console
679 * title [O] Address of buffer for title
680 * size [I] Size of buffer
683 * Success: Length of string copied
686 DWORD WINAPI
GetConsoleTitleW( LPWSTR title
, DWORD size
)
691 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, size
))) return 0;
692 ret
= GetConsoleTitleA( tmp
, size
);
693 lstrcpyAtoW( title
, tmp
);
694 HeapFree( GetProcessHeap(), 0, tmp
);
699 /***********************************************************************
700 * WriteConsoleA (KERNEL32.729)
702 BOOL WINAPI
WriteConsoleA( HANDLE hConsoleOutput
,
704 DWORD nNumberOfCharsToWrite
,
705 LPDWORD lpNumberOfCharsWritten
,
708 /* FIXME: should I check if this is a console handle? */
709 return WriteFile(hConsoleOutput
, lpBuffer
, nNumberOfCharsToWrite
,
710 lpNumberOfCharsWritten
, NULL
);
715 if (bufused==curbufsize-1) \
716 buffer = HeapReAlloc(GetProcessHeap(),0,buffer,(curbufsize+=100));\
718 #define SADD(s) { char *x=s;while (*x) {CADD(*x);x++;}}
720 /***********************************************************************
721 * WriteConsoleOutputA (KERNEL32.732)
723 BOOL WINAPI
WriteConsoleOutputA( HANDLE hConsoleOutput
,
724 LPCHAR_INFO lpBuffer
,
727 LPSMALL_RECT lpWriteRegion
)
729 int i
,j
,off
=0,lastattr
=-1;
730 char sbuf
[20],*buffer
=NULL
;
731 int bufused
=0,curbufsize
= 100;
733 const int colormap
[8] = {
737 CONSOLE_make_complex(hConsoleOutput
);
738 buffer
= HeapAlloc(GetProcessHeap(),0,100);;
741 TRACE(console
,"wr: top = %d, bottom=%d, left=%d,right=%d\n",
743 lpWriteRegion
->Bottom
,
748 for (i
=lpWriteRegion
->Top
;i
<=lpWriteRegion
->Bottom
;i
++) {
749 sprintf(sbuf
,"%c[%d;%dH",27,i
+1,lpWriteRegion
->Left
+1);
751 for (j
=lpWriteRegion
->Left
;j
<=lpWriteRegion
->Right
;j
++) {
752 if (lastattr
!=lpBuffer
[off
].Attributes
) {
753 lastattr
= lpBuffer
[off
].Attributes
;
754 sprintf(sbuf
,"%c[0;%s3%d;4%dm",
756 (lastattr
& FOREGROUND_INTENSITY
)?"1;":"",
757 colormap
[lastattr
&7],
758 colormap
[(lastattr
&0x70)>>4]
760 /* FIXME: BACKGROUND_INTENSITY */
763 CADD(lpBuffer
[off
].Char
.AsciiChar
);
767 sprintf(sbuf
,"%c[0m",27);SADD(sbuf
);
768 WriteFile(hConsoleOutput
,buffer
,bufused
,&res
,NULL
);
769 HeapFree(GetProcessHeap(),0,buffer
);
773 /***********************************************************************
774 * WriteConsoleW (KERNEL32.577)
776 BOOL WINAPI
WriteConsoleW( HANDLE hConsoleOutput
,
778 DWORD nNumberOfCharsToWrite
,
779 LPDWORD lpNumberOfCharsWritten
,
783 LPSTR xstring
=HeapAlloc( GetProcessHeap(), 0, nNumberOfCharsToWrite
);
785 lstrcpynWtoA( xstring
, lpBuffer
,nNumberOfCharsToWrite
);
787 /* FIXME: should I check if this is a console handle? */
788 ret
= WriteFile(hConsoleOutput
, xstring
, nNumberOfCharsToWrite
,
789 lpNumberOfCharsWritten
, NULL
);
790 HeapFree( GetProcessHeap(), 0, xstring
);
795 /***********************************************************************
796 * ReadConsoleA (KERNEL32.419)
798 BOOL WINAPI
ReadConsoleA( HANDLE hConsoleInput
,
800 DWORD nNumberOfCharsToRead
,
801 LPDWORD lpNumberOfCharsRead
,
805 LPSTR xbuf
= (LPSTR
)lpBuffer
;
806 struct read_console_input_request req
;
809 TRACE(console
,"(%d,%p,%ld,%p,%p)\n",
810 hConsoleInput
,lpBuffer
,nNumberOfCharsToRead
,
811 lpNumberOfCharsRead
,lpReserved
814 CONSOLE_get_input(hConsoleInput
,FALSE
);
816 req
.handle
= hConsoleInput
;
820 /* FIXME: should we read at least 1 char? The SDK does not say */
821 while (charsread
<nNumberOfCharsToRead
)
825 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT
, -1, 1, &req
, sizeof(req
) );
826 if (CLIENT_WaitReply( &len
, NULL
, 1, &ir
, sizeof(ir
) ))
828 assert( !(len
% sizeof(ir
)) );
830 if (!ir
.Event
.KeyEvent
.bKeyDown
)
832 if (ir
.EventType
!= KEY_EVENT
)
834 *xbuf
++ = ir
.Event
.KeyEvent
.uChar
.AsciiChar
;
837 if (lpNumberOfCharsRead
)
838 *lpNumberOfCharsRead
= charsread
;
842 /***********************************************************************
843 * ReadConsoleW (KERNEL32.427)
845 BOOL WINAPI
ReadConsoleW( HANDLE hConsoleInput
,
847 DWORD nNumberOfCharsToRead
,
848 LPDWORD lpNumberOfCharsRead
,
852 LPSTR buf
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead
);
857 nNumberOfCharsToRead
,
862 lstrcpynAtoW(lpBuffer
,buf
,nNumberOfCharsToRead
);
863 HeapFree( GetProcessHeap(), 0, buf
);
868 /******************************************************************************
869 * ReadConsoleInput32A [KERNEL32.569] Reads data from a console
872 * hConsoleInput [I] Handle to console input buffer
873 * lpBuffer [O] Address of buffer for read data
874 * nLength [I] Number of records to read
875 * lpNumberOfEventsRead [O] Address of number of records read
881 BOOL WINAPI
ReadConsoleInputA(HANDLE hConsoleInput
,
882 LPINPUT_RECORD lpBuffer
,
883 DWORD nLength
, LPDWORD lpNumberOfEventsRead
)
885 struct read_console_input_request req
;
888 req
.handle
= hConsoleInput
;
892 /* loop until we get at least one event */
895 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT
, -1, 1, &req
, sizeof(req
) );
896 if (CLIENT_WaitReply( &len
, NULL
, 1, lpBuffer
, nLength
* sizeof(*lpBuffer
) ))
898 assert( !(len
% sizeof(INPUT_RECORD
)) );
900 CONSOLE_get_input(hConsoleInput
,TRUE
);
901 /*WaitForSingleObject( hConsoleInput, INFINITE32 );*/
903 if (lpNumberOfEventsRead
) *lpNumberOfEventsRead
= len
/ sizeof(INPUT_RECORD
);
908 /***********************************************************************
909 * ReadConsoleInput32W (KERNEL32.570)
911 BOOL WINAPI
ReadConsoleInputW( HANDLE handle
, LPINPUT_RECORD buffer
,
912 DWORD count
, LPDWORD read
)
914 /* FIXME: Fix this if we get UNICODE input. */
915 return ReadConsoleInputA( handle
, buffer
, count
, read
);
919 /***********************************************************************
920 * FlushConsoleInputBuffer (KERNEL32.132)
922 BOOL WINAPI
FlushConsoleInputBuffer( HANDLE handle
)
924 struct read_console_input_request req
;
928 req
.count
= -1; /* get all records */
930 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT
, -1, 1, &req
, sizeof(req
) );
931 return !CLIENT_WaitReply( &len
, NULL
, 0 );
935 /***********************************************************************
936 * PeekConsoleInputA (KERNEL32.550)
938 * Gets 'count' first events (or less) from input queue.
940 * Does not need a complex console.
942 BOOL WINAPI
PeekConsoleInputA( HANDLE handle
, LPINPUT_RECORD buffer
,
943 DWORD count
, LPDWORD read
)
945 struct read_console_input_request req
;
948 CONSOLE_get_input(handle
,FALSE
);
953 CLIENT_SendRequest( REQ_READ_CONSOLE_INPUT
, -1, 1, &req
, sizeof(req
) );
954 if (CLIENT_WaitReply( &len
, NULL
, 1, buffer
, count
* sizeof(*buffer
) ))
956 assert( !(len
% sizeof(INPUT_RECORD
)) );
957 if (read
) *read
= len
/ sizeof(INPUT_RECORD
);
962 /***********************************************************************
963 * PeekConsoleInputW (KERNEL32.551)
965 BOOL WINAPI
PeekConsoleInputW(HANDLE hConsoleInput
,
966 LPINPUT_RECORD pirBuffer
,
970 /* FIXME: Hmm. Fix this if we get UNICODE input. */
971 return PeekConsoleInputA(hConsoleInput
,pirBuffer
,cInRecords
,lpcRead
);
975 /******************************************************************************
976 * WriteConsoleInput32A [KERNEL32.730] Write data to a console input buffer
979 BOOL WINAPI
WriteConsoleInputA( HANDLE handle
, INPUT_RECORD
*buffer
,
980 DWORD count
, LPDWORD written
)
982 struct write_console_input_request req
;
983 struct write_console_input_reply reply
;
987 CLIENT_SendRequest( REQ_WRITE_CONSOLE_INPUT
, -1, 2, &req
, sizeof(req
),
988 buffer
, count
* sizeof(*buffer
) );
989 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
)) return FALSE
;
990 if (written
) *written
= reply
.written
;
995 /***********************************************************************
996 * SetConsoleTitle32A (KERNEL32.476)
998 * Sets the console title.
1000 * We do not necessarily need to create a complex console for that,
1001 * but should remember the title and set it on creation of the latter.
1002 * (not fixed at this time).
1004 BOOL WINAPI
SetConsoleTitleA(LPCSTR title
)
1006 struct set_console_info_request req
;
1007 struct get_console_info_reply info
;
1011 if ((hcon
= CreateFileA( "CONOUT$", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1012 OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
1015 req
.mask
= SET_CONSOLE_INFO_TITLE
;
1016 CLIENT_SendRequest( REQ_SET_CONSOLE_INFO
, -1, 2, &req
, sizeof(req
),
1017 title
, strlen(title
)+1 );
1018 if (CLIENT_WaitReply( NULL
, NULL
, 0 )) goto error
;
1019 if (CONSOLE_GetInfo( hcon
, &info
) && info
.pid
)
1021 /* only set title for complex console (own xterm) */
1022 WriteFile( hcon
, "\033]2;", 4, &written
, NULL
);
1023 WriteFile( hcon
, title
, strlen(title
), &written
, NULL
);
1024 WriteFile( hcon
, "\a", 1, &written
, NULL
);
1026 CloseHandle( hcon
);
1029 CloseHandle( hcon
);
1034 /******************************************************************************
1035 * SetConsoleTitle32W [KERNEL32.477] Sets title bar string for console
1038 * title [I] Address of new title
1041 * This should not be calling the A version
1047 BOOL WINAPI
SetConsoleTitleW( LPCWSTR title
)
1051 LPSTR titleA
= HEAP_strdupWtoA( GetProcessHeap(), 0, title
);
1052 ret
= SetConsoleTitleA(titleA
);
1053 HeapFree( GetProcessHeap(), 0, titleA
);
1057 /******************************************************************************
1058 * SetConsoleCursorPosition [KERNEL32.627]
1059 * Sets the cursor position in console
1062 * hConsoleOutput [I] Handle of console screen buffer
1063 * dwCursorPosition [I] New cursor position coordinates
1067 BOOL WINAPI
SetConsoleCursorPosition( HANDLE hcon
, COORD pos
)
1072 /* make console complex only if we change lines, not just in the line */
1074 CONSOLE_make_complex(hcon
);
1076 TRACE(console
, "%d (%dx%d)\n", hcon
, pos
.x
, pos
.y
);
1077 /* x are columns, y rows */
1079 /* full screen cursor absolute positioning */
1080 sprintf(xbuf
,"%c[%d;%dH", 0x1B, pos
.y
+1, pos
.x
+1);
1082 /* relative cursor positioning in line (\r to go to 0) */
1083 sprintf(xbuf
,"\r%c[%dC", 0x1B, pos
.x
);
1084 /* FIXME: store internal if we start using own console buffers */
1085 WriteFile(hcon
,xbuf
,strlen(xbuf
),&xlen
,NULL
);
1089 /***********************************************************************
1090 * GetNumberOfConsoleInputEvents (KERNEL32.246)
1092 BOOL WINAPI
GetNumberOfConsoleInputEvents(HANDLE hcon
,LPDWORD nrofevents
)
1094 CONSOLE_get_input(hcon
,FALSE
);
1095 *nrofevents
= 1; /* UMM */
1099 /***********************************************************************
1100 * GetNumberOfConsoleMouseButtons (KERNEL32.358)
1102 BOOL WINAPI
GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons
)
1104 FIXME(console
,"(%p): stub\n", nrofbuttons
);
1109 /******************************************************************************
1110 * GetConsoleCursorInfo32 [KERNEL32.296] Gets size and visibility of console
1113 * hcon [I] Handle to console screen buffer
1114 * cinfo [O] Address of cursor information
1120 BOOL WINAPI
GetConsoleCursorInfo( HANDLE hcon
,
1121 LPCONSOLE_CURSOR_INFO cinfo
)
1123 struct get_console_info_reply reply
;
1125 if (!CONSOLE_GetInfo( hcon
, &reply
)) return FALSE
;
1128 cinfo
->dwSize
= reply
.cursor_size
;
1129 cinfo
->bVisible
= reply
.cursor_visible
;
1135 /******************************************************************************
1136 * SetConsoleCursorInfo32 [KERNEL32.626] Sets size and visibility of cursor
1142 BOOL WINAPI
SetConsoleCursorInfo(
1143 HANDLE hcon
, /* [in] Handle to console screen buffer */
1144 LPCONSOLE_CURSOR_INFO cinfo
) /* [in] Address of cursor information */
1146 struct set_console_info_request req
;
1151 CONSOLE_make_complex(hcon
);
1152 sprintf(buf
,"\033[?25%c",cinfo
->bVisible
?'h':'l');
1153 WriteFile(hcon
,buf
,strlen(buf
),&xlen
,NULL
);
1155 req
.cursor_size
= cinfo
->dwSize
;
1156 req
.cursor_visible
= cinfo
->bVisible
;
1157 req
.mask
= SET_CONSOLE_INFO_CURSOR
;
1158 CLIENT_SendRequest( REQ_SET_CONSOLE_INFO
, -1, 1, &req
, sizeof(req
) );
1159 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
1163 /******************************************************************************
1164 * SetConsoleWindowInfo [KERNEL32.634] Sets size and position of console
1170 BOOL WINAPI
SetConsoleWindowInfo(
1171 HANDLE hcon
, /* [in] Handle to console screen buffer */
1172 BOOL bAbsolute
, /* [in] Coordinate type flag */
1173 LPSMALL_RECT window
) /* [in] Address of new window rectangle */
1175 FIXME(console
, "(%x,%d,%p): stub\n", hcon
, bAbsolute
, window
);
1180 /******************************************************************************
1181 * SetConsoleTextAttribute32 [KERNEL32.631] Sets colors for text
1183 * Sets the foreground and background color attributes of characters
1184 * written to the screen buffer.
1190 BOOL WINAPI
SetConsoleTextAttribute(HANDLE hConsoleOutput
,WORD wAttr
)
1192 const int colormap
[8] = {
1199 TRACE(console
,"(%d,%d)\n",hConsoleOutput
,wAttr
);
1200 sprintf(buffer
,"%c[0;%s3%d;4%dm",
1202 (wAttr
& FOREGROUND_INTENSITY
)?"1;":"",
1204 colormap
[(wAttr
&0x70)>>4]
1206 WriteFile(hConsoleOutput
,buffer
,strlen(buffer
),&xlen
,NULL
);
1211 /******************************************************************************
1212 * SetConsoleScreenBufferSize [KERNEL32.630] Changes size of console
1215 * hConsoleOutput [I] Handle to console screen buffer
1216 * dwSize [I] New size in character rows and cols
1222 BOOL WINAPI
SetConsoleScreenBufferSize( HANDLE hConsoleOutput
,
1225 FIXME(console
, "(%d,%dx%d): stub\n",hConsoleOutput
,dwSize
.x
,dwSize
.y
);
1230 /******************************************************************************
1231 * FillConsoleOutputCharacterA [KERNEL32.242]
1234 * hConsoleOutput [I] Handle to screen buffer
1235 * cCharacter [I] Character to write
1236 * nLength [I] Number of cells to write to
1237 * dwCoord [I] Coords of first cell
1238 * lpNumCharsWritten [O] Pointer to number of cells written
1244 BOOL WINAPI
FillConsoleOutputCharacterA(
1245 HANDLE hConsoleOutput
,
1249 LPDWORD lpNumCharsWritten
)
1254 SetConsoleCursorPosition(hConsoleOutput
,dwCoord
);
1255 for(count
=0;count
<nLength
;count
++)
1256 WriteFile(hConsoleOutput
,&cCharacter
,1,&xlen
,NULL
);
1257 *lpNumCharsWritten
= nLength
;
1262 /******************************************************************************
1263 * FillConsoleOutputCharacterW [KERNEL32.243] Writes characters to console
1266 * hConsoleOutput [I] Handle to screen buffer
1267 * cCharacter [I] Character to write
1268 * nLength [I] Number of cells to write to
1269 * dwCoord [I] Coords of first cell
1270 * lpNumCharsWritten [O] Pointer to number of cells written
1276 BOOL WINAPI
FillConsoleOutputCharacterW(HANDLE hConsoleOutput
,
1280 LPDWORD lpNumCharsWritten
)
1285 SetConsoleCursorPosition(hConsoleOutput
,dwCoord
);
1286 /* FIXME: not quite correct ... but the lower part of UNICODE char comes
1289 for(count
=0;count
<nLength
;count
++)
1290 WriteFile(hConsoleOutput
,&cCharacter
,1,&xlen
,NULL
);
1291 *lpNumCharsWritten
= nLength
;
1296 /******************************************************************************
1297 * FillConsoleOutputAttribute [KERNEL32.241] Sets attributes for console
1300 * hConsoleOutput [I] Handle to screen buffer
1301 * wAttribute [I] Color attribute to write
1302 * nLength [I] Number of cells to write to
1303 * dwCoord [I] Coords of first cell
1304 * lpNumAttrsWritten [O] Pointer to number of cells written
1310 BOOL WINAPI
FillConsoleOutputAttribute( HANDLE hConsoleOutput
,
1311 WORD wAttribute
, DWORD nLength
, COORD dwCoord
,
1312 LPDWORD lpNumAttrsWritten
)
1314 FIXME(console
, "(%d,%d,%ld,%dx%d,%p): stub\n", hConsoleOutput
,
1315 wAttribute
,nLength
,dwCoord
.x
,dwCoord
.y
,lpNumAttrsWritten
);
1316 *lpNumAttrsWritten
= nLength
;
1320 /******************************************************************************
1321 * ReadConsoleOutputCharacter32A [KERNEL32.573]
1326 BOOL WINAPI
ReadConsoleOutputCharacterA(HANDLE hConsoleOutput
,
1327 LPSTR lpstr
, DWORD dword
, COORD coord
, LPDWORD lpdword
)
1329 FIXME(console
, "(%d,%p,%ld,%dx%d,%p): stub\n", hConsoleOutput
,lpstr
,
1330 dword
,coord
.x
,coord
.y
,lpdword
);
1331 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1336 /******************************************************************************
1337 * ScrollConsoleScreenBuffer [KERNEL32.612]
1342 BOOL WINAPI
ScrollConsoleScreenBuffer( HANDLE hConsoleOutput
,
1343 LPSMALL_RECT lpScrollRect
, LPSMALL_RECT lpClipRect
,
1344 COORD dwDestOrigin
, LPCHAR_INFO lpFill
)
1346 FIXME(console
, "(%d,%p,%p,%dx%d,%p): stub\n", hConsoleOutput
,lpScrollRect
,
1347 lpClipRect
,dwDestOrigin
.x
,dwDestOrigin
.y
,lpFill
);
1348 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);