By default, console handles are inheritable.
[wine.git] / dlls / kernel / console.c
blobfa7286d94ff92c3ff8412f33569644c32e9f583e
1 /*
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.
31 #include "config.h"
32 #include "wine/port.h"
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <string.h>
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 #include <assert.h>
42 #include "windef.h"
43 #include "winbase.h"
44 #include "winnls.h"
45 #include "winerror.h"
46 #include "wincon.h"
47 #include "wine/server.h"
48 #include "wine/exception.h"
49 #include "wine/unicode.h"
50 #include "wine/debug.h"
51 #include "excpt.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 )
64 int i;
65 char ch;
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 )
79 int i;
80 WCHAR ch;
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 )
94 char ch;
96 while (count-- > 0)
98 WideCharToMultiByte( GetConsoleOutputCP(), 0, &buffer->Char.UnicodeChar, 1,
99 &ch, 1, NULL, NULL );
100 buffer->Char.AsciiChar = ch;
101 buffer++;
105 /* map char infos to Unicode */
106 static void char_info_AtoW( CHAR_INFO *buffer, int count )
108 WCHAR ch;
110 while (count-- > 0)
112 MultiByteToWideChar( GetConsoleOutputCP(), 0, &buffer->Char.AsciiChar, 1, &ch, 1 );
113 buffer->Char.UnicodeChar = ch;
114 buffer++;
119 /******************************************************************************
120 * GetConsoleCP [KERNEL32.@] Returns the OEM code page for the console
122 * RETURNS
123 * Code page code
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;
139 return TRUE;
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
156 * PARAMS
157 * cp [I] code page to set
159 * RETURNS
160 * Success: TRUE
161 * Failure: FALSE
163 BOOL WINAPI SetConsoleOutputCP(UINT cp)
165 if (!IsValidCodePage( cp )) return FALSE;
166 console_output_codepage = cp;
167 return TRUE;
171 /******************************************************************************
172 * WriteConsoleInputA [KERNEL32.@]
174 BOOL WINAPI WriteConsoleInputA( HANDLE handle, const INPUT_RECORD *buffer,
175 DWORD count, LPDWORD written )
177 INPUT_RECORD *recW;
178 BOOL ret;
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 );
185 return ret;
189 /******************************************************************************
190 * WriteConsoleInputW [KERNEL32.@]
192 BOOL WINAPI WriteConsoleInputW( HANDLE handle, const INPUT_RECORD *buffer,
193 DWORD count, LPDWORD written )
195 BOOL ret;
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;
207 SERVER_END_REQ;
209 return ret;
213 /***********************************************************************
214 * WriteConsoleOutputA (KERNEL32.@)
216 BOOL WINAPI WriteConsoleOutputA( HANDLE hConsoleOutput, const CHAR_INFO *lpBuffer,
217 COORD size, COORD coord, LPSMALL_RECT region )
219 int y;
220 BOOL ret;
221 COORD new_size, new_coord;
222 CHAR_INFO *ciw;
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;
231 return TRUE;
234 /* only copy the useful rectangle */
235 if (!(ciw = HeapAlloc( GetProcessHeap(), 0, sizeof(CHAR_INFO) * new_size.X * new_size.Y )))
236 return FALSE;
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 );
246 return ret;
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;
257 BOOL ret = TRUE;
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;
276 req->wrap = FALSE;
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 );
285 SERVER_END_REQ;
286 if (!ret) break;
289 region->Bottom = region->Top + height - 1;
290 region->Right = region->Left + width - 1;
291 return ret;
295 /******************************************************************************
296 * WriteConsoleOutputCharacterA [KERNEL32.@] Copies character to consecutive
297 * cells in the console screen buffer
299 * PARAMS
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 )
309 BOOL ret;
310 LPWSTR strW;
311 DWORD lenW;
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 );
325 return ret;
329 /******************************************************************************
330 * WriteConsoleOutputAttribute [KERNEL32.@] Sets attributes for some cells in
331 * the console screen buffer
333 * PARAMS
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
340 * RETURNS
341 * Success: TRUE
342 * Failure: FALSE
345 BOOL WINAPI WriteConsoleOutputAttribute( HANDLE hConsoleOutput, CONST WORD *attr, DWORD length,
346 COORD coord, LPDWORD lpNumAttrsWritten )
348 BOOL ret;
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);
355 req->x = coord.X;
356 req->y = coord.Y;
357 req->mode = CHAR_INFO_MODE_ATTR;
358 req->wrap = TRUE;
359 wine_server_add_data( req, attr, length * sizeof(WORD) );
360 if ((ret = !wine_server_call_err( req )))
362 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
365 SERVER_END_REQ;
366 return ret;
370 /******************************************************************************
371 * FillConsoleOutputCharacterA [KERNEL32.@]
373 * PARAMS
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
380 * RETURNS
381 * Success: TRUE
382 * Failure: FALSE
384 BOOL WINAPI FillConsoleOutputCharacterA( HANDLE hConsoleOutput, CHAR ch, DWORD length,
385 COORD coord, LPDWORD lpNumCharsWritten )
387 WCHAR wch;
389 MultiByteToWideChar( GetConsoleOutputCP(), 0, &ch, 1, &wch, 1 );
390 return FillConsoleOutputCharacterW(hConsoleOutput, wch, length, coord, lpNumCharsWritten);
394 /******************************************************************************
395 * FillConsoleOutputCharacterW [KERNEL32.@] Writes characters to console
397 * PARAMS
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
404 * RETURNS
405 * Success: TRUE
406 * Failure: FALSE
408 BOOL WINAPI FillConsoleOutputCharacterW( HANDLE hConsoleOutput, WCHAR ch, DWORD length,
409 COORD coord, LPDWORD lpNumCharsWritten)
411 BOOL ret;
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);
419 req->x = coord.X;
420 req->y = coord.Y;
421 req->mode = CHAR_INFO_MODE_TEXT;
422 req->wrap = TRUE;
423 req->data.ch = ch;
424 req->count = length;
425 if ((ret = !wine_server_call_err( req )))
427 if (lpNumCharsWritten) *lpNumCharsWritten = reply->written;
430 SERVER_END_REQ;
431 return ret;
435 /******************************************************************************
436 * FillConsoleOutputAttribute [KERNEL32.@] Sets attributes for console
438 * PARAMS
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
445 * RETURNS
446 * Success: TRUE
447 * Failure: FALSE
449 BOOL WINAPI FillConsoleOutputAttribute( HANDLE hConsoleOutput, WORD attr, DWORD length,
450 COORD coord, LPDWORD lpNumAttrsWritten )
452 BOOL ret;
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);
460 req->x = coord.X;
461 req->y = coord.Y;
462 req->mode = CHAR_INFO_MODE_ATTR;
463 req->wrap = TRUE;
464 req->data.attr = attr;
465 req->count = length;
466 if ((ret = !wine_server_call_err( req )))
468 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
471 SERVER_END_REQ;
472 return ret;
476 /******************************************************************************
477 * ReadConsoleOutputCharacterA [KERNEL32.@]
480 BOOL WINAPI ReadConsoleOutputCharacterA(HANDLE hConsoleOutput, LPSTR lpstr, DWORD count,
481 COORD coord, LPDWORD read_count)
483 DWORD read;
484 BOOL ret;
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 );
496 return ret;
500 /******************************************************************************
501 * ReadConsoleOutputCharacterW [KERNEL32.@]
504 BOOL WINAPI ReadConsoleOutputCharacterW( HANDLE hConsoleOutput, LPWSTR buffer, DWORD count,
505 COORD coord, LPDWORD read_count )
507 BOOL ret;
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);
514 req->x = coord.X;
515 req->y = coord.Y;
516 req->mode = CHAR_INFO_MODE_TEXT;
517 req->wrap = TRUE;
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);
524 SERVER_END_REQ;
525 return ret;
529 /******************************************************************************
530 * ReadConsoleOutputAttribute [KERNEL32.@]
532 BOOL WINAPI ReadConsoleOutputAttribute(HANDLE hConsoleOutput, LPWORD lpAttribute, DWORD length,
533 COORD coord, LPDWORD read_count)
535 BOOL ret;
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);
543 req->x = coord.X;
544 req->y = coord.Y;
545 req->mode = CHAR_INFO_MODE_ATTR;
546 req->wrap = TRUE;
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);
553 SERVER_END_REQ;
554 return ret;
558 /******************************************************************************
559 * ReadConsoleOutputA [KERNEL32.@]
562 BOOL WINAPI ReadConsoleOutputA( HANDLE hConsoleOutput, LPCHAR_INFO lpBuffer, COORD size,
563 COORD coord, LPSMALL_RECT region )
565 BOOL ret;
566 int y;
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 );
577 return ret;
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;
591 BOOL ret = TRUE;
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;
606 req->wrap = FALSE;
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 );
615 SERVER_END_REQ;
616 if (!ret) break;
619 region->Bottom = region->Top + height - 1;
620 region->Right = region->Left + width - 1;
621 return ret;
625 /******************************************************************************
626 * ReadConsoleInputA [KERNEL32.@] Reads data from a console
628 * PARAMS
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
634 * RETURNS
635 * Success: TRUE
636 * Failure: FALSE
638 BOOL WINAPI ReadConsoleInputA( HANDLE handle, LPINPUT_RECORD buffer, DWORD count, LPDWORD pRead )
640 DWORD read;
642 if (!ReadConsoleInputW( handle, buffer, count, &read )) return FALSE;
643 input_records_WtoA( buffer, read );
644 if (pRead) *pRead = read;
645 return TRUE;
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 )
656 DWORD read;
658 if (!PeekConsoleInputW( handle, buffer, count, &read )) return FALSE;
659 input_records_WtoA( buffer, read );
660 if (pRead) *pRead = read;
661 return TRUE;
665 /***********************************************************************
666 * PeekConsoleInputW (KERNEL32.@)
668 BOOL WINAPI PeekConsoleInputW( HANDLE handle, LPINPUT_RECORD buffer, DWORD count, LPDWORD read )
670 BOOL ret;
671 SERVER_START_REQ( read_console_input )
673 req->handle = console_handle_unmap(handle);
674 req->flush = FALSE;
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;
681 SERVER_END_REQ;
682 return ret;
686 /***********************************************************************
687 * GetNumberOfConsoleInputEvents (KERNEL32.@)
689 BOOL WINAPI GetNumberOfConsoleInputEvents( HANDLE handle, LPDWORD nrofevents )
691 BOOL ret;
692 SERVER_START_REQ( read_console_input )
694 req->handle = console_handle_unmap(handle);
695 req->flush = FALSE;
696 if ((ret = !wine_server_call_err( req )))
698 if (nrofevents) *nrofevents = reply->read;
701 SERVER_END_REQ;
702 return ret;
706 /******************************************************************************
707 * read_console_input
709 * Helper function for ReadConsole, ReadConsoleInput and FlushConsoleInputBuffer
711 * Returns
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)
720 return rci_timeout;
721 SERVER_START_REQ( read_console_input )
723 req->handle = console_handle_unmap(handle);
724 req->flush = TRUE;
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;
729 SERVER_END_REQ;
731 return ret;
735 /***********************************************************************
736 * FlushConsoleInputBuffer (KERNEL32.@)
738 BOOL WINAPI FlushConsoleInputBuffer( HANDLE handle )
740 enum read_console_input_return last;
741 INPUT_RECORD ir;
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 )
754 LPWSTR titleW;
755 BOOL ret;
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);
762 return ret;
766 /***********************************************************************
767 * GetConsoleTitleA (KERNEL32.@)
769 DWORD WINAPI GetConsoleTitleA(LPSTR title, DWORD size)
771 WCHAR *ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * size);
772 DWORD ret;
774 if (!ptr) return 0;
775 ret = GetConsoleTitleW( ptr, size );
776 if (ret)
778 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr, ret + 1, title, size, NULL, NULL);
779 ret = strlen(title);
781 HeapFree(GetProcessHeap(), 0, ptr);
782 return ret;
786 /******************************************************************************
787 * GetConsoleTitleW [KERNEL32.@] Retrieves title string for console
789 * PARAMS
790 * title [O] Address of buffer for title
791 * size [I] Size of buffer
793 * RETURNS
794 * Success: Length of string copied
795 * Failure: 0
797 DWORD WINAPI GetConsoleTitleW(LPWSTR title, DWORD size)
799 DWORD ret = 0;
801 SERVER_START_REQ( get_console_input_info )
803 req->handle = 0;
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);
808 title[ret] = 0;
811 SERVER_END_REQ;
812 return ret;
816 /***********************************************************************
817 * GetLargestConsoleWindowSize (KERNEL32.@)
819 * NOTE
820 * This should return a COORD, but calling convention for returning
821 * structures is different between Windows and gcc on i386.
823 * VERSION: [i386]
825 #ifdef __i386__
826 #undef GetLargestConsoleWindowSize
827 DWORD WINAPI GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
829 union {
830 COORD c;
831 DWORD w;
832 } x;
833 x.c.X = 80;
834 x.c.Y = 24;
835 return x.w;
837 #endif /* defined(__i386__) */
840 /***********************************************************************
841 * GetLargestConsoleWindowSize (KERNEL32.@)
843 * NOTE
844 * This should return a COORD, but calling convention for returning
845 * structures is different between Windows and gcc on i386.
847 * VERSION: [!i386]
849 #ifndef __i386__
850 COORD WINAPI GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
852 COORD c;
853 c.X = 80;
854 c.Y = 24;
855 return c;
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)
867 BOOL ret;
869 SERVER_START_REQ(free_console)
871 ret = !wine_server_call_err( req );
873 SERVER_END_REQ;
874 return ret;
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,
884 HANDLE hEvent)
886 char buffer[1024];
887 int ret;
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);
901 return TRUE;
903 return FALSE;
906 static BOOL start_console_renderer(STARTUPINFOA* si)
908 HANDLE hEvent = 0;
909 LPSTR p;
910 OBJECT_ATTRIBUTES attr;
911 BOOL ret = FALSE;
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);
927 if (!ret)
928 ERR("Couldn't launch Wine console from WINECONSOLE env var (%s)... "
929 "trying default access\n", p);
932 /* then try the regular PATH */
933 if (!ret)
934 ret = start_console_renderer_helper("wineconsole", si, hEvent);
936 CloseHandle(hEvent);
937 return ret;
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;
952 char buffer[1024];
953 SECURITY_ATTRIBUTES sa;
955 TRACE("()\n");
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);
964 return FALSE;
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))
990 goto the_end;
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))
1007 goto the_end;
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);
1016 return TRUE;
1018 the_end:
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);
1023 FreeConsole();
1024 return FALSE;
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));
1035 DWORD ncr = 0;
1036 BOOL ret;
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);
1044 return ret;
1047 /***********************************************************************
1048 * ReadConsoleW (KERNEL32.@)
1050 BOOL WINAPI ReadConsoleW(HANDLE hConsoleInput, LPVOID lpBuffer,
1051 DWORD nNumberOfCharsToRead, LPDWORD lpNumberOfCharsRead, LPVOID lpReserved)
1053 DWORD charsread;
1054 LPWSTR xbuf = (LPWSTR)lpBuffer;
1055 DWORD mode;
1057 TRACE("(%p,%p,%ld,%p,%p)\n",
1058 hConsoleInput, lpBuffer, nNumberOfCharsToRead, lpNumberOfCharsRead, lpReserved);
1060 if (!GetConsoleMode(hConsoleInput, &mode))
1061 return FALSE;
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)))
1069 return FALSE;
1070 S_EditStrPos = 0;
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;
1077 else
1079 INPUT_RECORD ir;
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...)
1086 charsread = 0;
1089 if (read_console_input(hConsoleInput, &ir, timeout) != rci_gotone) break;
1090 timeout = 0;
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;
1104 return TRUE;
1108 /***********************************************************************
1109 * ReadConsoleInputW (KERNEL32.@)
1111 BOOL WINAPI ReadConsoleInputW(HANDLE hConsoleInput, LPINPUT_RECORD lpBuffer,
1112 DWORD nLength, LPDWORD lpNumberOfEventsRead)
1114 DWORD idx = 0;
1115 DWORD timeout = INFINITE;
1117 if (!nLength)
1119 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = 0;
1120 return TRUE;
1123 /* loop until we get at least one event */
1124 while (read_console_input(hConsoleInput, &lpBuffer[idx], timeout) == rci_gotone &&
1125 ++idx < nLength)
1126 timeout = 0;
1128 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = idx;
1129 return idx != 0;
1133 /******************************************************************************
1134 * WriteConsoleOutputCharacterW [KERNEL32.@] Copies character to consecutive
1135 * cells in the console screen buffer
1137 * PARAMS
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
1144 * RETURNS
1145 * Success: TRUE
1146 * Failure: FALSE
1149 BOOL WINAPI WriteConsoleOutputCharacterW( HANDLE hConsoleOutput, LPCWSTR str, DWORD length,
1150 COORD coord, LPDWORD lpNumCharsWritten )
1152 BOOL ret;
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);
1160 req->x = coord.X;
1161 req->y = coord.Y;
1162 req->mode = CHAR_INFO_MODE_TEXT;
1163 req->wrap = TRUE;
1164 wine_server_add_data( req, str, length * sizeof(WCHAR) );
1165 if ((ret = !wine_server_call_err( req )))
1167 if (lpNumCharsWritten) *lpNumCharsWritten = reply->written;
1170 SERVER_END_REQ;
1171 return ret;
1175 /******************************************************************************
1176 * SetConsoleTitleW [KERNEL32.@] Sets title bar string for console
1178 * PARAMS
1179 * title [I] Address of new title
1181 * RETURNS
1182 * Success: TRUE
1183 * Failure: FALSE
1185 BOOL WINAPI SetConsoleTitleW(LPCWSTR title)
1187 BOOL ret;
1189 SERVER_START_REQ( set_console_input_info )
1191 req->handle = 0;
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 );
1196 SERVER_END_REQ;
1197 return ret;
1201 /***********************************************************************
1202 * GetNumberOfConsoleMouseButtons (KERNEL32.@)
1204 BOOL WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
1206 FIXME("(%p): stub\n", nrofbuttons);
1207 *nrofbuttons = 2;
1208 return TRUE;
1211 /******************************************************************************
1212 * SetConsoleInputExeNameW [KERNEL32.@]
1214 * BUGS
1215 * Unimplemented
1217 BOOL WINAPI SetConsoleInputExeNameW(LPCWSTR name)
1219 FIXME("(%s): stub!\n", debugstr_w(name));
1221 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1222 return TRUE;
1225 /******************************************************************************
1226 * SetConsoleInputExeNameA [KERNEL32.@]
1228 * BUGS
1229 * Unimplemented
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));
1235 BOOL ret;
1237 if (!xptr) return FALSE;
1239 MultiByteToWideChar(CP_ACP, 0, name, -1, xptr, len);
1240 ret = SetConsoleInputExeNameW(xptr);
1241 HeapFree(GetProcessHeap(), 0, xptr);
1243 return ret;
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);
1254 ExitProcess(0);
1255 /* should never go here */
1256 return TRUE;
1259 /******************************************************************************
1260 * SetConsoleCtrlHandler [KERNEL32.@] Adds function to calling process list
1262 * PARAMS
1263 * func [I] Address of handler function
1264 * add [I] Handler to add or remove
1266 * RETURNS
1267 * Success: TRUE
1268 * Failure: FALSE
1270 * CHANGED
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)
1299 BOOL ret = TRUE;
1301 FIXME("(%p,%i) - no error checking or testing yet\n", func, add);
1303 if (!func)
1305 CONSOLE_IgnoreCtrlC = add;
1307 else if (add)
1309 struct ConsoleHandler* ch = HeapAlloc(GetProcessHeap(), 0, sizeof(struct ConsoleHandler));
1311 if (!ch) return FALSE;
1312 ch->handler = func;
1313 RtlEnterCriticalSection(&CONSOLE_CritSect);
1314 ch->next = CONSOLE_Handlers;
1315 CONSOLE_Handlers = ch;
1316 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1318 else
1320 struct ConsoleHandler** ch;
1321 RtlEnterCriticalSection(&CONSOLE_CritSect);
1322 for (ch = &CONSOLE_Handlers; *ch; *ch = (*ch)->next)
1324 if ((*ch)->handler == func) break;
1326 if (*ch)
1328 struct ConsoleHandler* rch = *ch;
1330 /* sanity check */
1331 if (rch == &CONSOLE_DefaultConsoleHandler)
1333 ERR("Who's trying to remove default handler???\n");
1334 ret = FALSE;
1336 else
1338 rch = *ch;
1339 *ch = (*ch)->next;
1340 HeapFree(GetProcessHeap(), 0, rch);
1343 else
1345 WARN("Attempt to remove non-installed CtrlHandler %p\n", func);
1346 ret = FALSE;
1348 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1350 return ret;
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);
1370 return 0;
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
1389 __TRY
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);
1400 __ENDTRY;
1401 return 1;
1404 /******************************************************************************
1405 * GenerateConsoleCtrlEvent [KERNEL32.@] Simulate a CTRL-C or CTRL-BREAK
1407 * PARAMS
1408 * dwCtrlEvent [I] Type of event
1409 * dwProcessGroupID [I] Process group ID to send event to
1411 * RETURNS
1412 * Success: True
1413 * Failure: False (and *should* [but doesn't] set LastError)
1415 BOOL WINAPI GenerateConsoleCtrlEvent(DWORD dwCtrlEvent,
1416 DWORD dwProcessGroupID)
1418 BOOL ret;
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);
1425 return FALSE;
1428 SERVER_START_REQ( send_console_signal )
1430 req->signal = dwCtrlEvent;
1431 req->group_id = dwProcessGroupID;
1432 ret = !wine_server_call_err( req );
1434 SERVER_END_REQ;
1436 return ret;
1440 /******************************************************************************
1441 * CreateConsoleScreenBuffer [KERNEL32.@] Creates a console screen buffer
1443 * PARAMS
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
1450 * NOTES
1451 * Should call SetLastError
1453 * RETURNS
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)
1474 req->handle_in = 0;
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;
1480 SERVER_END_REQ;
1482 return ret;
1486 /***********************************************************************
1487 * GetConsoleScreenBufferInfo (KERNEL32.@)
1489 BOOL WINAPI GetConsoleScreenBufferInfo(HANDLE hConsoleOutput, LPCONSOLE_SCREEN_BUFFER_INFO csbi)
1491 BOOL ret;
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;
1511 SERVER_END_REQ;
1513 return ret;
1517 /******************************************************************************
1518 * SetConsoleActiveScreenBuffer [KERNEL32.@] Sets buffer to current console
1520 * RETURNS
1521 * Success: TRUE
1522 * Failure: FALSE
1524 BOOL WINAPI SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput)
1526 BOOL ret;
1528 TRACE("(%p)\n", hConsoleOutput);
1530 SERVER_START_REQ( set_console_input_info )
1532 req->handle = 0;
1533 req->mask = SET_CONSOLE_INPUT_INFO_ACTIVE_SB;
1534 req->active_sb = hConsoleOutput;
1535 ret = !wine_server_call_err( req );
1537 SERVER_END_REQ;
1538 return ret;
1542 /***********************************************************************
1543 * GetConsoleMode (KERNEL32.@)
1545 BOOL WINAPI GetConsoleMode(HANDLE hcon, LPDWORD mode)
1547 BOOL ret;
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;
1555 SERVER_END_REQ;
1556 return ret;
1560 /******************************************************************************
1561 * SetConsoleMode [KERNEL32.@] Sets input mode of console's input buffer
1563 * PARAMS
1564 * hcon [I] Handle to console input or screen buffer
1565 * mode [I] Input or output mode to set
1567 * RETURNS
1568 * Success: TRUE
1569 * Failure: FALSE
1571 * mode:
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)
1580 BOOL ret;
1582 SERVER_START_REQ(set_console_mode)
1584 req->handle = console_handle_unmap(hcon);
1585 req->mode = mode;
1586 ret = !wine_server_call_err( req );
1588 SERVER_END_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);
1595 return 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)
1607 int written = -1;
1609 if (!nc) return 0;
1611 SERVER_START_REQ( write_console_output )
1613 req->handle = console_handle_unmap(hCon);
1614 req->x = pos->X;
1615 req->y = pos->Y;
1616 req->mode = CHAR_INFO_MODE_TEXTSTDATTR;
1617 req->wrap = FALSE;
1618 wine_server_add_data( req, lpBuffer, nc * sizeof(WCHAR) );
1619 if (!wine_server_call_err( req )) written = reply->written;
1621 SERVER_END_REQ;
1623 if (written > 0) pos->X += written;
1624 return written;
1627 /******************************************************************
1628 * next_line
1630 * WriteConsoleOutput helper: handles passing to next line (+scrolling if necessary)
1633 static int next_line(HANDLE hCon, CONSOLE_SCREEN_BUFFER_INFO* csbi)
1635 SMALL_RECT src;
1636 CHAR_INFO ci;
1637 COORD dst;
1639 csbi->dwCursorPosition.X = 0;
1640 csbi->dwCursorPosition.Y++;
1642 if (csbi->dwCursorPosition.Y < csbi->dwSize.Y) return 1;
1644 src.Top = 1;
1645 src.Bottom = csbi->dwSize.Y - 1;
1646 src.Left = 0;
1647 src.Right = csbi->dwSize.X - 1;
1649 dst.X = 0;
1650 dst.Y = 0;
1652 ci.Attributes = csbi->wAttributes;
1653 ci.Char.UnicodeChar = ' ';
1655 csbi->dwCursorPosition.Y--;
1656 if (!ScrollConsoleScreenBufferW(hCon, &src, NULL, dst, &ci))
1657 return 0;
1658 return 1;
1661 /******************************************************************
1662 * write_block
1664 * WriteConsoleOutput helper: writes a block of non special characters
1665 * Block can spread on several lines, and wrapping, if needed, is
1666 * handled
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)
1684 return 0;
1685 if (csbi->dwCursorPosition.X == csbi->dwSize.X && !next_line(hCon, csbi))
1686 return 0;
1689 else
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,
1695 * which is unlikely
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)
1703 return 0;
1707 return 1;
1710 /***********************************************************************
1711 * WriteConsoleW (KERNEL32.@)
1713 BOOL WINAPI WriteConsoleW(HANDLE hConsoleOutput, LPCVOID lpBuffer, DWORD nNumberOfCharsToWrite,
1714 LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
1716 DWORD mode;
1717 DWORD nw = 0;
1718 WCHAR* psz = (WCHAR*)lpBuffer;
1719 CONSOLE_SCREEN_BUFFER_INFO csbi;
1720 int k, first = 0;
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))
1730 return FALSE;
1732 if (mode & ENABLE_PROCESSED_OUTPUT)
1734 int i;
1736 for (i = 0; i < nNumberOfCharsToWrite; i++)
1738 switch (psz[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))
1745 goto the_end;
1746 nw += k;
1748 first = i + 1;
1749 nw++;
1751 switch (psz[i])
1753 case '\b':
1754 if (csbi.dwCursorPosition.X > 0) csbi.dwCursorPosition.X--;
1755 break;
1756 case '\t':
1758 WCHAR tmp[8] = {' ',' ',' ',' ',' ',' ',' ',' '};
1760 if (!write_block(hConsoleOutput, &csbi, mode, tmp,
1761 ((csbi.dwCursorPosition.X + 8) & ~7) - csbi.dwCursorPosition.X))
1762 goto the_end;
1764 break;
1765 case '\n':
1766 next_line(hConsoleOutput, &csbi);
1767 break;
1768 case '\a':
1769 Beep(400, 300);
1770 break;
1771 case '\r':
1772 csbi.dwCursorPosition.X = 0;
1773 break;
1774 default:
1775 break;
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))
1786 goto the_end;
1787 nw += k;
1790 the_end:
1791 SetConsoleCursorPosition(hConsoleOutput, csbi.dwCursorPosition);
1792 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = nw;
1793 return nw != 0;
1797 /***********************************************************************
1798 * WriteConsoleA (KERNEL32.@)
1800 BOOL WINAPI WriteConsoleA(HANDLE hConsoleOutput, LPCVOID lpBuffer, DWORD nNumberOfCharsToWrite,
1801 LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
1803 BOOL ret;
1804 LPWSTR xstring;
1805 DWORD n;
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);
1819 return ret;
1822 /******************************************************************************
1823 * SetConsoleCursorPosition [KERNEL32.@]
1824 * Sets the cursor position in console
1826 * PARAMS
1827 * hConsoleOutput [I] Handle of console screen buffer
1828 * dwCursorPosition [I] New cursor position coordinates
1830 * RETURNS STD
1832 BOOL WINAPI SetConsoleCursorPosition(HANDLE hcon, COORD pos)
1834 BOOL ret;
1835 CONSOLE_SCREEN_BUFFER_INFO csbi;
1836 int do_move = 0;
1837 int w, h;
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 );
1849 SERVER_END_REQ;
1851 if (!ret || !GetConsoleScreenBufferInfo(hcon, &csbi))
1852 return FALSE;
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);
1860 do_move++;
1862 else if (pos.X > csbi.srWindow.Right)
1864 csbi.srWindow.Left = max(pos.X, w) - w + 1;
1865 do_move++;
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);
1872 do_move++;
1874 else if (pos.Y > csbi.srWindow.Bottom)
1876 csbi.srWindow.Top = max(pos.Y, h) - h + 1;
1877 do_move++;
1879 csbi.srWindow.Bottom = csbi.srWindow.Top + h - 1;
1881 ret = (do_move) ? SetConsoleWindowInfo(hcon, TRUE, &csbi.srWindow) : TRUE;
1883 return ret;
1886 /******************************************************************************
1887 * GetConsoleCursorInfo [KERNEL32.@] Gets size and visibility of console
1889 * PARAMS
1890 * hcon [I] Handle to console screen buffer
1891 * cinfo [O] Address of cursor information
1893 * RETURNS
1894 * Success: TRUE
1895 * Failure: FALSE
1897 BOOL WINAPI GetConsoleCursorInfo(HANDLE hcon, LPCONSOLE_CURSOR_INFO cinfo)
1899 BOOL ret;
1901 SERVER_START_REQ(get_console_output_info)
1903 req->handle = console_handle_unmap(hcon);
1904 ret = !wine_server_call_err( req );
1905 if (ret && cinfo)
1907 cinfo->dwSize = reply->cursor_size;
1908 cinfo->bVisible = reply->cursor_visible;
1911 SERVER_END_REQ;
1912 return ret;
1916 /******************************************************************************
1917 * SetConsoleCursorInfo [KERNEL32.@] Sets size and visibility of cursor
1919 * PARAMS
1920 * hcon [I] Handle to console screen buffer
1921 * cinfo [I] Address of cursor information
1922 * RETURNS
1923 * Success: TRUE
1924 * Failure: FALSE
1926 BOOL WINAPI SetConsoleCursorInfo(HANDLE hCon, LPCONSOLE_CURSOR_INFO cinfo)
1928 BOOL ret;
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 );
1938 SERVER_END_REQ;
1939 return ret;
1943 /******************************************************************************
1944 * SetConsoleWindowInfo [KERNEL32.@] Sets size and position of console
1946 * PARAMS
1947 * hcon [I] Handle to console screen buffer
1948 * bAbsolute [I] Coordinate type flag
1949 * window [I] Address of new window rectangle
1950 * RETURNS
1951 * Success: TRUE
1952 * Failure: FALSE
1954 BOOL WINAPI SetConsoleWindowInfo(HANDLE hCon, BOOL bAbsolute, LPSMALL_RECT window)
1956 SMALL_RECT p = *window;
1957 BOOL ret;
1959 if (!bAbsolute)
1961 CONSOLE_SCREEN_BUFFER_INFO csbi;
1962 if (!GetConsoleScreenBufferInfo(hCon, &csbi))
1963 return FALSE;
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 );
1979 SERVER_END_REQ;
1981 return ret;
1985 /******************************************************************************
1986 * SetConsoleTextAttribute [KERNEL32.@] Sets colors for text
1988 * Sets the foreground and background color attributes of characters
1989 * written to the screen buffer.
1991 * RETURNS
1992 * Success: TRUE
1993 * Failure: FALSE
1995 BOOL WINAPI SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttr)
1997 BOOL ret;
1999 SERVER_START_REQ(set_console_output_info)
2001 req->handle = console_handle_unmap(hConsoleOutput);
2002 req->attr = wAttr;
2003 req->mask = SET_CONSOLE_OUTPUT_INFO_ATTR;
2004 ret = !wine_server_call_err( req );
2006 SERVER_END_REQ;
2007 return ret;
2011 /******************************************************************************
2012 * SetConsoleScreenBufferSize [KERNEL32.@] Changes size of console
2014 * PARAMS
2015 * hConsoleOutput [I] Handle to console screen buffer
2016 * dwSize [I] New size in character rows and cols
2018 * RETURNS
2019 * Success: TRUE
2020 * Failure: FALSE
2022 BOOL WINAPI SetConsoleScreenBufferSize(HANDLE hConsoleOutput, COORD dwSize)
2024 BOOL ret;
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 );
2034 SERVER_END_REQ;
2035 return ret;
2039 /******************************************************************************
2040 * ScrollConsoleScreenBufferA [KERNEL32.@]
2043 BOOL WINAPI ScrollConsoleScreenBufferA(HANDLE hConsoleOutput, LPSMALL_RECT lpScrollRect,
2044 LPSMALL_RECT lpClipRect, COORD dwDestOrigin,
2045 LPCHAR_INFO lpFill)
2047 CHAR_INFO ciw;
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;
2068 req->x = i;
2069 req->y = j;
2070 req->count = len;
2071 req->wrap = FALSE;
2072 req->data.ch = lpFill->Char.UnicodeChar;
2073 req->data.attr = lpFill->Attributes;
2074 wine_server_call_err( req );
2076 SERVER_END_REQ;
2079 /******************************************************************************
2080 * ScrollConsoleScreenBufferW [KERNEL32.@]
2084 BOOL WINAPI ScrollConsoleScreenBufferW(HANDLE hConsoleOutput, LPSMALL_RECT lpScrollRect,
2085 LPSMALL_RECT lpClipRect, COORD dwDestOrigin,
2086 LPCHAR_INFO lpFill)
2088 SMALL_RECT dst;
2089 DWORD ret;
2090 int i, j;
2091 int start = -1;
2092 SMALL_RECT clip;
2093 CONSOLE_SCREEN_BUFFER_INFO csbi;
2094 BOOL inside;
2096 if (lpClipRect)
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);
2103 else
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))
2110 return FALSE;
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 */
2119 if (lpClipRect)
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);
2126 else
2128 clip.Left = 0;
2129 clip.Right = csbi.dwSize.X - 1;
2130 clip.Top = 0;
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 );
2153 SERVER_END_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
2161 * outside of clip)
2163 for (j = max(lpScrollRect->Top, clip.Top); j <= min(lpScrollRect->Bottom, clip.Bottom); j++)
2165 inside = dst.Top <= j && j <= dst.Bottom;
2166 start = -1;
2167 for (i = max(lpScrollRect->Left, clip.Left); i <= min(lpScrollRect->Right, clip.Right); i++)
2169 if (inside && dst.Left <= i && i <= dst.Right)
2171 if (start != -1)
2173 CONSOLE_FillLineUniform(hConsoleOutput, start, j, i - start, lpFill);
2174 start = -1;
2177 else
2179 if (start == -1) start = i;
2182 if (start != -1)
2183 CONSOLE_FillLineUniform(hConsoleOutput, start, j, i - start, lpFill);
2186 return TRUE;
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)
2206 int len = 0;
2208 SERVER_START_REQ( get_console_input_history )
2210 req->handle = 0;
2211 req->index = idx;
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;
2222 SERVER_END_REQ;
2223 return len;
2226 /******************************************************************
2227 * CONSOLE_AppendHistory
2231 BOOL CONSOLE_AppendHistory(const WCHAR* ptr)
2233 size_t len = strlenW(ptr);
2234 BOOL ret;
2236 while (len && (ptr[len - 1] == '\n' || ptr[len - 1] == '\r')) len--;
2238 SERVER_START_REQ( append_console_input_history )
2240 req->handle = 0;
2241 wine_server_add_data( req, ptr, len * sizeof(WCHAR) );
2242 ret = !wine_server_call_err( req );
2244 SERVER_END_REQ;
2245 return ret;
2248 /******************************************************************
2249 * CONSOLE_GetNumHistoryEntries
2253 unsigned CONSOLE_GetNumHistoryEntries(void)
2255 unsigned ret = -1;
2256 SERVER_START_REQ(get_console_input_info)
2258 req->handle = 0;
2259 if (!wine_server_call_err( req )) ret = reply->history_index;
2261 SERVER_END_REQ;
2262 return ret;
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;
2279 SERVER_END_REQ;
2280 return ret;