jonavin.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. // TIMEOUTS
  38. #ifdef IDLE_TIMEOUT_ENABLE
  39. static uint16_t timeout_timer = 0;
  40. static uint16_t timeout_counter = 0; //in minute intervals
  41. static uint16_t timeout_threshold = TIMEOUT_THRESHOLD_DEFAULT;
  42. uint16_t get_timeout_threshold(void) {
  43. return timeout_threshold;
  44. }
  45. void timeout_reset_timer(void) {
  46. timeout_timer = timer_read();
  47. timeout_counter = 0;
  48. };
  49. void timeout_update_threshold(bool increase) {
  50. if (increase && timeout_threshold < TIMEOUT_THRESHOLD_MAX) timeout_threshold++;
  51. if (!increase && timeout_threshold > 0) timeout_threshold--;
  52. };
  53. void timeout_tick_timer(void) {
  54. if (timeout_threshold > 0) {
  55. if (timer_elapsed(timeout_timer) >= 60000) { // 1 minute tick
  56. timeout_counter++;
  57. timeout_timer = timer_read();
  58. }
  59. #ifdef RGB_MATRIX_ENABLE
  60. if (timeout_threshold > 0 && timeout_counter >= timeout_threshold) {
  61. rgb_matrix_disable_noeeprom();
  62. }
  63. #endif
  64. } // timeout_threshold = 0 will disable timeout
  65. }
  66. __attribute__((weak)) void matrix_scan_keymap(void) {}
  67. void matrix_scan_user(void) {
  68. timeout_tick_timer();
  69. matrix_scan_keymap();
  70. }
  71. #endif // IDLE_TIMEOUT_ENABLE
  72. #if defined(ENCODER_ENABLE) && defined(ENCODER_DEFAULTACTIONS_ENABLE) // Encoder Functionality
  73. #ifndef DYNAMIC_KEYMAP_LAYER_COUNT
  74. #define DYNAMIC_KEYMAP_LAYER_COUNT 4 //default in case this is not already defined elsewhere
  75. #endif
  76. #ifndef ENCODER_DEFAULTACTIONS_INDEX
  77. #define ENCODER_DEFAULTACTIONS_INDEX 0 // can select encoder index if there are multiple encoders
  78. #endif
  79. uint8_t selected_layer = 0;
  80. __attribute__((weak)) bool encoder_update_keymap(uint8_t index, bool clockwise) { return true; }
  81. bool encoder_update_user(uint8_t index, bool clockwise) {
  82. if (!encoder_update_keymap(index, clockwise)) { return false; }
  83. if (index != ENCODER_DEFAULTACTIONS_INDEX) {return true;} // exit if the index doesn't match
  84. if ( clockwise ) {
  85. if (keyboard_report->mods & MOD_BIT(KC_LSFT) ) { // If you are holding L shift, encoder changes layers
  86. if(selected_layer < (DYNAMIC_KEYMAP_LAYER_COUNT - 1)) {
  87. selected_layer ++;
  88. layer_move(selected_layer);
  89. }
  90. } else if (keyboard_report->mods & MOD_BIT(KC_RSFT) ) { // If you are holding R shift, Page up
  91. unregister_mods(MOD_BIT(KC_RSFT));
  92. register_code(KC_PGDN);
  93. register_mods(MOD_BIT(KC_RSFT));
  94. } else if (keyboard_report->mods & MOD_BIT(KC_LCTL)) { // if holding Left Ctrl, navigate next word
  95. tap_code16(LCTL(KC_RGHT));
  96. } else if (keyboard_report->mods & MOD_BIT(KC_LALT)) { // if holding Left Alt, change media next track
  97. tap_code(KC_MEDIA_NEXT_TRACK);
  98. } else {
  99. switch (selected_layer) {
  100. case _FN1:
  101. #ifdef IDLE_TIMEOUT_ENABLE
  102. timeout_update_threshold(true);
  103. #endif
  104. break;
  105. default:
  106. tap_code(KC_VOLU); // Otherwise it just changes volume
  107. break;
  108. }
  109. }
  110. } else {
  111. if (keyboard_report->mods & MOD_BIT(KC_LSFT) ) {
  112. if (selected_layer > 0) {
  113. selected_layer --;
  114. layer_move(selected_layer);
  115. }
  116. } else if (keyboard_report->mods & MOD_BIT(KC_RSFT) ) {
  117. unregister_mods(MOD_BIT(KC_RSFT));
  118. register_code(KC_PGUP);
  119. register_mods(MOD_BIT(KC_RSFT));
  120. } else if (keyboard_report->mods & MOD_BIT(KC_LCTL)) { // if holding Left Ctrl, navigate previous word
  121. tap_code16(LCTL(KC_LEFT));
  122. } else if (keyboard_report->mods & MOD_BIT(KC_LALT)) { // if holding Left Alt, change media previous track
  123. tap_code(KC_MEDIA_PREV_TRACK);
  124. } else {
  125. switch (selected_layer) {
  126. case _FN1:
  127. #ifdef IDLE_TIMEOUT_ENABLE
  128. timeout_update_threshold(false);
  129. #endif
  130. break;
  131. default:
  132. tap_code(KC_VOLD);
  133. break;
  134. }
  135. }
  136. }
  137. return true;
  138. }
  139. #endif // ENCODER_ENABLE
  140. // PROCESS KEY CODES
  141. __attribute__ ((weak)) bool process_record_keymap(uint16_t keycode, keyrecord_t *record) { return true; }
  142. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  143. if (!process_record_keymap(keycode, record)) { return false; }
  144. switch (keycode) {
  145. case KC_00:
  146. if (record->event.pressed) {
  147. // when keycode KC_00 is pressed
  148. SEND_STRING("00");
  149. } else unregister_code16(keycode);
  150. break;
  151. case KC_WINLCK:
  152. if (record->event.pressed) {
  153. keymap_config.no_gui = !keymap_config.no_gui; //toggle status
  154. } else unregister_code16(keycode);
  155. break;
  156. #ifdef IDLE_TIMEOUT_ENABLE
  157. case RGB_TOI:
  158. if(record->event.pressed) {
  159. timeout_update_threshold(true);
  160. } else unregister_code16(keycode);
  161. break;
  162. case RGB_TOD:
  163. if(record->event.pressed) {
  164. timeout_update_threshold(false); //decrease timeout
  165. } else unregister_code16(keycode);
  166. break;
  167. #endif // IDLE_TIMEOUT_ENABLE
  168. default:
  169. if (record->event.pressed) {
  170. #ifdef RGB_MATRIX_ENABLE
  171. rgb_matrix_enable();
  172. #endif
  173. #ifdef IDLE_TIMEOUT_ENABLE
  174. timeout_reset_timer(); //reset activity timer
  175. #endif
  176. }
  177. break;
  178. }
  179. return true;
  180. };
  181. // Turn on/off NUM LOCK if current state is different
  182. void activate_numlock(bool turn_on) {
  183. if (IS_HOST_LED_ON(USB_LED_NUM_LOCK) != turn_on) {
  184. tap_code(KC_NUMLOCK);
  185. }
  186. }
  187. // INITIAL STARTUP
  188. __attribute__ ((weak)) void keyboard_post_init_keymap(void) {}
  189. void keyboard_post_init_user(void) {
  190. keyboard_post_init_keymap();
  191. #ifdef STARTUP_NUMLOCK_ON
  192. activate_numlock(true); // turn on Num lock by default so that the numpad layer always has predictable results
  193. #endif // STARTUP_NUMLOC_ON
  194. #ifdef IDLE_TIMEOUT_ENABLE
  195. timeout_timer = timer_read(); // set inital time for ide timeout
  196. #endif
  197. }