2018-10-19 10:49:37 +02:00
|
|
|
from kmk.consts import LeaderMode
|
2018-12-30 00:29:11 +01:00
|
|
|
from kmk.keys import KC
|
2018-12-29 13:44:52 +01:00
|
|
|
from kmk.kmktime import ticks_ms
|
|
|
|
from kmk.types import TapDanceKeyMeta
|
2018-10-19 08:24:19 +02:00
|
|
|
from kmk.util import intify_coordinate
|
2018-10-06 12:58:19 +02:00
|
|
|
|
|
|
|
|
2018-09-03 22:50:12 +02:00
|
|
|
class InternalState:
|
2018-10-01 09:31:45 +02:00
|
|
|
keys_pressed = set()
|
2018-10-19 08:24:19 +02:00
|
|
|
coord_keys_pressed = {}
|
2018-10-08 11:31:30 +02:00
|
|
|
leader_pending = None
|
2018-10-08 11:51:31 +02:00
|
|
|
leader_last_len = 0
|
2018-10-01 09:31:45 +02:00
|
|
|
hid_pending = False
|
2018-10-08 11:31:30 +02:00
|
|
|
leader_mode_history = []
|
2019-07-13 00:31:27 +02:00
|
|
|
|
|
|
|
# this should almost always be PREpended to, replaces
|
|
|
|
# former use of reversed_active_layers which had pointless
|
|
|
|
# overhead (the underlying list was never used anyway)
|
2018-09-22 02:22:03 +02:00
|
|
|
active_layers = [0]
|
2019-07-13 00:31:27 +02:00
|
|
|
|
2018-09-28 23:35:52 +02:00
|
|
|
start_time = {
|
|
|
|
'lt': None,
|
|
|
|
'tg': None,
|
|
|
|
'tt': None,
|
2018-10-08 11:31:30 +02:00
|
|
|
'lm': None,
|
|
|
|
'leader': None,
|
2018-09-28 23:35:52 +02:00
|
|
|
}
|
2018-10-19 10:49:37 +02:00
|
|
|
timeouts = {}
|
2018-10-19 12:04:54 +02:00
|
|
|
tapping = False
|
|
|
|
tap_dance_counts = {}
|
2018-10-21 01:55:17 +02:00
|
|
|
tap_side_effects = {}
|
2018-09-22 09:46:14 +02:00
|
|
|
|
2018-10-16 13:04:39 +02:00
|
|
|
def __init__(self, config):
|
|
|
|
self.config = config
|
2018-09-23 06:49:58 +02:00
|
|
|
|
2018-10-16 13:04:39 +02:00
|
|
|
def __repr__(self):
|
2019-07-13 00:16:33 +02:00
|
|
|
return (
|
|
|
|
'InternalState('
|
|
|
|
'keys_pressed={} '
|
|
|
|
'coord_keys_pressed={} '
|
|
|
|
'leader_pending={} '
|
|
|
|
'leader_last_len={} '
|
|
|
|
'hid_pending={} '
|
|
|
|
'leader_mode_history={} '
|
|
|
|
'active_layers={} '
|
|
|
|
'start_time={} '
|
|
|
|
'timeouts={} '
|
|
|
|
'tapping={} '
|
|
|
|
'tap_dance_counts={} '
|
|
|
|
'tap_side_effects={}'
|
|
|
|
')'
|
|
|
|
).format(
|
|
|
|
self.keys_pressed,
|
|
|
|
self.coord_keys_pressed,
|
|
|
|
self.leader_pending,
|
|
|
|
self.leader_last_len,
|
|
|
|
self.hid_pending,
|
|
|
|
self.leader_mode_history,
|
|
|
|
self.active_layers,
|
|
|
|
self.start_time,
|
|
|
|
self.timeouts,
|
|
|
|
self.tapping,
|
|
|
|
self.tap_dance_counts,
|
|
|
|
self.tap_side_effects,
|
|
|
|
)
|
2018-09-22 09:46:14 +02:00
|
|
|
|
2018-10-16 13:04:39 +02:00
|
|
|
def _find_key_in_map(self, row, col):
|
2019-05-13 02:04:23 +02:00
|
|
|
ic = intify_coordinate(row, col)
|
|
|
|
|
|
|
|
try:
|
|
|
|
idx = self.config.coord_mapping.index(ic)
|
|
|
|
except ValueError:
|
|
|
|
if self.config.debug_enabled:
|
|
|
|
print(
|
2019-07-13 00:16:33 +02:00
|
|
|
'CoordMappingNotFound(ic={}, row={}, col={})'.format(
|
2019-05-13 02:04:23 +02:00
|
|
|
ic,
|
|
|
|
row,
|
|
|
|
col,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2019-07-13 00:31:27 +02:00
|
|
|
for layer in self.active_layers:
|
2019-05-13 02:04:23 +02:00
|
|
|
layer_key = self.config.keymap[layer][idx]
|
2018-09-03 22:50:12 +02:00
|
|
|
|
2018-12-29 13:44:52 +01:00
|
|
|
if not layer_key or layer_key == KC.TRNS:
|
2018-10-16 13:04:39 +02:00
|
|
|
continue
|
2018-09-03 22:50:12 +02:00
|
|
|
|
2018-10-19 08:24:19 +02:00
|
|
|
if self.config.debug_enabled:
|
2019-07-13 00:16:33 +02:00
|
|
|
print('KeyResolution(key={})'.format(layer_key))
|
2018-09-23 06:49:58 +02:00
|
|
|
|
2018-10-16 13:04:39 +02:00
|
|
|
return layer_key
|
2018-09-23 06:49:58 +02:00
|
|
|
|
2018-10-19 10:49:37 +02:00
|
|
|
def set_timeout(self, after_ticks, callback):
|
2018-10-21 02:53:58 +02:00
|
|
|
if after_ticks is False:
|
|
|
|
# We allow passing False as an implicit "run this on the next process timeouts cycle"
|
|
|
|
timeout_key = ticks_ms()
|
|
|
|
else:
|
|
|
|
timeout_key = ticks_ms() + after_ticks
|
|
|
|
|
2019-04-26 23:54:08 +02:00
|
|
|
while timeout_key in self.timeouts:
|
|
|
|
timeout_key += 1
|
|
|
|
|
2018-10-19 10:49:37 +02:00
|
|
|
self.timeouts[timeout_key] = callback
|
2019-04-26 23:54:08 +02:00
|
|
|
return timeout_key
|
|
|
|
|
|
|
|
def cancel_timeout(self, timeout_key):
|
|
|
|
if timeout_key in self.timeouts:
|
|
|
|
del self.timeouts[timeout_key]
|
2018-10-19 10:49:37 +02:00
|
|
|
|
|
|
|
def process_timeouts(self):
|
2018-10-21 02:53:58 +02:00
|
|
|
if not self.timeouts:
|
|
|
|
return self
|
|
|
|
|
2018-10-19 10:49:37 +02:00
|
|
|
current_time = ticks_ms()
|
|
|
|
|
2018-10-21 02:53:58 +02:00
|
|
|
# cast this to a tuple to ensure that if a callback itself sets
|
|
|
|
# timeouts, we do not handle them on the current cycle
|
|
|
|
timeouts = tuple(self.timeouts.items())
|
|
|
|
|
|
|
|
for k, v in timeouts:
|
2018-10-19 10:49:37 +02:00
|
|
|
if k <= current_time:
|
|
|
|
v()
|
|
|
|
del self.timeouts[k]
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
2018-10-16 13:04:39 +02:00
|
|
|
def matrix_changed(self, row, col, is_pressed):
|
|
|
|
if self.config.debug_enabled:
|
2019-07-13 00:16:33 +02:00
|
|
|
print('MatrixChange(col={} row={} pressed={})'.format(
|
|
|
|
col,
|
|
|
|
row,
|
|
|
|
is_pressed,
|
2018-10-16 13:04:39 +02:00
|
|
|
))
|
2018-09-23 06:49:58 +02:00
|
|
|
|
2018-10-19 08:24:19 +02:00
|
|
|
int_coord = intify_coordinate(row, col)
|
2018-10-16 13:04:39 +02:00
|
|
|
kc_changed = self._find_key_in_map(row, col)
|
2018-09-23 06:49:58 +02:00
|
|
|
|
2018-10-16 13:04:39 +02:00
|
|
|
if kc_changed is None:
|
2019-07-13 00:16:33 +02:00
|
|
|
print('MatrixUndefinedCoordinate(col={} row={})'.format(col, row))
|
2018-10-16 13:04:39 +02:00
|
|
|
return self
|
2018-09-23 06:49:58 +02:00
|
|
|
|
2018-12-29 13:44:52 +01:00
|
|
|
return self.process_key(kc_changed, is_pressed, int_coord, (row, col))
|
|
|
|
|
|
|
|
def process_key(self, key, is_pressed, coord_int=None, coord_raw=None):
|
|
|
|
if self.tapping and not isinstance(key.meta, TapDanceKeyMeta):
|
|
|
|
self._process_tap_dance(key, is_pressed)
|
2018-10-16 13:04:39 +02:00
|
|
|
else:
|
2018-10-19 12:04:54 +02:00
|
|
|
if is_pressed:
|
2019-02-19 00:08:07 +01:00
|
|
|
key._on_press(self, coord_int, coord_raw)
|
2018-10-19 12:04:54 +02:00
|
|
|
else:
|
2019-02-19 00:08:07 +01:00
|
|
|
key._on_release(self, coord_int, coord_raw)
|
2018-10-19 12:04:54 +02:00
|
|
|
|
|
|
|
if self.config.leader_mode % 2 == 1:
|
|
|
|
self._process_leader_mode()
|
2018-09-03 22:50:12 +02:00
|
|
|
|
2018-10-16 13:04:39 +02:00
|
|
|
return self
|
2018-09-03 22:50:12 +02:00
|
|
|
|
2018-10-21 02:53:58 +02:00
|
|
|
def remove_key(self, keycode):
|
2018-10-16 13:04:39 +02:00
|
|
|
self.keys_pressed.discard(keycode)
|
2018-12-29 13:44:52 +01:00
|
|
|
return self.process_key(keycode, False)
|
2018-09-23 06:49:58 +02:00
|
|
|
|
2018-10-21 02:53:58 +02:00
|
|
|
def add_key(self, keycode):
|
2018-12-29 13:44:52 +01:00
|
|
|
self.keys_pressed.add(keycode)
|
|
|
|
return self.process_key(keycode, True)
|
2018-10-01 03:03:43 +02:00
|
|
|
|
2018-10-21 02:53:58 +02:00
|
|
|
def tap_key(self, keycode):
|
|
|
|
self.add_key(keycode)
|
2019-01-14 20:25:21 +01:00
|
|
|
# On the next cycle, we'll remove the key.
|
2018-10-21 02:53:58 +02:00
|
|
|
self.set_timeout(False, lambda: self.remove_key(keycode))
|
2018-10-16 13:04:39 +02:00
|
|
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
def resolve_hid(self):
|
|
|
|
self.hid_pending = False
|
|
|
|
return self
|
|
|
|
|
2018-10-19 12:04:54 +02:00
|
|
|
def _process_tap_dance(self, changed_key, is_pressed):
|
|
|
|
if is_pressed:
|
2018-12-29 13:44:52 +01:00
|
|
|
if not isinstance(changed_key.meta, TapDanceKeyMeta):
|
2018-12-30 00:29:11 +01:00
|
|
|
# If we get here, changed_key is not a TapDanceKey and thus
|
2018-10-21 02:53:58 +02:00
|
|
|
# the user kept typing elsewhere (presumably). End ALL of the
|
|
|
|
# currently outstanding tap dance runs.
|
|
|
|
for k, v in self.tap_dance_counts.items():
|
|
|
|
if v:
|
|
|
|
self._end_tap_dance(k)
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
2018-10-19 12:04:54 +02:00
|
|
|
if (
|
2019-07-08 00:35:15 +02:00
|
|
|
changed_key not in self.tap_dance_counts or
|
|
|
|
not self.tap_dance_counts[changed_key]
|
2018-10-19 12:04:54 +02:00
|
|
|
):
|
2018-10-21 01:55:17 +02:00
|
|
|
self.tap_dance_counts[changed_key] = 1
|
2018-10-21 02:53:58 +02:00
|
|
|
self.set_timeout(self.config.tap_time, lambda: self._end_tap_dance(changed_key))
|
2018-10-21 01:55:17 +02:00
|
|
|
self.tapping = True
|
2018-10-19 12:04:54 +02:00
|
|
|
else:
|
|
|
|
self.tap_dance_counts[changed_key] += 1
|
|
|
|
|
2018-10-21 01:55:17 +02:00
|
|
|
if changed_key not in self.tap_side_effects:
|
|
|
|
self.tap_side_effects[changed_key] = None
|
|
|
|
else:
|
|
|
|
if (
|
|
|
|
self.tap_side_effects[changed_key] is not None or
|
|
|
|
self.tap_dance_counts[changed_key] == len(changed_key.codes)
|
|
|
|
):
|
|
|
|
self._end_tap_dance(changed_key)
|
2018-10-19 12:04:54 +02:00
|
|
|
|
|
|
|
return self
|
|
|
|
|
2018-10-21 01:55:17 +02:00
|
|
|
def _end_tap_dance(self, td_key):
|
|
|
|
v = self.tap_dance_counts[td_key] - 1
|
2018-10-19 12:04:54 +02:00
|
|
|
|
2018-10-21 01:55:17 +02:00
|
|
|
if v >= 0:
|
|
|
|
if td_key in self.keys_pressed:
|
|
|
|
key_to_press = td_key.codes[v]
|
2018-10-21 02:53:58 +02:00
|
|
|
self.add_key(key_to_press)
|
2018-10-21 01:55:17 +02:00
|
|
|
self.tap_side_effects[td_key] = key_to_press
|
|
|
|
self.hid_pending = True
|
|
|
|
else:
|
|
|
|
if self.tap_side_effects[td_key]:
|
2018-10-21 02:53:58 +02:00
|
|
|
self.remove_key(self.tap_side_effects[td_key])
|
2018-10-21 01:55:17 +02:00
|
|
|
self.tap_side_effects[td_key] = None
|
|
|
|
self.hid_pending = True
|
|
|
|
self._cleanup_tap_dance(td_key)
|
|
|
|
else:
|
2018-10-21 02:53:58 +02:00
|
|
|
self.tap_key(td_key.codes[v])
|
2018-10-21 01:55:17 +02:00
|
|
|
self._cleanup_tap_dance(td_key)
|
2018-10-19 12:04:54 +02:00
|
|
|
|
2018-10-21 01:55:17 +02:00
|
|
|
return self
|
2018-10-19 12:04:54 +02:00
|
|
|
|
2018-10-21 01:55:17 +02:00
|
|
|
def _cleanup_tap_dance(self, td_key):
|
|
|
|
self.tap_dance_counts[td_key] = 0
|
|
|
|
self.tapping = any(count > 0 for count in self.tap_dance_counts.values())
|
2018-10-19 12:04:54 +02:00
|
|
|
return self
|
|
|
|
|
2018-10-16 13:04:39 +02:00
|
|
|
def _begin_leader_mode(self):
|
2018-10-19 10:49:37 +02:00
|
|
|
if self.config.leader_mode % 2 == 0:
|
2018-12-29 13:44:52 +01:00
|
|
|
self.keys_pressed.discard(KC.LEAD)
|
2018-10-16 13:04:39 +02:00
|
|
|
# All leader modes are one number higher when activating
|
2018-10-19 10:49:37 +02:00
|
|
|
self.config.leader_mode += 1
|
|
|
|
|
|
|
|
if self.config.leader_mode == LeaderMode.TIMEOUT_ACTIVE:
|
|
|
|
self.set_timeout(self.config.leader_timeout, self._handle_leader_sequence)
|
2018-10-06 12:58:19 +02:00
|
|
|
|
2018-10-16 13:04:39 +02:00
|
|
|
return self
|
|
|
|
|
2018-10-19 10:49:37 +02:00
|
|
|
def _handle_leader_sequence(self):
|
|
|
|
lmh = tuple(self.leader_mode_history)
|
2018-12-30 01:52:06 +01:00
|
|
|
# Will get caught in infinite processing loops if we don't
|
|
|
|
# exit leader mode before processing the target key
|
|
|
|
self._exit_leader_mode()
|
2018-10-19 10:49:37 +02:00
|
|
|
|
|
|
|
if lmh in self.config.leader_dictionary:
|
2018-12-30 01:52:06 +01:00
|
|
|
# Stack depth exceeded if try to use add_key here with a unicode sequence
|
2018-12-29 13:44:52 +01:00
|
|
|
self.process_key(self.config.leader_dictionary[lmh], True)
|
2018-10-19 10:49:37 +02:00
|
|
|
|
2018-12-30 01:52:06 +01:00
|
|
|
self.set_timeout(
|
|
|
|
False,
|
|
|
|
lambda: self.remove_key(self.config.leader_dictionary[lmh]),
|
|
|
|
)
|
|
|
|
|
|
|
|
return self
|
2018-10-19 10:49:37 +02:00
|
|
|
|
2018-10-16 13:04:39 +02:00
|
|
|
def _process_leader_mode(self):
|
|
|
|
keys_pressed = self.keys_pressed
|
2018-10-06 12:58:19 +02:00
|
|
|
|
2018-10-16 13:04:39 +02:00
|
|
|
if self.leader_last_len and self.leader_mode_history:
|
|
|
|
history_set = set(self.leader_mode_history)
|
2018-10-06 12:58:19 +02:00
|
|
|
|
2018-10-16 13:04:39 +02:00
|
|
|
keys_pressed = keys_pressed - history_set
|
2018-10-06 12:58:19 +02:00
|
|
|
|
2018-10-16 13:04:39 +02:00
|
|
|
self.leader_last_len = len(self.keys_pressed)
|
2018-10-06 12:58:19 +02:00
|
|
|
|
2018-10-16 13:04:39 +02:00
|
|
|
for key in keys_pressed:
|
2018-10-19 10:49:37 +02:00
|
|
|
if (
|
2019-07-08 00:35:15 +02:00
|
|
|
self.config.leader_mode == LeaderMode.ENTER_ACTIVE and
|
|
|
|
key == KC.ENT
|
2018-10-19 10:49:37 +02:00
|
|
|
):
|
|
|
|
self._handle_leader_sequence()
|
2018-10-16 13:04:39 +02:00
|
|
|
break
|
2018-12-29 13:44:52 +01:00
|
|
|
elif key == KC.ESC or key == KC.GESC:
|
2018-10-16 13:04:39 +02:00
|
|
|
# Clean self and turn leader mode off.
|
|
|
|
self._exit_leader_mode()
|
|
|
|
break
|
2018-12-29 13:44:52 +01:00
|
|
|
elif key == KC.LEAD:
|
2018-10-16 13:04:39 +02:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
# Add key if not needing to escape
|
|
|
|
# This needs replaced later with a proper debounce
|
|
|
|
self.leader_mode_history.append(key)
|
2018-10-08 11:31:30 +02:00
|
|
|
|
2018-10-16 13:04:39 +02:00
|
|
|
self.hid_pending = False
|
|
|
|
return self
|
2018-10-08 11:31:30 +02:00
|
|
|
|
2018-10-16 13:04:39 +02:00
|
|
|
def _exit_leader_mode(self):
|
|
|
|
self.leader_mode_history.clear()
|
2018-10-19 10:49:37 +02:00
|
|
|
self.config.leader_mode -= 1
|
2018-10-16 13:04:39 +02:00
|
|
|
self.leader_last_len = 0
|
|
|
|
self.keys_pressed.clear()
|
|
|
|
return self
|