process_space_cadet.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #ifndef TAPPING_TERM
  18. #define TAPPING_TERM 200
  19. #endif
  20. // ********** OBSOLETE DEFINES, STOP USING! (pls?) **********
  21. // Shift / paren setup
  22. #ifndef LSPO_KEY
  23. #define LSPO_KEY KC_9
  24. #endif
  25. #ifndef RSPC_KEY
  26. #define RSPC_KEY KC_0
  27. #endif
  28. // Shift / Enter setup
  29. #ifndef SFTENT_KEY
  30. #define SFTENT_KEY KC_ENT
  31. #endif
  32. #ifdef DISABLE_SPACE_CADET_MODIFIER
  33. #ifndef LSPO_MOD
  34. #define LSPO_MOD KC_TRNS
  35. #endif
  36. #ifndef RSPC_MOD
  37. #define RSPC_MOD KC_TRNS
  38. #endif
  39. #else
  40. #ifndef LSPO_MOD
  41. #define LSPO_MOD KC_LSFT
  42. #endif
  43. #ifndef RSPC_MOD
  44. #define RSPC_MOD KC_RSFT
  45. #endif
  46. #endif
  47. // **********************************************************
  48. // Shift / paren setup
  49. #ifndef LSPO_KEYS
  50. #define LSPO_KEYS KC_LSFT, LSPO_MOD, LSPO_KEY
  51. #endif
  52. #ifndef RSPC_KEYS
  53. #define RSPC_KEYS KC_RSFT, RSPC_MOD, RSPC_KEY
  54. #endif
  55. // Control / paren setup
  56. #ifndef LCPO_KEYS
  57. #define LCPO_KEYS KC_LCTL, KC_LCTL, KC_9
  58. #endif
  59. #ifndef RCPO_KEYS
  60. #define RCPO_KEYS KC_RCTL, KC_RCTL, KC_0
  61. #endif
  62. // Alt / paren setup
  63. #ifndef LAPO_KEYS
  64. #define LAPO_KEYS KC_LALT, KC_LALT, KC_9
  65. #endif
  66. #ifndef RAPO_KEYS
  67. #define RAPO_KEYS KC_RALT, KC_RALT, KC_0
  68. #endif
  69. // Shift / Enter setup
  70. #ifndef SFTENT_KEYS
  71. #define SFTENT_KEYS KC_RSFT, KC_TRNS, SFTENT_KEY
  72. #endif
  73. static uint8_t sc_last = 0;
  74. static uint16_t sc_timer = 0;
  75. void perform_space_cadet(keyrecord_t *record, uint8_t normalMod, uint8_t tapMod, uint8_t keycode) {
  76. if (record->event.pressed) {
  77. sc_last = normalMod;
  78. sc_timer = timer_read ();
  79. if (IS_MOD(normalMod)) {
  80. register_mods(MOD_BIT(normalMod));
  81. }
  82. }
  83. else {
  84. if (IS_MOD(normalMod)) {
  85. unregister_mods(MOD_BIT(normalMod));
  86. }
  87. if (sc_last == normalMod && timer_elapsed(sc_timer) < TAPPING_TERM) {
  88. if (IS_MOD(tapMod)) {
  89. register_mods(MOD_BIT(tapMod));
  90. }
  91. tap_code(keycode);
  92. if (IS_MOD(tapMod)) {
  93. unregister_mods(MOD_BIT(tapMod));
  94. }
  95. }
  96. }
  97. }
  98. bool process_space_cadet(uint16_t keycode, keyrecord_t *record) {
  99. switch(keycode) {
  100. case KC_LSPO: {
  101. perform_space_cadet(record, LSPO_KEYS);
  102. return false;
  103. }
  104. case KC_RSPC: {
  105. perform_space_cadet(record, RSPC_KEYS);
  106. return false;
  107. }
  108. case KC_LCPO: {
  109. perform_space_cadet(record, LCPO_KEYS);
  110. return false;
  111. }
  112. case KC_RCPC: {
  113. perform_space_cadet(record, RCPO_KEYS);
  114. return false;
  115. }
  116. case KC_LAPO: {
  117. perform_space_cadet(record, LAPO_KEYS);
  118. return false;
  119. }
  120. case KC_RAPC: {
  121. perform_space_cadet(record, RAPO_KEYS);
  122. return false;
  123. }
  124. case KC_SFTENT: {
  125. perform_space_cadet(record, SFTENT_KEYS);
  126. return false;
  127. }
  128. default: {
  129. sc_last = 0;
  130. break;
  131. }
  132. }
  133. return true;
  134. }