drashna.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "drashna.h"
  17. userspace_config_t userspace_config;
  18. bool mod_key_press_timer(uint16_t code, uint16_t mod_code, bool pressed) {
  19. static uint16_t this_timer;
  20. if (pressed) {
  21. this_timer = timer_read();
  22. } else {
  23. if (timer_elapsed(this_timer) < TAPPING_TERM) {
  24. tap_code(code);
  25. } else {
  26. register_code(mod_code);
  27. tap_code(code);
  28. unregister_code(mod_code);
  29. }
  30. }
  31. return false;
  32. }
  33. bool mod_key_press(uint16_t code, uint16_t mod_code, bool pressed, uint16_t this_timer) {
  34. if (pressed) {
  35. this_timer = timer_read();
  36. } else {
  37. if (timer_elapsed(this_timer) < TAPPING_TERM) {
  38. tap_code(code);
  39. } else {
  40. register_code(mod_code);
  41. tap_code(code);
  42. unregister_code(mod_code);
  43. }
  44. }
  45. return false;
  46. }
  47. __attribute__((weak)) void keyboard_pre_init_keymap(void) {}
  48. void keyboard_pre_init_user(void) {
  49. userspace_config.raw = eeconfig_read_user();
  50. keyboard_pre_init_keymap();
  51. }
  52. // Add reconfigurable functions here, for keymap customization
  53. // This allows for a global, userspace functions, and continued
  54. // customization of the keymap. Use _keymap instead of _user
  55. // functions in the keymaps
  56. // Call user matrix init, set default RGB colors and then
  57. // call the keymap's init function
  58. __attribute__((weak)) void matrix_init_keymap(void) {}
  59. __attribute__((weak)) void matrix_init_secret(void) {}
  60. void matrix_init_user(void) {
  61. #if defined(BOOTLOADER_CATERINA) && defined(__AVR__)
  62. DDRD &= ~(1 << 5);
  63. PORTD &= ~(1 << 5);
  64. DDRB &= ~(1 << 0);
  65. PORTB &= ~(1 << 0);
  66. #endif
  67. matrix_init_secret();
  68. matrix_init_keymap();
  69. #if defined(AUDIO_ENABLE) && defined(SPLIT_KEYBOARD)
  70. if (!is_keyboard_master()) {
  71. stop_all_notes();
  72. }
  73. #endif
  74. }
  75. __attribute__((weak)) void keyboard_post_init_keymap(void) {}
  76. void keyboard_post_init_user(void) {
  77. #if defined(RGBLIGHT_ENABLE)
  78. keyboard_post_init_rgb_light();
  79. #endif
  80. #if defined(RGB_MATRIX_ENABLE)
  81. keyboard_post_init_rgb_matrix();
  82. #endif
  83. #if defined(SPLIT_KEYBOARD) && defined(SPLIT_TRANSACTION_IDS_USER)
  84. keyboard_post_init_transport_sync();
  85. #endif
  86. keyboard_post_init_keymap();
  87. }
  88. #ifdef RGB_MATRIX_ENABLE
  89. void rgb_matrix_update_pwm_buffers(void);
  90. #endif
  91. __attribute__((weak)) void shutdown_keymap(void) {}
  92. void shutdown_user(void) {
  93. #ifdef RGBLIGHT_ENABLE
  94. rgblight_enable_noeeprom();
  95. rgblight_mode_noeeprom(1);
  96. rgblight_setrgb_red();
  97. #endif // RGBLIGHT_ENABLE
  98. #ifdef RGB_MATRIX_ENABLE
  99. rgb_matrix_set_color_all(0xFF, 0x00, 0x00);
  100. rgb_matrix_update_pwm_buffers();
  101. #endif // RGB_MATRIX_ENABLE
  102. shutdown_keymap();
  103. }
  104. __attribute__((weak)) void suspend_power_down_keymap(void) {}
  105. void suspend_power_down_user(void) {
  106. #ifdef OLED_ENABLE
  107. oled_off();
  108. #endif
  109. suspend_power_down_keymap();
  110. }
  111. __attribute__((weak)) void suspend_wakeup_init_keymap(void) {}
  112. void suspend_wakeup_init_user(void) {
  113. if (layer_state_is(_GAMEPAD)) {
  114. layer_off(_GAMEPAD);
  115. }
  116. if (layer_state_is(_DIABLO)) {
  117. layer_off(_DIABLO);
  118. }
  119. suspend_wakeup_init_keymap();
  120. }
  121. // No global matrix scan code, so just run keymap's matrix
  122. // scan function
  123. __attribute__((weak)) void matrix_scan_keymap(void) {}
  124. __attribute__((weak)) void matrix_scan_secret(void) {}
  125. void matrix_scan_user(void) {
  126. static bool has_ran_yet;
  127. if (!has_ran_yet) {
  128. has_ran_yet = true;
  129. startup_user();
  130. }
  131. #ifdef TAP_DANCE_ENABLE // Run Diablo 3 macro checking code.
  132. run_diablo_macro_check();
  133. #endif // TAP_DANCE_ENABLE
  134. #if defined(RGBLIGHT_ENABLE)
  135. matrix_scan_rgb_light();
  136. #endif // RGBLIGHT_ENABLE
  137. #if defined(RGB_MATRIX_ENABLE)
  138. matrix_scan_rgb_matrix();
  139. #endif
  140. matrix_scan_secret();
  141. matrix_scan_keymap();
  142. }
  143. #ifdef AUDIO_ENABLE
  144. float doom_song[][2] = SONG(E1M1_DOOM);
  145. #endif
  146. // on layer change, no matter where the change was initiated
  147. // Then runs keymap's layer change check
  148. __attribute__((weak)) layer_state_t layer_state_set_keymap(layer_state_t state) { return state; }
  149. layer_state_t layer_state_set_user(layer_state_t state) {
  150. if (!is_keyboard_master()) {
  151. return state;
  152. }
  153. state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
  154. #if defined(RGBLIGHT_ENABLE)
  155. state = layer_state_set_rgb_light(state);
  156. #endif // RGBLIGHT_ENABLE
  157. #if defined(AUDIO_ENABLE) && !defined(__arm__)
  158. static bool is_gamepad_on = false;
  159. if (layer_state_cmp(state, _GAMEPAD) != is_gamepad_on) {
  160. is_gamepad_on = layer_state_cmp(state, _GAMEPAD);
  161. if (is_gamepad_on) {
  162. PLAY_LOOP(doom_song);
  163. } else {
  164. stop_all_notes();
  165. }
  166. }
  167. #endif
  168. state = layer_state_set_keymap(state);
  169. return state;
  170. }
  171. // Runs state check and changes underglow color and animation
  172. __attribute__((weak)) layer_state_t default_layer_state_set_keymap(layer_state_t state) { return state; }
  173. layer_state_t default_layer_state_set_user(layer_state_t state) {
  174. if (!is_keyboard_master()) {
  175. return state;
  176. }
  177. state = default_layer_state_set_keymap(state);
  178. #if 0
  179. # if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  180. state = default_layer_state_set_rgb(state);
  181. # endif // RGBLIGHT_ENABLE
  182. #endif
  183. return state;
  184. }
  185. __attribute__((weak)) void led_set_keymap(uint8_t usb_led) {}
  186. void led_set_user(uint8_t usb_led) { led_set_keymap(usb_led); }
  187. __attribute__((weak)) void eeconfig_init_keymap(void) {}
  188. void eeconfig_init_user(void) {
  189. userspace_config.raw = 0;
  190. userspace_config.rgb_layer_change = true;
  191. eeconfig_update_user(userspace_config.raw);
  192. eeconfig_init_keymap();
  193. keyboard_init();
  194. }
  195. bool hasAllBitsInMask(uint8_t value, uint8_t mask) {
  196. value &= 0xF;
  197. mask &= 0xF;
  198. return (value & mask) == mask;
  199. }
  200. #ifdef SPLIT_KEYBOARD
  201. # if defined(AUDIO_ENABLE)
  202. bool delayed_tasks_run = false;
  203. # endif
  204. __attribute__((weak)) void matrix_slave_scan_keymap(void) {}
  205. void matrix_slave_scan_user(void) {
  206. # if defined(AUDIO_ENABLE)
  207. # if !defined(NO_MUSIC_MODE)
  208. music_task();
  209. # endif
  210. if (!is_keyboard_master()) {
  211. static uint16_t delayed_task_timer = 0;
  212. if (!delayed_tasks_run) {
  213. if (!delayed_task_timer) {
  214. delayed_task_timer = timer_read();
  215. } else if (timer_elapsed(delayed_task_timer) > 300) {
  216. audio_startup();
  217. delayed_tasks_run = true;
  218. }
  219. }
  220. }
  221. # endif
  222. # ifdef SEQUENCER_ENABLE
  223. sequencer_task();
  224. # endif
  225. # ifdef LED_MATRIX_ENABLE
  226. led_matrix_task();
  227. # endif
  228. # ifdef HAPTIC_ENABLE
  229. haptic_task();
  230. # endif
  231. matrix_slave_scan_keymap();
  232. }
  233. #endif