Patch by Moritz Venn: Allow to set an additional delay via config.
[enigma2.git] / lib / python / Components / AVSwitch.py
1 from config import config, ConfigSlider, ConfigSelection, ConfigYesNo, \
2         ConfigEnableDisable, ConfigSubsection, ConfigBoolean, ConfigNumber
3 from enigma import eAVSwitch, getDesktop
4 from SystemInfo import SystemInfo
5
6 class AVSwitch:
7         def setInput(self, input):
8                 INPUT = { "ENCODER": 0, "SCART": 1, "AUX": 2 }
9                 eAVSwitch.getInstance().setInput(INPUT[input])
10
11         def setColorFormat(self, value):
12                 eAVSwitch.getInstance().setColorFormat(value)
13
14         def setAspectRatio(self, value):
15                 eAVSwitch.getInstance().setAspectRatio(value)
16
17         def setSystem(self, value):
18                 eAVSwitch.getInstance().setVideomode(value)
19
20         def getOutputAspect(self):
21                 valstr = config.av.aspectratio.value
22                 if valstr in ("4_3_letterbox", "4_3_panscan"): # 4:3
23                         return (4,3)
24                 elif valstr == "16_9": # auto ... 4:3 or 16:9
25                         try:
26                                 aspect_str = open("/proc/stb/vmpeg/0/aspect", "r").read()
27                                 if aspect_str == "1": # 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=False)
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 = "4_3_letterbox")
86
87         config.av.aspect = ConfigSelection(choices={
88                         "4_3": _("4:3"),
89                         "16_9": _("16:9"), 
90                         "16_10": _("16:10"),
91                         "auto": _("Automatic")},
92                         default = "auto")
93         config.av.policy_169 = ConfigSelection(choices={
94                                 # TRANSLATORS: (aspect ratio policy: black bars on top/bottom) in doubt, keep english term.
95                         "letterbox": _("Letterbox"), 
96                                 # TRANSLATORS: (aspect ratio policy: cropped content on left/right) in doubt, keep english term
97                         "panscan": _("Pan&Scan"),  
98                                 # TRANSLATORS: (aspect ratio policy: display as fullscreen, even if this breaks the aspect)
99                         "scale": _("Just Scale")},
100                         default = "letterbox")
101         config.av.policy_43 = ConfigSelection(choices={
102                                 # TRANSLATORS: (aspect ratio policy: black bars on left/right) in doubt, keep english term.
103                         "pillarbox": _("Pillarbox"), 
104                                 # TRANSLATORS: (aspect ratio policy: cropped content on left/right) in doubt, keep english term
105                         "panscan": _("Pan&Scan"),  
106                                 # TRANSLATORS: (aspect ratio policy: display as fullscreen, with stretching the left/right)
107                         "nonlinear": _("Nonlinear"),  
108                                 # TRANSLATORS: (aspect ratio policy: display as fullscreen, even if this breaks the aspect)
109                         "scale": _("Just Scale")},
110                         default = "pillarbox")
111         config.av.tvsystem = ConfigSelection(choices = {"pal": _("PAL"), "ntsc": _("NTSC"), "multinorm": _("multinorm")}, default="pal")
112         config.av.wss = ConfigEnableDisable(default = True)
113         config.av.defaultac3 = ConfigYesNo(default = False)
114         config.av.generalAC3delay = ConfigNumber(default = 0)
115         config.av.generalPCMdelay = ConfigNumber(default = 0)
116         config.av.vcrswitch = ConfigEnableDisable(default = False)
117
118         iAVSwitch = AVSwitch()
119
120         def setColorFormat(configElement):
121                 map = {"cvbs": 0, "rgb": 1, "svideo": 2, "yuv": 3}
122                 iAVSwitch.setColorFormat(map[configElement.value])
123
124         def setAspectRatio(configElement):
125                 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}
126                 iAVSwitch.setAspectRatio(map[configElement.value])
127
128         def setSystem(configElement):
129                 map = {"pal": 0, "ntsc": 1, "multinorm" : 2}
130                 iAVSwitch.setSystem(map[configElement.value])
131
132         def setWSS(configElement):
133                 iAVSwitch.setAspectWSS()
134
135         # this will call the "setup-val" initial
136         config.av.colorformat.addNotifier(setColorFormat)
137         config.av.aspectratio.addNotifier(setAspectRatio)
138         config.av.tvsystem.addNotifier(setSystem)
139         config.av.wss.addNotifier(setWSS)
140
141         iAVSwitch.setInput("ENCODER") # init on startup
142         SystemInfo["ScartSwitch"] = eAVSwitch.getInstance().haveScartSwitch()
143
144         try:
145                 can_downmix = open("/proc/stb/audio/ac3_choices", "r").read()[:-1].find("downmix") != -1
146         except:
147                 can_downmix = False
148
149         SystemInfo["CanDownmixAC3"] = can_downmix
150         if can_downmix:
151                 def setAC3Downmix(configElement):
152                         open("/proc/stb/audio/ac3", "w").write(configElement.value and "downmix" or "passthrough")
153                 config.av.downmix_ac3 = ConfigYesNo(default = True)
154                 config.av.downmix_ac3.addNotifier(setAC3Downmix)
155
156         try:
157                 can_osd_alpha = open("/proc/stb/video/alpha", "r") and True or False
158         except:
159                 can_osd_alpha = False
160
161         SystemInfo["CanChangeOsdAlpha"] = can_osd_alpha
162
163         def setAlpha(config):
164                 open("/proc/stb/video/alpha", "w").write(str(config.value))
165
166         if can_osd_alpha:
167                 config.av.osd_alpha = ConfigSlider(default=255, limits=(0,255))
168                 config.av.osd_alpha.addNotifier(setAlpha)