Merge remote-tracking branch 'schwerkraft/master'
[enigma2-plugins.git] / audiorestart / src / plugin.py
1 from Components.config import config, ConfigSubsection, ConfigInteger, ConfigSelection, getConfigListEntry
2 from Components.ConfigList import ConfigListScreen
3 from Components.ActionMap import NumberActionMap
4 from Components.Button import Button
5 from Components.Label import Label,MultiColorLabel
6 from Components.SystemInfo import SystemInfo
7 from enigma import eTimer
8 from Plugins.Plugin import PluginDescriptor
9 from Screens import Standby
10 from Screens.Screen import Screen
11 from __init__ import _
12 import NavigationInstance
13
14 config.plugins.AudioRestart = ConfigSubsection()
15 config.plugins.AudioRestart.restartSelection = ConfigSelection( default = "disabled", choices = [("disabled", _("disabled")), ("restart", _("after restart")), ("standby", _("after standby")), ("both", _("after restart/standby"))])
16 config.plugins.AudioRestart.restartDelay = ConfigInteger(default = 5, limits = (0,30))
17
18 PLUGIN_BASE = "AudioRestart"
19 PLUGIN_VERSION = "0.1"
20
21 class AudioRestart():
22     def __init__(self):
23         self.activateTimer = eTimer()
24         self.activateTimer_conn = self.activateTimer.timeout.connect(self.restartAudio)
25         if config.plugins.AudioRestart.restartSelection.value in ["standby", "both"]:
26             config.misc.standbyCounter.addNotifier(self.enterStandby, initial_call = False)
27         if config.plugins.AudioRestart.restartSelection.value in ["restart", "both"]:
28             self.startTimer()
29         
30     def enterStandby(self,configElement):
31         Standby.inStandby.onClose.append(self.endStandby)
32       
33     def endStandby(self):
34         self.startTimer()
35
36     def startTimer(self):
37         self.intDelay = config.plugins.AudioRestart.restartDelay.value*1000
38         print "[AudioSync] audio restart in ",self.intDelay
39         self.activateTimer.start(self.intDelay, True)
40
41     def restartAudio(self):
42         self.activateTimer.stop()
43         if self.audioIsAC3() and SystemInfo["CanDownmixAC3"] and (config.av.downmix_ac3.value == False):
44             config.av.downmix_ac3.value = True
45             config.av.downmix_ac3.save()
46             config.av.downmix_ac3.value = False
47             config.av.downmix_ac3.save()
48             print "[AudioSync] audio restarted"
49
50     def audioIsAC3(self):
51         service = NavigationInstance.instance.getCurrentService()
52         audioTracks = service and service.audioTracks()
53         blnReturn = False
54         if audioTracks is not None:
55             n = audioTracks and audioTracks.getNumberOfTracks() or 0
56             if n >= 0:
57                 selectedAudioIndex = audioTracks.getCurrentTrack()
58                 if selectedAudioIndex <= n:
59                     trackInfo = audioTracks.getTrackInfo(selectedAudioIndex)
60                     description = trackInfo.getDescription()
61                     if (description.find("AC3") != -1 or description.find("AC-3") != -1) or description.find("DTS") != -1:
62                         blnReturn = True
63         return blnReturn
64     
65 class AudioRestartSetup(ConfigListScreen, Screen):
66     skin = """
67     <screen position="center,center" size="560,400" title="Audio Restart Setup">
68       <ePixmap pixmap="~/img/button-red.png" position="0,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
69       <ePixmap pixmap="~/img/button-green.png" position="140,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
70       <ePixmap pixmap="~/img/button-yellow.png" position="280,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
71       <ePixmap pixmap="~/img/button-blue.png" position="420,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
72       <widget name="key_red" position="0,0" zPosition="1" size="140,40"
73         font="Regular;20" valign="center" halign="center" backgroundColor="#9f1313" transparent="1"
74         shadowColor="#000000" shadowOffset="-1,-1" />
75       <widget name="key_green" position="140,0" zPosition="1" size="140,40"
76         font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1"
77         shadowColor="#000000" shadowOffset="-1,-1" />
78       <widget name="key_yellow" position="280,0" zPosition="1" size="140,40"
79         font="Regular;20" valign="center" halign="center" backgroundColor="#a08500" transparent="1"
80         shadowColor="#000000" shadowOffset="-1,-1" />
81       <widget name="key_blue" position="420,0" zPosition="1" size="140,40"
82         font="Regular;20" valign="center" halign="center" backgroundColor="#18188b" transparent="1"
83         shadowColor="#000000" shadowOffset="-1,-1" />
84       <widget name="config" position="10,40" size="540,320" scrollbarMode="showOnDemand" />
85       <widget name="PluginInfo" position="10,370" size="540,20" zPosition="4" font="Regular;18" foregroundColor="#cccccc" />
86     </screen>"""
87
88     def __init__(self, session, plugin_path):
89         Screen.__init__(self, session)
90
91         # Lets get a list of elements for the config list
92         self.list = [
93             getConfigListEntry(_("Restart audio"), config.plugins.AudioRestart.restartSelection),
94             getConfigListEntry(_("Restart audio delay (in sec)"), config.plugins.AudioRestart.restartDelay)
95         ]
96         
97         ConfigListScreen.__init__(self, self.list)
98
99         self["config"].list = self.list
100
101         self.skin_path = plugin_path
102
103         # Plugin Information
104         self["PluginInfo"] = Label(_("Plugin: %(plugin)s , Version: %(version)s") %dict(plugin=PLUGIN_BASE,version=PLUGIN_VERSION))
105
106         # BUTTONS
107         self["key_red"] = Button(_("Cancel"))
108         self["key_green"] = Button(_("Save"))
109         self["key_yellow"] = Button(_(" "))
110         self["key_blue"] = Button(" ")
111
112         self["setupActions"] = NumberActionMap(["SetupActions", "ColorActions"],
113         {
114             "save": self.save,
115             "cancel": self.cancel,
116             "green": self.save,
117             "red": self.cancel,
118             "ok": self.save,
119         }, -2)
120
121     def save(self):
122         for x in self.list:
123             x[1].save()
124         self.close()
125
126     def cancel(self):
127         for x in self["config"].list:
128             x[1].cancel()
129         self.close()
130
131 def sessionstart(reason, **kwargs):
132     if reason == 0:
133         AudioRestart()
134
135 def setup(session, **kwargs):
136 #    reload(AC3setup)
137     session.open(AudioRestartSetup, plugin_path)
138         
139 def Plugins(path,**kwargs):
140     global plugin_path
141     plugin_path = path
142     pluginList = [ PluginDescriptor(name=_("Audio restart Setup"), description=_("Setup for the AudioRestart Plugin"), icon = "AudioRestart.png", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=setup)]
143     if config.plugins.AudioRestart.restartSelection.value <> "disabled":
144         pluginAutoStart = PluginDescriptor(name="Audio restart", description = _("Restart audio"), where=PluginDescriptor.WHERE_SESSIONSTART, fnc = sessionstart)
145         pluginList.append(pluginAutoStart)
146     return pluginList