enigma2 20131001 (master) -> 20131112 (master)
[enigma2.git] / usr / lib / enigma2 / python / Plugins / SystemPlugins / Videomode / plugin.py
1 from Screens.Screen import Screen
2 from Plugins.Plugin import PluginDescriptor
3 from Components.SystemInfo import SystemInfo
4 from Components.ConfigList import ConfigListScreen
5 from Components.config import getConfigListEntry, config, ConfigBoolean, ConfigNothing
6 from Components.Sources.StaticText import StaticText
7
8 from VideoHardware import video_hw
9
10 config.misc.videowizardenabled = ConfigBoolean(default = True)
11
12 class VideoSetup(Screen, ConfigListScreen):
13
14         def __init__(self, session, hw):
15                 Screen.__init__(self, session)
16                 # for the skin: first try VideoSetup, then Setup, this allows individual skinning
17                 self.skinName = ["VideoSetup", "Setup" ]
18                 self.setup_title = _("A/V Settings")
19                 self.hw = hw
20                 self.onChangedEntry = [ ]
21
22                 # handle hotplug by re-creating setup
23                 self.onShow.append(self.startHotplug)
24                 self.onHide.append(self.stopHotplug)
25
26                 self.list = [ ]
27                 ConfigListScreen.__init__(self, self.list, session = session, on_change = self.changedEntry)
28
29                 from Components.ActionMap import ActionMap
30                 self["actions"] = ActionMap(["SetupActions"], 
31                         {
32                                 "cancel": self.keyCancel,
33                                 "save": self.apply,
34                         }, -2)
35
36                 self["key_red"] = StaticText(_("Cancel"))
37                 self["key_green"] = StaticText(_("OK"))
38
39                 self.createSetup()
40                 self.grabLastGoodMode()
41                 self.onLayoutFinish.append(self.layoutFinished)
42
43         def layoutFinished(self):
44                 self.setTitle(self.setup_title)
45
46         def startHotplug(self):
47                 self.hw.on_hotplug.append(self.createSetup)
48
49         def stopHotplug(self):
50                 self.hw.on_hotplug.remove(self.createSetup)
51
52         def createSetup(self):
53                 level = config.usage.setup_level.index
54
55                 self.list = [
56                         getConfigListEntry(_("Video Output"), config.av.videoport)
57                 ]
58
59                 (port, mode, rate) = self._getPortModeRate()
60
61                 # if we have modes for this port:
62                 if mode and rate:
63                         # add mode- and rate-selection:
64                         self.list.append(getConfigListEntry(_("Mode"), mode))
65                         if mode.value == 'PC':
66                                 self.list.append(getConfigListEntry(_("Resolution"), rate))
67                         else:
68                                 self.list.append(getConfigListEntry(_("Refresh Rate"), rate))
69
70                 # some modes (720p, 1080i) are always widescreen. Don't let the user select something here, "auto" is not what he wants.
71                 force_wide = self.hw.isWidescreenMode(port, mode and mode.value or None)
72
73                 if not force_wide:
74                         self.list.append(getConfigListEntry(_("Aspect Ratio"), config.av.aspect))
75
76                 if force_wide or config.av.aspect.value in ("16_9", "16_10"):
77                         self.list.extend((
78                                 getConfigListEntry(_("Display 4:3 content as"), config.av.policy_43),
79                                 getConfigListEntry(_("Display >16:9 content as"), config.av.policy_169)
80                         ))
81                 elif config.av.aspect.value == "4_3":
82                         self.list.append(getConfigListEntry(_("Display 16:9 content as"), config.av.policy_169))
83
84 #               if port == "DVI":
85 #                       self.list.append(getConfigListEntry(_("Allow Unsupported Modes"), config.av.edid_override))
86                 if port and port.value and port.value == "Scart":
87                         self.list.append(getConfigListEntry(_("Color Format"), config.av.colorformat))
88                         if level >= 1:
89                                 self.list.append(getConfigListEntry(_("WSS on 4:3"), config.av.wss))
90                                 if SystemInfo["ScartSwitch"]:
91                                         self.list.append(getConfigListEntry(_("Auto scart switching"), config.av.vcrswitch))
92
93                 if level >= 1:
94                         self.list.append(getConfigListEntry(_("AC3 default"), config.av.defaultac3))
95                         if SystemInfo["CanDownmixAC3"]:
96                                 self.list.append(getConfigListEntry(_("AC3 downmix"), config.av.downmix_ac3))
97                         self.list.extend((
98                                 getConfigListEntry(_("General AC3 Delay"), config.av.generalAC3delay),
99                                 getConfigListEntry(_("General PCM Delay"), config.av.generalPCMdelay)
100                         ))
101                         if SystemInfo["SupportsAC3PlusTranscode"]:
102                                 self.list.append(getConfigListEntry(_("Convert AC3+ to AC3"), config.av.convert_ac3plus))
103
104                 if SystemInfo["CanChangeOsdAlpha"]:
105                         self.list.append(getConfigListEntry(_("OSD visibility"), config.av.osd_alpha))
106
107                 if not isinstance(config.av.scaler_sharpness, ConfigNothing):
108                         self.list.append(getConfigListEntry(_("Scaler sharpness"), config.av.scaler_sharpness))
109
110                 self["config"].list = self.list
111                 self["config"].l.setList(self.list)
112
113         def keyLeft(self):
114                 ConfigListScreen.keyLeft(self)
115                 self.createSetup()
116
117         def keyRight(self):
118                 ConfigListScreen.keyRight(self)
119                 self.createSetup()
120
121         def confirm(self, confirmed):
122                 if not confirmed:
123                         config.av.videoport.value = self.last_good[0]
124                         config.av.videomode[self.last_good[0]].value = self.last_good[1]
125                         config.av.videorate[self.last_good[1]].value = self.last_good[2]
126                         self.hw.setMode(*self.last_good)
127                 else:
128                         self.keySave()
129
130         def _getPortModeRate(self):
131                 mode = None
132                 rate = None
133                 port = config.av.videoport
134                 if config.av.videomode.has_key(port.value):
135                         mode = config.av.videomode[port.value]
136                         if config.av.videorate.has_key(mode.value):
137                                 rate = config.av.videorate[mode.value]
138                 return (port, mode, rate)
139
140         def _getPortModeRateValues(self):
141                 mode = None
142                 rate = None
143                 port = config.av.videoport.value
144                 if config.av.videomode.has_key(port):
145                         mode = config.av.videomode[port].value
146                         if config.av.videorate.has_key(mode):
147                                 rate = config.av.videorate[mode].value
148                 return (port, mode, rate)
149
150         def grabLastGoodMode(self):
151                 self.last_good = self._getPortModeRateValues()
152
153         def apply(self):
154                 (port, mode, rate) = self._getPortModeRateValues()
155                 if (port, mode, rate) != self.last_good:
156                         self.hw.setMode(port, mode, rate)
157                         from Screens.MessageBox import MessageBox
158                         self.session.openWithCallback(self.confirm, MessageBox, _("Is this videomode ok?"), MessageBox.TYPE_YESNO, timeout = 20, default = False)
159                 else:
160                         self.keySave()
161
162         # for summary:
163         def changedEntry(self):
164                 for x in self.onChangedEntry:
165                         x()
166
167         def getCurrentEntry(self):
168                 return self["config"].getCurrent()[0]
169
170         def getCurrentValue(self):
171                 return str(self["config"].getCurrent()[1].getText())
172
173         def createSummary(self):
174                 from Screens.Setup import SetupSummary
175                 return SetupSummary
176
177 class VideomodeHotplug:
178         def __init__(self, hw):
179                 self.hw = hw
180
181         def start(self):
182                 self.hw.on_hotplug.append(self.hotplug)
183
184         def stop(self):
185                 self.hw.on_hotplug.remove(self.hotplug)
186
187         def hotplug(self, what):
188                 print "hotplug detected on port '%s'" % (what)
189                 (port, mode, rate) = self._getPortModeRateValues()
190
191                 if not self.hw.isModeAvailable(port, mode, rate):
192                         print "mode %s/%s/%s went away!" % (port, mode, rate)
193                         modelist = self.hw.getModeList(port)
194                         if not len(modelist):
195                                 print "sorry, no other mode is available (unplug?). Doing nothing."
196                                 return
197                         mode = modelist[0][0]
198                         rate = modelist[0][1]
199                         print "setting %s/%s/%s" % (port, mode, rate)
200                         self.hw.setMode(port, mode, rate)
201
202 hotplug = None
203
204 def startHotplug():
205         global hotplug, video_hw
206         hotplug = VideomodeHotplug(video_hw)
207         hotplug.start()
208
209 def stopHotplug():
210         global hotplug
211         hotplug.stop()
212
213
214 def autostart(reason, session = None, **kwargs):
215         if session is not None:
216                 global my_global_session
217                 my_global_session = session
218                 return
219
220         if reason == 0:
221                 startHotplug()
222         elif reason == 1:
223                 stopHotplug()
224
225 def videoSetupMain(session, **kwargs):
226         session.open(VideoSetup, video_hw)
227
228 def startSetup(menuid):
229         if menuid != "system": 
230                 return [ ]
231
232         return [(_("A/V Settings"), videoSetupMain, "av_setup", 40)]
233
234 def VideoWizard(*args, **kwargs):
235         from VideoWizard import VideoWizard
236         return VideoWizard(*args, **kwargs)
237
238 def Plugins(**kwargs):
239         list = [
240 #               PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc = autostart),
241                 PluginDescriptor(name=_("Video Setup"), description=_("Advanced Video Setup"), where = PluginDescriptor.WHERE_MENU, needsRestart = False, fnc=startSetup) 
242         ]
243         if config.misc.videowizardenabled.value:
244                 list.append(PluginDescriptor(name=_("Video Wizard"), where = PluginDescriptor.WHERE_WIZARD, needsRestart = False, fnc=(0, VideoWizard)))
245         return list