shdocvw: Always create BindStatusCallback object.
[wine.git] / dlls / kernel32 / console.c
blob446e882a406c6ebd8c772519d807e0742ef529e8
1 /*
2 * Win32 console 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,2004,2005 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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/winbase16.h"
48 #include "wine/server.h"
49 #include "wine/exception.h"
50 #include "wine/unicode.h"
51 #include "wine/debug.h"
52 #include "excpt.h"
53 #include "console_private.h"
54 #include "kernel_private.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(console);
58 static CRITICAL_SECTION CONSOLE_CritSect;
59 static CRITICAL_SECTION_DEBUG critsect_debug =
61 0, 0, &CONSOLE_CritSect,
62 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
63 0, 0, { (DWORD_PTR)(__FILE__ ": CONSOLE_CritSect") }
65 static CRITICAL_SECTION CONSOLE_CritSect = { &critsect_debug, -1, 0, 0, 0, 0 };
67 static const WCHAR coninW[] = {'C','O','N','I','N','$',0};
68 static const WCHAR conoutW[] = {'C','O','N','O','U','T','$',0};
70 /* FIXME: this is not thread safe */
71 static HANDLE console_wait_event;
73 /* map input records to ASCII */
74 static void input_records_WtoA( INPUT_RECORD *buffer, int count )
76 int i;
77 char ch;
79 for (i = 0; i < count; i++)
81 if (buffer[i].EventType != KEY_EVENT) continue;
82 WideCharToMultiByte( GetConsoleCP(), 0,
83 &buffer[i].Event.KeyEvent.uChar.UnicodeChar, 1, &ch, 1, NULL, NULL );
84 buffer[i].Event.KeyEvent.uChar.AsciiChar = ch;
88 /* map input records to Unicode */
89 static void input_records_AtoW( INPUT_RECORD *buffer, int count )
91 int i;
92 WCHAR ch;
94 for (i = 0; i < count; i++)
96 if (buffer[i].EventType != KEY_EVENT) continue;
97 MultiByteToWideChar( GetConsoleCP(), 0,
98 &buffer[i].Event.KeyEvent.uChar.AsciiChar, 1, &ch, 1 );
99 buffer[i].Event.KeyEvent.uChar.UnicodeChar = ch;
103 /* map char infos to ASCII */
104 static void char_info_WtoA( CHAR_INFO *buffer, int count )
106 char ch;
108 while (count-- > 0)
110 WideCharToMultiByte( GetConsoleOutputCP(), 0, &buffer->Char.UnicodeChar, 1,
111 &ch, 1, NULL, NULL );
112 buffer->Char.AsciiChar = ch;
113 buffer++;
117 /* map char infos to Unicode */
118 static void char_info_AtoW( CHAR_INFO *buffer, int count )
120 WCHAR ch;
122 while (count-- > 0)
124 MultiByteToWideChar( GetConsoleOutputCP(), 0, &buffer->Char.AsciiChar, 1, &ch, 1 );
125 buffer->Char.UnicodeChar = ch;
126 buffer++;
131 /******************************************************************************
132 * GetConsoleWindow [KERNEL32.@] Get hwnd of the console window.
134 * RETURNS
135 * Success: hwnd of the console window.
136 * Failure: NULL
138 HWND WINAPI GetConsoleWindow(VOID)
140 HWND hWnd = NULL;
142 SERVER_START_REQ(get_console_input_info)
144 req->handle = 0;
145 if (!wine_server_call_err(req)) hWnd = reply->win;
147 SERVER_END_REQ;
149 return hWnd;
153 /******************************************************************************
154 * GetConsoleCP [KERNEL32.@] Returns the OEM code page for the console
156 * RETURNS
157 * Code page code
159 UINT WINAPI GetConsoleCP(VOID)
161 BOOL ret;
162 UINT codepage = GetOEMCP(); /* default value */
164 SERVER_START_REQ(get_console_input_info)
166 req->handle = 0;
167 ret = !wine_server_call_err(req);
168 if (ret && reply->input_cp)
169 codepage = reply->input_cp;
171 SERVER_END_REQ;
173 return codepage;
177 /******************************************************************************
178 * SetConsoleCP [KERNEL32.@]
180 BOOL WINAPI SetConsoleCP(UINT cp)
182 BOOL ret;
184 if (!IsValidCodePage(cp))
186 SetLastError(ERROR_INVALID_PARAMETER);
187 return FALSE;
190 SERVER_START_REQ(set_console_input_info)
192 req->handle = 0;
193 req->mask = SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE;
194 req->input_cp = cp;
195 ret = !wine_server_call_err(req);
197 SERVER_END_REQ;
199 return ret;
203 /***********************************************************************
204 * GetConsoleOutputCP (KERNEL32.@)
206 UINT WINAPI GetConsoleOutputCP(VOID)
208 BOOL ret;
209 UINT codepage = GetOEMCP(); /* default value */
211 SERVER_START_REQ(get_console_input_info)
213 req->handle = 0;
214 ret = !wine_server_call_err(req);
215 if (ret && reply->output_cp)
216 codepage = reply->output_cp;
218 SERVER_END_REQ;
220 return codepage;
224 /******************************************************************************
225 * SetConsoleOutputCP [KERNEL32.@] Set the output codepage used by the console
227 * PARAMS
228 * cp [I] code page to set
230 * RETURNS
231 * Success: TRUE
232 * Failure: FALSE
234 BOOL WINAPI SetConsoleOutputCP(UINT cp)
236 BOOL ret;
238 if (!IsValidCodePage(cp))
240 SetLastError(ERROR_INVALID_PARAMETER);
241 return FALSE;
244 SERVER_START_REQ(set_console_input_info)
246 req->handle = 0;
247 req->mask = SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE;
248 req->output_cp = cp;
249 ret = !wine_server_call_err(req);
251 SERVER_END_REQ;
253 return ret;
257 /***********************************************************************
258 * Beep (KERNEL32.@)
260 BOOL WINAPI Beep( DWORD dwFreq, DWORD dwDur )
262 static const char beep = '\a';
263 /* dwFreq and dwDur are ignored by Win95 */
264 if (isatty(2)) write( 2, &beep, 1 );
265 return TRUE;
269 /******************************************************************
270 * OpenConsoleW (KERNEL32.@)
272 * Undocumented
273 * Open a handle to the current process console.
274 * Returns INVALID_HANDLE_VALUE on failure.
276 HANDLE WINAPI OpenConsoleW(LPCWSTR name, DWORD access, BOOL inherit, DWORD creation)
278 HANDLE output;
279 HANDLE ret;
281 if (strcmpiW(coninW, name) == 0)
282 output = (HANDLE) FALSE;
283 else if (strcmpiW(conoutW, name) == 0)
284 output = (HANDLE) TRUE;
285 else
287 SetLastError(ERROR_INVALID_NAME);
288 return INVALID_HANDLE_VALUE;
290 if (creation != OPEN_EXISTING)
292 SetLastError(ERROR_INVALID_PARAMETER);
293 return INVALID_HANDLE_VALUE;
296 SERVER_START_REQ( open_console )
298 req->from = output;
299 req->access = access;
300 req->attributes = inherit ? OBJ_INHERIT : 0;
301 req->share = FILE_SHARE_READ | FILE_SHARE_WRITE;
302 SetLastError(0);
303 wine_server_call_err( req );
304 ret = reply->handle;
306 SERVER_END_REQ;
307 if (ret)
308 ret = console_handle_map(ret);
309 else
311 /* likely, we're not attached to wineconsole
312 * let's try to return a handle to the unix-console
314 int fd = open("/dev/tty", output ? O_WRONLY : O_RDONLY);
315 ret = INVALID_HANDLE_VALUE;
316 if (fd != -1)
318 DWORD access = (output ? GENERIC_WRITE : GENERIC_READ) | SYNCHRONIZE;
319 wine_server_fd_to_handle(fd, access, inherit ? OBJ_INHERIT : 0, &ret);
320 close(fd);
323 return ret;
326 /******************************************************************
327 * VerifyConsoleIoHandle (KERNEL32.@)
329 * Undocumented
331 BOOL WINAPI VerifyConsoleIoHandle(HANDLE handle)
333 BOOL ret;
335 if (!is_console_handle(handle)) return FALSE;
336 SERVER_START_REQ(get_console_mode)
338 req->handle = console_handle_unmap(handle);
339 ret = !wine_server_call_err( req );
341 SERVER_END_REQ;
342 return ret;
345 /******************************************************************
346 * DuplicateConsoleHandle (KERNEL32.@)
348 * Undocumented
350 HANDLE WINAPI DuplicateConsoleHandle(HANDLE handle, DWORD access, BOOL inherit,
351 DWORD options)
353 HANDLE ret;
355 if (!is_console_handle(handle) ||
356 !DuplicateHandle(GetCurrentProcess(), console_handle_unmap(handle),
357 GetCurrentProcess(), &ret, access, inherit, options))
358 return INVALID_HANDLE_VALUE;
359 return console_handle_map(ret);
362 /******************************************************************
363 * CloseConsoleHandle (KERNEL32.@)
365 * Undocumented
367 BOOL WINAPI CloseConsoleHandle(HANDLE handle)
369 if (!is_console_handle(handle))
371 SetLastError(ERROR_INVALID_PARAMETER);
372 return FALSE;
374 return CloseHandle(console_handle_unmap(handle));
377 /******************************************************************
378 * GetConsoleInputWaitHandle (KERNEL32.@)
380 * Undocumented
382 HANDLE WINAPI GetConsoleInputWaitHandle(void)
384 if (!console_wait_event)
386 SERVER_START_REQ(get_console_wait_event)
388 if (!wine_server_call_err( req )) console_wait_event = reply->handle;
390 SERVER_END_REQ;
392 return console_wait_event;
396 /******************************************************************************
397 * WriteConsoleInputA [KERNEL32.@]
399 BOOL WINAPI WriteConsoleInputA( HANDLE handle, const INPUT_RECORD *buffer,
400 DWORD count, LPDWORD written )
402 INPUT_RECORD *recW;
403 BOOL ret;
405 if (!(recW = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*recW) ))) return FALSE;
406 memcpy( recW, buffer, count*sizeof(*recW) );
407 input_records_AtoW( recW, count );
408 ret = WriteConsoleInputW( handle, recW, count, written );
409 HeapFree( GetProcessHeap(), 0, recW );
410 return ret;
414 /******************************************************************************
415 * WriteConsoleInputW [KERNEL32.@]
417 BOOL WINAPI WriteConsoleInputW( HANDLE handle, const INPUT_RECORD *buffer,
418 DWORD count, LPDWORD written )
420 BOOL ret;
422 TRACE("(%p,%p,%d,%p)\n", handle, buffer, count, written);
424 if (written) *written = 0;
425 SERVER_START_REQ( write_console_input )
427 req->handle = console_handle_unmap(handle);
428 wine_server_add_data( req, buffer, count * sizeof(INPUT_RECORD) );
429 if ((ret = !wine_server_call_err( req )) && written)
430 *written = reply->written;
432 SERVER_END_REQ;
434 return ret;
438 /***********************************************************************
439 * WriteConsoleOutputA (KERNEL32.@)
441 BOOL WINAPI WriteConsoleOutputA( HANDLE hConsoleOutput, const CHAR_INFO *lpBuffer,
442 COORD size, COORD coord, LPSMALL_RECT region )
444 int y;
445 BOOL ret;
446 COORD new_size, new_coord;
447 CHAR_INFO *ciw;
449 new_size.X = min( region->Right - region->Left + 1, size.X - coord.X );
450 new_size.Y = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
452 if (new_size.X <= 0 || new_size.Y <= 0)
454 region->Bottom = region->Top + new_size.Y - 1;
455 region->Right = region->Left + new_size.X - 1;
456 return TRUE;
459 /* only copy the useful rectangle */
460 if (!(ciw = HeapAlloc( GetProcessHeap(), 0, sizeof(CHAR_INFO) * new_size.X * new_size.Y )))
461 return FALSE;
462 for (y = 0; y < new_size.Y; y++)
464 memcpy( &ciw[y * new_size.X], &lpBuffer[(y + coord.Y) * size.X + coord.X],
465 new_size.X * sizeof(CHAR_INFO) );
466 char_info_AtoW( &ciw[ y * new_size.X ], new_size.X );
468 new_coord.X = new_coord.Y = 0;
469 ret = WriteConsoleOutputW( hConsoleOutput, ciw, new_size, new_coord, region );
470 HeapFree( GetProcessHeap(), 0, ciw );
471 return ret;
475 /***********************************************************************
476 * WriteConsoleOutputW (KERNEL32.@)
478 BOOL WINAPI WriteConsoleOutputW( HANDLE hConsoleOutput, const CHAR_INFO *lpBuffer,
479 COORD size, COORD coord, LPSMALL_RECT region )
481 int width, height, y;
482 BOOL ret = TRUE;
484 TRACE("(%p,%p,(%d,%d),(%d,%d),(%d,%dx%d,%d)\n",
485 hConsoleOutput, lpBuffer, size.X, size.Y, coord.X, coord.Y,
486 region->Left, region->Top, region->Right, region->Bottom);
488 width = min( region->Right - region->Left + 1, size.X - coord.X );
489 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
491 if (width > 0 && height > 0)
493 for (y = 0; y < height; y++)
495 SERVER_START_REQ( write_console_output )
497 req->handle = console_handle_unmap(hConsoleOutput);
498 req->x = region->Left;
499 req->y = region->Top + y;
500 req->mode = CHAR_INFO_MODE_TEXTATTR;
501 req->wrap = FALSE;
502 wine_server_add_data( req, &lpBuffer[(y + coord.Y) * size.X + coord.X],
503 width * sizeof(CHAR_INFO));
504 if ((ret = !wine_server_call_err( req )))
506 width = min( width, reply->width - region->Left );
507 height = min( height, reply->height - region->Top );
510 SERVER_END_REQ;
511 if (!ret) break;
514 region->Bottom = region->Top + height - 1;
515 region->Right = region->Left + width - 1;
516 return ret;
520 /******************************************************************************
521 * WriteConsoleOutputCharacterA [KERNEL32.@]
523 * See WriteConsoleOutputCharacterW.
525 BOOL WINAPI WriteConsoleOutputCharacterA( HANDLE hConsoleOutput, LPCSTR str, DWORD length,
526 COORD coord, LPDWORD lpNumCharsWritten )
528 BOOL ret;
529 LPWSTR strW;
530 DWORD lenW;
532 TRACE("(%p,%s,%d,%dx%d,%p)\n", hConsoleOutput,
533 debugstr_an(str, length), length, coord.X, coord.Y, lpNumCharsWritten);
535 lenW = MultiByteToWideChar( GetConsoleOutputCP(), 0, str, length, NULL, 0 );
537 if (lpNumCharsWritten) *lpNumCharsWritten = 0;
539 if (!(strW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) ))) return FALSE;
540 MultiByteToWideChar( GetConsoleOutputCP(), 0, str, length, strW, lenW );
542 ret = WriteConsoleOutputCharacterW( hConsoleOutput, strW, lenW, coord, lpNumCharsWritten );
543 HeapFree( GetProcessHeap(), 0, strW );
544 return ret;
548 /******************************************************************************
549 * WriteConsoleOutputAttribute [KERNEL32.@] Sets attributes for some cells in
550 * the console screen buffer
552 * PARAMS
553 * hConsoleOutput [I] Handle to screen buffer
554 * attr [I] Pointer to buffer with write attributes
555 * length [I] Number of cells to write to
556 * coord [I] Coords of first cell
557 * lpNumAttrsWritten [O] Pointer to number of cells written
559 * RETURNS
560 * Success: TRUE
561 * Failure: FALSE
564 BOOL WINAPI WriteConsoleOutputAttribute( HANDLE hConsoleOutput, CONST WORD *attr, DWORD length,
565 COORD coord, LPDWORD lpNumAttrsWritten )
567 BOOL ret;
569 TRACE("(%p,%p,%d,%dx%d,%p)\n", hConsoleOutput,attr,length,coord.X,coord.Y,lpNumAttrsWritten);
571 SERVER_START_REQ( write_console_output )
573 req->handle = console_handle_unmap(hConsoleOutput);
574 req->x = coord.X;
575 req->y = coord.Y;
576 req->mode = CHAR_INFO_MODE_ATTR;
577 req->wrap = TRUE;
578 wine_server_add_data( req, attr, length * sizeof(WORD) );
579 if ((ret = !wine_server_call_err( req )))
581 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
584 SERVER_END_REQ;
585 return ret;
589 /******************************************************************************
590 * FillConsoleOutputCharacterA [KERNEL32.@]
592 * See FillConsoleOutputCharacterW.
594 BOOL WINAPI FillConsoleOutputCharacterA( HANDLE hConsoleOutput, CHAR ch, DWORD length,
595 COORD coord, LPDWORD lpNumCharsWritten )
597 WCHAR wch;
599 MultiByteToWideChar( GetConsoleOutputCP(), 0, &ch, 1, &wch, 1 );
600 return FillConsoleOutputCharacterW(hConsoleOutput, wch, length, coord, lpNumCharsWritten);
604 /******************************************************************************
605 * FillConsoleOutputCharacterW [KERNEL32.@] Writes characters to console
607 * PARAMS
608 * hConsoleOutput [I] Handle to screen buffer
609 * ch [I] Character to write
610 * length [I] Number of cells to write to
611 * coord [I] Coords of first cell
612 * lpNumCharsWritten [O] Pointer to number of cells written
614 * RETURNS
615 * Success: TRUE
616 * Failure: FALSE
618 BOOL WINAPI FillConsoleOutputCharacterW( HANDLE hConsoleOutput, WCHAR ch, DWORD length,
619 COORD coord, LPDWORD lpNumCharsWritten)
621 BOOL ret;
623 TRACE("(%p,%s,%d,(%dx%d),%p)\n",
624 hConsoleOutput, debugstr_wn(&ch, 1), length, coord.X, coord.Y, lpNumCharsWritten);
626 SERVER_START_REQ( fill_console_output )
628 req->handle = console_handle_unmap(hConsoleOutput);
629 req->x = coord.X;
630 req->y = coord.Y;
631 req->mode = CHAR_INFO_MODE_TEXT;
632 req->wrap = TRUE;
633 req->data.ch = ch;
634 req->count = length;
635 if ((ret = !wine_server_call_err( req )))
637 if (lpNumCharsWritten) *lpNumCharsWritten = reply->written;
640 SERVER_END_REQ;
641 return ret;
645 /******************************************************************************
646 * FillConsoleOutputAttribute [KERNEL32.@] Sets attributes for console
648 * PARAMS
649 * hConsoleOutput [I] Handle to screen buffer
650 * attr [I] Color attribute to write
651 * length [I] Number of cells to write to
652 * coord [I] Coords of first cell
653 * lpNumAttrsWritten [O] Pointer to number of cells written
655 * RETURNS
656 * Success: TRUE
657 * Failure: FALSE
659 BOOL WINAPI FillConsoleOutputAttribute( HANDLE hConsoleOutput, WORD attr, DWORD length,
660 COORD coord, LPDWORD lpNumAttrsWritten )
662 BOOL ret;
664 TRACE("(%p,%d,%d,(%dx%d),%p)\n",
665 hConsoleOutput, attr, length, coord.X, coord.Y, lpNumAttrsWritten);
667 SERVER_START_REQ( fill_console_output )
669 req->handle = console_handle_unmap(hConsoleOutput);
670 req->x = coord.X;
671 req->y = coord.Y;
672 req->mode = CHAR_INFO_MODE_ATTR;
673 req->wrap = TRUE;
674 req->data.attr = attr;
675 req->count = length;
676 if ((ret = !wine_server_call_err( req )))
678 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
681 SERVER_END_REQ;
682 return ret;
686 /******************************************************************************
687 * ReadConsoleOutputCharacterA [KERNEL32.@]
690 BOOL WINAPI ReadConsoleOutputCharacterA(HANDLE hConsoleOutput, LPSTR lpstr, DWORD count,
691 COORD coord, LPDWORD read_count)
693 DWORD read;
694 BOOL ret;
695 LPWSTR wptr = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
697 if (read_count) *read_count = 0;
698 if (!wptr) return FALSE;
700 if ((ret = ReadConsoleOutputCharacterW( hConsoleOutput, wptr, count, coord, &read )))
702 read = WideCharToMultiByte( GetConsoleOutputCP(), 0, wptr, read, lpstr, count, NULL, NULL);
703 if (read_count) *read_count = read;
705 HeapFree( GetProcessHeap(), 0, wptr );
706 return ret;
710 /******************************************************************************
711 * ReadConsoleOutputCharacterW [KERNEL32.@]
714 BOOL WINAPI ReadConsoleOutputCharacterW( HANDLE hConsoleOutput, LPWSTR buffer, DWORD count,
715 COORD coord, LPDWORD read_count )
717 BOOL ret;
719 TRACE( "(%p,%p,%d,%dx%d,%p)\n", hConsoleOutput, buffer, count, coord.X, coord.Y, read_count );
721 SERVER_START_REQ( read_console_output )
723 req->handle = console_handle_unmap(hConsoleOutput);
724 req->x = coord.X;
725 req->y = coord.Y;
726 req->mode = CHAR_INFO_MODE_TEXT;
727 req->wrap = TRUE;
728 wine_server_set_reply( req, buffer, count * sizeof(WCHAR) );
729 if ((ret = !wine_server_call_err( req )))
731 if (read_count) *read_count = wine_server_reply_size(reply) / sizeof(WCHAR);
734 SERVER_END_REQ;
735 return ret;
739 /******************************************************************************
740 * ReadConsoleOutputAttribute [KERNEL32.@]
742 BOOL WINAPI ReadConsoleOutputAttribute(HANDLE hConsoleOutput, LPWORD lpAttribute, DWORD length,
743 COORD coord, LPDWORD read_count)
745 BOOL ret;
747 TRACE("(%p,%p,%d,%dx%d,%p)\n",
748 hConsoleOutput, lpAttribute, length, coord.X, coord.Y, read_count);
750 SERVER_START_REQ( read_console_output )
752 req->handle = console_handle_unmap(hConsoleOutput);
753 req->x = coord.X;
754 req->y = coord.Y;
755 req->mode = CHAR_INFO_MODE_ATTR;
756 req->wrap = TRUE;
757 wine_server_set_reply( req, lpAttribute, length * sizeof(WORD) );
758 if ((ret = !wine_server_call_err( req )))
760 if (read_count) *read_count = wine_server_reply_size(reply) / sizeof(WORD);
763 SERVER_END_REQ;
764 return ret;
768 /******************************************************************************
769 * ReadConsoleOutputA [KERNEL32.@]
772 BOOL WINAPI ReadConsoleOutputA( HANDLE hConsoleOutput, LPCHAR_INFO lpBuffer, COORD size,
773 COORD coord, LPSMALL_RECT region )
775 BOOL ret;
776 int y;
778 ret = ReadConsoleOutputW( hConsoleOutput, lpBuffer, size, coord, region );
779 if (ret && region->Right >= region->Left)
781 for (y = 0; y <= region->Bottom - region->Top; y++)
783 char_info_WtoA( &lpBuffer[(coord.Y + y) * size.X + coord.X],
784 region->Right - region->Left + 1 );
787 return ret;
791 /******************************************************************************
792 * ReadConsoleOutputW [KERNEL32.@]
794 * NOTE: The NT4 (sp5) kernel crashes on me if size is (0,0). I don't
795 * think we need to be *that* compatible. -- AJ
797 BOOL WINAPI ReadConsoleOutputW( HANDLE hConsoleOutput, LPCHAR_INFO lpBuffer, COORD size,
798 COORD coord, LPSMALL_RECT region )
800 int width, height, y;
801 BOOL ret = TRUE;
803 width = min( region->Right - region->Left + 1, size.X - coord.X );
804 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
806 if (width > 0 && height > 0)
808 for (y = 0; y < height; y++)
810 SERVER_START_REQ( read_console_output )
812 req->handle = console_handle_unmap(hConsoleOutput);
813 req->x = region->Left;
814 req->y = region->Top + y;
815 req->mode = CHAR_INFO_MODE_TEXTATTR;
816 req->wrap = FALSE;
817 wine_server_set_reply( req, &lpBuffer[(y+coord.Y) * size.X + coord.X],
818 width * sizeof(CHAR_INFO) );
819 if ((ret = !wine_server_call_err( req )))
821 width = min( width, reply->width - region->Left );
822 height = min( height, reply->height - region->Top );
825 SERVER_END_REQ;
826 if (!ret) break;
829 region->Bottom = region->Top + height - 1;
830 region->Right = region->Left + width - 1;
831 return ret;
835 /******************************************************************************
836 * ReadConsoleInputA [KERNEL32.@] Reads data from a console
838 * PARAMS
839 * handle [I] Handle to console input buffer
840 * buffer [O] Address of buffer for read data
841 * count [I] Number of records to read
842 * pRead [O] Address of number of records read
844 * RETURNS
845 * Success: TRUE
846 * Failure: FALSE
848 BOOL WINAPI ReadConsoleInputA( HANDLE handle, PINPUT_RECORD buffer, DWORD count, LPDWORD pRead )
850 DWORD read;
852 if (!ReadConsoleInputW( handle, buffer, count, &read )) return FALSE;
853 input_records_WtoA( buffer, read );
854 if (pRead) *pRead = read;
855 return TRUE;
859 /***********************************************************************
860 * PeekConsoleInputA (KERNEL32.@)
862 * Gets 'count' first events (or less) from input queue.
864 BOOL WINAPI PeekConsoleInputA( HANDLE handle, PINPUT_RECORD buffer, DWORD count, LPDWORD pRead )
866 DWORD read;
868 if (!PeekConsoleInputW( handle, buffer, count, &read )) return FALSE;
869 input_records_WtoA( buffer, read );
870 if (pRead) *pRead = read;
871 return TRUE;
875 /***********************************************************************
876 * PeekConsoleInputW (KERNEL32.@)
878 BOOL WINAPI PeekConsoleInputW( HANDLE handle, PINPUT_RECORD buffer, DWORD count, LPDWORD read )
880 BOOL ret;
881 SERVER_START_REQ( read_console_input )
883 req->handle = console_handle_unmap(handle);
884 req->flush = FALSE;
885 wine_server_set_reply( req, buffer, count * sizeof(INPUT_RECORD) );
886 if ((ret = !wine_server_call_err( req )))
888 if (read) *read = count ? reply->read : 0;
891 SERVER_END_REQ;
892 return ret;
896 /***********************************************************************
897 * GetNumberOfConsoleInputEvents (KERNEL32.@)
899 BOOL WINAPI GetNumberOfConsoleInputEvents( HANDLE handle, LPDWORD nrofevents )
901 BOOL ret;
902 SERVER_START_REQ( read_console_input )
904 req->handle = console_handle_unmap(handle);
905 req->flush = FALSE;
906 if ((ret = !wine_server_call_err( req )))
908 if (nrofevents) *nrofevents = reply->read;
911 SERVER_END_REQ;
912 return ret;
916 /******************************************************************************
917 * read_console_input
919 * Helper function for ReadConsole, ReadConsoleInput and FlushConsoleInputBuffer
921 * Returns
922 * 0 for error, 1 for no INPUT_RECORD ready, 2 with INPUT_RECORD ready
924 enum read_console_input_return {rci_error = 0, rci_timeout = 1, rci_gotone = 2};
925 static enum read_console_input_return read_console_input(HANDLE handle, PINPUT_RECORD ir, DWORD timeout)
927 enum read_console_input_return ret;
929 if (WaitForSingleObject(GetConsoleInputWaitHandle(), timeout) != WAIT_OBJECT_0)
930 return rci_timeout;
931 SERVER_START_REQ( read_console_input )
933 req->handle = console_handle_unmap(handle);
934 req->flush = TRUE;
935 wine_server_set_reply( req, ir, sizeof(INPUT_RECORD) );
936 if (wine_server_call_err( req ) || !reply->read) ret = rci_error;
937 else ret = rci_gotone;
939 SERVER_END_REQ;
941 return ret;
945 /***********************************************************************
946 * FlushConsoleInputBuffer (KERNEL32.@)
948 BOOL WINAPI FlushConsoleInputBuffer( HANDLE handle )
950 enum read_console_input_return last;
951 INPUT_RECORD ir;
953 while ((last = read_console_input(handle, &ir, 0)) == rci_gotone);
955 return last == rci_timeout;
959 /***********************************************************************
960 * SetConsoleTitleA (KERNEL32.@)
962 BOOL WINAPI SetConsoleTitleA( LPCSTR title )
964 LPWSTR titleW;
965 BOOL ret;
967 DWORD len = MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, NULL, 0 );
968 if (!(titleW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)))) return FALSE;
969 MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, titleW, len );
970 ret = SetConsoleTitleW(titleW);
971 HeapFree(GetProcessHeap(), 0, titleW);
972 return ret;
976 /***********************************************************************
977 * GetConsoleKeyboardLayoutNameA (KERNEL32.@)
979 BOOL WINAPI GetConsoleKeyboardLayoutNameA(LPSTR layoutName)
981 FIXME( "stub %p\n", layoutName);
982 return TRUE;
985 /***********************************************************************
986 * GetConsoleKeyboardLayoutNameW (KERNEL32.@)
988 BOOL WINAPI GetConsoleKeyboardLayoutNameW(LPWSTR layoutName)
990 FIXME( "stub %p\n", layoutName);
991 return TRUE;
994 static WCHAR input_exe[MAX_PATH + 1];
996 /***********************************************************************
997 * GetConsoleInputExeNameW (KERNEL32.@)
999 BOOL WINAPI GetConsoleInputExeNameW(DWORD buflen, LPWSTR buffer)
1001 TRACE("%u %p\n", buflen, buffer);
1003 RtlEnterCriticalSection(&CONSOLE_CritSect);
1004 if (buflen > strlenW(input_exe)) strcpyW(buffer, input_exe);
1005 else SetLastError(ERROR_BUFFER_OVERFLOW);
1006 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1008 return TRUE;
1011 /***********************************************************************
1012 * GetConsoleInputExeNameA (KERNEL32.@)
1014 BOOL WINAPI GetConsoleInputExeNameA(DWORD buflen, LPSTR buffer)
1016 TRACE("%u %p\n", buflen, buffer);
1018 RtlEnterCriticalSection(&CONSOLE_CritSect);
1019 if (WideCharToMultiByte(CP_ACP, 0, input_exe, -1, NULL, 0, NULL, NULL) <= buflen)
1020 WideCharToMultiByte(CP_ACP, 0, input_exe, -1, buffer, buflen, NULL, NULL);
1021 else SetLastError(ERROR_BUFFER_OVERFLOW);
1022 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1024 return TRUE;
1027 /***********************************************************************
1028 * GetConsoleTitleA (KERNEL32.@)
1030 * See GetConsoleTitleW.
1032 DWORD WINAPI GetConsoleTitleA(LPSTR title, DWORD size)
1034 WCHAR *ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * size);
1035 DWORD ret;
1037 if (!ptr) return 0;
1038 ret = GetConsoleTitleW( ptr, size );
1039 if (ret)
1041 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr, ret + 1, title, size, NULL, NULL);
1042 ret = strlen(title);
1044 HeapFree(GetProcessHeap(), 0, ptr);
1045 return ret;
1049 /******************************************************************************
1050 * GetConsoleTitleW [KERNEL32.@] Retrieves title string for console
1052 * PARAMS
1053 * title [O] Address of buffer for title
1054 * size [I] Size of buffer
1056 * RETURNS
1057 * Success: Length of string copied
1058 * Failure: 0
1060 DWORD WINAPI GetConsoleTitleW(LPWSTR title, DWORD size)
1062 DWORD ret = 0;
1064 SERVER_START_REQ( get_console_input_info )
1066 req->handle = 0;
1067 wine_server_set_reply( req, title, (size-1) * sizeof(WCHAR) );
1068 if (!wine_server_call_err( req ))
1070 ret = wine_server_reply_size(reply) / sizeof(WCHAR);
1071 title[ret] = 0;
1074 SERVER_END_REQ;
1075 return ret;
1079 /***********************************************************************
1080 * GetLargestConsoleWindowSize (KERNEL32.@)
1082 * NOTE
1083 * This should return a COORD, but calling convention for returning
1084 * structures is different between Windows and gcc on i386.
1086 * VERSION: [i386]
1088 #ifdef __i386__
1089 #undef GetLargestConsoleWindowSize
1090 DWORD WINAPI GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
1092 union {
1093 COORD c;
1094 DWORD w;
1095 } x;
1096 x.c.X = 80;
1097 x.c.Y = 24;
1098 TRACE("(%p), returning %dx%d (%x)\n", hConsoleOutput, x.c.X, x.c.Y, x.w);
1099 return x.w;
1101 #endif /* defined(__i386__) */
1104 /***********************************************************************
1105 * GetLargestConsoleWindowSize (KERNEL32.@)
1107 * NOTE
1108 * This should return a COORD, but calling convention for returning
1109 * structures is different between Windows and gcc on i386.
1111 * VERSION: [!i386]
1113 #ifndef __i386__
1114 COORD WINAPI GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
1116 COORD c;
1117 c.X = 80;
1118 c.Y = 24;
1119 TRACE("(%p), returning %dx%d\n", hConsoleOutput, c.X, c.Y);
1120 return c;
1122 #endif /* defined(__i386__) */
1124 static WCHAR* S_EditString /* = NULL */;
1125 static unsigned S_EditStrPos /* = 0 */;
1127 /***********************************************************************
1128 * FreeConsole (KERNEL32.@)
1130 BOOL WINAPI FreeConsole(VOID)
1132 BOOL ret;
1134 /* invalidate local copy of input event handle */
1135 console_wait_event = 0;
1137 SERVER_START_REQ(free_console)
1139 ret = !wine_server_call_err( req );
1141 SERVER_END_REQ;
1142 return ret;
1145 /******************************************************************
1146 * start_console_renderer
1148 * helper for AllocConsole
1149 * starts the renderer process
1151 static BOOL start_console_renderer_helper(const char* appname, STARTUPINFOA* si,
1152 HANDLE hEvent)
1154 char buffer[1024];
1155 int ret;
1156 PROCESS_INFORMATION pi;
1158 /* FIXME: use dynamic allocation for most of the buffers below */
1159 ret = snprintf(buffer, sizeof(buffer), "%s --use-event=%ld", appname, (DWORD_PTR)hEvent);
1160 if ((ret > -1) && (ret < sizeof(buffer)) &&
1161 CreateProcessA(NULL, buffer, NULL, NULL, TRUE, DETACHED_PROCESS,
1162 NULL, NULL, si, &pi))
1164 if (WaitForSingleObject(hEvent, INFINITE) != WAIT_OBJECT_0) return FALSE;
1166 TRACE("Started wineconsole pid=%08x tid=%08x\n",
1167 pi.dwProcessId, pi.dwThreadId);
1169 return TRUE;
1171 return FALSE;
1174 static BOOL start_console_renderer(STARTUPINFOA* si)
1176 HANDLE hEvent = 0;
1177 LPSTR p;
1178 OBJECT_ATTRIBUTES attr;
1179 BOOL ret = FALSE;
1181 attr.Length = sizeof(attr);
1182 attr.RootDirectory = 0;
1183 attr.Attributes = OBJ_INHERIT;
1184 attr.ObjectName = NULL;
1185 attr.SecurityDescriptor = NULL;
1186 attr.SecurityQualityOfService = NULL;
1188 NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, &attr, TRUE, FALSE);
1189 if (!hEvent) return FALSE;
1191 /* first try environment variable */
1192 if ((p = getenv("WINECONSOLE")) != NULL)
1194 ret = start_console_renderer_helper(p, si, hEvent);
1195 if (!ret)
1196 ERR("Couldn't launch Wine console from WINECONSOLE env var (%s)... "
1197 "trying default access\n", p);
1200 /* then try the regular PATH */
1201 if (!ret)
1202 ret = start_console_renderer_helper("wineconsole", si, hEvent);
1204 CloseHandle(hEvent);
1205 return ret;
1208 /***********************************************************************
1209 * AllocConsole (KERNEL32.@)
1211 * creates an xterm with a pty to our program
1213 BOOL WINAPI AllocConsole(void)
1215 HANDLE handle_in = INVALID_HANDLE_VALUE;
1216 HANDLE handle_out = INVALID_HANDLE_VALUE;
1217 HANDLE handle_err = INVALID_HANDLE_VALUE;
1218 STARTUPINFOA siCurrent;
1219 STARTUPINFOA siConsole;
1220 char buffer[1024];
1222 TRACE("()\n");
1224 handle_in = OpenConsoleW( coninW, GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,
1225 FALSE, OPEN_EXISTING );
1227 if (VerifyConsoleIoHandle(handle_in))
1229 /* we already have a console opened on this process, don't create a new one */
1230 CloseHandle(handle_in);
1231 return FALSE;
1233 /* happens when we're running on a Unix console */
1234 if (handle_in != INVALID_HANDLE_VALUE) CloseHandle(handle_in);
1236 /* invalidate local copy of input event handle */
1237 console_wait_event = 0;
1239 GetStartupInfoA(&siCurrent);
1241 memset(&siConsole, 0, sizeof(siConsole));
1242 siConsole.cb = sizeof(siConsole);
1243 /* setup a view arguments for wineconsole (it'll use them as default values) */
1244 if (siCurrent.dwFlags & STARTF_USECOUNTCHARS)
1246 siConsole.dwFlags |= STARTF_USECOUNTCHARS;
1247 siConsole.dwXCountChars = siCurrent.dwXCountChars;
1248 siConsole.dwYCountChars = siCurrent.dwYCountChars;
1250 if (siCurrent.dwFlags & STARTF_USEFILLATTRIBUTE)
1252 siConsole.dwFlags |= STARTF_USEFILLATTRIBUTE;
1253 siConsole.dwFillAttribute = siCurrent.dwFillAttribute;
1255 if (siCurrent.dwFlags & STARTF_USESHOWWINDOW)
1257 siConsole.dwFlags |= STARTF_USESHOWWINDOW;
1258 siConsole.wShowWindow = siCurrent.wShowWindow;
1260 /* FIXME (should pass the unicode form) */
1261 if (siCurrent.lpTitle)
1262 siConsole.lpTitle = siCurrent.lpTitle;
1263 else if (GetModuleFileNameA(0, buffer, sizeof(buffer)))
1265 buffer[sizeof(buffer) - 1] = '\0';
1266 siConsole.lpTitle = buffer;
1269 if (!start_console_renderer(&siConsole))
1270 goto the_end;
1272 if( !(siCurrent.dwFlags & STARTF_USESTDHANDLES) ) {
1273 /* all std I/O handles are inheritable by default */
1274 handle_in = OpenConsoleW( coninW, GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,
1275 TRUE, OPEN_EXISTING );
1276 if (handle_in == INVALID_HANDLE_VALUE) goto the_end;
1278 handle_out = OpenConsoleW( conoutW, GENERIC_READ|GENERIC_WRITE,
1279 TRUE, OPEN_EXISTING );
1280 if (handle_out == INVALID_HANDLE_VALUE) goto the_end;
1282 if (!DuplicateHandle(GetCurrentProcess(), handle_out, GetCurrentProcess(),
1283 &handle_err, 0, TRUE, DUPLICATE_SAME_ACCESS))
1284 goto the_end;
1285 } else {
1286 /* STARTF_USESTDHANDLES flag: use handles from StartupInfo */
1287 handle_in = siCurrent.hStdInput;
1288 handle_out = siCurrent.hStdOutput;
1289 handle_err = siCurrent.hStdError;
1292 /* NT resets the STD_*_HANDLEs on console alloc */
1293 SetStdHandle(STD_INPUT_HANDLE, handle_in);
1294 SetStdHandle(STD_OUTPUT_HANDLE, handle_out);
1295 SetStdHandle(STD_ERROR_HANDLE, handle_err);
1297 SetLastError(ERROR_SUCCESS);
1299 return TRUE;
1301 the_end:
1302 ERR("Can't allocate console\n");
1303 if (handle_in != INVALID_HANDLE_VALUE) CloseHandle(handle_in);
1304 if (handle_out != INVALID_HANDLE_VALUE) CloseHandle(handle_out);
1305 if (handle_err != INVALID_HANDLE_VALUE) CloseHandle(handle_err);
1306 FreeConsole();
1307 return FALSE;
1311 /***********************************************************************
1312 * ReadConsoleA (KERNEL32.@)
1314 BOOL WINAPI ReadConsoleA(HANDLE hConsoleInput, LPVOID lpBuffer, DWORD nNumberOfCharsToRead,
1315 LPDWORD lpNumberOfCharsRead, LPVOID lpReserved)
1317 LPWSTR ptr = HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead * sizeof(WCHAR));
1318 DWORD ncr = 0;
1319 BOOL ret;
1321 if ((ret = ReadConsoleW(hConsoleInput, ptr, nNumberOfCharsToRead, &ncr, NULL)))
1322 ncr = WideCharToMultiByte(GetConsoleCP(), 0, ptr, ncr, lpBuffer, nNumberOfCharsToRead, NULL, NULL);
1324 if (lpNumberOfCharsRead) *lpNumberOfCharsRead = ncr;
1325 HeapFree(GetProcessHeap(), 0, ptr);
1327 return ret;
1330 /***********************************************************************
1331 * ReadConsoleW (KERNEL32.@)
1333 BOOL WINAPI ReadConsoleW(HANDLE hConsoleInput, LPVOID lpBuffer,
1334 DWORD nNumberOfCharsToRead, LPDWORD lpNumberOfCharsRead, LPVOID lpReserved)
1336 DWORD charsread;
1337 LPWSTR xbuf = (LPWSTR)lpBuffer;
1338 DWORD mode;
1340 TRACE("(%p,%p,%d,%p,%p)\n",
1341 hConsoleInput, lpBuffer, nNumberOfCharsToRead, lpNumberOfCharsRead, lpReserved);
1343 if (!GetConsoleMode(hConsoleInput, &mode))
1344 return FALSE;
1346 if (mode & ENABLE_LINE_INPUT)
1348 if (!S_EditString || S_EditString[S_EditStrPos] == 0)
1350 HeapFree(GetProcessHeap(), 0, S_EditString);
1351 if (!(S_EditString = CONSOLE_Readline(hConsoleInput)))
1352 return FALSE;
1353 S_EditStrPos = 0;
1355 charsread = lstrlenW(&S_EditString[S_EditStrPos]);
1356 if (charsread > nNumberOfCharsToRead) charsread = nNumberOfCharsToRead;
1357 memcpy(xbuf, &S_EditString[S_EditStrPos], charsread * sizeof(WCHAR));
1358 S_EditStrPos += charsread;
1360 else
1362 INPUT_RECORD ir;
1363 DWORD timeout = INFINITE;
1365 /* FIXME: should we read at least 1 char? The SDK does not say */
1366 /* wait for at least one available input record (it doesn't mean we'll have
1367 * chars stored in xbuf...)
1369 * Although SDK doc keeps silence about 1 char, SDK examples assume
1370 * that we should wait for at least one character (not key). --KS
1372 charsread = 0;
1375 if (read_console_input(hConsoleInput, &ir, timeout) != rci_gotone) break;
1376 if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown &&
1377 ir.Event.KeyEvent.uChar.UnicodeChar &&
1378 !(ir.Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
1380 xbuf[charsread++] = ir.Event.KeyEvent.uChar.UnicodeChar;
1381 timeout = 0;
1383 } while (charsread < nNumberOfCharsToRead);
1384 /* nothing has been read */
1385 if (timeout == INFINITE) return FALSE;
1388 if (lpNumberOfCharsRead) *lpNumberOfCharsRead = charsread;
1390 return TRUE;
1394 /***********************************************************************
1395 * ReadConsoleInputW (KERNEL32.@)
1397 BOOL WINAPI ReadConsoleInputW(HANDLE hConsoleInput, PINPUT_RECORD lpBuffer,
1398 DWORD nLength, LPDWORD lpNumberOfEventsRead)
1400 DWORD idx = 0;
1401 DWORD timeout = INFINITE;
1403 if (!nLength)
1405 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = 0;
1406 return TRUE;
1409 /* loop until we get at least one event */
1410 while (read_console_input(hConsoleInput, &lpBuffer[idx], timeout) == rci_gotone &&
1411 ++idx < nLength)
1412 timeout = 0;
1414 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = idx;
1415 return idx != 0;
1419 /******************************************************************************
1420 * WriteConsoleOutputCharacterW [KERNEL32.@]
1422 * Copy character to consecutive cells in the console screen buffer.
1424 * PARAMS
1425 * hConsoleOutput [I] Handle to screen buffer
1426 * str [I] Pointer to buffer with chars to write
1427 * length [I] Number of cells to write to
1428 * coord [I] Coords of first cell
1429 * lpNumCharsWritten [O] Pointer to number of cells written
1431 * RETURNS
1432 * Success: TRUE
1433 * Failure: FALSE
1436 BOOL WINAPI WriteConsoleOutputCharacterW( HANDLE hConsoleOutput, LPCWSTR str, DWORD length,
1437 COORD coord, LPDWORD lpNumCharsWritten )
1439 BOOL ret;
1441 TRACE("(%p,%s,%d,%dx%d,%p)\n", hConsoleOutput,
1442 debugstr_wn(str, length), length, coord.X, coord.Y, lpNumCharsWritten);
1444 SERVER_START_REQ( write_console_output )
1446 req->handle = console_handle_unmap(hConsoleOutput);
1447 req->x = coord.X;
1448 req->y = coord.Y;
1449 req->mode = CHAR_INFO_MODE_TEXT;
1450 req->wrap = TRUE;
1451 wine_server_add_data( req, str, length * sizeof(WCHAR) );
1452 if ((ret = !wine_server_call_err( req )))
1454 if (lpNumCharsWritten) *lpNumCharsWritten = reply->written;
1457 SERVER_END_REQ;
1458 return ret;
1462 /******************************************************************************
1463 * SetConsoleTitleW [KERNEL32.@] Sets title bar string for console
1465 * PARAMS
1466 * title [I] Address of new title
1468 * RETURNS
1469 * Success: TRUE
1470 * Failure: FALSE
1472 BOOL WINAPI SetConsoleTitleW(LPCWSTR title)
1474 BOOL ret;
1476 TRACE("(%s)\n", debugstr_w(title));
1477 SERVER_START_REQ( set_console_input_info )
1479 req->handle = 0;
1480 req->mask = SET_CONSOLE_INPUT_INFO_TITLE;
1481 wine_server_add_data( req, title, strlenW(title) * sizeof(WCHAR) );
1482 ret = !wine_server_call_err( req );
1484 SERVER_END_REQ;
1485 return ret;
1489 /***********************************************************************
1490 * GetNumberOfConsoleMouseButtons (KERNEL32.@)
1492 BOOL WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
1494 FIXME("(%p): stub\n", nrofbuttons);
1495 *nrofbuttons = 2;
1496 return TRUE;
1499 /******************************************************************************
1500 * SetConsoleInputExeNameW [KERNEL32.@]
1502 BOOL WINAPI SetConsoleInputExeNameW(LPCWSTR name)
1504 TRACE("(%s)\n", debugstr_w(name));
1506 if (!name || !name[0])
1508 SetLastError(ERROR_INVALID_PARAMETER);
1509 return FALSE;
1512 RtlEnterCriticalSection(&CONSOLE_CritSect);
1513 if (strlenW(name) < sizeof(input_exe)/sizeof(WCHAR)) strcpyW(input_exe, name);
1514 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1516 return TRUE;
1519 /******************************************************************************
1520 * SetConsoleInputExeNameA [KERNEL32.@]
1522 BOOL WINAPI SetConsoleInputExeNameA(LPCSTR name)
1524 int len;
1525 LPWSTR nameW;
1526 BOOL ret;
1528 if (!name || !name[0])
1530 SetLastError(ERROR_INVALID_PARAMETER);
1531 return FALSE;
1534 len = MultiByteToWideChar(CP_ACP, 0, name, -1, NULL, 0);
1535 if (!(nameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)))) return FALSE;
1537 MultiByteToWideChar(CP_ACP, 0, name, -1, nameW, len);
1538 ret = SetConsoleInputExeNameW(nameW);
1539 HeapFree(GetProcessHeap(), 0, nameW);
1541 return ret;
1544 /******************************************************************
1545 * CONSOLE_DefaultHandler
1547 * Final control event handler
1549 static BOOL WINAPI CONSOLE_DefaultHandler(DWORD dwCtrlType)
1551 FIXME("Terminating process %x on event %x\n", GetCurrentProcessId(), dwCtrlType);
1552 ExitProcess(0);
1553 /* should never go here */
1554 return TRUE;
1557 /******************************************************************************
1558 * SetConsoleCtrlHandler [KERNEL32.@] Adds function to calling process list
1560 * PARAMS
1561 * func [I] Address of handler function
1562 * add [I] Handler to add or remove
1564 * RETURNS
1565 * Success: TRUE
1566 * Failure: FALSE
1569 struct ConsoleHandler
1571 PHANDLER_ROUTINE handler;
1572 struct ConsoleHandler* next;
1575 static struct ConsoleHandler CONSOLE_DefaultConsoleHandler = {CONSOLE_DefaultHandler, NULL};
1576 static struct ConsoleHandler* CONSOLE_Handlers = &CONSOLE_DefaultConsoleHandler;
1578 /*****************************************************************************/
1580 /******************************************************************
1581 * SetConsoleCtrlHandler (KERNEL32.@)
1583 BOOL WINAPI SetConsoleCtrlHandler(PHANDLER_ROUTINE func, BOOL add)
1585 BOOL ret = TRUE;
1587 TRACE("(%p,%i)\n", func, add);
1589 if (!func)
1591 RtlEnterCriticalSection(&CONSOLE_CritSect);
1592 if (add)
1593 NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags |= 1;
1594 else
1595 NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags &= ~1;
1596 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1598 else if (add)
1600 struct ConsoleHandler* ch = HeapAlloc(GetProcessHeap(), 0, sizeof(struct ConsoleHandler));
1602 if (!ch) return FALSE;
1603 ch->handler = func;
1604 RtlEnterCriticalSection(&CONSOLE_CritSect);
1605 ch->next = CONSOLE_Handlers;
1606 CONSOLE_Handlers = ch;
1607 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1609 else
1611 struct ConsoleHandler** ch;
1612 RtlEnterCriticalSection(&CONSOLE_CritSect);
1613 for (ch = &CONSOLE_Handlers; *ch; ch = &(*ch)->next)
1615 if ((*ch)->handler == func) break;
1617 if (*ch)
1619 struct ConsoleHandler* rch = *ch;
1621 /* sanity check */
1622 if (rch == &CONSOLE_DefaultConsoleHandler)
1624 ERR("Who's trying to remove default handler???\n");
1625 SetLastError(ERROR_INVALID_PARAMETER);
1626 ret = FALSE;
1628 else
1630 *ch = rch->next;
1631 HeapFree(GetProcessHeap(), 0, rch);
1634 else
1636 WARN("Attempt to remove non-installed CtrlHandler %p\n", func);
1637 SetLastError(ERROR_INVALID_PARAMETER);
1638 ret = FALSE;
1640 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1642 return ret;
1645 static WINE_EXCEPTION_FILTER(CONSOLE_CtrlEventHandler)
1647 TRACE("(%x)\n", GetExceptionCode());
1648 return EXCEPTION_EXECUTE_HANDLER;
1651 /******************************************************************
1652 * CONSOLE_SendEventThread
1654 * Internal helper to pass an event to the list on installed handlers
1656 static DWORD WINAPI CONSOLE_SendEventThread(void* pmt)
1658 DWORD_PTR event = (DWORD_PTR)pmt;
1659 struct ConsoleHandler* ch;
1661 if (event == CTRL_C_EVENT)
1663 BOOL caught_by_dbg = TRUE;
1664 /* First, try to pass the ctrl-C event to the debugger (if any)
1665 * If it continues, there's nothing more to do
1666 * Otherwise, we need to send the ctrl-C event to the handlers
1668 __TRY
1670 RaiseException( DBG_CONTROL_C, 0, 0, NULL );
1672 __EXCEPT(CONSOLE_CtrlEventHandler)
1674 caught_by_dbg = FALSE;
1676 __ENDTRY;
1677 if (caught_by_dbg) return 0;
1678 /* the debugger didn't continue... so, pass to ctrl handlers */
1680 RtlEnterCriticalSection(&CONSOLE_CritSect);
1681 for (ch = CONSOLE_Handlers; ch; ch = ch->next)
1683 if (ch->handler(event)) break;
1685 RtlLeaveCriticalSection(&CONSOLE_CritSect);
1686 return 1;
1689 /******************************************************************
1690 * CONSOLE_HandleCtrlC
1692 * Check whether the shall manipulate CtrlC events
1694 int CONSOLE_HandleCtrlC(unsigned sig)
1696 /* FIXME: better test whether a console is attached to this process ??? */
1697 extern unsigned CONSOLE_GetNumHistoryEntries(void);
1698 if (CONSOLE_GetNumHistoryEntries() == (unsigned)-1) return 0;
1700 /* check if we have to ignore ctrl-C events */
1701 if (!(NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags & 1))
1703 /* Create a separate thread to signal all the events.
1704 * This is needed because:
1705 * - this function can be called in an Unix signal handler (hence on an
1706 * different stack than the thread that's running). This breaks the
1707 * Win32 exception mechanisms (where the thread's stack is checked).
1708 * - since the current thread, while processing the signal, can hold the
1709 * console critical section, we need another execution environment where
1710 * we can wait on this critical section
1712 CreateThread(NULL, 0, CONSOLE_SendEventThread, (void*)CTRL_C_EVENT, 0, NULL);
1714 return 1;
1717 /******************************************************************************
1718 * GenerateConsoleCtrlEvent [KERNEL32.@] Simulate a CTRL-C or CTRL-BREAK
1720 * PARAMS
1721 * dwCtrlEvent [I] Type of event
1722 * dwProcessGroupID [I] Process group ID to send event to
1724 * RETURNS
1725 * Success: True
1726 * Failure: False (and *should* [but doesn't] set LastError)
1728 BOOL WINAPI GenerateConsoleCtrlEvent(DWORD dwCtrlEvent,
1729 DWORD dwProcessGroupID)
1731 BOOL ret;
1733 TRACE("(%d, %d)\n", dwCtrlEvent, dwProcessGroupID);
1735 if (dwCtrlEvent != CTRL_C_EVENT && dwCtrlEvent != CTRL_BREAK_EVENT)
1737 ERR("Invalid event %d for PGID %d\n", dwCtrlEvent, dwProcessGroupID);
1738 return FALSE;
1741 SERVER_START_REQ( send_console_signal )
1743 req->signal = dwCtrlEvent;
1744 req->group_id = dwProcessGroupID;
1745 ret = !wine_server_call_err( req );
1747 SERVER_END_REQ;
1749 /* FIXME: shall this function be synchronous, ie only return when all events
1750 * have been handled by all processes in the given group ?
1751 * As of today, we don't wait...
1753 return ret;
1757 /******************************************************************************
1758 * CreateConsoleScreenBuffer [KERNEL32.@] Creates a console screen buffer
1760 * PARAMS
1761 * dwDesiredAccess [I] Access flag
1762 * dwShareMode [I] Buffer share mode
1763 * sa [I] Security attributes
1764 * dwFlags [I] Type of buffer to create
1765 * lpScreenBufferData [I] Reserved
1767 * NOTES
1768 * Should call SetLastError
1770 * RETURNS
1771 * Success: Handle to new console screen buffer
1772 * Failure: INVALID_HANDLE_VALUE
1774 HANDLE WINAPI CreateConsoleScreenBuffer(DWORD dwDesiredAccess, DWORD dwShareMode,
1775 LPSECURITY_ATTRIBUTES sa, DWORD dwFlags,
1776 LPVOID lpScreenBufferData)
1778 HANDLE ret = INVALID_HANDLE_VALUE;
1780 TRACE("(%d,%d,%p,%d,%p)\n",
1781 dwDesiredAccess, dwShareMode, sa, dwFlags, lpScreenBufferData);
1783 if (dwFlags != CONSOLE_TEXTMODE_BUFFER || lpScreenBufferData != NULL)
1785 SetLastError(ERROR_INVALID_PARAMETER);
1786 return INVALID_HANDLE_VALUE;
1789 SERVER_START_REQ(create_console_output)
1791 req->handle_in = 0;
1792 req->access = dwDesiredAccess;
1793 req->attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
1794 req->share = dwShareMode;
1795 if (!wine_server_call_err( req )) ret = reply->handle_out;
1797 SERVER_END_REQ;
1799 return ret;
1803 /***********************************************************************
1804 * GetConsoleScreenBufferInfo (KERNEL32.@)
1806 BOOL WINAPI GetConsoleScreenBufferInfo(HANDLE hConsoleOutput, LPCONSOLE_SCREEN_BUFFER_INFO csbi)
1808 BOOL ret;
1810 SERVER_START_REQ(get_console_output_info)
1812 req->handle = console_handle_unmap(hConsoleOutput);
1813 if ((ret = !wine_server_call_err( req )))
1815 csbi->dwSize.X = reply->width;
1816 csbi->dwSize.Y = reply->height;
1817 csbi->dwCursorPosition.X = reply->cursor_x;
1818 csbi->dwCursorPosition.Y = reply->cursor_y;
1819 csbi->wAttributes = reply->attr;
1820 csbi->srWindow.Left = reply->win_left;
1821 csbi->srWindow.Right = reply->win_right;
1822 csbi->srWindow.Top = reply->win_top;
1823 csbi->srWindow.Bottom = reply->win_bottom;
1824 csbi->dwMaximumWindowSize.X = reply->max_width;
1825 csbi->dwMaximumWindowSize.Y = reply->max_height;
1828 SERVER_END_REQ;
1830 TRACE("(%p,(%d,%d) (%d,%d) %d (%d,%d-%d,%d) (%d,%d)\n",
1831 hConsoleOutput, csbi->dwSize.X, csbi->dwSize.Y,
1832 csbi->dwCursorPosition.X, csbi->dwCursorPosition.Y,
1833 csbi->wAttributes,
1834 csbi->srWindow.Left, csbi->srWindow.Top, csbi->srWindow.Right, csbi->srWindow.Bottom,
1835 csbi->dwMaximumWindowSize.X, csbi->dwMaximumWindowSize.Y);
1837 return ret;
1841 /******************************************************************************
1842 * SetConsoleActiveScreenBuffer [KERNEL32.@] Sets buffer to current console
1844 * RETURNS
1845 * Success: TRUE
1846 * Failure: FALSE
1848 BOOL WINAPI SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput)
1850 BOOL ret;
1852 TRACE("(%p)\n", hConsoleOutput);
1854 SERVER_START_REQ( set_console_input_info )
1856 req->handle = 0;
1857 req->mask = SET_CONSOLE_INPUT_INFO_ACTIVE_SB;
1858 req->active_sb = hConsoleOutput;
1859 ret = !wine_server_call_err( req );
1861 SERVER_END_REQ;
1862 return ret;
1866 /***********************************************************************
1867 * GetConsoleMode (KERNEL32.@)
1869 BOOL WINAPI GetConsoleMode(HANDLE hcon, LPDWORD mode)
1871 BOOL ret;
1873 SERVER_START_REQ(get_console_mode)
1875 req->handle = console_handle_unmap(hcon);
1876 ret = !wine_server_call_err( req );
1877 if (ret && mode) *mode = reply->mode;
1879 SERVER_END_REQ;
1880 return ret;
1884 /******************************************************************************
1885 * SetConsoleMode [KERNEL32.@] Sets input mode of console's input buffer
1887 * PARAMS
1888 * hcon [I] Handle to console input or screen buffer
1889 * mode [I] Input or output mode to set
1891 * RETURNS
1892 * Success: TRUE
1893 * Failure: FALSE
1895 * mode:
1896 * ENABLE_PROCESSED_INPUT 0x01
1897 * ENABLE_LINE_INPUT 0x02
1898 * ENABLE_ECHO_INPUT 0x04
1899 * ENABLE_WINDOW_INPUT 0x08
1900 * ENABLE_MOUSE_INPUT 0x10
1902 BOOL WINAPI SetConsoleMode(HANDLE hcon, DWORD mode)
1904 BOOL ret;
1906 SERVER_START_REQ(set_console_mode)
1908 req->handle = console_handle_unmap(hcon);
1909 req->mode = mode;
1910 ret = !wine_server_call_err( req );
1912 SERVER_END_REQ;
1913 /* FIXME: when resetting a console input to editline mode, I think we should
1914 * empty the S_EditString buffer
1917 TRACE("(%p,%x) retval == %d\n", hcon, mode, ret);
1919 return ret;
1923 /******************************************************************
1924 * CONSOLE_WriteChars
1926 * WriteConsoleOutput helper: hides server call semantics
1927 * writes a string at a given pos with standard attribute
1929 static int CONSOLE_WriteChars(HANDLE hCon, LPCWSTR lpBuffer, int nc, COORD* pos)
1931 int written = -1;
1933 if (!nc) return 0;
1935 SERVER_START_REQ( write_console_output )
1937 req->handle = console_handle_unmap(hCon);
1938 req->x = pos->X;
1939 req->y = pos->Y;
1940 req->mode = CHAR_INFO_MODE_TEXTSTDATTR;
1941 req->wrap = FALSE;
1942 wine_server_add_data( req, lpBuffer, nc * sizeof(WCHAR) );
1943 if (!wine_server_call_err( req )) written = reply->written;
1945 SERVER_END_REQ;
1947 if (written > 0) pos->X += written;
1948 return written;
1951 /******************************************************************
1952 * next_line
1954 * WriteConsoleOutput helper: handles passing to next line (+scrolling if necessary)
1957 static int next_line(HANDLE hCon, CONSOLE_SCREEN_BUFFER_INFO* csbi)
1959 SMALL_RECT src;
1960 CHAR_INFO ci;
1961 COORD dst;
1963 csbi->dwCursorPosition.X = 0;
1964 csbi->dwCursorPosition.Y++;
1966 if (csbi->dwCursorPosition.Y < csbi->dwSize.Y) return 1;
1968 src.Top = 1;
1969 src.Bottom = csbi->dwSize.Y - 1;
1970 src.Left = 0;
1971 src.Right = csbi->dwSize.X - 1;
1973 dst.X = 0;
1974 dst.Y = 0;
1976 ci.Attributes = csbi->wAttributes;
1977 ci.Char.UnicodeChar = ' ';
1979 csbi->dwCursorPosition.Y--;
1980 if (!ScrollConsoleScreenBufferW(hCon, &src, NULL, dst, &ci))
1981 return 0;
1982 return 1;
1985 /******************************************************************
1986 * write_block
1988 * WriteConsoleOutput helper: writes a block of non special characters
1989 * Block can spread on several lines, and wrapping, if needed, is
1990 * handled
1993 static int write_block(HANDLE hCon, CONSOLE_SCREEN_BUFFER_INFO* csbi,
1994 DWORD mode, LPCWSTR ptr, int len)
1996 int blk; /* number of chars to write on current line */
1997 int done; /* number of chars already written */
1999 if (len <= 0) return 1;
2001 if (mode & ENABLE_WRAP_AT_EOL_OUTPUT) /* writes remaining on next line */
2003 for (done = 0; done < len; done += blk)
2005 blk = min(len - done, csbi->dwSize.X - csbi->dwCursorPosition.X);
2007 if (CONSOLE_WriteChars(hCon, ptr + done, blk, &csbi->dwCursorPosition) != blk)
2008 return 0;
2009 if (csbi->dwCursorPosition.X == csbi->dwSize.X && !next_line(hCon, csbi))
2010 return 0;
2013 else
2015 int pos = csbi->dwCursorPosition.X;
2016 /* FIXME: we could reduce the number of loops
2017 * but, in most cases we wouldn't gain lots of time (it would only
2018 * happen if we're asked to overwrite more than twice the part of the line,
2019 * which is unlikely
2021 for (blk = done = 0; done < len; done += blk)
2023 blk = min(len - done, csbi->dwSize.X - csbi->dwCursorPosition.X);
2025 csbi->dwCursorPosition.X = pos;
2026 if (CONSOLE_WriteChars(hCon, ptr + done, blk, &csbi->dwCursorPosition) != blk)
2027 return 0;
2031 return 1;
2034 /***********************************************************************
2035 * WriteConsoleW (KERNEL32.@)
2037 BOOL WINAPI WriteConsoleW(HANDLE hConsoleOutput, LPCVOID lpBuffer, DWORD nNumberOfCharsToWrite,
2038 LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
2040 DWORD mode;
2041 DWORD nw = 0;
2042 const WCHAR* psz = lpBuffer;
2043 CONSOLE_SCREEN_BUFFER_INFO csbi;
2044 int k, first = 0;
2046 TRACE("%p %s %d %p %p\n",
2047 hConsoleOutput, debugstr_wn(lpBuffer, nNumberOfCharsToWrite),
2048 nNumberOfCharsToWrite, lpNumberOfCharsWritten, lpReserved);
2050 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = 0;
2052 if (!GetConsoleMode(hConsoleOutput, &mode) ||
2053 !GetConsoleScreenBufferInfo(hConsoleOutput, &csbi))
2054 return FALSE;
2056 if (mode & ENABLE_PROCESSED_OUTPUT)
2058 unsigned int i;
2060 for (i = 0; i < nNumberOfCharsToWrite; i++)
2062 switch (psz[i])
2064 case '\b': case '\t': case '\n': case '\a': case '\r':
2065 /* don't handle here the i-th char... done below */
2066 if ((k = i - first) > 0)
2068 if (!write_block(hConsoleOutput, &csbi, mode, &psz[first], k))
2069 goto the_end;
2070 nw += k;
2072 first = i + 1;
2073 nw++;
2075 switch (psz[i])
2077 case '\b':
2078 if (csbi.dwCursorPosition.X > 0) csbi.dwCursorPosition.X--;
2079 break;
2080 case '\t':
2082 WCHAR tmp[8] = {' ',' ',' ',' ',' ',' ',' ',' '};
2084 if (!write_block(hConsoleOutput, &csbi, mode, tmp,
2085 ((csbi.dwCursorPosition.X + 8) & ~7) - csbi.dwCursorPosition.X))
2086 goto the_end;
2088 break;
2089 case '\n':
2090 next_line(hConsoleOutput, &csbi);
2091 break;
2092 case '\a':
2093 Beep(400, 300);
2094 break;
2095 case '\r':
2096 csbi.dwCursorPosition.X = 0;
2097 break;
2098 default:
2099 break;
2104 /* write the remaining block (if any) if processed output is enabled, or the
2105 * entire buffer otherwise
2107 if ((k = nNumberOfCharsToWrite - first) > 0)
2109 if (!write_block(hConsoleOutput, &csbi, mode, &psz[first], k))
2110 goto the_end;
2111 nw += k;
2114 the_end:
2115 SetConsoleCursorPosition(hConsoleOutput, csbi.dwCursorPosition);
2116 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = nw;
2117 return nw != 0;
2121 /***********************************************************************
2122 * WriteConsoleA (KERNEL32.@)
2124 BOOL WINAPI WriteConsoleA(HANDLE hConsoleOutput, LPCVOID lpBuffer, DWORD nNumberOfCharsToWrite,
2125 LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
2127 BOOL ret;
2128 LPWSTR xstring;
2129 DWORD n;
2131 n = MultiByteToWideChar(GetConsoleOutputCP(), 0, lpBuffer, nNumberOfCharsToWrite, NULL, 0);
2133 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = 0;
2134 xstring = HeapAlloc(GetProcessHeap(), 0, n * sizeof(WCHAR));
2135 if (!xstring) return 0;
2137 MultiByteToWideChar(GetConsoleOutputCP(), 0, lpBuffer, nNumberOfCharsToWrite, xstring, n);
2139 ret = WriteConsoleW(hConsoleOutput, xstring, n, lpNumberOfCharsWritten, 0);
2141 HeapFree(GetProcessHeap(), 0, xstring);
2143 return ret;
2146 /******************************************************************************
2147 * SetConsoleCursorPosition [KERNEL32.@]
2148 * Sets the cursor position in console
2150 * PARAMS
2151 * hConsoleOutput [I] Handle of console screen buffer
2152 * dwCursorPosition [I] New cursor position coordinates
2154 * RETURNS
2155 * Success: TRUE
2156 * Failure: FALSE
2158 BOOL WINAPI SetConsoleCursorPosition(HANDLE hcon, COORD pos)
2160 BOOL ret;
2161 CONSOLE_SCREEN_BUFFER_INFO csbi;
2162 int do_move = 0;
2163 int w, h;
2165 TRACE("%p %d %d\n", hcon, pos.X, pos.Y);
2167 SERVER_START_REQ(set_console_output_info)
2169 req->handle = console_handle_unmap(hcon);
2170 req->cursor_x = pos.X;
2171 req->cursor_y = pos.Y;
2172 req->mask = SET_CONSOLE_OUTPUT_INFO_CURSOR_POS;
2173 ret = !wine_server_call_err( req );
2175 SERVER_END_REQ;
2177 if (!ret || !GetConsoleScreenBufferInfo(hcon, &csbi))
2178 return FALSE;
2180 /* if cursor is no longer visible, scroll the visible window... */
2181 w = csbi.srWindow.Right - csbi.srWindow.Left + 1;
2182 h = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
2183 if (pos.X < csbi.srWindow.Left)
2185 csbi.srWindow.Left = min(pos.X, csbi.dwSize.X - w);
2186 do_move++;
2188 else if (pos.X > csbi.srWindow.Right)
2190 csbi.srWindow.Left = max(pos.X, w) - w + 1;
2191 do_move++;
2193 csbi.srWindow.Right = csbi.srWindow.Left + w - 1;
2195 if (pos.Y < csbi.srWindow.Top)
2197 csbi.srWindow.Top = min(pos.Y, csbi.dwSize.Y - h);
2198 do_move++;
2200 else if (pos.Y > csbi.srWindow.Bottom)
2202 csbi.srWindow.Top = max(pos.Y, h) - h + 1;
2203 do_move++;
2205 csbi.srWindow.Bottom = csbi.srWindow.Top + h - 1;
2207 ret = (do_move) ? SetConsoleWindowInfo(hcon, TRUE, &csbi.srWindow) : TRUE;
2209 return ret;
2212 /******************************************************************************
2213 * GetConsoleCursorInfo [KERNEL32.@] Gets size and visibility of console
2215 * PARAMS
2216 * hcon [I] Handle to console screen buffer
2217 * cinfo [O] Address of cursor information
2219 * RETURNS
2220 * Success: TRUE
2221 * Failure: FALSE
2223 BOOL WINAPI GetConsoleCursorInfo(HANDLE hCon, LPCONSOLE_CURSOR_INFO cinfo)
2225 BOOL ret;
2227 SERVER_START_REQ(get_console_output_info)
2229 req->handle = console_handle_unmap(hCon);
2230 ret = !wine_server_call_err( req );
2231 if (ret && cinfo)
2233 cinfo->dwSize = reply->cursor_size;
2234 cinfo->bVisible = reply->cursor_visible;
2237 SERVER_END_REQ;
2239 TRACE("(%p) returning (%d,%d)\n", hCon, cinfo->dwSize, cinfo->bVisible);
2240 return ret;
2244 /******************************************************************************
2245 * SetConsoleCursorInfo [KERNEL32.@] Sets size and visibility of cursor
2247 * PARAMS
2248 * hcon [I] Handle to console screen buffer
2249 * cinfo [I] Address of cursor information
2250 * RETURNS
2251 * Success: TRUE
2252 * Failure: FALSE
2254 BOOL WINAPI SetConsoleCursorInfo(HANDLE hCon, LPCONSOLE_CURSOR_INFO cinfo)
2256 BOOL ret;
2258 TRACE("(%p,%d,%d)\n", hCon, cinfo->dwSize, cinfo->bVisible);
2259 SERVER_START_REQ(set_console_output_info)
2261 req->handle = console_handle_unmap(hCon);
2262 req->cursor_size = cinfo->dwSize;
2263 req->cursor_visible = cinfo->bVisible;
2264 req->mask = SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM;
2265 ret = !wine_server_call_err( req );
2267 SERVER_END_REQ;
2268 return ret;
2272 /******************************************************************************
2273 * SetConsoleWindowInfo [KERNEL32.@] Sets size and position of console
2275 * PARAMS
2276 * hcon [I] Handle to console screen buffer
2277 * bAbsolute [I] Coordinate type flag
2278 * window [I] Address of new window rectangle
2279 * RETURNS
2280 * Success: TRUE
2281 * Failure: FALSE
2283 BOOL WINAPI SetConsoleWindowInfo(HANDLE hCon, BOOL bAbsolute, LPSMALL_RECT window)
2285 SMALL_RECT p = *window;
2286 BOOL ret;
2288 TRACE("(%p,%d,(%d,%d-%d,%d))\n", hCon, bAbsolute, p.Left, p.Top, p.Right, p.Bottom);
2290 if (!bAbsolute)
2292 CONSOLE_SCREEN_BUFFER_INFO csbi;
2294 if (!GetConsoleScreenBufferInfo(hCon, &csbi))
2295 return FALSE;
2296 p.Left += csbi.srWindow.Left;
2297 p.Top += csbi.srWindow.Top;
2298 p.Right += csbi.srWindow.Right;
2299 p.Bottom += csbi.srWindow.Bottom;
2301 SERVER_START_REQ(set_console_output_info)
2303 req->handle = console_handle_unmap(hCon);
2304 req->win_left = p.Left;
2305 req->win_top = p.Top;
2306 req->win_right = p.Right;
2307 req->win_bottom = p.Bottom;
2308 req->mask = SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW;
2309 ret = !wine_server_call_err( req );
2311 SERVER_END_REQ;
2313 return ret;
2317 /******************************************************************************
2318 * SetConsoleTextAttribute [KERNEL32.@] Sets colors for text
2320 * Sets the foreground and background color attributes of characters
2321 * written to the screen buffer.
2323 * RETURNS
2324 * Success: TRUE
2325 * Failure: FALSE
2327 BOOL WINAPI SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttr)
2329 BOOL ret;
2331 TRACE("(%p,%d)\n", hConsoleOutput, wAttr);
2332 SERVER_START_REQ(set_console_output_info)
2334 req->handle = console_handle_unmap(hConsoleOutput);
2335 req->attr = wAttr;
2336 req->mask = SET_CONSOLE_OUTPUT_INFO_ATTR;
2337 ret = !wine_server_call_err( req );
2339 SERVER_END_REQ;
2340 return ret;
2344 /******************************************************************************
2345 * SetConsoleScreenBufferSize [KERNEL32.@] Changes size of console
2347 * PARAMS
2348 * hConsoleOutput [I] Handle to console screen buffer
2349 * dwSize [I] New size in character rows and cols
2351 * RETURNS
2352 * Success: TRUE
2353 * Failure: FALSE
2355 BOOL WINAPI SetConsoleScreenBufferSize(HANDLE hConsoleOutput, COORD dwSize)
2357 BOOL ret;
2359 TRACE("(%p,(%d,%d))\n", hConsoleOutput, dwSize.X, dwSize.Y);
2360 SERVER_START_REQ(set_console_output_info)
2362 req->handle = console_handle_unmap(hConsoleOutput);
2363 req->width = dwSize.X;
2364 req->height = dwSize.Y;
2365 req->mask = SET_CONSOLE_OUTPUT_INFO_SIZE;
2366 ret = !wine_server_call_err( req );
2368 SERVER_END_REQ;
2369 return ret;
2373 /******************************************************************************
2374 * ScrollConsoleScreenBufferA [KERNEL32.@]
2377 BOOL WINAPI ScrollConsoleScreenBufferA(HANDLE hConsoleOutput, LPSMALL_RECT lpScrollRect,
2378 LPSMALL_RECT lpClipRect, COORD dwDestOrigin,
2379 LPCHAR_INFO lpFill)
2381 CHAR_INFO ciw;
2383 ciw.Attributes = lpFill->Attributes;
2384 MultiByteToWideChar(GetConsoleOutputCP(), 0, &lpFill->Char.AsciiChar, 1, &ciw.Char.UnicodeChar, 1);
2386 return ScrollConsoleScreenBufferW(hConsoleOutput, lpScrollRect, lpClipRect,
2387 dwDestOrigin, &ciw);
2390 /******************************************************************
2391 * CONSOLE_FillLineUniform
2393 * Helper function for ScrollConsoleScreenBufferW
2394 * Fills a part of a line with a constant character info
2396 void CONSOLE_FillLineUniform(HANDLE hConsoleOutput, int i, int j, int len, LPCHAR_INFO lpFill)
2398 SERVER_START_REQ( fill_console_output )
2400 req->handle = console_handle_unmap(hConsoleOutput);
2401 req->mode = CHAR_INFO_MODE_TEXTATTR;
2402 req->x = i;
2403 req->y = j;
2404 req->count = len;
2405 req->wrap = FALSE;
2406 req->data.ch = lpFill->Char.UnicodeChar;
2407 req->data.attr = lpFill->Attributes;
2408 wine_server_call_err( req );
2410 SERVER_END_REQ;
2413 /******************************************************************************
2414 * ScrollConsoleScreenBufferW [KERNEL32.@]
2418 BOOL WINAPI ScrollConsoleScreenBufferW(HANDLE hConsoleOutput, LPSMALL_RECT lpScrollRect,
2419 LPSMALL_RECT lpClipRect, COORD dwDestOrigin,
2420 LPCHAR_INFO lpFill)
2422 SMALL_RECT dst;
2423 DWORD ret;
2424 int i, j;
2425 int start = -1;
2426 SMALL_RECT clip;
2427 CONSOLE_SCREEN_BUFFER_INFO csbi;
2428 BOOL inside;
2429 COORD src;
2431 if (lpClipRect)
2432 TRACE("(%p,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", hConsoleOutput,
2433 lpScrollRect->Left, lpScrollRect->Top,
2434 lpScrollRect->Right, lpScrollRect->Bottom,
2435 lpClipRect->Left, lpClipRect->Top,
2436 lpClipRect->Right, lpClipRect->Bottom,
2437 dwDestOrigin.X, dwDestOrigin.Y, lpFill);
2438 else
2439 TRACE("(%p,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", hConsoleOutput,
2440 lpScrollRect->Left, lpScrollRect->Top,
2441 lpScrollRect->Right, lpScrollRect->Bottom,
2442 dwDestOrigin.X, dwDestOrigin.Y, lpFill);
2444 if (!GetConsoleScreenBufferInfo(hConsoleOutput, &csbi))
2445 return FALSE;
2447 src.X = lpScrollRect->Left;
2448 src.Y = lpScrollRect->Top;
2450 /* step 1: get dst rect */
2451 dst.Left = dwDestOrigin.X;
2452 dst.Top = dwDestOrigin.Y;
2453 dst.Right = dst.Left + (lpScrollRect->Right - lpScrollRect->Left);
2454 dst.Bottom = dst.Top + (lpScrollRect->Bottom - lpScrollRect->Top);
2456 /* step 2a: compute the final clip rect (optional passed clip and screen buffer limits */
2457 if (lpClipRect)
2459 clip.Left = max(0, lpClipRect->Left);
2460 clip.Right = min(csbi.dwSize.X - 1, lpClipRect->Right);
2461 clip.Top = max(0, lpClipRect->Top);
2462 clip.Bottom = min(csbi.dwSize.Y - 1, lpClipRect->Bottom);
2464 else
2466 clip.Left = 0;
2467 clip.Right = csbi.dwSize.X - 1;
2468 clip.Top = 0;
2469 clip.Bottom = csbi.dwSize.Y - 1;
2471 if (clip.Left > clip.Right || clip.Top > clip.Bottom) return FALSE;
2473 /* step 2b: clip dst rect */
2474 if (dst.Left < clip.Left ) {src.X += clip.Left - dst.Left; dst.Left = clip.Left;}
2475 if (dst.Top < clip.Top ) {src.Y += clip.Top - dst.Top; dst.Top = clip.Top;}
2476 if (dst.Right > clip.Right ) dst.Right = clip.Right;
2477 if (dst.Bottom > clip.Bottom) dst.Bottom = clip.Bottom;
2479 /* step 3: transfer the bits */
2480 SERVER_START_REQ(move_console_output)
2482 req->handle = console_handle_unmap(hConsoleOutput);
2483 req->x_src = src.X;
2484 req->y_src = src.Y;
2485 req->x_dst = dst.Left;
2486 req->y_dst = dst.Top;
2487 req->w = dst.Right - dst.Left + 1;
2488 req->h = dst.Bottom - dst.Top + 1;
2489 ret = !wine_server_call_err( req );
2491 SERVER_END_REQ;
2493 if (!ret) return FALSE;
2495 /* step 4: clean out the exposed part */
2497 /* have to write cell [i,j] if it is not in dst rect (because it has already
2498 * been written to by the scroll) and is in clip (we shall not write
2499 * outside of clip)
2501 for (j = max(lpScrollRect->Top, clip.Top); j <= min(lpScrollRect->Bottom, clip.Bottom); j++)
2503 inside = dst.Top <= j && j <= dst.Bottom;
2504 start = -1;
2505 for (i = max(lpScrollRect->Left, clip.Left); i <= min(lpScrollRect->Right, clip.Right); i++)
2507 if (inside && dst.Left <= i && i <= dst.Right)
2509 if (start != -1)
2511 CONSOLE_FillLineUniform(hConsoleOutput, start, j, i - start, lpFill);
2512 start = -1;
2515 else
2517 if (start == -1) start = i;
2520 if (start != -1)
2521 CONSOLE_FillLineUniform(hConsoleOutput, start, j, i - start, lpFill);
2524 return TRUE;
2527 /******************************************************************
2528 * AttachConsole (KERNEL32.@)
2530 BOOL WINAPI AttachConsole(DWORD dwProcessId)
2532 FIXME("stub %x\n",dwProcessId);
2533 return TRUE;
2537 /* ====================================================================
2539 * Console manipulation functions
2541 * ====================================================================*/
2543 /* some missing functions...
2544 * FIXME: those are likely to be defined as undocumented function in kernel32 (or part of them)
2545 * should get the right API and implement them
2546 * GetConsoleCommandHistory[AW] (dword dword dword)
2547 * GetConsoleCommandHistoryLength[AW]
2548 * SetConsoleCommandHistoryMode
2549 * SetConsoleNumberOfCommands[AW]
2551 int CONSOLE_GetHistory(int idx, WCHAR* buf, int buf_len)
2553 int len = 0;
2555 SERVER_START_REQ( get_console_input_history )
2557 req->handle = 0;
2558 req->index = idx;
2559 if (buf && buf_len > 1)
2561 wine_server_set_reply( req, buf, (buf_len - 1) * sizeof(WCHAR) );
2563 if (!wine_server_call_err( req ))
2565 if (buf) buf[wine_server_reply_size(reply) / sizeof(WCHAR)] = 0;
2566 len = reply->total / sizeof(WCHAR) + 1;
2569 SERVER_END_REQ;
2570 return len;
2573 /******************************************************************
2574 * CONSOLE_AppendHistory
2578 BOOL CONSOLE_AppendHistory(const WCHAR* ptr)
2580 size_t len = strlenW(ptr);
2581 BOOL ret;
2583 while (len && (ptr[len - 1] == '\n' || ptr[len - 1] == '\r')) len--;
2584 if (!len) return FALSE;
2586 SERVER_START_REQ( append_console_input_history )
2588 req->handle = 0;
2589 wine_server_add_data( req, ptr, len * sizeof(WCHAR) );
2590 ret = !wine_server_call_err( req );
2592 SERVER_END_REQ;
2593 return ret;
2596 /******************************************************************
2597 * CONSOLE_GetNumHistoryEntries
2601 unsigned CONSOLE_GetNumHistoryEntries(void)
2603 unsigned ret = -1;
2604 SERVER_START_REQ(get_console_input_info)
2606 req->handle = 0;
2607 if (!wine_server_call_err( req )) ret = reply->history_index;
2609 SERVER_END_REQ;
2610 return ret;
2613 /******************************************************************
2614 * CONSOLE_GetEditionMode
2618 BOOL CONSOLE_GetEditionMode(HANDLE hConIn, int* mode)
2620 unsigned ret = FALSE;
2621 SERVER_START_REQ(get_console_input_info)
2623 req->handle = console_handle_unmap(hConIn);
2624 if ((ret = !wine_server_call_err( req )))
2625 *mode = reply->edition_mode;
2627 SERVER_END_REQ;
2628 return ret;