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_USECOUNTCHARS
)
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
869 * Block can spread on several lines, and wrapping, if needed, is
873 static int write_block(HANDLE hCon
, CONSOLE_SCREEN_BUFFER_INFO
* csbi
,
874 DWORD mode
, LPWSTR ptr
, int len
)
876 int blk
; /* number of chars to write on current line */
878 if (len
<= 0) return 1;
880 if (mode
& ENABLE_WRAP_AT_EOL_OUTPUT
) /* writes remaining on next line */
884 for (done
= 0; done
< len
; done
+= blk
)
886 blk
= min(len
- done
, csbi
->dwSize
.X
- csbi
->dwCursorPosition
.X
);
888 if (write_char(hCon
, ptr
+ done
, blk
, &csbi
->dwCursorPosition
) != blk
)
890 if (csbi
->dwCursorPosition
.X
== csbi
->dwSize
.X
&& !next_line(hCon
, csbi
))
896 blk
= min(len
, csbi
->dwSize
.X
- csbi
->dwCursorPosition
.X
);
898 if (write_char(hCon
, ptr
, blk
, &csbi
->dwCursorPosition
) != blk
)
902 csbi
->dwCursorPosition
.X
= csbi
->dwSize
.X
- 1;
903 /* all remaining chars should be written on last column,
904 * so only overwrite the last column with last char in block
906 if (write_char(hCon
, ptr
+ len
- 1, 1, &csbi
->dwCursorPosition
) != 1)
908 csbi
->dwCursorPosition
.X
= csbi
->dwSize
.X
- 1;
915 /***********************************************************************
916 * WriteConsoleW (KERNEL32.@)
918 BOOL WINAPI
WriteConsoleW(HANDLE hConsoleOutput
, LPCVOID lpBuffer
, DWORD nNumberOfCharsToWrite
,
919 LPDWORD lpNumberOfCharsWritten
, LPVOID lpReserved
)
923 WCHAR
* psz
= (WCHAR
*)lpBuffer
;
924 CONSOLE_SCREEN_BUFFER_INFO csbi
;
927 TRACE("%d %s %ld %p %p\n",
928 hConsoleOutput
, debugstr_wn(lpBuffer
, nNumberOfCharsToWrite
),
929 nNumberOfCharsToWrite
, lpNumberOfCharsWritten
, lpReserved
);
931 if (lpNumberOfCharsWritten
) *lpNumberOfCharsWritten
= 0;
933 if (!GetConsoleMode(hConsoleOutput
, &mode
) ||
934 !GetConsoleScreenBufferInfo(hConsoleOutput
, &csbi
))
937 if (mode
& ENABLE_PROCESSED_OUTPUT
)
941 for (i
= 0; i
< nNumberOfCharsToWrite
; i
++)
945 case '\b': case '\t': case '\n': case '\a': case '\r':
946 /* don't handle here the i-th char... done below */
947 if ((k
= i
- first
) > 0)
949 if (!write_block(hConsoleOutput
, &csbi
, mode
, &psz
[first
], k
))
959 if (csbi
.dwCursorPosition
.X
> 0) csbi
.dwCursorPosition
.X
--;
963 WCHAR tmp
[8] = {' ',' ',' ',' ',' ',' ',' ',' '};
965 if (!write_block(hConsoleOutput
, &csbi
, mode
, tmp
,
966 ((csbi
.dwCursorPosition
.X
+ 8) & ~7) - csbi
.dwCursorPosition
.X
))
971 next_line(hConsoleOutput
, &csbi
);
977 csbi
.dwCursorPosition
.X
= 0;
985 /* write the remaining block (if any) if processed output is enabled, or the
986 * entire buffer otherwise
988 if ((k
= nNumberOfCharsToWrite
- first
) > 0)
990 if (!write_block(hConsoleOutput
, &csbi
, mode
, &psz
[first
], k
))
996 SetConsoleCursorPosition(hConsoleOutput
, csbi
.dwCursorPosition
);
997 if (lpNumberOfCharsWritten
) *lpNumberOfCharsWritten
= nw
;
1002 /***********************************************************************
1003 * WriteConsoleA (KERNEL32.@)
1005 BOOL WINAPI
WriteConsoleA(HANDLE hConsoleOutput
, LPCVOID lpBuffer
, DWORD nNumberOfCharsToWrite
,
1006 LPDWORD lpNumberOfCharsWritten
, LPVOID lpReserved
)
1012 n
= MultiByteToWideChar(CP_ACP
, 0, lpBuffer
, nNumberOfCharsToWrite
, NULL
, 0);
1014 if (lpNumberOfCharsWritten
) *lpNumberOfCharsWritten
= 0;
1015 xstring
= HeapAlloc(GetProcessHeap(), 0, n
* sizeof(WCHAR
));
1016 if (!xstring
) return 0;
1018 MultiByteToWideChar(CP_ACP
, 0, lpBuffer
, nNumberOfCharsToWrite
, xstring
, n
);
1020 ret
= WriteConsoleW(hConsoleOutput
, xstring
, n
, lpNumberOfCharsWritten
, 0);
1022 HeapFree(GetProcessHeap(), 0, xstring
);
1027 /******************************************************************************
1028 * SetConsoleCursorPosition [KERNEL32.@]
1029 * Sets the cursor position in console
1032 * hConsoleOutput [I] Handle of console screen buffer
1033 * dwCursorPosition [I] New cursor position coordinates
1037 BOOL WINAPI
SetConsoleCursorPosition(HANDLE hcon
, COORD pos
)
1040 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1044 TRACE("%x %d %d\n", hcon
, pos
.X
, pos
.Y
);
1046 SERVER_START_REQ(set_console_output_info
)
1049 req
->cursor_x
= pos
.X
;
1050 req
->cursor_y
= pos
.Y
;
1051 req
->mask
= SET_CONSOLE_OUTPUT_INFO_CURSOR_POS
;
1052 ret
= !wine_server_call_err( req
);
1056 if (!ret
|| !GetConsoleScreenBufferInfo(hcon
, &csbi
))
1059 /* if cursor is no longer visible, scroll the visible window... */
1060 w
= csbi
.srWindow
.Right
- csbi
.srWindow
.Left
+ 1;
1061 h
= csbi
.srWindow
.Bottom
- csbi
.srWindow
.Top
+ 1;
1062 if (pos
.X
< csbi
.srWindow
.Left
)
1064 csbi
.srWindow
.Left
= min(pos
.X
, csbi
.dwSize
.X
- w
);
1067 else if (pos
.X
> csbi
.srWindow
.Right
)
1069 csbi
.srWindow
.Left
= max(pos
.X
, w
) - w
+ 1;
1072 csbi
.srWindow
.Right
= csbi
.srWindow
.Left
+ w
- 1;
1074 if (pos
.Y
< csbi
.srWindow
.Top
)
1076 csbi
.srWindow
.Top
= min(pos
.Y
, csbi
.dwSize
.Y
- h
);
1079 else if (pos
.Y
> csbi
.srWindow
.Bottom
)
1081 csbi
.srWindow
.Top
= max(pos
.Y
, h
) - h
+ 1;
1084 csbi
.srWindow
.Bottom
= csbi
.srWindow
.Top
+ h
- 1;
1086 ret
= (do_move
) ? SetConsoleWindowInfo(hcon
, TRUE
, &csbi
.srWindow
) : TRUE
;
1091 /******************************************************************************
1092 * GetConsoleCursorInfo [KERNEL32.@] Gets size and visibility of console
1095 * hcon [I] Handle to console screen buffer
1096 * cinfo [O] Address of cursor information
1102 BOOL WINAPI
GetConsoleCursorInfo(HANDLE hcon
, LPCONSOLE_CURSOR_INFO cinfo
)
1106 SERVER_START_REQ(get_console_output_info
)
1109 ret
= !wine_server_call_err( req
);
1112 cinfo
->dwSize
= reply
->cursor_size
;
1113 cinfo
->bVisible
= reply
->cursor_visible
;
1121 /******************************************************************************
1122 * SetConsoleCursorInfo [KERNEL32.@] Sets size and visibility of cursor
1125 * hcon [I] Handle to console screen buffer
1126 * cinfo [I] Address of cursor information
1131 BOOL WINAPI
SetConsoleCursorInfo(HANDLE hCon
, LPCONSOLE_CURSOR_INFO cinfo
)
1135 SERVER_START_REQ(set_console_output_info
)
1138 req
->cursor_size
= cinfo
->dwSize
;
1139 req
->cursor_visible
= cinfo
->bVisible
;
1140 req
->mask
= SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM
;
1141 ret
= !wine_server_call_err( req
);
1148 /******************************************************************************
1149 * SetConsoleWindowInfo [KERNEL32.@] Sets size and position of console
1152 * hcon [I] Handle to console screen buffer
1153 * bAbsolute [I] Coordinate type flag
1154 * window [I] Address of new window rectangle
1159 BOOL WINAPI
SetConsoleWindowInfo(HANDLE hCon
, BOOL bAbsolute
, LPSMALL_RECT window
)
1161 SMALL_RECT p
= *window
;
1166 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1167 if (!GetConsoleScreenBufferInfo(hCon
, &csbi
))
1169 p
.Left
+= csbi
.srWindow
.Left
;
1170 p
.Top
+= csbi
.srWindow
.Top
;
1171 p
.Right
+= csbi
.srWindow
.Left
;
1172 p
.Bottom
+= csbi
.srWindow
.Top
;
1174 SERVER_START_REQ(set_console_output_info
)
1177 req
->win_left
= p
.Left
;
1178 req
->win_top
= p
.Top
;
1179 req
->win_right
= p
.Right
;
1180 req
->win_bottom
= p
.Bottom
;
1181 req
->mask
= SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW
;
1182 ret
= !wine_server_call_err( req
);
1190 /******************************************************************************
1191 * SetConsoleTextAttribute [KERNEL32.@] Sets colors for text
1193 * Sets the foreground and background color attributes of characters
1194 * written to the screen buffer.
1200 BOOL WINAPI
SetConsoleTextAttribute(HANDLE hConsoleOutput
, WORD wAttr
)
1204 SERVER_START_REQ(set_console_output_info
)
1206 req
->handle
= hConsoleOutput
;
1208 req
->mask
= SET_CONSOLE_OUTPUT_INFO_ATTR
;
1209 ret
= !wine_server_call_err( req
);
1216 /******************************************************************************
1217 * SetConsoleScreenBufferSize [KERNEL32.@] Changes size of console
1220 * hConsoleOutput [I] Handle to console screen buffer
1221 * dwSize [I] New size in character rows and cols
1227 BOOL WINAPI
SetConsoleScreenBufferSize(HANDLE hConsoleOutput
, COORD dwSize
)
1231 SERVER_START_REQ(set_console_output_info
)
1233 req
->handle
= hConsoleOutput
;
1234 req
->width
= dwSize
.X
;
1235 req
->height
= dwSize
.Y
;
1236 req
->mask
= SET_CONSOLE_OUTPUT_INFO_SIZE
;
1237 ret
= !wine_server_call_err( req
);
1244 /******************************************************************************
1245 * ScrollConsoleScreenBufferA [KERNEL32.@]
1248 BOOL WINAPI
ScrollConsoleScreenBufferA(HANDLE hConsoleOutput
, LPSMALL_RECT lpScrollRect
,
1249 LPSMALL_RECT lpClipRect
, COORD dwDestOrigin
,
1254 ciw
.Attributes
= lpFill
->Attributes
;
1255 MultiByteToWideChar(CP_ACP
, 0, &lpFill
->Char
.AsciiChar
, 1, &ciw
.Char
.UnicodeChar
, 1);
1257 return ScrollConsoleScreenBufferW(hConsoleOutput
, lpScrollRect
, lpClipRect
,
1258 dwDestOrigin
, &ciw
);
1261 /******************************************************************
1264 * Helper function for ScrollConsoleScreenBufferW
1265 * Fills a part of a line with a constant character info
1267 static void fill_line_uniform(HANDLE hConsoleOutput
, int i
, int j
, int len
, LPCHAR_INFO lpFill
)
1269 SERVER_START_REQ( fill_console_output
)
1271 req
->handle
= hConsoleOutput
;
1272 req
->mode
= CHAR_INFO_MODE_TEXTATTR
;
1277 req
->data
.ch
= lpFill
->Char
.UnicodeChar
;
1278 req
->data
.attr
= lpFill
->Attributes
;
1279 wine_server_call_err( req
);
1284 /******************************************************************************
1285 * ScrollConsoleScreenBufferW [KERNEL32.@]
1289 BOOL WINAPI
ScrollConsoleScreenBufferW(HANDLE hConsoleOutput
, LPSMALL_RECT lpScrollRect
,
1290 LPSMALL_RECT lpClipRect
, COORD dwDestOrigin
,
1298 CONSOLE_SCREEN_BUFFER_INFO csbi
;
1302 TRACE("(%d,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", hConsoleOutput
,
1303 lpScrollRect
->Left
, lpScrollRect
->Top
,
1304 lpScrollRect
->Right
, lpScrollRect
->Bottom
,
1305 lpClipRect
->Left
, lpClipRect
->Top
,
1306 lpClipRect
->Right
, lpClipRect
->Bottom
,
1307 dwDestOrigin
.X
, dwDestOrigin
.Y
, lpFill
);
1309 TRACE("(%d,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", hConsoleOutput
,
1310 lpScrollRect
->Left
, lpScrollRect
->Top
,
1311 lpScrollRect
->Right
, lpScrollRect
->Bottom
,
1312 dwDestOrigin
.X
, dwDestOrigin
.Y
, lpFill
);
1314 if (!GetConsoleScreenBufferInfo(hConsoleOutput
, &csbi
))
1317 /* step 1: get dst rect */
1318 dst
.Left
= dwDestOrigin
.X
;
1319 dst
.Top
= dwDestOrigin
.Y
;
1320 dst
.Right
= dst
.Left
+ (lpScrollRect
->Right
- lpScrollRect
->Left
);
1321 dst
.Bottom
= dst
.Top
+ (lpScrollRect
->Bottom
- lpScrollRect
->Top
);
1323 /* step 2a: compute the final clip rect (optional passed clip and screen buffer limits */
1326 clip
.Left
= max(0, lpClipRect
->Left
);
1327 clip
.Right
= min(csbi
.dwSize
.X
- 1, lpClipRect
->Right
);
1328 clip
.Top
= max(0, lpClipRect
->Top
);
1329 clip
.Bottom
= min(csbi
.dwSize
.Y
- 1, lpClipRect
->Bottom
);
1334 clip
.Right
= csbi
.dwSize
.X
- 1;
1336 clip
.Bottom
= csbi
.dwSize
.Y
- 1;
1338 if (clip
.Left
> clip
.Right
|| clip
.Top
> clip
.Bottom
) return FALSE
;
1340 /* step 2b: clip dst rect */
1341 if (dst
.Left
< clip
.Left
) dst
.Left
= clip
.Left
;
1342 if (dst
.Top
< clip
.Top
) dst
.Top
= clip
.Top
;
1343 if (dst
.Right
> clip
.Right
) dst
.Right
= clip
.Right
;
1344 if (dst
.Bottom
> clip
.Bottom
) dst
.Bottom
= clip
.Bottom
;
1346 /* step 3: transfer the bits */
1347 SERVER_START_REQ(move_console_output
)
1349 req
->handle
= hConsoleOutput
;
1350 req
->x_src
= lpScrollRect
->Left
;
1351 req
->y_src
= lpScrollRect
->Top
;
1352 req
->x_dst
= dst
.Left
;
1353 req
->y_dst
= dst
.Top
;
1354 req
->w
= dst
.Right
- dst
.Left
+ 1;
1355 req
->h
= dst
.Bottom
- dst
.Top
+ 1;
1356 ret
= !wine_server_call_err( req
);
1360 if (!ret
) return FALSE
;
1362 /* step 4: clean out the exposed part */
1364 /* have to write celll [i,j] if it is not in dst rect (because it has already
1365 * been written to by the scroll) and is in clip (we shall not write
1368 for (j
= max(lpScrollRect
->Top
, clip
.Top
); j
<= min(lpScrollRect
->Bottom
, clip
.Bottom
); j
++)
1370 inside
= dst
.Top
<= j
&& j
<= dst
.Bottom
;
1372 for (i
= max(lpScrollRect
->Left
, clip
.Left
); i
<= min(lpScrollRect
->Right
, clip
.Right
); i
++)
1374 if (inside
&& dst
.Left
<= i
&& i
<= dst
.Right
)
1378 fill_line_uniform(hConsoleOutput
, start
, j
, i
- start
, lpFill
);
1384 if (start
== -1) start
= i
;
1388 fill_line_uniform(hConsoleOutput
, start
, j
, i
- start
, lpFill
);
1395 /* ====================================================================
1397 * Console manipulation functions
1399 * ====================================================================*/
1400 /* some missing functions...
1401 * FIXME: those are likely to be defined as undocumented function in kernel32 (or part of them)
1402 * should get the right API and implement them
1403 * GetConsoleCommandHistory[AW] (dword dword dword)
1404 * GetConsoleCommandHistoryLength[AW]
1405 * SetConsoleCommandHistoryMode
1406 * SetConsoleNumberOfCommands[AW]
1408 int CONSOLE_GetHistory(int idx
, WCHAR
* buf
, int buf_len
)
1412 SERVER_START_REQ( get_console_input_history
)
1416 if (buf
&& buf_len
> 1)
1418 wine_server_set_reply( req
, buf
, (buf_len
- 1) * sizeof(WCHAR
) );
1420 if (!wine_server_call_err( req
))
1422 if (buf
) buf
[wine_server_reply_size(reply
) / sizeof(WCHAR
)] = 0;
1423 len
= reply
->total
/ sizeof(WCHAR
) + 1;
1430 /******************************************************************
1431 * CONSOLE_AppendHistory
1435 BOOL
CONSOLE_AppendHistory(const WCHAR
* ptr
)
1437 size_t len
= strlenW(ptr
);
1440 while (len
&& (ptr
[len
- 1] == '\n' || ptr
[len
- 1] == '\r')) len
--;
1442 SERVER_START_REQ( append_console_input_history
)
1445 wine_server_add_data( req
, ptr
, len
* sizeof(WCHAR
) );
1446 ret
= !wine_server_call_err( req
);
1452 /******************************************************************
1453 * CONSOLE_GetNumHistoryEntries
1457 unsigned CONSOLE_GetNumHistoryEntries(void)
1460 SERVER_START_REQ(get_console_input_info
)
1463 if (!wine_server_call_err( req
)) ret
= reply
->history_index
;