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
8 * Copyright 2001,2002 Eric Pouech
9 * Copyright 2001 Alexandre Julliard
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /* Reference applications:
27 * - IDA (interactive disassembler) full version 3.75. Works.
28 * - LYNX/W32. Works mostly, some keys crash it.
32 #include "wine/port.h"
47 #include "wine/server.h"
48 #include "wine/exception.h"
49 #include "wine/unicode.h"
50 #include "wine/debug.h"
52 #include "console_private.h"
53 #include "kernel_private.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(console
);
57 static UINT console_input_codepage
;
58 static UINT console_output_codepage
;
61 /* map input records to ASCII */
62 static void input_records_WtoA( INPUT_RECORD
*buffer
, int count
)
67 for (i
= 0; i
< count
; i
++)
69 if (buffer
[i
].EventType
!= KEY_EVENT
) continue;
70 WideCharToMultiByte( GetConsoleCP(), 0,
71 &buffer
[i
].Event
.KeyEvent
.uChar
.UnicodeChar
, 1, &ch
, 1, NULL
, NULL
);
72 buffer
[i
].Event
.KeyEvent
.uChar
.AsciiChar
= ch
;
76 /* map input records to Unicode */
77 static void input_records_AtoW( INPUT_RECORD
*buffer
, int count
)
82 for (i
= 0; i
< count
; i
++)
84 if (buffer
[i
].EventType
!= KEY_EVENT
) continue;
85 MultiByteToWideChar( GetConsoleCP(), 0,
86 &buffer
[i
].Event
.KeyEvent
.uChar
.AsciiChar
, 1, &ch
, 1 );
87 buffer
[i
].Event
.KeyEvent
.uChar
.UnicodeChar
= ch
;
91 /* map char infos to ASCII */
92 static void char_info_WtoA( CHAR_INFO
*buffer
, int count
)
98 WideCharToMultiByte( GetConsoleOutputCP(), 0, &buffer
->Char
.UnicodeChar
, 1,
100 buffer
->Char
.AsciiChar
= ch
;
105 /* map char infos to Unicode */
106 static void char_info_AtoW( CHAR_INFO
*buffer
, int count
)
112 MultiByteToWideChar( GetConsoleOutputCP(), 0, &buffer
->Char
.AsciiChar
, 1, &ch
, 1 );
113 buffer
->Char
.UnicodeChar
= ch
;
119 /******************************************************************************
120 * GetConsoleCP [KERNEL32.@] Returns the OEM code page for the console
125 UINT WINAPI
GetConsoleCP(VOID
)
127 if (!console_input_codepage
) console_input_codepage
= GetOEMCP();
128 return console_input_codepage
;
132 /******************************************************************************
133 * SetConsoleCP [KERNEL32.@]
135 BOOL WINAPI
SetConsoleCP(UINT cp
)
137 if (!IsValidCodePage( cp
)) return FALSE
;
138 console_input_codepage
= cp
;
143 /***********************************************************************
144 * GetConsoleOutputCP (KERNEL32.@)
146 UINT WINAPI
GetConsoleOutputCP(VOID
)
148 if (!console_output_codepage
) console_output_codepage
= GetOEMCP();
149 return console_output_codepage
;
153 /******************************************************************************
154 * SetConsoleOutputCP [KERNEL32.@] Set the output codepage used by the console
157 * cp [I] code page to set
163 BOOL WINAPI
SetConsoleOutputCP(UINT cp
)
165 if (!IsValidCodePage( cp
)) return FALSE
;
166 console_output_codepage
= cp
;
171 /******************************************************************************
172 * WriteConsoleInputA [KERNEL32.@]
174 BOOL WINAPI
WriteConsoleInputA( HANDLE handle
, const INPUT_RECORD
*buffer
,
175 DWORD count
, LPDWORD written
)
180 if (!(recW
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*recW
) ))) return FALSE
;
181 memcpy( recW
, buffer
, count
*sizeof(*recW
) );
182 input_records_AtoW( recW
, count
);
183 ret
= WriteConsoleInputW( handle
, recW
, count
, written
);
184 HeapFree( GetProcessHeap(), 0, recW
);
189 /******************************************************************************
190 * WriteConsoleInputW [KERNEL32.@]
192 BOOL WINAPI
WriteConsoleInputW( HANDLE handle
, const INPUT_RECORD
*buffer
,
193 DWORD count
, LPDWORD written
)
197 TRACE("(%p,%p,%ld,%p)\n", handle
, buffer
, count
, written
);
199 if (written
) *written
= 0;
200 SERVER_START_REQ( write_console_input
)
202 req
->handle
= console_handle_unmap(handle
);
203 wine_server_add_data( req
, buffer
, count
* sizeof(INPUT_RECORD
) );
204 if ((ret
= !wine_server_call_err( req
)) && written
)
205 *written
= reply
->written
;
213 /***********************************************************************
214 * WriteConsoleOutputA (KERNEL32.@)
216 BOOL WINAPI
WriteConsoleOutputA( HANDLE hConsoleOutput
, const CHAR_INFO
*lpBuffer
,
217 COORD size
, COORD coord
, LPSMALL_RECT region
)
221 COORD new_size
, new_coord
;
224 new_size
.X
= min( region
->Right
- region
->Left
+ 1, size
.X
- coord
.X
);
225 new_size
.Y
= min( region
->Bottom
- region
->Top
+ 1, size
.Y
- coord
.Y
);
227 if (new_size
.X
<= 0 || new_size
.Y
<= 0)
229 region
->Bottom
= region
->Top
+ new_size
.Y
- 1;
230 region
->Right
= region
->Left
+ new_size
.X
- 1;
234 /* only copy the useful rectangle */
235 if (!(ciw
= HeapAlloc( GetProcessHeap(), 0, sizeof(CHAR_INFO
) * new_size
.X
* new_size
.Y
)))
237 for (y
= 0; y
< new_size
.Y
; y
++)
239 memcpy( &ciw
[y
* new_size
.X
], &lpBuffer
[(y
+ coord
.Y
) * size
.X
+ coord
.X
],
240 new_size
.X
* sizeof(CHAR_INFO
) );
241 char_info_AtoW( ciw
, new_size
.X
);
243 new_coord
.X
= new_coord
.Y
= 0;
244 ret
= WriteConsoleOutputW( hConsoleOutput
, ciw
, new_size
, new_coord
, region
);
245 if (ciw
) HeapFree( GetProcessHeap(), 0, ciw
);
250 /***********************************************************************
251 * WriteConsoleOutputW (KERNEL32.@)
253 BOOL WINAPI
WriteConsoleOutputW( HANDLE hConsoleOutput
, const CHAR_INFO
*lpBuffer
,
254 COORD size
, COORD coord
, LPSMALL_RECT region
)
256 int width
, height
, y
;
259 TRACE("(%p,%p,(%d,%d),(%d,%d),(%d,%dx%d,%d)\n",
260 hConsoleOutput
, lpBuffer
, size
.X
, size
.Y
, coord
.X
, coord
.Y
,
261 region
->Left
, region
->Top
, region
->Right
, region
->Bottom
);
263 width
= min( region
->Right
- region
->Left
+ 1, size
.X
- coord
.X
);
264 height
= min( region
->Bottom
- region
->Top
+ 1, size
.Y
- coord
.Y
);
266 if (width
> 0 && height
> 0)
268 for (y
= 0; y
< height
; y
++)
270 SERVER_START_REQ( write_console_output
)
272 req
->handle
= console_handle_unmap(hConsoleOutput
);
273 req
->x
= region
->Left
;
274 req
->y
= region
->Top
+ y
;
275 req
->mode
= CHAR_INFO_MODE_TEXTATTR
;
277 wine_server_add_data( req
, &lpBuffer
[(y
+ coord
.Y
) * size
.X
+ coord
.X
],
278 width
* sizeof(CHAR_INFO
));
279 if ((ret
= !wine_server_call_err( req
)))
281 width
= min( width
, reply
->width
- region
->Left
);
282 height
= min( height
, reply
->height
- region
->Top
);
289 region
->Bottom
= region
->Top
+ height
- 1;
290 region
->Right
= region
->Left
+ width
- 1;
295 /******************************************************************************
296 * WriteConsoleOutputCharacterA [KERNEL32.@] Copies character to consecutive
297 * cells in the console screen buffer
300 * hConsoleOutput [I] Handle to screen buffer
301 * str [I] Pointer to buffer with chars to write
302 * length [I] Number of cells to write to
303 * coord [I] Coords of first cell
304 * lpNumCharsWritten [O] Pointer to number of cells written
306 BOOL WINAPI
WriteConsoleOutputCharacterA( HANDLE hConsoleOutput
, LPCSTR str
, DWORD length
,
307 COORD coord
, LPDWORD lpNumCharsWritten
)
313 TRACE("(%p,%s,%ld,%dx%d,%p)\n", hConsoleOutput
,
314 debugstr_an(str
, length
), length
, coord
.X
, coord
.Y
, lpNumCharsWritten
);
316 lenW
= MultiByteToWideChar( GetConsoleOutputCP(), 0, str
, length
, NULL
, 0 );
318 if (lpNumCharsWritten
) *lpNumCharsWritten
= 0;
320 if (!(strW
= HeapAlloc( GetProcessHeap(), 0, lenW
* sizeof(WCHAR
) ))) return FALSE
;
321 MultiByteToWideChar( GetConsoleOutputCP(), 0, str
, length
, strW
, lenW
);
323 ret
= WriteConsoleOutputCharacterW( hConsoleOutput
, strW
, lenW
, coord
, lpNumCharsWritten
);
324 HeapFree( GetProcessHeap(), 0, strW
);
329 /******************************************************************************
330 * WriteConsoleOutputAttribute [KERNEL32.@] Sets attributes for some cells in
331 * the console screen buffer
334 * hConsoleOutput [I] Handle to screen buffer
335 * attr [I] Pointer to buffer with write attributes
336 * length [I] Number of cells to write to
337 * coord [I] Coords of first cell
338 * lpNumAttrsWritten [O] Pointer to number of cells written
345 BOOL WINAPI
WriteConsoleOutputAttribute( HANDLE hConsoleOutput
, CONST WORD
*attr
, DWORD length
,
346 COORD coord
, LPDWORD lpNumAttrsWritten
)
350 TRACE("(%p,%p,%ld,%dx%d,%p)\n", hConsoleOutput
,attr
,length
,coord
.X
,coord
.Y
,lpNumAttrsWritten
);
352 SERVER_START_REQ( write_console_output
)
354 req
->handle
= console_handle_unmap(hConsoleOutput
);
357 req
->mode
= CHAR_INFO_MODE_ATTR
;
359 wine_server_add_data( req
, attr
, length
* sizeof(WORD
) );
360 if ((ret
= !wine_server_call_err( req
)))
362 if (lpNumAttrsWritten
) *lpNumAttrsWritten
= reply
->written
;
370 /******************************************************************************
371 * FillConsoleOutputCharacterA [KERNEL32.@]
374 * hConsoleOutput [I] Handle to screen buffer
375 * ch [I] Character to write
376 * length [I] Number of cells to write to
377 * coord [I] Coords of first cell
378 * lpNumCharsWritten [O] Pointer to number of cells written
384 BOOL WINAPI
FillConsoleOutputCharacterA( HANDLE hConsoleOutput
, CHAR ch
, DWORD length
,
385 COORD coord
, LPDWORD lpNumCharsWritten
)
389 MultiByteToWideChar( GetConsoleOutputCP(), 0, &ch
, 1, &wch
, 1 );
390 return FillConsoleOutputCharacterW(hConsoleOutput
, wch
, length
, coord
, lpNumCharsWritten
);
394 /******************************************************************************
395 * FillConsoleOutputCharacterW [KERNEL32.@] Writes characters to console
398 * hConsoleOutput [I] Handle to screen buffer
399 * ch [I] Character to write
400 * length [I] Number of cells to write to
401 * coord [I] Coords of first cell
402 * lpNumCharsWritten [O] Pointer to number of cells written
408 BOOL WINAPI
FillConsoleOutputCharacterW( HANDLE hConsoleOutput
, WCHAR ch
, DWORD length
,
409 COORD coord
, LPDWORD lpNumCharsWritten
)
413 TRACE("(%p,%s,%ld,(%dx%d),%p)\n",
414 hConsoleOutput
, debugstr_wn(&ch
, 1), length
, coord
.X
, coord
.Y
, lpNumCharsWritten
);
416 SERVER_START_REQ( fill_console_output
)
418 req
->handle
= console_handle_unmap(hConsoleOutput
);
421 req
->mode
= CHAR_INFO_MODE_TEXT
;
425 if ((ret
= !wine_server_call_err( req
)))
427 if (lpNumCharsWritten
) *lpNumCharsWritten
= reply
->written
;
435 /******************************************************************************
436 * FillConsoleOutputAttribute [KERNEL32.@] Sets attributes for console
439 * hConsoleOutput [I] Handle to screen buffer
440 * attr [I] Color attribute to write
441 * length [I] Number of cells to write to
442 * coord [I] Coords of first cell
443 * lpNumAttrsWritten [O] Pointer to number of cells written
449 BOOL WINAPI
FillConsoleOutputAttribute( HANDLE hConsoleOutput
, WORD attr
, DWORD length
,
450 COORD coord
, LPDWORD lpNumAttrsWritten
)
454 TRACE("(%p,%d,%ld,(%dx%d),%p)\n",
455 hConsoleOutput
, attr
, length
, coord
.X
, coord
.Y
, lpNumAttrsWritten
);
457 SERVER_START_REQ( fill_console_output
)
459 req
->handle
= console_handle_unmap(hConsoleOutput
);
462 req
->mode
= CHAR_INFO_MODE_ATTR
;
464 req
->data
.attr
= attr
;
466 if ((ret
= !wine_server_call_err( req
)))
468 if (lpNumAttrsWritten
) *lpNumAttrsWritten
= reply
->written
;
476 /******************************************************************************
477 * ReadConsoleOutputCharacterA [KERNEL32.@]
480 BOOL WINAPI
ReadConsoleOutputCharacterA(HANDLE hConsoleOutput
, LPSTR lpstr
, DWORD count
,
481 COORD coord
, LPDWORD read_count
)
485 LPWSTR wptr
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(WCHAR
));
487 if (read_count
) *read_count
= 0;
488 if (!wptr
) return FALSE
;
490 if ((ret
= ReadConsoleOutputCharacterW( hConsoleOutput
, wptr
, count
, coord
, &read
)))
492 read
= WideCharToMultiByte( GetConsoleOutputCP(), 0, wptr
, read
, lpstr
, count
, NULL
, NULL
);
493 if (read_count
) *read_count
= read
;
495 HeapFree( GetProcessHeap(), 0, wptr
);
500 /******************************************************************************
501 * ReadConsoleOutputCharacterW [KERNEL32.@]
504 BOOL WINAPI
ReadConsoleOutputCharacterW( HANDLE hConsoleOutput
, LPWSTR buffer
, DWORD count
,
505 COORD coord
, LPDWORD read_count
)
509 TRACE( "(%p,%p,%ld,%dx%d,%p)\n", hConsoleOutput
, buffer
, count
, coord
.X
, coord
.Y
, read_count
);
511 SERVER_START_REQ( read_console_output
)
513 req
->handle
= console_handle_unmap(hConsoleOutput
);
516 req
->mode
= CHAR_INFO_MODE_TEXT
;
518 wine_server_set_reply( req
, buffer
, count
* sizeof(WCHAR
) );
519 if ((ret
= !wine_server_call_err( req
)))
521 if (read_count
) *read_count
= wine_server_reply_size(reply
) / sizeof(WCHAR
);
529 /******************************************************************************
530 * ReadConsoleOutputAttribute [KERNEL32.@]
532 BOOL WINAPI
ReadConsoleOutputAttribute(HANDLE hConsoleOutput
, LPWORD lpAttribute
, DWORD length
,
533 COORD coord
, LPDWORD read_count
)
537 TRACE("(%p,%p,%ld,%dx%d,%p)\n",
538 hConsoleOutput
, lpAttribute
, length
, coord
.X
, coord
.Y
, read_count
);
540 SERVER_START_REQ( read_console_output
)
542 req
->handle
= console_handle_unmap(hConsoleOutput
);
545 req
->mode
= CHAR_INFO_MODE_ATTR
;
547 wine_server_set_reply( req
, lpAttribute
, length
* sizeof(WORD
) );
548 if ((ret
= !wine_server_call_err( req
)))
550 if (read_count
) *read_count
= wine_server_reply_size(reply
) / sizeof(WORD
);
558 /******************************************************************************
559 * ReadConsoleOutputA [KERNEL32.@]
562 BOOL WINAPI
ReadConsoleOutputA( HANDLE hConsoleOutput
, LPCHAR_INFO lpBuffer
, COORD size
,
563 COORD coord
, LPSMALL_RECT region
)
568 ret
= ReadConsoleOutputW( hConsoleOutput
, lpBuffer
, size
, coord
, region
);
569 if (ret
&& region
->Right
>= region
->Left
)
571 for (y
= 0; y
<= region
->Bottom
- region
->Top
; y
++)
573 char_info_WtoA( &lpBuffer
[(coord
.Y
+ y
) * size
.X
+ coord
.X
],
574 region
->Right
- region
->Left
+ 1 );
581 /******************************************************************************
582 * ReadConsoleOutputW [KERNEL32.@]
584 * NOTE: The NT4 (sp5) kernel crashes on me if size is (0,0). I don't
585 * think we need to be *that* compatible. -- AJ
587 BOOL WINAPI
ReadConsoleOutputW( HANDLE hConsoleOutput
, LPCHAR_INFO lpBuffer
, COORD size
,
588 COORD coord
, LPSMALL_RECT region
)
590 int width
, height
, y
;
593 width
= min( region
->Right
- region
->Left
+ 1, size
.X
- coord
.X
);
594 height
= min( region
->Bottom
- region
->Top
+ 1, size
.Y
- coord
.Y
);
596 if (width
> 0 && height
> 0)
598 for (y
= 0; y
< height
; y
++)
600 SERVER_START_REQ( read_console_output
)
602 req
->handle
= console_handle_unmap(hConsoleOutput
);
603 req
->x
= region
->Left
;
604 req
->y
= region
->Top
+ y
;
605 req
->mode
= CHAR_INFO_MODE_TEXTATTR
;
607 wine_server_set_reply( req
, &lpBuffer
[(y
+coord
.Y
) * size
.X
+ coord
.X
],
608 width
* sizeof(CHAR_INFO
) );
609 if ((ret
= !wine_server_call_err( req
)))
611 width
= min( width
, reply
->width
- region
->Left
);
612 height
= min( height
, reply
->height
- region
->Top
);
619 region
->Bottom
= region
->Top
+ height
- 1;
620 region
->Right
= region
->Left
+ width
- 1;
625 /******************************************************************************
626 * ReadConsoleInputA [KERNEL32.@] Reads data from a console
629 * handle [I] Handle to console input buffer
630 * buffer [O] Address of buffer for read data
631 * count [I] Number of records to read
632 * pRead [O] Address of number of records read
638 BOOL WINAPI
ReadConsoleInputA( HANDLE handle
, LPINPUT_RECORD buffer
, DWORD count
, LPDWORD pRead
)
642 if (!ReadConsoleInputW( handle
, buffer
, count
, &read
)) return FALSE
;
643 input_records_WtoA( buffer
, read
);
644 if (pRead
) *pRead
= read
;
649 /***********************************************************************
650 * PeekConsoleInputA (KERNEL32.@)
652 * Gets 'count' first events (or less) from input queue.
654 BOOL WINAPI
PeekConsoleInputA( HANDLE handle
, LPINPUT_RECORD buffer
, DWORD count
, LPDWORD pRead
)
658 if (!PeekConsoleInputW( handle
, buffer
, count
, &read
)) return FALSE
;
659 input_records_WtoA( buffer
, read
);
660 if (pRead
) *pRead
= read
;
665 /***********************************************************************
666 * PeekConsoleInputW (KERNEL32.@)
668 BOOL WINAPI
PeekConsoleInputW( HANDLE handle
, LPINPUT_RECORD buffer
, DWORD count
, LPDWORD read
)
671 SERVER_START_REQ( read_console_input
)
673 req
->handle
= console_handle_unmap(handle
);
675 wine_server_set_reply( req
, buffer
, count
* sizeof(INPUT_RECORD
) );
676 if ((ret
= !wine_server_call_err( req
)))
678 if (read
) *read
= count
? reply
->read
: 0;
686 /***********************************************************************
687 * GetNumberOfConsoleInputEvents (KERNEL32.@)
689 BOOL WINAPI
GetNumberOfConsoleInputEvents( HANDLE handle
, LPDWORD nrofevents
)
692 SERVER_START_REQ( read_console_input
)
694 req
->handle
= console_handle_unmap(handle
);
696 if ((ret
= !wine_server_call_err( req
)))
698 if (nrofevents
) *nrofevents
= reply
->read
;
706 /******************************************************************************
709 * Helper function for ReadConsole, ReadConsoleInput and FlushConsoleInputBuffer
712 * 0 for error, 1 for no INPUT_RECORD ready, 2 with INPUT_RECORD ready
714 enum read_console_input_return
{rci_error
= 0, rci_timeout
= 1, rci_gotone
= 2};
715 static enum read_console_input_return
read_console_input(HANDLE handle
, LPINPUT_RECORD ir
, DWORD timeout
)
717 enum read_console_input_return ret
;
719 if (WaitForSingleObject(GetConsoleInputWaitHandle(), timeout
) != WAIT_OBJECT_0
)
721 SERVER_START_REQ( read_console_input
)
723 req
->handle
= console_handle_unmap(handle
);
725 wine_server_set_reply( req
, ir
, sizeof(INPUT_RECORD
) );
726 if (wine_server_call_err( req
) || !reply
->read
) ret
= rci_error
;
727 else ret
= rci_gotone
;
735 /***********************************************************************
736 * FlushConsoleInputBuffer (KERNEL32.@)
738 BOOL WINAPI
FlushConsoleInputBuffer( HANDLE handle
)
740 enum read_console_input_return last
;
743 while ((last
= read_console_input(handle
, &ir
, 0)) == rci_gotone
);
745 return last
== rci_timeout
;
749 /***********************************************************************
750 * SetConsoleTitleA (KERNEL32.@)
752 BOOL WINAPI
SetConsoleTitleA( LPCSTR title
)
757 DWORD len
= MultiByteToWideChar( GetConsoleOutputCP(), 0, title
, -1, NULL
, 0 );
758 if (!(titleW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
)))) return FALSE
;
759 MultiByteToWideChar( GetConsoleOutputCP(), 0, title
, -1, titleW
, len
);
760 ret
= SetConsoleTitleW(titleW
);
761 HeapFree(GetProcessHeap(), 0, titleW
);
766 /***********************************************************************
767 * GetConsoleTitleA (KERNEL32.@)
769 DWORD WINAPI
GetConsoleTitleA(LPSTR title
, DWORD size
)
771 WCHAR
*ptr
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * size
);
775 ret
= GetConsoleTitleW( ptr
, size
);
778 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr
, ret
+ 1, title
, size
, NULL
, NULL
);
781 HeapFree(GetProcessHeap(), 0, ptr
);
786 /******************************************************************************
787 * GetConsoleTitleW [KERNEL32.@] Retrieves title string for console
790 * title [O] Address of buffer for title
791 * size [I] Size of buffer
794 * Success: Length of string copied
797 DWORD WINAPI
GetConsoleTitleW(LPWSTR title
, DWORD size
)
801 SERVER_START_REQ( get_console_input_info
)
804 wine_server_set_reply( req
, title
, (size
-1) * sizeof(WCHAR
) );
805 if (!wine_server_call_err( req
))
807 ret
= wine_server_reply_size(reply
) / sizeof(WCHAR
);
816 /***********************************************************************
817 * GetLargestConsoleWindowSize (KERNEL32.@)
820 * This should return a COORD, but calling convention for returning
821 * structures is different between Windows and gcc on i386.
826 #undef GetLargestConsoleWindowSize
827 DWORD WINAPI
GetLargestConsoleWindowSize(HANDLE hConsoleOutput
)
837 #endif /* defined(__i386__) */
840 /***********************************************************************
841 * GetLargestConsoleWindowSize (KERNEL32.@)
844 * This should return a COORD, but calling convention for returning
845 * structures is different between Windows and gcc on i386.
850 COORD WINAPI
GetLargestConsoleWindowSize(HANDLE hConsoleOutput
)
857 #endif /* defined(__i386__) */
859 static WCHAR
* S_EditString
/* = NULL */;
860 static unsigned S_EditStrPos
/* = 0 */;
862 /***********************************************************************
863 * FreeConsole (KERNEL32.@)
865 BOOL WINAPI
FreeConsole(VOID
)
869 SERVER_START_REQ(free_console
)
871 ret
= !wine_server_call_err( req
);
877 /******************************************************************
878 * start_console_renderer
880 * helper for AllocConsole
881 * starts the renderer process
883 static BOOL
start_console_renderer_helper(const char* appname
, STARTUPINFOA
* si
,
888 PROCESS_INFORMATION pi
;
890 /* FIXME: use dynamic allocation for most of the buffers below */
891 ret
= snprintf(buffer
, sizeof(buffer
), "%s --use-event=%d", appname
, (INT
)hEvent
);
892 if ((ret
> -1) && (ret
< sizeof(buffer
)) &&
893 CreateProcessA(NULL
, buffer
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
,
894 NULL
, NULL
, si
, &pi
))
896 if (WaitForSingleObject(hEvent
, INFINITE
) != WAIT_OBJECT_0
) return FALSE
;
898 TRACE("Started wineconsole pid=%08lx tid=%08lx\n",
899 pi
.dwProcessId
, pi
.dwThreadId
);
906 static BOOL
start_console_renderer(STARTUPINFOA
* si
)
910 OBJECT_ATTRIBUTES attr
;
913 attr
.Length
= sizeof(attr
);
914 attr
.RootDirectory
= 0;
915 attr
.Attributes
= OBJ_INHERIT
;
916 attr
.ObjectName
= NULL
;
917 attr
.SecurityDescriptor
= NULL
;
918 attr
.SecurityQualityOfService
= NULL
;
920 NtCreateEvent(&hEvent
, EVENT_ALL_ACCESS
, &attr
, TRUE
, FALSE
);
921 if (!hEvent
) return FALSE
;
923 /* first try environment variable */
924 if ((p
= getenv("WINECONSOLE")) != NULL
)
926 ret
= start_console_renderer_helper(p
, si
, hEvent
);
928 ERR("Couldn't launch Wine console from WINECONSOLE env var (%s)... "
929 "trying default access\n", p
);
932 /* then try the regular PATH */
934 ret
= start_console_renderer_helper("wineconsole", si
, hEvent
);
940 /***********************************************************************
941 * AllocConsole (KERNEL32.@)
943 * creates an xterm with a pty to our program
945 BOOL WINAPI
AllocConsole(void)
947 HANDLE handle_in
= INVALID_HANDLE_VALUE
;
948 HANDLE handle_out
= INVALID_HANDLE_VALUE
;
949 HANDLE handle_err
= INVALID_HANDLE_VALUE
;
950 STARTUPINFOA siCurrent
;
951 STARTUPINFOA siConsole
;
953 SECURITY_ATTRIBUTES sa
;
957 handle_in
= CreateFileA( "CONIN$", GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,
958 0, NULL
, OPEN_EXISTING
, 0, 0 );
960 if (handle_in
!= INVALID_HANDLE_VALUE
)
962 /* we already have a console opened on this process, don't create a new one */
963 CloseHandle(handle_in
);
967 GetStartupInfoA(&siCurrent
);
969 memset(&siConsole
, 0, sizeof(siConsole
));
970 siConsole
.cb
= sizeof(siConsole
);
971 /* setup a view arguments for wineconsole (it'll use them as default values) */
972 if (siCurrent
.dwFlags
& STARTF_USECOUNTCHARS
)
974 siConsole
.dwFlags
|= STARTF_USECOUNTCHARS
;
975 siConsole
.dwXCountChars
= siCurrent
.dwXCountChars
;
976 siConsole
.dwYCountChars
= siCurrent
.dwYCountChars
;
978 if (siCurrent
.dwFlags
& STARTF_USEFILLATTRIBUTE
)
980 siConsole
.dwFlags
|= STARTF_USEFILLATTRIBUTE
;
981 siConsole
.dwFillAttribute
= siCurrent
.dwFillAttribute
;
983 /* FIXME (should pass the unicode form) */
984 if (siCurrent
.lpTitle
)
985 siConsole
.lpTitle
= siCurrent
.lpTitle
;
986 else if (GetModuleFileNameA(0, buffer
, sizeof(buffer
)))
987 siConsole
.lpTitle
= buffer
;
989 if (!start_console_renderer(&siConsole
))
992 /* all std I/O handles are inheritable by default */
993 sa
.nLength
= sizeof(sa
);
994 sa
.lpSecurityDescriptor
= NULL
;
995 sa
.bInheritHandle
= TRUE
;
997 handle_in
= CreateFileA( "CONIN$", GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,
998 0, &sa
, OPEN_EXISTING
, 0, 0 );
999 if (handle_in
== INVALID_HANDLE_VALUE
) goto the_end
;
1001 handle_out
= CreateFileA( "CONOUT$", GENERIC_READ
|GENERIC_WRITE
,
1002 0, &sa
, OPEN_EXISTING
, 0, 0 );
1003 if (handle_out
== INVALID_HANDLE_VALUE
) goto the_end
;
1005 if (!DuplicateHandle(GetCurrentProcess(), handle_out
, GetCurrentProcess(), &handle_err
,
1006 0, TRUE
, DUPLICATE_SAME_ACCESS
))
1009 /* NT resets the STD_*_HANDLEs on console alloc */
1010 SetStdHandle(STD_INPUT_HANDLE
, handle_in
);
1011 SetStdHandle(STD_OUTPUT_HANDLE
, handle_out
);
1012 SetStdHandle(STD_ERROR_HANDLE
, handle_err
);
1014 SetLastError(ERROR_SUCCESS
);
1019 ERR("Can't allocate console\n");
1020 if (handle_in
!= INVALID_HANDLE_VALUE
) CloseHandle(handle_in
);
1021 if (handle_out
!= INVALID_HANDLE_VALUE
) CloseHandle(handle_out
);
1022 if (handle_err
!= INVALID_HANDLE_VALUE
) CloseHandle(handle_err
);
1028 /***********************************************************************
1029 * ReadConsoleA (KERNEL32.@)
1031 BOOL WINAPI
ReadConsoleA(HANDLE hConsoleInput
, LPVOID lpBuffer
, DWORD nNumberOfCharsToRead
,
1032 LPDWORD lpNumberOfCharsRead
, LPVOID lpReserved
)
1034 LPWSTR ptr
= HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead
* sizeof(WCHAR
));
1038 if ((ret
= ReadConsoleW(hConsoleInput
, ptr
, nNumberOfCharsToRead
, &ncr
, NULL
)))
1039 ncr
= WideCharToMultiByte(CP_ACP
, 0, ptr
, ncr
, lpBuffer
, nNumberOfCharsToRead
, NULL
, NULL
);
1041 if (lpNumberOfCharsRead
) *lpNumberOfCharsRead
= ncr
;
1042 HeapFree(GetProcessHeap(), 0, ptr
);
1047 /***********************************************************************
1048 * ReadConsoleW (KERNEL32.@)
1050 BOOL WINAPI
ReadConsoleW(HANDLE hConsoleInput
, LPVOID lpBuffer
,
1051 DWORD nNumberOfCharsToRead
, LPDWORD lpNumberOfCharsRead
, LPVOID lpReserved
)
1054 LPWSTR xbuf
= (LPWSTR
)lpBuffer
;
1057 TRACE("(%p,%p,%ld,%p,%p)\n",
1058 hConsoleInput
, lpBuffer
, nNumberOfCharsToRead
, lpNumberOfCharsRead
, lpReserved
);
1060 if (!GetConsoleMode(hConsoleInput
, &mode
))
1063 if (mode
& ENABLE_LINE_INPUT
)
1065 if (!S_EditString
|| S_EditString
[S_EditStrPos
] == 0)
1067 if (S_EditString
) HeapFree(GetProcessHeap(), 0, S_EditString
);
1068 if (!(S_EditString
= CONSOLE_Readline(hConsoleInput
)))
1072 charsread
= lstrlenW(&S_EditString
[S_EditStrPos
]);
1073 if (charsread
> nNumberOfCharsToRead
) charsread
= nNumberOfCharsToRead
;
1074 memcpy(xbuf
, &S_EditString
[S_EditStrPos
], charsread
* sizeof(WCHAR
));
1075 S_EditStrPos
+= charsread
;
1080 DWORD timeout
= INFINITE
;
1082 /* FIXME: should we read at least 1 char? The SDK does not say */
1083 /* wait for at least one available input record (it doesn't mean we'll have
1084 * chars stored in xbuf...)
1089 if (read_console_input(hConsoleInput
, &ir
, timeout
) != rci_gotone
) break;
1091 if (ir
.EventType
== KEY_EVENT
&& ir
.Event
.KeyEvent
.bKeyDown
&&
1092 ir
.Event
.KeyEvent
.uChar
.UnicodeChar
&&
1093 !(ir
.Event
.KeyEvent
.dwControlKeyState
& ENHANCED_KEY
))
1095 xbuf
[charsread
++] = ir
.Event
.KeyEvent
.uChar
.UnicodeChar
;
1097 } while (charsread
< nNumberOfCharsToRead
);
1098 /* nothing has been read */
1099 if (timeout
== INFINITE
) return FALSE
;
1102 if (lpNumberOfCharsRead
) *lpNumberOfCharsRead
= charsread
;
1108 /***********************************************************************
1109 * ReadConsoleInputW (KERNEL32.@)
1111 BOOL WINAPI
ReadConsoleInputW(HANDLE hConsoleInput
, LPINPUT_RECORD lpBuffer
,
1112 DWORD nLength
, LPDWORD lpNumberOfEventsRead
)
1115 DWORD timeout
= INFINITE
;
1119 if (lpNumberOfEventsRead
) *lpNumberOfEventsRead
= 0;
1123 /* loop until we get at least one event */
1124 while (read_console_input(hConsoleInput
, &lpBuffer
[idx
], timeout
) == rci_gotone
&&
1128 if (lpNumberOfEventsRead
) *lpNumberOfEventsRead
= idx
;
1133 /******************************************************************************
1134 * WriteConsoleOutputCharacterW [KERNEL32.@] Copies character to consecutive
1135 * cells in the console screen buffer
1138 * hConsoleOutput [I] Handle to screen buffer
1139 * str [I] Pointer to buffer with chars to write
1140 * length [I] Number of cells to write to
1141 * coord [I] Coords of first cell
1142 * lpNumCharsWritten [O] Pointer to number of cells written
1149 BOOL WINAPI
WriteConsoleOutputCharacterW( HANDLE hConsoleOutput
, LPCWSTR str
, DWORD length
,
1150 COORD coord
, LPDWORD lpNumCharsWritten
)
1154 TRACE("(%p,%s,%ld,%dx%d,%p)\n", hConsoleOutput
,
1155 debugstr_wn(str
, length
), length
, coord
.X
, coord
.Y
, lpNumCharsWritten
);
1157 SERVER_START_REQ( write_console_output
)
1159 req
->handle
= console_handle_unmap(hConsoleOutput
);
1162 req
->mode
= CHAR_INFO_MODE_TEXT
;
1164 wine_server_add_data( req
, str
, length
* sizeof(WCHAR
) );
1165 if ((ret
= !wine_server_call_err( req
)))
1167 if (lpNumCharsWritten
) *lpNumCharsWritten
= reply
->written
;
1175 /******************************************************************************
1176 * SetConsoleTitleW [KERNEL32.@] Sets title bar string for console
1179 * title [I] Address of new title
1185 BOOL WINAPI
SetConsoleTitleW(LPCWSTR title
)
1189 SERVER_START_REQ( set_console_input_info
)
1192 req
->mask
= SET_CONSOLE_INPUT_INFO_TITLE
;
1193 wine_server_add_data( req
, title
, strlenW(title
) * sizeof(WCHAR
) );
1194 ret
= !wine_server_call_err( req
);
1201 /***********************************************************************
1202 * GetNumberOfConsoleMouseButtons (KERNEL32.@)
1204 BOOL WINAPI
GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons
)
1206 FIXME("(%p): stub\n", nrofbuttons
);
1211 /******************************************************************************
1212 * SetConsoleInputExeNameW [KERNEL32.@]
1217 BOOL WINAPI
SetConsoleInputExeNameW(LPCWSTR name
)
1219 FIXME("(%s): stub!\n", debugstr_w(name
));
1221 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1225 /******************************************************************************
1226 * SetConsoleInputExeNameA [KERNEL32.@]
1231 BOOL WINAPI
SetConsoleInputExeNameA(LPCSTR name
)
1233 int len
= MultiByteToWideChar(CP_ACP
, 0, name
, -1, NULL
, 0);
1234 LPWSTR xptr
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1237 if (!xptr
) return FALSE
;
1239 MultiByteToWideChar(CP_ACP
, 0, name
, -1, xptr
, len
);
1240 ret
= SetConsoleInputExeNameW(xptr
);
1241 HeapFree(GetProcessHeap(), 0, xptr
);
1246 /******************************************************************
1247 * CONSOLE_DefaultHandler
1249 * Final control event handler
1251 static BOOL WINAPI
CONSOLE_DefaultHandler(DWORD dwCtrlType
)
1253 FIXME("Terminating process %lx on event %lx\n", GetCurrentProcessId(), dwCtrlType
);
1255 /* should never go here */
1259 /******************************************************************************
1260 * SetConsoleCtrlHandler [KERNEL32.@] Adds function to calling process list
1263 * func [I] Address of handler function
1264 * add [I] Handler to add or remove
1271 * James Sutherland (JamesSutherland@gmx.de)
1272 * Added global variables console_ignore_ctrl_c and handlers[]
1273 * Does not yet do any error checking, or set LastError if failed.
1274 * This doesn't yet matter, since these handlers are not yet called...!
1277 struct ConsoleHandler
{
1278 PHANDLER_ROUTINE handler
;
1279 struct ConsoleHandler
* next
;
1282 static unsigned int CONSOLE_IgnoreCtrlC
= 0; /* FIXME: this should be inherited somehow */
1283 static struct ConsoleHandler CONSOLE_DefaultConsoleHandler
= {CONSOLE_DefaultHandler
, NULL
};
1284 static struct ConsoleHandler
* CONSOLE_Handlers
= &CONSOLE_DefaultConsoleHandler
;
1286 static CRITICAL_SECTION CONSOLE_CritSect
;
1287 static CRITICAL_SECTION_DEBUG critsect_debug
=
1289 0, 0, &CONSOLE_CritSect
,
1290 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
1291 0, 0, { 0, (DWORD
)(__FILE__
": CONSOLE_CritSect") }
1293 static CRITICAL_SECTION CONSOLE_CritSect
= { &critsect_debug
, -1, 0, 0, 0, 0 };
1295 /*****************************************************************************/
1297 BOOL WINAPI
SetConsoleCtrlHandler(PHANDLER_ROUTINE func
, BOOL add
)
1301 FIXME("(%p,%i) - no error checking or testing yet\n", func
, add
);
1305 CONSOLE_IgnoreCtrlC
= add
;
1309 struct ConsoleHandler
* ch
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct ConsoleHandler
));
1311 if (!ch
) return FALSE
;
1313 RtlEnterCriticalSection(&CONSOLE_CritSect
);
1314 ch
->next
= CONSOLE_Handlers
;
1315 CONSOLE_Handlers
= ch
;
1316 RtlLeaveCriticalSection(&CONSOLE_CritSect
);
1320 struct ConsoleHandler
** ch
;
1321 RtlEnterCriticalSection(&CONSOLE_CritSect
);
1322 for (ch
= &CONSOLE_Handlers
; *ch
; *ch
= (*ch
)->next
)
1324 if ((*ch
)->handler
== func
) break;
1328 struct ConsoleHandler
* rch
= *ch
;
1331 if (rch
== &CONSOLE_DefaultConsoleHandler
)
1333 ERR("Who's trying to remove default handler???\n");
1340 HeapFree(GetProcessHeap(), 0, rch
);
1345 WARN("Attempt to remove non-installed CtrlHandler %p\n", func
);
1348 RtlLeaveCriticalSection(&CONSOLE_CritSect
);
1353 static WINE_EXCEPTION_FILTER(CONSOLE_CtrlEventHandler
)
1355 TRACE("(%lx)\n", GetExceptionCode());
1356 return EXCEPTION_EXECUTE_HANDLER
;
1359 static DWORD WINAPI
CONSOLE_HandleCtrlCEntry(void* pmt
)
1361 struct ConsoleHandler
* ch
;
1363 RtlEnterCriticalSection(&CONSOLE_CritSect
);
1364 /* the debugger didn't continue... so, pass to ctrl handlers */
1365 for (ch
= CONSOLE_Handlers
; ch
; ch
= ch
->next
)
1367 if (ch
->handler((DWORD
)pmt
)) break;
1369 RtlLeaveCriticalSection(&CONSOLE_CritSect
);
1373 /******************************************************************
1374 * CONSOLE_HandleCtrlC
1376 * Check whether the shall manipulate CtrlC events
1378 int CONSOLE_HandleCtrlC(unsigned sig
)
1380 /* FIXME: better test whether a console is attached to this process ??? */
1381 extern unsigned CONSOLE_GetNumHistoryEntries(void);
1382 if (CONSOLE_GetNumHistoryEntries() == (unsigned)-1) return 0;
1383 if (CONSOLE_IgnoreCtrlC
) return 1;
1385 /* try to pass the exception to the debugger
1386 * if it continues, there's nothing more to do
1387 * otherwise, we need to send the ctrl-event to the handlers
1391 RaiseException( DBG_CONTROL_C
, 0, 0, NULL
);
1393 __EXCEPT(CONSOLE_CtrlEventHandler
)
1395 /* Create a separate thread to signal all the events. This would allow to
1396 * synchronize between setting the handlers and actually calling them
1398 CreateThread(NULL
, 0, CONSOLE_HandleCtrlCEntry
, (void*)CTRL_C_EVENT
, 0, NULL
);
1404 /******************************************************************************
1405 * GenerateConsoleCtrlEvent [KERNEL32.@] Simulate a CTRL-C or CTRL-BREAK
1408 * dwCtrlEvent [I] Type of event
1409 * dwProcessGroupID [I] Process group ID to send event to
1413 * Failure: False (and *should* [but doesn't] set LastError)
1415 BOOL WINAPI
GenerateConsoleCtrlEvent(DWORD dwCtrlEvent
,
1416 DWORD dwProcessGroupID
)
1420 TRACE("(%ld, %ld)\n", dwCtrlEvent
, dwProcessGroupID
);
1422 if (dwCtrlEvent
!= CTRL_C_EVENT
&& dwCtrlEvent
!= CTRL_BREAK_EVENT
)
1424 ERR("Invalid event %ld for PGID %ld\n", dwCtrlEvent
, dwProcessGroupID
);
1428 SERVER_START_REQ( send_console_signal
)
1430 req
->signal
= dwCtrlEvent
;
1431 req
->group_id
= dwProcessGroupID
;
1432 ret
= !wine_server_call_err( req
);
1440 /******************************************************************************
1441 * CreateConsoleScreenBuffer [KERNEL32.@] Creates a console screen buffer
1444 * dwDesiredAccess [I] Access flag
1445 * dwShareMode [I] Buffer share mode
1446 * sa [I] Security attributes
1447 * dwFlags [I] Type of buffer to create
1448 * lpScreenBufferData [I] Reserved
1451 * Should call SetLastError
1454 * Success: Handle to new console screen buffer
1455 * Failure: INVALID_HANDLE_VALUE
1457 HANDLE WINAPI
CreateConsoleScreenBuffer(DWORD dwDesiredAccess
, DWORD dwShareMode
,
1458 LPSECURITY_ATTRIBUTES sa
, DWORD dwFlags
,
1459 LPVOID lpScreenBufferData
)
1461 HANDLE ret
= INVALID_HANDLE_VALUE
;
1463 TRACE("(%ld,%ld,%p,%ld,%p)\n",
1464 dwDesiredAccess
, dwShareMode
, sa
, dwFlags
, lpScreenBufferData
);
1466 if (dwFlags
!= CONSOLE_TEXTMODE_BUFFER
|| lpScreenBufferData
!= NULL
)
1468 SetLastError(ERROR_INVALID_PARAMETER
);
1469 return INVALID_HANDLE_VALUE
;
1472 SERVER_START_REQ(create_console_output
)
1475 req
->access
= dwDesiredAccess
;
1476 req
->share
= dwShareMode
;
1477 req
->inherit
= (sa
&& sa
->bInheritHandle
);
1478 if (!wine_server_call_err( req
)) ret
= reply
->handle_out
;
1486 /***********************************************************************
1487 * GetConsoleScreenBufferInfo (KERNEL32.@)
1489 BOOL WINAPI
GetConsoleScreenBufferInfo(HANDLE hConsoleOutput
, LPCONSOLE_SCREEN_BUFFER_INFO csbi
)
1493 SERVER_START_REQ(get_console_output_info
)
1495 req
->handle
= console_handle_unmap(hConsoleOutput
);
1496 if ((ret
= !wine_server_call_err( req
)))
1498 csbi
->dwSize
.X
= reply
->width
;
1499 csbi
->dwSize
.Y
= reply
->height
;
1500 csbi
->dwCursorPosition
.X
= reply
->cursor_x
;
1501 csbi
->dwCursorPosition
.Y
= reply
->cursor_y
;
1502 csbi
->wAttributes
= reply
->attr
;
1503 csbi
->srWindow
.Left
= reply
->win_left
;
1504 csbi
->srWindow
.Right
= reply
->win_right
;
1505 csbi
->srWindow
.Top
= reply
->win_top
;
1506 csbi
->srWindow
.Bottom
= reply
->win_bottom
;
1507 csbi
->dwMaximumWindowSize
.X
= reply
->max_width
;
1508 csbi
->dwMaximumWindowSize
.Y
= reply
->max_height
;
1517 /******************************************************************************
1518 * SetConsoleActiveScreenBuffer [KERNEL32.@] Sets buffer to current console
1524 BOOL WINAPI
SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput
)
1528 TRACE("(%p)\n", hConsoleOutput
);
1530 SERVER_START_REQ( set_console_input_info
)
1533 req
->mask
= SET_CONSOLE_INPUT_INFO_ACTIVE_SB
;
1534 req
->active_sb
= hConsoleOutput
;
1535 ret
= !wine_server_call_err( req
);
1542 /***********************************************************************
1543 * GetConsoleMode (KERNEL32.@)
1545 BOOL WINAPI
GetConsoleMode(HANDLE hcon
, LPDWORD mode
)
1549 SERVER_START_REQ(get_console_mode
)
1551 req
->handle
= console_handle_unmap(hcon
);
1552 ret
= !wine_server_call_err( req
);
1553 if (ret
&& mode
) *mode
= reply
->mode
;
1560 /******************************************************************************
1561 * SetConsoleMode [KERNEL32.@] Sets input mode of console's input buffer
1564 * hcon [I] Handle to console input or screen buffer
1565 * mode [I] Input or output mode to set
1572 * ENABLE_PROCESSED_INPUT 0x01
1573 * ENABLE_LINE_INPUT 0x02
1574 * ENABLE_ECHO_INPUT 0x04
1575 * ENABLE_WINDOW_INPUT 0x08
1576 * ENABLE_MOUSE_INPUT 0x10
1578 BOOL WINAPI
SetConsoleMode(HANDLE hcon
, DWORD mode
)
1582 SERVER_START_REQ(set_console_mode
)
1584 req
->handle
= console_handle_unmap(hcon
);
1586 ret
= !wine_server_call_err( req
);
1589 /* FIXME: when resetting a console input to editline mode, I think we should
1590 * empty the S_EditString buffer
1593 TRACE("(%p,%lx) retval == %d\n", hcon
, mode
, ret
);
1599 /******************************************************************
1600 * CONSOLE_WriteChars
1602 * WriteConsoleOutput helper: hides server call semantics
1603 * writes a string at a given pos with standard attribute
1605 int CONSOLE_WriteChars(HANDLE hCon
, LPCWSTR lpBuffer
, int nc
, COORD
* pos
)
1611 SERVER_START_REQ( write_console_output
)
1613 req
->handle
= console_handle_unmap(hCon
);
1616 req
->mode
= CHAR_INFO_MODE_TEXTSTDATTR
;
1618 wine_server_add_data( req
, lpBuffer
, nc
* sizeof(WCHAR
) );
1619 if (!wine_server_call_err( req
)) written
= reply
->written
;
1623 if (written
> 0) pos
->X
+= written
;
1627 /******************************************************************
1630 * WriteConsoleOutput helper: handles passing to next line (+scrolling if necessary)
1633 static int next_line(HANDLE hCon
, CONSOLE_SCREEN_BUFFER_INFO
* csbi
)
1639 csbi
->dwCursorPosition
.X
= 0;
1640 csbi
->dwCursorPosition
.Y
++;
1642 if (csbi
->dwCursorPosition
.Y
< csbi
->dwSize
.Y
) return 1;
1645 src
.Bottom
= csbi
->dwSize
.Y
- 1;
1647 src
.Right
= csbi
->dwSize
.X
- 1;
1652 ci
.Attributes
= csbi
->wAttributes
;
1653 ci
.Char
.UnicodeChar
= ' ';
1655 csbi
->dwCursorPosition
.Y
--;
1656 if (!ScrollConsoleScreenBufferW(hCon
, &src
, NULL
, dst
, &ci
))
1661 /******************************************************************
1664 * WriteConsoleOutput helper: writes a block of non special characters
1665 * Block can spread on several lines, and wrapping, if needed, is
1669 static int write_block(HANDLE hCon
, CONSOLE_SCREEN_BUFFER_INFO
* csbi
,
1670 DWORD mode
, LPWSTR ptr
, int len
)
1672 int blk
; /* number of chars to write on current line */
1673 int done
; /* number of chars already written */
1675 if (len
<= 0) return 1;
1677 if (mode
& ENABLE_WRAP_AT_EOL_OUTPUT
) /* writes remaining on next line */
1679 for (done
= 0; done
< len
; done
+= blk
)
1681 blk
= min(len
- done
, csbi
->dwSize
.X
- csbi
->dwCursorPosition
.X
);
1683 if (CONSOLE_WriteChars(hCon
, ptr
+ done
, blk
, &csbi
->dwCursorPosition
) != blk
)
1685 if (csbi
->dwCursorPosition
.X
== csbi
->dwSize
.X
&& !next_line(hCon
, csbi
))
1691 int pos
= csbi
->dwCursorPosition
.X
;
1692 /* FIXME: we could reduce the number of loops
1693 * but, in most cases we wouldn't gain lots of time (it would only
1694 * happen if we're asked to overwrite more than twice the part of the line,
1697 for (blk
= done
= 0; done
< len
; done
+= blk
)
1699 blk
= min(len
- done
, csbi
->dwSize
.X
- csbi
->dwCursorPosition
.X
);
1701 csbi
->dwCursorPosition
.X
= pos
;
1702 if (CONSOLE_WriteChars(hCon
, ptr
+ done
, blk
, &csbi
->dwCursorPosition
) != blk
)
1710 /***********************************************************************
1711 * WriteConsoleW (KERNEL32.@)
1713 BOOL WINAPI
WriteConsoleW(HANDLE hConsoleOutput
, LPCVOID lpBuffer
, DWORD nNumberOfCharsToWrite
,
1714 LPDWORD lpNumberOfCharsWritten
, LPVOID lpReserved
)
1718 WCHAR
* psz
= (WCHAR
*)lpBuffer
;
1719 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1722 TRACE("%p %s %ld %p %p\n",
1723 hConsoleOutput
, debugstr_wn(lpBuffer
, nNumberOfCharsToWrite
),
1724 nNumberOfCharsToWrite
, lpNumberOfCharsWritten
, lpReserved
);
1726 if (lpNumberOfCharsWritten
) *lpNumberOfCharsWritten
= 0;
1728 if (!GetConsoleMode(hConsoleOutput
, &mode
) ||
1729 !GetConsoleScreenBufferInfo(hConsoleOutput
, &csbi
))
1732 if (mode
& ENABLE_PROCESSED_OUTPUT
)
1736 for (i
= 0; i
< nNumberOfCharsToWrite
; i
++)
1740 case '\b': case '\t': case '\n': case '\a': case '\r':
1741 /* don't handle here the i-th char... done below */
1742 if ((k
= i
- first
) > 0)
1744 if (!write_block(hConsoleOutput
, &csbi
, mode
, &psz
[first
], k
))
1754 if (csbi
.dwCursorPosition
.X
> 0) csbi
.dwCursorPosition
.X
--;
1758 WCHAR tmp
[8] = {' ',' ',' ',' ',' ',' ',' ',' '};
1760 if (!write_block(hConsoleOutput
, &csbi
, mode
, tmp
,
1761 ((csbi
.dwCursorPosition
.X
+ 8) & ~7) - csbi
.dwCursorPosition
.X
))
1766 next_line(hConsoleOutput
, &csbi
);
1772 csbi
.dwCursorPosition
.X
= 0;
1780 /* write the remaining block (if any) if processed output is enabled, or the
1781 * entire buffer otherwise
1783 if ((k
= nNumberOfCharsToWrite
- first
) > 0)
1785 if (!write_block(hConsoleOutput
, &csbi
, mode
, &psz
[first
], k
))
1791 SetConsoleCursorPosition(hConsoleOutput
, csbi
.dwCursorPosition
);
1792 if (lpNumberOfCharsWritten
) *lpNumberOfCharsWritten
= nw
;
1797 /***********************************************************************
1798 * WriteConsoleA (KERNEL32.@)
1800 BOOL WINAPI
WriteConsoleA(HANDLE hConsoleOutput
, LPCVOID lpBuffer
, DWORD nNumberOfCharsToWrite
,
1801 LPDWORD lpNumberOfCharsWritten
, LPVOID lpReserved
)
1807 n
= MultiByteToWideChar(CP_ACP
, 0, lpBuffer
, nNumberOfCharsToWrite
, NULL
, 0);
1809 if (lpNumberOfCharsWritten
) *lpNumberOfCharsWritten
= 0;
1810 xstring
= HeapAlloc(GetProcessHeap(), 0, n
* sizeof(WCHAR
));
1811 if (!xstring
) return 0;
1813 MultiByteToWideChar(CP_ACP
, 0, lpBuffer
, nNumberOfCharsToWrite
, xstring
, n
);
1815 ret
= WriteConsoleW(hConsoleOutput
, xstring
, n
, lpNumberOfCharsWritten
, 0);
1817 HeapFree(GetProcessHeap(), 0, xstring
);
1822 /******************************************************************************
1823 * SetConsoleCursorPosition [KERNEL32.@]
1824 * Sets the cursor position in console
1827 * hConsoleOutput [I] Handle of console screen buffer
1828 * dwCursorPosition [I] New cursor position coordinates
1832 BOOL WINAPI
SetConsoleCursorPosition(HANDLE hcon
, COORD pos
)
1835 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1839 TRACE("%p %d %d\n", hcon
, pos
.X
, pos
.Y
);
1841 SERVER_START_REQ(set_console_output_info
)
1843 req
->handle
= console_handle_unmap(hcon
);
1844 req
->cursor_x
= pos
.X
;
1845 req
->cursor_y
= pos
.Y
;
1846 req
->mask
= SET_CONSOLE_OUTPUT_INFO_CURSOR_POS
;
1847 ret
= !wine_server_call_err( req
);
1851 if (!ret
|| !GetConsoleScreenBufferInfo(hcon
, &csbi
))
1854 /* if cursor is no longer visible, scroll the visible window... */
1855 w
= csbi
.srWindow
.Right
- csbi
.srWindow
.Left
+ 1;
1856 h
= csbi
.srWindow
.Bottom
- csbi
.srWindow
.Top
+ 1;
1857 if (pos
.X
< csbi
.srWindow
.Left
)
1859 csbi
.srWindow
.Left
= min(pos
.X
, csbi
.dwSize
.X
- w
);
1862 else if (pos
.X
> csbi
.srWindow
.Right
)
1864 csbi
.srWindow
.Left
= max(pos
.X
, w
) - w
+ 1;
1867 csbi
.srWindow
.Right
= csbi
.srWindow
.Left
+ w
- 1;
1869 if (pos
.Y
< csbi
.srWindow
.Top
)
1871 csbi
.srWindow
.Top
= min(pos
.Y
, csbi
.dwSize
.Y
- h
);
1874 else if (pos
.Y
> csbi
.srWindow
.Bottom
)
1876 csbi
.srWindow
.Top
= max(pos
.Y
, h
) - h
+ 1;
1879 csbi
.srWindow
.Bottom
= csbi
.srWindow
.Top
+ h
- 1;
1881 ret
= (do_move
) ? SetConsoleWindowInfo(hcon
, TRUE
, &csbi
.srWindow
) : TRUE
;
1886 /******************************************************************************
1887 * GetConsoleCursorInfo [KERNEL32.@] Gets size and visibility of console
1890 * hcon [I] Handle to console screen buffer
1891 * cinfo [O] Address of cursor information
1897 BOOL WINAPI
GetConsoleCursorInfo(HANDLE hcon
, LPCONSOLE_CURSOR_INFO cinfo
)
1901 SERVER_START_REQ(get_console_output_info
)
1903 req
->handle
= console_handle_unmap(hcon
);
1904 ret
= !wine_server_call_err( req
);
1907 cinfo
->dwSize
= reply
->cursor_size
;
1908 cinfo
->bVisible
= reply
->cursor_visible
;
1916 /******************************************************************************
1917 * SetConsoleCursorInfo [KERNEL32.@] Sets size and visibility of cursor
1920 * hcon [I] Handle to console screen buffer
1921 * cinfo [I] Address of cursor information
1926 BOOL WINAPI
SetConsoleCursorInfo(HANDLE hCon
, LPCONSOLE_CURSOR_INFO cinfo
)
1930 SERVER_START_REQ(set_console_output_info
)
1932 req
->handle
= console_handle_unmap(hCon
);
1933 req
->cursor_size
= cinfo
->dwSize
;
1934 req
->cursor_visible
= cinfo
->bVisible
;
1935 req
->mask
= SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM
;
1936 ret
= !wine_server_call_err( req
);
1943 /******************************************************************************
1944 * SetConsoleWindowInfo [KERNEL32.@] Sets size and position of console
1947 * hcon [I] Handle to console screen buffer
1948 * bAbsolute [I] Coordinate type flag
1949 * window [I] Address of new window rectangle
1954 BOOL WINAPI
SetConsoleWindowInfo(HANDLE hCon
, BOOL bAbsolute
, LPSMALL_RECT window
)
1956 SMALL_RECT p
= *window
;
1961 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1962 if (!GetConsoleScreenBufferInfo(hCon
, &csbi
))
1964 p
.Left
+= csbi
.srWindow
.Left
;
1965 p
.Top
+= csbi
.srWindow
.Top
;
1966 p
.Right
+= csbi
.srWindow
.Left
;
1967 p
.Bottom
+= csbi
.srWindow
.Top
;
1969 SERVER_START_REQ(set_console_output_info
)
1971 req
->handle
= console_handle_unmap(hCon
);
1972 req
->win_left
= p
.Left
;
1973 req
->win_top
= p
.Top
;
1974 req
->win_right
= p
.Right
;
1975 req
->win_bottom
= p
.Bottom
;
1976 req
->mask
= SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW
;
1977 ret
= !wine_server_call_err( req
);
1985 /******************************************************************************
1986 * SetConsoleTextAttribute [KERNEL32.@] Sets colors for text
1988 * Sets the foreground and background color attributes of characters
1989 * written to the screen buffer.
1995 BOOL WINAPI
SetConsoleTextAttribute(HANDLE hConsoleOutput
, WORD wAttr
)
1999 SERVER_START_REQ(set_console_output_info
)
2001 req
->handle
= console_handle_unmap(hConsoleOutput
);
2003 req
->mask
= SET_CONSOLE_OUTPUT_INFO_ATTR
;
2004 ret
= !wine_server_call_err( req
);
2011 /******************************************************************************
2012 * SetConsoleScreenBufferSize [KERNEL32.@] Changes size of console
2015 * hConsoleOutput [I] Handle to console screen buffer
2016 * dwSize [I] New size in character rows and cols
2022 BOOL WINAPI
SetConsoleScreenBufferSize(HANDLE hConsoleOutput
, COORD dwSize
)
2026 SERVER_START_REQ(set_console_output_info
)
2028 req
->handle
= console_handle_unmap(hConsoleOutput
);
2029 req
->width
= dwSize
.X
;
2030 req
->height
= dwSize
.Y
;
2031 req
->mask
= SET_CONSOLE_OUTPUT_INFO_SIZE
;
2032 ret
= !wine_server_call_err( req
);
2039 /******************************************************************************
2040 * ScrollConsoleScreenBufferA [KERNEL32.@]
2043 BOOL WINAPI
ScrollConsoleScreenBufferA(HANDLE hConsoleOutput
, LPSMALL_RECT lpScrollRect
,
2044 LPSMALL_RECT lpClipRect
, COORD dwDestOrigin
,
2049 ciw
.Attributes
= lpFill
->Attributes
;
2050 MultiByteToWideChar(CP_ACP
, 0, &lpFill
->Char
.AsciiChar
, 1, &ciw
.Char
.UnicodeChar
, 1);
2052 return ScrollConsoleScreenBufferW(hConsoleOutput
, lpScrollRect
, lpClipRect
,
2053 dwDestOrigin
, &ciw
);
2056 /******************************************************************
2057 * CONSOLE_FillLineUniform
2059 * Helper function for ScrollConsoleScreenBufferW
2060 * Fills a part of a line with a constant character info
2062 void CONSOLE_FillLineUniform(HANDLE hConsoleOutput
, int i
, int j
, int len
, LPCHAR_INFO lpFill
)
2064 SERVER_START_REQ( fill_console_output
)
2066 req
->handle
= console_handle_unmap(hConsoleOutput
);
2067 req
->mode
= CHAR_INFO_MODE_TEXTATTR
;
2072 req
->data
.ch
= lpFill
->Char
.UnicodeChar
;
2073 req
->data
.attr
= lpFill
->Attributes
;
2074 wine_server_call_err( req
);
2079 /******************************************************************************
2080 * ScrollConsoleScreenBufferW [KERNEL32.@]
2084 BOOL WINAPI
ScrollConsoleScreenBufferW(HANDLE hConsoleOutput
, LPSMALL_RECT lpScrollRect
,
2085 LPSMALL_RECT lpClipRect
, COORD dwDestOrigin
,
2093 CONSOLE_SCREEN_BUFFER_INFO csbi
;
2097 TRACE("(%p,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", hConsoleOutput
,
2098 lpScrollRect
->Left
, lpScrollRect
->Top
,
2099 lpScrollRect
->Right
, lpScrollRect
->Bottom
,
2100 lpClipRect
->Left
, lpClipRect
->Top
,
2101 lpClipRect
->Right
, lpClipRect
->Bottom
,
2102 dwDestOrigin
.X
, dwDestOrigin
.Y
, lpFill
);
2104 TRACE("(%p,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", hConsoleOutput
,
2105 lpScrollRect
->Left
, lpScrollRect
->Top
,
2106 lpScrollRect
->Right
, lpScrollRect
->Bottom
,
2107 dwDestOrigin
.X
, dwDestOrigin
.Y
, lpFill
);
2109 if (!GetConsoleScreenBufferInfo(hConsoleOutput
, &csbi
))
2112 /* step 1: get dst rect */
2113 dst
.Left
= dwDestOrigin
.X
;
2114 dst
.Top
= dwDestOrigin
.Y
;
2115 dst
.Right
= dst
.Left
+ (lpScrollRect
->Right
- lpScrollRect
->Left
);
2116 dst
.Bottom
= dst
.Top
+ (lpScrollRect
->Bottom
- lpScrollRect
->Top
);
2118 /* step 2a: compute the final clip rect (optional passed clip and screen buffer limits */
2121 clip
.Left
= max(0, lpClipRect
->Left
);
2122 clip
.Right
= min(csbi
.dwSize
.X
- 1, lpClipRect
->Right
);
2123 clip
.Top
= max(0, lpClipRect
->Top
);
2124 clip
.Bottom
= min(csbi
.dwSize
.Y
- 1, lpClipRect
->Bottom
);
2129 clip
.Right
= csbi
.dwSize
.X
- 1;
2131 clip
.Bottom
= csbi
.dwSize
.Y
- 1;
2133 if (clip
.Left
> clip
.Right
|| clip
.Top
> clip
.Bottom
) return FALSE
;
2135 /* step 2b: clip dst rect */
2136 if (dst
.Left
< clip
.Left
) dst
.Left
= clip
.Left
;
2137 if (dst
.Top
< clip
.Top
) dst
.Top
= clip
.Top
;
2138 if (dst
.Right
> clip
.Right
) dst
.Right
= clip
.Right
;
2139 if (dst
.Bottom
> clip
.Bottom
) dst
.Bottom
= clip
.Bottom
;
2141 /* step 3: transfer the bits */
2142 SERVER_START_REQ(move_console_output
)
2144 req
->handle
= console_handle_unmap(hConsoleOutput
);
2145 req
->x_src
= lpScrollRect
->Left
;
2146 req
->y_src
= lpScrollRect
->Top
;
2147 req
->x_dst
= dst
.Left
;
2148 req
->y_dst
= dst
.Top
;
2149 req
->w
= dst
.Right
- dst
.Left
+ 1;
2150 req
->h
= dst
.Bottom
- dst
.Top
+ 1;
2151 ret
= !wine_server_call_err( req
);
2155 if (!ret
) return FALSE
;
2157 /* step 4: clean out the exposed part */
2159 /* have to write cell [i,j] if it is not in dst rect (because it has already
2160 * been written to by the scroll) and is in clip (we shall not write
2163 for (j
= max(lpScrollRect
->Top
, clip
.Top
); j
<= min(lpScrollRect
->Bottom
, clip
.Bottom
); j
++)
2165 inside
= dst
.Top
<= j
&& j
<= dst
.Bottom
;
2167 for (i
= max(lpScrollRect
->Left
, clip
.Left
); i
<= min(lpScrollRect
->Right
, clip
.Right
); i
++)
2169 if (inside
&& dst
.Left
<= i
&& i
<= dst
.Right
)
2173 CONSOLE_FillLineUniform(hConsoleOutput
, start
, j
, i
- start
, lpFill
);
2179 if (start
== -1) start
= i
;
2183 CONSOLE_FillLineUniform(hConsoleOutput
, start
, j
, i
- start
, lpFill
);
2190 /* ====================================================================
2192 * Console manipulation functions
2194 * ====================================================================*/
2196 /* some missing functions...
2197 * FIXME: those are likely to be defined as undocumented function in kernel32 (or part of them)
2198 * should get the right API and implement them
2199 * GetConsoleCommandHistory[AW] (dword dword dword)
2200 * GetConsoleCommandHistoryLength[AW]
2201 * SetConsoleCommandHistoryMode
2202 * SetConsoleNumberOfCommands[AW]
2204 int CONSOLE_GetHistory(int idx
, WCHAR
* buf
, int buf_len
)
2208 SERVER_START_REQ( get_console_input_history
)
2212 if (buf
&& buf_len
> 1)
2214 wine_server_set_reply( req
, buf
, (buf_len
- 1) * sizeof(WCHAR
) );
2216 if (!wine_server_call_err( req
))
2218 if (buf
) buf
[wine_server_reply_size(reply
) / sizeof(WCHAR
)] = 0;
2219 len
= reply
->total
/ sizeof(WCHAR
) + 1;
2226 /******************************************************************
2227 * CONSOLE_AppendHistory
2231 BOOL
CONSOLE_AppendHistory(const WCHAR
* ptr
)
2233 size_t len
= strlenW(ptr
);
2236 while (len
&& (ptr
[len
- 1] == '\n' || ptr
[len
- 1] == '\r')) len
--;
2238 SERVER_START_REQ( append_console_input_history
)
2241 wine_server_add_data( req
, ptr
, len
* sizeof(WCHAR
) );
2242 ret
= !wine_server_call_err( req
);
2248 /******************************************************************
2249 * CONSOLE_GetNumHistoryEntries
2253 unsigned CONSOLE_GetNumHistoryEntries(void)
2256 SERVER_START_REQ(get_console_input_info
)
2259 if (!wine_server_call_err( req
)) ret
= reply
->history_index
;
2265 /******************************************************************
2266 * CONSOLE_GetEditionMode
2270 BOOL
CONSOLE_GetEditionMode(HANDLE hConIn
, int* mode
)
2272 unsigned ret
= FALSE
;
2273 SERVER_START_REQ(get_console_input_info
)
2275 req
->handle
= console_handle_unmap(hConIn
);
2276 if ((ret
= !wine_server_call_err( req
)))
2277 *mode
= reply
->edition_mode
;