forked from Grumbel/udraw
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevdev.hpp
138 lines (115 loc) · 3 KB
/
evdev.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Linux driver for the uDraw graphic tablet
// Copyright (C) 2012 Ingo Ruhnke <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// 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. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef HEADER_EVDEV_HPP
#define HEADER_EVDEV_HPP
#include <sstream>
class Evdev
{
private:
int m_fd;
uinput_user_dev m_user_dev;
bool ev_abs_bit;
bool ev_rel_bit;
bool ev_key_bit;
public:
Evdev() :
m_fd(),
m_user_dev(),
ev_abs_bit(false),
ev_rel_bit(false),
ev_key_bit(false)
{
m_fd = open("/dev/uinput", O_RDWR | O_NDELAY);
if (m_fd < 0)
{
throw std::runtime_error(strerror(errno));
}
memset(&m_user_dev, 0, sizeof(m_user_dev));
}
void finish()
{
strncpy(m_user_dev.name, "uDraw Game Tablet for PS3", UINPUT_MAX_NAME_SIZE);
m_user_dev.id.version = 0;
m_user_dev.id.bustype = 0;
m_user_dev.id.vendor = 0;
m_user_dev.id.product = 0;
if (write(m_fd, &m_user_dev, sizeof(m_user_dev)) < 0)
{
std::ostringstream out;
out << "write: " << strerror(errno);
throw std::runtime_error(out.str());
}
if (ioctl(m_fd, UI_DEV_CREATE) < 0)
{
std::ostringstream out;
out << "ioctl: " << strerror(errno);
throw std::runtime_error(out.str());
}
}
void send(uint16_t type, uint16_t code, int32_t value)
{
struct input_event ev;
memset(&ev, 0, sizeof(ev));
gettimeofday(&ev.time, NULL);
ev.type = type;
ev.code = code;
if (ev.type == EV_KEY)
{
ev.value = (value>0) ? 1 : 0;
}
else
{
ev.value = value;
}
if (write(m_fd, &ev, sizeof(ev)) < 0)
throw std::runtime_error(std::string("uinput:send_button: ") + strerror(errno));
// sync: send(EV_SYN, SYN_REPORT, 0);
}
public:
void add_key(int code)
{
if (!ev_key_bit)
{
ev_key_bit = true;
ioctl(m_fd, UI_SET_EVBIT, EV_KEY);
}
ioctl(m_fd, UI_SET_KEYBIT, code);
}
void add_rel(int code)
{
if (!ev_rel_bit)
{
ev_rel_bit = true;
ioctl(m_fd, UI_SET_EVBIT, EV_REL);
}
ioctl(m_fd, UI_SET_RELBIT, code);
}
void add_abs(int code, int min, int max, int fuzz, int flat)
{
if (!ev_abs_bit)
{
ev_abs_bit = true;
ioctl(m_fd, UI_SET_EVBIT, EV_ABS);
}
m_user_dev.absmin[code] = min;
m_user_dev.absmax[code] = max;
m_user_dev.absfuzz[code] = fuzz;
m_user_dev.absflat[code] = flat;
ioctl(m_fd, UI_SET_ABSBIT, code);
}
};
#endif
/* EOF */