2004-01-16 21:18:38 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2011-02-25 01:05:47 +03:00
|
|
|
// $Id$
|
2004-01-16 21:18:38 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2003 by David N. Welton <davidw@dedasys.com>.
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
2009-02-08 12:05:52 +03:00
|
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2004-01-16 19:30:46 +03:00
|
|
|
|
2004-02-02 02:42:04 +03:00
|
|
|
#define BX_PLUGGABLE
|
|
|
|
|
2004-06-19 19:20:15 +04:00
|
|
|
#include "iodev.h"
|
2004-09-05 14:30:19 +04:00
|
|
|
#include "speaker.h"
|
2012-05-20 22:58:08 +04:00
|
|
|
|
|
|
|
#if BX_SUPPORT_SOUNDLOW
|
2012-05-20 18:19:49 +04:00
|
|
|
#include "sound/soundmod.h"
|
2012-05-20 22:58:08 +04:00
|
|
|
#endif
|
2004-06-19 19:20:15 +04:00
|
|
|
|
2004-01-16 21:18:38 +03:00
|
|
|
#ifdef __linux__
|
2004-01-16 19:30:46 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <linux/kd.h>
|
2004-01-16 21:18:38 +03:00
|
|
|
#endif
|
2004-01-16 19:30:46 +03:00
|
|
|
|
2004-02-02 02:42:04 +03:00
|
|
|
#define LOG_THIS theSpeaker->
|
|
|
|
|
|
|
|
bx_speaker_c *theSpeaker= NULL;
|
|
|
|
|
2006-09-10 21:18:44 +04:00
|
|
|
int libspeaker_LTX_plugin_init(plugin_t *plugin, plugintype_t type, int argc, char *argv[])
|
2004-02-02 02:42:04 +03:00
|
|
|
{
|
2006-09-10 21:18:44 +04:00
|
|
|
theSpeaker = new bx_speaker_c();
|
2004-02-02 02:42:04 +03:00
|
|
|
bx_devices.pluginSpeaker = theSpeaker;
|
|
|
|
BX_REGISTER_DEVICE_DEVMODEL(plugin, type, theSpeaker, BX_PLUGIN_SPEAKER);
|
|
|
|
return(0); // Success
|
|
|
|
}
|
|
|
|
|
2006-09-10 21:18:44 +04:00
|
|
|
void libspeaker_LTX_plugin_fini(void)
|
2004-02-02 02:42:04 +03:00
|
|
|
{
|
2011-12-27 00:32:56 +04:00
|
|
|
bx_devices.pluginSpeaker = &bx_devices.stubSpeaker;
|
2006-09-10 21:18:44 +04:00
|
|
|
delete theSpeaker;
|
2004-02-02 02:42:04 +03:00
|
|
|
}
|
|
|
|
|
2006-09-10 21:18:44 +04:00
|
|
|
bx_speaker_c::bx_speaker_c()
|
|
|
|
{
|
2011-12-30 15:13:37 +04:00
|
|
|
put("speaker", "SPEAK");
|
2004-02-02 02:42:04 +03:00
|
|
|
|
|
|
|
beep_frequency = 0.0; // Off
|
|
|
|
#ifdef __linux__
|
2012-05-24 22:06:40 +04:00
|
|
|
consolefd = -1;
|
2004-02-02 02:42:04 +03:00
|
|
|
#endif
|
2004-01-16 19:30:46 +03:00
|
|
|
}
|
|
|
|
|
2006-09-10 21:18:44 +04:00
|
|
|
bx_speaker_c::~bx_speaker_c()
|
|
|
|
{
|
2004-01-16 21:18:38 +03:00
|
|
|
#ifdef __linux__
|
2006-09-10 21:18:44 +04:00
|
|
|
if (consolefd >= 0) {
|
2004-01-16 21:18:38 +03:00
|
|
|
ioctl(consolefd, KIOCSOUND, 0);
|
|
|
|
close(consolefd);
|
2004-02-02 02:42:04 +03:00
|
|
|
}
|
2004-01-16 21:18:38 +03:00
|
|
|
#endif
|
2006-09-10 21:18:44 +04:00
|
|
|
BX_DEBUG(("Exit"));
|
2004-02-02 02:42:04 +03:00
|
|
|
}
|
|
|
|
|
2008-01-27 01:24:03 +03:00
|
|
|
void bx_speaker_c::init(void)
|
2004-02-02 02:42:04 +03:00
|
|
|
{
|
2012-05-24 22:06:40 +04:00
|
|
|
outputinit = 0;
|
2013-06-12 01:04:07 +04:00
|
|
|
#if defined(WIN32)
|
|
|
|
useBeep = FALSE;
|
|
|
|
#endif
|
2004-01-16 19:30:46 +03:00
|
|
|
}
|
|
|
|
|
2008-01-27 01:24:03 +03:00
|
|
|
void bx_speaker_c::reset(unsigned type)
|
2004-01-16 19:30:46 +03:00
|
|
|
{
|
2012-05-24 22:06:40 +04:00
|
|
|
if (!outputinit) {
|
|
|
|
outputinit = 1;
|
|
|
|
#if BX_SUPPORT_SOUNDLOW
|
|
|
|
if (DEV_soundmod_beep_off()) {
|
|
|
|
BX_INFO(("Using lowlevel sound support for output"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef __linux__
|
|
|
|
consolefd = open("/dev/console", O_WRONLY);
|
|
|
|
if (consolefd != -1) {
|
|
|
|
BX_INFO(("Using /dev/console for output"));
|
|
|
|
} else {
|
|
|
|
BX_ERROR(("Failed to open /dev/console: %s", strerror(errno)));
|
|
|
|
BX_ERROR(("Deactivating beep on console"));
|
|
|
|
}
|
|
|
|
#elif defined(WIN32)
|
|
|
|
BX_INFO(("Using system beep for output"));
|
2013-06-12 01:04:07 +04:00
|
|
|
useBeep = TRUE;
|
2012-05-24 22:06:40 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-02-02 02:42:04 +03:00
|
|
|
beep_off();
|
|
|
|
}
|
|
|
|
|
|
|
|
void bx_speaker_c::beep_on(float frequency)
|
|
|
|
{
|
2013-06-12 01:04:07 +04:00
|
|
|
#if defined(WIN32)
|
|
|
|
if (useBeep && (beep_frequency != 0.0)) {
|
|
|
|
beep_off();
|
|
|
|
}
|
|
|
|
#endif
|
2004-02-02 02:42:04 +03:00
|
|
|
beep_frequency = frequency;
|
|
|
|
|
2012-05-20 18:19:49 +04:00
|
|
|
#if BX_SUPPORT_SOUNDLOW
|
|
|
|
if (DEV_soundmod_beep_on(frequency))
|
|
|
|
return;
|
|
|
|
#endif
|
2004-01-16 21:18:38 +03:00
|
|
|
#ifdef __linux__
|
2004-02-02 02:42:04 +03:00
|
|
|
if (consolefd != -1) {
|
2004-01-16 21:18:38 +03:00
|
|
|
this->info("pc speaker on with frequency %f", frequency);
|
|
|
|
ioctl(consolefd, KIOCSOUND, (int)(clock_tick_rate/frequency));
|
|
|
|
}
|
2005-01-29 15:08:31 +03:00
|
|
|
#elif defined(WIN32)
|
2013-06-12 01:04:07 +04:00
|
|
|
if (useBeep) {
|
|
|
|
usec_start = bx_pc_system.time_usec();
|
|
|
|
}
|
2004-02-02 02:42:04 +03:00
|
|
|
#endif
|
2006-08-03 20:01:23 +04:00
|
|
|
|
2012-05-20 18:19:49 +04:00
|
|
|
// give the gui a chance to signal beep on
|
2004-02-02 02:42:04 +03:00
|
|
|
bx_gui->beep_on(frequency);
|
2004-01-16 19:30:46 +03:00
|
|
|
}
|
|
|
|
|
2005-02-09 21:25:57 +03:00
|
|
|
#if defined(WIN32)
|
|
|
|
|
2010-02-08 17:53:31 +03:00
|
|
|
static struct {
|
2005-02-09 21:25:57 +03:00
|
|
|
DWORD frequency;
|
|
|
|
DWORD msec;
|
|
|
|
} beep_info;
|
|
|
|
|
|
|
|
DWORD WINAPI BeepThread(LPVOID)
|
|
|
|
{
|
|
|
|
static BOOL threadActive = FALSE;
|
|
|
|
|
|
|
|
while (threadActive) Sleep(10);
|
|
|
|
threadActive = TRUE;
|
|
|
|
Beep(beep_info.frequency, beep_info.msec);
|
|
|
|
threadActive = FALSE;
|
2005-02-12 11:59:47 +03:00
|
|
|
return 0;
|
2005-02-09 21:25:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2004-02-02 02:42:04 +03:00
|
|
|
void bx_speaker_c::beep_off()
|
2004-01-16 19:30:46 +03:00
|
|
|
{
|
2012-05-20 18:19:49 +04:00
|
|
|
#if BX_SUPPORT_SOUNDLOW
|
|
|
|
if (DEV_soundmod_beep_off())
|
|
|
|
return;
|
|
|
|
#endif
|
2004-02-02 02:42:04 +03:00
|
|
|
if (beep_frequency != 0.0) {
|
2004-01-16 21:18:38 +03:00
|
|
|
#ifdef __linux__
|
2004-02-02 02:42:04 +03:00
|
|
|
if (consolefd != -1) {
|
|
|
|
ioctl(consolefd, KIOCSOUND, 0);
|
|
|
|
}
|
2005-01-29 15:08:31 +03:00
|
|
|
#elif defined(WIN32)
|
2005-02-09 21:25:57 +03:00
|
|
|
// FIXME: sound should start at beep_on() and end here
|
2013-06-12 01:04:07 +04:00
|
|
|
if (useBeep) {
|
|
|
|
DWORD threadID;
|
|
|
|
beep_info.msec = (DWORD)((bx_pc_system.time_usec() - usec_start) / 1000);
|
|
|
|
beep_info.frequency = (DWORD)beep_frequency;
|
|
|
|
CreateThread(NULL, 0, BeepThread, NULL, 0, &threadID);
|
|
|
|
}
|
2004-01-16 21:18:38 +03:00
|
|
|
#endif
|
2004-02-02 02:42:04 +03:00
|
|
|
|
2005-01-29 15:08:31 +03:00
|
|
|
// give the gui a chance to signal beep off
|
2004-02-02 02:42:04 +03:00
|
|
|
bx_gui->beep_off();
|
2006-08-03 20:01:23 +04:00
|
|
|
|
2004-02-02 02:42:04 +03:00
|
|
|
beep_frequency = 0.0;
|
2004-01-16 21:18:38 +03:00
|
|
|
}
|
2004-01-16 19:30:46 +03:00
|
|
|
}
|