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