Support turbo2.
[vuplus_dvbapp] / lib / python / Components / AVSwitch.py
1 from config import config, ConfigSlider, ConfigSelection, ConfigYesNo, \
2         ConfigEnableDisable, ConfigSubsection, ConfigBoolean, ConfigSelectionNumber, ConfigNothing, NoSave
3 from enigma import eAVSwitch, getDesktop
4 from SystemInfo import SystemInfo
5 from os import path as os_path
6 from os import access, W_OK
7
8 class AVSwitch:
9         def setInput(self, input):
10                 INPUT = { "ENCODER": 0, "SCART": 1, "AUX": 2 }
11                 eAVSwitch.getInstance().setInput(INPUT[input])
12
13         def setColorFormat(self, value):
14                 eAVSwitch.getInstance().setColorFormat(value)
15
16         def setAspectRatio(self, value):
17                 eAVSwitch.getInstance().setAspectRatio(value)
18
19         def setSystem(self, value):
20                 eAVSwitch.getInstance().setVideomode(value)
21
22         def getOutputAspect(self):
23                 valstr = config.av.aspectratio.value
24                 if valstr in ("4_3_letterbox", "4_3_panscan"): # 4:3
25                         return (4,3)
26                 elif valstr == "16_9": # auto ... 4:3 or 16:9
27                         try:
28                                 aspect_str = open("/proc/stb/vmpeg/0/aspect", "r").read()
29                                 if aspect_str == "1": # 4:3
30                                         return (4,3)
31                         except IOError:
32                                 pass
33                 elif valstr in ("16_9_always", "16_9_letterbox"): # 16:9
34                         pass
35                 elif valstr in ("16_10_letterbox", "16_10_panscan"): # 16:10
36                         return (16,10)
37                 return (16,9)
38
39         def getFramebufferScale(self):
40                 aspect = self.getOutputAspect()
41                 fb_size = getDesktop(0).size()
42                 return (aspect[0] * fb_size.height(), aspect[1] * fb_size.width())
43
44         def getAspectRatioSetting(self):
45                 valstr = config.av.aspectratio.value
46                 if valstr == "4_3_letterbox":
47                         val = 0
48                 elif valstr == "4_3_panscan":
49                         val = 1
50                 elif valstr == "16_9":
51                         val = 2
52                 elif valstr == "16_9_always":
53                         val = 3
54                 elif valstr == "16_10_letterbox":
55                         val = 4
56                 elif valstr == "16_10_panscan":
57                         val = 5
58                 elif valstr == "16_9_letterbox":
59                         val = 6
60                 return val
61
62         def setAspectWSS(self, aspect=None):
63                 if not config.av.wss.value:
64                         value = 2 # auto(4:3_off)
65                 else:
66                         value = 1 # auto
67                 eAVSwitch.getInstance().setWSS(value)
68
69 def InitAVSwitch():
70         config.av = ConfigSubsection()
71         config.av.yuvenabled = ConfigBoolean(default=False)
72         colorformat_choices = {"cvbs": _("CVBS"), "rgb": _("RGB"), "svideo": _("S-Video")}
73         
74         # when YUV is not enabled, don't let the user select it
75         if config.av.yuvenabled.value:
76                 colorformat_choices["yuv"] = _("YPbPr")
77
78         config.av.colorformat = ConfigSelection(choices=colorformat_choices, default="cvbs")
79         config.av.aspectratio = ConfigSelection(choices={
80                         "4_3_letterbox": _("4:3 Letterbox"),
81                         "4_3_panscan": _("4:3 PanScan"), 
82                         "16_9": _("16:9"), 
83                         "16_9_always": _("16:9 always"),
84                         "16_10_letterbox": _("16:10 Letterbox"),
85                         "16_10_panscan": _("16:10 PanScan"), 
86                         "16_9_letterbox": _("16:9 Letterbox")}, 
87                         default = "4_3_letterbox")
88
89         config.av.aspect = ConfigSelection(choices={
90                         "4_3": _("4:3"),
91                         "16_9": _("16:9"), 
92                         "16_10": _("16:10"),
93                         "auto": _("Automatic")},
94                         default = "auto")
95         config.av.policy_169 = ConfigSelection(choices={
96                                 # TRANSLATORS: (aspect ratio policy: black bars on top/bottom) in doubt, keep english term.
97                         "letterbox": _("Letterbox"), 
98                                 # TRANSLATORS: (aspect ratio policy: cropped content on left/right) in doubt, keep english term
99                         "panscan": _("Pan&Scan"),  
100                                 # TRANSLATORS: (aspect ratio policy: display as fullscreen, even if this breaks the aspect)
101                         "scale": _("Just Scale")},
102                         default = "letterbox")
103         config.av.policy_43 = ConfigSelection(choices={
104                                 # TRANSLATORS: (aspect ratio policy: black bars on left/right) in doubt, keep english term.
105                         "pillarbox": _("Pillarbox"), 
106                                 # TRANSLATORS: (aspect ratio policy: cropped content on left/right) in doubt, keep english term
107                         "panscan": _("Pan&Scan"),  
108                                 # TRANSLATORS: (aspect ratio policy: display as fullscreen, with stretching the left/right)
109                         "nonlinear": _("Nonlinear"),  
110                                 # TRANSLATORS: (aspect ratio policy: display as fullscreen, even if this breaks the aspect)
111                         "scale": _("Just Scale")},
112                         default = "pillarbox")
113         config.av.tvsystem = ConfigSelection(choices = {"pal": _("PAL"), "ntsc": _("NTSC"), "multinorm": _("multinorm")}, default="pal")
114         config.av.wss = ConfigEnableDisable(default = True)
115         config.av.defaultac3 = ConfigYesNo(default = False)
116         config.av.generalAC3delay = ConfigSelectionNumber(-1000, 1000, 25, default = 0)
117         config.av.generalPCMdelay = ConfigSelectionNumber(-1000, 1000, 25, default = 0)
118         config.av.vcrswitch = ConfigEnableDisable(default = False)
119
120         iAVSwitch = AVSwitch()
121
122         def setColorFormat(configElement):
123                 map = {"cvbs": 0, "rgb": 1, "svideo": 2, "yuv": 3}
124                 iAVSwitch.setColorFormat(map[configElement.value])
125
126         def setAspectRatio(configElement):
127                 map = {"4_3_letterbox": 0, "4_3_panscan": 1, "16_9": 2, "16_9_always": 3, "16_10_letterbox": 4, "16_10_panscan": 5, "16_9_letterbox" : 6}
128                 iAVSwitch.setAspectRatio(map[configElement.value])
129
130         def setSystem(configElement):
131                 map = {"pal": 0, "ntsc": 1, "multinorm" : 2}
132                 iAVSwitch.setSystem(map[configElement.value])
133
134         def setWSS(configElement):
135                 iAVSwitch.setAspectWSS()
136
137         # this will call the "setup-val" initial
138         config.av.colorformat.addNotifier(setColorFormat)
139         config.av.aspectratio.addNotifier(setAspectRatio)
140         config.av.tvsystem.addNotifier(setSystem)
141         config.av.wss.addNotifier(setWSS)
142
143         iAVSwitch.setInput("ENCODER") # init on startup
144         SystemInfo["ScartSwitch"] = eAVSwitch.getInstance().haveScartSwitch()
145
146         try:
147                 can_pcm_multichannel = access("/proc/stb/audio/multichannel_pcm", W_OK)
148         except:
149                 can_pcm_multichannel = False
150
151         SystemInfo["supportPcmMultichannel"] = can_pcm_multichannel
152
153         if can_pcm_multichannel:
154                 def setPCMMultichannel(configElement):
155                         open("/proc/stb/audio/multichannel_pcm", "w").write(configElement.value and "enable" or "disable")
156                 config.av.pcm_multichannel = ConfigYesNo(default = False)
157                 config.av.pcm_multichannel.addNotifier(setPCMMultichannel)
158
159         try:
160                 can_downmix = open("/proc/stb/audio/ac3_choices", "r").read()[:-1].find("downmix") != -1
161         except:
162                 can_downmix = False
163
164         SystemInfo["CanDownmixAC3"] = can_downmix
165         if can_downmix:
166                 def setAC3Downmix(configElement):
167                         open("/proc/stb/audio/ac3", "w").write(configElement.value and "downmix" or "passthrough")
168                         if SystemInfo.get("supportPcmMultichannel", False) and (not configElement.value) :
169                                 SystemInfo["CanPcmMultichannel"] = True
170                         else:
171                                 SystemInfo["CanPcmMultichannel"] = False
172
173                 config.av.downmix_ac3 = ConfigYesNo(default = True)
174                 config.av.downmix_ac3.addNotifier(setAC3Downmix)
175
176         try:
177                 can_downmix_aac = open("/proc/stb/audio/aac_choices", "r").read()[:-1].find("downmix") != -1
178         except:
179                 can_downmix_aac = False
180
181         SystemInfo["CanDownmixAAC"] = can_downmix_aac
182         if can_downmix_aac:
183                 def setAACDownmix(configElement):
184                         open("/proc/stb/audio/aac", "w").write(configElement.value and "downmix" or "passthrough")
185                 config.av.downmix_aac = ConfigYesNo(default = True)
186                 config.av.downmix_aac.addNotifier(setAACDownmix)
187
188         try:
189                 can_osd_alpha = open("/proc/stb/video/alpha", "r") and True or False
190         except:
191                 can_osd_alpha = False
192
193         SystemInfo["CanChangeOsdAlpha"] = can_osd_alpha
194
195         def setAlpha(config):
196                 open("/proc/stb/video/alpha", "w").write(str(config.value))
197
198         if can_osd_alpha:
199                 config.av.osd_alpha = ConfigSlider(default=255, limits=(0,255))
200                 config.av.osd_alpha.addNotifier(setAlpha)
201
202         if os_path.exists("/proc/stb/vmpeg/0/pep_scaler_sharpness"):
203                 def setScaler_sharpness(config):
204                         myval = int(config.value)
205                         try:
206                                 print "--> setting scaler_sharpness to: %0.8X" % myval
207                                 open("/proc/stb/vmpeg/0/pep_scaler_sharpness", "w").write("%0.8X" % myval)
208                                 open("/proc/stb/vmpeg/0/pep_apply", "w").write("1")
209                         except IOError:
210                                 print "couldn't write pep_scaler_sharpness"
211
212                 config.av.scaler_sharpness = ConfigSlider(default=13, limits=(0,26))
213                 config.av.scaler_sharpness.addNotifier(setScaler_sharpness)
214         else:
215                 config.av.scaler_sharpness = NoSave(ConfigNothing())
216