InputDevice: Do not force default RC
[openblackhole/openblackhole-enigma2.git] / lib / python / Components / InputDevice.py
1 from config import config, ConfigSlider, ConfigSubsection, ConfigYesNo, ConfigText, ConfigInteger
2 from SystemInfo import SystemInfo
3 from fcntl import ioctl
4 import os
5 import struct
6
7 # asm-generic/ioctl.h
8 IOC_NRBITS = 8L
9 IOC_TYPEBITS = 8L
10 IOC_SIZEBITS = 13L
11 IOC_DIRBITS = 3L
12
13 IOC_NRSHIFT = 0L
14 IOC_TYPESHIFT = IOC_NRSHIFT+IOC_NRBITS
15 IOC_SIZESHIFT = IOC_TYPESHIFT+IOC_TYPEBITS
16 IOC_DIRSHIFT = IOC_SIZESHIFT+IOC_SIZEBITS
17
18 IOC_READ = 2L
19
20 def EVIOCGNAME(length):
21         return (IOC_READ<<IOC_DIRSHIFT)|(length<<IOC_SIZESHIFT)|(0x45<<IOC_TYPESHIFT)|(0x06<<IOC_NRSHIFT)
22
23
24 class inputDevices:
25
26         def __init__(self):
27                 self.Devices = {}
28                 self.currentDevice = ""
29                 self.getInputDevices()
30
31         def getInputDevices(self):
32                 devices = os.listdir("/dev/input/")
33
34                 for evdev in devices:
35                         try:
36                                 buffer = "\0"*512
37                                 self.fd = os.open("/dev/input/" + evdev, os.O_RDWR | os.O_NONBLOCK)
38                                 self.name = ioctl(self.fd, EVIOCGNAME(256), buffer)
39                                 self.name = self.name[:self.name.find("\0")]
40                                 os.close(self.fd)
41                         except (IOError,OSError), err:
42                                 print '[iInputDevices] getInputDevices ' + evdev + ' <ERROR: ioctl(EVIOCGNAME): ' + str(err) + ' >'
43                                 self.name = None
44
45                         if self.name:
46                                 self.Devices[evdev] = {'name': self.name, 'type': self.getInputDeviceType(self.name),'enabled': False, 'configuredName': None }
47
48
49         def getInputDeviceType(self,name):
50                 if "remote control" in name:
51                         return "remote"
52                 elif "keyboard" in name:
53                         return "keyboard"
54                 elif "mouse" in name:
55                         return "mouse"
56                 else:
57                         print "Unknown device type:",name
58                         return None
59
60         def getDeviceName(self, x):
61                 if x in self.Devices.keys():
62                         return self.Devices[x].get("name", x)
63                 else:
64                         return "Unknown device name"
65
66         def getDeviceList(self):
67                 return sorted(self.Devices.iterkeys())
68
69         def setDeviceAttribute(self, device, attribute, value):
70                 #print "[iInputDevices] setting for device", device, "attribute", attribute, " to value", value
71                 if self.Devices.has_key(device):
72                         self.Devices[device][attribute] = value
73
74         def getDeviceAttribute(self, device, attribute):
75                 if self.Devices.has_key(device):
76                         if self.Devices[device].has_key(attribute):
77                                 return self.Devices[device][attribute]
78                 return None
79
80         def setEnabled(self, device, value):
81                 oldval = self.getDeviceAttribute(device, 'enabled')
82                 #print "[iInputDevices] setEnabled for device %s to %s from %s" % (device,value,oldval)
83                 self.setDeviceAttribute(device, 'enabled', value)
84                 if oldval is True and value is False:
85                         self.setDefaults(device)
86
87         def setName(self, device, value):
88                 #print "[iInputDevices] setName for device %s to %s" % (device,value)
89                 self.setDeviceAttribute(device, 'configuredName', value)
90
91         #struct input_event {
92         #       struct timeval time;    -> ignored
93         #       __u16 type;             -> EV_REP (0x14)
94         #       __u16 code;             -> REP_DELAY (0x00) or REP_PERIOD (0x01)
95         #       __s32 value;            -> DEFAULTS: 700(REP_DELAY) or 100(REP_PERIOD)
96         #}; -> size = 16
97
98         def setDefaults(self, device):
99                 print "[iInputDevices] setDefaults for device %s" % (device)
100                 self.setDeviceAttribute(device, 'configuredName', None)
101                 event_repeat = struct.pack('iihhi', 0, 0, 0x14, 0x01, 100)
102                 event_delay = struct.pack('iihhi', 0, 0, 0x14, 0x00, 700)
103                 fd = os.open("/dev/input/" + device, os.O_RDWR)
104                 os.write(fd, event_repeat)
105                 os.write(fd, event_delay)
106                 os.close(fd)
107
108         def setRepeat(self, device, value): #REP_PERIOD
109                 if self.getDeviceAttribute(device, 'enabled') == True:
110                         print "[iInputDevices] setRepeat for device %s to %d ms" % (device,value)
111                         event = struct.pack('iihhi', 0, 0, 0x14, 0x01, int(value))
112                         fd = os.open("/dev/input/" + device, os.O_RDWR)
113                         os.write(fd, event)
114                         os.close(fd)
115
116         def setDelay(self, device, value): #REP_DELAY
117                 if self.getDeviceAttribute(device, 'enabled') == True:
118                         print "[iInputDevices] setDelay for device %s to %d ms" % (device,value)
119                         event = struct.pack('iihhi', 0, 0, 0x14, 0x00, int(value))
120                         fd = os.open("/dev/input/" + device, os.O_RDWR)
121                         os.write(fd, event)
122                         os.close(fd)
123
124
125 class InitInputDevices:
126
127         def __init__(self):
128                 self.currentDevice = ""
129                 self.createConfig()
130
131         def createConfig(self, *args):
132                 config.inputDevices = ConfigSubsection()
133                 for device in sorted(iInputDevices.Devices.iterkeys()):
134                         self.currentDevice = device
135                         #print "[InitInputDevices] -> creating config entry for device: %s -> %s  " % (self.currentDevice, iInputDevices.Devices[device]["name"])
136                         self.setupConfigEntries(self.currentDevice)
137                         self.currentDevice = ""
138
139         def inputDevicesEnabledChanged(self,configElement):
140                 if self.currentDevice != "" and iInputDevices.currentDevice == "":
141                         iInputDevices.setEnabled(self.currentDevice, configElement.value)
142                 elif iInputDevices.currentDevice != "":
143                         iInputDevices.setEnabled(iInputDevices.currentDevice, configElement.value)
144
145         def inputDevicesNameChanged(self,configElement):
146                 if self.currentDevice != "" and iInputDevices.currentDevice == "":
147                         iInputDevices.setName(self.currentDevice, configElement.value)
148                         if configElement.value != "":
149                                 devname = iInputDevices.getDeviceAttribute(self.currentDevice, 'name')
150                                 if devname != configElement.value:
151                                         cmd = "config.inputDevices." + self.currentDevice + ".enabled.value = False"
152                                         exec (cmd)
153                                         cmd = "config.inputDevices." + self.currentDevice + ".enabled.save()"
154                                         exec (cmd)
155                 elif iInputDevices.currentDevice != "":
156                         iInputDevices.setName(iInputDevices.currentDevice, configElement.value)
157
158         def inputDevicesRepeatChanged(self,configElement):
159                 if self.currentDevice != "" and iInputDevices.currentDevice == "":
160                         iInputDevices.setRepeat(self.currentDevice, configElement.value)
161                 elif iInputDevices.currentDevice != "":
162                         iInputDevices.setRepeat(iInputDevices.currentDevice, configElement.value)
163
164         def inputDevicesDelayChanged(self,configElement):
165                 if self.currentDevice != "" and iInputDevices.currentDevice == "":
166                         iInputDevices.setDelay(self.currentDevice, configElement.value)
167                 elif iInputDevices.currentDevice != "":
168                         iInputDevices.setDelay(iInputDevices.currentDevice, configElement.value)
169
170         def setupConfigEntries(self,device):
171                 cmd = "config.inputDevices." + device + " = ConfigSubsection()"
172                 exec (cmd)
173                 cmd = "config.inputDevices." + device + ".enabled = ConfigYesNo(default = False)"
174                 exec (cmd)
175                 cmd = "config.inputDevices." + device + ".enabled.addNotifier(self.inputDevicesEnabledChanged,config.inputDevices." + device + ".enabled)"
176                 exec (cmd)
177                 cmd = "config.inputDevices." + device + '.name = ConfigText(default="")'
178                 exec (cmd)
179                 cmd = "config.inputDevices." + device + ".name.addNotifier(self.inputDevicesNameChanged,config.inputDevices." + device + ".name)"
180                 exec (cmd)
181                 cmd = "config.inputDevices." + device + ".repeat = ConfigSlider(default=100, increment = 10, limits=(0, 500))"
182                 exec (cmd)
183                 cmd = "config.inputDevices." + device + ".repeat.addNotifier(self.inputDevicesRepeatChanged,config.inputDevices." + device + ".repeat)"
184                 exec (cmd)
185                 cmd = "config.inputDevices." + device + ".delay = ConfigSlider(default=700, increment = 100, limits=(0, 5000))"
186                 exec (cmd)
187                 cmd = "config.inputDevices." + device + ".delay.addNotifier(self.inputDevicesDelayChanged,config.inputDevices." + device + ".delay)"
188                 exec (cmd)
189
190
191 iInputDevices = inputDevices()
192
193
194 config.plugins.remotecontroltype = ConfigSubsection()
195 config.plugins.remotecontroltype.rctype = ConfigInteger(default = 0)
196
197 class RcTypeControl():
198         def __init__(self):
199                 if SystemInfo["RcTypeChangable"] and os.path.exists('/proc/stb/info/boxtype'):
200                         self.isSupported = True
201                         self.boxType = open('/proc/stb/info/boxtype', 'r').read().strip()
202                         if config.plugins.remotecontroltype.rctype.value != 0:
203                                 self.writeRcType(config.plugins.remotecontroltype.rctype.value)
204                 else:
205                         self.isSupported = False
206
207         def multipleRcSupported(self):
208                 return self.isSupported
209
210         def getBoxType(self):
211                 return self.boxType
212
213         def writeRcType(self, rctype):
214                 open('/proc/stb/ir/rc/type', 'w').write(rctype and '%d' % rctype or '0')
215
216 iRcTypeControl = RcTypeControl()