drashna.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. // This block is for all of the gaming macros, as they were all doing
  23. // the same thing, but with differring text sent.
  24. bool send_game_macro(const char *str, keyrecord_t *record, bool override) {
  25. if (!record->event.pressed || override) {
  26. uint16_t keycode;
  27. if (userspace_config.is_overwatch) {
  28. keycode = KC_BSPC;
  29. } else {
  30. keycode = KC_ENTER;
  31. }
  32. clear_keyboard();
  33. tap_code(keycode);
  34. wait_ms(TAP_CODE_DELAY);
  35. send_string_with_delay(str, TAP_CODE_DELAY);
  36. wait_ms(TAP_CODE_DELAY);
  37. tap_code(KC_ENTER);
  38. }
  39. if (override) wait_ms(3000);
  40. return false;
  41. }
  42. bool mod_key_press_timer(uint16_t code, uint16_t mod_code, bool pressed) {
  43. static uint16_t this_timer;
  44. if (pressed) {
  45. this_timer = timer_read();
  46. } else {
  47. if (timer_elapsed(this_timer) < TAPPING_TERM) {
  48. tap_code(code);
  49. } else {
  50. register_code(mod_code);
  51. tap_code(code);
  52. unregister_code(mod_code);
  53. }
  54. }
  55. return false;
  56. }
  57. bool mod_key_press(uint16_t code, uint16_t mod_code, bool pressed, uint16_t this_timer) {
  58. if (pressed) {
  59. this_timer = timer_read();
  60. } else {
  61. if (timer_elapsed(this_timer) < TAPPING_TERM) {
  62. tap_code(code);
  63. } else {
  64. register_code(mod_code);
  65. tap_code(code);
  66. unregister_code(mod_code);
  67. }
  68. }
  69. return false;
  70. }
  71. void bootmagic_lite(void) {
  72. matrix_scan();
  73. #if defined(DEBOUNCING_DELAY) && DEBOUNCING_DELAY > 0
  74. wait_ms(DEBOUNCING_DELAY * 2);
  75. #elif defined(DEBOUNCE) && DEBOUNCE > 0
  76. wait_ms(DEBOUNCE * 2);
  77. #else
  78. wait_ms(30);
  79. #endif
  80. matrix_scan();
  81. if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
  82. bootloader_jump();
  83. }
  84. }
  85. // Add reconfigurable functions here, for keymap customization
  86. // This allows for a global, userspace functions, and continued
  87. // customization of the keymap. Use _keymap instead of _user
  88. // functions in the keymaps
  89. __attribute__((weak))
  90. void matrix_init_keymap(void) {}
  91. // Call user matrix init, set default RGB colors and then
  92. // call the keymap's init function
  93. void matrix_init_user(void) {
  94. userspace_config.raw = eeconfig_read_user();
  95. #ifdef BOOTLOADER_CATERINA
  96. DDRD &= ~(1 << 5);
  97. PORTD &= ~(1 << 5);
  98. DDRB &= ~(1 << 0);
  99. PORTB &= ~(1 << 0);
  100. #endif
  101. #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
  102. set_unicode_input_mode(DRASHNA_UNICODE_MODE);
  103. get_unicode_input_mode();
  104. #endif // UNICODE_ENABLE
  105. matrix_init_keymap();
  106. }
  107. __attribute__((weak))
  108. void keyboard_post_init_keymap(void) {}
  109. void keyboard_post_init_user(void) {
  110. #ifdef RGBLIGHT_ENABLE
  111. keyboard_post_init_rgb();
  112. #endif
  113. keyboard_post_init_keymap();
  114. }
  115. __attribute__((weak))
  116. void shutdown_keymap(void) {}
  117. void shutdown_user(void) {
  118. #ifdef RGBLIGHT_ENABLE
  119. rgblight_enable_noeeprom();
  120. rgblight_mode_noeeprom(1);
  121. rgblight_setrgb_red();
  122. #endif // RGBLIGHT_ENABLE
  123. #ifdef RGB_MATRIX_ENABLE
  124. // uint16_t timer_start = timer_read();
  125. // rgb_matrix_set_color_all( 0xFF, 0x00, 0x00 );
  126. // while(timer_elapsed(timer_start) < 250) { wait_ms(1); }
  127. #endif // RGB_MATRIX_ENABLE
  128. shutdown_keymap();
  129. }
  130. __attribute__((weak))
  131. void suspend_power_down_keymap(void) {}
  132. void suspend_power_down_user(void) {
  133. suspend_power_down_keymap();
  134. }
  135. __attribute__((weak))
  136. void suspend_wakeup_init_keymap(void) {}
  137. void suspend_wakeup_init_user(void) {
  138. suspend_wakeup_init_keymap();
  139. }
  140. __attribute__((weak))
  141. void matrix_scan_keymap(void) {}
  142. // No global matrix scan code, so just run keymap's matrix
  143. // scan function
  144. void matrix_scan_user(void) {
  145. static bool has_ran_yet;
  146. if (!has_ran_yet) {
  147. has_ran_yet = true;
  148. startup_user();
  149. }
  150. #ifdef TAP_DANCE_ENABLE // Run Diablo 3 macro checking code.
  151. run_diablo_macro_check();
  152. #endif // TAP_DANCE_ENABLE
  153. #ifdef RGBLIGHT_ENABLE
  154. matrix_scan_rgb();
  155. #endif // RGBLIGHT_ENABLE
  156. matrix_scan_keymap();
  157. }
  158. __attribute__((weak))
  159. layer_state_t layer_state_set_keymap(layer_state_t state) { return state; }
  160. // on layer change, no matter where the change was initiated
  161. // Then runs keymap's layer change check
  162. layer_state_t layer_state_set_user(layer_state_t state) {
  163. state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
  164. #ifdef RGBLIGHT_ENABLE
  165. state = layer_state_set_rgb(state);
  166. #endif // RGBLIGHT_ENABLE
  167. return layer_state_set_keymap(state);
  168. }
  169. __attribute__((weak))
  170. layer_state_t default_layer_state_set_keymap(layer_state_t state) { return state; }
  171. // Runs state check and changes underglow color and animation
  172. layer_state_t default_layer_state_set_user(layer_state_t state) {
  173. state = default_layer_state_set_keymap(state);
  174. #if 0
  175. # ifdef RGBLIGHT_ENABLE
  176. state = default_layer_state_set_rgb(state);
  177. # endif // RGBLIGHT_ENABLE
  178. #endif
  179. return state;
  180. }
  181. __attribute__((weak))
  182. void led_set_keymap(uint8_t usb_led) {}
  183. // Any custom LED code goes here.
  184. // So far, I only have keyboard specific code,
  185. // So nothing goes here.
  186. void led_set_user(uint8_t usb_led) {
  187. led_set_keymap(usb_led);
  188. }
  189. __attribute__((weak))
  190. void eeconfig_init_keymap(void) {}
  191. void eeconfig_init_user(void) {
  192. userspace_config.raw = 0;
  193. userspace_config.rgb_layer_change = true;
  194. eeconfig_update_user(userspace_config.raw);
  195. #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
  196. set_unicode_input_mode(DRASHNA_UNICODE_MODE);
  197. get_unicode_input_mode();
  198. #else
  199. eeprom_update_byte(EECONFIG_UNICODEMODE, DRASHNA_UNICODE_MODE);
  200. #endif
  201. eeconfig_init_keymap();
  202. keyboard_init();
  203. }