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.

219 lines
7.3 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 ENCODER_ENABLE
  16. #ifndef DYNAMIC_KEYMAP_LAYER_COUNT
  17. #define DYNAMIC_KEYMAP_LAYER_COUNT 4 //default in case this is not already defined elsewhere
  18. #endif
  19. #ifndef ENCODER_DEFAULTACTIONS_INDEX
  20. #define ENCODER_DEFAULTACTIONS_INDEX 0 // can select encoder index if there are multiple encoders
  21. #endif
  22. void encoder_action_volume(bool clockwise) {
  23. if (clockwise)
  24. tap_code(KC_VOLU);
  25. else
  26. tap_code(KC_VOLD);
  27. }
  28. void encoder_action_mediatrack(bool clockwise) {
  29. if (clockwise)
  30. tap_code(KC_MEDIA_NEXT_TRACK);
  31. else
  32. tap_code(KC_MEDIA_PREV_TRACK);
  33. }
  34. void encoder_action_navword(bool clockwise) {
  35. if (clockwise)
  36. tap_code16(LCTL(KC_RGHT));
  37. else
  38. tap_code16(LCTL(KC_LEFT));
  39. }
  40. void encoder_action_navpage(bool clockwise) {
  41. if (clockwise)
  42. tap_code16(KC_PGUP);
  43. else
  44. tap_code16(KC_PGDN);
  45. }
  46. // LAYER HANDLING
  47. uint8_t selected_layer = 0;
  48. uint8_t get_selected_layer(void) {
  49. return selected_layer;
  50. }
  51. void encoder_action_layerchange(bool clockwise) {
  52. if (clockwise) {
  53. if(selected_layer < (DYNAMIC_KEYMAP_LAYER_COUNT - 1)) {
  54. selected_layer ++;
  55. layer_move(selected_layer);
  56. }
  57. } else {
  58. if (selected_layer > 0) {
  59. selected_layer --;
  60. layer_move(selected_layer);
  61. }
  62. }
  63. }
  64. #ifdef RGB_MATRIX_ENABLE
  65. void encoder_action_rgb_speed(bool clockwise) {
  66. if (clockwise)
  67. rgb_matrix_increase_speed_noeeprom();
  68. else
  69. rgb_matrix_decrease_speed_noeeprom();
  70. }
  71. void encoder_action_rgb_hue(bool clockwise) {
  72. if (clockwise)
  73. rgb_matrix_increase_hue_noeeprom();
  74. else
  75. rgb_matrix_decrease_hue_noeeprom();
  76. }
  77. void encoder_action_rgb_saturation(bool clockwise) {
  78. if (clockwise)
  79. rgb_matrix_increase_sat_noeeprom();
  80. else
  81. rgb_matrix_decrease_sat_noeeprom();
  82. }
  83. void encoder_action_rgb_brightness(bool clockwise) {
  84. if (clockwise)
  85. rgb_matrix_increase_val_noeeprom();
  86. else
  87. rgb_matrix_decrease_val_noeeprom();
  88. }
  89. void encoder_action_rgb_mode(bool clockwise) {
  90. if (clockwise)
  91. rgb_matrix_step_noeeprom();
  92. else
  93. rgb_matrix_step_reverse_noeeprom();
  94. }
  95. #elif defined(RGBLIGHT_ENABLE)
  96. void encoder_action_rgb_speed(bool clockwise) {
  97. if (clockwise)
  98. rgblight_increase_speed_noeeprom();
  99. else
  100. rgblight_decrease_speed_noeeprom();
  101. }
  102. void encoder_action_rgb_hue(bool clockwise) {
  103. if (clockwise)
  104. rgblight_increase_hue_noeeprom();
  105. else
  106. rgblight_decrease_hue_noeeprom();
  107. }
  108. void encoder_action_rgb_saturation(bool clockwise) {
  109. if (clockwise)
  110. rgblight_increase_sat_noeeprom();
  111. else
  112. rgblight_decrease_sat_noeeprom();
  113. }
  114. void encoder_action_rgb_brightness(bool clockwise) {
  115. if (clockwise)
  116. rgblight_increase_val_noeeprom();
  117. else
  118. rgblight_decrease_val_noeeprom();
  119. }
  120. void encoder_action_rgb_mode(bool clockwise) {
  121. if (clockwise)
  122. rgblight_step_noeeprom();
  123. else
  124. rgblight_step_reverse_noeeprom();
  125. }
  126. #endif // RGB_MATRIX_ENABLE || RGBLIGHT_ENABLE
  127. #ifdef ALTTAB_SCROLL_ENABLE
  128. bool is_tab_scrolling = false;
  129. bool is_alt_tab_active = false;
  130. uint16_t alt_tab_timer = 0;
  131. void encoder_toggle_alttabscroll(void) {
  132. is_tab_scrolling = !is_tab_scrolling;
  133. }
  134. void encoder_action_alttabscroll(bool clockwise) {
  135. if (clockwise) {
  136. if (!is_alt_tab_active) {
  137. is_alt_tab_active = true;
  138. register_mods(MOD_RALT);
  139. }
  140. tap_code16(KC_TAB);
  141. }
  142. else {
  143. tap_code16(S(KC_TAB));
  144. }
  145. alt_tab_timer = timer_read();
  146. }
  147. void encoder_tick_alttabscroll(void) {
  148. if (is_alt_tab_active) {
  149. if (timer_elapsed(alt_tab_timer) > 600) {
  150. unregister_mods(MOD_RALT);
  151. is_alt_tab_active = false;
  152. }
  153. }
  154. }
  155. #endif // ALTTAB_SCROLL_ENABLE
  156. #endif // ENCODER_ENABLE
  157. #if defined(ENCODER_ENABLE) && defined(ENCODER_DEFAULTACTIONS_ENABLE) // Encoder Functionality
  158. __attribute__((weak)) bool encoder_update_keymap(uint8_t index, bool clockwise) { return true; }
  159. bool encoder_update_user(uint8_t index, bool clockwise) {
  160. if (!encoder_update_keymap(index, clockwise)) { return false; }
  161. if (index != ENCODER_DEFAULTACTIONS_INDEX) {return true;} // exit if the index doesn't match
  162. uint8_t mods_state = get_mods();
  163. if (mods_state & MOD_BIT(KC_LSFT) ) { // If you are holding L shift, encoder changes layers
  164. encoder_action_layerchange(clockwise);
  165. } else if (mods_state & MOD_BIT(KC_RSFT) ) { // If you are holding R shift, Page up/dn
  166. unregister_mods(MOD_BIT(KC_RSFT));
  167. encoder_action_navpage(clockwise);
  168. register_mods(MOD_BIT(KC_RSFT));
  169. } else if (mods_state & MOD_BIT(KC_LCTL)) { // if holding Left Ctrl, navigate next/prev word
  170. encoder_action_navword(clockwise);
  171. } else if (mods_state & MOD_BIT(KC_LALT)) { // if holding Left Alt, change media next/prev track
  172. encoder_action_mediatrack(clockwise);
  173. } else {
  174. switch(get_highest_layer(layer_state)) {
  175. case _FN1:
  176. #ifdef IDLE_TIMEOUT_ENABLE
  177. timeout_update_threshold(clockwise);
  178. #endif
  179. break;
  180. default:
  181. #ifdef ALTTAB_SCROLL_ENABLE
  182. if (is_tab_scrolling)
  183. encoder_action_alttabscroll(clockwise);
  184. else
  185. encoder_action_volume(clockwise); // Otherwise it just changes volume
  186. #else
  187. encoder_action_volume(clockwise); // Otherwise it just changes volume
  188. #endif // ALTTAB_SCROLL_ENABLE
  189. break;
  190. }
  191. }
  192. return false;
  193. }
  194. #endif // ENCODER_ENABLE