drashna.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. rgb_matrix_set_color_all(0xFF, 0x00, 0x00);
  107. rgb_matrix_update_pwm_buffers();
  108. #endif // RGB_MATRIX_ENABLE
  109. shutdown_keymap();
  110. }
  111. __attribute__((weak)) void suspend_power_down_keymap(void) {}
  112. void suspend_power_down_user(void) { suspend_power_down_keymap(); }
  113. __attribute__((weak)) void suspend_wakeup_init_keymap(void) {}
  114. void suspend_wakeup_init_user(void) { suspend_wakeup_init_keymap(); }
  115. __attribute__((weak)) void matrix_scan_keymap(void) {}
  116. // No global matrix scan code, so just run keymap's matrix
  117. // scan function
  118. void matrix_scan_user(void) {
  119. static bool has_ran_yet;
  120. if (!has_ran_yet) {
  121. has_ran_yet = true;
  122. startup_user();
  123. }
  124. #ifdef TAP_DANCE_ENABLE // Run Diablo 3 macro checking code.
  125. run_diablo_macro_check();
  126. #endif // TAP_DANCE_ENABLE
  127. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  128. matrix_scan_rgb();
  129. #endif // RGBLIGHT_ENABLE
  130. matrix_scan_keymap();
  131. }
  132. __attribute__((weak)) layer_state_t layer_state_set_keymap(layer_state_t state) { return state; }
  133. // on layer change, no matter where the change was initiated
  134. // Then runs keymap's layer change check
  135. layer_state_t layer_state_set_user(layer_state_t state) {
  136. state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
  137. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  138. state = layer_state_set_rgb(state);
  139. #endif // RGBLIGHT_ENABLE
  140. return layer_state_set_keymap(state);
  141. }
  142. __attribute__((weak)) layer_state_t default_layer_state_set_keymap(layer_state_t state) { return state; }
  143. // Runs state check and changes underglow color and animation
  144. layer_state_t default_layer_state_set_user(layer_state_t state) {
  145. state = default_layer_state_set_keymap(state);
  146. #if 0
  147. # if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  148. state = default_layer_state_set_rgb(state);
  149. # endif // RGBLIGHT_ENABLE
  150. #endif
  151. return state;
  152. }
  153. __attribute__((weak)) void led_set_keymap(uint8_t usb_led) {}
  154. // Any custom LED code goes here.
  155. // So far, I only have keyboard specific code,
  156. // So nothing goes here.
  157. void led_set_user(uint8_t usb_led) { led_set_keymap(usb_led); }
  158. __attribute__((weak)) void eeconfig_init_keymap(void) {}
  159. void eeconfig_init_user(void) {
  160. userspace_config.raw = 0;
  161. userspace_config.rgb_layer_change = true;
  162. eeconfig_update_user(userspace_config.raw);
  163. #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
  164. set_unicode_input_mode(DRASHNA_UNICODE_MODE);
  165. get_unicode_input_mode();
  166. #else
  167. eeprom_update_byte(EECONFIG_UNICODEMODE, DRASHNA_UNICODE_MODE);
  168. #endif
  169. eeconfig_init_keymap();
  170. keyboard_init();
  171. }
  172. bool hasAllBitsInMask(uint8_t value, uint8_t mask) {
  173. value &= 0xF;
  174. mask &= 0xF;
  175. return (value & mask) == mask;
  176. }