process_leader.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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)) void leader_start(void) {}
  23. __attribute__((weak)) void leader_end(void) {}
  24. // Leader key stuff
  25. bool leading = false;
  26. uint16_t leader_time = 0;
  27. uint16_t leader_sequence[5] = {0, 0, 0, 0, 0};
  28. uint8_t leader_sequence_size = 0;
  29. void qk_leader_start(void) {
  30. if (leading) {
  31. return;
  32. }
  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. # ifndef LEADER_NO_TIMEOUT
  44. if (timer_elapsed(leader_time) < LEADER_TIMEOUT)
  45. # endif // LEADER_NO_TIMEOUT
  46. {
  47. # ifndef LEADER_KEY_STRICT_KEY_PROCESSING
  48. if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
  49. keycode = keycode & 0xFF;
  50. }
  51. # endif // LEADER_KEY_STRICT_KEY_PROCESSING
  52. if (leader_sequence_size < (sizeof(leader_sequence) / sizeof(leader_sequence[0]))) {
  53. leader_sequence[leader_sequence_size] = keycode;
  54. leader_sequence_size++;
  55. } else {
  56. leading = false;
  57. leader_end();
  58. }
  59. # ifdef LEADER_PER_KEY_TIMING
  60. leader_time = timer_read();
  61. # endif
  62. return false;
  63. }
  64. } else {
  65. if (keycode == KC_LEAD) {
  66. qk_leader_start();
  67. }
  68. }
  69. }
  70. return true;
  71. }
  72. #endif