quantum.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. #ifdef KEY_OVERRIDE_ENABLE
  49. # include "process_key_override_private.h"
  50. #endif
  51. uint8_t extract_mod_bits(uint16_t code) {
  52. switch (code) {
  53. case QK_MODS ... QK_MODS_MAX:
  54. break;
  55. default:
  56. return 0;
  57. }
  58. uint8_t mods_to_send = 0;
  59. if (code & QK_RMODS_MIN) { // Right mod flag is set
  60. if (code & QK_LCTL) mods_to_send |= MOD_BIT(KC_RCTL);
  61. if (code & QK_LSFT) mods_to_send |= MOD_BIT(KC_RSFT);
  62. if (code & QK_LALT) mods_to_send |= MOD_BIT(KC_RALT);
  63. if (code & QK_LGUI) mods_to_send |= MOD_BIT(KC_RGUI);
  64. } else {
  65. if (code & QK_LCTL) mods_to_send |= MOD_BIT(KC_LCTL);
  66. if (code & QK_LSFT) mods_to_send |= MOD_BIT(KC_LSFT);
  67. if (code & QK_LALT) mods_to_send |= MOD_BIT(KC_LALT);
  68. if (code & QK_LGUI) mods_to_send |= MOD_BIT(KC_LGUI);
  69. }
  70. return mods_to_send;
  71. }
  72. static void do_code16(uint16_t code, void (*f)(uint8_t)) { f(extract_mod_bits(code)); }
  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(VIA_ENABLE)
  194. process_record_via(keycode, record) &&
  195. #endif
  196. process_record_kb(keycode, record) &&
  197. #if defined(SEQUENCER_ENABLE)
  198. process_sequencer(keycode, record) &&
  199. #endif
  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. #if defined(BACKLIGHT_ENABLE) || defined(LED_MATRIX_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 KEY_OVERRIDE_ENABLE
  216. process_key_override(keycode, record) &&
  217. #endif
  218. #ifdef TAP_DANCE_ENABLE
  219. process_tap_dance(keycode, record) &&
  220. #endif
  221. #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
  222. process_unicode_common(keycode, record) &&
  223. #endif
  224. #ifdef LEADER_ENABLE
  225. process_leader(keycode, record) &&
  226. #endif
  227. #ifdef COMBO_ENABLE
  228. process_combo(keycode, record) &&
  229. #endif
  230. #ifdef PRINTING_ENABLE
  231. process_printer(keycode, record) &&
  232. #endif
  233. #ifdef AUTO_SHIFT_ENABLE
  234. process_auto_shift(keycode, record) &&
  235. #endif
  236. #ifdef TERMINAL_ENABLE
  237. process_terminal(keycode, record) &&
  238. #endif
  239. #ifdef SPACE_CADET_ENABLE
  240. process_space_cadet(keycode, record) &&
  241. #endif
  242. #ifdef MAGIC_KEYCODE_ENABLE
  243. process_magic(keycode, record) &&
  244. #endif
  245. #ifdef GRAVE_ESC_ENABLE
  246. process_grave_esc(keycode, record) &&
  247. #endif
  248. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  249. process_rgb(keycode, record) &&
  250. #endif
  251. #ifdef JOYSTICK_ENABLE
  252. process_joystick(keycode, record) &&
  253. #endif
  254. true)) {
  255. return false;
  256. }
  257. if (record->event.pressed) {
  258. switch (keycode) {
  259. #ifndef NO_RESET
  260. case RESET:
  261. reset_keyboard();
  262. return false;
  263. #endif
  264. #ifndef NO_DEBUG
  265. case DEBUG:
  266. debug_enable ^= 1;
  267. if (debug_enable) {
  268. print("DEBUG: enabled.\n");
  269. } else {
  270. print("DEBUG: disabled.\n");
  271. }
  272. #endif
  273. return false;
  274. case EEPROM_RESET:
  275. eeconfig_init();
  276. return false;
  277. #ifdef VELOCIKEY_ENABLE
  278. case VLK_TOG:
  279. velocikey_toggle();
  280. return false;
  281. #endif
  282. #ifdef BLUETOOTH_ENABLE
  283. case OUT_AUTO:
  284. set_output(OUTPUT_AUTO);
  285. return false;
  286. case OUT_USB:
  287. set_output(OUTPUT_USB);
  288. return false;
  289. case OUT_BT:
  290. set_output(OUTPUT_BLUETOOTH);
  291. return false;
  292. #endif
  293. #ifndef NO_ACTION_ONESHOT
  294. case ONESHOT_TOGGLE:
  295. oneshot_toggle();
  296. break;
  297. case ONESHOT_ENABLE:
  298. oneshot_enable();
  299. break;
  300. case ONESHOT_DISABLE:
  301. oneshot_disable();
  302. break;
  303. #endif
  304. }
  305. }
  306. return process_action_kb(record);
  307. }
  308. void set_single_persistent_default_layer(uint8_t default_layer) {
  309. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  310. PLAY_SONG(default_layer_songs[default_layer]);
  311. #endif
  312. eeconfig_update_default_layer((layer_state_t)1 << default_layer);
  313. default_layer_set((layer_state_t)1 << default_layer);
  314. }
  315. layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  316. layer_state_t mask12 = ((layer_state_t)1 << layer1) | ((layer_state_t)1 << layer2);
  317. layer_state_t mask3 = (layer_state_t)1 << layer3;
  318. return (state & mask12) == mask12 ? (state | mask3) : (state & ~mask3);
  319. }
  320. 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)); }
  321. void matrix_init_quantum() {
  322. magic();
  323. #if defined(LED_NUM_LOCK_PIN) || defined(LED_CAPS_LOCK_PIN) || defined(LED_SCROLL_LOCK_PIN) || defined(LED_COMPOSE_PIN) || defined(LED_KANA_PIN)
  324. // TODO: remove calls to led_init_ports from keyboards and remove ifdef
  325. led_init_ports();
  326. #endif
  327. #ifdef BACKLIGHT_ENABLE
  328. backlight_init_ports();
  329. #endif
  330. #ifdef AUDIO_ENABLE
  331. audio_init();
  332. #endif
  333. #ifdef LED_MATRIX_ENABLE
  334. led_matrix_init();
  335. #endif
  336. #ifdef RGB_MATRIX_ENABLE
  337. rgb_matrix_init();
  338. #endif
  339. #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
  340. unicode_input_mode_init();
  341. #endif
  342. #ifdef HAPTIC_ENABLE
  343. haptic_init();
  344. #endif
  345. #if defined(BLUETOOTH_ENABLE) && defined(OUTPUT_AUTO_ENABLE)
  346. set_output(OUTPUT_AUTO);
  347. #endif
  348. matrix_init_kb();
  349. }
  350. void matrix_scan_quantum() {
  351. #if defined(AUDIO_ENABLE)
  352. // There are some tasks that need to be run a little bit
  353. // after keyboard startup, or else they will not work correctly
  354. // because of interaction with the USB device state, which
  355. // may still be in flux...
  356. //
  357. // At the moment the only feature that needs this is the
  358. // startup song.
  359. static bool delayed_tasks_run = false;
  360. static uint16_t delayed_task_timer = 0;
  361. if (!delayed_tasks_run) {
  362. if (!delayed_task_timer) {
  363. delayed_task_timer = timer_read();
  364. } else if (timer_elapsed(delayed_task_timer) > 300) {
  365. audio_startup();
  366. delayed_tasks_run = true;
  367. }
  368. }
  369. #endif
  370. #if defined(AUDIO_ENABLE) && !defined(NO_MUSIC_MODE)
  371. matrix_scan_music();
  372. #endif
  373. #ifdef KEY_OVERRIDE_ENABLE
  374. matrix_scan_key_override();
  375. #endif
  376. #ifdef SEQUENCER_ENABLE
  377. matrix_scan_sequencer();
  378. #endif
  379. #ifdef TAP_DANCE_ENABLE
  380. matrix_scan_tap_dance();
  381. #endif
  382. #ifdef COMBO_ENABLE
  383. matrix_scan_combo();
  384. #endif
  385. #ifdef LED_MATRIX_ENABLE
  386. led_matrix_task();
  387. #endif
  388. #ifdef WPM_ENABLE
  389. decay_wpm();
  390. #endif
  391. #ifdef HAPTIC_ENABLE
  392. haptic_task();
  393. #endif
  394. #ifdef DIP_SWITCH_ENABLE
  395. dip_switch_read(false);
  396. #endif
  397. #ifdef AUTO_SHIFT_ENABLE
  398. autoshift_matrix_scan();
  399. #endif
  400. matrix_scan_kb();
  401. }
  402. #ifdef HD44780_ENABLED
  403. # include "hd44780.h"
  404. #endif
  405. void api_send_unicode(uint32_t unicode) {
  406. #ifdef API_ENABLE
  407. uint8_t chunk[4];
  408. dword_to_bytes(unicode, chunk);
  409. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  410. #endif
  411. }
  412. //------------------------------------------------------------------------------
  413. // Override these functions in your keymap file to play different tunes on
  414. // different events such as startup and bootloader jump
  415. __attribute__((weak)) void startup_user() {}
  416. __attribute__((weak)) void shutdown_user() {}