Merge pull request #65 from KMKfw/topic-low-hanging-fruit
Add MEH and HYPER modifiers, refactor some cruft, and banish mostly-unused InternalState.update()
This commit is contained in:
		| @@ -102,6 +102,10 @@ class AbstractHidHelper: | |||||||
|  |  | ||||||
|     def add_modifier(self, modifier): |     def add_modifier(self, modifier): | ||||||
|         if isinstance(modifier, ModifierKeycode): |         if isinstance(modifier, ModifierKeycode): | ||||||
|  |             if modifier.code == ModifierKeycode.FAKE_CODE: | ||||||
|  |                 for mod in modifier.has_modifiers: | ||||||
|  |                     self.report_mods[0] |= mod | ||||||
|  |             else: | ||||||
|                 self.report_mods[0] |= modifier.code |                 self.report_mods[0] |= modifier.code | ||||||
|         else: |         else: | ||||||
|             self.report_mods[0] |= modifier |             self.report_mods[0] |= modifier | ||||||
| @@ -110,6 +114,10 @@ class AbstractHidHelper: | |||||||
|  |  | ||||||
|     def remove_modifier(self, modifier): |     def remove_modifier(self, modifier): | ||||||
|         if isinstance(modifier, ModifierKeycode): |         if isinstance(modifier, ModifierKeycode): | ||||||
|  |             if modifier.code == ModifierKeycode.FAKE_CODE: | ||||||
|  |                 for mod in modifier.has_modifiers: | ||||||
|  |                     self.report_mods[0] ^= mod | ||||||
|  |             else: | ||||||
|                 self.report_mods[0] ^= modifier.code |                 self.report_mods[0] ^= modifier.code | ||||||
|         else: |         else: | ||||||
|             self.report_mods[0] ^= modifier |             self.report_mods[0] ^= modifier | ||||||
|   | |||||||
| @@ -32,6 +32,9 @@ class Store: | |||||||
|         self.callbacks = [] |         self.callbacks = [] | ||||||
|  |  | ||||||
|     def dispatch(self, action): |     def dispatch(self, action): | ||||||
|  |         if self.state.preserve_intermediate_states: | ||||||
|  |             self.state._oldstates.append(repr(self.state.to_dict(verbose=True))) | ||||||
|  |  | ||||||
|         if callable(action): |         if callable(action): | ||||||
|             self.logger.debug('Received thunk') |             self.logger.debug('Received thunk') | ||||||
|             action(self.dispatch, self.get_state) |             action(self.dispatch, self.get_state) | ||||||
| @@ -124,15 +127,6 @@ class InternalState: | |||||||
|     def __repr__(self): |     def __repr__(self): | ||||||
|         return 'InternalState({})'.format(self.to_dict()) |         return 'InternalState({})'.format(self.to_dict()) | ||||||
|  |  | ||||||
|     def update(self, **kwargs): |  | ||||||
|         if self.preserve_intermediate_states: |  | ||||||
|             self._oldstates.append(repr(self.to_dict(verbose=True))) |  | ||||||
|  |  | ||||||
|         for k, v in kwargs.items(): |  | ||||||
|             setattr(self, k, v) |  | ||||||
|  |  | ||||||
|         return self |  | ||||||
|  |  | ||||||
|  |  | ||||||
| def find_key_in_map(state, row, col): | def find_key_in_map(state, row, col): | ||||||
|     # Later-added layers have priority. Sift through the layers |     # Later-added layers have priority. Sift through the layers | ||||||
| @@ -178,10 +172,12 @@ def kmk_reducer(state=None, action=None, logger=None): | |||||||
|             if not changed_key: |             if not changed_key: | ||||||
|                 continue |                 continue | ||||||
|             elif changed_key.code >= FIRST_KMK_INTERNAL_KEYCODE: |             elif changed_key.code >= FIRST_KMK_INTERNAL_KEYCODE: | ||||||
|                 state = process_internal_key_event(state, |                 state = process_internal_key_event( | ||||||
|  |                     state, | ||||||
|                     KEY_UP_EVENT, |                     KEY_UP_EVENT, | ||||||
|                     changed_key, |                     changed_key, | ||||||
|                                                    logger=logger) |                     logger=logger, | ||||||
|  |                 ) | ||||||
|  |  | ||||||
|         for changed_key in pressed: |         for changed_key in pressed: | ||||||
|             if not changed_key: |             if not changed_key: | ||||||
| @@ -213,12 +209,11 @@ def kmk_reducer(state=None, action=None, logger=None): | |||||||
|         return state |         return state | ||||||
|  |  | ||||||
|     if action.type == INIT_FIRMWARE_EVENT: |     if action.type == INIT_FIRMWARE_EVENT: | ||||||
|         return state.update( |         state.keymap = action.keymap | ||||||
|             keymap=action.keymap, |         state.row_pins = action.row_pins | ||||||
|             row_pins=action.row_pins, |         state.col_pins = action.col_pins | ||||||
|             col_pins=action.col_pins, |         state.diode_orientation = action.diode_orientation | ||||||
|             diode_orientation=action.diode_orientation, |         return state | ||||||
|         ) |  | ||||||
|  |  | ||||||
|     # HID events are non-mutating, used exclusively for listeners to know |     # HID events are non-mutating, used exclusively for listeners to know | ||||||
|     # they should be doing things. This could/should arguably be folded back |     # they should be doing things. This could/should arguably be folded back | ||||||
| @@ -228,7 +223,8 @@ def kmk_reducer(state=None, action=None, logger=None): | |||||||
|         return state |         return state | ||||||
|  |  | ||||||
|     if action.type == MACRO_COMPLETE_EVENT: |     if action.type == MACRO_COMPLETE_EVENT: | ||||||
|         return state.update(macro_pending=None) |         state.macro_pending = None | ||||||
|  |         return state | ||||||
|  |  | ||||||
|     if action.type == PENDING_KEYCODE_POP_EVENT: |     if action.type == PENDING_KEYCODE_POP_EVENT: | ||||||
|         state.pending_keys.pop() |         state.pending_keys.pop() | ||||||
|   | |||||||
| @@ -81,11 +81,27 @@ class Keycode: | |||||||
|  |  | ||||||
|  |  | ||||||
| class ModifierKeycode(Keycode): | class ModifierKeycode(Keycode): | ||||||
|  |     FAKE_CODE = -1 | ||||||
|  |  | ||||||
|     def __call__(self, modified_code=None, no_press=None, no_release=None): |     def __call__(self, modified_code=None, no_press=None, no_release=None): | ||||||
|         if modified_code is None and no_press is None and no_release is None: |         if modified_code is None and no_press is None and no_release is None: | ||||||
|             return self |             return self | ||||||
|  |  | ||||||
|         if modified_code is not None: |         if modified_code is not None: | ||||||
|  |             if isinstance(modified_code, ModifierKeycode): | ||||||
|  |                 new_keycode = ModifierKeycode( | ||||||
|  |                     ModifierKeycode.FAKE_CODE, | ||||||
|  |                     set() if self.has_modifiers is None else self.has_modifiers, | ||||||
|  |                     no_press=no_press, | ||||||
|  |                     no_release=no_release, | ||||||
|  |                 ) | ||||||
|  |  | ||||||
|  |                 if self.code != ModifierKeycode.FAKE_CODE: | ||||||
|  |                     new_keycode.has_modifiers.add(self.code) | ||||||
|  |  | ||||||
|  |                 if modified_code.code != ModifierKeycode.FAKE_CODE: | ||||||
|  |                     new_keycode.has_modifiers.add(modified_code.code) | ||||||
|  |             else: | ||||||
|                 new_keycode = Keycode( |                 new_keycode = Keycode( | ||||||
|                     modified_code.code, |                     modified_code.code, | ||||||
|                     {self.code}, |                     {self.code}, | ||||||
| @@ -104,6 +120,9 @@ class ModifierKeycode(Keycode): | |||||||
|  |  | ||||||
|         return new_keycode |         return new_keycode | ||||||
|  |  | ||||||
|  |     def __repr__(self): | ||||||
|  |         return 'ModifierKeycode(code={}, has_modifiers={})'.format(self.code, self.has_modifiers) | ||||||
|  |  | ||||||
|  |  | ||||||
| class ConsumerKeycode(Keycode): | class ConsumerKeycode(Keycode): | ||||||
|     pass |     pass | ||||||
| @@ -220,6 +239,9 @@ class Modifiers(KeycodeCategory): | |||||||
|     KC_RALT = ModifierKeycode(RawKeycodes.RALT) |     KC_RALT = ModifierKeycode(RawKeycodes.RALT) | ||||||
|     KC_RGUI = KC_RCMD = KC_RWIN = ModifierKeycode(RawKeycodes.RGUI) |     KC_RGUI = KC_RCMD = KC_RWIN = ModifierKeycode(RawKeycodes.RGUI) | ||||||
|  |  | ||||||
|  |     KC_MEH = KC_LSHIFT(KC_LALT(KC_LCTRL)) | ||||||
|  |     KC_HYPR = KC_HYPER = KC_MEH(KC_LGUI) | ||||||
|  |  | ||||||
|  |  | ||||||
| class Common(KeycodeCategory): | class Common(KeycodeCategory): | ||||||
|     KC_A = Keycode(4) |     KC_A = Keycode(4) | ||||||
|   | |||||||
| @@ -72,7 +72,7 @@ MACRO_HELLO_WORLD = simple_key_sequence([ | |||||||
|  |  | ||||||
| keymap = [ | keymap = [ | ||||||
|     [ |     [ | ||||||
|         [KC.GESC,              KC.A,     KC.RESET], |         [KC.GESC,              KC.HYPR,     KC.RESET], | ||||||
|         [KC.MO(1),             KC.B,     KC.MUTE], |         [KC.MO(1),             KC.B,     KC.MUTE], | ||||||
|         [KC.LT(2, KC.EXCLAIM), KC.HASH,  KC.ENTER], |         [KC.LT(2, KC.EXCLAIM), KC.HASH,  KC.ENTER], | ||||||
|     ], |     ], | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user