quantum.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. #include "magic.h"
  18. #ifdef BLUETOOTH_ENABLE
  19. # include "outputselect.h"
  20. #endif
  21. #ifdef BACKLIGHT_ENABLE
  22. # include "backlight.h"
  23. #endif
  24. #ifdef MIDI_ENABLE
  25. # include "process_midi.h"
  26. #endif
  27. #ifdef VELOCIKEY_ENABLE
  28. # include "velocikey.h"
  29. #endif
  30. #ifdef HAPTIC_ENABLE
  31. # include "haptic.h"
  32. #endif
  33. #ifdef AUDIO_ENABLE
  34. # ifndef GOODBYE_SONG
  35. # define GOODBYE_SONG SONG(GOODBYE_SOUND)
  36. # endif
  37. float goodbye_song[][2] = GOODBYE_SONG;
  38. # ifdef DEFAULT_LAYER_SONGS
  39. float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
  40. # endif
  41. #endif
  42. uint8_t extract_mod_bits(uint16_t code) {
  43. switch (code) {
  44. case QK_MODS ... QK_MODS_MAX:
  45. break;
  46. default:
  47. return 0;
  48. }
  49. uint8_t mods_to_send = 0;
  50. if (code & QK_RMODS_MIN) { // Right mod flag is set
  51. if (code & QK_LCTL) mods_to_send |= MOD_BIT(KC_RIGHT_CTRL);
  52. if (code & QK_LSFT) mods_to_send |= MOD_BIT(KC_RIGHT_SHIFT);
  53. if (code & QK_LALT) mods_to_send |= MOD_BIT(KC_RIGHT_ALT);
  54. if (code & QK_LGUI) mods_to_send |= MOD_BIT(KC_RIGHT_GUI);
  55. } else {
  56. if (code & QK_LCTL) mods_to_send |= MOD_BIT(KC_LEFT_CTRL);
  57. if (code & QK_LSFT) mods_to_send |= MOD_BIT(KC_LEFT_SHIFT);
  58. if (code & QK_LALT) mods_to_send |= MOD_BIT(KC_LEFT_ALT);
  59. if (code & QK_LGUI) mods_to_send |= MOD_BIT(KC_LEFT_GUI);
  60. }
  61. return mods_to_send;
  62. }
  63. void do_code16(uint16_t code, void (*f)(uint8_t)) { f(extract_mod_bits(code)); }
  64. __attribute__((weak)) void register_code16(uint16_t code) {
  65. if (IS_MOD(code) || code == KC_NO) {
  66. do_code16(code, register_mods);
  67. } else {
  68. do_code16(code, register_weak_mods);
  69. }
  70. register_code(code);
  71. }
  72. __attribute__((weak)) void unregister_code16(uint16_t code) {
  73. unregister_code(code);
  74. if (IS_MOD(code) || code == KC_NO) {
  75. do_code16(code, unregister_mods);
  76. } else {
  77. do_code16(code, unregister_weak_mods);
  78. }
  79. }
  80. __attribute__((weak)) void tap_code16(uint16_t code) {
  81. register_code16(code);
  82. #if TAP_CODE_DELAY > 0
  83. wait_ms(TAP_CODE_DELAY);
  84. #endif
  85. unregister_code16(code);
  86. }
  87. __attribute__((weak)) bool process_action_kb(keyrecord_t *record) { return true; }
  88. __attribute__((weak)) bool process_record_kb(uint16_t keycode, keyrecord_t *record) { return process_record_user(keycode, record); }
  89. __attribute__((weak)) bool process_record_user(uint16_t keycode, keyrecord_t *record) { return true; }
  90. __attribute__((weak)) void post_process_record_kb(uint16_t keycode, keyrecord_t *record) { post_process_record_user(keycode, record); }
  91. __attribute__((weak)) void post_process_record_user(uint16_t keycode, keyrecord_t *record) {}
  92. void reset_keyboard(void) {
  93. clear_keyboard();
  94. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  95. process_midi_all_notes_off();
  96. #endif
  97. #ifdef AUDIO_ENABLE
  98. # ifndef NO_MUSIC_MODE
  99. music_all_notes_off();
  100. # endif
  101. uint16_t timer_start = timer_read();
  102. PLAY_SONG(goodbye_song);
  103. shutdown_user();
  104. while (timer_elapsed(timer_start) < 250) wait_ms(1);
  105. stop_all_notes();
  106. #else
  107. shutdown_user();
  108. wait_ms(250);
  109. #endif
  110. #ifdef HAPTIC_ENABLE
  111. haptic_shutdown();
  112. #endif
  113. bootloader_jump();
  114. }
  115. /* Convert record into usable keycode via the contained event. */
  116. uint16_t get_record_keycode(keyrecord_t *record, bool update_layer_cache) {
  117. #ifdef COMBO_ENABLE
  118. if (record->keycode) {
  119. return record->keycode;
  120. }
  121. #endif
  122. return get_event_keycode(record->event, update_layer_cache);
  123. }
  124. /* Convert event into usable keycode. Checks the layer cache to ensure that it
  125. * retains the correct keycode after a layer change, if the key is still pressed.
  126. * "update_layer_cache" is to ensure that it only updates the layer cache when
  127. * appropriate, otherwise, it will update it and cause layer tap (and other keys)
  128. * from triggering properly.
  129. */
  130. uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache) {
  131. #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
  132. /* TODO: Use store_or_get_action() or a similar function. */
  133. if (!disable_action_cache) {
  134. uint8_t layer;
  135. if (event.pressed && update_layer_cache) {
  136. layer = layer_switch_get_layer(event.key);
  137. update_source_layers_cache(event.key, layer);
  138. } else {
  139. layer = read_source_layers_cache(event.key);
  140. }
  141. return keymap_key_to_keycode(layer, event.key);
  142. } else
  143. #endif
  144. return keymap_key_to_keycode(layer_switch_get_layer(event.key), event.key);
  145. }
  146. /* Get keycode, and then process pre tapping functionality */
  147. bool pre_process_record_quantum(keyrecord_t *record) {
  148. if (!(
  149. #ifdef COMBO_ENABLE
  150. process_combo(get_record_keycode(record, true), record) &&
  151. #endif
  152. true)) {
  153. return false;
  154. }
  155. return true; // continue processing
  156. }
  157. /* Get keycode, and then call keyboard function */
  158. void post_process_record_quantum(keyrecord_t *record) {
  159. uint16_t keycode = get_record_keycode(record, false);
  160. post_process_record_kb(keycode, record);
  161. }
  162. /* Core keycode function, hands off handling to other functions,
  163. then processes internal quantum keycodes, and then processes
  164. ACTIONs. */
  165. bool process_record_quantum(keyrecord_t *record) {
  166. uint16_t keycode = get_record_keycode(record, true);
  167. // This is how you use actions here
  168. // if (keycode == KC_LEAD) {
  169. // action_t action;
  170. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  171. // process_action(record, action);
  172. // return false;
  173. // }
  174. #ifdef VELOCIKEY_ENABLE
  175. if (velocikey_enabled() && record->event.pressed) {
  176. velocikey_accelerate();
  177. }
  178. #endif
  179. #ifdef WPM_ENABLE
  180. if (record->event.pressed) {
  181. update_wpm(keycode);
  182. }
  183. #endif
  184. #ifdef TAP_DANCE_ENABLE
  185. preprocess_tap_dance(keycode, record);
  186. #endif
  187. if (!(
  188. #if defined(KEY_LOCK_ENABLE)
  189. // Must run first to be able to mask key_up events.
  190. process_key_lock(&keycode, record) &&
  191. #endif
  192. #if defined(DYNAMIC_MACRO_ENABLE) && !defined(DYNAMIC_MACRO_USER_CALL)
  193. // Must run asap to ensure all keypresses are recorded.
  194. process_dynamic_macro(keycode, record) &&
  195. #endif
  196. #if defined(AUDIO_ENABLE) && defined(AUDIO_CLICKY)
  197. process_clicky(keycode, record) &&
  198. #endif
  199. #ifdef HAPTIC_ENABLE
  200. process_haptic(keycode, record) &&
  201. #endif
  202. #if defined(VIA_ENABLE)
  203. process_record_via(keycode, record) &&
  204. #endif
  205. process_record_kb(keycode, record) &&
  206. #if defined(SEQUENCER_ENABLE)
  207. process_sequencer(keycode, record) &&
  208. #endif
  209. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  210. process_midi(keycode, record) &&
  211. #endif
  212. #ifdef AUDIO_ENABLE
  213. process_audio(keycode, record) &&
  214. #endif
  215. #if defined(BACKLIGHT_ENABLE) || defined(LED_MATRIX_ENABLE)
  216. process_backlight(keycode, record) &&
  217. #endif
  218. #ifdef STENO_ENABLE
  219. process_steno(keycode, record) &&
  220. #endif
  221. #if (defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))) && !defined(NO_MUSIC_MODE)
  222. process_music(keycode, record) &&
  223. #endif
  224. #ifdef KEY_OVERRIDE_ENABLE
  225. process_key_override(keycode, record) &&
  226. #endif
  227. #ifdef TAP_DANCE_ENABLE
  228. process_tap_dance(keycode, record) &&
  229. #endif
  230. #if defined(UNICODE_COMMON_ENABLE)
  231. process_unicode_common(keycode, record) &&
  232. #endif
  233. #ifdef LEADER_ENABLE
  234. process_leader(keycode, record) &&
  235. #endif
  236. #ifdef PRINTING_ENABLE
  237. process_printer(keycode, record) &&
  238. #endif
  239. #ifdef AUTO_SHIFT_ENABLE
  240. process_auto_shift(keycode, record) &&
  241. #endif
  242. #ifdef DYNAMIC_TAPPING_TERM_ENABLE
  243. process_dynamic_tapping_term(keycode, record) &&
  244. #endif
  245. #ifdef TERMINAL_ENABLE
  246. process_terminal(keycode, record) &&
  247. #endif
  248. #ifdef SPACE_CADET_ENABLE
  249. process_space_cadet(keycode, record) &&
  250. #endif
  251. #ifdef MAGIC_KEYCODE_ENABLE
  252. process_magic(keycode, record) &&
  253. #endif
  254. #ifdef GRAVE_ESC_ENABLE
  255. process_grave_esc(keycode, record) &&
  256. #endif
  257. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  258. process_rgb(keycode, record) &&
  259. #endif
  260. #ifdef JOYSTICK_ENABLE
  261. process_joystick(keycode, record) &&
  262. #endif
  263. #ifdef PROGRAMMABLE_BUTTON_ENABLE
  264. process_programmable_button(keycode, record) &&
  265. #endif
  266. true)) {
  267. return false;
  268. }
  269. if (record->event.pressed) {
  270. switch (keycode) {
  271. #ifndef NO_RESET
  272. case QK_BOOTLOADER:
  273. reset_keyboard();
  274. return false;
  275. #endif
  276. #ifndef NO_DEBUG
  277. case QK_DEBUG_TOGGLE:
  278. debug_enable ^= 1;
  279. if (debug_enable) {
  280. print("DEBUG: enabled.\n");
  281. } else {
  282. print("DEBUG: disabled.\n");
  283. }
  284. #endif
  285. return false;
  286. case QK_CLEAR_EEPROM:
  287. eeconfig_init();
  288. return false;
  289. #ifdef VELOCIKEY_ENABLE
  290. case VLK_TOG:
  291. velocikey_toggle();
  292. return false;
  293. #endif
  294. #ifdef BLUETOOTH_ENABLE
  295. case OUT_AUTO:
  296. set_output(OUTPUT_AUTO);
  297. return false;
  298. case OUT_USB:
  299. set_output(OUTPUT_USB);
  300. return false;
  301. case OUT_BT:
  302. set_output(OUTPUT_BLUETOOTH);
  303. return false;
  304. #endif
  305. #ifndef NO_ACTION_ONESHOT
  306. case ONESHOT_TOGGLE:
  307. oneshot_toggle();
  308. break;
  309. case ONESHOT_ENABLE:
  310. oneshot_enable();
  311. break;
  312. case ONESHOT_DISABLE:
  313. oneshot_disable();
  314. break;
  315. #endif
  316. }
  317. }
  318. return process_action_kb(record);
  319. }
  320. void set_single_persistent_default_layer(uint8_t default_layer) {
  321. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  322. PLAY_SONG(default_layer_songs[default_layer]);
  323. #endif
  324. eeconfig_update_default_layer((layer_state_t)1 << default_layer);
  325. default_layer_set((layer_state_t)1 << default_layer);
  326. }
  327. layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  328. layer_state_t mask12 = ((layer_state_t)1 << layer1) | ((layer_state_t)1 << layer2);
  329. layer_state_t mask3 = (layer_state_t)1 << layer3;
  330. return (state & mask12) == mask12 ? (state | mask3) : (state & ~mask3);
  331. }
  332. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) { layer_state_set(update_tri_layer_state(layer_state, layer1, layer2, layer3)); }
  333. void matrix_init_quantum() {
  334. magic();
  335. led_init_ports();
  336. #ifdef BACKLIGHT_ENABLE
  337. backlight_init_ports();
  338. #endif
  339. #ifdef AUDIO_ENABLE
  340. audio_init();
  341. #endif
  342. #ifdef LED_MATRIX_ENABLE
  343. led_matrix_init();
  344. #endif
  345. #ifdef RGB_MATRIX_ENABLE
  346. rgb_matrix_init();
  347. #endif
  348. #if defined(UNICODE_COMMON_ENABLE)
  349. unicode_input_mode_init();
  350. #endif
  351. #ifdef HAPTIC_ENABLE
  352. haptic_init();
  353. #endif
  354. #if defined(BLUETOOTH_ENABLE) && defined(OUTPUT_AUTO_ENABLE)
  355. set_output(OUTPUT_AUTO);
  356. #endif
  357. matrix_init_kb();
  358. }
  359. // TODO: remove legacy api
  360. void matrix_scan_quantum() { matrix_scan_kb(); }
  361. //------------------------------------------------------------------------------
  362. // Override these functions in your keymap file to play different tunes on
  363. // different events such as startup and bootloader jump
  364. __attribute__((weak)) void startup_user() {}
  365. __attribute__((weak)) void shutdown_user() {}
  366. /** \brief Run keyboard level Power down
  367. *
  368. * FIXME: needs doc
  369. */
  370. __attribute__((weak)) void suspend_power_down_user(void) {}
  371. /** \brief Run keyboard level Power down
  372. *
  373. * FIXME: needs doc
  374. */
  375. __attribute__((weak)) void suspend_power_down_kb(void) { suspend_power_down_user(); }
  376. void suspend_power_down_quantum(void) {
  377. #ifndef NO_SUSPEND_POWER_DOWN
  378. // Turn off backlight
  379. # ifdef BACKLIGHT_ENABLE
  380. backlight_set(0);
  381. # endif
  382. # ifdef LED_MATRIX_ENABLE
  383. led_matrix_task();
  384. # endif
  385. # ifdef RGB_MATRIX_ENABLE
  386. rgb_matrix_task();
  387. # endif
  388. // Turn off LED indicators
  389. uint8_t leds_off = 0;
  390. # if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE)
  391. if (is_backlight_enabled()) {
  392. // Don't try to turn off Caps Lock indicator as it is backlight and backlight is already off
  393. leds_off |= (1 << USB_LED_CAPS_LOCK);
  394. }
  395. # endif
  396. led_set(leds_off);
  397. // Turn off audio
  398. # ifdef AUDIO_ENABLE
  399. stop_all_notes();
  400. # endif
  401. // Turn off underglow
  402. # if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
  403. rgblight_suspend();
  404. # endif
  405. # if defined(LED_MATRIX_ENABLE)
  406. led_matrix_set_suspend_state(true);
  407. # endif
  408. # if defined(RGB_MATRIX_ENABLE)
  409. rgb_matrix_set_suspend_state(true);
  410. # endif
  411. # ifdef OLED_ENABLE
  412. oled_off();
  413. # endif
  414. # ifdef ST7565_ENABLE
  415. st7565_off();
  416. # endif
  417. # if defined(POINTING_DEVICE_ENABLE)
  418. // run to ensure scanning occurs while suspended
  419. pointing_device_task();
  420. # endif
  421. #endif
  422. }
  423. /** \brief run user level code immediately after wakeup
  424. *
  425. * FIXME: needs doc
  426. */
  427. __attribute__((weak)) void suspend_wakeup_init_user(void) {}
  428. /** \brief run keyboard level code immediately after wakeup
  429. *
  430. * FIXME: needs doc
  431. */
  432. __attribute__((weak)) void suspend_wakeup_init_kb(void) { suspend_wakeup_init_user(); }
  433. __attribute__((weak)) void suspend_wakeup_init_quantum(void) {
  434. // Turn on backlight
  435. #ifdef BACKLIGHT_ENABLE
  436. backlight_init();
  437. #endif
  438. // Restore LED indicators
  439. led_set(host_keyboard_leds());
  440. // Wake up underglow
  441. #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
  442. rgblight_wakeup();
  443. #endif
  444. #if defined(LED_MATRIX_ENABLE)
  445. led_matrix_set_suspend_state(false);
  446. #endif
  447. #if defined(RGB_MATRIX_ENABLE)
  448. rgb_matrix_set_suspend_state(false);
  449. #endif
  450. suspend_wakeup_init_kb();
  451. }
  452. /** \brief converts unsigned integers into char arrays
  453. *
  454. * Takes an unsigned integer and converts that value into an equivalent char array
  455. * A padding character may be specified, ' ' for leading spaces, '0' for leading zeros.
  456. */
  457. const char *get_numeric_str(char *buf, size_t buf_len, uint32_t curr_num, char curr_pad) {
  458. buf[buf_len - 1] = '\0';
  459. for (size_t i = 0; i < buf_len - 1; ++i) {
  460. char c = '0' + curr_num % 10;
  461. buf[buf_len - 2 - i] = (c == '0' && i == 0) ? '0' : (curr_num > 0 ? c : curr_pad);
  462. curr_num /= 10;
  463. }
  464. return buf;
  465. }
  466. /** \brief converts uint8_t into char array
  467. *
  468. * Takes an uint8_t, and uses an internal static buffer to render that value into a char array
  469. * A padding character may be specified, ' ' for leading spaces, '0' for leading zeros.
  470. *
  471. * NOTE: Subsequent invocations will reuse the same static buffer and overwrite the previous
  472. * contents. Use the result immediately, instead of caching it.
  473. */
  474. const char *get_u8_str(uint8_t curr_num, char curr_pad) {
  475. static char buf[4] = {0};
  476. static uint8_t last_num = 0xFF;
  477. static char last_pad = '\0';
  478. if (last_num == curr_num && last_pad == curr_pad) {
  479. return buf;
  480. }
  481. last_num = curr_num;
  482. last_pad = curr_pad;
  483. return get_numeric_str(buf, sizeof(buf), curr_num, curr_pad);
  484. }
  485. /** \brief converts uint16_t into char array
  486. *
  487. * Takes an uint16_t, and uses an internal static buffer to render that value into a char array
  488. * A padding character may be specified, ' ' for leading spaces, '0' for leading zeros.
  489. *
  490. * NOTE: Subsequent invocations will reuse the same static buffer and overwrite the previous
  491. * contents. Use the result immediately, instead of caching it.
  492. */
  493. const char *get_u16_str(uint16_t curr_num, char curr_pad) {
  494. static char buf[6] = {0};
  495. static uint16_t last_num = 0xFF;
  496. static char last_pad = '\0';
  497. if (last_num == curr_num && last_pad == curr_pad) {
  498. return buf;
  499. }
  500. last_num = curr_num;
  501. last_pad = curr_pad;
  502. return get_numeric_str(buf, sizeof(buf), curr_num, curr_pad);
  503. }