forked from zvxryb/Linux-Virtual-Joystick
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdevice.cpp
More file actions
303 lines (226 loc) · 7.68 KB
/
Copy pathdevice.cpp
File metadata and controls
303 lines (226 loc) · 7.68 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "device.h"
#include "python_include.h"
#include "vjoy.h"
#include <cassert>
#include <errno.h>
#include <fcntl.h>
#include <libudev.h>
#include <iostream>
#include <sstream>
using namespace std;
device* device::dev = NULL;
pthread_mutex_t device::devMutex;
static void vjoy_parse_block(PyObject* info, char* key, int* count, int* array, int max) {
PyObject* items = PyMapping_GetItemString(info, key);
if (items) {
*count = PySequence_Size(items);
if (*count > max) cerr << "looks like there are more items in the " << key << " than allowed (" << max << ")" << endl;
for (int i = 0; i < *count; i++) {
PyObject* item = PySequence_GetItem(items, i);
assert(item);
array[i] = PyLong_AsLong(item);
assert(array[i] >= 0);
Py_DECREF(item);
}
Py_DECREF(items);
}
}
void device::parseInfo() {
memset(&devinfo, 0, sizeof(vjoy_info));
pthread_mutex_lock(&vjoy::pymutex);
//check for callback functions
PyObject* dict = PyModule_GetDict(pymodule);
pause_callback = PyDict_GetItemString(dict, "on_pause");
resume_callback = PyDict_GetItemString(dict, "on_resume");
kill_callback = PyDict_GetItemString(dict, "on_kill");
// Get info
PyObject* pyinfo = PyObject_CallMethod(pymodule, const_cast<char*>("getVJoyInfo"), NULL);
if (PyErr_Occurred()) {
PyErr_Print();
throw(error("python error"));
}
if (!pyinfo) {
err() << "Module has no getVJoyInfo() method." << endl;
return;
}
// Joystick name
PyObject *pyname = PyMapping_GetItemString(pyinfo, const_cast<char*>("name"));
if (pyname) {
char* name = NULL;
#ifdef PYTHON3
int size;
wchar_t* _name = PyUnicode_AsWideCharString(pyname, &size);
wcstombs(name, _name, size);
PyMem_Free(_name);
#else
name = PyString_AsString(pyname);
#endif
if (name) {
// Two names... This is kind of redundant.
strncpy(devinfo.name, name, UINPUT_MAX_NAME_SIZE);
strncpy(uidev.name, name, UINPUT_MAX_NAME_SIZE);
}
Py_DECREF(pyname);
}
// Relative axises
vjoy_parse_block(pyinfo, const_cast<char*>("relaxis"), &devinfo.relaxiscount, devinfo.relaxis, REL_CNT);
// Absolute axises
vjoy_parse_block(pyinfo, const_cast<char*>("absaxis"), &devinfo.absaxiscount, devinfo.absaxis, ABS_CNT);
// Force Feedback effects
vjoy_parse_block(pyinfo, const_cast<char*>("feedback"), &devinfo.feedbackcount, devinfo.feedback, FF_CNT);
PyObject* pymaxeffects = PyMapping_GetItemString(pyinfo, const_cast<char*>("maxeffects"));
if (pymaxeffects) {
devinfo.maxeffects = PyLong_AsLong(pymaxeffects);
Py_DECREF(pymaxeffects);
}
PyObject* pyEnableFF = PyMapping_GetItemString(pyinfo, const_cast<char*>("enable_ff"));
if (pyEnableFF){
enableFF = PyObject_IsTrue(pyEnableFF);
Py_DECREF(pyEnableFF);
}
// Buttons and keys
vjoy_parse_block(pyinfo, const_cast<char*>("buttons"), &devinfo.buttoncount, devinfo.buttons, KEY_CNT);
Py_DECREF(pyinfo);
pthread_mutex_unlock(&vjoy::pymutex);
}
void device::load() {
// Create device
cout << "Initializing device: " << name << ", updated every " << delay / 1000 << " milliseconds" << endl;
// Start up Python
cout << "\tImporting python module " << name << endl << endl;
pthread_mutex_lock(&vjoy::pymutex);
//PyImport_ImportModuleEx()
pymodule = PyImport_ImportModule(name.c_str());
if (PyErr_Occurred()) {
PyErr_Print();
pthread_mutex_unlock(&vjoy::pymutex);
throw(error("Python error in module"));
}
if (!pymodule) {
pthread_mutex_unlock(&vjoy::pymutex);
throw(error(string("Failed to load device ") + name));
}
//add a constant with the device name
PyModule_AddStringConstant(pymodule, "device_name", const_cast<const char*>(name.c_str()));
//add a suicide method to the module
PyMethodDef killMethodDef =
{ "kill_self", device::moduleKillSelf, METH_VARARGS, "kill this virtual joystick" };
PyObject* killFunction = PyCFunction_New(&killMethodDef, NULL);
PyMethod_New(killFunction, NULL, pymodule);
//Py_DECREF(killFunction);
pthread_mutex_unlock(&vjoy::pymutex);
//create a virtual input device file path
udev* _udev;
udev_device* udev_dev;
const char* devnode;
int orig_errno;
_udev = udev_new();
if (!_udev) {
cerr << "errno " << errno << " - " << strerror(errno) << endl;
throw(error("could create a udev"));
}
udev_dev = udev_device_new_from_subsystem_sysname(_udev, "misc", "uinput");
if (!udev_dev) {
cerr << "errno " << errno << " - " << strerror(errno) << endl;
throw(error("could create a udev_device"));
}
devnode = udev_device_get_devnode(udev_dev);
if (!devnode) {
cerr << "errno " << errno << " - " << strerror(errno) << endl;
throw(error("could not get devnoce from udev_dev"));
}
devPath = (char*) malloc(strlen(devnode) + 1);
strcpy(devPath, devnode);
udev_device_unref(udev_dev);
udev_unref(_udev);
//open the device file
cout << "\tInitializing UInput" << endl;
//this read is non-blocking
uifd = open(devPath, O_RDWR | O_NONBLOCK);
if (uifd == -1) {
cerr << "errno " << errno << " - " << strerror(errno) << endl;
throw(error("could not open uinput file"));
}
cout << "\t\tSuccess! (file descriptor is " << uifd << ")" << endl << endl;
// Read device info from the Python module
parseInfo();
// Configure uinput device
uidev.id.bustype = BUS_VIRTUAL;
if (devinfo.relaxiscount > 0) {
ioctl(uifd, UI_SET_EVBIT, EV_REL);
for (int i = 0; i < devinfo.relaxiscount; i++)
ioctl(uifd, UI_SET_RELBIT, devinfo.relaxis[i]);
}
if (devinfo.absaxiscount > 0) {
ioctl(uifd, UI_SET_EVBIT, EV_ABS);
for (int i = 0; i < devinfo.absaxiscount; i++)
ioctl(uifd, UI_SET_ABSBIT, devinfo.absaxis[i]);
}
if (devinfo.feedbackcount > 0) {
ioctl(uifd, UI_SET_EVBIT, EV_FF);
for (int i = 0; i < devinfo.feedbackcount; i++)
ioctl(uifd, UI_SET_FFBIT, devinfo.feedback[i]);
}
if (devinfo.buttoncount > 0) {
ioctl(uifd, UI_SET_EVBIT, EV_KEY);
for (int i = 0; i < devinfo.buttoncount; i++)
ioctl(uifd, UI_SET_KEYBIT, devinfo.buttons[i]);
}
//TODO: make it an option to specify the range on absolute axes (separately)
for (int i = 0; i < ABS_MAX; i++) {
uidev.absmin[i] = SHRT_MIN;
uidev.absmax[i] = SHRT_MAX;
}
uidev.ff_effects_max = devinfo.maxeffects;
errno = 0;
int size = sizeof(uinput_user_dev);
int w = write(uifd, &uidev, size);
//TOOD:this part fails in ubuntu 12.04. find a solution
//but it works in ubuntu 12.10
if (w != sizeof(uinput_user_dev)) {
cerr << "errno " << errno << " - " << strerror(errno) << endl;
cerr << "(written " << w << " bytes)" << endl;
throw(error("could not write device info"));
}
errno = 0;
if (ioctl(uifd, UI_DEV_CREATE) != 0) {
cerr << "errno " << errno << " - " << strerror(errno) << endl;
throw(error("could not register device"));
}
cout << "\tDevice created." << endl;
cout << "\tStarting control threads." << endl;
cout << endl;
//start the threads
resume();
}
PyObject* device::moduleKillSelf(PyObject* self, PyObject* args) {
pthread_mutex_lock(&vjoy::pymutex);
Py_INCREF(Py_None);
pthread_mutex_unlock(&vjoy::pymutex);
cout << "\tkill command issued by module" << endl;
dev->kill();
return Py_None;
}
void device::kill() {
pause();
pthread_mutex_lock(&vjoy::pymutex);
Py_DECREF(pymodule);
pthread_mutex_unlock(&vjoy::pymutex);
if (ioctl(uifd, UI_DEV_DESTROY) != 0) {
//this really should never happen, since it would cause the process the stall indefinitely and make it 'immortal'
//and possible prevent uinput from working until a reboot
cerr << "errno " << errno << " - " << strerror(errno) << endl;
throw(error("could not unregister device"));
}
close(uifd);
}
void device::pause() {
setState(TO_STALL);
pthread_join(inputThread, NULL);
setState(STALLED);
}
void device::resume() {
//this can only be called from the main thread, so no need to mutex it
currentStatus = LIVE;
pthread_create(&inputThread, NULL, device::inputLoop, this);
}