process_caps_word.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright 2021-2022 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_caps_word.h"
  15. #include "process_auto_shift.h"
  16. #include "caps_word.h"
  17. #include "keycodes.h"
  18. #include "quantum_keycodes.h"
  19. #include "modifiers.h"
  20. #include "timer.h"
  21. #include "action_tapping.h"
  22. #include "action_util.h"
  23. #ifdef CAPS_WORD_INVERT_ON_SHIFT
  24. static uint8_t held_mods = 0;
  25. static bool handle_shift(uint16_t keycode, keyrecord_t* record) {
  26. switch (keycode) {
  27. case OSM(MOD_LSFT):
  28. keycode = KC_LSFT;
  29. break;
  30. case OSM(MOD_RSFT):
  31. keycode = KC_RSFT;
  32. break;
  33. # ifndef NO_ACTION_TAPPING
  34. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  35. if (record->tap.count == 0) { // Mod-tap key is held.
  36. switch (QK_MOD_TAP_GET_MODS(keycode)) {
  37. case MOD_LSFT:
  38. keycode = KC_LSFT;
  39. break;
  40. case MOD_RSFT:
  41. keycode = KC_RSFT;
  42. break;
  43. }
  44. }
  45. # endif // NO_ACTION_TAPPING
  46. }
  47. if (keycode == KC_LSFT || keycode == KC_RSFT) {
  48. const uint8_t mod = MOD_BIT(keycode);
  49. if (is_caps_word_on()) {
  50. if (record->event.pressed) {
  51. held_mods |= mod;
  52. } else {
  53. held_mods &= ~mod;
  54. }
  55. return false;
  56. } else if ((held_mods & mod) != 0) {
  57. held_mods &= ~mod;
  58. del_mods(mod);
  59. return record->event.pressed;
  60. }
  61. }
  62. return true;
  63. }
  64. #endif // CAPS_WORD_INVERT_ON_SHIFT
  65. bool process_caps_word(uint16_t keycode, keyrecord_t* record) {
  66. if (keycode == QK_CAPS_WORD_TOGGLE) {
  67. if (record->event.pressed) {
  68. caps_word_toggle();
  69. }
  70. return false;
  71. }
  72. #ifdef CAPS_WORD_INVERT_ON_SHIFT
  73. if (!handle_shift(keycode, record)) {
  74. return false;
  75. }
  76. #endif // CAPS_WORD_INVERT_ON_SHIFT
  77. #ifndef NO_ACTION_ONESHOT
  78. const uint8_t mods = get_mods() | get_oneshot_mods();
  79. #else
  80. const uint8_t mods = get_mods();
  81. #endif // NO_ACTION_ONESHOT
  82. if (!is_caps_word_on()) {
  83. // The following optionally turns on Caps Word by holding left and
  84. // right shifts or by double tapping left shift. This way Caps Word
  85. // may be used without needing a dedicated key and also without
  86. // needing combos or tap dance.
  87. #ifdef BOTH_SHIFTS_TURNS_ON_CAPS_WORD
  88. // Many keyboards enable the Command feature by default, which also
  89. // uses left+right shift. It can be configured to use a different
  90. // key combination by defining IS_COMMAND(). We make a non-fatal
  91. // warning if Command is enabled but IS_COMMAND() is *not* defined.
  92. # if defined(COMMAND_ENABLE) && !defined(IS_COMMAND)
  93. # pragma message "BOTH_SHIFTS_TURNS_ON_CAPS_WORD and Command should not be enabled at the same time, since both use the Left Shift + Right Shift key combination. Please disable Command, or ensure that `IS_COMMAND` is not set to (get_mods() == MOD_MASK_SHIFT)."
  94. # else
  95. if (mods == MOD_MASK_SHIFT
  96. # ifdef COMMAND_ENABLE
  97. // Don't activate Caps Word at the same time as Command.
  98. && !(IS_COMMAND())
  99. # endif // COMMAND_ENABLE
  100. ) {
  101. caps_word_on();
  102. }
  103. # endif // defined(COMMAND_ENABLE) && !defined(IS_COMMAND)
  104. #endif // BOTH_SHIFTS_TURNS_ON_CAPS_WORD
  105. #ifdef DOUBLE_TAP_SHIFT_TURNS_ON_CAPS_WORD
  106. // Double tapping left shift turns on Caps Word.
  107. //
  108. // NOTE: This works with KC_LSFT and one-shot left shift. It
  109. // wouldn't make sense with mod-tap or Space Cadet shift since
  110. // double tapping would of course trigger the tapping action.
  111. if (record->event.pressed) {
  112. static bool tapped = false;
  113. static uint16_t timer = 0;
  114. if (keycode == KC_LSFT || keycode == OSM(MOD_LSFT)) {
  115. if (tapped && !timer_expired(record->event.time, timer)) {
  116. // Left shift was double tapped, activate Caps Word.
  117. caps_word_on();
  118. }
  119. tapped = true;
  120. timer = record->event.time + GET_TAPPING_TERM(keycode, record);
  121. } else {
  122. tapped = false; // Reset when any other key is pressed.
  123. }
  124. }
  125. #endif // DOUBLE_TAP_SHIFT_TURNS_ON_CAPS_WORD
  126. return true;
  127. }
  128. #if CAPS_WORD_IDLE_TIMEOUT > 0
  129. caps_word_reset_idle_timer();
  130. #endif // CAPS_WORD_IDLE_TIMEOUT > 0
  131. // From here on, we only take action on press events.
  132. if (!record->event.pressed) {
  133. return true;
  134. }
  135. if (!(mods & ~(MOD_MASK_SHIFT | MOD_BIT(KC_RALT)))) {
  136. switch (keycode) {
  137. // Ignore MO, TO, TG, TT, and OSL layer switch keys.
  138. case QK_MOMENTARY ... QK_MOMENTARY_MAX:
  139. case QK_TO ... QK_TO_MAX:
  140. case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX:
  141. case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX:
  142. case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX:
  143. case QK_TRI_LAYER_LOWER ... QK_TRI_LAYER_UPPER:
  144. // Ignore AltGr.
  145. case KC_RALT:
  146. case OSM(MOD_RALT):
  147. return true;
  148. #ifndef NO_ACTION_TAPPING
  149. // Corresponding to mod keys above, a held mod-tap is handled as:
  150. // * For shift mods, pass KC_LSFT or KC_RSFT to
  151. // caps_word_press_user() to determine whether to continue.
  152. // * For Shift + AltGr (MOD_RSFT | MOD_RALT), pass RSFT(KC_RALT).
  153. // * AltGr (MOD_RALT) is ignored.
  154. // * Otherwise stop Caps Word.
  155. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  156. if (record->tap.count == 0) { // Mod-tap key is held.
  157. const uint8_t mods = QK_MOD_TAP_GET_MODS(keycode);
  158. switch (mods) {
  159. # ifndef CAPS_WORD_INVERT_ON_SHIFT
  160. case MOD_LSFT:
  161. keycode = KC_LSFT;
  162. break;
  163. case MOD_RSFT:
  164. keycode = KC_RSFT;
  165. break;
  166. # endif // CAPS_WORD_INVERT_ON_SHIFT
  167. case MOD_RSFT | MOD_RALT:
  168. keycode = RSFT(KC_RALT);
  169. break;
  170. case MOD_RALT:
  171. return true;
  172. default:
  173. caps_word_off();
  174. # ifdef CAPS_WORD_INVERT_ON_SHIFT
  175. add_mods(held_mods);
  176. # endif // CAPS_WORD_INVERT_ON_SHIFT
  177. return true;
  178. }
  179. } else {
  180. keycode = QK_MOD_TAP_GET_TAP_KEYCODE(keycode);
  181. }
  182. break;
  183. # ifndef NO_ACTION_LAYER
  184. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
  185. # endif // NO_ACTION_LAYER
  186. if (record->tap.count == 0) {
  187. return true;
  188. }
  189. keycode = QK_LAYER_TAP_GET_TAP_KEYCODE(keycode);
  190. break;
  191. #endif // NO_ACTION_TAPPING
  192. #ifdef SWAP_HANDS_ENABLE
  193. case QK_SWAP_HANDS ... QK_SWAP_HANDS_MAX:
  194. // Note: IS_SWAP_HANDS_KEYCODE() actually tests for the special action keycodes like SH_TOGG, SH_TT, ...,
  195. // which currently overlap the SH_T(kc) range.
  196. if (IS_SWAP_HANDS_KEYCODE(keycode)
  197. # ifndef NO_ACTION_TAPPING
  198. || record->tap.count == 0
  199. # endif // NO_ACTION_TAPPING
  200. ) {
  201. return true;
  202. }
  203. keycode = QK_SWAP_HANDS_GET_TAP_KEYCODE(keycode);
  204. break;
  205. #endif // SWAP_HANDS_ENABLE
  206. }
  207. #ifdef AUTO_SHIFT_ENABLE
  208. del_weak_mods(get_autoshift_state() ? ~MOD_BIT(KC_LSFT) : 0xff);
  209. #else
  210. clear_weak_mods();
  211. #endif // AUTO_SHIFT_ENABLE
  212. if (caps_word_press_user(keycode)) {
  213. #ifdef CAPS_WORD_INVERT_ON_SHIFT
  214. if (held_mods) {
  215. set_weak_mods(get_weak_mods() ^ MOD_BIT(KC_LSFT));
  216. }
  217. #endif // CAPS_WORD_INVERT_ON_SHIFT
  218. send_keyboard_report();
  219. return true;
  220. }
  221. }
  222. caps_word_off();
  223. #ifdef CAPS_WORD_INVERT_ON_SHIFT
  224. add_mods(held_mods);
  225. #endif // CAPS_WORD_INVERT_ON_SHIFT
  226. return true;
  227. }
  228. __attribute__((weak)) bool caps_word_press_user(uint16_t keycode) {
  229. switch (keycode) {
  230. // Keycodes that continue Caps Word, with shift applied.
  231. case KC_A ... KC_Z:
  232. case KC_MINS:
  233. add_weak_mods(MOD_BIT(KC_LSFT)); // Apply shift to next key.
  234. return true;
  235. // Keycodes that continue Caps Word, without shifting.
  236. case KC_1 ... KC_0:
  237. case KC_BSPC:
  238. case KC_DEL:
  239. case KC_UNDS:
  240. return true;
  241. default:
  242. return false; // Deactivate Caps Word.
  243. }
  244. }