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.

293 lines
8.2 KiB

  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 CUSTOM_DYNAMIC_MACROS_ENABLE
  5. # include "keyrecords/dynamic_macros.h"
  6. #endif
  7. #ifdef I2C_SCANNER_ENABLE
  8. void housekeeping_task_i2c_scanner(void);
  9. void keyboard_post_init_i2c(void);
  10. #endif
  11. __attribute__((weak)) void keyboard_pre_init_keymap(void) {}
  12. void keyboard_pre_init_user(void) {
  13. eeconfig_read_user_config(&userspace_config.raw);
  14. if (!userspace_config.check) {
  15. eeconfig_init_user();
  16. }
  17. keyboard_pre_init_keymap();
  18. }
  19. // Add reconfigurable functions here, for keymap customization
  20. // This allows for a global, userspace functions, and continued
  21. // customization of the keymap. Use _keymap instead of _user
  22. // functions in the keymaps
  23. // Call user matrix init, set default RGB colors and then
  24. // call the keymap's init function
  25. #ifdef CUSTOM_QUANTUM_PAINTER_ENABLE
  26. void keyboard_post_init_qp(void);
  27. #endif
  28. #if defined(OS_DETECTION_ENABLE) && defined(DEFERRED_EXEC_ENABLE)
  29. uint32_t startup_exec(uint32_t trigger_time, void *cb_arg);
  30. #endif
  31. __attribute__((weak)) void keyboard_post_init_keymap(void) {}
  32. void keyboard_post_init_user(void) {
  33. #if defined(CUSTOM_RGBLIGHT)
  34. keyboard_post_init_rgb_light();
  35. #endif
  36. #if defined(CUSTOM_RGB_MATRIX)
  37. keyboard_post_init_rgb_matrix();
  38. #endif
  39. #if defined(SPLIT_KEYBOARD) && defined(SPLIT_TRANSACTION_IDS_USER)
  40. keyboard_post_init_transport_sync();
  41. #endif
  42. #ifdef I2C_SCANNER_ENABLE
  43. keyboard_post_init_i2c();
  44. #endif
  45. #ifdef CUSTOM_UNICODE_ENABLE
  46. keyboard_post_init_unicode();
  47. #endif
  48. #if defined(BOOTLOADER_CATERINA) && defined(__AVR__) && defined(__AVR_ATmega32U4__)
  49. DDRD &= ~(1 << 5);
  50. PORTD &= ~(1 << 5);
  51. DDRB &= ~(1 << 0);
  52. PORTB &= ~(1 << 0);
  53. #endif
  54. #ifdef CUSTOM_DYNAMIC_MACROS_ENABLE
  55. dynamic_macro_init();
  56. #endif
  57. #if defined(OS_DETECTION_ENABLE) && defined(DEFERRED_EXEC_ENABLE)
  58. defer_exec(100, startup_exec, NULL);
  59. #endif
  60. keyboard_post_init_keymap();
  61. }
  62. #ifdef RGB_MATRIX_ENABLE
  63. void rgb_matrix_update_pwm_buffers(void);
  64. #endif
  65. __attribute__((weak)) void shutdown_keymap(void) {}
  66. void shutdown_user(void) {
  67. #ifdef RGBLIGHT_ENABLE
  68. rgblight_enable_noeeprom();
  69. rgblight_mode_noeeprom(1);
  70. rgblight_setrgb(rgblight_get_val(), 0x00, 0x00);
  71. #endif // RGBLIGHT_ENABLE
  72. #ifdef RGB_MATRIX_ENABLE
  73. rgb_matrix_set_color_all(rgb_matrix_get_val(), 0x00, 0x00);
  74. rgb_matrix_update_pwm_buffers();
  75. #endif // RGB_MATRIX_ENABLE
  76. #ifdef OLED_ENABLE
  77. oled_off();
  78. #endif
  79. shutdown_keymap();
  80. }
  81. __attribute__((weak)) void suspend_power_down_keymap(void) {}
  82. void suspend_power_down_user(void) {
  83. if (layer_state_is(_GAMEPAD)) {
  84. layer_off(_GAMEPAD);
  85. }
  86. if (layer_state_is(_DIABLO)) {
  87. layer_off(_DIABLO);
  88. }
  89. if (layer_state_is(_DIABLOII)) {
  90. layer_off(_DIABLOII);
  91. }
  92. #ifdef OLED_ENABLE
  93. oled_off();
  94. #endif
  95. suspend_power_down_keymap();
  96. }
  97. __attribute__((weak)) void suspend_wakeup_init_keymap(void) {}
  98. void suspend_wakeup_init_user(void) {
  99. suspend_wakeup_init_keymap();
  100. }
  101. // No global matrix scan code, so just run keymap's matrix
  102. // scan function
  103. __attribute__((weak)) void matrix_scan_keymap(void) {}
  104. void matrix_scan_user(void) {
  105. matrix_scan_keymap();
  106. }
  107. #ifdef AUDIO_ENABLE
  108. float doom_song[][2] = SONG(E1M1_DOOM);
  109. #endif
  110. // on layer change, no matter where the change was initiated
  111. // Then runs keymap's layer change check
  112. __attribute__((weak)) layer_state_t layer_state_set_keymap(layer_state_t state) {
  113. return state;
  114. }
  115. layer_state_t layer_state_set_user(layer_state_t state) {
  116. state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
  117. #if defined(CUSTOM_POINTING_DEVICE)
  118. state = layer_state_set_pointing(state);
  119. #endif
  120. #if defined(CUSTOM_RGBLIGHT)
  121. state = layer_state_set_rgb_light(state);
  122. #endif // CUSTOM_RGBLIGHT
  123. #if defined(AUDIO_ENABLE)
  124. static bool is_gamepad_on = false;
  125. if (layer_state_cmp(state, _GAMEPAD) != is_gamepad_on) {
  126. static bool is_click_on = false;
  127. is_gamepad_on = layer_state_cmp(state, _GAMEPAD);
  128. if (is_gamepad_on) {
  129. is_click_on = is_clicky_on();
  130. if (is_click_on) {
  131. clicky_off();
  132. }
  133. PLAY_LOOP(doom_song);
  134. } else {
  135. if (is_click_on) {
  136. clicky_on();
  137. }
  138. stop_all_notes();
  139. }
  140. }
  141. #endif
  142. state = layer_state_set_keymap(state);
  143. #ifdef CONSOLE_ENABLE
  144. char layer_buffer[16 + 5];
  145. format_layer_bitmap_string(layer_buffer, state, default_layer_state);
  146. dprintf("layer state: %s\n", layer_buffer);
  147. #endif
  148. return state;
  149. }
  150. // Runs state check and changes underglow color and animation
  151. __attribute__((weak)) layer_state_t default_layer_state_set_keymap(layer_state_t state) {
  152. return state;
  153. }
  154. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  155. static float default_layer_songs[][MAX_LAYER][2] = DEFAULT_LAYER_SONGS;
  156. #endif
  157. layer_state_t default_layer_state_set_user(layer_state_t state) {
  158. if (!is_keyboard_master()) {
  159. return state;
  160. }
  161. state = default_layer_state_set_keymap(state);
  162. #if defined(CUSTOM_RGBLIGHT)
  163. state = default_layer_state_set_rgb_light(state);
  164. #endif
  165. static bool has_init_been_ran = false;
  166. // We don't want to run this the first time it's called, since it's read from eeeprom and called
  167. // as part of the startup process. But after that, it's okay.
  168. if (has_init_been_ran) {
  169. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  170. if (get_highest_layer(state) < MAX_LAYER) {
  171. PLAY_SONG(default_layer_songs[get_highest_layer(state)]);
  172. }
  173. #endif
  174. eeconfig_update_default_layer(state);
  175. } else {
  176. has_init_been_ran = true;
  177. }
  178. return state;
  179. }
  180. __attribute__((weak)) void led_set_keymap(uint8_t usb_led) {}
  181. void led_set_user(uint8_t usb_led) {
  182. led_set_keymap(usb_led);
  183. }
  184. __attribute__((weak)) void eeconfig_init_keymap(void) {}
  185. void eeconfig_init_user(void) {
  186. userspace_config.raw = 0;
  187. userspace_config.rgb_layer_change = true;
  188. userspace_config.check = true;
  189. #if defined(OLED_ENABLE)
  190. userspace_config.oled_brightness = OLED_BRIGHTNESS;
  191. #else
  192. userspace_config.oled_brightness = 255;
  193. #endif
  194. eeconfig_update_user_config(&userspace_config.raw);
  195. eeconfig_init_keymap();
  196. }
  197. void eeconfig_init_user_datablock(void) {
  198. #if (EECONFIG_USER_DATA_SIZE) > 4
  199. uint8_t eeconfig_empty_temp[(EECONFIG_USER_DATA_SIZE)-4] = {0};
  200. eeconfig_update_user_data(eeconfig_empty_temp);
  201. #endif
  202. }
  203. #ifdef SPLIT_KEYBOARD
  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. # ifdef AUDIO_INIT_DELAY
  211. if (!is_keyboard_master()) {
  212. static bool delayed_tasks_run = false;
  213. static uint16_t delayed_task_timer = 0;
  214. if (!delayed_tasks_run) {
  215. if (!delayed_task_timer) {
  216. delayed_task_timer = timer_read();
  217. } else if (timer_elapsed(delayed_task_timer) > 300) {
  218. audio_startup();
  219. delayed_tasks_run = true;
  220. }
  221. }
  222. }
  223. # endif
  224. # endif
  225. # ifdef SEQUENCER_ENABLE
  226. sequencer_task();
  227. # endif
  228. # ifdef LED_MATRIX_ENABLE
  229. led_matrix_task();
  230. # endif
  231. # ifdef HAPTIC_ENABLE
  232. haptic_task();
  233. # endif
  234. matrix_slave_scan_keymap();
  235. }
  236. #endif
  237. __attribute__((weak)) void housekeeping_task_keymap(void) {}
  238. void housekeeping_task_user(void) {
  239. static bool has_ran_yet;
  240. if (!has_ran_yet) {
  241. has_ran_yet = true;
  242. startup_user();
  243. }
  244. #ifdef TAP_DANCE_ENABLE // Run Diablo 3 macro checking code.
  245. run_diablo_macro_check();
  246. #endif // TAP_DANCE_ENABLE
  247. #if defined(CUSTOM_RGB_MATRIX)
  248. housekeeping_task_rgb_matrix();
  249. #endif
  250. #ifdef I2C_SCANNER_ENABLE
  251. housekeeping_task_i2c_scanner();
  252. #endif
  253. #ifdef CUSTOM_OLED_DRIVER
  254. housekeeping_task_oled();
  255. #endif
  256. #if defined(SPLIT_KEYBOARD) && defined(SPLIT_TRANSACTION_IDS_USER)
  257. housekeeping_task_transport_sync();
  258. #endif
  259. housekeeping_task_keymap();
  260. }