forked from krnlyng/sfdroid_renderer
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathuinput.cpp
More file actions
190 lines (163 loc) · 4.87 KB
/
uinput.cpp
File metadata and controls
190 lines (163 loc) · 4.87 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* this file is part of sfdroid
* Copyright (C) 2015, Franz-Josef Haider <f_haider@gmx.at>
* based on harmattandroid by Thomas Perl
*
* 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/>.
*
*/
#include <iostream>
#include <linux/input.h>
#include <linux/uinput.h>
#include <sys/time.h>
#include <fcntl.h>
#include "sfdroid_defs.h"
#include "uinput.h"
#include "utility.h"
using namespace std;
int uinput_t::init(int win_width, int win_height)
{
int err = 0;
#if DEBUG
cout << "setting up uinput" << endl;
#endif
// try different uinput device nodes
int errnos[3];
fd_uinput = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
errnos[0] = errno;
if(fd_uinput < 0)
{
fd_uinput = open("/dev/input/uinput", O_WRONLY | O_NONBLOCK);
errnos[1] = errno;
}
if(fd_uinput < 0)
{
fd_uinput = open("/dev/misc/uinput", O_WRONLY | O_NONBLOCK);
errnos[2] = errno;
}
if(fd_uinput < 0)
{
cerr << "failed to open uinput device:" << endl;
cerr << "/dev/uinput: " << strerror(errnos[0]) << endl;
cerr << "/dev/input/uinput: " << strerror(errnos[1]) << endl;
cerr << "/dev/misc/uinput: " << strerror(errnos[2]) << endl;
for(unsigned int i=0;i<sizeof(errnos)/sizeof(int);i++)
{
if(errnos[i] == EACCES)
{
cerr << "wrong permissions, allow nemo to use uinput." << endl;
break;
}
}
err = 17;
goto quit;
}
if(ioctl(fd_uinput, UI_SET_EVBIT, EV_SYN) < 0)
{
cerr << "UI_SET_EVBIT EV_SYN ioctl failed" << endl;
err = 20;
goto quit;
}
if(ioctl(fd_uinput, UI_SET_EVBIT, EV_ABS) < 0)
{
cerr << "UI_SET_EVBIT EV_ABS ioctl failed" << endl;
err = 21;
goto quit;
}
if(ioctl(fd_uinput, UI_SET_ABSBIT, ABS_MT_TRACKING_ID) < 0)
{
cerr << "UI_SET_ABSBIT ABS_MT_TRACKING_ID ioctl failed" << endl;
err = 22;
goto quit;
}
if(ioctl(fd_uinput, UI_SET_ABSBIT, ABS_MT_SLOT) < 0)
{
cerr << "UI_SET_ABSBIT ABS_MT_SLOT ioctl failed" << endl;
err = 23;
goto quit;
}
if(ioctl(fd_uinput, UI_SET_ABSBIT, ABS_MT_POSITION_X) < 0)
{
cerr << "UI_SET_ABSBIT ABS_MT_POSITION_X ioctl failed" << endl;
err = 24;
goto quit;
}
if(ioctl(fd_uinput, UI_SET_ABSBIT, ABS_MT_POSITION_Y) < 0)
{
cerr << "UI_SET_ABSBIT ABS_MT_POSITION_Y ioctl failed" << endl;
err = 25;
goto quit;
}
if(ioctl(fd_uinput, UI_SET_ABSBIT, ABS_MT_PRESSURE) < 0)
{
cerr << "UI_SET_ABSBIT ABS_MT_PRESSURE ioctl failed" << endl;
err = 28;
goto quit;
}
if(ioctl(fd_uinput, UI_SET_PROPBIT, INPUT_PROP_DIRECT) < 0)
{
cerr << "UI_SET_PROPBIT INPUT_PROP_DIRECT ioctl failed" << endl;
err = 26;
goto quit;
}
memset(&uidev, 0, sizeof(uidev));
snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "sfdroid-input");
uidev.absmin[ABS_MT_POSITION_X] = 0;
uidev.absmax[ABS_MT_POSITION_X] = win_width;
uidev.absmin[ABS_MT_POSITION_Y] = 0;
uidev.absmax[ABS_MT_POSITION_Y] = win_height;
uidev.absmin[ABS_MT_PRESSURE] = 0;
uidev.absmax[ABS_MT_PRESSURE] = MAX_PRESSURE;
// hmm
uidev.absmax[ABS_MT_SLOT] = 255;
uidev.absmax[ABS_MT_TRACKING_ID] = 255;
uidev.id.bustype = BUS_VIRTUAL;
// hmm
uidev.id.vendor = 0x1;
uidev.id.product = 0x1;
uidev.id.version = 1;
if(write(fd_uinput, &uidev, sizeof(uidev)) < 0)
{
cerr << "failed to write sfdroid-input structure to uinput: " << strerror(errno) << endl;
err = 18;
goto quit;
}
if(ioctl(fd_uinput, UI_DEV_CREATE) < 0)
{
cerr << "failed to create sfdroid-input uinput device: " << strerror(errno) << endl;
err = 19;
goto quit;
}
uinput_dev_created = 1;
quit:
return err;
}
int uinput_t::send_event(int type, int code, int value)
{
struct input_event ev;
gettimeofday(&ev.time, NULL);
ev.type = type;
ev.code = code;
ev.value = value;
if(write(fd_uinput, &ev, sizeof(ev)) < 0)
{
return 0;
}
return 1;
}
void uinput_t::deinit()
{
if(uinput_dev_created) ioctl(fd_uinput, UI_DEV_DESTROY);
if(fd_uinput >= 0) close(fd_uinput);
}