drashna.c 6.3 KB

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