char-win-stdio.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  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:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  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
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qapi/error.h"
  26. #include "qemu/main-loop.h"
  27. #include "qemu/module.h"
  28. #include "chardev/char-win.h"
  29. #include "chardev/char-win-stdio.h"
  30. #include "qom/object.h"
  31. struct WinStdioChardev {
  32. Chardev parent;
  33. HANDLE hStdIn;
  34. HANDLE hInputReadyEvent;
  35. HANDLE hInputDoneEvent;
  36. HANDLE hInputThread;
  37. uint8_t win_stdio_buf;
  38. };
  39. typedef struct WinStdioChardev WinStdioChardev;
  40. DECLARE_INSTANCE_CHECKER(WinStdioChardev, WIN_STDIO_CHARDEV,
  41. TYPE_CHARDEV_WIN_STDIO)
  42. static void win_stdio_wait_func(void *opaque)
  43. {
  44. Chardev *chr = CHARDEV(opaque);
  45. WinStdioChardev *stdio = WIN_STDIO_CHARDEV(opaque);
  46. INPUT_RECORD buf[4];
  47. int ret;
  48. DWORD dwSize;
  49. int i;
  50. ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
  51. if (!ret) {
  52. /* Avoid error storm */
  53. qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
  54. return;
  55. }
  56. for (i = 0; i < dwSize; i++) {
  57. KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
  58. if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
  59. int j;
  60. if (kev->uChar.AsciiChar != 0) {
  61. for (j = 0; j < kev->wRepeatCount; j++) {
  62. if (qemu_chr_be_can_write(chr)) {
  63. uint8_t c = kev->uChar.AsciiChar;
  64. qemu_chr_be_write(chr, &c, 1);
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. static DWORD WINAPI win_stdio_thread(LPVOID param)
  72. {
  73. WinStdioChardev *stdio = WIN_STDIO_CHARDEV(param);
  74. int ret;
  75. DWORD dwSize;
  76. while (1) {
  77. /* Wait for one byte */
  78. ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
  79. /* Exit in case of error, continue if nothing read */
  80. if (!ret) {
  81. break;
  82. }
  83. if (!dwSize) {
  84. continue;
  85. }
  86. /* Some terminal emulator returns \r\n for Enter, just pass \n */
  87. if (stdio->win_stdio_buf == '\r') {
  88. continue;
  89. }
  90. /* Signal the main thread and wait until the byte was eaten */
  91. if (!SetEvent(stdio->hInputReadyEvent)) {
  92. break;
  93. }
  94. if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
  95. != WAIT_OBJECT_0) {
  96. break;
  97. }
  98. }
  99. qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
  100. return 0;
  101. }
  102. static void win_stdio_thread_wait_func(void *opaque)
  103. {
  104. Chardev *chr = CHARDEV(opaque);
  105. WinStdioChardev *stdio = WIN_STDIO_CHARDEV(opaque);
  106. if (qemu_chr_be_can_write(chr)) {
  107. qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
  108. }
  109. SetEvent(stdio->hInputDoneEvent);
  110. }
  111. static void qemu_chr_set_echo_win_stdio(Chardev *chr, bool echo)
  112. {
  113. WinStdioChardev *stdio = WIN_STDIO_CHARDEV(chr);
  114. DWORD dwMode = 0;
  115. GetConsoleMode(stdio->hStdIn, &dwMode);
  116. if (echo) {
  117. SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
  118. } else {
  119. SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
  120. }
  121. }
  122. static void qemu_chr_open_stdio(Chardev *chr,
  123. ChardevBackend *backend,
  124. bool *be_opened,
  125. Error **errp)
  126. {
  127. ChardevStdio *opts = backend->u.stdio.data;
  128. bool stdio_allow_signal = !opts->has_signal || opts->signal;
  129. WinStdioChardev *stdio = WIN_STDIO_CHARDEV(chr);
  130. DWORD dwMode;
  131. int is_console = 0;
  132. stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
  133. if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
  134. error_setg(errp, "cannot open stdio: invalid handle");
  135. return;
  136. }
  137. is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
  138. if (is_console) {
  139. if (qemu_add_wait_object(stdio->hStdIn,
  140. win_stdio_wait_func, chr)) {
  141. error_setg(errp, "qemu_add_wait_object: failed");
  142. goto err1;
  143. }
  144. } else {
  145. DWORD dwId;
  146. stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  147. stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  148. if (stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
  149. || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
  150. error_setg(errp, "cannot create event");
  151. goto err2;
  152. }
  153. if (qemu_add_wait_object(stdio->hInputReadyEvent,
  154. win_stdio_thread_wait_func, chr)) {
  155. error_setg(errp, "qemu_add_wait_object: failed");
  156. goto err2;
  157. }
  158. stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
  159. chr, 0, &dwId);
  160. if (stdio->hInputThread == INVALID_HANDLE_VALUE) {
  161. error_setg(errp, "cannot create stdio thread");
  162. goto err3;
  163. }
  164. }
  165. dwMode |= ENABLE_LINE_INPUT;
  166. if (is_console) {
  167. /* set the terminal in raw mode */
  168. /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
  169. if (stdio_allow_signal) {
  170. dwMode |= ENABLE_PROCESSED_INPUT;
  171. } else {
  172. dwMode &= ~ENABLE_PROCESSED_INPUT;
  173. }
  174. }
  175. SetConsoleMode(stdio->hStdIn, dwMode);
  176. qemu_chr_set_echo_win_stdio(chr, false);
  177. return;
  178. err3:
  179. qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
  180. err2:
  181. CloseHandle(stdio->hInputReadyEvent);
  182. CloseHandle(stdio->hInputDoneEvent);
  183. err1:
  184. qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
  185. }
  186. static void char_win_stdio_finalize(Object *obj)
  187. {
  188. WinStdioChardev *stdio = WIN_STDIO_CHARDEV(obj);
  189. if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
  190. CloseHandle(stdio->hInputReadyEvent);
  191. }
  192. if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
  193. CloseHandle(stdio->hInputDoneEvent);
  194. }
  195. if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
  196. TerminateThread(stdio->hInputThread, 0);
  197. }
  198. }
  199. static int win_stdio_write(Chardev *chr, const uint8_t *buf, int len)
  200. {
  201. HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  202. DWORD dwSize;
  203. int len1;
  204. len1 = len;
  205. while (len1 > 0) {
  206. if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
  207. break;
  208. }
  209. buf += dwSize;
  210. len1 -= dwSize;
  211. }
  212. return len - len1;
  213. }
  214. static void char_win_stdio_class_init(ObjectClass *oc, void *data)
  215. {
  216. ChardevClass *cc = CHARDEV_CLASS(oc);
  217. cc->open = qemu_chr_open_stdio;
  218. cc->chr_write = win_stdio_write;
  219. cc->chr_set_echo = qemu_chr_set_echo_win_stdio;
  220. }
  221. static const TypeInfo char_win_stdio_type_info = {
  222. .name = TYPE_CHARDEV_WIN_STDIO,
  223. .parent = TYPE_CHARDEV,
  224. .instance_size = sizeof(WinStdioChardev),
  225. .instance_finalize = char_win_stdio_finalize,
  226. .class_init = char_win_stdio_class_init,
  227. .abstract = true,
  228. };
  229. static void register_types(void)
  230. {
  231. type_register_static(&char_win_stdio_type_info);
  232. }
  233. type_init(register_types);