1*4882a593Smuzhiyun# the diff between Alessandro Zummo's copy of beep.c and the original 2*4882a593Smuzhiyun# one... 3*4882a593Smuzhiyun 4*4882a593Smuzhiyun--- beep-1.2.2/beep.c.orig 2006-01-29 12:13:36.994560551 -0800 5*4882a593Smuzhiyun+++ beep-1.2.2/beep.c 2006-01-29 12:35:02.950558713 -0800 6*4882a593Smuzhiyun@@ -26,6 +26,7 @@ 7*4882a593Smuzhiyun #include <sys/ioctl.h> 8*4882a593Smuzhiyun #include <sys/types.h> 9*4882a593Smuzhiyun #include <linux/kd.h> 10*4882a593Smuzhiyun+#include <linux/input.h> 11*4882a593Smuzhiyun 12*4882a593Smuzhiyun /* I don't know where this number comes from, I admit that freely. A 13*4882a593Smuzhiyun wonderful human named Raine M. Ekman used it in a program that played 14*4882a593Smuzhiyun@@ -86,18 +87,28 @@ typedef struct beep_parms_t { 15*4882a593Smuzhiyun struct beep_parms_t *next; /* in case -n/--new is used. */ 16*4882a593Smuzhiyun } beep_parms_t; 17*4882a593Smuzhiyun 18*4882a593Smuzhiyun+enum { BEEP_TYPE_CONSOLE, BEEP_TYPE_EVDEV }; 19*4882a593Smuzhiyun+ 20*4882a593Smuzhiyun /* Momma taught me never to use globals, but we need something the signal 21*4882a593Smuzhiyun handlers can get at.*/ 22*4882a593Smuzhiyun int console_fd = -1; 23*4882a593Smuzhiyun+int console_type = BEEP_TYPE_CONSOLE; 24*4882a593Smuzhiyun+char *console_device = NULL; 25*4882a593Smuzhiyun+ 26*4882a593Smuzhiyun+void do_beep(int freq); 27*4882a593Smuzhiyun 28*4882a593Smuzhiyun /* If we get interrupted, it would be nice to not leave the speaker beeping in 29*4882a593Smuzhiyun perpetuity. */ 30*4882a593Smuzhiyun void handle_signal(int signum) { 31*4882a593Smuzhiyun+ 32*4882a593Smuzhiyun+ if(console_device) 33*4882a593Smuzhiyun+ free(console_device); 34*4882a593Smuzhiyun+ 35*4882a593Smuzhiyun switch(signum) { 36*4882a593Smuzhiyun case SIGINT: 37*4882a593Smuzhiyun if(console_fd >= 0) { 38*4882a593Smuzhiyun /* Kill the sound, quit gracefully */ 39*4882a593Smuzhiyun- ioctl(console_fd, KIOCSOUND, 0); 40*4882a593Smuzhiyun+ do_beep(0); 41*4882a593Smuzhiyun close(console_fd); 42*4882a593Smuzhiyun exit(signum); 43*4882a593Smuzhiyun } else { 44*4882a593Smuzhiyun@@ -110,7 +121,7 @@ void handle_signal(int signum) { 45*4882a593Smuzhiyun /* print usage and exit */ 46*4882a593Smuzhiyun void usage_bail(const char *executable_name) { 47*4882a593Smuzhiyun printf("Usage:\n%s [-f freq] [-l length] [-r reps] [-d delay] " 48*4882a593Smuzhiyun- "[-D delay] [-s] [-c]\n", 49*4882a593Smuzhiyun+ "[-D delay] [-s] [-c] [-e device]\n", 50*4882a593Smuzhiyun executable_name); 51*4882a593Smuzhiyun printf("%s [Options...] [-n] [--new] [Options...] ... \n", executable_name); 52*4882a593Smuzhiyun printf("%s [-h] [--help]\n", executable_name); 53*4882a593Smuzhiyun@@ -141,11 +152,12 @@ void usage_bail(const char *executable_n 54*4882a593Smuzhiyun void parse_command_line(int argc, char **argv, beep_parms_t *result) { 55*4882a593Smuzhiyun int c; 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun- struct option opt_list[4] = {{"help", 0, NULL, 'h'}, 58*4882a593Smuzhiyun+ struct option opt_list[] = {{"help", 0, NULL, 'h'}, 59*4882a593Smuzhiyun {"version", 0, NULL, 'V'}, 60*4882a593Smuzhiyun {"new", 0, NULL, 'n'}, 61*4882a593Smuzhiyun+ {"device", 1, NULL, 'e'}, 62*4882a593Smuzhiyun {0,0,0,0}}; 63*4882a593Smuzhiyun- while((c = getopt_long(argc, argv, "f:l:r:d:D:schvVn", opt_list, NULL)) 64*4882a593Smuzhiyun+ while((c = getopt_long(argc, argv, "f:l:r:d:D:schvVne:", opt_list, NULL)) 65*4882a593Smuzhiyun != EOF) { 66*4882a593Smuzhiyun int argval = -1; /* handle parsed numbers for various arguments */ 67*4882a593Smuzhiyun float argfreq = -1; 68*4882a593Smuzhiyun@@ -207,6 +219,9 @@ void parse_command_line(int argc, char * 69*4882a593Smuzhiyun result->next->next = NULL; 70*4882a593Smuzhiyun result = result->next; /* yes, I meant to do that. */ 71*4882a593Smuzhiyun break; 72*4882a593Smuzhiyun+ case 'e' : /* also --device */ 73*4882a593Smuzhiyun+ console_device = strdup(optarg); 74*4882a593Smuzhiyun+ break; 75*4882a593Smuzhiyun case 'h' : /* notice that this is also --help */ 76*4882a593Smuzhiyun default : 77*4882a593Smuzhiyun usage_bail(argv[0]); 78*4882a593Smuzhiyun@@ -214,26 +229,61 @@ void parse_command_line(int argc, char * 79*4882a593Smuzhiyun } 80*4882a593Smuzhiyun } 81*4882a593Smuzhiyun 82*4882a593Smuzhiyun+void do_beep(int freq) 83*4882a593Smuzhiyun+{ 84*4882a593Smuzhiyun+ if (console_type == BEEP_TYPE_CONSOLE) 85*4882a593Smuzhiyun+ { 86*4882a593Smuzhiyun+ if(ioctl(console_fd, KIOCSOUND, freq != 0 87*4882a593Smuzhiyun+ ? (int)(CLOCK_TICK_RATE/freq) 88*4882a593Smuzhiyun+ : freq) < 0) { 89*4882a593Smuzhiyun+ printf("\a"); /* Output the only beep we can, in an effort to fall back on usefulness */ 90*4882a593Smuzhiyun+ perror("ioctl"); 91*4882a593Smuzhiyun+ } 92*4882a593Smuzhiyun+ } 93*4882a593Smuzhiyun+ else 94*4882a593Smuzhiyun+ { 95*4882a593Smuzhiyun+ /* BEEP_TYPE_EVDEV */ 96*4882a593Smuzhiyun+ struct input_event e; 97*4882a593Smuzhiyun+ 98*4882a593Smuzhiyun+ e.type = EV_SND; 99*4882a593Smuzhiyun+ e.code = SND_TONE; 100*4882a593Smuzhiyun+ e.value = freq; 101*4882a593Smuzhiyun+ 102*4882a593Smuzhiyun+ write(console_fd, &e, sizeof(struct input_event)); 103*4882a593Smuzhiyun+ } 104*4882a593Smuzhiyun+} 105*4882a593Smuzhiyun+ 106*4882a593Smuzhiyun void play_beep(beep_parms_t parms) { 107*4882a593Smuzhiyun int i; /* loop counter */ 108*4882a593Smuzhiyun 109*4882a593Smuzhiyun /* try to snag the console */ 110*4882a593Smuzhiyun- if((console_fd = open("/dev/console", O_WRONLY)) == -1) { 111*4882a593Smuzhiyun- fprintf(stderr, "Could not open /dev/console for writing.\n"); 112*4882a593Smuzhiyun+ 113*4882a593Smuzhiyun+ if(console_device) 114*4882a593Smuzhiyun+ console_fd = open(console_device, O_WRONLY); 115*4882a593Smuzhiyun+ else 116*4882a593Smuzhiyun+ if((console_fd = open("/dev/input/event0", O_WRONLY)) == -1) 117*4882a593Smuzhiyun+ if((console_fd = open("/dev/tty0", O_WRONLY)) == -1) 118*4882a593Smuzhiyun+ console_fd = open("/dev/vc/0", O_WRONLY); 119*4882a593Smuzhiyun+ 120*4882a593Smuzhiyun+ if(console_fd == -1) { 121*4882a593Smuzhiyun+ fprintf(stderr, "Could not open %s for writing\n", 122*4882a593Smuzhiyun+ console_device != NULL ? console_device : "/dev/tty0 or /dev/vc/0"); 123*4882a593Smuzhiyun printf("\a"); /* Output the only beep we can, in an effort to fall back on usefulness */ 124*4882a593Smuzhiyun perror("open"); 125*4882a593Smuzhiyun exit(1); 126*4882a593Smuzhiyun } 127*4882a593Smuzhiyun 128*4882a593Smuzhiyun+ if (ioctl(console_fd, EVIOCGSND(0)) != -1) 129*4882a593Smuzhiyun+ console_type = BEEP_TYPE_EVDEV; 130*4882a593Smuzhiyun+ else 131*4882a593Smuzhiyun+ console_type = BEEP_TYPE_CONSOLE; 132*4882a593Smuzhiyun+ 133*4882a593Smuzhiyun /* Beep */ 134*4882a593Smuzhiyun for (i = 0; i < parms.reps; i++) { /* start beep */ 135*4882a593Smuzhiyun- if(ioctl(console_fd, KIOCSOUND, (int)(CLOCK_TICK_RATE/parms.freq)) < 0) { 136*4882a593Smuzhiyun- printf("\a"); /* Output the only beep we can, in an effort to fall back on usefulness */ 137*4882a593Smuzhiyun- perror("ioctl"); 138*4882a593Smuzhiyun- } 139*4882a593Smuzhiyun+ do_beep(parms.freq); 140*4882a593Smuzhiyun /* Look ma, I'm not ansi C compatible! */ 141*4882a593Smuzhiyun usleep(1000*parms.length); /* wait... */ 142*4882a593Smuzhiyun- ioctl(console_fd, KIOCSOUND, 0); /* stop beep */ 143*4882a593Smuzhiyun+ do_beep(0); 144*4882a593Smuzhiyun if(parms.end_delay || (i+1 < parms.reps)) 145*4882a593Smuzhiyun usleep(1000*parms.delay); /* wait... */ 146*4882a593Smuzhiyun } /* repeat. */ 147*4882a593Smuzhiyun@@ -295,5 +345,8 @@ int main(int argc, char **argv) { 148*4882a593Smuzhiyun parms = next; 149*4882a593Smuzhiyun } 150*4882a593Smuzhiyun 151*4882a593Smuzhiyun+ if(console_device) 152*4882a593Smuzhiyun+ free(console_device); 153*4882a593Smuzhiyun+ 154*4882a593Smuzhiyun return EXIT_SUCCESS; 155*4882a593Smuzhiyun } 156