cleanup PC Modes
[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                 elif config.av.aspect.value == "4_3":
82                         self.list.append(getConfigListEntry(_("Display 16:9 content as"), config.av.policy_169))
83
84 #               if config.av.videoport.value == "DVI":
85 #                       self.list.append(getConfigListEntry(_("Allow Unsupported Modes"), config.av.edid_override))
86                 if config.av.videoport.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
98                 self["config"].list = self.list
99                 self["config"].l.setList(self.list)
100
101         def keyLeft(self):
102                 ConfigListScreen.keyLeft(self)
103                 self.createSetup()
104
105         def keyRight(self):
106                 ConfigListScreen.keyRight(self)
107                 self.createSetup()
108
109         def confirm(self, confirmed):
110                 if not confirmed:
111                         self.hw.setMode(*self.last_good)
112                 else:
113                         self.keySave()
114
115         def grabLastGoodMode(self):
116                 port = config.av.videoport.value
117                 mode = config.av.videomode[port].value
118                 rate = config.av.videorate[mode].value
119                 self.last_good = (port, mode, rate)
120
121         def apply(self):
122                 port = config.av.videoport.value
123                 mode = config.av.videomode[port].value
124                 rate = config.av.videorate[mode].value
125                 if (port, mode, rate) != self.last_good:
126                         self.hw.setMode(port, mode, rate)
127                         from Screens.MessageBox import MessageBox
128                         self.session.openWithCallback(self.confirm, MessageBox, "Is this videomode ok?", MessageBox.TYPE_YESNO, timeout = 20, default = False)
129                 else:
130                         self.keySave()
131
132         # for summary:
133         def changedEntry(self):
134                 for x in self.onChangedEntry:
135                         x()
136
137         def getCurrentEntry(self):
138                 return self["config"].getCurrent()[0]
139
140         def getCurrentValue(self):
141                 return str(self["config"].getCurrent()[1].getText())
142
143         def createSummary(self):
144                 from Screens.Setup import SetupSummary
145                 return SetupSummary
146
147 class VideomodeHotplug:
148         def __init__(self, hw):
149                 self.hw = hw
150
151         def start(self):
152                 self.hw.on_hotplug.append(self.hotplug)
153
154         def stop(self):
155                 self.hw.on_hotplug.remove(self.hotplug)
156
157         def hotplug(self, what):
158                 print "hotplug detected on port '%s'" % (what)
159                 port = config.av.videoport.value
160                 mode = config.av.videomode[port].value
161                 rate = config.av.videorate[mode].value
162
163                 if not self.hw.isModeAvailable(port, mode, rate):
164                         print "mode %s/%s/%s went away!" % (port, mode, rate)
165                         modelist = self.hw.getModeList(port)
166                         if not len(modelist):
167                                 print "sorry, no other mode is available (unplug?). Doing nothing."
168                                 return
169                         mode = modelist[0][0]
170                         rate = modelist[0][1]
171                         print "setting %s/%s/%s" % (port, mode, rate)
172                         self.hw.setMode(port, mode, rate)
173
174 hotplug = None
175
176 def startHotplug():
177         global hotplug, video_hw
178         hotplug = VideomodeHotplug(video_hw)
179         hotplug.start()
180
181 def stopHotplug():
182         global hotplug
183         hotplug.stop()
184
185
186 def autostart(reason, session = None, **kwargs):
187         if session is not None:
188                 global my_global_session
189                 my_global_session = session
190                 return
191
192         if reason == 0:
193                 startHotplug()
194         elif reason == 1:
195                 stopHotplug()
196
197 def videoSetupMain(session, **kwargs):
198         session.open(VideoSetup, video_hw)
199
200 def startSetup(menuid):
201         if menuid != "system": 
202                 return [ ]
203
204         return [(_("A/V Settings") + "...", videoSetupMain, "av_setup", 40)]
205
206 def VideoWizard(*args, **kwargs):
207         from VideoWizard import VideoWizard
208         return VideoWizard(*args, **kwargs)
209
210 def Plugins(**kwargs):
211         list = [
212 #               PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc = autostart),
213                 PluginDescriptor(name=_("Video Setup"), description=_("Advanced Video Setup"), where = PluginDescriptor.WHERE_MENU, fnc=startSetup) 
214         ]
215         if config.misc.videowizardenabled.value:
216                 list.append(PluginDescriptor(name=_("Video Wizard"), where = PluginDescriptor.WHERE_WIZARD, fnc=(0, VideoWizard)))
217         return list