logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

process_space_cadet.c (4943B)


  1. /* Copyright 2019 Jack Humbert
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "process_space_cadet.h"
  17. #include "keycodes.h"
  18. #include "timer.h"
  19. #include "action.h"
  20. #include "action_tapping.h"
  21. #include "action_util.h"
  22. // ********** OBSOLETE DEFINES, STOP USING! (pls?) **********
  23. // Shift / paren setup
  24. #ifndef LSPO_KEY
  25. # define LSPO_KEY KC_9
  26. #endif
  27. #ifndef RSPC_KEY
  28. # define RSPC_KEY KC_0
  29. #endif
  30. // Shift / Enter setup
  31. #ifndef SFTENT_KEY
  32. # define SFTENT_KEY KC_ENTER
  33. #endif
  34. #ifdef DISABLE_SPACE_CADET_MODIFIER
  35. # ifndef LSPO_MOD
  36. # define LSPO_MOD KC_TRANSPARENT
  37. # endif
  38. # ifndef RSPC_MOD
  39. # define RSPC_MOD KC_TRANSPARENT
  40. # endif
  41. #else
  42. # ifndef LSPO_MOD
  43. # define LSPO_MOD KC_LEFT_SHIFT
  44. # endif
  45. # ifndef RSPC_MOD
  46. # define RSPC_MOD KC_RIGHT_SHIFT
  47. # endif
  48. #endif
  49. // **********************************************************
  50. // Shift / paren setup
  51. #ifndef LSPO_KEYS
  52. # define LSPO_KEYS KC_LEFT_SHIFT, LSPO_MOD, LSPO_KEY
  53. #endif
  54. #ifndef RSPC_KEYS
  55. # define RSPC_KEYS KC_RIGHT_SHIFT, RSPC_MOD, RSPC_KEY
  56. #endif
  57. // Control / paren setup
  58. #ifndef LCPO_KEYS
  59. # define LCPO_KEYS KC_LEFT_CTRL, KC_LEFT_SHIFT, KC_9
  60. #endif
  61. #ifndef RCPC_KEYS
  62. # define RCPC_KEYS KC_RIGHT_CTRL, KC_RIGHT_SHIFT, KC_0
  63. #endif
  64. // Alt / paren setup
  65. #ifndef LAPO_KEYS
  66. # define LAPO_KEYS KC_LEFT_ALT, KC_LEFT_SHIFT, KC_9
  67. #endif
  68. #ifndef RAPC_KEYS
  69. # define RAPC_KEYS KC_RIGHT_ALT, KC_RIGHT_SHIFT, KC_0
  70. #endif
  71. // Shift / Enter setup
  72. #ifndef SFTENT_KEYS
  73. # define SFTENT_KEYS KC_RIGHT_SHIFT, KC_TRANSPARENT, SFTENT_KEY
  74. #endif
  75. static uint8_t sc_last = 0;
  76. static uint16_t sc_timer = 0;
  77. #ifdef SPACE_CADET_MODIFIER_CARRYOVER
  78. static uint8_t sc_mods = 0;
  79. #endif
  80. void perform_space_cadet(keyrecord_t *record, uint16_t sc_keycode, uint8_t holdMod, uint8_t tapMod, uint8_t keycode) {
  81. if (record->event.pressed) {
  82. sc_last = holdMod;
  83. sc_timer = timer_read();
  84. #ifdef SPACE_CADET_MODIFIER_CARRYOVER
  85. sc_mods = get_mods();
  86. #endif
  87. if (IS_MODIFIER_KEYCODE(holdMod)) {
  88. register_mods(MOD_BIT(holdMod));
  89. }
  90. } else {
  91. if (sc_last == holdMod && timer_elapsed(sc_timer) < GET_TAPPING_TERM(sc_keycode, record)) {
  92. if (holdMod != tapMod) {
  93. if (IS_MODIFIER_KEYCODE(holdMod)) {
  94. unregister_mods(MOD_BIT(holdMod));
  95. }
  96. if (IS_MODIFIER_KEYCODE(tapMod)) {
  97. register_mods(MOD_BIT(tapMod));
  98. }
  99. }
  100. #ifdef SPACE_CADET_MODIFIER_CARRYOVER
  101. set_weak_mods(sc_mods);
  102. #endif
  103. tap_code(keycode);
  104. #ifdef SPACE_CADET_MODIFIER_CARRYOVER
  105. clear_weak_mods();
  106. #endif
  107. if (IS_MODIFIER_KEYCODE(tapMod)) {
  108. unregister_mods(MOD_BIT(tapMod));
  109. }
  110. } else {
  111. if (IS_MODIFIER_KEYCODE(holdMod)) {
  112. unregister_mods(MOD_BIT(holdMod));
  113. }
  114. }
  115. }
  116. }
  117. bool process_space_cadet(uint16_t keycode, keyrecord_t *record) {
  118. switch (keycode) {
  119. case QK_SPACE_CADET_LEFT_SHIFT_PARENTHESIS_OPEN: {
  120. perform_space_cadet(record, keycode, LSPO_KEYS);
  121. return false;
  122. }
  123. case QK_SPACE_CADET_RIGHT_SHIFT_PARENTHESIS_CLOSE: {
  124. perform_space_cadet(record, keycode, RSPC_KEYS);
  125. return false;
  126. }
  127. case QK_SPACE_CADET_LEFT_CTRL_PARENTHESIS_OPEN: {
  128. perform_space_cadet(record, keycode, LCPO_KEYS);
  129. return false;
  130. }
  131. case QK_SPACE_CADET_RIGHT_CTRL_PARENTHESIS_CLOSE: {
  132. perform_space_cadet(record, keycode, RCPC_KEYS);
  133. return false;
  134. }
  135. case QK_SPACE_CADET_LEFT_ALT_PARENTHESIS_OPEN: {
  136. perform_space_cadet(record, keycode, LAPO_KEYS);
  137. return false;
  138. }
  139. case QK_SPACE_CADET_RIGHT_ALT_PARENTHESIS_CLOSE: {
  140. perform_space_cadet(record, keycode, RAPC_KEYS);
  141. return false;
  142. }
  143. case QK_SPACE_CADET_RIGHT_SHIFT_ENTER: {
  144. perform_space_cadet(record, keycode, SFTENT_KEYS);
  145. return false;
  146. }
  147. default: {
  148. if (record->event.pressed) {
  149. reset_space_cadet();
  150. }
  151. break;
  152. }
  153. }
  154. return true;
  155. }
  156. void reset_space_cadet() {
  157. sc_last = 0;
  158. }