remove generated file "config.h":
[enigma2.git] / lib / dvb / volume.cpp
1 #include <lib/dvb/volume.h>
2 #include <stdio.h>
3 #include <fcntl.h>
4 #include <sys/ioctl.h>
5 #include <unistd.h>
6
7 #if HAVE_DVB_API_VERSION < 3
8 #define VIDEO_DEV "/dev/dvb/card0/video0"
9 #define AUDIO_DEV "/dev/dvb/card0/audio0"
10 #include <ost/audio.h>
11 #include <ost/video.h>
12 #else
13 #define VIDEO_DEV "/dev/dvb/adapter0/video0"
14 #define AUDIO_DEV "/dev/dvb/adapter0/audio0"
15 #include <linux/dvb/audio.h>
16 #include <linux/dvb/video.h>
17 #endif
18
19 eDVBVolumecontrol* eDVBVolumecontrol::instance = NULL;
20
21 eDVBVolumecontrol* eDVBVolumecontrol::getInstance()
22 {
23         if (instance == NULL)
24                 instance = new eDVBVolumecontrol;
25         return instance;
26 }
27
28 eDVBVolumecontrol::eDVBVolumecontrol()
29 {
30         volumeUnMute();
31         setVolume(100, 100);
32 }
33
34 int eDVBVolumecontrol::openMixer()
35 {
36         return open( AUDIO_DEV, O_RDWR );
37 }
38
39 void eDVBVolumecontrol::closeMixer(int fd)
40 {
41         close(fd);
42 }
43
44 void eDVBVolumecontrol::volumeUp(int left, int right)
45 {
46         printf("[volume.cpp] Volume up\n");
47         setVolume(leftVol + left, rightVol + right);
48 }
49
50 void eDVBVolumecontrol::volumeDown(int left, int right)
51 {
52         printf("[volume.cpp] Volume down\n");
53         setVolume(leftVol - left, rightVol - right);
54 }
55
56 int eDVBVolumecontrol::checkVolume(int vol)
57 {
58         if (vol < 0)
59                 vol = 0;
60         else if (vol > 100)
61                 vol = 100;
62         return vol;
63 }
64
65 void eDVBVolumecontrol::setVolume(int left, int right)
66 {
67         leftVol = checkVolume(left);
68         rightVol = checkVolume(right);
69         
70         left = 63 - leftVol * 63 / 100;
71         right = 63 - rightVol * 63 / 100;
72         
73 #if HAVE_DVB_API_VERSION < 3   
74                 audioMixer_t mixer;
75 #else
76                 audio_mixer_t mixer;
77 #endif
78
79 #ifdef HAVE_DVB_API_VERSION
80                 mixer.volume_left = (left * left) / 64;
81                 mixer.volume_right = (right * right) / 64;
82 #endif
83
84                 int fd = openMixer();
85 #ifdef HAVE_DVB_API_VERSION
86                 ioctl(fd, AUDIO_SET_MIXER, &mixer);
87 #endif
88                 closeMixer(fd);
89                 
90                 printf("Setvolume: %d %d\n", leftVol, rightVol);
91                 printf("Setvolume: %d %d\n", left, right);              
92                 
93         //HACK?
94         FILE *f;
95         if((f = fopen("/proc/stb/avs/0/volume", "wb")) == NULL) {
96                 printf("cannot open /proc/stb/avs/0/volume\n");
97                 return;
98         }
99         
100         fprintf(f, "%d", left);
101
102         fclose(f);
103 }
104
105 int eDVBVolumecontrol::getVolume()
106 {
107         if (isMuted())
108                 return 0;
109         return leftVol;
110 }
111
112 bool eDVBVolumecontrol::isMuted()
113 {
114         return muted;
115 }
116
117
118 void eDVBVolumecontrol::volumeMute()
119 {
120         int fd = openMixer();
121 #ifdef HAVE_DVB_API_VERSION     
122         ioctl(fd, AUDIO_SET_MUTE, true);
123 #endif
124         closeMixer(fd);
125         muted = true;
126
127         //HACK?
128         FILE *f;
129         if((f = fopen("/proc/stb/audio/j1_mute", "wb")) == NULL) {
130                 printf("cannot open /proc/stb/audio/j1_mute\n");
131                 return;
132         }
133         
134         fprintf(f, "%d", 1);
135
136         fclose(f);
137 }
138
139 void eDVBVolumecontrol::volumeUnMute()
140 {
141         int fd = openMixer();
142 #ifdef HAVE_DVB_API_VERSION
143         ioctl(fd, AUDIO_SET_MUTE, false);
144 #endif
145         closeMixer(fd);
146         muted = false;
147
148         //HACK?
149         FILE *f;
150         if((f = fopen("/proc/stb/audio/j1_mute", "wb")) == NULL) {
151                 printf("cannot open /proc/stb/audio/j1_mute\n");
152                 return;
153         }
154         
155         fprintf(f, "%d", 0);
156
157         fclose(f);
158 }
159
160 void eDVBVolumecontrol::volumeToggleMute()
161 {
162         if (isMuted())
163                 volumeUnMute();
164         else
165                 volumeMute();
166 }