4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
27 #include "char-win-stdio.h"
32 HANDLE hInputReadyEvent
;
33 HANDLE hInputDoneEvent
;
35 uint8_t win_stdio_buf
;
38 #define WIN_STDIO_CHARDEV(obj) \
39 OBJECT_CHECK(WinStdioChardev, (obj), TYPE_CHARDEV_WIN_STDIO)
41 static void win_stdio_wait_func(void *opaque
)
43 Chardev
*chr
= CHARDEV(opaque
);
44 WinStdioChardev
*stdio
= WIN_STDIO_CHARDEV(opaque
);
50 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, ARRAY_SIZE(buf
), &dwSize
);
53 /* Avoid error storm */
54 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
58 for (i
= 0; i
< dwSize
; i
++) {
59 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
61 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
63 if (kev
->uChar
.AsciiChar
!= 0) {
64 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
65 if (qemu_chr_be_can_write(chr
)) {
66 uint8_t c
= kev
->uChar
.AsciiChar
;
67 qemu_chr_be_write(chr
, &c
, 1);
75 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
77 WinStdioChardev
*stdio
= WIN_STDIO_CHARDEV(param
);
83 /* Wait for one byte */
84 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
86 /* Exit in case of error, continue if nothing read */
94 /* Some terminal emulator returns \r\n for Enter, just pass \n */
95 if (stdio
->win_stdio_buf
== '\r') {
99 /* Signal the main thread and wait until the byte was eaten */
100 if (!SetEvent(stdio
->hInputReadyEvent
)) {
103 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
109 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
113 static void win_stdio_thread_wait_func(void *opaque
)
115 Chardev
*chr
= CHARDEV(opaque
);
116 WinStdioChardev
*stdio
= WIN_STDIO_CHARDEV(opaque
);
118 if (qemu_chr_be_can_write(chr
)) {
119 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
122 SetEvent(stdio
->hInputDoneEvent
);
125 static void qemu_chr_set_echo_win_stdio(Chardev
*chr
, bool echo
)
127 WinStdioChardev
*stdio
= WIN_STDIO_CHARDEV(chr
);
130 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
133 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
135 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
139 static void qemu_chr_open_stdio(Chardev
*chr
,
140 ChardevBackend
*backend
,
144 WinStdioChardev
*stdio
= WIN_STDIO_CHARDEV(chr
);
148 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
149 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
150 error_setg(errp
, "cannot open stdio: invalid handle");
154 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
157 if (qemu_add_wait_object(stdio
->hStdIn
,
158 win_stdio_wait_func
, chr
)) {
159 error_setg(errp
, "qemu_add_wait_object: failed");
165 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
166 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
167 if (stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
168 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
169 error_setg(errp
, "cannot create event");
172 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
173 win_stdio_thread_wait_func
, chr
)) {
174 error_setg(errp
, "qemu_add_wait_object: failed");
177 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
180 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
) {
181 error_setg(errp
, "cannot create stdio thread");
186 dwMode
|= ENABLE_LINE_INPUT
;
189 /* set the terminal in raw mode */
190 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
191 dwMode
|= ENABLE_PROCESSED_INPUT
;
194 SetConsoleMode(stdio
->hStdIn
, dwMode
);
196 qemu_chr_set_echo_win_stdio(chr
, false);
201 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
203 CloseHandle(stdio
->hInputReadyEvent
);
204 CloseHandle(stdio
->hInputDoneEvent
);
206 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
209 static void char_win_stdio_finalize(Object
*obj
)
211 WinStdioChardev
*stdio
= WIN_STDIO_CHARDEV(obj
);
213 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
214 CloseHandle(stdio
->hInputReadyEvent
);
216 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
217 CloseHandle(stdio
->hInputDoneEvent
);
219 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
220 TerminateThread(stdio
->hInputThread
, 0);
224 static int win_stdio_write(Chardev
*chr
, const uint8_t *buf
, int len
)
226 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
233 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
243 static void char_win_stdio_class_init(ObjectClass
*oc
, void *data
)
245 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
247 cc
->open
= qemu_chr_open_stdio
;
248 cc
->chr_write
= win_stdio_write
;
249 cc
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
252 static const TypeInfo char_win_stdio_type_info
= {
253 .name
= TYPE_CHARDEV_WIN_STDIO
,
254 .parent
= TYPE_CHARDEV
,
255 .instance_size
= sizeof(WinStdioChardev
),
256 .instance_finalize
= char_win_stdio_finalize
,
257 .class_init
= char_win_stdio_class_init
,
261 static void register_types(void)
263 type_register_static(&char_win_stdio_type_info
);
266 type_init(register_types
);