quantum.c 23 KB

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