process_leader.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Copyright 2016 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. #ifdef LEADER_ENABLE
  17. #include "process_leader.h"
  18. #include <string.h>
  19. #ifndef LEADER_TIMEOUT
  20. #define LEADER_TIMEOUT 300
  21. #endif
  22. __attribute__ ((weak))
  23. void leader_start(void) {}
  24. __attribute__ ((weak))
  25. void leader_end(void) {}
  26. // Leader key stuff
  27. bool leading = false;
  28. uint16_t leader_time = 0;
  29. uint16_t leader_sequence[5] = {0, 0, 0, 0, 0};
  30. uint8_t leader_sequence_size = 0;
  31. void qk_leader_start(void) {
  32. if (leading) { return; }
  33. leader_start();
  34. leading = true;
  35. leader_time = timer_read();
  36. leader_sequence_size = 0;
  37. memset(leader_sequence, 0, sizeof(leader_sequence));
  38. }
  39. bool process_leader(uint16_t keycode, keyrecord_t *record) {
  40. // Leader key set-up
  41. if (record->event.pressed) {
  42. if (leading) {
  43. if (timer_elapsed(leader_time) < LEADER_TIMEOUT) {
  44. #ifndef LEADER_KEY_STRICT_KEY_PROCESSING
  45. if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
  46. keycode = keycode & 0xFF;
  47. }
  48. #endif // LEADER_KEY_STRICT_KEY_PROCESSING
  49. if ( leader_sequence_size < ( sizeof(leader_sequence) / sizeof(leader_sequence[0]) ) ) {
  50. leader_sequence[leader_sequence_size] = keycode;
  51. leader_sequence_size++;
  52. } else {
  53. leading = false;
  54. leader_end();
  55. }
  56. #ifdef LEADER_PER_KEY_TIMING
  57. leader_time = timer_read();
  58. #endif
  59. return false;
  60. }
  61. } else {
  62. if (keycode == KC_LEAD) {
  63. qk_leader_start();
  64. }
  65. }
  66. }
  67. return true;
  68. }
  69. #endif