79e75473c86ae617a882e05605a9d7845caac30c
[oweals/busybox.git] / miscutils / beep.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * beep implementation for busybox
4  *
5  * Copyright (C) 2009 Bernhard Reutner-Fischer
6  *
7  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
8  *
9  */
10 #include "libbb.h"
11
12 #include <linux/kd.h>
13 #ifndef CLOCK_TICK_RATE
14 #define CLOCK_TICK_RATE 1193180
15 #endif
16
17 #define OPT_f (1<<0)
18 #define OPT_l (1<<1)
19 #define OPT_d (1<<2)
20 #define OPT_r (1<<3)
21 /* defaults */
22 #ifndef CONFIG_FEATURE_BEEP_FREQ
23 # define FREQ (4000)
24 #else
25 # define FREQ (CONFIG_FEATURE_BEEP_FREQ)
26 #endif
27 #ifndef CONFIG_FEATURE_BEEP_LENGTH
28 # define LENGTH (30)
29 #else
30 # define LENGTH (CONFIG_FEATURE_BEEP_LENGTH)
31 #endif
32 #define DELAY (0)
33 #define REPETITIONS (1)
34
35 #define GET_ARG do { if (!*++opt) opt = *++argv; if (opt == NULL) bb_show_usage();} while (0)
36 #define NEW_BEEP() { \
37         freq = FREQ; \
38         length = LENGTH; \
39         delay = DELAY; \
40         rep = REPETITIONS; \
41         }
42
43 int beep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
44 int beep_main(int argc UNUSED_PARAM, char **argv)
45 {
46         int speaker = get_console_fd_or_die();
47         unsigned freq, length, delay, rep;
48         unsigned long ioctl_arg;
49         char *opt = NULL;
50         bool do_parse = true;
51
52         NEW_BEEP()
53         while (*argv && *++argv) {
54                 opt = *argv;
55
56                 while (*opt == '-')
57                         ++opt;
58                 if (do_parse)
59                         switch (*opt) {
60                         case 'f':
61                                 GET_ARG;
62                                 freq = xatoul(opt);
63                                 continue;
64                         case 'l':
65                                 GET_ARG;
66                                 length = xatoul(opt);
67                                 continue;
68                         case 'd':
69                                 GET_ARG;
70                                 delay = xatoul(opt);
71                                 continue;
72                         case 'r':
73                                 GET_ARG;
74                                 rep = xatoul(opt);
75                                 continue;
76                         case 'n':
77                                 break;
78                         default:
79                                 bb_show_usage();
80                                 break;
81                         }
82  again:
83                 while (rep) {
84 //bb_info_msg("rep[%d] freq=%d, length=%d, delay=%d", rep, freq, length, delay);
85                         ioctl_arg = (int)(CLOCK_TICK_RATE/freq);
86                         xioctl(speaker, KIOCSOUND, (void*)ioctl_arg);
87                         usleep(1000 * length);
88                         ioctl(speaker, KIOCSOUND, 0);
89                         if (rep--)
90                                 usleep(delay);
91                 }
92                 if (opt && *opt == 'n')
93                                 NEW_BEEP()
94                 if (!do_parse && *argv == NULL)
95                         goto out;
96         }
97         do_parse = false;
98         goto again;
99  out:
100         if (ENABLE_FEATURE_CLEAN_UP)
101                 close(speaker);
102         return EXIT_SUCCESS;
103 }
104 /*
105  * so, e.g. Beethoven's 9th symphony "Ode an die Freude" would be
106  * something like:
107 a=$((220*3))
108 b=$((247*3))
109 c=$((262*3))
110 d=$((294*3))
111 e=$((329*3))
112 f=$((349*3))
113 g=$((392*3))
114 #./beep -f$d -l200 -r2 -n -f$e -l100 -d 10 -n -f$c -l400 -f$g -l200
115 ./beep -f$e -l200 -r2 \
116         -n -d 100 -f$f -l200 \
117         -n -f$g -l200 -r2 \
118         -n -f$f -l200 \
119         -n -f$e -l200 \
120         -n -f$d -l200  \
121         -n -f$c -l200 -r2  \
122         -n -f$d -l200  \
123         -n -f$e -l200  \
124         -n -f$e -l400  \
125         -n -f$d -l100 \
126         -n -f$d -l200 \
127 */