drashna.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. return false;
  188. break;
  189. case KC_COLEMAK:
  190. if (record->event.pressed) {
  191. set_single_persistent_default_layer(_COLEMAK);
  192. }
  193. return false;
  194. break;
  195. case KC_DVORAK:
  196. if (record->event.pressed) {
  197. set_single_persistent_default_layer(_DVORAK);
  198. }
  199. return false;
  200. break;
  201. case KC_WORKMAN:
  202. if (record->event.pressed) {
  203. set_single_persistent_default_layer(_WORKMAN);
  204. }
  205. return false;
  206. break;
  207. case KC_MAKE: // Compiles the firmware, and adds the flash command based on keyboard bootloader
  208. if (!record->event.pressed) {
  209. uint8_t temp_mod = get_mods();
  210. clear_mods();
  211. send_string_with_delay_P(PSTR("make " QMK_KEYBOARD ":" QMK_KEYMAP), 10);
  212. if (temp_mod & MODS_SHIFT_MASK) {
  213. send_string_with_delay_P(PSTR(
  214. #if defined(__ARM__)
  215. ":dfu-util"
  216. #elif defined(BOOTLOADER_DFU)
  217. ":dfu"
  218. #elif defined(BOOTLOADER_HALFKAY)
  219. ":teensy"
  220. #elif defined(BOOTLOADER_CATERINA)
  221. ":avrdude"
  222. #endif // bootloader options
  223. ), 10);
  224. }
  225. if (temp_mod & MODS_CTRL_MASK) { send_string_with_delay_P(PSTR(" -j8 --output-sync"), 10); }
  226. send_string_with_delay_P(PSTR(SS_TAP(X_ENTER)), 10);
  227. set_mods(temp_mod);
  228. }
  229. return false;
  230. break;
  231. case EPRM: // Resets EEPROM
  232. if (record->event.pressed) {
  233. eeconfig_init();
  234. }
  235. return false;
  236. break;
  237. case VRSN: // Prints firmware version
  238. if (record->event.pressed) {
  239. send_string_with_delay_P(PSTR(QMK_KEYBOARD "/" QMK_KEYMAP " @ " QMK_VERSION ", Built on: " QMK_BUILDDATE), MACRO_TIMER);
  240. }
  241. return false;
  242. break;
  243. /* Code has been depreciated
  244. case KC_SECRET_1 ... KC_SECRET_5: // Secrets! Externally defined strings, not stored in repo
  245. if (!record->event.pressed) {
  246. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  247. send_string(decoy_secret[keycode - KC_SECRET_1]);
  248. }
  249. return false;
  250. break;
  251. */
  252. // These are a serious of gaming macros.
  253. // Only enables for the viterbi, basically,
  254. // to save on firmware space, since it's limited.
  255. #ifdef MACROS_ENABLED
  256. case KC_OVERWATCH: // Toggle's if we hit "ENTER" or "BACKSPACE" to input macros
  257. if (record->event.pressed) { userspace_config.is_overwatch ^= 1; eeconfig_update_user(userspace_config.raw); }
  258. #ifdef RGBLIGHT_ENABLE
  259. userspace_config.is_overwatch ? rgblight_mode_noeeprom(17) : rgblight_mode_noeeprom(18);
  260. #endif //RGBLIGHT_ENABLE
  261. return false; break;
  262. case KC_SALT:
  263. return send_game_macro("Salt, salt, salt...", record, false);
  264. case KC_MORESALT:
  265. return send_game_macro("Please sir, can I have some more salt?!", record, false);
  266. case KC_SALTHARD:
  267. return send_game_macro("Your salt only makes me harder, and even more aggressive!", record, false);
  268. case KC_GOODGAME:
  269. return send_game_macro("Good game, everyone!", record, false);
  270. case KC_GLHF:
  271. return send_game_macro("Good luck, have fun!!!", record, false);
  272. case KC_SYMM:
  273. return send_game_macro("Left click to win!", record, false);
  274. case KC_JUSTGAME:
  275. 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);
  276. case KC_TORB:
  277. return send_game_macro("That was positively riveting!", record, false);
  278. case KC_AIM:
  279. send_game_macro("That aim is absolutely amazing. It's almost like you're a machine!", record, true);
  280. return send_game_macro("Wait! That aim is TOO good! You're clearly using an aim hack! CHEATER!", record, false);
  281. case KC_C9:
  282. return send_game_macro("OMG!!! C9!!!", record, false);
  283. case KC_GGEZ:
  284. return send_game_macro("That was a fantastic game, though it was a bit easy. Try harder next time!", record, false);
  285. #endif // MACROS_ENABLED
  286. case KC_DIABLO_CLEAR: // reset all Diablo timers, disabling them
  287. #ifdef TAP_DANCE_ENABLE
  288. if (record->event.pressed) {
  289. uint8_t dtime;
  290. for (dtime = 0; dtime < 4; dtime++) {
  291. diablo_key_time[dtime] = diablo_times[0];
  292. }
  293. }
  294. #endif // TAP_DANCE_ENABLE
  295. return false; break;
  296. case KC_CCCV: // One key copy/paste
  297. if(record->event.pressed){
  298. copy_paste_timer = timer_read();
  299. } else {
  300. if (timer_elapsed(copy_paste_timer) > TAPPING_TERM) { // Hold, copy
  301. register_code(KC_LCTL);
  302. tap_code(KC_C);
  303. unregister_code(KC_LCTL);
  304. } else { // Tap, paste
  305. register_code(KC_LCTL);
  306. tap_code(KC_V);
  307. unregister_code(KC_LCTL);
  308. }
  309. }
  310. return false;
  311. break;
  312. #ifdef UNICODE_ENABLE
  313. case UC_FLIP: // (ノಠ痊ಠ)ノ彡┻━┻
  314. if (record->event.pressed) {
  315. send_unicode_hex_string("0028 30CE 0CA0 75CA 0CA0 0029 30CE 5F61 253B 2501 253B");
  316. }
  317. return false;
  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. return false;
  324. break;
  325. case UC_SHRG: // ¯\_(ツ)_/¯
  326. if (record->event.pressed) {
  327. send_unicode_hex_string("00AF 005C 005F 0028 30C4 0029 005F 002F 00AF");
  328. }
  329. return false;
  330. break;
  331. case UC_DISA: // ಠ_ಠ
  332. if (record->event.pressed) {
  333. send_unicode_hex_string("0CA0 005F 0CA0");
  334. }
  335. return false;
  336. break;
  337. #endif
  338. }
  339. return process_record_keymap(keycode, record) &&
  340. #ifdef RGBLIGHT_ENABLE
  341. process_record_user_rgb(keycode, record) &&
  342. #endif // RGBLIGHT_ENABLE
  343. process_record_secrets(keycode, record);
  344. }
  345. // Runs state check and changes underglow color and animation
  346. // on layer change, no matter where the change was initiated
  347. // Then runs keymap's layer change check
  348. uint32_t layer_state_set_user(uint32_t state) {
  349. state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
  350. #ifdef RGBLIGHT_ENABLE
  351. state = layer_state_set_rgb(state);
  352. #endif // RGBLIGHT_ENABLE
  353. return layer_state_set_keymap (state);
  354. }
  355. uint32_t default_layer_state_set_user(uint32_t state) {
  356. return default_layer_state_set_keymap(state);
  357. }
  358. // Any custom LED code goes here.
  359. // So far, I only have keyboard specific code,
  360. // So nothing goes here.
  361. void led_set_user(uint8_t usb_led) {
  362. led_set_keymap(usb_led);
  363. }
  364. void eeconfig_init_user(void) {
  365. userspace_config.raw = 0;
  366. eeconfig_update_user(userspace_config.raw);
  367. }