|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# uinput - Simple Python API to the Linux uinput-system |
| 3 | +# Copyright (C) 2009 Tuomas Räsänen <tuos@codegrove.org> |
| 4 | + |
| 5 | +# This library is free software; you can redistribute it and/or |
| 6 | +# modify it under the terms of the GNU Lesser General Public |
| 7 | +# License as published by the Free Software Foundation; either |
| 8 | +# version 3 of the License, or (at your option) any later version. |
| 9 | + |
| 10 | +# This library is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | +# Lesser General Public License for more details. |
| 14 | + |
| 15 | +# You should have received a copy of the GNU Lesser General Public |
| 16 | +# License along with this library; if not, write to the Free Software |
| 17 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | + |
| 19 | +"""Simple Python API to the Linux uinput-system |
| 20 | +
|
| 21 | +A high-level API to programmatically generate Linux input events. |
| 22 | +
|
| 23 | +Example usage: |
| 24 | +>>> import uinput |
| 25 | +>>> driver = uinput.Driver() |
| 26 | +>>> driver.move_pointer(100, 100) |
| 27 | +>>> driver.click(uinput.codes.BTN_LEFT) |
| 28 | +""" |
| 29 | + |
| 30 | +import _suinput |
| 31 | +from _suinput import BUS_PCI |
| 32 | +from _suinput import BUS_ISAPNP |
| 33 | +from _suinput import BUS_USB |
| 34 | +from _suinput import BUS_HIL |
| 35 | +from _suinput import BUS_BLUETOOTH |
| 36 | +from _suinput import BUS_VIRTUAL |
| 37 | +from _suinput import BUS_ISA |
| 38 | +from _suinput import BUS_I8042 |
| 39 | +from _suinput import BUS_XTKBD |
| 40 | +from _suinput import BUS_RS232 |
| 41 | +from _suinput import BUS_GAMEPORT |
| 42 | +from _suinput import BUS_PARPORT |
| 43 | +from _suinput import BUS_AMIGA |
| 44 | +from _suinput import BUS_ADB |
| 45 | +from _suinput import BUS_I2C |
| 46 | +from _suinput import BUS_HOST |
| 47 | +from _suinput import BUS_GSC |
| 48 | +from _suinput import BUS_ATARI |
| 49 | + |
| 50 | +import codes |
| 51 | + |
| 52 | +__all__ = [ |
| 53 | + "Driver", |
| 54 | + ] |
| 55 | + |
| 56 | +class Driver(object): |
| 57 | + |
| 58 | + """Device driver for the Linux uinput-system. |
| 59 | +
|
| 60 | + For the documentation of the constructor arguments, see the |
| 61 | + documentation of the corresponding properties. |
| 62 | + """ |
| 63 | + |
| 64 | + def __init__(self, name="python-uinput", bustype=_suinput.BUS_VIRTUAL, |
| 65 | + vendor=0, product=0, version=0): |
| 66 | + self._input_fd = _suinput.open(name, bustype, vendor, product, version) |
| 67 | + self._name = name |
| 68 | + self._bustype = bustype |
| 69 | + self._vendor = vendor |
| 70 | + self._product = product |
| 71 | + self._version = version |
| 72 | + |
| 73 | + @property |
| 74 | + def name(self): |
| 75 | + "Kernel name of the device." |
| 76 | + return self._name |
| 77 | + |
| 78 | + @property |
| 79 | + def bustype(self): |
| 80 | + """Bustype of the device. |
| 81 | + Must be one of the BUS_ -prefixed constant values. |
| 82 | + """ |
| 83 | + return self._bustype |
| 84 | + |
| 85 | + @property |
| 86 | + def vendor(self): |
| 87 | + """Vendor number of the device.""" |
| 88 | + return self._vendor |
| 89 | + |
| 90 | + @property |
| 91 | + def product(self): |
| 92 | + """Product number of the device.""" |
| 93 | + return self._product |
| 94 | + |
| 95 | + @property |
| 96 | + def version(self): |
| 97 | + """Version number of the device.""" |
| 98 | + return self._version |
| 99 | + |
| 100 | + def move_pointer(self, x, y): |
| 101 | + """Sends a relative pointer motion event to the event device. |
| 102 | + Values increase towards right-bottom. |
| 103 | + """ |
| 104 | + _suinput.move_pointer(self._input_fd, x, y) |
| 105 | + |
| 106 | + def press(self, uinput_code): |
| 107 | + """Sends a press event to the event device. Event is repeated after |
| 108 | + a short delay until a release event is sent. |
| 109 | +
|
| 110 | + `uinput_code` must be one of the constant values defined in |
| 111 | + uinput.codes -module. |
| 112 | + """ |
| 113 | + _suinput.press(self._input_fd, uinput_code) |
| 114 | + |
| 115 | + def release(self, uinput_code): |
| 116 | + """Sends a release event to the event device. |
| 117 | +
|
| 118 | + `uinput_code` must be one of the constant values defined in |
| 119 | + uinput.codes -module. |
| 120 | + """ |
| 121 | + _suinput.release(self._input_fd, uinput_code) |
| 122 | + |
| 123 | + def click(self, uinput_code): |
| 124 | + """Sends a press and release events to the event device. |
| 125 | +
|
| 126 | + `uinput_code` must be one of the constant values defined in |
| 127 | + uinput.codes -module. |
| 128 | +
|
| 129 | + This method is provided as a convenience and has effectively the |
| 130 | + same result as calling press(uinput_fd) and release(uinput_fd) |
| 131 | + sequentially. |
| 132 | + """ |
| 133 | + _suinput.click(self._input_fd, uinput_code) |
| 134 | + |
| 135 | + def __del__(self): |
| 136 | + _suinput.close(self._input_fd) |
0 commit comments