|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2015 Damien P. George |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | + |
| 28 | +#include "py/mpstate.h" |
| 29 | + |
| 30 | +#include MICROPY_HAL_H |
| 31 | +#include <windows.h> |
| 32 | +#include <unistd.h> |
| 33 | + |
| 34 | +HANDLE std_in = NULL; |
| 35 | +HANDLE con_out = NULL; |
| 36 | +DWORD orig_mode = 0; |
| 37 | + |
| 38 | +STATIC void assure_stdin_handle() { |
| 39 | + if (!std_in) { |
| 40 | + std_in = GetStdHandle(STD_INPUT_HANDLE); |
| 41 | + assert(std_in != INVALID_HANDLE_VALUE); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +STATIC void assure_conout_handle() { |
| 46 | + if (!con_out) { |
| 47 | + con_out = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE, |
| 48 | + FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 49 | + NULL, OPEN_EXISTING, 0, 0); |
| 50 | + assert(con_out != INVALID_HANDLE_VALUE); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +void mp_hal_stdio_mode_raw(void) { |
| 55 | + assure_stdin_handle(); |
| 56 | + GetConsoleMode(std_in, &orig_mode); |
| 57 | + DWORD mode = orig_mode; |
| 58 | + mode &= ~ENABLE_ECHO_INPUT; |
| 59 | + mode &= ~ENABLE_LINE_INPUT; |
| 60 | + mode &= ~ENABLE_PROCESSED_INPUT; |
| 61 | + SetConsoleMode(std_in, mode); |
| 62 | +} |
| 63 | + |
| 64 | +void mp_hal_stdio_mode_orig(void) { |
| 65 | + assure_stdin_handle(); |
| 66 | + SetConsoleMode(std_in, orig_mode); |
| 67 | +} |
| 68 | + |
| 69 | +void mp_hal_set_interrupt_char(char c) { |
| 70 | + assure_stdin_handle(); |
| 71 | + if (c == CHAR_CTRL_C) { |
| 72 | + DWORD mode; |
| 73 | + GetConsoleMode(std_in, &mode); |
| 74 | + mode |= ENABLE_PROCESSED_INPUT; |
| 75 | + SetConsoleMode(std_in, mode); |
| 76 | + } else { |
| 77 | + DWORD mode; |
| 78 | + GetConsoleMode(std_in, &mode); |
| 79 | + mode &= ~ENABLE_PROCESSED_INPUT; |
| 80 | + SetConsoleMode(std_in, mode); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +void mp_hal_move_cursor_back(uint pos) { |
| 85 | + assure_conout_handle(); |
| 86 | + CONSOLE_SCREEN_BUFFER_INFO info; |
| 87 | + GetConsoleScreenBufferInfo(con_out, &info); |
| 88 | + info.dwCursorPosition.X -= (short)pos; |
| 89 | + if (info.dwCursorPosition.X < 0) { |
| 90 | + info.dwCursorPosition.X = 0; |
| 91 | + } |
| 92 | + SetConsoleCursorPosition(con_out, info.dwCursorPosition); |
| 93 | +} |
| 94 | + |
| 95 | +void mp_hal_erase_line_from_cursor() { |
| 96 | + assure_conout_handle(); |
| 97 | + CONSOLE_SCREEN_BUFFER_INFO info; |
| 98 | + GetConsoleScreenBufferInfo(con_out, &info); |
| 99 | + const short len = info.dwSize.X - info.dwCursorPosition.X; |
| 100 | + DWORD written; |
| 101 | + FillConsoleOutputCharacter(con_out, ' ', len, info.dwCursorPosition, &written); |
| 102 | + FillConsoleOutputAttribute(con_out, info.wAttributes, len, info.dwCursorPosition, &written); |
| 103 | +} |
| 104 | + |
| 105 | +typedef struct item_t { |
| 106 | + WORD vkey; |
| 107 | + const char *seq; |
| 108 | +} item_t; |
| 109 | + |
| 110 | +// map virtual key codes to VT100 escape sequences |
| 111 | +STATIC item_t keyCodeMap[] = { |
| 112 | + {VK_UP, "[A"}, |
| 113 | + {VK_DOWN, "[B"}, |
| 114 | + {VK_RIGHT, "[C"}, |
| 115 | + {VK_LEFT, "[D"}, |
| 116 | + {VK_HOME, "[H"}, |
| 117 | + {VK_END, "[F"}, |
| 118 | + {VK_DELETE, "[3~"}, |
| 119 | + {0, ""} //sentinel |
| 120 | +}; |
| 121 | + |
| 122 | +STATIC const char *cur_esc_seq = NULL; |
| 123 | + |
| 124 | +STATIC int esc_seq_process_vk(int vk) { |
| 125 | + for (item_t *p = keyCodeMap; p->vkey != 0; ++p) { |
| 126 | + if (p->vkey == vk) { |
| 127 | + cur_esc_seq = p->seq; |
| 128 | + return 27; // ESC, start of escape sequence |
| 129 | + } |
| 130 | + } |
| 131 | + return 0; // nothing found |
| 132 | +} |
| 133 | + |
| 134 | +STATIC int esc_seq_chr() { |
| 135 | + if (cur_esc_seq) { |
| 136 | + const char c = *cur_esc_seq++; |
| 137 | + if (c) { |
| 138 | + return c; |
| 139 | + } |
| 140 | + cur_esc_seq = NULL; |
| 141 | + } |
| 142 | + return 0; |
| 143 | +} |
| 144 | + |
| 145 | +int mp_hal_stdin_rx_chr(void) { |
| 146 | + // currently processing escape seq? |
| 147 | + const int ret = esc_seq_chr(); |
| 148 | + if (ret) { |
| 149 | + return ret; |
| 150 | + } |
| 151 | + |
| 152 | + // poll until key which we handle is pressed |
| 153 | + assure_stdin_handle(); |
| 154 | + DWORD num_read; |
| 155 | + INPUT_RECORD rec; |
| 156 | + for (;;) { |
| 157 | + if (!ReadConsoleInput(std_in, &rec, 1, &num_read) || !num_read) { |
| 158 | + return CHAR_CTRL_C; // EOF, ctrl-D |
| 159 | + } |
| 160 | + if (rec.EventType != KEY_EVENT || !rec.Event.KeyEvent.bKeyDown) { // only want key down events |
| 161 | + continue; |
| 162 | + } |
| 163 | + const char c = rec.Event.KeyEvent.uChar.AsciiChar; |
| 164 | + if (c) { // plain ascii char, return it |
| 165 | + return c; |
| 166 | + } |
| 167 | + const int ret = esc_seq_process_vk(rec.Event.KeyEvent.wVirtualKeyCode); |
| 168 | + if (ret) { |
| 169 | + return ret; |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { |
| 175 | + write(1, str, len); |
| 176 | +} |
| 177 | + |
| 178 | +void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) { |
| 179 | + mp_hal_stdout_tx_strn(str, len); |
| 180 | +} |
| 181 | + |
| 182 | +void mp_hal_stdout_tx_str(const char *str) { |
| 183 | + mp_hal_stdout_tx_strn(str, strlen(str)); |
| 184 | +} |
0 commit comments