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.

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