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.

376 lines
12 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. #ifdef RGB_MATRIX_ENABLE
  135. void encoder_action_rgb_speed(bool clockwise) {
  136. if (clockwise)
  137. rgb_matrix_increase_speed_noeeprom();
  138. else
  139. rgb_matrix_decrease_speed_noeeprom();
  140. }
  141. void encoder_action_rgb_hue(bool clockwise) {
  142. if (clockwise)
  143. rgb_matrix_increase_hue_noeeprom();
  144. else
  145. rgb_matrix_decrease_hue_noeeprom();
  146. }
  147. void encoder_action_rgb_saturation(bool clockwise) {
  148. if (clockwise)
  149. rgb_matrix_increase_sat_noeeprom();
  150. else
  151. rgb_matrix_decrease_sat_noeeprom();
  152. }
  153. void encoder_action_rgb_brightness(bool clockwise) {
  154. if (clockwise)
  155. rgb_matrix_increase_val_noeeprom();
  156. else
  157. rgb_matrix_decrease_val_noeeprom();
  158. }
  159. void encoder_action_rgb_mode(bool clockwise) {
  160. if (clockwise)
  161. rgb_matrix_step_noeeprom();
  162. else
  163. rgb_matrix_step_reverse_noeeprom();
  164. }
  165. #elif defined(RGBLIGHT_ENABLE)
  166. void encoder_action_rgb_speed(bool clockwise) {
  167. if (clockwise)
  168. rgblight_increase_speed_noeeprom();
  169. else
  170. rgblight_decrease_speed_noeeprom();
  171. }
  172. void encoder_action_rgb_hue(bool clockwise) {
  173. if (clockwise)
  174. rgblight_increase_hue_noeeprom();
  175. else
  176. rgblight_decrease_hue_noeeprom();
  177. }
  178. void encoder_action_rgb_saturation(bool clockwise) {
  179. if (clockwise)
  180. rgblight_increase_sat_noeeprom();
  181. else
  182. rgblight_decrease_sat_noeeprom();
  183. }
  184. void encoder_action_rgb_brightness(bool clockwise) {
  185. if (clockwise)
  186. rgblight_increase_val_noeeprom();
  187. else
  188. rgblight_decrease_val_noeeprom();
  189. }
  190. void encoder_action_rgb_mode(bool clockwise) {
  191. if (clockwise)
  192. rgblight_step_noeeprom();
  193. else
  194. rgblight_step_reverse_noeeprom();
  195. }
  196. #endif // RGB_MATRIX_ENABLE || RGBLIGHT_ENABLE
  197. #endif // ENCODER_ENABLE
  198. #if defined(ENCODER_ENABLE) && defined(ENCODER_DEFAULTACTIONS_ENABLE) // Encoder Functionality
  199. __attribute__((weak)) bool encoder_update_keymap(uint8_t index, bool clockwise) { return true; }
  200. bool encoder_update_user(uint8_t index, bool clockwise) {
  201. if (!encoder_update_keymap(index, clockwise)) { return false; }
  202. if (index != ENCODER_DEFAULTACTIONS_INDEX) {return true;} // exit if the index doesn't match
  203. uint8_t mods_state = get_mods();
  204. if (mods_state & MOD_BIT(KC_LSFT) ) { // If you are holding L shift, encoder changes layers
  205. encoder_action_layerchange(clockwise);
  206. } else if (mods_state & MOD_BIT(KC_RSFT) ) { // If you are holding R shift, Page up/dn
  207. unregister_mods(MOD_BIT(KC_RSFT));
  208. encoder_action_navpage(clockwise);
  209. register_mods(MOD_BIT(KC_RSFT));
  210. } else if (mods_state & MOD_BIT(KC_LCTL)) { // if holding Left Ctrl, navigate next/prev word
  211. encoder_action_navword(clockwise);
  212. } else if (mods_state & MOD_BIT(KC_LALT)) { // if holding Left Alt, change media next/prev track
  213. encoder_action_mediatrack(clockwise);
  214. } else {
  215. switch(get_highest_layer(layer_state)) {
  216. case _FN1:
  217. #ifdef IDLE_TIMEOUT_ENABLE
  218. timeout_update_threshold(clockwise);
  219. #endif
  220. break;
  221. default:
  222. encoder_action_volume(clockwise); // Otherwise it just changes volume
  223. break;
  224. }
  225. }
  226. return false;
  227. }
  228. #endif // ENCODER_ENABLE
  229. // PROCESS KEY CODES
  230. __attribute__ ((weak)) bool process_record_keymap(uint16_t keycode, keyrecord_t *record) { return true; }
  231. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  232. if (!process_record_keymap(keycode, record)) { return false; }
  233. switch (keycode) {
  234. case KC_00:
  235. if (record->event.pressed) {
  236. // when keycode KC_00 is pressed
  237. SEND_STRING("00");
  238. } else unregister_code16(keycode);
  239. break;
  240. case KC_WINLCK:
  241. if (record->event.pressed) {
  242. keymap_config.no_gui = !keymap_config.no_gui; //toggle status
  243. } else unregister_code16(keycode);
  244. break;
  245. #ifdef IDLE_TIMEOUT_ENABLE
  246. case RGB_TOI:
  247. if(record->event.pressed) {
  248. timeout_update_threshold(true);
  249. } else unregister_code16(keycode);
  250. break;
  251. case RGB_TOD:
  252. if(record->event.pressed) {
  253. timeout_update_threshold(false); //decrease timeout
  254. } else unregister_code16(keycode);
  255. break;
  256. #endif // IDLE_TIMEOUT_ENABLE
  257. #ifdef RGB_MATRIX_ENABLE
  258. case RGB_NITE:
  259. if(record->event.pressed) {
  260. rgb_nightmode = !rgb_nightmode;
  261. } else unregister_code16(keycode);
  262. break;
  263. #endif // RGB_MATRIX_ENABLE
  264. #ifdef EMOTICON_ENABLE
  265. case EMO_SHRUG:
  266. if (record->event.pressed) SEND_STRING("`\\_(\"/)_/`");
  267. else unregister_code16(keycode);
  268. break;
  269. case EMO_CONFUSE:
  270. if (record->event.pressed) SEND_STRING("(*_*)");
  271. else unregister_code16(keycode);
  272. break;
  273. case EMO_TEARS:
  274. if (record->event.pressed) SEND_STRING("(T_T)");
  275. else unregister_code16(keycode);
  276. break;
  277. case EMO_NERVOUS:
  278. if (record->event.pressed) SEND_STRING("(~_~;)");
  279. else unregister_code16(keycode);
  280. break;
  281. case EMO_JOY:
  282. if (record->event.pressed) SEND_STRING("(^o^)");
  283. else unregister_code16(keycode);
  284. break;
  285. case EMO_SAD:
  286. if (record->event.pressed) SEND_STRING(":'-(");
  287. else unregister_code16(keycode);
  288. break;
  289. #endif // EMOTICON_ENABLE
  290. default:
  291. if (record->event.pressed) {
  292. #ifdef RGB_MATRIX_ENABLE
  293. rgb_matrix_enable();
  294. #endif
  295. #ifdef IDLE_TIMEOUT_ENABLE
  296. timeout_reset_timer(); //reset activity timer
  297. #endif
  298. }
  299. break;
  300. }
  301. return true;
  302. };
  303. uint16_t get_tapping_term(uint16_t keycode, keyrecord_t *record) {
  304. switch (keycode) {
  305. case KC_SFTUP:
  306. return 300;
  307. case KC_RAISESPC:
  308. case KC_LOWERSPC:
  309. return 450;
  310. default:
  311. return TAPPING_TERM;
  312. }
  313. }
  314. // Turn on/off NUM LOCK if current state is different
  315. void activate_numlock(bool turn_on) {
  316. if (IS_HOST_LED_ON(USB_LED_NUM_LOCK) != turn_on) {
  317. tap_code(KC_NUMLOCK);
  318. }
  319. }
  320. // INITIAL STARTUP
  321. __attribute__ ((weak)) void keyboard_post_init_keymap(void) {}
  322. void keyboard_post_init_user(void) {
  323. keyboard_post_init_keymap();
  324. #ifdef STARTUP_NUMLOCK_ON
  325. activate_numlock(true); // turn on Num lock by default so that the numpad layer always has predictable results
  326. #endif // STARTUP_NUMLOC_ON
  327. #ifdef IDLE_TIMEOUT_ENABLE
  328. timeout_timer = timer_read(); // set inital time for ide timeout
  329. #endif
  330. }