2
0

drashna.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. // Helper Functions
  19. // This block is for all of the gaming macros, as they were all doing
  20. // the same thing, but with differring text sent.
  21. bool send_game_macro(const char *str, keyrecord_t *record, bool override) {
  22. if (!record->event.pressed || override) {
  23. uint16_t keycode;
  24. if (userspace_config.is_overwatch) {
  25. keycode = KC_BSPC;
  26. } else {
  27. keycode = KC_ENTER;
  28. }
  29. clear_keyboard();
  30. tap(keycode);
  31. wait_ms(50);
  32. send_string_with_delay(str, MACRO_TIMER);
  33. wait_ms(50);
  34. tap(KC_ENTER);
  35. }
  36. if (override) wait_ms(3000);
  37. return false;
  38. }
  39. bool mod_key_press_timer (uint16_t code, uint16_t mod_code, bool pressed) {
  40. static uint16_t this_timer;
  41. if(pressed) {
  42. this_timer= timer_read();
  43. } else {
  44. if (timer_elapsed(this_timer) < TAPPING_TERM){
  45. register_code(code);
  46. unregister_code(code);
  47. } else {
  48. register_code(mod_code);
  49. register_code(code);
  50. unregister_code(code);
  51. unregister_code(mod_code);
  52. }
  53. }
  54. return false;
  55. }
  56. bool mod_key_press (uint16_t code, uint16_t mod_code, bool pressed, uint16_t this_timer) {
  57. if(pressed) {
  58. this_timer= timer_read();
  59. } else {
  60. if (timer_elapsed(this_timer) < TAPPING_TERM){
  61. register_code(code);
  62. unregister_code(code);
  63. } else {
  64. register_code(mod_code);
  65. register_code(code);
  66. unregister_code(code);
  67. unregister_code(mod_code);
  68. }
  69. }
  70. return false;
  71. }
  72. // Add reconfigurable functions here, for keymap customization
  73. // This allows for a global, userspace functions, and continued
  74. // customization of the keymap. Use _keymap instead of _user
  75. // functions in the keymaps
  76. __attribute__ ((weak))
  77. void matrix_init_keymap(void) {}
  78. __attribute__ ((weak))
  79. void startup_keymap(void) {}
  80. __attribute__ ((weak))
  81. void shutdown_keymap(void) {}
  82. __attribute__ ((weak))
  83. void suspend_power_down_keymap(void) {}
  84. __attribute__ ((weak))
  85. void suspend_wakeup_init_keymap(void) {}
  86. __attribute__ ((weak))
  87. void matrix_scan_keymap(void) {}
  88. __attribute__ ((weak))
  89. bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
  90. return true;
  91. }
  92. __attribute__ ((weak))
  93. bool process_record_secrets(uint16_t keycode, keyrecord_t *record) {
  94. return true;
  95. }
  96. __attribute__ ((weak))
  97. uint32_t layer_state_set_keymap (uint32_t state) {
  98. return state;
  99. }
  100. __attribute__ ((weak))
  101. uint32_t default_layer_state_set_keymap (uint32_t state) {
  102. return state;
  103. }
  104. __attribute__ ((weak))
  105. void led_set_keymap(uint8_t usb_led) {}
  106. // Call user matrix init, set default RGB colors and then
  107. // call the keymap's init function
  108. void matrix_init_user(void) {
  109. userspace_config.raw = eeprom_read_byte(EECONFIG_USERSPACE);
  110. #ifdef BOOTLOADER_CATERINA
  111. DDRD &= ~(1<<5);
  112. PORTD &= ~(1<<5);
  113. DDRB &= ~(1<<0);
  114. PORTB &= ~(1<<0);
  115. #endif
  116. #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
  117. set_unicode_input_mode(UC_WINC);
  118. #endif //UNICODE_ENABLE
  119. matrix_init_keymap();
  120. }
  121. void startup_user (void) {
  122. #ifdef RGBLIGHT_ENABLE
  123. matrix_init_rgb();
  124. #endif //RGBLIGHT_ENABLE
  125. startup_keymap();
  126. }
  127. void shutdown_user (void) {
  128. #ifdef RGBLIGHT_ENABLE
  129. rgblight_enable_noeeprom();
  130. rgblight_mode_noeeprom(1);
  131. rgblight_setrgb_red();
  132. #endif // RGBLIGHT_ENABLE
  133. #ifdef RGB_MATRIX_ENABLE
  134. rgb_led led;
  135. for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
  136. led = g_rgb_leds[i];
  137. if (led.matrix_co.raw < 0xFF) {
  138. rgb_matrix_set_color( i, 0xFF, 0x00, 0x00 );
  139. }
  140. }
  141. #endif //RGB_MATRIX_ENABLE
  142. shutdown_keymap();
  143. }
  144. void suspend_power_down_user(void)
  145. {
  146. suspend_power_down_keymap();
  147. }
  148. void suspend_wakeup_init_user(void)
  149. {
  150. suspend_wakeup_init_keymap();
  151. #ifdef KEYBOARD_ergodox_ez
  152. wait_ms(10);
  153. #endif
  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. xprintf("KL: row: %u, column: %u, pressed: %u\n", record->event.key.col, record->event.key.row, record->event.pressed);
  177. #endif //KEYLOGGER_ENABLE
  178. switch (keycode) {
  179. case KC_QWERTY:
  180. if (record->event.pressed) {
  181. set_single_persistent_default_layer(_QWERTY);
  182. }
  183. return false;
  184. break;
  185. case KC_COLEMAK:
  186. if (record->event.pressed) {
  187. set_single_persistent_default_layer(_COLEMAK);
  188. }
  189. return false;
  190. break;
  191. case KC_DVORAK:
  192. if (record->event.pressed) {
  193. set_single_persistent_default_layer(_DVORAK);
  194. }
  195. return false;
  196. break;
  197. case KC_WORKMAN:
  198. if (record->event.pressed) {
  199. set_single_persistent_default_layer(_WORKMAN);
  200. }
  201. return false;
  202. break;
  203. case KC_MAKE: // Compiles the firmware, and adds the flash command based on keyboard bootloader
  204. if (!record->event.pressed) {
  205. send_string_with_delay_P(PSTR("make " QMK_KEYBOARD ":" QMK_KEYMAP
  206. #if (defined(BOOTLOADER_DFU) || defined(BOOTLOADER_LUFA_DFU) || defined(BOOTLOADER_QMK_DFU))
  207. ":dfu"
  208. #elif defined(BOOTLOADER_HALFKAY)
  209. ":teensy"
  210. #elif defined(BOOTLOADER_CATERINA)
  211. ":avrdude"
  212. #endif // bootloader options
  213. SS_TAP(X_ENTER)), 10);
  214. }
  215. return false;
  216. break;
  217. case EPRM: // Resets EEPROM
  218. if (record->event.pressed) {
  219. eeconfig_init();
  220. default_layer_set(1UL<<eeconfig_read_default_layer());
  221. layer_state_set(layer_state);
  222. }
  223. return false;
  224. break;
  225. case VRSN: // Prints firmware version
  226. if (record->event.pressed) {
  227. send_string_with_delay_P(PSTR(QMK_KEYBOARD "/" QMK_KEYMAP " @ " QMK_VERSION ", Built on: " QMK_BUILDDATE), MACRO_TIMER);
  228. }
  229. return false;
  230. break;
  231. /* Code has been depreciated
  232. case KC_SECRET_1 ... KC_SECRET_5: // Secrets! Externally defined strings, not stored in repo
  233. if (!record->event.pressed) {
  234. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  235. send_string(decoy_secret[keycode - KC_SECRET_1]);
  236. }
  237. return false;
  238. break;
  239. */
  240. // These are a serious of gaming macros.
  241. // Only enables for the viterbi, basically,
  242. // to save on firmware space, since it's limited.
  243. #ifdef MACROS_ENABLED
  244. case KC_OVERWATCH: // Toggle's if we hit "ENTER" or "BACKSPACE" to input macros
  245. if (record->event.pressed) { userspace_config.is_overwatch ^= 1; eeprom_update_byte(EECONFIG_USERSPACE, userspace_config.raw); }
  246. #ifdef RGBLIGHT_ENABLE
  247. userspace_config.is_overwatch ? rgblight_mode_noeeprom(17) : rgblight_mode_noeeprom(18);
  248. #endif //RGBLIGHT_ENABLE
  249. return false; break;
  250. case KC_SALT:
  251. return send_game_macro("Salt, salt, salt...", record, false);
  252. case KC_MORESALT:
  253. return send_game_macro("Please sir, can I have some more salt?!", record, false);
  254. case KC_SALTHARD:
  255. return send_game_macro("Your salt only makes me harder, and even more aggressive!", record, false);
  256. case KC_GOODGAME:
  257. return send_game_macro("Good game, everyone!", record, false);
  258. case KC_GLHF:
  259. return send_game_macro("Good luck, have fun!!!", record, false);
  260. case KC_SYMM:
  261. return send_game_macro("Left click to win!", record, false);
  262. case KC_JUSTGAME:
  263. 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);
  264. case KC_TORB:
  265. return send_game_macro("That was positively riveting!", record, false);
  266. case KC_AIM:
  267. send_game_macro("That aim is absolutely amazing. It's almost like you're a machine!", record, true);
  268. return send_game_macro("Wait! That aim is TOO good! You're clearly using an aim hack! CHEATER!", record, false);
  269. case KC_C9:
  270. return send_game_macro("OMG!!! C9!!!", record, false);
  271. case KC_GGEZ:
  272. return send_game_macro("That was a fantastic game, though it was a bit easy. Try harder next time!", record, false);
  273. #endif // MACROS_ENABLED
  274. case KC_DIABLO_CLEAR: // reset all Diablo timers, disabling them
  275. #ifdef TAP_DANCE_ENABLE
  276. if (record->event.pressed) {
  277. uint8_t dtime;
  278. for (dtime = 0; dtime < 4; dtime++) {
  279. diablo_key_time[dtime] = diablo_times[0];
  280. }
  281. }
  282. #endif // TAP_DANCE_ENABLE
  283. return false; break;
  284. case KC_CCCV: // One key copy/paste
  285. if(record->event.pressed){
  286. copy_paste_timer = timer_read();
  287. } else {
  288. if (timer_elapsed(copy_paste_timer) > TAPPING_TERM) { // Hold, copy
  289. register_code(KC_LCTL);
  290. tap(KC_C);
  291. unregister_code(KC_LCTL);
  292. #ifdef AUDIO_ENABLE
  293. PLAY_SONG(tone_copy);
  294. #endif
  295. } else { // Tap, paste
  296. register_code(KC_LCTL);
  297. tap(KC_V);
  298. unregister_code(KC_LCTL);
  299. #ifdef AUDIO_ENABLE
  300. PLAY_SONG(tone_paste);
  301. #endif
  302. }
  303. }
  304. return false;
  305. break;
  306. #ifdef UNICODE_ENABLE
  307. case UC_FLIP: // (╯°□°)╯ ︵ ┻━┻
  308. if (record->event.pressed) {
  309. register_code(KC_RSFT);
  310. tap(KC_9);
  311. unregister_code(KC_RSFT);
  312. process_unicode((0x256F | QK_UNICODE), record); // Arm
  313. process_unicode((0x00B0 | QK_UNICODE), record); // Eye
  314. process_unicode((0x25A1 | QK_UNICODE), record); // Mouth
  315. process_unicode((0x00B0 | QK_UNICODE), record); // Eye
  316. register_code(KC_RSFT);
  317. tap(KC_0);
  318. unregister_code(KC_RSFT);
  319. process_unicode((0x256F | QK_UNICODE), record); // Arm
  320. tap(KC_SPC);
  321. process_unicode((0x0361 | QK_UNICODE), record); // Flippy
  322. tap(KC_SPC);
  323. process_unicode((0x253B | QK_UNICODE), record); // Table
  324. process_unicode((0x2501 | QK_UNICODE), record); // Table
  325. process_unicode((0x253B | QK_UNICODE), record); // Table
  326. }
  327. return false;
  328. break;
  329. #endif // UNICODE_ENABLE
  330. }
  331. return process_record_keymap(keycode, record) &&
  332. #ifdef RGBLIGHT_ENABLE
  333. process_record_user_rgb(keycode, record) &&
  334. #endif // RGBLIGHT_ENABLE
  335. process_record_secrets(keycode, record);
  336. }
  337. // Runs state check and changes underglow color and animation
  338. // on layer change, no matter where the change was initiated
  339. // Then runs keymap's layer change check
  340. uint32_t layer_state_set_user(uint32_t state) {
  341. state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
  342. #ifdef RGBLIGHT_ENABLE
  343. state = layer_state_set_rgb(state);
  344. #endif // RGBLIGHT_ENABLE
  345. return layer_state_set_keymap (state);
  346. }
  347. uint32_t default_layer_state_set_kb(uint32_t state) {
  348. return default_layer_state_set_keymap (state);
  349. }
  350. // Any custom LED code goes here.
  351. // So far, I only have keyboard specific code,
  352. // So nothing goes here.
  353. void led_set_user(uint8_t usb_led) {
  354. led_set_keymap(usb_led);
  355. }