drashna.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 AUDIO_CLICKY
  111. clicky_enable = userspace_config.clicky_enable;
  112. #endif
  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. set_unicode_input_mode(UC_WINC);
  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. {
  149. suspend_power_down_keymap();
  150. }
  151. void suspend_wakeup_init_user(void)
  152. {
  153. suspend_wakeup_init_keymap();
  154. #ifdef KEYBOARD_ergodox_ez
  155. wait_ms(10);
  156. #endif
  157. }
  158. // No global matrix scan code, so just run keymap's matrix
  159. // scan function
  160. void matrix_scan_user(void) {
  161. static bool has_ran_yet;
  162. if (!has_ran_yet) {
  163. has_ran_yet = true;
  164. startup_user();
  165. }
  166. #ifdef TAP_DANCE_ENABLE // Run Diablo 3 macro checking code.
  167. run_diablo_macro_check();
  168. #endif // TAP_DANCE_ENABLE
  169. #ifdef RGBLIGHT_ENABLE
  170. matrix_scan_rgb();
  171. #endif // RGBLIGHT_ENABLE
  172. matrix_scan_keymap();
  173. }
  174. // Defines actions tor my global custom keycodes. Defined in drashna.h file
  175. // Then runs the _keymap's record handier if not processed here
  176. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  177. // If console is enabled, it will print the matrix position and status of each key pressed
  178. #ifdef KEYLOGGER_ENABLE
  179. xprintf("KL: row: %u, column: %u, pressed: %u\n", record->event.key.col, record->event.key.row, record->event.pressed);
  180. #endif //KEYLOGGER_ENABLE
  181. switch (keycode) {
  182. case KC_QWERTY:
  183. if (record->event.pressed) {
  184. set_single_persistent_default_layer(_QWERTY);
  185. }
  186. return false;
  187. break;
  188. case KC_COLEMAK:
  189. if (record->event.pressed) {
  190. set_single_persistent_default_layer(_COLEMAK);
  191. }
  192. return false;
  193. break;
  194. case KC_DVORAK:
  195. if (record->event.pressed) {
  196. set_single_persistent_default_layer(_DVORAK);
  197. }
  198. return false;
  199. break;
  200. case KC_WORKMAN:
  201. if (record->event.pressed) {
  202. set_single_persistent_default_layer(_WORKMAN);
  203. }
  204. return false;
  205. break;
  206. case KC_MAKE: // Compiles the firmware, and adds the flash command based on keyboard bootloader
  207. if (!record->event.pressed) {
  208. send_string_with_delay_P(PSTR("make " QMK_KEYBOARD ":" QMK_KEYMAP
  209. #if (defined(BOOTLOADER_DFU) || defined(BOOTLOADER_LUFA_DFU) || defined(BOOTLOADER_QMK_DFU))
  210. ":dfu"
  211. #elif defined(BOOTLOADER_HALFKAY)
  212. ":teensy"
  213. #elif defined(BOOTLOADER_CATERINA)
  214. ":avrdude"
  215. #endif // bootloader options
  216. SS_TAP(X_ENTER)), 10);
  217. }
  218. return false;
  219. break;
  220. case EPRM: // Resets EEPROM
  221. if (record->event.pressed) {
  222. eeconfig_init();
  223. default_layer_set(1UL<<eeconfig_read_default_layer());
  224. layer_state_set(layer_state);
  225. }
  226. return false;
  227. break;
  228. case VRSN: // Prints firmware version
  229. if (record->event.pressed) {
  230. send_string_with_delay_P(PSTR(QMK_KEYBOARD "/" QMK_KEYMAP " @ " QMK_VERSION ", Built on: " QMK_BUILDDATE), MACRO_TIMER);
  231. }
  232. return false;
  233. break;
  234. /* Code has been depreciated
  235. case KC_SECRET_1 ... KC_SECRET_5: // Secrets! Externally defined strings, not stored in repo
  236. if (!record->event.pressed) {
  237. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  238. send_string(decoy_secret[keycode - KC_SECRET_1]);
  239. }
  240. return false;
  241. break;
  242. */
  243. // These are a serious of gaming macros.
  244. // Only enables for the viterbi, basically,
  245. // to save on firmware space, since it's limited.
  246. #ifdef MACROS_ENABLED
  247. case KC_OVERWATCH: // Toggle's if we hit "ENTER" or "BACKSPACE" to input macros
  248. if (record->event.pressed) { userspace_config.is_overwatch ^= 1; eeprom_update_byte(EECONFIG_USERSPACE, userspace_config.raw); }
  249. #ifdef RGBLIGHT_ENABLE
  250. userspace_config.is_overwatch ? rgblight_mode_noeeprom(17) : rgblight_mode_noeeprom(18);
  251. #endif //RGBLIGHT_ENABLE
  252. return false; break;
  253. case KC_SALT:
  254. return send_game_macro("Salt, salt, salt...", record, false);
  255. case KC_MORESALT:
  256. return send_game_macro("Please sir, can I have some more salt?!", record, false);
  257. case KC_SALTHARD:
  258. return send_game_macro("Your salt only makes me harder, and even more aggressive!", record, false);
  259. case KC_GOODGAME:
  260. return send_game_macro("Good game, everyone!", record, false);
  261. case KC_GLHF:
  262. return send_game_macro("Good luck, have fun!!!", record, false);
  263. case KC_SYMM:
  264. return send_game_macro("Left click to win!", record, false);
  265. case KC_JUSTGAME:
  266. 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);
  267. case KC_TORB:
  268. return send_game_macro("That was positively riveting!", record, false);
  269. case KC_AIM:
  270. send_game_macro("That aim is absolutely amazing. It's almost like you're a machine!", record, true);
  271. return send_game_macro("Wait! That aim is TOO good! You're clearly using an aim hack! CHEATER!", record, false);
  272. case KC_C9:
  273. return send_game_macro("OMG!!! C9!!!", record, false);
  274. case KC_GGEZ:
  275. return send_game_macro("That was a fantastic game, though it was a bit easy. Try harder next time!", record, false);
  276. #endif // MACROS_ENABLED
  277. case KC_DIABLO_CLEAR: // reset all Diablo timers, disabling them
  278. #ifdef TAP_DANCE_ENABLE
  279. if (record->event.pressed) {
  280. uint8_t dtime;
  281. for (dtime = 0; dtime < 4; dtime++) {
  282. diablo_key_time[dtime] = diablo_times[0];
  283. }
  284. }
  285. #endif // TAP_DANCE_ENABLE
  286. return false; break;
  287. case CLICKY_TOGGLE:
  288. #ifdef AUDIO_CLICKY
  289. userspace_config.clicky_enable = clicky_enable;
  290. eeprom_update_byte(EECONFIG_USERSPACE, userspace_config.raw);
  291. #endif
  292. break;
  293. #ifdef UNICODE_ENABLE
  294. case UC_FLIP: // (╯°□°)╯ ︵ ┻━┻
  295. if (record->event.pressed) {
  296. register_code(KC_RSFT);
  297. tap(KC_9);
  298. unregister_code(KC_RSFT);
  299. process_unicode((0x256F | QK_UNICODE), record); // Arm
  300. process_unicode((0x00B0 | QK_UNICODE), record); // Eye
  301. process_unicode((0x25A1 | QK_UNICODE), record); // Mouth
  302. process_unicode((0x00B0 | QK_UNICODE), record); // Eye
  303. register_code(KC_RSFT);
  304. tap(KC_0);
  305. unregister_code(KC_RSFT);
  306. process_unicode((0x256F | QK_UNICODE), record); // Arm
  307. tap(KC_SPC);
  308. process_unicode((0x0361 | QK_UNICODE), record); // Flippy
  309. tap(KC_SPC);
  310. process_unicode((0x253B | QK_UNICODE), record); // Table
  311. process_unicode((0x2501 | QK_UNICODE), record); // Table
  312. process_unicode((0x253B | QK_UNICODE), record); // Table
  313. }
  314. return false;
  315. break;
  316. #endif // UNICODE_ENABLE
  317. }
  318. return process_record_keymap(keycode, record) &&
  319. #ifdef RGBLIGHT_ENABLE
  320. process_record_user_rgb(keycode, record) &&
  321. #endif // RGBLIGHT_ENABLE
  322. process_record_secrets(keycode, record);
  323. }
  324. // Runs state check and changes underglow color and animation
  325. // on layer change, no matter where the change was initiated
  326. // Then runs keymap's layer change check
  327. uint32_t layer_state_set_user(uint32_t state) {
  328. state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
  329. #ifdef RGBLIGHT_ENABLE
  330. state = layer_state_set_rgb(state);
  331. #endif // RGBLIGHT_ENABLE
  332. return layer_state_set_keymap (state);
  333. }
  334. uint32_t default_layer_state_set_kb(uint32_t state) {
  335. return default_layer_state_set_keymap (state);
  336. }
  337. // Any custom LED code goes here.
  338. // So far, I only have keyboard specific code,
  339. // So nothing goes here.
  340. void led_set_user(uint8_t usb_led) {
  341. led_set_keymap(usb_led);
  342. }