Input.h 985 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #pragma once
  2. #include <input/input.h>
  3. #include "List.h"
  4. class InputEventHandler {
  5. struct Subscription {
  6. void *ctx;
  7. size_t id;
  8. void (*callback)(void *ctx, int, InputType);
  9. };
  10. List<Subscription> events;
  11. size_t _id = 0;
  12. public:
  13. InputEventHandler() {
  14. events = List<Subscription>();
  15. }
  16. ~InputEventHandler() {
  17. events.deleteData();
  18. events.clear();
  19. }
  20. int subscribe(void *caller, void(*p)(void *, int, InputType)) {
  21. size_t currID=_id;
  22. _id++;
  23. events.push_back(new Subscription{.ctx = caller, .id=currID, .callback=p});
  24. return currID;
  25. }
  26. void unsubscribe(size_t id) {
  27. for(auto item : events){
  28. if (item->id == id) {
  29. events.remove(item);
  30. return;
  31. }
  32. }
  33. }
  34. void Set(int key, InputType type) {
  35. for (auto *evt: events) {
  36. evt->callback(evt->ctx, key, type);
  37. }
  38. }
  39. };