RADLib
RADical C++ application framework
RADInput.h
Go to the documentation of this file.
1 #ifndef RADINPUT_H
2 #define RADINPUT_H
3 
9 #pragma once
10 
11 #include <RADCore/RADCore.h>
12 
13 #include <atomic>
14 #include <cstdint>
15 #include <memory>
16 #include <string>
17 #include <thread>
18 #include <vector>
19 
20 namespace RADInput {
21 
23  struct InputPaths {
25  std::string devInput = "/dev/input";
27  std::string sysClassInput = "/sys/class/input";
28  };
29 
31  struct InputDeviceInfo {
33  std::string path;
35  std::string name;
37  std::string sysfsPath;
38  };
39 
41  struct InputEvent {
43  std::string devicePath;
45  int64_t seconds = 0;
47  int64_t microseconds = 0;
49  uint16_t type = 0;
51  uint16_t code = 0;
53  int32_t value = 0;
54  };
55 
57  struct Calibration {
59  double inMinX = 0.0;
61  double inMaxX = 1.0;
63  double inMinY = 0.0;
65  double inMaxY = 1.0;
67  double outMinX = 0.0;
69  double outMaxX = 1.0;
71  double outMinY = 0.0;
73  double outMaxY = 1.0;
75  bool invertX = false;
77  bool invertY = false;
78 
80  std::pair<double, double> map(double x, double y) const;
81  };
82 
85  public:
87  explicit InputManager(InputPaths paths = {}, RADCore::EventLoop* loop = nullptr);
90 
92  std::vector<InputDeviceInfo> discover() const;
94  bool startDevice(const std::string& path, std::string* error = nullptr);
96  void startHotplugMonitor(int intervalMs = 1000);
100  void stopAll();
102  bool isRunning() const;
104  const InputPaths& paths() const;
105 
106  RADEvents:
110  RAD_EVENT(devicesChanged, (const std::vector<RADInput::InputDeviceInfo>&));
111 
112  private:
113  struct Reader;
114  void publishEvent(InputEvent event);
115 
116  InputPaths paths_;
117  RADCore::EventLoop* loop_ = nullptr;
118  std::vector<std::unique_ptr<Reader>> readers_;
119  std::atomic<bool> hotplugRunning_{false};
120  std::thread hotplugThread_;
121  };
122 
123 } // namespace RADInput
124 
125 #endif
#define RAD_EVENT(Name, Signature)
Declares a typed RADCore event; example: RAD_EVENT(done, (int)).
Definition: RADCore.h:65
#define RADEvents
Marks a public event declaration block in RADObject-derived classes.
Definition: RADCore.h:43
RADCore task event loop with locked and low-latency scheduling strategies.
Definition: RADCore.h:956
Base class for event receivers, senders, and thread-affinity aware objects.
Definition: RADCore.h:931
Input manager that reads evdev events and emits RADCore events.
Definition: RADInput.h:84
RADCore::Event< void(const std::vector< RADInput::InputDeviceInfo > &) > devicesChanged
Emitted when discovery finds a changed device list.
Definition: RADInput.h:110
const InputPaths & paths() const
Returns the configured paths.
bool startDevice(const std::string &path, std::string *error=nullptr)
Starts reading a single evdev event path.
void stopHotplugMonitor()
Stops the hotplug monitor thread.
void stopAll()
Stops all active readers.
InputManager(InputPaths paths={}, RADCore::EventLoop *loop=nullptr)
Creates a manager using paths and optional target event loop.
void startHotplugMonitor(int intervalMs=1000)
Starts lightweight polling hotplug detection and emits devicesChanged on changes.
~InputManager()
Stops all readers.
RADCore::Event< void(const RADInput::InputEvent &) > eventReceived
Emitted when a raw input event is read.
Definition: RADInput.h:108
bool isRunning() const
Returns true when at least one device reader is active.
std::vector< InputDeviceInfo > discover() const
Discovers event devices from devInput and sysClassInput.
Definition: RADInput.h:20
2D touch/pointer calibration transform.
Definition: RADInput.h:57
double inMinX
Input minimum X.
Definition: RADInput.h:59
double outMinX
Output minimum X.
Definition: RADInput.h:67
bool invertY
Inverts Y after scaling.
Definition: RADInput.h:77
double inMinY
Input minimum Y.
Definition: RADInput.h:63
std::pair< double, double > map(double x, double y) const
Maps raw coordinates through the calibration transform.
double outMaxX
Output maximum X.
Definition: RADInput.h:69
double outMinY
Output minimum Y.
Definition: RADInput.h:71
double outMaxY
Output maximum Y.
Definition: RADInput.h:73
double inMaxY
Input maximum Y.
Definition: RADInput.h:65
double inMaxX
Input maximum X.
Definition: RADInput.h:61
bool invertX
Inverts X after scaling.
Definition: RADInput.h:75
Discovered evdev device.
Definition: RADInput.h:31
std::string path
Device node path.
Definition: RADInput.h:33
std::string sysfsPath
Sysfs event directory path when available.
Definition: RADInput.h:37
std::string name
Kernel/sysfs name when available.
Definition: RADInput.h:35
Raw Linux input_event translated into RADInput form.
Definition: RADInput.h:41
std::string devicePath
Device path that produced the event.
Definition: RADInput.h:43
int32_t value
Linux event value.
Definition: RADInput.h:53
int64_t seconds
Event seconds timestamp.
Definition: RADInput.h:45
uint16_t type
Linux event type.
Definition: RADInput.h:49
uint16_t code
Linux event code.
Definition: RADInput.h:51
int64_t microseconds
Event microseconds timestamp.
Definition: RADInput.h:47
Filesystem roots used for real devices or tests.
Definition: RADInput.h:23
std::string sysClassInput
Sysfs input class directory.
Definition: RADInput.h:27
std::string devInput
Directory containing event nodes.
Definition: RADInput.h:25