1 Index: beep-1.2.2/beep.c
2 ===================================================================
3 --- beep-1.2.2.orig/beep.c
9 +#include <linux/input.h>
11 /* I don't know where this number comes from, I admit that freely. A
12 wonderful human named Raine M. Ekman used it in a program that played
13 @@ -87,18 +88,48 @@ typedef struct beep_parms_t {
14 struct beep_parms_t *next; /* in case -n/--new is used. */
17 +enum { BEEP_TYPE_CONSOLE, BEEP_TYPE_EVDEV };
19 /* Momma taught me never to use globals, but we need something the signal
20 handlers can get at.*/
22 +int console_type = BEEP_TYPE_CONSOLE;
23 +char *console_device = NULL;
26 +void do_beep(int freq) {
27 + if (console_type == BEEP_TYPE_CONSOLE) {
28 + if(ioctl(console_fd, KIOCSOUND, freq != 0
29 + ? (int)(CLOCK_TICK_RATE/freq)
31 + printf("\a"); /* Output the only beep we can, in an effort to fall back on usefulness */
35 + /* BEEP_TYPE_EVDEV */
36 + struct input_event e;
42 + write(console_fd, &e, sizeof(struct input_event));
47 /* If we get interrupted, it would be nice to not leave the speaker beeping in
49 void handle_signal(int signum) {
52 + free(console_device);
57 /* Kill the sound, quit gracefully */
58 - ioctl(console_fd, KIOCSOUND, 0);
63 @@ -111,7 +142,7 @@ void handle_signal(int signum) {
64 /* print usage and exit */
65 void usage_bail(const char *executable_name) {
66 printf("Usage:\n%s [-f freq] [-l length] [-r reps] [-d delay] "
67 - "[-D delay] [-s] [-c] [--verbose | --debug]\n",
68 + "[-D delay] [-s] [-c] [--verbose | --debug] [-e device]\n",
70 printf("%s [Options...] [-n] [--new] [Options...] ... \n", executable_name);
71 printf("%s [-h] [--help]\n", executable_name);
72 @@ -143,13 +174,14 @@ void usage_bail(const char *executable_n
73 void parse_command_line(int argc, char **argv, beep_parms_t *result) {
76 - struct option opt_list[6] = {{"help", 0, NULL, 'h'},
77 + struct option opt_list[7] = {{"help", 0, NULL, 'h'},
78 {"version", 0, NULL, 'V'},
79 {"new", 0, NULL, 'n'},
80 {"verbose", 0, NULL, 'X'},
81 {"debug", 0, NULL, 'X'},
82 + {"device", 1, NULL, 'e'},
84 - while((c = getopt_long(argc, argv, "f:l:r:d:D:schvVn", opt_list, NULL))
85 + while((c = getopt_long(argc, argv, "f:l:r:d:D:schvVne:", opt_list, NULL))
87 int argval = -1; /* handle parsed numbers for various arguments */
89 @@ -220,6 +252,9 @@ void parse_command_line(int argc, char *
90 case 'X' : /* --debug / --verbose */
93 + case 'e' : /* also --device */
94 + console_device = strdup(optarg);
96 case 'h' : /* notice that this is also --help */
99 @@ -238,24 +273,31 @@ void play_beep(beep_parms_t parms) {
100 parms.reps, parms.length, parms.delay, parms.end_delay, parms.freq);
102 /* try to snag the console */
103 - if((console_fd = open("/dev/tty0", O_WRONLY)) == -1) {
104 - if((console_fd = open("/dev/vc/0", O_WRONLY)) == -1) {
105 - fprintf(stderr, "Could not open /dev/tty0 or /dev/vc/0 for writing.\n");
106 - printf("\a"); /* Output the only beep we can, in an effort to fall back on usefulness */
111 + console_fd = open(console_device, O_WRONLY);
113 + if((console_fd = open("/dev/tty0", O_WRONLY)) == -1)
114 + console_fd = open("/dev/vc/0", O_WRONLY);
116 + if(console_fd == -1) {
117 + fprintf(stderr, "Could not open %s for writing\n",
118 + console_device != NULL ? console_device : "/dev/tty0 or /dev/vc/0");
119 + printf("\a"); /* Output the only beep we can, in an effort to fall back on usefulness */
124 + if (ioctl(console_fd, EVIOCGSND(0)) != -1)
125 + console_type = BEEP_TYPE_EVDEV;
127 + console_type = BEEP_TYPE_CONSOLE;
130 for (i = 0; i < parms.reps; i++) { /* start beep */
131 - if(ioctl(console_fd, KIOCSOUND, (int)(CLOCK_TICK_RATE/parms.freq)) < 0) {
132 - printf("\a"); /* Output the only beep we can, in an effort to fall back on usefulness */
135 + do_beep(parms.freq);
136 /* Look ma, I'm not ansi C compatible! */
137 usleep(1000*parms.length); /* wait... */
138 - ioctl(console_fd, KIOCSOUND, 0); /* stop beep */
139 + do_beep(0); /* stop beep */
140 if(parms.end_delay || (i+1 < parms.reps))
141 usleep(1000*parms.delay); /* wait... */
143 @@ -318,5 +360,8 @@ int main(int argc, char **argv) {
148 + free(console_device);