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
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"
43 #include "wine/server.h"
44 #include "wine/exception.h"
45 #include "wine/debug.h"
47 #include "msvcrt/excpt.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(console
);
52 extern WCHAR
* CONSOLE_Readline(HANDLE
, int);
54 static WCHAR
* S_EditString
/* = NULL */;
55 static unsigned S_EditStrPos
/* = 0 */;
57 /***********************************************************************
58 * FreeConsole (KERNEL32.@)
60 BOOL WINAPI
FreeConsole(VOID
)
64 SERVER_START_REQ(free_console
)
66 ret
= !wine_server_call_err( req
);
72 /******************************************************************
73 * start_console_renderer
75 * helper for AllocConsole
76 * starts the renderer process
78 static BOOL
start_console_renderer(void)
83 PROCESS_INFORMATION pi
;
86 OBJECT_ATTRIBUTES attr
;
88 attr
.Length
= sizeof(attr
);
89 attr
.RootDirectory
= 0;
90 attr
.Attributes
= OBJ_INHERIT
;
91 attr
.ObjectName
= NULL
;
92 attr
.SecurityDescriptor
= NULL
;
93 attr
.SecurityQualityOfService
= NULL
;
95 NtCreateEvent(&hEvent
, EVENT_ALL_ACCESS
, &attr
, TRUE
, FALSE
);
96 if (!hEvent
) return FALSE
;
98 memset(&si
, 0, sizeof(si
));
101 /* FIXME: use dynamic allocation for most of the buffers below */
102 /* first try environment variable */
103 if ((p
= getenv("WINECONSOLE")) != NULL
)
105 ret
= snprintf(buffer
, sizeof(buffer
), "%s --use-event=%d", p
, hEvent
);
106 if ((ret
> -1) && (ret
< sizeof(buffer
)) &&
107 CreateProcessA(NULL
, buffer
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
109 ERR("Couldn't launch Wine console from WINECONSOLE env var... trying default access\n");
112 /* then the regular installation dir */
113 ret
= snprintf(buffer
, sizeof(buffer
), "%s --use-event=%d", BINDIR
"/wineconsole", hEvent
);
114 if ((ret
> -1) && (ret
< sizeof(buffer
)) &&
115 CreateProcessA(NULL
, buffer
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
118 /* then try the dir where we were started from */
119 if ((path
= HeapAlloc(GetProcessHeap(), 0, strlen(full_argv0
) + sizeof(buffer
))))
123 if ((p
= strrchr(strcpy( path
, full_argv0
), '/')))
126 sprintf(p
, "wineconsole --use-event=%d", hEvent
);
127 if (CreateProcessA(NULL
, path
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
129 sprintf(p
, "programs/wineconsole/wineconsole --use-event=%d", hEvent
);
130 if (CreateProcessA(NULL
, path
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
134 n
= readlink(full_argv0
, buffer
, sizeof(buffer
));
135 if (n
!= -1 && n
< sizeof(buffer
))
138 if (buffer
[0] == '/') /* absolute path ? */
139 strcpy(path
, buffer
);
140 else if ((p
= strrchr(strcpy( path
, full_argv0
), '/')))
142 strcpy(p
+ 1, buffer
);
146 if ((p
= strrchr(path
, '/')))
149 sprintf(p
, "wineconsole --use-event=%d", hEvent
);
150 if (CreateProcessA(NULL
, path
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
152 sprintf(p
, "programs/wineconsole/wineconsole --use-event=%d", hEvent
);
153 if (CreateProcessA(NULL
, path
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
156 } else perror("readlink");
158 HeapFree(GetProcessHeap(), 0, path
); path
= NULL
;
161 /* then try the regular PATH */
162 sprintf(buffer
, "wineconsole --use-event=%d\n", hEvent
);
163 if (CreateProcessA(NULL
, buffer
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
169 if (path
) HeapFree(GetProcessHeap(), 0, path
);
170 if (WaitForSingleObject(hEvent
, INFINITE
) != WAIT_OBJECT_0
) goto the_end
;
173 TRACE("Started wineconsole pid=%08lx tid=%08lx\n", pi
.dwProcessId
, pi
.dwThreadId
);
178 ERR("Can't allocate console\n");
179 if (path
) HeapFree(GetProcessHeap(), 0, path
);
184 /***********************************************************************
185 * AllocConsole (KERNEL32.@)
187 * creates an xterm with a pty to our program
189 BOOL WINAPI
AllocConsole(void)
191 HANDLE handle_in
= INVALID_HANDLE_VALUE
;
192 HANDLE handle_out
= INVALID_HANDLE_VALUE
;
193 HANDLE handle_err
= INVALID_HANDLE_VALUE
;
198 handle_in
= CreateFileA( "CONIN$", GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,
199 0, NULL
, OPEN_EXISTING
, 0, 0 );
201 if (handle_in
!= INVALID_HANDLE_VALUE
)
203 /* we already have a console opened on this process, don't create a new one */
204 CloseHandle(handle_in
);
208 if (!start_console_renderer())
211 handle_in
= CreateFileA( "CONIN$", GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,
212 0, NULL
, OPEN_EXISTING
, 0, 0 );
213 if (handle_in
== INVALID_HANDLE_VALUE
) goto the_end
;
215 handle_out
= CreateFileA( "CONOUT$", GENERIC_READ
|GENERIC_WRITE
,
216 0, NULL
, OPEN_EXISTING
, 0, 0 );
217 if (handle_out
== INVALID_HANDLE_VALUE
) goto the_end
;
219 if (!DuplicateHandle(GetCurrentProcess(), handle_out
, GetCurrentProcess(), &handle_err
,
220 0, TRUE
, DUPLICATE_SAME_ACCESS
))
223 /* NT resets the STD_*_HANDLEs on console alloc */
224 SetStdHandle(STD_INPUT_HANDLE
, handle_in
);
225 SetStdHandle(STD_OUTPUT_HANDLE
, handle_out
);
226 SetStdHandle(STD_ERROR_HANDLE
, handle_err
);
228 GetStartupInfoW(&si
);
229 if (si
.dwFlags
& STARTF_USECOUNTCHARS
)
232 c
.X
= si
.dwXCountChars
;
233 c
.Y
= si
.dwYCountChars
;
234 SetConsoleScreenBufferSize(handle_out
, c
);
236 if (si
.dwFlags
& STARTF_USEFILLATTRIBUTE
)
237 SetConsoleTextAttribute(handle_out
, si
.dwFillAttribute
);
239 SetConsoleTitleW(si
.lpTitle
);
241 SetLastError(ERROR_SUCCESS
);
246 ERR("Can't allocate console\n");
247 if (handle_in
!= INVALID_HANDLE_VALUE
) CloseHandle(handle_in
);
248 if (handle_out
!= INVALID_HANDLE_VALUE
) CloseHandle(handle_out
);
249 if (handle_err
!= INVALID_HANDLE_VALUE
) CloseHandle(handle_err
);
255 /******************************************************************************
258 * Helper function for ReadConsole, ReadConsoleInput and PeekConsoleInput
260 static BOOL
read_console_input(HANDLE handle
, LPINPUT_RECORD buffer
, DWORD count
,
261 LPDWORD pRead
, BOOL flush
)
267 SERVER_START_REQ( read_console_input
)
269 req
->handle
= handle
;
271 wine_server_set_reply( req
, buffer
, count
* sizeof(INPUT_RECORD
) );
272 if ((ret
= !wine_server_call_err( req
))) read
= reply
->read
;
275 if (count
&& flush
&& GetConsoleMode(handle
, &mode
) && (mode
& ENABLE_PROCESSED_INPUT
))
279 for (i
= 0; i
< read
; i
++)
281 if (buffer
[i
].EventType
== KEY_EVENT
&& buffer
[i
].Event
.KeyEvent
.bKeyDown
&&
282 buffer
[i
].Event
.KeyEvent
.uChar
.UnicodeChar
== 'C' - 64 &&
283 !(buffer
[i
].Event
.KeyEvent
.dwControlKeyState
& ENHANCED_KEY
))
285 GenerateConsoleCtrlEvent(CTRL_C_EVENT
, GetCurrentProcessId());
286 /* FIXME: this is hackish, but it easily disables IR handling afterwards */
287 buffer
[i
].Event
.KeyEvent
.uChar
.UnicodeChar
= 0;
291 if (pRead
) *pRead
= read
;
296 /***********************************************************************
297 * ReadConsoleA (KERNEL32.@)
299 BOOL WINAPI
ReadConsoleA(HANDLE hConsoleInput
, LPVOID lpBuffer
, DWORD nNumberOfCharsToRead
,
300 LPDWORD lpNumberOfCharsRead
, LPVOID lpReserved
)
302 LPWSTR ptr
= HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead
* sizeof(WCHAR
));
306 if ((ret
= ReadConsoleW(hConsoleInput
, ptr
, nNumberOfCharsToRead
, &ncr
, 0)))
307 ncr
= WideCharToMultiByte(CP_ACP
, 0, ptr
, ncr
, lpBuffer
, nNumberOfCharsToRead
, NULL
, NULL
);
309 if (lpNumberOfCharsRead
) *lpNumberOfCharsRead
= ncr
;
310 HeapFree(GetProcessHeap(), 0, ptr
);
315 /***********************************************************************
316 * ReadConsoleW (KERNEL32.@)
318 BOOL WINAPI
ReadConsoleW(HANDLE hConsoleInput
, LPVOID lpBuffer
,
319 DWORD nNumberOfCharsToRead
, LPDWORD lpNumberOfCharsRead
, LPVOID lpReserved
)
322 LPWSTR xbuf
= (LPWSTR
)lpBuffer
;
325 TRACE("(%d,%p,%ld,%p,%p)\n",
326 hConsoleInput
, lpBuffer
, nNumberOfCharsToRead
, lpNumberOfCharsRead
, lpReserved
);
328 if (!GetConsoleMode(hConsoleInput
, &mode
))
331 if (mode
& ENABLE_LINE_INPUT
)
333 if (!S_EditString
|| S_EditString
[S_EditStrPos
] == 0)
335 if (S_EditString
) HeapFree(GetProcessHeap(), 0, S_EditString
);
336 if (!(S_EditString
= CONSOLE_Readline(hConsoleInput
, mode
& WINE_ENABLE_LINE_INPUT_EMACS
)))
340 charsread
= lstrlenW(&S_EditString
[S_EditStrPos
]);
341 if (charsread
> nNumberOfCharsToRead
) charsread
= nNumberOfCharsToRead
;
342 memcpy(xbuf
, &S_EditString
[S_EditStrPos
], charsread
* sizeof(WCHAR
));
343 S_EditStrPos
+= charsread
;
350 /* FIXME: should we read at least 1 char? The SDK does not say */
351 /* wait for at least one available input record (it doesn't mean we'll have
352 * chars stored in xbuf...
354 WaitForSingleObject(hConsoleInput
, INFINITE
);
355 for (charsread
= 0; charsread
< nNumberOfCharsToRead
;)
357 if (!read_console_input(hConsoleInput
, &ir
, 1, &count
, TRUE
)) return FALSE
;
358 if (count
&& ir
.EventType
== KEY_EVENT
&& ir
.Event
.KeyEvent
.bKeyDown
&&
359 ir
.Event
.KeyEvent
.uChar
.UnicodeChar
&&
360 !(ir
.Event
.KeyEvent
.dwControlKeyState
& ENHANCED_KEY
))
362 xbuf
[charsread
++] = ir
.Event
.KeyEvent
.uChar
.UnicodeChar
;
367 if (lpNumberOfCharsRead
) *lpNumberOfCharsRead
= charsread
;
373 /***********************************************************************
374 * ReadConsoleInputW (KERNEL32.@)
376 BOOL WINAPI
ReadConsoleInputW(HANDLE hConsoleInput
, LPINPUT_RECORD lpBuffer
,
377 DWORD nLength
, LPDWORD lpNumberOfEventsRead
)
383 if (lpNumberOfEventsRead
) *lpNumberOfEventsRead
= 0;
387 /* loop until we get at least one event */
390 WaitForSingleObject(hConsoleInput
, INFINITE
);
391 if (!read_console_input(hConsoleInput
, lpBuffer
, nLength
, &count
, TRUE
))
395 if (lpNumberOfEventsRead
) *lpNumberOfEventsRead
= count
;
402 /******************************************************************************
403 * WriteConsoleOutputCharacterW [KERNEL32.@] Copies character to consecutive
404 * cells in the console screen buffer
407 * hConsoleOutput [I] Handle to screen buffer
408 * str [I] Pointer to buffer with chars to write
409 * length [I] Number of cells to write to
410 * coord [I] Coords of first cell
411 * lpNumCharsWritten [O] Pointer to number of cells written
418 BOOL WINAPI
WriteConsoleOutputCharacterW( HANDLE hConsoleOutput
, LPCWSTR str
, DWORD length
,
419 COORD coord
, LPDWORD lpNumCharsWritten
)
423 TRACE("(%d,%s,%ld,%dx%d,%p)\n", hConsoleOutput
,
424 debugstr_wn(str
, length
), length
, coord
.X
, coord
.Y
, lpNumCharsWritten
);
426 SERVER_START_REQ( write_console_output
)
428 req
->handle
= hConsoleOutput
;
431 req
->mode
= CHAR_INFO_MODE_TEXT
;
433 wine_server_add_data( req
, str
, length
* sizeof(WCHAR
) );
434 if ((ret
= !wine_server_call_err( req
)))
436 if (lpNumCharsWritten
) *lpNumCharsWritten
= reply
->written
;
444 /******************************************************************************
445 * SetConsoleTitleW [KERNEL32.@] Sets title bar string for console
448 * title [I] Address of new title
454 BOOL WINAPI
SetConsoleTitleW(LPCWSTR title
)
458 SERVER_START_REQ( set_console_input_info
)
461 req
->mask
= SET_CONSOLE_INPUT_INFO_TITLE
;
462 wine_server_add_data( req
, title
, strlenW(title
) * sizeof(WCHAR
) );
463 ret
= !wine_server_call_err( req
);
470 /***********************************************************************
471 * GetNumberOfConsoleMouseButtons (KERNEL32.@)
473 BOOL WINAPI
GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons
)
475 FIXME("(%p): stub\n", nrofbuttons
);
480 /******************************************************************************
481 * SetConsoleInputExeNameW [KERNEL32.@]
486 BOOL WINAPI
SetConsoleInputExeNameW(LPCWSTR name
)
488 FIXME("(%s): stub!\n", debugstr_w(name
));
490 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
494 /******************************************************************************
495 * SetConsoleInputExeNameA [KERNEL32.@]
500 BOOL WINAPI
SetConsoleInputExeNameA(LPCSTR name
)
502 int len
= MultiByteToWideChar(CP_ACP
, 0, name
, -1, NULL
, 0);
503 LPWSTR xptr
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
506 if (!xptr
) return FALSE
;
508 MultiByteToWideChar(CP_ACP
, 0, name
, -1, xptr
, len
);
509 ret
= SetConsoleInputExeNameW(xptr
);
510 HeapFree(GetProcessHeap(), 0, xptr
);
515 static BOOL WINAPI
CONSOLE_DefaultHandler(DWORD dwCtrlType
)
517 FIXME("Terminating process %lx on event %lx\n", GetCurrentProcessId(), dwCtrlType
);
519 /* should never go here */
523 /******************************************************************************
524 * SetConsoleCtrlHandler [KERNEL32.@] Adds function to calling process list
527 * func [I] Address of handler function
528 * add [I] Handler to add or remove
535 * James Sutherland (JamesSutherland@gmx.de)
536 * Added global variables console_ignore_ctrl_c and handlers[]
537 * Does not yet do any error checking, or set LastError if failed.
538 * This doesn't yet matter, since these handlers are not yet called...!
541 static unsigned int console_ignore_ctrl_c
= 0; /* FIXME: this should be inherited somehow */
542 static PHANDLER_ROUTINE handlers
[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,CONSOLE_DefaultHandler
};
544 /*****************************************************************************/
546 BOOL WINAPI
SetConsoleCtrlHandler(PHANDLER_ROUTINE func
, BOOL add
)
548 int alloc_loop
= sizeof(handlers
)/sizeof(handlers
[0]) - 1;
550 FIXME("(%p,%i) - no error checking or testing yet\n", func
, add
);
554 console_ignore_ctrl_c
= add
;
559 for (; alloc_loop
>= 0 && handlers
[alloc_loop
]; alloc_loop
--);
562 FIXME("Out of space on CtrlHandler table\n");
565 handlers
[alloc_loop
] = func
;
569 for (; alloc_loop
>= 0 && handlers
[alloc_loop
] != func
; alloc_loop
--);
572 WARN("Attempt to remove non-installed CtrlHandler %p\n", func
);
576 if (alloc_loop
== sizeof(handlers
)/sizeof(handlers
[0]) - 1)
578 ERR("Who's trying to remove default handler???\n");
582 memmove(&handlers
[1], &handlers
[0], alloc_loop
* sizeof(handlers
[0]));
588 static WINE_EXCEPTION_FILTER(CONSOLE_CtrlEventHandler
)
590 TRACE("(%lx)\n", GetExceptionCode());
591 return EXCEPTION_EXECUTE_HANDLER
;
594 /******************************************************************************
595 * GenerateConsoleCtrlEvent [KERNEL32.@] Simulate a CTRL-C or CTRL-BREAK
598 * dwCtrlEvent [I] Type of event
599 * dwProcessGroupID [I] Process group ID to send event to
602 * Doesn't yet work...!
606 * Failure: False (and *should* [but doesn't] set LastError)
608 BOOL WINAPI
GenerateConsoleCtrlEvent(DWORD dwCtrlEvent
,
609 DWORD dwProcessGroupID
)
611 if (dwCtrlEvent
!= CTRL_C_EVENT
&& dwCtrlEvent
!= CTRL_BREAK_EVENT
)
613 ERR("invalid event %ld for PGID %ld\n", dwCtrlEvent
, dwProcessGroupID
);
617 if (dwProcessGroupID
== GetCurrentProcessId() || dwProcessGroupID
== 0)
621 FIXME("Attempt to send event %ld to self groupID, doing locally only\n", dwCtrlEvent
);
623 /* this is only meaningfull when done locally, otherwise it will have to be done on
624 * the 'receive' side of the event generation
626 if (dwCtrlEvent
== CTRL_C_EVENT
&& console_ignore_ctrl_c
)
629 /* try to pass the exception to the debugger
630 * if it continues, there's nothing more to do
631 * otherwise, we need to send the ctrl-event to the handlers
635 RaiseException( (dwCtrlEvent
== CTRL_C_EVENT
) ? DBG_CONTROL_C
: DBG_CONTROL_BREAK
,
638 __EXCEPT(CONSOLE_CtrlEventHandler
)
640 /* the debugger didn't continue... so, pass to ctrl handlers */
641 for (i
= 0; i
< sizeof(handlers
)/sizeof(handlers
[0]); i
++)
643 if (handlers
[i
] && (handlers
[i
])(dwCtrlEvent
)) break;
649 FIXME("event %ld to external PGID %ld - not implemented yet\n", dwCtrlEvent
, dwProcessGroupID
);
654 /******************************************************************************
655 * CreateConsoleScreenBuffer [KERNEL32.@] Creates a console screen buffer
658 * dwDesiredAccess [I] Access flag
659 * dwShareMode [I] Buffer share mode
660 * sa [I] Security attributes
661 * dwFlags [I] Type of buffer to create
662 * lpScreenBufferData [I] Reserved
665 * Should call SetLastError
668 * Success: Handle to new console screen buffer
669 * Failure: INVALID_HANDLE_VALUE
671 HANDLE WINAPI
CreateConsoleScreenBuffer(DWORD dwDesiredAccess
, DWORD dwShareMode
,
672 LPSECURITY_ATTRIBUTES sa
, DWORD dwFlags
,
673 LPVOID lpScreenBufferData
)
675 HANDLE ret
= INVALID_HANDLE_VALUE
;
677 TRACE("(%ld,%ld,%p,%ld,%p)\n",
678 dwDesiredAccess
, dwShareMode
, sa
, dwFlags
, lpScreenBufferData
);
680 if (dwFlags
!= CONSOLE_TEXTMODE_BUFFER
|| lpScreenBufferData
!= NULL
)
682 SetLastError(ERROR_INVALID_PARAMETER
);
683 return INVALID_HANDLE_VALUE
;
686 SERVER_START_REQ(create_console_output
)
689 req
->access
= dwDesiredAccess
;
690 req
->share
= dwShareMode
;
691 req
->inherit
= (sa
&& sa
->bInheritHandle
);
692 if (!wine_server_call_err( req
)) ret
= reply
->handle_out
;
700 /***********************************************************************
701 * GetConsoleScreenBufferInfo (KERNEL32.@)
703 BOOL WINAPI
GetConsoleScreenBufferInfo(HANDLE hConsoleOutput
, LPCONSOLE_SCREEN_BUFFER_INFO csbi
)
707 SERVER_START_REQ(get_console_output_info
)
709 req
->handle
= hConsoleOutput
;
710 if ((ret
= !wine_server_call_err( req
)))
712 csbi
->dwSize
.X
= reply
->width
;
713 csbi
->dwSize
.Y
= reply
->height
;
714 csbi
->dwCursorPosition
.X
= reply
->cursor_x
;
715 csbi
->dwCursorPosition
.Y
= reply
->cursor_y
;
716 csbi
->wAttributes
= reply
->attr
;
717 csbi
->srWindow
.Left
= reply
->win_left
;
718 csbi
->srWindow
.Right
= reply
->win_right
;
719 csbi
->srWindow
.Top
= reply
->win_top
;
720 csbi
->srWindow
.Bottom
= reply
->win_bottom
;
721 csbi
->dwMaximumWindowSize
.X
= reply
->max_width
;
722 csbi
->dwMaximumWindowSize
.Y
= reply
->max_height
;
731 /******************************************************************************
732 * SetConsoleActiveScreenBuffer [KERNEL32.@] Sets buffer to current console
738 BOOL WINAPI
SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput
)
742 TRACE("(%x)\n", hConsoleOutput
);
744 SERVER_START_REQ( set_console_input_info
)
747 req
->mask
= SET_CONSOLE_INPUT_INFO_ACTIVE_SB
;
748 req
->active_sb
= hConsoleOutput
;
749 ret
= !wine_server_call_err( req
);
756 /***********************************************************************
757 * GetConsoleMode (KERNEL32.@)
759 BOOL WINAPI
GetConsoleMode(HANDLE hcon
, LPDWORD mode
)
763 SERVER_START_REQ(get_console_mode
)
766 ret
= !wine_server_call_err( req
);
767 if (ret
&& mode
) *mode
= reply
->mode
;
774 /******************************************************************************
775 * SetConsoleMode [KERNEL32.@] Sets input mode of console's input buffer
778 * hcon [I] Handle to console input or screen buffer
779 * mode [I] Input or output mode to set
785 BOOL WINAPI
SetConsoleMode(HANDLE hcon
, DWORD mode
)
789 TRACE("(%x,%lx)\n", hcon
, mode
);
791 SERVER_START_REQ(set_console_mode
)
795 ret
= !wine_server_call_err( req
);
798 /* FIXME: when resetting a console input to editline mode, I think we should
799 * empty the S_EditString buffer
805 /******************************************************************
808 * WriteConsoleOutput helper: hides server call semantics
810 static int write_char(HANDLE hCon
, LPCWSTR lpBuffer
, int nc
, COORD
* pos
)
816 SERVER_START_REQ( write_console_output
)
821 req
->mode
= CHAR_INFO_MODE_TEXTSTDATTR
;
823 wine_server_add_data( req
, lpBuffer
, nc
* sizeof(WCHAR
) );
824 if (!wine_server_call_err( req
)) written
= reply
->written
;
828 if (written
> 0) pos
->X
+= written
;
832 /******************************************************************
835 * WriteConsoleOutput helper: handles passing to next line (+scrolling if necessary)
838 static int next_line(HANDLE hCon
, CONSOLE_SCREEN_BUFFER_INFO
* csbi
)
844 csbi
->dwCursorPosition
.X
= 0;
845 csbi
->dwCursorPosition
.Y
++;
847 if (csbi
->dwCursorPosition
.Y
< csbi
->dwSize
.Y
) return 1;
850 src
.Bottom
= csbi
->dwSize
.Y
- 1;
852 src
.Right
= csbi
->dwSize
.X
- 1;
857 ci
.Attributes
= csbi
->wAttributes
;
858 ci
.Char
.UnicodeChar
= ' ';
860 csbi
->dwCursorPosition
.Y
--;
861 if (!ScrollConsoleScreenBufferW(hCon
, &src
, NULL
, dst
, &ci
))
866 /******************************************************************
869 * WriteConsoleOutput helper: writes a block of non special characters
870 * Block can spread on several lines, and wrapping, if needed, is
874 static int write_block(HANDLE hCon
, CONSOLE_SCREEN_BUFFER_INFO
* csbi
,
875 DWORD mode
, LPWSTR ptr
, int len
)
877 int blk
; /* number of chars to write on current line */
879 if (len
<= 0) return 1;
881 if (mode
& ENABLE_WRAP_AT_EOL_OUTPUT
) /* writes remaining on next line */
885 for (done
= 0; done
< len
; done
+= blk
)
887 blk
= min(len
- done
, csbi
->dwSize
.X
- csbi
->dwCursorPosition
.X
);
889 if (write_char(hCon
, ptr
+ done
, blk
, &csbi
->dwCursorPosition
) != blk
)
891 if (csbi
->dwCursorPosition
.X
== csbi
->dwSize
.X
&& !next_line(hCon
, csbi
))
897 blk
= min(len
, csbi
->dwSize
.X
- csbi
->dwCursorPosition
.X
);
899 if (write_char(hCon
, ptr
, blk
, &csbi
->dwCursorPosition
) != blk
)
903 csbi
->dwCursorPosition
.X
= csbi
->dwSize
.X
- 1;
904 /* all remaining chars should be written on last column,
905 * so only overwrite the last column with last char in block
907 if (write_char(hCon
, ptr
+ len
- 1, 1, &csbi
->dwCursorPosition
) != 1)
909 csbi
->dwCursorPosition
.X
= csbi
->dwSize
.X
- 1;
916 /***********************************************************************
917 * WriteConsoleW (KERNEL32.@)
919 BOOL WINAPI
WriteConsoleW(HANDLE hConsoleOutput
, LPCVOID lpBuffer
, DWORD nNumberOfCharsToWrite
,
920 LPDWORD lpNumberOfCharsWritten
, LPVOID lpReserved
)
924 WCHAR
* psz
= (WCHAR
*)lpBuffer
;
925 CONSOLE_SCREEN_BUFFER_INFO csbi
;
928 TRACE("%d %s %ld %p %p\n",
929 hConsoleOutput
, debugstr_wn(lpBuffer
, nNumberOfCharsToWrite
),
930 nNumberOfCharsToWrite
, lpNumberOfCharsWritten
, lpReserved
);
932 if (lpNumberOfCharsWritten
) *lpNumberOfCharsWritten
= 0;
934 if (!GetConsoleMode(hConsoleOutput
, &mode
) ||
935 !GetConsoleScreenBufferInfo(hConsoleOutput
, &csbi
))
938 if (mode
& ENABLE_PROCESSED_OUTPUT
)
942 for (i
= 0; i
< nNumberOfCharsToWrite
; i
++)
946 case '\b': case '\t': case '\n': case '\a': case '\r':
947 /* don't handle here the i-th char... done below */
948 if ((k
= i
- first
) > 0)
950 if (!write_block(hConsoleOutput
, &csbi
, mode
, &psz
[first
], k
))
960 if (csbi
.dwCursorPosition
.X
> 0) csbi
.dwCursorPosition
.X
--;
964 WCHAR tmp
[8] = {' ',' ',' ',' ',' ',' ',' ',' '};
966 if (!write_block(hConsoleOutput
, &csbi
, mode
, tmp
,
967 ((csbi
.dwCursorPosition
.X
+ 8) & ~7) - csbi
.dwCursorPosition
.X
))
972 next_line(hConsoleOutput
, &csbi
);
978 csbi
.dwCursorPosition
.X
= 0;
986 /* write the remaining block (if any) if processed output is enabled, or the
987 * entire buffer otherwise
989 if ((k
= nNumberOfCharsToWrite
- first
) > 0)
991 if (!write_block(hConsoleOutput
, &csbi
, mode
, &psz
[first
], k
))
997 SetConsoleCursorPosition(hConsoleOutput
, csbi
.dwCursorPosition
);
998 if (lpNumberOfCharsWritten
) *lpNumberOfCharsWritten
= nw
;
1003 /***********************************************************************
1004 * WriteConsoleA (KERNEL32.@)
1006 BOOL WINAPI
WriteConsoleA(HANDLE hConsoleOutput
, LPCVOID lpBuffer
, DWORD nNumberOfCharsToWrite
,
1007 LPDWORD lpNumberOfCharsWritten
, LPVOID lpReserved
)
1013 n
= MultiByteToWideChar(CP_ACP
, 0, lpBuffer
, nNumberOfCharsToWrite
, NULL
, 0);
1015 if (lpNumberOfCharsWritten
) *lpNumberOfCharsWritten
= 0;
1016 xstring
= HeapAlloc(GetProcessHeap(), 0, n
* sizeof(WCHAR
));
1017 if (!xstring
) return 0;
1019 MultiByteToWideChar(CP_ACP
, 0, lpBuffer
, nNumberOfCharsToWrite
, xstring
, n
);
1021 ret
= WriteConsoleW(hConsoleOutput
, xstring
, n
, lpNumberOfCharsWritten
, 0);
1023 HeapFree(GetProcessHeap(), 0, xstring
);
1028 /******************************************************************************
1029 * SetConsoleCursorPosition [KERNEL32.@]
1030 * Sets the cursor position in console
1033 * hConsoleOutput [I] Handle of console screen buffer
1034 * dwCursorPosition [I] New cursor position coordinates
1038 BOOL WINAPI
SetConsoleCursorPosition(HANDLE hcon
, COORD pos
)
1041 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1045 TRACE("%x %d %d\n", hcon
, pos
.X
, pos
.Y
);
1047 SERVER_START_REQ(set_console_output_info
)
1050 req
->cursor_x
= pos
.X
;
1051 req
->cursor_y
= pos
.Y
;
1052 req
->mask
= SET_CONSOLE_OUTPUT_INFO_CURSOR_POS
;
1053 ret
= !wine_server_call_err( req
);
1057 if (!ret
|| !GetConsoleScreenBufferInfo(hcon
, &csbi
))
1060 /* if cursor is no longer visible, scroll the visible window... */
1061 w
= csbi
.srWindow
.Right
- csbi
.srWindow
.Left
+ 1;
1062 h
= csbi
.srWindow
.Bottom
- csbi
.srWindow
.Top
+ 1;
1063 if (pos
.X
< csbi
.srWindow
.Left
)
1065 csbi
.srWindow
.Left
= min(pos
.X
, csbi
.dwSize
.X
- w
);
1068 else if (pos
.X
> csbi
.srWindow
.Right
)
1070 csbi
.srWindow
.Left
= max(pos
.X
, w
) - w
+ 1;
1073 csbi
.srWindow
.Right
= csbi
.srWindow
.Left
+ w
- 1;
1075 if (pos
.Y
< csbi
.srWindow
.Top
)
1077 csbi
.srWindow
.Top
= min(pos
.Y
, csbi
.dwSize
.Y
- h
);
1080 else if (pos
.Y
> csbi
.srWindow
.Bottom
)
1082 csbi
.srWindow
.Top
= max(pos
.Y
, h
) - h
+ 1;
1085 csbi
.srWindow
.Bottom
= csbi
.srWindow
.Top
+ h
- 1;
1087 ret
= (do_move
) ? SetConsoleWindowInfo(hcon
, TRUE
, &csbi
.srWindow
) : TRUE
;
1092 /******************************************************************************
1093 * GetConsoleCursorInfo [KERNEL32.@] Gets size and visibility of console
1096 * hcon [I] Handle to console screen buffer
1097 * cinfo [O] Address of cursor information
1103 BOOL WINAPI
GetConsoleCursorInfo(HANDLE hcon
, LPCONSOLE_CURSOR_INFO cinfo
)
1107 SERVER_START_REQ(get_console_output_info
)
1110 ret
= !wine_server_call_err( req
);
1113 cinfo
->dwSize
= reply
->cursor_size
;
1114 cinfo
->bVisible
= reply
->cursor_visible
;
1122 /******************************************************************************
1123 * SetConsoleCursorInfo [KERNEL32.@] Sets size and visibility of cursor
1126 * hcon [I] Handle to console screen buffer
1127 * cinfo [I] Address of cursor information
1132 BOOL WINAPI
SetConsoleCursorInfo(HANDLE hCon
, LPCONSOLE_CURSOR_INFO cinfo
)
1136 SERVER_START_REQ(set_console_output_info
)
1139 req
->cursor_size
= cinfo
->dwSize
;
1140 req
->cursor_visible
= cinfo
->bVisible
;
1141 req
->mask
= SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM
;
1142 ret
= !wine_server_call_err( req
);
1149 /******************************************************************************
1150 * SetConsoleWindowInfo [KERNEL32.@] Sets size and position of console
1153 * hcon [I] Handle to console screen buffer
1154 * bAbsolute [I] Coordinate type flag
1155 * window [I] Address of new window rectangle
1160 BOOL WINAPI
SetConsoleWindowInfo(HANDLE hCon
, BOOL bAbsolute
, LPSMALL_RECT window
)
1162 SMALL_RECT p
= *window
;
1167 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1168 if (!GetConsoleScreenBufferInfo(hCon
, &csbi
))
1170 p
.Left
+= csbi
.srWindow
.Left
;
1171 p
.Top
+= csbi
.srWindow
.Top
;
1172 p
.Right
+= csbi
.srWindow
.Left
;
1173 p
.Bottom
+= csbi
.srWindow
.Top
;
1175 SERVER_START_REQ(set_console_output_info
)
1178 req
->win_left
= p
.Left
;
1179 req
->win_top
= p
.Top
;
1180 req
->win_right
= p
.Right
;
1181 req
->win_bottom
= p
.Bottom
;
1182 req
->mask
= SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW
;
1183 ret
= !wine_server_call_err( req
);
1191 /******************************************************************************
1192 * SetConsoleTextAttribute [KERNEL32.@] Sets colors for text
1194 * Sets the foreground and background color attributes of characters
1195 * written to the screen buffer.
1201 BOOL WINAPI
SetConsoleTextAttribute(HANDLE hConsoleOutput
, WORD wAttr
)
1205 SERVER_START_REQ(set_console_output_info
)
1207 req
->handle
= hConsoleOutput
;
1209 req
->mask
= SET_CONSOLE_OUTPUT_INFO_ATTR
;
1210 ret
= !wine_server_call_err( req
);
1217 /******************************************************************************
1218 * SetConsoleScreenBufferSize [KERNEL32.@] Changes size of console
1221 * hConsoleOutput [I] Handle to console screen buffer
1222 * dwSize [I] New size in character rows and cols
1228 BOOL WINAPI
SetConsoleScreenBufferSize(HANDLE hConsoleOutput
, COORD dwSize
)
1232 SERVER_START_REQ(set_console_output_info
)
1234 req
->handle
= hConsoleOutput
;
1235 req
->width
= dwSize
.X
;
1236 req
->height
= dwSize
.Y
;
1237 req
->mask
= SET_CONSOLE_OUTPUT_INFO_SIZE
;
1238 ret
= !wine_server_call_err( req
);
1245 /******************************************************************************
1246 * ScrollConsoleScreenBufferA [KERNEL32.@]
1249 BOOL WINAPI
ScrollConsoleScreenBufferA(HANDLE hConsoleOutput
, LPSMALL_RECT lpScrollRect
,
1250 LPSMALL_RECT lpClipRect
, COORD dwDestOrigin
,
1255 ciw
.Attributes
= lpFill
->Attributes
;
1256 MultiByteToWideChar(CP_ACP
, 0, &lpFill
->Char
.AsciiChar
, 1, &ciw
.Char
.UnicodeChar
, 1);
1258 return ScrollConsoleScreenBufferW(hConsoleOutput
, lpScrollRect
, lpClipRect
,
1259 dwDestOrigin
, &ciw
);
1262 /******************************************************************
1265 * Helper function for ScrollConsoleScreenBufferW
1266 * Fills a part of a line with a constant character info
1268 static void fill_line_uniform(HANDLE hConsoleOutput
, int i
, int j
, int len
, LPCHAR_INFO lpFill
)
1270 SERVER_START_REQ( fill_console_output
)
1272 req
->handle
= hConsoleOutput
;
1273 req
->mode
= CHAR_INFO_MODE_TEXTATTR
;
1278 req
->data
.ch
= lpFill
->Char
.UnicodeChar
;
1279 req
->data
.attr
= lpFill
->Attributes
;
1280 wine_server_call_err( req
);
1285 /******************************************************************************
1286 * ScrollConsoleScreenBufferW [KERNEL32.@]
1290 BOOL WINAPI
ScrollConsoleScreenBufferW(HANDLE hConsoleOutput
, LPSMALL_RECT lpScrollRect
,
1291 LPSMALL_RECT lpClipRect
, COORD dwDestOrigin
,
1299 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1303 TRACE("(%d,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", hConsoleOutput
,
1304 lpScrollRect
->Left
, lpScrollRect
->Top
,
1305 lpScrollRect
->Right
, lpScrollRect
->Bottom
,
1306 lpClipRect
->Left
, lpClipRect
->Top
,
1307 lpClipRect
->Right
, lpClipRect
->Bottom
,
1308 dwDestOrigin
.X
, dwDestOrigin
.Y
, lpFill
);
1310 TRACE("(%d,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", hConsoleOutput
,
1311 lpScrollRect
->Left
, lpScrollRect
->Top
,
1312 lpScrollRect
->Right
, lpScrollRect
->Bottom
,
1313 dwDestOrigin
.X
, dwDestOrigin
.Y
, lpFill
);
1315 if (!GetConsoleScreenBufferInfo(hConsoleOutput
, &csbi
))
1318 /* step 1: get dst rect */
1319 dst
.Left
= dwDestOrigin
.X
;
1320 dst
.Top
= dwDestOrigin
.Y
;
1321 dst
.Right
= dst
.Left
+ (lpScrollRect
->Right
- lpScrollRect
->Left
);
1322 dst
.Bottom
= dst
.Top
+ (lpScrollRect
->Bottom
- lpScrollRect
->Top
);
1324 /* step 2a: compute the final clip rect (optional passed clip and screen buffer limits */
1327 clip
.Left
= max(0, lpClipRect
->Left
);
1328 clip
.Right
= min(csbi
.dwSize
.X
- 1, lpClipRect
->Right
);
1329 clip
.Top
= max(0, lpClipRect
->Top
);
1330 clip
.Bottom
= min(csbi
.dwSize
.Y
- 1, lpClipRect
->Bottom
);
1335 clip
.Right
= csbi
.dwSize
.X
- 1;
1337 clip
.Bottom
= csbi
.dwSize
.Y
- 1;
1339 if (clip
.Left
> clip
.Right
|| clip
.Top
> clip
.Bottom
) return FALSE
;
1341 /* step 2b: clip dst rect */
1342 if (dst
.Left
< clip
.Left
) dst
.Left
= clip
.Left
;
1343 if (dst
.Top
< clip
.Top
) dst
.Top
= clip
.Top
;
1344 if (dst
.Right
> clip
.Right
) dst
.Right
= clip
.Right
;
1345 if (dst
.Bottom
> clip
.Bottom
) dst
.Bottom
= clip
.Bottom
;
1347 /* step 3: transfer the bits */
1348 SERVER_START_REQ(move_console_output
)
1350 req
->handle
= hConsoleOutput
;
1351 req
->x_src
= lpScrollRect
->Left
;
1352 req
->y_src
= lpScrollRect
->Top
;
1353 req
->x_dst
= dst
.Left
;
1354 req
->y_dst
= dst
.Top
;
1355 req
->w
= dst
.Right
- dst
.Left
+ 1;
1356 req
->h
= dst
.Bottom
- dst
.Top
+ 1;
1357 ret
= !wine_server_call_err( req
);
1361 if (!ret
) return FALSE
;
1363 /* step 4: clean out the exposed part */
1365 /* have to write celll [i,j] if it is not in dst rect (because it has already
1366 * been written to by the scroll) and is in clip (we shall not write
1369 for (j
= max(lpScrollRect
->Top
, clip
.Top
); j
<= min(lpScrollRect
->Bottom
, clip
.Bottom
); j
++)
1371 inside
= dst
.Top
<= j
&& j
<= dst
.Bottom
;
1373 for (i
= max(lpScrollRect
->Left
, clip
.Left
); i
<= min(lpScrollRect
->Right
, clip
.Right
); i
++)
1375 if (inside
&& dst
.Left
<= i
&& i
<= dst
.Right
)
1379 fill_line_uniform(hConsoleOutput
, start
, j
, i
- start
, lpFill
);
1385 if (start
== -1) start
= i
;
1389 fill_line_uniform(hConsoleOutput
, start
, j
, i
- start
, lpFill
);
1396 /* ====================================================================
1398 * Console manipulation functions
1400 * ====================================================================*/
1401 /* some missing functions...
1402 * FIXME: those are likely to be defined as undocumented function in kernel32 (or part of them)
1403 * should get the right API and implement them
1404 * GetConsoleCommandHistory[AW] (dword dword dword)
1405 * GetConsoleCommandHistoryLength[AW]
1406 * SetConsoleCommandHistoryMode
1407 * SetConsoleNumberOfCommands[AW]
1409 int CONSOLE_GetHistory(int idx
, WCHAR
* buf
, int buf_len
)
1413 SERVER_START_REQ( get_console_input_history
)
1417 if (buf
&& buf_len
> 1)
1419 wine_server_set_reply( req
, buf
, (buf_len
- 1) * sizeof(WCHAR
) );
1421 if (!wine_server_call_err( req
))
1423 if (buf
) buf
[wine_server_reply_size(reply
) / sizeof(WCHAR
)] = 0;
1424 len
= reply
->total
/ sizeof(WCHAR
) + 1;
1431 /******************************************************************
1432 * CONSOLE_AppendHistory
1436 BOOL
CONSOLE_AppendHistory(const WCHAR
* ptr
)
1438 size_t len
= strlenW(ptr
);
1441 while (len
&& (ptr
[len
- 1] == '\n' || ptr
[len
- 1] == '\r')) len
--;
1443 SERVER_START_REQ( append_console_input_history
)
1446 wine_server_add_data( req
, ptr
, len
* sizeof(WCHAR
) );
1447 ret
= !wine_server_call_err( req
);
1453 /******************************************************************
1454 * CONSOLE_GetNumHistoryEntries
1458 unsigned CONSOLE_GetNumHistoryEntries(void)
1461 SERVER_START_REQ(get_console_input_info
)
1464 if (!wine_server_call_err( req
)) ret
= reply
->history_index
;