File: fm.c

package info (click to toggle)
fmtools 1.0.2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 104 kB
  • ctags: 291
  • sloc: ansic: 738; makefile: 74
file content (411 lines) | stat: -rw-r--r-- 9,234 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 <sys/ioctl.h>
#include <limits.h>
#include "videodev.h"

#include "version.h"

#define DEFAULT_DEVICE	"/dev/radio0"



static int convert_time (const char *string)
{
	if (strcmp (string, "forever") == 0 ||
            strcmp (string, "-") == 0 ||
            atoi (string) < 0)
		return 0;
	else if (strcmp (string, "none") == 0 ||
                 strcmp (string, "0") == 0)
		return -1;
	else
	{
		char worktime[80+1];
		int inttime;
		const char *suffix;

		suffix = string + strspn (string, "0123456789");

		strncpy (worktime, string, suffix - string);
		worktime[suffix - string] = '\0';
		inttime = atoi (worktime);

		switch (*suffix)
		{
		case 's':
		case '\0':
			break;
		case 'm':
			inttime *= 60;
			break;
		case 'h':
			inttime *= 60 * 60;
			break;
		case 'd':
			inttime *= 24 * 60 * 60;
			break;
		default:
			break;
		}

		return inttime;
	}
}



static char *format_time (char *buffer,
                          const char *string)
{
    if (strcmp (string, "forever") == 0 ||
        strcmp (string, "-") == 0 ||
        atoi (string) < 0)
        strcpy (buffer, "forever");
    else if (strcmp (string, "none") == 0 ||
             strcmp (string, "0") == 0)
        strcpy (buffer, "none");
    else
    {
        char worktime[80+1];
        const char *suffix;
        char *format;
        int int_time;

        suffix = string + strspn (string, "0123456789");
        strncpy (worktime, string, suffix - string);
        worktime[suffix - string] = '\0';
        int_time = atoi (worktime);

        switch (*suffix)
        {
        case 'm':
            format = "%d minute(s)";
            break;
        case 'h':
            format = "%d hour(s)";
            break;
        case 'd':
            format = "%d day(s)";
            break;
        case 's':
        case '\0':
        default:
            format = "%d second(s)";
            break;
        }

        sprintf (buffer, format, int_time);
    }

    return buffer;
}



static void maybe_sleep (const char *wait_time)
{
    char message[80+1];
    int int_wait_time;

    int_wait_time = convert_time (wait_time);

    if (int_wait_time > 0)
    {
        printf ("Sleeping for %s\n", format_time (message, wait_time));
        sleep (int_wait_time);
    }
    else if (int_wait_time == 0)
    {
        printf ("Sleeping forever...CTRL-C exits\n");
        do {
            sleep (INT_MAX);
        } while (1);
    }
}



void help(char *prog)
{
	printf("fmtools fm version %s\n\n", FMT_VERSION);
	printf("usage: %s [-h] [-o] [-q] [-d <dev>] [-t <tuner>] "
               "[-T none|forever|time] <freq>|on|off [<volume>]\n\n", 
               prog);
	printf("A small controller for Video for Linux radio devices.\n\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("  -T <time>  after setting frequency, sleep for some time\n"\
               "             (default: none; -=forever)\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, char *wait_time)
{
	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);
		if (!strncmp(buf, "TIME", 4))
			sscanf(buf, "%*s %s", wait_time);
	}

	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		wait_time_buf[256+1] = "";
	char		*wait_time = "none";
	char	*dev = NULL;

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

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

	getconfig(&defaultvol, &increment, wait_time_buf);
        if (*wait_time_buf)
            wait_time = wait_time_buf;

	while ((i = getopt(argc, argv, "+qhot:T:d:")) != EOF) {
		switch (i) {
			case 'q':
				quiet = 1;
				break;
			case 'o':
				override = 1;
				break;
			case 't':
				tuner = atoi(optarg);
				break;
			case 'T':
				wait_time = 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 = DEFAULT_DEVICE;

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

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

	/* initialize this so it doesn't pick up random junk data */
	va.balance = 32768;

	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));
		maybe_sleep (wait_time);
		exit(0);
	}

	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);
		}

		maybe_sleep (wait_time);
		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);
		}

		maybe_sleep (wait_time);
		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 = rint(atof(argv[0]) * fact);	/* rounding to nearest int */

	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.2f MHz at %2.2f%% volume\n", 
			(freq / fact), (tunevol / 655.35));

	maybe_sleep (wait_time);
	return 0;
}