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 Eric Pouech
11 /* Reference applications:
12 * - IDA (interactive disassembler) full version 3.75. Works.
13 * - LYNX/W32. Works mostly, some keys crash it.
28 #include "wine/server.h"
29 #include "wine/exception.h"
30 #include "debugtools.h"
33 DEFAULT_DEBUG_CHANNEL(console
);
36 extern WCHAR
* CONSOLE_Readline(HANDLE
, int);
38 static WCHAR
* S_EditString
/* = NULL */;
39 static unsigned S_EditStrPos
/* = 0 */;
41 /***********************************************************************
42 * FreeConsole (KERNEL32.@)
44 BOOL WINAPI
FreeConsole(VOID
)
48 SERVER_START_REQ(free_console
)
50 ret
= !wine_server_call_err( req
);
56 /******************************************************************
57 * start_console_renderer
59 * helper for AllocConsole
60 * starts the renderer process
62 static BOOL
start_console_renderer(void)
67 PROCESS_INFORMATION pi
;
70 OBJECT_ATTRIBUTES attr
;
72 attr
.Length
= sizeof(attr
);
73 attr
.RootDirectory
= 0;
74 attr
.Attributes
= OBJ_INHERIT
;
75 attr
.ObjectName
= NULL
;
76 attr
.SecurityDescriptor
= NULL
;
77 attr
.SecurityQualityOfService
= NULL
;
79 NtCreateEvent(&hEvent
, EVENT_ALL_ACCESS
, &attr
, TRUE
, FALSE
);
80 if (!hEvent
) return FALSE
;
82 memset(&si
, 0, sizeof(si
));
85 /* FIXME: use dynamic allocation for most of the buffers below */
86 /* first try environment variable */
87 if ((p
= getenv("WINECONSOLE")) != NULL
)
89 ret
= snprintf(buffer
, sizeof(buffer
), "%s -- --use-event=%d", p
, hEvent
);
90 if ((ret
> -1) && (ret
< sizeof(buffer
)) &&
91 CreateProcessA(NULL
, buffer
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
93 ERR("Couldn't launch Wine console from WINECONSOLE env var... trying default access\n");
96 /* then the regular installation dir */
97 ret
= snprintf(buffer
, sizeof(buffer
), "%s -- --use-event=%d", BINDIR
"/wineconsole", hEvent
);
98 if ((ret
> -1) && (ret
< sizeof(buffer
)) &&
99 CreateProcessA(NULL
, buffer
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
102 /* then try the dir where we were started from */
103 if ((path
= HeapAlloc(GetProcessHeap(), 0, strlen(full_argv0
) + sizeof(buffer
))))
107 if ((p
= strrchr(strcpy( path
, full_argv0
), '/')))
110 sprintf(p
, "wineconsole -- --use-event=%d", hEvent
);
111 if (CreateProcessA(NULL
, path
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
113 sprintf(p
, "programs/wineconsole/wineconsole -- --use-event=%d", hEvent
);
114 if (CreateProcessA(NULL
, path
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
118 n
= readlink(full_argv0
, buffer
, sizeof(buffer
));
119 if (n
!= -1 && n
< sizeof(buffer
))
122 if (buffer
[0] == '/') /* absolute path ? */
123 strcpy(path
, buffer
);
124 else if ((p
= strrchr(strcpy( path
, full_argv0
), '/')))
126 strcpy(p
+ 1, buffer
);
130 if ((p
= strrchr(path
, '/')))
133 sprintf(p
, "wineconsole -- --use-event=%d", hEvent
);
134 if (CreateProcessA(NULL
, path
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
136 sprintf(p
, "programs/wineconsole/wineconsole -- --use-event=%d", hEvent
);
137 if (CreateProcessA(NULL
, path
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
140 } else perror("readlink");
142 HeapFree(GetProcessHeap(), 0, path
); path
= NULL
;
145 /* then try the regular PATH */
146 sprintf(buffer
, "wineconsole -- --use-event=%d\n", hEvent
);
147 if (CreateProcessA(NULL
, buffer
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
153 if (path
) HeapFree(GetProcessHeap(), 0, path
);
154 if (WaitForSingleObject(hEvent
, INFINITE
) != WAIT_OBJECT_0
) goto the_end
;
157 TRACE("Started wineconsole pid=%08lx tid=%08lx\n", pi
.dwProcessId
, pi
.dwThreadId
);
162 ERR("Can't allocate console\n");
163 if (path
) HeapFree(GetProcessHeap(), 0, path
);
168 /***********************************************************************
169 * AllocConsole (KERNEL32.@)
171 * creates an xterm with a pty to our program
173 BOOL WINAPI
AllocConsole(void)
175 HANDLE handle_in
= INVALID_HANDLE_VALUE
;
176 HANDLE handle_out
= INVALID_HANDLE_VALUE
;
177 HANDLE handle_err
= INVALID_HANDLE_VALUE
;
182 handle_in
= CreateFileA( "CONIN$", GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,
183 0, NULL
, OPEN_EXISTING
, 0, 0 );
185 if (handle_in
!= INVALID_HANDLE_VALUE
)
187 /* we already have a console opened on this process, don't create a new one */
188 CloseHandle(handle_in
);
192 if (!start_console_renderer())
195 handle_in
= CreateFileA( "CONIN$", GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,
196 0, NULL
, OPEN_EXISTING
, 0, 0 );
197 if (handle_in
== INVALID_HANDLE_VALUE
) goto the_end
;
199 handle_out
= CreateFileA( "CONOUT$", GENERIC_READ
|GENERIC_WRITE
,
200 0, NULL
, OPEN_EXISTING
, 0, 0 );
201 if (handle_out
== INVALID_HANDLE_VALUE
) goto the_end
;
203 if (!DuplicateHandle(GetCurrentProcess(), handle_out
, GetCurrentProcess(), &handle_err
,
204 0, TRUE
, DUPLICATE_SAME_ACCESS
))
207 /* NT resets the STD_*_HANDLEs on console alloc */
208 SetStdHandle(STD_INPUT_HANDLE
, handle_in
);
209 SetStdHandle(STD_OUTPUT_HANDLE
, handle_out
);
210 SetStdHandle(STD_ERROR_HANDLE
, handle_err
);
212 GetStartupInfoW(&si
);
213 if (si
.dwFlags
& STARTF_USESIZE
)
216 c
.X
= si
.dwXCountChars
;
217 c
.Y
= si
.dwYCountChars
;
218 SetConsoleScreenBufferSize(handle_out
, c
);
220 if (si
.dwFlags
& STARTF_USEFILLATTRIBUTE
)
221 SetConsoleTextAttribute(handle_out
, si
.dwFillAttribute
);
223 SetConsoleTitleW(si
.lpTitle
);
225 SetLastError(ERROR_SUCCESS
);
230 ERR("Can't allocate console\n");
231 if (handle_in
!= INVALID_HANDLE_VALUE
) CloseHandle(handle_in
);
232 if (handle_out
!= INVALID_HANDLE_VALUE
) CloseHandle(handle_out
);
233 if (handle_err
!= INVALID_HANDLE_VALUE
) CloseHandle(handle_err
);
239 /******************************************************************************
242 * Helper function for ReadConsole, ReadConsoleInput and PeekConsoleInput
244 static BOOL
read_console_input(HANDLE handle
, LPINPUT_RECORD buffer
, DWORD count
,
245 LPDWORD pRead
, BOOL flush
)
251 SERVER_START_REQ( read_console_input
)
253 req
->handle
= handle
;
255 wine_server_set_reply( req
, buffer
, count
* sizeof(INPUT_RECORD
) );
256 if ((ret
= !wine_server_call_err( req
))) read
= reply
->read
;
259 if (count
&& flush
&& GetConsoleMode(handle
, &mode
) && (mode
& ENABLE_PROCESSED_INPUT
))
263 for (i
= 0; i
< read
; i
++)
265 if (buffer
[i
].EventType
== KEY_EVENT
&& buffer
[i
].Event
.KeyEvent
.bKeyDown
&&
266 buffer
[i
].Event
.KeyEvent
.uChar
.UnicodeChar
== 'C' - 64 &&
267 !(buffer
[i
].Event
.KeyEvent
.dwControlKeyState
& ENHANCED_KEY
))
269 GenerateConsoleCtrlEvent(CTRL_C_EVENT
, GetCurrentProcessId());
270 /* FIXME: this is hackish, but it easily disables IR handling afterwards */
271 buffer
[i
].Event
.KeyEvent
.uChar
.UnicodeChar
= 0;
275 if (pRead
) *pRead
= read
;
280 /***********************************************************************
281 * ReadConsoleA (KERNEL32.@)
283 BOOL WINAPI
ReadConsoleA(HANDLE hConsoleInput
, LPVOID lpBuffer
, DWORD nNumberOfCharsToRead
,
284 LPDWORD lpNumberOfCharsRead
, LPVOID lpReserved
)
286 LPWSTR ptr
= HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead
* sizeof(WCHAR
));
290 if ((ret
= ReadConsoleW(hConsoleInput
, ptr
, nNumberOfCharsToRead
, &ncr
, 0)))
291 ncr
= WideCharToMultiByte(CP_ACP
, 0, ptr
, ncr
, lpBuffer
, nNumberOfCharsToRead
, NULL
, NULL
);
293 if (lpNumberOfCharsRead
) *lpNumberOfCharsRead
= ncr
;
294 HeapFree(GetProcessHeap(), 0, ptr
);
299 /***********************************************************************
300 * ReadConsoleW (KERNEL32.@)
302 BOOL WINAPI
ReadConsoleW(HANDLE hConsoleInput
, LPVOID lpBuffer
,
303 DWORD nNumberOfCharsToRead
, LPDWORD lpNumberOfCharsRead
, LPVOID lpReserved
)
306 LPWSTR xbuf
= (LPWSTR
)lpBuffer
;
309 TRACE("(%d,%p,%ld,%p,%p)\n",
310 hConsoleInput
, lpBuffer
, nNumberOfCharsToRead
, lpNumberOfCharsRead
, lpReserved
);
312 if (!GetConsoleMode(hConsoleInput
, &mode
))
315 if (mode
& ENABLE_LINE_INPUT
)
317 if (!S_EditString
|| S_EditString
[S_EditStrPos
] == 0)
319 if (S_EditString
) HeapFree(GetProcessHeap(), 0, S_EditString
);
320 if (!(S_EditString
= CONSOLE_Readline(hConsoleInput
, mode
& WINE_ENABLE_LINE_INPUT_EMACS
)))
324 charsread
= lstrlenW(&S_EditString
[S_EditStrPos
]);
325 if (charsread
> nNumberOfCharsToRead
) charsread
= nNumberOfCharsToRead
;
326 memcpy(xbuf
, &S_EditString
[S_EditStrPos
], charsread
* sizeof(WCHAR
));
327 S_EditStrPos
+= charsread
;
334 /* FIXME: should we read at least 1 char? The SDK does not say */
335 /* wait for at least one available input record (it doesn't mean we'll have
336 * chars stored in xbuf...
338 WaitForSingleObject(hConsoleInput
, INFINITE
);
339 for (charsread
= 0; charsread
< nNumberOfCharsToRead
;)
341 if (!read_console_input(hConsoleInput
, &ir
, 1, &count
, TRUE
)) return FALSE
;
342 if (count
&& ir
.EventType
== KEY_EVENT
&& ir
.Event
.KeyEvent
.bKeyDown
&&
343 ir
.Event
.KeyEvent
.uChar
.UnicodeChar
&&
344 !(ir
.Event
.KeyEvent
.dwControlKeyState
& ENHANCED_KEY
))
346 xbuf
[charsread
++] = ir
.Event
.KeyEvent
.uChar
.UnicodeChar
;
351 if (lpNumberOfCharsRead
) *lpNumberOfCharsRead
= charsread
;
357 /***********************************************************************
358 * ReadConsoleInputW (KERNEL32.@)
360 BOOL WINAPI
ReadConsoleInputW(HANDLE hConsoleInput
, LPINPUT_RECORD lpBuffer
,
361 DWORD nLength
, LPDWORD lpNumberOfEventsRead
)
367 if (lpNumberOfEventsRead
) *lpNumberOfEventsRead
= 0;
371 /* loop until we get at least one event */
374 WaitForSingleObject(hConsoleInput
, INFINITE
);
375 if (!read_console_input(hConsoleInput
, lpBuffer
, nLength
, &count
, TRUE
))
379 if (lpNumberOfEventsRead
) *lpNumberOfEventsRead
= count
;
386 /******************************************************************************
387 * WriteConsoleOutputCharacterW [KERNEL32.@] Copies character to consecutive
388 * cells in the console screen buffer
391 * hConsoleOutput [I] Handle to screen buffer
392 * str [I] Pointer to buffer with chars to write
393 * length [I] Number of cells to write to
394 * coord [I] Coords of first cell
395 * lpNumCharsWritten [O] Pointer to number of cells written
402 BOOL WINAPI
WriteConsoleOutputCharacterW( HANDLE hConsoleOutput
, LPCWSTR str
, DWORD length
,
403 COORD coord
, LPDWORD lpNumCharsWritten
)
407 TRACE("(%d,%s,%ld,%dx%d,%p)\n", hConsoleOutput
,
408 debugstr_wn(str
, length
), length
, coord
.X
, coord
.Y
, lpNumCharsWritten
);
410 SERVER_START_REQ( write_console_output
)
412 req
->handle
= hConsoleOutput
;
415 req
->mode
= CHAR_INFO_MODE_TEXT
;
417 wine_server_add_data( req
, str
, length
* sizeof(WCHAR
) );
418 if ((ret
= !wine_server_call_err( req
)))
420 if (lpNumCharsWritten
) *lpNumCharsWritten
= reply
->written
;
428 /******************************************************************************
429 * SetConsoleTitleW [KERNEL32.@] Sets title bar string for console
432 * title [I] Address of new title
438 BOOL WINAPI
SetConsoleTitleW(LPCWSTR title
)
442 SERVER_START_REQ( set_console_input_info
)
445 req
->mask
= SET_CONSOLE_INPUT_INFO_TITLE
;
446 wine_server_add_data( req
, title
, strlenW(title
) * sizeof(WCHAR
) );
447 ret
= !wine_server_call_err( req
);
454 /***********************************************************************
455 * GetNumberOfConsoleMouseButtons (KERNEL32.@)
457 BOOL WINAPI
GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons
)
459 FIXME("(%p): stub\n", nrofbuttons
);
464 /******************************************************************************
465 * SetConsoleInputExeNameW [KERNEL32.@]
470 BOOL WINAPI
SetConsoleInputExeNameW(LPCWSTR name
)
472 FIXME("(%s): stub!\n", debugstr_w(name
));
474 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
478 /******************************************************************************
479 * SetConsoleInputExeNameA [KERNEL32.@]
484 BOOL WINAPI
SetConsoleInputExeNameA(LPCSTR name
)
486 int len
= MultiByteToWideChar(CP_ACP
, 0, name
, -1, NULL
, 0);
487 LPWSTR xptr
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
490 if (!xptr
) return FALSE
;
492 MultiByteToWideChar(CP_ACP
, 0, name
, -1, xptr
, len
);
493 ret
= SetConsoleInputExeNameW(xptr
);
494 HeapFree(GetProcessHeap(), 0, xptr
);
499 static BOOL WINAPI
CONSOLE_DefaultHandler(DWORD dwCtrlType
)
501 FIXME("Terminating process %lx on event %lx\n", GetCurrentProcessId(), dwCtrlType
);
503 /* should never go here */
507 /******************************************************************************
508 * SetConsoleCtrlHandler [KERNEL32.@] Adds function to calling process list
511 * func [I] Address of handler function
512 * add [I] Handler to add or remove
519 * James Sutherland (JamesSutherland@gmx.de)
520 * Added global variables console_ignore_ctrl_c and handlers[]
521 * Does not yet do any error checking, or set LastError if failed.
522 * This doesn't yet matter, since these handlers are not yet called...!
524 static unsigned int console_ignore_ctrl_c
= 0; /* FIXME: this should be inherited somehow */
525 static PHANDLER_ROUTINE handlers
[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,CONSOLE_DefaultHandler
};
527 BOOL WINAPI
SetConsoleCtrlHandler(PHANDLER_ROUTINE func
, BOOL add
)
529 int alloc_loop
= sizeof(handlers
)/sizeof(handlers
[0]) - 1;
531 FIXME("(%p,%i) - no error checking or testing yet\n", func
, add
);
535 console_ignore_ctrl_c
= add
;
540 for (; alloc_loop
>= 0 && handlers
[alloc_loop
]; alloc_loop
--);
543 FIXME("Out of space on CtrlHandler table\n");
546 handlers
[alloc_loop
] = func
;
550 for (; alloc_loop
>= 0 && handlers
[alloc_loop
] != func
; alloc_loop
--);
553 WARN("Attempt to remove non-installed CtrlHandler %p\n", func
);
557 if (alloc_loop
== sizeof(handlers
)/sizeof(handlers
[0]) - 1)
559 ERR("Who's trying to remove default handler???\n");
563 memmove(&handlers
[1], &handlers
[0], alloc_loop
* sizeof(handlers
[0]));
569 static WINE_EXCEPTION_FILTER(CONSOLE_CtrlEventHandler
)
571 TRACE("(%lx)\n", GetExceptionCode());
572 return EXCEPTION_EXECUTE_HANDLER
;
575 /******************************************************************************
576 * GenerateConsoleCtrlEvent [KERNEL32.@] Simulate a CTRL-C or CTRL-BREAK
579 * dwCtrlEvent [I] Type of event
580 * dwProcessGroupID [I] Process group ID to send event to
583 * Doesn't yet work...!
587 * Failure: False (and *should* [but doesn't] set LastError)
589 BOOL WINAPI
GenerateConsoleCtrlEvent(DWORD dwCtrlEvent
,
590 DWORD dwProcessGroupID
)
592 if (dwCtrlEvent
!= CTRL_C_EVENT
&& dwCtrlEvent
!= CTRL_BREAK_EVENT
)
594 ERR("invalid event %ld for PGID %ld\n", dwCtrlEvent
, dwProcessGroupID
);
598 if (dwProcessGroupID
== GetCurrentProcessId() || dwProcessGroupID
== 0)
602 FIXME("Attempt to send event %ld to self groupID, doing locally only\n", dwCtrlEvent
);
604 /* this is only meaningfull when done locally, otherwise it will have to be done on
605 * the 'receive' side of the event generation
607 if (dwCtrlEvent
== CTRL_C_EVENT
&& console_ignore_ctrl_c
)
610 /* try to pass the exception to the debugger
611 * if it continues, there's nothing more to do
612 * otherwise, we need to send the ctrl-event to the handlers
616 RaiseException( (dwCtrlEvent
== CTRL_C_EVENT
) ? DBG_CONTROL_C
: DBG_CONTROL_BREAK
,
619 __EXCEPT(CONSOLE_CtrlEventHandler
)
621 /* the debugger didn't continue... so, pass to ctrl handlers */
622 for (i
= 0; i
< sizeof(handlers
)/sizeof(handlers
[0]); i
++)
624 if (handlers
[i
] && (handlers
[i
])(dwCtrlEvent
)) break;
630 FIXME("event %ld to external PGID %ld - not implemented yet\n", dwCtrlEvent
, dwProcessGroupID
);
635 /******************************************************************************
636 * CreateConsoleScreenBuffer [KERNEL32.@] Creates a console screen buffer
639 * dwDesiredAccess [I] Access flag
640 * dwShareMode [I] Buffer share mode
641 * sa [I] Security attributes
642 * dwFlags [I] Type of buffer to create
643 * lpScreenBufferData [I] Reserved
646 * Should call SetLastError
649 * Success: Handle to new console screen buffer
650 * Failure: INVALID_HANDLE_VALUE
652 HANDLE WINAPI
CreateConsoleScreenBuffer(DWORD dwDesiredAccess
, DWORD dwShareMode
,
653 LPSECURITY_ATTRIBUTES sa
, DWORD dwFlags
,
654 LPVOID lpScreenBufferData
)
656 HANDLE ret
= INVALID_HANDLE_VALUE
;
658 TRACE("(%ld,%ld,%p,%ld,%p)\n",
659 dwDesiredAccess
, dwShareMode
, sa
, dwFlags
, lpScreenBufferData
);
661 if (dwFlags
!= CONSOLE_TEXTMODE_BUFFER
|| lpScreenBufferData
!= NULL
)
663 SetLastError(ERROR_INVALID_PARAMETER
);
664 return INVALID_HANDLE_VALUE
;
667 SERVER_START_REQ(create_console_output
)
670 req
->access
= dwDesiredAccess
;
671 req
->share
= dwShareMode
;
672 req
->inherit
= (sa
&& sa
->bInheritHandle
);
673 if (!wine_server_call_err( req
)) ret
= reply
->handle_out
;
681 /***********************************************************************
682 * GetConsoleScreenBufferInfo (KERNEL32.@)
684 BOOL WINAPI
GetConsoleScreenBufferInfo(HANDLE hConsoleOutput
, LPCONSOLE_SCREEN_BUFFER_INFO csbi
)
688 SERVER_START_REQ(get_console_output_info
)
690 req
->handle
= hConsoleOutput
;
691 if ((ret
= !wine_server_call_err( req
)))
693 csbi
->dwSize
.X
= reply
->width
;
694 csbi
->dwSize
.Y
= reply
->height
;
695 csbi
->dwCursorPosition
.X
= reply
->cursor_x
;
696 csbi
->dwCursorPosition
.Y
= reply
->cursor_y
;
697 csbi
->wAttributes
= reply
->attr
;
698 csbi
->srWindow
.Left
= reply
->win_left
;
699 csbi
->srWindow
.Right
= reply
->win_right
;
700 csbi
->srWindow
.Top
= reply
->win_top
;
701 csbi
->srWindow
.Bottom
= reply
->win_bottom
;
702 csbi
->dwMaximumWindowSize
.X
= reply
->max_width
;
703 csbi
->dwMaximumWindowSize
.Y
= reply
->max_height
;
712 /******************************************************************************
713 * SetConsoleActiveScreenBuffer [KERNEL32.@] Sets buffer to current console
719 BOOL WINAPI
SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput
)
723 TRACE("(%x)\n", hConsoleOutput
);
725 SERVER_START_REQ( set_console_input_info
)
728 req
->mask
= SET_CONSOLE_INPUT_INFO_ACTIVE_SB
;
729 req
->active_sb
= hConsoleOutput
;
730 ret
= !wine_server_call_err( req
);
737 /***********************************************************************
738 * GetConsoleMode (KERNEL32.@)
740 BOOL WINAPI
GetConsoleMode(HANDLE hcon
, LPDWORD mode
)
744 SERVER_START_REQ(get_console_mode
)
747 ret
= !wine_server_call_err( req
);
748 if (ret
&& mode
) *mode
= reply
->mode
;
755 /******************************************************************************
756 * SetConsoleMode [KERNEL32.@] Sets input mode of console's input buffer
759 * hcon [I] Handle to console input or screen buffer
760 * mode [I] Input or output mode to set
766 BOOL WINAPI
SetConsoleMode(HANDLE hcon
, DWORD mode
)
770 TRACE("(%x,%lx)\n", hcon
, mode
);
772 SERVER_START_REQ(set_console_mode
)
776 ret
= !wine_server_call_err( req
);
779 /* FIXME: when resetting a console input to editline mode, I think we should
780 * empty the S_EditString buffer
786 /******************************************************************
789 * WriteConsoleOutput helper: hides server call semantics
791 static int write_char(HANDLE hCon
, LPCWSTR lpBuffer
, int nc
, COORD
* pos
)
797 SERVER_START_REQ( write_console_output
)
802 req
->mode
= CHAR_INFO_MODE_TEXTSTDATTR
;
804 wine_server_add_data( req
, lpBuffer
, nc
* sizeof(WCHAR
) );
805 if (!wine_server_call_err( req
)) written
= reply
->written
;
809 if (written
> 0) pos
->X
+= written
;
813 /******************************************************************
816 * WriteConsoleOutput helper: handles passing to next line (+scrolling if necessary)
819 static int next_line(HANDLE hCon
, CONSOLE_SCREEN_BUFFER_INFO
* csbi
)
825 csbi
->dwCursorPosition
.X
= 0;
826 csbi
->dwCursorPosition
.Y
++;
828 if (csbi
->dwCursorPosition
.Y
< csbi
->dwSize
.Y
) return 1;
831 src
.Bottom
= csbi
->dwSize
.Y
- 1;
833 src
.Right
= csbi
->dwSize
.X
- 1;
838 ci
.Attributes
= csbi
->wAttributes
;
839 ci
.Char
.UnicodeChar
= ' ';
841 csbi
->dwCursorPosition
.Y
--;
842 if (!ScrollConsoleScreenBufferW(hCon
, &src
, NULL
, dst
, &ci
))
847 /******************************************************************
850 * WriteConsoleOutput helper: writes a block of non special characters
853 static int write_block(HANDLE hCon
, CONSOLE_SCREEN_BUFFER_INFO
* csbi
,
854 DWORD mode
, LPWSTR ptr
, int len
)
856 int blk
; /* number of chars to write on first line */
858 if (len
<= 0) return 1;
860 blk
= min(len
, csbi
->dwSize
.X
- csbi
->dwCursorPosition
.X
);
862 if (write_char(hCon
, ptr
, blk
, &csbi
->dwCursorPosition
) != blk
)
865 if (blk
< len
) /* special handling for right border */
867 if (mode
& ENABLE_WRAP_AT_EOL_OUTPUT
) /* writes remaining on next line */
869 if (!next_line(hCon
, csbi
) ||
870 write_char(hCon
, ptr
+ blk
, len
- blk
, &csbi
->dwCursorPosition
) != len
- blk
)
873 else /* all remaining chars should be written on last column, so only write the last one */
875 csbi
->dwCursorPosition
.X
= csbi
->dwSize
.X
- 1;
876 if (write_char(hCon
, ptr
+ len
- 1, 1, &csbi
->dwCursorPosition
) != 1)
878 csbi
->dwCursorPosition
.X
= csbi
->dwSize
.X
- 1;
884 /***********************************************************************
885 * WriteConsoleW (KERNEL32.@)
887 BOOL WINAPI
WriteConsoleW(HANDLE hConsoleOutput
, LPCVOID lpBuffer
, DWORD nNumberOfCharsToWrite
,
888 LPDWORD lpNumberOfCharsWritten
, LPVOID lpReserved
)
892 WCHAR
* psz
= (WCHAR
*)lpBuffer
;
893 CONSOLE_SCREEN_BUFFER_INFO csbi
;
896 TRACE("%d %s %ld %p %p\n",
897 hConsoleOutput
, debugstr_wn(lpBuffer
, nNumberOfCharsToWrite
),
898 nNumberOfCharsToWrite
, lpNumberOfCharsWritten
, lpReserved
);
900 if (lpNumberOfCharsWritten
) *lpNumberOfCharsWritten
= 0;
902 if (!GetConsoleMode(hConsoleOutput
, &mode
) ||
903 !GetConsoleScreenBufferInfo(hConsoleOutput
, &csbi
))
906 if (mode
& ENABLE_PROCESSED_OUTPUT
)
910 for (i
= 0; i
< nNumberOfCharsToWrite
; i
++)
914 case '\b': case '\t': case '\n': case '\a': case '\r':
915 /* don't handle here the i-th char... done below */
916 if ((k
= i
- first
) > 0)
918 if (!write_block(hConsoleOutput
, &csbi
, mode
, &psz
[first
], k
))
928 if (csbi
.dwCursorPosition
.X
> 0) csbi
.dwCursorPosition
.X
--;
932 WCHAR tmp
[8] = {' ',' ',' ',' ',' ',' ',' ',' '};
934 if (!write_block(hConsoleOutput
, &csbi
, mode
, tmp
,
935 ((csbi
.dwCursorPosition
.X
+ 8) & ~7) - csbi
.dwCursorPosition
.X
))
940 next_line(hConsoleOutput
, &csbi
);
946 csbi
.dwCursorPosition
.X
= 0;
954 /* write the remaining block (if any) if processed output is enabled, or the
955 * entire buffer otherwise
957 if ((k
= nNumberOfCharsToWrite
- first
) > 0)
959 if (!write_block(hConsoleOutput
, &csbi
, mode
, &psz
[first
], k
))
965 SetConsoleCursorPosition(hConsoleOutput
, csbi
.dwCursorPosition
);
966 if (lpNumberOfCharsWritten
) *lpNumberOfCharsWritten
= nw
;
971 /***********************************************************************
972 * WriteConsoleA (KERNEL32.@)
974 BOOL WINAPI
WriteConsoleA(HANDLE hConsoleOutput
, LPCVOID lpBuffer
, DWORD nNumberOfCharsToWrite
,
975 LPDWORD lpNumberOfCharsWritten
, LPVOID lpReserved
)
981 n
= MultiByteToWideChar(CP_ACP
, 0, lpBuffer
, nNumberOfCharsToWrite
, NULL
, 0);
983 if (lpNumberOfCharsWritten
) *lpNumberOfCharsWritten
= 0;
984 xstring
= HeapAlloc(GetProcessHeap(), 0, n
* sizeof(WCHAR
));
985 if (!xstring
) return 0;
987 MultiByteToWideChar(CP_ACP
, 0, lpBuffer
, nNumberOfCharsToWrite
, xstring
, n
);
989 ret
= WriteConsoleW(hConsoleOutput
, xstring
, n
, lpNumberOfCharsWritten
, 0);
991 HeapFree(GetProcessHeap(), 0, xstring
);
996 /******************************************************************************
997 * SetConsoleCursorPosition [KERNEL32.@]
998 * Sets the cursor position in console
1001 * hConsoleOutput [I] Handle of console screen buffer
1002 * dwCursorPosition [I] New cursor position coordinates
1006 BOOL WINAPI
SetConsoleCursorPosition(HANDLE hcon
, COORD pos
)
1009 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1013 TRACE("%x %d %d\n", hcon
, pos
.X
, pos
.Y
);
1015 SERVER_START_REQ(set_console_output_info
)
1018 req
->cursor_x
= pos
.X
;
1019 req
->cursor_y
= pos
.Y
;
1020 req
->mask
= SET_CONSOLE_OUTPUT_INFO_CURSOR_POS
;
1021 ret
= !wine_server_call_err( req
);
1025 if (!ret
|| !GetConsoleScreenBufferInfo(hcon
, &csbi
))
1028 /* if cursor is no longer visible, scroll the visible window... */
1029 w
= csbi
.srWindow
.Right
- csbi
.srWindow
.Left
+ 1;
1030 h
= csbi
.srWindow
.Bottom
- csbi
.srWindow
.Top
+ 1;
1031 if (pos
.X
< csbi
.srWindow
.Left
)
1033 csbi
.srWindow
.Left
= min(pos
.X
, csbi
.dwSize
.X
- w
);
1036 else if (pos
.X
> csbi
.srWindow
.Right
)
1038 csbi
.srWindow
.Left
= max(pos
.X
, w
) - w
+ 1;
1041 csbi
.srWindow
.Right
= csbi
.srWindow
.Left
+ w
- 1;
1043 if (pos
.Y
< csbi
.srWindow
.Top
)
1045 csbi
.srWindow
.Top
= min(pos
.Y
, csbi
.dwSize
.Y
- h
);
1048 else if (pos
.Y
> csbi
.srWindow
.Bottom
)
1050 csbi
.srWindow
.Top
= max(pos
.Y
, h
) - h
+ 1;
1053 csbi
.srWindow
.Bottom
= csbi
.srWindow
.Top
+ h
- 1;
1055 ret
= (do_move
) ? SetConsoleWindowInfo(hcon
, TRUE
, &csbi
.srWindow
) : TRUE
;
1060 /******************************************************************************
1061 * GetConsoleCursorInfo [KERNEL32.@] Gets size and visibility of console
1064 * hcon [I] Handle to console screen buffer
1065 * cinfo [O] Address of cursor information
1071 BOOL WINAPI
GetConsoleCursorInfo(HANDLE hcon
, LPCONSOLE_CURSOR_INFO cinfo
)
1075 SERVER_START_REQ(get_console_output_info
)
1078 ret
= !wine_server_call_err( req
);
1081 cinfo
->dwSize
= reply
->cursor_size
;
1082 cinfo
->bVisible
= reply
->cursor_visible
;
1090 /******************************************************************************
1091 * SetConsoleCursorInfo [KERNEL32.@] Sets size and visibility of cursor
1094 * hcon [I] Handle to console screen buffer
1095 * cinfo [I] Address of cursor information
1100 BOOL WINAPI
SetConsoleCursorInfo(HANDLE hCon
, LPCONSOLE_CURSOR_INFO cinfo
)
1104 SERVER_START_REQ(set_console_output_info
)
1107 req
->cursor_size
= cinfo
->dwSize
;
1108 req
->cursor_visible
= cinfo
->bVisible
;
1109 req
->mask
= SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM
;
1110 ret
= !wine_server_call_err( req
);
1117 /******************************************************************************
1118 * SetConsoleWindowInfo [KERNEL32.@] Sets size and position of console
1121 * hcon [I] Handle to console screen buffer
1122 * bAbsolute [I] Coordinate type flag
1123 * window [I] Address of new window rectangle
1128 BOOL WINAPI
SetConsoleWindowInfo(HANDLE hCon
, BOOL bAbsolute
, LPSMALL_RECT window
)
1130 SMALL_RECT p
= *window
;
1135 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1136 if (!GetConsoleScreenBufferInfo(hCon
, &csbi
))
1138 p
.Left
+= csbi
.srWindow
.Left
;
1139 p
.Top
+= csbi
.srWindow
.Top
;
1140 p
.Right
+= csbi
.srWindow
.Left
;
1141 p
.Bottom
+= csbi
.srWindow
.Top
;
1143 SERVER_START_REQ(set_console_output_info
)
1146 req
->win_left
= p
.Left
;
1147 req
->win_top
= p
.Top
;
1148 req
->win_right
= p
.Right
;
1149 req
->win_bottom
= p
.Bottom
;
1150 req
->mask
= SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW
;
1151 ret
= !wine_server_call_err( req
);
1159 /******************************************************************************
1160 * SetConsoleTextAttribute [KERNEL32.@] Sets colors for text
1162 * Sets the foreground and background color attributes of characters
1163 * written to the screen buffer.
1169 BOOL WINAPI
SetConsoleTextAttribute(HANDLE hConsoleOutput
, WORD wAttr
)
1173 SERVER_START_REQ(set_console_output_info
)
1175 req
->handle
= hConsoleOutput
;
1177 req
->mask
= SET_CONSOLE_OUTPUT_INFO_ATTR
;
1178 ret
= !wine_server_call_err( req
);
1185 /******************************************************************************
1186 * SetConsoleScreenBufferSize [KERNEL32.@] Changes size of console
1189 * hConsoleOutput [I] Handle to console screen buffer
1190 * dwSize [I] New size in character rows and cols
1196 BOOL WINAPI
SetConsoleScreenBufferSize(HANDLE hConsoleOutput
, COORD dwSize
)
1200 SERVER_START_REQ(set_console_output_info
)
1202 req
->handle
= hConsoleOutput
;
1203 req
->width
= dwSize
.X
;
1204 req
->height
= dwSize
.Y
;
1205 req
->mask
= SET_CONSOLE_OUTPUT_INFO_SIZE
;
1206 ret
= !wine_server_call_err( req
);
1213 /******************************************************************************
1214 * ScrollConsoleScreenBufferA [KERNEL32.@]
1217 BOOL WINAPI
ScrollConsoleScreenBufferA(HANDLE hConsoleOutput
, LPSMALL_RECT lpScrollRect
,
1218 LPSMALL_RECT lpClipRect
, COORD dwDestOrigin
,
1223 ciw
.Attributes
= lpFill
->Attributes
;
1224 MultiByteToWideChar(CP_ACP
, 0, &lpFill
->Char
.AsciiChar
, 1, &ciw
.Char
.UnicodeChar
, 1);
1226 return ScrollConsoleScreenBufferW(hConsoleOutput
, lpScrollRect
, lpClipRect
,
1227 dwDestOrigin
, &ciw
);
1230 /******************************************************************
1233 * Helper function for ScrollConsoleScreenBufferW
1234 * Fills a part of a line with a constant character info
1236 static void fill_line_uniform(HANDLE hConsoleOutput
, int i
, int j
, int len
, LPCHAR_INFO lpFill
)
1238 SERVER_START_REQ( fill_console_output
)
1240 req
->handle
= hConsoleOutput
;
1241 req
->mode
= CHAR_INFO_MODE_TEXTATTR
;
1246 req
->data
.ch
= lpFill
->Char
.UnicodeChar
;
1247 req
->data
.attr
= lpFill
->Attributes
;
1248 wine_server_call_err( req
);
1253 /******************************************************************************
1254 * ScrollConsoleScreenBufferW [KERNEL32.@]
1258 BOOL WINAPI
ScrollConsoleScreenBufferW(HANDLE hConsoleOutput
, LPSMALL_RECT lpScrollRect
,
1259 LPSMALL_RECT lpClipRect
, COORD dwDestOrigin
,
1267 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1271 TRACE("(%d,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", hConsoleOutput
,
1272 lpScrollRect
->Left
, lpScrollRect
->Top
,
1273 lpScrollRect
->Right
, lpScrollRect
->Bottom
,
1274 lpClipRect
->Left
, lpClipRect
->Top
,
1275 lpClipRect
->Right
, lpClipRect
->Bottom
,
1276 dwDestOrigin
.X
, dwDestOrigin
.Y
, lpFill
);
1278 TRACE("(%d,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", hConsoleOutput
,
1279 lpScrollRect
->Left
, lpScrollRect
->Top
,
1280 lpScrollRect
->Right
, lpScrollRect
->Bottom
,
1281 dwDestOrigin
.X
, dwDestOrigin
.Y
, lpFill
);
1283 if (!GetConsoleScreenBufferInfo(hConsoleOutput
, &csbi
))
1286 /* step 1: get dst rect */
1287 dst
.Left
= dwDestOrigin
.X
;
1288 dst
.Top
= dwDestOrigin
.Y
;
1289 dst
.Right
= dst
.Left
+ (lpScrollRect
->Right
- lpScrollRect
->Left
);
1290 dst
.Bottom
= dst
.Top
+ (lpScrollRect
->Bottom
- lpScrollRect
->Top
);
1292 /* step 2a: compute the final clip rect (optional passed clip and screen buffer limits */
1295 clip
.Left
= max(0, lpClipRect
->Left
);
1296 clip
.Right
= min(csbi
.dwSize
.X
- 1, lpClipRect
->Right
);
1297 clip
.Top
= max(0, lpClipRect
->Top
);
1298 clip
.Bottom
= min(csbi
.dwSize
.Y
- 1, lpClipRect
->Bottom
);
1303 clip
.Right
= csbi
.dwSize
.X
- 1;
1305 clip
.Bottom
= csbi
.dwSize
.Y
- 1;
1307 if (clip
.Left
> clip
.Right
|| clip
.Top
> clip
.Bottom
) return FALSE
;
1309 /* step 2b: clip dst rect */
1310 if (dst
.Left
< clip
.Left
) dst
.Left
= clip
.Left
;
1311 if (dst
.Top
< clip
.Top
) dst
.Top
= clip
.Top
;
1312 if (dst
.Right
> clip
.Right
) dst
.Right
= clip
.Right
;
1313 if (dst
.Bottom
> clip
.Bottom
) dst
.Bottom
= clip
.Bottom
;
1315 /* step 3: transfer the bits */
1316 SERVER_START_REQ(move_console_output
)
1318 req
->handle
= hConsoleOutput
;
1319 req
->x_src
= lpScrollRect
->Left
;
1320 req
->y_src
= lpScrollRect
->Top
;
1321 req
->x_dst
= dst
.Left
;
1322 req
->y_dst
= dst
.Top
;
1323 req
->w
= dst
.Right
- dst
.Left
+ 1;
1324 req
->h
= dst
.Bottom
- dst
.Top
+ 1;
1325 ret
= !wine_server_call_err( req
);
1329 if (!ret
) return FALSE
;
1331 /* step 4: clean out the exposed part */
1333 /* have to write celll [i,j] if it is not in dst rect (because it has already
1334 * been written to by the scroll) and is in clip (we shall not write
1337 for (j
= max(lpScrollRect
->Top
, clip
.Top
); j
<= min(lpScrollRect
->Bottom
, clip
.Bottom
); j
++)
1339 inside
= dst
.Top
<= j
&& j
<= dst
.Bottom
;
1341 for (i
= max(lpScrollRect
->Left
, clip
.Left
); i
<= min(lpScrollRect
->Right
, clip
.Right
); i
++)
1343 if (inside
&& dst
.Left
<= i
&& i
<= dst
.Right
)
1347 fill_line_uniform(hConsoleOutput
, start
, j
, i
- start
, lpFill
);
1353 if (start
== -1) start
= i
;
1357 fill_line_uniform(hConsoleOutput
, start
, j
, i
- start
, lpFill
);
1364 /* ====================================================================
1366 * Console manipulation functions
1368 * ====================================================================*/
1369 /* some missing functions...
1370 * FIXME: those are likely to be defined as undocumented function in kernel32 (or part of them)
1371 * should get the right API and implement them
1372 * GetConsoleCommandHistory[AW] (dword dword dword)
1373 * GetConsoleCommandHistoryLength[AW]
1374 * SetConsoleCommandHistoryMode
1375 * SetConsoleNumberOfCommands[AW]
1377 int CONSOLE_GetHistory(int idx
, WCHAR
* buf
, int buf_len
)
1381 SERVER_START_REQ( get_console_input_history
)
1385 if (buf
&& buf_len
> 1)
1387 wine_server_set_reply( req
, buf
, (buf_len
- 1) * sizeof(WCHAR
) );
1389 if (!wine_server_call_err( req
))
1391 if (buf
) buf
[wine_server_reply_size(reply
) / sizeof(WCHAR
)] = 0;
1392 len
= reply
->total
/ sizeof(WCHAR
) + 1;
1399 /******************************************************************
1400 * CONSOLE_AppendHistory
1404 BOOL
CONSOLE_AppendHistory(const WCHAR
* ptr
)
1406 size_t len
= strlenW(ptr
);
1409 while (len
&& (ptr
[len
- 1] == '\n' || ptr
[len
- 1] == '\r')) len
--;
1411 SERVER_START_REQ( append_console_input_history
)
1414 wine_server_add_data( req
, ptr
, len
* sizeof(WCHAR
) );
1415 ret
= !wine_server_call_err( req
);
1421 /******************************************************************
1422 * CONSOLE_GetNumHistoryEntries
1426 unsigned CONSOLE_GetNumHistoryEntries(void)
1429 SERVER_START_REQ(get_console_input_info
)
1432 if (!wine_server_call_err( req
)) ret
= reply
->history_index
;