quantum.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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 <ctype.h>
  17. #include "quantum.h"
  18. #ifdef PROTOCOL_LUFA
  19. # include "outputselect.h"
  20. #endif
  21. #ifdef BACKLIGHT_ENABLE
  22. # include "backlight.h"
  23. extern backlight_config_t backlight_config;
  24. #endif
  25. #ifdef FAUXCLICKY_ENABLE
  26. # include "fauxclicky.h"
  27. #endif
  28. #ifdef API_ENABLE
  29. # include "api.h"
  30. #endif
  31. #ifdef MIDI_ENABLE
  32. # include "process_midi.h"
  33. #endif
  34. #ifdef VELOCIKEY_ENABLE
  35. # include "velocikey.h"
  36. #endif
  37. #ifdef HAPTIC_ENABLE
  38. # include "haptic.h"
  39. #endif
  40. #ifdef AUDIO_ENABLE
  41. # ifndef GOODBYE_SONG
  42. # define GOODBYE_SONG SONG(GOODBYE_SOUND)
  43. # endif
  44. float goodbye_song[][2] = GOODBYE_SONG;
  45. # ifdef DEFAULT_LAYER_SONGS
  46. float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
  47. # endif
  48. # ifdef SENDSTRING_BELL
  49. float bell_song[][2] = SONG(TERMINAL_SOUND);
  50. # endif
  51. #endif
  52. static void do_code16(uint16_t code, void (*f)(uint8_t)) {
  53. switch (code) {
  54. case QK_MODS ... QK_MODS_MAX:
  55. break;
  56. default:
  57. return;
  58. }
  59. uint8_t mods_to_send = 0;
  60. if (code & QK_RMODS_MIN) { // Right mod flag is set
  61. if (code & QK_LCTL) mods_to_send |= MOD_BIT(KC_RCTL);
  62. if (code & QK_LSFT) mods_to_send |= MOD_BIT(KC_RSFT);
  63. if (code & QK_LALT) mods_to_send |= MOD_BIT(KC_RALT);
  64. if (code & QK_LGUI) mods_to_send |= MOD_BIT(KC_RGUI);
  65. } else {
  66. if (code & QK_LCTL) mods_to_send |= MOD_BIT(KC_LCTL);
  67. if (code & QK_LSFT) mods_to_send |= MOD_BIT(KC_LSFT);
  68. if (code & QK_LALT) mods_to_send |= MOD_BIT(KC_LALT);
  69. if (code & QK_LGUI) mods_to_send |= MOD_BIT(KC_LGUI);
  70. }
  71. f(mods_to_send);
  72. }
  73. void register_code16(uint16_t code) {
  74. if (IS_MOD(code) || code == KC_NO) {
  75. do_code16(code, register_mods);
  76. } else {
  77. do_code16(code, register_weak_mods);
  78. }
  79. register_code(code);
  80. }
  81. void unregister_code16(uint16_t code) {
  82. unregister_code(code);
  83. if (IS_MOD(code) || code == KC_NO) {
  84. do_code16(code, unregister_mods);
  85. } else {
  86. do_code16(code, unregister_weak_mods);
  87. }
  88. }
  89. void tap_code16(uint16_t code) {
  90. register_code16(code);
  91. #if TAP_CODE_DELAY > 0
  92. wait_ms(TAP_CODE_DELAY);
  93. #endif
  94. unregister_code16(code);
  95. }
  96. __attribute__((weak)) bool process_action_kb(keyrecord_t *record) { return true; }
  97. __attribute__((weak)) bool process_record_kb(uint16_t keycode, keyrecord_t *record) { return process_record_user(keycode, record); }
  98. __attribute__((weak)) bool process_record_user(uint16_t keycode, keyrecord_t *record) { return true; }
  99. __attribute__((weak)) void post_process_record_kb(uint16_t keycode, keyrecord_t *record) { post_process_record_user(keycode, record); }
  100. __attribute__((weak)) void post_process_record_user(uint16_t keycode, keyrecord_t *record) {}
  101. void reset_keyboard(void) {
  102. clear_keyboard();
  103. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  104. process_midi_all_notes_off();
  105. #endif
  106. #ifdef AUDIO_ENABLE
  107. # ifndef NO_MUSIC_MODE
  108. music_all_notes_off();
  109. # endif
  110. uint16_t timer_start = timer_read();
  111. PLAY_SONG(goodbye_song);
  112. shutdown_user();
  113. while (timer_elapsed(timer_start) < 250) wait_ms(1);
  114. stop_all_notes();
  115. #else
  116. shutdown_user();
  117. wait_ms(250);
  118. #endif
  119. #ifdef HAPTIC_ENABLE
  120. haptic_shutdown();
  121. #endif
  122. bootloader_jump();
  123. }
  124. /* Convert record into usable keycode via the contained event. */
  125. uint16_t get_record_keycode(keyrecord_t *record, bool update_layer_cache) { return get_event_keycode(record->event, update_layer_cache); }
  126. /* Convert event into usable keycode. Checks the layer cache to ensure that it
  127. * retains the correct keycode after a layer change, if the key is still pressed.
  128. * "update_layer_cache" is to ensure that it only updates the layer cache when
  129. * appropriate, otherwise, it will update it and cause layer tap (and other keys)
  130. * from triggering properly.
  131. */
  132. uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache) {
  133. #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
  134. /* TODO: Use store_or_get_action() or a similar function. */
  135. if (!disable_action_cache) {
  136. uint8_t layer;
  137. if (event.pressed && update_layer_cache) {
  138. layer = layer_switch_get_layer(event.key);
  139. update_source_layers_cache(event.key, layer);
  140. } else {
  141. layer = read_source_layers_cache(event.key);
  142. }
  143. return keymap_key_to_keycode(layer, event.key);
  144. } else
  145. #endif
  146. return keymap_key_to_keycode(layer_switch_get_layer(event.key), event.key);
  147. }
  148. /* Get keycode, and then call keyboard function */
  149. void post_process_record_quantum(keyrecord_t *record) {
  150. uint16_t keycode = get_record_keycode(record, false);
  151. post_process_record_kb(keycode, record);
  152. }
  153. /* Core keycode function, hands off handling to other functions,
  154. then processes internal quantum keycodes, and then processes
  155. ACTIONs. */
  156. bool process_record_quantum(keyrecord_t *record) {
  157. uint16_t keycode = get_record_keycode(record, true);
  158. // This is how you use actions here
  159. // if (keycode == KC_LEAD) {
  160. // action_t action;
  161. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  162. // process_action(record, action);
  163. // return false;
  164. // }
  165. #ifdef VELOCIKEY_ENABLE
  166. if (velocikey_enabled() && record->event.pressed) {
  167. velocikey_accelerate();
  168. }
  169. #endif
  170. #ifdef WPM_ENABLE
  171. if (record->event.pressed) {
  172. update_wpm(keycode);
  173. }
  174. #endif
  175. #ifdef TAP_DANCE_ENABLE
  176. preprocess_tap_dance(keycode, record);
  177. #endif
  178. if (!(
  179. #if defined(KEY_LOCK_ENABLE)
  180. // Must run first to be able to mask key_up events.
  181. process_key_lock(&keycode, record) &&
  182. #endif
  183. #if defined(DYNAMIC_MACRO_ENABLE) && !defined(DYNAMIC_MACRO_USER_CALL)
  184. // Must run asap to ensure all keypresses are recorded.
  185. process_dynamic_macro(keycode, record) &&
  186. #endif
  187. #if defined(AUDIO_ENABLE) && defined(AUDIO_CLICKY)
  188. process_clicky(keycode, record) &&
  189. #endif // AUDIO_CLICKY
  190. #ifdef HAPTIC_ENABLE
  191. process_haptic(keycode, record) &&
  192. #endif // HAPTIC_ENABLE
  193. #if defined(RGB_MATRIX_ENABLE)
  194. process_rgb_matrix(keycode, record) &&
  195. #endif
  196. #if defined(VIA_ENABLE)
  197. process_record_via(keycode, record) &&
  198. #endif
  199. process_record_kb(keycode, record) &&
  200. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  201. process_midi(keycode, record) &&
  202. #endif
  203. #ifdef AUDIO_ENABLE
  204. process_audio(keycode, record) &&
  205. #endif
  206. #ifdef BACKLIGHT_ENABLE
  207. process_backlight(keycode, record) &&
  208. #endif
  209. #ifdef STENO_ENABLE
  210. process_steno(keycode, record) &&
  211. #endif
  212. #if (defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))) && !defined(NO_MUSIC_MODE)
  213. process_music(keycode, record) &&
  214. #endif
  215. #ifdef TAP_DANCE_ENABLE
  216. process_tap_dance(keycode, record) &&
  217. #endif
  218. #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
  219. process_unicode_common(keycode, record) &&
  220. #endif
  221. #ifdef LEADER_ENABLE
  222. process_leader(keycode, record) &&
  223. #endif
  224. #ifdef COMBO_ENABLE
  225. process_combo(keycode, record) &&
  226. #endif
  227. #ifdef PRINTING_ENABLE
  228. process_printer(keycode, record) &&
  229. #endif
  230. #ifdef AUTO_SHIFT_ENABLE
  231. process_auto_shift(keycode, record) &&
  232. #endif
  233. #ifdef TERMINAL_ENABLE
  234. process_terminal(keycode, record) &&
  235. #endif
  236. #ifdef SPACE_CADET_ENABLE
  237. process_space_cadet(keycode, record) &&
  238. #endif
  239. #ifdef MAGIC_KEYCODE_ENABLE
  240. process_magic(keycode, record) &&
  241. #endif
  242. #ifdef GRAVE_ESC_ENABLE
  243. process_grave_esc(keycode, record) &&
  244. #endif
  245. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  246. process_rgb(keycode, record) &&
  247. #endif
  248. true)) {
  249. return false;
  250. }
  251. if (record->event.pressed) {
  252. switch (keycode) {
  253. #ifndef NO_RESET
  254. case RESET:
  255. reset_keyboard();
  256. return false;
  257. #endif
  258. #ifndef NO_DEBUG
  259. case DEBUG:
  260. debug_enable ^= 1;
  261. if (debug_enable) {
  262. print("DEBUG: enabled.\n");
  263. } else {
  264. print("DEBUG: disabled.\n");
  265. }
  266. #endif
  267. return false;
  268. case EEPROM_RESET:
  269. eeconfig_init();
  270. return false;
  271. #ifdef FAUXCLICKY_ENABLE
  272. case FC_TOG:
  273. FAUXCLICKY_TOGGLE;
  274. return false;
  275. case FC_ON:
  276. FAUXCLICKY_ON;
  277. return false;
  278. case FC_OFF:
  279. FAUXCLICKY_OFF;
  280. return false;
  281. #endif
  282. #ifdef VELOCIKEY_ENABLE
  283. case VLK_TOG:
  284. velocikey_toggle();
  285. return false;
  286. #endif
  287. #ifdef BLUETOOTH_ENABLE
  288. case OUT_AUTO:
  289. set_output(OUTPUT_AUTO);
  290. return false;
  291. case OUT_USB:
  292. set_output(OUTPUT_USB);
  293. return false;
  294. case OUT_BT:
  295. set_output(OUTPUT_BLUETOOTH);
  296. return false;
  297. #endif
  298. }
  299. }
  300. return process_action_kb(record);
  301. }
  302. // clang-format off
  303. /* Bit-Packed look-up table to convert an ASCII character to whether
  304. * [Shift] needs to be sent with the keycode.
  305. */
  306. __attribute__((weak)) const uint8_t ascii_to_shift_lut[16] PROGMEM = {
  307. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  308. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  309. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  310. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  311. KCLUT_ENTRY(0, 1, 1, 1, 1, 1, 1, 0),
  312. KCLUT_ENTRY(1, 1, 1, 1, 0, 0, 0, 0),
  313. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  314. KCLUT_ENTRY(0, 0, 1, 0, 1, 0, 1, 1),
  315. KCLUT_ENTRY(1, 1, 1, 1, 1, 1, 1, 1),
  316. KCLUT_ENTRY(1, 1, 1, 1, 1, 1, 1, 1),
  317. KCLUT_ENTRY(1, 1, 1, 1, 1, 1, 1, 1),
  318. KCLUT_ENTRY(1, 1, 1, 0, 0, 0, 1, 1),
  319. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  320. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  321. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  322. KCLUT_ENTRY(0, 0, 0, 1, 1, 1, 1, 0),
  323. };
  324. /* Bit-Packed look-up table to convert an ASCII character to whether
  325. * [AltGr] needs to be sent with the keycode.
  326. */
  327. __attribute__((weak)) const uint8_t ascii_to_altgr_lut[16] PROGMEM = {
  328. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  329. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  330. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  331. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  332. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  333. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  334. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  335. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  336. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  337. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  338. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  339. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  340. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  341. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  342. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  343. KCLUT_ENTRY(0, 0, 0, 0, 0, 0, 0, 0),
  344. };
  345. /* Look-up table to convert an ASCII character to a keycode.
  346. */
  347. __attribute__((weak)) const uint8_t ascii_to_keycode_lut[128] PROGMEM = {
  348. // NUL SOH STX ETX EOT ENQ ACK BEL
  349. XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  350. // BS TAB LF VT FF CR SO SI
  351. KC_BSPC, KC_TAB, KC_ENT, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  352. // DLE DC1 DC2 DC3 DC4 NAK SYN ETB
  353. XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  354. // CAN EM SUB ESC FS GS RS US
  355. XXXXXXX, XXXXXXX, XXXXXXX, KC_ESC, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  356. // ! " # $ % & '
  357. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  358. // ( ) * + , - . /
  359. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  360. // 0 1 2 3 4 5 6 7
  361. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  362. // 8 9 : ; < = > ?
  363. KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  364. // @ A B C D E F G
  365. KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  366. // H I J K L M N O
  367. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  368. // P Q R S T U V W
  369. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  370. // X Y Z [ \ ] ^ _
  371. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  372. // ` a b c d e f g
  373. KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  374. // h i j k l m n o
  375. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  376. // p q r s t u v w
  377. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  378. // x y z { | } ~ DEL
  379. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  380. };
  381. // clang-format on
  382. // Note: we bit-pack in "reverse" order to optimize loading
  383. #define PGM_LOADBIT(mem, pos) ((pgm_read_byte(&((mem)[(pos) / 8])) >> ((pos) % 8)) & 0x01)
  384. void send_string(const char *str) { send_string_with_delay(str, 0); }
  385. void send_string_P(const char *str) { send_string_with_delay_P(str, 0); }
  386. void send_string_with_delay(const char *str, uint8_t interval) {
  387. while (1) {
  388. char ascii_code = *str;
  389. if (!ascii_code) break;
  390. if (ascii_code == SS_QMK_PREFIX) {
  391. ascii_code = *(++str);
  392. if (ascii_code == SS_TAP_CODE) {
  393. // tap
  394. uint8_t keycode = *(++str);
  395. tap_code(keycode);
  396. } else if (ascii_code == SS_DOWN_CODE) {
  397. // down
  398. uint8_t keycode = *(++str);
  399. register_code(keycode);
  400. } else if (ascii_code == SS_UP_CODE) {
  401. // up
  402. uint8_t keycode = *(++str);
  403. unregister_code(keycode);
  404. } else if (ascii_code == SS_DELAY_CODE) {
  405. // delay
  406. int ms = 0;
  407. uint8_t keycode = *(++str);
  408. while (isdigit(keycode)) {
  409. ms *= 10;
  410. ms += keycode - '0';
  411. keycode = *(++str);
  412. }
  413. while (ms--) wait_ms(1);
  414. }
  415. } else {
  416. send_char(ascii_code);
  417. }
  418. ++str;
  419. // interval
  420. {
  421. uint8_t ms = interval;
  422. while (ms--) wait_ms(1);
  423. }
  424. }
  425. }
  426. void send_string_with_delay_P(const char *str, uint8_t interval) {
  427. while (1) {
  428. char ascii_code = pgm_read_byte(str);
  429. if (!ascii_code) break;
  430. if (ascii_code == SS_QMK_PREFIX) {
  431. ascii_code = pgm_read_byte(++str);
  432. if (ascii_code == SS_TAP_CODE) {
  433. // tap
  434. uint8_t keycode = pgm_read_byte(++str);
  435. tap_code(keycode);
  436. } else if (ascii_code == SS_DOWN_CODE) {
  437. // down
  438. uint8_t keycode = pgm_read_byte(++str);
  439. register_code(keycode);
  440. } else if (ascii_code == SS_UP_CODE) {
  441. // up
  442. uint8_t keycode = pgm_read_byte(++str);
  443. unregister_code(keycode);
  444. } else if (ascii_code == SS_DELAY_CODE) {
  445. // delay
  446. int ms = 0;
  447. uint8_t keycode = pgm_read_byte(++str);
  448. while (isdigit(keycode)) {
  449. ms *= 10;
  450. ms += keycode - '0';
  451. keycode = pgm_read_byte(++str);
  452. }
  453. while (ms--) wait_ms(1);
  454. }
  455. } else {
  456. send_char(ascii_code);
  457. }
  458. ++str;
  459. // interval
  460. {
  461. uint8_t ms = interval;
  462. while (ms--) wait_ms(1);
  463. }
  464. }
  465. }
  466. void send_char(char ascii_code) {
  467. #if defined(AUDIO_ENABLE) && defined(SENDSTRING_BELL)
  468. if (ascii_code == '\a') { // BEL
  469. PLAY_SONG(bell_song);
  470. return;
  471. }
  472. #endif
  473. uint8_t keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]);
  474. bool is_shifted = PGM_LOADBIT(ascii_to_shift_lut, (uint8_t)ascii_code);
  475. bool is_altgred = PGM_LOADBIT(ascii_to_altgr_lut, (uint8_t)ascii_code);
  476. if (is_shifted) {
  477. register_code(KC_LSFT);
  478. }
  479. if (is_altgred) {
  480. register_code(KC_RALT);
  481. }
  482. tap_code(keycode);
  483. if (is_altgred) {
  484. unregister_code(KC_RALT);
  485. }
  486. if (is_shifted) {
  487. unregister_code(KC_LSFT);
  488. }
  489. }
  490. void set_single_persistent_default_layer(uint8_t default_layer) {
  491. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  492. PLAY_SONG(default_layer_songs[default_layer]);
  493. #endif
  494. eeconfig_update_default_layer(1U << default_layer);
  495. default_layer_set(1U << default_layer);
  496. }
  497. layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  498. layer_state_t mask12 = (1UL << layer1) | (1UL << layer2);
  499. layer_state_t mask3 = 1UL << layer3;
  500. return (state & mask12) == mask12 ? (state | mask3) : (state & ~mask3);
  501. }
  502. 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)); }
  503. void tap_random_base64(void) {
  504. #if defined(__AVR_ATmega32U4__)
  505. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  506. #else
  507. uint8_t key = rand() % 64;
  508. #endif
  509. switch (key) {
  510. case 0 ... 25:
  511. register_code(KC_LSFT);
  512. register_code(key + KC_A);
  513. unregister_code(key + KC_A);
  514. unregister_code(KC_LSFT);
  515. break;
  516. case 26 ... 51:
  517. register_code(key - 26 + KC_A);
  518. unregister_code(key - 26 + KC_A);
  519. break;
  520. case 52:
  521. register_code(KC_0);
  522. unregister_code(KC_0);
  523. break;
  524. case 53 ... 61:
  525. register_code(key - 53 + KC_1);
  526. unregister_code(key - 53 + KC_1);
  527. break;
  528. case 62:
  529. register_code(KC_LSFT);
  530. register_code(KC_EQL);
  531. unregister_code(KC_EQL);
  532. unregister_code(KC_LSFT);
  533. break;
  534. case 63:
  535. register_code(KC_SLSH);
  536. unregister_code(KC_SLSH);
  537. break;
  538. }
  539. }
  540. void matrix_init_quantum() {
  541. #ifdef BOOTMAGIC_LITE
  542. bootmagic_lite();
  543. #endif
  544. if (!eeconfig_is_enabled()) {
  545. eeconfig_init();
  546. }
  547. #ifdef BACKLIGHT_ENABLE
  548. # ifdef LED_MATRIX_ENABLE
  549. led_matrix_init();
  550. # else
  551. backlight_init_ports();
  552. # endif
  553. #endif
  554. #ifdef AUDIO_ENABLE
  555. audio_init();
  556. #endif
  557. #ifdef RGB_MATRIX_ENABLE
  558. rgb_matrix_init();
  559. #endif
  560. #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
  561. unicode_input_mode_init();
  562. #endif
  563. #ifdef HAPTIC_ENABLE
  564. haptic_init();
  565. #endif
  566. #ifdef OUTPUT_AUTO_ENABLE
  567. set_output(OUTPUT_AUTO);
  568. #endif
  569. matrix_init_kb();
  570. }
  571. void matrix_scan_quantum() {
  572. #if defined(AUDIO_ENABLE) && !defined(NO_MUSIC_MODE)
  573. matrix_scan_music();
  574. #endif
  575. #ifdef TAP_DANCE_ENABLE
  576. matrix_scan_tap_dance();
  577. #endif
  578. #ifdef COMBO_ENABLE
  579. matrix_scan_combo();
  580. #endif
  581. #ifdef LED_MATRIX_ENABLE
  582. led_matrix_task();
  583. #endif
  584. #ifdef RGB_MATRIX_ENABLE
  585. rgb_matrix_task();
  586. #endif
  587. #ifdef WPM_ENABLE
  588. decay_wpm();
  589. #endif
  590. #ifdef HAPTIC_ENABLE
  591. haptic_task();
  592. #endif
  593. #ifdef DIP_SWITCH_ENABLE
  594. dip_switch_read(false);
  595. #endif
  596. matrix_scan_kb();
  597. }
  598. #ifdef HD44780_ENABLED
  599. # include "hd44780.h"
  600. #endif
  601. // Functions for spitting out values
  602. //
  603. void send_dword(uint32_t number) {
  604. uint16_t word = (number >> 16);
  605. send_word(word);
  606. send_word(number & 0xFFFFUL);
  607. }
  608. void send_word(uint16_t number) {
  609. uint8_t byte = number >> 8;
  610. send_byte(byte);
  611. send_byte(number & 0xFF);
  612. }
  613. void send_byte(uint8_t number) {
  614. uint8_t nibble = number >> 4;
  615. send_nibble(nibble);
  616. send_nibble(number & 0xF);
  617. }
  618. void send_nibble(uint8_t number) {
  619. switch (number) {
  620. case 0:
  621. register_code(KC_0);
  622. unregister_code(KC_0);
  623. break;
  624. case 1 ... 9:
  625. register_code(KC_1 + (number - 1));
  626. unregister_code(KC_1 + (number - 1));
  627. break;
  628. case 0xA ... 0xF:
  629. register_code(KC_A + (number - 0xA));
  630. unregister_code(KC_A + (number - 0xA));
  631. break;
  632. }
  633. }
  634. __attribute__((weak)) uint16_t hex_to_keycode(uint8_t hex) {
  635. hex = hex & 0xF;
  636. if (hex == 0x0) {
  637. return KC_0;
  638. } else if (hex < 0xA) {
  639. return KC_1 + (hex - 0x1);
  640. } else {
  641. return KC_A + (hex - 0xA);
  642. }
  643. }
  644. void api_send_unicode(uint32_t unicode) {
  645. #ifdef API_ENABLE
  646. uint8_t chunk[4];
  647. dword_to_bytes(unicode, chunk);
  648. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  649. #endif
  650. }
  651. /** \brief Lock LED set callback - keymap/user level
  652. *
  653. * \deprecated Use led_update_user() instead.
  654. */
  655. __attribute__((weak)) void led_set_user(uint8_t usb_led) {}
  656. /** \brief Lock LED set callback - keyboard level
  657. *
  658. * \deprecated Use led_update_kb() instead.
  659. */
  660. __attribute__((weak)) void led_set_kb(uint8_t usb_led) { led_set_user(usb_led); }
  661. /** \brief Lock LED update callback - keymap/user level
  662. *
  663. * \return True if led_update_kb() should run its own code, false otherwise.
  664. */
  665. __attribute__((weak)) bool led_update_user(led_t led_state) { return true; }
  666. /** \brief Lock LED update callback - keyboard level
  667. *
  668. * \return Ignored for now.
  669. */
  670. __attribute__((weak)) bool led_update_kb(led_t led_state) { return led_update_user(led_state); }
  671. __attribute__((weak)) void led_init_ports(void) {}
  672. __attribute__((weak)) void led_set(uint8_t usb_led) {
  673. #if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE)
  674. // Use backlight as Caps Lock indicator
  675. uint8_t bl_toggle_lvl = 0;
  676. if (IS_LED_ON(usb_led, USB_LED_CAPS_LOCK) && !backlight_config.enable) {
  677. // Turning Caps Lock ON and backlight is disabled in config
  678. // Toggling backlight to the brightest level
  679. bl_toggle_lvl = BACKLIGHT_LEVELS;
  680. } else if (IS_LED_OFF(usb_led, USB_LED_CAPS_LOCK) && backlight_config.enable) {
  681. // Turning Caps Lock OFF and backlight is enabled in config
  682. // Toggling backlight and restoring config level
  683. bl_toggle_lvl = backlight_config.level;
  684. }
  685. // Set level without modify backlight_config to keep ability to restore state
  686. backlight_set(bl_toggle_lvl);
  687. #endif
  688. led_set_kb(usb_led);
  689. led_update_kb((led_t)usb_led);
  690. }
  691. //------------------------------------------------------------------------------
  692. // Override these functions in your keymap file to play different tunes on
  693. // different events such as startup and bootloader jump
  694. __attribute__((weak)) void startup_user() {}
  695. __attribute__((weak)) void shutdown_user() {}
  696. //------------------------------------------------------------------------------