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/")
36 if not evdev.startswith("event"):
41 self.fd = os_open("/dev/input/" + evdev, O_RDWR | O_NONBLOCK)
42 self.name = ioctl(self.fd, EVIOCGNAME(256), buffer)
43 self.name = self.name[:self.name.find("\0")]
45 except (IOError,OSError), err:
46 print '[iInputDevices] getInputDevices <ERROR: ioctl(EVIOCGNAME): ' + str(err) + ' >'
50 if self.name == 'dreambox front panel':
52 if self.name == "dreambox advanced remote control (native)" and config.misc.rcused.value != 0:
54 if self.name == "dreambox remote control (native)" and config.misc.rcused.value == 0:
56 self.Devices[evdev] = {'name': self.name, 'type': self.getInputDeviceType(self.name),'enabled': False, 'configuredName': None }
59 def getInputDeviceType(self,name):
60 if name.find("remote control") != -1:
62 elif name.find("keyboard") != -1:
64 elif name.find("mouse") != -1:
67 print "Unknown device type:",name
70 def getDeviceName(self, x):
71 if x in self.Devices.keys():
72 return self.Devices[x].get("name", x)
74 return "Unknown device name"
76 def getDeviceList(self):
77 return sorted(self.Devices.iterkeys())
79 def getDefaultRCdeviceName(self):
80 if config.misc.rcused.value == 0:
81 for device in self.Devices.iterkeys():
82 if self.Devices[device]["name"] == "dreambox advanced remote control (native)":
85 for device in self.Devices.iterkeys():
86 if self.Devices[device]["name"] == "dreambox remote control (native)":
89 def setDeviceAttribute(self, device, attribute, value):
90 #print "[iInputDevices] setting for device", device, "attribute", attribute, " to value", value
91 if self.Devices.has_key(device):
92 self.Devices[device][attribute] = value
94 def getDeviceAttribute(self, device, attribute):
95 if self.Devices.has_key(device):
96 if self.Devices[device].has_key(attribute):
97 return self.Devices[device][attribute]
100 def setEnabled(self, device, value):
101 oldval = self.getDeviceAttribute(device, 'enabled')
102 #print "[iInputDevices] setEnabled for device %s to %s from %s" % (device,value,oldval)
103 self.setDeviceAttribute(device, 'enabled', value)
104 if oldval is True and value is False:
105 self.setDefaults(device)
107 def setName(self, device, value):
108 #print "[iInputDevices] setName for device %s to %s" % (device,value)
109 self.setDeviceAttribute(device, 'configuredName', value)
111 #struct input_event {
112 # struct timeval time; -> ignored
113 # __u16 type; -> EV_REP (0x14)
114 # __u16 code; -> REP_DELAY (0x00) or REP_PERIOD (0x01)
115 # __s32 value; -> DEFAULTS: 700(REP_DELAY) or 100(REP_PERIOD)
118 def setDefaults(self, device):
119 print "[iInputDevices] setDefaults for device %s" % (device)
120 self.setDeviceAttribute(device, 'configuredName', None)
121 event_repeat = struct.pack('iihhi', 0, 0, 0x14, 0x01, 100)
122 event_delay = struct.pack('iihhi', 0, 0, 0x14, 0x00, 700)
123 fd = os_open("/dev/input/" + device, O_RDWR)
124 os_write(fd, event_repeat)
125 os_write(fd, event_delay)
128 def setRepeat(self, device, value): #REP_PERIOD
129 if self.getDeviceAttribute(device, 'enabled') == True:
130 print "[iInputDevices] setRepeat for device %s to %d ms" % (device,value)
131 event = struct.pack('iihhi', 0, 0, 0x14, 0x01, int(value))
132 fd = os_open("/dev/input/" + device, O_RDWR)
136 def setDelay(self, device, value): #REP_DELAY
137 if self.getDeviceAttribute(device, 'enabled') == True:
138 print "[iInputDevices] setDelay for device %s to %d ms" % (device,value)
139 event = struct.pack('iihhi', 0, 0, 0x14, 0x00, int(value))
140 fd = os_open("/dev/input/" + device, O_RDWR)
145 class InitInputDevices:
148 self.currentDevice = ""
151 def createConfig(self, *args):
152 config.inputDevices = ConfigSubsection()
153 for device in sorted(iInputDevices.Devices.iterkeys()):
154 self.currentDevice = device
155 #print "[InitInputDevices] -> creating config entry for device: %s -> %s " % (self.currentDevice, iInputDevices.Devices[device]["name"])
156 self.setupConfigEntries(self.currentDevice)
157 self.currentDevice = ""
159 def inputDevicesEnabledChanged(self,configElement):
160 if self.currentDevice != "" and iInputDevices.currentDevice == "":
161 iInputDevices.setEnabled(self.currentDevice, configElement.value)
162 elif iInputDevices.currentDevice != "":
163 iInputDevices.setEnabled(iInputDevices.currentDevice, configElement.value)
165 def inputDevicesNameChanged(self,configElement):
166 if self.currentDevice != "" and iInputDevices.currentDevice == "":
167 iInputDevices.setName(self.currentDevice, configElement.value)
168 if configElement.value != "":
169 devname = iInputDevices.getDeviceAttribute(self.currentDevice, 'name')
170 if devname != configElement.value:
171 cmd = "config.inputDevices." + self.currentDevice + ".enabled.value = False"
173 cmd = "config.inputDevices." + self.currentDevice + ".enabled.save()"
175 elif iInputDevices.currentDevice != "":
176 iInputDevices.setName(iInputDevices.currentDevice, configElement.value)
178 def inputDevicesRepeatChanged(self,configElement):
179 if self.currentDevice != "" and iInputDevices.currentDevice == "":
180 iInputDevices.setRepeat(self.currentDevice, configElement.value)
181 elif iInputDevices.currentDevice != "":
182 iInputDevices.setRepeat(iInputDevices.currentDevice, configElement.value)
184 def inputDevicesDelayChanged(self,configElement):
185 if self.currentDevice != "" and iInputDevices.currentDevice == "":
186 iInputDevices.setDelay(self.currentDevice, configElement.value)
187 elif iInputDevices.currentDevice != "":
188 iInputDevices.setDelay(iInputDevices.currentDevice, configElement.value)
190 def setupConfigEntries(self,device):
191 cmd = "config.inputDevices." + device + " = ConfigSubsection()"
193 cmd = "config.inputDevices." + device + ".enabled = ConfigYesNo(default = False)"
195 cmd = "config.inputDevices." + device + ".enabled.addNotifier(self.inputDevicesEnabledChanged,config.inputDevices." + device + ".enabled)"
197 cmd = "config.inputDevices." + device + '.name = ConfigText(default="")'
199 cmd = "config.inputDevices." + device + ".name.addNotifier(self.inputDevicesNameChanged,config.inputDevices." + device + ".name)"
201 cmd = "config.inputDevices." + device + ".repeat = ConfigSlider(default=100, increment = 10, limits=(0, 500))"
203 cmd = "config.inputDevices." + device + ".repeat.addNotifier(self.inputDevicesRepeatChanged,config.inputDevices." + device + ".repeat)"
205 cmd = "config.inputDevices." + device + ".delay = ConfigSlider(default=700, increment = 100, limits=(0, 5000))"
207 cmd = "config.inputDevices." + device + ".delay.addNotifier(self.inputDevicesDelayChanged,config.inputDevices." + device + ".delay)"
211 iInputDevices = inputDevices()