oneshot.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include QMK_KEYBOARD_H
  2. #include USERSPACE_H
  3. #include "oneshot.h"
  4. #ifdef ONESHOT_MOD_ENABLE
  5. /* -------------------------------------------- */
  6. // Add to process_record_user.
  7. /* int8_t keycode_consumed = 0; */
  8. /* #ifdef ONESHOT_ENABLE */
  9. /* keycode_consumed += update_oneshot_modifiers(keycode, record, keycode_consumed); */
  10. /* #endif */
  11. /* -------------------------------------------- */
  12. #define ONESHOT(KEYCODE, MOD) case KEYCODE: return MOD;
  13. #define A_KEY(KEYCODE) case KEYCODE:
  14. #define BLANK(...)
  15. #define CANCEL_KEY BLANK
  16. #define IGNORE_KEY BLANK
  17. // the basic states a oneshot modifier can be in
  18. typedef enum {
  19. ONESHOT_STATE_OFF = 0,
  20. ONESHOT_STATE_PRESSED = 1,
  21. ONESHOT_STATE_QUEUED = 2,
  22. ONESHOT_STATE_CAPSWORD = 3,
  23. ONESHOT_STATE_LOCK = 4,
  24. ONESHOT_STATE_END_PRESSED = 5,
  25. } oneshot_state;
  26. oneshot_state modifiers_state_transitions_normal[5] = {ONESHOT_STATE_PRESSED, ONESHOT_STATE_QUEUED, ONESHOT_STATE_LOCK, ONESHOT_STATE_END_PRESSED, ONESHOT_STATE_END_PRESSED};
  27. static oneshot_state modifiers_with_state[ONESHOT_MOD_COUNT] = {
  28. ONESHOT_STATE_OFF, ONESHOT_STATE_OFF, ONESHOT_STATE_OFF, ONESHOT_STATE_OFF, ONESHOT_STATE_OFF, ONESHOT_STATE_OFF, ONESHOT_STATE_OFF, ONESHOT_STATE_OFF,
  29. };
  30. // oneshot mods always get registered immediately to the operating system, but we also
  31. // need to keep track if the mod(s) got combined with a normal key (applied)
  32. static bool unapplied_mods_present = false;
  33. // keycode of the last pressed 'normal' key which haven't been released yet
  34. static uint16_t repeating_normal_key = 0;
  35. // utility functions (implemented at the bottom of this file)
  36. static void set_modifier_state(oneshot_mod osmod, oneshot_state new_state);
  37. static int8_t set_modifier_state_all(oneshot_state new_state);
  38. static void set_modifier_state_all_from_to(oneshot_state oneshot_state_from, oneshot_state oneshot_state_to);
  39. static bool all_modifiers_are_off(void);
  40. int8_t turnoff_oneshot_modifiers() {
  41. return set_modifier_state_all(ONESHOT_STATE_OFF);
  42. }
  43. // see comment in corresponding headerfile
  44. int8_t update_oneshot_modifiers(uint16_t keycode, keyrecord_t *record, int8_t keycode_consumed) {
  45. // cancel keys
  46. if (is_oneshot_modifier_cancel_key(keycode) && record->event.pressed) {
  47. if (keycode_consumed == 0) {
  48. unapplied_mods_present = false;
  49. keycode_consumed += set_modifier_state_all(ONESHOT_STATE_OFF);
  50. } else {
  51. keycode_consumed = 0;
  52. }
  53. return keycode_consumed;
  54. }
  55. // ignored keys
  56. if (is_oneshot_modifier_ignored_key(keycode)) {
  57. return keycode_consumed;
  58. }
  59. oneshot_mod osmod = get_modifier_for_trigger_key(keycode);
  60. // trigger keys
  61. if (osmod != ONESHOT_NONE) {
  62. oneshot_state state = modifiers_with_state[osmod];
  63. if (record->event.pressed) {
  64. if (state == ONESHOT_STATE_OFF) {
  65. unapplied_mods_present = (repeating_normal_key == 0);
  66. }
  67. oneshot_state tostate = modifiers_state_transitions_normal[state];
  68. set_modifier_state(osmod, tostate);
  69. } else {
  70. if (state == ONESHOT_STATE_PRESSED) {
  71. if (!unapplied_mods_present) {
  72. set_modifier_state(osmod, ONESHOT_STATE_OFF);
  73. } else {
  74. set_modifier_state(osmod, ONESHOT_STATE_QUEUED);
  75. }
  76. } else if (state == ONESHOT_STATE_END_PRESSED) {
  77. set_modifier_state(osmod, ONESHOT_STATE_OFF);
  78. }
  79. }
  80. }
  81. // normal keys
  82. else {
  83. if (record->event.pressed) {
  84. if (!all_modifiers_are_off()) {
  85. if (unapplied_mods_present) {
  86. unapplied_mods_present = false;
  87. } else {
  88. unregister_code(repeating_normal_key);
  89. set_modifier_state_all_from_to(ONESHOT_STATE_QUEUED, ONESHOT_STATE_OFF);
  90. }
  91. }
  92. repeating_normal_key = keycode;
  93. } else {
  94. if (!all_modifiers_are_off()) {
  95. unregister_code(keycode);
  96. set_modifier_state_all_from_to(ONESHOT_STATE_QUEUED, ONESHOT_STATE_OFF);
  97. }
  98. repeating_normal_key = 0;
  99. }
  100. }
  101. return 0;
  102. }
  103. // implementation of utility functions
  104. // registers/unregisters a mod to the operating system on state change if necessary
  105. void update_modifier(oneshot_mod osmod, oneshot_state previous_state, oneshot_state current_state) {
  106. if (previous_state == ONESHOT_STATE_OFF) {
  107. register_code(KC_LCTRL + osmod);
  108. } else {
  109. if (current_state == ONESHOT_STATE_OFF) {
  110. unregister_code(KC_LCTRL + osmod);
  111. }
  112. }
  113. }
  114. void set_modifier_state(oneshot_mod osmod, oneshot_state new_state) {
  115. oneshot_state previous_state = modifiers_with_state[osmod];
  116. if (previous_state != new_state) {
  117. modifiers_with_state[osmod] = new_state;
  118. update_modifier(osmod, previous_state, new_state);
  119. }
  120. }
  121. int8_t set_modifier_state_all(oneshot_state new_state) {
  122. int8_t c = 0;
  123. for (int8_t i = 0; i < ONESHOT_MOD_COUNT; i++) {
  124. oneshot_state previous_state = modifiers_with_state[i];
  125. if (previous_state != new_state) {
  126. modifiers_with_state[i] = new_state;
  127. update_modifier(i, previous_state, new_state);
  128. c += 1;
  129. }
  130. }
  131. return c;
  132. }
  133. void set_modifier_state_all_from_to(oneshot_state oneshot_state_from, oneshot_state oneshot_state_to) {
  134. for (int8_t i = 0; i < ONESHOT_MOD_COUNT; i++) {
  135. if (modifiers_with_state[i] == oneshot_state_from) {
  136. modifiers_with_state[i] = oneshot_state_to;
  137. update_modifier(i, oneshot_state_from, oneshot_state_to);
  138. }
  139. }
  140. }
  141. bool all_modifiers_are_off() {
  142. for (int8_t i = 0; i < ONESHOT_MOD_COUNT; i++) {
  143. if (modifiers_with_state[i] != ONESHOT_STATE_OFF) {
  144. return false;
  145. }
  146. }
  147. return true;
  148. }
  149. oneshot_mod get_modifier_for_trigger_key(uint16_t keycode)
  150. {
  151. switch (keycode)
  152. {
  153. #include "oneshot.def"
  154. return true;
  155. default:
  156. return ONESHOT_NONE;
  157. }
  158. }
  159. // turn off the oneshot macros
  160. #undef ONESHOT
  161. #define ONESHOT BLANK
  162. #define NSHOT BLANK
  163. #undef CANCEL_KEY
  164. #undef IGNORE_KEY
  165. #define CANCEL_KEY A_KEY
  166. #define IGNORE_KEY BLANK
  167. bool is_oneshot_modifier_cancel_key(uint16_t keycode) {
  168. switch (keycode) {
  169. #include "oneshot.def"
  170. return true;
  171. default:
  172. return false;
  173. }
  174. }
  175. #undef CANCEL_KEY
  176. #undef IGNORE_KEY
  177. #define CANCEL_KEY BLANK
  178. #define IGNORE_KEY A_KEY
  179. bool is_oneshot_modifier_ignored_key(uint16_t keycode) {
  180. switch (keycode) {
  181. #include "oneshot.def"
  182. return true;
  183. default:
  184. return false;
  185. }
  186. }
  187. #endif