Make some events (and especially macros) faster by caching BareEvents and many key events in RAM

This commit is contained in:
Josh Klar
2018-10-08 05:04:06 -07:00
parent c0b78fe3f2
commit 0d847f99ef
4 changed files with 106 additions and 60 deletions

View File

@@ -2,10 +2,20 @@ 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, Macro, Modifiers
from kmk.common.macros.simple import simple_key_sequence
from kmk.common.macros.simple import lookup_kc_with_cache, simple_key_sequence
from kmk.common.util import get_wide_ordinal
IBUS_KEY_COMBO = Modifiers.KC_LCTRL(Modifiers.KC_LSHIFT(Common.KC_U))
IBUS_KEY_DOWN = keycode_down_event(IBUS_KEY_COMBO)
IBUS_KEY_UP = keycode_up_event(IBUS_KEY_COMBO)
RALT_DOWN = keycode_down_event(Modifiers.KC_RALT)
RALT_UP = keycode_up_event(Modifiers.KC_RALT)
U_DOWN = keycode_down_event(Common.KC_U)
U_UP = keycode_up_event(Common.KC_U)
ENTER_DOWN = keycode_down_event(Common.KC_ENTER)
ENTER_UP = keycode_up_event(Common.KC_ENTER)
RALT_DOWN_NO_RELEASE = keycode_down_event(Modifiers.KC_RALT(no_release=True))
RALT_UP_NO_PRESS = keycode_up_event(Modifiers.KC_RALT(no_press=True))
def generate_codepoint_keysym_seq(codepoint):
@@ -20,7 +30,7 @@ def generate_codepoint_keysym_seq(codepoint):
seq = [Common.KC_0 for _ in range(max(len(codepoint), 4))]
for idx, codepoint_fragment in enumerate(reversed(codepoint)):
seq[-(idx + 1)] = getattr(Common, 'KC_{}'.format(codepoint_fragment.upper()))
seq[-(idx + 1)] = lookup_kc_with_cache(codepoint_fragment)
return seq
@@ -37,45 +47,63 @@ def unicode_string_sequence(unistring):
def unicode_codepoint_sequence(codepoints):
kc_seqs = (
generate_codepoint_keysym_seq(codepoint)
for codepoint in codepoints
)
kc_macros = [
simple_key_sequence(kc_seq)
for kc_seq in kc_seqs
]
def _unicode_sequence(state):
if state.unicode_mode == UnicodeModes.IBUS:
yield from _ibus_unicode_sequence(codepoints, state)
yield from _ibus_unicode_sequence(kc_macros, state)
elif state.unicode_mode == UnicodeModes.RALT:
yield from _ralt_unicode_sequence(codepoints, state)
yield from _ralt_unicode_sequence(kc_macros, state)
elif state.unicode_mode == UnicodeModes.WINC:
yield from _winc_unicode_sequence(codepoints, state)
yield from _winc_unicode_sequence(kc_macros, state)
return Macro(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 _ralt_unicode_sequence(kc_macros, state):
for kc_macro in kc_macros:
yield RALT_DOWN_NO_RELEASE
yield hid_report_event
yield from kc_macro.keydown(state)
yield RALT_UP_NO_PRESS
yield hid_report_event
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)
def _ibus_unicode_sequence(kc_macros, state):
for kc_macro in kc_macros:
yield IBUS_KEY_DOWN
yield hid_report_event
yield IBUS_KEY_UP
yield hid_report_event
yield from kc_macro.keydown(state)
yield ENTER_DOWN
yield hid_report_event
yield ENTER_UP
yield hid_report_event
def _winc_unicode_sequence(codepoints, state):
def _winc_unicode_sequence(kc_macros, state):
'''
Send unicode sequence using WinCompose:
http://wincompose.info/
https://github.com/SamHocevar/wincompose
'''
for codepoint in codepoints:
yield keycode_down_event(Modifiers.RALT())
yield keycode_down_event(Common.KC_U())
yield from simple_key_sequence(generate_codepoint_keysym_seq(codepoint)).keydown(state)
for kc_macro in kc_macros:
yield RALT_DOWN
yield hid_report_event
yield RALT_UP
yield hid_report_event
yield U_DOWN
yield hid_report_event
yield U_UP
yield hid_report_event
yield from kc_macro.keydown(state)