callbacks.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 housekeeping_task_i2c_scanner(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. #ifdef CUSTOM_QUANTUM_PAINTER_ENABLE
  20. void keyboard_post_init_qp(void);
  21. #endif
  22. #ifdef OS_DETECTION_ENABLE
  23. os_variant_t os_type;
  24. uint32_t startup_exec(uint32_t trigger_time, void *cb_arg) {
  25. /* do something */
  26. if (is_keyboard_master()) {
  27. os_type = detected_host_os();
  28. if (os_type) {
  29. bool is_mac = (os_type == OS_MACOS) || (os_type == OS_IOS);
  30. keymap_config.swap_lctl_lgui = keymap_config.swap_rctl_rgui = is_mac;
  31. # ifdef UNICODE_COMMON_ENABLE
  32. uint8_t mode = is_mac ? UNICODE_MODE_MACOS : UNICODE_MODE_WINCOMPOSE;
  33. if (mode != get_unicode_input_mode()) {
  34. set_unicode_input_mode(mode);
  35. }
  36. # endif
  37. switch (os_type) {
  38. case OS_UNSURE:
  39. xprintf("unknown OS Detected\n");
  40. break;
  41. case OS_LINUX:
  42. xprintf("Linux Detected\n");
  43. break;
  44. case OS_WINDOWS:
  45. xprintf("Windows Detected\n");
  46. break;
  47. # if 0
  48. case OS_WINDOWS_UNSURE:
  49. xprintf("Windows? Detected\n");
  50. break;
  51. # endif
  52. case OS_MACOS:
  53. xprintf("MacOS Detected\n");
  54. break;
  55. case OS_IOS:
  56. xprintf("iOS Detected\n");
  57. break;
  58. # if 0
  59. case OS_PS5:
  60. xprintf("PlayStation 5 Detected\n");
  61. break;
  62. case OS_HANDHELD:
  63. xprintf("Nintend Switch/Quest 2 Detected\n");
  64. break;
  65. # endif
  66. }
  67. }
  68. }
  69. return os_type ? 0 : 500;
  70. }
  71. #endif
  72. __attribute__((weak)) void keyboard_post_init_keymap(void) {}
  73. void keyboard_post_init_user(void) {
  74. #if defined(CUSTOM_RGBLIGHT)
  75. keyboard_post_init_rgb_light();
  76. #endif
  77. #if defined(CUSTOM_RGB_MATRIX)
  78. keyboard_post_init_rgb_matrix();
  79. #endif
  80. #if defined(SPLIT_KEYBOARD) && defined(SPLIT_TRANSACTION_IDS_USER)
  81. keyboard_post_init_transport_sync();
  82. #endif
  83. #ifdef I2C_SCANNER_ENABLE
  84. keyboard_post_init_i2c();
  85. #endif
  86. #ifdef CUSTOM_UNICODE_ENABLE
  87. keyboard_post_init_unicode();
  88. #endif
  89. #if defined(BOOTLOADER_CATERINA) && defined(__AVR__) && defined(__AVR_ATmega32U4__)
  90. DDRD &= ~(1 << 5);
  91. PORTD &= ~(1 << 5);
  92. DDRB &= ~(1 << 0);
  93. PORTB &= ~(1 << 0);
  94. #endif
  95. #ifdef OS_DETECTION_ENABLE
  96. defer_exec(100, startup_exec, NULL);
  97. #endif
  98. keyboard_post_init_keymap();
  99. }
  100. #ifdef RGB_MATRIX_ENABLE
  101. void rgb_matrix_update_pwm_buffers(void);
  102. #endif
  103. __attribute__((weak)) void shutdown_keymap(void) {}
  104. void shutdown_user(void) {
  105. #ifdef RGBLIGHT_ENABLE
  106. rgblight_enable_noeeprom();
  107. rgblight_mode_noeeprom(1);
  108. rgblight_setrgb(rgblight_get_val(), 0x00, 0x00);
  109. #endif // RGBLIGHT_ENABLE
  110. #ifdef RGB_MATRIX_ENABLE
  111. rgb_matrix_set_color_all(rgb_matrix_get_val(), 0x00, 0x00);
  112. rgb_matrix_update_pwm_buffers();
  113. #endif // RGB_MATRIX_ENABLE
  114. #ifdef OLED_ENABLE
  115. oled_off();
  116. #endif
  117. shutdown_keymap();
  118. }
  119. __attribute__((weak)) void suspend_power_down_keymap(void) {}
  120. void suspend_power_down_user(void) {
  121. if (layer_state_is(_GAMEPAD)) {
  122. layer_off(_GAMEPAD);
  123. }
  124. if (layer_state_is(_DIABLO)) {
  125. layer_off(_DIABLO);
  126. }
  127. if (layer_state_is(_DIABLOII)) {
  128. layer_off(_DIABLOII);
  129. }
  130. #ifdef OLED_ENABLE
  131. oled_off();
  132. #endif
  133. suspend_power_down_keymap();
  134. }
  135. __attribute__((weak)) void suspend_wakeup_init_keymap(void) {}
  136. void suspend_wakeup_init_user(void) {
  137. #ifdef OLED_ENABLE
  138. oled_timer_reset();
  139. #endif
  140. suspend_wakeup_init_keymap();
  141. }
  142. // No global matrix scan code, so just run keymap's matrix
  143. // scan function
  144. __attribute__((weak)) void matrix_scan_keymap(void) {}
  145. void matrix_scan_user(void) {
  146. matrix_scan_keymap();
  147. }
  148. #ifdef AUDIO_ENABLE
  149. float doom_song[][2] = SONG(E1M1_DOOM);
  150. #endif
  151. // on layer change, no matter where the change was initiated
  152. // Then runs keymap's layer change check
  153. __attribute__((weak)) layer_state_t layer_state_set_keymap(layer_state_t state) {
  154. return state;
  155. }
  156. layer_state_t layer_state_set_user(layer_state_t state) {
  157. state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
  158. #if defined(CUSTOM_POINTING_DEVICE)
  159. state = layer_state_set_pointing(state);
  160. #endif
  161. #if defined(CUSTOM_RGBLIGHT)
  162. state = layer_state_set_rgb_light(state);
  163. #endif // CUSTOM_RGBLIGHT
  164. #if defined(AUDIO_ENABLE)
  165. static bool is_gamepad_on = false;
  166. if (layer_state_cmp(state, _GAMEPAD) != is_gamepad_on) {
  167. static bool is_click_on = false;
  168. is_gamepad_on = layer_state_cmp(state, _GAMEPAD);
  169. if (is_gamepad_on) {
  170. is_click_on = is_clicky_on();
  171. if (is_click_on) {
  172. clicky_off();
  173. }
  174. PLAY_LOOP(doom_song);
  175. } else {
  176. if (is_click_on) {
  177. clicky_on();
  178. }
  179. stop_all_notes();
  180. }
  181. }
  182. #endif
  183. state = layer_state_set_keymap(state);
  184. #ifdef CONSOLE_ENABLE
  185. char layer_buffer[16 + 5];
  186. format_layer_bitmap_string(layer_buffer, state, default_layer_state);
  187. dprintf("layer state: %s\n", layer_buffer);
  188. #endif
  189. return state;
  190. }
  191. // Runs state check and changes underglow color and animation
  192. __attribute__((weak)) layer_state_t default_layer_state_set_keymap(layer_state_t state) {
  193. return state;
  194. }
  195. layer_state_t default_layer_state_set_user(layer_state_t state) {
  196. if (!is_keyboard_master()) {
  197. return state;
  198. }
  199. state = default_layer_state_set_keymap(state);
  200. #if defined(CUSTOM_RGBLIGHT)
  201. state = default_layer_state_set_rgb_light(state);
  202. #endif
  203. return state;
  204. }
  205. __attribute__((weak)) void led_set_keymap(uint8_t usb_led) {}
  206. void led_set_user(uint8_t usb_led) {
  207. led_set_keymap(usb_led);
  208. }
  209. __attribute__((weak)) void eeconfig_init_keymap(void) {}
  210. void eeconfig_init_user(void) {
  211. userspace_config.raw = 0;
  212. userspace_config.rgb_layer_change = true;
  213. userspace_config.autocorrection = true;
  214. eeconfig_update_user(userspace_config.raw);
  215. eeconfig_init_keymap();
  216. }
  217. #ifdef SPLIT_KEYBOARD
  218. __attribute__((weak)) void matrix_slave_scan_keymap(void) {}
  219. void matrix_slave_scan_user(void) {
  220. # if defined(AUDIO_ENABLE)
  221. # if !defined(NO_MUSIC_MODE)
  222. music_task();
  223. # endif
  224. # ifdef AUDIO_INIT_DELAY
  225. if (!is_keyboard_master()) {
  226. static bool delayed_tasks_run = false;
  227. static uint16_t delayed_task_timer = 0;
  228. if (!delayed_tasks_run) {
  229. if (!delayed_task_timer) {
  230. delayed_task_timer = timer_read();
  231. } else if (timer_elapsed(delayed_task_timer) > 300) {
  232. audio_startup();
  233. delayed_tasks_run = true;
  234. }
  235. }
  236. }
  237. # endif
  238. # endif
  239. # ifdef SEQUENCER_ENABLE
  240. sequencer_task();
  241. # endif
  242. # ifdef LED_MATRIX_ENABLE
  243. led_matrix_task();
  244. # endif
  245. # ifdef HAPTIC_ENABLE
  246. haptic_task();
  247. # endif
  248. matrix_slave_scan_keymap();
  249. }
  250. #endif
  251. __attribute__((weak)) void housekeeping_task_keymap(void) {}
  252. void housekeeping_task_user(void) {
  253. static bool has_ran_yet;
  254. if (!has_ran_yet) {
  255. has_ran_yet = true;
  256. startup_user();
  257. }
  258. #ifdef TAP_DANCE_ENABLE // Run Diablo 3 macro checking code.
  259. run_diablo_macro_check();
  260. #endif // TAP_DANCE_ENABLE
  261. #if defined(CUSTOM_RGB_MATRIX)
  262. housekeeping_task_rgb_matrix();
  263. #endif
  264. #ifdef I2C_SCANNER_ENABLE
  265. housekeeping_task_i2c_scanner();
  266. #endif
  267. #ifdef CUSTOM_OLED_DRIVER
  268. housekeeping_task_oled();
  269. #endif
  270. #if defined(SPLIT_KEYBOARD) && defined(SPLIT_TRANSACTION_IDS_USER)
  271. housekeeping_task_transport_sync();
  272. #endif
  273. housekeeping_task_keymap();
  274. }