Merge commit 'origin/master' into tmbinc/FixTimingBugs
[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                 self.createConfig()
99 #               self.on_hotplug.append(self.createConfig)
100
101                 self.readPreferredModes()
102
103                 # take over old AVSwitch component :)
104                 from Components.AVSwitch import AVSwitch
105 #               config.av.colorformat.notifiers = [ ] 
106                 config.av.aspectratio.notifiers = [ ]
107                 config.av.tvsystem.notifiers = [ ]
108                 config.av.wss.notifiers = [ ]
109                 AVSwitch.getOutputAspect = self.getOutputAspect
110
111                 config.av.aspect.addNotifier(self.updateAspect)
112                 config.av.wss.addNotifier(self.updateAspect)
113                 config.av.policy_169.addNotifier(self.updateAspect)
114                 config.av.policy_43.addNotifier(self.updateAspect)
115
116                 # until we have the hotplug poll socket
117 #               self.timer = eTimer()
118 #               self.timer.callback.append(self.readPreferredModes)
119 #               self.timer.start(1000)
120
121         def readAvailableModes(self):
122                 try:
123                         modes = open("/proc/stb/video/videomode_choices").read()[:-1]
124                 except IOError:
125                         print "couldn't read available videomodes."
126                         self.modes_available = [ ]
127                         return
128                 self.modes_available = modes.split(' ')
129
130         def readPreferredModes(self):
131                 try:
132                         modes = open("/proc/stb/video/videomode_preferred").read()[:-1]
133                         self.modes_preferred = modes.split(' ')
134                 except IOError:
135                         print "reading preferred modes failed, using all modes"
136                         self.modes_preferred = self.modes_available
137
138                 if self.modes_preferred != self.last_modes_preferred:
139                         self.last_modes_preferred = self.modes_preferred
140                         print "hotplug on dvi"
141                         self.on_hotplug("DVI") # must be DVI
142
143         # check if a high-level mode with a given rate is available.
144         def isModeAvailable(self, port, mode, rate):
145                 rate = self.rates[mode][rate]
146                 for mode in rate.values():
147                         # DVI modes must be in "modes_preferred"
148 #                       if port == "DVI":
149 #                               if mode not in self.modes_preferred and not config.av.edid_override.value:
150 #                                       print "no, not preferred"
151 #                                       return False
152                         if mode not in self.modes_available:
153                                 return False
154                 return True
155
156         def isWidescreenMode(self, port, mode):
157                 return mode in self.widescreen_modes
158
159         def setMode(self, port, mode, rate, force = None):
160                 print "setMode - port:", port, "mode:", mode, "rate:", rate
161                 # we can ignore "port"
162                 self.current_mode = mode
163                 self.current_port = port
164                 modes = self.rates[mode][rate]
165
166                 mode_50 = modes.get(50)
167                 mode_60 = modes.get(60)
168                 if mode_50 is None or force == 60:
169                         mode_50 = mode_60
170                 if mode_60 is None or force == 50: 
171                         mode_60 = mode_50
172
173                 try:
174                         open("/proc/stb/video/videomode_50hz", "w").write(mode_50)
175                         open("/proc/stb/video/videomode_60hz", "w").write(mode_60)
176                 except IOError:
177                         try:
178                                 # fallback if no possibility to setup 50/60 hz mode
179                                 open("/proc/stb/video/videomode", "w").write(mode_50)
180                         except IOError:
181                                 print "setting videomode failed."
182
183                 try:
184                         open("/etc/videomode", "w").write(mode_50) # use 50Hz mode (if available) for booting
185                 except IOError:
186                         print "writing initial videomode to /etc/videomode failed."
187
188                 self.updateAspect(None)
189
190         def saveMode(self, port, mode, rate):
191                 print "saveMode", port, mode, rate
192                 config.av.videoport.value = port
193                 config.av.videoport.save()
194                 config.av.videomode[port].value = mode
195                 config.av.videomode[port].save()
196                 config.av.videorate[mode].value = rate
197                 config.av.videorate[mode].save()
198
199         def isPortAvailable(self, port):
200                 # fixme
201                 return True
202
203         def isPortUsed(self, port):
204                 if port == "DVI":
205                         self.readPreferredModes()
206                         return len(self.modes_preferred) != 0
207                 else:
208                         return True
209
210         def getPortList(self):
211                 return [port for port in self.modes if self.isPortAvailable(port)]
212
213         # get a list with all modes, with all rates, for a given port.
214         def getModeList(self, port):
215                 print "getModeList for port", port
216                 res = [ ]
217                 for mode in self.modes[port]:
218                         # list all rates which are completely valid
219                         rates = [rate for rate in self.rates[mode] if self.isModeAvailable(port, mode, rate)]
220
221                         # if at least one rate is ok, add this mode
222                         if len(rates):
223                                 res.append( (mode, rates) )
224                 return res
225
226         def createConfig(self, *args):
227                 # create list of output ports
228                 portlist = self.getPortList()
229
230                 # create list of available modes
231                 config.av.videoport = ConfigSelection(choices = [(port, _(port)) for port in portlist])
232                 config.av.videomode = ConfigSubDict()
233                 config.av.videorate = ConfigSubDict()
234
235                 for port in portlist:
236                         modes = self.getModeList(port)
237                         if len(modes):
238                                 config.av.videomode[port] = ConfigSelection(choices = [mode for (mode, rates) in modes])
239                         for (mode, rates) in modes:
240                                 config.av.videorate[mode] = ConfigSelection(choices = rates)
241
242         def setConfiguredMode(self):
243                 port = config.av.videoport.value
244                 if port not in config.av.videomode:
245                         print "current port not available, not setting videomode"
246                         return
247
248                 mode = config.av.videomode[port].value
249
250                 if mode not in config.av.videorate:
251                         print "current mode not available, not setting videomode"
252                         return
253
254                 rate = config.av.videorate[mode].value
255                 self.setMode(port, mode, rate)
256
257         def updateAspect(self, cfgelement):
258                 # determine aspect = {any,4:3,16:9,16:10}
259                 # determine policy = {bestfit,letterbox,panscan,nonlinear}
260
261                 # based on;
262                 #   config.av.videoport.value: current video output device
263                 #     Scart: 
264                 #   config.av.aspect:
265                 #     4_3:            use policy_169
266                 #     16_9,16_10:     use policy_43
267                 #     auto            always "bestfit"
268                 #   config.av.policy_169
269                 #     letterbox       use letterbox
270                 #     panscan         use panscan
271                 #     scale           use bestfit
272                 #   config.av.policy_43
273                 #     pillarbox       use panscan
274                 #     panscan         use letterbox  ("panscan" is just a bad term, it's inverse-panscan)
275                 #     nonlinear       use nonlinear
276                 #     scale           use bestfit
277
278                 port = config.av.videoport.value
279                 if port not in config.av.videomode:
280                         print "current port not available, not setting videomode"
281                         return
282                 mode = config.av.videomode[port].value
283
284                 force_widescreen = self.isWidescreenMode(port, mode)
285
286                 is_widescreen = force_widescreen or config.av.aspect.value in ("16_9", "16_10")
287                 is_auto = config.av.aspect.value == "auto"
288                 policy2 = "policy" # use main policy
289
290                 if is_widescreen:
291                         if force_widescreen:
292                                 aspect = "16:9"
293                         else:
294                                 aspect = {"16_9": "16:9", "16_10": "16:10"}[config.av.aspect.value]
295                         policy = {"pillarbox": "panscan", "panscan": "letterbox", "nonlinear": "nonlinear", "scale": "bestfit"}[config.av.policy_43.value]
296                         policy2 = {"letterbox": "letterbox", "panscan": "panscan", "scale": "bestfit"}[config.av.policy_169.value]
297                 elif is_auto:
298                         aspect = "any"
299                         policy = "bestfit"
300                 else:
301                         aspect = "4:3"
302                         policy = {"letterbox": "letterbox", "panscan": "panscan", "scale": "bestfit"}[config.av.policy_169.value]
303
304                 if not config.av.wss.value:
305                         wss = "auto(4:3_off)"
306                 else:
307                         wss = "auto"
308
309                 print "-> setting aspect, policy, policy2, wss", aspect, policy, policy2, wss
310                 open("/proc/stb/video/aspect", "w").write(aspect)
311                 open("/proc/stb/video/policy", "w").write(policy)
312                 open("/proc/stb/denc/0/wss", "w").write(wss)
313                 try:
314                         open("/proc/stb/video/policy2", "w").write(policy2)
315                 except IOError:
316                         pass
317
318 config.av.edid_override = ConfigYesNo(default = False)
319 video_hw = VideoHardware()
320 video_hw.setConfiguredMode()