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 #ifdef HAVE_SYS_ERRNO_H
33 #include <sys/errno.h>
39 #include "wine/winuser16.h"
40 #include "wine/keyboard16.h"
48 #include "debugtools.h"
50 DEFAULT_DEBUG_CHANNEL(console
)
53 /* FIXME: Should be in an internal header file. OK, so which one?
54 Used by CONSOLE_makecomplex. */
55 int wine_openpty(int *master
, int *slave
, char *name
,
56 struct termios
*term
, struct winsize
*winsize
);
58 /****************************************************************************
61 static int CONSOLE_GetPid( HANDLE handle
)
63 struct get_console_info_request
*req
= get_req_buffer();
65 if (server_call( REQ_GET_CONSOLE_INFO
)) return 0;
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
;
113 if ((inchar
=='\n')||(inchar
=='\r')) {
114 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= '\r';
115 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VK_RETURN
;
116 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0x1c;
117 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
120 /* FIXME: find good values for ^X */
121 ir
.Event
.KeyEvent
.wVirtualKeyCode
= 0xdead;
122 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0xbeef;
127 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
128 ir
.Event
.KeyEvent
.bKeyDown
= 0;
129 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
133 if ((j
==len
-1) || (buf
[j
+1]!='[')) {/* add ESCape on its own */
134 ir
.EventType
= 1; /* Key_event */
135 ir
.Event
.KeyEvent
.bKeyDown
= 1;
136 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
138 ir
.Event
.KeyEvent
.wVirtualKeyCode
= VkKeyScan16(27);
139 ir
.Event
.KeyEvent
.wVirtualScanCode
= MapVirtualKey16(
140 ir
.Event
.KeyEvent
.wVirtualKeyCode
,0
142 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
143 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= 27;
144 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
145 ir
.Event
.KeyEvent
.bKeyDown
= 0;
146 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
149 for (k
=j
;k
<len
;k
++) {
150 if (((buf
[k
]>='A') && (buf
[k
]<='Z')) ||
151 ((buf
[k
]>='a') && (buf
[k
]<='z')) ||
157 int subid
,scancode
=0;
159 ir
.EventType
= 1; /* Key_event */
160 ir
.Event
.KeyEvent
.bKeyDown
= 1;
161 ir
.Event
.KeyEvent
.wRepeatCount
= 0;
162 ir
.Event
.KeyEvent
.dwControlKeyState
= 0;
164 ir
.Event
.KeyEvent
.wVirtualKeyCode
= 0xad; /* FIXME */
165 ir
.Event
.KeyEvent
.wVirtualScanCode
= 0xad; /* FIXME */
166 ir
.Event
.KeyEvent
.uChar
.AsciiChar
= 0;
170 sscanf(&buf
[j
+2],"%d",&subid
);
172 case 2:/*INS */scancode
= 0xe052;break;
173 case 3:/*DEL */scancode
= 0xe053;break;
174 case 6:/*PGDW*/scancode
= 0xe051;break;
175 case 5:/*PGUP*/scancode
= 0xe049;break;
176 case 11:/*F1 */scancode
= 0x003b;break;
177 case 12:/*F2 */scancode
= 0x003c;break;
178 case 13:/*F3 */scancode
= 0x003d;break;
179 case 14:/*F4 */scancode
= 0x003e;break;
180 case 15:/*F5 */scancode
= 0x003f;break;
181 case 17:/*F6 */scancode
= 0x0040;break;
182 case 18:/*F7 */scancode
= 0x0041;break;
183 case 19:/*F8 */scancode
= 0x0042;break;
184 case 20:/*F9 */scancode
= 0x0043;break;
185 case 21:/*F10 */scancode
= 0x0044;break;
186 case 23:/*F11 */scancode
= 0x00d9;break;
187 case 24:/*F12 */scancode
= 0x00da;break;
188 /* FIXME: Shift-Fx */
190 FIXME("parse ESC[%d~\n",subid
);
194 case 'A': /* Cursor Up */scancode
= 0xe048;break;
195 case 'B': /* Cursor Down */scancode
= 0xe050;break;
196 case 'D': /* Cursor Left */scancode
= 0xe04b;break;
197 case 'C': /* Cursor Right */scancode
= 0xe04d;break;
198 case 'F': /* End */scancode
= 0xe04f;break;
199 case 'H': /* Home */scancode
= 0xe047;break;
201 /* Mouse Button Press (ESCM<button+'!'><x+'!'><y+'!'>) or
202 * Release (ESCM#<x+'!'><y+'!'>
205 ir
.EventType
= MOUSE_EVENT
;
206 ir
.Event
.MouseEvent
.dwMousePosition
.x
= buf
[k
+2]-'!';
207 ir
.Event
.MouseEvent
.dwMousePosition
.y
= buf
[k
+3]-'!';
209 ir
.Event
.MouseEvent
.dwButtonState
= 0;
211 ir
.Event
.MouseEvent
.dwButtonState
= 1<<(buf
[k
+1]-' ');
212 ir
.Event
.MouseEvent
.dwEventFlags
= 0; /* FIXME */
213 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
222 ir
.Event
.KeyEvent
.wVirtualScanCode
= scancode
;
223 ir
.Event
.KeyEvent
.wVirtualKeyCode
= MapVirtualKey16(scancode
,1);
224 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
225 ir
.Event
.KeyEvent
.bKeyDown
= 0;
226 assert(WriteConsoleInputA( hConsoleInput
, &ir
, 1, &junk
));
234 /****************************************************************************
235 * CONSOLE_get_input (internal)
237 * Reads (nonblocking) as much input events as possible and stores them
238 * in an internal queue.
241 CONSOLE_get_input( HANDLE handle
, BOOL blockwait
)
243 char *buf
= HeapAlloc(GetProcessHeap(),0,1);
250 if (WaitForSingleObject( handle
, 0 )) break;
251 if (!ReadFile( handle
, &inchar
, 1, &res
, NULL
)) break;
252 if (!res
) /* res 0 but readable means EOF? Hmm. */
254 buf
= HeapReAlloc(GetProcessHeap(),0,buf
,len
+1);
257 CONSOLE_string_to_IR(handle
,buf
,len
);
258 HeapFree(GetProcessHeap(),0,buf
);
261 /******************************************************************************
262 * SetConsoleCtrlHandler [KERNEL32.459] Adds function to calling process list
265 * func [I] Address of handler function
266 * add [I] Handler to add or remove
273 * James Sutherland (JamesSutherland@gmx.de)
274 * Added global variables console_ignore_ctrl_c and handlers[]
275 * Does not yet do any error checking, or set LastError if failed.
276 * This doesn't yet matter, since these handlers are not yet called...!
278 static unsigned int console_ignore_ctrl_c
= 0;
279 static HANDLER_ROUTINE
*handlers
[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
280 BOOL WINAPI
SetConsoleCtrlHandler( HANDLER_ROUTINE
*func
, BOOL add
)
282 unsigned int alloc_loop
= sizeof(handlers
)/sizeof(HANDLER_ROUTINE
*);
283 unsigned int done
= 0;
284 FIXME("(%p,%i) - no error checking or testing yet\n", func
, add
);
287 console_ignore_ctrl_c
= add
;
293 if (!handlers
[alloc_loop
] && !done
)
295 handlers
[alloc_loop
] = func
;
299 FIXME("Out of space on CtrlHandler table\n");
305 if (handlers
[alloc_loop
] == func
&& !done
)
307 handlers
[alloc_loop
] = 0;
311 WARN("Attempt to remove non-installed CtrlHandler %p\n",
319 /******************************************************************************
320 * GenerateConsoleCtrlEvent [KERNEL32.275] Simulate a CTRL-C or CTRL-BREAK
323 * dwCtrlEvent [I] Type of event
324 * dwProcessGroupID [I] Process group ID to send event to
327 * Doesn't yet work...!
331 * Failure: False (and *should* [but doesn't] set LastError)
333 BOOL WINAPI
GenerateConsoleCtrlEvent( DWORD dwCtrlEvent
,
334 DWORD dwProcessGroupID
)
336 if (dwCtrlEvent
!= CTRL_C_EVENT
&& dwCtrlEvent
!= CTRL_BREAK_EVENT
)
338 ERR("invalid event %d for PGID %ld\n",
339 (unsigned short)dwCtrlEvent
, dwProcessGroupID
);
342 if (dwProcessGroupID
== GetCurrentProcessId() )
344 FIXME("Attempt to send event %d to self - stub\n",
345 (unsigned short)dwCtrlEvent
);
348 FIXME("event %d to external PGID %ld - not implemented yet\n",
349 (unsigned short)dwCtrlEvent
, dwProcessGroupID
);
354 /******************************************************************************
355 * CreateConsoleScreenBuffer [KERNEL32.151] Creates a console screen buffer
358 * dwDesiredAccess [I] Access flag
359 * dwShareMode [I] Buffer share mode
360 * sa [I] Security attributes
361 * dwFlags [I] Type of buffer to create
362 * lpScreenBufferData [I] Reserved
365 * Should call SetLastError
368 * Success: Handle to new console screen buffer
369 * Failure: INVALID_HANDLE_VALUE
371 HANDLE WINAPI
CreateConsoleScreenBuffer( DWORD dwDesiredAccess
,
372 DWORD dwShareMode
, LPSECURITY_ATTRIBUTES sa
,
373 DWORD dwFlags
, LPVOID lpScreenBufferData
)
375 FIXME("(%ld,%ld,%p,%ld,%p): stub\n",dwDesiredAccess
,
376 dwShareMode
, sa
, dwFlags
, lpScreenBufferData
);
377 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
378 return INVALID_HANDLE_VALUE
;
382 /***********************************************************************
383 * GetConsoleScreenBufferInfo (KERNEL32.190)
385 BOOL WINAPI
GetConsoleScreenBufferInfo( HANDLE hConsoleOutput
,
386 LPCONSOLE_SCREEN_BUFFER_INFO csbi
)
390 csbi
->dwCursorPosition
.x
= 0;
391 csbi
->dwCursorPosition
.y
= 0;
392 csbi
->wAttributes
= 0;
393 csbi
->srWindow
.Left
= 0;
394 csbi
->srWindow
.Right
= 79;
395 csbi
->srWindow
.Top
= 0;
396 csbi
->srWindow
.Bottom
= 23;
397 csbi
->dwMaximumWindowSize
.x
= 80;
398 csbi
->dwMaximumWindowSize
.y
= 24;
403 /******************************************************************************
404 * SetConsoleActiveScreenBuffer [KERNEL32.623] Sets buffer to current console
410 BOOL WINAPI
SetConsoleActiveScreenBuffer(
411 HANDLE hConsoleOutput
) /* [in] Handle to console screen buffer */
413 FIXME("(%x): stub\n", hConsoleOutput
);
418 /***********************************************************************
419 * GetLargestConsoleWindowSize (KERNEL32.226)
421 DWORD WINAPI
GetLargestConsoleWindowSize( HANDLE hConsoleOutput
)
423 return (DWORD
)MAKELONG(80,24);
426 /***********************************************************************
427 * FreeConsole (KERNEL32.267)
429 BOOL WINAPI
FreeConsole(VOID
)
431 return !server_call( REQ_FREE_CONSOLE
);
435 /*************************************************************************
438 * Open a handle to the current process console.
440 HANDLE
CONSOLE_OpenHandle( BOOL output
, DWORD access
, LPSECURITY_ATTRIBUTES sa
)
443 struct open_console_request
*req
= get_req_buffer();
445 req
->output
= output
;
446 req
->access
= access
;
447 req
->inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
449 if (!server_call( REQ_OPEN_CONSOLE
)) ret
= req
->handle
;
454 /*************************************************************************
455 * CONSOLE_make_complex [internal]
457 * Turns a CONSOLE kernel object into a complex one.
458 * (switches from output/input using the terminal where WINE was started to
461 * This makes simple commandline tools pipeable, while complex commandline
462 * tools work without getting messed up by debugoutput.
464 * All other functions should work indedependend from this call.
466 * To test for complex console: pid == 0 -> simple, otherwise complex.
468 static BOOL
CONSOLE_make_complex(HANDLE handle
)
470 struct set_console_fd_request
*req
= get_req_buffer();
474 int i
,xpid
,master
,slave
,pty_handle
;
476 if (CONSOLE_GetPid( handle
)) return TRUE
; /* already complex */
478 MESSAGE("Console: Making console complex (creating an xterm)...\n");
480 if (tcgetattr(0, &term
) < 0) {
481 /* ignore failure, or we can't run from a script */
483 term
.c_lflag
= ~(ECHO
|ICANON
);
485 if (wine_openpty(&master
, &slave
, NULL
, &term
, NULL
) < 0)
488 if ((xpid
=fork()) == 0) {
489 tcsetattr(slave
, TCSADRAIN
, &term
);
491 sprintf(buf
, "-Sxx%d", master
);
492 /* "-fn vga" for VGA font. Harmless if vga is not present:
493 * xterm: unable to open font "vga", trying "fixed"....
495 execlp("xterm", "xterm", buf
, "-fn","vga",NULL
);
496 ERR("error creating AllocConsole xterm\n");
499 pty_handle
= FILE_DupUnixHandle( slave
, GENERIC_READ
| GENERIC_WRITE
);
502 if (pty_handle
== -1) return FALSE
;
504 /* most xterms like to print their window ID when used with -S;
505 * read it and continue before the user has a chance...
507 for (i
= 0; i
< 10000; i
++)
509 BOOL ok
= ReadFile( pty_handle
, &c
, 1, NULL
, NULL
);
510 if (!ok
&& !c
) usleep(100); /* wait for xterm to be created */
511 else if (c
== '\n') break;
515 ERR("can't read xterm WID\n");
516 CloseHandle( pty_handle
);
519 req
->handle
= handle
;
520 req
->file_handle
= pty_handle
;
522 server_call( REQ_SET_CONSOLE_FD
);
523 CloseHandle( pty_handle
);
525 /* enable mouseclicks */
526 strcpy( buf
, "\033[?1001s\033[?1000h" );
527 WriteFile(handle
,buf
,strlen(buf
),NULL
,NULL
);
529 strcpy( buf
, "\033]2;" );
530 if (GetConsoleTitleA( buf
+ 4, sizeof(buf
) - 5 ))
533 WriteFile(handle
,buf
,strlen(buf
),NULL
,NULL
);
540 /***********************************************************************
541 * AllocConsole (KERNEL32.103)
543 * creates an xterm with a pty to our program
545 BOOL WINAPI
AllocConsole(VOID
)
547 struct alloc_console_request
*req
= get_req_buffer();
549 int handle_in
, handle_out
;
552 req
->access
= GENERIC_READ
| GENERIC_WRITE
| SYNCHRONIZE
;
553 req
->inherit
= FALSE
;
554 if (server_call( REQ_ALLOC_CONSOLE
)) return FALSE
;
555 handle_in
= req
->handle_in
;
556 handle_out
= req
->handle_out
;
558 if (!DuplicateHandle( GetCurrentProcess(), req
->handle_out
, GetCurrentProcess(), &hStderr
,
559 0, TRUE
, DUPLICATE_SAME_ACCESS
))
561 CloseHandle( handle_in
);
562 CloseHandle( handle_out
);
567 /* NT resets the STD_*_HANDLEs on console alloc */
568 SetStdHandle( STD_INPUT_HANDLE
, handle_in
);
569 SetStdHandle( STD_OUTPUT_HANDLE
, handle_out
);
570 SetStdHandle( STD_ERROR_HANDLE
, hStderr
);
572 SetLastError(ERROR_SUCCESS
);
573 SetConsoleTitleA("Wine Console");
578 /******************************************************************************
579 * GetConsoleCP [KERNEL32.295] Returns the OEM code page for the console
584 UINT WINAPI
GetConsoleCP(VOID
)
590 /***********************************************************************
591 * GetConsoleOutputCP (KERNEL32.189)
593 UINT WINAPI
GetConsoleOutputCP(VOID
)
595 return GetConsoleCP();
598 /***********************************************************************
599 * GetConsoleMode (KERNEL32.188)
601 BOOL WINAPI
GetConsoleMode(HANDLE hcon
,LPDWORD mode
)
604 struct get_console_mode_request
*req
= get_req_buffer();
606 if (!server_call( REQ_GET_CONSOLE_MODE
))
608 if (mode
) *mode
= req
->mode
;
615 /******************************************************************************
616 * SetConsoleMode [KERNEL32.628] Sets input mode of console's input buffer
619 * hcon [I] Handle to console input or screen buffer
620 * mode [I] Input or output mode to set
626 BOOL WINAPI
SetConsoleMode( HANDLE hcon
, DWORD mode
)
628 struct set_console_mode_request
*req
= get_req_buffer();
631 return !server_call( REQ_SET_CONSOLE_MODE
);
635 /***********************************************************************
636 * GetConsoleTitleA (KERNEL32.191)
638 DWORD WINAPI
GetConsoleTitleA(LPSTR title
,DWORD size
)
640 struct get_console_info_request
*req
= get_req_buffer();
644 if ((hcon
= CreateFileA( "CONOUT$", GENERIC_READ
, 0, NULL
,
645 OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
648 if (!server_call( REQ_GET_CONSOLE_INFO
))
650 lstrcpynA( title
, req
->title
, size
);
651 ret
= strlen(req
->title
);
658 /******************************************************************************
659 * GetConsoleTitle32W [KERNEL32.192] Retrieves title string for console
662 * title [O] Address of buffer for title
663 * size [I] Size of buffer
666 * Success: Length of string copied
669 DWORD WINAPI
GetConsoleTitleW( LPWSTR title
, DWORD size
)
674 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, size
))) return 0;
675 ret
= GetConsoleTitleA( tmp
, size
);
676 lstrcpyAtoW( title
, tmp
);
677 HeapFree( GetProcessHeap(), 0, tmp
);
682 /***********************************************************************
683 * WriteConsoleA (KERNEL32.729)
685 BOOL WINAPI
WriteConsoleA( HANDLE hConsoleOutput
,
687 DWORD nNumberOfCharsToWrite
,
688 LPDWORD lpNumberOfCharsWritten
,
691 /* FIXME: should I check if this is a console handle? */
692 return WriteFile(hConsoleOutput
, lpBuffer
, nNumberOfCharsToWrite
,
693 lpNumberOfCharsWritten
, NULL
);
698 if (bufused==curbufsize-1) \
699 buffer = HeapReAlloc(GetProcessHeap(),0,buffer,(curbufsize+=100));\
701 #define SADD(s) { char *x=s;while (*x) {CADD(*x);x++;}}
703 /***********************************************************************
704 * WriteConsoleOutputA (KERNEL32.732)
706 BOOL WINAPI
WriteConsoleOutputA( HANDLE hConsoleOutput
,
707 LPCHAR_INFO lpBuffer
,
710 LPSMALL_RECT lpWriteRegion
)
712 int i
,j
,off
=0,lastattr
=-1;
714 char sbuf
[20],*buffer
=NULL
;
715 int bufused
=0,curbufsize
= 100;
717 CONSOLE_SCREEN_BUFFER_INFO csbi
;
718 const int colormap
[8] = {
722 CONSOLE_make_complex(hConsoleOutput
);
723 buffer
= HeapAlloc(GetProcessHeap(),0,curbufsize
);
724 offbase
= (dwBufferCoord
.y
- 1) * dwBufferSize
.x
+
725 (dwBufferCoord
.x
- lpWriteRegion
->Left
);
727 TRACE("orig rect top = %d, bottom=%d, left=%d, right=%d\n",
729 lpWriteRegion
->Bottom
,
734 GetConsoleScreenBufferInfo(hConsoleOutput
, &csbi
);
736 /* Step 1. Make (Bottom,Right) offset of intersection with
738 lpWriteRegion
->Bottom
= min(lpWriteRegion
->Bottom
, csbi
.dwSize
.y
-1) -
740 lpWriteRegion
->Right
= min(lpWriteRegion
->Right
, csbi
.dwSize
.x
-1) -
743 /* Step 2. If either offset is negative, then no action
744 should be performed. (Implies that requested rectangle is
745 outside the current screen buffer rectangle.) */
746 if ((lpWriteRegion
->Bottom
< 0) ||
747 (lpWriteRegion
->Right
< 0)) {
748 /* readjust (Bottom Right) for rectangle */
749 lpWriteRegion
->Bottom
+= lpWriteRegion
->Top
;
750 lpWriteRegion
->Right
+= lpWriteRegion
->Left
;
752 TRACE("invisible rect top = %d, bottom=%d, left=%d, right=%d\n",
754 lpWriteRegion
->Bottom
,
759 HeapFree(GetProcessHeap(),0,buffer
);
763 /* Step 3. Intersect with source rectangle */
764 lpWriteRegion
->Bottom
= lpWriteRegion
->Top
- dwBufferCoord
.y
+
765 min(lpWriteRegion
->Bottom
+ dwBufferCoord
.y
, dwBufferSize
.y
-1);
766 lpWriteRegion
->Right
= lpWriteRegion
->Left
- dwBufferCoord
.x
+
767 min(lpWriteRegion
->Right
+ dwBufferCoord
.x
, dwBufferSize
.x
-1);
769 TRACE("clipped rect top = %d, bottom=%d, left=%d,right=%d\n",
771 lpWriteRegion
->Bottom
,
776 /* Validate above computations made sense, if not then issue
777 error and fudge to single character rectangle */
778 if ((lpWriteRegion
->Bottom
< lpWriteRegion
->Top
) ||
779 (lpWriteRegion
->Right
< lpWriteRegion
->Left
)) {
780 ERR("Invalid clipped rectangle top = %d, bottom=%d, left=%d,right=%d\n",
782 lpWriteRegion
->Bottom
,
786 lpWriteRegion
->Bottom
= lpWriteRegion
->Top
;
787 lpWriteRegion
->Right
= lpWriteRegion
->Left
;
790 /* Now do the real processing and move the characters */
791 for (i
=lpWriteRegion
->Top
;i
<=lpWriteRegion
->Bottom
;i
++) {
792 offbase
+= dwBufferSize
.x
;
793 sprintf(sbuf
,"%c[%d;%dH",27,i
+1,lpWriteRegion
->Left
+1);
795 for (j
=lpWriteRegion
->Left
;j
<=lpWriteRegion
->Right
;j
++) {
797 if (lastattr
!=lpBuffer
[off
].Attributes
) {
798 lastattr
= lpBuffer
[off
].Attributes
;
799 sprintf(sbuf
,"%c[0;%s3%d;4%dm",
801 (lastattr
& FOREGROUND_INTENSITY
)?"1;":"",
802 colormap
[lastattr
&7],
803 colormap
[(lastattr
&0x70)>>4]
805 /* FIXME: BACKGROUND_INTENSITY */
808 CADD(lpBuffer
[off
].Char
.AsciiChar
);
811 sprintf(sbuf
,"%c[0m",27);SADD(sbuf
);
812 WriteFile(hConsoleOutput
,buffer
,bufused
,&res
,NULL
);
813 HeapFree(GetProcessHeap(),0,buffer
);
817 /***********************************************************************
818 * WriteConsoleW (KERNEL32.577)
820 BOOL WINAPI
WriteConsoleW( HANDLE hConsoleOutput
,
822 DWORD nNumberOfCharsToWrite
,
823 LPDWORD lpNumberOfCharsWritten
,
827 LPSTR xstring
=HeapAlloc( GetProcessHeap(), 0, nNumberOfCharsToWrite
);
829 lstrcpynWtoA( xstring
, lpBuffer
,nNumberOfCharsToWrite
);
831 /* FIXME: should I check if this is a console handle? */
832 ret
= WriteFile(hConsoleOutput
, xstring
, nNumberOfCharsToWrite
,
833 lpNumberOfCharsWritten
, NULL
);
834 HeapFree( GetProcessHeap(), 0, xstring
);
839 /***********************************************************************
840 * ReadConsoleA (KERNEL32.419)
842 BOOL WINAPI
ReadConsoleA( HANDLE hConsoleInput
,
844 DWORD nNumberOfCharsToRead
,
845 LPDWORD lpNumberOfCharsRead
,
849 LPSTR xbuf
= (LPSTR
)lpBuffer
;
852 TRACE("(%d,%p,%ld,%p,%p)\n",
853 hConsoleInput
,lpBuffer
,nNumberOfCharsToRead
,
854 lpNumberOfCharsRead
,lpReserved
857 CONSOLE_get_input(hConsoleInput
,FALSE
);
859 /* FIXME: should we read at least 1 char? The SDK does not say */
860 while (charsread
<nNumberOfCharsToRead
)
862 struct read_console_input_request
*req
= get_req_buffer();
863 req
->handle
= hConsoleInput
;
866 if (server_call( REQ_READ_CONSOLE_INPUT
)) return FALSE
;
867 if (!req
->read
) break;
868 ir
= (LPINPUT_RECORD
)(req
+1);
869 if (!ir
->Event
.KeyEvent
.bKeyDown
)
871 if (ir
->EventType
!= KEY_EVENT
)
873 *xbuf
++ = ir
->Event
.KeyEvent
.uChar
.AsciiChar
;
876 if (lpNumberOfCharsRead
)
877 *lpNumberOfCharsRead
= charsread
;
881 /***********************************************************************
882 * ReadConsoleW (KERNEL32.427)
884 BOOL WINAPI
ReadConsoleW( HANDLE hConsoleInput
,
886 DWORD nNumberOfCharsToRead
,
887 LPDWORD lpNumberOfCharsRead
,
891 LPSTR buf
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead
);
896 nNumberOfCharsToRead
,
901 lstrcpynAtoW(lpBuffer
,buf
,nNumberOfCharsToRead
);
902 HeapFree( GetProcessHeap(), 0, buf
);
907 /******************************************************************************
908 * ReadConsoleInput32A [KERNEL32.569] Reads data from a console
911 * hConsoleInput [I] Handle to console input buffer
912 * lpBuffer [O] Address of buffer for read data
913 * nLength [I] Number of records to read
914 * lpNumberOfEventsRead [O] Address of number of records read
920 BOOL WINAPI
ReadConsoleInputA(HANDLE hConsoleInput
, LPINPUT_RECORD lpBuffer
,
921 DWORD nLength
, LPDWORD lpNumberOfEventsRead
)
923 struct read_console_input_request
*req
= get_req_buffer();
925 /* loop until we get at least one event */
928 req
->handle
= hConsoleInput
;
929 req
->count
= nLength
;
931 if (server_call( REQ_READ_CONSOLE_INPUT
)) return FALSE
;
934 memcpy( lpBuffer
, req
+ 1, req
->read
* sizeof(*lpBuffer
) );
935 if (lpNumberOfEventsRead
) *lpNumberOfEventsRead
= req
->read
;
938 CONSOLE_get_input(hConsoleInput
,TRUE
);
939 /*WaitForSingleObject( hConsoleInput, INFINITE32 );*/
944 /***********************************************************************
945 * ReadConsoleInput32W (KERNEL32.570)
947 BOOL WINAPI
ReadConsoleInputW( HANDLE handle
, LPINPUT_RECORD buffer
,
948 DWORD count
, LPDWORD read
)
950 /* FIXME: Fix this if we get UNICODE input. */
951 return ReadConsoleInputA( handle
, buffer
, count
, read
);
955 /***********************************************************************
956 * FlushConsoleInputBuffer (KERNEL32.132)
958 BOOL WINAPI
FlushConsoleInputBuffer( HANDLE handle
)
960 struct read_console_input_request
*req
= get_req_buffer();
961 req
->handle
= handle
;
962 req
->count
= -1; /* get all records */
964 return !server_call( REQ_READ_CONSOLE_INPUT
);
968 /***********************************************************************
969 * PeekConsoleInputA (KERNEL32.550)
971 * Gets 'count' first events (or less) from input queue.
973 * Does not need a complex console.
975 BOOL WINAPI
PeekConsoleInputA( HANDLE handle
, LPINPUT_RECORD buffer
,
976 DWORD count
, LPDWORD read
)
978 struct read_console_input_request
*req
= get_req_buffer();
980 CONSOLE_get_input(handle
,FALSE
);
982 req
->handle
= handle
;
985 if (server_call( REQ_READ_CONSOLE_INPUT
)) return FALSE
;
986 if (req
->read
) memcpy( buffer
, req
+ 1, req
->read
* sizeof(*buffer
) );
987 if (read
) *read
= req
->read
;
992 /***********************************************************************
993 * PeekConsoleInputW (KERNEL32.551)
995 BOOL WINAPI
PeekConsoleInputW(HANDLE hConsoleInput
,
996 LPINPUT_RECORD pirBuffer
,
1000 /* FIXME: Hmm. Fix this if we get UNICODE input. */
1001 return PeekConsoleInputA(hConsoleInput
,pirBuffer
,cInRecords
,lpcRead
);
1005 /******************************************************************************
1006 * WriteConsoleInput32A [KERNEL32.730] Write data to a console input buffer
1009 BOOL WINAPI
WriteConsoleInputA( HANDLE handle
, INPUT_RECORD
*buffer
,
1010 DWORD count
, LPDWORD written
)
1012 struct write_console_input_request
*req
= get_req_buffer();
1013 const DWORD max
= server_remaining( req
+ 1 ) / sizeof(INPUT_RECORD
);
1015 if (written
) *written
= 0;
1018 DWORD len
= count
< max
? count
: max
;
1020 req
->handle
= handle
;
1021 memcpy( req
+ 1, buffer
, len
* sizeof(*buffer
) );
1022 if (server_call( REQ_WRITE_CONSOLE_INPUT
)) return FALSE
;
1023 if (written
) *written
+= req
->written
;
1031 /***********************************************************************
1032 * SetConsoleTitle32A (KERNEL32.476)
1034 * Sets the console title.
1036 * We do not necessarily need to create a complex console for that,
1037 * but should remember the title and set it on creation of the latter.
1038 * (not fixed at this time).
1040 BOOL WINAPI
SetConsoleTitleA(LPCSTR title
)
1042 struct set_console_info_request
*req
= get_req_buffer();
1046 if ((hcon
= CreateFileA( "CONOUT$", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
1047 OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
1050 req
->mask
= SET_CONSOLE_INFO_TITLE
;
1051 lstrcpynA( req
->title
, title
, server_remaining(req
->title
) );
1052 if (server_call( REQ_SET_CONSOLE_INFO
)) goto error
;
1053 if (CONSOLE_GetPid( hcon
))
1055 /* only set title for complex console (own xterm) */
1056 WriteFile( hcon
, "\033]2;", 4, &written
, NULL
);
1057 WriteFile( hcon
, title
, strlen(title
), &written
, NULL
);
1058 WriteFile( hcon
, "\a", 1, &written
, NULL
);
1060 CloseHandle( hcon
);
1063 CloseHandle( hcon
);
1068 /******************************************************************************
1069 * SetConsoleTitle32W [KERNEL32.477] Sets title bar string for console
1072 * title [I] Address of new title
1075 * This should not be calling the A version
1081 BOOL WINAPI
SetConsoleTitleW( LPCWSTR title
)
1085 LPSTR titleA
= HEAP_strdupWtoA( GetProcessHeap(), 0, title
);
1086 ret
= SetConsoleTitleA(titleA
);
1087 HeapFree( GetProcessHeap(), 0, titleA
);
1091 /******************************************************************************
1092 * SetConsoleCursorPosition [KERNEL32.627]
1093 * Sets the cursor position in console
1096 * hConsoleOutput [I] Handle of console screen buffer
1097 * dwCursorPosition [I] New cursor position coordinates
1101 BOOL WINAPI
SetConsoleCursorPosition( HANDLE hcon
, COORD pos
)
1106 /* make console complex only if we change lines, not just in the line */
1108 CONSOLE_make_complex(hcon
);
1110 TRACE("%d (%dx%d)\n", hcon
, pos
.x
, pos
.y
);
1111 /* x are columns, y rows */
1113 /* full screen cursor absolute positioning */
1114 sprintf(xbuf
,"%c[%d;%dH", 0x1B, pos
.y
+1, pos
.x
+1);
1116 /* relative cursor positioning in line (\r to go to 0) */
1117 sprintf(xbuf
,"\r%c[%dC", 0x1B, pos
.x
);
1118 /* FIXME: store internal if we start using own console buffers */
1119 WriteFile(hcon
,xbuf
,strlen(xbuf
),&xlen
,NULL
);
1123 /***********************************************************************
1124 * GetNumberOfConsoleInputEvents (KERNEL32.246)
1126 BOOL WINAPI
GetNumberOfConsoleInputEvents(HANDLE hcon
,LPDWORD nrofevents
)
1128 struct read_console_input_request
*req
= get_req_buffer();
1130 CONSOLE_get_input(hcon
,FALSE
);
1135 if (server_call( REQ_READ_CONSOLE_INPUT
)) return FALSE
;
1136 if (nrofevents
) *nrofevents
= req
->read
;
1140 /***********************************************************************
1141 * GetNumberOfConsoleMouseButtons (KERNEL32.358)
1143 BOOL WINAPI
GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons
)
1145 FIXME("(%p): stub\n", nrofbuttons
);
1150 /******************************************************************************
1151 * GetConsoleCursorInfo32 [KERNEL32.296] Gets size and visibility of console
1154 * hcon [I] Handle to console screen buffer
1155 * cinfo [O] Address of cursor information
1161 BOOL WINAPI
GetConsoleCursorInfo( HANDLE hcon
, LPCONSOLE_CURSOR_INFO cinfo
)
1163 struct get_console_info_request
*req
= get_req_buffer();
1165 if (server_call( REQ_GET_CONSOLE_INFO
)) return FALSE
;
1168 cinfo
->dwSize
= req
->cursor_size
;
1169 cinfo
->bVisible
= req
->cursor_visible
;
1175 /******************************************************************************
1176 * SetConsoleCursorInfo32 [KERNEL32.626] Sets size and visibility of cursor
1182 BOOL WINAPI
SetConsoleCursorInfo(
1183 HANDLE hcon
, /* [in] Handle to console screen buffer */
1184 LPCONSOLE_CURSOR_INFO cinfo
) /* [in] Address of cursor information */
1186 struct set_console_info_request
*req
= get_req_buffer();
1190 CONSOLE_make_complex(hcon
);
1191 sprintf(buf
,"\033[?25%c",cinfo
->bVisible
?'h':'l');
1192 WriteFile(hcon
,buf
,strlen(buf
),&xlen
,NULL
);
1195 req
->cursor_size
= cinfo
->dwSize
;
1196 req
->cursor_visible
= cinfo
->bVisible
;
1197 req
->mask
= SET_CONSOLE_INFO_CURSOR
;
1198 return !server_call( REQ_SET_CONSOLE_INFO
);
1202 /******************************************************************************
1203 * SetConsoleWindowInfo [KERNEL32.634] Sets size and position of console
1209 BOOL WINAPI
SetConsoleWindowInfo(
1210 HANDLE hcon
, /* [in] Handle to console screen buffer */
1211 BOOL bAbsolute
, /* [in] Coordinate type flag */
1212 LPSMALL_RECT window
) /* [in] Address of new window rectangle */
1214 FIXME("(%x,%d,%p): stub\n", hcon
, bAbsolute
, window
);
1219 /******************************************************************************
1220 * SetConsoleTextAttribute32 [KERNEL32.631] Sets colors for text
1222 * Sets the foreground and background color attributes of characters
1223 * written to the screen buffer.
1229 BOOL WINAPI
SetConsoleTextAttribute(HANDLE hConsoleOutput
,WORD wAttr
)
1231 const int colormap
[8] = {
1238 TRACE("(%d,%d)\n",hConsoleOutput
,wAttr
);
1239 sprintf(buffer
,"%c[0;%s3%d;4%dm",
1241 (wAttr
& FOREGROUND_INTENSITY
)?"1;":"",
1243 colormap
[(wAttr
&0x70)>>4]
1245 WriteFile(hConsoleOutput
,buffer
,strlen(buffer
),&xlen
,NULL
);
1250 /******************************************************************************
1251 * SetConsoleScreenBufferSize [KERNEL32.630] Changes size of console
1254 * hConsoleOutput [I] Handle to console screen buffer
1255 * dwSize [I] New size in character rows and cols
1261 BOOL WINAPI
SetConsoleScreenBufferSize( HANDLE hConsoleOutput
,
1264 FIXME("(%d,%dx%d): stub\n",hConsoleOutput
,dwSize
.x
,dwSize
.y
);
1269 /******************************************************************************
1270 * FillConsoleOutputCharacterA [KERNEL32.242]
1273 * hConsoleOutput [I] Handle to screen buffer
1274 * cCharacter [I] Character to write
1275 * nLength [I] Number of cells to write to
1276 * dwCoord [I] Coords of first cell
1277 * lpNumCharsWritten [O] Pointer to number of cells written
1283 BOOL WINAPI
FillConsoleOutputCharacterA(
1284 HANDLE hConsoleOutput
,
1288 LPDWORD lpNumCharsWritten
)
1293 SetConsoleCursorPosition(hConsoleOutput
,dwCoord
);
1294 for(count
=0;count
<nLength
;count
++)
1295 WriteFile(hConsoleOutput
,&cCharacter
,1,&xlen
,NULL
);
1296 *lpNumCharsWritten
= nLength
;
1301 /******************************************************************************
1302 * FillConsoleOutputCharacterW [KERNEL32.243] Writes characters to console
1305 * hConsoleOutput [I] Handle to screen buffer
1306 * cCharacter [I] Character to write
1307 * nLength [I] Number of cells to write to
1308 * dwCoord [I] Coords of first cell
1309 * lpNumCharsWritten [O] Pointer to number of cells written
1315 BOOL WINAPI
FillConsoleOutputCharacterW(HANDLE hConsoleOutput
,
1319 LPDWORD lpNumCharsWritten
)
1324 SetConsoleCursorPosition(hConsoleOutput
,dwCoord
);
1325 /* FIXME: not quite correct ... but the lower part of UNICODE char comes
1328 for(count
=0;count
<nLength
;count
++)
1329 WriteFile(hConsoleOutput
,&cCharacter
,1,&xlen
,NULL
);
1330 *lpNumCharsWritten
= nLength
;
1335 /******************************************************************************
1336 * FillConsoleOutputAttribute [KERNEL32.241] Sets attributes for console
1339 * hConsoleOutput [I] Handle to screen buffer
1340 * wAttribute [I] Color attribute to write
1341 * nLength [I] Number of cells to write to
1342 * dwCoord [I] Coords of first cell
1343 * lpNumAttrsWritten [O] Pointer to number of cells written
1349 BOOL WINAPI
FillConsoleOutputAttribute( HANDLE hConsoleOutput
,
1350 WORD wAttribute
, DWORD nLength
, COORD dwCoord
,
1351 LPDWORD lpNumAttrsWritten
)
1353 FIXME("(%d,%d,%ld,%dx%d,%p): stub\n", hConsoleOutput
,
1354 wAttribute
,nLength
,dwCoord
.x
,dwCoord
.y
,lpNumAttrsWritten
);
1355 *lpNumAttrsWritten
= nLength
;
1359 /******************************************************************************
1360 * ReadConsoleOutputCharacter32A [KERNEL32.573]
1365 BOOL WINAPI
ReadConsoleOutputCharacterA(HANDLE hConsoleOutput
,
1366 LPSTR lpstr
, DWORD dword
, COORD coord
, LPDWORD lpdword
)
1368 FIXME("(%d,%p,%ld,%dx%d,%p): stub\n", hConsoleOutput
,lpstr
,
1369 dword
,coord
.x
,coord
.y
,lpdword
);
1370 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1375 /******************************************************************************
1376 * ScrollConsoleScreenBuffer [KERNEL32.612]
1381 BOOL WINAPI
ScrollConsoleScreenBuffer( HANDLE hConsoleOutput
,
1382 LPSMALL_RECT lpScrollRect
, LPSMALL_RECT lpClipRect
,
1383 COORD dwDestOrigin
, LPCHAR_INFO lpFill
)
1385 FIXME("(%d,%p,%p,%dx%d,%p): stub\n", hConsoleOutput
,lpScrollRect
,
1386 lpClipRect
,dwDestOrigin
.x
,dwDestOrigin
.y
,lpFill
);
1387 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);