drashna.c 12 KB

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