drashna.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. __attribute__((weak)) void keyboard_pre_init_keymap(void) {}
  66. void keyboard_pre_init_user(void) {
  67. userspace_config.raw = eeconfig_read_user();
  68. keyboard_pre_init_keymap();
  69. }
  70. // Add reconfigurable functions here, for keymap customization
  71. // This allows for a global, userspace functions, and continued
  72. // customization of the keymap. Use _keymap instead of _user
  73. // functions in the keymaps
  74. __attribute__((weak)) void matrix_init_keymap(void) {}
  75. // Call user matrix init, set default RGB colors and then
  76. // call the keymap's init function
  77. void matrix_init_user(void) {
  78. #if defined(BOOTLOADER_CATERINA) && defined(__AVR__)
  79. DDRD &= ~(1 << 5);
  80. PORTD &= ~(1 << 5);
  81. DDRB &= ~(1 << 0);
  82. PORTB &= ~(1 << 0);
  83. #endif
  84. #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
  85. set_unicode_input_mode(DRASHNA_UNICODE_MODE);
  86. get_unicode_input_mode();
  87. #endif // UNICODE_ENABLE
  88. matrix_init_keymap();
  89. }
  90. __attribute__((weak)) void keyboard_post_init_keymap(void) {}
  91. void keyboard_post_init_user(void) {
  92. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  93. keyboard_post_init_rgb();
  94. #endif
  95. keyboard_post_init_keymap();
  96. }
  97. __attribute__((weak)) void shutdown_keymap(void) {}
  98. void rgb_matrix_update_pwm_buffers(void);
  99. void shutdown_user(void) {
  100. #ifdef RGBLIGHT_ENABLE
  101. rgblight_enable_noeeprom();
  102. rgblight_mode_noeeprom(1);
  103. rgblight_setrgb_red();
  104. #endif // RGBLIGHT_ENABLE
  105. #ifdef RGB_MATRIX_ENABLE
  106. # ifdef __AVR__
  107. rgb_matrix_set_color_all(0xFF, 0x00, 0x00);
  108. rgb_matrix_update_pwm_buffers();
  109. # else
  110. rgb_matrix_sethsv_noeeprom(0, 255, 255);
  111. rgb_matrix_mode_noeeprom(1);
  112. # endif
  113. #endif // RGB_MATRIX_ENABLE
  114. shutdown_keymap();
  115. }
  116. __attribute__((weak)) void suspend_power_down_keymap(void) {}
  117. void suspend_power_down_user(void) { suspend_power_down_keymap(); }
  118. __attribute__((weak)) void suspend_wakeup_init_keymap(void) {}
  119. void suspend_wakeup_init_user(void) { suspend_wakeup_init_keymap(); }
  120. __attribute__((weak)) void matrix_scan_keymap(void) {}
  121. // No global matrix scan code, so just run keymap's matrix
  122. // scan function
  123. void matrix_scan_user(void) {
  124. static bool has_ran_yet;
  125. if (!has_ran_yet) {
  126. has_ran_yet = true;
  127. startup_user();
  128. }
  129. #ifdef TAP_DANCE_ENABLE // Run Diablo 3 macro checking code.
  130. run_diablo_macro_check();
  131. #endif // TAP_DANCE_ENABLE
  132. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  133. matrix_scan_rgb();
  134. #endif // RGBLIGHT_ENABLE
  135. matrix_scan_keymap();
  136. }
  137. __attribute__((weak)) layer_state_t layer_state_set_keymap(layer_state_t state) { return state; }
  138. // on layer change, no matter where the change was initiated
  139. // Then runs keymap's layer change check
  140. layer_state_t layer_state_set_user(layer_state_t state) {
  141. state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
  142. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  143. state = layer_state_set_rgb(state);
  144. #endif // RGBLIGHT_ENABLE
  145. return layer_state_set_keymap(state);
  146. }
  147. __attribute__((weak)) layer_state_t default_layer_state_set_keymap(layer_state_t state) { return state; }
  148. // Runs state check and changes underglow color and animation
  149. layer_state_t default_layer_state_set_user(layer_state_t state) {
  150. state = default_layer_state_set_keymap(state);
  151. #if 0
  152. # if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  153. state = default_layer_state_set_rgb(state);
  154. # endif // RGBLIGHT_ENABLE
  155. #endif
  156. return state;
  157. }
  158. __attribute__((weak)) void led_set_keymap(uint8_t usb_led) {}
  159. // Any custom LED code goes here.
  160. // So far, I only have keyboard specific code,
  161. // So nothing goes here.
  162. void led_set_user(uint8_t usb_led) { led_set_keymap(usb_led); }
  163. __attribute__((weak)) void eeconfig_init_keymap(void) {}
  164. void eeconfig_init_user(void) {
  165. userspace_config.raw = 0;
  166. userspace_config.rgb_layer_change = true;
  167. eeconfig_update_user(userspace_config.raw);
  168. #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
  169. set_unicode_input_mode(DRASHNA_UNICODE_MODE);
  170. get_unicode_input_mode();
  171. #else
  172. eeprom_update_byte(EECONFIG_UNICODEMODE, DRASHNA_UNICODE_MODE);
  173. #endif
  174. eeconfig_init_keymap();
  175. keyboard_init();
  176. }
  177. bool hasAllBitsInMask(uint8_t value, uint8_t mask) {
  178. value &= 0xF;
  179. mask &= 0xF;
  180. return (value & mask) == mask;
  181. }