Do not pass unnecessary flags to wrc in generated makefiles.
[wine/multimedia.git] / dlls / kernel / console.c
bloba611d28bec4703128daa6e1561b51f44f7dd4046
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 <stdio.h>
35 #include <string.h>
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39 #include <assert.h>
41 #include "winbase.h"
42 #include "winnls.h"
43 #include "winerror.h"
44 #include "wincon.h"
45 #include "wine/server.h"
46 #include "wine/exception.h"
47 #include "wine/unicode.h"
48 #include "wine/debug.h"
49 #include "excpt.h"
50 #include "console_private.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(console);
54 static UINT console_input_codepage;
55 static UINT console_output_codepage;
57 /* map input records to ASCII */
58 static void input_records_WtoA( INPUT_RECORD *buffer, int count )
60 int i;
61 char ch;
63 for (i = 0; i < count; i++)
65 if (buffer[i].EventType != KEY_EVENT) continue;
66 WideCharToMultiByte( GetConsoleCP(), 0,
67 &buffer[i].Event.KeyEvent.uChar.UnicodeChar, 1, &ch, 1, NULL, NULL );
68 buffer[i].Event.KeyEvent.uChar.AsciiChar = ch;
72 /* map input records to Unicode */
73 static void input_records_AtoW( INPUT_RECORD *buffer, int count )
75 int i;
76 WCHAR ch;
78 for (i = 0; i < count; i++)
80 if (buffer[i].EventType != KEY_EVENT) continue;
81 MultiByteToWideChar( GetConsoleCP(), 0,
82 &buffer[i].Event.KeyEvent.uChar.AsciiChar, 1, &ch, 1 );
83 buffer[i].Event.KeyEvent.uChar.UnicodeChar = ch;
87 /* map char infos to ASCII */
88 static void char_info_WtoA( CHAR_INFO *buffer, int count )
90 char ch;
92 while (count-- > 0)
94 WideCharToMultiByte( GetConsoleOutputCP(), 0, &buffer->Char.UnicodeChar, 1,
95 &ch, 1, NULL, NULL );
96 buffer->Char.AsciiChar = ch;
97 buffer++;
101 /* map char infos to Unicode */
102 static void char_info_AtoW( CHAR_INFO *buffer, int count )
104 WCHAR ch;
106 while (count-- > 0)
108 MultiByteToWideChar( GetConsoleOutputCP(), 0, &buffer->Char.AsciiChar, 1, &ch, 1 );
109 buffer->Char.UnicodeChar = ch;
110 buffer++;
115 /******************************************************************************
116 * GetConsoleCP [KERNEL32.@] Returns the OEM code page for the console
118 * RETURNS
119 * Code page code
121 UINT WINAPI GetConsoleCP(VOID)
123 if (!console_input_codepage) console_input_codepage = GetOEMCP();
124 return console_input_codepage;
128 /******************************************************************************
129 * SetConsoleCP [KERNEL32.@]
131 BOOL WINAPI SetConsoleCP(UINT cp)
133 if (!IsValidCodePage( cp )) return FALSE;
134 console_input_codepage = cp;
135 return TRUE;
139 /***********************************************************************
140 * GetConsoleOutputCP (KERNEL32.@)
142 UINT WINAPI GetConsoleOutputCP(VOID)
144 if (!console_output_codepage) console_output_codepage = GetOEMCP();
145 return console_output_codepage;
149 /******************************************************************************
150 * SetConsoleOutputCP [KERNEL32.@] Set the output codepage used by the console
152 * PARAMS
153 * cp [I] code page to set
155 * RETURNS
156 * Success: TRUE
157 * Failure: FALSE
159 BOOL WINAPI SetConsoleOutputCP(UINT cp)
161 if (!IsValidCodePage( cp )) return FALSE;
162 console_output_codepage = cp;
163 return TRUE;
167 /******************************************************************************
168 * WriteConsoleInputA [KERNEL32.@]
170 BOOL WINAPI WriteConsoleInputA( HANDLE handle, const INPUT_RECORD *buffer,
171 DWORD count, LPDWORD written )
173 INPUT_RECORD *recW;
174 BOOL ret;
176 if (!(recW = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*recW) ))) return FALSE;
177 memcpy( recW, buffer, count*sizeof(*recW) );
178 input_records_AtoW( recW, count );
179 ret = WriteConsoleInputW( handle, recW, count, written );
180 HeapFree( GetProcessHeap(), 0, recW );
181 return ret;
185 /******************************************************************************
186 * WriteConsoleInputW [KERNEL32.@]
188 BOOL WINAPI WriteConsoleInputW( HANDLE handle, const INPUT_RECORD *buffer,
189 DWORD count, LPDWORD written )
191 BOOL ret;
193 TRACE("(%p,%p,%ld,%p)\n", handle, buffer, count, written);
195 if (written) *written = 0;
196 SERVER_START_REQ( write_console_input )
198 req->handle = handle;
199 wine_server_add_data( req, buffer, count * sizeof(INPUT_RECORD) );
200 if ((ret = !wine_server_call_err( req )))
202 if (written) *written = reply->written;
205 SERVER_END_REQ;
206 return ret;
210 /***********************************************************************
211 * WriteConsoleOutputA (KERNEL32.@)
213 BOOL WINAPI WriteConsoleOutputA( HANDLE hConsoleOutput, const CHAR_INFO *lpBuffer,
214 COORD size, COORD coord, LPSMALL_RECT region )
216 int y;
217 BOOL ret;
218 COORD new_size, new_coord;
219 CHAR_INFO *ciw;
221 new_size.X = min( region->Right - region->Left + 1, size.X - coord.X );
222 new_size.Y = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
224 if (new_size.X <= 0 || new_size.Y <= 0)
226 region->Bottom = region->Top + new_size.Y - 1;
227 region->Right = region->Left + new_size.X - 1;
228 return TRUE;
231 /* only copy the useful rectangle */
232 if (!(ciw = HeapAlloc( GetProcessHeap(), 0, sizeof(CHAR_INFO) * new_size.X * new_size.Y )))
233 return FALSE;
234 for (y = 0; y < new_size.Y; y++)
236 memcpy( &ciw[y * new_size.X], &lpBuffer[(y + coord.Y) * size.X + coord.X],
237 new_size.X * sizeof(CHAR_INFO) );
238 char_info_AtoW( ciw, new_size.X );
240 new_coord.X = new_coord.Y = 0;
241 ret = WriteConsoleOutputW( hConsoleOutput, ciw, new_size, new_coord, region );
242 if (ciw) HeapFree( GetProcessHeap(), 0, ciw );
243 return ret;
247 /***********************************************************************
248 * WriteConsoleOutputW (KERNEL32.@)
250 BOOL WINAPI WriteConsoleOutputW( HANDLE hConsoleOutput, const CHAR_INFO *lpBuffer,
251 COORD size, COORD coord, LPSMALL_RECT region )
253 int width, height, y;
254 BOOL ret = TRUE;
256 TRACE("(%p,%p,(%d,%d),(%d,%d),(%d,%dx%d,%d)\n",
257 hConsoleOutput, lpBuffer, size.X, size.Y, coord.X, coord.Y,
258 region->Left, region->Top, region->Right, region->Bottom);
260 width = min( region->Right - region->Left + 1, size.X - coord.X );
261 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
263 if (width > 0 && height > 0)
265 for (y = 0; y < height; y++)
267 SERVER_START_REQ( write_console_output )
269 req->handle = hConsoleOutput;
270 req->x = region->Left;
271 req->y = region->Top + y;
272 req->mode = CHAR_INFO_MODE_TEXTATTR;
273 req->wrap = FALSE;
274 wine_server_add_data( req, &lpBuffer[(y + coord.Y) * size.X + coord.X],
275 width * sizeof(CHAR_INFO));
276 if ((ret = !wine_server_call_err( req )))
278 width = min( width, reply->width - region->Left );
279 height = min( height, reply->height - region->Top );
282 SERVER_END_REQ;
283 if (!ret) break;
286 region->Bottom = region->Top + height - 1;
287 region->Right = region->Left + width - 1;
288 return ret;
292 /******************************************************************************
293 * WriteConsoleOutputCharacterA [KERNEL32.@] Copies character to consecutive
294 * cells in the console screen buffer
296 * PARAMS
297 * hConsoleOutput [I] Handle to screen buffer
298 * str [I] Pointer to buffer with chars to write
299 * length [I] Number of cells to write to
300 * coord [I] Coords of first cell
301 * lpNumCharsWritten [O] Pointer to number of cells written
303 BOOL WINAPI WriteConsoleOutputCharacterA( HANDLE hConsoleOutput, LPCSTR str, DWORD length,
304 COORD coord, LPDWORD lpNumCharsWritten )
306 BOOL ret;
307 LPWSTR strW;
308 DWORD lenW;
310 TRACE("(%p,%s,%ld,%dx%d,%p)\n", hConsoleOutput,
311 debugstr_an(str, length), length, coord.X, coord.Y, lpNumCharsWritten);
313 lenW = MultiByteToWideChar( GetConsoleOutputCP(), 0, str, length, NULL, 0 );
315 if (lpNumCharsWritten) *lpNumCharsWritten = 0;
317 if (!(strW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) ))) return FALSE;
318 MultiByteToWideChar( GetConsoleOutputCP(), 0, str, length, strW, lenW );
320 ret = WriteConsoleOutputCharacterW( hConsoleOutput, strW, lenW, coord, lpNumCharsWritten );
321 HeapFree( GetProcessHeap(), 0, strW );
322 return ret;
326 /******************************************************************************
327 * WriteConsoleOutputAttribute [KERNEL32.@] Sets attributes for some cells in
328 * the console screen buffer
330 * PARAMS
331 * hConsoleOutput [I] Handle to screen buffer
332 * attr [I] Pointer to buffer with write attributes
333 * length [I] Number of cells to write to
334 * coord [I] Coords of first cell
335 * lpNumAttrsWritten [O] Pointer to number of cells written
337 * RETURNS
338 * Success: TRUE
339 * Failure: FALSE
342 BOOL WINAPI WriteConsoleOutputAttribute( HANDLE hConsoleOutput, CONST WORD *attr, DWORD length,
343 COORD coord, LPDWORD lpNumAttrsWritten )
345 BOOL ret;
347 TRACE("(%p,%p,%ld,%dx%d,%p)\n", hConsoleOutput,attr,length,coord.X,coord.Y,lpNumAttrsWritten);
349 SERVER_START_REQ( write_console_output )
351 req->handle = hConsoleOutput;
352 req->x = coord.X;
353 req->y = coord.Y;
354 req->mode = CHAR_INFO_MODE_ATTR;
355 req->wrap = TRUE;
356 wine_server_add_data( req, attr, length * sizeof(WORD) );
357 if ((ret = !wine_server_call_err( req )))
359 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
362 SERVER_END_REQ;
363 return ret;
367 /******************************************************************************
368 * FillConsoleOutputCharacterA [KERNEL32.@]
370 * PARAMS
371 * hConsoleOutput [I] Handle to screen buffer
372 * ch [I] Character to write
373 * length [I] Number of cells to write to
374 * coord [I] Coords of first cell
375 * lpNumCharsWritten [O] Pointer to number of cells written
377 * RETURNS
378 * Success: TRUE
379 * Failure: FALSE
381 BOOL WINAPI FillConsoleOutputCharacterA( HANDLE hConsoleOutput, CHAR ch, DWORD length,
382 COORD coord, LPDWORD lpNumCharsWritten )
384 WCHAR wch;
386 MultiByteToWideChar( GetConsoleOutputCP(), 0, &ch, 1, &wch, 1 );
387 return FillConsoleOutputCharacterW(hConsoleOutput, wch, length, coord, lpNumCharsWritten);
391 /******************************************************************************
392 * FillConsoleOutputCharacterW [KERNEL32.@] Writes characters to console
394 * PARAMS
395 * hConsoleOutput [I] Handle to screen buffer
396 * ch [I] Character to write
397 * length [I] Number of cells to write to
398 * coord [I] Coords of first cell
399 * lpNumCharsWritten [O] Pointer to number of cells written
401 * RETURNS
402 * Success: TRUE
403 * Failure: FALSE
405 BOOL WINAPI FillConsoleOutputCharacterW( HANDLE hConsoleOutput, WCHAR ch, DWORD length,
406 COORD coord, LPDWORD lpNumCharsWritten)
408 BOOL ret;
410 TRACE("(%p,%s,%ld,(%dx%d),%p)\n",
411 hConsoleOutput, debugstr_wn(&ch, 1), length, coord.X, coord.Y, lpNumCharsWritten);
413 SERVER_START_REQ( fill_console_output )
415 req->handle = hConsoleOutput;
416 req->x = coord.X;
417 req->y = coord.Y;
418 req->mode = CHAR_INFO_MODE_TEXT;
419 req->wrap = TRUE;
420 req->data.ch = ch;
421 req->count = length;
422 if ((ret = !wine_server_call_err( req )))
424 if (lpNumCharsWritten) *lpNumCharsWritten = reply->written;
427 SERVER_END_REQ;
428 return ret;
432 /******************************************************************************
433 * FillConsoleOutputAttribute [KERNEL32.@] Sets attributes for console
435 * PARAMS
436 * hConsoleOutput [I] Handle to screen buffer
437 * attr [I] Color attribute to write
438 * length [I] Number of cells to write to
439 * coord [I] Coords of first cell
440 * lpNumAttrsWritten [O] Pointer to number of cells written
442 * RETURNS
443 * Success: TRUE
444 * Failure: FALSE
446 BOOL WINAPI FillConsoleOutputAttribute( HANDLE hConsoleOutput, WORD attr, DWORD length,
447 COORD coord, LPDWORD lpNumAttrsWritten )
449 BOOL ret;
451 TRACE("(%p,%d,%ld,(%dx%d),%p)\n",
452 hConsoleOutput, attr, length, coord.X, coord.Y, lpNumAttrsWritten);
454 SERVER_START_REQ( fill_console_output )
456 req->handle = hConsoleOutput;
457 req->x = coord.X;
458 req->y = coord.Y;
459 req->mode = CHAR_INFO_MODE_ATTR;
460 req->wrap = TRUE;
461 req->data.attr = attr;
462 req->count = length;
463 if ((ret = !wine_server_call_err( req )))
465 if (lpNumAttrsWritten) *lpNumAttrsWritten = reply->written;
468 SERVER_END_REQ;
469 return ret;
473 /******************************************************************************
474 * ReadConsoleOutputCharacterA [KERNEL32.@]
477 BOOL WINAPI ReadConsoleOutputCharacterA(HANDLE hConsoleOutput, LPSTR lpstr, DWORD count,
478 COORD coord, LPDWORD read_count)
480 DWORD read;
481 BOOL ret;
482 LPWSTR wptr = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
484 if (read_count) *read_count = 0;
485 if (!wptr) return FALSE;
487 if ((ret = ReadConsoleOutputCharacterW( hConsoleOutput, wptr, count, coord, &read )))
489 read = WideCharToMultiByte( GetConsoleOutputCP(), 0, wptr, read, lpstr, count, NULL, NULL);
490 if (read_count) *read_count = read;
492 HeapFree( GetProcessHeap(), 0, wptr );
493 return ret;
497 /******************************************************************************
498 * ReadConsoleOutputCharacterW [KERNEL32.@]
501 BOOL WINAPI ReadConsoleOutputCharacterW( HANDLE hConsoleOutput, LPWSTR buffer, DWORD count,
502 COORD coord, LPDWORD read_count )
504 BOOL ret;
506 TRACE( "(%p,%p,%ld,%dx%d,%p)\n", hConsoleOutput, buffer, count, coord.X, coord.Y, read_count );
508 SERVER_START_REQ( read_console_output )
510 req->handle = hConsoleOutput;
511 req->x = coord.X;
512 req->y = coord.Y;
513 req->mode = CHAR_INFO_MODE_TEXT;
514 req->wrap = TRUE;
515 wine_server_set_reply( req, buffer, count * sizeof(WCHAR) );
516 if ((ret = !wine_server_call_err( req )))
518 if (read_count) *read_count = wine_server_reply_size(reply) / sizeof(WCHAR);
521 SERVER_END_REQ;
522 return ret;
526 /******************************************************************************
527 * ReadConsoleOutputAttribute [KERNEL32.@]
529 BOOL WINAPI ReadConsoleOutputAttribute(HANDLE hConsoleOutput, LPWORD lpAttribute, DWORD length,
530 COORD coord, LPDWORD read_count)
532 BOOL ret;
534 TRACE("(%p,%p,%ld,%dx%d,%p)\n",
535 hConsoleOutput, lpAttribute, length, coord.X, coord.Y, read_count);
537 SERVER_START_REQ( read_console_output )
539 req->handle = hConsoleOutput;
540 req->x = coord.X;
541 req->y = coord.Y;
542 req->mode = CHAR_INFO_MODE_ATTR;
543 req->wrap = TRUE;
544 wine_server_set_reply( req, lpAttribute, length * sizeof(WORD) );
545 if ((ret = !wine_server_call_err( req )))
547 if (read_count) *read_count = wine_server_reply_size(reply) / sizeof(WORD);
550 SERVER_END_REQ;
551 return ret;
555 /******************************************************************************
556 * ReadConsoleOutputA [KERNEL32.@]
559 BOOL WINAPI ReadConsoleOutputA( HANDLE hConsoleOutput, LPCHAR_INFO lpBuffer, COORD size,
560 COORD coord, LPSMALL_RECT region )
562 BOOL ret;
563 int y;
565 ret = ReadConsoleOutputW( hConsoleOutput, lpBuffer, size, coord, region );
566 if (ret && region->Right >= region->Left)
568 for (y = 0; y <= region->Bottom - region->Top; y++)
570 char_info_WtoA( &lpBuffer[(coord.Y + y) * size.X + coord.X],
571 region->Right - region->Left + 1 );
574 return ret;
578 /******************************************************************************
579 * ReadConsoleOutputW [KERNEL32.@]
581 * NOTE: The NT4 (sp5) kernel crashes on me if size is (0,0). I don't
582 * think we need to be *that* compatible. -- AJ
584 BOOL WINAPI ReadConsoleOutputW( HANDLE hConsoleOutput, LPCHAR_INFO lpBuffer, COORD size,
585 COORD coord, LPSMALL_RECT region )
587 int width, height, y;
588 BOOL ret = TRUE;
590 width = min( region->Right - region->Left + 1, size.X - coord.X );
591 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
593 if (width > 0 && height > 0)
595 for (y = 0; y < height; y++)
597 SERVER_START_REQ( read_console_output )
599 req->handle = hConsoleOutput;
600 req->x = region->Left;
601 req->y = region->Top + y;
602 req->mode = CHAR_INFO_MODE_TEXTATTR;
603 req->wrap = FALSE;
604 wine_server_set_reply( req, &lpBuffer[(y+coord.Y) * size.X + coord.X],
605 width * sizeof(CHAR_INFO) );
606 if ((ret = !wine_server_call_err( req )))
608 width = min( width, reply->width - region->Left );
609 height = min( height, reply->height - region->Top );
612 SERVER_END_REQ;
613 if (!ret) break;
616 region->Bottom = region->Top + height - 1;
617 region->Right = region->Left + width - 1;
618 return ret;
622 /******************************************************************************
623 * ReadConsoleInputA [KERNEL32.@] Reads data from a console
625 * PARAMS
626 * handle [I] Handle to console input buffer
627 * buffer [O] Address of buffer for read data
628 * count [I] Number of records to read
629 * pRead [O] Address of number of records read
631 * RETURNS
632 * Success: TRUE
633 * Failure: FALSE
635 BOOL WINAPI ReadConsoleInputA( HANDLE handle, LPINPUT_RECORD buffer, DWORD count, LPDWORD pRead )
637 DWORD read;
639 if (!ReadConsoleInputW( handle, buffer, count, &read )) return FALSE;
640 input_records_WtoA( buffer, read );
641 if (pRead) *pRead = read;
642 return TRUE;
646 /***********************************************************************
647 * PeekConsoleInputA (KERNEL32.@)
649 * Gets 'count' first events (or less) from input queue.
651 BOOL WINAPI PeekConsoleInputA( HANDLE handle, LPINPUT_RECORD buffer, DWORD count, LPDWORD pRead )
653 DWORD read;
655 if (!PeekConsoleInputW( handle, buffer, count, &read )) return FALSE;
656 input_records_WtoA( buffer, read );
657 if (pRead) *pRead = read;
658 return TRUE;
662 /***********************************************************************
663 * PeekConsoleInputW (KERNEL32.@)
665 BOOL WINAPI PeekConsoleInputW( HANDLE handle, LPINPUT_RECORD buffer, DWORD count, LPDWORD read )
667 BOOL ret;
668 SERVER_START_REQ( read_console_input )
670 req->handle = handle;
671 req->flush = FALSE;
672 wine_server_set_reply( req, buffer, count * sizeof(INPUT_RECORD) );
673 if ((ret = !wine_server_call_err( req )))
675 if (read) *read = count ? reply->read : 0;
678 SERVER_END_REQ;
679 return ret;
683 /***********************************************************************
684 * GetNumberOfConsoleInputEvents (KERNEL32.@)
686 BOOL WINAPI GetNumberOfConsoleInputEvents( HANDLE handle, LPDWORD nrofevents )
688 BOOL ret;
689 SERVER_START_REQ( read_console_input )
691 req->handle = handle;
692 req->flush = FALSE;
693 if ((ret = !wine_server_call_err( req )))
695 if (nrofevents) *nrofevents = reply->read;
698 SERVER_END_REQ;
699 return ret;
703 /***********************************************************************
704 * FlushConsoleInputBuffer (KERNEL32.@)
706 BOOL WINAPI FlushConsoleInputBuffer( HANDLE handle )
708 BOOL ret;
709 SERVER_START_REQ( read_console_input )
711 req->handle = handle;
712 req->flush = TRUE;
713 ret = !wine_server_call_err( req );
715 SERVER_END_REQ;
716 return ret;
720 /***********************************************************************
721 * SetConsoleTitleA (KERNEL32.@)
723 BOOL WINAPI SetConsoleTitleA( LPCSTR title )
725 LPWSTR titleW;
726 BOOL ret;
728 DWORD len = MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, NULL, 0 );
729 if (!(titleW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)))) return FALSE;
730 MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, titleW, len );
731 ret = SetConsoleTitleW(titleW);
732 HeapFree(GetProcessHeap(), 0, titleW);
733 return ret;
737 /***********************************************************************
738 * GetConsoleTitleA (KERNEL32.@)
740 DWORD WINAPI GetConsoleTitleA(LPSTR title, DWORD size)
742 WCHAR *ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * size);
743 DWORD ret;
745 if (!ptr) return 0;
746 ret = GetConsoleTitleW( ptr, size );
747 if (ret)
749 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr, ret + 1, title, size, NULL, NULL);
750 ret = strlen(title);
752 return ret;
756 /******************************************************************************
757 * GetConsoleTitleW [KERNEL32.@] Retrieves title string for console
759 * PARAMS
760 * title [O] Address of buffer for title
761 * size [I] Size of buffer
763 * RETURNS
764 * Success: Length of string copied
765 * Failure: 0
767 DWORD WINAPI GetConsoleTitleW(LPWSTR title, DWORD size)
769 DWORD ret = 0;
771 SERVER_START_REQ( get_console_input_info )
773 req->handle = 0;
774 wine_server_set_reply( req, title, (size-1) * sizeof(WCHAR) );
775 if (!wine_server_call_err( req ))
777 ret = wine_server_reply_size(reply) / sizeof(WCHAR);
778 title[ret] = 0;
781 SERVER_END_REQ;
782 return ret;
786 /***********************************************************************
787 * GetLargestConsoleWindowSize (KERNEL32.@)
789 * NOTE
790 * This should return a COORD, but calling convention for returning
791 * structures is different between Windows and gcc on i386.
793 * VERSION: [i386]
795 #ifdef __i386__
796 #undef GetLargestConsoleWindowSize
797 DWORD WINAPI GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
799 union {
800 COORD c;
801 DWORD w;
802 } x;
803 x.c.X = 80;
804 x.c.Y = 24;
805 return x.w;
807 #endif /* defined(__i386__) */
810 /***********************************************************************
811 * GetLargestConsoleWindowSize (KERNEL32.@)
813 * NOTE
814 * This should return a COORD, but calling convention for returning
815 * structures is different between Windows and gcc on i386.
817 * VERSION: [!i386]
819 #ifndef __i386__
820 COORD WINAPI GetLargestConsoleWindowSize(HANDLE hConsoleOutput)
822 COORD c;
823 c.X = 80;
824 c.Y = 24;
825 return c;
827 #endif /* defined(__i386__) */
829 static WCHAR* S_EditString /* = NULL */;
830 static unsigned S_EditStrPos /* = 0 */;
832 /***********************************************************************
833 * FreeConsole (KERNEL32.@)
835 BOOL WINAPI FreeConsole(VOID)
837 BOOL ret;
839 SERVER_START_REQ(free_console)
841 ret = !wine_server_call_err( req );
843 SERVER_END_REQ;
844 return ret;
847 /******************************************************************
848 * start_console_renderer
850 * helper for AllocConsole
851 * starts the renderer process
853 static BOOL start_console_renderer_helper(const char* appname, STARTUPINFOA* si,
854 HANDLE hEvent)
856 char buffer[1024];
857 int ret;
858 PROCESS_INFORMATION pi;
860 /* FIXME: use dynamic allocation for most of the buffers below */
861 ret = snprintf(buffer, sizeof(buffer), "%s --use-event=%d", appname, (INT)hEvent);
862 if ((ret > -1) && (ret < sizeof(buffer)) &&
863 CreateProcessA(NULL, buffer, NULL, NULL, TRUE, DETACHED_PROCESS,
864 NULL, NULL, si, &pi))
866 if (WaitForSingleObject(hEvent, INFINITE) != WAIT_OBJECT_0) return FALSE;
868 TRACE("Started wineconsole pid=%08lx tid=%08lx\n",
869 pi.dwProcessId, pi.dwThreadId);
871 return TRUE;
873 return FALSE;
876 static BOOL start_console_renderer(STARTUPINFOA* si)
878 HANDLE hEvent = 0;
879 LPSTR p;
880 OBJECT_ATTRIBUTES attr;
881 BOOL ret = FALSE;
883 attr.Length = sizeof(attr);
884 attr.RootDirectory = 0;
885 attr.Attributes = OBJ_INHERIT;
886 attr.ObjectName = NULL;
887 attr.SecurityDescriptor = NULL;
888 attr.SecurityQualityOfService = NULL;
890 NtCreateEvent(&hEvent, EVENT_ALL_ACCESS, &attr, TRUE, FALSE);
891 if (!hEvent) return FALSE;
893 /* first try environment variable */
894 if ((p = getenv("WINECONSOLE")) != NULL)
896 ret = start_console_renderer_helper(p, si, hEvent);
897 if (!ret)
898 ERR("Couldn't launch Wine console from WINECONSOLE env var (%s)... "
899 "trying default access\n", p);
902 /* then try the regular PATH */
903 if (!ret)
904 ret = start_console_renderer_helper("wineconsole", si, hEvent);
906 CloseHandle(hEvent);
907 return ret;
910 /***********************************************************************
911 * AllocConsole (KERNEL32.@)
913 * creates an xterm with a pty to our program
915 BOOL WINAPI AllocConsole(void)
917 HANDLE handle_in = INVALID_HANDLE_VALUE;
918 HANDLE handle_out = INVALID_HANDLE_VALUE;
919 HANDLE handle_err = INVALID_HANDLE_VALUE;
920 STARTUPINFOA siCurrent;
921 STARTUPINFOA siConsole;
922 char buffer[1024];
924 TRACE("()\n");
926 handle_in = CreateFileA( "CONIN$", GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,
927 0, NULL, OPEN_EXISTING, 0, 0 );
929 if (handle_in != INVALID_HANDLE_VALUE)
931 /* we already have a console opened on this process, don't create a new one */
932 CloseHandle(handle_in);
933 return FALSE;
936 GetStartupInfoA(&siCurrent);
938 memset(&siConsole, 0, sizeof(siConsole));
939 siConsole.cb = sizeof(siConsole);
940 /* setup a view arguments for wineconsole (it'll use them as default values) */
941 if (siCurrent.dwFlags & STARTF_USECOUNTCHARS)
943 siConsole.dwFlags |= STARTF_USECOUNTCHARS;
944 siConsole.dwXCountChars = siCurrent.dwXCountChars;
945 siConsole.dwYCountChars = siCurrent.dwYCountChars;
947 if (siCurrent.dwFlags & STARTF_USEFILLATTRIBUTE)
949 siConsole.dwFlags |= STARTF_USEFILLATTRIBUTE;
950 siConsole.dwFillAttribute = siCurrent.dwFillAttribute;
952 /* FIXME (should pass the unicode form) */
953 if (siCurrent.lpTitle)
954 siConsole.lpTitle = siCurrent.lpTitle;
955 else if (GetModuleFileNameA(0, buffer, sizeof(buffer)))
956 siConsole.lpTitle = buffer;
958 if (!start_console_renderer(&siConsole))
959 goto the_end;
961 handle_in = CreateFileA( "CONIN$", GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,
962 0, NULL, OPEN_EXISTING, 0, 0 );
963 if (handle_in == INVALID_HANDLE_VALUE) goto the_end;
965 handle_out = CreateFileA( "CONOUT$", GENERIC_READ|GENERIC_WRITE,
966 0, NULL, OPEN_EXISTING, 0, 0 );
967 if (handle_out == INVALID_HANDLE_VALUE) goto the_end;
969 if (!DuplicateHandle(GetCurrentProcess(), handle_out, GetCurrentProcess(), &handle_err,
970 0, TRUE, DUPLICATE_SAME_ACCESS))
971 goto the_end;
973 /* NT resets the STD_*_HANDLEs on console alloc */
974 SetStdHandle(STD_INPUT_HANDLE, handle_in);
975 SetStdHandle(STD_OUTPUT_HANDLE, handle_out);
976 SetStdHandle(STD_ERROR_HANDLE, handle_err);
978 SetLastError(ERROR_SUCCESS);
980 return TRUE;
982 the_end:
983 ERR("Can't allocate console\n");
984 if (handle_in != INVALID_HANDLE_VALUE) CloseHandle(handle_in);
985 if (handle_out != INVALID_HANDLE_VALUE) CloseHandle(handle_out);
986 if (handle_err != INVALID_HANDLE_VALUE) CloseHandle(handle_err);
987 FreeConsole();
988 return FALSE;
992 /******************************************************************************
993 * read_console_input
995 * Helper function for ReadConsole, ReadConsoleInput and PeekConsoleInput
997 static BOOL read_console_input(HANDLE handle, LPINPUT_RECORD buffer, DWORD count,
998 LPDWORD pRead, BOOL flush)
1000 BOOL ret;
1001 unsigned read = 0;
1003 SERVER_START_REQ( read_console_input )
1005 req->handle = handle;
1006 req->flush = flush;
1007 wine_server_set_reply( req, buffer, count * sizeof(INPUT_RECORD) );
1008 if ((ret = !wine_server_call_err( req ))) read = reply->read;
1010 SERVER_END_REQ;
1011 if (pRead) *pRead = read;
1012 return ret;
1016 /***********************************************************************
1017 * ReadConsoleA (KERNEL32.@)
1019 BOOL WINAPI ReadConsoleA(HANDLE hConsoleInput, LPVOID lpBuffer, DWORD nNumberOfCharsToRead,
1020 LPDWORD lpNumberOfCharsRead, LPVOID lpReserved)
1022 LPWSTR ptr = HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead * sizeof(WCHAR));
1023 DWORD ncr = 0;
1024 BOOL ret;
1026 if ((ret = ReadConsoleW(hConsoleInput, ptr, nNumberOfCharsToRead, &ncr, NULL)))
1027 ncr = WideCharToMultiByte(CP_ACP, 0, ptr, ncr, lpBuffer, nNumberOfCharsToRead, NULL, NULL);
1029 if (lpNumberOfCharsRead) *lpNumberOfCharsRead = ncr;
1030 HeapFree(GetProcessHeap(), 0, ptr);
1032 return ret;
1035 /***********************************************************************
1036 * ReadConsoleW (KERNEL32.@)
1038 BOOL WINAPI ReadConsoleW(HANDLE hConsoleInput, LPVOID lpBuffer,
1039 DWORD nNumberOfCharsToRead, LPDWORD lpNumberOfCharsRead, LPVOID lpReserved)
1041 DWORD charsread;
1042 LPWSTR xbuf = (LPWSTR)lpBuffer;
1043 DWORD mode;
1045 TRACE("(%p,%p,%ld,%p,%p)\n",
1046 hConsoleInput, lpBuffer, nNumberOfCharsToRead, lpNumberOfCharsRead, lpReserved);
1048 if (!GetConsoleMode(hConsoleInput, &mode))
1049 return FALSE;
1051 if (mode & ENABLE_LINE_INPUT)
1053 if (!S_EditString || S_EditString[S_EditStrPos] == 0)
1055 if (S_EditString) HeapFree(GetProcessHeap(), 0, S_EditString);
1056 if (!(S_EditString = CONSOLE_Readline(hConsoleInput)))
1057 return FALSE;
1058 S_EditStrPos = 0;
1060 charsread = lstrlenW(&S_EditString[S_EditStrPos]);
1061 if (charsread > nNumberOfCharsToRead) charsread = nNumberOfCharsToRead;
1062 memcpy(xbuf, &S_EditString[S_EditStrPos], charsread * sizeof(WCHAR));
1063 S_EditStrPos += charsread;
1065 else
1067 INPUT_RECORD ir;
1068 DWORD count;
1070 /* FIXME: should we read at least 1 char? The SDK does not say */
1071 /* wait for at least one available input record (it doesn't mean we'll have
1072 * chars stored in xbuf...
1074 WaitForSingleObject(hConsoleInput, INFINITE);
1075 for (charsread = 0; charsread < nNumberOfCharsToRead;)
1077 if (!read_console_input(hConsoleInput, &ir, 1, &count, TRUE)) return FALSE;
1078 if (count && ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown &&
1079 ir.Event.KeyEvent.uChar.UnicodeChar &&
1080 !(ir.Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
1082 xbuf[charsread++] = ir.Event.KeyEvent.uChar.UnicodeChar;
1087 if (lpNumberOfCharsRead) *lpNumberOfCharsRead = charsread;
1089 return TRUE;
1093 /***********************************************************************
1094 * ReadConsoleInputW (KERNEL32.@)
1096 BOOL WINAPI ReadConsoleInputW(HANDLE hConsoleInput, LPINPUT_RECORD lpBuffer,
1097 DWORD nLength, LPDWORD lpNumberOfEventsRead)
1099 DWORD count;
1101 if (!nLength)
1103 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = 0;
1104 return TRUE;
1107 /* loop until we get at least one event */
1108 for (;;)
1110 WaitForSingleObject(hConsoleInput, INFINITE);
1111 if (!read_console_input(hConsoleInput, lpBuffer, nLength, &count, TRUE))
1112 return FALSE;
1113 if (count)
1115 if (lpNumberOfEventsRead) *lpNumberOfEventsRead = count;
1116 return TRUE;
1122 /******************************************************************************
1123 * WriteConsoleOutputCharacterW [KERNEL32.@] Copies character to consecutive
1124 * cells in the console screen buffer
1126 * PARAMS
1127 * hConsoleOutput [I] Handle to screen buffer
1128 * str [I] Pointer to buffer with chars to write
1129 * length [I] Number of cells to write to
1130 * coord [I] Coords of first cell
1131 * lpNumCharsWritten [O] Pointer to number of cells written
1133 * RETURNS
1134 * Success: TRUE
1135 * Failure: FALSE
1138 BOOL WINAPI WriteConsoleOutputCharacterW( HANDLE hConsoleOutput, LPCWSTR str, DWORD length,
1139 COORD coord, LPDWORD lpNumCharsWritten )
1141 BOOL ret;
1143 TRACE("(%p,%s,%ld,%dx%d,%p)\n", hConsoleOutput,
1144 debugstr_wn(str, length), length, coord.X, coord.Y, lpNumCharsWritten);
1146 SERVER_START_REQ( write_console_output )
1148 req->handle = hConsoleOutput;
1149 req->x = coord.X;
1150 req->y = coord.Y;
1151 req->mode = CHAR_INFO_MODE_TEXT;
1152 req->wrap = TRUE;
1153 wine_server_add_data( req, str, length * sizeof(WCHAR) );
1154 if ((ret = !wine_server_call_err( req )))
1156 if (lpNumCharsWritten) *lpNumCharsWritten = reply->written;
1159 SERVER_END_REQ;
1160 return ret;
1164 /******************************************************************************
1165 * SetConsoleTitleW [KERNEL32.@] Sets title bar string for console
1167 * PARAMS
1168 * title [I] Address of new title
1170 * RETURNS
1171 * Success: TRUE
1172 * Failure: FALSE
1174 BOOL WINAPI SetConsoleTitleW(LPCWSTR title)
1176 BOOL ret;
1178 SERVER_START_REQ( set_console_input_info )
1180 req->handle = 0;
1181 req->mask = SET_CONSOLE_INPUT_INFO_TITLE;
1182 wine_server_add_data( req, title, strlenW(title) * sizeof(WCHAR) );
1183 ret = !wine_server_call_err( req );
1185 SERVER_END_REQ;
1186 return ret;
1190 /***********************************************************************
1191 * GetNumberOfConsoleMouseButtons (KERNEL32.@)
1193 BOOL WINAPI GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons)
1195 FIXME("(%p): stub\n", nrofbuttons);
1196 *nrofbuttons = 2;
1197 return TRUE;
1200 /******************************************************************************
1201 * SetConsoleInputExeNameW [KERNEL32.@]
1203 * BUGS
1204 * Unimplemented
1206 BOOL WINAPI SetConsoleInputExeNameW(LPCWSTR name)
1208 FIXME("(%s): stub!\n", debugstr_w(name));
1210 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1211 return TRUE;
1214 /******************************************************************************
1215 * SetConsoleInputExeNameA [KERNEL32.@]
1217 * BUGS
1218 * Unimplemented
1220 BOOL WINAPI SetConsoleInputExeNameA(LPCSTR name)
1222 int len = MultiByteToWideChar(CP_ACP, 0, name, -1, NULL, 0);
1223 LPWSTR xptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1224 BOOL ret;
1226 if (!xptr) return FALSE;
1228 MultiByteToWideChar(CP_ACP, 0, name, -1, xptr, len);
1229 ret = SetConsoleInputExeNameW(xptr);
1230 HeapFree(GetProcessHeap(), 0, xptr);
1232 return ret;
1235 /******************************************************************
1236 * CONSOLE_DefaultHandler
1238 * Final control event handler
1240 static BOOL WINAPI CONSOLE_DefaultHandler(DWORD dwCtrlType)
1242 FIXME("Terminating process %lx on event %lx\n", GetCurrentProcessId(), dwCtrlType);
1243 ExitProcess(0);
1244 /* should never go here */
1245 return TRUE;
1248 /******************************************************************************
1249 * SetConsoleCtrlHandler [KERNEL32.@] Adds function to calling process list
1251 * PARAMS
1252 * func [I] Address of handler function
1253 * add [I] Handler to add or remove
1255 * RETURNS
1256 * Success: TRUE
1257 * Failure: FALSE
1259 * CHANGED
1260 * James Sutherland (JamesSutherland@gmx.de)
1261 * Added global variables console_ignore_ctrl_c and handlers[]
1262 * Does not yet do any error checking, or set LastError if failed.
1263 * This doesn't yet matter, since these handlers are not yet called...!
1266 struct ConsoleHandler {
1267 PHANDLER_ROUTINE handler;
1268 struct ConsoleHandler* next;
1271 static unsigned int CONSOLE_IgnoreCtrlC = 0; /* FIXME: this should be inherited somehow */
1272 static struct ConsoleHandler CONSOLE_DefaultConsoleHandler = {CONSOLE_DefaultHandler, NULL};
1273 static struct ConsoleHandler* CONSOLE_Handlers = &CONSOLE_DefaultConsoleHandler;
1274 static CRITICAL_SECTION CONSOLE_CritSect = CRITICAL_SECTION_INIT("console_ctrl_section");
1276 /*****************************************************************************/
1278 BOOL WINAPI SetConsoleCtrlHandler(PHANDLER_ROUTINE func, BOOL add)
1280 BOOL ret = TRUE;
1282 FIXME("(%p,%i) - no error checking or testing yet\n", func, add);
1284 if (!func)
1286 CONSOLE_IgnoreCtrlC = add;
1288 else if (add)
1290 struct ConsoleHandler* ch = HeapAlloc(GetProcessHeap(), 0, sizeof(struct ConsoleHandler));
1292 if (!ch) return FALSE;
1293 ch->handler = func;
1294 EnterCriticalSection(&CONSOLE_CritSect);
1295 ch->next = CONSOLE_Handlers;
1296 CONSOLE_Handlers = ch;
1297 LeaveCriticalSection(&CONSOLE_CritSect);
1299 else
1301 struct ConsoleHandler** ch;
1302 EnterCriticalSection(&CONSOLE_CritSect);
1303 for (ch = &CONSOLE_Handlers; *ch; *ch = (*ch)->next)
1305 if ((*ch)->handler == func) break;
1307 if (*ch)
1309 struct ConsoleHandler* rch = *ch;
1311 /* sanity check */
1312 if (rch == &CONSOLE_DefaultConsoleHandler)
1314 ERR("Who's trying to remove default handler???\n");
1315 ret = FALSE;
1317 else
1319 rch = *ch;
1320 *ch = (*ch)->next;
1321 HeapFree(GetProcessHeap(), 0, rch);
1324 else
1326 WARN("Attempt to remove non-installed CtrlHandler %p\n", func);
1327 ret = FALSE;
1329 LeaveCriticalSection(&CONSOLE_CritSect);
1331 return ret;
1334 static WINE_EXCEPTION_FILTER(CONSOLE_CtrlEventHandler)
1336 TRACE("(%lx)\n", GetExceptionCode());
1337 return EXCEPTION_EXECUTE_HANDLER;
1340 static DWORD WINAPI CONSOLE_HandleCtrlCEntry(void* pmt)
1342 struct ConsoleHandler* ch;
1344 EnterCriticalSection(&CONSOLE_CritSect);
1345 /* the debugger didn't continue... so, pass to ctrl handlers */
1346 for (ch = CONSOLE_Handlers; ch; ch = ch->next)
1348 if (ch->handler((DWORD)pmt)) break;
1350 LeaveCriticalSection(&CONSOLE_CritSect);
1351 return 0;
1354 /******************************************************************
1355 * CONSOLE_HandleCtrlC
1357 * Check whether the shall manipulate CtrlC events
1359 int CONSOLE_HandleCtrlC(unsigned sig)
1361 /* FIXME: better test whether a console is attached to this process ??? */
1362 extern unsigned CONSOLE_GetNumHistoryEntries(void);
1363 if (CONSOLE_GetNumHistoryEntries() == (unsigned)-1) return 0;
1364 if (CONSOLE_IgnoreCtrlC) return 1;
1366 /* try to pass the exception to the debugger
1367 * if it continues, there's nothing more to do
1368 * otherwise, we need to send the ctrl-event to the handlers
1370 __TRY
1372 RaiseException( DBG_CONTROL_C, 0, 0, NULL );
1374 __EXCEPT(CONSOLE_CtrlEventHandler)
1376 /* Create a separate thread to signal all the events. This would allow to
1377 * synchronize between setting the handlers and actually calling them
1379 CreateThread(NULL, 0, CONSOLE_HandleCtrlCEntry, (void*)CTRL_C_EVENT, 0, NULL);
1381 __ENDTRY;
1382 return 1;
1385 /******************************************************************************
1386 * GenerateConsoleCtrlEvent [KERNEL32.@] Simulate a CTRL-C or CTRL-BREAK
1388 * PARAMS
1389 * dwCtrlEvent [I] Type of event
1390 * dwProcessGroupID [I] Process group ID to send event to
1392 * RETURNS
1393 * Success: True
1394 * Failure: False (and *should* [but doesn't] set LastError)
1396 BOOL WINAPI GenerateConsoleCtrlEvent(DWORD dwCtrlEvent,
1397 DWORD dwProcessGroupID)
1399 BOOL ret;
1401 TRACE("(%ld, %ld)\n", dwCtrlEvent, dwProcessGroupID);
1403 if (dwCtrlEvent != CTRL_C_EVENT && dwCtrlEvent != CTRL_BREAK_EVENT)
1405 ERR("Invalid event %ld for PGID %ld\n", dwCtrlEvent, dwProcessGroupID);
1406 return FALSE;
1409 SERVER_START_REQ( send_console_signal )
1411 req->signal = dwCtrlEvent;
1412 req->group_id = dwProcessGroupID;
1413 ret = !wine_server_call_err( req );
1415 SERVER_END_REQ;
1417 return ret;
1421 /******************************************************************************
1422 * CreateConsoleScreenBuffer [KERNEL32.@] Creates a console screen buffer
1424 * PARAMS
1425 * dwDesiredAccess [I] Access flag
1426 * dwShareMode [I] Buffer share mode
1427 * sa [I] Security attributes
1428 * dwFlags [I] Type of buffer to create
1429 * lpScreenBufferData [I] Reserved
1431 * NOTES
1432 * Should call SetLastError
1434 * RETURNS
1435 * Success: Handle to new console screen buffer
1436 * Failure: INVALID_HANDLE_VALUE
1438 HANDLE WINAPI CreateConsoleScreenBuffer(DWORD dwDesiredAccess, DWORD dwShareMode,
1439 LPSECURITY_ATTRIBUTES sa, DWORD dwFlags,
1440 LPVOID lpScreenBufferData)
1442 HANDLE ret = INVALID_HANDLE_VALUE;
1444 TRACE("(%ld,%ld,%p,%ld,%p)\n",
1445 dwDesiredAccess, dwShareMode, sa, dwFlags, lpScreenBufferData);
1447 if (dwFlags != CONSOLE_TEXTMODE_BUFFER || lpScreenBufferData != NULL)
1449 SetLastError(ERROR_INVALID_PARAMETER);
1450 return INVALID_HANDLE_VALUE;
1453 SERVER_START_REQ(create_console_output)
1455 req->handle_in = 0;
1456 req->access = dwDesiredAccess;
1457 req->share = dwShareMode;
1458 req->inherit = (sa && sa->bInheritHandle);
1459 if (!wine_server_call_err( req )) ret = reply->handle_out;
1461 SERVER_END_REQ;
1463 return ret;
1467 /***********************************************************************
1468 * GetConsoleScreenBufferInfo (KERNEL32.@)
1470 BOOL WINAPI GetConsoleScreenBufferInfo(HANDLE hConsoleOutput, LPCONSOLE_SCREEN_BUFFER_INFO csbi)
1472 BOOL ret;
1474 SERVER_START_REQ(get_console_output_info)
1476 req->handle = hConsoleOutput;
1477 if ((ret = !wine_server_call_err( req )))
1479 csbi->dwSize.X = reply->width;
1480 csbi->dwSize.Y = reply->height;
1481 csbi->dwCursorPosition.X = reply->cursor_x;
1482 csbi->dwCursorPosition.Y = reply->cursor_y;
1483 csbi->wAttributes = reply->attr;
1484 csbi->srWindow.Left = reply->win_left;
1485 csbi->srWindow.Right = reply->win_right;
1486 csbi->srWindow.Top = reply->win_top;
1487 csbi->srWindow.Bottom = reply->win_bottom;
1488 csbi->dwMaximumWindowSize.X = reply->max_width;
1489 csbi->dwMaximumWindowSize.Y = reply->max_height;
1492 SERVER_END_REQ;
1494 return ret;
1498 /******************************************************************************
1499 * SetConsoleActiveScreenBuffer [KERNEL32.@] Sets buffer to current console
1501 * RETURNS
1502 * Success: TRUE
1503 * Failure: FALSE
1505 BOOL WINAPI SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput)
1507 BOOL ret;
1509 TRACE("(%p)\n", hConsoleOutput);
1511 SERVER_START_REQ( set_console_input_info )
1513 req->handle = 0;
1514 req->mask = SET_CONSOLE_INPUT_INFO_ACTIVE_SB;
1515 req->active_sb = hConsoleOutput;
1516 ret = !wine_server_call_err( req );
1518 SERVER_END_REQ;
1519 return ret;
1523 /***********************************************************************
1524 * GetConsoleMode (KERNEL32.@)
1526 BOOL WINAPI GetConsoleMode(HANDLE hcon, LPDWORD mode)
1528 BOOL ret;
1530 SERVER_START_REQ(get_console_mode)
1532 req->handle = hcon;
1533 ret = !wine_server_call_err( req );
1534 if (ret && mode) *mode = reply->mode;
1536 SERVER_END_REQ;
1537 return ret;
1541 /******************************************************************************
1542 * SetConsoleMode [KERNEL32.@] Sets input mode of console's input buffer
1544 * PARAMS
1545 * hcon [I] Handle to console input or screen buffer
1546 * mode [I] Input or output mode to set
1548 * RETURNS
1549 * Success: TRUE
1550 * Failure: FALSE
1552 * mode:
1553 * ENABLE_PROCESSED_INPUT 0x01
1554 * ENABLE_LINE_INPUT 0x02
1555 * ENABLE_ECHO_INPUT 0x04
1556 * ENABLE_WINDOW_INPUT 0x08
1557 * ENABLE_MOUSE_INPUT 0x10
1559 BOOL WINAPI SetConsoleMode(HANDLE hcon, DWORD mode)
1561 BOOL ret;
1563 SERVER_START_REQ(set_console_mode)
1565 req->handle = hcon;
1566 req->mode = mode;
1567 ret = !wine_server_call_err( req );
1569 SERVER_END_REQ;
1570 /* FIXME: when resetting a console input to editline mode, I think we should
1571 * empty the S_EditString buffer
1574 TRACE("(%p,%lx) retval == %d\n", hcon, mode, ret);
1576 return ret;
1580 /******************************************************************
1581 * write_char
1583 * WriteConsoleOutput helper: hides server call semantics
1585 static int write_char(HANDLE hCon, LPCWSTR lpBuffer, int nc, COORD* pos)
1587 int written = -1;
1589 if (!nc) return 0;
1591 SERVER_START_REQ( write_console_output )
1593 req->handle = hCon;
1594 req->x = pos->X;
1595 req->y = pos->Y;
1596 req->mode = CHAR_INFO_MODE_TEXTSTDATTR;
1597 req->wrap = FALSE;
1598 wine_server_add_data( req, lpBuffer, nc * sizeof(WCHAR) );
1599 if (!wine_server_call_err( req )) written = reply->written;
1601 SERVER_END_REQ;
1603 if (written > 0) pos->X += written;
1604 return written;
1607 /******************************************************************
1608 * next_line
1610 * WriteConsoleOutput helper: handles passing to next line (+scrolling if necessary)
1613 static int next_line(HANDLE hCon, CONSOLE_SCREEN_BUFFER_INFO* csbi)
1615 SMALL_RECT src;
1616 CHAR_INFO ci;
1617 COORD dst;
1619 csbi->dwCursorPosition.X = 0;
1620 csbi->dwCursorPosition.Y++;
1622 if (csbi->dwCursorPosition.Y < csbi->dwSize.Y) return 1;
1624 src.Top = 1;
1625 src.Bottom = csbi->dwSize.Y - 1;
1626 src.Left = 0;
1627 src.Right = csbi->dwSize.X - 1;
1629 dst.X = 0;
1630 dst.Y = 0;
1632 ci.Attributes = csbi->wAttributes;
1633 ci.Char.UnicodeChar = ' ';
1635 csbi->dwCursorPosition.Y--;
1636 if (!ScrollConsoleScreenBufferW(hCon, &src, NULL, dst, &ci))
1637 return 0;
1638 return 1;
1641 /******************************************************************
1642 * write_block
1644 * WriteConsoleOutput helper: writes a block of non special characters
1645 * Block can spread on several lines, and wrapping, if needed, is
1646 * handled
1649 static int write_block(HANDLE hCon, CONSOLE_SCREEN_BUFFER_INFO* csbi,
1650 DWORD mode, LPWSTR ptr, int len)
1652 int blk; /* number of chars to write on current line */
1654 if (len <= 0) return 1;
1656 if (mode & ENABLE_WRAP_AT_EOL_OUTPUT) /* writes remaining on next line */
1658 int done;
1660 for (done = 0; done < len; done += blk)
1662 blk = min(len - done, csbi->dwSize.X - csbi->dwCursorPosition.X);
1664 if (write_char(hCon, ptr + done, blk, &csbi->dwCursorPosition) != blk)
1665 return 0;
1666 if (csbi->dwCursorPosition.X == csbi->dwSize.X && !next_line(hCon, csbi))
1667 return 0;
1670 else
1672 blk = min(len, csbi->dwSize.X - csbi->dwCursorPosition.X);
1674 if (write_char(hCon, ptr, blk, &csbi->dwCursorPosition) != blk)
1675 return 0;
1676 if (blk < len)
1678 csbi->dwCursorPosition.X = csbi->dwSize.X - 1;
1679 /* all remaining chars should be written on last column,
1680 * so only overwrite the last column with last char in block
1682 if (write_char(hCon, ptr + len - 1, 1, &csbi->dwCursorPosition) != 1)
1683 return 0;
1684 csbi->dwCursorPosition.X = csbi->dwSize.X - 1;
1688 return 1;
1691 /***********************************************************************
1692 * WriteConsoleW (KERNEL32.@)
1694 BOOL WINAPI WriteConsoleW(HANDLE hConsoleOutput, LPCVOID lpBuffer, DWORD nNumberOfCharsToWrite,
1695 LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
1697 DWORD mode;
1698 DWORD nw = 0;
1699 WCHAR* psz = (WCHAR*)lpBuffer;
1700 CONSOLE_SCREEN_BUFFER_INFO csbi;
1701 int k, first = 0;
1703 TRACE("%p %s %ld %p %p\n",
1704 hConsoleOutput, debugstr_wn(lpBuffer, nNumberOfCharsToWrite),
1705 nNumberOfCharsToWrite, lpNumberOfCharsWritten, lpReserved);
1707 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = 0;
1709 if (!GetConsoleMode(hConsoleOutput, &mode) ||
1710 !GetConsoleScreenBufferInfo(hConsoleOutput, &csbi))
1711 return FALSE;
1713 if (mode & ENABLE_PROCESSED_OUTPUT)
1715 int i;
1717 for (i = 0; i < nNumberOfCharsToWrite; i++)
1719 switch (psz[i])
1721 case '\b': case '\t': case '\n': case '\a': case '\r':
1722 /* don't handle here the i-th char... done below */
1723 if ((k = i - first) > 0)
1725 if (!write_block(hConsoleOutput, &csbi, mode, &psz[first], k))
1726 goto the_end;
1727 nw += k;
1729 first = i + 1;
1730 nw++;
1732 switch (psz[i])
1734 case '\b':
1735 if (csbi.dwCursorPosition.X > 0) csbi.dwCursorPosition.X--;
1736 break;
1737 case '\t':
1739 WCHAR tmp[8] = {' ',' ',' ',' ',' ',' ',' ',' '};
1741 if (!write_block(hConsoleOutput, &csbi, mode, tmp,
1742 ((csbi.dwCursorPosition.X + 8) & ~7) - csbi.dwCursorPosition.X))
1743 goto the_end;
1745 break;
1746 case '\n':
1747 next_line(hConsoleOutput, &csbi);
1748 break;
1749 case '\a':
1750 Beep(400, 300);
1751 break;
1752 case '\r':
1753 csbi.dwCursorPosition.X = 0;
1754 break;
1755 default:
1756 break;
1761 /* write the remaining block (if any) if processed output is enabled, or the
1762 * entire buffer otherwise
1764 if ((k = nNumberOfCharsToWrite - first) > 0)
1766 if (!write_block(hConsoleOutput, &csbi, mode, &psz[first], k))
1767 goto the_end;
1768 nw += k;
1771 the_end:
1772 SetConsoleCursorPosition(hConsoleOutput, csbi.dwCursorPosition);
1773 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = nw;
1774 return nw != 0;
1778 /***********************************************************************
1779 * WriteConsoleA (KERNEL32.@)
1781 BOOL WINAPI WriteConsoleA(HANDLE hConsoleOutput, LPCVOID lpBuffer, DWORD nNumberOfCharsToWrite,
1782 LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
1784 BOOL ret;
1785 LPWSTR xstring;
1786 DWORD n;
1788 n = MultiByteToWideChar(CP_ACP, 0, lpBuffer, nNumberOfCharsToWrite, NULL, 0);
1790 if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = 0;
1791 xstring = HeapAlloc(GetProcessHeap(), 0, n * sizeof(WCHAR));
1792 if (!xstring) return 0;
1794 MultiByteToWideChar(CP_ACP, 0, lpBuffer, nNumberOfCharsToWrite, xstring, n);
1796 ret = WriteConsoleW(hConsoleOutput, xstring, n, lpNumberOfCharsWritten, 0);
1798 HeapFree(GetProcessHeap(), 0, xstring);
1800 return ret;
1803 /******************************************************************************
1804 * SetConsoleCursorPosition [KERNEL32.@]
1805 * Sets the cursor position in console
1807 * PARAMS
1808 * hConsoleOutput [I] Handle of console screen buffer
1809 * dwCursorPosition [I] New cursor position coordinates
1811 * RETURNS STD
1813 BOOL WINAPI SetConsoleCursorPosition(HANDLE hcon, COORD pos)
1815 BOOL ret;
1816 CONSOLE_SCREEN_BUFFER_INFO csbi;
1817 int do_move = 0;
1818 int w, h;
1820 TRACE("%p %d %d\n", hcon, pos.X, pos.Y);
1822 SERVER_START_REQ(set_console_output_info)
1824 req->handle = hcon;
1825 req->cursor_x = pos.X;
1826 req->cursor_y = pos.Y;
1827 req->mask = SET_CONSOLE_OUTPUT_INFO_CURSOR_POS;
1828 ret = !wine_server_call_err( req );
1830 SERVER_END_REQ;
1832 if (!ret || !GetConsoleScreenBufferInfo(hcon, &csbi))
1833 return FALSE;
1835 /* if cursor is no longer visible, scroll the visible window... */
1836 w = csbi.srWindow.Right - csbi.srWindow.Left + 1;
1837 h = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
1838 if (pos.X < csbi.srWindow.Left)
1840 csbi.srWindow.Left = min(pos.X, csbi.dwSize.X - w);
1841 do_move++;
1843 else if (pos.X > csbi.srWindow.Right)
1845 csbi.srWindow.Left = max(pos.X, w) - w + 1;
1846 do_move++;
1848 csbi.srWindow.Right = csbi.srWindow.Left + w - 1;
1850 if (pos.Y < csbi.srWindow.Top)
1852 csbi.srWindow.Top = min(pos.Y, csbi.dwSize.Y - h);
1853 do_move++;
1855 else if (pos.Y > csbi.srWindow.Bottom)
1857 csbi.srWindow.Top = max(pos.Y, h) - h + 1;
1858 do_move++;
1860 csbi.srWindow.Bottom = csbi.srWindow.Top + h - 1;
1862 ret = (do_move) ? SetConsoleWindowInfo(hcon, TRUE, &csbi.srWindow) : TRUE;
1864 return ret;
1867 /******************************************************************************
1868 * GetConsoleCursorInfo [KERNEL32.@] Gets size and visibility of console
1870 * PARAMS
1871 * hcon [I] Handle to console screen buffer
1872 * cinfo [O] Address of cursor information
1874 * RETURNS
1875 * Success: TRUE
1876 * Failure: FALSE
1878 BOOL WINAPI GetConsoleCursorInfo(HANDLE hcon, LPCONSOLE_CURSOR_INFO cinfo)
1880 BOOL ret;
1882 SERVER_START_REQ(get_console_output_info)
1884 req->handle = hcon;
1885 ret = !wine_server_call_err( req );
1886 if (ret && cinfo)
1888 cinfo->dwSize = reply->cursor_size;
1889 cinfo->bVisible = reply->cursor_visible;
1892 SERVER_END_REQ;
1893 return ret;
1897 /******************************************************************************
1898 * SetConsoleCursorInfo [KERNEL32.@] Sets size and visibility of cursor
1900 * PARAMS
1901 * hcon [I] Handle to console screen buffer
1902 * cinfo [I] Address of cursor information
1903 * RETURNS
1904 * Success: TRUE
1905 * Failure: FALSE
1907 BOOL WINAPI SetConsoleCursorInfo(HANDLE hCon, LPCONSOLE_CURSOR_INFO cinfo)
1909 BOOL ret;
1911 SERVER_START_REQ(set_console_output_info)
1913 req->handle = hCon;
1914 req->cursor_size = cinfo->dwSize;
1915 req->cursor_visible = cinfo->bVisible;
1916 req->mask = SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM;
1917 ret = !wine_server_call_err( req );
1919 SERVER_END_REQ;
1920 return ret;
1924 /******************************************************************************
1925 * SetConsoleWindowInfo [KERNEL32.@] Sets size and position of console
1927 * PARAMS
1928 * hcon [I] Handle to console screen buffer
1929 * bAbsolute [I] Coordinate type flag
1930 * window [I] Address of new window rectangle
1931 * RETURNS
1932 * Success: TRUE
1933 * Failure: FALSE
1935 BOOL WINAPI SetConsoleWindowInfo(HANDLE hCon, BOOL bAbsolute, LPSMALL_RECT window)
1937 SMALL_RECT p = *window;
1938 BOOL ret;
1940 if (!bAbsolute)
1942 CONSOLE_SCREEN_BUFFER_INFO csbi;
1943 if (!GetConsoleScreenBufferInfo(hCon, &csbi))
1944 return FALSE;
1945 p.Left += csbi.srWindow.Left;
1946 p.Top += csbi.srWindow.Top;
1947 p.Right += csbi.srWindow.Left;
1948 p.Bottom += csbi.srWindow.Top;
1950 SERVER_START_REQ(set_console_output_info)
1952 req->handle = hCon;
1953 req->win_left = p.Left;
1954 req->win_top = p.Top;
1955 req->win_right = p.Right;
1956 req->win_bottom = p.Bottom;
1957 req->mask = SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW;
1958 ret = !wine_server_call_err( req );
1960 SERVER_END_REQ;
1962 return ret;
1966 /******************************************************************************
1967 * SetConsoleTextAttribute [KERNEL32.@] Sets colors for text
1969 * Sets the foreground and background color attributes of characters
1970 * written to the screen buffer.
1972 * RETURNS
1973 * Success: TRUE
1974 * Failure: FALSE
1976 BOOL WINAPI SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttr)
1978 BOOL ret;
1980 SERVER_START_REQ(set_console_output_info)
1982 req->handle = hConsoleOutput;
1983 req->attr = wAttr;
1984 req->mask = SET_CONSOLE_OUTPUT_INFO_ATTR;
1985 ret = !wine_server_call_err( req );
1987 SERVER_END_REQ;
1988 return ret;
1992 /******************************************************************************
1993 * SetConsoleScreenBufferSize [KERNEL32.@] Changes size of console
1995 * PARAMS
1996 * hConsoleOutput [I] Handle to console screen buffer
1997 * dwSize [I] New size in character rows and cols
1999 * RETURNS
2000 * Success: TRUE
2001 * Failure: FALSE
2003 BOOL WINAPI SetConsoleScreenBufferSize(HANDLE hConsoleOutput, COORD dwSize)
2005 BOOL ret;
2007 SERVER_START_REQ(set_console_output_info)
2009 req->handle = hConsoleOutput;
2010 req->width = dwSize.X;
2011 req->height = dwSize.Y;
2012 req->mask = SET_CONSOLE_OUTPUT_INFO_SIZE;
2013 ret = !wine_server_call_err( req );
2015 SERVER_END_REQ;
2016 return ret;
2020 /******************************************************************************
2021 * ScrollConsoleScreenBufferA [KERNEL32.@]
2024 BOOL WINAPI ScrollConsoleScreenBufferA(HANDLE hConsoleOutput, LPSMALL_RECT lpScrollRect,
2025 LPSMALL_RECT lpClipRect, COORD dwDestOrigin,
2026 LPCHAR_INFO lpFill)
2028 CHAR_INFO ciw;
2030 ciw.Attributes = lpFill->Attributes;
2031 MultiByteToWideChar(CP_ACP, 0, &lpFill->Char.AsciiChar, 1, &ciw.Char.UnicodeChar, 1);
2033 return ScrollConsoleScreenBufferW(hConsoleOutput, lpScrollRect, lpClipRect,
2034 dwDestOrigin, &ciw);
2037 /******************************************************************
2038 * CONSOLE_FillLineUniform
2040 * Helper function for ScrollConsoleScreenBufferW
2041 * Fills a part of a line with a constant character info
2043 void CONSOLE_FillLineUniform(HANDLE hConsoleOutput, int i, int j, int len, LPCHAR_INFO lpFill)
2045 SERVER_START_REQ( fill_console_output )
2047 req->handle = hConsoleOutput;
2048 req->mode = CHAR_INFO_MODE_TEXTATTR;
2049 req->x = i;
2050 req->y = j;
2051 req->count = len;
2052 req->wrap = FALSE;
2053 req->data.ch = lpFill->Char.UnicodeChar;
2054 req->data.attr = lpFill->Attributes;
2055 wine_server_call_err( req );
2057 SERVER_END_REQ;
2060 /******************************************************************************
2061 * ScrollConsoleScreenBufferW [KERNEL32.@]
2065 BOOL WINAPI ScrollConsoleScreenBufferW(HANDLE hConsoleOutput, LPSMALL_RECT lpScrollRect,
2066 LPSMALL_RECT lpClipRect, COORD dwDestOrigin,
2067 LPCHAR_INFO lpFill)
2069 SMALL_RECT dst;
2070 DWORD ret;
2071 int i, j;
2072 int start = -1;
2073 SMALL_RECT clip;
2074 CONSOLE_SCREEN_BUFFER_INFO csbi;
2075 BOOL inside;
2077 if (lpClipRect)
2078 TRACE("(%p,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", hConsoleOutput,
2079 lpScrollRect->Left, lpScrollRect->Top,
2080 lpScrollRect->Right, lpScrollRect->Bottom,
2081 lpClipRect->Left, lpClipRect->Top,
2082 lpClipRect->Right, lpClipRect->Bottom,
2083 dwDestOrigin.X, dwDestOrigin.Y, lpFill);
2084 else
2085 TRACE("(%p,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", hConsoleOutput,
2086 lpScrollRect->Left, lpScrollRect->Top,
2087 lpScrollRect->Right, lpScrollRect->Bottom,
2088 dwDestOrigin.X, dwDestOrigin.Y, lpFill);
2090 if (!GetConsoleScreenBufferInfo(hConsoleOutput, &csbi))
2091 return FALSE;
2093 /* step 1: get dst rect */
2094 dst.Left = dwDestOrigin.X;
2095 dst.Top = dwDestOrigin.Y;
2096 dst.Right = dst.Left + (lpScrollRect->Right - lpScrollRect->Left);
2097 dst.Bottom = dst.Top + (lpScrollRect->Bottom - lpScrollRect->Top);
2099 /* step 2a: compute the final clip rect (optional passed clip and screen buffer limits */
2100 if (lpClipRect)
2102 clip.Left = max(0, lpClipRect->Left);
2103 clip.Right = min(csbi.dwSize.X - 1, lpClipRect->Right);
2104 clip.Top = max(0, lpClipRect->Top);
2105 clip.Bottom = min(csbi.dwSize.Y - 1, lpClipRect->Bottom);
2107 else
2109 clip.Left = 0;
2110 clip.Right = csbi.dwSize.X - 1;
2111 clip.Top = 0;
2112 clip.Bottom = csbi.dwSize.Y - 1;
2114 if (clip.Left > clip.Right || clip.Top > clip.Bottom) return FALSE;
2116 /* step 2b: clip dst rect */
2117 if (dst.Left < clip.Left ) dst.Left = clip.Left;
2118 if (dst.Top < clip.Top ) dst.Top = clip.Top;
2119 if (dst.Right > clip.Right ) dst.Right = clip.Right;
2120 if (dst.Bottom > clip.Bottom) dst.Bottom = clip.Bottom;
2122 /* step 3: transfer the bits */
2123 SERVER_START_REQ(move_console_output)
2125 req->handle = hConsoleOutput;
2126 req->x_src = lpScrollRect->Left;
2127 req->y_src = lpScrollRect->Top;
2128 req->x_dst = dst.Left;
2129 req->y_dst = dst.Top;
2130 req->w = dst.Right - dst.Left + 1;
2131 req->h = dst.Bottom - dst.Top + 1;
2132 ret = !wine_server_call_err( req );
2134 SERVER_END_REQ;
2136 if (!ret) return FALSE;
2138 /* step 4: clean out the exposed part */
2140 /* have to write cell [i,j] if it is not in dst rect (because it has already
2141 * been written to by the scroll) and is in clip (we shall not write
2142 * outside of clip)
2144 for (j = max(lpScrollRect->Top, clip.Top); j <= min(lpScrollRect->Bottom, clip.Bottom); j++)
2146 inside = dst.Top <= j && j <= dst.Bottom;
2147 start = -1;
2148 for (i = max(lpScrollRect->Left, clip.Left); i <= min(lpScrollRect->Right, clip.Right); i++)
2150 if (inside && dst.Left <= i && i <= dst.Right)
2152 if (start != -1)
2154 CONSOLE_FillLineUniform(hConsoleOutput, start, j, i - start, lpFill);
2155 start = -1;
2158 else
2160 if (start == -1) start = i;
2163 if (start != -1)
2164 CONSOLE_FillLineUniform(hConsoleOutput, start, j, i - start, lpFill);
2167 return TRUE;
2171 /* ====================================================================
2173 * Console manipulation functions
2175 * ====================================================================*/
2177 /* some missing functions...
2178 * FIXME: those are likely to be defined as undocumented function in kernel32 (or part of them)
2179 * should get the right API and implement them
2180 * GetConsoleCommandHistory[AW] (dword dword dword)
2181 * GetConsoleCommandHistoryLength[AW]
2182 * SetConsoleCommandHistoryMode
2183 * SetConsoleNumberOfCommands[AW]
2185 int CONSOLE_GetHistory(int idx, WCHAR* buf, int buf_len)
2187 int len = 0;
2189 SERVER_START_REQ( get_console_input_history )
2191 req->handle = 0;
2192 req->index = idx;
2193 if (buf && buf_len > 1)
2195 wine_server_set_reply( req, buf, (buf_len - 1) * sizeof(WCHAR) );
2197 if (!wine_server_call_err( req ))
2199 if (buf) buf[wine_server_reply_size(reply) / sizeof(WCHAR)] = 0;
2200 len = reply->total / sizeof(WCHAR) + 1;
2203 SERVER_END_REQ;
2204 return len;
2207 /******************************************************************
2208 * CONSOLE_AppendHistory
2212 BOOL CONSOLE_AppendHistory(const WCHAR* ptr)
2214 size_t len = strlenW(ptr);
2215 BOOL ret;
2217 while (len && (ptr[len - 1] == '\n' || ptr[len - 1] == '\r')) len--;
2219 SERVER_START_REQ( append_console_input_history )
2221 req->handle = 0;
2222 wine_server_add_data( req, ptr, len * sizeof(WCHAR) );
2223 ret = !wine_server_call_err( req );
2225 SERVER_END_REQ;
2226 return ret;
2229 /******************************************************************
2230 * CONSOLE_GetNumHistoryEntries
2234 unsigned CONSOLE_GetNumHistoryEntries(void)
2236 unsigned ret = -1;
2237 SERVER_START_REQ(get_console_input_info)
2239 req->handle = 0;
2240 if (!wine_server_call_err( req )) ret = reply->history_index;
2242 SERVER_END_REQ;
2243 return ret;
2246 /******************************************************************
2247 * CONSOLE_GetEditionMode
2251 BOOL CONSOLE_GetEditionMode(HANDLE hConIn, int* mode)
2253 unsigned ret = FALSE;
2254 SERVER_START_REQ(get_console_input_info)
2256 req->handle = hConIn;
2257 if ((ret = !wine_server_call_err( req )))
2258 *mode = reply->edition_mode;
2260 SERVER_END_REQ;
2261 return ret;