quantum.c 16 KB

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