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.
29 #include <sys/ioctl.h>
30 #include <sys/types.h>
34 #ifdef HAVE_SYS_ERRNO_H
35 #include <sys/errno.h>
43 #include "wine/winuser16.h"
44 #include "wine/keyboard16.h"
52 #include "debugtools.h"
55 DEFAULT_DEBUG_CHANNEL(console
)
58 /* FIXME: Should be in an internal header file. OK, so which one?
59 Used by CONSOLE_makecomplex. */
60 int wine_openpty(int *master
, int *slave
, char *name
,
61 struct termios
*term
, struct winsize
*winsize
);
63 /****************************************************************************
66 static int CONSOLE_GetPid( HANDLE handle
)
68 struct get_console_info_request
*req
= get_req_buffer();
70 if (server_call( REQ_GET_CONSOLE_INFO
)) return 0;
74 /****************************************************************************
75 * XTERM_string_to_IR [internal]
77 * Transfers a string read from XTERM to INPUT_RECORDs and adds them to the
78 * queue. Does translation of vt100 style function keys and xterm-mouse clicks.
81 CONSOLE_string_to_IR( HANDLE hConsoleInput
,unsigned char *buf
,int len
) {
87 unsigned char inchar
= buf
[j
];
89 if (inchar
!=27) { /* no escape -> 'normal' keyboard event */
90 ir
.EventType
= 1; /* Key_event */
92 ir
.Event
.KeyEvent
.bKeyDown
= 1;
93 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
95 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
97 ir
.Event
.KeyEvent
.dwControlKeyState
|=LEFT_ALT_PRESSED
;
100 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VkKeyScan16(inchar
);
101 if (ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x0100)
102 ir
.Event
.KeyEvent
.dwControlKeyState
|=SHIFT_PRESSED
;
103 if (ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x0200)
104 ir
.Event
.KeyEvent
.dwControlKeyState
|=LEFT_CTRL_PRESSED
;
105 if (ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x0400)
106 ir
.Event
.KeyEvent
.dwControlKeyState
|=LEFT_ALT_PRESSED
;
107 ir
.Event
.KeyEvent
.wVirtualScanCode
= MapVirtualKey16(
108 ir
.Event
.KeyEvent
.wVirtualKeyCode
& 0x00ff,
109 0 /* VirtualKeyCodes to ScanCode */
111 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= inchar
;
113 if ((inchar
==127)||(inchar
=='\b')) { /* backspace */
114 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= '\b'; /* FIXME: hmm */
115 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0x0e;
116 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VK_BACK
;
118 if ((inchar
=='\n')||(inchar
=='\r')) {
119 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= '\r';
120 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VK_RETURN
;
121 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0x1c;
122 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
125 /* FIXME: find good values for ^X */
126 ir
.Event
.KeyEvent
.wVirtualKeyCode
= 0xdead;
127 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0xbeef;
132 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
133 ir
.Event
.KeyEvent
.bKeyDown
= 0;
134 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
138 if ((j
==len
-1) || (buf
[j
+1]!='[')) {/* add ESCape on its own */
139 ir
.EventType
= 1; /* Key_event */
140 ir
.Event
.KeyEvent
.bKeyDown
= 1;
141 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
143 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VkKeyScan16(27);
144 ir
.Event
.KeyEvent
.wVirtualScanCode
= MapVirtualKey16(
145 ir
.Event
.KeyEvent
.wVirtualKeyCode
,0
147 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
148 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= 27;
149 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
150 ir
.Event
.KeyEvent
.bKeyDown
= 0;
151 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
154 for (k
=j
;k
<len
;k
++) {
155 if (((buf
[k
]>='A') && (buf
[k
]<='Z')) ||
156 ((buf
[k
]>='a') && (buf
[k
]<='z')) ||
162 int subid
,scancode
=0;
164 ir
.EventType
= 1; /* Key_event */
165 ir
.Event
.KeyEvent
.bKeyDown
= 1;
166 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
167 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
169 ir
.Event
.KeyEvent
.wVirtualKeyCode
= 0xad; /* FIXME */
170 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0xad; /* FIXME */
171 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= 0;
175 sscanf(&buf
[j
+2],"%d",&subid
);
177 case 2:/*INS */scancode
= 0xe052;break;
178 case 3:/*DEL */scancode
= 0xe053;break;
179 case 6:/*PGDW*/scancode
= 0xe051;break;
180 case 5:/*PGUP*/scancode
= 0xe049;break;
181 case 11:/*F1 */scancode
= 0x003b;break;
182 case 12:/*F2 */scancode
= 0x003c;break;
183 case 13:/*F3 */scancode
= 0x003d;break;
184 case 14:/*F4 */scancode
= 0x003e;break;
185 case 15:/*F5 */scancode
= 0x003f;break;
186 case 17:/*F6 */scancode
= 0x0040;break;
187 case 18:/*F7 */scancode
= 0x0041;break;
188 case 19:/*F8 */scancode
= 0x0042;break;
189 case 20:/*F9 */scancode
= 0x0043;break;
190 case 21:/*F10 */scancode
= 0x0044;break;
191 case 23:/*F11 */scancode
= 0x00d9;break;
192 case 24:/*F12 */scancode
= 0x00da;break;
193 /* FIXME: Shift-Fx */
195 FIXME("parse ESC[%d~\n",subid
);
199 case 'A': /* Cursor Up */scancode
= 0xe048;break;
200 case 'B': /* Cursor Down */scancode
= 0xe050;break;
201 case 'D': /* Cursor Left */scancode
= 0xe04b;break;
202 case 'C': /* Cursor Right */scancode
= 0xe04d;break;
203 case 'F': /* End */scancode
= 0xe04f;break;
204 case 'H': /* Home */scancode
= 0xe047;break;
206 /* Mouse Button Press (ESCM<button+'!'><x+'!'><y+'!'>) or
207 * Release (ESCM#<x+'!'><y+'!'>
210 ir
.EventType
= MOUSE_EVENT
;
211 ir
.Event
.MouseEvent
.dwMousePosition
.X
= buf
[k
+2]-'!';
212 ir
.Event
.MouseEvent
.dwMousePosition
.Y
= buf
[k
+3]-'!';
214 ir
.Event
.MouseEvent
.dwButtonState
= 0;
216 ir
.Event
.MouseEvent
.dwButtonState
= 1<<(buf
[k
+1]-' ');
217 ir
.Event
.MouseEvent
.dwEventFlags
= 0; /* FIXME */
218 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
227 ir
.Event
.KeyEvent
.wVirtualScanCode
= scancode
;
228 ir
.Event
.KeyEvent
.wVirtualKeyCode
= MapVirtualKey16(scancode
,1);
229 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
230 ir
.Event
.KeyEvent
.bKeyDown
= 0;
231 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
239 /****************************************************************************
240 * CONSOLE_get_input (internal)
242 * Reads (nonblocking) as much input events as possible and stores them
243 * in an internal queue.
246 CONSOLE_get_input( HANDLE handle
, BOOL blockwait
)
248 char *buf
= HeapAlloc(GetProcessHeap(),0,1);
249 int len
= 0, escape_seen
= 0;
256 /* If we have at one time seen escape in this loop, we are
257 * within an Escape sequence, so wait for a bit more input for the
260 if (WaitForSingleObject( handle
, escape_seen
*10 )) break;
261 if (!ReadFile( handle
, &inchar
, 1, &res
, NULL
)) break;
262 if (!res
) /* res 0 but readable means EOF? Hmm. */
264 buf
= HeapReAlloc(GetProcessHeap(),0,buf
,len
+1);
268 /* If we spot an ESC, we flush all up to it
269 * since we can be sure that we have a complete
272 CONSOLE_string_to_IR(handle
,buf
,len
-1);
273 buf
= HeapReAlloc(GetProcessHeap(),0,buf
,1);
280 CONSOLE_string_to_IR(handle
,buf
,len
);
281 HeapFree(GetProcessHeap(),0,buf
);
284 /******************************************************************************
285 * SetConsoleCtrlHandler [KERNEL32.459] Adds function to calling process list
288 * func [I] Address of handler function
289 * add [I] Handler to add or remove
296 * James Sutherland (JamesSutherland@gmx.de)
297 * Added global variables console_ignore_ctrl_c and handlers[]
298 * Does not yet do any error checking, or set LastError if failed.
299 * This doesn't yet matter, since these handlers are not yet called...!
301 static unsigned int console_ignore_ctrl_c
= 0;
302 static HANDLER_ROUTINE
*handlers
[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
303 BOOL WINAPI
SetConsoleCtrlHandler( HANDLER_ROUTINE
*func
, BOOL add
)
305 unsigned int alloc_loop
= sizeof(handlers
)/sizeof(HANDLER_ROUTINE
*);
306 unsigned int done
= 0;
307 FIXME("(%p,%i) - no error checking or testing yet\n", func
, add
);
310 console_ignore_ctrl_c
= add
;
316 if (!handlers
[alloc_loop
] && !done
)
318 handlers
[alloc_loop
] = func
;
322 FIXME("Out of space on CtrlHandler table\n");
328 if (handlers
[alloc_loop
] == func
&& !done
)
330 handlers
[alloc_loop
] = 0;
334 WARN("Attempt to remove non-installed CtrlHandler %p\n",
342 /******************************************************************************
343 * GenerateConsoleCtrlEvent [KERNEL32.275] Simulate a CTRL-C or CTRL-BREAK
346 * dwCtrlEvent [I] Type of event
347 * dwProcessGroupID [I] Process group ID to send event to
350 * Doesn't yet work...!
354 * Failure: False (and *should* [but doesn't] set LastError)
356 BOOL WINAPI
GenerateConsoleCtrlEvent( DWORD dwCtrlEvent
,
357 DWORD dwProcessGroupID
)
359 if (dwCtrlEvent
!= CTRL_C_EVENT
&& dwCtrlEvent
!= CTRL_BREAK_EVENT
)
361 ERR("invalid event %d for PGID %ld\n",
362 (unsigned short)dwCtrlEvent
, dwProcessGroupID
);
365 if (dwProcessGroupID
== GetCurrentProcessId() )
367 FIXME("Attempt to send event %d to self - stub\n",
368 (unsigned short)dwCtrlEvent
);
371 FIXME("event %d to external PGID %ld - not implemented yet\n",
372 (unsigned short)dwCtrlEvent
, dwProcessGroupID
);
377 /******************************************************************************
378 * CreateConsoleScreenBuffer [KERNEL32.151] Creates a console screen buffer
381 * dwDesiredAccess [I] Access flag
382 * dwShareMode [I] Buffer share mode
383 * sa [I] Security attributes
384 * dwFlags [I] Type of buffer to create
385 * lpScreenBufferData [I] Reserved
388 * Should call SetLastError
391 * Success: Handle to new console screen buffer
392 * Failure: INVALID_HANDLE_VALUE
394 HANDLE WINAPI
CreateConsoleScreenBuffer( DWORD dwDesiredAccess
,
395 DWORD dwShareMode
, LPSECURITY_ATTRIBUTES sa
,
396 DWORD dwFlags
, LPVOID lpScreenBufferData
)
398 FIXME("(%ld,%ld,%p,%ld,%p): stub\n",dwDesiredAccess
,
399 dwShareMode
, sa
, dwFlags
, lpScreenBufferData
);
400 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
401 return INVALID_HANDLE_VALUE
;
405 /***********************************************************************
406 * GetConsoleScreenBufferInfo (KERNEL32.190)
408 BOOL WINAPI
GetConsoleScreenBufferInfo( HANDLE hConsoleOutput
,
409 LPCONSOLE_SCREEN_BUFFER_INFO csbi
)
413 csbi
->dwCursorPosition
.X
= 0;
414 csbi
->dwCursorPosition
.Y
= 0;
415 csbi
->wAttributes
= 0;
416 csbi
->srWindow
.Left
= 0;
417 csbi
->srWindow
.Right
= 79;
418 csbi
->srWindow
.Top
= 0;
419 csbi
->srWindow
.Bottom
= 23;
420 csbi
->dwMaximumWindowSize
.X
= 80;
421 csbi
->dwMaximumWindowSize
.Y
= 24;
426 /******************************************************************************
427 * SetConsoleActiveScreenBuffer [KERNEL32.623] Sets buffer to current console
433 BOOL WINAPI
SetConsoleActiveScreenBuffer(
434 HANDLE hConsoleOutput
) /* [in] Handle to console screen buffer */
436 FIXME("(%x): stub\n", hConsoleOutput
);
441 /***********************************************************************
442 * GetLargestConsoleWindowSize (KERNEL32.226)
444 COORD WINAPI
GetLargestConsoleWindowSize( HANDLE hConsoleOutput
)
452 /* gcc doesn't return structures the same way as dwords */
453 DWORD WINAPI
WIN32_GetLargestConsoleWindowSize( HANDLE hConsoleOutput
)
455 COORD c
= GetLargestConsoleWindowSize( hConsoleOutput
);
459 /***********************************************************************
460 * FreeConsole (KERNEL32.267)
462 BOOL WINAPI
FreeConsole(VOID
)
464 return !server_call( REQ_FREE_CONSOLE
);
468 /*************************************************************************
471 * Open a handle to the current process console.
473 HANDLE
CONSOLE_OpenHandle( BOOL output
, DWORD access
, LPSECURITY_ATTRIBUTES sa
)
476 struct open_console_request
*req
= get_req_buffer();
478 req
->output
= output
;
479 req
->access
= access
;
480 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
482 if (!server_call( REQ_OPEN_CONSOLE
)) ret
= req
->handle
;
487 /*************************************************************************
488 * CONSOLE_make_complex [internal]
490 * Turns a CONSOLE kernel object into a complex one.
491 * (switches from output/input using the terminal where WINE was started to
494 * This makes simple commandline tools pipeable, while complex commandline
495 * tools work without getting messed up by debugoutput.
497 * All other functions should work indedependend from this call.
499 * To test for complex console: pid == 0 -> simple, otherwise complex.
501 static BOOL
CONSOLE_make_complex(HANDLE handle
)
503 struct set_console_fd_request
*req
= get_req_buffer();
507 int i
,xpid
,master
,slave
,pty_handle
;
509 if (CONSOLE_GetPid( handle
)) return TRUE
; /* already complex */
511 MESSAGE("Console: Making console complex (creating an xterm)...\n");
513 if (tcgetattr(0, &term
) < 0) {
514 /* ignore failure, or we can't run from a script */
516 term
.c_lflag
= ~(ECHO
|ICANON
);
518 if (wine_openpty(&master
, &slave
, NULL
, &term
, NULL
) < 0)
521 if ((xpid
=fork()) == 0) {
522 tcsetattr(slave
, TCSADRAIN
, &term
);
524 sprintf(buf
, "-Sxx%d", master
);
525 /* "-fn vga" for VGA font. Harmless if vga is not present:
526 * xterm: unable to open font "vga", trying "fixed"....
528 execlp("xterm", "xterm", buf
, "-fn","vga",NULL
);
529 ERR("error creating AllocConsole xterm\n");
532 pty_handle
= FILE_DupUnixHandle( slave
, GENERIC_READ
| GENERIC_WRITE
);
535 if (pty_handle
== -1) return FALSE
;
537 /* most xterms like to print their window ID when used with -S;
538 * read it and continue before the user has a chance...
540 for (i
= 0; i
< 10000; i
++)
542 BOOL ok
= ReadFile( pty_handle
, &c
, 1, NULL
, NULL
);
543 if (!ok
&& !c
) usleep(100); /* wait for xterm to be created */
544 else if (c
== '\n') break;
548 ERR("can't read xterm WID\n");
549 CloseHandle( pty_handle
);
552 req
->handle
= handle
;
553 req
->file_handle
= pty_handle
;
555 server_call( REQ_SET_CONSOLE_FD
);
556 CloseHandle( pty_handle
);
558 /* enable mouseclicks */
559 strcpy( buf
, "\033[?1001s\033[?1000h" );
560 WriteFile(handle
,buf
,strlen(buf
),NULL
,NULL
);
562 strcpy( buf
, "\033]2;" );
563 if (GetConsoleTitleA( buf
+ 4, sizeof(buf
) - 5 ))
566 WriteFile(handle
,buf
,strlen(buf
),NULL
,NULL
);
573 /***********************************************************************
574 * AllocConsole (KERNEL32.103)
576 * creates an xterm with a pty to our program
578 BOOL WINAPI
AllocConsole(VOID
)
580 struct alloc_console_request
*req
= get_req_buffer();
582 int handle_in
, handle_out
;
585 req
->access
= GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
;
586 req
->inherit
= FALSE
;
587 if (server_call( REQ_ALLOC_CONSOLE
)) return FALSE
;
588 handle_in
= req
->handle_in
;
589 handle_out
= req
->handle_out
;
591 if (!DuplicateHandle( GetCurrentProcess(), req
->handle_out
, GetCurrentProcess(), &hStderr
,
592 0, TRUE
, DUPLICATE_SAME_ACCESS
))
594 CloseHandle( handle_in
);
595 CloseHandle( handle_out
);
600 /* NT resets the STD_*_HANDLEs on console alloc */
601 SetStdHandle( STD_INPUT_HANDLE
, handle_in
);
602 SetStdHandle( STD_OUTPUT_HANDLE
, handle_out
);
603 SetStdHandle( STD_ERROR_HANDLE
, hStderr
);
605 SetLastError(ERROR_SUCCESS
);
606 SetConsoleTitleA("Wine Console");
611 /******************************************************************************
612 * GetConsoleCP [KERNEL32.295] Returns the OEM code page for the console
617 UINT WINAPI
GetConsoleCP(VOID
)
623 /***********************************************************************
624 * GetConsoleOutputCP (KERNEL32.189)
626 UINT WINAPI
GetConsoleOutputCP(VOID
)
628 return GetConsoleCP();
631 /***********************************************************************
632 * GetConsoleMode (KERNEL32.188)
634 BOOL WINAPI
GetConsoleMode(HANDLE hcon
,LPDWORD mode
)
637 struct get_console_mode_request
*req
= get_req_buffer();
639 if (!server_call( REQ_GET_CONSOLE_MODE
))
641 if (mode
) *mode
= req
->mode
;
648 /******************************************************************************
649 * SetConsoleMode [KERNEL32.628] Sets input mode of console's input buffer
652 * hcon [I] Handle to console input or screen buffer
653 * mode [I] Input or output mode to set
659 BOOL WINAPI
SetConsoleMode( HANDLE hcon
, DWORD mode
)
661 struct set_console_mode_request
*req
= get_req_buffer();
664 return !server_call( REQ_SET_CONSOLE_MODE
);
668 /***********************************************************************
669 * GetConsoleTitleA (KERNEL32.191)
671 DWORD WINAPI
GetConsoleTitleA(LPSTR title
,DWORD size
)
673 struct get_console_info_request
*req
= get_req_buffer();
677 if ((hcon
= CreateFileA( "CONOUT$", GENERIC_READ
, 0, NULL
,
678 OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
681 if (!server_call( REQ_GET_CONSOLE_INFO
))
683 lstrcpynA( title
, req
->title
, size
);
684 ret
= strlen(req
->title
);
691 /******************************************************************************
692 * GetConsoleTitleW [KERNEL32.192] Retrieves title string for console
695 * title [O] Address of buffer for title
696 * size [I] Size of buffer
699 * Success: Length of string copied
702 DWORD WINAPI
GetConsoleTitleW( LPWSTR title
, DWORD size
)
707 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, size
))) return 0;
708 ret
= GetConsoleTitleA( tmp
, size
);
709 lstrcpyAtoW( title
, tmp
);
710 HeapFree( GetProcessHeap(), 0, tmp
);
715 /***********************************************************************
716 * WriteConsoleA (KERNEL32.729)
718 BOOL WINAPI
WriteConsoleA( HANDLE hConsoleOutput
,
720 DWORD nNumberOfCharsToWrite
,
721 LPDWORD lpNumberOfCharsWritten
,
724 /* FIXME: should I check if this is a console handle? */
725 return WriteFile(hConsoleOutput
, lpBuffer
, nNumberOfCharsToWrite
,
726 lpNumberOfCharsWritten
, NULL
);
731 if (bufused==curbufsize-1) \
732 buffer = HeapReAlloc(GetProcessHeap(),0,buffer,(curbufsize+=100));\
734 #define SADD(s) { char *x=s;while (*x) {CADD(*x);x++;}}
736 /***********************************************************************
737 * WriteConsoleOutputA (KERNEL32.732)
739 BOOL WINAPI
WriteConsoleOutputA( HANDLE hConsoleOutput
,
740 LPCHAR_INFO lpBuffer
,
743 LPSMALL_RECT lpWriteRegion
)
745 int i
,j
,off
=0,lastattr
=-1;
747 char sbuf
[20],*buffer
=NULL
;
748 int bufused
=0,curbufsize
= 100;
750 CONSOLE_SCREEN_BUFFER_INFO csbi
;
751 const int colormap
[8] = {
755 CONSOLE_make_complex(hConsoleOutput
);
756 buffer
= HeapAlloc(GetProcessHeap(),0,curbufsize
);
757 offbase
= (dwBufferCoord
.Y
- 1) * dwBufferSize
.X
+
758 (dwBufferCoord
.X
- lpWriteRegion
->Left
);
760 TRACE("orig rect top = %d, bottom=%d, left=%d, right=%d\n",
762 lpWriteRegion
->Bottom
,
767 GetConsoleScreenBufferInfo(hConsoleOutput
, &csbi
);
768 sprintf(sbuf
,"%c7",27);SADD(sbuf
);
770 /* Step 1. Make (Bottom,Right) offset of intersection with
772 lpWriteRegion
->Bottom
= min(lpWriteRegion
->Bottom
, csbi
.dwSize
.Y
-1) -
774 lpWriteRegion
->Right
= min(lpWriteRegion
->Right
, csbi
.dwSize
.X
-1) -
777 /* Step 2. If either offset is negative, then no action
778 should be performed. (Implies that requested rectangle is
779 outside the current screen buffer rectangle.) */
780 if ((lpWriteRegion
->Bottom
< 0) ||
781 (lpWriteRegion
->Right
< 0)) {
782 /* readjust (Bottom Right) for rectangle */
783 lpWriteRegion
->Bottom
+= lpWriteRegion
->Top
;
784 lpWriteRegion
->Right
+= lpWriteRegion
->Left
;
786 TRACE("invisible rect top = %d, bottom=%d, left=%d, right=%d\n",
788 lpWriteRegion
->Bottom
,
793 HeapFree(GetProcessHeap(),0,buffer
);
797 /* Step 3. Intersect with source rectangle */
798 lpWriteRegion
->Bottom
= lpWriteRegion
->Top
- dwBufferCoord
.Y
+
799 min(lpWriteRegion
->Bottom
+ dwBufferCoord
.Y
, dwBufferSize
.Y
-1);
800 lpWriteRegion
->Right
= lpWriteRegion
->Left
- dwBufferCoord
.X
+
801 min(lpWriteRegion
->Right
+ dwBufferCoord
.X
, dwBufferSize
.X
-1);
803 TRACE("clipped rect top = %d, bottom=%d, left=%d,right=%d\n",
805 lpWriteRegion
->Bottom
,
810 /* Validate above computations made sense, if not then issue
811 error and fudge to single character rectangle */
812 if ((lpWriteRegion
->Bottom
< lpWriteRegion
->Top
) ||
813 (lpWriteRegion
->Right
< lpWriteRegion
->Left
)) {
814 ERR("Invalid clipped rectangle top = %d, bottom=%d, left=%d,right=%d\n",
816 lpWriteRegion
->Bottom
,
820 lpWriteRegion
->Bottom
= lpWriteRegion
->Top
;
821 lpWriteRegion
->Right
= lpWriteRegion
->Left
;
824 /* Now do the real processing and move the characters */
825 for (i
=lpWriteRegion
->Top
;i
<=lpWriteRegion
->Bottom
;i
++) {
826 offbase
+= dwBufferSize
.X
;
827 sprintf(sbuf
,"%c[%d;%dH",27,i
+1,lpWriteRegion
->Left
+1);
829 for (j
=lpWriteRegion
->Left
;j
<=lpWriteRegion
->Right
;j
++) {
831 if (lastattr
!=lpBuffer
[off
].Attributes
) {
832 lastattr
= lpBuffer
[off
].Attributes
;
833 sprintf(sbuf
,"%c[0;%s3%d;4%dm",
835 (lastattr
& FOREGROUND_INTENSITY
)?"1;":"",
836 colormap
[lastattr
&7],
837 colormap
[(lastattr
&0x70)>>4]
839 /* FIXME: BACKGROUND_INTENSITY */
842 CADD(lpBuffer
[off
].Char
.AsciiChar
);
845 sprintf(sbuf
,"%c[0m%c8",27,27);SADD(sbuf
);
846 WriteFile(hConsoleOutput
,buffer
,bufused
,&res
,NULL
);
847 HeapFree(GetProcessHeap(),0,buffer
);
851 /***********************************************************************
852 * WriteConsoleW (KERNEL32.577)
854 BOOL WINAPI
WriteConsoleW( HANDLE hConsoleOutput
,
856 DWORD nNumberOfCharsToWrite
,
857 LPDWORD lpNumberOfCharsWritten
,
864 n
= WideCharToMultiByte(CP_ACP
,0,lpBuffer
,nNumberOfCharsToWrite
,NULL
,0,NULL
,NULL
);
865 xstring
=HeapAlloc( GetProcessHeap(), 0, n
);
867 n
= WideCharToMultiByte(CP_ACP
,0,lpBuffer
,nNumberOfCharsToWrite
,xstring
,n
,NULL
,NULL
);
869 /* FIXME: should I check if this is a console handle? */
870 ret
= WriteFile(hConsoleOutput
, xstring
, n
,
871 lpNumberOfCharsWritten
, NULL
);
872 /* FIXME: lpNumberOfCharsWritten should be converted to numofchars in UNICODE */
873 HeapFree( GetProcessHeap(), 0, xstring
);
878 /***********************************************************************
879 * ReadConsoleA (KERNEL32.419)
881 BOOL WINAPI
ReadConsoleA( HANDLE hConsoleInput
,
883 DWORD nNumberOfCharsToRead
,
884 LPDWORD lpNumberOfCharsRead
,
888 LPSTR xbuf
= (LPSTR
)lpBuffer
;
891 TRACE("(%d,%p,%ld,%p,%p)\n",
892 hConsoleInput
,lpBuffer
,nNumberOfCharsToRead
,
893 lpNumberOfCharsRead
,lpReserved
896 CONSOLE_get_input(hConsoleInput
,FALSE
);
898 /* FIXME: should we read at least 1 char? The SDK does not say */
899 while (charsread
<nNumberOfCharsToRead
)
901 struct read_console_input_request
*req
= get_req_buffer();
902 req
->handle
= hConsoleInput
;
905 if (server_call( REQ_READ_CONSOLE_INPUT
)) return FALSE
;
906 if (!req
->read
) break;
907 ir
= (LPINPUT_RECORD
)(req
+1);
908 if (!ir
->Event
.KeyEvent
.bKeyDown
)
910 if (ir
->EventType
!= KEY_EVENT
)
912 *xbuf
++ = ir
->Event
.KeyEvent
.uChar
.AsciiChar
;
915 if (lpNumberOfCharsRead
)
916 *lpNumberOfCharsRead
= charsread
;
920 /***********************************************************************
921 * ReadConsoleW (KERNEL32.427)
923 BOOL WINAPI
ReadConsoleW( HANDLE hConsoleInput
,
925 DWORD nNumberOfCharsToRead
,
926 LPDWORD lpNumberOfCharsRead
,
930 LPSTR buf
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead
);
935 nNumberOfCharsToRead
,
940 lstrcpynAtoW(lpBuffer
,buf
,nNumberOfCharsToRead
);
941 HeapFree( GetProcessHeap(), 0, buf
);
946 /******************************************************************************
947 * ReadConsoleInputA [KERNEL32.569] Reads data from a console
950 * hConsoleInput [I] Handle to console input buffer
951 * lpBuffer [O] Address of buffer for read data
952 * nLength [I] Number of records to read
953 * lpNumberOfEventsRead [O] Address of number of records read
959 BOOL WINAPI
ReadConsoleInputA(HANDLE hConsoleInput
, LPINPUT_RECORD lpBuffer
,
960 DWORD nLength
, LPDWORD lpNumberOfEventsRead
)
962 struct read_console_input_request
*req
= get_req_buffer();
964 /* loop until we get at least one event */
967 req
->handle
= hConsoleInput
;
968 req
->count
= nLength
;
970 if (server_call( REQ_READ_CONSOLE_INPUT
)) return FALSE
;
973 memcpy( lpBuffer
, req
+ 1, req
->read
* sizeof(*lpBuffer
) );
974 if (lpNumberOfEventsRead
) *lpNumberOfEventsRead
= req
->read
;
977 CONSOLE_get_input(hConsoleInput
,TRUE
);
978 /*WaitForSingleObject( hConsoleInput, INFINITE32 );*/
983 /***********************************************************************
984 * ReadConsoleInputW (KERNEL32.570)
986 BOOL WINAPI
ReadConsoleInputW( HANDLE handle
, LPINPUT_RECORD buffer
,
987 DWORD count
, LPDWORD read
)
989 /* FIXME: Fix this if we get UNICODE input. */
990 return ReadConsoleInputA( handle
, buffer
, count
, read
);
994 /***********************************************************************
995 * FlushConsoleInputBuffer (KERNEL32.132)
997 BOOL WINAPI
FlushConsoleInputBuffer( HANDLE handle
)
999 struct read_console_input_request
*req
= get_req_buffer();
1000 req
->handle
= handle
;
1001 req
->count
= -1; /* get all records */
1003 return !server_call( REQ_READ_CONSOLE_INPUT
);
1007 /***********************************************************************
1008 * PeekConsoleInputA (KERNEL32.550)
1010 * Gets 'count' first events (or less) from input queue.
1012 * Does not need a complex console.
1014 BOOL WINAPI
PeekConsoleInputA( HANDLE handle
, LPINPUT_RECORD buffer
,
1015 DWORD count
, LPDWORD read
)
1017 struct read_console_input_request
*req
= get_req_buffer();
1019 CONSOLE_get_input(handle
,FALSE
);
1021 req
->handle
= handle
;
1024 if (server_call( REQ_READ_CONSOLE_INPUT
)) return FALSE
;
1025 if (req
->read
) memcpy( buffer
, req
+ 1, req
->read
* sizeof(*buffer
) );
1026 if (read
) *read
= req
->read
;
1031 /***********************************************************************
1032 * PeekConsoleInputW (KERNEL32.551)
1034 BOOL WINAPI
PeekConsoleInputW(HANDLE hConsoleInput
,
1035 LPINPUT_RECORD pirBuffer
,
1039 /* FIXME: Hmm. Fix this if we get UNICODE input. */
1040 return PeekConsoleInputA(hConsoleInput
,pirBuffer
,cInRecords
,lpcRead
);
1044 /******************************************************************************
1045 * WriteConsoleInputA [KERNEL32.730] Write data to a console input buffer
1048 BOOL WINAPI
WriteConsoleInputA( HANDLE handle
, INPUT_RECORD
*buffer
,
1049 DWORD count
, LPDWORD written
)
1051 struct write_console_input_request
*req
= get_req_buffer();
1052 const DWORD max
= server_remaining( req
+ 1 ) / sizeof(INPUT_RECORD
);
1054 if (written
) *written
= 0;
1057 DWORD len
= count
< max
? count
: max
;
1059 req
->handle
= handle
;
1060 memcpy( req
+ 1, buffer
, len
* sizeof(*buffer
) );
1061 if (server_call( REQ_WRITE_CONSOLE_INPUT
)) return FALSE
;
1062 if (written
) *written
+= req
->written
;
1070 /***********************************************************************
1071 * SetConsoleTitleA (KERNEL32.476)
1073 * Sets the console title.
1075 * We do not necessarily need to create a complex console for that,
1076 * but should remember the title and set it on creation of the latter.
1077 * (not fixed at this time).
1079 BOOL WINAPI
SetConsoleTitleA(LPCSTR title
)
1081 struct set_console_info_request
*req
= get_req_buffer();
1085 if ((hcon
= CreateFileA( "CONOUT$", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1086 OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
1089 req
->mask
= SET_CONSOLE_INFO_TITLE
;
1090 lstrcpynA( req
->title
, title
, server_remaining(req
->title
) );
1091 if (server_call( REQ_SET_CONSOLE_INFO
)) goto error
;
1092 if (CONSOLE_GetPid( hcon
))
1094 /* only set title for complex console (own xterm) */
1095 WriteFile( hcon
, "\033]2;", 4, &written
, NULL
);
1096 WriteFile( hcon
, title
, strlen(title
), &written
, NULL
);
1097 WriteFile( hcon
, "\a", 1, &written
, NULL
);
1099 CloseHandle( hcon
);
1102 CloseHandle( hcon
);
1107 /******************************************************************************
1108 * SetConsoleTitleW [KERNEL32.477] Sets title bar string for console
1111 * title [I] Address of new title
1114 * This should not be calling the A version
1120 BOOL WINAPI
SetConsoleTitleW( LPCWSTR title
)
1124 LPSTR titleA
= HEAP_strdupWtoA( GetProcessHeap(), 0, title
);
1125 ret
= SetConsoleTitleA(titleA
);
1126 HeapFree( GetProcessHeap(), 0, titleA
);
1130 /******************************************************************************
1131 * SetConsoleCursorPosition [KERNEL32.627]
1132 * Sets the cursor position in console
1135 * hConsoleOutput [I] Handle of console screen buffer
1136 * dwCursorPosition [I] New cursor position coordinates
1140 BOOL WINAPI
SetConsoleCursorPosition( HANDLE hcon
, COORD pos
)
1145 /* make console complex only if we change lines, not just in the line */
1147 CONSOLE_make_complex(hcon
);
1149 TRACE("%d (%dx%d)\n", hcon
, pos
.X
, pos
.Y
);
1150 /* x are columns, y rows */
1152 /* full screen cursor absolute positioning */
1153 sprintf(xbuf
,"%c[%d;%dH", 0x1B, pos
.Y
+1, pos
.X
+1);
1155 /* relative cursor positioning in line (\r to go to 0) */
1156 sprintf(xbuf
,"\r%c[%dC", 0x1B, pos
.X
);
1157 /* FIXME: store internal if we start using own console buffers */
1158 WriteFile(hcon
,xbuf
,strlen(xbuf
),&xlen
,NULL
);
1162 /***********************************************************************
1163 * GetNumberOfConsoleInputEvents (KERNEL32.246)
1165 BOOL WINAPI
GetNumberOfConsoleInputEvents(HANDLE hcon
,LPDWORD nrofevents
)
1167 struct read_console_input_request
*req
= get_req_buffer();
1169 CONSOLE_get_input(hcon
,FALSE
);
1174 if (server_call( REQ_READ_CONSOLE_INPUT
)) return FALSE
;
1175 if (nrofevents
) *nrofevents
= req
->read
;
1179 /***********************************************************************
1180 * GetNumberOfConsoleMouseButtons (KERNEL32.358)
1182 BOOL WINAPI
GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons
)
1184 FIXME("(%p): stub\n", nrofbuttons
);
1189 /******************************************************************************
1190 * GetConsoleCursorInfo [KERNEL32.296] Gets size and visibility of console
1193 * hcon [I] Handle to console screen buffer
1194 * cinfo [O] Address of cursor information
1200 BOOL WINAPI
GetConsoleCursorInfo( HANDLE hcon
, LPCONSOLE_CURSOR_INFO cinfo
)
1202 struct get_console_info_request
*req
= get_req_buffer();
1204 if (server_call( REQ_GET_CONSOLE_INFO
)) return FALSE
;
1207 cinfo
->dwSize
= req
->cursor_size
;
1208 cinfo
->bVisible
= req
->cursor_visible
;
1214 /******************************************************************************
1215 * SetConsoleCursorInfo [KERNEL32.626] Sets size and visibility of cursor
1221 BOOL WINAPI
SetConsoleCursorInfo(
1222 HANDLE hcon
, /* [in] Handle to console screen buffer */
1223 LPCONSOLE_CURSOR_INFO cinfo
) /* [in] Address of cursor information */
1225 struct set_console_info_request
*req
= get_req_buffer();
1229 CONSOLE_make_complex(hcon
);
1230 sprintf(buf
,"\033[?25%c",cinfo
->bVisible
?'h':'l');
1231 WriteFile(hcon
,buf
,strlen(buf
),&xlen
,NULL
);
1234 req
->cursor_size
= cinfo
->dwSize
;
1235 req
->cursor_visible
= cinfo
->bVisible
;
1236 req
->mask
= SET_CONSOLE_INFO_CURSOR
;
1237 return !server_call( REQ_SET_CONSOLE_INFO
);
1241 /******************************************************************************
1242 * SetConsoleWindowInfo [KERNEL32.634] Sets size and position of console
1248 BOOL WINAPI
SetConsoleWindowInfo(
1249 HANDLE hcon
, /* [in] Handle to console screen buffer */
1250 BOOL bAbsolute
, /* [in] Coordinate type flag */
1251 LPSMALL_RECT window
) /* [in] Address of new window rectangle */
1253 FIXME("(%x,%d,%p): stub\n", hcon
, bAbsolute
, window
);
1258 /******************************************************************************
1259 * SetConsoleTextAttribute [KERNEL32.631] Sets colors for text
1261 * Sets the foreground and background color attributes of characters
1262 * written to the screen buffer.
1268 BOOL WINAPI
SetConsoleTextAttribute(HANDLE hConsoleOutput
,WORD wAttr
)
1270 const int colormap
[8] = {
1277 TRACE("(%d,%d)\n",hConsoleOutput
,wAttr
);
1278 sprintf(buffer
,"%c[0;%s3%d;4%dm",
1280 (wAttr
& FOREGROUND_INTENSITY
)?"1;":"",
1282 colormap
[(wAttr
&0x70)>>4]
1284 WriteFile(hConsoleOutput
,buffer
,strlen(buffer
),&xlen
,NULL
);
1289 /******************************************************************************
1290 * SetConsoleScreenBufferSize [KERNEL32.630] Changes size of console
1293 * hConsoleOutput [I] Handle to console screen buffer
1294 * dwSize [I] New size in character rows and cols
1300 BOOL WINAPI
SetConsoleScreenBufferSize( HANDLE hConsoleOutput
,
1303 FIXME("(%d,%dx%d): stub\n",hConsoleOutput
,dwSize
.X
,dwSize
.Y
);
1308 /******************************************************************************
1309 * FillConsoleOutputCharacterA [KERNEL32.242]
1312 * hConsoleOutput [I] Handle to screen buffer
1313 * cCharacter [I] Character to write
1314 * nLength [I] Number of cells to write to
1315 * dwCoord [I] Coords of first cell
1316 * lpNumCharsWritten [O] Pointer to number of cells written
1322 BOOL WINAPI
FillConsoleOutputCharacterA(
1323 HANDLE hConsoleOutput
,
1327 LPDWORD lpNumCharsWritten
)
1332 SetConsoleCursorPosition(hConsoleOutput
,dwCoord
);
1333 for(count
=0;count
<nLength
;count
++)
1334 WriteFile(hConsoleOutput
,&cCharacter
,1,&xlen
,NULL
);
1335 *lpNumCharsWritten
= nLength
;
1340 /******************************************************************************
1341 * FillConsoleOutputCharacterW [KERNEL32.243] Writes characters to console
1344 * hConsoleOutput [I] Handle to screen buffer
1345 * cCharacter [I] Character to write
1346 * nLength [I] Number of cells to write to
1347 * dwCoord [I] Coords of first cell
1348 * lpNumCharsWritten [O] Pointer to number of cells written
1354 BOOL WINAPI
FillConsoleOutputCharacterW(HANDLE hConsoleOutput
,
1358 LPDWORD lpNumCharsWritten
)
1363 SetConsoleCursorPosition(hConsoleOutput
,dwCoord
);
1364 /* FIXME: not quite correct ... but the lower part of UNICODE char comes
1367 for(count
=0;count
<nLength
;count
++)
1368 WriteFile(hConsoleOutput
,&cCharacter
,1,&xlen
,NULL
);
1369 *lpNumCharsWritten
= nLength
;
1374 /******************************************************************************
1375 * FillConsoleOutputAttribute [KERNEL32.241] Sets attributes for console
1378 * hConsoleOutput [I] Handle to screen buffer
1379 * wAttribute [I] Color attribute to write
1380 * nLength [I] Number of cells to write to
1381 * dwCoord [I] Coords of first cell
1382 * lpNumAttrsWritten [O] Pointer to number of cells written
1388 BOOL WINAPI
FillConsoleOutputAttribute( HANDLE hConsoleOutput
,
1389 WORD wAttribute
, DWORD nLength
, COORD dwCoord
,
1390 LPDWORD lpNumAttrsWritten
)
1392 FIXME("(%d,%d,%ld,%dx%d,%p): stub\n", hConsoleOutput
,
1393 wAttribute
,nLength
,dwCoord
.X
,dwCoord
.Y
,lpNumAttrsWritten
);
1394 *lpNumAttrsWritten
= nLength
;
1398 /******************************************************************************
1399 * ReadConsoleOutputCharacterA [KERNEL32.573]
1404 BOOL WINAPI
ReadConsoleOutputCharacterA(HANDLE hConsoleOutput
,
1405 LPSTR lpstr
, DWORD dword
, COORD coord
, LPDWORD lpdword
)
1407 FIXME("(%d,%p,%ld,%dx%d,%p): stub\n", hConsoleOutput
,lpstr
,
1408 dword
,coord
.X
,coord
.Y
,lpdword
);
1409 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1414 /******************************************************************************
1415 * ScrollConsoleScreenBufferA [KERNEL32.612]
1420 BOOL WINAPI
ScrollConsoleScreenBufferA( HANDLE hConsoleOutput
,
1421 LPSMALL_RECT lpScrollRect
, LPSMALL_RECT lpClipRect
,
1422 COORD dwDestOrigin
, LPCHAR_INFO lpFill
)
1424 FIXME("(%d,%p,%p,%dx%d,%p): stub\n", hConsoleOutput
,lpScrollRect
,
1425 lpClipRect
,dwDestOrigin
.X
,dwDestOrigin
.Y
,lpFill
);
1426 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);