logo

qmk_firmware

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

process_caps_word.c (9834B)


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