quantum.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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) { return record->keycode; }
  125. #endif
  126. return get_event_keycode(record->event, update_layer_cache);
  127. }
  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 process pre tapping functionality */
  151. bool pre_process_record_quantum(keyrecord_t *record) {
  152. if (!(
  153. #ifdef COMBO_ENABLE
  154. process_combo(get_record_keycode(record, true), record) &&
  155. #endif
  156. true)) {
  157. return false;
  158. }
  159. return true; // continue processing
  160. }
  161. /* Get keycode, and then call keyboard function */
  162. void post_process_record_quantum(keyrecord_t *record) {
  163. uint16_t keycode = get_record_keycode(record, false);
  164. post_process_record_kb(keycode, record);
  165. }
  166. /* Core keycode function, hands off handling to other functions,
  167. then processes internal quantum keycodes, and then processes
  168. ACTIONs. */
  169. bool process_record_quantum(keyrecord_t *record) {
  170. uint16_t keycode = get_record_keycode(record, true);
  171. // This is how you use actions here
  172. // if (keycode == KC_LEAD) {
  173. // action_t action;
  174. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  175. // process_action(record, action);
  176. // return false;
  177. // }
  178. #ifdef VELOCIKEY_ENABLE
  179. if (velocikey_enabled() && record->event.pressed) {
  180. velocikey_accelerate();
  181. }
  182. #endif
  183. #ifdef WPM_ENABLE
  184. if (record->event.pressed) {
  185. update_wpm(keycode);
  186. }
  187. #endif
  188. #ifdef TAP_DANCE_ENABLE
  189. preprocess_tap_dance(keycode, record);
  190. #endif
  191. if (!(
  192. #if defined(KEY_LOCK_ENABLE)
  193. // Must run first to be able to mask key_up events.
  194. process_key_lock(&keycode, record) &&
  195. #endif
  196. #if defined(DYNAMIC_MACRO_ENABLE) && !defined(DYNAMIC_MACRO_USER_CALL)
  197. // Must run asap to ensure all keypresses are recorded.
  198. process_dynamic_macro(keycode, record) &&
  199. #endif
  200. #if defined(AUDIO_ENABLE) && defined(AUDIO_CLICKY)
  201. process_clicky(keycode, record) &&
  202. #endif
  203. #ifdef HAPTIC_ENABLE
  204. process_haptic(keycode, record) &&
  205. #endif
  206. #if defined(VIA_ENABLE)
  207. process_record_via(keycode, record) &&
  208. #endif
  209. process_record_kb(keycode, record) &&
  210. #if defined(SEQUENCER_ENABLE)
  211. process_sequencer(keycode, record) &&
  212. #endif
  213. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  214. process_midi(keycode, record) &&
  215. #endif
  216. #ifdef AUDIO_ENABLE
  217. process_audio(keycode, record) &&
  218. #endif
  219. #if defined(BACKLIGHT_ENABLE) || defined(LED_MATRIX_ENABLE)
  220. process_backlight(keycode, record) &&
  221. #endif
  222. #ifdef STENO_ENABLE
  223. process_steno(keycode, record) &&
  224. #endif
  225. #if (defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))) && !defined(NO_MUSIC_MODE)
  226. process_music(keycode, record) &&
  227. #endif
  228. #ifdef KEY_OVERRIDE_ENABLE
  229. process_key_override(keycode, record) &&
  230. #endif
  231. #ifdef TAP_DANCE_ENABLE
  232. process_tap_dance(keycode, record) &&
  233. #endif
  234. #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
  235. process_unicode_common(keycode, record) &&
  236. #endif
  237. #ifdef LEADER_ENABLE
  238. process_leader(keycode, record) &&
  239. #endif
  240. #ifdef PRINTING_ENABLE
  241. process_printer(keycode, record) &&
  242. #endif
  243. #ifdef AUTO_SHIFT_ENABLE
  244. process_auto_shift(keycode, record) &&
  245. #endif
  246. #ifdef TERMINAL_ENABLE
  247. process_terminal(keycode, record) &&
  248. #endif
  249. #ifdef SPACE_CADET_ENABLE
  250. process_space_cadet(keycode, record) &&
  251. #endif
  252. #ifdef MAGIC_KEYCODE_ENABLE
  253. process_magic(keycode, record) &&
  254. #endif
  255. #ifdef GRAVE_ESC_ENABLE
  256. process_grave_esc(keycode, record) &&
  257. #endif
  258. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  259. process_rgb(keycode, record) &&
  260. #endif
  261. #ifdef JOYSTICK_ENABLE
  262. process_joystick(keycode, record) &&
  263. #endif
  264. true)) {
  265. return false;
  266. }
  267. if (record->event.pressed) {
  268. switch (keycode) {
  269. #ifndef NO_RESET
  270. case RESET:
  271. reset_keyboard();
  272. return false;
  273. #endif
  274. #ifndef NO_DEBUG
  275. case DEBUG:
  276. debug_enable ^= 1;
  277. if (debug_enable) {
  278. print("DEBUG: enabled.\n");
  279. } else {
  280. print("DEBUG: disabled.\n");
  281. }
  282. #endif
  283. return false;
  284. case EEPROM_RESET:
  285. eeconfig_init();
  286. return false;
  287. #ifdef VELOCIKEY_ENABLE
  288. case VLK_TOG:
  289. velocikey_toggle();
  290. return false;
  291. #endif
  292. #ifdef BLUETOOTH_ENABLE
  293. case OUT_AUTO:
  294. set_output(OUTPUT_AUTO);
  295. return false;
  296. case OUT_USB:
  297. set_output(OUTPUT_USB);
  298. return false;
  299. case OUT_BT:
  300. set_output(OUTPUT_BLUETOOTH);
  301. return false;
  302. #endif
  303. #ifndef NO_ACTION_ONESHOT
  304. case ONESHOT_TOGGLE:
  305. oneshot_toggle();
  306. break;
  307. case ONESHOT_ENABLE:
  308. oneshot_enable();
  309. break;
  310. case ONESHOT_DISABLE:
  311. oneshot_disable();
  312. break;
  313. #endif
  314. }
  315. }
  316. return process_action_kb(record);
  317. }
  318. void set_single_persistent_default_layer(uint8_t default_layer) {
  319. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  320. PLAY_SONG(default_layer_songs[default_layer]);
  321. #endif
  322. eeconfig_update_default_layer((layer_state_t)1 << default_layer);
  323. default_layer_set((layer_state_t)1 << default_layer);
  324. }
  325. layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  326. layer_state_t mask12 = ((layer_state_t)1 << layer1) | ((layer_state_t)1 << layer2);
  327. layer_state_t mask3 = (layer_state_t)1 << layer3;
  328. return (state & mask12) == mask12 ? (state | mask3) : (state & ~mask3);
  329. }
  330. 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)); }
  331. void matrix_init_quantum() {
  332. magic();
  333. led_init_ports();
  334. #ifdef BACKLIGHT_ENABLE
  335. backlight_init_ports();
  336. #endif
  337. #ifdef AUDIO_ENABLE
  338. audio_init();
  339. #endif
  340. #ifdef LED_MATRIX_ENABLE
  341. led_matrix_init();
  342. #endif
  343. #ifdef RGB_MATRIX_ENABLE
  344. rgb_matrix_init();
  345. #endif
  346. #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
  347. unicode_input_mode_init();
  348. #endif
  349. #ifdef HAPTIC_ENABLE
  350. haptic_init();
  351. #endif
  352. #if defined(BLUETOOTH_ENABLE) && defined(OUTPUT_AUTO_ENABLE)
  353. set_output(OUTPUT_AUTO);
  354. #endif
  355. matrix_init_kb();
  356. }
  357. void matrix_scan_quantum() {
  358. #if defined(AUDIO_ENABLE) && defined(AUDIO_INIT_DELAY)
  359. // There are some tasks that need to be run a little bit
  360. // after keyboard startup, or else they will not work correctly
  361. // because of interaction with the USB device state, which
  362. // may still be in flux...
  363. //
  364. // At the moment the only feature that needs this is the
  365. // startup song.
  366. static bool delayed_tasks_run = false;
  367. static uint16_t delayed_task_timer = 0;
  368. if (!delayed_tasks_run) {
  369. if (!delayed_task_timer) {
  370. delayed_task_timer = timer_read();
  371. } else if (timer_elapsed(delayed_task_timer) > 300) {
  372. audio_startup();
  373. delayed_tasks_run = true;
  374. }
  375. }
  376. #endif
  377. #if defined(AUDIO_ENABLE) && !defined(NO_MUSIC_MODE)
  378. music_task();
  379. #endif
  380. #ifdef KEY_OVERRIDE_ENABLE
  381. key_override_task();
  382. #endif
  383. #ifdef SEQUENCER_ENABLE
  384. sequencer_task();
  385. #endif
  386. #ifdef TAP_DANCE_ENABLE
  387. tap_dance_task();
  388. #endif
  389. #ifdef COMBO_ENABLE
  390. combo_task();
  391. #endif
  392. #ifdef LED_MATRIX_ENABLE
  393. led_matrix_task();
  394. #endif
  395. #ifdef WPM_ENABLE
  396. decay_wpm();
  397. #endif
  398. #ifdef HAPTIC_ENABLE
  399. haptic_task();
  400. #endif
  401. #ifdef DIP_SWITCH_ENABLE
  402. dip_switch_read(false);
  403. #endif
  404. #ifdef AUTO_SHIFT_ENABLE
  405. autoshift_matrix_scan();
  406. #endif
  407. matrix_scan_kb();
  408. }
  409. #ifdef HD44780_ENABLED
  410. # include "hd44780.h"
  411. #endif
  412. void api_send_unicode(uint32_t unicode) {
  413. #ifdef API_ENABLE
  414. uint8_t chunk[4];
  415. dword_to_bytes(unicode, chunk);
  416. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  417. #endif
  418. }
  419. //------------------------------------------------------------------------------
  420. // Override these functions in your keymap file to play different tunes on
  421. // different events such as startup and bootloader jump
  422. __attribute__((weak)) void startup_user() {}
  423. __attribute__((weak)) void shutdown_user() {}