2
0

drashna.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. #include "tap_dances.h"
  16. #include "rgb_stuff.h"
  17. userspace_config_t userspace_config;
  18. uint16_t copy_paste_timer;
  19. // Helper Functions
  20. // This block is for all of the gaming macros, as they were all doing
  21. // the same thing, but with differring text sent.
  22. bool send_game_macro(const char *str, keyrecord_t *record, bool override) {
  23. if (!record->event.pressed || override) {
  24. uint16_t keycode;
  25. if (userspace_config.is_overwatch) {
  26. keycode = KC_BSPC;
  27. } else {
  28. keycode = KC_ENTER;
  29. }
  30. clear_keyboard();
  31. tap_code(keycode);
  32. wait_ms(50);
  33. send_string_with_delay(str, MACRO_TIMER);
  34. wait_ms(50);
  35. tap_code(KC_ENTER);
  36. }
  37. if (override) wait_ms(3000);
  38. return false;
  39. }
  40. bool mod_key_press_timer (uint16_t code, uint16_t mod_code, bool pressed) {
  41. static uint16_t this_timer;
  42. if(pressed) {
  43. this_timer= timer_read();
  44. } else {
  45. if (timer_elapsed(this_timer) < TAPPING_TERM){
  46. register_code(code);
  47. unregister_code(code);
  48. } else {
  49. register_code(mod_code);
  50. register_code(code);
  51. unregister_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. register_code(code);
  63. unregister_code(code);
  64. } else {
  65. register_code(mod_code);
  66. register_code(code);
  67. unregister_code(code);
  68. unregister_code(mod_code);
  69. }
  70. }
  71. return false;
  72. }
  73. void bootmagic_lite(void) {
  74. matrix_scan();
  75. #if defined(DEBOUNCING_DELAY) && DEBOUNCING_DELAY > 0
  76. wait_ms(DEBOUNCING_DELAY * 2);
  77. #elif defined(DEBOUNCE) && DEBOUNCE > 0
  78. wait_ms(DEBOUNCE * 2);
  79. #else
  80. wait_ms(30);
  81. #endif
  82. matrix_scan();
  83. if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
  84. bootloader_jump();
  85. }
  86. }
  87. // Add reconfigurable functions here, for keymap customization
  88. // This allows for a global, userspace functions, and continued
  89. // customization of the keymap. Use _keymap instead of _user
  90. // functions in the keymaps
  91. __attribute__ ((weak))
  92. void matrix_init_keymap(void) {}
  93. __attribute__ ((weak))
  94. void startup_keymap(void) {}
  95. __attribute__ ((weak))
  96. void shutdown_keymap(void) {}
  97. __attribute__ ((weak))
  98. void suspend_power_down_keymap(void) {}
  99. __attribute__ ((weak))
  100. void suspend_wakeup_init_keymap(void) {}
  101. __attribute__ ((weak))
  102. void matrix_scan_keymap(void) {}
  103. __attribute__ ((weak))
  104. bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
  105. return true;
  106. }
  107. __attribute__ ((weak))
  108. bool process_record_secrets(uint16_t keycode, keyrecord_t *record) {
  109. return true;
  110. }
  111. __attribute__ ((weak))
  112. uint32_t layer_state_set_keymap (uint32_t state) {
  113. return state;
  114. }
  115. __attribute__ ((weak))
  116. uint32_t default_layer_state_set_keymap (uint32_t state) {
  117. return state;
  118. }
  119. __attribute__ ((weak))
  120. void led_set_keymap(uint8_t usb_led) {}
  121. __attribute__ ((weak))
  122. void eeconfig_init_keymap(void) {}
  123. // Call user matrix init, set default RGB colors and then
  124. // call the keymap's init function
  125. void matrix_init_user(void) {
  126. #if !defined(BOOTMAGIC_LITE) && !defined(BOOTMAGIC_ENABLE)
  127. bootmagic_lite();
  128. #endif
  129. userspace_config.raw = eeconfig_read_user();
  130. #ifdef BOOTLOADER_CATERINA
  131. DDRD &= ~(1<<5);
  132. PORTD &= ~(1<<5);
  133. DDRB &= ~(1<<0);
  134. PORTB &= ~(1<<0);
  135. #endif
  136. #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
  137. if (eeprom_read_byte(EECONFIG_UNICODEMODE) != UC_WIN) {
  138. set_unicode_input_mode(UC_WIN);
  139. }
  140. #endif //UNICODE_ENABLE
  141. matrix_init_keymap();
  142. }
  143. void startup_user (void) {
  144. #ifdef RGBLIGHT_ENABLE
  145. matrix_init_rgb();
  146. #endif //RGBLIGHT_ENABLE
  147. startup_keymap();
  148. }
  149. void shutdown_user (void) {
  150. #ifdef RGBLIGHT_ENABLE
  151. rgblight_enable_noeeprom();
  152. rgblight_mode_noeeprom(1);
  153. rgblight_setrgb_red();
  154. #endif // RGBLIGHT_ENABLE
  155. #ifdef RGB_MATRIX_ENABLE
  156. rgb_led led;
  157. for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
  158. led = g_rgb_leds[i];
  159. if (led.matrix_co.raw < 0xFF) {
  160. rgb_matrix_set_color( i, 0xFF, 0x00, 0x00 );
  161. }
  162. }
  163. #endif //RGB_MATRIX_ENABLE
  164. shutdown_keymap();
  165. }
  166. void suspend_power_down_user(void) {
  167. suspend_power_down_keymap();
  168. }
  169. void suspend_wakeup_init_user(void) {
  170. suspend_wakeup_init_keymap();
  171. }
  172. // No global matrix scan code, so just run keymap's matrix
  173. // scan function
  174. void matrix_scan_user(void) {
  175. static bool has_ran_yet;
  176. if (!has_ran_yet) {
  177. has_ran_yet = true;
  178. startup_user();
  179. }
  180. #ifdef TAP_DANCE_ENABLE // Run Diablo 3 macro checking code.
  181. run_diablo_macro_check();
  182. #endif // TAP_DANCE_ENABLE
  183. #ifdef RGBLIGHT_ENABLE
  184. matrix_scan_rgb();
  185. #endif // RGBLIGHT_ENABLE
  186. matrix_scan_keymap();
  187. }
  188. // Defines actions tor my global custom keycodes. Defined in drashna.h file
  189. // Then runs the _keymap's record handier if not processed here
  190. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  191. // If console is enabled, it will print the matrix position and status of each key pressed
  192. #ifdef KEYLOGGER_ENABLE
  193. #if defined(KEYBOARD_ergodox_ez) || defined(KEYBOARD_iris_rev2)
  194. xprintf("KL: col: %u, row: %u, pressed: %u\n", record->event.key.row, record->event.key.col, record->event.pressed);
  195. #else
  196. xprintf("KL: col: %u, row: %u, pressed: %u\n", record->event.key.col, record->event.key.row, record->event.pressed);
  197. #endif
  198. #endif //KEYLOGGER_ENABLE
  199. switch (keycode) {
  200. case KC_QWERTY:
  201. if (record->event.pressed) {
  202. set_single_persistent_default_layer(_QWERTY);
  203. }
  204. break;
  205. case KC_COLEMAK:
  206. if (record->event.pressed) {
  207. set_single_persistent_default_layer(_COLEMAK);
  208. }
  209. break;
  210. case KC_DVORAK:
  211. if (record->event.pressed) {
  212. set_single_persistent_default_layer(_DVORAK);
  213. }
  214. break;
  215. case KC_WORKMAN:
  216. if (record->event.pressed) {
  217. set_single_persistent_default_layer(_WORKMAN);
  218. }
  219. break;
  220. case KC_MAKE: // Compiles the firmware, and adds the flash command based on keyboard bootloader
  221. if (!record->event.pressed) {
  222. uint8_t temp_mod = get_mods();
  223. clear_mods();
  224. send_string_with_delay_P(PSTR("make " QMK_KEYBOARD ":" QMK_KEYMAP), 10);
  225. if (temp_mod & MODS_SHIFT_MASK) {
  226. #if defined(__ARM__)
  227. send_string_with_delay_P(PSTR(":dfu-util"), 10);
  228. #elif defined(BOOTLOADER_DFU)
  229. send_string_with_delay_P(PSTR(":dfu"), 10);
  230. #elif defined(BOOTLOADER_HALFKAY)
  231. send_string_with_delay_P(PSTR(":teensy"), 10);
  232. #elif defined(BOOTLOADER_CATERINA)
  233. send_string_with_delay_P(PSTR(":avrdude"), 10);
  234. #endif // bootloader options
  235. }
  236. #if defined(KEYBOARD_viterbi)
  237. send_string_with_delay_P(PSTR(":dfu"), 10);
  238. #endif
  239. if (temp_mod & MODS_CTRL_MASK) { send_string_with_delay_P(PSTR(" -j8 --output-sync"), 10); }
  240. send_string_with_delay_P(PSTR(SS_TAP(X_ENTER)), 10);
  241. set_mods(temp_mod);
  242. }
  243. break;
  244. case EPRM: // Resets EEPROM
  245. if (record->event.pressed) {
  246. eeconfig_init();
  247. }
  248. break;
  249. case VRSN: // Prints firmware version
  250. if (record->event.pressed) {
  251. send_string_with_delay_P(PSTR(QMK_KEYBOARD "/" QMK_KEYMAP " @ " QMK_VERSION ", Built on: " QMK_BUILDDATE), MACRO_TIMER);
  252. }
  253. break;
  254. // These are a serious of gaming macros.
  255. // Only enables for the viterbi, basically,
  256. // to save on firmware space, since it's limited.
  257. #ifdef MACROS_ENABLED
  258. case KC_OVERWATCH: // Toggle's if we hit "ENTER" or "BACKSPACE" to input macros
  259. if (record->event.pressed) { userspace_config.is_overwatch ^= 1; eeconfig_update_user(userspace_config.raw); }
  260. #ifdef RGBLIGHT_ENABLE
  261. userspace_config.is_overwatch ? rgblight_mode_noeeprom(17) : rgblight_mode_noeeprom(18);
  262. #endif //RGBLIGHT_ENABLE
  263. break;
  264. case KC_SALT:
  265. return send_game_macro("Salt, salt, salt...", record, false);
  266. case KC_MORESALT:
  267. return send_game_macro("Please sir, can I have some more salt?!", record, false);
  268. case KC_SALTHARD:
  269. return send_game_macro("Your salt only makes me harder, and even more aggressive!", record, false);
  270. case KC_GOODGAME:
  271. return send_game_macro("Good game, everyone!", record, false);
  272. case KC_GLHF:
  273. return send_game_macro("Good luck, have fun!!!", record, false);
  274. case KC_SYMM:
  275. return send_game_macro("Left click to win!", record, false);
  276. case KC_JUSTGAME:
  277. return send_game_macro("It may be a game, but if you don't want to actually try, please go play AI, so that people that actually want to take the game seriously and \"get good\" have a place to do so without trolls like you throwing games.", record, false);
  278. case KC_TORB:
  279. return send_game_macro("That was positively riveting!", record, false);
  280. case KC_AIM:
  281. send_game_macro("That aim is absolutely amazing. It's almost like you're a machine!", record, true);
  282. return send_game_macro("Wait! That aim is TOO good! You're clearly using an aim hack! CHEATER!", record, false);
  283. case KC_C9:
  284. return send_game_macro("OMG!!! C9!!!", record, false);
  285. case KC_GGEZ:
  286. return send_game_macro("That was a fantastic game, though it was a bit easy. Try harder next time!", record, false);
  287. #endif // MACROS_ENABLED
  288. case KC_DIABLO_CLEAR: // reset all Diablo timers, disabling them
  289. #ifdef TAP_DANCE_ENABLE
  290. if (record->event.pressed) {
  291. uint8_t dtime;
  292. for (dtime = 0; dtime < 4; dtime++) {
  293. diablo_key_time[dtime] = diablo_times[0];
  294. }
  295. }
  296. #endif // TAP_DANCE_ENABLE
  297. break;
  298. case KC_CCCV: // One key copy/paste
  299. if(record->event.pressed){
  300. copy_paste_timer = timer_read();
  301. } else {
  302. if (timer_elapsed(copy_paste_timer) > TAPPING_TERM) { // Hold, copy
  303. register_code(KC_LCTL);
  304. tap_code(KC_C);
  305. unregister_code(KC_LCTL);
  306. } else { // Tap, paste
  307. register_code(KC_LCTL);
  308. tap_code(KC_V);
  309. unregister_code(KC_LCTL);
  310. }
  311. }
  312. break;
  313. #ifdef UNICODE_ENABLE
  314. case UC_FLIP: // (ノಠ痊ಠ)ノ彡┻━┻
  315. if (record->event.pressed) {
  316. send_unicode_hex_string("0028 30CE 0CA0 75CA 0CA0 0029 30CE 5F61 253B 2501 253B");
  317. }
  318. break;
  319. case UC_TABL: // ┬─┬ノ( º _ ºノ)
  320. if (record->event.pressed) {
  321. send_unicode_hex_string("252C 2500 252C 30CE 0028 0020 00BA 0020 005F 0020 00BA 30CE 0029");
  322. }
  323. break;
  324. case UC_SHRG: // ¯\_(ツ)_/¯
  325. if (record->event.pressed) {
  326. send_unicode_hex_string("00AF 005C 005F 0028 30C4 0029 005F 002F 00AF");
  327. }
  328. break;
  329. case UC_DISA: // ಠ_ಠ
  330. if (record->event.pressed) {
  331. send_unicode_hex_string("0CA0 005F 0CA0");
  332. }
  333. break;
  334. #endif
  335. }
  336. return process_record_keymap(keycode, record) &&
  337. #ifdef RGBLIGHT_ENABLE
  338. process_record_user_rgb(keycode, record) &&
  339. #endif // RGBLIGHT_ENABLE
  340. process_record_secrets(keycode, record);
  341. }
  342. // Runs state check and changes underglow color and animation
  343. // on layer change, no matter where the change was initiated
  344. // Then runs keymap's layer change check
  345. uint32_t layer_state_set_user(uint32_t state) {
  346. state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
  347. #ifdef RGBLIGHT_ENABLE
  348. state = layer_state_set_rgb(state);
  349. #endif // RGBLIGHT_ENABLE
  350. return layer_state_set_keymap (state);
  351. }
  352. uint32_t default_layer_state_set_user(uint32_t state) {
  353. return default_layer_state_set_keymap(state);
  354. }
  355. // Any custom LED code goes here.
  356. // So far, I only have keyboard specific code,
  357. // So nothing goes here.
  358. void led_set_user(uint8_t usb_led) {
  359. led_set_keymap(usb_led);
  360. }
  361. void eeconfig_init_user(void) {
  362. userspace_config.raw = 0;
  363. userspace_config.rgb_layer_change = true;
  364. eeconfig_update_user(userspace_config.raw);
  365. }