Skip to content

Commit b504266

Browse files
Refresh documentation and move uinput.is_valid_keycode to uinput.keycodes.is_valid
1 parent 7339a36 commit b504266

4 files changed

Lines changed: 68 additions & 150 deletions

File tree

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
)
1212

1313
keycodes_module = Extension('uinput.keycodes',
14-
sources=['src/keycodes.c'])
14+
sources=['src/keycodes.c'],
15+
libraries=['suinput'],
16+
)
1517

1618
setup(name='python-uinput',
1719
version='0.2',

src/__init__.py

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
class Driver(object):
5757
"""Device driver for the Linux uinput-system.
5858
59+
keycodes are defined in uinput.keycodes -module.
60+
5961
For the documentation of the constructor arguments, see the
6062
documentation of the corresponding properties.
6163
"""
@@ -76,8 +78,7 @@ def name(self):
7678

7779
@property
7880
def bustype(self):
79-
"""Bustype of the device.
80-
Must be one of the BUS_ -prefixed constant values.
81+
"""One of the BUS_ -prefixed constant values.
8182
"""
8283
return self._bustype
8384

@@ -97,74 +98,34 @@ def version(self):
9798
return self._version
9899

99100
def move_pointer(self, x, y):
100-
"""Sends a relative pointer motion event to the event device.
101-
Values increase towards right-bottom.
102-
"""
101+
"Move pointer towards bottom-right."
103102
_suinput.move_pointer(self._context, x, y)
104103

105104
def press(self, keycode):
106-
"""Sends a press event to the event device. Event is repeated after
107-
a short delay until a release event is sent.
108-
109-
keycode must be one of the constant values defined in
110-
uinput.keycodes -module.
111-
"""
105+
"""Send a press event.
106+
Event is repeated after a short delay until a release event is sent."""
112107
_suinput.press(self._context, keycode)
113108

114109
def release(self, keycode):
115-
"""Sends a release event to the event device.
116-
117-
keycode must be one of the constant values defined in
118-
uinput.keycodes -module.
119-
"""
110+
"Send a release event."
120111
_suinput.release(self._context, keycode)
121112

122113
def click(self, keycode):
123-
"""Sends a press and release events to the event device.
124-
125-
keycode must be one of the constant values defined in
126-
uinput.keycodes -module.
127-
128-
This method is provided as a convenience and has effectively the
129-
same result as calling press() and release() sequentially.
130-
"""
114+
"Send a press and a release event."
131115
_suinput.click(self._context, keycode)
132116

133117
def press_release(self, signed_keycode):
134-
"""Sends a press or a release event to the event device.
135-
The sign of the signed_keycode determines which type of event is sent.
136-
Positive signed_keycode means press and negative means release.
137-
138-
Absolute value of signed_keycode must be one of the constant
139-
values defined in uinput.keycodes module.
140-
141-
This method is provided as a convenience and has effectively the
142-
same result as calling press() when the value of signed_keycode
143-
is positive and release() when negative.
144-
"""
118+
"""Send a press event if signed_keycode > 0, otherwise send
119+
a release event."""
145120
_suinput.press_release(self._context, signed_keycode)
146121

147122
def toggle(self, keycode):
148-
"""Press button if it is not pressed currently, release it otherwise.
149-
150-
keycode must be one of the constant values defined in
151-
uinput.keycodes -module.
152-
153-
This method is provided as a convenience and has effectively the
154-
same result as calling press() if is_pressed() returns False
155-
and release() otherwise.
156-
"""
123+
"Press button if it is not pressed currently, release it otherwise."
157124
_suinput.toggle(self._context, keycode)
158125

159126
def is_pressed(self, keycode):
160-
"""Return True if button is pressed, False otherwise.
161-
162-
keycode must be one of the constant values defined in
163-
uinput.keycodes -module.
164-
"""
127+
"Return True if button is pressed, otherwise return False."
165128
return _suinput.is_pressed(self._context, keycode)
166129

167130
def __del__(self):
168131
_suinput.close(self._context)
169-
170-
is_valid_keycode = _suinput.is_valid_code

src/keycodes.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,22 @@
1818
*/
1919

2020
#include <Python.h>
21+
#include <suinput.h>
2122

22-
#include "linux/uinput.h"
23+
static PyObject *pysuinput_is_valid(PyObject *self, PyObject *args)
24+
{
25+
uint16_t keycode;
26+
if (!PyArg_ParseTuple(args, "H", &keycode))
27+
return NULL;
28+
return PyBool_FromLong(suinput_is_valid_keycode(keycode));
29+
}
2330

2431
static PyMethodDef keycodesMethods[] = {
32+
{"is_valid", pysuinput_is_valid, METH_VARARGS,
33+
"is_valid(keycode)\n\n"
34+
"Return True if keycode is valid, otherwise return False."
35+
},
36+
2537
{NULL, NULL, 0, NULL}
2638
};
2739

src/pysuinput.c

Lines changed: 40 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ static PyObject *pysuinput_press(PyObject *self, PyObject *args)
5454
{
5555
PyObject *py_driver;
5656
struct suinput_driver *c_driver;
57-
uint16_t code;
58-
if (!PyArg_ParseTuple(args, "OH", &py_driver, &code))
57+
uint16_t keycode;
58+
if (!PyArg_ParseTuple(args, "OH", &py_driver, &keycode))
5959
return NULL;
6060
c_driver = (struct suinput_driver *)PyCObject_AsVoidPtr(py_driver);
61-
if (suinput_press(c_driver, code) == -1)
61+
if (suinput_press(c_driver, keycode) == -1)
6262
return PyErr_SetFromErrno(PyExc_IOError);
6363
Py_RETURN_NONE;
6464
}
@@ -67,11 +67,11 @@ static PyObject *pysuinput_release(PyObject *self, PyObject *args)
6767
{
6868
PyObject *py_driver;
6969
struct suinput_driver *c_driver;
70-
uint16_t code;
71-
if (!PyArg_ParseTuple(args, "OH", &py_driver, &code))
70+
uint16_t keycode;
71+
if (!PyArg_ParseTuple(args, "OH", &py_driver, &keycode))
7272
return NULL;
7373
c_driver = (struct suinput_driver *)PyCObject_AsVoidPtr(py_driver);
74-
if (suinput_release(c_driver, code) == -1)
74+
if (suinput_release(c_driver, keycode) == -1)
7575
return PyErr_SetFromErrno(PyExc_IOError);
7676
Py_RETURN_NONE;
7777
}
@@ -80,11 +80,11 @@ static PyObject *pysuinput_click(PyObject *self, PyObject *args)
8080
{
8181
PyObject *py_driver;
8282
struct suinput_driver *c_driver;
83-
uint16_t code;
84-
if (!PyArg_ParseTuple(args, "OH", &py_driver, &code))
83+
uint16_t keycode;
84+
if (!PyArg_ParseTuple(args, "OH", &py_driver, &keycode))
8585
return NULL;
8686
c_driver = (struct suinput_driver *)PyCObject_AsVoidPtr(py_driver);
87-
if (suinput_click(c_driver, code) == -1)
87+
if (suinput_click(c_driver, keycode) == -1)
8888
return PyErr_SetFromErrno(PyExc_IOError);
8989
Py_RETURN_NONE;
9090
}
@@ -108,11 +108,11 @@ static PyObject *pysuinput_press_release(PyObject *self, PyObject *args)
108108
{
109109
PyObject *py_driver;
110110
struct suinput_driver *c_driver;
111-
int16_t code;
112-
if (!PyArg_ParseTuple(args, "Oh", &py_driver, &code))
111+
int16_t keycode;
112+
if (!PyArg_ParseTuple(args, "Oh", &py_driver, &keycode))
113113
return NULL;
114114
c_driver = (struct suinput_driver *)PyCObject_AsVoidPtr(py_driver);
115-
if (suinput_press_release(c_driver, code) == -1)
115+
if (suinput_press_release(c_driver, keycode) == -1)
116116
return PyErr_SetFromErrno(PyExc_IOError);
117117
Py_RETURN_NONE;
118118
}
@@ -121,11 +121,11 @@ static PyObject *pysuinput_toggle(PyObject *self, PyObject *args)
121121
{
122122
PyObject *py_driver;
123123
struct suinput_driver *c_driver;
124-
uint16_t code;
125-
if (!PyArg_ParseTuple(args, "OH", &py_driver, &code))
124+
uint16_t keycode;
125+
if (!PyArg_ParseTuple(args, "OH", &py_driver, &keycode))
126126
return NULL;
127127
c_driver = (struct suinput_driver *)PyCObject_AsVoidPtr(py_driver);
128-
if (suinput_toggle(c_driver, code) == -1)
128+
if (suinput_toggle(c_driver, keycode) == -1)
129129
return PyErr_SetFromErrno(PyExc_IOError);
130130
Py_RETURN_NONE;
131131
}
@@ -134,121 +134,64 @@ static PyObject *pysuinput_is_pressed(PyObject *self, PyObject *args)
134134
{
135135
PyObject *py_driver;
136136
struct suinput_driver *c_driver;
137-
uint16_t code;
138-
if (!PyArg_ParseTuple(args, "OH", &py_driver, &code))
137+
uint16_t keycode;
138+
if (!PyArg_ParseTuple(args, "OH", &py_driver, &keycode))
139139
return NULL;
140140
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));
141+
return PyBool_FromLong(suinput_is_pressed(c_driver, keycode));
150142
}
151143

152144
static PyMethodDef pysuinputMethods[] = {
153145
{"open", pysuinput_open, METH_VARARGS,
154146
"open(name, bustype, vendor, product, version)\n\n"
155-
"Creates and opens a connection to the event device. Returns an uinput\n"
156-
"file descriptor on success. On error, -1 is returned, and errno is set\n"
157-
"appropriately.\n"
158-
"\n"
159-
"All possible values of `bustype` are defined as constants prefixed\n"
160-
"by BUS_.\n"
147+
"Return an input driver.\n"
148+
"All possible values of bustype are defined as constants prefixed\n"
149+
"by BUS_, vendor, product and version are unsigned 16 bit integers."
150+
"close() must be called for every object returned by this function."
161151
},
162152

163153
{"close", pysuinput_close, METH_VARARGS,
164154
"close(driver)\n\n"
165-
"Destroys and closes a connection to the event device. Returns 0 on\n"
166-
"success. On error, -1 is returned, and errno is set appropriately.\n"
167-
"\n"
168-
"Behaviour is undefined when passed a file descriptor not returned by\n"
169-
"suinput_open()."
155+
"Close and destroy the driver."
170156
},
171157

172158
{"click", pysuinput_click, METH_VARARGS,
173-
"click(driver, code)\n\n"
174-
"Sends a press and release events to the event device. Returns 0 on\n"
175-
"success. On error, -1 is returned, and errno is set appropriately.\n"
176-
"\n"
177-
"Behaviour is undefined when passed a file descriptor not returned by\n"
178-
"suinput_open().\n"
179-
"\n"
180-
"All possible values of `code` are defined as constants in\n"
181-
"uinput.codes -module.\n"
182-
"\n"
183-
"This function is provided as a convenience and has effectively the\n"
184-
"same result as calling suinput_press() and suinput_release()\n"
185-
"sequentially."
159+
"click(driver, keycode)\n\n"
160+
"Send a press and a release event."
186161
},
187162

188163
{"press", pysuinput_press, METH_VARARGS,
189-
"press(driver, code)\n\n"
190-
"Sends a press event to the event device. Event is repeated after\n"
191-
"a short delay until a release event is sent. Returns 0 on success.\n"
192-
"On error, -1 is returned, and errno is set appropriately.\n"
193-
"\n"
194-
"Behaviour is undefined when passed a file descriptor not returned by\n"
195-
"suinput_open().\n"
196-
"\n"
197-
"All possible values of `code` are defined as constants in\n"
198-
"uinput.codes -module."
164+
"press(driver, keycode)\n\n"
165+
"Send a press event.\n"
166+
"Event is repeated after a short delay until a release event is sent."
199167
},
200168

201169
{"release", pysuinput_release, METH_VARARGS,
202-
"release(driver, code)\n\n"
203-
"Sends a release event to the event device. Returns 0 on success.\n"
204-
"On error, -1 is returned, and errno is set appropriately.\n"
205-
"\n"
206-
"Behaviour is undefined when passed a file descriptor not returned by\n"
207-
"suinput_open().\n"
208-
"\n"
209-
"All possible values of `code` are defined as constants in\n"
210-
"uinput.codes -module."
170+
"release(driver, keycode)\n\n"
171+
"Send a release event."
211172
},
212173

213174
{"move_pointer", pysuinput_move_pointer, METH_VARARGS,
214175
"move_pointer(driver, x, y)\n\n"
215-
"Sends a relative pointer motion event to the event device. Values\n"
216-
"increase towards right-bottom. Returns 0 on success. On error, -1\n"
217-
"is returned, and errno is set appropriately.\n"
218-
"\n"
219-
"Behaviour is undefined when passed a file descriptor not returned by\n"
220-
"suinput_open()."
176+
"Move pointer towards bottom-right."
221177
},
222178

223179
{"press_release", pysuinput_press_release, METH_VARARGS,
224-
"press_release(driver, signed_code)\n\n"
225-
"Sends a press or a release event to the event device. The sign of\n"
226-
"`code` determines which type of event is sent. Positive `code`\n"
227-
"means press and negative `code` means release. Returns 0 on\n"
228-
"success. On error, -1 is returned, and errno is set appropriately.\n"
229-
"\n"
230-
"Behaviour is undefined when passed a file descriptor not returned by\n"
231-
"suinput_open().\n"
232-
"\n"
233-
"All possible absolute values of `code` are defined as constants in\n"
234-
"uinput.codes -module.\n"
235-
"\n"
236-
"This function is provided as a convenience and has effectively the\n"
237-
"same result as calling suinput_press() when the value of `code` is\n"
238-
"positive and suinput_release() when negative."
239-
180+
"press_release(driver, signed_keycode)\n\n"
181+
"Send a press event if signed_keycode > 0, otherwise send\n"
182+
"a release event."
240183
},
241184

242185
{"toggle", pysuinput_toggle, METH_VARARGS,
243-
""
186+
"toggle(driver, keycode)\n\n"
187+
"Press button if it is not pressed currently, release it otherwise."
244188
},
245189

246190
{"is_pressed", pysuinput_is_pressed, METH_VARARGS,
247-
""
248-
},
249-
250-
{"is_valid_code", pysuinput_is_valid_code, METH_VARARGS,
251-
""
191+
"is_pressed(driver, keycode)\n\n"
192+
"Return True if button is pressed, False otherwise.\n"
193+
"keycode must be one of the constant values defined in\n"
194+
"uinput.keycodes -module."
252195
},
253196

254197
{NULL, NULL, 0, NULL}

0 commit comments

Comments
 (0)