process_leader.c 2.2 KB

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