callbacks.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright 2021 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "drashna.h"
  4. #ifdef I2C_SCANNER_ENABLE
  5. void matrix_scan_i2c(void);
  6. void keyboard_post_init_i2c(void);
  7. #endif
  8. __attribute__((weak)) void keyboard_pre_init_keymap(void) {}
  9. void keyboard_pre_init_user(void) {
  10. userspace_config.raw = eeconfig_read_user();
  11. keyboard_pre_init_keymap();
  12. }
  13. // Add reconfigurable functions here, for keymap customization
  14. // This allows for a global, userspace functions, and continued
  15. // customization of the keymap. Use _keymap instead of _user
  16. // functions in the keymaps
  17. // Call user matrix init, set default RGB colors and then
  18. // call the keymap's init function
  19. __attribute__((weak)) void keyboard_post_init_keymap(void) {}
  20. void keyboard_post_init_user(void) {
  21. #if defined(CUSTOM_RGBLIGHT)
  22. keyboard_post_init_rgb_light();
  23. #endif
  24. #if defined(CUSTOM_RGB_MATRIX)
  25. keyboard_post_init_rgb_matrix();
  26. #endif
  27. #if defined(SPLIT_KEYBOARD) && defined(SPLIT_TRANSACTION_IDS_USER)
  28. keyboard_post_init_transport_sync();
  29. #endif
  30. #ifdef I2C_SCANNER_ENABLE
  31. keyboard_post_init_i2c();
  32. #endif
  33. #ifdef CUSTOM_UNICODE_ENABLE
  34. keyboard_post_init_unicode();
  35. #endif
  36. #if defined(BOOTLOADER_CATERINA) && defined(__AVR__) && defined(__AVR_ATmega32U4__)
  37. DDRD &= ~(1 << 5);
  38. PORTD &= ~(1 << 5);
  39. DDRB &= ~(1 << 0);
  40. PORTB &= ~(1 << 0);
  41. #endif
  42. keyboard_post_init_keymap();
  43. }
  44. #ifdef RGB_MATRIX_ENABLE
  45. void rgb_matrix_update_pwm_buffers(void);
  46. #endif
  47. __attribute__((weak)) void shutdown_keymap(void) {}
  48. void shutdown_user(void) {
  49. #ifdef RGBLIGHT_ENABLE
  50. rgblight_enable_noeeprom();
  51. rgblight_mode_noeeprom(1);
  52. rgblight_setrgb(rgblight_get_val(), 0x00, 0x00);
  53. #endif // RGBLIGHT_ENABLE
  54. #ifdef RGB_MATRIX_ENABLE
  55. rgb_matrix_set_color_all(rgb_matrix_get_val(), 0x00, 0x00);
  56. rgb_matrix_update_pwm_buffers();
  57. #endif // RGB_MATRIX_ENABLE
  58. #ifdef OLED_ENABLE
  59. oled_off();
  60. #endif
  61. shutdown_keymap();
  62. }
  63. __attribute__((weak)) void suspend_power_down_keymap(void) {}
  64. void suspend_power_down_user(void) {
  65. if (layer_state_is(_GAMEPAD)) {
  66. layer_off(_GAMEPAD);
  67. }
  68. if (layer_state_is(_DIABLO)) {
  69. layer_off(_DIABLO);
  70. }
  71. if (layer_state_is(_DIABLOII)) {
  72. layer_off(_DIABLOII);
  73. }
  74. #ifdef OLED_ENABLE
  75. oled_off();
  76. #endif
  77. suspend_power_down_keymap();
  78. }
  79. __attribute__((weak)) void suspend_wakeup_init_keymap(void) {}
  80. void suspend_wakeup_init_user(void) {
  81. #ifdef OLED_ENABLE
  82. oled_timer_reset();
  83. #endif
  84. suspend_wakeup_init_keymap();
  85. }
  86. // No global matrix scan code, so just run keymap's matrix
  87. // scan function
  88. __attribute__((weak)) void matrix_scan_keymap(void) {}
  89. void matrix_scan_user(void) {
  90. static bool has_ran_yet;
  91. if (!has_ran_yet) {
  92. has_ran_yet = true;
  93. startup_user();
  94. }
  95. #ifdef TAP_DANCE_ENABLE // Run Diablo 3 macro checking code.
  96. run_diablo_macro_check();
  97. #endif // TAP_DANCE_ENABLE
  98. #if defined(CUSTOM_RGB_MATRIX)
  99. matrix_scan_rgb_matrix();
  100. #endif
  101. #ifdef I2C_SCANNER_ENABLE
  102. matrix_scan_i2c();
  103. #endif
  104. #ifdef CUSTOM_OLED_DRIVER
  105. matrix_scan_oled();
  106. #endif
  107. matrix_scan_keymap();
  108. }
  109. #ifdef AUDIO_ENABLE
  110. float doom_song[][2] = SONG(E1M1_DOOM);
  111. #endif
  112. // on layer change, no matter where the change was initiated
  113. // Then runs keymap's layer change check
  114. __attribute__((weak)) layer_state_t layer_state_set_keymap(layer_state_t state) {
  115. return state;
  116. }
  117. layer_state_t layer_state_set_user(layer_state_t state) {
  118. if (!is_keyboard_master()) {
  119. return state;
  120. }
  121. state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
  122. #if defined(CUSTOM_POINTING_DEVICE)
  123. state = layer_state_set_pointing(state);
  124. #endif
  125. #if defined(CUSTOM_RGBLIGHT)
  126. state = layer_state_set_rgb_light(state);
  127. #endif // CUSTOM_RGBLIGHT
  128. #if defined(AUDIO_ENABLE) && !defined(__arm__)
  129. static bool is_gamepad_on = false;
  130. if (layer_state_cmp(state, _GAMEPAD) != is_gamepad_on) {
  131. is_gamepad_on = layer_state_cmp(state, _GAMEPAD);
  132. if (is_gamepad_on) {
  133. PLAY_LOOP(doom_song);
  134. } else {
  135. stop_all_notes();
  136. }
  137. }
  138. #endif
  139. state = layer_state_set_keymap(state);
  140. return state;
  141. }
  142. // Runs state check and changes underglow color and animation
  143. __attribute__((weak)) layer_state_t default_layer_state_set_keymap(layer_state_t state) {
  144. return state;
  145. }
  146. layer_state_t default_layer_state_set_user(layer_state_t state) {
  147. if (!is_keyboard_master()) {
  148. return state;
  149. }
  150. state = default_layer_state_set_keymap(state);
  151. #if defined(CUSTOM_RGBLIGHT)
  152. state = default_layer_state_set_rgb_light(state);
  153. #endif
  154. return state;
  155. }
  156. __attribute__((weak)) void led_set_keymap(uint8_t usb_led) {}
  157. void led_set_user(uint8_t usb_led) {
  158. led_set_keymap(usb_led);
  159. }
  160. __attribute__((weak)) void eeconfig_init_keymap(void) {}
  161. void eeconfig_init_user(void) {
  162. userspace_config.raw = 0;
  163. userspace_config.rgb_layer_change = true;
  164. userspace_config.autocorrection = true;
  165. eeconfig_update_user(userspace_config.raw);
  166. eeconfig_init_keymap();
  167. }
  168. #ifdef SPLIT_KEYBOARD
  169. __attribute__((weak)) void matrix_slave_scan_keymap(void) {}
  170. void matrix_slave_scan_user(void) {
  171. # if defined(AUDIO_ENABLE)
  172. # if !defined(NO_MUSIC_MODE)
  173. music_task();
  174. # endif
  175. # ifdef AUDIO_INIT_DELAY
  176. if (!is_keyboard_master()) {
  177. static bool delayed_tasks_run = false;
  178. static uint16_t delayed_task_timer = 0;
  179. if (!delayed_tasks_run) {
  180. if (!delayed_task_timer) {
  181. delayed_task_timer = timer_read();
  182. } else if (timer_elapsed(delayed_task_timer) > 300) {
  183. audio_startup();
  184. delayed_tasks_run = true;
  185. }
  186. }
  187. }
  188. # endif
  189. # endif
  190. # ifdef SEQUENCER_ENABLE
  191. sequencer_task();
  192. # endif
  193. # ifdef LED_MATRIX_ENABLE
  194. led_matrix_task();
  195. # endif
  196. # ifdef HAPTIC_ENABLE
  197. haptic_task();
  198. # endif
  199. matrix_slave_scan_keymap();
  200. }
  201. #endif
  202. __attribute__((weak)) void housekeeping_task_keymap(void) {}
  203. void housekeeping_task_user(void) {
  204. #if defined(SPLIT_KEYBOARD) && defined(SPLIT_TRANSACTION_IDS_USER)
  205. housekeeping_task_transport_sync();
  206. #endif
  207. housekeeping_task_keymap();
  208. }