2 from config import config, configfile, ConfigSlider, ConfigSubsection, ConfigYesNo, ConfigText
4 import struct, sys, time, errno
5 from fcntl import ioctl
6 from os import path as os_path, listdir, open as os_open, close as os_close, write as os_write, read as os_read, O_RDWR, O_NONBLOCK
15 IOC_TYPESHIFT = IOC_NRSHIFT+IOC_NRBITS
16 IOC_SIZESHIFT = IOC_TYPESHIFT+IOC_TYPEBITS
17 IOC_DIRSHIFT = IOC_SIZESHIFT+IOC_SIZEBITS
21 def EVIOCGNAME(length):
22 return (IOC_READ<<IOC_DIRSHIFT)|(length<<IOC_SIZESHIFT)|(0x45<<IOC_TYPESHIFT)|(0x06<<IOC_NRSHIFT)
29 self.currentDevice = ""
30 self.getInputDevices()
32 def getInputDevices(self):
33 devices = listdir("/dev/input/")
38 self.fd = os_open("/dev/input/" + evdev, O_RDWR | O_NONBLOCK)
39 self.name = ioctl(self.fd, EVIOCGNAME(256), buffer)
40 self.name = self.name[:self.name.find("\0")]
42 except (IOError,OSError), err:
43 print '[iInputDevices] getInputDevices <ERROR: ioctl(EVIOCGNAME): ' + str(err) + ' >'
47 if self.name == 'dreambox front panel':
49 if self.name == "dreambox advanced remote control (native)" and config.misc.rcused.value != 0:
51 if self.name == "dreambox remote control (native)" and config.misc.rcused.value == 0:
53 self.Devices[evdev] = {'name': self.name, 'type': self.getInputDeviceType(self.name),'enabled': False, 'configuredName': None }
56 def getInputDeviceType(self,name):
57 if name.find("remote control") != -1:
59 elif name.find("keyboard") != -1:
61 elif name.find("mouse") != -1:
64 print "Unknown device type:",name
67 def getDeviceName(self, x):
68 if x in self.Devices.keys():
69 return self.Devices[x].get("name", x)
71 return "Unknown device name"
73 def getDeviceList(self):
74 return sorted(self.Devices.iterkeys())
76 def getDefaultRCdeviceName(self):
77 if config.misc.rcused.value == 0:
78 for device in self.Devices.iterkeys():
79 if self.Devices[device]["name"] == "dreambox advanced remote control (native)":
82 for device in self.Devices.iterkeys():
83 if self.Devices[device]["name"] == "dreambox remote control (native)":
86 def setDeviceAttribute(self, device, attribute, value):
87 #print "[iInputDevices] setting for device", device, "attribute", attribute, " to value", value
88 if self.Devices.has_key(device):
89 self.Devices[device][attribute] = value
91 def getDeviceAttribute(self, device, attribute):
92 if self.Devices.has_key(device):
93 if self.Devices[device].has_key(attribute):
94 return self.Devices[device][attribute]
97 def setEnabled(self, device, value):
98 oldval = self.getDeviceAttribute(device, 'enabled')
99 #print "[iInputDevices] setEnabled for device %s to %s from %s" % (device,value,oldval)
100 self.setDeviceAttribute(device, 'enabled', value)
101 if oldval is True and value is False:
102 self.setDefaults(device)
104 def setName(self, device, value):
105 #print "[iInputDevices] setName for device %s to %s" % (device,value)
106 self.setDeviceAttribute(device, 'configuredName', value)
108 #struct input_event {
109 # struct timeval time; -> ignored
110 # __u16 type; -> EV_REP (0x14)
111 # __u16 code; -> REP_DELAY (0x00) or REP_PERIOD (0x01)
112 # __s32 value; -> DEFAULTS: 700(REP_DELAY) or 100(REP_PERIOD)
115 def setDefaults(self, device):
116 print "[iInputDevices] setDefaults for device %s" % (device)
117 self.setDeviceAttribute(device, 'configuredName', None)
118 event_repeat = struct.pack('iihhi', 0, 0, 0x14, 0x01, 100)
119 event_delay = struct.pack('iihhi', 0, 0, 0x14, 0x00, 700)
120 fd = os_open("/dev/input/" + device, O_RDWR)
121 os_write(fd, event_repeat)
122 os_write(fd, event_delay)
125 def setRepeat(self, device, value): #REP_PERIOD
126 if self.getDeviceAttribute(device, 'enabled') == True:
127 print "[iInputDevices] setRepeat for device %s to %d ms" % (device,value)
128 event = struct.pack('iihhi', 0, 0, 0x14, 0x01, int(value))
129 fd = os_open("/dev/input/" + device, O_RDWR)
133 def setDelay(self, device, value): #REP_DELAY
134 if self.getDeviceAttribute(device, 'enabled') == True:
135 print "[iInputDevices] setDelay for device %s to %d ms" % (device,value)
136 event = struct.pack('iihhi', 0, 0, 0x14, 0x00, int(value))
137 fd = os_open("/dev/input/" + device, O_RDWR)
142 class InitInputDevices:
145 self.currentDevice = ""
148 def createConfig(self, *args):
149 config.inputDevices = ConfigSubsection()
150 for device in sorted(iInputDevices.Devices.iterkeys()):
151 self.currentDevice = device
152 #print "[InitInputDevices] -> creating config entry for device: %s -> %s " % (self.currentDevice, iInputDevices.Devices[device]["name"])
153 self.setupConfigEntries(self.currentDevice)
154 self.currentDevice = ""
156 def inputDevicesEnabledChanged(self,configElement):
157 if self.currentDevice != "" and iInputDevices.currentDevice == "":
158 iInputDevices.setEnabled(self.currentDevice, configElement.value)
159 elif iInputDevices.currentDevice != "":
160 iInputDevices.setEnabled(iInputDevices.currentDevice, configElement.value)
162 def inputDevicesNameChanged(self,configElement):
163 if self.currentDevice != "" and iInputDevices.currentDevice == "":
164 iInputDevices.setName(self.currentDevice, configElement.value)
165 if configElement.value != "":
166 devname = iInputDevices.getDeviceAttribute(self.currentDevice, 'name')
167 if devname != configElement.value:
168 cmd = "config.inputDevices." + self.currentDevice + ".enabled.value = False"
170 cmd = "config.inputDevices." + self.currentDevice + ".enabled.save()"
172 elif iInputDevices.currentDevice != "":
173 iInputDevices.setName(iInputDevices.currentDevice, configElement.value)
175 def inputDevicesRepeatChanged(self,configElement):
176 if self.currentDevice != "" and iInputDevices.currentDevice == "":
177 iInputDevices.setRepeat(self.currentDevice, configElement.value)
178 elif iInputDevices.currentDevice != "":
179 iInputDevices.setRepeat(iInputDevices.currentDevice, configElement.value)
181 def inputDevicesDelayChanged(self,configElement):
182 if self.currentDevice != "" and iInputDevices.currentDevice == "":
183 iInputDevices.setDelay(self.currentDevice, configElement.value)
184 elif iInputDevices.currentDevice != "":
185 iInputDevices.setDelay(iInputDevices.currentDevice, configElement.value)
187 def setupConfigEntries(self,device):
188 cmd = "config.inputDevices." + device + " = ConfigSubsection()"
190 cmd = "config.inputDevices." + device + ".enabled = ConfigYesNo(default = False)"
192 cmd = "config.inputDevices." + device + ".enabled.addNotifier(self.inputDevicesEnabledChanged,config.inputDevices." + device + ".enabled)"
194 cmd = "config.inputDevices." + device + '.name = ConfigText(default="")'
196 cmd = "config.inputDevices." + device + ".name.addNotifier(self.inputDevicesNameChanged,config.inputDevices." + device + ".name)"
198 cmd = "config.inputDevices." + device + ".repeat = ConfigSlider(default=100, increment = 10, limits=(0, 500))"
200 cmd = "config.inputDevices." + device + ".repeat.addNotifier(self.inputDevicesRepeatChanged,config.inputDevices." + device + ".repeat)"
202 cmd = "config.inputDevices." + device + ".delay = ConfigSlider(default=700, increment = 100, limits=(0, 5000))"
204 cmd = "config.inputDevices." + device + ".delay.addNotifier(self.inputDevicesDelayChanged,config.inputDevices." + device + ".delay)"
208 iInputDevices = inputDevices()