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