You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
8.3 KiB

Process combos earlier & overlapping combos (#8591) * Combo processing improvements. Now it is possible to use ModTap and LayerTap keys as part of combos. Overlapping combos also don't trigger all the combos, just exactly the one that you press. New settings: - COMBO_MUST_HOLD_MODS - COMBO_MOD_TERM - COMBO_TERM_PER_COMBO - COMBO_MUST_HOLD_PER_COMBO - COMBO_STRICT_TIMER - COMBO_NO_TIMER * Remove the size flags from combo_t struct boolean members. This in the end actually saves space as the members are accessed so many times. The amount of operations needed to access the bits uses more memory than setting the size saves. * Fix `process_combo_key_release` not called correctly with tap-only combos * Fix not passing a pointer when NO_ACTION_TAPPING is defined. * Docs for `COMBO_ONLY_FROM_LAYER` * Update docs/feature_combo.md Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> * Update quantum/process_keycode/process_combo.c Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> * Add `EXTRA_SHORT_COMBOS` option. Stuff combo's `disabled` and `active` flags into `state`. Possibly can save some space. * Add more examples and clarify things with dict management system. - Simple examples now has a combo that has modifiers included. - The slightly more advanced examples now are actually more advanced instead of just `tap_code16(<modded-keycode>)`. - Added a note that `COMBO_ACTION`s are not needed anymore as you can just use custom keycodes. - Added a note that the `g/keymap_combo.h` macros use the `process_combo_event` function and that it is not usable in one's keymap afterwards. * Update docs/feature_combo.md Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> * Update docs/feature_combo.md Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> * Update docs/feature_combo.md Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> * Update docs/feature_combo.md Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> * Update docs/feature_combo.md Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> * Change "the" combo action example to "email" example. * Update docs/feature_combo.md Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> * Fix sneaky infinite loop with `combo_disable()` No need to call `dump_key_buffer` when disabling combos because the buffer is either being dumped if a combo-key was pressed, or the buffer is empty if a non-combo-key is pressed. * Update docs/feature_combo.md Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> * Update docs/feature_combo.md Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> Co-authored-by: Drashna Jaelre <drashna@live.com>
2 years ago
Process combos earlier & overlapping combos (#8591) * Combo processing improvements. Now it is possible to use ModTap and LayerTap keys as part of combos. Overlapping combos also don't trigger all the combos, just exactly the one that you press. New settings: - COMBO_MUST_HOLD_MODS - COMBO_MOD_TERM - COMBO_TERM_PER_COMBO - COMBO_MUST_HOLD_PER_COMBO - COMBO_STRICT_TIMER - COMBO_NO_TIMER * Remove the size flags from combo_t struct boolean members. This in the end actually saves space as the members are accessed so many times. The amount of operations needed to access the bits uses more memory than setting the size saves. * Fix `process_combo_key_release` not called correctly with tap-only combos * Fix not passing a pointer when NO_ACTION_TAPPING is defined. * Docs for `COMBO_ONLY_FROM_LAYER` * Update docs/feature_combo.md Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> * Update quantum/process_keycode/process_combo.c Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> * Add `EXTRA_SHORT_COMBOS` option. Stuff combo's `disabled` and `active` flags into `state`. Possibly can save some space. * Add more examples and clarify things with dict management system. - Simple examples now has a combo that has modifiers included. - The slightly more advanced examples now are actually more advanced instead of just `tap_code16(<modded-keycode>)`. - Added a note that `COMBO_ACTION`s are not needed anymore as you can just use custom keycodes. - Added a note that the `g/keymap_combo.h` macros use the `process_combo_event` function and that it is not usable in one's keymap afterwards. * Update docs/feature_combo.md Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> * Update docs/feature_combo.md Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> * Update docs/feature_combo.md Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> * Update docs/feature_combo.md Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> * Update docs/feature_combo.md Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> * Change "the" combo action example to "email" example. * Update docs/feature_combo.md Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> * Fix sneaky infinite loop with `combo_disable()` No need to call `dump_key_buffer` when disabling combos because the buffer is either being dumped if a combo-key was pressed, or the buffer is empty if a non-combo-key is pressed. * Update docs/feature_combo.md Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> * Update docs/feature_combo.md Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> Co-authored-by: precondition <57645186+precondition@users.noreply.github.com> Co-authored-by: Drashna Jaelre <drashna@live.com>
2 years ago
  1. /*
  2. Copyright 2012-2017 Jun Wako <wakojun@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "keymap_common.h"
  15. #include "keymap_introspection.h"
  16. #include "report.h"
  17. #include "keycode.h"
  18. #include "action_layer.h"
  19. #include "action.h"
  20. #include "debug.h"
  21. #include "keycode_config.h"
  22. #include "quantum_keycodes.h"
  23. #ifdef ENCODER_MAP_ENABLE
  24. # include "encoder.h"
  25. #endif
  26. #ifdef BACKLIGHT_ENABLE
  27. # include "backlight.h"
  28. #endif
  29. #ifdef MIDI_ENABLE
  30. # include "process_midi.h"
  31. #endif
  32. extern keymap_config_t keymap_config;
  33. #include <inttypes.h>
  34. /* converts key to action */
  35. action_t action_for_key(uint8_t layer, keypos_t key) {
  36. // 16bit keycodes - important
  37. uint16_t keycode = keymap_key_to_keycode(layer, key);
  38. return action_for_keycode(keycode);
  39. };
  40. action_t action_for_keycode(uint16_t keycode) {
  41. // keycode remapping
  42. keycode = keycode_config(keycode);
  43. action_t action = {};
  44. uint8_t action_layer, mod;
  45. (void)action_layer;
  46. (void)mod;
  47. switch (keycode) {
  48. case BASIC_KEYCODE_RANGE:
  49. case MODIFIER_KEYCODE_RANGE:
  50. action.code = ACTION_KEY(keycode);
  51. break;
  52. #ifdef EXTRAKEY_ENABLE
  53. case SYSTEM_KEYCODE_RANGE:
  54. action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
  55. break;
  56. case CONSUMER_KEYCODE_RANGE:
  57. action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
  58. break;
  59. #endif
  60. case MOUSE_KEYCODE_RANGE:
  61. action.code = ACTION_MOUSEKEY(keycode);
  62. break;
  63. case KC_TRANSPARENT:
  64. action.code = ACTION_TRANSPARENT;
  65. break;
  66. case QK_MODS ... QK_MODS_MAX:;
  67. // Has a modifier
  68. // Split it up
  69. #ifdef LEGACY_MAGIC_HANDLING
  70. action.code = ACTION_MODS_KEY(QK_MODS_GET_MODS(keycode), QK_MODS_GET_BASIC_KEYCODE(keycode)); // adds modifier to key
  71. #else // LEGACY_MAGIC_HANDLING
  72. action.code = ACTION_MODS_KEY(mod_config(QK_MODS_GET_MODS(keycode)), keycode_config(QK_MODS_GET_BASIC_KEYCODE(keycode))); // adds modifier to key
  73. #endif // LEGACY_MAGIC_HANDLING
  74. break;
  75. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
  76. #if !defined(NO_ACTION_LAYER) && !defined(NO_ACTION_TAPPING)
  77. # ifdef LEGACY_MAGIC_HANDLING
  78. action.code = ACTION_LAYER_TAP_KEY(QK_LAYER_TAP_GET_LAYER(keycode), QK_LAYER_TAP_GET_TAP_KEYCODE(keycode));
  79. # else // LEGACY_MAGIC_HANDLING
  80. action.code = ACTION_LAYER_TAP_KEY(QK_LAYER_TAP_GET_LAYER(keycode), keycode_config(QK_LAYER_TAP_GET_TAP_KEYCODE(keycode)));
  81. # endif // LEGACY_MAGIC_HANDLING
  82. #else
  83. // pass through keycode_config again, since it previously missed it
  84. // and then only send as ACTION_KEY to bypass most of action.c handling
  85. action.code = ACTION_KEY(keycode_config(QK_LAYER_TAP_GET_TAP_KEYCODE(keycode)));
  86. #endif
  87. break;
  88. #ifndef NO_ACTION_LAYER
  89. case QK_TO ... QK_TO_MAX:;
  90. // Layer set "GOTO"
  91. action_layer = QK_TO_GET_LAYER(keycode);
  92. action.code = ACTION_LAYER_GOTO(action_layer);
  93. break;
  94. case QK_MOMENTARY ... QK_MOMENTARY_MAX:;
  95. // Momentary action_layer
  96. action_layer = QK_MOMENTARY_GET_LAYER(keycode);
  97. action.code = ACTION_LAYER_MOMENTARY(action_layer);
  98. break;
  99. case QK_DEF_LAYER ... QK_DEF_LAYER_MAX:;
  100. // Set default action_layer
  101. action_layer = QK_DEF_LAYER_GET_LAYER(keycode);
  102. action.code = ACTION_DEFAULT_LAYER_SET(action_layer);
  103. break;
  104. case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX:;
  105. // Set toggle
  106. action_layer = QK_TOGGLE_LAYER_GET_LAYER(keycode);
  107. action.code = ACTION_LAYER_TOGGLE(action_layer);
  108. break;
  109. #endif
  110. #ifndef NO_ACTION_ONESHOT
  111. case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX:;
  112. // OSL(action_layer) - One-shot action_layer
  113. action_layer = QK_ONE_SHOT_LAYER_GET_LAYER(keycode);
  114. action.code = ACTION_LAYER_ONESHOT(action_layer);
  115. break;
  116. #endif // NO_ACTION_ONESHOT
  117. case QK_ONE_SHOT_MOD ... QK_ONE_SHOT_MOD_MAX:;
  118. // OSM(mod) - One-shot mod
  119. mod = mod_config(QK_ONE_SHOT_MOD_GET_MODS(keycode));
  120. #if defined(NO_ACTION_TAPPING) || defined(NO_ACTION_ONESHOT)
  121. action.code = ACTION_MODS(mod);
  122. #else // defined(NO_ACTION_TAPPING) || defined(NO_ACTION_ONESHOT)
  123. action.code = ACTION_MODS_ONESHOT(mod);
  124. #endif // defined(NO_ACTION_TAPPING) || defined(NO_ACTION_ONESHOT)
  125. break;
  126. #ifndef NO_ACTION_LAYER
  127. case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX:
  128. # ifndef NO_ACTION_TAPPING
  129. action.code = ACTION_LAYER_TAP_TOGGLE(QK_LAYER_TAP_TOGGLE_GET_LAYER(keycode));
  130. # else // NO_ACTION_TAPPING
  131. # ifdef NO_ACTION_TAPPING_TAP_TOGGLE_MO
  132. action.code = ACTION_LAYER_MOMENTARY(QK_LAYER_TAP_TOGGLE_GET_LAYER(keycode));
  133. # else // NO_ACTION_TAPPING_TAP_TOGGLE_MO
  134. action.code = ACTION_LAYER_TOGGLE(QK_LAYER_TAP_TOGGLE_GET_LAYER(keycode));
  135. # endif // NO_ACTION_TAPPING_TAP_TOGGLE_MO
  136. # endif // NO_ACTION_TAPPING
  137. break;
  138. case QK_LAYER_MOD ... QK_LAYER_MOD_MAX:
  139. mod = mod_config(QK_LAYER_MOD_GET_MODS(keycode));
  140. action_layer = QK_LAYER_MOD_GET_LAYER(keycode);
  141. action.code = ACTION_LAYER_MODS(action_layer, (mod & 0x10) ? mod << 4 : mod);
  142. break;
  143. #endif // NO_ACTION_LAYER
  144. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  145. #ifndef NO_ACTION_TAPPING
  146. mod = mod_config(QK_MOD_TAP_GET_MODS(keycode));
  147. # ifdef LEGACY_MAGIC_HANDLING
  148. action.code = ACTION_MODS_TAP_KEY(mod, QK_MOD_TAP_GET_TAP_KEYCODE(keycode));
  149. # else // LEGACY_MAGIC_HANDLING
  150. action.code = ACTION_MODS_TAP_KEY(mod, keycode_config(QK_MOD_TAP_GET_TAP_KEYCODE(keycode)));
  151. # endif // LEGACY_MAGIC_HANDLING
  152. #else // NO_ACTION_TAPPING
  153. # ifdef NO_ACTION_TAPPING_MODTAP_MODS
  154. // pass through mod_config again, since it previously missed it
  155. // and then only send as ACTION_KEY to bypass most of action.c handling
  156. action.code = ACTION_MODS(mod_config(QK_MOD_TAP_GET_MODS(keycode)));
  157. # else // NO_ACTION_TAPPING_MODTAP_MODS
  158. // pass through keycode_config again, since it previously missed it
  159. // and then only send as ACTION_KEY to bypass most of action.c handling
  160. action.code = ACTION_KEY(keycode_config(QK_MOD_TAP_GET_TAP_KEYCODE(keycode)));
  161. # endif // NO_ACTION_TAPPING_MODTAP_MODS
  162. #endif // NO_ACTION_TAPPING
  163. break;
  164. #ifdef SWAP_HANDS_ENABLE
  165. case QK_SWAP_HANDS ... QK_SWAP_HANDS_MAX:
  166. # ifdef LEGACY_MAGIC_HANDLING
  167. action.code = ACTION(ACT_SWAP_HANDS, QK_SWAP_HANDS_GET_TAP_KEYCODE(keycode));
  168. # else // LEGACY_MAGIC_HANDLING
  169. action.code = ACTION(ACT_SWAP_HANDS, keycode_config(QK_SWAP_HANDS_GET_TAP_KEYCODE(keycode)));
  170. # endif // LEGACY_MAGIC_HANDLING
  171. break;
  172. #endif
  173. default:
  174. action.code = ACTION_NO;
  175. break;
  176. }
  177. return action;
  178. }
  179. // translates key to keycode
  180. __attribute__((weak)) uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) {
  181. if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) {
  182. return keycode_at_keymap_location(layer, key.row, key.col);
  183. }
  184. #ifdef ENCODER_MAP_ENABLE
  185. else if (key.row == KEYLOC_ENCODER_CW && key.col < NUM_ENCODERS) {
  186. return keycode_at_encodermap_location(layer, key.col, true);
  187. } else if (key.row == KEYLOC_ENCODER_CCW && key.col < NUM_ENCODERS) {
  188. return keycode_at_encodermap_location(layer, key.col, false);
  189. }
  190. #endif // ENCODER_MAP_ENABLE
  191. return KC_NO;
  192. }