@@ -46,12 +46,10 @@ int test_bit(const char* bitmask, int bit) {
46
46
static PyObject *
47
47
device_read (PyObject * self , PyObject * args )
48
48
{
49
- int fd ;
50
49
struct input_event event ;
51
50
52
51
// get device file descriptor (O_RDONLY|O_NONBLOCK)
53
- if (PyArg_ParseTuple (args , "i" , & fd ) < 0 )
54
- return NULL ;
52
+ int fd = (int )PyLong_AsLong (PyTuple_GET_ITEM (args , 0 ));
55
53
56
54
int n = read (fd , & event , sizeof (event ));
57
55
@@ -68,12 +66,9 @@ device_read(PyObject *self, PyObject *args)
68
66
PyObject * sec = PyLong_FromLong (event .input_event_sec );
69
67
PyObject * usec = PyLong_FromLong (event .input_event_usec );
70
68
PyObject * val = PyLong_FromLong (event .value );
71
- PyObject * py_input_event = NULL ;
72
-
73
- py_input_event = Py_BuildValue ("OOhhO" , sec , usec , event .type , event .code , val );
74
- Py_DECREF (sec );
75
- Py_DECREF (usec );
76
- Py_DECREF (val );
69
+ PyObject * type = PyLong_FromLong (event .type );
70
+ PyObject * code = PyLong_FromLong (event .code );
71
+ PyObject * py_input_event = PyTuple_Pack (5 , sec , usec , type , code , val );
77
72
78
73
return py_input_event ;
79
74
}
@@ -83,17 +78,16 @@ device_read(PyObject *self, PyObject *args)
83
78
static PyObject *
84
79
device_read_many (PyObject * self , PyObject * args )
85
80
{
86
- int fd ;
87
-
88
81
// get device file descriptor (O_RDONLY|O_NONBLOCK)
89
- int ret = PyArg_ParseTuple (args , "i" , & fd );
90
- if (!ret ) return NULL ;
82
+ int fd = (int )PyLong_AsLong (PyTuple_GET_ITEM (args , 0 ));
91
83
92
- PyObject * event_list = PyList_New (0 );
93
84
PyObject * py_input_event = NULL ;
85
+ PyObject * events = NULL ;
94
86
PyObject * sec = NULL ;
95
87
PyObject * usec = NULL ;
96
88
PyObject * val = NULL ;
89
+ PyObject * type = NULL ;
90
+ PyObject * code = NULL ;
97
91
98
92
struct input_event event [64 ];
99
93
@@ -102,26 +96,24 @@ device_read_many(PyObject *self, PyObject *args)
102
96
103
97
if (nread < 0 ) {
104
98
PyErr_SetFromErrno (PyExc_OSError );
105
- Py_DECREF (event_list );
106
99
return NULL ;
107
100
}
108
101
109
- // Construct a list of event tuples, which we'll make sense of in Python
110
- for (unsigned i = 0 ; i < nread /event_size ; i ++ ) {
102
+ // Construct a tuple of event tuples. Each tuple is the arguments to InputEvent.
103
+ size_t num_events = nread / event_size ;
104
+ events = PyTuple_New (num_events );
105
+ for (size_t i = 0 ; i < num_events ; i ++ ) {
111
106
sec = PyLong_FromLong (event [i ].input_event_sec );
112
107
usec = PyLong_FromLong (event [i ].input_event_usec );
113
108
val = PyLong_FromLong (event [i ].value );
109
+ type = PyLong_FromLong (event [i ].type );
110
+ code = PyLong_FromLong (event [i ].code );
114
111
115
- py_input_event = Py_BuildValue ("OOhhO" , sec , usec , event [i ].type , event [i ].code , val );
116
- PyList_Append (event_list , py_input_event );
117
-
118
- Py_DECREF (py_input_event );
119
- Py_DECREF (sec );
120
- Py_DECREF (usec );
121
- Py_DECREF (val );
112
+ py_input_event = PyTuple_Pack (5 , sec , usec , type , code , val );
113
+ PyTuple_SET_ITEM (events , i , py_input_event );
122
114
}
123
115
124
- return event_list ;
116
+ return events ;
125
117
}
126
118
127
119
@@ -539,7 +531,6 @@ ioctl_EVIOCGPROP(PyObject *self, PyObject *args)
539
531
}
540
532
541
533
542
-
543
534
static PyMethodDef MethodTable [] = {
544
535
{ "ioctl_devinfo" , ioctl_devinfo , METH_VARARGS , "fetch input device info" },
545
536
{ "ioctl_capabilities" , ioctl_capabilities , METH_VARARGS , "fetch input device capabilities" },
0 commit comments