quantum.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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_MOD(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_MOD(code) || code == KC_NO) {
  76. do_code16(code, unregister_mods);
  77. } else {
  78. do_code16(code, unregister_weak_mods);
  79. }
  80. }
  81. __attribute__((weak)) void tap_code16(uint16_t code) {
  82. register_code16(code);
  83. if (code == KC_CAPS_LOCK) {
  84. wait_ms(TAP_HOLD_CAPS_DELAY);
  85. } else if (TAP_CODE_DELAY > 0) {
  86. wait_ms(TAP_CODE_DELAY);
  87. }
  88. unregister_code16(code);
  89. }
  90. __attribute__((weak)) bool process_action_kb(keyrecord_t *record) {
  91. return true;
  92. }
  93. __attribute__((weak)) bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  94. return process_record_user(keycode, record);
  95. }
  96. __attribute__((weak)) bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  97. return true;
  98. }
  99. __attribute__((weak)) void post_process_record_kb(uint16_t keycode, keyrecord_t *record) {
  100. post_process_record_user(keycode, record);
  101. }
  102. __attribute__((weak)) void post_process_record_user(uint16_t keycode, keyrecord_t *record) {}
  103. void shutdown_quantum(void) {
  104. clear_keyboard();
  105. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  106. process_midi_all_notes_off();
  107. #endif
  108. #ifdef AUDIO_ENABLE
  109. # ifndef NO_MUSIC_MODE
  110. music_all_notes_off();
  111. # endif
  112. uint16_t timer_start = timer_read();
  113. PLAY_SONG(goodbye_song);
  114. shutdown_user();
  115. while (timer_elapsed(timer_start) < 250)
  116. wait_ms(1);
  117. stop_all_notes();
  118. #else
  119. shutdown_user();
  120. wait_ms(250);
  121. #endif
  122. #ifdef HAPTIC_ENABLE
  123. haptic_shutdown();
  124. #endif
  125. }
  126. void reset_keyboard(void) {
  127. shutdown_quantum();
  128. bootloader_jump();
  129. }
  130. void soft_reset_keyboard(void) {
  131. shutdown_quantum();
  132. mcu_reset();
  133. }
  134. /* Convert record into usable keycode via the contained event. */
  135. uint16_t get_record_keycode(keyrecord_t *record, bool update_layer_cache) {
  136. #ifdef COMBO_ENABLE
  137. if (record->keycode) {
  138. return record->keycode;
  139. }
  140. #endif
  141. return get_event_keycode(record->event, update_layer_cache);
  142. }
  143. /* Convert event into usable keycode. Checks the layer cache to ensure that it
  144. * retains the correct keycode after a layer change, if the key is still pressed.
  145. * "update_layer_cache" is to ensure that it only updates the layer cache when
  146. * appropriate, otherwise, it will update it and cause layer tap (and other keys)
  147. * from triggering properly.
  148. */
  149. uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache) {
  150. #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
  151. /* TODO: Use store_or_get_action() or a similar function. */
  152. if (!disable_action_cache) {
  153. uint8_t layer;
  154. if (event.pressed && update_layer_cache) {
  155. layer = layer_switch_get_layer(event.key);
  156. update_source_layers_cache(event.key, layer);
  157. } else {
  158. layer = read_source_layers_cache(event.key);
  159. }
  160. return keymap_key_to_keycode(layer, event.key);
  161. } else
  162. #endif
  163. return keymap_key_to_keycode(layer_switch_get_layer(event.key), event.key);
  164. }
  165. /* Get keycode, and then process pre tapping functionality */
  166. bool pre_process_record_quantum(keyrecord_t *record) {
  167. if (!(
  168. #ifdef COMBO_ENABLE
  169. process_combo(get_record_keycode(record, true), record) &&
  170. #endif
  171. true)) {
  172. return false;
  173. }
  174. return true; // continue processing
  175. }
  176. /* Get keycode, and then call keyboard function */
  177. void post_process_record_quantum(keyrecord_t *record) {
  178. uint16_t keycode = get_record_keycode(record, false);
  179. post_process_record_kb(keycode, record);
  180. }
  181. /* Core keycode function, hands off handling to other functions,
  182. then processes internal quantum keycodes, and then processes
  183. ACTIONs. */
  184. bool process_record_quantum(keyrecord_t *record) {
  185. uint16_t keycode = get_record_keycode(record, true);
  186. // This is how you use actions here
  187. // if (keycode == KC_LEAD) {
  188. // action_t action;
  189. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  190. // process_action(record, action);
  191. // return false;
  192. // }
  193. #if defined(SECURE_ENABLE)
  194. if (!preprocess_secure(keycode, record)) {
  195. return false;
  196. }
  197. #endif
  198. #ifdef VELOCIKEY_ENABLE
  199. if (velocikey_enabled() && record->event.pressed) {
  200. velocikey_accelerate();
  201. }
  202. #endif
  203. #ifdef WPM_ENABLE
  204. if (record->event.pressed) {
  205. update_wpm(keycode);
  206. }
  207. #endif
  208. #ifdef TAP_DANCE_ENABLE
  209. preprocess_tap_dance(keycode, record);
  210. #endif
  211. if (!(
  212. #if defined(KEY_LOCK_ENABLE)
  213. // Must run first to be able to mask key_up events.
  214. process_key_lock(&keycode, record) &&
  215. #endif
  216. #if defined(DYNAMIC_MACRO_ENABLE) && !defined(DYNAMIC_MACRO_USER_CALL)
  217. // Must run asap to ensure all keypresses are recorded.
  218. process_dynamic_macro(keycode, record) &&
  219. #endif
  220. #if defined(AUDIO_ENABLE) && defined(AUDIO_CLICKY)
  221. process_clicky(keycode, record) &&
  222. #endif
  223. #ifdef HAPTIC_ENABLE
  224. process_haptic(keycode, record) &&
  225. #endif
  226. #if defined(VIA_ENABLE)
  227. process_record_via(keycode, record) &&
  228. #endif
  229. process_record_kb(keycode, record) &&
  230. #if defined(SECURE_ENABLE)
  231. process_secure(keycode, record) &&
  232. #endif
  233. #if defined(SEQUENCER_ENABLE)
  234. process_sequencer(keycode, record) &&
  235. #endif
  236. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  237. process_midi(keycode, record) &&
  238. #endif
  239. #ifdef AUDIO_ENABLE
  240. process_audio(keycode, record) &&
  241. #endif
  242. #if defined(BACKLIGHT_ENABLE) || defined(LED_MATRIX_ENABLE)
  243. process_backlight(keycode, record) &&
  244. #endif
  245. #ifdef STENO_ENABLE
  246. process_steno(keycode, record) &&
  247. #endif
  248. #if (defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))) && !defined(NO_MUSIC_MODE)
  249. process_music(keycode, record) &&
  250. #endif
  251. #ifdef KEY_OVERRIDE_ENABLE
  252. process_key_override(keycode, record) &&
  253. #endif
  254. #ifdef TAP_DANCE_ENABLE
  255. process_tap_dance(keycode, record) &&
  256. #endif
  257. #if defined(UNICODE_COMMON_ENABLE)
  258. process_unicode_common(keycode, record) &&
  259. #endif
  260. #ifdef LEADER_ENABLE
  261. process_leader(keycode, record) &&
  262. #endif
  263. #ifdef PRINTING_ENABLE
  264. process_printer(keycode, record) &&
  265. #endif
  266. #ifdef AUTO_SHIFT_ENABLE
  267. process_auto_shift(keycode, record) &&
  268. #endif
  269. #ifdef DYNAMIC_TAPPING_TERM_ENABLE
  270. process_dynamic_tapping_term(keycode, record) &&
  271. #endif
  272. #ifdef CAPS_WORD_ENABLE
  273. process_caps_word(keycode, record) &&
  274. #endif
  275. #ifdef SPACE_CADET_ENABLE
  276. process_space_cadet(keycode, record) &&
  277. #endif
  278. #ifdef MAGIC_KEYCODE_ENABLE
  279. process_magic(keycode, record) &&
  280. #endif
  281. #ifdef GRAVE_ESC_ENABLE
  282. process_grave_esc(keycode, record) &&
  283. #endif
  284. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  285. process_rgb(keycode, record) &&
  286. #endif
  287. #ifdef JOYSTICK_ENABLE
  288. process_joystick(keycode, record) &&
  289. #endif
  290. #ifdef PROGRAMMABLE_BUTTON_ENABLE
  291. process_programmable_button(keycode, record) &&
  292. #endif
  293. true)) {
  294. return false;
  295. }
  296. if (record->event.pressed) {
  297. switch (keycode) {
  298. #ifndef NO_RESET
  299. case QK_BOOTLOADER:
  300. reset_keyboard();
  301. return false;
  302. case QK_REBOOT:
  303. soft_reset_keyboard();
  304. return false;
  305. #endif
  306. #ifndef NO_DEBUG
  307. case QK_DEBUG_TOGGLE:
  308. debug_enable ^= 1;
  309. if (debug_enable) {
  310. print("DEBUG: enabled.\n");
  311. } else {
  312. print("DEBUG: disabled.\n");
  313. }
  314. #endif
  315. return false;
  316. case QK_CLEAR_EEPROM:
  317. eeconfig_init();
  318. #ifndef NO_RESET
  319. soft_reset_keyboard();
  320. #endif
  321. return false;
  322. #ifdef VELOCIKEY_ENABLE
  323. case VLK_TOG:
  324. velocikey_toggle();
  325. return false;
  326. #endif
  327. #ifdef BLUETOOTH_ENABLE
  328. case OUT_AUTO:
  329. set_output(OUTPUT_AUTO);
  330. return false;
  331. case OUT_USB:
  332. set_output(OUTPUT_USB);
  333. return false;
  334. case OUT_BT:
  335. set_output(OUTPUT_BLUETOOTH);
  336. return false;
  337. #endif
  338. #ifndef NO_ACTION_ONESHOT
  339. case ONESHOT_TOGGLE:
  340. oneshot_toggle();
  341. break;
  342. case ONESHOT_ENABLE:
  343. oneshot_enable();
  344. break;
  345. case ONESHOT_DISABLE:
  346. oneshot_disable();
  347. break;
  348. #endif
  349. #ifdef ENABLE_COMPILE_KEYCODE
  350. case QK_MAKE: // Compiles the firmware, and adds the flash command based on keyboard bootloader
  351. {
  352. # ifdef NO_ACTION_ONESHOT
  353. const uint8_t temp_mod = mod_config(get_mods());
  354. # else
  355. const uint8_t temp_mod = mod_config(get_mods() | get_oneshot_mods());
  356. clear_oneshot_mods();
  357. # endif
  358. clear_mods();
  359. SEND_STRING_DELAY("qmk", TAP_CODE_DELAY);
  360. if (temp_mod & MOD_MASK_SHIFT) { // if shift is held, flash rather than compile
  361. SEND_STRING_DELAY(" flash ", TAP_CODE_DELAY);
  362. } else {
  363. SEND_STRING_DELAY(" compile ", TAP_CODE_DELAY);
  364. }
  365. SEND_STRING_DELAY("-kb " QMK_KEYBOARD " -km " QMK_KEYMAP SS_TAP(X_ENTER), TAP_CODE_DELAY);
  366. }
  367. #endif
  368. }
  369. }
  370. return process_action_kb(record);
  371. }
  372. void set_single_persistent_default_layer(uint8_t default_layer) {
  373. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  374. PLAY_SONG(default_layer_songs[default_layer]);
  375. #endif
  376. eeconfig_update_default_layer((layer_state_t)1 << default_layer);
  377. default_layer_set((layer_state_t)1 << default_layer);
  378. }
  379. layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  380. layer_state_t mask12 = ((layer_state_t)1 << layer1) | ((layer_state_t)1 << layer2);
  381. layer_state_t mask3 = (layer_state_t)1 << layer3;
  382. return (state & mask12) == mask12 ? (state | mask3) : (state & ~mask3);
  383. }
  384. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  385. layer_state_set(update_tri_layer_state(layer_state, layer1, layer2, layer3));
  386. }
  387. // TODO: remove legacy api
  388. void matrix_init_quantum() {
  389. matrix_init_kb();
  390. }
  391. void matrix_scan_quantum() {
  392. matrix_scan_kb();
  393. }
  394. //------------------------------------------------------------------------------
  395. // Override these functions in your keymap file to play different tunes on
  396. // different events such as startup and bootloader jump
  397. __attribute__((weak)) void startup_user() {}
  398. __attribute__((weak)) void shutdown_user() {}
  399. void suspend_power_down_quantum(void) {
  400. suspend_power_down_kb();
  401. #ifndef NO_SUSPEND_POWER_DOWN
  402. // Turn off backlight
  403. # ifdef BACKLIGHT_ENABLE
  404. backlight_set(0);
  405. # endif
  406. # ifdef LED_MATRIX_ENABLE
  407. led_matrix_task();
  408. # endif
  409. # ifdef RGB_MATRIX_ENABLE
  410. rgb_matrix_task();
  411. # endif
  412. // Turn off LED indicators
  413. led_suspend();
  414. // Turn off audio
  415. # ifdef AUDIO_ENABLE
  416. stop_all_notes();
  417. # endif
  418. // Turn off underglow
  419. # if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
  420. rgblight_suspend();
  421. # endif
  422. # if defined(LED_MATRIX_ENABLE)
  423. led_matrix_set_suspend_state(true);
  424. # endif
  425. # if defined(RGB_MATRIX_ENABLE)
  426. rgb_matrix_set_suspend_state(true);
  427. # endif
  428. # ifdef OLED_ENABLE
  429. oled_off();
  430. # endif
  431. # ifdef ST7565_ENABLE
  432. st7565_off();
  433. # endif
  434. # if defined(POINTING_DEVICE_ENABLE)
  435. // run to ensure scanning occurs while suspended
  436. pointing_device_task();
  437. # endif
  438. #endif
  439. }
  440. __attribute__((weak)) void suspend_wakeup_init_quantum(void) {
  441. // Turn on backlight
  442. #ifdef BACKLIGHT_ENABLE
  443. backlight_init();
  444. #endif
  445. // Restore LED indicators
  446. led_wakeup();
  447. // Wake up underglow
  448. #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
  449. rgblight_wakeup();
  450. #endif
  451. #if defined(LED_MATRIX_ENABLE)
  452. led_matrix_set_suspend_state(false);
  453. #endif
  454. #if defined(RGB_MATRIX_ENABLE)
  455. rgb_matrix_set_suspend_state(false);
  456. #endif
  457. suspend_wakeup_init_kb();
  458. }
  459. /** \brief converts unsigned integers into char arrays
  460. *
  461. * Takes an unsigned integer and converts that value into an equivalent char array
  462. * A padding character may be specified, ' ' for leading spaces, '0' for leading zeros.
  463. */
  464. const char *get_numeric_str(char *buf, size_t buf_len, uint32_t curr_num, char curr_pad) {
  465. buf[buf_len - 1] = '\0';
  466. for (size_t i = 0; i < buf_len - 1; ++i) {
  467. char c = '0' + curr_num % 10;
  468. buf[buf_len - 2 - i] = (c == '0' && i == 0) ? '0' : (curr_num > 0 ? c : curr_pad);
  469. curr_num /= 10;
  470. }
  471. return buf;
  472. }
  473. /** \brief converts uint8_t into char array
  474. *
  475. * Takes an uint8_t, and uses an internal static buffer to render that value into a char array
  476. * A padding character may be specified, ' ' for leading spaces, '0' for leading zeros.
  477. *
  478. * NOTE: Subsequent invocations will reuse the same static buffer and overwrite the previous
  479. * contents. Use the result immediately, instead of caching it.
  480. */
  481. const char *get_u8_str(uint8_t curr_num, char curr_pad) {
  482. static char buf[4] = {0};
  483. static uint8_t last_num = 0xFF;
  484. static char last_pad = '\0';
  485. if (last_num == curr_num && last_pad == curr_pad) {
  486. return buf;
  487. }
  488. last_num = curr_num;
  489. last_pad = curr_pad;
  490. return get_numeric_str(buf, sizeof(buf), curr_num, curr_pad);
  491. }
  492. /** \brief converts uint16_t into char array
  493. *
  494. * Takes an uint16_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_u16_str(uint16_t curr_num, char curr_pad) {
  501. static char buf[6] = {0};
  502. static uint16_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. #if defined(SECURE_ENABLE)
  512. void secure_hook_quantum(secure_status_t secure_status) {
  513. // If keys are being held when this is triggered, they may not be released properly
  514. // this can result in stuck keys, mods and layers. To prevent that, manually
  515. // clear these, when it is triggered.
  516. if (secure_status == SECURE_PENDING) {
  517. clear_keyboard();
  518. layer_clear();
  519. }
  520. }
  521. #endif