You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
5.5 KiB

  1. /*
  2. Copyright 2021 Jakob Hærvig <jakob.haervig@gmail.com>
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "haervig.h"
  15. #ifdef DANISH_ENABLE
  16. // These indicate if left and right shift are physically pressed
  17. bool lshift = false;
  18. bool rshift = false;
  19. // Interrupt and times for space cadet shift
  20. bool lshiftp = false;
  21. bool rshiftp = false;
  22. uint16_t lshift_timer = 0;
  23. uint16_t rshift_timer = 0;
  24. // Number of items that are saved in prev_kcs
  25. uint8_t prev_indx = 0;
  26. // Used to save the last 6 actual keycodes activated by frankenkeycodes
  27. uint16_t prev_kcs[6] = {0, 0, 0, 0, 0, 0};
  28. // If true the deadkey characters grave and circonflexe are not automatically escaped
  29. bool esct = false;
  30. /*
  31. Used to add a keycode to a prev_kcs to remember it.
  32. When full the last code gets discarded and replaced by
  33. the new one.
  34. */
  35. void add_to_prev(uint16_t kc){
  36. for (int i=0; i<prev_indx; i++){
  37. if (kc == prev_kcs[i])
  38. return;
  39. }
  40. if (prev_indx == 6){
  41. for (int i=5; i>0; i--){
  42. prev_kcs[i] = prev_kcs[i-1];
  43. }
  44. prev_kcs[0] = kc;
  45. } else {
  46. prev_kcs[prev_indx] = kc;
  47. prev_indx++;
  48. }
  49. }
  50. /*
  51. Unregisters all codes saved in prev_kcs and resets prev_indx.
  52. gets called on multiple occasions mainly when shift is released
  53. and when frankenkeycodes are pressed. Prevents output of
  54. wrong characters when really specific key combinations
  55. that would never occur during normal usage are pressed.
  56. */
  57. void unreg_prev(void){
  58. if (prev_indx == 0)
  59. return;
  60. for (int i=0; i<prev_indx; i++){
  61. unregister_code(prev_kcs[i]);
  62. }
  63. prev_indx = 0;
  64. }
  65. #endif
  66. // Interrupt and times for Nav/Esc
  67. bool navesc = false;
  68. uint16_t navesc_timer = 0;
  69. // Interrupts all timers
  70. void timer_timeout(void){
  71. #ifdef DANISH_ENABLE
  72. lshiftp = false;
  73. rshiftp = false;
  74. #endif
  75. navesc = false;
  76. timer_timeout_keymap();
  77. }
  78. __attribute__((weak))
  79. void timer_timeout_keymap(void){
  80. }
  81. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  82. switch (keycode) {
  83. case KC_LGUI:
  84. case KC_RGUI:
  85. if (record->event.pressed)
  86. timer_timeout();
  87. return true;
  88. case CU_NAV:
  89. if(record->event.pressed) {
  90. navesc = true;
  91. navesc_timer = timer_read();
  92. layer_on(_NAV);
  93. } else {
  94. if (timer_elapsed(navesc_timer) < TAPPING_TERM && navesc) {
  95. tap_code(KC_ESC);
  96. }
  97. layer_off(_NAV);
  98. }
  99. return false;
  100. #ifdef DANISH_ENABLE
  101. case CU_LSFT:
  102. if(record->event.pressed) {
  103. lshiftp = true;
  104. lshift_timer = timer_read();
  105. unregister_code(KC_LSFT);
  106. register_code(KC_LSFT);
  107. lshift = true;
  108. } else {
  109. if (timer_elapsed(lshift_timer) < TAPPING_TERM && lshiftp) {
  110. register_code(KC_LSFT);
  111. tap_code(KC_8);
  112. unregister_code(KC_LSFT);
  113. }
  114. unreg_prev();
  115. if (!rshift)
  116. unregister_code(KC_LSFT);
  117. lshift = false;
  118. }
  119. return false;
  120. case CU_RSFT:
  121. if(record->event.pressed) {
  122. rshiftp = true;
  123. rshift_timer = timer_read();
  124. unregister_code(KC_LSFT);
  125. register_code(KC_LSFT);
  126. rshift = true;
  127. } else {
  128. if (timer_elapsed(rshift_timer) < TAPPING_TERM && rshiftp) {
  129. register_code(KC_LSFT);
  130. tap_code(KC_9);
  131. unregister_code(KC_LSFT);
  132. }
  133. unreg_prev();
  134. if (!lshift)
  135. unregister_code(KC_LSFT);
  136. rshift = false;
  137. }
  138. return false;
  139. case CU_COMM:
  140. SHIFT_NO(DK_COMM, KC_GRV)
  141. case CU_DOT:
  142. SHIFT_NORM(DK_DOT, KC_GRV)
  143. case CU_SLSH:
  144. SHIFT_ALL(DK_7, KC_MINS)
  145. case CU_SCLN:
  146. SHIFT_ALL(DK_COMM, DK_DOT)
  147. case CU_QUOT:
  148. SHIFT_NORM(DK_QUOT, DK_2)
  149. case CU_2:
  150. NORM_ALGR(DK_2, KC_NUHS)
  151. case CU_4:
  152. if (record->event.pressed) { \
  153. timer_timeout(); \
  154. if (lshift || rshift) { \
  155. register_code(KC_LSFT); \
  156. register_code(KC_ALGR); \
  157. unregister_code(KC_3); \
  158. tap_code(KC_3); \
  159. unregister_code(KC_3); \
  160. } else { \
  161. unregister_code(KC_4); \
  162. tap_code(KC_4); \
  163. } \
  164. unregister_code(KC_ALGR); \
  165. unregister_code(KC_LSFT); \
  166. } \
  167. return false;
  168. case CU_6:
  169. SHIFT_NORM(DK_6, KC_RBRC)
  170. case CU_7:
  171. SHIFT_NORM(DK_7, DK_6)
  172. case CU_8:
  173. SHIFT_NORM(DK_8, KC_NUHS)
  174. case CU_9:
  175. SHIFT_NORM(DK_9, DK_8)
  176. case CU_0:
  177. SHIFT_NORM(DK_0, DK_9)
  178. case CU_MINS:
  179. SHIFT_NORM(KC_SLSH, KC_SLSH)
  180. case CU_EQL:
  181. SHIFT_SWITCH(DK_0, DK_PLUS)
  182. case CU_BSPC:
  183. SHIFT_NO(KC_BSPC, KC_DEL)
  184. case CU_LBRC:
  185. NORM_ALGRSHIFT(DK_8, DK_8)
  186. case CU_RBRC:
  187. NORM_ALGRSHIFT(DK_9, DK_9)
  188. case CU_BSLS:
  189. ALGR_SWITCH(DK_7, DK_I)
  190. case KC_LCTL:
  191. case KC_RCTL:
  192. if(!record->event.pressed) {
  193. timer_timeout();
  194. unregister_code(KC_Z);
  195. unregister_code(KC_Y);
  196. }
  197. return true;
  198. #endif
  199. default:
  200. if(record->event.pressed) {
  201. timer_timeout();
  202. #ifdef DANISH_ENABLE
  203. if (lshift || rshift)
  204. register_code(KC_LSFT);
  205. else
  206. unregister_code(KC_LSFT);
  207. #endif
  208. }
  209. return process_record_keymap(keycode, record);
  210. }
  211. }
  212. __attribute__((weak))
  213. bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
  214. return true;
  215. }