-
Notifications
You must be signed in to change notification settings - Fork 6
/
evdevPlus.hpp
119 lines (87 loc) · 2.54 KB
/
evdevPlus.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
This file is part of libevdevPlus.
Copyright (C) 2018-2021 Reimu NotMoe <reimu@sudomaker.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the MIT License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#pragma once
#include "CommonIncludes.hpp"
#include "InputEvent.hpp"
namespace evdevPlus {
extern const std::unordered_map<std::string, int> Table_FunctionKeys;
extern const std::unordered_map<std::string, int> Table_ModifierKeys;
extern const std::unordered_map<char, int> Table_LowerKeys;
extern const std::unordered_map<char, int> Table_UpperKeys;
extern const std::unordered_map<std::string, int> Table_KeyCodes;
class EventDeviceID {
public:
input_id inputId{};
uint16_t &BusType = inputId.bustype;
uint16_t &Vendor = inputId.vendor;
uint16_t &Product = inputId.product;
uint16_t &Version = inputId.version;
EventDeviceID() = default;
EventDeviceID(uint16_t bus_type, uint16_t vid, uint16_t pid, uint16_t version) {
BusType = bus_type;
Vendor = vid;
Product = pid;
Version = version;
}
friend void swap(EventDeviceID &first, EventDeviceID &second) {
using std::swap;
swap(first.inputId, second.inputId);
}
EventDeviceID(EventDeviceID &&other) noexcept : EventDeviceID() {
swap(*this, other);
}
EventDeviceID& operator= (EventDeviceID other) {
swap(*this, other);
return *this;
}
EventDeviceID(const EventDeviceID &other) {
memcpy(&inputId, &(other.inputId), sizeof(input_id));
}
};
class EventDevice {
private:
int fd_ = -1;
std::string path_;
int DriverVersion = -1;
EventDeviceID DeviceID;
std::string DeviceName;
std::set<int> EventTypes;
void __open(const std::string &path, int open_flags = O_RDONLY);
void __init();
void __close();
public:
EventDevice() = default;
EventDevice(const std::string &path, int open_flags = O_RDONLY) {
__open(path, open_flags);
__init();
}
~EventDevice() {
__close();
}
int fd() const noexcept {
return fd_;
}
int driver_version() const noexcept {
return DriverVersion;
}
const EventDeviceID& device_id() const noexcept {
return DeviceID;
}
const std::string& device_name() const noexcept {
return DeviceName;
}
void grab();
void ungrab();
InputEvent read();
bool const operator== (const EventDevice &o) const {
return (path_ == o.path_) && (fd_ == o.fd_);
}
};
}