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