libevdevxx 0.5.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) 2025 Daniel K. O.
5 * SPDX-License-Identifier: MIT
6 */
7
8#ifndef LIBEVDEVXX_DEVICE_HPP
9#define LIBEVDEVXX_DEVICE_HPP
10
11#include <cstddef>
12#include <cstdarg>
13#include <cstddef>
14#include <cstdint>
15#include <filesystem>
16#include <memory>
17#include <optional>
18#include <string>
19#include <tuple>
20#include <vector>
21
22#include <fcntl.h>
23
24#include <libevdev/libevdev.h>
25
26#include "AbsInfo.hpp"
27#include "basic_wrapper.hpp"
28#include "Event.hpp"
29#include "Property.hpp"
30#include "ReadFlag.hpp"
31#include "ReadStatus.hpp"
32#include "TypeCode.hpp"
33
34
36namespace evdev {
37
45 class Device :
46 public detail::basic_wrapper<libevdev*> {
47
48
49 using BaseType = detail::basic_wrapper<libevdev*>;
50
51 int owned_fd = -1;
52
53 using state_type = std::tuple<BaseType::state_type, int>;
54
55
56 static
57 void
58 log_helper(const libevdev* dev,
59 libevdev_log_priority priority,
60 void* data,
61 const char* file,
62 int line,
63 const char* func,
64 const char* format,
65 std::va_list args)
66 noexcept;
67
68 protected:
69
70
71 // ------------------------- //
72 // Initialization and setup. //
73 // ------------------------- //
74
76 enum class LogLevel {
77 none = -1,
78 error = LIBEVDEV_LOG_ERROR,
79 info = LIBEVDEV_LOG_INFO,
80 debug = LIBEVDEV_LOG_DEBUG
81 };
82
91 Device(LogLevel priority);
92
105 Device(LogLevel priority,
106 int fd);
107
121 Device(LogLevel priority,
122 const std::filesystem::path& filename,
123 int flags = O_RDONLY | O_NONBLOCK);
124
125
134 void
135 create(LogLevel priority);
136
137
151 void
152 create(LogLevel priority,
153 int fd);
154
155
169 void
170 create(LogLevel priority,
171 const std::filesystem::path& filename,
172 int flags = O_RDONLY | O_NONBLOCK);
173
174
175 public:
176
183 Device(std::nullptr_t)
184 noexcept;
185
201
209 Device(int fd);
210
214 Device(const std::filesystem::path& filename,
215 int flags = O_RDONLY | O_NONBLOCK);
216
217
219 noexcept;
220
221
236 void
238
239
247 void
248 create(int fd);
249
250
254 void
255 create(const std::filesystem::path& filename,
256 int flags = O_RDONLY | O_NONBLOCK);
257
258
264 void
266 noexcept override;
267
268
270 Device(Device&& other)
271 noexcept;
272
273
275 Device&
276 operator =(Device&& other)
277 noexcept;
278
279
280 void
281 acquire(libevdev* new_raw,
282 int new_fd)
283 noexcept;
284
285
286 void
287 acquire(state_type new_state)
288 noexcept;
289
290
291 state_type
293 noexcept;
294
295
305 void
307
315 void
317
323 void
324 set_fd(int fd);
325
332 void
333 change_fd(int fd);
334
336 int
338 const;
339
346 void
347 open(const std::filesystem::path& filename,
348 int flags = O_RDONLY | O_NONBLOCK);
349
351 bool
353 const noexcept;
354
356 void
358
359
360 // ------------------ //
361 // Logging facilities //
362 // ------------------ //
363
364 virtual
365 void
366 log(LogLevel priority,
367 const char* file,
368 int line,
369 const char* func,
370 const char* format,
371 std::va_list args)
372 const noexcept;
373
374
375 // --------------------- //
376 // Querying capabilities //
377 // --------------------- //
378
379
380 std::string
382 const;
383
384 std::optional<std::string>
386 const;
387
388 std::optional<std::string>
390 const;
391
392 std::uint16_t
394 const noexcept;
395
396 std::uint16_t
398 const noexcept;
399
400 std::uint16_t
402 const noexcept;
403
404 std::uint16_t
406 const noexcept;
407
408 int
410 const noexcept;
411
412
413 bool
415 const noexcept;
416
417 bool
418 has(Type type)
419 const noexcept;
420
421 bool
422 has(Type type, Code code)
423 const noexcept;
424
425 bool
426 has(const TypeCode& tc)
427 const noexcept;
428
429
430 int
432 const noexcept;
433 int
435 const noexcept;
436 int
438 const noexcept;
439 int
441 const noexcept;
442 int
444 const noexcept;
445
446 AbsInfo
448 const;
449
450 int
452 Code code)
453 const noexcept;
454
455 int
457 const noexcept;
458
459 std::optional<int>
461 Code code)
462 const noexcept;
463
464 std::optional<int>
466 const noexcept;
467
468 bool
469 get_repeat(int& delay,
470 int& period)
471 const noexcept;
472
473
474 struct DelayPeriod {
475 int delay;
477 };
478
479 std::optional<DelayPeriod>
481 const noexcept;
482
483
484 // ----------------------------- //
485 // Multi-touch related functions //
486 // ----------------------------- //
487
488
489 int
490 get_slot(unsigned slot,
491 Code code)
492 const noexcept;
493
494 std::optional<int>
495 try_get_slot(unsigned slot,
496 Code code)
497 const noexcept;
498
499 int
501 const;
502
503 std::optional<int>
505 const noexcept;
506
507 int
509 const noexcept;
510
511
512 // ---------------------------------------- //
513 // Modifying the appearance or capabilities //
514 // ---------------------------------------- //
515
516
517 void
518 set_name(const std::string& name)
519 noexcept;
520
521 void
522 set_phys(const std::string& phys)
523 noexcept;
524
525 void
526 set_uniq(const std::string& uniq)
527 noexcept;
528
529 void
530 set_product(std::uint16_t pid)
531 noexcept;
532
533 void
534 set_vendor(std::uint16_t vid)
535 noexcept;
536
537 void
538 set_bustype(std::uint16_t bus)
539 noexcept;
540
541 void
542 set_version(std::uint16_t ver)
543 noexcept;
544
545 void
547
548 void
550
551 void
553 Code code,
554 int value);
555
556 void
558 int value);
559
560 void
561 set_slot(unsigned slot,
562 Code code,
563 int value);
564
565 void
567 int val)
568 noexcept;
569
570 void
572 int val)
573 noexcept;
574
575 void
577 int val)
578 noexcept;
579
580 void
582 int val)
583 noexcept;
584
585 void
587 int val)
588 noexcept;
589
590 void
592 const AbsInfo& abs)
593 noexcept;
594
595 void
597
598 void
600
601 void
603 Code code);
604
605 void
606 enable(const TypeCode& tc);
607
608 void
610 const AbsInfo& info);
611
612 void
614 int arg);
615
616 void
618 Code code);
619
620 void
621 disable(const TypeCode& tc);
622
623 void
625 const AbsInfo& abs);
626
627 void
629 libevdev_led_value value);
630
631 void
632 set_clock_id(int clockid);
633
634
635 // -------------- //
636 // Event handling //
637 // -------------- //
638
639
640 Event
642
644 read(Event& event,
645 ReadFlag flags = ReadFlag::normal)
646 noexcept;
647
648 bool
650
651
652 // ------------------- //
653 // Convenience methods //
654 // ------------------- //
655
656
657 std::vector<Property>
659 const;
660
661 std::vector<Type>
663 const;
664
665 std::vector<Code>
667 const;
668
669 std::vector<Code>
671 Code max)
672 const;
673
674
675 void
677
678 void
680
681 void
683
684 }; // class Device
685
686} // namespace evdev
687
688#endif
std::uint16_t get_bustype() const noexcept
bool has(Property prop) const noexcept
void set_fd(int fd)
Set a file descriptor and read the device metadata.
std::string get_name() const
void set_abs_res(Code code, int val) noexcept
void acquire(libevdev *new_raw, int new_fd) noexcept
void destroy() noexcept override
free all allocated resources and make the instance invalid.
void set_clock_id(int clockid)
void set_product(std::uint16_t pid) noexcept
int get_abs_fuzz(Code code) const noexcept
state_type release() noexcept
void disable(Property prop)
AbsInfo get_abs_info(Code code) const
void set_kernel_led_value(Code code, libevdev_led_value value)
~Device() noexcept
void set_name(const std::string &name) noexcept
void create(LogLevel priority, const std::filesystem::path &filename, int flags=O_RDONLY|O_NONBLOCK)
Constructor to enable logging and a device filename, in-place.
void set_slot(unsigned slot, Code code, int value)
int get_value(Type type, Code code) const noexcept
int period
Definition Device.hpp:476
void set_abs_info(Code code, const AbsInfo &abs) noexcept
std::vector< Type > get_types() const
Device(const std::filesystem::path &filename, int flags=O_RDONLY|O_NONBLOCK)
Construct from a device file path.
void set_abs_max(Code code, int val) noexcept
void enable_key(Code code)
std::uint16_t get_product() const noexcept
int get_num_slots() const
std::optional< std::string > get_uniq() const
void create(LogLevel priority)
Constructor to enable logging, in-place.
Device(LogLevel priority, const std::filesystem::path &filename, int flags=O_RDONLY|O_NONBLOCK)
Constructor to enable logging and a device filename.
Device(int fd)
Construct form a device file descriptor.
bool has_pending()
std::optional< int > try_get_slot(unsigned slot, Code code) const noexcept
int delay
Definition Device.hpp:475
int get_abs_max(Code code) const noexcept
std::optional< std::string > get_phys() const
void set_abs_min(Code code, int val) noexcept
int get_abs_min(Code code) const noexcept
int get_slot(unsigned slot, Code code) const noexcept
void enable(Property prop)
void set_version(std::uint16_t ver) noexcept
int get_fd() const
Return the internal file descriptor used to access the device file.
bool is_open() const noexcept
Check if a file was opened with open().
void set_abs_fuzz(Code code, int val) noexcept
void set_phys(const std::string &phys) noexcept
std::optional< int > try_get_num_slots() const noexcept
void enable_abs(Code code, const AbsInfo &info)
int get_abs_res(Code code) const noexcept
void change_fd(int fd)
Change the file descriptor used internally, without re-reading the actual device.
Device(LogLevel priority, int fd)
Constructor to enable logging, and a non-owning device file descriptor.
void set_nonblock(bool enable)
Set the owned file handle's O_NONBLOCK flag.
void set_vendor(std::uint16_t vid) noexcept
void create(LogLevel priority, int fd)
Constructor to enable logging, and a non-owning device file descriptor, in-place.
std::uint16_t get_vendor() const noexcept
void set_uniq(const std::string &uniq) noexcept
void set_kernel_abs_info(Code code, const AbsInfo &abs)
int get_driver_version() const noexcept
void open(const std::filesystem::path &filename, int flags=O_RDONLY|O_NONBLOCK)
Open a device file and set it for the device.
LogLevel
Helper enum to use the logging functions.
Definition Device.hpp:76
@ none
Invalid level.
Definition Device.hpp:77
@ debug
Debug level.
Definition Device.hpp:80
@ info
Information level.
Definition Device.hpp:79
@ error
Error level.
Definition Device.hpp:78
int get_current_slot() const noexcept
bool get_repeat(int &delay, int &period) const noexcept
void set_bustype(std::uint16_t bus) noexcept
std::vector< Property > get_properties() const
Event read(ReadFlag flags=ReadFlag::normal)
void enable_rel(Code code)
void ungrab()
Ungrab the device.
std::vector< Code > get_codes(Type type) const
void grab()
Grab the device through a EVIOCGRAB syscall.
void enable_rep(Code code, int arg)
int get_abs_flat(Code code) const noexcept
void set_value(Type type, Code code, int value)
std::uint16_t get_version() const noexcept
std::optional< int > try_get_value(Type type, Code code) const noexcept
std::optional< DelayPeriod > try_get_repeat() const noexcept
Device()
Default constructor.
Device(std::nullptr_t) noexcept
Construct invalid device.
void set_abs_flat(Code code, int val) noexcept
virtual void log(LogLevel priority, const char *file, int line, const char *func, const char *format, std::va_list args) const noexcept
Device(LogLevel priority)
Constructor to enable logging.
Definition Device.hpp:474
Definition Property.hpp:25
Definition Type.hpp:26
Definition basic_wrapper.hpp:21
raw_type data() noexcept
Definition basic_wrapper.hpp:102
The namespace of libevdevxx.
Definition AbsInfo.hpp:18
ReadFlag
Definition ReadFlag.hpp:19
@ normal
Definition ReadFlag.hpp:20
ReadStatus
Definition ReadStatus.hpp:20
Definition TypeCode.hpp:20
Maps to the input_absinfo struct from linux/input.h.
Definition AbsInfo.hpp:21
Type-safe class for evdev codes (KEY_*, BTN_*, REL_*, etc).
Definition Code.hpp:30
Definition Event.hpp:28