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.

272 lines
8.5 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. __attribute__((weak)) void matrix_scan_keymap(void) {}
  80. void matrix_scan_user(void) {
  81. timeout_tick_timer();
  82. matrix_scan_keymap();
  83. }
  84. #endif // IDLE_TIMEOUT_ENABLE
  85. #ifdef ENCODER_ENABLE
  86. #ifndef DYNAMIC_KEYMAP_LAYER_COUNT
  87. #define DYNAMIC_KEYMAP_LAYER_COUNT 4 //default in case this is not already defined elsewhere
  88. #endif
  89. #ifndef ENCODER_DEFAULTACTIONS_INDEX
  90. #define ENCODER_DEFAULTACTIONS_INDEX 0 // can select encoder index if there are multiple encoders
  91. #endif
  92. void encoder_action_volume(bool clockwise) {
  93. if (clockwise)
  94. tap_code(KC_VOLU);
  95. else
  96. tap_code(KC_VOLD);
  97. }
  98. void encoder_action_mediatrack(bool clockwise) {
  99. if (clockwise)
  100. tap_code(KC_MEDIA_NEXT_TRACK);
  101. else
  102. tap_code(KC_MEDIA_PREV_TRACK);
  103. }
  104. void encoder_action_navword(bool clockwise) {
  105. if (clockwise)
  106. tap_code16(LCTL(KC_RGHT));
  107. else
  108. tap_code16(LCTL(KC_LEFT));
  109. }
  110. void encoder_action_navpage(bool clockwise) {
  111. if (clockwise)
  112. tap_code16(KC_PGUP);
  113. else
  114. tap_code16(KC_PGDN);
  115. }
  116. // LAYER HANDLING
  117. uint8_t selected_layer = 0;
  118. uint8_t get_selected_layer(void) {
  119. return selected_layer;
  120. }
  121. void encoder_action_layerchange(bool clockwise) {
  122. if (clockwise) {
  123. if(selected_layer < (DYNAMIC_KEYMAP_LAYER_COUNT - 1)) {
  124. selected_layer ++;
  125. layer_move(selected_layer);
  126. }
  127. } else {
  128. if (selected_layer > 0) {
  129. selected_layer --;
  130. layer_move(selected_layer);
  131. }
  132. }
  133. }
  134. #endif // ENCODER_ENABLE
  135. #if defined(ENCODER_ENABLE) && defined(ENCODER_DEFAULTACTIONS_ENABLE) // Encoder Functionality
  136. __attribute__((weak)) bool encoder_update_keymap(uint8_t index, bool clockwise) { return true; }
  137. bool encoder_update_user(uint8_t index, bool clockwise) {
  138. if (!encoder_update_keymap(index, clockwise)) { return false; }
  139. if (index != ENCODER_DEFAULTACTIONS_INDEX) {return true;} // exit if the index doesn't match
  140. uint8_t mods_state = get_mods();
  141. if (mods_state & MOD_BIT(KC_LSFT) ) { // If you are holding L shift, encoder changes layers
  142. encoder_action_layerchange(clockwise);
  143. } else if (mods_state & MOD_BIT(KC_RSFT) ) { // If you are holding R shift, Page up/dn
  144. unregister_mods(MOD_BIT(KC_RSFT));
  145. encoder_action_navpage(clockwise);
  146. register_mods(MOD_BIT(KC_RSFT));
  147. } else if (mods_state & MOD_BIT(KC_LCTL)) { // if holding Left Ctrl, navigate next/prev word
  148. encoder_action_navword(clockwise);
  149. } else if (mods_state & MOD_BIT(KC_LALT)) { // if holding Left Alt, change media next/prev track
  150. encoder_action_mediatrack(clockwise);
  151. } else {
  152. switch(get_highest_layer(layer_state)) {
  153. case _FN1:
  154. #ifdef IDLE_TIMEOUT_ENABLE
  155. timeout_update_threshold(clockwise);
  156. #endif
  157. break;
  158. default:
  159. encoder_action_volume(clockwise); // Otherwise it just changes volume
  160. break;
  161. }
  162. }
  163. return true;
  164. }
  165. #endif // ENCODER_ENABLE
  166. // PROCESS KEY CODES
  167. __attribute__ ((weak)) bool process_record_keymap(uint16_t keycode, keyrecord_t *record) { return true; }
  168. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  169. if (!process_record_keymap(keycode, record)) { return false; }
  170. switch (keycode) {
  171. case KC_00:
  172. if (record->event.pressed) {
  173. // when keycode KC_00 is pressed
  174. SEND_STRING("00");
  175. } else unregister_code16(keycode);
  176. break;
  177. case KC_WINLCK:
  178. if (record->event.pressed) {
  179. keymap_config.no_gui = !keymap_config.no_gui; //toggle status
  180. } else unregister_code16(keycode);
  181. break;
  182. #ifdef IDLE_TIMEOUT_ENABLE
  183. case RGB_TOI:
  184. if(record->event.pressed) {
  185. timeout_update_threshold(true);
  186. } else unregister_code16(keycode);
  187. break;
  188. case RGB_TOD:
  189. if(record->event.pressed) {
  190. timeout_update_threshold(false); //decrease timeout
  191. } else unregister_code16(keycode);
  192. break;
  193. #endif // IDLE_TIMEOUT_ENABLE
  194. #ifdef RGB_MATRIX_ENABLE
  195. case RGB_NITE:
  196. if(record->event.pressed) {
  197. rgb_nightmode = !rgb_nightmode;
  198. } else unregister_code16(keycode);
  199. break;
  200. #endif // RGB_MATRIX_ENABLE
  201. default:
  202. if (record->event.pressed) {
  203. #ifdef RGB_MATRIX_ENABLE
  204. rgb_matrix_enable();
  205. #endif
  206. #ifdef IDLE_TIMEOUT_ENABLE
  207. timeout_reset_timer(); //reset activity timer
  208. #endif
  209. }
  210. break;
  211. }
  212. return true;
  213. };
  214. // Turn on/off NUM LOCK if current state is different
  215. void activate_numlock(bool turn_on) {
  216. if (IS_HOST_LED_ON(USB_LED_NUM_LOCK) != turn_on) {
  217. tap_code(KC_NUMLOCK);
  218. }
  219. }
  220. // INITIAL STARTUP
  221. __attribute__ ((weak)) void keyboard_post_init_keymap(void) {}
  222. void keyboard_post_init_user(void) {
  223. keyboard_post_init_keymap();
  224. #ifdef STARTUP_NUMLOCK_ON
  225. activate_numlock(true); // turn on Num lock by default so that the numpad layer always has predictable results
  226. #endif // STARTUP_NUMLOC_ON
  227. #ifdef IDLE_TIMEOUT_ENABLE
  228. timeout_timer = timer_read(); // set inital time for ide timeout
  229. #endif
  230. }