add possibilty to hide menu entries when needed hardware is not available
[enigma2.git] / lib / driver / avswitch.cpp
1 #include <unistd.h>
2 #include <fcntl.h>
3 #include <sys/ioctl.h>
4 #include <string.h>
5
6 #include <lib/base/init.h>
7 #include <lib/base/init_num.h>
8 #include <lib/base/eerror.h>
9 #include <lib/base/ebase.h>
10 #include <lib/driver/avswitch.h>
11
12 eAVSwitch *eAVSwitch::instance = 0;
13
14 eAVSwitch::eAVSwitch()
15 {
16         ASSERT(!instance);
17         instance = this;
18         m_video_mode = 0;
19         m_fp_fd = open("/dev/dbox/fp0", O_RDONLY|O_NONBLOCK);
20         if (m_fp_fd == -1)
21         {
22                 eDebug("couldnt open /dev/dbox/fp0 to monitor vcr scart slow blanking changed!");
23                 m_fp_notifier=0;
24         }
25         else
26         {
27                 m_fp_notifier = new eSocketNotifier(eApp, m_fp_fd, eSocketNotifier::Read|POLLERR);
28                 CONNECT(m_fp_notifier->activated, eAVSwitch::fp_event);
29         }
30 }
31
32 #ifndef FP_IOCTL_GET_EVENT
33 #define FP_IOCTL_GET_EVENT 20
34 #endif
35
36 #ifndef FP_IOCTL_GET_VCR
37 #define FP_IOCTL_GET_VCR 7
38 #endif
39
40 #ifndef FP_EVENT_VCR_SB_CHANGED
41 #define FP_EVENT_VCR_SB_CHANGED 1
42 #endif
43
44 int eAVSwitch::getVCRSlowBlanking()
45 {
46         int val=0;
47         if (m_fp_fd >= 0)
48         {
49                 if (ioctl(m_fp_fd, FP_IOCTL_GET_VCR, &val) < 0)
50                         eDebug("FP_GET_VCR failed (%m)");
51         }
52         return val;
53 }
54
55 void eAVSwitch::fp_event(int what)
56 {
57         if (what & POLLERR) // driver not ready for fp polling
58         {
59                 eDebug("fp driver not read for polling.. so disable polling");
60                 m_fp_notifier->stop();
61         }
62         else
63         {
64                 int val = FP_EVENT_VCR_SB_CHANGED;  // ask only for this event
65                 if (ioctl(m_fp_fd, FP_IOCTL_GET_EVENT, &val) < 0)
66                         eDebug("FP_IOCTL_GET_EVENT failed (%m)");
67                 else if (val & FP_EVENT_VCR_SB_CHANGED)
68                         /* emit */ vcr_sb_notifier(getVCRSlowBlanking());
69         }
70 }
71
72 eAVSwitch::~eAVSwitch()
73 {
74         if ( m_fp_fd >= 0 )
75                 close(m_fp_fd);
76         if (m_fp_notifier)
77                 delete m_fp_notifier;
78 }
79
80 eAVSwitch *eAVSwitch::getInstance()
81 {
82         return instance;
83 }
84
85 bool eAVSwitch::haveScartSwitch()
86 {
87         char tmp[255];
88         int fd = open("/proc/stb/avs/0/input_choices", O_RDONLY);
89         if(fd < 0) {
90                 eDebug("cannot open /proc/stb/avs/0/input_choices");
91                 return false;
92         }
93         read(fd, tmp, 255);
94         close(fd);
95         return !!strstr(tmp, "scart");
96 }
97
98 void eAVSwitch::setInput(int val)
99 {
100         /*
101         0-encoder
102         1-scart
103         2-aux
104         */
105
106         char *input[] = {"encoder", "scart", "aux"};
107
108         int fd;
109         
110         if((fd = open("/proc/stb/avs/0/input", O_WRONLY)) < 0) {
111                 eDebug("cannot open /proc/stb/avs/0/input");
112                 return;
113         }
114
115         write(fd, input[val], strlen(input[val]));
116         close(fd);
117         
118         if (val == 1)
119                 setFastBlank(2);
120 }
121
122 void eAVSwitch::setFastBlank(int val)
123 {
124         int fd;
125         char *fb[] = {"low", "high", "vcr"};
126
127         if((fd = open("/proc/stb/avs/0/fb", O_WRONLY)) < 0) {
128                 eDebug("cannot open /proc/stb/avs/0/fb");
129                 return;
130         }
131
132         write(fd, fb[val], strlen(fb[0]));
133         close(fd);
134 }
135
136 void eAVSwitch::setColorFormat(int format)
137 {
138         /*
139         0-CVBS
140         1-RGB
141         2-S-Video
142         */
143         char *cvbs="cvbs";
144         char *rgb="rgb";
145         char *svideo="svideo";
146         char *yuv="yuv";
147         int fd;
148         
149         if((fd = open("/proc/stb/avs/0/colorformat", O_WRONLY)) < 0) {
150                 printf("cannot open /proc/stb/avs/0/colorformat\n");
151                 return;
152         }
153         switch(format) {
154                 case 0:
155                         write(fd, cvbs, strlen(cvbs));
156                         break;
157                 case 1:
158                         write(fd, rgb, strlen(rgb));
159                         break;
160                 case 2:
161                         write(fd, svideo, strlen(svideo));
162                         break;
163                 case 3:
164                         write(fd, yuv, strlen(yuv));
165                         break;
166         }       
167         close(fd);
168 }
169
170 void eAVSwitch::setAspectRatio(int ratio)
171 {
172         /*
173         0-4:3 Letterbox
174         1-4:3 PanScan
175         2-16:9
176         3-16:9 forced ("panscan")
177         4-16:10 Letterbox
178         5-16:10 PanScan
179         6-16:9 forced ("letterbox")
180         */
181         char *aspect[] = {"4:3", "4:3", "any", "16:9", "16:10", "16:10", "16:9", "16:9"};
182         char *policy[] = {"letterbox", "panscan", "bestfit", "panscan", "letterbox", "panscan", "letterbox"};
183
184         int fd;
185         if((fd = open("/proc/stb/video/aspect", O_WRONLY)) < 0) {
186                 eDebug("cannot open /proc/stb/video/aspect");
187                 return;
188         }
189 //      eDebug("set aspect to %s", aspect[ratio]);
190         write(fd, aspect[ratio], strlen(aspect[ratio]));
191         close(fd);
192
193         if((fd = open("/proc/stb/video/policy", O_WRONLY)) < 0) {
194                 eDebug("cannot open /proc/stb/video/policy");
195                 return;
196         }
197 //      eDebug("set ratio to %s", policy[ratio]);
198         write(fd, policy[ratio], strlen(policy[ratio]));
199         close(fd);
200
201 }
202
203 void eAVSwitch::setVideomode(int mode)
204 {
205         char *pal="pal";
206         char *ntsc="ntsc";
207         
208         if (mode == m_video_mode)
209                 return;
210
211         if (mode == 2)
212         {
213                 int fd1 = open("/proc/stb/video/videomode_50hz", O_WRONLY);
214                 if(fd1 < 0) {
215                         eDebug("cannot open /proc/stb/video/videomode_50hz");
216                         return;
217                 }
218                 int fd2 = open("/proc/stb/video/videomode_60hz", O_WRONLY);
219                 if(fd2 < 0) {
220                         eDebug("cannot open /proc/stb/video/videomode_60hz");
221                         close(fd1);
222                         return;
223                 }
224                 write(fd1, pal, strlen(pal));
225                 write(fd2, ntsc, strlen(ntsc));
226                 close(fd1);
227                 close(fd2);
228         }
229         else
230         {
231                 int fd = open("/proc/stb/video/videomode", O_WRONLY);
232                 if(fd < 0) {
233                         eDebug("cannot open /proc/stb/video/videomode");
234                         return;
235                 }
236                 switch(mode) {
237                         case 0:
238                                 write(fd, pal, strlen(pal));
239                                 break;
240                         case 1:
241                                 write(fd, ntsc, strlen(ntsc));
242                                 break;
243                         default:
244                                 eDebug("unknown videomode %d", mode);
245                 }
246                 close(fd);
247         }
248
249         m_video_mode = mode;
250 }
251
252 void eAVSwitch::setWSS(int val) // 0 = auto, 1 = auto(4:3_off)
253 {
254         int fd;
255         if((fd = open("/proc/stb/denc/0/wss", O_WRONLY)) < 0) {
256                 eDebug("cannot open /proc/stb/denc/0/wss");
257                 return;
258         }
259         char *wss[] = {
260                 "off", "auto", "auto(4:3_off)", "4:3_full_format", "16:9_full_format",
261                 "14:9_letterbox_center", "14:9_letterbox_top", "16:9_letterbox_center",
262                 "16:9_letterbox_top", ">16:9_letterbox_center", "14:9_full_format"
263         };
264         write(fd, wss[val], strlen(wss[val]));
265 //      eDebug("set wss to %s", wss[val]);
266         close(fd);
267 }
268
269 void eAVSwitch::setSlowblank(int val)
270 {
271         int fd;
272         if((fd = open("/proc/stb/avs/0/sb", O_WRONLY)) < 0) {
273                 eDebug("cannot open /proc/stb/avs/0/sb");
274                 return;
275         }
276         char *sb[] = {"0", "6", "12", "vcr", "auto"};
277         write(fd, sb[val], strlen(sb[val]));
278 //      eDebug("set slow blanking to %s", sb[val]);
279         close(fd);
280 }
281
282 //FIXME: correct "run/startlevel"
283 eAutoInitP0<eAVSwitch> init_avswitch(eAutoInitNumbers::rc, "AVSwitch Driver");