quantum.c 13 KB

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