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
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu/module.h"
28 #include "chardev/char-win.h"
29 #include "chardev/char-win-stdio.h"
34 HANDLE hInputReadyEvent
;
35 HANDLE hInputDoneEvent
;
37 uint8_t win_stdio_buf
;
40 #define WIN_STDIO_CHARDEV(obj) \
41 OBJECT_CHECK(WinStdioChardev, (obj), TYPE_CHARDEV_WIN_STDIO)
43 static void win_stdio_wait_func(void *opaque
)
45 Chardev
*chr
= CHARDEV(opaque
);
46 WinStdioChardev
*stdio
= WIN_STDIO_CHARDEV(opaque
);
52 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, ARRAY_SIZE(buf
), &dwSize
);
55 /* Avoid error storm */
56 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
60 for (i
= 0; i
< dwSize
; i
++) {
61 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
63 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
65 if (kev
->uChar
.AsciiChar
!= 0) {
66 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
67 if (qemu_chr_be_can_write(chr
)) {
68 uint8_t c
= kev
->uChar
.AsciiChar
;
69 qemu_chr_be_write(chr
, &c
, 1);
77 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
79 WinStdioChardev
*stdio
= WIN_STDIO_CHARDEV(param
);
85 /* Wait for one byte */
86 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
88 /* Exit in case of error, continue if nothing read */
96 /* Some terminal emulator returns \r\n for Enter, just pass \n */
97 if (stdio
->win_stdio_buf
== '\r') {
101 /* Signal the main thread and wait until the byte was eaten */
102 if (!SetEvent(stdio
->hInputReadyEvent
)) {
105 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
111 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
115 static void win_stdio_thread_wait_func(void *opaque
)
117 Chardev
*chr
= CHARDEV(opaque
);
118 WinStdioChardev
*stdio
= WIN_STDIO_CHARDEV(opaque
);
120 if (qemu_chr_be_can_write(chr
)) {
121 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
124 SetEvent(stdio
->hInputDoneEvent
);
127 static void qemu_chr_set_echo_win_stdio(Chardev
*chr
, bool echo
)
129 WinStdioChardev
*stdio
= WIN_STDIO_CHARDEV(chr
);
132 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
135 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
137 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
141 static void qemu_chr_open_stdio(Chardev
*chr
,
142 ChardevBackend
*backend
,
146 WinStdioChardev
*stdio
= WIN_STDIO_CHARDEV(chr
);
150 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
151 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
152 error_setg(errp
, "cannot open stdio: invalid handle");
156 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
159 if (qemu_add_wait_object(stdio
->hStdIn
,
160 win_stdio_wait_func
, chr
)) {
161 error_setg(errp
, "qemu_add_wait_object: failed");
167 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
168 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
169 if (stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
170 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
171 error_setg(errp
, "cannot create event");
174 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
175 win_stdio_thread_wait_func
, chr
)) {
176 error_setg(errp
, "qemu_add_wait_object: failed");
179 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
182 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
) {
183 error_setg(errp
, "cannot create stdio thread");
188 dwMode
|= ENABLE_LINE_INPUT
;
191 /* set the terminal in raw mode */
192 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
193 dwMode
|= ENABLE_PROCESSED_INPUT
;
196 SetConsoleMode(stdio
->hStdIn
, dwMode
);
198 qemu_chr_set_echo_win_stdio(chr
, false);
203 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
205 CloseHandle(stdio
->hInputReadyEvent
);
206 CloseHandle(stdio
->hInputDoneEvent
);
208 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
211 static void char_win_stdio_finalize(Object
*obj
)
213 WinStdioChardev
*stdio
= WIN_STDIO_CHARDEV(obj
);
215 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
216 CloseHandle(stdio
->hInputReadyEvent
);
218 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
219 CloseHandle(stdio
->hInputDoneEvent
);
221 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
222 TerminateThread(stdio
->hInputThread
, 0);
226 static int win_stdio_write(Chardev
*chr
, const uint8_t *buf
, int len
)
228 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
235 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
245 static void char_win_stdio_class_init(ObjectClass
*oc
, void *data
)
247 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
249 cc
->open
= qemu_chr_open_stdio
;
250 cc
->chr_write
= win_stdio_write
;
251 cc
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
254 static const TypeInfo char_win_stdio_type_info
= {
255 .name
= TYPE_CHARDEV_WIN_STDIO
,
256 .parent
= TYPE_CHARDEV
,
257 .instance_size
= sizeof(WinStdioChardev
),
258 .instance_finalize
= char_win_stdio_finalize
,
259 .class_init
= char_win_stdio_class_init
,
263 static void register_types(void)
265 type_register_static(&char_win_stdio_type_info
);
268 type_init(register_types
);