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
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 /* Reference applications:
26 * - IDA (interactive disassembler) full version 3.75. Works.
27 * - LYNX/W32. Works mostly, some keys crash it.
31 #include "wine/port.h"
42 #include "wine/server.h"
43 #include "wine/exception.h"
44 #include "wine/debug.h"
45 #include "msvcrt/excpt.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(console
);
50 extern WCHAR
* CONSOLE_Readline(HANDLE
, int);
52 static WCHAR
* S_EditString
/* = NULL */;
53 static unsigned S_EditStrPos
/* = 0 */;
55 /***********************************************************************
56 * FreeConsole (KERNEL32.@)
58 BOOL WINAPI
FreeConsole(VOID
)
62 SERVER_START_REQ(free_console
)
64 ret
= !wine_server_call_err( req
);
70 /******************************************************************
71 * start_console_renderer
73 * helper for AllocConsole
74 * starts the renderer process
76 static BOOL
start_console_renderer(void)
81 PROCESS_INFORMATION pi
;
84 OBJECT_ATTRIBUTES attr
;
86 attr
.Length
= sizeof(attr
);
87 attr
.RootDirectory
= 0;
88 attr
.Attributes
= OBJ_INHERIT
;
89 attr
.ObjectName
= NULL
;
90 attr
.SecurityDescriptor
= NULL
;
91 attr
.SecurityQualityOfService
= NULL
;
93 NtCreateEvent(&hEvent
, EVENT_ALL_ACCESS
, &attr
, TRUE
, FALSE
);
94 if (!hEvent
) return FALSE
;
96 memset(&si
, 0, sizeof(si
));
99 /* FIXME: use dynamic allocation for most of the buffers below */
100 /* first try environment variable */
101 if ((p
= getenv("WINECONSOLE")) != NULL
)
103 ret
= snprintf(buffer
, sizeof(buffer
), "%s --use-event=%d", p
, hEvent
);
104 if ((ret
> -1) && (ret
< sizeof(buffer
)) &&
105 CreateProcessA(NULL
, buffer
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
107 ERR("Couldn't launch Wine console from WINECONSOLE env var... trying default access\n");
110 /* then try the regular PATH */
111 sprintf(buffer
, "wineconsole --use-event=%d", hEvent
);
112 if (CreateProcessA(NULL
, buffer
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
118 if (WaitForSingleObject(hEvent
, INFINITE
) != WAIT_OBJECT_0
) goto the_end
;
121 TRACE("Started wineconsole pid=%08lx tid=%08lx\n", pi
.dwProcessId
, pi
.dwThreadId
);
126 ERR("Can't allocate console\n");
131 /***********************************************************************
132 * AllocConsole (KERNEL32.@)
134 * creates an xterm with a pty to our program
136 BOOL WINAPI
AllocConsole(void)
138 HANDLE handle_in
= INVALID_HANDLE_VALUE
;
139 HANDLE handle_out
= INVALID_HANDLE_VALUE
;
140 HANDLE handle_err
= INVALID_HANDLE_VALUE
;
145 handle_in
= CreateFileA( "CONIN$", GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,
146 0, NULL
, OPEN_EXISTING
, 0, 0 );
148 if (handle_in
!= INVALID_HANDLE_VALUE
)
150 /* we already have a console opened on this process, don't create a new one */
151 CloseHandle(handle_in
);
155 if (!start_console_renderer())
158 handle_in
= CreateFileA( "CONIN$", GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,
159 0, NULL
, OPEN_EXISTING
, 0, 0 );
160 if (handle_in
== INVALID_HANDLE_VALUE
) goto the_end
;
162 handle_out
= CreateFileA( "CONOUT$", GENERIC_READ
|GENERIC_WRITE
,
163 0, NULL
, OPEN_EXISTING
, 0, 0 );
164 if (handle_out
== INVALID_HANDLE_VALUE
) goto the_end
;
166 if (!DuplicateHandle(GetCurrentProcess(), handle_out
, GetCurrentProcess(), &handle_err
,
167 0, TRUE
, DUPLICATE_SAME_ACCESS
))
170 /* NT resets the STD_*_HANDLEs on console alloc */
171 SetStdHandle(STD_INPUT_HANDLE
, handle_in
);
172 SetStdHandle(STD_OUTPUT_HANDLE
, handle_out
);
173 SetStdHandle(STD_ERROR_HANDLE
, handle_err
);
175 GetStartupInfoW(&si
);
176 if (si
.dwFlags
& STARTF_USECOUNTCHARS
)
179 c
.X
= si
.dwXCountChars
;
180 c
.Y
= si
.dwYCountChars
;
181 SetConsoleScreenBufferSize(handle_out
, c
);
183 if (si
.dwFlags
& STARTF_USEFILLATTRIBUTE
)
184 SetConsoleTextAttribute(handle_out
, si
.dwFillAttribute
);
186 SetConsoleTitleW(si
.lpTitle
);
188 SetLastError(ERROR_SUCCESS
);
193 ERR("Can't allocate console\n");
194 if (handle_in
!= INVALID_HANDLE_VALUE
) CloseHandle(handle_in
);
195 if (handle_out
!= INVALID_HANDLE_VALUE
) CloseHandle(handle_out
);
196 if (handle_err
!= INVALID_HANDLE_VALUE
) CloseHandle(handle_err
);
202 /******************************************************************************
205 * Helper function for ReadConsole, ReadConsoleInput and PeekConsoleInput
207 static BOOL
read_console_input(HANDLE handle
, LPINPUT_RECORD buffer
, DWORD count
,
208 LPDWORD pRead
, BOOL flush
)
213 SERVER_START_REQ( read_console_input
)
215 req
->handle
= handle
;
217 wine_server_set_reply( req
, buffer
, count
* sizeof(INPUT_RECORD
) );
218 if ((ret
= !wine_server_call_err( req
))) read
= reply
->read
;
221 if (pRead
) *pRead
= read
;
226 /***********************************************************************
227 * ReadConsoleA (KERNEL32.@)
229 BOOL WINAPI
ReadConsoleA(HANDLE hConsoleInput
, LPVOID lpBuffer
, DWORD nNumberOfCharsToRead
,
230 LPDWORD lpNumberOfCharsRead
, LPVOID lpReserved
)
232 LPWSTR ptr
= HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead
* sizeof(WCHAR
));
236 if ((ret
= ReadConsoleW(hConsoleInput
, ptr
, nNumberOfCharsToRead
, &ncr
, NULL
)))
237 ncr
= WideCharToMultiByte(CP_ACP
, 0, ptr
, ncr
, lpBuffer
, nNumberOfCharsToRead
, NULL
, NULL
);
239 if (lpNumberOfCharsRead
) *lpNumberOfCharsRead
= ncr
;
240 HeapFree(GetProcessHeap(), 0, ptr
);
245 /***********************************************************************
246 * ReadConsoleW (KERNEL32.@)
248 BOOL WINAPI
ReadConsoleW(HANDLE hConsoleInput
, LPVOID lpBuffer
,
249 DWORD nNumberOfCharsToRead
, LPDWORD lpNumberOfCharsRead
, LPVOID lpReserved
)
252 LPWSTR xbuf
= (LPWSTR
)lpBuffer
;
255 TRACE("(%d,%p,%ld,%p,%p)\n",
256 hConsoleInput
, lpBuffer
, nNumberOfCharsToRead
, lpNumberOfCharsRead
, lpReserved
);
258 if (!GetConsoleMode(hConsoleInput
, &mode
))
261 if (mode
& ENABLE_LINE_INPUT
)
263 if (!S_EditString
|| S_EditString
[S_EditStrPos
] == 0)
265 if (S_EditString
) HeapFree(GetProcessHeap(), 0, S_EditString
);
266 if (!(S_EditString
= CONSOLE_Readline(hConsoleInput
, mode
& WINE_ENABLE_LINE_INPUT_EMACS
)))
270 charsread
= lstrlenW(&S_EditString
[S_EditStrPos
]);
271 if (charsread
> nNumberOfCharsToRead
) charsread
= nNumberOfCharsToRead
;
272 memcpy(xbuf
, &S_EditString
[S_EditStrPos
], charsread
* sizeof(WCHAR
));
273 S_EditStrPos
+= charsread
;
280 /* FIXME: should we read at least 1 char? The SDK does not say */
281 /* wait for at least one available input record (it doesn't mean we'll have
282 * chars stored in xbuf...
284 WaitForSingleObject(hConsoleInput
, INFINITE
);
285 for (charsread
= 0; charsread
< nNumberOfCharsToRead
;)
287 if (!read_console_input(hConsoleInput
, &ir
, 1, &count
, TRUE
)) return FALSE
;
288 if (count
&& ir
.EventType
== KEY_EVENT
&& ir
.Event
.KeyEvent
.bKeyDown
&&
289 ir
.Event
.KeyEvent
.uChar
.UnicodeChar
&&
290 !(ir
.Event
.KeyEvent
.dwControlKeyState
& ENHANCED_KEY
))
292 xbuf
[charsread
++] = ir
.Event
.KeyEvent
.uChar
.UnicodeChar
;
297 if (lpNumberOfCharsRead
) *lpNumberOfCharsRead
= charsread
;
303 /***********************************************************************
304 * ReadConsoleInputW (KERNEL32.@)
306 BOOL WINAPI
ReadConsoleInputW(HANDLE hConsoleInput
, LPINPUT_RECORD lpBuffer
,
307 DWORD nLength
, LPDWORD lpNumberOfEventsRead
)
313 if (lpNumberOfEventsRead
) *lpNumberOfEventsRead
= 0;
317 /* loop until we get at least one event */
320 WaitForSingleObject(hConsoleInput
, INFINITE
);
321 if (!read_console_input(hConsoleInput
, lpBuffer
, nLength
, &count
, TRUE
))
325 if (lpNumberOfEventsRead
) *lpNumberOfEventsRead
= count
;
332 /******************************************************************************
333 * WriteConsoleOutputCharacterW [KERNEL32.@] Copies character to consecutive
334 * cells in the console screen buffer
337 * hConsoleOutput [I] Handle to screen buffer
338 * str [I] Pointer to buffer with chars to write
339 * length [I] Number of cells to write to
340 * coord [I] Coords of first cell
341 * lpNumCharsWritten [O] Pointer to number of cells written
348 BOOL WINAPI
WriteConsoleOutputCharacterW( HANDLE hConsoleOutput
, LPCWSTR str
, DWORD length
,
349 COORD coord
, LPDWORD lpNumCharsWritten
)
353 TRACE("(%d,%s,%ld,%dx%d,%p)\n", hConsoleOutput
,
354 debugstr_wn(str
, length
), length
, coord
.X
, coord
.Y
, lpNumCharsWritten
);
356 SERVER_START_REQ( write_console_output
)
358 req
->handle
= hConsoleOutput
;
361 req
->mode
= CHAR_INFO_MODE_TEXT
;
363 wine_server_add_data( req
, str
, length
* sizeof(WCHAR
) );
364 if ((ret
= !wine_server_call_err( req
)))
366 if (lpNumCharsWritten
) *lpNumCharsWritten
= reply
->written
;
374 /******************************************************************************
375 * SetConsoleTitleW [KERNEL32.@] Sets title bar string for console
378 * title [I] Address of new title
384 BOOL WINAPI
SetConsoleTitleW(LPCWSTR title
)
388 SERVER_START_REQ( set_console_input_info
)
391 req
->mask
= SET_CONSOLE_INPUT_INFO_TITLE
;
392 wine_server_add_data( req
, title
, strlenW(title
) * sizeof(WCHAR
) );
393 ret
= !wine_server_call_err( req
);
400 /***********************************************************************
401 * GetNumberOfConsoleMouseButtons (KERNEL32.@)
403 BOOL WINAPI
GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons
)
405 FIXME("(%p): stub\n", nrofbuttons
);
410 /******************************************************************************
411 * SetConsoleInputExeNameW [KERNEL32.@]
416 BOOL WINAPI
SetConsoleInputExeNameW(LPCWSTR name
)
418 FIXME("(%s): stub!\n", debugstr_w(name
));
420 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
424 /******************************************************************************
425 * SetConsoleInputExeNameA [KERNEL32.@]
430 BOOL WINAPI
SetConsoleInputExeNameA(LPCSTR name
)
432 int len
= MultiByteToWideChar(CP_ACP
, 0, name
, -1, NULL
, 0);
433 LPWSTR xptr
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
436 if (!xptr
) return FALSE
;
438 MultiByteToWideChar(CP_ACP
, 0, name
, -1, xptr
, len
);
439 ret
= SetConsoleInputExeNameW(xptr
);
440 HeapFree(GetProcessHeap(), 0, xptr
);
445 /******************************************************************
446 * CONSOLE_DefaultHandler
448 * Final control event handler
450 static BOOL WINAPI
CONSOLE_DefaultHandler(DWORD dwCtrlType
)
452 FIXME("Terminating process %lx on event %lx\n", GetCurrentProcessId(), dwCtrlType
);
454 /* should never go here */
458 /******************************************************************************
459 * SetConsoleCtrlHandler [KERNEL32.@] Adds function to calling process list
462 * func [I] Address of handler function
463 * add [I] Handler to add or remove
470 * James Sutherland (JamesSutherland@gmx.de)
471 * Added global variables console_ignore_ctrl_c and handlers[]
472 * Does not yet do any error checking, or set LastError if failed.
473 * This doesn't yet matter, since these handlers are not yet called...!
476 struct ConsoleHandler
{
477 PHANDLER_ROUTINE handler
;
478 struct ConsoleHandler
* next
;
481 static unsigned int CONSOLE_IgnoreCtrlC
= 0; /* FIXME: this should be inherited somehow */
482 static struct ConsoleHandler CONSOLE_DefaultConsoleHandler
= {CONSOLE_DefaultHandler
, NULL
};
483 static struct ConsoleHandler
* CONSOLE_Handlers
= &CONSOLE_DefaultConsoleHandler
;
484 static CRITICAL_SECTION CONSOLE_CritSect
= CRITICAL_SECTION_INIT("console_ctrl_section");
486 /*****************************************************************************/
488 BOOL WINAPI
SetConsoleCtrlHandler(PHANDLER_ROUTINE func
, BOOL add
)
492 FIXME("(%p,%i) - no error checking or testing yet\n", func
, add
);
496 CONSOLE_IgnoreCtrlC
= add
;
500 struct ConsoleHandler
* ch
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct ConsoleHandler
));
502 if (!ch
) return FALSE
;
504 EnterCriticalSection(&CONSOLE_CritSect
);
505 ch
->next
= CONSOLE_Handlers
;
506 CONSOLE_Handlers
= ch
;
507 LeaveCriticalSection(&CONSOLE_CritSect
);
511 struct ConsoleHandler
** ch
;
512 EnterCriticalSection(&CONSOLE_CritSect
);
513 for (ch
= &CONSOLE_Handlers
; *ch
; *ch
= (*ch
)->next
)
515 if ((*ch
)->handler
== func
) break;
519 struct ConsoleHandler
* rch
= *ch
;
522 if (rch
== &CONSOLE_DefaultConsoleHandler
)
524 ERR("Who's trying to remove default handler???\n");
531 HeapFree(GetProcessHeap(), 0, rch
);
536 WARN("Attempt to remove non-installed CtrlHandler %p\n", func
);
539 LeaveCriticalSection(&CONSOLE_CritSect
);
544 static WINE_EXCEPTION_FILTER(CONSOLE_CtrlEventHandler
)
546 TRACE("(%lx)\n", GetExceptionCode());
547 return EXCEPTION_EXECUTE_HANDLER
;
550 static DWORD WINAPI
CONSOLE_HandleCtrlCEntry(void* pmt
)
552 struct ConsoleHandler
* ch
;
554 EnterCriticalSection(&CONSOLE_CritSect
);
555 /* the debugger didn't continue... so, pass to ctrl handlers */
556 for (ch
= CONSOLE_Handlers
; ch
; ch
= ch
->next
)
558 if (ch
->handler((DWORD
)pmt
)) break;
560 LeaveCriticalSection(&CONSOLE_CritSect
);
564 /******************************************************************
565 * CONSOLE_HandleCtrlC
567 * Check whether the shall manipulate CtrlC events
569 int CONSOLE_HandleCtrlC(void)
571 /* FIXME: better test whether a console is attached to this process ??? */
572 extern unsigned CONSOLE_GetNumHistoryEntries(void);
573 if (CONSOLE_GetNumHistoryEntries() == (unsigned)-1) return 0;
574 if (CONSOLE_IgnoreCtrlC
) return 1;
576 /* try to pass the exception to the debugger
577 * if it continues, there's nothing more to do
578 * otherwise, we need to send the ctrl-event to the handlers
582 RaiseException( DBG_CONTROL_C
, 0, 0, NULL
);
584 __EXCEPT(CONSOLE_CtrlEventHandler
)
586 /* Create a separate thread to signal all the events. This would allow to
587 * synchronize between setting the handlers and actually calling them
589 CreateThread(NULL
, 0, CONSOLE_HandleCtrlCEntry
, (void*)CTRL_C_EVENT
, 0, NULL
);
595 /******************************************************************************
596 * GenerateConsoleCtrlEvent [KERNEL32.@] Simulate a CTRL-C or CTRL-BREAK
599 * dwCtrlEvent [I] Type of event
600 * dwProcessGroupID [I] Process group ID to send event to
604 * Failure: False (and *should* [but doesn't] set LastError)
606 BOOL WINAPI
GenerateConsoleCtrlEvent(DWORD dwCtrlEvent
,
607 DWORD dwProcessGroupID
)
611 TRACE("(%ld, %ld)\n", dwCtrlEvent
, dwProcessGroupID
);
613 if (dwCtrlEvent
!= CTRL_C_EVENT
&& dwCtrlEvent
!= CTRL_BREAK_EVENT
)
615 ERR("Invalid event %ld for PGID %ld\n", dwCtrlEvent
, dwProcessGroupID
);
619 SERVER_START_REQ( send_console_signal
)
621 req
->signal
= dwCtrlEvent
;
622 req
->group_id
= (void*)dwProcessGroupID
;
623 ret
= !wine_server_call_err( req
);
631 /******************************************************************************
632 * CreateConsoleScreenBuffer [KERNEL32.@] Creates a console screen buffer
635 * dwDesiredAccess [I] Access flag
636 * dwShareMode [I] Buffer share mode
637 * sa [I] Security attributes
638 * dwFlags [I] Type of buffer to create
639 * lpScreenBufferData [I] Reserved
642 * Should call SetLastError
645 * Success: Handle to new console screen buffer
646 * Failure: INVALID_HANDLE_VALUE
648 HANDLE WINAPI
CreateConsoleScreenBuffer(DWORD dwDesiredAccess
, DWORD dwShareMode
,
649 LPSECURITY_ATTRIBUTES sa
, DWORD dwFlags
,
650 LPVOID lpScreenBufferData
)
652 HANDLE ret
= INVALID_HANDLE_VALUE
;
654 TRACE("(%ld,%ld,%p,%ld,%p)\n",
655 dwDesiredAccess
, dwShareMode
, sa
, dwFlags
, lpScreenBufferData
);
657 if (dwFlags
!= CONSOLE_TEXTMODE_BUFFER
|| lpScreenBufferData
!= NULL
)
659 SetLastError(ERROR_INVALID_PARAMETER
);
660 return INVALID_HANDLE_VALUE
;
663 SERVER_START_REQ(create_console_output
)
666 req
->access
= dwDesiredAccess
;
667 req
->share
= dwShareMode
;
668 req
->inherit
= (sa
&& sa
->bInheritHandle
);
669 if (!wine_server_call_err( req
)) ret
= reply
->handle_out
;
677 /***********************************************************************
678 * GetConsoleScreenBufferInfo (KERNEL32.@)
680 BOOL WINAPI
GetConsoleScreenBufferInfo(HANDLE hConsoleOutput
, LPCONSOLE_SCREEN_BUFFER_INFO csbi
)
684 SERVER_START_REQ(get_console_output_info
)
686 req
->handle
= hConsoleOutput
;
687 if ((ret
= !wine_server_call_err( req
)))
689 csbi
->dwSize
.X
= reply
->width
;
690 csbi
->dwSize
.Y
= reply
->height
;
691 csbi
->dwCursorPosition
.X
= reply
->cursor_x
;
692 csbi
->dwCursorPosition
.Y
= reply
->cursor_y
;
693 csbi
->wAttributes
= reply
->attr
;
694 csbi
->srWindow
.Left
= reply
->win_left
;
695 csbi
->srWindow
.Right
= reply
->win_right
;
696 csbi
->srWindow
.Top
= reply
->win_top
;
697 csbi
->srWindow
.Bottom
= reply
->win_bottom
;
698 csbi
->dwMaximumWindowSize
.X
= reply
->max_width
;
699 csbi
->dwMaximumWindowSize
.Y
= reply
->max_height
;
708 /******************************************************************************
709 * SetConsoleActiveScreenBuffer [KERNEL32.@] Sets buffer to current console
715 BOOL WINAPI
SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput
)
719 TRACE("(%x)\n", hConsoleOutput
);
721 SERVER_START_REQ( set_console_input_info
)
724 req
->mask
= SET_CONSOLE_INPUT_INFO_ACTIVE_SB
;
725 req
->active_sb
= hConsoleOutput
;
726 ret
= !wine_server_call_err( req
);
733 /***********************************************************************
734 * GetConsoleMode (KERNEL32.@)
736 BOOL WINAPI
GetConsoleMode(HANDLE hcon
, LPDWORD mode
)
740 SERVER_START_REQ(get_console_mode
)
743 ret
= !wine_server_call_err( req
);
744 if (ret
&& mode
) *mode
= reply
->mode
;
751 /******************************************************************************
752 * SetConsoleMode [KERNEL32.@] Sets input mode of console's input buffer
755 * hcon [I] Handle to console input or screen buffer
756 * mode [I] Input or output mode to set
762 BOOL WINAPI
SetConsoleMode(HANDLE hcon
, DWORD mode
)
766 TRACE("(%x,%lx)\n", hcon
, mode
);
768 SERVER_START_REQ(set_console_mode
)
772 ret
= !wine_server_call_err( req
);
775 /* FIXME: when resetting a console input to editline mode, I think we should
776 * empty the S_EditString buffer
782 /******************************************************************
785 * WriteConsoleOutput helper: hides server call semantics
787 static int write_char(HANDLE hCon
, LPCWSTR lpBuffer
, int nc
, COORD
* pos
)
793 SERVER_START_REQ( write_console_output
)
798 req
->mode
= CHAR_INFO_MODE_TEXTSTDATTR
;
800 wine_server_add_data( req
, lpBuffer
, nc
* sizeof(WCHAR
) );
801 if (!wine_server_call_err( req
)) written
= reply
->written
;
805 if (written
> 0) pos
->X
+= written
;
809 /******************************************************************
812 * WriteConsoleOutput helper: handles passing to next line (+scrolling if necessary)
815 static int next_line(HANDLE hCon
, CONSOLE_SCREEN_BUFFER_INFO
* csbi
)
821 csbi
->dwCursorPosition
.X
= 0;
822 csbi
->dwCursorPosition
.Y
++;
824 if (csbi
->dwCursorPosition
.Y
< csbi
->dwSize
.Y
) return 1;
827 src
.Bottom
= csbi
->dwSize
.Y
- 1;
829 src
.Right
= csbi
->dwSize
.X
- 1;
834 ci
.Attributes
= csbi
->wAttributes
;
835 ci
.Char
.UnicodeChar
= ' ';
837 csbi
->dwCursorPosition
.Y
--;
838 if (!ScrollConsoleScreenBufferW(hCon
, &src
, NULL
, dst
, &ci
))
843 /******************************************************************
846 * WriteConsoleOutput helper: writes a block of non special characters
847 * Block can spread on several lines, and wrapping, if needed, is
851 static int write_block(HANDLE hCon
, CONSOLE_SCREEN_BUFFER_INFO
* csbi
,
852 DWORD mode
, LPWSTR ptr
, int len
)
854 int blk
; /* number of chars to write on current line */
856 if (len
<= 0) return 1;
858 if (mode
& ENABLE_WRAP_AT_EOL_OUTPUT
) /* writes remaining on next line */
862 for (done
= 0; done
< len
; done
+= blk
)
864 blk
= min(len
- done
, csbi
->dwSize
.X
- csbi
->dwCursorPosition
.X
);
866 if (write_char(hCon
, ptr
+ done
, blk
, &csbi
->dwCursorPosition
) != blk
)
868 if (csbi
->dwCursorPosition
.X
== csbi
->dwSize
.X
&& !next_line(hCon
, csbi
))
874 blk
= min(len
, csbi
->dwSize
.X
- csbi
->dwCursorPosition
.X
);
876 if (write_char(hCon
, ptr
, blk
, &csbi
->dwCursorPosition
) != blk
)
880 csbi
->dwCursorPosition
.X
= csbi
->dwSize
.X
- 1;
881 /* all remaining chars should be written on last column,
882 * so only overwrite the last column with last char in block
884 if (write_char(hCon
, ptr
+ len
- 1, 1, &csbi
->dwCursorPosition
) != 1)
886 csbi
->dwCursorPosition
.X
= csbi
->dwSize
.X
- 1;
893 /***********************************************************************
894 * WriteConsoleW (KERNEL32.@)
896 BOOL WINAPI
WriteConsoleW(HANDLE hConsoleOutput
, LPCVOID lpBuffer
, DWORD nNumberOfCharsToWrite
,
897 LPDWORD lpNumberOfCharsWritten
, LPVOID lpReserved
)
901 WCHAR
* psz
= (WCHAR
*)lpBuffer
;
902 CONSOLE_SCREEN_BUFFER_INFO csbi
;
905 TRACE("%d %s %ld %p %p\n",
906 hConsoleOutput
, debugstr_wn(lpBuffer
, nNumberOfCharsToWrite
),
907 nNumberOfCharsToWrite
, lpNumberOfCharsWritten
, lpReserved
);
909 if (lpNumberOfCharsWritten
) *lpNumberOfCharsWritten
= 0;
911 if (!GetConsoleMode(hConsoleOutput
, &mode
) ||
912 !GetConsoleScreenBufferInfo(hConsoleOutput
, &csbi
))
915 if (mode
& ENABLE_PROCESSED_OUTPUT
)
919 for (i
= 0; i
< nNumberOfCharsToWrite
; i
++)
923 case '\b': case '\t': case '\n': case '\a': case '\r':
924 /* don't handle here the i-th char... done below */
925 if ((k
= i
- first
) > 0)
927 if (!write_block(hConsoleOutput
, &csbi
, mode
, &psz
[first
], k
))
937 if (csbi
.dwCursorPosition
.X
> 0) csbi
.dwCursorPosition
.X
--;
941 WCHAR tmp
[8] = {' ',' ',' ',' ',' ',' ',' ',' '};
943 if (!write_block(hConsoleOutput
, &csbi
, mode
, tmp
,
944 ((csbi
.dwCursorPosition
.X
+ 8) & ~7) - csbi
.dwCursorPosition
.X
))
949 next_line(hConsoleOutput
, &csbi
);
955 csbi
.dwCursorPosition
.X
= 0;
963 /* write the remaining block (if any) if processed output is enabled, or the
964 * entire buffer otherwise
966 if ((k
= nNumberOfCharsToWrite
- first
) > 0)
968 if (!write_block(hConsoleOutput
, &csbi
, mode
, &psz
[first
], k
))
974 SetConsoleCursorPosition(hConsoleOutput
, csbi
.dwCursorPosition
);
975 if (lpNumberOfCharsWritten
) *lpNumberOfCharsWritten
= nw
;
980 /***********************************************************************
981 * WriteConsoleA (KERNEL32.@)
983 BOOL WINAPI
WriteConsoleA(HANDLE hConsoleOutput
, LPCVOID lpBuffer
, DWORD nNumberOfCharsToWrite
,
984 LPDWORD lpNumberOfCharsWritten
, LPVOID lpReserved
)
990 n
= MultiByteToWideChar(CP_ACP
, 0, lpBuffer
, nNumberOfCharsToWrite
, NULL
, 0);
992 if (lpNumberOfCharsWritten
) *lpNumberOfCharsWritten
= 0;
993 xstring
= HeapAlloc(GetProcessHeap(), 0, n
* sizeof(WCHAR
));
994 if (!xstring
) return 0;
996 MultiByteToWideChar(CP_ACP
, 0, lpBuffer
, nNumberOfCharsToWrite
, xstring
, n
);
998 ret
= WriteConsoleW(hConsoleOutput
, xstring
, n
, lpNumberOfCharsWritten
, 0);
1000 HeapFree(GetProcessHeap(), 0, xstring
);
1005 /******************************************************************************
1006 * SetConsoleCursorPosition [KERNEL32.@]
1007 * Sets the cursor position in console
1010 * hConsoleOutput [I] Handle of console screen buffer
1011 * dwCursorPosition [I] New cursor position coordinates
1015 BOOL WINAPI
SetConsoleCursorPosition(HANDLE hcon
, COORD pos
)
1018 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1022 TRACE("%x %d %d\n", hcon
, pos
.X
, pos
.Y
);
1024 SERVER_START_REQ(set_console_output_info
)
1027 req
->cursor_x
= pos
.X
;
1028 req
->cursor_y
= pos
.Y
;
1029 req
->mask
= SET_CONSOLE_OUTPUT_INFO_CURSOR_POS
;
1030 ret
= !wine_server_call_err( req
);
1034 if (!ret
|| !GetConsoleScreenBufferInfo(hcon
, &csbi
))
1037 /* if cursor is no longer visible, scroll the visible window... */
1038 w
= csbi
.srWindow
.Right
- csbi
.srWindow
.Left
+ 1;
1039 h
= csbi
.srWindow
.Bottom
- csbi
.srWindow
.Top
+ 1;
1040 if (pos
.X
< csbi
.srWindow
.Left
)
1042 csbi
.srWindow
.Left
= min(pos
.X
, csbi
.dwSize
.X
- w
);
1045 else if (pos
.X
> csbi
.srWindow
.Right
)
1047 csbi
.srWindow
.Left
= max(pos
.X
, w
) - w
+ 1;
1050 csbi
.srWindow
.Right
= csbi
.srWindow
.Left
+ w
- 1;
1052 if (pos
.Y
< csbi
.srWindow
.Top
)
1054 csbi
.srWindow
.Top
= min(pos
.Y
, csbi
.dwSize
.Y
- h
);
1057 else if (pos
.Y
> csbi
.srWindow
.Bottom
)
1059 csbi
.srWindow
.Top
= max(pos
.Y
, h
) - h
+ 1;
1062 csbi
.srWindow
.Bottom
= csbi
.srWindow
.Top
+ h
- 1;
1064 ret
= (do_move
) ? SetConsoleWindowInfo(hcon
, TRUE
, &csbi
.srWindow
) : TRUE
;
1069 /******************************************************************************
1070 * GetConsoleCursorInfo [KERNEL32.@] Gets size and visibility of console
1073 * hcon [I] Handle to console screen buffer
1074 * cinfo [O] Address of cursor information
1080 BOOL WINAPI
GetConsoleCursorInfo(HANDLE hcon
, LPCONSOLE_CURSOR_INFO cinfo
)
1084 SERVER_START_REQ(get_console_output_info
)
1087 ret
= !wine_server_call_err( req
);
1090 cinfo
->dwSize
= reply
->cursor_size
;
1091 cinfo
->bVisible
= reply
->cursor_visible
;
1099 /******************************************************************************
1100 * SetConsoleCursorInfo [KERNEL32.@] Sets size and visibility of cursor
1103 * hcon [I] Handle to console screen buffer
1104 * cinfo [I] Address of cursor information
1109 BOOL WINAPI
SetConsoleCursorInfo(HANDLE hCon
, LPCONSOLE_CURSOR_INFO cinfo
)
1113 SERVER_START_REQ(set_console_output_info
)
1116 req
->cursor_size
= cinfo
->dwSize
;
1117 req
->cursor_visible
= cinfo
->bVisible
;
1118 req
->mask
= SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM
;
1119 ret
= !wine_server_call_err( req
);
1126 /******************************************************************************
1127 * SetConsoleWindowInfo [KERNEL32.@] Sets size and position of console
1130 * hcon [I] Handle to console screen buffer
1131 * bAbsolute [I] Coordinate type flag
1132 * window [I] Address of new window rectangle
1137 BOOL WINAPI
SetConsoleWindowInfo(HANDLE hCon
, BOOL bAbsolute
, LPSMALL_RECT window
)
1139 SMALL_RECT p
= *window
;
1144 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1145 if (!GetConsoleScreenBufferInfo(hCon
, &csbi
))
1147 p
.Left
+= csbi
.srWindow
.Left
;
1148 p
.Top
+= csbi
.srWindow
.Top
;
1149 p
.Right
+= csbi
.srWindow
.Left
;
1150 p
.Bottom
+= csbi
.srWindow
.Top
;
1152 SERVER_START_REQ(set_console_output_info
)
1155 req
->win_left
= p
.Left
;
1156 req
->win_top
= p
.Top
;
1157 req
->win_right
= p
.Right
;
1158 req
->win_bottom
= p
.Bottom
;
1159 req
->mask
= SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW
;
1160 ret
= !wine_server_call_err( req
);
1168 /******************************************************************************
1169 * SetConsoleTextAttribute [KERNEL32.@] Sets colors for text
1171 * Sets the foreground and background color attributes of characters
1172 * written to the screen buffer.
1178 BOOL WINAPI
SetConsoleTextAttribute(HANDLE hConsoleOutput
, WORD wAttr
)
1182 SERVER_START_REQ(set_console_output_info
)
1184 req
->handle
= hConsoleOutput
;
1186 req
->mask
= SET_CONSOLE_OUTPUT_INFO_ATTR
;
1187 ret
= !wine_server_call_err( req
);
1194 /******************************************************************************
1195 * SetConsoleScreenBufferSize [KERNEL32.@] Changes size of console
1198 * hConsoleOutput [I] Handle to console screen buffer
1199 * dwSize [I] New size in character rows and cols
1205 BOOL WINAPI
SetConsoleScreenBufferSize(HANDLE hConsoleOutput
, COORD dwSize
)
1209 SERVER_START_REQ(set_console_output_info
)
1211 req
->handle
= hConsoleOutput
;
1212 req
->width
= dwSize
.X
;
1213 req
->height
= dwSize
.Y
;
1214 req
->mask
= SET_CONSOLE_OUTPUT_INFO_SIZE
;
1215 ret
= !wine_server_call_err( req
);
1222 /******************************************************************************
1223 * ScrollConsoleScreenBufferA [KERNEL32.@]
1226 BOOL WINAPI
ScrollConsoleScreenBufferA(HANDLE hConsoleOutput
, LPSMALL_RECT lpScrollRect
,
1227 LPSMALL_RECT lpClipRect
, COORD dwDestOrigin
,
1232 ciw
.Attributes
= lpFill
->Attributes
;
1233 MultiByteToWideChar(CP_ACP
, 0, &lpFill
->Char
.AsciiChar
, 1, &ciw
.Char
.UnicodeChar
, 1);
1235 return ScrollConsoleScreenBufferW(hConsoleOutput
, lpScrollRect
, lpClipRect
,
1236 dwDestOrigin
, &ciw
);
1239 /******************************************************************
1240 * CONSOLE_FillLineUniform
1242 * Helper function for ScrollConsoleScreenBufferW
1243 * Fills a part of a line with a constant character info
1245 void CONSOLE_FillLineUniform(HANDLE hConsoleOutput
, int i
, int j
, int len
, LPCHAR_INFO lpFill
)
1247 SERVER_START_REQ( fill_console_output
)
1249 req
->handle
= hConsoleOutput
;
1250 req
->mode
= CHAR_INFO_MODE_TEXTATTR
;
1255 req
->data
.ch
= lpFill
->Char
.UnicodeChar
;
1256 req
->data
.attr
= lpFill
->Attributes
;
1257 wine_server_call_err( req
);
1262 /******************************************************************************
1263 * ScrollConsoleScreenBufferW [KERNEL32.@]
1267 BOOL WINAPI
ScrollConsoleScreenBufferW(HANDLE hConsoleOutput
, LPSMALL_RECT lpScrollRect
,
1268 LPSMALL_RECT lpClipRect
, COORD dwDestOrigin
,
1276 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1280 TRACE("(%d,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", hConsoleOutput
,
1281 lpScrollRect
->Left
, lpScrollRect
->Top
,
1282 lpScrollRect
->Right
, lpScrollRect
->Bottom
,
1283 lpClipRect
->Left
, lpClipRect
->Top
,
1284 lpClipRect
->Right
, lpClipRect
->Bottom
,
1285 dwDestOrigin
.X
, dwDestOrigin
.Y
, lpFill
);
1287 TRACE("(%d,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", hConsoleOutput
,
1288 lpScrollRect
->Left
, lpScrollRect
->Top
,
1289 lpScrollRect
->Right
, lpScrollRect
->Bottom
,
1290 dwDestOrigin
.X
, dwDestOrigin
.Y
, lpFill
);
1292 if (!GetConsoleScreenBufferInfo(hConsoleOutput
, &csbi
))
1295 /* step 1: get dst rect */
1296 dst
.Left
= dwDestOrigin
.X
;
1297 dst
.Top
= dwDestOrigin
.Y
;
1298 dst
.Right
= dst
.Left
+ (lpScrollRect
->Right
- lpScrollRect
->Left
);
1299 dst
.Bottom
= dst
.Top
+ (lpScrollRect
->Bottom
- lpScrollRect
->Top
);
1301 /* step 2a: compute the final clip rect (optional passed clip and screen buffer limits */
1304 clip
.Left
= max(0, lpClipRect
->Left
);
1305 clip
.Right
= min(csbi
.dwSize
.X
- 1, lpClipRect
->Right
);
1306 clip
.Top
= max(0, lpClipRect
->Top
);
1307 clip
.Bottom
= min(csbi
.dwSize
.Y
- 1, lpClipRect
->Bottom
);
1312 clip
.Right
= csbi
.dwSize
.X
- 1;
1314 clip
.Bottom
= csbi
.dwSize
.Y
- 1;
1316 if (clip
.Left
> clip
.Right
|| clip
.Top
> clip
.Bottom
) return FALSE
;
1318 /* step 2b: clip dst rect */
1319 if (dst
.Left
< clip
.Left
) dst
.Left
= clip
.Left
;
1320 if (dst
.Top
< clip
.Top
) dst
.Top
= clip
.Top
;
1321 if (dst
.Right
> clip
.Right
) dst
.Right
= clip
.Right
;
1322 if (dst
.Bottom
> clip
.Bottom
) dst
.Bottom
= clip
.Bottom
;
1324 /* step 3: transfer the bits */
1325 SERVER_START_REQ(move_console_output
)
1327 req
->handle
= hConsoleOutput
;
1328 req
->x_src
= lpScrollRect
->Left
;
1329 req
->y_src
= lpScrollRect
->Top
;
1330 req
->x_dst
= dst
.Left
;
1331 req
->y_dst
= dst
.Top
;
1332 req
->w
= dst
.Right
- dst
.Left
+ 1;
1333 req
->h
= dst
.Bottom
- dst
.Top
+ 1;
1334 ret
= !wine_server_call_err( req
);
1338 if (!ret
) return FALSE
;
1340 /* step 4: clean out the exposed part */
1342 /* have to write cell [i,j] if it is not in dst rect (because it has already
1343 * been written to by the scroll) and is in clip (we shall not write
1346 for (j
= max(lpScrollRect
->Top
, clip
.Top
); j
<= min(lpScrollRect
->Bottom
, clip
.Bottom
); j
++)
1348 inside
= dst
.Top
<= j
&& j
<= dst
.Bottom
;
1350 for (i
= max(lpScrollRect
->Left
, clip
.Left
); i
<= min(lpScrollRect
->Right
, clip
.Right
); i
++)
1352 if (inside
&& dst
.Left
<= i
&& i
<= dst
.Right
)
1356 CONSOLE_FillLineUniform(hConsoleOutput
, start
, j
, i
- start
, lpFill
);
1362 if (start
== -1) start
= i
;
1366 CONSOLE_FillLineUniform(hConsoleOutput
, start
, j
, i
- start
, lpFill
);
1373 /* ====================================================================
1375 * Console manipulation functions
1377 * ====================================================================*/
1379 /* some missing functions...
1380 * FIXME: those are likely to be defined as undocumented function in kernel32 (or part of them)
1381 * should get the right API and implement them
1382 * GetConsoleCommandHistory[AW] (dword dword dword)
1383 * GetConsoleCommandHistoryLength[AW]
1384 * SetConsoleCommandHistoryMode
1385 * SetConsoleNumberOfCommands[AW]
1387 int CONSOLE_GetHistory(int idx
, WCHAR
* buf
, int buf_len
)
1391 SERVER_START_REQ( get_console_input_history
)
1395 if (buf
&& buf_len
> 1)
1397 wine_server_set_reply( req
, buf
, (buf_len
- 1) * sizeof(WCHAR
) );
1399 if (!wine_server_call_err( req
))
1401 if (buf
) buf
[wine_server_reply_size(reply
) / sizeof(WCHAR
)] = 0;
1402 len
= reply
->total
/ sizeof(WCHAR
) + 1;
1409 /******************************************************************
1410 * CONSOLE_AppendHistory
1414 BOOL
CONSOLE_AppendHistory(const WCHAR
* ptr
)
1416 size_t len
= strlenW(ptr
);
1419 while (len
&& (ptr
[len
- 1] == '\n' || ptr
[len
- 1] == '\r')) len
--;
1421 SERVER_START_REQ( append_console_input_history
)
1424 wine_server_add_data( req
, ptr
, len
* sizeof(WCHAR
) );
1425 ret
= !wine_server_call_err( req
);
1431 /******************************************************************
1432 * CONSOLE_GetNumHistoryEntries
1436 unsigned CONSOLE_GetNumHistoryEntries(void)
1439 SERVER_START_REQ(get_console_input_info
)
1442 if (!wine_server_call_err( req
)) ret
= reply
->history_index
;