drashna.c 12 KB

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