drashna.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. Copyright 2017 Christopher Courtney <drashna@live.com> @drashna
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "drashna.h"
  15. userspace_config_t userspace_config;
  16. #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
  17. # define DRASHNA_UNICODE_MODE UC_WIN
  18. #else
  19. // set to 2 for UC_WIN, set to 4 for UC_WINC
  20. # define DRASHNA_UNICODE_MODE 2
  21. #endif
  22. bool mod_key_press_timer(uint16_t code, uint16_t mod_code, bool pressed) {
  23. static uint16_t this_timer;
  24. if (pressed) {
  25. this_timer = timer_read();
  26. } else {
  27. if (timer_elapsed(this_timer) < TAPPING_TERM) {
  28. tap_code(code);
  29. } else {
  30. register_code(mod_code);
  31. tap_code(code);
  32. unregister_code(mod_code);
  33. }
  34. }
  35. return false;
  36. }
  37. bool mod_key_press(uint16_t code, uint16_t mod_code, bool pressed, uint16_t this_timer) {
  38. if (pressed) {
  39. this_timer = timer_read();
  40. } else {
  41. if (timer_elapsed(this_timer) < TAPPING_TERM) {
  42. tap_code(code);
  43. } else {
  44. register_code(mod_code);
  45. tap_code(code);
  46. unregister_code(mod_code);
  47. }
  48. }
  49. return false;
  50. }
  51. void bootmagic_lite(void) {
  52. matrix_scan();
  53. #if defined(DEBOUNCING_DELAY) && DEBOUNCING_DELAY > 0
  54. wait_ms(DEBOUNCING_DELAY * 2);
  55. #elif defined(DEBOUNCE) && DEBOUNCE > 0
  56. wait_ms(DEBOUNCE * 2);
  57. #else
  58. wait_ms(30);
  59. #endif
  60. matrix_scan();
  61. if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
  62. bootloader_jump();
  63. }
  64. }
  65. // Add reconfigurable functions here, for keymap customization
  66. // This allows for a global, userspace functions, and continued
  67. // customization of the keymap. Use _keymap instead of _user
  68. // functions in the keymaps
  69. __attribute__((weak))
  70. void matrix_init_keymap(void) {}
  71. // Call user matrix init, set default RGB colors and then
  72. // call the keymap's init function
  73. void matrix_init_user(void) {
  74. userspace_config.raw = eeconfig_read_user();
  75. #ifdef BOOTLOADER_CATERINA
  76. DDRD &= ~(1 << 5);
  77. PORTD &= ~(1 << 5);
  78. DDRB &= ~(1 << 0);
  79. PORTB &= ~(1 << 0);
  80. #endif
  81. #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
  82. set_unicode_input_mode(DRASHNA_UNICODE_MODE);
  83. get_unicode_input_mode();
  84. #endif // UNICODE_ENABLE
  85. matrix_init_keymap();
  86. }
  87. __attribute__((weak))
  88. void keyboard_post_init_keymap(void) {}
  89. void keyboard_post_init_user(void) {
  90. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  91. keyboard_post_init_rgb();
  92. #endif
  93. keyboard_post_init_keymap();
  94. }
  95. __attribute__((weak))
  96. void shutdown_keymap(void) {}
  97. void rgb_matrix_update_pwm_buffers(void);
  98. void shutdown_user(void) {
  99. #ifdef RGBLIGHT_ENABLE
  100. rgblight_enable_noeeprom();
  101. rgblight_mode_noeeprom(1);
  102. rgblight_setrgb_red();
  103. #endif // RGBLIGHT_ENABLE
  104. #ifdef RGB_MATRIX_ENABLE
  105. rgb_matrix_set_color_all( 0xFF, 0x00, 0x00 );
  106. rgb_matrix_update_pwm_buffers();
  107. #endif // RGB_MATRIX_ENABLE
  108. shutdown_keymap();
  109. }
  110. __attribute__((weak))
  111. void suspend_power_down_keymap(void) {}
  112. void suspend_power_down_user(void) {
  113. suspend_power_down_keymap();
  114. }
  115. __attribute__((weak))
  116. void suspend_wakeup_init_keymap(void) {}
  117. void suspend_wakeup_init_user(void) {
  118. suspend_wakeup_init_keymap();
  119. }
  120. __attribute__((weak))
  121. void matrix_scan_keymap(void) {}
  122. // No global matrix scan code, so just run keymap's matrix
  123. // scan function
  124. void matrix_scan_user(void) {
  125. static bool has_ran_yet;
  126. if (!has_ran_yet) {
  127. has_ran_yet = true;
  128. startup_user();
  129. }
  130. #ifdef TAP_DANCE_ENABLE // Run Diablo 3 macro checking code.
  131. run_diablo_macro_check();
  132. #endif // TAP_DANCE_ENABLE
  133. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  134. matrix_scan_rgb();
  135. #endif // RGBLIGHT_ENABLE
  136. matrix_scan_keymap();
  137. }
  138. __attribute__((weak))
  139. layer_state_t layer_state_set_keymap(layer_state_t state) { return state; }
  140. // on layer change, no matter where the change was initiated
  141. // Then runs keymap's layer change check
  142. layer_state_t layer_state_set_user(layer_state_t state) {
  143. state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
  144. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  145. state = layer_state_set_rgb(state);
  146. #endif // RGBLIGHT_ENABLE
  147. return layer_state_set_keymap(state);
  148. }
  149. __attribute__((weak))
  150. layer_state_t default_layer_state_set_keymap(layer_state_t state) { return state; }
  151. // Runs state check and changes underglow color and animation
  152. layer_state_t default_layer_state_set_user(layer_state_t state) {
  153. state = default_layer_state_set_keymap(state);
  154. #if 0
  155. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  156. state = default_layer_state_set_rgb(state);
  157. # endif // RGBLIGHT_ENABLE
  158. #endif
  159. return state;
  160. }
  161. __attribute__((weak))
  162. void led_set_keymap(uint8_t usb_led) {}
  163. // Any custom LED code goes here.
  164. // So far, I only have keyboard specific code,
  165. // So nothing goes here.
  166. void led_set_user(uint8_t usb_led) {
  167. led_set_keymap(usb_led);
  168. }
  169. __attribute__((weak))
  170. void eeconfig_init_keymap(void) {}
  171. void eeconfig_init_user(void) {
  172. userspace_config.raw = 0;
  173. userspace_config.rgb_layer_change = true;
  174. eeconfig_update_user(userspace_config.raw);
  175. #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
  176. set_unicode_input_mode(DRASHNA_UNICODE_MODE);
  177. get_unicode_input_mode();
  178. #else
  179. eeprom_update_byte(EECONFIG_UNICODEMODE, DRASHNA_UNICODE_MODE);
  180. #endif
  181. eeconfig_init_keymap();
  182. keyboard_init();
  183. }