Merge remote branch 'remotes/origin/acid-burn/bug_629_show_autofs_volumes_as_backuplo...
[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 = eSocketNotifier::create(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 }
97
98 eAVSwitch *eAVSwitch::getInstance()
99 {
100         return instance;
101 }
102
103 bool eAVSwitch::haveScartSwitch()
104 {
105         char tmp[255];
106         int fd = open("/proc/stb/avs/0/input_choices", O_RDONLY);
107         if(fd < 0) {
108                 eDebug("cannot open /proc/stb/avs/0/input_choices");
109                 return false;
110         }
111         read(fd, tmp, 255);
112         close(fd);
113         return !!strstr(tmp, "scart");
114 }
115
116 void eAVSwitch::setInput(int val)
117 {
118         /*
119         0-encoder
120         1-scart
121         2-aux
122         */
123
124         const char *input[] = {"encoder", "scart", "aux"};
125
126         int fd;
127         
128         if((fd = open("/proc/stb/avs/0/input", O_WRONLY)) < 0) {
129                 eDebug("cannot open /proc/stb/avs/0/input");
130                 return;
131         }
132
133         write(fd, input[val], strlen(input[val]));
134         close(fd);
135 }
136
137 void eAVSwitch::setColorFormat(int format)
138 {
139         /*
140         0-CVBS
141         1-RGB
142         2-S-Video
143         */
144         const char *cvbs="cvbs";
145         const char *rgb="rgb";
146         const char *svideo="svideo";
147         const char *yuv="yuv";
148         int fd;
149         
150         if((fd = open("/proc/stb/avs/0/colorformat", O_WRONLY)) < 0) {
151                 printf("cannot open /proc/stb/avs/0/colorformat\n");
152                 return;
153         }
154         switch(format) {
155                 case 0:
156                         write(fd, cvbs, strlen(cvbs));
157                         break;
158                 case 1:
159                         write(fd, rgb, strlen(rgb));
160                         break;
161                 case 2:
162                         write(fd, svideo, strlen(svideo));
163                         break;
164                 case 3:
165                         write(fd, yuv, strlen(yuv));
166                         break;
167         }       
168         close(fd);
169 }
170
171 void eAVSwitch::setAspectRatio(int ratio)
172 {
173         /*
174         0-4:3 Letterbox
175         1-4:3 PanScan
176         2-16:9
177         3-16:9 forced ("panscan")
178         4-16:10 Letterbox
179         5-16:10 PanScan
180         6-16:9 forced ("letterbox")
181         */
182         const char *aspect[] = {"4:3", "4:3", "any", "16:9", "16:10", "16:10", "16:9", "16:9"};
183         const char *policy[] = {"letterbox", "panscan", "bestfit", "panscan", "letterbox", "panscan", "letterbox"};
184
185         int fd;
186         if((fd = open("/proc/stb/video/aspect", O_WRONLY)) < 0) {
187                 eDebug("cannot open /proc/stb/video/aspect");
188                 return;
189         }
190 //      eDebug("set aspect to %s", aspect[ratio]);
191         write(fd, aspect[ratio], strlen(aspect[ratio]));
192         close(fd);
193
194         if((fd = open("/proc/stb/video/policy", O_WRONLY)) < 0) {
195                 eDebug("cannot open /proc/stb/video/policy");
196                 return;
197         }
198 //      eDebug("set ratio to %s", policy[ratio]);
199         write(fd, policy[ratio], strlen(policy[ratio]));
200         close(fd);
201
202 }
203
204 void eAVSwitch::setVideomode(int mode)
205 {
206         const char *pal="pal";
207         const char *ntsc="ntsc";
208         
209         if (mode == m_video_mode)
210                 return;
211
212         if (mode == 2)
213         {
214                 int fd1 = open("/proc/stb/video/videomode_50hz", O_WRONLY);
215                 if(fd1 < 0) {
216                         eDebug("cannot open /proc/stb/video/videomode_50hz");
217                         return;
218                 }
219                 int fd2 = open("/proc/stb/video/videomode_60hz", O_WRONLY);
220                 if(fd2 < 0) {
221                         eDebug("cannot open /proc/stb/video/videomode_60hz");
222                         close(fd1);
223                         return;
224                 }
225                 write(fd1, pal, strlen(pal));
226                 write(fd2, ntsc, strlen(ntsc));
227                 close(fd1);
228                 close(fd2);
229         }
230         else
231         {
232                 int fd = open("/proc/stb/video/videomode", O_WRONLY);
233                 if(fd < 0) {
234                         eDebug("cannot open /proc/stb/video/videomode");
235                         return;
236                 }
237                 switch(mode) {
238                         case 0:
239                                 write(fd, pal, strlen(pal));
240                                 break;
241                         case 1:
242                                 write(fd, ntsc, strlen(ntsc));
243                                 break;
244                         default:
245                                 eDebug("unknown videomode %d", mode);
246                 }
247                 close(fd);
248         }
249
250         m_video_mode = mode;
251 }
252
253 void eAVSwitch::setWSS(int val) // 0 = auto, 1 = auto(4:3_off)
254 {
255         int fd;
256         if((fd = open("/proc/stb/denc/0/wss", O_WRONLY)) < 0) {
257                 eDebug("cannot open /proc/stb/denc/0/wss");
258                 return;
259         }
260         const char *wss[] = {
261                 "off", "auto", "auto(4:3_off)", "4:3_full_format", "16:9_full_format",
262                 "14:9_letterbox_center", "14:9_letterbox_top", "16:9_letterbox_center",
263                 "16:9_letterbox_top", ">16:9_letterbox_center", "14:9_full_format"
264         };
265         write(fd, wss[val], strlen(wss[val]));
266 //      eDebug("set wss to %s", wss[val]);
267         close(fd);
268 }
269
270 //FIXME: correct "run/startlevel"
271 eAutoInitP0<eAVSwitch> init_avswitch(eAutoInitNumbers::rc, "AVSwitch Driver");