4.2.10r1
[enigma2.git] / usr / lib / enigma2 / python / Plugins / SystemPlugins / HdmiCec / plugin.py
1 from enigma import eActionMap, eCec
2
3 from Components.HdmiCec import hdmi_cec
4 from Components.config import config
5
6 from Plugins.Plugin import PluginDescriptor
7
8 from CecConfig import CecConfig
9
10 from enigma import getExitCode
11 from Tools.Log import Log
12 from Tools.Notifications import isPendingOrVisibleNotificationID
13
14 from CecRemoteHandler import CecRemoteHandler
15
16 class Cec(object):
17         def __init__(self):
18                 self.session = None
19                 self.cec_recvStandby_conn = None
20                 self.cec_isNowActive_conn = None
21                 self._remoteHandler = None
22                 self.actionSlot = None
23                 self._idle_to_standby = False
24                 self._skip_next_poweroff_message = False
25                 self._skip_next_poweron_message = False
26                 self._started = False
27
28         def start(self, session):
29                 if self._started:
30                         return
31                 self.session = session
32                 config.misc.standbyCounter.addNotifier(self._onStandby, initial_call = False)
33                 self._cec_recvStandby_conn = hdmi_cec.instance.receivedStandby.connect(self.__receivedStandby)
34                 self._cec_isNowActive_conn = hdmi_cec.instance.isNowActive.connect(self.__receivedNowActive)
35                 self._cec_ready_conn = hdmi_cec.instance.ready.connect(self.ready)
36                 self.actionSlot = eActionMap.getInstance().bindAction('', -0x7FFFFFFF, self._onKeyPress) #highest prio
37                 self._remoteHandler = CecRemoteHandler() #the handler registeres all cec stuff itself, so no need to do anything else here
38                 self._started = True
39                 hdmi_cec.setPowerState(eCec.POWER_STATE_ON)
40
41         def ready(self):
42                 if hdmi_cec.isReady():
43                         Log.i("READY to power on!")
44                         if self._cec_ready_conn:
45                                 self._cec_ready_conn = None
46                         self.powerOn()
47
48         def __receivedStandby(self):
49                 if config.cec.receivepower.value:
50                         self._skip_next_poweroff_message = True
51                         from Screens.Standby import Standby, inStandby
52                         if not inStandby and self.session.current_dialog and self.session.current_dialog.ALLOW_SUSPEND and self.session.in_exec:
53                                 self.session.open(Standby)
54
55         def __receivedNowActive(self):
56                 if config.cec.receivepower.value:
57                         from Screens.Standby import inStandby
58                         if inStandby != None:
59                                 inStandby.Power()
60                         self.powerOn(forceOtp=True)
61                         self._skip_next_poweron_message = True
62
63         def powerOn(self, forceOtp=False):
64                 if self._skip_next_poweron_message:
65                         self._skip_next_poweron_message = False
66                         return
67                 if self.session.shutdown:
68                         self._idle_to_standby = True
69                         return
70                 hdmi_cec.setPowerState(hdmi_cec.POWER_STATE_ON)
71                 if config.cec.sendpower.value or forceOtp:
72                         print "[Cec] power on"
73                         hdmi_cec.otpEnable()
74                         if config.cec.avr_power_explicit.value:
75                                 self._remoteHandler.sendKey(5, eCec.RC_POWER_ON)
76                 if config.cec.enable_avr.value:
77                         hdmi_cec.systemAudioRequest()
78
79         def powerOff(self):
80                 if self._idle_to_standby:
81                         return
82                 if config.cec.sendpower.value:
83                         print "[Cec] power off"
84                         if self._skip_next_poweroff_message:
85                                 self._skip_next_poweroff_message = False
86                         else:
87                                 hdmi_cec.systemStandby()
88                                 if config.cec.avr_power_explicit.value:
89                                         self._remoteHandler.sendKey(5, eCec.RC_POWER_OFF)
90                                         hdmi_cec.systemStandby(target=5)
91                 hdmi_cec.setPowerState(hdmi_cec.POWER_STATE_STANDBY)
92
93         def _onStandby(self, element):
94                 from Screens.Standby import inStandby
95                 inStandby.onClose.append(self.powerOn)
96                 self.powerOff()
97
98         def _onKeyPress(self, keyid, flag):
99                 if config.cec.volume_forward.value:
100                         if flag == 0 or flag == 2:
101                                 self._remoteHandler.sendSystemAudioKey(keyid)
102                 return 0
103
104 cec = Cec()
105
106 def autostart(reason, **kwargs):
107         session = kwargs.get('session', None)
108         if session is not None:
109                 cec.start(session)
110         if reason == 0:
111                 if session is not None:
112                         if not isPendingOrVisibleNotificationID("Standby"):
113                                 cec.ready()
114         elif getExitCode() == 1: # send CEC poweroff only on complete box shutdown
115                 cec.powerOff()
116
117 def conf(session, **kwargs):
118         session.open(CecConfig)
119
120 def menu(menuid, **kwargs):
121         if menuid == "devices":
122                 return [(_("HDMI CEC"), conf, "hdmi_cec", 40)]
123         else:
124                 return []
125
126 def Plugins(**kwargs):
127         return [
128                 PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART] , fnc = autostart, weight=100),
129                 PluginDescriptor(name = "HDMI CEC", description = "Configure HDMI CEC", where = PluginDescriptor.WHERE_MENU, needsRestart = True, fnc = menu)
130                 ]