blob: 013a72543e9f2fcf914555cd4f913ff4e8cedc13 [file] [log] [blame]
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +02001/* vi: set sw=4 ts=4: */
2/*
3 * beep implementation for busybox
4 *
5 * Copyright (C) 2009 Bernhard Reutner-Fischer
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +02008 *
9 */
10#include "libbb.h"
11
12#include <linux/kd.h>
13#ifndef CLOCK_TICK_RATE
Denys Vlasenko0da1c0a2009-08-22 18:00:39 +020014# define CLOCK_TICK_RATE 1193180
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +020015#endif
16
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +020017/* defaults */
Bernhard Reutner-Fischer00ea82e2009-08-21 14:40:29 +020018#ifndef CONFIG_FEATURE_BEEP_FREQ
19# define FREQ (4000)
20#else
21# define FREQ (CONFIG_FEATURE_BEEP_FREQ)
22#endif
Denys Vlasenko0da1c0a2009-08-22 18:00:39 +020023#ifndef CONFIG_FEATURE_BEEP_LENGTH_MS
Bernhard Reutner-Fischer00ea82e2009-08-21 14:40:29 +020024# define LENGTH (30)
25#else
Denys Vlasenko0da1c0a2009-08-22 18:00:39 +020026# define LENGTH (CONFIG_FEATURE_BEEP_LENGTH_MS)
Bernhard Reutner-Fischer00ea82e2009-08-21 14:40:29 +020027#endif
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +020028#define DELAY (0)
29#define REPETITIONS (1)
30
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +020031int beep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko0da1c0a2009-08-22 18:00:39 +020032int beep_main(int argc, char **argv)
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +020033{
34 int speaker = get_console_fd_or_die();
Denys Vlasenko31e2e7b2009-12-12 02:42:35 +010035 unsigned tickrate_div_freq = tickrate_div_freq; /* for compiler */
36 unsigned length = length;
37 unsigned delay = delay;
38 unsigned rep = rep;
Denys Vlasenko0da1c0a2009-08-22 18:00:39 +020039 int c;
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +020040
Denys Vlasenko0da1c0a2009-08-22 18:00:39 +020041 c = 'n';
42 while (c != -1) {
43 if (c == 'n') {
44 tickrate_div_freq = CLOCK_TICK_RATE / FREQ;
45 length = LENGTH;
46 delay = DELAY;
47 rep = REPETITIONS;
48 }
49 c = getopt(argc, argv, "f:l:d:r:n");
50/* TODO: -s, -c:
51 * pipe stdin to stdout, but also beep after each line (-s) or char (-c)
52 */
53 switch (c) {
54 case 'f':
55/* TODO: what "-f 0" should do? */
56 tickrate_div_freq = (unsigned)CLOCK_TICK_RATE / xatou(optarg);
57 continue;
58 case 'l':
59 length = xatou(optarg);
60 continue;
61 case 'd':
62/* TODO:
63 * -d N, -D N
64 * specify a delay of N milliseconds between repetitions.
65 * -d specifies that this delay should only occur between beeps,
66 * that is, it should not occur after the last repetition.
67 * -D indicates that the delay should occur after every repetition
68 */
69 delay = xatou(optarg);
70 continue;
71 case 'r':
72 rep = xatou(optarg);
73 continue;
74 case 'n':
75 case -1:
76 break;
77 default:
78 bb_show_usage();
79 }
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +020080 while (rep) {
81//bb_info_msg("rep[%d] freq=%d, length=%d, delay=%d", rep, freq, length, delay);
Natanael Copa7cfec4b2010-03-14 15:32:49 +010082 xioctl(speaker, KIOCSOUND, (void*)(uintptr_t)tickrate_div_freq);
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +020083 usleep(1000 * length);
Denys Vlasenko0da1c0a2009-08-22 18:00:39 +020084 ioctl(speaker, KIOCSOUND, (void*)0);
85 if (--rep)
Natanael Copa7cfec4b2010-03-14 15:32:49 +010086 usleep(1000 * delay);
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +020087 }
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +020088 }
Denys Vlasenko0da1c0a2009-08-22 18:00:39 +020089
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +020090 if (ENABLE_FEATURE_CLEAN_UP)
91 close(speaker);
92 return EXIT_SUCCESS;
93}
94/*
95 * so, e.g. Beethoven's 9th symphony "Ode an die Freude" would be
96 * something like:
97a=$((220*3))
98b=$((247*3))
99c=$((262*3))
100d=$((294*3))
101e=$((329*3))
102f=$((349*3))
103g=$((392*3))
104#./beep -f$d -l200 -r2 -n -f$e -l100 -d 10 -n -f$c -l400 -f$g -l200
105./beep -f$e -l200 -r2 \
106 -n -d 100 -f$f -l200 \
107 -n -f$g -l200 -r2 \
108 -n -f$f -l200 \
109 -n -f$e -l200 \
Denys Vlasenko0da1c0a2009-08-22 18:00:39 +0200110 -n -f$d -l200 \
111 -n -f$c -l200 -r2 \
112 -n -f$d -l200 \
113 -n -f$e -l200 \
114 -n -f$e -l400 \
Bernhard Reutner-Fischer45de0742009-08-21 13:18:31 +0200115 -n -f$d -l100 \
116 -n -f$d -l200 \
117*/