2000-08-07 20:47:05 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
1999-06-28 23:39:35 +00:00
|
|
|
*
|
2000-08-07 20:47:05 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 2000 Netscape Communications Corporation.
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Stuart Parmenter <pavlov@netscape.com>
|
|
|
|
* John Fairhurst <john_fairhurst@iname.com>
|
|
|
|
* IBM Corp.
|
1999-06-28 23:39:35 +00:00
|
|
|
*/
|
|
|
|
|
2000-08-07 20:47:05 +00:00
|
|
|
#include "nscore.h"
|
|
|
|
#include "plstr.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <os2.h>
|
|
|
|
|
1999-06-28 23:39:35 +00:00
|
|
|
#include "nsSound.h"
|
2000-08-07 20:47:05 +00:00
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsNetUtil.h"
|
2001-12-11 20:48:24 +00:00
|
|
|
#include "nsIPref.h"
|
1999-06-28 23:39:35 +00:00
|
|
|
|
2002-04-03 15:00:50 +00:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
|
2000-08-07 20:47:05 +00:00
|
|
|
NS_IMPL_ISUPPORTS2(nsSound, nsISound, nsIStreamLoaderObserver)
|
1999-06-28 23:39:35 +00:00
|
|
|
|
2000-08-07 20:47:05 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
1999-06-28 23:39:35 +00:00
|
|
|
nsSound::nsSound()
|
2000-08-07 20:47:05 +00:00
|
|
|
{
|
2002-09-07 17:13:19 +00:00
|
|
|
NS_INIT_ISUPPORTS();
|
2000-08-07 20:47:05 +00:00
|
|
|
}
|
1999-06-28 23:39:35 +00:00
|
|
|
|
|
|
|
nsSound::~nsSound()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2000-08-07 20:47:05 +00:00
|
|
|
NS_IMETHODIMP nsSound::OnStreamComplete(nsIStreamLoader *aLoader,
|
|
|
|
nsISupports *context,
|
|
|
|
nsresult aStatus,
|
|
|
|
PRUint32 stringLen,
|
|
|
|
const char *string)
|
1999-06-28 23:39:35 +00:00
|
|
|
{
|
2002-04-03 15:00:50 +00:00
|
|
|
|
2000-08-07 20:47:05 +00:00
|
|
|
if (NS_FAILED(aStatus)) {
|
2002-04-03 15:00:50 +00:00
|
|
|
#ifdef DEBUG
|
2000-08-07 20:47:05 +00:00
|
|
|
if (aLoader) {
|
2001-02-21 20:38:08 +00:00
|
|
|
nsCOMPtr<nsIRequest> request;
|
|
|
|
aLoader->GetRequest(getter_AddRefs(request));
|
|
|
|
if (request) {
|
2000-08-07 20:47:05 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2001-02-21 20:38:08 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
|
|
|
|
if (channel) {
|
|
|
|
channel->GetURI(getter_AddRefs(uri));
|
|
|
|
if (uri) {
|
2002-03-06 15:56:23 +00:00
|
|
|
nsCAutoString uriSpec;
|
|
|
|
uri->GetSpec(uriSpec);
|
|
|
|
printf("Failed to load %s\n", uriSpec.get());
|
2001-02-21 20:38:08 +00:00
|
|
|
}
|
2000-08-07 20:47:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-04-03 15:00:50 +00:00
|
|
|
#endif
|
|
|
|
return NS_ERROR_FAILURE;
|
2000-08-07 20:47:05 +00:00
|
|
|
}
|
|
|
|
|
2002-04-03 15:00:50 +00:00
|
|
|
if (PL_strncmp(string, "RIFF", 4)) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("We only support WAV files currently.\n");
|
|
|
|
#endif
|
|
|
|
return NS_ERROR_FAILURE;
|
2000-08-07 20:47:05 +00:00
|
|
|
}
|
1999-06-28 23:39:35 +00:00
|
|
|
|
2002-04-03 15:00:50 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
static const char *kSoundTmpFileName = "mozsound.wav";
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> soundTmp;
|
|
|
|
rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(soundTmp));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-05-08 02:16:43 +00:00
|
|
|
rv = soundTmp->AppendNative(nsDependentCString(kSoundTmpFileName));
|
2002-04-27 16:16:23 +00:00
|
|
|
nsCAutoString soundFilename;
|
|
|
|
(void) soundTmp->GetNativePath(soundFilename);
|
|
|
|
FILE *fp = fopen(soundFilename.get(), "wb+");
|
2002-04-03 15:00:50 +00:00
|
|
|
fwrite(string, stringLen, 1, fp);
|
|
|
|
fclose(fp);
|
2002-04-27 16:16:23 +00:00
|
|
|
HOBJECT hobject = WinQueryObject(soundFilename.get());
|
2002-04-03 15:00:50 +00:00
|
|
|
WinSetObjectData(hobject, "OPEN=DEFAULT");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
#ifdef OLDCODE /* Some day we could try to get this working */
|
|
|
|
ULONG ulRC;
|
|
|
|
CHAR errorBuffer[128];
|
|
|
|
|
|
|
|
HMMIO hmmio;
|
|
|
|
MMIOINFO mmioinfo;
|
|
|
|
|
|
|
|
memset(&mmioinfo, 0, sizeof(MMIOINFO));
|
|
|
|
mmioinfo.fccIOProc = FOURCC_MEM;
|
|
|
|
mmioinfo.cchBuffer = stringLen;
|
|
|
|
mmioinfo.pchBuffer = (char*)string;
|
|
|
|
USHORT usDeviceID;
|
|
|
|
|
|
|
|
hmmio = mmioOpen(NULL, &mmioinfo, MMIO_READWRITE);
|
|
|
|
|
|
|
|
MCI_OPEN_PARMS mop;
|
|
|
|
memset(&mop, 0, sizeof(MCI_OPEN_PARMS));
|
|
|
|
|
|
|
|
mop.pszElementName = (char*)hmmio;
|
|
|
|
CHAR DeviceType[] = "waveaudio";
|
|
|
|
mop.pszDeviceType = (PSZ)&DeviceType;
|
|
|
|
|
|
|
|
ulRC = mciSendCommand(0, MCI_OPEN, MCI_OPEN_MMIO | MCI_WAIT, &mop, 0);
|
|
|
|
|
|
|
|
if (ulRC != MCIERR_SUCCESS) {
|
|
|
|
ulRC = mciGetErrorString(ulRC, errorBuffer, 128);
|
|
|
|
}
|
|
|
|
|
|
|
|
usDeviceID = mop.usDeviceID;
|
|
|
|
|
|
|
|
MCI_OPEN_PARMS mpp;
|
|
|
|
|
|
|
|
memset(&mpp, 0, sizeof(MCI_OPEN_PARMS));
|
|
|
|
ulRC = mciSendCommand(usDeviceID, MCI_PLAY, MCI_WAIT, &mpp, 0);
|
|
|
|
|
|
|
|
if (ulRC != MCIERR_SUCCESS) {
|
|
|
|
ulRC = mciGetErrorString(ulRC, errorBuffer, 128);
|
|
|
|
}
|
|
|
|
|
|
|
|
MCI_GENERIC_PARMS mgp;
|
|
|
|
memset(&mgp, 0, sizeof(MCI_GENERIC_PARMS));
|
|
|
|
ulRC = mciSendCommand(usDeviceID, MCI_CLOSE, MCI_WAIT, &mgp, 0);
|
|
|
|
|
|
|
|
if (ulRC != MCIERR_SUCCESS) {
|
|
|
|
ulRC = mciGetErrorString(ulRC, errorBuffer, 128);
|
|
|
|
}
|
|
|
|
|
|
|
|
mmioClose(hmmio, 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD nsSound::Beep()
|
|
|
|
{
|
|
|
|
WinAlarm(HWND_DESKTOP, WA_WARNING);
|
|
|
|
|
2000-08-07 20:47:05 +00:00
|
|
|
return NS_OK;
|
1999-06-28 23:39:35 +00:00
|
|
|
}
|
2000-08-07 20:47:05 +00:00
|
|
|
|
|
|
|
NS_METHOD nsSound::Play(nsIURL *aURL)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamLoader> loader;
|
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(loader), aURL, this);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-12-14 00:27:33 +00:00
|
|
|
NS_IMETHODIMP nsSound::Init()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-10-10 08:08:22 +00:00
|
|
|
NS_IMETHODIMP nsSound::PlaySystemSound(const char *aSoundAlias)
|
|
|
|
{
|
2001-12-11 20:48:24 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsCAutoString prefName("system.sound.");
|
|
|
|
prefName += aSoundAlias;
|
|
|
|
|
|
|
|
nsXPIDLCString soundPrefValue;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPref> prefs(do_GetService(NS_PREF_CONTRACTID, &rv));
|
|
|
|
if (NS_SUCCEEDED(rv) && prefs)
|
|
|
|
rv = prefs->CopyCharPref(prefName.get(), getter_Copies(soundPrefValue));
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && soundPrefValue.Length() > 0) {
|
|
|
|
nsCOMPtr<nsIURI> soundURI;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(soundURI), soundPrefValue);
|
|
|
|
nsCOMPtr<nsIURL> soundURL = do_QueryInterface(soundURI);
|
2002-04-03 15:00:50 +00:00
|
|
|
rv = Play(soundURL);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return NS_OK;
|
2001-12-11 20:48:24 +00:00
|
|
|
}
|
2002-04-03 15:00:50 +00:00
|
|
|
Beep();
|
|
|
|
return NS_OK;
|
2001-10-10 08:08:22 +00:00
|
|
|
}
|
|
|
|
|