| 1 |
# Copyright 2000-2008 Michael Hudson-Doyle <[email protected]> |
| 2 |
# Armin Rigo |
| 3 |
# |
| 4 |
# All Rights Reserved |
| 5 |
# |
| 6 |
# |
| 7 |
# Permission to use, copy, modify, and distribute this software and |
| 8 |
# its documentation for any purpose is hereby granted without fee, |
| 9 |
# provided that the above copyright notice appear in all copies and |
| 10 |
# that both that copyright notice and this permission notice appear in |
| 11 |
# supporting documentation. |
| 12 |
# |
| 13 |
# THE AUTHOR MICHAEL HUDSON DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 14 |
# THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 15 |
# AND FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, |
| 16 |
# INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 17 |
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 18 |
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 19 |
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 20 |
|
| 21 |
""" |
| 22 |
OS-independent base for an event and VT sequence scanner |
| 23 |
|
| 24 |
See unix_eventqueue and windows_eventqueue for subclasses. |
| 25 |
""" |
| 26 |
|
| 27 |
from collections import deque |
| 28 |
|
| 29 |
from . import keymap |
| 30 |
from .console import Event |
| 31 |
from .trace import trace |
| 32 |
|
| 33 |
class BaseEventQueue: |
| 34 |
def __init__(self, encoding: str, keymap_dict: dict[bytes, str]) -> None: |
| 35 |
self.compiled_keymap = keymap.compile_keymap(keymap_dict) |
| 36 |
self.keymap = self.compiled_keymap |
| 37 |
trace("keymap {k!r}", k=self.keymap) |
| 38 |
self.encoding = encoding |
| 39 |
self.events: deque[Event] = deque() |
| 40 |
self.buf = bytearray() |
| 41 |
|
| 42 |
def get(self) -> Event | None: |
| 43 |
""" |
| 44 |
Retrieves the next event from the queue. |
| 45 |
""" |
| 46 |
if self.events: |
| 47 |
return self.events.popleft() |
| 48 |
else: |
| 49 |
return None |
| 50 |
|
| 51 |
def empty(self) -> bool: |
| 52 |
""" |
| 53 |
Checks if the queue is empty. |
| 54 |
""" |
| 55 |
return not self.events |
| 56 |
|
| 57 |
def flush_buf(self) -> bytearray: |
| 58 |
""" |
| 59 |
Flushes the buffer and returns its contents. |
| 60 |
""" |
| 61 |
old = self.buf |
| 62 |
self.buf = bytearray() |
| 63 |
return old |
| 64 |
|
| 65 |
def insert(self, event: Event) -> None: |
| 66 |
""" |
| 67 |
Inserts an event into the queue. |
| 68 |
""" |
| 69 |
trace('added event {event}', event=event) |
| 70 |
self.events.append(event) |
| 71 |
|
| 72 |
def push(self, char: int | bytes) -> None: |
| 73 |
""" |
| 74 |
Processes a character by updating the buffer and handling special key mappings. |
| 75 |
""" |
| 76 |
assert isinstance(char, (int, bytes)) |
| 77 |
ord_char = char if isinstance(char, int) else ord(char) |
| 78 |
char = ord_char.to_bytes() |
| 79 |
self.buf.append(ord_char) |
| 80 |
|
| 81 |
if char in self.keymap: |
| 82 |
if self.keymap is self.compiled_keymap: |
| 83 |
# sanity check, buffer is empty when a special key comes |
| 84 |
assert len(self.buf) == 1 |
| 85 |
k = self.keymap[char] |
| 86 |
trace('found map {k!r}', k=k) |
| 87 |
if isinstance(k, dict): |
| 88 |
self.keymap = k |
| 89 |
else: |
| 90 |
self.insert(Event('key', k, bytes(self.flush_buf()))) |
| 91 |
self.keymap = self.compiled_keymap |
| 92 |
|
| 93 |
elif self.buf and self.buf[0] == 27: # escape |
| 94 |
# escape sequence not recognized by our keymap: propagate it |
| 95 |
# outside so that i can be recognized as an M-... key (see also |
| 96 |
# the docstring in keymap.py |
| 97 |
trace('unrecognized escape sequence, propagating...') |
| 98 |
self.keymap = self.compiled_keymap |
| 99 |
self.insert(Event('key', '\033', b'\033')) |
| 100 |
for _c in self.flush_buf()[1:]: |
| 101 |
self.push(_c) |
| 102 |
|
| 103 |
else: |
| 104 |
try: |
| 105 |
decoded = bytes(self.buf).decode(self.encoding) |
| 106 |
except UnicodeError: |
| 107 |
return |
| 108 |
else: |
| 109 |
self.insert(Event('key', decoded, bytes(self.flush_buf()))) |
| 110 |
self.keymap = self.compiled_keymap |