~blackskad/gnomeradio/dev-vol-button

« back to all changes in this revision

Viewing changes to src/v4l2.c

  • Committer: mfcn
  • Date: 2008-04-13 14:30:58 UTC
  • Revision ID: svn-v3-trunk0:ba97a3d1-ec25-0410-b1c6-e06ad936ea6c:trunk:194
        * data/gnomeradio.schemas.in:
        * src/Makefile.am:
        * src/gui.c (initial_frequency_scan_cb), (initial_frequency_scan),
        (start_radio), (redraw_status_window), (adj_value_changed_cb):
        * src/gui.h:
        * src/prefs.c (save_settings), (load_settings):
        * src/radio.c (radio_init), (radio_is_init), (radio_stop),
        (radio_set_freq), (radio_unmute), (radio_mute), (radio_get_stereo),
        (radio_get_signal), (radio_check_station):
        * src/radio.h:
        * src/tech.c (mixer_get_volume):
        * src/tech.h:
        * src/v4l1.c (v4l1_radio_init), (v4l1_radio_is_init),
        (v4l1_radio_set_freq), (v4l1_radio_mute), (v4l1_radio_get_stereo),
        (v4l1_radio_get_signal), (v4l1_radio_finalize),
        (v4l1_radio_dev_new):
        * src/v4l1.h:
        * src/v4l2.c (v4l2_radio_init), (v4l2_radio_is_init),
        (v4l2_radio_set_freq), (v4l2_radio_mute), (v4l2_radio_get_stereo),
        (v4l2_radio_get_signal), (v4l2_radio_finalize),
        (v4l2_radio_dev_new):
        * src/v4l2.h:
        Refactored backend so that it supports both v4l and v4l2 and selection can
        be made at runtime. The v4l2 code is based on the patch contributed by
        James Henstridge (bug #429005).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  This program is free software; you can redistribute it and/or modify
 
3
 *  it under the terms of the GNU General Public License as published by
 
4
 *  the Free Software Foundation; either version 2 of the License, or
 
5
 *  (at your option) any later version.
 
6
 *
 
7
 *  This program is distributed in the hope that it will be useful,
 
8
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
 *  GNU Library General Public License for more details.
 
11
 *
 
12
 *  You should have received a copy of the GNU General Public License
 
13
 *  along with this program; if not, write to the Free Software
 
14
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
15
 */
 
16
 
 
17
#include <stdio.h>
 
18
#include <stdlib.h>
 
19
#include <unistd.h>
 
20
#include <string.h>
 
21
#include <math.h>
 
22
#include <fcntl.h>
 
23
#include <sys/ioctl.h>
 
24
#include <assert.h>
 
25
 
 
26
#include <linux/videodev2.h>
 
27
#include "v4l2.h"
 
28
 
 
29
typedef struct _V4L2RadioDev V4L2RadioDev;
 
30
struct _V4L2RadioDev
 
31
{
 
32
        struct _RadioDev parent;
 
33
 
 
34
        int fd;
 
35
        int freq_fact;
 
36
        int radio_rangelow;
 
37
        int radio_rangehigh;
 
38
};
 
39
 
 
40
static int
 
41
v4l2_radio_init(RadioDev *radio_dev, char *device)
 
42
{
 
43
        V4L2RadioDev *dev = (V4L2RadioDev*)radio_dev;
 
44
        struct v4l2_capability caps;
 
45
        struct v4l2_tuner tuner;
 
46
        
 
47
        if ((dev->fd = open(device, O_RDONLY))< 0)
 
48
                goto err;
 
49
        
 
50
        /* does this device provide a tuner? */
 
51
        memset(&caps, 0, sizeof(caps));
 
52
        if (ioctl(dev->fd, VIDIOC_QUERYCAP, &caps) < 0) {
 
53
                perror("VIDIOC_QUERYCAP");
 
54
                goto err;
 
55
        }
 
56
        if ((caps.capabilities & V4L2_CAP_TUNER) == 0) {
 
57
                fprintf(stderr, "Not a radio tuner\n");
 
58
                goto err;
 
59
        }
 
60
 
 
61
        /* check the tuner */
 
62
        memset(&tuner, 0, sizeof(tuner));
 
63
        tuner.index = 0;
 
64
        if (ioctl(dev->fd, VIDIOC_G_TUNER, &tuner) < 0) {
 
65
                perror("VIDIOC_G_TUNER");
 
66
                goto err;
 
67
        }
 
68
        if (tuner.type != V4L2_TUNER_RADIO) {
 
69
                fprintf(stderr, "Not a radio tuner\n");
 
70
                goto err;
 
71
        }
 
72
        /* Does this tuner expect data in 62.5Hz or 62.5kHz multiples? */
 
73
        dev->radio_rangelow = tuner.rangelow;
 
74
        dev->radio_rangehigh = tuner.rangehigh;
 
75
        if ((tuner.capability & V4L2_TUNER_CAP_LOW) != 0)
 
76
                dev->freq_fact = 16000;
 
77
        else
 
78
                dev->freq_fact = 16;
 
79
        
 
80
        return 1;
 
81
 
 
82
 err:
 
83
        if (dev->fd >= 0)
 
84
                close(dev->fd);
 
85
        dev->fd = -1;
 
86
        return 0;
 
87
}
 
88
 
 
89
static int
 
90
v4l2_radio_is_init(RadioDev *radio_dev)
 
91
{
 
92
        V4L2RadioDev *dev = (V4L2RadioDev*)radio_dev;
 
93
        return (dev->fd >= 0);
 
94
}
 
95
 
 
96
static void
 
97
v4l2_radio_set_freq(RadioDev *radio_dev, float frequency)
 
98
{
 
99
        V4L2RadioDev *dev = (V4L2RadioDev*)radio_dev;
 
100
        struct v4l2_frequency freq;
 
101
 
 
102
        if (dev->fd<0)
 
103
                return;
 
104
 
 
105
        memset(&freq, 0, sizeof(freq));
 
106
        freq.tuner = 0;
 
107
        freq.type = V4L2_TUNER_RADIO;
 
108
        freq.frequency = frequency * dev->freq_fact;
 
109
 
 
110
        if (freq.frequency < dev->radio_rangelow ||
 
111
            freq.frequency > dev->radio_rangehigh)
 
112
                return;
 
113
        if (ioctl(dev->fd, VIDIOC_S_FREQUENCY, &freq) < 0)
 
114
                perror("VIDIOC_S_FREQUENCY");
 
115
}
 
116
 
 
117
static void
 
118
v4l2_radio_mute(RadioDev *radio_dev, int mute)
 
119
{
 
120
        V4L2RadioDev *dev = (V4L2RadioDev*)radio_dev;
 
121
        struct v4l2_control control;
 
122
 
 
123
        if (dev->fd<0)
 
124
                return;
 
125
 
 
126
        memset(&control, 0, sizeof(control));
 
127
        control.id = V4L2_CID_AUDIO_MUTE;
 
128
        control.value = mute;
 
129
        if (ioctl(dev->fd, VIDIOC_S_CTRL, &control) < 0)
 
130
                perror("VIDIOC_S_CTRL");
 
131
}
 
132
 
 
133
static int
 
134
v4l2_radio_get_stereo(RadioDev *radio_dev)
 
135
{
 
136
        V4L2RadioDev *dev = (V4L2RadioDev*)radio_dev;
 
137
        struct v4l2_tuner tuner;
 
138
 
 
139
        if (dev->fd<0)
 
140
                return -1;
 
141
 
 
142
        memset(&tuner, 0, sizeof(tuner));
 
143
        tuner.index = 0;
 
144
        if (ioctl(dev->fd, VIDIOC_G_TUNER, &tuner) < 0) {
 
145
                perror("VIDIOC_G_TUNER");
 
146
                return -1;
 
147
        }
 
148
 
 
149
        return tuner.audmode == V4L2_TUNER_MODE_STEREO;
 
150
}
 
151
 
 
152
static int
 
153
v4l2_radio_get_signal(RadioDev *radio_dev)
 
154
{
 
155
        V4L2RadioDev *dev = (V4L2RadioDev*)radio_dev;
 
156
        struct v4l2_tuner tuner;
 
157
 
 
158
        if (dev->fd<0)
 
159
                return -1;
 
160
 
 
161
        memset(&tuner, 0, sizeof(tuner));
 
162
        tuner.index = 0;
 
163
        if (ioctl(dev->fd, VIDIOC_G_TUNER, &tuner) < 0) {
 
164
                perror("VIDIOC_G_TUNER");
 
165
                return -1;
 
166
        }
 
167
 
 
168
        return tuner.signal >> 13;
 
169
}
 
170
 
 
171
static void
 
172
v4l2_radio_finalize(RadioDev *radio_dev)
 
173
{
 
174
        V4L2RadioDev *dev = (V4L2RadioDev*)radio_dev;
 
175
        
 
176
        if (dev->fd >= 0)
 
177
                close(dev->fd);
 
178
        free (dev);
 
179
}
 
180
 
 
181
RadioDev*
 
182
v4l2_radio_dev_new (void)
 
183
{
 
184
    RadioDev *dev;
 
185
        V4L2RadioDev *v4l2_dev;
 
186
 
 
187
        v4l2_dev = malloc (sizeof (V4L2RadioDev));
 
188
        v4l2_dev->fd = -1;
 
189
 
 
190
        dev = (RadioDev*)v4l2_dev;
 
191
        dev->init       = v4l2_radio_init;
 
192
        dev->is_init    = v4l2_radio_is_init;
 
193
        dev->set_freq   = v4l2_radio_set_freq;
 
194
        dev->mute       = v4l2_radio_mute;
 
195
        dev->get_stereo = v4l2_radio_get_stereo;
 
196
        dev->get_signal = v4l2_radio_get_signal;
 
197
        dev->finalize   = v4l2_radio_finalize;
 
198
 
 
199
        return dev;
 
200
}
 
201