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
|
/* fm.c - simple V4L2 compatible tuner for radio cards
Copyright (C) 2004, 2006, 2009, 2012 Ben Pfaff <blp@cs.stanford.edu>
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, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <limits.h>
#include "fmlib.h"
#define DEFAULT_DEVICE "/dev/radio0"
static double
clamp(double percent)
{
return percent < 0.0 ? 0.0 : percent > 100.0 ? 100.0 : percent;
}
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 struct tuner *tuner, 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));
tuner_sleep(tuner, int_wait_time);
} else if (int_wait_time == 0)
{
printf("Sleeping forever...CTRL-C exits\n");
for (;;)
pause();
}
}
static void
usage(void)
{
printf("fmtools fm version %s\n\n", VERSION);
printf("usage: %s [-h] [-o] [-q] [-d <dev>] [-t <tuner>] "
"[-T none|forever|time] <freq>|on|off [<volume>]\n\n",
program_name);
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> percentage (0-100)\n");
exit(EXIT_SUCCESS);
}
static void
getconfig(const char *fn,
double *defaultvol, double *increment, char *wait_time)
{
FILE *conf;
char buf[256];
if (!fn) {
snprintf(buf, sizeof buf, "%s/.fmrc", getenv("HOME"));
fn = buf;
}
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 %lf", defaultvol);
if (!strncmp(buf, "INCR", 3))
sscanf(buf, "%*s %lf", increment);
if (!strncmp(buf, "TIME", 4))
sscanf(buf, "%*s %s", wait_time);
}
fclose(conf);
}
static void
print_volume(const struct tuner *tuner)
{
if (tuner_has_volume_control(tuner))
printf(" at %.2f%% volume", tuner_get_volume(tuner));
else
printf(" (radio does not support volume control)");
}
static void
print_mute(const struct tuner *tuner)
{
if (tuner_is_muted(tuner))
printf(" (radio is muted, use \"fm on\" to unmute)");
}
int main(int argc, char **argv)
{
struct tuner tuner;
const char *device = NULL;
int index = 0;
bool quiet = false;
bool override = false;
const char *config_file = NULL;
double defaultvol = 12.5;
double increment = 10;
char wait_time_buf[256+1] = "";
const char *wait_time = NULL;
program_name = argv[0];
/* need at least a frequency */
if (argc < 2)
fatal(0, "usage: %s [-h] [-o] [-q] [-d <dev>] [-t <tuner>] "
"[-T time|forever|none] "
"<freq>|on|off [<volume>]\n", program_name);
for (;;) {
int option = getopt(argc, argv, "+qhot:T:c:d:");
if (option == -1)
break;
switch (option) {
case 'q':
quiet = 1;
break;
case 'o':
override = 1;
break;
case 't':
index = atoi(optarg);
break;
case 'T':
wait_time = optarg;
break;
case 'd':
device = optarg;
break;
case 'c':
config_file = optarg;
break;
case 'h':
default:
usage();
break;
}
}
getconfig(config_file, &defaultvol, &increment, wait_time_buf);
if (!wait_time)
wait_time = *wait_time_buf ? wait_time_buf : "none";
argc -= optind;
argv += optind;
if (argc == 0) /* no frequency, on|off, or +|- given */
usage();
tuner_open(&tuner, device, index);
if (!strcmp(argv[0], "off")) {
tuner_set_mute(&tuner, true);
if (!quiet)
printf("Radio muted\n");
} else if (!strcmp(argv[0], "on")) {
tuner_set_mute(&tuner, false);
if (!quiet) {
printf("Radio on");
print_volume(&tuner);
putchar('\n');
}
} else if (!strcmp(argv[0], "+") || !strcmp(argv[0], "-")) {
double new_volume;
if (!tuner_has_volume_control(&tuner))
fatal(0, "Radio does not support volume control");
new_volume = tuner_get_volume(&tuner);
if (argv[0][0] == '+')
new_volume += increment;
else
new_volume -= increment;
new_volume = clamp(new_volume);
tuner_set_volume(&tuner, new_volume);
if (!quiet) {
printf("Setting volume to %.2f%%", new_volume);
print_mute(&tuner);
putchar('\n');
}
} else if (atof(argv[0])) {
double frequency = atof(argv[0]);
double volume = argc > 1 ? clamp(atof(argv[1])) : defaultvol;
tuner_set_freq(&tuner, frequency * 16000.0, override);
if (tuner_has_volume_control(&tuner))
tuner_set_volume(&tuner, volume);
if (!quiet) {
printf("Radio tuned to %2.2f MHz", frequency);
print_volume(&tuner);
print_mute(&tuner);
putchar('\n');
}
} else {
fatal(0, "unrecognized command syntax; use --help for help");
}
maybe_sleep(&tuner, wait_time);
tuner_close(&tuner);
return 0;
}
|