process_repeat_key.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2022-2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "process_repeat_key.h"
  15. #include "repeat_key.h"
  16. #include "keycodes.h"
  17. #include "quantum_keycodes.h"
  18. #include "action_util.h"
  19. // Default implementation of remember_last_key_user().
  20. __attribute__((weak)) bool remember_last_key_user(uint16_t keycode, keyrecord_t* record, uint8_t* remembered_mods) {
  21. return true;
  22. }
  23. static bool remember_last_key(uint16_t keycode, keyrecord_t* record, uint8_t* remembered_mods) {
  24. switch (keycode) {
  25. // Ignore MO, TO, TG, TT, and TL layer switch keys.
  26. case QK_MOMENTARY ... QK_MOMENTARY_MAX:
  27. case QK_TO ... QK_TO_MAX:
  28. case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX:
  29. case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX:
  30. // Ignore mod keys.
  31. case KC_LCTL ... KC_RGUI:
  32. case KC_HYPR:
  33. case KC_MEH:
  34. #ifndef NO_ACTION_ONESHOT // Ignore one-shot keys.
  35. case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX:
  36. case QK_ONE_SHOT_MOD ... QK_ONE_SHOT_MOD_MAX:
  37. #endif // NO_ACTION_ONESHOT
  38. #ifdef TRI_LAYER_ENABLE // Ignore Tri Layer keys.
  39. case QK_TRI_LAYER_LOWER:
  40. case QK_TRI_LAYER_UPPER:
  41. #endif // TRI_LAYER_ENABLE
  42. return false;
  43. // Ignore hold events on tap-hold keys.
  44. #ifndef NO_ACTION_TAPPING
  45. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  46. # ifndef NO_ACTION_LAYER
  47. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
  48. # endif // NO_ACTION_LAYER
  49. if (record->tap.count == 0) {
  50. return false;
  51. }
  52. break;
  53. #endif // NO_ACTION_TAPPING
  54. #ifdef SWAP_HANDS_ENABLE
  55. case QK_SWAP_HANDS ... QK_SWAP_HANDS_MAX:
  56. if (IS_SWAP_HANDS_KEYCODE(keycode) || record->tap.count == 0) {
  57. return false;
  58. }
  59. break;
  60. #endif // SWAP_HANDS_ENABLE
  61. case QK_REPEAT_KEY:
  62. #ifndef NO_ALT_REPEAT_KEY
  63. case QK_ALT_REPEAT_KEY:
  64. #endif // NO_ALT_REPEAT_KEY
  65. return false;
  66. }
  67. return remember_last_key_user(keycode, record, remembered_mods);
  68. }
  69. bool process_last_key(uint16_t keycode, keyrecord_t* record) {
  70. if (get_repeat_key_count()) {
  71. return true;
  72. }
  73. if (record->event.pressed) {
  74. uint8_t remembered_mods = get_mods() | get_weak_mods();
  75. #ifndef NO_ACTION_ONESHOT
  76. remembered_mods |= get_oneshot_mods();
  77. #endif // NO_ACTION_ONESHOT
  78. if (remember_last_key(keycode, record, &remembered_mods)) {
  79. set_last_record(keycode, record);
  80. set_last_mods(remembered_mods);
  81. }
  82. }
  83. return true;
  84. }
  85. bool process_repeat_key(uint16_t keycode, keyrecord_t* record) {
  86. if (get_repeat_key_count()) {
  87. return true;
  88. }
  89. if (keycode == QK_REPEAT_KEY) {
  90. repeat_key_invoke(&record->event);
  91. return false;
  92. #ifndef NO_ALT_REPEAT_KEY
  93. } else if (keycode == QK_ALT_REPEAT_KEY) {
  94. alt_repeat_key_invoke(&record->event);
  95. return false;
  96. #endif // NO_ALT_REPEAT_KEY
  97. }
  98. return true;
  99. }