Fawkes API Fawkes Development Version
beep.cpp
1
2/***************************************************************************
3 * beep.cpp - Beeper utility class
4 *
5 * Created: Sun Apr 11 19:41:23 2010
6 * Copyright 2006-2010 Tim Niemueller [www.niemueller.de]
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version. A runtime exception applies to
14 * this software (see LICENSE.GPL_WRE file mentioned below for details).
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
20 *
21 * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22 */
23
24#include "beep.h"
25
26#include <linux/kd.h>
27#include <sys/ioctl.h>
28#include <sys/types.h>
29
30#include <cerrno>
31#include <cstdio>
32#include <cstring>
33#include <fcntl.h>
34#include <termios.h>
35#include <unistd.h>
36
37/* From console_ioctl man page, explained in the beep too by
38 * Johnathan Nightingale:
39 * This number represents the fixed frequency of the original PC XT's
40 * timer chip (the 8254 AFAIR), which is approximately 1.193 MHz. This
41 * number is divided with the desired frequency to obtain a counter value,
42 * that is subsequently fed into the timer chip, tied to the PC speaker.
43 * The chip decreases this counter at every tick (1.193 MHz) and when it
44 * reaches zero, it toggles the state of the speaker (on/off, or in/out),
45 * resets the counter to the original value, and starts over. The end
46 * result of this is a tone at approximately the desired frequency. :)
47 */
48#define CLOCK_TICK_RATE 1193180
49
50#define CONSOLE_FILE "/dev/console"
51
52/** @class BeepController "beep.h"
53 * Simple speaker beep controller.
54 * @author Tim Niemueller
55 */
56
57/** Constructor. */
59{
60 disable_beeping_ = false;
61}
62
63/** Enable beeping.
64 * @param freq frequency to beep with
65 */
66void
68{
69 if (disable_beeping_)
70 return;
71
72 int beep_fd = open(CONSOLE_FILE, O_WRONLY);
73 if (beep_fd == -1) {
74 char errstr[1024];
75 strerror_r(errno, errstr, sizeof(errstr));
76 //logger->log_warn(name(), "Could not open console (%s). "
77 // "Disabling warning beeps.", errstr);
78 disable_beeping_ = true;
79 } else {
80 if (ioctl(beep_fd, KIOCSOUND, (int)(CLOCK_TICK_RATE / freq)) < 0) {
81 //logger->log_warn(name(), "Starting to beep failed. Disabling warning beeps.");
82 disable_beeping_ = true;
83 }
84 close(beep_fd);
85 }
86}
87
88/** Disable beeping. */
89void
91{
92 if (disable_beeping_)
93 return;
94
95 int beep_fd = open(CONSOLE_FILE, O_WRONLY);
96 if (beep_fd == -1) {
97 char errstr[1024];
98 strerror_r(errno, errstr, sizeof(errstr));
99 //logger->log_warn(name(), "Could not open console (%s) [stop]. "
100 // "Disabling warning beeps.", errstr);
101 disable_beeping_ = true;
102 } else {
103 if (ioctl(beep_fd, KIOCSOUND, 0) < 0) {
104 //logger->log_warn(name(), "Stopping beeping failed. "
105 // "Disabling warning beeps.");
106 disable_beeping_ = true;
107 }
108 close(beep_fd);
109 }
110}
void beep_on(float freq=1000)
Enable beeping.
Definition: beep.cpp:67
void beep_off()
Disable beeping.
Definition: beep.cpp:90
BeepController()
Constructor.
Definition: beep.cpp:58