@@ -32,82 +32,126 @@ static PyObject *pysuinput_open(PyObject *self, PyObject *args)
3232 & version ))
3333 return NULL ;
3434 struct input_id id = {bustype , vendor , product , version };
35- int uinput_fd = suinput_open (name , & id );
36- if (uinput_fd == -1 )
35+ struct suinput_driver * c_driver = suinput_open (name , & id );
36+ if (c_driver == NULL )
3737 return PyErr_SetFromErrno (PyExc_IOError );
38- return Py_BuildValue ( "i" , uinput_fd );
38+ return PyCObject_FromVoidPtr (( void * ) c_driver , NULL );
3939}
4040
4141static PyObject * pysuinput_close (PyObject * self , PyObject * args )
4242{
43- int uinput_fd ;
44- if (!PyArg_ParseTuple (args , "i" , & uinput_fd ))
43+ PyObject * py_driver ;
44+ struct suinput_driver * c_driver ;
45+ if (!PyArg_ParseTuple (args , "O" , & py_driver ))
4546 return NULL ;
46- if (suinput_close (uinput_fd ) == -1 )
47+ c_driver = (struct suinput_driver * )PyCObject_AsVoidPtr (py_driver );
48+ if (suinput_close (c_driver ) == -1 )
4749 return PyErr_SetFromErrno (PyExc_IOError );
4850 Py_RETURN_NONE ;
4951}
5052
5153static PyObject * pysuinput_press (PyObject * self , PyObject * args )
5254{
53- int uinput_fd ;
55+ PyObject * py_driver ;
56+ struct suinput_driver * c_driver ;
5457 uint16_t code ;
55- if (!PyArg_ParseTuple (args , "iH " , & uinput_fd , & code ))
58+ if (!PyArg_ParseTuple (args , "OH " , & py_driver , & code ))
5659 return NULL ;
57- if (suinput_press (uinput_fd , code ) == -1 )
60+ c_driver = (struct suinput_driver * )PyCObject_AsVoidPtr (py_driver );
61+ if (suinput_press (c_driver , code ) == -1 )
5862 return PyErr_SetFromErrno (PyExc_IOError );
5963 Py_RETURN_NONE ;
6064}
6165
6266static PyObject * pysuinput_release (PyObject * self , PyObject * args )
6367{
64- int uinput_fd ;
68+ PyObject * py_driver ;
69+ struct suinput_driver * c_driver ;
6570 uint16_t code ;
66- if (!PyArg_ParseTuple (args , "iH " , & uinput_fd , & code ))
71+ if (!PyArg_ParseTuple (args , "OH " , & py_driver , & code ))
6772 return NULL ;
68- if (suinput_release (uinput_fd , code ) == -1 )
73+ c_driver = (struct suinput_driver * )PyCObject_AsVoidPtr (py_driver );
74+ if (suinput_release (c_driver , code ) == -1 )
6975 return PyErr_SetFromErrno (PyExc_IOError );
7076 Py_RETURN_NONE ;
7177}
7278
7379static PyObject * pysuinput_click (PyObject * self , PyObject * args )
7480{
75- int uinput_fd ;
81+ PyObject * py_driver ;
82+ struct suinput_driver * c_driver ;
7683 uint16_t code ;
77- if (!PyArg_ParseTuple (args , "iH " , & uinput_fd , & code ))
84+ if (!PyArg_ParseTuple (args , "OH " , & py_driver , & code ))
7885 return NULL ;
79- if (suinput_click (uinput_fd , code ) == -1 )
86+ c_driver = (struct suinput_driver * )PyCObject_AsVoidPtr (py_driver );
87+ if (suinput_click (c_driver , code ) == -1 )
8088 return PyErr_SetFromErrno (PyExc_IOError );
8189 Py_RETURN_NONE ;
8290}
8391
8492static PyObject * pysuinput_move_pointer (PyObject * self , PyObject * args )
8593{
86- int uinput_fd ;
94+ PyObject * py_driver ;
95+ struct suinput_driver * c_driver ;
8796 int32_t x ;
8897 int32_t y ;
89- if (!PyArg_ParseTuple (args , "iii " , & uinput_fd , & x , & y )) {
98+ if (!PyArg_ParseTuple (args , "Oii " , & py_driver , & x , & y )) {
9099 return NULL ;
91100 }
92- if (suinput_move_pointer (uinput_fd , x , y ) == -1 )
101+ c_driver = (struct suinput_driver * )PyCObject_AsVoidPtr (py_driver );
102+ if (suinput_move_pointer (c_driver , x , y ) == -1 )
93103 return PyErr_SetFromErrno (PyExc_IOError );
94104 Py_RETURN_NONE ;
95105}
96106
97107static PyObject * pysuinput_press_release (PyObject * self , PyObject * args )
98108{
99- int uinput_fd ;
109+ PyObject * py_driver ;
110+ struct suinput_driver * c_driver ;
100111 int16_t code ;
101- if (!PyArg_ParseTuple (args , "ih " , & uinput_fd , & code ))
112+ if (!PyArg_ParseTuple (args , "Oh " , & py_driver , & code ))
102113 return NULL ;
103- if (suinput_press_release (uinput_fd , code ) == -1 )
114+ c_driver = (struct suinput_driver * )PyCObject_AsVoidPtr (py_driver );
115+ if (suinput_press_release (c_driver , code ) == -1 )
104116 return PyErr_SetFromErrno (PyExc_IOError );
105117 Py_RETURN_NONE ;
106118}
107119
120+ static PyObject * pysuinput_toggle (PyObject * self , PyObject * args )
121+ {
122+ PyObject * py_driver ;
123+ struct suinput_driver * c_driver ;
124+ uint16_t code ;
125+ if (!PyArg_ParseTuple (args , "OH" , & py_driver , & code ))
126+ return NULL ;
127+ c_driver = (struct suinput_driver * )PyCObject_AsVoidPtr (py_driver );
128+ if (suinput_toggle (c_driver , code ) == -1 )
129+ return PyErr_SetFromErrno (PyExc_IOError );
130+ Py_RETURN_NONE ;
131+ }
132+
133+ static PyObject * pysuinput_is_pressed (PyObject * self , PyObject * args )
134+ {
135+ PyObject * py_driver ;
136+ struct suinput_driver * c_driver ;
137+ uint16_t code ;
138+ if (!PyArg_ParseTuple (args , "OH" , & py_driver , & code ))
139+ return NULL ;
140+ c_driver = (struct suinput_driver * )PyCObject_AsVoidPtr (py_driver );
141+ return PyBool_FromLong (suinput_is_pressed (c_driver , code ));
142+ }
143+
144+ static PyObject * pysuinput_is_valid_code (PyObject * self , PyObject * args )
145+ {
146+ uint16_t code ;
147+ if (!PyArg_ParseTuple (args , "H" , & code ))
148+ return NULL ;
149+ return PyBool_FromLong (suinput_is_valid_code (code ));
150+ }
151+
108152static PyMethodDef pysuinputMethods [] = {
109153 {"open" , pysuinput_open , METH_VARARGS ,
110- "open(str( name), int( bustype), int( vendor), int( product), int( version) )\n\n"
154+ "open(name, bustype, vendor, product, version)\n\n"
111155 "Creates and opens a connection to the event device. Returns an uinput\n"
112156 "file descriptor on success. On error, -1 is returned, and errno is set\n"
113157 "appropriately.\n"
@@ -117,7 +161,7 @@ static PyMethodDef pysuinputMethods[] = {
117161 },
118162
119163 {"close" , pysuinput_close , METH_VARARGS ,
120- "close(int(uinput_fd) )\n\n"
164+ "close(driver )\n\n"
121165 "Destroys and closes a connection to the event device. Returns 0 on\n"
122166 "success. On error, -1 is returned, and errno is set appropriately.\n"
123167 "\n"
@@ -126,7 +170,7 @@ static PyMethodDef pysuinputMethods[] = {
126170 },
127171
128172 {"click" , pysuinput_click , METH_VARARGS ,
129- "click(int(uinput_fd), int( code) )\n\n"
173+ "click(driver, code)\n\n"
130174 "Sends a press and release events to the event device. Returns 0 on\n"
131175 "success. On error, -1 is returned, and errno is set appropriately.\n"
132176 "\n"
@@ -142,7 +186,7 @@ static PyMethodDef pysuinputMethods[] = {
142186 },
143187
144188 {"press" , pysuinput_press , METH_VARARGS ,
145- "press(int(uinput_fd), int( code) )\n\n"
189+ "press(driver, code)\n\n"
146190 "Sends a press event to the event device. Event is repeated after\n"
147191 "a short delay until a release event is sent. Returns 0 on success.\n"
148192 "On error, -1 is returned, and errno is set appropriately.\n"
@@ -155,7 +199,7 @@ static PyMethodDef pysuinputMethods[] = {
155199 },
156200
157201 {"release" , pysuinput_release , METH_VARARGS ,
158- "release(int(uinput_fd), int( code) )\n\n"
202+ "release(driver, code)\n\n"
159203 "Sends a release event to the event device. Returns 0 on success.\n"
160204 "On error, -1 is returned, and errno is set appropriately.\n"
161205 "\n"
@@ -167,7 +211,7 @@ static PyMethodDef pysuinputMethods[] = {
167211 },
168212
169213 {"move_pointer" , pysuinput_move_pointer , METH_VARARGS ,
170- "move_pointer(int(uinput_fd), int(x), int(y) )\n\n"
214+ "move_pointer(driver, x, y )\n\n"
171215 "Sends a relative pointer motion event to the event device. Values\n"
172216 "increase towards right-bottom. Returns 0 on success. On error, -1\n"
173217 "is returned, and errno is set appropriately.\n"
@@ -177,7 +221,7 @@ static PyMethodDef pysuinputMethods[] = {
177221 },
178222
179223 {"press_release" , pysuinput_press_release , METH_VARARGS ,
180- "press_release(int(uinput_fd), int( signed_code) )\n\n"
224+ "press_release(driver, signed_code)\n\n"
181225 "Sends a press or a release event to the event device. The sign of\n"
182226 "`code` determines which type of event is sent. Positive `code`\n"
183227 "means press and negative `code` means release. Returns 0 on\n"
@@ -195,6 +239,18 @@ static PyMethodDef pysuinputMethods[] = {
195239
196240 },
197241
242+ {"toggle" , pysuinput_toggle , METH_VARARGS ,
243+ ""
244+ },
245+
246+ {"is_pressed" , pysuinput_is_pressed , METH_VARARGS ,
247+ ""
248+ },
249+
250+ {"is_valid_code" , pysuinput_is_valid_code , METH_VARARGS ,
251+ ""
252+ },
253+
198254 {NULL , NULL , 0 , NULL }
199255};
200256
0 commit comments