Skip to content

Commit 87ad80e

Browse files
stinospfalcon
authored andcommitted
windows: Implement the mp_hal_xxx functions and enable mp-readline
1 parent 567b349 commit 87ad80e

File tree

6 files changed

+235
-5
lines changed

6 files changed

+235
-5
lines changed

windows/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ SRC_C = \
3131
unix/main.c \
3232
unix/file.c \
3333
unix/input.c \
34-
unix/unix_mphal.c \
3534
unix/modos.c \
3635
unix/modtime.c \
3736
unix/gccollect.c \
37+
windows_mphal.c \
3838
realpath.c \
3939
init.c \
4040
sleep.c \
@@ -43,6 +43,9 @@ OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
4343

4444
ifeq ($(MICROPY_USE_READLINE),1)
4545
CFLAGS_MOD += -DMICROPY_USE_READLINE=1
46+
SRC_C += lib/mp-readline/readline.c
47+
else ifeq ($(MICROPY_USE_READLINE),2)
48+
CFLAGS_MOD += -DMICROPY_USE_READLINE=2
4649
LDFLAGS_MOD += -lreadline
4750
# the following is needed for BSD
4851
#LDFLAGS_MOD += -ltermcap

windows/mpconfigport.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626

2727
// options to control how Micro Python is built
2828

29-
// Linking with GNU readline causes binary to be licensed under GPL
29+
// Linking with GNU readline (MICROPY_USE_READLINE == 2) causes binary to be licensed under GPL
3030
#ifndef MICROPY_USE_READLINE
31-
#define MICROPY_USE_READLINE (0)
31+
#define MICROPY_USE_READLINE (1)
3232
#endif
3333

3434
#define MICROPY_ALLOC_PATH_MAX (260) //see minwindef.h for msvc or limits.h for mingw
@@ -43,6 +43,7 @@
4343
#define MICROPY_MALLOC_USES_ALLOCATED_SIZE (1)
4444
#define MICROPY_MEM_STATS (1)
4545
#define MICROPY_DEBUG_PRINTERS (1)
46+
#define MICROPY_USE_READLINE_HISTORY (1)
4647
#define MICROPY_HELPER_REPL (1)
4748
#define MICROPY_HELPER_LEXER_UNIX (1)
4849
#define MICROPY_ENABLE_SOURCE_LINE (1)
@@ -139,6 +140,15 @@ extern const struct _mp_obj_module_t mp_module_time;
139140
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&mp_module_time }, \
140141
{ MP_OBJ_NEW_QSTR(MP_QSTR__os), (mp_obj_t)&mp_module_os }, \
141142

143+
#if MICROPY_USE_READLINE == 1
144+
#define MICROPY_PORT_ROOT_POINTERS \
145+
char *readline_hist[50];
146+
#endif
147+
148+
#define MP_STATE_PORT MP_STATE_VM
149+
150+
#define MICROPY_HAL_H "windows_mphal.h"
151+
142152
// We need to provide a declaration/definition of alloca()
143153
#include <malloc.h>
144154

windows/mpconfigport.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
MICROPY_FORCE_32BIT = 0
55

66
# Linking with GNU readline causes binary to be licensed under GPL
7-
MICROPY_USE_READLINE = 0
7+
MICROPY_USE_READLINE = 1
88

99
# Subset of CPython time module
1010
MICROPY_PY_TIME = 1

windows/msvc/sources.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
<ItemGroup>
55
<ClCompile Include="$(PyBaseDir)py\*.c" />
66
<ClCompile Include="$(PyBaseDir)extmod\*.c" />
7-
<ClCompile Include="$(PyBaseDir)unix\*.c" Exclude="$(PyBaseDir)unix\alloc.c;$(PyBaseDir)unix\modffi.c;$(PyBaseDir)unix\modsocket.c;$(PyBaseDir)unix\modtermios.c;$(PyBaseDir)unix\seg_helpers.c" />
7+
<ClCompile Include="$(PyBaseDir)unix\*.c" Exclude="$(PyBaseDir)unix\alloc.c;$(PyBaseDir)unix\modffi.c;$(PyBaseDir)unix\modsocket.c;$(PyBaseDir)unix\modtermios.c;$(PyBaseDir)unix\seg_helpers.c;$(PyBaseDir)unix\unix_mphal.c" />
88
<ClCompile Include="$(PyBaseDir)windows\*.c" />
99
<ClCompile Include="$(PyBaseDir)windows\msvc\*.c" />
10+
<ClCompile Include="$(PyBaseDir)lib\mp-readline\*.c" />
1011
</ItemGroup>
1112
<ItemGroup>
1213
<ClInclude Include="$(PyBaseDir)py\*.h" />

windows/windows_mphal.c

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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+
}

windows/windows_mphal.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
#include "unix/unix_mphal.h"
28+
29+
#define MICROPY_HAL_HAS_VT100 (0)
30+
31+
void mp_hal_move_cursor_back(unsigned int pos);
32+
void mp_hal_erase_line_from_cursor();

0 commit comments

Comments
 (0)