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