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.
42 #include "wine/server.h"
43 #include "wine/exception.h"
44 #include "wine/debug.h"
46 #include "msvcrt/excpt.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(console
);
51 extern WCHAR
* CONSOLE_Readline(HANDLE
, int);
53 static WCHAR
* S_EditString
/* = NULL */;
54 static unsigned S_EditStrPos
/* = 0 */;
56 /***********************************************************************
57 * FreeConsole (KERNEL32.@)
59 BOOL WINAPI
FreeConsole(VOID
)
63 SERVER_START_REQ(free_console
)
65 ret
= !wine_server_call_err( req
);
71 /******************************************************************
72 * start_console_renderer
74 * helper for AllocConsole
75 * starts the renderer process
77 static BOOL
start_console_renderer(void)
82 PROCESS_INFORMATION pi
;
85 OBJECT_ATTRIBUTES attr
;
87 attr
.Length
= sizeof(attr
);
88 attr
.RootDirectory
= 0;
89 attr
.Attributes
= OBJ_INHERIT
;
90 attr
.ObjectName
= NULL
;
91 attr
.SecurityDescriptor
= NULL
;
92 attr
.SecurityQualityOfService
= NULL
;
94 NtCreateEvent(&hEvent
, EVENT_ALL_ACCESS
, &attr
, TRUE
, FALSE
);
95 if (!hEvent
) return FALSE
;
97 memset(&si
, 0, sizeof(si
));
100 /* FIXME: use dynamic allocation for most of the buffers below */
101 /* first try environment variable */
102 if ((p
= getenv("WINECONSOLE")) != NULL
)
104 ret
= snprintf(buffer
, sizeof(buffer
), "%s -- --use-event=%d", p
, hEvent
);
105 if ((ret
> -1) && (ret
< sizeof(buffer
)) &&
106 CreateProcessA(NULL
, buffer
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
108 ERR("Couldn't launch Wine console from WINECONSOLE env var... trying default access\n");
111 /* then the regular installation dir */
112 ret
= snprintf(buffer
, sizeof(buffer
), "%s -- --use-event=%d", BINDIR
"/wineconsole", hEvent
);
113 if ((ret
> -1) && (ret
< sizeof(buffer
)) &&
114 CreateProcessA(NULL
, buffer
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
117 /* then try the dir where we were started from */
118 if ((path
= HeapAlloc(GetProcessHeap(), 0, strlen(full_argv0
) + sizeof(buffer
))))
122 if ((p
= strrchr(strcpy( path
, full_argv0
), '/')))
125 sprintf(p
, "wineconsole -- --use-event=%d", hEvent
);
126 if (CreateProcessA(NULL
, path
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
128 sprintf(p
, "programs/wineconsole/wineconsole -- --use-event=%d", hEvent
);
129 if (CreateProcessA(NULL
, path
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
133 n
= readlink(full_argv0
, buffer
, sizeof(buffer
));
134 if (n
!= -1 && n
< sizeof(buffer
))
137 if (buffer
[0] == '/') /* absolute path ? */
138 strcpy(path
, buffer
);
139 else if ((p
= strrchr(strcpy( path
, full_argv0
), '/')))
141 strcpy(p
+ 1, buffer
);
145 if ((p
= strrchr(path
, '/')))
148 sprintf(p
, "wineconsole -- --use-event=%d", hEvent
);
149 if (CreateProcessA(NULL
, path
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
151 sprintf(p
, "programs/wineconsole/wineconsole -- --use-event=%d", hEvent
);
152 if (CreateProcessA(NULL
, path
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
155 } else perror("readlink");
157 HeapFree(GetProcessHeap(), 0, path
); path
= NULL
;
160 /* then try the regular PATH */
161 sprintf(buffer
, "wineconsole -- --use-event=%d\n", hEvent
);
162 if (CreateProcessA(NULL
, buffer
, NULL
, NULL
, TRUE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
))
168 if (path
) HeapFree(GetProcessHeap(), 0, path
);
169 if (WaitForSingleObject(hEvent
, INFINITE
) != WAIT_OBJECT_0
) goto the_end
;
172 TRACE("Started wineconsole pid=%08lx tid=%08lx\n", pi
.dwProcessId
, pi
.dwThreadId
);
177 ERR("Can't allocate console\n");
178 if (path
) HeapFree(GetProcessHeap(), 0, path
);
183 /***********************************************************************
184 * AllocConsole (KERNEL32.@)
186 * creates an xterm with a pty to our program
188 BOOL WINAPI
AllocConsole(void)
190 HANDLE handle_in
= INVALID_HANDLE_VALUE
;
191 HANDLE handle_out
= INVALID_HANDLE_VALUE
;
192 HANDLE handle_err
= INVALID_HANDLE_VALUE
;
197 handle_in
= CreateFileA( "CONIN$", GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,
198 0, NULL
, OPEN_EXISTING
, 0, 0 );
200 if (handle_in
!= INVALID_HANDLE_VALUE
)
202 /* we already have a console opened on this process, don't create a new one */
203 CloseHandle(handle_in
);
207 if (!start_console_renderer())
210 handle_in
= CreateFileA( "CONIN$", GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,
211 0, NULL
, OPEN_EXISTING
, 0, 0 );
212 if (handle_in
== INVALID_HANDLE_VALUE
) goto the_end
;
214 handle_out
= CreateFileA( "CONOUT$", GENERIC_READ
|GENERIC_WRITE
,
215 0, NULL
, OPEN_EXISTING
, 0, 0 );
216 if (handle_out
== INVALID_HANDLE_VALUE
) goto the_end
;
218 if (!DuplicateHandle(GetCurrentProcess(), handle_out
, GetCurrentProcess(), &handle_err
,
219 0, TRUE
, DUPLICATE_SAME_ACCESS
))
222 /* NT resets the STD_*_HANDLEs on console alloc */
223 SetStdHandle(STD_INPUT_HANDLE
, handle_in
);
224 SetStdHandle(STD_OUTPUT_HANDLE
, handle_out
);
225 SetStdHandle(STD_ERROR_HANDLE
, handle_err
);
227 GetStartupInfoW(&si
);
228 if (si
.dwFlags
& STARTF_USESIZE
)
231 c
.X
= si
.dwXCountChars
;
232 c
.Y
= si
.dwYCountChars
;
233 SetConsoleScreenBufferSize(handle_out
, c
);
235 if (si
.dwFlags
& STARTF_USEFILLATTRIBUTE
)
236 SetConsoleTextAttribute(handle_out
, si
.dwFillAttribute
);
238 SetConsoleTitleW(si
.lpTitle
);
240 SetLastError(ERROR_SUCCESS
);
245 ERR("Can't allocate console\n");
246 if (handle_in
!= INVALID_HANDLE_VALUE
) CloseHandle(handle_in
);
247 if (handle_out
!= INVALID_HANDLE_VALUE
) CloseHandle(handle_out
);
248 if (handle_err
!= INVALID_HANDLE_VALUE
) CloseHandle(handle_err
);
254 /******************************************************************************
257 * Helper function for ReadConsole, ReadConsoleInput and PeekConsoleInput
259 static BOOL
read_console_input(HANDLE handle
, LPINPUT_RECORD buffer
, DWORD count
,
260 LPDWORD pRead
, BOOL flush
)
266 SERVER_START_REQ( read_console_input
)
268 req
->handle
= handle
;
270 wine_server_set_reply( req
, buffer
, count
* sizeof(INPUT_RECORD
) );
271 if ((ret
= !wine_server_call_err( req
))) read
= reply
->read
;
274 if (count
&& flush
&& GetConsoleMode(handle
, &mode
) && (mode
& ENABLE_PROCESSED_INPUT
))
278 for (i
= 0; i
< read
; i
++)
280 if (buffer
[i
].EventType
== KEY_EVENT
&& buffer
[i
].Event
.KeyEvent
.bKeyDown
&&
281 buffer
[i
].Event
.KeyEvent
.uChar
.UnicodeChar
== 'C' - 64 &&
282 !(buffer
[i
].Event
.KeyEvent
.dwControlKeyState
& ENHANCED_KEY
))
284 GenerateConsoleCtrlEvent(CTRL_C_EVENT
, GetCurrentProcessId());
285 /* FIXME: this is hackish, but it easily disables IR handling afterwards */
286 buffer
[i
].Event
.KeyEvent
.uChar
.UnicodeChar
= 0;
290 if (pRead
) *pRead
= read
;
295 /***********************************************************************
296 * ReadConsoleA (KERNEL32.@)
298 BOOL WINAPI
ReadConsoleA(HANDLE hConsoleInput
, LPVOID lpBuffer
, DWORD nNumberOfCharsToRead
,
299 LPDWORD lpNumberOfCharsRead
, LPVOID lpReserved
)
301 LPWSTR ptr
= HeapAlloc(GetProcessHeap(), 0, nNumberOfCharsToRead
* sizeof(WCHAR
));
305 if ((ret
= ReadConsoleW(hConsoleInput
, ptr
, nNumberOfCharsToRead
, &ncr
, 0)))
306 ncr
= WideCharToMultiByte(CP_ACP
, 0, ptr
, ncr
, lpBuffer
, nNumberOfCharsToRead
, NULL
, NULL
);
308 if (lpNumberOfCharsRead
) *lpNumberOfCharsRead
= ncr
;
309 HeapFree(GetProcessHeap(), 0, ptr
);
314 /***********************************************************************
315 * ReadConsoleW (KERNEL32.@)
317 BOOL WINAPI
ReadConsoleW(HANDLE hConsoleInput
, LPVOID lpBuffer
,
318 DWORD nNumberOfCharsToRead
, LPDWORD lpNumberOfCharsRead
, LPVOID lpReserved
)
321 LPWSTR xbuf
= (LPWSTR
)lpBuffer
;
324 TRACE("(%d,%p,%ld,%p,%p)\n",
325 hConsoleInput
, lpBuffer
, nNumberOfCharsToRead
, lpNumberOfCharsRead
, lpReserved
);
327 if (!GetConsoleMode(hConsoleInput
, &mode
))
330 if (mode
& ENABLE_LINE_INPUT
)
332 if (!S_EditString
|| S_EditString
[S_EditStrPos
] == 0)
334 if (S_EditString
) HeapFree(GetProcessHeap(), 0, S_EditString
);
335 if (!(S_EditString
= CONSOLE_Readline(hConsoleInput
, mode
& WINE_ENABLE_LINE_INPUT_EMACS
)))
339 charsread
= lstrlenW(&S_EditString
[S_EditStrPos
]);
340 if (charsread
> nNumberOfCharsToRead
) charsread
= nNumberOfCharsToRead
;
341 memcpy(xbuf
, &S_EditString
[S_EditStrPos
], charsread
* sizeof(WCHAR
));
342 S_EditStrPos
+= charsread
;
349 /* FIXME: should we read at least 1 char? The SDK does not say */
350 /* wait for at least one available input record (it doesn't mean we'll have
351 * chars stored in xbuf...
353 WaitForSingleObject(hConsoleInput
, INFINITE
);
354 for (charsread
= 0; charsread
< nNumberOfCharsToRead
;)
356 if (!read_console_input(hConsoleInput
, &ir
, 1, &count
, TRUE
)) return FALSE
;
357 if (count
&& ir
.EventType
== KEY_EVENT
&& ir
.Event
.KeyEvent
.bKeyDown
&&
358 ir
.Event
.KeyEvent
.uChar
.UnicodeChar
&&
359 !(ir
.Event
.KeyEvent
.dwControlKeyState
& ENHANCED_KEY
))
361 xbuf
[charsread
++] = ir
.Event
.KeyEvent
.uChar
.UnicodeChar
;
366 if (lpNumberOfCharsRead
) *lpNumberOfCharsRead
= charsread
;
372 /***********************************************************************
373 * ReadConsoleInputW (KERNEL32.@)
375 BOOL WINAPI
ReadConsoleInputW(HANDLE hConsoleInput
, LPINPUT_RECORD lpBuffer
,
376 DWORD nLength
, LPDWORD lpNumberOfEventsRead
)
382 if (lpNumberOfEventsRead
) *lpNumberOfEventsRead
= 0;
386 /* loop until we get at least one event */
389 WaitForSingleObject(hConsoleInput
, INFINITE
);
390 if (!read_console_input(hConsoleInput
, lpBuffer
, nLength
, &count
, TRUE
))
394 if (lpNumberOfEventsRead
) *lpNumberOfEventsRead
= count
;
401 /******************************************************************************
402 * WriteConsoleOutputCharacterW [KERNEL32.@] Copies character to consecutive
403 * cells in the console screen buffer
406 * hConsoleOutput [I] Handle to screen buffer
407 * str [I] Pointer to buffer with chars to write
408 * length [I] Number of cells to write to
409 * coord [I] Coords of first cell
410 * lpNumCharsWritten [O] Pointer to number of cells written
417 BOOL WINAPI
WriteConsoleOutputCharacterW( HANDLE hConsoleOutput
, LPCWSTR str
, DWORD length
,
418 COORD coord
, LPDWORD lpNumCharsWritten
)
422 TRACE("(%d,%s,%ld,%dx%d,%p)\n", hConsoleOutput
,
423 debugstr_wn(str
, length
), length
, coord
.X
, coord
.Y
, lpNumCharsWritten
);
425 SERVER_START_REQ( write_console_output
)
427 req
->handle
= hConsoleOutput
;
430 req
->mode
= CHAR_INFO_MODE_TEXT
;
432 wine_server_add_data( req
, str
, length
* sizeof(WCHAR
) );
433 if ((ret
= !wine_server_call_err( req
)))
435 if (lpNumCharsWritten
) *lpNumCharsWritten
= reply
->written
;
443 /******************************************************************************
444 * SetConsoleTitleW [KERNEL32.@] Sets title bar string for console
447 * title [I] Address of new title
453 BOOL WINAPI
SetConsoleTitleW(LPCWSTR title
)
457 SERVER_START_REQ( set_console_input_info
)
460 req
->mask
= SET_CONSOLE_INPUT_INFO_TITLE
;
461 wine_server_add_data( req
, title
, strlenW(title
) * sizeof(WCHAR
) );
462 ret
= !wine_server_call_err( req
);
469 /***********************************************************************
470 * GetNumberOfConsoleMouseButtons (KERNEL32.@)
472 BOOL WINAPI
GetNumberOfConsoleMouseButtons(LPDWORD nrofbuttons
)
474 FIXME("(%p): stub\n", nrofbuttons
);
479 /******************************************************************************
480 * SetConsoleInputExeNameW [KERNEL32.@]
485 BOOL WINAPI
SetConsoleInputExeNameW(LPCWSTR name
)
487 FIXME("(%s): stub!\n", debugstr_w(name
));
489 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
493 /******************************************************************************
494 * SetConsoleInputExeNameA [KERNEL32.@]
499 BOOL WINAPI
SetConsoleInputExeNameA(LPCSTR name
)
501 int len
= MultiByteToWideChar(CP_ACP
, 0, name
, -1, NULL
, 0);
502 LPWSTR xptr
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
505 if (!xptr
) return FALSE
;
507 MultiByteToWideChar(CP_ACP
, 0, name
, -1, xptr
, len
);
508 ret
= SetConsoleInputExeNameW(xptr
);
509 HeapFree(GetProcessHeap(), 0, xptr
);
514 static BOOL WINAPI
CONSOLE_DefaultHandler(DWORD dwCtrlType
)
516 FIXME("Terminating process %lx on event %lx\n", GetCurrentProcessId(), dwCtrlType
);
518 /* should never go here */
522 /******************************************************************************
523 * SetConsoleCtrlHandler [KERNEL32.@] Adds function to calling process list
526 * func [I] Address of handler function
527 * add [I] Handler to add or remove
534 * James Sutherland (JamesSutherland@gmx.de)
535 * Added global variables console_ignore_ctrl_c and handlers[]
536 * Does not yet do any error checking, or set LastError if failed.
537 * This doesn't yet matter, since these handlers are not yet called...!
540 static unsigned int console_ignore_ctrl_c
= 0; /* FIXME: this should be inherited somehow */
541 static PHANDLER_ROUTINE handlers
[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,CONSOLE_DefaultHandler
};
543 /*****************************************************************************/
545 BOOL WINAPI
SetConsoleCtrlHandler(PHANDLER_ROUTINE func
, BOOL add
)
547 int alloc_loop
= sizeof(handlers
)/sizeof(handlers
[0]) - 1;
549 FIXME("(%p,%i) - no error checking or testing yet\n", func
, add
);
553 console_ignore_ctrl_c
= add
;
558 for (; alloc_loop
>= 0 && handlers
[alloc_loop
]; alloc_loop
--);
561 FIXME("Out of space on CtrlHandler table\n");
564 handlers
[alloc_loop
] = func
;
568 for (; alloc_loop
>= 0 && handlers
[alloc_loop
] != func
; alloc_loop
--);
571 WARN("Attempt to remove non-installed CtrlHandler %p\n", func
);
575 if (alloc_loop
== sizeof(handlers
)/sizeof(handlers
[0]) - 1)
577 ERR("Who's trying to remove default handler???\n");
581 memmove(&handlers
[1], &handlers
[0], alloc_loop
* sizeof(handlers
[0]));
587 static WINE_EXCEPTION_FILTER(CONSOLE_CtrlEventHandler
)
589 TRACE("(%lx)\n", GetExceptionCode());
590 return EXCEPTION_EXECUTE_HANDLER
;
593 /******************************************************************************
594 * GenerateConsoleCtrlEvent [KERNEL32.@] Simulate a CTRL-C or CTRL-BREAK
597 * dwCtrlEvent [I] Type of event
598 * dwProcessGroupID [I] Process group ID to send event to
601 * Doesn't yet work...!
605 * Failure: False (and *should* [but doesn't] set LastError)
607 BOOL WINAPI
GenerateConsoleCtrlEvent(DWORD dwCtrlEvent
,
608 DWORD dwProcessGroupID
)
610 if (dwCtrlEvent
!= CTRL_C_EVENT
&& dwCtrlEvent
!= CTRL_BREAK_EVENT
)
612 ERR("invalid event %ld for PGID %ld\n", dwCtrlEvent
, dwProcessGroupID
);
616 if (dwProcessGroupID
== GetCurrentProcessId() || dwProcessGroupID
== 0)
620 FIXME("Attempt to send event %ld to self groupID, doing locally only\n", dwCtrlEvent
);
622 /* this is only meaningfull when done locally, otherwise it will have to be done on
623 * the 'receive' side of the event generation
625 if (dwCtrlEvent
== CTRL_C_EVENT
&& console_ignore_ctrl_c
)
628 /* try to pass the exception to the debugger
629 * if it continues, there's nothing more to do
630 * otherwise, we need to send the ctrl-event to the handlers
634 RaiseException( (dwCtrlEvent
== CTRL_C_EVENT
) ? DBG_CONTROL_C
: DBG_CONTROL_BREAK
,
637 __EXCEPT(CONSOLE_CtrlEventHandler
)
639 /* the debugger didn't continue... so, pass to ctrl handlers */
640 for (i
= 0; i
< sizeof(handlers
)/sizeof(handlers
[0]); i
++)
642 if (handlers
[i
] && (handlers
[i
])(dwCtrlEvent
)) break;
648 FIXME("event %ld to external PGID %ld - not implemented yet\n", dwCtrlEvent
, dwProcessGroupID
);
653 /******************************************************************************
654 * CreateConsoleScreenBuffer [KERNEL32.@] Creates a console screen buffer
657 * dwDesiredAccess [I] Access flag
658 * dwShareMode [I] Buffer share mode
659 * sa [I] Security attributes
660 * dwFlags [I] Type of buffer to create
661 * lpScreenBufferData [I] Reserved
664 * Should call SetLastError
667 * Success: Handle to new console screen buffer
668 * Failure: INVALID_HANDLE_VALUE
670 HANDLE WINAPI
CreateConsoleScreenBuffer(DWORD dwDesiredAccess
, DWORD dwShareMode
,
671 LPSECURITY_ATTRIBUTES sa
, DWORD dwFlags
,
672 LPVOID lpScreenBufferData
)
674 HANDLE ret
= INVALID_HANDLE_VALUE
;
676 TRACE("(%ld,%ld,%p,%ld,%p)\n",
677 dwDesiredAccess
, dwShareMode
, sa
, dwFlags
, lpScreenBufferData
);
679 if (dwFlags
!= CONSOLE_TEXTMODE_BUFFER
|| lpScreenBufferData
!= NULL
)
681 SetLastError(ERROR_INVALID_PARAMETER
);
682 return INVALID_HANDLE_VALUE
;
685 SERVER_START_REQ(create_console_output
)
688 req
->access
= dwDesiredAccess
;
689 req
->share
= dwShareMode
;
690 req
->inherit
= (sa
&& sa
->bInheritHandle
);
691 if (!wine_server_call_err( req
)) ret
= reply
->handle_out
;
699 /***********************************************************************
700 * GetConsoleScreenBufferInfo (KERNEL32.@)
702 BOOL WINAPI
GetConsoleScreenBufferInfo(HANDLE hConsoleOutput
, LPCONSOLE_SCREEN_BUFFER_INFO csbi
)
706 SERVER_START_REQ(get_console_output_info
)
708 req
->handle
= hConsoleOutput
;
709 if ((ret
= !wine_server_call_err( req
)))
711 csbi
->dwSize
.X
= reply
->width
;
712 csbi
->dwSize
.Y
= reply
->height
;
713 csbi
->dwCursorPosition
.X
= reply
->cursor_x
;
714 csbi
->dwCursorPosition
.Y
= reply
->cursor_y
;
715 csbi
->wAttributes
= reply
->attr
;
716 csbi
->srWindow
.Left
= reply
->win_left
;
717 csbi
->srWindow
.Right
= reply
->win_right
;
718 csbi
->srWindow
.Top
= reply
->win_top
;
719 csbi
->srWindow
.Bottom
= reply
->win_bottom
;
720 csbi
->dwMaximumWindowSize
.X
= reply
->max_width
;
721 csbi
->dwMaximumWindowSize
.Y
= reply
->max_height
;
730 /******************************************************************************
731 * SetConsoleActiveScreenBuffer [KERNEL32.@] Sets buffer to current console
737 BOOL WINAPI
SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput
)
741 TRACE("(%x)\n", hConsoleOutput
);
743 SERVER_START_REQ( set_console_input_info
)
746 req
->mask
= SET_CONSOLE_INPUT_INFO_ACTIVE_SB
;
747 req
->active_sb
= hConsoleOutput
;
748 ret
= !wine_server_call_err( req
);
755 /***********************************************************************
756 * GetConsoleMode (KERNEL32.@)
758 BOOL WINAPI
GetConsoleMode(HANDLE hcon
, LPDWORD mode
)
762 SERVER_START_REQ(get_console_mode
)
765 ret
= !wine_server_call_err( req
);
766 if (ret
&& mode
) *mode
= reply
->mode
;
773 /******************************************************************************
774 * SetConsoleMode [KERNEL32.@] Sets input mode of console's input buffer
777 * hcon [I] Handle to console input or screen buffer
778 * mode [I] Input or output mode to set
784 BOOL WINAPI
SetConsoleMode(HANDLE hcon
, DWORD mode
)
788 TRACE("(%x,%lx)\n", hcon
, mode
);
790 SERVER_START_REQ(set_console_mode
)
794 ret
= !wine_server_call_err( req
);
797 /* FIXME: when resetting a console input to editline mode, I think we should
798 * empty the S_EditString buffer
804 /******************************************************************
807 * WriteConsoleOutput helper: hides server call semantics
809 static int write_char(HANDLE hCon
, LPCWSTR lpBuffer
, int nc
, COORD
* pos
)
815 SERVER_START_REQ( write_console_output
)
820 req
->mode
= CHAR_INFO_MODE_TEXTSTDATTR
;
822 wine_server_add_data( req
, lpBuffer
, nc
* sizeof(WCHAR
) );
823 if (!wine_server_call_err( req
)) written
= reply
->written
;
827 if (written
> 0) pos
->X
+= written
;
831 /******************************************************************
834 * WriteConsoleOutput helper: handles passing to next line (+scrolling if necessary)
837 static int next_line(HANDLE hCon
, CONSOLE_SCREEN_BUFFER_INFO
* csbi
)
843 csbi
->dwCursorPosition
.X
= 0;
844 csbi
->dwCursorPosition
.Y
++;
846 if (csbi
->dwCursorPosition
.Y
< csbi
->dwSize
.Y
) return 1;
849 src
.Bottom
= csbi
->dwSize
.Y
- 1;
851 src
.Right
= csbi
->dwSize
.X
- 1;
856 ci
.Attributes
= csbi
->wAttributes
;
857 ci
.Char
.UnicodeChar
= ' ';
859 csbi
->dwCursorPosition
.Y
--;
860 if (!ScrollConsoleScreenBufferW(hCon
, &src
, NULL
, dst
, &ci
))
865 /******************************************************************
868 * WriteConsoleOutput helper: writes a block of non special characters
871 static int write_block(HANDLE hCon
, CONSOLE_SCREEN_BUFFER_INFO
* csbi
,
872 DWORD mode
, LPWSTR ptr
, int len
)
874 int blk
; /* number of chars to write on first line */
876 if (len
<= 0) return 1;
878 blk
= min(len
, csbi
->dwSize
.X
- csbi
->dwCursorPosition
.X
);
880 if (write_char(hCon
, ptr
, blk
, &csbi
->dwCursorPosition
) != blk
)
883 if (blk
< len
) /* special handling for right border */
885 if (mode
& ENABLE_WRAP_AT_EOL_OUTPUT
) /* writes remaining on next line */
887 if (!next_line(hCon
, csbi
) ||
888 write_char(hCon
, ptr
+ blk
, len
- blk
, &csbi
->dwCursorPosition
) != len
- blk
)
891 else /* all remaining chars should be written on last column, so only write the last one */
893 csbi
->dwCursorPosition
.X
= csbi
->dwSize
.X
- 1;
894 if (write_char(hCon
, ptr
+ len
- 1, 1, &csbi
->dwCursorPosition
) != 1)
896 csbi
->dwCursorPosition
.X
= csbi
->dwSize
.X
- 1;
902 /***********************************************************************
903 * WriteConsoleW (KERNEL32.@)
905 BOOL WINAPI
WriteConsoleW(HANDLE hConsoleOutput
, LPCVOID lpBuffer
, DWORD nNumberOfCharsToWrite
,
906 LPDWORD lpNumberOfCharsWritten
, LPVOID lpReserved
)
910 WCHAR
* psz
= (WCHAR
*)lpBuffer
;
911 CONSOLE_SCREEN_BUFFER_INFO csbi
;
914 TRACE("%d %s %ld %p %p\n",
915 hConsoleOutput
, debugstr_wn(lpBuffer
, nNumberOfCharsToWrite
),
916 nNumberOfCharsToWrite
, lpNumberOfCharsWritten
, lpReserved
);
918 if (lpNumberOfCharsWritten
) *lpNumberOfCharsWritten
= 0;
920 if (!GetConsoleMode(hConsoleOutput
, &mode
) ||
921 !GetConsoleScreenBufferInfo(hConsoleOutput
, &csbi
))
924 if (mode
& ENABLE_PROCESSED_OUTPUT
)
928 for (i
= 0; i
< nNumberOfCharsToWrite
; i
++)
932 case '\b': case '\t': case '\n': case '\a': case '\r':
933 /* don't handle here the i-th char... done below */
934 if ((k
= i
- first
) > 0)
936 if (!write_block(hConsoleOutput
, &csbi
, mode
, &psz
[first
], k
))
946 if (csbi
.dwCursorPosition
.X
> 0) csbi
.dwCursorPosition
.X
--;
950 WCHAR tmp
[8] = {' ',' ',' ',' ',' ',' ',' ',' '};
952 if (!write_block(hConsoleOutput
, &csbi
, mode
, tmp
,
953 ((csbi
.dwCursorPosition
.X
+ 8) & ~7) - csbi
.dwCursorPosition
.X
))
958 next_line(hConsoleOutput
, &csbi
);
964 csbi
.dwCursorPosition
.X
= 0;
972 /* write the remaining block (if any) if processed output is enabled, or the
973 * entire buffer otherwise
975 if ((k
= nNumberOfCharsToWrite
- first
) > 0)
977 if (!write_block(hConsoleOutput
, &csbi
, mode
, &psz
[first
], k
))
983 SetConsoleCursorPosition(hConsoleOutput
, csbi
.dwCursorPosition
);
984 if (lpNumberOfCharsWritten
) *lpNumberOfCharsWritten
= nw
;
989 /***********************************************************************
990 * WriteConsoleA (KERNEL32.@)
992 BOOL WINAPI
WriteConsoleA(HANDLE hConsoleOutput
, LPCVOID lpBuffer
, DWORD nNumberOfCharsToWrite
,
993 LPDWORD lpNumberOfCharsWritten
, LPVOID lpReserved
)
999 n
= MultiByteToWideChar(CP_ACP
, 0, lpBuffer
, nNumberOfCharsToWrite
, NULL
, 0);
1001 if (lpNumberOfCharsWritten
) *lpNumberOfCharsWritten
= 0;
1002 xstring
= HeapAlloc(GetProcessHeap(), 0, n
* sizeof(WCHAR
));
1003 if (!xstring
) return 0;
1005 MultiByteToWideChar(CP_ACP
, 0, lpBuffer
, nNumberOfCharsToWrite
, xstring
, n
);
1007 ret
= WriteConsoleW(hConsoleOutput
, xstring
, n
, lpNumberOfCharsWritten
, 0);
1009 HeapFree(GetProcessHeap(), 0, xstring
);
1014 /******************************************************************************
1015 * SetConsoleCursorPosition [KERNEL32.@]
1016 * Sets the cursor position in console
1019 * hConsoleOutput [I] Handle of console screen buffer
1020 * dwCursorPosition [I] New cursor position coordinates
1024 BOOL WINAPI
SetConsoleCursorPosition(HANDLE hcon
, COORD pos
)
1027 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1031 TRACE("%x %d %d\n", hcon
, pos
.X
, pos
.Y
);
1033 SERVER_START_REQ(set_console_output_info
)
1036 req
->cursor_x
= pos
.X
;
1037 req
->cursor_y
= pos
.Y
;
1038 req
->mask
= SET_CONSOLE_OUTPUT_INFO_CURSOR_POS
;
1039 ret
= !wine_server_call_err( req
);
1043 if (!ret
|| !GetConsoleScreenBufferInfo(hcon
, &csbi
))
1046 /* if cursor is no longer visible, scroll the visible window... */
1047 w
= csbi
.srWindow
.Right
- csbi
.srWindow
.Left
+ 1;
1048 h
= csbi
.srWindow
.Bottom
- csbi
.srWindow
.Top
+ 1;
1049 if (pos
.X
< csbi
.srWindow
.Left
)
1051 csbi
.srWindow
.Left
= min(pos
.X
, csbi
.dwSize
.X
- w
);
1054 else if (pos
.X
> csbi
.srWindow
.Right
)
1056 csbi
.srWindow
.Left
= max(pos
.X
, w
) - w
+ 1;
1059 csbi
.srWindow
.Right
= csbi
.srWindow
.Left
+ w
- 1;
1061 if (pos
.Y
< csbi
.srWindow
.Top
)
1063 csbi
.srWindow
.Top
= min(pos
.Y
, csbi
.dwSize
.Y
- h
);
1066 else if (pos
.Y
> csbi
.srWindow
.Bottom
)
1068 csbi
.srWindow
.Top
= max(pos
.Y
, h
) - h
+ 1;
1071 csbi
.srWindow
.Bottom
= csbi
.srWindow
.Top
+ h
- 1;
1073 ret
= (do_move
) ? SetConsoleWindowInfo(hcon
, TRUE
, &csbi
.srWindow
) : TRUE
;
1078 /******************************************************************************
1079 * GetConsoleCursorInfo [KERNEL32.@] Gets size and visibility of console
1082 * hcon [I] Handle to console screen buffer
1083 * cinfo [O] Address of cursor information
1089 BOOL WINAPI
GetConsoleCursorInfo(HANDLE hcon
, LPCONSOLE_CURSOR_INFO cinfo
)
1093 SERVER_START_REQ(get_console_output_info
)
1096 ret
= !wine_server_call_err( req
);
1099 cinfo
->dwSize
= reply
->cursor_size
;
1100 cinfo
->bVisible
= reply
->cursor_visible
;
1108 /******************************************************************************
1109 * SetConsoleCursorInfo [KERNEL32.@] Sets size and visibility of cursor
1112 * hcon [I] Handle to console screen buffer
1113 * cinfo [I] Address of cursor information
1118 BOOL WINAPI
SetConsoleCursorInfo(HANDLE hCon
, LPCONSOLE_CURSOR_INFO cinfo
)
1122 SERVER_START_REQ(set_console_output_info
)
1125 req
->cursor_size
= cinfo
->dwSize
;
1126 req
->cursor_visible
= cinfo
->bVisible
;
1127 req
->mask
= SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM
;
1128 ret
= !wine_server_call_err( req
);
1135 /******************************************************************************
1136 * SetConsoleWindowInfo [KERNEL32.@] Sets size and position of console
1139 * hcon [I] Handle to console screen buffer
1140 * bAbsolute [I] Coordinate type flag
1141 * window [I] Address of new window rectangle
1146 BOOL WINAPI
SetConsoleWindowInfo(HANDLE hCon
, BOOL bAbsolute
, LPSMALL_RECT window
)
1148 SMALL_RECT p
= *window
;
1153 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1154 if (!GetConsoleScreenBufferInfo(hCon
, &csbi
))
1156 p
.Left
+= csbi
.srWindow
.Left
;
1157 p
.Top
+= csbi
.srWindow
.Top
;
1158 p
.Right
+= csbi
.srWindow
.Left
;
1159 p
.Bottom
+= csbi
.srWindow
.Top
;
1161 SERVER_START_REQ(set_console_output_info
)
1164 req
->win_left
= p
.Left
;
1165 req
->win_top
= p
.Top
;
1166 req
->win_right
= p
.Right
;
1167 req
->win_bottom
= p
.Bottom
;
1168 req
->mask
= SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW
;
1169 ret
= !wine_server_call_err( req
);
1177 /******************************************************************************
1178 * SetConsoleTextAttribute [KERNEL32.@] Sets colors for text
1180 * Sets the foreground and background color attributes of characters
1181 * written to the screen buffer.
1187 BOOL WINAPI
SetConsoleTextAttribute(HANDLE hConsoleOutput
, WORD wAttr
)
1191 SERVER_START_REQ(set_console_output_info
)
1193 req
->handle
= hConsoleOutput
;
1195 req
->mask
= SET_CONSOLE_OUTPUT_INFO_ATTR
;
1196 ret
= !wine_server_call_err( req
);
1203 /******************************************************************************
1204 * SetConsoleScreenBufferSize [KERNEL32.@] Changes size of console
1207 * hConsoleOutput [I] Handle to console screen buffer
1208 * dwSize [I] New size in character rows and cols
1214 BOOL WINAPI
SetConsoleScreenBufferSize(HANDLE hConsoleOutput
, COORD dwSize
)
1218 SERVER_START_REQ(set_console_output_info
)
1220 req
->handle
= hConsoleOutput
;
1221 req
->width
= dwSize
.X
;
1222 req
->height
= dwSize
.Y
;
1223 req
->mask
= SET_CONSOLE_OUTPUT_INFO_SIZE
;
1224 ret
= !wine_server_call_err( req
);
1231 /******************************************************************************
1232 * ScrollConsoleScreenBufferA [KERNEL32.@]
1235 BOOL WINAPI
ScrollConsoleScreenBufferA(HANDLE hConsoleOutput
, LPSMALL_RECT lpScrollRect
,
1236 LPSMALL_RECT lpClipRect
, COORD dwDestOrigin
,
1241 ciw
.Attributes
= lpFill
->Attributes
;
1242 MultiByteToWideChar(CP_ACP
, 0, &lpFill
->Char
.AsciiChar
, 1, &ciw
.Char
.UnicodeChar
, 1);
1244 return ScrollConsoleScreenBufferW(hConsoleOutput
, lpScrollRect
, lpClipRect
,
1245 dwDestOrigin
, &ciw
);
1248 /******************************************************************
1251 * Helper function for ScrollConsoleScreenBufferW
1252 * Fills a part of a line with a constant character info
1254 static void fill_line_uniform(HANDLE hConsoleOutput
, int i
, int j
, int len
, LPCHAR_INFO lpFill
)
1256 SERVER_START_REQ( fill_console_output
)
1258 req
->handle
= hConsoleOutput
;
1259 req
->mode
= CHAR_INFO_MODE_TEXTATTR
;
1264 req
->data
.ch
= lpFill
->Char
.UnicodeChar
;
1265 req
->data
.attr
= lpFill
->Attributes
;
1266 wine_server_call_err( req
);
1271 /******************************************************************************
1272 * ScrollConsoleScreenBufferW [KERNEL32.@]
1276 BOOL WINAPI
ScrollConsoleScreenBufferW(HANDLE hConsoleOutput
, LPSMALL_RECT lpScrollRect
,
1277 LPSMALL_RECT lpClipRect
, COORD dwDestOrigin
,
1285 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1289 TRACE("(%d,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", hConsoleOutput
,
1290 lpScrollRect
->Left
, lpScrollRect
->Top
,
1291 lpScrollRect
->Right
, lpScrollRect
->Bottom
,
1292 lpClipRect
->Left
, lpClipRect
->Top
,
1293 lpClipRect
->Right
, lpClipRect
->Bottom
,
1294 dwDestOrigin
.X
, dwDestOrigin
.Y
, lpFill
);
1296 TRACE("(%d,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", hConsoleOutput
,
1297 lpScrollRect
->Left
, lpScrollRect
->Top
,
1298 lpScrollRect
->Right
, lpScrollRect
->Bottom
,
1299 dwDestOrigin
.X
, dwDestOrigin
.Y
, lpFill
);
1301 if (!GetConsoleScreenBufferInfo(hConsoleOutput
, &csbi
))
1304 /* step 1: get dst rect */
1305 dst
.Left
= dwDestOrigin
.X
;
1306 dst
.Top
= dwDestOrigin
.Y
;
1307 dst
.Right
= dst
.Left
+ (lpScrollRect
->Right
- lpScrollRect
->Left
);
1308 dst
.Bottom
= dst
.Top
+ (lpScrollRect
->Bottom
- lpScrollRect
->Top
);
1310 /* step 2a: compute the final clip rect (optional passed clip and screen buffer limits */
1313 clip
.Left
= max(0, lpClipRect
->Left
);
1314 clip
.Right
= min(csbi
.dwSize
.X
- 1, lpClipRect
->Right
);
1315 clip
.Top
= max(0, lpClipRect
->Top
);
1316 clip
.Bottom
= min(csbi
.dwSize
.Y
- 1, lpClipRect
->Bottom
);
1321 clip
.Right
= csbi
.dwSize
.X
- 1;
1323 clip
.Bottom
= csbi
.dwSize
.Y
- 1;
1325 if (clip
.Left
> clip
.Right
|| clip
.Top
> clip
.Bottom
) return FALSE
;
1327 /* step 2b: clip dst rect */
1328 if (dst
.Left
< clip
.Left
) dst
.Left
= clip
.Left
;
1329 if (dst
.Top
< clip
.Top
) dst
.Top
= clip
.Top
;
1330 if (dst
.Right
> clip
.Right
) dst
.Right
= clip
.Right
;
1331 if (dst
.Bottom
> clip
.Bottom
) dst
.Bottom
= clip
.Bottom
;
1333 /* step 3: transfer the bits */
1334 SERVER_START_REQ(move_console_output
)
1336 req
->handle
= hConsoleOutput
;
1337 req
->x_src
= lpScrollRect
->Left
;
1338 req
->y_src
= lpScrollRect
->Top
;
1339 req
->x_dst
= dst
.Left
;
1340 req
->y_dst
= dst
.Top
;
1341 req
->w
= dst
.Right
- dst
.Left
+ 1;
1342 req
->h
= dst
.Bottom
- dst
.Top
+ 1;
1343 ret
= !wine_server_call_err( req
);
1347 if (!ret
) return FALSE
;
1349 /* step 4: clean out the exposed part */
1351 /* have to write celll [i,j] if it is not in dst rect (because it has already
1352 * been written to by the scroll) and is in clip (we shall not write
1355 for (j
= max(lpScrollRect
->Top
, clip
.Top
); j
<= min(lpScrollRect
->Bottom
, clip
.Bottom
); j
++)
1357 inside
= dst
.Top
<= j
&& j
<= dst
.Bottom
;
1359 for (i
= max(lpScrollRect
->Left
, clip
.Left
); i
<= min(lpScrollRect
->Right
, clip
.Right
); i
++)
1361 if (inside
&& dst
.Left
<= i
&& i
<= dst
.Right
)
1365 fill_line_uniform(hConsoleOutput
, start
, j
, i
- start
, lpFill
);
1371 if (start
== -1) start
= i
;
1375 fill_line_uniform(hConsoleOutput
, start
, j
, i
- start
, lpFill
);
1382 /* ====================================================================
1384 * Console manipulation functions
1386 * ====================================================================*/
1387 /* some missing functions...
1388 * FIXME: those are likely to be defined as undocumented function in kernel32 (or part of them)
1389 * should get the right API and implement them
1390 * GetConsoleCommandHistory[AW] (dword dword dword)
1391 * GetConsoleCommandHistoryLength[AW]
1392 * SetConsoleCommandHistoryMode
1393 * SetConsoleNumberOfCommands[AW]
1395 int CONSOLE_GetHistory(int idx
, WCHAR
* buf
, int buf_len
)
1399 SERVER_START_REQ( get_console_input_history
)
1403 if (buf
&& buf_len
> 1)
1405 wine_server_set_reply( req
, buf
, (buf_len
- 1) * sizeof(WCHAR
) );
1407 if (!wine_server_call_err( req
))
1409 if (buf
) buf
[wine_server_reply_size(reply
) / sizeof(WCHAR
)] = 0;
1410 len
= reply
->total
/ sizeof(WCHAR
) + 1;
1417 /******************************************************************
1418 * CONSOLE_AppendHistory
1422 BOOL
CONSOLE_AppendHistory(const WCHAR
* ptr
)
1424 size_t len
= strlenW(ptr
);
1427 while (len
&& (ptr
[len
- 1] == '\n' || ptr
[len
- 1] == '\r')) len
--;
1429 SERVER_START_REQ( append_console_input_history
)
1432 wine_server_add_data( req
, ptr
, len
* sizeof(WCHAR
) );
1433 ret
= !wine_server_call_err( req
);
1439 /******************************************************************
1440 * CONSOLE_GetNumHistoryEntries
1444 unsigned CONSOLE_GetNumHistoryEntries(void)
1447 SERVER_START_REQ(get_console_input_info
)
1450 if (!wine_server_call_err( req
)) ret
= reply
->history_index
;