quantum.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /* Copyright 2016-2017 Jack Humbert
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "quantum.h"
  17. #ifdef BLUETOOTH_ENABLE
  18. # include "outputselect.h"
  19. #endif
  20. #ifdef BACKLIGHT_ENABLE
  21. # include "backlight.h"
  22. #endif
  23. #ifdef MIDI_ENABLE
  24. # include "process_midi.h"
  25. #endif
  26. #ifdef VELOCIKEY_ENABLE
  27. # include "velocikey.h"
  28. #endif
  29. #ifdef HAPTIC_ENABLE
  30. # include "haptic.h"
  31. #endif
  32. #ifdef AUDIO_ENABLE
  33. # ifndef GOODBYE_SONG
  34. # define GOODBYE_SONG SONG(GOODBYE_SOUND)
  35. # endif
  36. float goodbye_song[][2] = GOODBYE_SONG;
  37. # ifdef DEFAULT_LAYER_SONGS
  38. float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
  39. # endif
  40. #endif
  41. uint8_t extract_mod_bits(uint16_t code) {
  42. switch (code) {
  43. case QK_MODS ... QK_MODS_MAX:
  44. break;
  45. default:
  46. return 0;
  47. }
  48. uint8_t mods_to_send = 0;
  49. if (code & QK_RMODS_MIN) { // Right mod flag is set
  50. if (code & QK_LCTL) mods_to_send |= MOD_BIT(KC_RIGHT_CTRL);
  51. if (code & QK_LSFT) mods_to_send |= MOD_BIT(KC_RIGHT_SHIFT);
  52. if (code & QK_LALT) mods_to_send |= MOD_BIT(KC_RIGHT_ALT);
  53. if (code & QK_LGUI) mods_to_send |= MOD_BIT(KC_RIGHT_GUI);
  54. } else {
  55. if (code & QK_LCTL) mods_to_send |= MOD_BIT(KC_LEFT_CTRL);
  56. if (code & QK_LSFT) mods_to_send |= MOD_BIT(KC_LEFT_SHIFT);
  57. if (code & QK_LALT) mods_to_send |= MOD_BIT(KC_LEFT_ALT);
  58. if (code & QK_LGUI) mods_to_send |= MOD_BIT(KC_LEFT_GUI);
  59. }
  60. return mods_to_send;
  61. }
  62. void do_code16(uint16_t code, void (*f)(uint8_t)) {
  63. f(extract_mod_bits(code));
  64. }
  65. __attribute__((weak)) void register_code16(uint16_t code) {
  66. if (IS_MODIFIER_KEYCODE(code) || code == KC_NO) {
  67. do_code16(code, register_mods);
  68. } else {
  69. do_code16(code, register_weak_mods);
  70. }
  71. register_code(code);
  72. }
  73. __attribute__((weak)) void unregister_code16(uint16_t code) {
  74. unregister_code(code);
  75. if (IS_MODIFIER_KEYCODE(code) || code == KC_NO) {
  76. do_code16(code, unregister_mods);
  77. } else {
  78. do_code16(code, unregister_weak_mods);
  79. }
  80. }
  81. /** \brief Tap a keycode with a delay.
  82. *
  83. * \param code The modded keycode to tap.
  84. * \param delay The amount of time in milliseconds to leave the keycode registered, before unregistering it.
  85. */
  86. __attribute__((weak)) void tap_code16_delay(uint16_t code, uint16_t delay) {
  87. register_code16(code);
  88. for (uint16_t i = delay; i > 0; i--) {
  89. wait_ms(1);
  90. }
  91. unregister_code16(code);
  92. }
  93. /** \brief Tap a keycode with the default delay.
  94. *
  95. * \param code The modded keycode to tap. If `code` is `KC_CAPS_LOCK`, the delay will be `TAP_HOLD_CAPS_DELAY`, otherwise `TAP_CODE_DELAY`, if defined.
  96. */
  97. __attribute__((weak)) void tap_code16(uint16_t code) {
  98. tap_code16_delay(code, code == KC_CAPS_LOCK ? TAP_HOLD_CAPS_DELAY : TAP_CODE_DELAY);
  99. }
  100. __attribute__((weak)) bool pre_process_record_kb(uint16_t keycode, keyrecord_t *record) {
  101. return pre_process_record_user(keycode, record);
  102. }
  103. __attribute__((weak)) bool pre_process_record_user(uint16_t keycode, keyrecord_t *record) {
  104. return true;
  105. }
  106. __attribute__((weak)) bool process_action_kb(keyrecord_t *record) {
  107. return true;
  108. }
  109. __attribute__((weak)) bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  110. return process_record_user(keycode, record);
  111. }
  112. __attribute__((weak)) bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  113. return true;
  114. }
  115. __attribute__((weak)) void post_process_record_kb(uint16_t keycode, keyrecord_t *record) {
  116. post_process_record_user(keycode, record);
  117. }
  118. __attribute__((weak)) void post_process_record_user(uint16_t keycode, keyrecord_t *record) {}
  119. void shutdown_quantum(void) {
  120. clear_keyboard();
  121. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  122. process_midi_all_notes_off();
  123. #endif
  124. #ifdef AUDIO_ENABLE
  125. # ifndef NO_MUSIC_MODE
  126. music_all_notes_off();
  127. # endif
  128. uint16_t timer_start = timer_read();
  129. PLAY_SONG(goodbye_song);
  130. shutdown_user();
  131. while (timer_elapsed(timer_start) < 250)
  132. wait_ms(1);
  133. stop_all_notes();
  134. #else
  135. shutdown_user();
  136. wait_ms(250);
  137. #endif
  138. #ifdef HAPTIC_ENABLE
  139. haptic_shutdown();
  140. #endif
  141. }
  142. void reset_keyboard(void) {
  143. shutdown_quantum();
  144. bootloader_jump();
  145. }
  146. void soft_reset_keyboard(void) {
  147. shutdown_quantum();
  148. mcu_reset();
  149. }
  150. /* Convert record into usable keycode via the contained event. */
  151. uint16_t get_record_keycode(keyrecord_t *record, bool update_layer_cache) {
  152. #ifdef COMBO_ENABLE
  153. if (record->keycode) {
  154. return record->keycode;
  155. }
  156. #endif
  157. return get_event_keycode(record->event, update_layer_cache);
  158. }
  159. /* Convert event into usable keycode. Checks the layer cache to ensure that it
  160. * retains the correct keycode after a layer change, if the key is still pressed.
  161. * "update_layer_cache" is to ensure that it only updates the layer cache when
  162. * appropriate, otherwise, it will update it and cause layer tap (and other keys)
  163. * from triggering properly.
  164. */
  165. uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache) {
  166. #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
  167. /* TODO: Use store_or_get_action() or a similar function. */
  168. if (!disable_action_cache) {
  169. uint8_t layer;
  170. if (event.pressed && update_layer_cache) {
  171. layer = layer_switch_get_layer(event.key);
  172. update_source_layers_cache(event.key, layer);
  173. } else {
  174. layer = read_source_layers_cache(event.key);
  175. }
  176. return keymap_key_to_keycode(layer, event.key);
  177. } else
  178. #endif
  179. return keymap_key_to_keycode(layer_switch_get_layer(event.key), event.key);
  180. }
  181. /* Get keycode, and then process pre tapping functionality */
  182. bool pre_process_record_quantum(keyrecord_t *record) {
  183. uint16_t keycode = get_record_keycode(record, true);
  184. #ifdef COMBO_ENABLE
  185. if (!(process_combo(keycode, record))) {
  186. return false;
  187. }
  188. #endif
  189. return pre_process_record_kb(keycode, record);
  190. }
  191. /* Get keycode, and then call keyboard function */
  192. void post_process_record_quantum(keyrecord_t *record) {
  193. uint16_t keycode = get_record_keycode(record, false);
  194. post_process_record_kb(keycode, record);
  195. }
  196. /* Core keycode function, hands off handling to other functions,
  197. then processes internal quantum keycodes, and then processes
  198. ACTIONs. */
  199. bool process_record_quantum(keyrecord_t *record) {
  200. uint16_t keycode = get_record_keycode(record, true);
  201. // This is how you use actions here
  202. // if (keycode == QK_LEADER) {
  203. // action_t action;
  204. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  205. // process_action(record, action);
  206. // return false;
  207. // }
  208. #if defined(SECURE_ENABLE)
  209. if (!preprocess_secure(keycode, record)) {
  210. return false;
  211. }
  212. #endif
  213. #ifdef TAP_DANCE_ENABLE
  214. if (preprocess_tap_dance(keycode, record)) {
  215. // The tap dance might have updated the layer state, therefore the
  216. // result of the keycode lookup might change.
  217. keycode = get_record_keycode(record, true);
  218. }
  219. #endif
  220. #ifdef VELOCIKEY_ENABLE
  221. if (velocikey_enabled() && record->event.pressed) {
  222. velocikey_accelerate();
  223. }
  224. #endif
  225. #ifdef WPM_ENABLE
  226. if (record->event.pressed) {
  227. update_wpm(keycode);
  228. }
  229. #endif
  230. if (!(
  231. #if defined(KEY_LOCK_ENABLE)
  232. // Must run first to be able to mask key_up events.
  233. process_key_lock(&keycode, record) &&
  234. #endif
  235. #if defined(DYNAMIC_MACRO_ENABLE) && !defined(DYNAMIC_MACRO_USER_CALL)
  236. // Must run asap to ensure all keypresses are recorded.
  237. process_dynamic_macro(keycode, record) &&
  238. #endif
  239. #if defined(AUDIO_ENABLE) && defined(AUDIO_CLICKY)
  240. process_clicky(keycode, record) &&
  241. #endif
  242. #ifdef HAPTIC_ENABLE
  243. process_haptic(keycode, record) &&
  244. #endif
  245. #if defined(VIA_ENABLE)
  246. process_record_via(keycode, record) &&
  247. #endif
  248. #if defined(POINTING_DEVICE_ENABLE) && defined(POINTING_DEVICE_AUTO_MOUSE_ENABLE)
  249. process_auto_mouse(keycode, record) &&
  250. #endif
  251. process_record_kb(keycode, record) &&
  252. #if defined(SECURE_ENABLE)
  253. process_secure(keycode, record) &&
  254. #endif
  255. #if defined(SEQUENCER_ENABLE)
  256. process_sequencer(keycode, record) &&
  257. #endif
  258. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  259. process_midi(keycode, record) &&
  260. #endif
  261. #ifdef AUDIO_ENABLE
  262. process_audio(keycode, record) &&
  263. #endif
  264. #if defined(BACKLIGHT_ENABLE) || defined(LED_MATRIX_ENABLE)
  265. process_backlight(keycode, record) &&
  266. #endif
  267. #ifdef STENO_ENABLE
  268. process_steno(keycode, record) &&
  269. #endif
  270. #if (defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))) && !defined(NO_MUSIC_MODE)
  271. process_music(keycode, record) &&
  272. #endif
  273. #ifdef KEY_OVERRIDE_ENABLE
  274. process_key_override(keycode, record) &&
  275. #endif
  276. #ifdef TAP_DANCE_ENABLE
  277. process_tap_dance(keycode, record) &&
  278. #endif
  279. #ifdef CAPS_WORD_ENABLE
  280. process_caps_word(keycode, record) &&
  281. #endif
  282. #if defined(UNICODE_COMMON_ENABLE)
  283. process_unicode_common(keycode, record) &&
  284. #endif
  285. #ifdef LEADER_ENABLE
  286. process_leader(keycode, record) &&
  287. #endif
  288. #ifdef AUTO_SHIFT_ENABLE
  289. process_auto_shift(keycode, record) &&
  290. #endif
  291. #ifdef DYNAMIC_TAPPING_TERM_ENABLE
  292. process_dynamic_tapping_term(keycode, record) &&
  293. #endif
  294. #ifdef SPACE_CADET_ENABLE
  295. process_space_cadet(keycode, record) &&
  296. #endif
  297. #ifdef MAGIC_KEYCODE_ENABLE
  298. process_magic(keycode, record) &&
  299. #endif
  300. #ifdef GRAVE_ESC_ENABLE
  301. process_grave_esc(keycode, record) &&
  302. #endif
  303. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  304. process_rgb(keycode, record) &&
  305. #endif
  306. #ifdef JOYSTICK_ENABLE
  307. process_joystick(keycode, record) &&
  308. #endif
  309. #ifdef PROGRAMMABLE_BUTTON_ENABLE
  310. process_programmable_button(keycode, record) &&
  311. #endif
  312. #ifdef AUTOCORRECT_ENABLE
  313. process_autocorrect(keycode, record) &&
  314. #endif
  315. #ifdef TRI_LAYER_ENABLE
  316. process_tri_layer(keycode, record) &&
  317. #endif
  318. true)) {
  319. return false;
  320. }
  321. if (record->event.pressed) {
  322. switch (keycode) {
  323. #ifndef NO_RESET
  324. case QK_BOOTLOADER:
  325. reset_keyboard();
  326. return false;
  327. case QK_REBOOT:
  328. soft_reset_keyboard();
  329. return false;
  330. #endif
  331. #ifndef NO_DEBUG
  332. case QK_DEBUG_TOGGLE:
  333. debug_enable ^= 1;
  334. if (debug_enable) {
  335. print("DEBUG: enabled.\n");
  336. } else {
  337. print("DEBUG: disabled.\n");
  338. }
  339. #endif
  340. return false;
  341. case QK_CLEAR_EEPROM:
  342. #ifdef NO_RESET
  343. eeconfig_init();
  344. #else
  345. eeconfig_disable();
  346. soft_reset_keyboard();
  347. #endif
  348. return false;
  349. #ifdef VELOCIKEY_ENABLE
  350. case QK_VELOCIKEY_TOGGLE:
  351. velocikey_toggle();
  352. return false;
  353. #endif
  354. #ifdef BLUETOOTH_ENABLE
  355. case QK_OUTPUT_AUTO:
  356. set_output(OUTPUT_AUTO);
  357. return false;
  358. case QK_OUTPUT_USB:
  359. set_output(OUTPUT_USB);
  360. return false;
  361. case QK_OUTPUT_BLUETOOTH:
  362. set_output(OUTPUT_BLUETOOTH);
  363. return false;
  364. #endif
  365. #ifndef NO_ACTION_ONESHOT
  366. case QK_ONE_SHOT_TOGGLE:
  367. oneshot_toggle();
  368. break;
  369. case QK_ONE_SHOT_ON:
  370. oneshot_enable();
  371. break;
  372. case QK_ONE_SHOT_OFF:
  373. oneshot_disable();
  374. break;
  375. #endif
  376. #ifdef ENABLE_COMPILE_KEYCODE
  377. case QK_MAKE: // Compiles the firmware, and adds the flash command based on keyboard bootloader
  378. {
  379. # ifdef NO_ACTION_ONESHOT
  380. const uint8_t temp_mod = mod_config(get_mods());
  381. # else
  382. const uint8_t temp_mod = mod_config(get_mods() | get_oneshot_mods());
  383. clear_oneshot_mods();
  384. # endif
  385. clear_mods();
  386. SEND_STRING_DELAY("qmk", TAP_CODE_DELAY);
  387. if (temp_mod & MOD_MASK_SHIFT) { // if shift is held, flash rather than compile
  388. SEND_STRING_DELAY(" flash ", TAP_CODE_DELAY);
  389. } else {
  390. SEND_STRING_DELAY(" compile ", TAP_CODE_DELAY);
  391. }
  392. # if defined(CONVERTER_ENABLED)
  393. SEND_STRING_DELAY("-kb " QMK_KEYBOARD " -km " QMK_KEYMAP " -e CONVERT_TO=" CONVERTER_TARGET SS_TAP(X_ENTER), TAP_CODE_DELAY);
  394. # else
  395. SEND_STRING_DELAY("-kb " QMK_KEYBOARD " -km " QMK_KEYMAP SS_TAP(X_ENTER), TAP_CODE_DELAY);
  396. # endif
  397. if (temp_mod & MOD_MASK_SHIFT && temp_mod & MOD_MASK_CTRL) {
  398. reset_keyboard();
  399. }
  400. }
  401. #endif
  402. }
  403. }
  404. return process_action_kb(record);
  405. }
  406. void set_single_persistent_default_layer(uint8_t default_layer) {
  407. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  408. PLAY_SONG(default_layer_songs[default_layer]);
  409. #endif
  410. eeconfig_update_default_layer((layer_state_t)1 << default_layer);
  411. default_layer_set((layer_state_t)1 << default_layer);
  412. }
  413. //------------------------------------------------------------------------------
  414. // Override these functions in your keymap file to play different tunes on
  415. // different events such as startup and bootloader jump
  416. __attribute__((weak)) void startup_user(void) {}
  417. __attribute__((weak)) void shutdown_user(void) {}
  418. void suspend_power_down_quantum(void) {
  419. suspend_power_down_kb();
  420. #ifndef NO_SUSPEND_POWER_DOWN
  421. // Turn off backlight
  422. # ifdef BACKLIGHT_ENABLE
  423. backlight_set(0);
  424. # endif
  425. # ifdef LED_MATRIX_ENABLE
  426. led_matrix_task();
  427. # endif
  428. # ifdef RGB_MATRIX_ENABLE
  429. rgb_matrix_task();
  430. # endif
  431. // Turn off LED indicators
  432. led_suspend();
  433. // Turn off audio
  434. # ifdef AUDIO_ENABLE
  435. stop_all_notes();
  436. # endif
  437. // Turn off underglow
  438. # if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
  439. rgblight_suspend();
  440. # endif
  441. # if defined(LED_MATRIX_ENABLE)
  442. led_matrix_set_suspend_state(true);
  443. # endif
  444. # if defined(RGB_MATRIX_ENABLE)
  445. rgb_matrix_set_suspend_state(true);
  446. # endif
  447. # ifdef OLED_ENABLE
  448. oled_off();
  449. # endif
  450. # ifdef ST7565_ENABLE
  451. st7565_off();
  452. # endif
  453. # if defined(POINTING_DEVICE_ENABLE)
  454. // run to ensure scanning occurs while suspended
  455. pointing_device_task();
  456. # endif
  457. #endif
  458. }
  459. __attribute__((weak)) void suspend_wakeup_init_quantum(void) {
  460. // Turn on backlight
  461. #ifdef BACKLIGHT_ENABLE
  462. backlight_init();
  463. #endif
  464. // Restore LED indicators
  465. led_wakeup();
  466. // Wake up underglow
  467. #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
  468. rgblight_wakeup();
  469. #endif
  470. #if defined(LED_MATRIX_ENABLE)
  471. led_matrix_set_suspend_state(false);
  472. #endif
  473. #if defined(RGB_MATRIX_ENABLE)
  474. rgb_matrix_set_suspend_state(false);
  475. #endif
  476. suspend_wakeup_init_kb();
  477. }
  478. /** \brief converts unsigned integers into char arrays
  479. *
  480. * Takes an unsigned integer and converts that value into an equivalent char array
  481. * A padding character may be specified, ' ' for leading spaces, '0' for leading zeros.
  482. */
  483. const char *get_numeric_str(char *buf, size_t buf_len, uint32_t curr_num, char curr_pad) {
  484. buf[buf_len - 1] = '\0';
  485. for (size_t i = 0; i < buf_len - 1; ++i) {
  486. char c = '0' + curr_num % 10;
  487. buf[buf_len - 2 - i] = (c == '0' && i == 0) ? '0' : (curr_num > 0 ? c : curr_pad);
  488. curr_num /= 10;
  489. }
  490. return buf;
  491. }
  492. /** \brief converts uint8_t into char array
  493. *
  494. * Takes an uint8_t, and uses an internal static buffer to render that value into a char array
  495. * A padding character may be specified, ' ' for leading spaces, '0' for leading zeros.
  496. *
  497. * NOTE: Subsequent invocations will reuse the same static buffer and overwrite the previous
  498. * contents. Use the result immediately, instead of caching it.
  499. */
  500. const char *get_u8_str(uint8_t curr_num, char curr_pad) {
  501. static char buf[4] = {0};
  502. static uint8_t last_num = 0xFF;
  503. static char last_pad = '\0';
  504. if (last_num == curr_num && last_pad == curr_pad) {
  505. return buf;
  506. }
  507. last_num = curr_num;
  508. last_pad = curr_pad;
  509. return get_numeric_str(buf, sizeof(buf), curr_num, curr_pad);
  510. }
  511. /** \brief converts uint16_t into char array
  512. *
  513. * Takes an uint16_t, and uses an internal static buffer to render that value into a char array
  514. * A padding character may be specified, ' ' for leading spaces, '0' for leading zeros.
  515. *
  516. * NOTE: Subsequent invocations will reuse the same static buffer and overwrite the previous
  517. * contents. Use the result immediately, instead of caching it.
  518. */
  519. const char *get_u16_str(uint16_t curr_num, char curr_pad) {
  520. static char buf[6] = {0};
  521. static uint16_t last_num = 0xFF;
  522. static char last_pad = '\0';
  523. if (last_num == curr_num && last_pad == curr_pad) {
  524. return buf;
  525. }
  526. last_num = curr_num;
  527. last_pad = curr_pad;
  528. return get_numeric_str(buf, sizeof(buf), curr_num, curr_pad);
  529. }
  530. #if defined(SECURE_ENABLE)
  531. void secure_hook_quantum(secure_status_t secure_status) {
  532. // If keys are being held when this is triggered, they may not be released properly
  533. // this can result in stuck keys, mods and layers. To prevent that, manually
  534. // clear these, when it is triggered.
  535. if (secure_status == SECURE_PENDING) {
  536. clear_keyboard();
  537. layer_clear();
  538. }
  539. }
  540. #endif