key_repeater.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright 2022 Paul Ewing
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #pragma once
  18. // The key_repeater_config_t type holds user configurable options set when
  19. // allocating a new key repeater.
  20. struct key_repeater_config_t {
  21. // The key code that will be repeatedly registered
  22. int key;
  23. // The minimum amount of time to press down the key when registered
  24. const uint32_t key_duration_min;
  25. // The maximum amount of time to press down the key when registered
  26. const uint32_t key_duration_max;
  27. // The minimum amount of time to wait between registering key presses
  28. const uint32_t wait_duration_min;
  29. // The maximum amount of time to wait between registering key presses
  30. const uint32_t wait_duration_max;
  31. };
  32. // The key_repeater_t type represents a key repeater. This is similar to the
  33. // "Rapid fire" feature on many game controllers. The intention behind this is
  34. // to periodically send a key code while the user is pressing a key.
  35. //
  36. // The duration of the key press as well as the time between key presses is
  37. // slightly randomized by design. This is to simulate more realistic human
  38. // behavior. By setting the minimum and maximum duration fields to the same
  39. // value in the configuration, this randomization can be disabled.
  40. //
  41. // This type is intentionally opaque to avoid the user setting internal fields
  42. // directly. It must be allocated and destroyed using the kr_new() and
  43. // kr_free() functions respectively.
  44. struct key_repeater_t;
  45. // Allocate a new key repeater.
  46. struct key_repeater_t* kr_new(struct key_repeater_config_t* cfg);
  47. // Release an allocated key repeater.
  48. void kr_free(struct key_repeater_t** kr);
  49. // Enable the key repeater such that it will start periodically registering the
  50. // configured key code.
  51. void kr_enable(struct key_repeater_t* kr);
  52. // Disable the key repeater such that it will stop periodically registering the
  53. // configured key code.
  54. void kr_disable(struct key_repeater_t* kr);
  55. // Poll the key repeater to execute, tyically called from matrix_scan_user().
  56. void kr_poll(struct key_repeater_t* kr);