Back to index

lightning-sunbird  0.9+nobinonly
nsSound.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 2000
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Stuart Parmenter <pavlov@netscape.com>
00025  *   John Fairhurst <john_fairhurst@iname.com>
00026  *   IBM Corp.
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either the GNU General Public License Version 2 or later (the "GPL"), or
00030  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 
00042 #include "nscore.h"
00043 #include "plstr.h"
00044 #include <stdio.h>
00045 #include <string.h>
00046 
00047 #define INCL_DOS
00048 #define INCL_DOSERRORS
00049 #include <os2.h>
00050 
00051 #include "nsSound.h"
00052 #include "nsIURL.h"
00053 #include "nsNetUtil.h"
00054 #include "nsIPref.h"
00055 
00056 #include "nsDirectoryServiceDefs.h"
00057 
00058 NS_IMPL_ISUPPORTS2(nsSound, nsISound, nsIStreamLoaderObserver)
00059 
00060 static int                gInitialized = 0;
00061 static PRBool             gMMPMInstalled = PR_FALSE;
00062 
00063 static void
00064 InitGlobals(void)
00065 {
00066   APIRET ulrc;
00067   HMODULE hmod;
00068   char LoadError[CCHMAXPATH];
00069   ulrc = DosLoadModule(LoadError, CCHMAXPATH, "MMPM", &hmod);
00070   if (ulrc == NO_ERROR) {
00071     gMMPMInstalled = PR_TRUE;
00072   }
00073   DosFreeModule(hmod);
00074   gInitialized = 1;
00075 }
00076 
00077 
00079 nsSound::nsSound()
00080 {
00081   if (!gInitialized) {
00082     InitGlobals();
00083   }
00084 }
00085 
00086 nsSound::~nsSound()
00087 {
00088 }
00089 
00090 NS_IMETHODIMP nsSound::OnStreamComplete(nsIStreamLoader *aLoader,
00091                                         nsISupports *context,
00092                                         nsresult aStatus,
00093                                         PRUint32 dataLen,
00094                                         const PRUint8 *data)
00095 {
00096 
00097   if (NS_FAILED(aStatus)) {
00098 #ifdef DEBUG
00099     if (aLoader) {
00100       nsCOMPtr<nsIRequest> request;
00101       aLoader->GetRequest(getter_AddRefs(request));
00102       if (request) {
00103         nsCOMPtr<nsIURI> uri;
00104         nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
00105         if (channel) {
00106             channel->GetURI(getter_AddRefs(uri));
00107             if (uri) {
00108                 nsCAutoString uriSpec;
00109                 uri->GetSpec(uriSpec);
00110                 printf("Failed to load %s\n", uriSpec.get());
00111             }
00112         }
00113       }
00114     }
00115 #endif
00116     return NS_ERROR_FAILURE;
00117   }
00118 
00119   if (memcmp(data, "RIFF", 4) || (!gMMPMInstalled)) {
00120     NS_WARNING("We only support WAV files currently.\n");
00121     Beep();
00122     return NS_OK;
00123   }
00124 
00125   nsresult rv;
00126     
00127   static const char *kSoundTmpFileName = "mozsound.wav";
00128 
00129   nsCOMPtr<nsIFile> soundTmp;
00130   rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(soundTmp));
00131   if (NS_FAILED(rv)) return rv;
00132   rv = soundTmp->AppendNative(nsDependentCString(kSoundTmpFileName));
00133   nsCAutoString soundFilename;
00134   (void) soundTmp->GetNativePath(soundFilename);
00135   FILE *fp = fopen(soundFilename.get(), "wb+");
00136   if (fp) {
00137     fwrite(data, dataLen, 1, fp);
00138     fclose(fp);
00139     HOBJECT hobject = WinQueryObject(soundFilename.get());
00140     WinSetObjectData(hobject, "OPEN=DEFAULT");
00141   } else {
00142     NS_WARNING("Could not open WAV file for binary writing.\n");
00143     Beep();
00144   }
00145 
00146   return NS_OK;
00147 
00148 #ifdef OLDCODE /* Some day we could try to get this working */
00149   ULONG ulRC;
00150   CHAR errorBuffer[128];
00151 
00152   HMMIO hmmio;
00153   MMIOINFO mmioinfo;
00154 
00155   memset(&mmioinfo, 0, sizeof(MMIOINFO));
00156   mmioinfo.fccIOProc = FOURCC_MEM;
00157   mmioinfo.cchBuffer = dataLen;
00158   mmioinfo.pchBuffer = (char*)data;
00159   USHORT usDeviceID;
00160 
00161   hmmio = mmioOpen(NULL, &mmioinfo, MMIO_READWRITE);
00162 
00163   MCI_OPEN_PARMS mop;
00164   memset(&mop, 0, sizeof(MCI_OPEN_PARMS));
00165 
00166   mop.pszElementName = (char*)hmmio;
00167   CHAR DeviceType[] = "waveaudio";
00168   mop.pszDeviceType = (PSZ)&DeviceType;
00169 
00170   ulRC = mciSendCommand(0, MCI_OPEN, MCI_OPEN_MMIO | MCI_WAIT, &mop, 0);
00171 
00172   if (ulRC != MCIERR_SUCCESS) {
00173      ulRC = mciGetErrorString(ulRC, errorBuffer, 128);
00174   }
00175 
00176   usDeviceID = mop.usDeviceID;
00177 
00178   MCI_OPEN_PARMS mpp;
00179 
00180   memset(&mpp, 0, sizeof(MCI_OPEN_PARMS));
00181   ulRC = mciSendCommand(usDeviceID, MCI_PLAY, MCI_WAIT, &mpp, 0);
00182 
00183   if (ulRC != MCIERR_SUCCESS) {
00184      ulRC = mciGetErrorString(ulRC, errorBuffer, 128);
00185   }
00186 
00187   MCI_GENERIC_PARMS mgp;
00188   memset(&mgp, 0, sizeof(MCI_GENERIC_PARMS));
00189   ulRC = mciSendCommand(usDeviceID, MCI_CLOSE, MCI_WAIT, &mgp, 0);
00190 
00191   if (ulRC != MCIERR_SUCCESS) {
00192      ulRC = mciGetErrorString(ulRC, errorBuffer, 128);
00193   }
00194 
00195   mmioClose(hmmio, 0);
00196 #endif
00197 }
00198 
00199 NS_METHOD nsSound::Beep()
00200 {
00201   WinAlarm(HWND_DESKTOP, WA_WARNING);
00202 
00203   return NS_OK;
00204 }
00205 
00206 NS_METHOD nsSound::Play(nsIURL *aURL)
00207 {
00208   nsresult rv;
00209 
00210   nsCOMPtr<nsIStreamLoader> loader;
00211   rv = NS_NewStreamLoader(getter_AddRefs(loader), aURL, this);
00212 
00213   return rv;
00214 }
00215 
00216 NS_IMETHODIMP nsSound::Init()
00217 {
00218   return NS_OK;
00219 }
00220 
00221 NS_IMETHODIMP nsSound::PlaySystemSound(const char *aSoundAlias)
00222 {
00223   /* We don't have a default mail sound on OS/2, so just beep */
00224   /* Also just beep if MMPM isn't installed */
00225   if ((strcmp("_moz_mailbeep", aSoundAlias) == 0) || (!gMMPMInstalled)) {
00226     Beep();
00227   }
00228   else {
00229     HOBJECT hobject = WinQueryObject(aSoundAlias);
00230     if (hobject)
00231       WinSetObjectData(hobject, "OPEN=DEFAULT");
00232     else 
00233       Beep();
00234   }
00235   return NS_OK;
00236 }
00237