@kdb424's Leader Mode work as a clean diff

This commit is contained in:
Josh Klar
2018-10-08 02:31:30 -07:00
parent 914b305fee
commit 8c9fc9db95
8 changed files with 205 additions and 34 deletions

View File

@@ -2,7 +2,7 @@ import logging
import sys
from kmk.common import kmktime
from kmk.common.consts import DiodeOrientation, UnicodeModes
from kmk.common.consts import DiodeOrientation, LeaderMode, UnicodeModes
from kmk.common.event_defs import (HID_REPORT_EVENT, INIT_FIRMWARE_EVENT,
KEY_DOWN_EVENT, KEY_UP_EVENT,
KEYCODE_DOWN_EVENT, KEYCODE_UP_EVENT,
@@ -69,28 +69,30 @@ class InternalState:
keys_pressed = set()
pending_keys = set()
macro_pending = None
leader_pending = None
hid_pending = False
unicode_mode = UnicodeModes.NOOP
tap_time = 300
keymap = []
row_pins = []
col_pins = []
matrix = []
diode_orientation = DiodeOrientation.COLUMNS
leader_mode_history = []
active_layers = [0]
start_time = {
'lt': None,
'tg': None,
'tt': None,
}
tick_time = {
'lt': None,
'tg': None,
'tt': None,
'lm': None,
'leader': None,
}
_oldstates = []
def __init__(self, preserve_intermediate_states=False):
import kmk_keyboard_user
self.unicode_mode = getattr(kmk_keyboard_user, 'unicode_mode', UnicodeModes.NOOP)
self.tap_time = getattr(kmk_keyboard_user, 'tap_time', 300)
self.leader_mode = getattr(kmk_keyboard_user, 'leader_mode', LeaderMode.Enter)
self.LEADER_DICTIONARY = getattr(kmk_keyboard_user, 'LEADER_DICTIONARY', {})
self.preserve_intermediate_states = preserve_intermediate_states
def __enter__(self):
@@ -105,8 +107,8 @@ class InternalState:
'active_layers': self.active_layers,
'unicode_mode': self.unicode_mode,
'tap_time': self.tap_time,
'leader_mode_history': self.leader_mode_history,
'start_time': self.start_time,
'tick_time': self.tick_time,
}
if verbose:
@@ -196,18 +198,19 @@ def kmk_reducer(state=None, action=None, logger=None):
state.matrix = action.matrix
state.keys_pressed |= pressed
state.keys_pressed -= released
state.hid_pending = True
if state.leader_mode % 2 == 1:
state.hid_pending = False
else:
state.hid_pending = True
return state
if action.type == KEYCODE_UP_EVENT:
state.keys_pressed.discard(action.keycode)
state.hid_pending = True
return state
if action.type == KEYCODE_DOWN_EVENT:
state.keys_pressed.add(action.keycode)
state.hid_pending = True
return state
if action.type == INIT_FIRMWARE_EVENT:
@@ -216,7 +219,6 @@ def kmk_reducer(state=None, action=None, logger=None):
row_pins=action.row_pins,
col_pins=action.col_pins,
diode_orientation=action.diode_orientation,
unicode_mode=action.unicode_mode,
)
# HID events are non-mutating, used exclusively for listeners to know
@@ -224,7 +226,6 @@ def kmk_reducer(state=None, action=None, logger=None):
# into KEY_UP_EVENT and KEY_DOWN_EVENT, but for now it's nice to separate
# this out for debugging's sake.
if action.type == HID_REPORT_EVENT:
state.hid_pending = False
return state
if action.type == MACRO_COMPLETE_EVENT:
@@ -268,6 +269,8 @@ def process_internal_key_event(state, action_type, changed_key, logger=None):
return unicode_mode(state, action_type, changed_key, logger=logger)
elif changed_key.code == RawKeycodes.KC_MACRO:
return macro(state, action_type, changed_key, logger=logger)
elif changed_key.code == Keycodes.KMK.KC_LEAD.code:
return leader(state)
else:
return state
@@ -410,3 +413,12 @@ def macro(state, action_type, changed_key, logger):
return state
return state
def leader(state):
if state.leader_mode % 2 == 0:
state.keys_pressed.discard(Keycodes.KMK.KC_LEAD)
# All leader modes are one number higher when activating
state.leader_mode += 1
return state