1 # the diff between Alessandro Zummo's copy of beep.c and the original
4 --- beep-1.2.2/beep.c.orig 2006-01-29 12:13:36.994560551 -0800
5 +++ beep-1.2.2/beep.c 2006-01-29 12:35:02.950558713 -0800
10 +#include <linux/input.h>
12 /* I don't know where this number comes from, I admit that freely. A
13 wonderful human named Raine M. Ekman used it in a program that played
14 @@ -86,18 +87,28 @@ typedef struct beep_parms_t {
15 struct beep_parms_t *next; /* in case -n/--new is used. */
18 +enum { BEEP_TYPE_CONSOLE, BEEP_TYPE_EVDEV };
20 /* Momma taught me never to use globals, but we need something the signal
21 handlers can get at.*/
23 +int console_type = BEEP_TYPE_CONSOLE;
24 +char *console_device = NULL;
26 +void do_beep(int freq);
28 /* If we get interrupted, it would be nice to not leave the speaker beeping in
30 void handle_signal(int signum) {
33 + free(console_device);
38 /* Kill the sound, quit gracefully */
39 - ioctl(console_fd, KIOCSOUND, 0);
44 @@ -110,7 +121,7 @@ void handle_signal(int signum) {
45 /* print usage and exit */
46 void usage_bail(const char *executable_name) {
47 printf("Usage:\n%s [-f freq] [-l length] [-r reps] [-d delay] "
48 - "[-D delay] [-s] [-c]\n",
49 + "[-D delay] [-s] [-c] [-e device]\n",
51 printf("%s [Options...] [-n] [--new] [Options...] ... \n", executable_name);
52 printf("%s [-h] [--help]\n", executable_name);
53 @@ -141,11 +152,12 @@ void usage_bail(const char *executable_n
54 void parse_command_line(int argc, char **argv, beep_parms_t *result) {
57 - struct option opt_list[4] = {{"help", 0, NULL, 'h'},
58 + struct option opt_list[] = {{"help", 0, NULL, 'h'},
59 {"version", 0, NULL, 'V'},
60 {"new", 0, NULL, 'n'},
61 + {"device", 1, NULL, 'e'},
63 - while((c = getopt_long(argc, argv, "f:l:r:d:D:schvVn", opt_list, NULL))
64 + while((c = getopt_long(argc, argv, "f:l:r:d:D:schvVne:", opt_list, NULL))
66 int argval = -1; /* handle parsed numbers for various arguments */
68 @@ -207,6 +219,9 @@ void parse_command_line(int argc, char *
69 result->next->next = NULL;
70 result = result->next; /* yes, I meant to do that. */
72 + case 'e' : /* also --device */
73 + console_device = strdup(optarg);
75 case 'h' : /* notice that this is also --help */
78 @@ -214,26 +229,61 @@ void parse_command_line(int argc, char *
82 +void do_beep(int freq)
84 + if (console_type == BEEP_TYPE_CONSOLE)
86 + if(ioctl(console_fd, KIOCSOUND, freq != 0
87 + ? (int)(CLOCK_TICK_RATE/freq)
89 + printf("\a"); /* Output the only beep we can, in an effort to fall back on usefulness */
95 + /* BEEP_TYPE_EVDEV */
96 + struct input_event e;
102 + write(console_fd, &e, sizeof(struct input_event));
106 void play_beep(beep_parms_t parms) {
107 int i; /* loop counter */
109 /* try to snag the console */
110 - if((console_fd = open("/dev/console", O_WRONLY)) == -1) {
111 - fprintf(stderr, "Could not open /dev/console for writing.\n");
114 + console_fd = open(console_device, O_WRONLY);
116 + if((console_fd = open("/dev/input/event0", O_WRONLY)) == -1)
117 + if((console_fd = open("/dev/tty0", O_WRONLY)) == -1)
118 + console_fd = open("/dev/vc/0", O_WRONLY);
120 + if(console_fd == -1) {
121 + fprintf(stderr, "Could not open %s for writing\n",
122 + console_device != NULL ? console_device : "/dev/tty0 or /dev/vc/0");
123 printf("\a"); /* Output the only beep we can, in an effort to fall back on usefulness */
128 + if (ioctl(console_fd, EVIOCGSND(0)) != -1)
129 + console_type = BEEP_TYPE_EVDEV;
131 + console_type = BEEP_TYPE_CONSOLE;
134 for (i = 0; i < parms.reps; i++) { /* start beep */
135 - if(ioctl(console_fd, KIOCSOUND, (int)(CLOCK_TICK_RATE/parms.freq)) < 0) {
136 - printf("\a"); /* Output the only beep we can, in an effort to fall back on usefulness */
139 + do_beep(parms.freq);
140 /* Look ma, I'm not ansi C compatible! */
141 usleep(1000*parms.length); /* wait... */
142 - ioctl(console_fd, KIOCSOUND, 0); /* stop beep */
144 if(parms.end_delay || (i+1 < parms.reps))
145 usleep(1000*parms.delay); /* wait... */
147 @@ -295,5 +345,8 @@ int main(int argc, char **argv) {
152 + free(console_device);