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