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/main-loop.h"
28 #include "qemu/module.h"
29 #include "chardev/char-win.h"
30 #include "chardev/char-win-stdio.h"
31 #include "qom/object.h"
33 struct WinStdioChardev
{
36 HANDLE hInputReadyEvent
;
37 HANDLE hInputDoneEvent
;
39 uint8_t win_stdio_buf
;
41 typedef struct WinStdioChardev WinStdioChardev
;
43 DECLARE_INSTANCE_CHECKER(WinStdioChardev
, WIN_STDIO_CHARDEV
,
44 TYPE_CHARDEV_WIN_STDIO
)
46 static void win_stdio_wait_func(void *opaque
)
48 Chardev
*chr
= CHARDEV(opaque
);
49 WinStdioChardev
*stdio
= WIN_STDIO_CHARDEV(opaque
);
55 ret
= ReadConsoleInput(stdio
->hStdIn
, buf
, ARRAY_SIZE(buf
), &dwSize
);
58 /* Avoid error storm */
59 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
63 for (i
= 0; i
< dwSize
; i
++) {
64 KEY_EVENT_RECORD
*kev
= &buf
[i
].Event
.KeyEvent
;
66 if (buf
[i
].EventType
== KEY_EVENT
&& kev
->bKeyDown
) {
68 if (kev
->uChar
.AsciiChar
!= 0) {
69 for (j
= 0; j
< kev
->wRepeatCount
; j
++) {
70 if (qemu_chr_be_can_write(chr
)) {
71 uint8_t c
= kev
->uChar
.AsciiChar
;
72 qemu_chr_be_write(chr
, &c
, 1);
80 static DWORD WINAPI
win_stdio_thread(LPVOID param
)
82 WinStdioChardev
*stdio
= WIN_STDIO_CHARDEV(param
);
88 /* Wait for one byte */
89 ret
= ReadFile(stdio
->hStdIn
, &stdio
->win_stdio_buf
, 1, &dwSize
, NULL
);
91 /* Exit in case of error, continue if nothing read */
99 /* Some terminal emulator returns \r\n for Enter, just pass \n */
100 if (stdio
->win_stdio_buf
== '\r') {
104 /* Signal the main thread and wait until the byte was eaten */
105 if (!SetEvent(stdio
->hInputReadyEvent
)) {
108 if (WaitForSingleObject(stdio
->hInputDoneEvent
, INFINITE
)
114 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
118 static void win_stdio_thread_wait_func(void *opaque
)
120 Chardev
*chr
= CHARDEV(opaque
);
121 WinStdioChardev
*stdio
= WIN_STDIO_CHARDEV(opaque
);
123 if (qemu_chr_be_can_write(chr
)) {
124 qemu_chr_be_write(chr
, &stdio
->win_stdio_buf
, 1);
127 SetEvent(stdio
->hInputDoneEvent
);
130 static void qemu_chr_set_echo_win_stdio(Chardev
*chr
, bool echo
)
132 WinStdioChardev
*stdio
= WIN_STDIO_CHARDEV(chr
);
135 GetConsoleMode(stdio
->hStdIn
, &dwMode
);
138 SetConsoleMode(stdio
->hStdIn
, dwMode
| ENABLE_ECHO_INPUT
);
140 SetConsoleMode(stdio
->hStdIn
, dwMode
& ~ENABLE_ECHO_INPUT
);
144 static void qemu_chr_open_stdio(Chardev
*chr
,
145 ChardevBackend
*backend
,
149 WinStdioChardev
*stdio
= WIN_STDIO_CHARDEV(chr
);
153 stdio
->hStdIn
= GetStdHandle(STD_INPUT_HANDLE
);
154 if (stdio
->hStdIn
== INVALID_HANDLE_VALUE
) {
155 error_setg(errp
, "cannot open stdio: invalid handle");
159 is_console
= GetConsoleMode(stdio
->hStdIn
, &dwMode
) != 0;
162 if (qemu_add_wait_object(stdio
->hStdIn
,
163 win_stdio_wait_func
, chr
)) {
164 error_setg(errp
, "qemu_add_wait_object: failed");
170 stdio
->hInputReadyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
171 stdio
->hInputDoneEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
172 if (stdio
->hInputReadyEvent
== INVALID_HANDLE_VALUE
173 || stdio
->hInputDoneEvent
== INVALID_HANDLE_VALUE
) {
174 error_setg(errp
, "cannot create event");
177 if (qemu_add_wait_object(stdio
->hInputReadyEvent
,
178 win_stdio_thread_wait_func
, chr
)) {
179 error_setg(errp
, "qemu_add_wait_object: failed");
182 stdio
->hInputThread
= CreateThread(NULL
, 0, win_stdio_thread
,
185 if (stdio
->hInputThread
== INVALID_HANDLE_VALUE
) {
186 error_setg(errp
, "cannot create stdio thread");
191 dwMode
|= ENABLE_LINE_INPUT
;
194 /* set the terminal in raw mode */
195 /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
196 dwMode
|= ENABLE_PROCESSED_INPUT
;
199 SetConsoleMode(stdio
->hStdIn
, dwMode
);
201 qemu_chr_set_echo_win_stdio(chr
, false);
206 qemu_del_wait_object(stdio
->hInputReadyEvent
, NULL
, NULL
);
208 CloseHandle(stdio
->hInputReadyEvent
);
209 CloseHandle(stdio
->hInputDoneEvent
);
211 qemu_del_wait_object(stdio
->hStdIn
, NULL
, NULL
);
214 static void char_win_stdio_finalize(Object
*obj
)
216 WinStdioChardev
*stdio
= WIN_STDIO_CHARDEV(obj
);
218 if (stdio
->hInputReadyEvent
!= INVALID_HANDLE_VALUE
) {
219 CloseHandle(stdio
->hInputReadyEvent
);
221 if (stdio
->hInputDoneEvent
!= INVALID_HANDLE_VALUE
) {
222 CloseHandle(stdio
->hInputDoneEvent
);
224 if (stdio
->hInputThread
!= INVALID_HANDLE_VALUE
) {
225 TerminateThread(stdio
->hInputThread
, 0);
229 static int win_stdio_write(Chardev
*chr
, const uint8_t *buf
, int len
)
231 HANDLE hStdOut
= GetStdHandle(STD_OUTPUT_HANDLE
);
238 if (!WriteFile(hStdOut
, buf
, len1
, &dwSize
, NULL
)) {
248 static void char_win_stdio_class_init(ObjectClass
*oc
, void *data
)
250 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
252 cc
->open
= qemu_chr_open_stdio
;
253 cc
->chr_write
= win_stdio_write
;
254 cc
->chr_set_echo
= qemu_chr_set_echo_win_stdio
;
257 static const TypeInfo char_win_stdio_type_info
= {
258 .name
= TYPE_CHARDEV_WIN_STDIO
,
259 .parent
= TYPE_CHARDEV
,
260 .instance_size
= sizeof(WinStdioChardev
),
261 .instance_finalize
= char_win_stdio_finalize
,
262 .class_init
= char_win_stdio_class_init
,
266 static void register_types(void)
268 type_register_static(&char_win_stdio_type_info
);
271 type_init(register_types
);