quantum.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. # ifdef SENDSTRING_BELL
  45. float bell_song[][2] = SONG(TERMINAL_SOUND);
  46. # endif
  47. #endif
  48. #ifdef AUTO_SHIFT_ENABLE
  49. # include "process_auto_shift.h"
  50. #endif
  51. #ifdef KEY_OVERRIDE_ENABLE
  52. # include "process_key_override_private.h"
  53. #endif
  54. uint8_t extract_mod_bits(uint16_t code) {
  55. switch (code) {
  56. case QK_MODS ... QK_MODS_MAX:
  57. break;
  58. default:
  59. return 0;
  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. return mods_to_send;
  74. }
  75. static void do_code16(uint16_t code, void (*f)(uint8_t)) { f(extract_mod_bits(code)); }
  76. void register_code16(uint16_t code) {
  77. if (IS_MOD(code) || code == KC_NO) {
  78. do_code16(code, register_mods);
  79. } else {
  80. do_code16(code, register_weak_mods);
  81. }
  82. register_code(code);
  83. }
  84. void unregister_code16(uint16_t code) {
  85. unregister_code(code);
  86. if (IS_MOD(code) || code == KC_NO) {
  87. do_code16(code, unregister_mods);
  88. } else {
  89. do_code16(code, unregister_weak_mods);
  90. }
  91. }
  92. void tap_code16(uint16_t code) {
  93. register_code16(code);
  94. #if TAP_CODE_DELAY > 0
  95. wait_ms(TAP_CODE_DELAY);
  96. #endif
  97. unregister_code16(code);
  98. }
  99. __attribute__((weak)) bool process_action_kb(keyrecord_t *record) { return true; }
  100. __attribute__((weak)) bool process_record_kb(uint16_t keycode, keyrecord_t *record) { return process_record_user(keycode, record); }
  101. __attribute__((weak)) bool process_record_user(uint16_t keycode, keyrecord_t *record) { return true; }
  102. __attribute__((weak)) void post_process_record_kb(uint16_t keycode, keyrecord_t *record) { post_process_record_user(keycode, record); }
  103. __attribute__((weak)) void post_process_record_user(uint16_t keycode, keyrecord_t *record) {}
  104. void reset_keyboard(void) {
  105. clear_keyboard();
  106. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  107. process_midi_all_notes_off();
  108. #endif
  109. #ifdef AUDIO_ENABLE
  110. # ifndef NO_MUSIC_MODE
  111. music_all_notes_off();
  112. # endif
  113. uint16_t timer_start = timer_read();
  114. PLAY_SONG(goodbye_song);
  115. shutdown_user();
  116. while (timer_elapsed(timer_start) < 250) wait_ms(1);
  117. stop_all_notes();
  118. #else
  119. shutdown_user();
  120. wait_ms(250);
  121. #endif
  122. #ifdef HAPTIC_ENABLE
  123. haptic_shutdown();
  124. #endif
  125. bootloader_jump();
  126. }
  127. /* Convert record into usable keycode via the contained event. */
  128. uint16_t get_record_keycode(keyrecord_t *record, bool update_layer_cache) { return get_event_keycode(record->event, update_layer_cache); }
  129. /* Convert event into usable keycode. Checks the layer cache to ensure that it
  130. * retains the correct keycode after a layer change, if the key is still pressed.
  131. * "update_layer_cache" is to ensure that it only updates the layer cache when
  132. * appropriate, otherwise, it will update it and cause layer tap (and other keys)
  133. * from triggering properly.
  134. */
  135. uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache) {
  136. #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
  137. /* TODO: Use store_or_get_action() or a similar function. */
  138. if (!disable_action_cache) {
  139. uint8_t layer;
  140. if (event.pressed && update_layer_cache) {
  141. layer = layer_switch_get_layer(event.key);
  142. update_source_layers_cache(event.key, layer);
  143. } else {
  144. layer = read_source_layers_cache(event.key);
  145. }
  146. return keymap_key_to_keycode(layer, event.key);
  147. } else
  148. #endif
  149. return keymap_key_to_keycode(layer_switch_get_layer(event.key), event.key);
  150. }
  151. /* Get keycode, and then call keyboard function */
  152. void post_process_record_quantum(keyrecord_t *record) {
  153. uint16_t keycode = get_record_keycode(record, false);
  154. post_process_record_kb(keycode, record);
  155. }
  156. /* Core keycode function, hands off handling to other functions,
  157. then processes internal quantum keycodes, and then processes
  158. ACTIONs. */
  159. bool process_record_quantum(keyrecord_t *record) {
  160. uint16_t keycode = get_record_keycode(record, true);
  161. // This is how you use actions here
  162. // if (keycode == KC_LEAD) {
  163. // action_t action;
  164. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  165. // process_action(record, action);
  166. // return false;
  167. // }
  168. #ifdef VELOCIKEY_ENABLE
  169. if (velocikey_enabled() && record->event.pressed) {
  170. velocikey_accelerate();
  171. }
  172. #endif
  173. #ifdef WPM_ENABLE
  174. if (record->event.pressed) {
  175. update_wpm(keycode);
  176. }
  177. #endif
  178. #ifdef TAP_DANCE_ENABLE
  179. preprocess_tap_dance(keycode, record);
  180. #endif
  181. if (!(
  182. #if defined(KEY_LOCK_ENABLE)
  183. // Must run first to be able to mask key_up events.
  184. process_key_lock(&keycode, record) &&
  185. #endif
  186. #if defined(DYNAMIC_MACRO_ENABLE) && !defined(DYNAMIC_MACRO_USER_CALL)
  187. // Must run asap to ensure all keypresses are recorded.
  188. process_dynamic_macro(keycode, record) &&
  189. #endif
  190. #if defined(AUDIO_ENABLE) && defined(AUDIO_CLICKY)
  191. process_clicky(keycode, record) &&
  192. #endif // AUDIO_CLICKY
  193. #ifdef HAPTIC_ENABLE
  194. process_haptic(keycode, record) &&
  195. #endif // HAPTIC_ENABLE
  196. #if defined(VIA_ENABLE)
  197. process_record_via(keycode, record) &&
  198. #endif
  199. process_record_kb(keycode, record) &&
  200. #if defined(SEQUENCER_ENABLE)
  201. process_sequencer(keycode, record) &&
  202. #endif
  203. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  204. process_midi(keycode, record) &&
  205. #endif
  206. #ifdef AUDIO_ENABLE
  207. process_audio(keycode, record) &&
  208. #endif
  209. #if defined(BACKLIGHT_ENABLE) || defined(LED_MATRIX_ENABLE)
  210. process_backlight(keycode, record) &&
  211. #endif
  212. #ifdef STENO_ENABLE
  213. process_steno(keycode, record) &&
  214. #endif
  215. #if (defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))) && !defined(NO_MUSIC_MODE)
  216. process_music(keycode, record) &&
  217. #endif
  218. #ifdef KEY_OVERRIDE_ENABLE
  219. process_key_override(keycode, record) &&
  220. #endif
  221. #ifdef TAP_DANCE_ENABLE
  222. process_tap_dance(keycode, record) &&
  223. #endif
  224. #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
  225. process_unicode_common(keycode, record) &&
  226. #endif
  227. #ifdef LEADER_ENABLE
  228. process_leader(keycode, record) &&
  229. #endif
  230. #ifdef COMBO_ENABLE
  231. process_combo(keycode, record) &&
  232. #endif
  233. #ifdef PRINTING_ENABLE
  234. process_printer(keycode, record) &&
  235. #endif
  236. #ifdef AUTO_SHIFT_ENABLE
  237. process_auto_shift(keycode, record) &&
  238. #endif
  239. #ifdef TERMINAL_ENABLE
  240. process_terminal(keycode, record) &&
  241. #endif
  242. #ifdef SPACE_CADET_ENABLE
  243. process_space_cadet(keycode, record) &&
  244. #endif
  245. #ifdef MAGIC_KEYCODE_ENABLE
  246. process_magic(keycode, record) &&
  247. #endif
  248. #ifdef GRAVE_ESC_ENABLE
  249. process_grave_esc(keycode, record) &&
  250. #endif
  251. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  252. process_rgb(keycode, record) &&
  253. #endif
  254. #ifdef JOYSTICK_ENABLE
  255. process_joystick(keycode, record) &&
  256. #endif
  257. true)) {
  258. return false;
  259. }
  260. if (record->event.pressed) {
  261. switch (keycode) {
  262. #ifndef NO_RESET
  263. case RESET:
  264. reset_keyboard();
  265. return false;
  266. #endif
  267. #ifndef NO_DEBUG
  268. case DEBUG:
  269. debug_enable ^= 1;
  270. if (debug_enable) {
  271. print("DEBUG: enabled.\n");
  272. } else {
  273. print("DEBUG: disabled.\n");
  274. }
  275. #endif
  276. return false;
  277. case EEPROM_RESET:
  278. eeconfig_init();
  279. return false;
  280. #ifdef VELOCIKEY_ENABLE
  281. case VLK_TOG:
  282. velocikey_toggle();
  283. return false;
  284. #endif
  285. #ifdef BLUETOOTH_ENABLE
  286. case OUT_AUTO:
  287. set_output(OUTPUT_AUTO);
  288. return false;
  289. case OUT_USB:
  290. set_output(OUTPUT_USB);
  291. return false;
  292. case OUT_BT:
  293. set_output(OUTPUT_BLUETOOTH);
  294. return false;
  295. #endif
  296. #ifndef NO_ACTION_ONESHOT
  297. case ONESHOT_TOGGLE:
  298. oneshot_toggle();
  299. break;
  300. case ONESHOT_ENABLE:
  301. oneshot_enable();
  302. break;
  303. case ONESHOT_DISABLE:
  304. oneshot_disable();
  305. break;
  306. #endif
  307. }
  308. }
  309. return process_action_kb(record);
  310. }
  311. void set_single_persistent_default_layer(uint8_t default_layer) {
  312. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  313. PLAY_SONG(default_layer_songs[default_layer]);
  314. #endif
  315. eeconfig_update_default_layer((layer_state_t)1 << default_layer);
  316. default_layer_set((layer_state_t)1 << default_layer);
  317. }
  318. layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  319. layer_state_t mask12 = ((layer_state_t)1 << layer1) | ((layer_state_t)1 << layer2);
  320. layer_state_t mask3 = (layer_state_t)1 << layer3;
  321. return (state & mask12) == mask12 ? (state | mask3) : (state & ~mask3);
  322. }
  323. 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)); }
  324. void matrix_init_quantum() {
  325. magic();
  326. #if defined(LED_NUM_LOCK_PIN) || defined(LED_CAPS_LOCK_PIN) || defined(LED_SCROLL_LOCK_PIN) || defined(LED_COMPOSE_PIN) || defined(LED_KANA_PIN)
  327. // TODO: remove calls to led_init_ports from keyboards and remove ifdef
  328. led_init_ports();
  329. #endif
  330. #ifdef BACKLIGHT_ENABLE
  331. backlight_init_ports();
  332. #endif
  333. #ifdef AUDIO_ENABLE
  334. audio_init();
  335. #endif
  336. #ifdef LED_MATRIX_ENABLE
  337. led_matrix_init();
  338. #endif
  339. #ifdef RGB_MATRIX_ENABLE
  340. rgb_matrix_init();
  341. #endif
  342. #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
  343. unicode_input_mode_init();
  344. #endif
  345. #ifdef HAPTIC_ENABLE
  346. haptic_init();
  347. #endif
  348. #if defined(BLUETOOTH_ENABLE) && defined(OUTPUT_AUTO_ENABLE)
  349. set_output(OUTPUT_AUTO);
  350. #endif
  351. matrix_init_kb();
  352. }
  353. void matrix_scan_quantum() {
  354. #if defined(AUDIO_ENABLE)
  355. // There are some tasks that need to be run a little bit
  356. // after keyboard startup, or else they will not work correctly
  357. // because of interaction with the USB device state, which
  358. // may still be in flux...
  359. //
  360. // At the moment the only feature that needs this is the
  361. // startup song.
  362. static bool delayed_tasks_run = false;
  363. static uint16_t delayed_task_timer = 0;
  364. if (!delayed_tasks_run) {
  365. if (!delayed_task_timer) {
  366. delayed_task_timer = timer_read();
  367. } else if (timer_elapsed(delayed_task_timer) > 300) {
  368. audio_startup();
  369. delayed_tasks_run = true;
  370. }
  371. }
  372. #endif
  373. #if defined(AUDIO_ENABLE) && !defined(NO_MUSIC_MODE)
  374. matrix_scan_music();
  375. #endif
  376. #ifdef KEY_OVERRIDE_ENABLE
  377. matrix_scan_key_override();
  378. #endif
  379. #ifdef SEQUENCER_ENABLE
  380. matrix_scan_sequencer();
  381. #endif
  382. #ifdef TAP_DANCE_ENABLE
  383. matrix_scan_tap_dance();
  384. #endif
  385. #ifdef COMBO_ENABLE
  386. matrix_scan_combo();
  387. #endif
  388. #ifdef LED_MATRIX_ENABLE
  389. led_matrix_task();
  390. #endif
  391. #ifdef WPM_ENABLE
  392. decay_wpm();
  393. #endif
  394. #ifdef HAPTIC_ENABLE
  395. haptic_task();
  396. #endif
  397. #ifdef DIP_SWITCH_ENABLE
  398. dip_switch_read(false);
  399. #endif
  400. #ifdef AUTO_SHIFT_ENABLE
  401. autoshift_matrix_scan();
  402. #endif
  403. matrix_scan_kb();
  404. }
  405. #ifdef HD44780_ENABLED
  406. # include "hd44780.h"
  407. #endif
  408. void api_send_unicode(uint32_t unicode) {
  409. #ifdef API_ENABLE
  410. uint8_t chunk[4];
  411. dword_to_bytes(unicode, chunk);
  412. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  413. #endif
  414. }
  415. //------------------------------------------------------------------------------
  416. // Override these functions in your keymap file to play different tunes on
  417. // different events such as startup and bootloader jump
  418. __attribute__((weak)) void startup_user() {}
  419. __attribute__((weak)) void shutdown_user() {}