| 1 |
|
| 2 |
""" |
| 3 |
Windows specific code for the terminal. |
| 4 |
|
| 5 |
Note that the naming convention here is non-pythonic because we are |
| 6 |
matching the Windows API naming. |
| 7 |
""" |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
import ctypes |
| 12 |
import enum |
| 13 |
from ctypes.wintypes import ( |
| 14 |
BOOL as _BOOL, |
| 15 |
CHAR as _CHAR, |
| 16 |
DWORD as _DWORD, |
| 17 |
HANDLE as _HANDLE, |
| 18 |
SHORT as _SHORT, |
| 19 |
UINT as _UINT, |
| 20 |
WCHAR as _WCHAR, |
| 21 |
WORD as _WORD, |
| 22 |
) |
| 23 |
|
| 24 |
_kernel32 = ctypes.windll.Kernel32 |
| 25 |
|
| 26 |
_STD_INPUT_HANDLE = _DWORD(-10) |
| 27 |
_STD_OUTPUT_HANDLE = _DWORD(-11) |
| 28 |
|
| 29 |
|
| 30 |
class WindowsConsoleModeFlags(enum.IntFlag): |
| 31 |
ENABLE_ECHO_INPUT = 0x0004 |
| 32 |
ENABLE_EXTENDED_FLAGS = 0x0080 |
| 33 |
ENABLE_INSERT_MODE = 0x0020 |
| 34 |
ENABLE_LINE_INPUT = 0x0002 |
| 35 |
ENABLE_MOUSE_INPUT = 0x0010 |
| 36 |
ENABLE_PROCESSED_INPUT = 0x0001 |
| 37 |
ENABLE_QUICK_EDIT_MODE = 0x0040 |
| 38 |
ENABLE_WINDOW_INPUT = 0x0008 |
| 39 |
ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200 |
| 40 |
|
| 41 |
ENABLE_PROCESSED_OUTPUT = 0x0001 |
| 42 |
ENABLE_WRAP_AT_EOL_OUTPUT = 0x0002 |
| 43 |
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004 |
| 44 |
DISABLE_NEWLINE_AUTO_RETURN = 0x0008 |
| 45 |
ENABLE_LVB_GRID_WORLDWIDE = 0x0010 |
| 46 |
|
| 47 |
def __str__(self) -> str: |
| 48 |
return f'{self.name} (0x{self.value:04X})' |
| 49 |
|
| 50 |
|
| 51 |
_GetConsoleMode = _kernel32.GetConsoleMode |
| 52 |
_GetConsoleMode.restype = _BOOL |
| 53 |
|
| 54 |
_SetConsoleMode = _kernel32.SetConsoleMode |
| 55 |
_SetConsoleMode.restype = _BOOL |
| 56 |
|
| 57 |
_GetStdHandle = _kernel32.GetStdHandle |
| 58 |
_GetStdHandle.restype = _HANDLE |
| 59 |
|
| 60 |
_ReadConsoleInput = _kernel32.ReadConsoleInputA |
| 61 |
_ReadConsoleInput.restype = _BOOL |
| 62 |
|
| 63 |
_h_console_input = _GetStdHandle(_STD_INPUT_HANDLE) |
| 64 |
_input_mode = _DWORD() |
| 65 |
_GetConsoleMode(_HANDLE(_h_console_input), ctypes.byref(_input_mode)) |
| 66 |
|
| 67 |
_h_console_output = _GetStdHandle(_STD_OUTPUT_HANDLE) |
| 68 |
_output_mode = _DWORD() |
| 69 |
_GetConsoleMode(_HANDLE(_h_console_output), ctypes.byref(_output_mode)) |
| 70 |
|
| 71 |
|
| 72 |
class _COORD(ctypes.Structure): |
| 73 |
_fields_ = (('X', _SHORT), ('Y', _SHORT)) |
| 74 |
|
| 75 |
|
| 76 |
class _FOCUS_EVENT_RECORD(ctypes.Structure): |
| 77 |
_fields_ = (('bSetFocus', _BOOL),) |
| 78 |
|
| 79 |
|
| 80 |
class _KEY_EVENT_RECORD(ctypes.Structure): |
| 81 |
class _uchar(ctypes.Union): |
| 82 |
_fields_ = (('UnicodeChar', _WCHAR), ('AsciiChar', _CHAR)) |
| 83 |
|
| 84 |
_fields_ = ( |
| 85 |
('bKeyDown', _BOOL), |
| 86 |
('wRepeatCount', _WORD), |
| 87 |
('wVirtualKeyCode', _WORD), |
| 88 |
('wVirtualScanCode', _WORD), |
| 89 |
('uChar', _uchar), |
| 90 |
('dwControlKeyState', _DWORD), |
| 91 |
) |
| 92 |
|
| 93 |
|
| 94 |
class _MENU_EVENT_RECORD(ctypes.Structure): |
| 95 |
_fields_ = (('dwCommandId', _UINT),) |
| 96 |
|
| 97 |
|
| 98 |
class _MOUSE_EVENT_RECORD(ctypes.Structure): |
| 99 |
_fields_ = ( |
| 100 |
('dwMousePosition', _COORD), |
| 101 |
('dwButtonState', _DWORD), |
| 102 |
('dwControlKeyState', _DWORD), |
| 103 |
('dwEventFlags', _DWORD), |
| 104 |
) |
| 105 |
|
| 106 |
|
| 107 |
class _WINDOW_BUFFER_SIZE_RECORD(ctypes.Structure): |
| 108 |
_fields_ = (('dwSize', _COORD),) |
| 109 |
|
| 110 |
|
| 111 |
class _INPUT_RECORD(ctypes.Structure): |
| 112 |
class _Event(ctypes.Union): |
| 113 |
_fields_ = ( |
| 114 |
('KeyEvent', _KEY_EVENT_RECORD), |
| 115 |
('MouseEvent', _MOUSE_EVENT_RECORD), |
| 116 |
('WindowBufferSizeEvent', _WINDOW_BUFFER_SIZE_RECORD), |
| 117 |
('MenuEvent', _MENU_EVENT_RECORD), |
| 118 |
('FocusEvent', _FOCUS_EVENT_RECORD), |
| 119 |
) |
| 120 |
|
| 121 |
_fields_ = (('EventType', _WORD), ('Event', _Event)) |
| 122 |
|
| 123 |
|
| 124 |
def reset_console_mode() -> None: |
| 125 |
_SetConsoleMode(_HANDLE(_h_console_input), _DWORD(_input_mode.value)) |
| 126 |
_SetConsoleMode(_HANDLE(_h_console_output), _DWORD(_output_mode.value)) |
| 127 |
|
| 128 |
|
| 129 |
def set_console_mode() -> bool: |
| 130 |
mode = ( |
| 131 |
_input_mode.value |
| 132 |
| WindowsConsoleModeFlags.ENABLE_VIRTUAL_TERMINAL_INPUT |
| 133 |
) |
| 134 |
_SetConsoleMode(_HANDLE(_h_console_input), _DWORD(mode)) |
| 135 |
|
| 136 |
mode = ( |
| 137 |
_output_mode.value |
| 138 |
| WindowsConsoleModeFlags.ENABLE_PROCESSED_OUTPUT |
| 139 |
| WindowsConsoleModeFlags.ENABLE_VIRTUAL_TERMINAL_PROCESSING |
| 140 |
) |
| 141 |
return bool(_SetConsoleMode(_HANDLE(_h_console_output), _DWORD(mode))) |
| 142 |
|
| 143 |
|
| 144 |
def get_console_mode() -> int: |
| 145 |
return _input_mode.value |
| 146 |
|
| 147 |
|
| 148 |
def set_text_color(color) -> None: |
| 149 |
_kernel32.SetConsoleTextAttribute(_h_console_output, color) |
| 150 |
|
| 151 |
|
| 152 |
def print_color(text, color) -> None: |
| 153 |
set_text_color(color) |
| 154 |
print(text) |
| 155 |
set_text_color(7) |
| 156 |
|
| 157 |
|
| 158 |
def getch(): |
| 159 |
lp_buffer = (_INPUT_RECORD * 2)() |
| 160 |
n_length = _DWORD(2) |
| 161 |
lp_number_of_events_read = _DWORD() |
| 162 |
|
| 163 |
_ReadConsoleInput( |
| 164 |
_HANDLE(_h_console_input), |
| 165 |
lp_buffer, |
| 166 |
n_length, |
| 167 |
ctypes.byref(lp_number_of_events_read), |
| 168 |
) |
| 169 |
|
| 170 |
char = lp_buffer[1].Event.KeyEvent.uChar.AsciiChar.decode('ascii') |
| 171 |
if char == '\x00': |
| 172 |
return None |
| 173 |
|
| 174 |
return char |