1 Author: Alessandro Zummo vim:ft=diff:
2 Description: evdev and general device node support (BTS #350214)
4 Index: beep-1.2.2/beep.c
5 ===================================================================
6 --- beep-1.2.2.orig/beep.c
10 #include <sys/types.h>
12 +#include <linux/input.h>
14 /* I don't know where this number comes from, I admit that freely. A
15 wonderful human named Raine M. Ekman used it in a program that played
16 @@ -87,18 +88,48 @@ typedef struct beep_parms_t {
17 struct beep_parms_t *next; /* in case -n/--new is used. */
20 +enum { BEEP_TYPE_CONSOLE, BEEP_TYPE_EVDEV };
22 /* Momma taught me never to use globals, but we need something the signal
23 handlers can get at.*/
25 +int console_type = BEEP_TYPE_CONSOLE;
26 +char *console_device = NULL;
29 +void do_beep(int freq) {
30 + if (console_type == BEEP_TYPE_CONSOLE) {
31 + if(ioctl(console_fd, KIOCSOUND, freq != 0
32 + ? (int)(CLOCK_TICK_RATE/freq)
34 + printf("\a"); /* Output the only beep we can, in an effort to fall back on usefulness */
38 + /* BEEP_TYPE_EVDEV */
39 + struct input_event e;
45 + write(console_fd, &e, sizeof(struct input_event));
50 /* If we get interrupted, it would be nice to not leave the speaker beeping in
52 void handle_signal(int signum) {
55 + free(console_device);
60 /* Kill the sound, quit gracefully */
61 - ioctl(console_fd, KIOCSOUND, 0);
66 @@ -111,7 +142,7 @@ void handle_signal(int signum) {
67 /* print usage and exit */
68 void usage_bail(const char *executable_name) {
69 printf("Usage:\n%s [-f freq] [-l length] [-r reps] [-d delay] "
70 - "[-D delay] [-s] [-c] [--verbose | --debug]\n",
71 + "[-D delay] [-s] [-c] [--verbose | --debug] [-e device]\n",
73 printf("%s [Options...] [-n] [--new] [Options...] ... \n", executable_name);
74 printf("%s [-h] [--help]\n", executable_name);
75 @@ -143,13 +174,14 @@ void usage_bail(const char *executable_n
76 void parse_command_line(int argc, char **argv, beep_parms_t *result) {
79 - struct option opt_list[6] = {{"help", 0, NULL, 'h'},
80 + struct option opt_list[7] = {{"help", 0, NULL, 'h'},
81 {"version", 0, NULL, 'V'},
82 {"new", 0, NULL, 'n'},
83 {"verbose", 0, NULL, 'X'},
84 {"debug", 0, NULL, 'X'},
85 + {"device", 1, NULL, 'e'},
87 - while((c = getopt_long(argc, argv, "f:l:r:d:D:schvVn", opt_list, NULL))
88 + while((c = getopt_long(argc, argv, "f:l:r:d:D:schvVne:", opt_list, NULL))
90 int argval = -1; /* handle parsed numbers for various arguments */
92 @@ -220,6 +252,9 @@ void parse_command_line(int argc, char *
93 case 'X' : /* --debug / --verbose */
96 + case 'e' : /* also --device */
97 + console_device = strdup(optarg);
99 case 'h' : /* notice that this is also --help */
102 @@ -238,24 +273,31 @@ void play_beep(beep_parms_t parms) {
103 parms.reps, parms.length, parms.delay, parms.end_delay, parms.freq);
105 /* try to snag the console */
106 - if((console_fd = open("/dev/tty0", O_WRONLY)) == -1) {
107 - if((console_fd = open("/dev/vc/0", O_WRONLY)) == -1) {
108 - fprintf(stderr, "Could not open /dev/tty0 or /dev/vc/0 for writing.\n");
109 - printf("\a"); /* Output the only beep we can, in an effort to fall back on usefulness */
114 + console_fd = open(console_device, O_WRONLY);
116 + if((console_fd = open("/dev/tty0", O_WRONLY)) == -1)
117 + console_fd = open("/dev/vc/0", O_WRONLY);
119 + if(console_fd == -1) {
120 + fprintf(stderr, "Could not open %s for writing\n",
121 + console_device != NULL ? console_device : "/dev/tty0 or /dev/vc/0");
122 + printf("\a"); /* Output the only beep we can, in an effort to fall back on usefulness */
127 + if (ioctl(console_fd, EVIOCGSND(0)) != -1)
128 + console_type = BEEP_TYPE_EVDEV;
130 + console_type = BEEP_TYPE_CONSOLE;
133 for (i = 0; i < parms.reps; i++) { /* start beep */
134 - if(ioctl(console_fd, KIOCSOUND, (int)(CLOCK_TICK_RATE/parms.freq)) < 0) {
135 - printf("\a"); /* Output the only beep we can, in an effort to fall back on usefulness */
138 + do_beep(parms.freq);
139 /* Look ma, I'm not ansi C compatible! */
140 usleep(1000*parms.length); /* wait... */
141 - ioctl(console_fd, KIOCSOUND, 0); /* stop beep */
142 + do_beep(0); /* stop beep */
143 if(parms.end_delay || (i+1 < parms.reps))
144 usleep(1000*parms.delay); /* wait... */
146 @@ -318,5 +360,8 @@ int main(int argc, char **argv) {
151 + free(console_device);