quantum.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 API_ENABLE
  25. # include "api.h"
  26. #endif
  27. #ifdef MIDI_ENABLE
  28. # include "process_midi.h"
  29. #endif
  30. #ifdef VELOCIKEY_ENABLE
  31. # include "velocikey.h"
  32. #endif
  33. #ifdef HAPTIC_ENABLE
  34. # include "haptic.h"
  35. #endif
  36. #ifdef AUDIO_ENABLE
  37. # ifndef GOODBYE_SONG
  38. # define GOODBYE_SONG SONG(GOODBYE_SOUND)
  39. # endif
  40. float goodbye_song[][2] = GOODBYE_SONG;
  41. # ifdef DEFAULT_LAYER_SONGS
  42. float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
  43. # endif
  44. #endif
  45. #ifdef AUTO_SHIFT_ENABLE
  46. # include "process_auto_shift.h"
  47. #endif
  48. uint8_t extract_mod_bits(uint16_t code) {
  49. switch (code) {
  50. case QK_MODS ... QK_MODS_MAX:
  51. break;
  52. default:
  53. return 0;
  54. }
  55. uint8_t mods_to_send = 0;
  56. if (code & QK_RMODS_MIN) { // Right mod flag is set
  57. if (code & QK_LCTL) mods_to_send |= MOD_BIT(KC_RCTL);
  58. if (code & QK_LSFT) mods_to_send |= MOD_BIT(KC_RSFT);
  59. if (code & QK_LALT) mods_to_send |= MOD_BIT(KC_RALT);
  60. if (code & QK_LGUI) mods_to_send |= MOD_BIT(KC_RGUI);
  61. } else {
  62. if (code & QK_LCTL) mods_to_send |= MOD_BIT(KC_LCTL);
  63. if (code & QK_LSFT) mods_to_send |= MOD_BIT(KC_LSFT);
  64. if (code & QK_LALT) mods_to_send |= MOD_BIT(KC_LALT);
  65. if (code & QK_LGUI) mods_to_send |= MOD_BIT(KC_LGUI);
  66. }
  67. return mods_to_send;
  68. }
  69. static void do_code16(uint16_t code, void (*f)(uint8_t)) { f(extract_mod_bits(code)); }
  70. void register_code16(uint16_t code) {
  71. if (IS_MOD(code) || code == KC_NO) {
  72. do_code16(code, register_mods);
  73. } else {
  74. do_code16(code, register_weak_mods);
  75. }
  76. register_code(code);
  77. }
  78. void unregister_code16(uint16_t code) {
  79. unregister_code(code);
  80. if (IS_MOD(code) || code == KC_NO) {
  81. do_code16(code, unregister_mods);
  82. } else {
  83. do_code16(code, unregister_weak_mods);
  84. }
  85. }
  86. void tap_code16(uint16_t code) {
  87. register_code16(code);
  88. #if TAP_CODE_DELAY > 0
  89. wait_ms(TAP_CODE_DELAY);
  90. #endif
  91. unregister_code16(code);
  92. }
  93. __attribute__((weak)) bool process_action_kb(keyrecord_t *record) { return true; }
  94. __attribute__((weak)) bool process_record_kb(uint16_t keycode, keyrecord_t *record) { return process_record_user(keycode, record); }
  95. __attribute__((weak)) bool process_record_user(uint16_t keycode, keyrecord_t *record) { return true; }
  96. __attribute__((weak)) void post_process_record_kb(uint16_t keycode, keyrecord_t *record) { post_process_record_user(keycode, record); }
  97. __attribute__((weak)) void post_process_record_user(uint16_t keycode, keyrecord_t *record) {}
  98. void reset_keyboard(void) {
  99. clear_keyboard();
  100. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  101. process_midi_all_notes_off();
  102. #endif
  103. #ifdef AUDIO_ENABLE
  104. # ifndef NO_MUSIC_MODE
  105. music_all_notes_off();
  106. # endif
  107. uint16_t timer_start = timer_read();
  108. PLAY_SONG(goodbye_song);
  109. shutdown_user();
  110. while (timer_elapsed(timer_start) < 250) wait_ms(1);
  111. stop_all_notes();
  112. #else
  113. shutdown_user();
  114. wait_ms(250);
  115. #endif
  116. #ifdef HAPTIC_ENABLE
  117. haptic_shutdown();
  118. #endif
  119. bootloader_jump();
  120. }
  121. /* Convert record into usable keycode via the contained event. */
  122. uint16_t get_record_keycode(keyrecord_t *record, bool update_layer_cache) {
  123. #ifdef COMBO_ENABLE
  124. if (record->keycode) {
  125. return record->keycode;
  126. }
  127. #endif
  128. return get_event_keycode(record->event, update_layer_cache);
  129. }
  130. /* Convert event into usable keycode. Checks the layer cache to ensure that it
  131. * retains the correct keycode after a layer change, if the key is still pressed.
  132. * "update_layer_cache" is to ensure that it only updates the layer cache when
  133. * appropriate, otherwise, it will update it and cause layer tap (and other keys)
  134. * from triggering properly.
  135. */
  136. uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache) {
  137. #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
  138. /* TODO: Use store_or_get_action() or a similar function. */
  139. if (!disable_action_cache) {
  140. uint8_t layer;
  141. if (event.pressed && update_layer_cache) {
  142. layer = layer_switch_get_layer(event.key);
  143. update_source_layers_cache(event.key, layer);
  144. } else {
  145. layer = read_source_layers_cache(event.key);
  146. }
  147. return keymap_key_to_keycode(layer, event.key);
  148. } else
  149. #endif
  150. return keymap_key_to_keycode(layer_switch_get_layer(event.key), event.key);
  151. }
  152. /* Get keycode, and then process pre tapping functionality */
  153. bool pre_process_record_quantum(keyrecord_t *record) {
  154. if (!(
  155. #ifdef COMBO_ENABLE
  156. process_combo(get_record_keycode(record, true), record) &&
  157. #endif
  158. true)) {
  159. return false;
  160. }
  161. return true; // continue processing
  162. }
  163. /* Get keycode, and then call keyboard function */
  164. void post_process_record_quantum(keyrecord_t *record) {
  165. uint16_t keycode = get_record_keycode(record, false);
  166. post_process_record_kb(keycode, record);
  167. }
  168. /* Core keycode function, hands off handling to other functions,
  169. then processes internal quantum keycodes, and then processes
  170. ACTIONs. */
  171. bool process_record_quantum(keyrecord_t *record) {
  172. uint16_t keycode = get_record_keycode(record, true);
  173. // This is how you use actions here
  174. // if (keycode == KC_LEAD) {
  175. // action_t action;
  176. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  177. // process_action(record, action);
  178. // return false;
  179. // }
  180. #ifdef VELOCIKEY_ENABLE
  181. if (velocikey_enabled() && record->event.pressed) {
  182. velocikey_accelerate();
  183. }
  184. #endif
  185. #ifdef WPM_ENABLE
  186. if (record->event.pressed) {
  187. update_wpm(keycode);
  188. }
  189. #endif
  190. #ifdef TAP_DANCE_ENABLE
  191. preprocess_tap_dance(keycode, record);
  192. #endif
  193. if (!(
  194. #if defined(KEY_LOCK_ENABLE)
  195. // Must run first to be able to mask key_up events.
  196. process_key_lock(&keycode, record) &&
  197. #endif
  198. #if defined(DYNAMIC_MACRO_ENABLE) && !defined(DYNAMIC_MACRO_USER_CALL)
  199. // Must run asap to ensure all keypresses are recorded.
  200. process_dynamic_macro(keycode, record) &&
  201. #endif
  202. #if defined(AUDIO_ENABLE) && defined(AUDIO_CLICKY)
  203. process_clicky(keycode, record) &&
  204. #endif
  205. #ifdef HAPTIC_ENABLE
  206. process_haptic(keycode, record) &&
  207. #endif
  208. #if defined(VIA_ENABLE)
  209. process_record_via(keycode, record) &&
  210. #endif
  211. process_record_kb(keycode, record) &&
  212. #if defined(SEQUENCER_ENABLE)
  213. process_sequencer(keycode, record) &&
  214. #endif
  215. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  216. process_midi(keycode, record) &&
  217. #endif
  218. #ifdef AUDIO_ENABLE
  219. process_audio(keycode, record) &&
  220. #endif
  221. #if defined(BACKLIGHT_ENABLE) || defined(LED_MATRIX_ENABLE)
  222. process_backlight(keycode, record) &&
  223. #endif
  224. #ifdef STENO_ENABLE
  225. process_steno(keycode, record) &&
  226. #endif
  227. #if (defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))) && !defined(NO_MUSIC_MODE)
  228. process_music(keycode, record) &&
  229. #endif
  230. #ifdef KEY_OVERRIDE_ENABLE
  231. process_key_override(keycode, record) &&
  232. #endif
  233. #ifdef TAP_DANCE_ENABLE
  234. process_tap_dance(keycode, record) &&
  235. #endif
  236. #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
  237. process_unicode_common(keycode, record) &&
  238. #endif
  239. #ifdef LEADER_ENABLE
  240. process_leader(keycode, record) &&
  241. #endif
  242. #ifdef PRINTING_ENABLE
  243. process_printer(keycode, record) &&
  244. #endif
  245. #ifdef AUTO_SHIFT_ENABLE
  246. process_auto_shift(keycode, record) &&
  247. #endif
  248. #ifdef TERMINAL_ENABLE
  249. process_terminal(keycode, record) &&
  250. #endif
  251. #ifdef SPACE_CADET_ENABLE
  252. process_space_cadet(keycode, record) &&
  253. #endif
  254. #ifdef MAGIC_KEYCODE_ENABLE
  255. process_magic(keycode, record) &&
  256. #endif
  257. #ifdef GRAVE_ESC_ENABLE
  258. process_grave_esc(keycode, record) &&
  259. #endif
  260. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  261. process_rgb(keycode, record) &&
  262. #endif
  263. #ifdef JOYSTICK_ENABLE
  264. process_joystick(keycode, record) &&
  265. #endif
  266. true)) {
  267. return false;
  268. }
  269. if (record->event.pressed) {
  270. switch (keycode) {
  271. #ifndef NO_RESET
  272. case RESET:
  273. reset_keyboard();
  274. return false;
  275. #endif
  276. #ifndef NO_DEBUG
  277. case DEBUG:
  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 EEPROM_RESET:
  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_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_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. void matrix_scan_quantum() {
  360. #if defined(AUDIO_ENABLE) && defined(AUDIO_INIT_DELAY)
  361. // There are some tasks that need to be run a little bit
  362. // after keyboard startup, or else they will not work correctly
  363. // because of interaction with the USB device state, which
  364. // may still be in flux...
  365. //
  366. // At the moment the only feature that needs this is the
  367. // startup song.
  368. static bool delayed_tasks_run = false;
  369. static uint16_t delayed_task_timer = 0;
  370. if (!delayed_tasks_run) {
  371. if (!delayed_task_timer) {
  372. delayed_task_timer = timer_read();
  373. } else if (timer_elapsed(delayed_task_timer) > 300) {
  374. audio_startup();
  375. delayed_tasks_run = true;
  376. }
  377. }
  378. #endif
  379. #if defined(AUDIO_ENABLE) && !defined(NO_MUSIC_MODE)
  380. music_task();
  381. #endif
  382. #ifdef KEY_OVERRIDE_ENABLE
  383. key_override_task();
  384. #endif
  385. #ifdef SEQUENCER_ENABLE
  386. sequencer_task();
  387. #endif
  388. #ifdef TAP_DANCE_ENABLE
  389. tap_dance_task();
  390. #endif
  391. #ifdef COMBO_ENABLE
  392. combo_task();
  393. #endif
  394. #ifdef LED_MATRIX_ENABLE
  395. led_matrix_task();
  396. #endif
  397. #ifdef WPM_ENABLE
  398. decay_wpm();
  399. #endif
  400. #ifdef HAPTIC_ENABLE
  401. haptic_task();
  402. #endif
  403. #ifdef DIP_SWITCH_ENABLE
  404. dip_switch_read(false);
  405. #endif
  406. #ifdef AUTO_SHIFT_ENABLE
  407. autoshift_matrix_scan();
  408. #endif
  409. matrix_scan_kb();
  410. }
  411. #ifdef HD44780_ENABLED
  412. # include "hd44780.h"
  413. #endif
  414. void api_send_unicode(uint32_t unicode) {
  415. #ifdef API_ENABLE
  416. uint8_t chunk[4];
  417. dword_to_bytes(unicode, chunk);
  418. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  419. #endif
  420. }
  421. //------------------------------------------------------------------------------
  422. // Override these functions in your keymap file to play different tunes on
  423. // different events such as startup and bootloader jump
  424. __attribute__((weak)) void startup_user() {}
  425. __attribute__((weak)) void shutdown_user() {}