1 /* fm.c - simple V4L2 compatible tuner for radio cards
3 Copyright (C) 2004, 2006, 2009, 2012 Ben Pfaff <blp@cs.stanford.edu>
4 Copyright (C) 1998 Russell Kroll <rkroll@exploits.org>
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2 of the License, or (at your option)
11 This program is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 You should have received a copy of the GNU General Public License along with
17 this program. If not, see <http://www.gnu.org/licenses/>.
34 #define DEFAULT_DEVICE "/dev/radio0"
39 return percent
< 0.0 ? 0.0 : percent
> 100.0 ? 100.0 : percent
;
43 convert_time (const char *string
)
45 if (strcmp(string
, "forever") == 0 ||
46 strcmp(string
, "-") == 0 ||
49 else if (strcmp(string
, "none") == 0 ||
50 strcmp(string
, "0") == 0)
58 suffix
= string
+ strspn(string
, "0123456789");
60 strncpy(worktime
, string
, suffix
- string
);
61 worktime
[suffix
- string
] = '\0';
62 inttime
= atoi(worktime
);
76 inttime
*= 24 * 60 * 60;
89 format_time (char *buffer
, const char *string
)
91 if (strcmp(string
, "forever") == 0 ||
92 strcmp(string
, "-") == 0 ||
94 strcpy(buffer
, "forever");
95 else if (strcmp(string
, "none") == 0 ||
96 strcmp(string
, "0") == 0)
97 strcpy(buffer
, "none");
105 suffix
= string
+ strspn(string
, "0123456789");
106 strncpy(worktime
, string
, suffix
- string
);
107 worktime
[suffix
- string
] = '\0';
108 int_time
= atoi(worktime
);
113 format
= "%d minute(s)";
116 format
= "%d hour(s)";
119 format
= "%d day(s)";
124 format
= "%d second(s)";
128 sprintf(buffer
, format
, int_time
);
135 maybe_sleep(const struct tuner
*tuner
, const char *wait_time
)
140 int_wait_time
= convert_time(wait_time
);
142 if (int_wait_time
> 0)
144 printf("Sleeping for %s\n", format_time(message
, wait_time
));
145 tuner_sleep(tuner
, int_wait_time
);
146 } else if (int_wait_time
== 0)
148 printf("Sleeping forever...CTRL-C exits\n");
157 printf("fmtools fm version %s\n\n", VERSION
);
158 printf("usage: %s [-h] [-o] [-q] [-d <dev>] [-t <tuner>] "
159 "[-T none|forever|time] <freq>|on|off [<volume>]\n\n",
161 printf("A small controller for Video for Linux radio devices.\n\n");
162 printf(" -h display this help\n");
163 printf(" -o override frequency range limits of card\n");
164 printf(" -q quiet mode\n");
165 printf(" -d <dev> select device (default: /dev/radio0)\n");
166 printf(" -t <tuner> select tuner (default: 0)\n");
167 printf(" -T <time> after setting frequency, sleep for some time\n"\
168 " (default: none; -=forever)\n");
169 printf(" <freq> frequency in MHz (i.e. 94.3)\n");
170 printf(" on turn radio on\n");
171 printf(" off turn radio off (mute)\n");
172 printf(" + increase volume\n");
173 printf(" - decrease volume\n");
174 printf(" <volume> percentage (0-100)\n");
179 getconfig(const char *fn
,
180 double *defaultvol
, double *increment
, char *wait_time
)
186 snprintf(buf
, sizeof buf
, "%s/.fmrc", getenv("HOME"));
189 conf
= fopen(fn
, "r");
194 while(fgets(buf
, sizeof(buf
), conf
)) {
195 buf
[strlen(buf
)-1] = 0;
196 if (!strncmp(buf
, "VOL", 3))
197 sscanf(buf
, "%*s %lf", defaultvol
);
198 if (!strncmp(buf
, "INCR", 3))
199 sscanf(buf
, "%*s %lf", increment
);
200 if (!strncmp(buf
, "TIME", 4))
201 sscanf(buf
, "%*s %s", wait_time
);
208 print_volume(const struct tuner
*tuner
)
210 if (tuner_has_volume_control(tuner
))
211 printf(" at %.2f%% volume", tuner_get_volume(tuner
));
213 printf(" (radio does not support volume control)");
217 print_mute(const struct tuner
*tuner
)
219 if (tuner_is_muted(tuner
))
220 printf(" (radio is muted, use \"fm on\" to unmute)");
223 int main(int argc
, char **argv
)
226 const char *device
= NULL
;
229 bool override
= false;
230 const char *config_file
= NULL
;
231 double defaultvol
= 12.5;
232 double increment
= 10;
233 char wait_time_buf
[256+1] = "";
234 const char *wait_time
= NULL
;
236 program_name
= argv
[0];
238 /* need at least a frequency */
240 fatal(0, "usage: %s [-h] [-o] [-q] [-d <dev>] [-t <tuner>] "
241 "[-T time|forever|none] "
242 "<freq>|on|off [<volume>]\n", program_name
);
245 int option
= getopt(argc
, argv
, "+qhot:T:c:d:");
257 index
= atoi(optarg
);
266 config_file
= optarg
;
275 getconfig(config_file
, &defaultvol
, &increment
, wait_time_buf
);
277 wait_time
= *wait_time_buf
? wait_time_buf
: "none";
282 if (argc
== 0) /* no frequency, on|off, or +|- given */
285 tuner_open(&tuner
, device
, index
);
287 if (!strcmp(argv
[0], "off")) {
288 tuner_set_mute(&tuner
, true);
290 printf("Radio muted\n");
291 } else if (!strcmp(argv
[0], "on")) {
292 tuner_set_mute(&tuner
, false);
295 print_volume(&tuner
);
298 } else if (!strcmp(argv
[0], "+") || !strcmp(argv
[0], "-")) {
300 if (!tuner_has_volume_control(&tuner
))
301 fatal(0, "Radio does not support volume control");
303 new_volume
= tuner_get_volume(&tuner
);
304 if (argv
[0][0] == '+')
305 new_volume
+= increment
;
307 new_volume
-= increment
;
308 new_volume
= clamp(new_volume
);
310 tuner_set_volume(&tuner
, new_volume
);
313 printf("Setting volume to %.2f%%", new_volume
);
317 } else if (atof(argv
[0])) {
318 double frequency
= atof(argv
[0]);
319 double volume
= argc
> 1 ? clamp(atof(argv
[1])) : defaultvol
;
320 tuner_set_freq(&tuner
, frequency
* 16000.0, override
);
321 if (tuner_has_volume_control(&tuner
))
322 tuner_set_volume(&tuner
, volume
);
324 printf("Radio tuned to %2.2f MHz", frequency
);
325 print_volume(&tuner
);
330 fatal(0, "unrecognized command syntax; use --help for help");
332 maybe_sleep(&tuner
, wait_time
);