Massive refactor largely to support Unicode on Mac
This does a bunch of crazy stuff: - The ability to set a unicode mode (right now only Linux+ibus or MacOS-RALT) in the keymap. This will be changeable at runtime soon, to allow a single keyboard to be able to send table flips and whatever other crazy stuff on any OS the board is plugged into (something that's not currently doable on QMK, so yay us?) - As part of the above, there is now just one user-facing macro for unicode codepoint submission, `kmk.common.macros.unicode.unicode_sequence`. Users should never use the platform-specific macros, partly because they just outright won't work. There's all sorts of fun stuff in these methods now, thank goodness MicroPython supports the `yield from` construct. - Keycode (these should really be renamed Keysym or something) objects that are intended to not be pressed, or not be released. Right now these properties are completely ignored if not part of a macro, and it's probably sane to keep it that way. This was necessary to support MacOS's "hold RALT while typing the codepoint characters" flow. - Other refactor-y bits, like moving macro support to `kmk/common` rather than sitting at the top level of the tree. One day `kmk/common` may make sense to surface at top level `kmk/`, but that's a discussion for another day.
This commit is contained in:
10
kmk/common/macros/__init__.py
Normal file
10
kmk/common/macros/__init__.py
Normal file
@@ -0,0 +1,10 @@
|
||||
class KMKMacro:
|
||||
def __init__(self, keydown=None, keyup=None):
|
||||
self.keydown = keydown
|
||||
self.keyup = keyup
|
||||
|
||||
def on_keydown(self):
|
||||
return self.keydown() if self.keydown else None
|
||||
|
||||
def on_keyup(self):
|
||||
return self.keyup() if self.keyup else None
|
17
kmk/common/macros/simple.py
Normal file
17
kmk/common/macros/simple.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from kmk.common.event_defs import (hid_report_event, keycode_down_event,
|
||||
keycode_up_event)
|
||||
from kmk.common.macros import KMKMacro
|
||||
|
||||
|
||||
def simple_key_sequence(seq):
|
||||
def _simple_key_sequence(state):
|
||||
for key in seq:
|
||||
if not getattr(key, 'no_press', None):
|
||||
yield keycode_down_event(key)
|
||||
yield hid_report_event()
|
||||
|
||||
if not getattr(key, 'no_release', None):
|
||||
yield keycode_up_event(key)
|
||||
yield hid_report_event()
|
||||
|
||||
return KMKMacro(keydown=_simple_key_sequence)
|
45
kmk/common/macros/unicode.py
Normal file
45
kmk/common/macros/unicode.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from kmk.common.consts import UnicodeModes
|
||||
from kmk.common.event_defs import (hid_report_event, keycode_down_event,
|
||||
keycode_up_event)
|
||||
from kmk.common.keycodes import Common, Modifiers
|
||||
from kmk.common.macros import KMKMacro
|
||||
from kmk.common.macros.simple import simple_key_sequence
|
||||
|
||||
IBUS_KEY_COMBO = Modifiers.KC_LCTRL(Modifiers.KC_LSHIFT(Common.KC_U))
|
||||
|
||||
|
||||
def generate_codepoint_keysym_seq(codepoint):
|
||||
return [
|
||||
getattr(Common, 'KC_{}'.format(codepoint_fragment.upper()))
|
||||
for codepoint_fragment in codepoint
|
||||
]
|
||||
|
||||
|
||||
def unicode_sequence(codepoints):
|
||||
def _unicode_sequence(state):
|
||||
if state.unicode_mode == UnicodeModes.IBUS:
|
||||
yield from _ibus_unicode_sequence(codepoints, state)
|
||||
elif state.unicode_mode == UnicodeModes.RALT:
|
||||
yield from _ralt_unicode_sequence(codepoints, state)
|
||||
|
||||
return KMKMacro(keydown=_unicode_sequence)
|
||||
|
||||
|
||||
def _ralt_unicode_sequence(codepoints, state):
|
||||
for codepoint in codepoints:
|
||||
yield keycode_down_event(Modifiers.RALT(no_release=True))
|
||||
yield from simple_key_sequence(generate_codepoint_keysym_seq(codepoint)).keydown(state)
|
||||
yield keycode_up_event(Modifiers.RALT(no_press=True))
|
||||
|
||||
|
||||
def _ibus_unicode_sequence(codepoints, state):
|
||||
for codepoint in codepoints:
|
||||
yield keycode_down_event(IBUS_KEY_COMBO)
|
||||
yield hid_report_event()
|
||||
yield keycode_up_event(IBUS_KEY_COMBO)
|
||||
yield hid_report_event()
|
||||
|
||||
seq = generate_codepoint_keysym_seq(codepoint)
|
||||
seq.append(Common.KC_ENTER)
|
||||
|
||||
yield from simple_key_sequence(seq).keydown(state)
|
Reference in New Issue
Block a user