make menu text translatable
[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                 FILE *f = fopen("/proc/stb/fp/vcr_fns", "r");
50                 if (f)
51                 {
52                         if (fscanf(f, "%d", &val) != 1)
53                                 eDebug("read /proc/stb/fp/vcr_fns failed!! (%m)");
54                         fclose(f);
55                 }
56                 else if (ioctl(m_fp_fd, FP_IOCTL_GET_VCR, &val) < 0)
57                         eDebug("FP_GET_VCR failed (%m)");
58         }
59         return val;
60 }
61
62 void eAVSwitch::fp_event(int what)
63 {
64         if (what & POLLERR) // driver not ready for fp polling
65         {
66                 eDebug("fp driver not read for polling.. so disable polling");
67                 m_fp_notifier->stop();
68         }
69         else
70         {
71                 FILE *f = fopen("/proc/stb/fp/events", "r");
72                 if (f)
73                 {
74                         int events;
75                         if (fscanf(f, "%d", &events) != 1)
76                                 eDebug("read /proc/stb/fp/events failed!! (%m)");
77                         else if (events & FP_EVENT_VCR_SB_CHANGED)
78                                 /* emit */ vcr_sb_notifier(getVCRSlowBlanking());
79                         fclose(f);
80                 }
81                 else
82                 {
83                         int val = FP_EVENT_VCR_SB_CHANGED;  // ask only for this event
84                         if (ioctl(m_fp_fd, FP_IOCTL_GET_EVENT, &val) < 0)
85                                 eDebug("FP_IOCTL_GET_EVENT failed (%m)");
86                         else if (val & FP_EVENT_VCR_SB_CHANGED)
87                                 /* emit */ vcr_sb_notifier(getVCRSlowBlanking());
88                 }
89         }
90 }
91
92 eAVSwitch::~eAVSwitch()
93 {
94         if ( m_fp_fd >= 0 )
95                 close(m_fp_fd);
96         if (m_fp_notifier)
97                 delete m_fp_notifier;
98 }
99
100 eAVSwitch *eAVSwitch::getInstance()
101 {
102         return instance;
103 }
104
105 bool eAVSwitch::haveScartSwitch()
106 {
107         char tmp[255];
108         int fd = open("/proc/stb/avs/0/input_choices", O_RDONLY);
109         if(fd < 0) {
110                 eDebug("cannot open /proc/stb/avs/0/input_choices");
111                 return false;
112         }
113         read(fd, tmp, 255);
114         close(fd);
115         return !!strstr(tmp, "scart");
116 }
117
118 void eAVSwitch::setInput(int val)
119 {
120         /*
121         0-encoder
122         1-scart
123         2-aux
124         */
125
126         const char *input[] = {"encoder", "scart", "aux"};
127
128         int fd;
129         
130         if((fd = open("/proc/stb/avs/0/input", O_WRONLY)) < 0) {
131                 eDebug("cannot open /proc/stb/avs/0/input");
132                 return;
133         }
134
135         write(fd, input[val], strlen(input[val]));
136         close(fd);
137         
138         if (val == 1)
139                 setFastBlank(2);
140 }
141
142 void eAVSwitch::setFastBlank(int val)
143 {
144         int fd;
145         const char *fb[] = {"low", "high", "vcr"};
146
147         if((fd = open("/proc/stb/avs/0/fb", O_WRONLY)) < 0) {
148                 eDebug("cannot open /proc/stb/avs/0/fb");
149                 return;
150         }
151
152         write(fd, fb[val], strlen(fb[0]));
153         close(fd);
154 }
155
156 void eAVSwitch::setColorFormat(int format)
157 {
158         /*
159         0-CVBS
160         1-RGB
161         2-S-Video
162         */
163         const char *cvbs="cvbs";
164         const char *rgb="rgb";
165         const char *svideo="svideo";
166         const char *yuv="yuv";
167         int fd;
168         
169         if((fd = open("/proc/stb/avs/0/colorformat", O_WRONLY)) < 0) {
170                 printf("cannot open /proc/stb/avs/0/colorformat\n");
171                 return;
172         }
173         switch(format) {
174                 case 0:
175                         write(fd, cvbs, strlen(cvbs));
176                         break;
177                 case 1:
178                         write(fd, rgb, strlen(rgb));
179                         break;
180                 case 2:
181                         write(fd, svideo, strlen(svideo));
182                         break;
183                 case 3:
184                         write(fd, yuv, strlen(yuv));
185                         break;
186         }       
187         close(fd);
188 }
189
190 void eAVSwitch::setAspectRatio(int ratio)
191 {
192         /*
193         0-4:3 Letterbox
194         1-4:3 PanScan
195         2-16:9
196         3-16:9 forced ("panscan")
197         4-16:10 Letterbox
198         5-16:10 PanScan
199         6-16:9 forced ("letterbox")
200         */
201         const char *aspect[] = {"4:3", "4:3", "any", "16:9", "16:10", "16:10", "16:9", "16:9"};
202         const char *policy[] = {"letterbox", "panscan", "bestfit", "panscan", "letterbox", "panscan", "letterbox"};
203
204         int fd;
205         if((fd = open("/proc/stb/video/aspect", O_WRONLY)) < 0) {
206                 eDebug("cannot open /proc/stb/video/aspect");
207                 return;
208         }
209 //      eDebug("set aspect to %s", aspect[ratio]);
210         write(fd, aspect[ratio], strlen(aspect[ratio]));
211         close(fd);
212
213         if((fd = open("/proc/stb/video/policy", O_WRONLY)) < 0) {
214                 eDebug("cannot open /proc/stb/video/policy");
215                 return;
216         }
217 //      eDebug("set ratio to %s", policy[ratio]);
218         write(fd, policy[ratio], strlen(policy[ratio]));
219         close(fd);
220
221 }
222
223 void eAVSwitch::setVideomode(int mode)
224 {
225         const char *pal="pal";
226         const char *ntsc="ntsc";
227         
228         if (mode == m_video_mode)
229                 return;
230
231         if (mode == 2)
232         {
233                 int fd1 = open("/proc/stb/video/videomode_50hz", O_WRONLY);
234                 if(fd1 < 0) {
235                         eDebug("cannot open /proc/stb/video/videomode_50hz");
236                         return;
237                 }
238                 int fd2 = open("/proc/stb/video/videomode_60hz", O_WRONLY);
239                 if(fd2 < 0) {
240                         eDebug("cannot open /proc/stb/video/videomode_60hz");
241                         close(fd1);
242                         return;
243                 }
244                 write(fd1, pal, strlen(pal));
245                 write(fd2, ntsc, strlen(ntsc));
246                 close(fd1);
247                 close(fd2);
248         }
249         else
250         {
251                 int fd = open("/proc/stb/video/videomode", O_WRONLY);
252                 if(fd < 0) {
253                         eDebug("cannot open /proc/stb/video/videomode");
254                         return;
255                 }
256                 switch(mode) {
257                         case 0:
258                                 write(fd, pal, strlen(pal));
259                                 break;
260                         case 1:
261                                 write(fd, ntsc, strlen(ntsc));
262                                 break;
263                         default:
264                                 eDebug("unknown videomode %d", mode);
265                 }
266                 close(fd);
267         }
268
269         m_video_mode = mode;
270 }
271
272 void eAVSwitch::setWSS(int val) // 0 = auto, 1 = auto(4:3_off)
273 {
274         int fd;
275         if((fd = open("/proc/stb/denc/0/wss", O_WRONLY)) < 0) {
276                 eDebug("cannot open /proc/stb/denc/0/wss");
277                 return;
278         }
279         const char *wss[] = {
280                 "off", "auto", "auto(4:3_off)", "4:3_full_format", "16:9_full_format",
281                 "14:9_letterbox_center", "14:9_letterbox_top", "16:9_letterbox_center",
282                 "16:9_letterbox_top", ">16:9_letterbox_center", "14:9_full_format"
283         };
284         write(fd, wss[val], strlen(wss[val]));
285 //      eDebug("set wss to %s", wss[val]);
286         close(fd);
287 }
288
289 void eAVSwitch::setSlowblank(int val)
290 {
291         int fd;
292         if((fd = open("/proc/stb/avs/0/sb", O_WRONLY)) < 0) {
293                 eDebug("cannot open /proc/stb/avs/0/sb");
294                 return;
295         }
296         const char *sb[] = {"0", "6", "12", "vcr", "auto"};
297         write(fd, sb[val], strlen(sb[val]));
298 //      eDebug("set slow blanking to %s", sb[val]);
299         close(fd);
300 }
301
302 //FIXME: correct "run/startlevel"
303 eAutoInitP0<eAVSwitch> init_avswitch(eAutoInitNumbers::rc, "AVSwitch Driver");