Merge branch 'master' of git+ssh://scm.schwerkraft.elitedvb.net/scmrepos/git/enigma2...
[enigma2-plugins.git] / advhdmi / src / plugin.py
1 # -*- coding: utf-8 -*-
2
3 # Screens
4 from Screens.Screen import Screen
5 from Components.Label import Label
6 from Components.ActionMap import ActionMap
7 from Components.Sources.StaticText import StaticText
8 from Screens.Setup import SetupSummary
9
10 # for localized messages
11 from . import _
12
13 # Plugin
14 from Plugins.Plugin import PluginDescriptor
15 from Components.config import config, ConfigYesNo, ConfigSelection, ConfigSubsection, ConfigClock, getConfigListEntry
16 from Components.ConfigList import ConfigListScreen
17
18 # for function
19 from time import localtime, mktime
20 from Components.HdmiCec import hdmi_cec
21 # overwrite functions
22 from Plugins.SystemPlugins.HdmiCec.plugin import Cec
23 from Plugins.Extensions.WebInterface.WebComponents.Sources.RemoteControl import RemoteControl
24 from Plugins.Extensions.WebInterface.WebComponents.Sources.PowerState import PowerState
25
26 global g_AdvHdmi_sessionstared
27 global g_AdvHdmi_fromwebif
28
29 now = localtime()
30 begin1 = mktime((now.tm_year, now.tm_mon, now.tm_mday, 1, 00, 0, now.tm_wday, now.tm_yday, now.tm_isdst))
31 end1 = mktime((now.tm_year, now.tm_mon, now.tm_mday, 6, 00, 0, now.tm_wday, now.tm_yday, now.tm_isdst))
32 begin2 = mktime((now.tm_year, now.tm_mon, now.tm_mday, 8, 00, 0, now.tm_wday, now.tm_yday, now.tm_isdst))
33 end2 = mktime((now.tm_year, now.tm_mon, now.tm_mday, 16, 00, 0, now.tm_wday, now.tm_yday, now.tm_isdst))
34
35 config.plugins.AdvHdmiCec = ConfigSubsection()
36 config.plugins.AdvHdmiCec.enable = ConfigYesNo(default = False)
37 config.plugins.AdvHdmiCec.disable_after_enigmastart = ConfigYesNo(default = False)
38 config.plugins.AdvHdmiCec.disable_from_webif = ConfigYesNo(default = False)
39 config.plugins.AdvHdmiCec.time_enable = ConfigYesNo(default = False)
40 config.plugins.AdvHdmiCec.fromWD = ConfigSelection(choices=[
41                 ("0", _("Monday")),
42                 ("1", _("Tuesday")),
43                 ("2", _("Wednesday")),
44                 ("3", _("Thursday")),
45                 ("4", _("Friday")),
46                 ("5", _("Saturday")),
47                 ("6", _("Sunday")),
48         ], default = "0")
49 config.plugins.AdvHdmiCec.toWD = ConfigSelection(choices=[
50                 ("0", _("Monday")),
51                 ("1", _("Tuesday")),
52                 ("2", _("Wednesday")),
53                 ("3", _("Thursday")),
54                 ("4", _("Friday")),
55                 ("5", _("Saturday")),
56                 ("6", _("Sunday")),
57         ], default = "6")
58 config.plugins.AdvHdmiCec.begin1 = ConfigClock(default = int(begin1))
59 config.plugins.AdvHdmiCec.end1 = ConfigClock(default = int(end1))
60 config.plugins.AdvHdmiCec.timespan2_enable = ConfigYesNo(default = False)
61 config.plugins.AdvHdmiCec.begin2 = ConfigClock(default = int(begin2))
62 config.plugins.AdvHdmiCec.end2 = ConfigClock(default = int(end2))
63 config.plugins.AdvHdmiCec.show_in = ConfigSelection(choices=[
64                 ("system", _("systemmenue")),
65                 ("plugin", _("pluginmenue")),
66                 ("extension", _("extensions")),
67         ], default = "system")
68
69 class AdvHdmiCecSetup(Screen, ConfigListScreen):
70         skin = """
71                 <screen name="menu_system" position="center,center" size="550,480" title="Advanced HDMI-Cec Setup" >
72                         <widget name="config" position="10,0" size="530,310" scrollbarMode="showOnDemand" enableWrapAround="1" />
73                         <ePixmap pixmap="skin_default/div-h.png" position="0,310" zPosition="1" size="550,2" />
74                         <widget source="help" render="Label" position="5,320" size="550,120" font="Regular;21" />
75                         <ePixmap pixmap="skin_default/buttons/red.png" position="10,430" size="140,40" transparent="1" alphatest="on" />
76                         <widget render="Label" source="key_red" position="10,430" size="140,40" transparent="1" halign="center" valign="center" zPosition="2" foregroundColor="white" font="Regular;18" />
77                         <ePixmap pixmap="skin_default/buttons/green.png" position="180,430" size="140,40" transparent="1" alphatest="on" />
78                         <widget render="Label" source="key_green" position="180,430" size="140,40" transparent="1" halign="center" valign="center" zPosition="2" foregroundColor="white" font="Regular;18" />
79                 </screen>"""
80
81         def __init__(self, session):
82                 Screen.__init__(self, session)
83                 self.setup_title = _("Advanced HDMI-Cec Setup")
84                 self.onChangedEntry = []
85                 
86                 self.list = []
87                 ConfigListScreen.__init__(self, self.list, session = session, on_change = self.changed)
88                 
89                 self["key_red"] = StaticText(_("Cancel"))
90                 self["key_green"] = StaticText(_("Save"))
91                 self["help"] = StaticText()
92                 
93                 self.getConfig()
94                 
95                 def selectionChanged():
96                         current = self["config"].getCurrent()
97                         if self["config"].current != current:
98                                 if self["config"].current:
99                                         self["config"].current[1].onDeselect(self.session)
100                                 if current:
101                                         current[1].onSelect(self.session)
102                                 self["config"].current = current
103                         for x in self["config"].onSelectionChanged:
104                                 x()
105
106                 self["config"].selectionChanged = selectionChanged
107                 self["config"].onSelectionChanged.append(self.updateHelp)
108                 
109                 # Actions
110                 self["actions"] = ActionMap(["SetupActions", "AdvHdmiConfigActions"],
111                         {
112                                 "cancel": self.keyCancel,
113                                 "save": self.keySave,
114                                 "nextBouquet":self.bouquetPlus,
115                                 "prevBouquet":self.bouquetMinus,
116                         }
117                 )
118
119                 # Trigger change
120                 self.changed()
121         
122         def getConfig(self):
123                 self.list = [ getConfigListEntry(_("partially disabel HdmiCec"), config.plugins.AdvHdmiCec.enable, _("Partially disabel HDMI-Cec?\nIt can be prevented only the signals that are sent from the Dreambox. Signals received by the Dreambox will not be prevented.")) ]
124                 if config.plugins.AdvHdmiCec.enable.value:
125                         self.list.append(getConfigListEntry(_("disable at GUI-start"), config.plugins.AdvHdmiCec.disable_after_enigmastart, _("Should HDMI-Cec be disabled when GUI service startup?")))
126                         self.list.append(getConfigListEntry(_("disable from webinterface"), config.plugins.AdvHdmiCec.disable_from_webif, _("Should HDMI-Cec be disabled when the commands are sent from the web interface?")))
127                         self.list.append(getConfigListEntry(_("disable chronologically"), config.plugins.AdvHdmiCec.time_enable, _("Should HDMI-Cec be disabled at certain times?")))
128                         if config.plugins.AdvHdmiCec.time_enable.value:
129                                 self.list.append(getConfigListEntry(_("from weekday"), config.plugins.AdvHdmiCec.fromWD, _("From which day of the week, HDMI-Cec should be disabled?")))
130                                 self.list.append(getConfigListEntry(_("to weekday"), config.plugins.AdvHdmiCec.toWD, _("To what day of the week, HDMI-Cec should be disabled?")))
131                                 self.list.append(getConfigListEntry(_("from (HH:MI)"), config.plugins.AdvHdmiCec.begin1, _("At which time, HDMI-Cec should be disabled?")))
132                                 self.list.append(getConfigListEntry(_("to (HH:MI)"), config.plugins.AdvHdmiCec.end1, _("Until the time at which, HDMI-Cec should be disabled?")))
133                                 self.list.append(getConfigListEntry(_("use second timespan"), config.plugins.AdvHdmiCec.timespan2_enable, _("Do you want a further period of time to be used?")))
134                                 if config.plugins.AdvHdmiCec.timespan2_enable.value:
135                                         self.list.append(getConfigListEntry(_("from (HH:MI)"), config.plugins.AdvHdmiCec.begin2, _("Second timespan from")))
136                                         self.list.append(getConfigListEntry(_("to (HH:MI)"), config.plugins.AdvHdmiCec.end2, _("Second timespan to")))
137                 self.list.append(getConfigListEntry(_("show in"), config.plugins.AdvHdmiCec.show_in, _("Where should this setup be displayed?")))
138
139                 self["config"].list = self.list
140                 self["config"].setList(self.list)
141
142         def keyLeft(self):
143                 ConfigListScreen.keyLeft(self)
144                 self.getConfig()
145
146         def keyRight(self):
147                 ConfigListScreen.keyRight(self)
148                 self.getConfig()
149
150         def bouquetPlus(self):
151                 self["config"].instance.moveSelection(self["config"].instance.pageUp)
152
153         def bouquetMinus(self):
154                 self["config"].instance.moveSelection(self["config"].instance.pageDown)
155
156         def updateHelp(self):
157                 cur = self["config"].getCurrent()
158                 if cur:
159                         self["help"].text = cur[2]
160
161         def keySave(self):
162                 for x in self["config"].list:
163                         x[1].save()
164
165                 self.close(self.session)
166
167         def changed(self):
168                 for x in self.onChangedEntry:
169                         try:
170                                 x()
171                         except Exception:
172                                 pass
173
174 def sessionstart(reason, **kwargs):
175         global g_AdvHdmi_sessionstared
176         if reason == 0:
177                 g_AdvHdmi_sessionstared = True
178                 
179 def autostart(reason, **kwargs):
180         if reason == 0:
181                 g_AdvHdmi_sessionstared = True
182
183 def main(session, **kwargs):
184         session.open(AdvHdmiCecSetup)
185
186 def showinSetup(menuid):
187         if menuid != "system":
188                 return []
189         return [(_("Advanced HDMI Cec Setup"), main, "", 46)]
190
191 def Plugins(**kwargs):
192         global g_AdvHdmi_sessionstared
193         global g_AdvHdmi_fromwebif
194         g_AdvHdmi_sessionstared = False
195         g_AdvHdmi_fromwebif = False
196         
197         list = [
198                 PluginDescriptor(
199                         where = PluginDescriptor.WHERE_SESSIONSTART,
200                         fnc = sessionstart),
201                 PluginDescriptor(
202                         where = PluginDescriptor.WHERE_AUTOSTART,
203                         fnc = autostart)
204         ]
205         if config.plugins.AdvHdmiCec.show_in.value == "system":
206                 list.append (PluginDescriptor(
207                         name="Advanced HDMI Cec Control", 
208                         description=_("manage when HDMI Cec is enabled"), 
209                         where = PluginDescriptor.WHERE_MENU, 
210                         fnc=showinSetup)
211                 )
212         if config.plugins.AdvHdmiCec.show_in.value == "plugin":
213                 list.append (PluginDescriptor(
214                         name = "Advanced HDMI Cec Control",
215                         description = _("manage when HDMI Cec is enabled"),
216                         where = PluginDescriptor.WHERE_PLUGINMENU,
217                         fnc = main,
218                         needsRestart = False)
219                 )
220         if config.plugins.AdvHdmiCec.show_in.value == "extension":
221                 list.append (PluginDescriptor(
222                                 name = "Advanced HDMI Cec Control",
223                                 description = _("manage when HDMI Cec is enabled"),
224                                 where = PluginDescriptor.WHERE_EXTENSIONSMENU,
225                                 fnc = main,
226                                 needsRestart = False)
227                 )
228         
229         return list
230         
231 def checkTimespan(lt, begin, end):
232         # Check if we span a day
233         if begin[0] > end[0] or (begin[0] == end[0] and begin[1] >= end[1]):
234                 # Check if begin of event is later than our timespan starts
235                 if lt.tm_hour > begin[0] or (lt.tm_hour == begin[0] and lt.tm_min >= begin[1]):
236                         # If so, event is in our timespan
237                         return True
238                 # Check if begin of event is earlier than our timespan end
239                 if lt.tm_hour < end[0] or (lt.tm_hour == end[0] and lt.tm_min <= end[1]):
240                         # If so, event is in our timespan
241                         return True
242                 return False
243         else:
244                 # Check if event begins earlier than our timespan starts
245                 if lt.tm_hour < begin[0] or (lt.tm_hour == begin[0] and lt.tm_min < begin[1]):
246                         # Its out of our timespan then
247                         return False
248                 # Check if event begins later than our timespan ends
249                 if lt.tm_hour > end[0] or (lt.tm_hour == end[0] and lt.tm_min > end[1]):
250                         # Its out of our timespan then
251                         return False
252                 return True
253
254 def AdvHdiCecDOIT():
255         global g_AdvHdmi_sessionstared
256         global g_AdvHdmi_fromwebif
257         ret_val = True
258         if config.plugins.AdvHdmiCec.enable.value:
259                 if g_AdvHdmi_sessionstared and config.plugins.AdvHdmiCec.disable_after_enigmastart.value:
260                         print "[AdvHdmiCec] prevent sending HDMICec, because of enigmastart"
261                         ret_val = False
262
263                 if ret_val and g_AdvHdmi_fromwebif and config.plugins.AdvHdmiCec.disable_from_webif.value:
264                         print "[AdvHdmiCec] prevent sending HDMICec, because it was from webif"
265                         ret_val = False
266                 
267                 if ret_val and config.plugins.AdvHdmiCec.time_enable.value:
268                         lt = localtime()
269                         if int(lt[6]) < int(config.plugins.AdvHdmiCec.fromWD.getValue()) \
270                                 or int(lt[6]) > int(config.plugins.AdvHdmiCec.toWD.getValue()):
271                                         print "[AdvHdmiCec] do sending HDMICec, because of weekday"
272                         else:
273                                 if checkTimespan(lt, config.plugins.AdvHdmiCec.begin1.getValue(), config.plugins.AdvHdmiCec.end1.getValue()):
274                                         print "[AdvHdmiCec] prevent sending HDMICec, because of timespan 1"
275                                         ret_val = False
276                 
277                                 if ret_val and config.plugins.AdvHdmiCec.timespan2_enable.getValue() \
278                                         and checkTimespan(lt, config.plugins.AdvHdmiCec.begin2.getValue(), config.plugins.AdvHdmiCec.end2.getValue()):
279                                                 print "[AdvHdmiCec] prevent sending HDMICec, because of timespan 2"
280                                                 ret_val = False
281         g_AdvHdmi_sessionstared = False
282         g_AdvHdmi_fromwebif = False
283
284         return ret_val
285
286 # Overwrite CEC-Base
287 def Cec_powerOn(self):
288         if config.plugins.cec.sendpower.value and AdvHdiCecDOIT():
289                 print "[AdvHdmiCec] power on"
290                 hdmi_cec.otp_source_enable()
291
292 def Cec_powerOff(self):
293         if config.plugins.cec.sendpower.value and AdvHdiCecDOIT():
294                 print "[AdvHdmiCec] power off"
295                 hdmi_cec.ss_standby()
296
297 # Overwrite WebIf
298 def RemoteControl_handleCommand(self, cmd):
299         global g_AdvHdmi_fromwebif
300         g_AdvHdmi_fromwebif = True
301         self.cmd = cmd
302         self.res = self.sendEvent()
303
304 def PowerState_handleCommand(self, cmd):
305         global g_AdvHdmi_fromwebif
306         g_AdvHdmi_fromwebif = True
307         self.cmd = cmd
308
309 if config.plugins.AdvHdmiCec.enable.value:
310         cec = Cec()
311         print "[AdvHdmiCec] enabled"
312         Cec.powerOn = Cec_powerOn
313         Cec.powerOff = Cec_powerOff
314         RemoteControl.handleCommand = RemoteControl_handleCommand
315         PowerState.handleCommand = PowerState_handleCommand