|
| 1 | +import sys |
| 2 | + |
| 3 | +try: |
| 4 | + import select, termios |
| 5 | +except ImportError: |
| 6 | + termios = None |
| 7 | + select = None |
| 8 | + import msvcrt |
| 9 | + |
| 10 | + |
| 11 | +class ConsolePosix: |
| 12 | + def __init__(self): |
| 13 | + self.infd = sys.stdin.fileno() |
| 14 | + self.infile = sys.stdin.buffer.raw |
| 15 | + self.outfile = sys.stdout.buffer.raw |
| 16 | + self.orig_attr = termios.tcgetattr(self.infd) |
| 17 | + |
| 18 | + def enter(self): |
| 19 | + # attr is: [iflag, oflag, cflag, lflag, ispeed, ospeed, cc] |
| 20 | + attr = termios.tcgetattr(self.infd) |
| 21 | + attr[0] &= ~( |
| 22 | + termios.BRKINT | termios.ICRNL | termios.INPCK | termios.ISTRIP | termios.IXON |
| 23 | + ) |
| 24 | + attr[1] = 0 |
| 25 | + attr[2] = attr[2] & ~(termios.CSIZE | termios.PARENB) | termios.CS8 |
| 26 | + attr[3] = 0 |
| 27 | + attr[6][termios.VMIN] = 1 |
| 28 | + attr[6][termios.VTIME] = 0 |
| 29 | + termios.tcsetattr(self.infd, termios.TCSANOW, attr) |
| 30 | + |
| 31 | + def exit(self): |
| 32 | + termios.tcsetattr(self.infd, termios.TCSANOW, self.orig_attr) |
| 33 | + |
| 34 | + def waitchar(self): |
| 35 | +
10000
# TODO pyb.serial might not have fd |
| 36 | + select.select([console_in.infd, pyb.serial.fd], [], []) |
| 37 | + |
| 38 | + def readchar(self): |
| 39 | + res = select.select([self.infd], [], [], 0) |
| 40 | + if res[0]: |
| 41 | + return self.infile.read(1) |
| 42 | + else: |
| 43 | + return None |
| 44 | + |
| 45 | + def write(self, buf): |
| 46 | + self.outfile.write(buf) |
| 47 | + |
| 48 | + |
| 49 | +class ConsoleWindows: |
| 50 | + KEY_MAP = { |
| 51 | + b"H": b"A", # UP |
| 52 | + b"P": b"B", # DOWN |
| 53 | + b"M": b"C", # RIGHT |
| 54 | + b"K": b"D", # LEFT |
| 55 | + b"G": b"H", # POS1 |
| 56 | + b"O": b"F", # END |
| 57 | + b"Q": b"6~", # PGDN |
| 58 | + b"I": b"5~", # PGUP |
| 59 | + b"s": b"1;5D", # CTRL-LEFT, |
| 60 | + b"t": b"1;5C", # CTRL-RIGHT, |
| 61 | + b"\x8d": b"1;5A", # CTRL-UP, |
| 62 | + b"\x91": b"1;5B", # CTRL-DOWN, |
| 63 | + b"w": b"1;5H", # CTRL-POS1 |
| 64 | + b"u": b"1;5F", # CTRL-END |
| 65 | + b"\x98": b"1;3A", # ALT-UP, |
| 66 | + b"\xa0": b"1;3B", # ALT-DOWN, |
| 67 | + b"\x9d": b"1;3C", # ALT-RIGHT, |
| 68 | + b"\x9b": b"1;3D", # ALT-LEFT, |
| 69 | + b"\x97": b"1;3H", # ALT-POS1, |
| 70 | + b"\x9f": b"1;3F", # ALT-END, |
| 71 | + b"S": b"3~", # DEL, |
| 72 | + b"\x93": b"3;5~", # CTRL-DEL |
| 73 | + b"R": b"2~", # INS |
| 74 | + b"\x92": b"2;5~", # CTRL-INS |
| 75 | + b"\x94": b"Z", # Ctrl-Tab = BACKTAB, |
| 76 | + } |
| 77 | + |
| 78 | + def enter(self): |
| 79 | + pass |
| 80 | + |
| 81 | + def exit(self): |
| 82 | + pass |
| 83 | + |
| 84 | + def inWaiting(self): |
| 85 | + return 1 if msvcrt.kbhit() else 0 |
| 86 | + |
| 87 | + def waitchar(self): |
| 88 | + while not (self.inWaiting() or pyb.serial.inWaiting()): |
| 89 | + time.sleep(0.01) |
| 90 | + |
| 91 | + def readchar(self): |
| 92 | + if msvcrt.kbhit(): |
| 93 | + ch = msvcrt.getch() |
| 94 | + while ch in b"\x00\xe0": # arrow or function key prefix? |
| 95 | + if not msvcrt.kbhit(): |
| 96 | + return None |
| 97 | + ch = msvcrt.getch() # second call returns the actual key code |
| 98 | + try: |
| 99 | + ch = b"\x1b[" + self.KEY_MAP[ch] |
| 100 | + except KeyError: |
| 101 | + return None |
| 102 | + return ch |
| 103 | + |
| 104 | + def write(self, buf): |
| 105 | + buf = buf.decode() if isinstance(buf, bytes) else buf |
| 106 | + sys.stdout.write(buf) |
| 107 | + sys.stdout.flush() |
| 108 | + # for b in buf: |
| 109 | + # if isinstance(b, bytes): |
| 110 | + # msvcrt.putch(b) |
| 111 | + # else: |
| 112 | + # msvcrt.putwch(b) |
| 113 | + |
| 114 | + |
| 115 | +if termios: |
| 116 | + Console = ConsolePosix |
| 117 | + VT_ENABLED = True |
| 118 | +else: |
| 119 | + Console = ConsoleWindows |
| 120 | + |
| 121 | + # Windows VT mode ( >= win10 only) |
| 122 | + # https://bugs.python.org/msg291732 |
| 123 | + import ctypes |
| 124 | + from ctypes import wintypes |
| 125 | + |
| 126 | + kernel32 = ctypes.WinDLL("kernel32", use_last_error=True) |
| 127 | + |
| 128 | + ERROR_INVALID_PARAMETER = 0x0057 |
| 129 | + ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004 |
| 130 | + |
| 131 | + def _check_bool(result, func, args): |
| 132 | + if not result: |
| 133 | + raise ctypes.WinError(ctypes.get_last_error()) |
| 134 | + return args |
| 135 | + |
| 136 | + LPDWORD = ctypes.POINTER(wintypes.DWORD) |
| 137 | + kernel32.GetConsoleMode.errcheck = _check_bool |
| 138 | + kernel32.GetConsoleMode.argtypes = (wintypes.HANDLE, LPDWORD) |
| 139 | + kernel32.SetConsoleMode.errcheck = _check_bool |
| 140 | + kernel32.SetConsoleMode.argtypes = (wintypes.HANDLE, wintypes.DWORD) |
| 141 | + |
| 142 | + def set_conout_mode(new_mode, mask=0xFFFFFFFF): |
| 143 | + # don't assume StandardOutput is a console. |
| 144 | + # open CONOUT$ instead |
| 145 | + fdout = os.open("CONOUT$", os.O_RDWR) |
| 146 | + try: |
| 147 | + hout = msvcrt.get_osfhandle(fdout) |
| 148 | + old_mode = wintypes.DWORD() |
| 149 | + kernel32.GetConsoleMode(hout, ctypes.byref(old_mode)) |
| 150 | + mode = (new_mode & mask) | (old_mode.value & ~mask) |
| 151 | + kernel32.SetConsoleMode(hout, mode) |
| 152 | + return old_mode.value |
| 153 | + finally: |
| 154 | + os.close(fdout) |
| 155 | + |
| 156 | + # def enable_vt_mode(): |
| 157 | + mode = mask = ENABLE_VIRTUAL_TERMINAL_PROCESSING |
| 158 | + try: |
| 159 | + set_conout_mode(mode, mask) |
| 160 | + VT_ENABLED = True |
| 161 | + except WindowsError as e: |
| 162 | + VT_ENABLED = False |
0 commit comments