casemodes.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Copyright 2021 Andrew Rae ajrae.nv@gmail.com @andrewjrae
  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 "casemodes.h"
  17. /* The caps word concept started with me @iaap on splitkb.com discord.
  18. * However it has been implemented and extended by many splitkb.com users:
  19. * - @theol0403 made many improvements to initial implementation
  20. * - @precondition used caps lock rather than shifting
  21. * - @dnaq his own implementation which also used caps lock
  22. * - @sevanteri added underscores on spaces
  23. * - @metheon extended on @sevanteri's work and added specific modes for
  24. * snake_case and SCREAMING_SNAKE_CASE
  25. * - @baffalop came up with the idea for xcase, which he implements in his own
  26. * repo, however this is implemented by @iaap with support also for one-shot-shift.
  27. * - @sevanteri
  28. * - fixed xcase waiting mode to allow more modified keys and keys from other layers.
  29. * - Added @baffalop's separator defaulting on first keypress, with a
  30. * configurable default separator and overrideable function to determine
  31. * if the default should be used.
  32. */
  33. #ifndef DEFAULT_XCASE_SEPARATOR
  34. # define DEFAULT_XCASE_SEPARATOR KC_UNDS
  35. #endif
  36. #define IS_OSM(keycode) (keycode >= QK_ONE_SHOT_MOD && keycode <= QK_ONE_SHOT_MOD_MAX)
  37. // bool to keep track of the caps word state
  38. static bool caps_word_on = false;
  39. // enum to keep track of the xcase state
  40. static enum xcase_state xcase_state = XCASE_OFF;
  41. // the keycode of the xcase delimiter
  42. static uint16_t xcase_delimiter;
  43. // the number of keys to the last delimiter
  44. static int8_t distance_to_last_delim = -1;
  45. // Check whether caps word is on
  46. bool caps_word_enabled(void) { return caps_word_on; }
  47. // Enable caps word
  48. void enable_caps_word(void) {
  49. caps_word_on = true;
  50. #ifndef CAPSWORD_USE_SHIFT
  51. if (!host_keyboard_led_state().caps_lock) {
  52. tap_code(KC_CAPS);
  53. }
  54. #endif
  55. }
  56. // Disable caps word
  57. void disable_caps_word(void) {
  58. caps_word_on = false;
  59. #ifndef CAPSWORD_USE_SHIFT
  60. if (host_keyboard_led_state().caps_lock) {
  61. tap_code(KC_CAPS);
  62. }
  63. #else
  64. unregister_mods(MOD_LSFT);
  65. #endif
  66. }
  67. // Toggle caps word
  68. void toggle_caps_word(void) {
  69. if (caps_word_on) {
  70. disable_caps_word();
  71. } else {
  72. enable_caps_word();
  73. }
  74. }
  75. // Get xcase state
  76. enum xcase_state get_xcase_state(void) { return xcase_state; }
  77. // Enable xcase and pickup the next keystroke as the delimiter
  78. void enable_xcase(void) { xcase_state = XCASE_WAIT; }
  79. // Enable xcase with the specified delimiter
  80. void enable_xcase_with(uint16_t delimiter) {
  81. xcase_state = XCASE_ON;
  82. xcase_delimiter = delimiter;
  83. distance_to_last_delim = -1;
  84. }
  85. // Disable xcase
  86. void disable_xcase(void) { xcase_state = XCASE_OFF; }
  87. // Place the current xcase delimiter
  88. static void place_delimiter(void) {
  89. if (IS_OSM(xcase_delimiter)) {
  90. // apparently set_oneshot_mods() is dumb and doesn't deal with handedness for you
  91. uint8_t mods = xcase_delimiter & 0x10 ? (xcase_delimiter & 0x0F) << 4 : xcase_delimiter & 0xFF;
  92. set_oneshot_mods(mods);
  93. } else {
  94. tap_code16(xcase_delimiter);
  95. }
  96. }
  97. // Removes a delimiter, used for double tap space exit
  98. static void remove_delimiter(void) {
  99. if (IS_OSM(xcase_delimiter)) {
  100. clear_oneshot_mods();
  101. } else {
  102. tap_code(KC_BSPC);
  103. }
  104. }
  105. // overrideable function to determine whether the case mode should stop
  106. __attribute__((weak)) bool terminate_case_modes(uint16_t keycode, const keyrecord_t *record) {
  107. switch (keycode) {
  108. // Keycodes to ignore (don't disable caps word)
  109. case KC_A ... KC_Z:
  110. case KC_1 ... KC_0:
  111. case KC_MINS:
  112. case KC_BSPC:
  113. // If mod chording disable the mods
  114. if (record->event.pressed && (get_mods() != 0)) {
  115. return true;
  116. }
  117. break;
  118. case KC_UNDS:
  119. // Allow to be pressed with or without a modifier (prob w/ shift)
  120. break;
  121. default:
  122. if (record->event.pressed) {
  123. return true;
  124. }
  125. break;
  126. }
  127. return false;
  128. }
  129. /* overrideable function to determine whether to use the default separator on
  130. * first keypress when waiting for the separator. */
  131. __attribute__((weak)) bool use_default_xcase_separator(uint16_t keycode, const keyrecord_t *record) {
  132. // for example:
  133. /* switch (keycode) { */
  134. /* case KC_A ... KC_Z: */
  135. /* case KC_1 ... KC_0: */
  136. /* return true; */
  137. /* } */
  138. return false;
  139. }
  140. bool process_case_modes(uint16_t keycode, const keyrecord_t *record) {
  141. if (caps_word_on || xcase_state) {
  142. if ((QK_MOD_TAP <= keycode && keycode <= QK_MOD_TAP_MAX) || (QK_LAYER_TAP <= keycode && keycode <= QK_LAYER_TAP_MAX)) {
  143. // Earlier return if this has not been considered tapped yet
  144. if (record->tap.count == 0) return true;
  145. keycode = keycode & 0xFF;
  146. }
  147. if (keycode >= QK_LAYER_TAP && keycode <= QK_ONE_SHOT_LAYER_MAX) {
  148. // let special keys and normal modifiers go through
  149. return true;
  150. }
  151. if (xcase_state == XCASE_WAIT) {
  152. // grab the next input to be the delimiter
  153. if (use_default_xcase_separator(keycode, record)) {
  154. enable_xcase_with(DEFAULT_XCASE_SEPARATOR);
  155. } else if (record->event.pressed) {
  156. // factor in mods
  157. if (get_mods() & MOD_MASK_SHIFT) {
  158. keycode = LSFT(keycode);
  159. } else if (get_mods() & MOD_BIT(KC_RALT)) {
  160. keycode = RALT(keycode);
  161. }
  162. enable_xcase_with(keycode);
  163. return false;
  164. } else {
  165. if (IS_OSM(keycode)) {
  166. // this catches the OSM release if no other key was pressed
  167. set_oneshot_mods(0);
  168. enable_xcase_with(keycode);
  169. return false;
  170. }
  171. // let other special keys go through
  172. return true;
  173. }
  174. }
  175. if (record->event.pressed) {
  176. // handle xcase mode
  177. if (xcase_state == XCASE_ON) {
  178. // place the delimiter if space is tapped
  179. if (keycode == KC_SPACE) {
  180. if (distance_to_last_delim != 0) {
  181. place_delimiter();
  182. distance_to_last_delim = 0;
  183. return false;
  184. }
  185. // remove the delimiter and disable modes
  186. else {
  187. remove_delimiter();
  188. disable_xcase();
  189. disable_caps_word();
  190. return true;
  191. }
  192. }
  193. // decrement distance to delimiter on back space
  194. else if (keycode == KC_BSPC) {
  195. --distance_to_last_delim;
  196. }
  197. // don't increment distance to last delim if negative
  198. else if (distance_to_last_delim >= 0) {
  199. // puts back a one shot delimiter if you we're back to the delimiter pos
  200. if (distance_to_last_delim == 0 && (IS_OSM(xcase_delimiter))) {
  201. place_delimiter();
  202. }
  203. ++distance_to_last_delim;
  204. }
  205. } // end XCASE_ON
  206. // check if the case modes have been terminated
  207. if (terminate_case_modes(keycode, record)) {
  208. disable_caps_word();
  209. disable_xcase();
  210. }
  211. #ifdef CAPSWORD_USE_SHIFT
  212. else if (keycode >= KC_A && keycode <= KC_Z) {
  213. tap_code16(LSFT(keycode));
  214. return false;
  215. }
  216. #endif
  217. } // end if event.pressed
  218. return true;
  219. }
  220. return true;
  221. }