File: fm.c

package info (click to toggle)
fmtools 0.99.0-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 84 kB
  • ctags: 12
  • sloc: ansic: 314; makefile: 52; sh: 22
file content (265 lines) | stat: -rw-r--r-- 6,069 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* fm.c - simple v4l compatible tuner for radio cards

   Copyright (C) 1998  Russell Kroll <rkroll@exploits.org>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <math.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <asm/types.h>
#include <sys/ioctl.h>
#include <linux/videodev.h>

void help(char *prog)
{
	printf("usage: %s [-h] [-o] [-q] [-d <dev>] [-t <tuner>] <freq>|on|off [<volume>]\n", prog);
	printf("\n");
	printf("  -h         display this help\n");
	printf("  -o         override frequency range limits of card\n");
	printf("  -q         quiet mode\n");
	printf("  -d <dev>   select device (default: /dev/radio0)\n");
	printf("  -t <tuner> select tuner (default: 0)\n");
	printf("  <freq>     frequency in MHz (i.e. 94.3)\n");
	printf("  on         turn radio on\n");
	printf("  off        turn radio off (mute)\n");
	printf("  +          increase volume\n");
	printf("  -          decrease volume\n");
	printf("  <volume>   intensity (0-65535)\n");
	exit(0);
}

void getconfig(int *defaultvol, int *increment)
{
	FILE	*conf;
	char	buf[256], fn[256];

	sprintf(fn, "%s/.fmrc", getenv("HOME"));
	conf = fopen(fn, "r");

	if (!conf)
		return;

	while(fgets(buf, sizeof(buf), conf)) {
		buf[strlen(buf)-1] = 0;
		if (!strncmp(buf, "VOL", 3))
			sscanf(buf, "%*s %i", defaultvol);
		if (!strncmp(buf, "INCR", 3))
			sscanf(buf, "%*s %i", increment);
	}

	fclose(conf);
}	

int main(int argc, char **argv)
{
	int		fd, ret, tunevol, quiet=0, i, override = 0;
	int		defaultvol = 8192;	/* default volume = 12.5% */
	int		increment = 6554;	/* default change = 10% */
	double		fact;
	unsigned long	freq;
	struct 		video_audio va;
	struct		video_tuner vt;
	char		*progname;
	int		tuner = 0;
	char	*dev = NULL;

	/* need at least a frequency */
	if (argc < 2) {
		printf ("usage: %s [-h] [-o] [-q] [-d <dev>] [-t <tuner>] <freq>|on|off [<volume>]\n", argv[0]);
		exit (1);
	}

	progname = argv[0];	/* used after getopt munges argv[] */

	getconfig(&defaultvol, &increment);

	while ((i = getopt(argc, argv, "+qhot:d:")) != EOF) {
		switch (i) {
			case 'q':
				quiet = 1;
				break;
			case 'o':
				override = 1;
				break;
			case 't':
				tuner = atoi(optarg);
				break;
			case 'd':
				dev = strdup(optarg);
				break;
			case 'h':
			default:
				help(argv[0]);
				break;
		}
	}

	argc -= optind;
	argv += optind;

	if (argc == 0)		/* no frequency, on|off, or +|- given */
		help(progname);

	if (argc == 2)
		tunevol = atoi(argv[1]);
	else
		tunevol = defaultvol;

	if (!dev)
		dev = strdup("/dev/radio0");	/* use default */

	fd = open(dev, O_RDONLY); 
	if (fd < 0) {
		fprintf(stderr, "Unable to open %s: %s\n", dev, strerror(errno));
		exit(1);
	}

	if (!strcmp("off", argv[0])) {		/* mute the radio */
		va.audio = 0;
		va.volume = 0;
		va.flags = VIDEO_AUDIO_MUTE;	
		ret = ioctl(fd, VIDIOCSAUDIO, &va);
		if (ret < 0) {
			perror("ioctl VIDIOCSAUDIO");
			exit(1);
		}
		
		if (!quiet)
			printf ("Radio muted\n");
		exit(0);
	}

	if (!strcmp("on", argv[0])) {		/* turn radio on */
		va.audio = 0;
		va.volume = tunevol;
		va.flags = 0;
		ret = ioctl(fd, VIDIOCSAUDIO, &va);
		if (ret < 0) {
			perror("ioctl VIDIOCSAUDIO");
			exit(1);
		}

		if (!quiet)
			printf("Radio on at %2.2f%% volume\n", 
			      (tunevol / 655.36));
		exit(0);
	}

	ret = ioctl(fd, VIDIOCGAUDIO, &va);
	if (ret < 0) {
		perror("ioctl VIDIOCGAUDIO");
		exit(1);
	}

	if (argv[0][0] == '+') {		/* volume up */
		if ((va.volume + increment) > 65535)
			va.volume = 65535;	/* catch overflows in __u16 */
		else
			va.volume += increment;

		if (!quiet)
			printf("Setting volume to %2.2f%%\n", 
			      (va.volume / 655.35));

		ret = ioctl (fd, VIDIOCSAUDIO, &va);
		if (ret < 0) {
			perror("ioctl VIDIOCSAUDIO");
			exit(1);
		}

		exit (0);
	}

	if (argv[0][0] == '-') {		/* volume down */
		if ((va.volume - increment) < 0) 
			va.volume = 0;		/* catch negative numbers */
		else
			va.volume -= increment;

		if (!quiet)
			printf("Setting volume to %2.2f%%\n", 
				(va.volume / 655.35));

		ret = ioctl (fd, VIDIOCSAUDIO, &va);
		if (ret < 0) {
			perror("ioctl VIDIOCSAUDIO");
			exit(1);
		}

		exit (0);
	}		

	/* at this point, we are trying to tune to a frequency */

	vt.tuner = tuner;
	ret = ioctl(fd, VIDIOCSTUNER, &vt);	/* set tuner # */
	if (ret < 0) {
		perror("ioctl VIDIOCSTUNER");
		exit(1);
	}

	ret = ioctl(fd, VIDIOCGTUNER, &vt);	/* get frequency range */
	if (ret < 0) {
		perror("ioctl VIDIOCGTUNER");
		exit(1);
	}

	if ((ret == -1) || ((vt.flags & VIDEO_TUNER_LOW) == 0))
		fact = 16.;
	else
		fact = 16000.;

	freq = ceil(atof(argv[0]) * fact);	/* rounding up matters */

	if ((freq < vt.rangelow) || (freq > vt.rangehigh)) {
		if (override == 0) {
			printf("Frequency %2.1f out of range (%2.1f - %2.1f MHz)\n",
			      (freq / fact), (vt.rangelow / fact), 
			      (vt.rangehigh / fact));
			exit(1);
		}
	}

	/* frequency sanity checked, proceed */
	ret = ioctl(fd, VIDIOCSFREQ, &freq);
	if (ret < 0) {
		perror("ioctl VIDIOCSFREQ");
		exit (1);
	}

	va.audio = 0;
	va.volume = tunevol;
	va.flags = 0;	
	va.mode = VIDEO_SOUND_STEREO;

	ret = ioctl(fd, VIDIOCSAUDIO, &va);	/* set the volume */
	if (ret < 0) {
		perror("ioctl VIDIOCSAUDIO");
		exit(1);
	}

	if (!quiet) 
		printf ("Radio tuned to %2.1f MHz at %2.2f%% volume\n", 
			(freq / fact), (tunevol / 655.35));

	return 0;
}