libevdevxx 0.4.0
A C++ wrapper for libevdev.
Loading...
Searching...
No Matches
Device.hpp
Go to the documentation of this file.
1/*
2 * libevdevxx - a C++ wrapper for libevdev
3 *
4 * Copyright (C) 2021-2023 Daniel K. O.
5 * SPDX-License-Identifier: MIT
6 */
7
8
9#ifndef LIBEVDEVXX_DEVICE_HPP
10#define LIBEVDEVXX_DEVICE_HPP
11
12
13#include <cstdarg>
14#include <cstdint>
15#include <filesystem>
16#include <memory>
17#include <optional>
18#include <string>
19#include <vector>
20
21#include <libevdev/libevdev.h>
22
23#include "AbsInfo.hpp"
24#include "Event.hpp"
25#include "Property.hpp"
26#include "ReadFlag.hpp"
27#include "ReadStatus.hpp"
28#include "TypeCode.hpp"
29
30#include "detail/ScopedFile.hpp"
31
33namespace evdev {
34
35
37 enum class LogPriority {
38 none = -1,
39 error = LIBEVDEV_LOG_ERROR,
40 info = LIBEVDEV_LOG_INFO,
41 debug = LIBEVDEV_LOG_DEBUG
42 };
43
44
52 class Device {
53
54 struct Deleter {
55 void operator()(::libevdev* dev) const noexcept;
56 };
57
58 std::unique_ptr<::libevdev, Deleter> dev;
59
60 detail::ScopedFile owned_file;
61
62
63 // ------------------------- //
64 // Initialization and setup. //
65 // ------------------------- //
66
67 protected:
68
78
92 int filedes);
93
108 const std::filesystem::path& filename,
109 int flags = O_RDONLY | O_NONBLOCK);
110
111
112 public:
113
127
133 Device(int filedes);
134
138 Device(const std::filesystem::path& filename,
139 int flags = O_RDONLY | O_NONBLOCK);
140
141 virtual ~Device() noexcept;
142
143
145 ::libevdev* data() noexcept;
146
148 const ::libevdev* data() const noexcept;
149
150
160 void grab();
161
169 void ungrab();
170
171
177 void fd (int fd);
178
185 void change_fd(int fd);
186
188 int fd() const;
189
190
191 void open(const std::filesystem::path& filename,
192 int flags = O_RDONLY | O_NONBLOCK);
193 // only valid if Device opened the file, not if given a fd.
194 bool is_open() const noexcept;
195 void close() noexcept;
196 void nonblock(bool enable);
197
198
199 // ------------------ //
200 // Logging facilities //
201 // ------------------ //
202
203 virtual
204 void
206 const char* file, int line, const char* func,
207 const char* format, std::va_list args)
208 const;
209
210
211
212 // --------------------- //
213 // Querying capabilities //
214 // --------------------- //
215
216 std::string name() const;
217 std::optional<std::string> phys() const;
218 std::optional<std::string> uniq() const;
219
220 std::uint16_t product() const noexcept;
221 std::uint16_t vendor() const noexcept;
222 std::uint16_t bustype() const noexcept;
223 std::uint16_t version() const noexcept;
224
225 int driver_version() const noexcept;
226
227
228 bool has(Property prop) const noexcept;
229
230
231 bool has(Type type) const noexcept;
232 bool has(Type type, Code code) const noexcept;
233 bool has(const TypeCode& tc) const noexcept;
234
235 int abs_min (Code code) const noexcept;
236 int abs_max (Code code) const noexcept;
237 int abs_fuzz(Code code) const noexcept;
238 int abs_flat(Code code) const noexcept;
239 int abs_res (Code code) const noexcept;
240
241 AbsInfo abs_info(Code code) const;
242
243 int get(Type type, Code code) const noexcept;
244 int get(const TypeCode& tc) const noexcept;
245
246 std::optional<int> fetch(Type type, Code code) const noexcept;
247 std::optional<int> fetch(const TypeCode& tc) const noexcept;
248
249 bool get_repeat(int& delay, int& period) const noexcept;
250
251
252
253 // ----------------------------- //
254 // Multi-touch related functions //
255 // ----------------------------- //
256
257 int
258 get_slot(unsigned slot,
259 Code code)
260 const noexcept;
261
262 std::optional<int>
263 fetch_slot(unsigned slot,
264 Code code)
265 const noexcept;
266
267 std::optional<int>
269 const noexcept;
270
271 int
273 const noexcept;
274
275
276
277 // ---------------------------------------- //
278 // Modifying the appearance or capabilities //
279 // ---------------------------------------- //
280
281 void name(const std::string& n);
282 void phys(const std::string& phys);
283 void uniq(const std::string& uniq);
284
285 void product (std::uint16_t pid);
286 void vendor (std::uint16_t vid);
287 void bustype (std::uint16_t bus);
288 void version (std::uint16_t ver);
289
290 void enable(Property prop);
291 void disable(Property prop);
292
293 void set(Type type, Code code, int value);
294 void set(const TypeCode& tc, int value);
295
296 void set_slot(unsigned slot, Code code, int value);
297
298 void abs_min (Code code, int val);
299 void abs_max (Code code, int val);
300 void abs_fuzz(Code code, int val);
301 void abs_flat(Code code, int val);
302 void abs_res (Code code, int val);
303
304 void abs_info(Code code, const AbsInfo& abs);
305
306 void enable(Type type);
307 void disable(Type type);
308
309 void enable(Type type, Code code);
310 void enable(const TypeCode& tc);
311
312 void enable_abs(Code code, const AbsInfo& info);
313 void enable_rep(Code code, int arg);
314
315 void disable(Type type, Code code);
316 void disable(const TypeCode& tc);
317
318 void kernel_abs_info(Code code, const AbsInfo& abs);
319 void kernel_led_value(Code code, ::libevdev_led_value value);
320
321 void set_clock_id(int clockid);
322
323
324
325 // -------------- //
326 // Event handling //
327 // -------------- //
328
329 Event
331
333 read(Event& event,
334 ReadFlag flags = ReadFlag::normal)
335 noexcept;
336
337 bool pending();
338
339
340 // ------------------- //
341 // Convenience methods //
342 // ------------------- //
343
344 std::vector<Property>
346 const;
347
348 std::vector<Type>
350 const;
351
352 std::vector<Code>
354 const;
355
356 std::vector<Code>
358 Code max)
359 const;
360
361
362 void enable_key(Code code);
363 void enable_rel(Code code);
364 void enable_abs(Code code);
365
366
367 }; // class Device
368
369
370} // namespace evdev
371
372
373#endif
Represents a device (real of not).
Definition: Device.hpp:52
std::vector< Type > types() const
int abs_res(Code code) const noexcept
bool has(Property prop) const noexcept
std::uint16_t product() const noexcept
Device(LogPriority priority, int filedes)
Constructor to enable logging, and a non-owning device file descriptor.
std::optional< int > num_slots() const noexcept
void set_clock_id(int clockid)
std::string name() const
virtual void log(LogPriority p, const char *file, int line, const char *func, const char *format, std::va_list args) const
void disable(Property prop)
std::vector< Code > codes(Type type) const
void close() noexcept
void kernel_led_value(Code code, ::libevdev_led_value value)
int abs_flat(Code code) const noexcept
void set_slot(unsigned slot, Code code, int value)
Device(const std::filesystem::path &filename, int flags=O_RDONLY|O_NONBLOCK)
Construct from a device file path.
int abs_max(Code code) const noexcept
void nonblock(bool enable)
std::optional< std::string > phys() const
std::uint16_t version() const noexcept
void enable_key(Code code)
std::vector< Property > properties() const
AbsInfo abs_info(Code code) const
::libevdev * data() noexcept
Access the internal ::libevdev object.
int get_slot(unsigned slot, Code code) const noexcept
void enable(Property prop)
int driver_version() const noexcept
bool is_open() const noexcept
std::uint16_t bustype() const noexcept
std::uint16_t vendor() const noexcept
void kernel_abs_info(Code code, const AbsInfo &abs)
std::optional< std::string > uniq() const
int get(Type type, Code code) const noexcept
int abs_min(Code code) const noexcept
Device(LogPriority priority, const std::filesystem::path &filename, int flags=O_RDONLY|O_NONBLOCK)
Constructor to enable logging and a device filename.
void enable_abs(Code code, const AbsInfo &info)
Device(LogPriority priority)
Constructor to enable logging.
void change_fd(int fd)
Change the file descriptor used internally, without re-reading the actual device.
int abs_fuzz(Code code) const noexcept
virtual ~Device() noexcept
Device(int filedes)
Construct form a device file descriptor. The file descriptor is not owned.
void open(const std::filesystem::path &filename, int flags=O_RDONLY|O_NONBLOCK)
bool get_repeat(int &delay, int &period) const noexcept
int fd() const
Return the internal file descriptor used to access the device file.
void set(Type type, Code code, int value)
Event read(ReadFlag flags=ReadFlag::normal)
void enable_rel(Code code)
void ungrab()
Ungrab the device.
void grab()
Grab the device through a EVIOCGRAB syscall.
std::optional< int > fetch_slot(unsigned slot, Code code) const noexcept
void enable_rep(Code code, int arg)
int current_slot() const noexcept
std::optional< int > fetch(Type type, Code code) const noexcept
Device()
Default constructor.
Definition: Property.hpp:28
Definition: Type.hpp:28
The namespace of libevdevxx.
Definition: AbsInfo.hpp:20
ReadFlag
Definition: ReadFlag.hpp:22
@ normal
Definition: ReadFlag.hpp:23
LogPriority
Helper enum to use the logging functions.
Definition: Device.hpp:37
ReadStatus
Definition: ReadStatus.hpp:23
Definition: TypeCode.hpp:23
Maps to the input_absinfo struct from linux/input.h.
Definition: AbsInfo.hpp:23
Type-safe class for evdev codes (KEY_*, BTN_*, REL_*, etc).
Definition: Code.hpp:32
Definition: Event.hpp:30