Merge branch 'bug_615_replace_rawfile' into experimental
[enigma2.git] / lib / python / Screens / AudioSelection.py
1 from Screen import Screen
2 from Components.ServiceEventTracker import ServiceEventTracker
3 from Components.ActionMap import ActionMap
4 from Components.ConfigList import ConfigListScreen
5 from Components.ChoiceList import ChoiceList, ChoiceEntryComponent
6 from Components.config import config, ConfigSubsection, getConfigListEntry, ConfigNothing, ConfigSelection, ConfigOnOff
7 from Components.MultiContent import MultiContentEntryText
8 from Components.Sources.List import List
9 from Components.Sources.Boolean import Boolean
10 from Components.SystemInfo import SystemInfo
11
12 from enigma import iPlayableService
13
14 from Tools.ISO639 import LanguageCodes
15 from Tools.BoundFunction import boundFunction
16 FOCUS_CONFIG, FOCUS_STREAMS = range(2)
17 [PAGE_AUDIO, PAGE_SUBTITLES] = ["audio", "subtitles"]
18
19 class AudioSelection(Screen, ConfigListScreen):
20         def __init__(self, session, infobar=None, page=PAGE_AUDIO):
21                 Screen.__init__(self, session)
22
23                 self["streams"] = List([])
24                 self["key_red"] = Boolean(False)
25                 self["key_green"] = Boolean(False)
26                 self["key_yellow"] = Boolean(True)
27                 self["key_blue"] = Boolean(False)
28                 
29                 ConfigListScreen.__init__(self, [])
30                 self.infobar = infobar or self.session.infobar
31
32                 self.__event_tracker = ServiceEventTracker(screen=self, eventmap=
33                         {
34                                 iPlayableService.evUpdatedInfo: self.__updatedInfo
35                         })
36                 self.cached_subtitle_checked = False
37                 self.__selected_subtitle = None
38         
39                 self["actions"] = ActionMap(["ColorActions", "SetupActions", "DirectionActions"],
40                 {
41                         "red": self.keyRed,
42                         "green": self.keyGreen,
43                         "yellow": self.keyYellow,
44                         "blue": self.keyBlue,
45                         "ok": self.keyOk,
46                         "cancel": self.cancel,
47                         "up": self.keyUp,
48                         "down": self.keyDown,
49                 }, -3)
50
51                 self.settings = ConfigSubsection()
52                 choicelist = [(PAGE_AUDIO,_("audio tracks")), (PAGE_SUBTITLES,_("Subtitles"))]
53                 self.settings.menupage = ConfigSelection(choices = choicelist, default=page)
54                 self.onLayoutFinish.append(self.__layoutFinished)
55
56         def __layoutFinished(self):
57                 self["config"].instance.setSelectionEnable(False)
58                 self.focus = FOCUS_STREAMS
59                 self.settings.menupage.addNotifier(self.fillList)
60
61         def fillList(self, arg=None):
62                 streams = []
63                 conflist = []
64                 selectedidx = 0
65                 
66                 service = self.session.nav.getCurrentService()
67                 self.audioTracks = audio = service and service.audioTracks()
68                 n = audio and audio.getNumberOfTracks() or 0
69                 
70                 if self.settings.menupage.getValue() == PAGE_AUDIO:
71                         self.setTitle(_("Select audio track"))
72                         if SystemInfo["CanDownmixAC3"]:
73                                 self.settings.downmix = ConfigOnOff(default=config.av.downmix_ac3.value)
74                                 self.settings.downmix.addNotifier(self.changeAC3Downmix, initial_call = False)
75                                 conflist.append(getConfigListEntry(_("AC3 downmix"), self.settings.downmix))
76                                 self["key_red"].setBoolean(True)
77
78                         if n > 0:
79                                 self.audioChannel = service.audioChannel()
80                                 choicelist = [("0",_("left")), ("1",_("stereo")), ("2", _("right"))]
81                                 self.settings.channelmode = ConfigSelection(choices = choicelist, default = str(self.audioChannel.getCurrentChannel()))
82                                 self.settings.channelmode.addNotifier(self.changeMode, initial_call = False)
83                                 conflist.append(getConfigListEntry(_("Channel"), self.settings.channelmode))
84                                 self["key_green"].setBoolean(True)
85                                 selectedAudio = self.audioTracks.getCurrentTrack()
86                                 for x in range(n):
87                                         number = str(x)
88                                         i = audio.getTrackInfo(x)
89                                         languages = i.getLanguage().split('/')
90                                         description = i.getDescription() or _("<unknown>")
91                                         selected = ""
92                                         language = ""
93
94                                         if selectedAudio == x:
95                                                 selected = _("Running")
96                                                 selectedidx = x
97
98                                         cnt = 0
99                                         for lang in languages:
100                                                 if cnt:
101                                                         language += ' / '
102                                                 if LanguageCodes.has_key(lang):
103                                                         language += LanguageCodes[lang][0]
104                                                 elif lang == "und":
105                                                         _("<unknown>")
106                                                 else:
107                                                         language += lang
108                                                 cnt += 1
109
110                                         streams.append((x, "", number, description, language, selected))
111
112                         else:
113                                 streams = []
114                                 conflist.append(('',))
115                                 self["key_green"].setBoolean(False)
116
117                 elif self.settings.menupage.getValue() == PAGE_SUBTITLES:
118                         self.setTitle(_("Subtitle selection"))
119                         conflist.append(('',))
120                         conflist.append(('',))
121                         self["key_red"].setBoolean(False)
122                         self["key_green"].setBoolean(False)
123
124                         if self.subtitlesEnabled():
125                                 sel = self.infobar.selected_subtitle
126                         else:
127                                 sel = None
128
129                         idx = 0
130                         
131                         subtitlelist = self.getSubtitleList()
132
133                         if len(subtitlelist):
134                                 for x in subtitlelist:
135                                         number = str(x[1])
136                                         description = "?"
137                                         language = _("<unknown>")
138                                         selected = ""
139
140                                         if sel and x[:4] == sel[:4]:
141                                                 selected = _("Running")
142                                                 selectedidx = idx
143                                         
144                                         if x[4] != "und":
145                                                 if LanguageCodes.has_key(x[4]):
146                                                         language = LanguageCodes[x[4]][0]
147                                                 else:
148                                                         language = x[4]
149
150                                         if x[0] == 0:
151                                                 description = "DVB"
152                                                 number = "%x" % (x[1])
153
154                                         elif x[0] == 1:
155                                                 description = "TTX"
156                                                 number = "%x%02x" % (x[3],x[2])
157
158                                         elif x[0] == 2:
159                                                 types = ("UTF-8 text","SSA / AAS",".SRT file")
160                                                 description = types[x[2]]
161
162                                         streams.append((x, "", number, description, language, selected))
163                                         idx += 1
164                         
165                         else:
166                                 streams = []
167
168                 conflist.append(getConfigListEntry(_("Menu"), self.settings.menupage))
169                 
170                 from Components.PluginComponent import plugins
171                 from Plugins.Plugin import PluginDescriptor
172                 
173                 if hasattr(self.infobar, "runPlugin"):
174                         class PluginCaller:
175                                 def __init__(self, fnc, *args):
176                                         self.fnc = fnc
177                                         self.args = args
178                                 def __call__(self, *args, **kwargs):
179                                         self.fnc(*self.args)
180
181                         Plugins = [ (p.name, PluginCaller(self.infobar.runPlugin, p)) for p in plugins.getPlugins(where = PluginDescriptor.WHERE_AUDIOMENU) ]
182
183                         if len(Plugins):
184                                 self["key_blue"].setBoolean(True)
185                                 conflist.append(getConfigListEntry(Plugins[0][0], ConfigNothing()))
186                                 self.plugincallfunc = Plugins[0][1]
187                         if len(Plugins) > 1:
188                                 print "these plugins are installed but not displayed in the dialog box:", Plugins[1:]
189
190                 self["config"].list = conflist
191                 self["config"].l.setList(conflist)
192
193                 self["streams"].list = streams
194                 self["streams"].setIndex(selectedidx)
195
196         def __updatedInfo(self):
197                 self.fillList()
198
199         def getSubtitleList(self):
200                 s = self.infobar and self.infobar.getCurrentServiceSubtitle()
201                 l = s and s.getSubtitleList() or [ ]
202                 return l
203
204         def subtitlesEnabled(self):
205                 return self.infobar.subtitles_enabled
206
207         def enableSubtitle(self, subtitles):
208                 if self.infobar.selected_subtitle != subtitles:
209                         self.infobar.subtitles_enabled = False
210                         self.infobar.selected_subtitle = subtitles
211                         if subtitles:
212                                 self.infobar.subtitles_enabled = True
213
214         def changeAC3Downmix(self, downmix):
215                 if downmix.getValue() == True:
216                         config.av.downmix_ac3.value = True
217                 else:
218                         config.av.downmix_ac3.value = False
219                 config.av.downmix_ac3.save()
220
221         def changeMode(self, mode):
222                 if mode is not None:
223                         self.audioChannel.selectChannel(int(mode.getValue()))
224
225         def changeAudio(self, audio):
226                 track = int(audio)
227                 if isinstance(track, int):
228                         if self.session.nav.getCurrentService().audioTracks().getNumberOfTracks() > track:
229                                 self.audioTracks.selectTrack(track)
230
231         def keyLeft(self):
232                 if self.focus == FOCUS_CONFIG:
233                         ConfigListScreen.keyLeft(self)
234                 elif self.focus == FOCUS_STREAMS:
235                         self["streams"].setIndex(0)
236
237         def keyRight(self, config = False):
238                 if config or self.focus == FOCUS_CONFIG:
239                         if self["config"].getCurrentIndex() < 3:
240                                 ConfigListScreen.keyRight(self)
241                         elif hasattr(self, "plugincallfunc"):
242                                 self.plugincallfunc()
243                 if self.focus == FOCUS_STREAMS and self["streams"].count() and config == False:
244                         self["streams"].setIndex(self["streams"].count()-1)
245
246         def keyRed(self):
247                 if self["key_red"].getBoolean():
248                         self.colorkey(0)
249
250         def keyGreen(self):
251                 if self["key_green"].getBoolean():
252                         self.colorkey(1)
253
254         def keyYellow(self):
255                 if self["key_yellow"].getBoolean():
256                         self.colorkey(2)
257
258         def keyBlue(self):
259                 if self["key_blue"].getBoolean():
260                         self.colorkey(3)
261
262         def colorkey(self, idx):
263                 self["config"].setCurrentIndex(idx)
264                 self.keyRight(True)
265
266         def keyUp(self):
267                 if self.focus == FOCUS_CONFIG:
268                         self["config"].instance.moveSelection(self["config"].instance.moveUp)
269                 elif self.focus == FOCUS_STREAMS:
270                         if self["streams"].getIndex() == 0:
271                                 self["config"].instance.setSelectionEnable(True)
272                                 self["streams"].style = "notselected"
273                                 self["config"].setCurrentIndex(len(self["config"].getList())-1)
274                                 self.focus = FOCUS_CONFIG
275                         else:
276                                 self["streams"].selectPrevious()
277
278         def keyDown(self):
279                 if self.focus == FOCUS_CONFIG:
280                         if self["config"].getCurrentIndex() < len(self["config"].getList())-1:
281                                 self["config"].instance.moveSelection(self["config"].instance.moveDown)
282                         else:
283                                 self["config"].instance.setSelectionEnable(False)
284                                 self["streams"].style = "default"
285                                 self.focus = FOCUS_STREAMS
286                 elif self.focus == FOCUS_STREAMS:
287                         self["streams"].selectNext()
288
289         def keyOk(self):
290                 if self.focus == FOCUS_STREAMS and self["streams"].list:
291                         cur = self["streams"].getCurrent()
292                         if self.settings.menupage.getValue() == PAGE_AUDIO and cur[0] is not None:
293                                 self.changeAudio(cur[2])
294                                 self.__updatedInfo()
295                         if self.settings.menupage.getValue() == PAGE_SUBTITLES and cur[0] is not None:
296                                 if self.infobar.selected_subtitle == cur[0]:
297                                         self.enableSubtitle(None)
298                                         selectedidx = self["streams"].getIndex()
299                                         self.__updatedInfo()
300                                         self["streams"].setIndex(selectedidx)
301                                 else:
302                                         self.enableSubtitle(cur[0])
303                                         self.__updatedInfo()
304                         self.close(0)
305                 elif self.focus == FOCUS_CONFIG:
306                         self.keyRight()
307
308         def cancel(self):
309                 self.close(0)
310
311 class SubtitleSelection(AudioSelection):
312         def __init__(self, session, infobar=None):
313                 AudioSelection.__init__(self, session, infobar, page=PAGE_SUBTITLES)
314                 self.skinName = ["AudioSelection"]