965a27ffe545c12a5bf02cc654279a7c921a4f58
[vuplus_dvbapp] / lib / python / Components / InputDevice.py
1 # coding: utf-8
2 from config import config, configfile, ConfigSlider, ConfigSubsection, ConfigYesNo, ConfigText
3
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
7
8 # asm-generic/ioctl.h
9 IOC_NRBITS = 8L
10 IOC_TYPEBITS = 8L
11 IOC_SIZEBITS = 14L
12 IOC_DIRBITS = 2L
13
14 IOC_NRSHIFT = 0L
15 IOC_TYPESHIFT = IOC_NRSHIFT+IOC_NRBITS
16 IOC_SIZESHIFT = IOC_TYPESHIFT+IOC_TYPEBITS
17 IOC_DIRSHIFT = IOC_SIZESHIFT+IOC_SIZEBITS
18
19 IOC_READ = 2L
20
21 def EVIOCGNAME(length):
22         return (IOC_READ<<IOC_DIRSHIFT)|(length<<IOC_SIZESHIFT)|(0x45<<IOC_TYPESHIFT)|(0x06<<IOC_NRSHIFT)
23
24
25 class inputDevices:
26
27         def __init__(self):
28                 self.Devices = {}
29                 self.currentDevice = ""
30                 self.getInputDevices()
31         
32         def getInputDevices(self):
33                 devices = listdir("/dev/input/")
34
35                 for evdev in devices:
36                         if not evdev.startswith("event"):
37                                 continue
38
39                         try:
40                                 buffer = "\0"*512
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")]
44                                 os_close(self.fd)
45                         except (IOError,OSError), err:
46                                 print '[iInputDevices] getInputDevices  <ERROR: ioctl(EVIOCGNAME): ' + str(err) + ' >'
47                                 self.name = None
48                         
49                         if self.name:
50                                 if self.name == 'dreambox front panel':
51                                         continue
52                                 if self.name == "dreambox advanced remote control (native)" and config.misc.rcused.value != 0:
53                                         continue
54                                 if self.name == "dreambox remote control (native)" and config.misc.rcused.value == 0:
55                                         continue
56                                 self.Devices[evdev] = {'name': self.name, 'type': self.getInputDeviceType(self.name),'enabled': False, 'configuredName': None }
57         
58
59         def getInputDeviceType(self,name):
60                 if name.find("remote control") != -1:
61                         return "remote"
62                 elif name.find("keyboard") != -1:
63                         return "keyboard"
64                 elif name.find("mouse") != -1:
65                         return "mouse"
66                 else:
67                         print "Unknown device type:",name
68                         return None
69                         
70         def getDeviceName(self, x):
71                 if x in self.Devices.keys():
72                         return self.Devices[x].get("name", x)
73                 else:
74                         return "Unknown device name"
75
76         def getDeviceList(self):
77                 return sorted(self.Devices.iterkeys())
78
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)":
83                                         return device
84                 else:
85                         for device in self.Devices.iterkeys():
86                                 if self.Devices[device]["name"] == "dreambox remote control (native)":
87                                         return device
88
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
93                         
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]
98                 return None
99                         
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)
106
107         def setName(self, device, value):
108                 #print "[iInputDevices] setName for device %s to %s" % (device,value)
109                 self.setDeviceAttribute(device, 'configuredName', value)
110                 
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)
116         #}; -> size = 16
117
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)
126                 os_close(fd)
127
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)
133                         os_write(fd, event)
134                         os_close(fd)
135
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)
141                         os_write(fd, event)
142                         os_close(fd)
143
144
145 class InitInputDevices:
146         
147         def __init__(self):
148                 self.currentDevice = ""
149                 self.createConfig()
150         
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 = ""
158
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)
164
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"
172                                         exec (cmd)
173                                         cmd = "config.inputDevices." + self.currentDevice + ".enabled.save()"
174                                         exec (cmd)
175                 elif iInputDevices.currentDevice != "":
176                         iInputDevices.setName(iInputDevices.currentDevice, configElement.value)
177
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)
183                 
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)
189
190         def setupConfigEntries(self,device):
191                 cmd = "config.inputDevices." + device + " = ConfigSubsection()"
192                 exec (cmd)
193                 cmd = "config.inputDevices." + device + ".enabled = ConfigYesNo(default = False)"
194                 exec (cmd)
195                 cmd = "config.inputDevices." + device + ".enabled.addNotifier(self.inputDevicesEnabledChanged,config.inputDevices." + device + ".enabled)"
196                 exec (cmd)
197                 cmd = "config.inputDevices." + device + '.name = ConfigText(default="")'
198                 exec (cmd)
199                 cmd = "config.inputDevices." + device + ".name.addNotifier(self.inputDevicesNameChanged,config.inputDevices." + device + ".name)"
200                 exec (cmd)
201                 cmd = "config.inputDevices." + device + ".repeat = ConfigSlider(default=100, increment = 10, limits=(0, 500))"
202                 exec (cmd)
203                 cmd = "config.inputDevices." + device + ".repeat.addNotifier(self.inputDevicesRepeatChanged,config.inputDevices." + device + ".repeat)"
204                 exec (cmd)
205                 cmd = "config.inputDevices." + device + ".delay = ConfigSlider(default=700, increment = 100, limits=(0, 5000))"
206                 exec (cmd)
207                 cmd = "config.inputDevices." + device + ".delay.addNotifier(self.inputDevicesDelayChanged,config.inputDevices." + device + ".delay)"
208                 exec (cmd)
209
210
211 iInputDevices = inputDevices()