drashna.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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(keycode);
  32. wait_ms(50);
  33. send_string_with_delay(str, MACRO_TIMER);
  34. wait_ms(50);
  35. tap(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. // Call user matrix init, set default RGB colors and then
  108. // call the keymap's init function
  109. void matrix_init_user(void) {
  110. userspace_config.raw = eeprom_read_dword(EECONFIG_USERSPACE);
  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. if (eeprom_read_byte(EECONFIG_UNICODEMODE) != UC_WIN) {
  119. set_unicode_input_mode(UC_WIN);
  120. }
  121. #endif //UNICODE_ENABLE
  122. matrix_init_keymap();
  123. }
  124. void startup_user (void) {
  125. #ifdef RGBLIGHT_ENABLE
  126. matrix_init_rgb();
  127. #endif //RGBLIGHT_ENABLE
  128. startup_keymap();
  129. }
  130. void shutdown_user (void) {
  131. #ifdef RGBLIGHT_ENABLE
  132. rgblight_enable_noeeprom();
  133. rgblight_mode_noeeprom(1);
  134. rgblight_setrgb_red();
  135. #endif // RGBLIGHT_ENABLE
  136. #ifdef RGB_MATRIX_ENABLE
  137. rgb_led led;
  138. for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
  139. led = g_rgb_leds[i];
  140. if (led.matrix_co.raw < 0xFF) {
  141. rgb_matrix_set_color( i, 0xFF, 0x00, 0x00 );
  142. }
  143. }
  144. #endif //RGB_MATRIX_ENABLE
  145. shutdown_keymap();
  146. }
  147. void suspend_power_down_user(void) {
  148. suspend_power_down_keymap();
  149. }
  150. void suspend_wakeup_init_user(void) {
  151. suspend_wakeup_init_keymap();
  152. }
  153. // No global matrix scan code, so just run keymap's matrix
  154. // scan function
  155. void matrix_scan_user(void) {
  156. static bool has_ran_yet;
  157. if (!has_ran_yet) {
  158. has_ran_yet = true;
  159. startup_user();
  160. }
  161. #ifdef TAP_DANCE_ENABLE // Run Diablo 3 macro checking code.
  162. run_diablo_macro_check();
  163. #endif // TAP_DANCE_ENABLE
  164. #ifdef RGBLIGHT_ENABLE
  165. matrix_scan_rgb();
  166. #endif // RGBLIGHT_ENABLE
  167. matrix_scan_keymap();
  168. }
  169. // Defines actions tor my global custom keycodes. Defined in drashna.h file
  170. // Then runs the _keymap's record handier if not processed here
  171. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  172. // If console is enabled, it will print the matrix position and status of each key pressed
  173. #ifdef KEYLOGGER_ENABLE
  174. #if defined(KEYBOARD_ergodox_ez) || defined(KEYBOARD_iris_rev2)
  175. xprintf("KL: col: %u, row: %u, pressed: %u\n", record->event.key.row, record->event.key.col, record->event.pressed);
  176. #else
  177. xprintf("KL: col: %u, row: %u, pressed: %u\n", record->event.key.col, record->event.key.row, record->event.pressed);
  178. #endif
  179. #endif //KEYLOGGER_ENABLE
  180. switch (keycode) {
  181. case KC_QWERTY:
  182. if (record->event.pressed) {
  183. set_single_persistent_default_layer(_QWERTY);
  184. }
  185. return false;
  186. break;
  187. case KC_COLEMAK:
  188. if (record->event.pressed) {
  189. set_single_persistent_default_layer(_COLEMAK);
  190. }
  191. return false;
  192. break;
  193. case KC_DVORAK:
  194. if (record->event.pressed) {
  195. set_single_persistent_default_layer(_DVORAK);
  196. }
  197. return false;
  198. break;
  199. case KC_WORKMAN:
  200. if (record->event.pressed) {
  201. set_single_persistent_default_layer(_WORKMAN);
  202. }
  203. return false;
  204. break;
  205. case KC_MAKE: // Compiles the firmware, and adds the flash command based on keyboard bootloader
  206. if (!record->event.pressed) {
  207. send_string_with_delay_P(PSTR("make " QMK_KEYBOARD ":" QMK_KEYMAP
  208. #if defined(__ARM__)
  209. ":dfu-util"
  210. #elif defined(BOOTLOADER_DFU)
  211. ":dfu"
  212. #elif defined(BOOTLOADER_HALFKAY)
  213. ":teensy"
  214. #elif defined(BOOTLOADER_CATERINA)
  215. ":avrdude"
  216. #endif // bootloader options
  217. SS_TAP(X_ENTER)), 10);
  218. }
  219. return false;
  220. break;
  221. case EPRM: // Resets EEPROM
  222. if (record->event.pressed) {
  223. eeconfig_init();
  224. default_layer_set(1UL<<eeconfig_read_default_layer());
  225. layer_state_set(layer_state);
  226. }
  227. return false;
  228. break;
  229. case VRSN: // Prints firmware version
  230. if (record->event.pressed) {
  231. send_string_with_delay_P(PSTR(QMK_KEYBOARD "/" QMK_KEYMAP " @ " QMK_VERSION ", Built on: " QMK_BUILDDATE), MACRO_TIMER);
  232. }
  233. return false;
  234. break;
  235. /* Code has been depreciated
  236. case KC_SECRET_1 ... KC_SECRET_5: // Secrets! Externally defined strings, not stored in repo
  237. if (!record->event.pressed) {
  238. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  239. send_string(decoy_secret[keycode - KC_SECRET_1]);
  240. }
  241. return false;
  242. break;
  243. */
  244. // These are a serious of gaming macros.
  245. // Only enables for the viterbi, basically,
  246. // to save on firmware space, since it's limited.
  247. #ifdef MACROS_ENABLED
  248. case KC_OVERWATCH: // Toggle's if we hit "ENTER" or "BACKSPACE" to input macros
  249. if (record->event.pressed) { userspace_config.is_overwatch ^= 1; eeprom_update_dword(EECONFIG_USERSPACE, userspace_config.raw); }
  250. #ifdef RGBLIGHT_ENABLE
  251. userspace_config.is_overwatch ? rgblight_mode_noeeprom(17) : rgblight_mode_noeeprom(18);
  252. #endif //RGBLIGHT_ENABLE
  253. return false; break;
  254. case KC_SALT:
  255. return send_game_macro("Salt, salt, salt...", record, false);
  256. case KC_MORESALT:
  257. return send_game_macro("Please sir, can I have some more salt?!", record, false);
  258. case KC_SALTHARD:
  259. return send_game_macro("Your salt only makes me harder, and even more aggressive!", record, false);
  260. case KC_GOODGAME:
  261. return send_game_macro("Good game, everyone!", record, false);
  262. case KC_GLHF:
  263. return send_game_macro("Good luck, have fun!!!", record, false);
  264. case KC_SYMM:
  265. return send_game_macro("Left click to win!", record, false);
  266. case KC_JUSTGAME:
  267. 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);
  268. case KC_TORB:
  269. return send_game_macro("That was positively riveting!", record, false);
  270. case KC_AIM:
  271. send_game_macro("That aim is absolutely amazing. It's almost like you're a machine!", record, true);
  272. return send_game_macro("Wait! That aim is TOO good! You're clearly using an aim hack! CHEATER!", record, false);
  273. case KC_C9:
  274. return send_game_macro("OMG!!! C9!!!", record, false);
  275. case KC_GGEZ:
  276. return send_game_macro("That was a fantastic game, though it was a bit easy. Try harder next time!", record, false);
  277. #endif // MACROS_ENABLED
  278. case KC_DIABLO_CLEAR: // reset all Diablo timers, disabling them
  279. #ifdef TAP_DANCE_ENABLE
  280. if (record->event.pressed) {
  281. uint8_t dtime;
  282. for (dtime = 0; dtime < 4; dtime++) {
  283. diablo_key_time[dtime] = diablo_times[0];
  284. }
  285. }
  286. #endif // TAP_DANCE_ENABLE
  287. return false; break;
  288. case KC_CCCV: // One key copy/paste
  289. if(record->event.pressed){
  290. copy_paste_timer = timer_read();
  291. } else {
  292. if (timer_elapsed(copy_paste_timer) > TAPPING_TERM) { // Hold, copy
  293. register_code(KC_LCTL);
  294. tap(KC_C);
  295. unregister_code(KC_LCTL);
  296. } else { // Tap, paste
  297. register_code(KC_LCTL);
  298. tap(KC_V);
  299. unregister_code(KC_LCTL);
  300. }
  301. }
  302. return false;
  303. break;
  304. #ifdef UNICODE_ENABLE
  305. case UC_FLIP: // (ノಠ痊ಠ)ノ彡┻━┻
  306. if (record->event.pressed) {
  307. send_unicode_hex_string("0028 30CE 0CA0 75CA 0CA0 0029 30CE 5F61 253B 2501 253B");
  308. }
  309. return false;
  310. break;
  311. case UC_TABL: // ┬─┬ノ( º _ ºノ)
  312. if (record->event.pressed) {
  313. send_unicode_hex_string("252C 2500 252C 30CE 0028 0020 00BA 0020 005F 0020 00BA 30CE 0029");
  314. }
  315. return false;
  316. break;
  317. case UC_SHRG: // ¯\_(ツ)_/¯
  318. if (record->event.pressed) {
  319. send_unicode_hex_string("00AF 005C 005F 0028 30C4 0029 005F 002F 00AF");
  320. }
  321. return false;
  322. break;
  323. case UC_DISA: // ಠ_ಠ
  324. if (record->event.pressed) {
  325. send_unicode_hex_string("0CA0 005F 0CA0");
  326. }
  327. return false;
  328. break;
  329. #endif
  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_user(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. }