| 1 |
|
| 2 |
/* |
| 3 |
* Python bindings to certain linux input subsystem functions. |
| 4 |
* |
| 5 |
* While everything here can be implemented in pure Python with struct and |
| 6 |
* fcntl.ioctl, imho, it is much more straightforward to do so in C. |
| 7 |
* |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <Python.h> |
| 11 |
|
| 12 |
#include <stdio.h> |
| 13 |
#include <stdint.h> |
| 14 |
#include <string.h> |
| 15 |
#include <errno.h> |
| 16 |
#include <sys/types.h> |
| 17 |
#include <sys/stat.h> |
| 18 |
#include <fcntl.h> |
| 19 |
#include <unistd.h> |
| 20 |
|
| 21 |
#ifdef __FreeBSD__ |
| 22 |
#include <dev/evdev/input.h> |
| 23 |
#else |
| 24 |
#include <linux/input.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#if defined(Py_GIL_DISABLED) |
| 28 |
#define MAYBE_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS |
| 29 |
#define MAYBE_END_ALLOW_THREADS Py_END_ALLOW_THREADS |
| 30 |
#else |
| 31 |
#define MAYBE_BEGIN_ALLOW_THREADS |
| 32 |
#define MAYBE_END_ALLOW_THREADS |
| 33 |
#endif |
| 34 |
|
| 35 |
#ifndef input_event_sec |
| 36 |
#define input_event_sec time.tv_sec |
| 37 |
#define input_event_usec time.tv_usec |
| 38 |
#endif |
| 39 |
|
| 40 |
#define MAX_NAME_SIZE 256 |
| 41 |
|
| 42 |
extern char* EV_NAME[EV_CNT]; |
| 43 |
extern int EV_TYPE_MAX[EV_CNT]; |
| 44 |
extern char** EV_TYPE_NAME[EV_CNT]; |
| 45 |
extern char* BUS_NAME[]; |
| 46 |
|
| 47 |
|
| 48 |
int test_bit(const char* bitmask, int bit) { |
| 49 |
return bitmask[bit/8] & (1 << (bit % 8)); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
// Read input event from a device and return a tuple that mimics input_event |
| 54 |
static PyObject * |
| 55 |
device_read(PyObject *self, PyObject *args) |
| 56 |
{ |
| 57 |
struct input_event event; |
| 58 |
|
| 59 |
// get device file descriptor (O_RDONLY|O_NONBLOCK) |
| 60 |
int fd = (int)PyLong_AsLong(PyTuple_GET_ITEM(args, 0)); |
| 61 |
|
| 62 |
int n; |
| 63 |
MAYBE_BEGIN_ALLOW_THREADS |
| 64 |
n = read(fd, &event, sizeof(event)); |
| 65 |
MAYBE_END_ALLOW_THREADS |
| 66 |
|
| 67 |
if (n < 0) { |
| 68 |
if (errno == EAGAIN) { |
| 69 |
Py_INCREF(Py_None); |
| 70 |
return Py_None; |
| 71 |
} |
| 72 |
|
| 73 |
PyErr_SetFromErrno(PyExc_OSError); |
| 74 |
return NULL; |
| 75 |
} |
| 76 |
|
| 77 |
PyObject *py_input_event = PyTuple_New(5); |
| 78 |
PyTuple_SET_ITEM(py_input_event, 0, PyLong_FromLong(event.input_event_sec)); |
| 79 |
PyTuple_SET_ITEM(py_input_event, 1, PyLong_FromLong(event.input_event_usec)); |
| 80 |
PyTuple_SET_ITEM(py_input_event, 2, PyLong_FromLong(event.type)); |
| 81 |
PyTuple_SET_ITEM(py_input_event, 3, PyLong_FromLong(event.code)); |
| 82 |
PyTuple_SET_ITEM(py_input_event, 4, PyLong_FromLong(event.value)); |
| 83 |
|
| 84 |
return py_input_event; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
// Read multiple input events from a device and return a list of tuples |
| 89 |
static PyObject * |
| 90 |
device_read_many(PyObject *self, PyObject *args) |
| 91 |
{ |
| 92 |
// get device file descriptor (O_RDONLY|O_NONBLOCK) |
| 93 |
int fd = (int)PyLong_AsLong(PyTuple_GET_ITEM(args, 0)); |
| 94 |
|
| 95 |
struct input_event event[64]; |
| 96 |
|
| 97 |
size_t event_size = sizeof(struct input_event); |
| 98 |
ssize_t nread; |
| 99 |
MAYBE_BEGIN_ALLOW_THREADS |
| 100 |
nread = read(fd, event, event_size*64); |
| 101 |
MAYBE_END_ALLOW_THREADS |
| 102 |
|
| 103 |
if (nread < 0) { |
| 104 |
PyErr_SetFromErrno(PyExc_OSError); |
| 105 |
return NULL; |
| 106 |
} |
| 107 |
|
| 108 |
// Construct a tuple of event tuples. Each tuple is the arguments to InputEvent. |
| 109 |
size_t num_events = nread / event_size; |
| 110 |
|
| 111 |
PyObject* events = PyTuple_New(num_events); |
| 112 |
for (size_t i = 0 ; i < num_events; i++) { |
| 113 |
PyObject *py_input_event = PyTuple_New(5); |
| 114 |
PyTuple_SET_ITEM(py_input_event, 0, PyLong_FromLong(event[i].input_event_sec)); |
| 115 |
PyTuple_SET_ITEM(py_input_event, 1, PyLong_FromLong(event[i].input_event_usec)); |
| 116 |
PyTuple_SET_ITEM(py_input_event, 2, PyLong_FromLong(event[i].type)); |
| 117 |
PyTuple_SET_ITEM(py_input_event, 3, PyLong_FromLong(event[i].code)); |
| 118 |
PyTuple_SET_ITEM(py_input_event, 4, PyLong_FromLong(event[i].value)); |
| 119 |
PyTuple_SET_ITEM(events, i, py_input_event); |
| 120 |
} |
| 121 |
|
| 122 |
return events; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
// Get the event types and event codes that the input device supports |
| 127 |
static PyObject * |
| 128 |
ioctl_capabilities(PyObject *self, PyObject *args) |
| 129 |
{ |
| 130 |
int fd, ev_type, ev_code; |
| 131 |
char ev_bits[EV_MAX/8 + 1], code_bits[KEY_MAX/8 + 1]; |
| 132 |
struct input_absinfo absinfo; |
| 133 |
|
| 134 |
int ret = PyArg_ParseTuple(args, "i", &fd); |
| 135 |
if (!ret) return NULL; |
| 136 |
|
| 137 |
// @todo: figure out why fd gets zeroed on an ioctl after the |
| 138 |
// refactoring and get rid of this workaround |
| 139 |
const int _fd = fd; |
| 140 |
|
| 141 |
// Capabilities is a mapping of supported event types to lists of handled |
| 142 |
// events e.g: {1: [272, 273, 274, 275], 2: [0, 1, 6, 8]} |
| 143 |
PyObject* capabilities = PyDict_New(); |
| 144 |
PyObject* eventcodes = NULL; |
| 145 |
PyObject* evlong = NULL; |
| 146 |
PyObject* capability = NULL; |
| 147 |
PyObject* py_absinfo = NULL; |
| 148 |
PyObject* absitem = NULL; |
| 149 |
|
| 150 |
memset(&ev_bits, 0, sizeof(ev_bits)); |
| 151 |
|
| 152 |
if (ioctl(_fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) < 0) |
| 153 |
goto on_err; |
| 154 |
|
| 155 |
// Build a dictionary of the device's capabilities |
| 156 |
for (ev_type=0 ; ev_type<EV_MAX ; ev_type++) { |
| 157 |
if (test_bit(ev_bits, ev_type)) { |
| 158 |
|
| 159 |
capability = PyLong_FromLong(ev_type); |
| 160 |
eventcodes = PyList_New(0); |
| 161 |
|
| 162 |
memset(&code_bits, 0, sizeof(code_bits)); |
| 163 |
ioctl(_fd, EVIOCGBIT(ev_type, sizeof(code_bits)), code_bits); |
| 164 |
|
| 165 |
for (ev_code = 0; ev_code < KEY_MAX; ev_code++) { |
| 166 |
if (test_bit(code_bits, ev_code)) { |
| 167 |
// Get abs{min,max,fuzz,flat} values for ABS_* event codes |
| 168 |
if (ev_type == EV_ABS) { |
| 169 |
memset(&absinfo, 0, sizeof(absinfo)); |
| 170 |
ioctl(_fd, EVIOCGABS(ev_code), &absinfo); |
| 171 |
|
| 172 |
py_absinfo = Py_BuildValue("(iiiiii)", |
| 173 |
absinfo.value, |
| 174 |
absinfo.minimum, |
| 175 |
absinfo.maximum, |
| 176 |
absinfo.fuzz, |
| 177 |
absinfo.flat, |
| 178 |
absinfo.resolution); |
| 179 |
|
| 180 |
evlong = PyLong_FromLong(ev_code); |
| 181 |
absitem = Py_BuildValue("(OO)", evlong, py_absinfo); |
| 182 |
|
| 183 |
// absitem -> tuple(ABS_X, (0, 255, 0, 0)) |
| 184 |
PyList_Append(eventcodes, absitem); |
| 185 |
|
| 186 |
Py_DECREF(absitem); |
| 187 |
Py_DECREF(py_absinfo); |
| 188 |
} |
| 189 |
else { |
| 190 |
evlong = PyLong_FromLong(ev_code); |
| 191 |
PyList_Append(eventcodes, evlong); |
| 192 |
} |
| 193 |
|
| 194 |
Py_DECREF(evlong); |
| 195 |
} |
| 196 |
} |
| 197 |
// capabilities[EV_KEY] = [KEY_A, KEY_B, KEY_C, ...] |
| 198 |
// capabilities[EV_ABS] = [(ABS_X, (0, 255, 0, 0)), ...] |
| 199 |
PyDict_SetItem(capabilities, capability, eventcodes); |
| 200 |
|
| 201 |
Py_DECREF(capability); |
| 202 |
Py_DECREF(eventcodes); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
return capabilities; |
| 207 |
|
| 208 |
on_err: |
| 209 |
Py_XDECREF(capabilities); |
| 210 |
Py_XDECREF(eventcodes); |
| 211 |
Py_XDECREF(capability); |
| 212 |
Py_XDECREF(py_absinfo); |
| 213 |
Py_XDECREF(absitem); |
| 214 |
PyErr_SetFromErrno(PyExc_OSError); |
| 215 |
return NULL; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
// An all-in-one function for describing an input device |
| 220 |
static PyObject * |
| 221 |
ioctl_devinfo(PyObject *self, PyObject *args) |
| 222 |
{ |
| 223 |
int fd; |
| 224 |
|
| 225 |
struct input_id iid; |
| 226 |
char name[MAX_NAME_SIZE]; |
| 227 |
char phys[MAX_NAME_SIZE] = {0}; |
| 228 |
char uniq[MAX_NAME_SIZE] = {0}; |
| 229 |
|
| 230 |
int ret = PyArg_ParseTuple(args, "i", &fd); |
| 231 |
if (!ret) return NULL; |
| 232 |
|
| 233 |
memset(&iid, 0, sizeof(iid)); |
| 234 |
|
| 235 |
if (ioctl(fd, EVIOCGID, &iid) < 0) goto on_err; |
| 236 |
if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0) goto on_err; |
| 237 |
|
| 238 |
// Some devices do not have a physical topology associated with them |
| 239 |
ioctl(fd, EVIOCGPHYS(sizeof(phys)), phys); |
| 240 |
|
| 241 |
// Some kernels have started reporting bluetooth controller MACs as phys. |
| 242 |
// This lets us get the real physical address. As with phys, it may be blank. |
| 243 |
ioctl(fd, EVIOCGUNIQ(sizeof(uniq)), uniq); |
| 244 |
|
| 245 |
return Py_BuildValue("hhhhsss", iid.bustype, iid.vendor, iid.product, iid.version, |
| 246 |
name, phys, uniq); |
| 247 |
|
| 248 |
on_err: |
| 249 |
PyErr_SetFromErrno(PyExc_OSError); |
| 250 |
return NULL; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
static PyObject * |
| 255 |
ioctl_EVIOCGABS(PyObject *self, PyObject *args) |
| 256 |
{ |
| 257 |
int fd, ev_code; |
| 258 |
struct input_absinfo absinfo; |
| 259 |
PyObject* py_absinfo = NULL; |
| 260 |
|
| 261 |
int ret = PyArg_ParseTuple(args, "ii", &fd, &ev_code); |
| 262 |
if (!ret) return NULL; |
| 263 |
|
| 264 |
memset(&absinfo, 0, sizeof(absinfo)); |
| 265 |
ret = ioctl(fd, EVIOCGABS(ev_code), &absinfo); |
| 266 |
if (ret == -1) { |
| 267 |
PyErr_SetFromErrno(PyExc_OSError); |
| 268 |
return NULL; |
| 269 |
} |
| 270 |
|
| 271 |
py_absinfo = Py_BuildValue("(iiiiii)", |
| 272 |
absinfo.value, |
| 273 |
absinfo.minimum, |
| 274 |
absinfo.maximum, |
| 275 |
absinfo.fuzz, |
| 276 |
absinfo.flat, |
| 277 |
absinfo.resolution); |
| 278 |
return py_absinfo; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
static PyObject * |
| 283 |
ioctl_EVIOCSABS(PyObject *self, PyObject *args) |
| 284 |
{ |
| 285 |
int fd, ev_code; |
| 286 |
struct input_absinfo absinfo; |
| 287 |
|
| 288 |
int ret = PyArg_ParseTuple(args, |
| 289 |
"ii(iiiiii)", |
| 290 |
&fd, |
| 291 |
&ev_code, |
| 292 |
&absinfo.value, |
| 293 |
&absinfo.minimum, |
| 294 |
&absinfo.maximum, |
| 295 |
&absinfo.fuzz, |
| 296 |
&absinfo.flat, |
| 297 |
&absinfo.resolution); |
| 298 |
if (!ret) return NULL; |
| 299 |
|
| 300 |
ret = ioctl(fd, EVIOCSABS(ev_code), &absinfo); |
| 301 |
if (ret == -1) { |
| 302 |
PyErr_SetFromErrno(PyExc_OSError); |
| 303 |
return NULL; |
| 304 |
} |
| 305 |
|
| 306 |
Py_INCREF(Py_None); |
| 307 |
return Py_None; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
static PyObject * |
| 312 |
ioctl_EVIOCGREP(PyObject *self, PyObject *args) |
| 313 |
{ |
| 314 |
int fd, ret; |
| 315 |
unsigned int rep[REP_CNT] = {0}; |
| 316 |
ret = PyArg_ParseTuple(args, "i", &fd); |
| 317 |
if (!ret) return NULL; |
| 318 |
|
| 319 |
ret = ioctl(fd, EVIOCGREP, &rep); |
| 320 |
if (ret == -1) |
| 321 |
return NULL; |
| 322 |
|
| 323 |
return Py_BuildValue("(ii)", rep[REP_DELAY], rep[REP_PERIOD]); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
static PyObject * |
| 328 |
ioctl_EVIOCSREP(PyObject *self, PyObject *args) |
| 329 |
{ |
| 330 |
int fd, ret; |
| 331 |
unsigned int rep[REP_CNT] = {0}; |
| 332 |
|
| 333 |
ret = PyArg_ParseTuple(args, "iii", &fd, &rep[0], &rep[1]); |
| 334 |
if (!ret) return NULL; |
| 335 |
|
| 336 |
ret = ioctl(fd, EVIOCSREP, &rep); |
| 337 |
if (ret == -1) |
| 338 |
return NULL; |
| 339 |
|
| 340 |
return Py_BuildValue("i", ret); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
static PyObject * |
| 345 |
ioctl_EVIOCGVERSION(PyObject *self, PyObject *args) |
| 346 |
{ |
| 347 |
int fd, ret, res; |
| 348 |
ret = PyArg_ParseTuple(args, "i", &fd); |
| 349 |
if (!ret) return NULL; |
| 350 |
|
| 351 |
ret = ioctl(fd, EVIOCGVERSION, &res); |
| 352 |
if (ret == -1) |
| 353 |
return NULL; |
| 354 |
|
| 355 |
return Py_BuildValue("i", res); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
static PyObject * |
| 360 |
ioctl_EVIOCGRAB(PyObject *self, PyObject *args) |
| 361 |
{ |
| 362 |
int fd, ret, flag; |
| 363 |
ret = PyArg_ParseTuple(args, "ii", &fd, &flag); |
| 364 |
if (!ret) return NULL; |
| 365 |
|
| 366 |
ret = ioctl(fd, EVIOCGRAB, (intptr_t)flag); |
| 367 |
if (ret != 0) { |
| 368 |
PyErr_SetFromErrno(PyExc_OSError); |
| 369 |
return NULL; |
| 370 |
} |
| 371 |
|
| 372 |
Py_INCREF(Py_None); |
| 373 |
return Py_None; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
static PyObject * |
| 378 |
ioctl_EVIOCG_bits(PyObject *self, PyObject *args) |
| 379 |
{ |
| 380 |
int max, fd, evtype, ret; |
| 381 |
|
| 382 |
ret = PyArg_ParseTuple(args, "ii", &fd, &evtype); |
| 383 |
if (!ret) return NULL; |
| 384 |
|
| 385 |
switch (evtype) { |
| 386 |
case EV_LED: |
| 387 |
max = LED_MAX; break; |
| 388 |
case EV_SND: |
| 389 |
max = SND_MAX; break; |
| 390 |
case EV_KEY: |
| 391 |
max = KEY_MAX; break; |
| 392 |
case EV_SW: |
| 393 |
max = SW_MAX; break; |
| 394 |
default: |
| 395 |
return NULL; |
| 396 |
} |
| 397 |
|
| 398 |
char bytes[(max+7)/8]; |
| 399 |
memset(bytes, 0, sizeof bytes); |
| 400 |
|
| 401 |
switch (evtype) { |
| 402 |
case EV_LED: |
| 403 |
ret = ioctl(fd, EVIOCGLED(sizeof(bytes)), &bytes); |
| 404 |
break; |
| 405 |
case EV_SND: |
| 406 |
ret = ioctl(fd, EVIOCGSND(sizeof(bytes)), &bytes); |
| 407 |
break; |
| 408 |
case EV_KEY: |
| 409 |
ret = ioctl(fd, EVIOCGKEY(sizeof(bytes)), &bytes); |
| 410 |
break; |
| 411 |
case EV_SW: |
| 412 |
ret = ioctl(fd, EVIOCGSW(sizeof(bytes)), &bytes); |
| 413 |
break; |
| 414 |
} |
| 415 |
|
| 416 |
if (ret == -1) |
| 417 |
return NULL; |
| 418 |
|
| 419 |
PyObject* res = PyList_New(0); |
| 420 |
for (int i=0; i<=max; i++) { |
| 421 |
if (test_bit(bytes, i)) { |
| 422 |
PyObject *val = PyLong_FromLong(i); |
| 423 |
PyList_Append(res, val); |
| 424 |
Py_DECREF(val); |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
return res; |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
static PyObject * |
| 433 |
ioctl_EVIOCGEFFECTS(PyObject *self, PyObject *args) |
| 434 |
{ |
| 435 |
int fd, ret, res; |
| 436 |
ret = PyArg_ParseTuple(args, "i", &fd); |
| 437 |
if (!ret) return NULL; |
| 438 |
|
| 439 |
ret = ioctl(fd, EVIOCGEFFECTS, &res); |
| 440 |
if (ret == -1) |
| 441 |
return NULL; |
| 442 |
|
| 443 |
return Py_BuildValue("i", res); |
| 444 |
} |
| 445 |
|
| 446 |
void print_ff_effect(struct ff_effect* effect) { |
| 447 |
fprintf(stderr, |
| 448 |
"ff_effect:\n" |
| 449 |
" type: %d \n" |
| 450 |
" id: %d \n" |
| 451 |
" direction: %d\n" |
| 452 |
" trigger: (%d, %d)\n" |
| 453 |
" replay: (%d, %d)\n", |
| 454 |
effect->type, effect->id, effect->direction, |
| 455 |
effect->trigger.button, effect->trigger.interval, |
| 456 |
effect->replay.length, effect->replay.delay |
| 457 |
); |
| 458 |
|
| 459 |
|
| 460 |
switch (effect->type) { |
| 461 |
case FF_CONSTANT: |
| 462 |
fprintf(stderr, " constant: (%d, (%d, %d, %d, %d))\n", effect->u.constant.level, |
| 463 |
effect->u.constant.envelope.attack_length, |
| 464 |
effect->u.constant.envelope.attack_level, |
| 465 |
effect->u.constant.envelope.fade_length, |
| 466 |
effect->u.constant.envelope.fade_level); |
| 467 |
break; |
| 468 |
case FF_RUMBLE: |
| 469 |
fprintf(stderr, " rumble: (%d, %d)\n", |
| 470 |
effect->u.rumble.strong_magnitude, |
| 471 |
effect->u.rumble.weak_magnitude); |
| 472 |
break; |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
static PyObject * |
| 478 |
upload_effect(PyObject *self, PyObject *args) |
| 479 |
{ |
| 480 |
int fd, ret; |
| 481 |
PyObject* effect_data; |
| 482 |
ret = PyArg_ParseTuple(args, "iO", &fd, &effect_data); |
| 483 |
if (!ret) return NULL; |
| 484 |
|
| 485 |
void* data = PyBytes_AsString(effect_data); |
| 486 |
struct ff_effect effect = {}; |
| 487 |
memmove(&effect, data, sizeof(struct ff_effect)); |
| 488 |
|
| 489 |
// print_ff_effect(&effect); |
| 490 |
|
| 491 |
ret = ioctl(fd, EVIOCSFF, &effect); |
| 492 |
if (ret != 0) { |
| 493 |
PyErr_SetFromErrno(PyExc_OSError); |
| 494 |
return NULL; |
| 495 |
} |
| 496 |
|
| 497 |
return Py_BuildValue("i", effect.id); |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
static PyObject * |
| 502 |
erase_effect(PyObject *self, PyObject *args) |
| 503 |
{ |
| 504 |
int fd, ret; |
| 505 |
PyObject* ff_id_obj; |
| 506 |
ret = PyArg_ParseTuple(args, "iO", &fd, &ff_id_obj); |
| 507 |
if (!ret) return NULL; |
| 508 |
|
| 509 |
long ff_id = PyLong_AsLong(ff_id_obj); |
| 510 |
ret = ioctl(fd, EVIOCRMFF, ff_id); |
| 511 |
if (ret != 0) { |
| 512 |
PyErr_SetFromErrno(PyExc_OSError); |
| 513 |
return NULL; |
| 514 |
} |
| 515 |
|
| 516 |
Py_INCREF(Py_None); |
| 517 |
return Py_None; |
| 518 |
} |
| 519 |
|
| 520 |
static PyObject * |
| 521 |
ioctl_EVIOCGPROP(PyObject *self, PyObject *args) |
| 522 |
{ |
| 523 |
int fd, ret; |
| 524 |
|
| 525 |
ret = PyArg_ParseTuple(args, "i", &fd); |
| 526 |
if (!ret) return NULL; |
| 527 |
|
| 528 |
char bytes[(INPUT_PROP_MAX+7)/8]; |
| 529 |
memset(bytes, 0, sizeof bytes); |
| 530 |
|
| 531 |
ret = ioctl(fd, EVIOCGPROP(sizeof(bytes)), &bytes); |
| 532 |
|
| 533 |
if (ret == -1) |
| 534 |
return NULL; |
| 535 |
|
| 536 |
PyObject* res = PyList_New(0); |
| 537 |
for (int i=0; i<INPUT_PROP_MAX; i++) { |
| 538 |
if (test_bit(bytes, i)) { |
| 539 |
PyObject *val = PyLong_FromLong(i); |
| 540 |
PyList_Append(res, val); |
| 541 |
Py_DECREF(val); |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
return res; |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
static PyMethodDef MethodTable[] = { |
| 550 |
{ "ioctl_devinfo", ioctl_devinfo, METH_VARARGS, "fetch input device info" }, |
| 551 |
{ "ioctl_capabilities", ioctl_capabilities, METH_VARARGS, "fetch input device capabilities" }, |
| 552 |
{ "ioctl_EVIOCGABS", ioctl_EVIOCGABS, METH_VARARGS, "get input device absinfo"}, |
| 553 |
{ "ioctl_EVIOCSABS", ioctl_EVIOCSABS, METH_VARARGS, "set input device absinfo"}, |
| 554 |
{ "ioctl_EVIOCGREP", ioctl_EVIOCGREP, METH_VARARGS}, |
| 555 |
{ "ioctl_EVIOCSREP", ioctl_EVIOCSREP, METH_VARARGS}, |
| 556 |
{ "ioctl_EVIOCGVERSION", ioctl_EVIOCGVERSION, METH_VARARGS}, |
| 557 |
{ "ioctl_EVIOCGRAB", ioctl_EVIOCGRAB, METH_VARARGS}, |
| 558 |
{ "ioctl_EVIOCGEFFECTS", ioctl_EVIOCGEFFECTS, METH_VARARGS, "fetch the number of effects the device can keep in its memory." }, |
| 559 |
{ "ioctl_EVIOCG_bits", ioctl_EVIOCG_bits, METH_VARARGS, "get state of KEY|LED|SND|SW"}, |
| 560 |
{ "ioctl_EVIOCGPROP", ioctl_EVIOCGPROP, METH_VARARGS, "get device properties"}, |
| 561 |
{ "device_read", device_read, METH_VARARGS, "read an input event from a device" }, |
| 562 |
{ "device_read_many", device_read_many, METH_VARARGS, "read all available input events from a device" }, |
| 563 |
{ "upload_effect", upload_effect, METH_VARARGS, "" }, |
| 564 |
{ "erase_effect", erase_effect, METH_VARARGS, "" }, |
| 565 |
|
| 566 |
{ NULL, NULL, 0, NULL} |
| 567 |
}; |
| 568 |
|
| 569 |
|
| 570 |
static struct PyModuleDef moduledef = { |
| 571 |
PyModuleDef_HEAD_INIT, |
| 572 |
"_input", |
| 573 |
"Python bindings to certain linux input subsystem functions", |
| 574 |
-1, /* m_size */ |
| 575 |
MethodTable, /* m_methods */ |
| 576 |
NULL, /* m_reload */ |
| 577 |
NULL, /* m_traverse */ |
| 578 |
NULL, /* m_clear */ |
| 579 |
NULL, /* m_free */ |
| 580 |
}; |
| 581 |
|
| 582 |
static PyObject * |
| 583 |
moduleinit(void) |
| 584 |
{ |
| 585 |
PyObject* m = PyModule_Create(&moduledef); |
| 586 |
if (m == NULL) return NULL; |
| 587 |
#ifdef Py_GIL_DISABLED |
| 588 |
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED); |
| 589 |
#endif |
| 590 |
return m; |
| 591 |
} |
| 592 |
|
| 593 |
PyMODINIT_FUNC |
| 594 |
PyInit__input(void) |
| 595 |
{ |
| 596 |
return moduleinit(); |
| 597 |
} |