diff --git a/beep.c b/beep.c index 7da2e70..9c9ddee 100644 --- a/beep.c +++ b/beep.c @@ -106,17 +106,29 @@ void do_beep(int freq) { perror("ioctl"); } } else { - /* BEEP_TYPE_EVDEV */ - struct input_event e; - - e.type = EV_SND; - e.code = SND_TONE; - e.value = freq; + /* BEEP_TYPE_EVDEV */ + struct input_event e; + unsigned long evbit = 0; + + e.type = EV_SND; + + /* check supported events and act accordingly */ + ioctl(console_fd, EVIOCGBIT(EV_SND, sizeof(evbit)), &evbit); + if(evbit & (1 << SND_TONE)) { + e.code = SND_TONE; + e.value = freq; + } else if(evbit & (1 << SND_BELL)) { + e.code = SND_BELL; + e.value = (freq != 0); + } else { + perror("no supported event type"); + return; + } - if(write(console_fd, &e, sizeof(struct input_event)) < 0) { - putchar('\a'); /* See above */ - perror("write"); - } + if(write(console_fd, &e, sizeof(struct input_event)) < 0) { + putchar('\a'); /* See above */ + perror("write"); + } } }