073eb14b70eb68b0c9b977e227101056f5f5b518
[openblackhole/openblackhole-enigma2.git] / lib / python / Screens / InputDeviceSetup.py
1 from Screen import Screen
2 from Screens.HelpMenu import HelpableScreen
3 from Screens.MessageBox import MessageBox
4 from Components.InputDevice import iInputDevices, iRcTypeControl
5 from Components.Sources.StaticText import StaticText
6 from Components.Sources.List import List
7 from Components.config import config, ConfigYesNo, getConfigListEntry, ConfigSelection
8 from Components.ConfigList import ConfigListScreen
9 from Components.ActionMap import ActionMap, HelpableActionMap
10 from Tools.Directories import resolveFilename, SCOPE_CURRENT_SKIN
11 from Tools.LoadPixmap import LoadPixmap
12
13 class InputDeviceSelection(Screen,HelpableScreen):
14         skin = """
15         <screen name="InputDeviceSelection" position="center,center" size="560,400" title="Select input device">
16                 <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on"/>
17                 <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on"/>
18                 <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on"/>
19                 <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" alphatest="on"/>
20                 <widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1"/>
21                 <widget source="key_green" render="Label" position="140,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1"/>
22                 <widget source="key_yellow" render="Label" position="280,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" transparent="1"/>
23                 <widget source="key_blue" render="Label" position="420,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#18188b" transparent="1"/>
24                 <widget source="list" render="Listbox" position="5,50" size="550,280" zPosition="10" scrollbarMode="showOnDemand">
25                         <convert type="TemplatedMultiContent">
26                         <!--  device, description, devicepng, divpng  -->
27                                                         {"template": [
28                                                                         MultiContentEntryPixmapAlphaTest(pos = (2, 8), size = (54, 54), png = 2), # index 3 is the interface pixmap
29                                                                         MultiContentEntryText(pos = (65, 6), size = (450, 54), font=0, flags = RT_HALIGN_LEFT|RT_VALIGN_CENTER|RT_WRAP, text = 1), # index 1 is the interfacename
30                                                                 ],
31                                                         "fonts": [gFont("Regular", 28),gFont("Regular", 20)],
32                                                         "itemHeight": 70
33                                                         }
34                         </convert>
35                 </widget>
36                 <ePixmap pixmap="skin_default/div-h.png" position="0,340" zPosition="1" size="560,2"/>
37                 <widget source="introduction" render="Label" position="0,350" size="560,50" zPosition="10" font="Regular;21" halign="center" valign="center" backgroundColor="#25062748" transparent="1"/>
38         </screen>"""
39
40
41         def __init__(self, session):
42                 Screen.__init__(self, session)
43                 HelpableScreen.__init__(self)
44
45                 self.edittext = _("Press OK to edit the settings.")
46
47                 self["key_red"] = StaticText(_("Close"))
48                 self["key_green"] = StaticText(_("Select"))
49                 self["key_yellow"] = StaticText("")
50                 self["key_blue"] = StaticText("")
51                 self["introduction"] = StaticText(self.edittext)
52
53                 self.devices = [(iInputDevices.getDeviceName(x),x) for x in iInputDevices.getDeviceList()]
54                 print "[InputDeviceSelection] found devices :->", len(self.devices),self.devices
55
56                 self["OkCancelActions"] = HelpableActionMap(self, "OkCancelActions",
57                         {
58                         "cancel": (self.close, _("Exit input device selection.")),
59                         "ok": (self.okbuttonClick, _("Select input device.")),
60                         }, -2)
61
62                 self["ColorActions"] = HelpableActionMap(self, "ColorActions",
63                         {
64                         "red": (self.close, _("Exit input device selection.")),
65                         "green": (self.okbuttonClick, _("Select input device.")),
66                         }, -2)
67
68                 self.currentIndex = 0
69                 self.list = []
70                 self["list"] = List(self.list)
71                 self.updateList()
72                 self.onLayoutFinish.append(self.layoutFinished)
73                 self.onClose.append(self.cleanup)
74
75         def layoutFinished(self):
76                 self.setTitle(_("Select input device"))
77
78         def cleanup(self):
79                 self.currentIndex = 0
80
81         def buildInterfaceList(self, device, description, type, isinputdevice = True):
82                 divpng = LoadPixmap(cached=True, path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/div-h.png"))
83                 activepng = None
84                 devicepng = None
85                 enabled = iInputDevices.getDeviceAttribute(device, 'enabled')
86
87                 if type == 'remote':
88                         if config.misc.rcused.value == 0:
89                                 if enabled:
90                                         devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_rcnew-configured.png"))
91                                 else:
92                                         devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_rcnew.png"))
93                         else:
94                                 if enabled:
95                                         devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_rcold-configured.png"))
96                                 else:
97                                         devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_rcold.png"))
98                 elif type == 'keyboard':
99                         if enabled:
100                                 devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_keyboard-configured.png"))
101                         else:
102                                 devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_keyboard.png"))
103                 elif type == 'mouse':
104                         if enabled:
105                                 devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_mouse-configured.png"))
106                         else:
107                                 devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_mouse.png"))
108                 elif isinputdevice:
109                         devicepng = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/input_rcnew.png"))
110                 return ((device, description, devicepng, divpng))
111
112         def updateList(self):
113                 self.list = []
114
115                 if iRcTypeControl.multipleRcSupported():
116                         self.list.append(self.buildInterfaceList('rctype', _('Configure remote control type'), None, False))
117
118                 for x in self.devices:
119                         dev_type = iInputDevices.getDeviceAttribute(x[1], 'type')
120                         self.list.append(self.buildInterfaceList(x[1],_(x[0]), dev_type))
121
122                 self["list"].setList(self.list)
123                 self["list"].setIndex(self.currentIndex)
124
125         def okbuttonClick(self):
126                 selection = self["list"].getCurrent()
127                 self.currentIndex = self["list"].getIndex()
128                 if selection is not None:
129                         if selection[0] == 'rctype':
130                                 self.session.open(RemoteControlType)
131                         else:
132                                 self.session.openWithCallback(self.DeviceSetupClosed, InputDeviceSetup, selection[0])
133
134         def DeviceSetupClosed(self, *ret):
135                 self.updateList()
136
137
138 class InputDeviceSetup(Screen, ConfigListScreen):
139
140         skin = """
141                 <screen name="InputDeviceSetup" position="center,center" size="560,440" title="Input device setup">
142                         <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
143                         <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
144                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on" />
145                         <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" alphatest="on" />
146                         <widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
147                         <widget source="key_green" render="Label" position="140,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
148                         <widget source="key_yellow" render="Label" position="280,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" transparent="1" />
149                         <widget source="key_blue" render="Label" position="420,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#18188b" transparent="1" />
150                         <widget name="config" position="5,50" size="550,350" scrollbarMode="showOnDemand" />
151                         <ePixmap pixmap="skin_default/div-h.png" position="0,400" zPosition="1" size="560,2" />
152                         <widget source="introduction" render="Label" position="5,410" size="550,30" zPosition="10" font="Regular;21" halign="center" valign="center" backgroundColor="#25062748" transparent="1" />
153                 </screen>"""
154
155         def __init__(self, session, device):
156                 Screen.__init__(self, session)
157                 self.inputDevice = device
158                 iInputDevices.currentDevice = self.inputDevice
159                 self.onChangedEntry = [ ]
160                 self.setup_title = _("Input device setup")
161                 self.isStepSlider = None
162                 self.enableEntry = None
163                 self.repeatEntry = None
164                 self.delayEntry = None
165                 self.nameEntry = None
166                 self.enableConfigEntry = None
167
168                 self.list = [ ]
169                 ConfigListScreen.__init__(self, self.list, session = session, on_change = self.changedEntry)
170
171                 self["actions"] = ActionMap(["SetupActions", "MenuActions"],
172                         {
173                                 "cancel": self.keyCancel,
174                                 "save": self.apply,
175                                 "menu": self.closeRecursive,
176                         }, -2)
177
178                 self["key_red"] = StaticText(_("Cancel"))
179                 self["key_green"] = StaticText(_("OK"))
180                 self["key_yellow"] = StaticText()
181                 self["key_blue"] = StaticText()
182                 self["introduction"] = StaticText()
183
184                 self.createSetup()
185                 self.onLayoutFinish.append(self.layoutFinished)
186                 self.onClose.append(self.cleanup)
187
188         def layoutFinished(self):
189                 self.setTitle(self.setup_title)
190
191         def cleanup(self):
192                 iInputDevices.currentDevice = ""
193
194         def createSetup(self):
195                 self.list = [ ]
196                 label = _("Change repeat and delay settings?")
197                 cmd = "self.enableEntry = getConfigListEntry(label, config.inputDevices." + self.inputDevice + ".enabled)"
198                 exec (cmd)
199                 label = _("Interval between keys when repeating:")
200                 cmd = "self.repeatEntry = getConfigListEntry(label, config.inputDevices." + self.inputDevice + ".repeat)"
201                 exec (cmd)
202                 label = _("Delay before key repeat starts:")
203                 cmd = "self.delayEntry = getConfigListEntry(label, config.inputDevices." + self.inputDevice + ".delay)"
204                 exec (cmd)
205                 label = _("Devicename:")
206                 cmd = "self.nameEntry = getConfigListEntry(label, config.inputDevices." + self.inputDevice + ".name)"
207                 exec (cmd)
208                 if self.enableEntry:
209                         if isinstance(self.enableEntry[1], ConfigYesNo):
210                                 self.enableConfigEntry = self.enableEntry[1]
211
212                 self.list.append(self.enableEntry)
213                 if self.enableConfigEntry:
214                         if self.enableConfigEntry.value is True:
215                                 self.list.append(self.repeatEntry)
216                                 self.list.append(self.delayEntry)
217                         else:
218                                 self.repeatEntry[1].setValue(self.repeatEntry[1].default)
219                                 self["config"].invalidate(self.repeatEntry)
220                                 self.delayEntry[1].setValue(self.delayEntry[1].default)
221                                 self["config"].invalidate(self.delayEntry)
222                                 self.nameEntry[1].setValue(self.nameEntry[1].default)
223                                 self["config"].invalidate(self.nameEntry)
224
225                 self["config"].list = self.list
226                 self["config"].l.setSeperation(600)
227                 self["config"].l.setList(self.list)
228                 if not self.selectionChanged in self["config"].onSelectionChanged:
229                         self["config"].onSelectionChanged.append(self.selectionChanged)
230                 self.selectionChanged()
231
232         def selectionChanged(self):
233                 if self["config"].getCurrent() == self.enableEntry:
234                         self["introduction"].setText(_("Current device: ") + str(iInputDevices.getDeviceAttribute(self.inputDevice, 'name')) )
235                 else:
236                         self["introduction"].setText(_("Current value: ") + self.getCurrentValue() + _(" ms"))
237
238         def newConfig(self):
239                 current = self["config"].getCurrent()
240                 if current:
241                         if current == self.enableEntry:
242                                 self.createSetup()
243
244         def keyLeft(self):
245                 ConfigListScreen.keyLeft(self)
246                 self.newConfig()
247
248         def keyRight(self):
249                 ConfigListScreen.keyRight(self)
250                 self.newConfig()
251
252         def confirm(self, confirmed):
253                 if not confirmed:
254                         print "not confirmed"
255                         return
256                 else:
257                         self.nameEntry[1].setValue(iInputDevices.getDeviceAttribute(self.inputDevice, 'name'))
258                         cmd = "config.inputDevices." + self.inputDevice + ".name.save()"
259                         exec (cmd)
260                         self.keySave()
261
262         def apply(self):
263                 self.session.openWithCallback(self.confirm, MessageBox, _("Use these input device settings?"), MessageBox.TYPE_YESNO, timeout = 20, default = True)
264
265         def cancelConfirm(self, result):
266                 if not result:
267                         return
268                 for x in self["config"].list:
269                         x[1].cancel()
270                 self.close()
271
272         def keyCancel(self):
273                 if self["config"].isChanged():
274                         self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"), MessageBox.TYPE_YESNO, timeout = 20, default = True)
275                 else:
276                         self.close()
277         # for summary:
278         def changedEntry(self):
279                 for x in self.onChangedEntry:
280                         x()
281                 self.selectionChanged()
282
283         def getCurrentEntry(self):
284                 return self["config"].getCurrent()[0]
285
286         def getCurrentValue(self):
287                 return str(self["config"].getCurrent()[1].value)
288
289         def createSummary(self):
290                 from Screens.Setup import SetupSummary
291                 return SetupSummary
292
293
294 class RemoteControlType(Screen, ConfigListScreen):
295         rcList = [
296                         ("0", _("Default")),
297                         ("4", _("DMM normal")), ("6", _("DMM advanced")),
298                         ("11", "et9x00/6500"), ("7", "et5000/6000"), ("13", "et4000"),
299                         ("14", "xp1000"),
300                         ("8", "VU+"),
301                 ]
302
303         defaultRcList = [
304                         ("et4000", 13),
305                         ("et5000", 7),
306                         ("et6000", 7),
307                         ("et6500", 11),
308                         ("et9000", 11),
309                         ("et9200", 11),
310                         ("et9500", 11),
311                         ("xp1000", 14),
312                 ]
313
314         def __init__(self, session):
315                 Screen.__init__(self, session)
316                 self.skinName = ["RemoteControlType", "Setup" ]
317
318                 self["actions"] = ActionMap(["SetupActions"],
319                 {
320                         "cancel": self.keyCancel,
321                         "save": self.keySave,
322                 }, -1)
323
324                 self["key_green"] = StaticText(_("Save"))
325                 self["key_red"] = StaticText(_("Cancel"))
326
327                 self.list = []
328                 ConfigListScreen.__init__(self, self.list, session = self.session)
329
330                 rctype = config.plugins.remotecontroltype.rctype.value
331                 self.rctype = ConfigSelection(choices = self.rcList, default = str(rctype))
332                 self.list.append(getConfigListEntry(_("Remote control type"), self.rctype))
333                 self["config"].list = self.list
334
335                 self.defaultRcType = None
336                 self.getDefaultRcType()
337
338         def getDefaultRcType(self):
339                 data = iRcTypeControl.getBoxType()
340                 for x in self.defaultRcList:
341                         if x[0] in data:
342                                 self.defaultRcType = x[1]
343                                 break
344
345         def setDefaultRcType(self):
346                 iRcTypeControl.writeRcType(self.defaultRcType)
347
348         def keySave(self):
349                 if config.plugins.remotecontroltype.rctype.value == int(self.rctype.value):
350                         self.close()
351                 else:
352                         self.setNewSetting()
353                         self.session.openWithCallback(self.keySaveCallback, MessageBox, _("Is this setting ok?"), MessageBox.TYPE_YESNO, timeout = 20, default = False)
354
355         def keySaveCallback(self, answer):
356                 if answer is False:
357                         self.restoreOldSetting()
358                 else:
359                         config.plugins.remotecontroltype.rctype.value = int(self.rctype.value)
360                         config.plugins.remotecontroltype.save()
361                         self.close()
362
363         def restoreOldSetting(self):
364                 if config.plugins.remotecontroltype.rctype.value == 0:
365                         self.setDefaultRcType()
366                 else:
367                         iRcTypeControl.writeRcType(config.plugins.remotecontroltype.rctype.value)
368
369         def setNewSetting(self):
370                 if int(self.rctype.value) == 0:
371                         self.setDefaultRcType()
372                 else:
373                         iRcTypeControl.writeRcType(int(self.rctype.value))
374
375         def keyCancel(self):
376                 self.restoreOldSetting()
377                 self.close()