drashna.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
  19. #define DRASHNA_UNICODE_MODE UC_WIN
  20. #else
  21. // set to 2 for UC_WIN, set to 4 for UC_WINC
  22. #define DRASHNA_UNICODE_MODE 2
  23. #endif
  24. uint16_t copy_paste_timer;
  25. // Helper Functions
  26. // This block is for all of the gaming macros, as they were all doing
  27. // the same thing, but with differring text sent.
  28. bool send_game_macro(const char *str, keyrecord_t *record, bool override) {
  29. if (!record->event.pressed || override) {
  30. uint16_t keycode;
  31. if (userspace_config.is_overwatch) {
  32. keycode = KC_BSPC;
  33. } else {
  34. keycode = KC_ENTER;
  35. }
  36. clear_keyboard();
  37. tap_code(keycode);
  38. wait_ms(50);
  39. send_string_with_delay(str, MACRO_TIMER);
  40. wait_ms(50);
  41. tap_code(KC_ENTER);
  42. }
  43. if (override) wait_ms(3000);
  44. return false;
  45. }
  46. bool mod_key_press_timer (uint16_t code, uint16_t mod_code, bool pressed) {
  47. static uint16_t this_timer;
  48. if(pressed) {
  49. this_timer= timer_read();
  50. } else {
  51. if (timer_elapsed(this_timer) < TAPPING_TERM){
  52. register_code(code);
  53. unregister_code(code);
  54. } else {
  55. register_code(mod_code);
  56. register_code(code);
  57. unregister_code(code);
  58. unregister_code(mod_code);
  59. }
  60. }
  61. return false;
  62. }
  63. bool mod_key_press (uint16_t code, uint16_t mod_code, bool pressed, uint16_t this_timer) {
  64. if(pressed) {
  65. this_timer= timer_read();
  66. } else {
  67. if (timer_elapsed(this_timer) < TAPPING_TERM){
  68. register_code(code);
  69. unregister_code(code);
  70. } else {
  71. register_code(mod_code);
  72. register_code(code);
  73. unregister_code(code);
  74. unregister_code(mod_code);
  75. }
  76. }
  77. return false;
  78. }
  79. void bootmagic_lite(void) {
  80. matrix_scan();
  81. #if defined(DEBOUNCING_DELAY) && DEBOUNCING_DELAY > 0
  82. wait_ms(DEBOUNCING_DELAY * 2);
  83. #elif defined(DEBOUNCE) && DEBOUNCE > 0
  84. wait_ms(DEBOUNCE * 2);
  85. #else
  86. wait_ms(30);
  87. #endif
  88. matrix_scan();
  89. if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
  90. bootloader_jump();
  91. }
  92. }
  93. // Add reconfigurable functions here, for keymap customization
  94. // This allows for a global, userspace functions, and continued
  95. // customization of the keymap. Use _keymap instead of _user
  96. // functions in the keymaps
  97. __attribute__ ((weak))
  98. void matrix_init_keymap(void) {}
  99. __attribute__ ((weak))
  100. void startup_keymap(void) {}
  101. __attribute__ ((weak))
  102. void shutdown_keymap(void) {}
  103. __attribute__ ((weak))
  104. void suspend_power_down_keymap(void) {}
  105. __attribute__ ((weak))
  106. void suspend_wakeup_init_keymap(void) {}
  107. __attribute__ ((weak))
  108. void matrix_scan_keymap(void) {}
  109. __attribute__ ((weak))
  110. bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
  111. return true;
  112. }
  113. __attribute__ ((weak))
  114. bool process_record_secrets(uint16_t keycode, keyrecord_t *record) {
  115. return true;
  116. }
  117. __attribute__ ((weak))
  118. uint32_t layer_state_set_keymap (uint32_t state) {
  119. return state;
  120. }
  121. __attribute__ ((weak))
  122. uint32_t default_layer_state_set_keymap (uint32_t state) {
  123. return state;
  124. }
  125. __attribute__ ((weak))
  126. void led_set_keymap(uint8_t usb_led) {}
  127. __attribute__ ((weak))
  128. void eeconfig_init_keymap(void) {}
  129. // Call user matrix init, set default RGB colors and then
  130. // call the keymap's init function
  131. void matrix_init_user(void) {
  132. #if !defined(BOOTMAGIC_LITE) && !defined(BOOTMAGIC_ENABLE)
  133. bootmagic_lite();
  134. #endif
  135. userspace_config.raw = eeconfig_read_user();
  136. #ifdef BOOTLOADER_CATERINA
  137. DDRD &= ~(1<<5);
  138. PORTD &= ~(1<<5);
  139. DDRB &= ~(1<<0);
  140. PORTB &= ~(1<<0);
  141. #endif
  142. #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
  143. set_unicode_input_mode(DRASHNA_UNICODE_MODE);
  144. get_unicode_input_mode();
  145. #endif //UNICODE_ENABLE
  146. matrix_init_keymap();
  147. #ifdef RGBLIGHT_ENABLE
  148. matrix_init_rgb();
  149. #endif //RGBLIGHT_ENABLE
  150. }
  151. void startup_user (void) {
  152. // #ifdef RGBLIGHT_ENABLE
  153. // matrix_init_rgb();
  154. // #endif //RGBLIGHT_ENABLE
  155. startup_keymap();
  156. }
  157. void shutdown_user (void) {
  158. #ifdef RGBLIGHT_ENABLE
  159. rgblight_enable_noeeprom();
  160. rgblight_mode_noeeprom(1);
  161. rgblight_setrgb_red();
  162. #endif // RGBLIGHT_ENABLE
  163. #ifdef RGB_MATRIX_ENABLE
  164. rgb_led led;
  165. for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
  166. led = g_rgb_leds[i];
  167. if (led.matrix_co.raw < 0xFF) {
  168. rgb_matrix_set_color( i, 0xFF, 0x00, 0x00 );
  169. }
  170. }
  171. #endif //RGB_MATRIX_ENABLE
  172. shutdown_keymap();
  173. }
  174. void suspend_power_down_user(void) {
  175. suspend_power_down_keymap();
  176. }
  177. void suspend_wakeup_init_user(void) {
  178. suspend_wakeup_init_keymap();
  179. }
  180. // No global matrix scan code, so just run keymap's matrix
  181. // scan function
  182. void matrix_scan_user(void) {
  183. static bool has_ran_yet;
  184. if (!has_ran_yet) {
  185. has_ran_yet = true;
  186. startup_user();
  187. }
  188. #ifdef TAP_DANCE_ENABLE // Run Diablo 3 macro checking code.
  189. run_diablo_macro_check();
  190. #endif // TAP_DANCE_ENABLE
  191. #ifdef RGBLIGHT_ENABLE
  192. matrix_scan_rgb();
  193. #endif // RGBLIGHT_ENABLE
  194. matrix_scan_keymap();
  195. }
  196. // Defines actions tor my global custom keycodes. Defined in drashna.h file
  197. // Then runs the _keymap's record handier if not processed here
  198. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  199. // If console is enabled, it will print the matrix position and status of each key pressed
  200. #ifdef KEYLOGGER_ENABLE
  201. #if defined(KEYBOARD_ergodox_ez) || defined(KEYBOARD_iris_rev2)
  202. xprintf("KL: col: %u, row: %u, pressed: %u\n", record->event.key.row, record->event.key.col, record->event.pressed);
  203. #else
  204. xprintf("KL: col: %u, row: %u, pressed: %u\n", record->event.key.col, record->event.key.row, record->event.pressed);
  205. #endif
  206. #endif //KEYLOGGER_ENABLE
  207. switch (keycode) {
  208. case KC_QWERTY:
  209. if (record->event.pressed) {
  210. set_single_persistent_default_layer(_QWERTY);
  211. }
  212. break;
  213. case KC_COLEMAK:
  214. if (record->event.pressed) {
  215. set_single_persistent_default_layer(_COLEMAK);
  216. }
  217. break;
  218. case KC_DVORAK:
  219. if (record->event.pressed) {
  220. set_single_persistent_default_layer(_DVORAK);
  221. }
  222. break;
  223. case KC_WORKMAN:
  224. if (record->event.pressed) {
  225. set_single_persistent_default_layer(_WORKMAN);
  226. }
  227. break;
  228. case KC_MAKE: // Compiles the firmware, and adds the flash command based on keyboard bootloader
  229. if (!record->event.pressed) {
  230. uint8_t temp_mod = get_mods();
  231. uint8_t temp_osm = get_oneshot_mods();
  232. clear_mods(); clear_oneshot_mods();
  233. send_string_with_delay_P(PSTR("make " QMK_KEYBOARD ":" QMK_KEYMAP), 10);
  234. if (temp_mod & MODS_SHIFT_MASK || temp_osm & MODS_SHIFT_MASK) {
  235. #if defined(__ARM__)
  236. send_string_with_delay_P(PSTR(":dfu-util"), 10);
  237. #elif defined(BOOTLOADER_DFU)
  238. send_string_with_delay_P(PSTR(":dfu"), 10);
  239. #elif defined(BOOTLOADER_HALFKAY)
  240. send_string_with_delay_P(PSTR(":teensy"), 10);
  241. #elif defined(BOOTLOADER_CATERINA)
  242. send_string_with_delay_P(PSTR(":avrdude"), 10);
  243. #endif // bootloader options
  244. }
  245. #if defined(KEYBOARD_viterbi)
  246. send_string_with_delay_P(PSTR(":dfu"), 10);
  247. #endif
  248. if (temp_mod & MODS_CTRL_MASK || temp_osm & MODS_CTRL_MASK) { send_string_with_delay_P(PSTR(" -j8 --output-sync"), 10); }
  249. send_string_with_delay_P(PSTR(SS_TAP(X_ENTER)), 10);
  250. set_mods(temp_mod);
  251. }
  252. break;
  253. case VRSN: // Prints firmware version
  254. if (record->event.pressed) {
  255. send_string_with_delay_P(PSTR(QMK_KEYBOARD "/" QMK_KEYMAP " @ " QMK_VERSION ", Built on: " QMK_BUILDDATE), MACRO_TIMER);
  256. }
  257. break;
  258. // These are a serious of gaming macros.
  259. // Only enables for the viterbi, basically,
  260. // to save on firmware space, since it's limited.
  261. #ifdef MACROS_ENABLED
  262. case KC_OVERWATCH: // Toggle's if we hit "ENTER" or "BACKSPACE" to input macros
  263. if (record->event.pressed) { userspace_config.is_overwatch ^= 1; eeconfig_update_user(userspace_config.raw); }
  264. #ifdef RGBLIGHT_ENABLE
  265. userspace_config.is_overwatch ? rgblight_mode_noeeprom(17) : rgblight_mode_noeeprom(18);
  266. #endif //RGBLIGHT_ENABLE
  267. break;
  268. case KC_SALT:
  269. return send_game_macro("Salt, salt, salt...", record, false);
  270. case KC_MORESALT:
  271. return send_game_macro("Please sir, can I have some more salt?!", record, false);
  272. case KC_SALTHARD:
  273. return send_game_macro("Your salt only makes me harder, and even more aggressive!", record, false);
  274. case KC_GOODGAME:
  275. return send_game_macro("Good game, everyone!", record, false);
  276. case KC_GLHF:
  277. return send_game_macro("Good luck, have fun!!!", record, false);
  278. case KC_SYMM:
  279. return send_game_macro("Left click to win!", record, false);
  280. case KC_JUSTGAME:
  281. 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);
  282. case KC_TORB:
  283. return send_game_macro("That was positively riveting!", record, false);
  284. case KC_AIM:
  285. send_game_macro("That aim is absolutely amazing. It's almost like you're a machine!", record, true);
  286. return send_game_macro("Wait! That aim is TOO good! You're clearly using an aim hack! CHEATER!", record, false);
  287. case KC_C9:
  288. return send_game_macro("OMG!!! C9!!!", record, false);
  289. case KC_GGEZ:
  290. return send_game_macro("That was a fantastic game, though it was a bit easy. Try harder next time!", record, false);
  291. #endif // MACROS_ENABLED
  292. case KC_DIABLO_CLEAR: // reset all Diablo timers, disabling them
  293. #ifdef TAP_DANCE_ENABLE
  294. if (record->event.pressed) {
  295. uint8_t dtime;
  296. for (dtime = 0; dtime < 4; dtime++) {
  297. diablo_key_time[dtime] = diablo_times[0];
  298. }
  299. }
  300. #endif // TAP_DANCE_ENABLE
  301. break;
  302. case KC_CCCV: // One key copy/paste
  303. if(record->event.pressed){
  304. copy_paste_timer = timer_read();
  305. } else {
  306. if (timer_elapsed(copy_paste_timer) > TAPPING_TERM) { // Hold, copy
  307. register_code(KC_LCTL);
  308. tap_code(KC_C);
  309. unregister_code(KC_LCTL);
  310. } else { // Tap, paste
  311. register_code(KC_LCTL);
  312. tap_code(KC_V);
  313. unregister_code(KC_LCTL);
  314. }
  315. }
  316. break;
  317. #ifdef UNICODE_ENABLE
  318. case UC_FLIP: // (ノಠ痊ಠ)ノ彡┻━┻
  319. if (record->event.pressed) {
  320. send_unicode_hex_string("0028 30CE 0CA0 75CA 0CA0 0029 30CE 5F61 253B 2501 253B");
  321. }
  322. break;
  323. case UC_TABL: // ┬─┬ノ( º _ ºノ)
  324. if (record->event.pressed) {
  325. send_unicode_hex_string("252C 2500 252C 30CE 0028 0020 00BA 0020 005F 0020 00BA 30CE 0029");
  326. }
  327. break;
  328. case UC_SHRG: // ¯\_(ツ)_/¯
  329. if (record->event.pressed) {
  330. send_unicode_hex_string("00AF 005C 005F 0028 30C4 0029 005F 002F 00AF");
  331. }
  332. break;
  333. case UC_DISA: // ಠ_ಠ
  334. if (record->event.pressed) {
  335. send_unicode_hex_string("0CA0 005F 0CA0");
  336. }
  337. break;
  338. #endif
  339. }
  340. return process_record_keymap(keycode, record) &&
  341. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  342. process_record_user_rgb(keycode, record) &&
  343. #endif // RGBLIGHT_ENABLE
  344. process_record_secrets(keycode, record);
  345. }
  346. // Runs state check and changes underglow color and animation
  347. // on layer change, no matter where the change was initiated
  348. // Then runs keymap's layer change check
  349. uint32_t layer_state_set_user(uint32_t state) {
  350. state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
  351. #ifdef RGBLIGHT_ENABLE
  352. state = layer_state_set_rgb(state);
  353. #endif // RGBLIGHT_ENABLE
  354. return layer_state_set_keymap (state);
  355. }
  356. uint32_t default_layer_state_set_user(uint32_t state) {
  357. state = default_layer_state_set_keymap(state);
  358. #ifdef RGBLIGHT_ENABLE
  359. state = default_layer_state_set_rgb(state);
  360. #endif // RGBLIGHT_ENABLE
  361. return state;
  362. }
  363. // Any custom LED code goes here.
  364. // So far, I only have keyboard specific code,
  365. // So nothing goes here.
  366. void led_set_user(uint8_t usb_led) {
  367. led_set_keymap(usb_led);
  368. }
  369. void eeconfig_init_user(void) {
  370. userspace_config.raw = 0;
  371. userspace_config.rgb_layer_change = true;
  372. eeconfig_update_user(userspace_config.raw);
  373. #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
  374. set_unicode_input_mode(DRASHNA_UNICODE_MODE);
  375. get_unicode_input_mode();
  376. #else
  377. eeprom_update_byte(EECONFIG_UNICODEMODE, DRASHNA_UNICODE_MODE);
  378. #endif
  379. }