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