Plugins/Videomode/VideoHardware.py: remove DVI-PC when not DVI-PC modes are usable
[enigma2.git] / lib / python / Plugins / SystemPlugins / Videomode / VideoHardware.py
1 from enigma import eTimer
2 from Components.config import config, ConfigSelection, ConfigSubDict, ConfigYesNo
3
4 from Tools.CList import CList
5
6 # The "VideoHardware" is the interface to /proc/stb/video.
7 # It generates hotplug events, and gives you the list of 
8 # available and preferred modes, as well as handling the currently
9 # selected mode. No other strict checking is done.
10 class VideoHardware:
11         rates = { } # high-level, use selectable modes.
12
13         modes = { }  # a list of (high-level) modes for a certain port.
14
15         rates["PAL"] =                  { "50Hz":               { 50: "pal" },
16                                                                 "60Hz":         { 60: "pal60" },
17                                                                 "multi":        { 50: "pal", 60: "pal60" } }
18
19         rates["NTSC"] =                 { "60Hz":       { 60: "ntsc" } }
20
21         rates["Multi"] =                { "multi":      { 50: "pal", 60: "ntsc" } }
22
23         rates["480i"] =                 { "60Hz":       { 60: "480i" } }
24
25         rates["576i"] =                 { "50Hz":       { 50: "576i" } }
26
27         rates["480p"] =                 { "60Hz":       { 60: "480p" } }
28
29         rates["576p"] =                 { "50Hz":       { 50: "576p" } }
30
31         rates["720p"] =                 { "50Hz":       { 50: "720p50" },
32                                                                 "60Hz":         { 60: "720p" },
33                                                                 "multi":        { 50: "720p50", 60: "720p" } }
34
35         rates["1080i"] =                { "50Hz":               { 50: "1080i50" },
36                                                                 "60Hz":         { 60: "1080i" },
37                                                                 "multi":        { 50: "1080i50", 60: "1080i" } }
38
39         rates["PC"] = { 
40                 "1024x768": { 60: "1024x768" }, # not possible on DM7025
41                 "800x600" : { 60: "800x600" },  # also not possible
42                 "720x480" : { 60: "720x480" },
43                 "720x576" : { 60: "720x576" },
44                 "1280x720": { 60: "1280x720" },
45                 "1280x720 multi": { 50: "1280x720_50", 60: "1280x720" },
46                 "1920x1080": { 60: "1920x1080"},
47                 "1920x1080 multi": { 50: "1920x1080", 60: "1920x1080_50" },
48                 "1280x1024" : { 60: "1280x1024"},
49                 "1366x768" : { 60: "1366x768"},
50                 "1366x768 multi" : { 50: "1366x768", 60: "1366x768_50" },
51                 "1280x768": { 60: "1280x768" },
52                 "640x480" : { 60: "640x480" }
53         }
54
55         modes["Scart"] = ["PAL", "NTSC", "Multi"]
56         modes["YPbPr"] = ["720p", "1080i", "576p", "480p", "576i", "480i"]
57         modes["DVI"] = ["720p", "1080i", "576p", "480p", "576i", "480i"]
58         modes["DVI-PC"] = ["PC"]
59
60         widescreen_modes = set(["720p", "1080i"])
61
62         def getOutputAspect(self):
63                 ret = (16,9)
64                 port = config.av.videoport.value
65                 if port not in config.av.videomode:
66                         print "current port not available in getOutputAspect!!! force 16:9"
67                 else:
68                         mode = config.av.videomode[port].value
69                         force_widescreen = self.isWidescreenMode(port, mode)
70                         is_widescreen = force_widescreen or config.av.aspect.value in ("16_9", "16_10")
71                         is_auto = config.av.aspect.value == "auto"
72                         if is_widescreen:
73                                 if force_widescreen:
74                                         pass
75                                 else:
76                                         aspect = {"16_9": "16:9", "16_10": "16:10"}[config.av.aspect.value]
77                                         if aspect == "16:10":
78                                                 ret = (16,10)
79                         elif is_auto:
80                                 try:
81                                         aspect_str = open("/proc/stb/vmpeg/0/aspect", "r").read()
82                                         if aspect_str == "1": # 4:3
83                                                 ret = (4,3)
84                                 except IOError:
85                                         pass
86                         else:  # 4:3
87                                 ret = (4,3)
88                 return ret
89
90         def __init__(self):
91                 self.last_modes_preferred =  [ ]
92                 self.on_hotplug = CList()
93                 self.current_mode = None
94                 self.current_port = None
95
96                 self.readAvailableModes()
97
98                 if self.modes.has_key("DVI-PC") and not self.getModeList("DVI-PC"):
99                         print "remove DVI-PC because of not existing modes"
100                         del self.modes["DVI-PC"]
101
102                 self.createConfig()
103 #               self.on_hotplug.append(self.createConfig)
104
105                 self.readPreferredModes()
106
107                 # take over old AVSwitch component :)
108                 from Components.AVSwitch import AVSwitch
109 #               config.av.colorformat.notifiers = [ ] 
110                 config.av.aspectratio.notifiers = [ ]
111                 config.av.tvsystem.notifiers = [ ]
112                 config.av.wss.notifiers = [ ]
113                 AVSwitch.getOutputAspect = self.getOutputAspect
114
115                 config.av.aspect.addNotifier(self.updateAspect)
116                 config.av.wss.addNotifier(self.updateAspect)
117                 config.av.policy_169.addNotifier(self.updateAspect)
118                 config.av.policy_43.addNotifier(self.updateAspect)
119
120                 # until we have the hotplug poll socket
121 #               self.timer = eTimer()
122 #               self.timer.callback.append(self.readPreferredModes)
123 #               self.timer.start(1000)
124
125         def readAvailableModes(self):
126                 try:
127                         modes = open("/proc/stb/video/videomode_choices").read()[:-1]
128                 except IOError:
129                         print "couldn't read available videomodes."
130                         self.modes_available = [ ]
131                         return
132                 self.modes_available = modes.split(' ')
133
134         def readPreferredModes(self):
135                 try:
136                         modes = open("/proc/stb/video/videomode_preferred").read()[:-1]
137                         self.modes_preferred = modes.split(' ')
138                 except IOError:
139                         print "reading preferred modes failed, using all modes"
140                         self.modes_preferred = self.modes_available
141
142                 if self.modes_preferred != self.last_modes_preferred:
143                         self.last_modes_preferred = self.modes_preferred
144                         print "hotplug on dvi"
145                         self.on_hotplug("DVI") # must be DVI
146
147         # check if a high-level mode with a given rate is available.
148         def isModeAvailable(self, port, mode, rate):
149                 rate = self.rates[mode][rate]
150                 for mode in rate.values():
151                         # DVI modes must be in "modes_preferred"
152 #                       if port == "DVI":
153 #                               if mode not in self.modes_preferred and not config.av.edid_override.value:
154 #                                       print "no, not preferred"
155 #                                       return False
156                         if mode not in self.modes_available:
157                                 return False
158                 return True
159
160         def isWidescreenMode(self, port, mode):
161                 return mode in self.widescreen_modes
162
163         def setMode(self, port, mode, rate, force = None):
164                 print "setMode - port:", port, "mode:", mode, "rate:", rate
165                 # we can ignore "port"
166                 self.current_mode = mode
167                 self.current_port = port
168                 modes = self.rates[mode][rate]
169
170                 mode_50 = modes.get(50)
171                 mode_60 = modes.get(60)
172                 if mode_50 is None or force == 60:
173                         mode_50 = mode_60
174                 if mode_60 is None or force == 50: 
175                         mode_60 = mode_50
176
177                 try:
178                         open("/proc/stb/video/videomode_50hz", "w").write(mode_50)
179                         open("/proc/stb/video/videomode_60hz", "w").write(mode_60)
180                 except IOError:
181                         try:
182                                 # fallback if no possibility to setup 50/60 hz mode
183                                 open("/proc/stb/video/videomode", "w").write(mode_50)
184                         except IOError:
185                                 print "setting videomode failed."
186
187                 try:
188                         open("/etc/videomode", "w").write(mode_50) # use 50Hz mode (if available) for booting
189                 except IOError:
190                         print "writing initial videomode to /etc/videomode failed."
191
192                 self.updateAspect(None)
193
194         def saveMode(self, port, mode, rate):
195                 print "saveMode", port, mode, rate
196                 config.av.videoport.value = port
197                 config.av.videoport.save()
198                 config.av.videomode[port].value = mode
199                 config.av.videomode[port].save()
200                 config.av.videorate[mode].value = rate
201                 config.av.videorate[mode].save()
202
203         def isPortAvailable(self, port):
204                 # fixme
205                 return True
206
207         def isPortUsed(self, port):
208                 if port == "DVI":
209                         self.readPreferredModes()
210                         return len(self.modes_preferred) != 0
211                 else:
212                         return True
213
214         def getPortList(self):
215                 return [port for port in self.modes if self.isPortAvailable(port)]
216
217         # get a list with all modes, with all rates, for a given port.
218         def getModeList(self, port):
219                 print "getModeList for port", port
220                 res = [ ]
221                 for mode in self.modes[port]:
222                         # list all rates which are completely valid
223                         rates = [rate for rate in self.rates[mode] if self.isModeAvailable(port, mode, rate)]
224
225                         # if at least one rate is ok, add this mode
226                         if len(rates):
227                                 res.append( (mode, rates) )
228                 return res
229
230         def createConfig(self, *args):
231                 # create list of output ports
232                 portlist = self.getPortList()
233
234                 # create list of available modes
235                 config.av.videoport = ConfigSelection(choices = [(port, _(port)) for port in portlist])
236                 config.av.videomode = ConfigSubDict()
237                 config.av.videorate = ConfigSubDict()
238
239                 for port in portlist:
240                         modes = self.getModeList(port)
241                         if len(modes):
242                                 config.av.videomode[port] = ConfigSelection(choices = [mode for (mode, rates) in modes])
243                         for (mode, rates) in modes:
244                                 config.av.videorate[mode] = ConfigSelection(choices = rates)
245
246         def setConfiguredMode(self):
247                 port = config.av.videoport.value
248                 if port not in config.av.videomode:
249                         print "current port not available, not setting videomode"
250                         return
251
252                 mode = config.av.videomode[port].value
253
254                 if mode not in config.av.videorate:
255                         print "current mode not available, not setting videomode"
256                         return
257
258                 rate = config.av.videorate[mode].value
259                 self.setMode(port, mode, rate)
260
261         def updateAspect(self, cfgelement):
262                 # determine aspect = {any,4:3,16:9,16:10}
263                 # determine policy = {bestfit,letterbox,panscan,nonlinear}
264
265                 # based on;
266                 #   config.av.videoport.value: current video output device
267                 #     Scart: 
268                 #   config.av.aspect:
269                 #     4_3:            use policy_169
270                 #     16_9,16_10:     use policy_43
271                 #     auto            always "bestfit"
272                 #   config.av.policy_169
273                 #     letterbox       use letterbox
274                 #     panscan         use panscan
275                 #     scale           use bestfit
276                 #   config.av.policy_43
277                 #     pillarbox       use panscan
278                 #     panscan         use letterbox  ("panscan" is just a bad term, it's inverse-panscan)
279                 #     nonlinear       use nonlinear
280                 #     scale           use bestfit
281
282                 port = config.av.videoport.value
283                 if port not in config.av.videomode:
284                         print "current port not available, not setting videomode"
285                         return
286                 mode = config.av.videomode[port].value
287
288                 force_widescreen = self.isWidescreenMode(port, mode)
289
290                 is_widescreen = force_widescreen or config.av.aspect.value in ("16_9", "16_10")
291                 is_auto = config.av.aspect.value == "auto"
292                 policy2 = "policy" # use main policy
293
294                 if is_widescreen:
295                         if force_widescreen:
296                                 aspect = "16:9"
297                         else:
298                                 aspect = {"16_9": "16:9", "16_10": "16:10"}[config.av.aspect.value]
299                         policy = {"pillarbox": "panscan", "panscan": "letterbox", "nonlinear": "nonlinear", "scale": "bestfit"}[config.av.policy_43.value]
300                         policy2 = {"letterbox": "letterbox", "panscan": "panscan", "scale": "bestfit"}[config.av.policy_169.value]
301                 elif is_auto:
302                         aspect = "any"
303                         policy = "bestfit"
304                 else:
305                         aspect = "4:3"
306                         policy = {"letterbox": "letterbox", "panscan": "panscan", "scale": "bestfit"}[config.av.policy_169.value]
307
308                 if not config.av.wss.value:
309                         wss = "auto(4:3_off)"
310                 else:
311                         wss = "auto"
312
313                 print "-> setting aspect, policy, policy2, wss", aspect, policy, policy2, wss
314                 open("/proc/stb/video/aspect", "w").write(aspect)
315                 open("/proc/stb/video/policy", "w").write(policy)
316                 open("/proc/stb/denc/0/wss", "w").write(wss)
317                 try:
318                         open("/proc/stb/video/policy2", "w").write(policy2)
319                 except IOError:
320                         pass
321
322 config.av.edid_override = ConfigYesNo(default = False)
323 video_hw = VideoHardware()
324 video_hw.setConfiguredMode()