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.

233 lines
7.0 KiB

  1. /* Copyright 2021 Jonavin Eng @Jonavin
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. #include QMK_KEYBOARD_H
  14. #include "jonavin.h"
  15. #ifdef TD_LSFT_CAPSLOCK_ENABLE
  16. // Tap once for shift, twice for Caps Lock but only if Win Key in not disabled
  17. void dance_LSFT_finished(qk_tap_dance_state_t *state, void *user_data) {
  18. if (state->count == 1 || keymap_config.no_gui) {
  19. register_code16(KC_LSFT);
  20. } else {
  21. register_code(KC_CAPS);
  22. }
  23. }
  24. void dance_LSFT_reset(qk_tap_dance_state_t *state, void *user_data) {
  25. if (state->count == 1 || keymap_config.no_gui) {
  26. unregister_code16(KC_LSFT);
  27. } else {
  28. unregister_code(KC_CAPS);
  29. }
  30. }
  31. qk_tap_dance_action_t tap_dance_actions[] = {
  32. // Tap once for shift, twice for Caps Lock
  33. [TD_LSFT_CAPSLOCK] = ACTION_TAP_DANCE_DOUBLE(KC_LSFT, KC_CAPS),
  34. [TD_LSFT_CAPS_WIN] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, dance_LSFT_finished, dance_LSFT_reset),
  35. };
  36. #endif // TD_LSFT_CAPSLOCK_ENABLE
  37. // RGB NIGHT MODE
  38. #ifdef RGB_MATRIX_ENABLE
  39. static bool rgb_nightmode = false;
  40. // Turn on/off NUM LOCK if current state is different
  41. void activate_rgb_nightmode (bool turn_on) {
  42. if (rgb_nightmode != turn_on) {
  43. rgb_nightmode = !rgb_nightmode;
  44. }
  45. }
  46. bool get_rgb_nightmode(void) {
  47. return rgb_nightmode;
  48. }
  49. #endif // RGB_MATRIX_ENABLE
  50. // TIMEOUTS
  51. #ifdef IDLE_TIMEOUT_ENABLE
  52. static uint16_t timeout_timer = 0;
  53. static uint16_t timeout_counter = 0; //in minute intervals
  54. static uint16_t timeout_threshold = TIMEOUT_THRESHOLD_DEFAULT;
  55. uint16_t get_timeout_threshold(void) {
  56. return timeout_threshold;
  57. }
  58. void timeout_reset_timer(void) {
  59. timeout_timer = timer_read();
  60. timeout_counter = 0;
  61. };
  62. void timeout_update_threshold(bool increase) {
  63. if (increase && timeout_threshold < TIMEOUT_THRESHOLD_MAX) timeout_threshold++;
  64. if (!increase && timeout_threshold > 0) timeout_threshold--;
  65. };
  66. void timeout_tick_timer(void) {
  67. if (timeout_threshold > 0) {
  68. if (timer_elapsed(timeout_timer) >= 60000) { // 1 minute tick
  69. timeout_counter++;
  70. timeout_timer = timer_read();
  71. }
  72. #ifdef RGB_MATRIX_ENABLE
  73. if (timeout_threshold > 0 && timeout_counter >= timeout_threshold) {
  74. rgb_matrix_disable_noeeprom();
  75. }
  76. #endif
  77. } // timeout_threshold = 0 will disable timeout
  78. }
  79. #endif // IDLE_TIMEOUT_ENABLE
  80. #if defined(ALTTAB_SCROLL_ENABLE) || defined(IDLE_TIMEOUT_ENABLE) // timer features
  81. __attribute__((weak)) void matrix_scan_keymap(void) {}
  82. void matrix_scan_user(void) {
  83. #ifdef ALTTAB_SCROLL_ENABLE
  84. encoder_tick_alttabscroll();
  85. #endif
  86. #ifdef IDLE_TIMEOUT_ENABLE
  87. timeout_tick_timer();
  88. #endif
  89. matrix_scan_keymap();
  90. }
  91. #endif // ALTTAB_SCROLL_ENABLE or IDLE_TIMEOUT_ENABLE
  92. // PROCESS KEY CODES
  93. __attribute__ ((weak)) bool process_record_keymap(uint16_t keycode, keyrecord_t *record) { return true; }
  94. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  95. if (!process_record_keymap(keycode, record)) { return false; }
  96. switch (keycode) {
  97. case KC_00:
  98. if (record->event.pressed) {
  99. // when keycode KC_00 is pressed
  100. SEND_STRING("00");
  101. } else unregister_code16(keycode);
  102. break;
  103. case KC_WINLCK:
  104. if (record->event.pressed) {
  105. keymap_config.no_gui = !keymap_config.no_gui; //toggle status
  106. } else unregister_code16(keycode);
  107. break;
  108. #ifdef IDLE_TIMEOUT_ENABLE
  109. case RGB_TOI:
  110. if(record->event.pressed) {
  111. timeout_update_threshold(true);
  112. } else unregister_code16(keycode);
  113. break;
  114. case RGB_TOD:
  115. if(record->event.pressed) {
  116. timeout_update_threshold(false); //decrease timeout
  117. } else unregister_code16(keycode);
  118. break;
  119. #endif // IDLE_TIMEOUT_ENABLE
  120. #ifdef RGB_MATRIX_ENABLE
  121. case RGB_NITE:
  122. if(record->event.pressed) {
  123. rgb_nightmode = !rgb_nightmode;
  124. } else unregister_code16(keycode);
  125. break;
  126. #endif // RGB_MATRIX_ENABLE
  127. #ifdef EMOTICON_ENABLE
  128. case EMO_SHRUG:
  129. if (record->event.pressed) SEND_STRING("`\\_(\"/)_/`");
  130. else unregister_code16(keycode);
  131. break;
  132. case EMO_CONFUSE:
  133. if (record->event.pressed) SEND_STRING("(*_*)");
  134. else unregister_code16(keycode);
  135. break;
  136. case EMO_TEARS:
  137. if (record->event.pressed) SEND_STRING("(T_T)");
  138. else unregister_code16(keycode);
  139. break;
  140. case EMO_NERVOUS:
  141. if (record->event.pressed) SEND_STRING("(~_~;)");
  142. else unregister_code16(keycode);
  143. break;
  144. case EMO_JOY:
  145. if (record->event.pressed) SEND_STRING("(^o^)");
  146. else unregister_code16(keycode);
  147. break;
  148. case EMO_SAD:
  149. if (record->event.pressed) SEND_STRING(":'-(");
  150. else unregister_code16(keycode);
  151. break;
  152. #endif // EMOTICON_ENABLE
  153. #ifdef ALTTAB_SCROLL_ENABLE
  154. case KC_TSTOG:
  155. if (record->event.pressed) encoder_toggle_alttabscroll();
  156. else unregister_code16(keycode);
  157. break;
  158. #endif // ALTTAB_SCROLL_ENABLE
  159. default:
  160. if (record->event.pressed) {
  161. #ifdef RGB_MATRIX_ENABLE
  162. rgb_matrix_enable();
  163. #endif
  164. #ifdef IDLE_TIMEOUT_ENABLE
  165. timeout_reset_timer(); //reset activity timer
  166. #endif
  167. }
  168. break;
  169. }
  170. return true;
  171. };
  172. uint16_t get_tapping_term(uint16_t keycode, keyrecord_t *record) {
  173. switch (keycode) {
  174. case KC_SFTUP:
  175. return 300;
  176. case KC_RAISESPC:
  177. case KC_LOWERSPC:
  178. return 450;
  179. default:
  180. return TAPPING_TERM;
  181. }
  182. }
  183. // Turn on/off NUM LOCK if current state is different
  184. void activate_numlock(bool turn_on) {
  185. if (IS_HOST_LED_ON(USB_LED_NUM_LOCK) != turn_on) {
  186. tap_code(KC_NUMLOCK);
  187. }
  188. }
  189. // INITIAL STARTUP
  190. __attribute__ ((weak)) void keyboard_post_init_keymap(void) {}
  191. void keyboard_post_init_user(void) {
  192. keyboard_post_init_keymap();
  193. #ifdef STARTUP_NUMLOCK_ON
  194. activate_numlock(true); // turn on Num lock by default so that the numpad layer always has predictable results
  195. #endif // STARTUP_NUMLOC_ON
  196. #ifdef IDLE_TIMEOUT_ENABLE
  197. timeout_timer = timer_read(); // set inital time for ide timeout
  198. #endif
  199. }