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