michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* vim:expandtab:shiftwidth=4:tabstop=4: michael@0: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: michael@0: #include "nscore.h" michael@0: #include "plstr.h" michael@0: #include "prlink.h" michael@0: michael@0: #include "nsSound.h" michael@0: michael@0: #include "nsIURL.h" michael@0: #include "nsIFileURL.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsString.h" michael@0: #include "nsDirectoryService.h" michael@0: #include "nsDirectoryServiceDefs.h" michael@0: #include "mozilla/FileUtils.h" michael@0: #include "mozilla/Services.h" michael@0: #include "nsIStringBundle.h" michael@0: #include "nsIXULAppInfo.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: static PRLibrary *libcanberra = nullptr; michael@0: michael@0: /* used to play sounds with libcanberra. */ michael@0: typedef struct _ca_context ca_context; michael@0: typedef struct _ca_proplist ca_proplist; michael@0: michael@0: typedef void (*ca_finish_callback_t) (ca_context *c, michael@0: uint32_t id, michael@0: int error_code, michael@0: void *userdata); michael@0: michael@0: typedef int (*ca_context_create_fn) (ca_context **); michael@0: typedef int (*ca_context_destroy_fn) (ca_context *); michael@0: typedef int (*ca_context_play_fn) (ca_context *c, michael@0: uint32_t id, michael@0: ...); michael@0: typedef int (*ca_context_change_props_fn) (ca_context *c, michael@0: ...); michael@0: typedef int (*ca_proplist_create_fn) (ca_proplist **); michael@0: typedef int (*ca_proplist_destroy_fn) (ca_proplist *); michael@0: typedef int (*ca_proplist_sets_fn) (ca_proplist *c, michael@0: const char *key, michael@0: const char *value); michael@0: typedef int (*ca_context_play_full_fn) (ca_context *c, michael@0: uint32_t id, michael@0: ca_proplist *p, michael@0: ca_finish_callback_t cb, michael@0: void *userdata); michael@0: michael@0: static ca_context_create_fn ca_context_create; michael@0: static ca_context_destroy_fn ca_context_destroy; michael@0: static ca_context_play_fn ca_context_play; michael@0: static ca_context_change_props_fn ca_context_change_props; michael@0: static ca_proplist_create_fn ca_proplist_create; michael@0: static ca_proplist_destroy_fn ca_proplist_destroy; michael@0: static ca_proplist_sets_fn ca_proplist_sets; michael@0: static ca_context_play_full_fn ca_context_play_full; michael@0: michael@0: struct ScopedCanberraFile { michael@0: ScopedCanberraFile(nsIFile *file): mFile(file) {}; michael@0: michael@0: ~ScopedCanberraFile() { michael@0: if (mFile) { michael@0: mFile->Remove(false); michael@0: } michael@0: } michael@0: michael@0: void forget() { michael@0: mFile.forget(); michael@0: } michael@0: nsIFile* operator->() { return mFile; } michael@0: operator nsIFile*() { return mFile; } michael@0: michael@0: nsCOMPtr mFile; michael@0: }; michael@0: michael@0: static ca_context* michael@0: ca_context_get_default() michael@0: { michael@0: // This allows us to avoid race conditions with freeing the context by handing that michael@0: // responsibility to Glib, and still use one context at a time michael@0: static GStaticPrivate ctx_static_private = G_STATIC_PRIVATE_INIT; michael@0: michael@0: ca_context* ctx = (ca_context*) g_static_private_get(&ctx_static_private); michael@0: michael@0: if (ctx) { michael@0: return ctx; michael@0: } michael@0: michael@0: ca_context_create(&ctx); michael@0: if (!ctx) { michael@0: return nullptr; michael@0: } michael@0: michael@0: g_static_private_set(&ctx_static_private, ctx, (GDestroyNotify) ca_context_destroy); michael@0: michael@0: GtkSettings* settings = gtk_settings_get_default(); michael@0: if (g_object_class_find_property(G_OBJECT_GET_CLASS(settings), michael@0: "gtk-sound-theme-name")) { michael@0: gchar* sound_theme_name = nullptr; michael@0: g_object_get(settings, "gtk-sound-theme-name", &sound_theme_name, michael@0: nullptr); michael@0: michael@0: if (sound_theme_name) { michael@0: ca_context_change_props(ctx, "canberra.xdg-theme.name", michael@0: sound_theme_name, nullptr); michael@0: g_free(sound_theme_name); michael@0: } michael@0: } michael@0: michael@0: nsCOMPtr bundleService = michael@0: mozilla::services::GetStringBundleService(); michael@0: if (bundleService) { michael@0: nsCOMPtr brandingBundle; michael@0: bundleService->CreateBundle("chrome://branding/locale/brand.properties", michael@0: getter_AddRefs(brandingBundle)); michael@0: if (brandingBundle) { michael@0: nsAutoString wbrand; michael@0: brandingBundle->GetStringFromName(MOZ_UTF16("brandShortName"), michael@0: getter_Copies(wbrand)); michael@0: NS_ConvertUTF16toUTF8 brand(wbrand); michael@0: michael@0: ca_context_change_props(ctx, "application.name", brand.get(), michael@0: nullptr); michael@0: } michael@0: } michael@0: michael@0: nsCOMPtr appInfo = do_GetService("@mozilla.org/xre/app-info;1"); michael@0: if (appInfo) { michael@0: nsAutoCString version; michael@0: appInfo->GetVersion(version); michael@0: michael@0: ca_context_change_props(ctx, "application.version", version.get(), michael@0: nullptr); michael@0: } michael@0: michael@0: ca_context_change_props(ctx, "application.icon_name", MOZ_APP_NAME, michael@0: nullptr); michael@0: michael@0: return ctx; michael@0: } michael@0: michael@0: static void michael@0: ca_finish_cb(ca_context *c, michael@0: uint32_t id, michael@0: int error_code, michael@0: void *userdata) michael@0: { michael@0: nsIFile *file = reinterpret_cast(userdata); michael@0: if (file) { michael@0: file->Remove(false); michael@0: NS_RELEASE(file); michael@0: } michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsSound, nsISound, nsIStreamLoaderObserver) michael@0: michael@0: //////////////////////////////////////////////////////////////////////// michael@0: nsSound::nsSound() michael@0: { michael@0: mInited = false; michael@0: } michael@0: michael@0: nsSound::~nsSound() michael@0: { michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSound::Init() michael@0: { michael@0: // This function is designed so that no library is compulsory, and michael@0: // one library missing doesn't cause the other(s) to not be used. michael@0: if (mInited) michael@0: return NS_OK; michael@0: michael@0: mInited = true; michael@0: michael@0: if (!libcanberra) { michael@0: libcanberra = PR_LoadLibrary("libcanberra.so.0"); michael@0: if (libcanberra) { michael@0: ca_context_create = (ca_context_create_fn) PR_FindFunctionSymbol(libcanberra, "ca_context_create"); michael@0: if (!ca_context_create) { michael@0: PR_UnloadLibrary(libcanberra); michael@0: libcanberra = nullptr; michael@0: } else { michael@0: // at this point we know we have a good libcanberra library michael@0: ca_context_destroy = (ca_context_destroy_fn) PR_FindFunctionSymbol(libcanberra, "ca_context_destroy"); michael@0: ca_context_play = (ca_context_play_fn) PR_FindFunctionSymbol(libcanberra, "ca_context_play"); michael@0: ca_context_change_props = (ca_context_change_props_fn) PR_FindFunctionSymbol(libcanberra, "ca_context_change_props"); michael@0: ca_proplist_create = (ca_proplist_create_fn) PR_FindFunctionSymbol(libcanberra, "ca_proplist_create"); michael@0: ca_proplist_destroy = (ca_proplist_destroy_fn) PR_FindFunctionSymbol(libcanberra, "ca_proplist_destroy"); michael@0: ca_proplist_sets = (ca_proplist_sets_fn) PR_FindFunctionSymbol(libcanberra, "ca_proplist_sets"); michael@0: ca_context_play_full = (ca_context_play_full_fn) PR_FindFunctionSymbol(libcanberra, "ca_context_play_full"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* static */ void michael@0: nsSound::Shutdown() michael@0: { michael@0: if (libcanberra) { michael@0: PR_UnloadLibrary(libcanberra); michael@0: libcanberra = nullptr; michael@0: } michael@0: } michael@0: michael@0: NS_IMETHODIMP nsSound::OnStreamComplete(nsIStreamLoader *aLoader, michael@0: nsISupports *context, michael@0: nsresult aStatus, michael@0: uint32_t dataLen, michael@0: const uint8_t *data) michael@0: { michael@0: // print a load error on bad status, and return michael@0: if (NS_FAILED(aStatus)) { michael@0: #ifdef DEBUG michael@0: if (aLoader) { michael@0: nsCOMPtr request; michael@0: aLoader->GetRequest(getter_AddRefs(request)); michael@0: if (request) { michael@0: nsCOMPtr uri; michael@0: nsCOMPtr channel = do_QueryInterface(request); michael@0: if (channel) { michael@0: channel->GetURI(getter_AddRefs(uri)); michael@0: if (uri) { michael@0: nsAutoCString uriSpec; michael@0: uri->GetSpec(uriSpec); michael@0: printf("Failed to load %s\n", uriSpec.get()); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: return aStatus; michael@0: } michael@0: michael@0: nsCOMPtr tmpFile; michael@0: nsDirectoryService::gService->Get(NS_OS_TEMP_DIR, NS_GET_IID(nsIFile), michael@0: getter_AddRefs(tmpFile)); michael@0: michael@0: nsresult rv = tmpFile->AppendNative(nsDependentCString("mozilla_audio_sample")); michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: michael@0: rv = tmpFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, PR_IRUSR | PR_IWUSR); michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: michael@0: ScopedCanberraFile canberraFile(tmpFile); michael@0: michael@0: mozilla::AutoFDClose fd; michael@0: rv = canberraFile->OpenNSPRFileDesc(PR_WRONLY, PR_IRUSR | PR_IWUSR, michael@0: &fd.rwget()); michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: michael@0: // XXX: Should we do this on another thread? michael@0: uint32_t length = dataLen; michael@0: while (length > 0) { michael@0: int32_t amount = PR_Write(fd, data, length); michael@0: if (amount < 0) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: length -= amount; michael@0: data += amount; michael@0: } michael@0: michael@0: ca_context* ctx = ca_context_get_default(); michael@0: if (!ctx) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: ca_proplist *p; michael@0: ca_proplist_create(&p); michael@0: if (!p) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: nsAutoCString path; michael@0: rv = canberraFile->GetNativePath(path); michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: michael@0: ca_proplist_sets(p, "media.filename", path.get()); michael@0: if (ca_context_play_full(ctx, 0, p, ca_finish_cb, canberraFile) >= 0) { michael@0: // Don't delete the temporary file here if ca_context_play_full succeeds michael@0: canberraFile.forget(); michael@0: } michael@0: ca_proplist_destroy(p); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_METHOD nsSound::Beep() michael@0: { michael@0: ::gdk_beep(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_METHOD nsSound::Play(nsIURL *aURL) michael@0: { michael@0: if (!mInited) michael@0: Init(); michael@0: michael@0: if (!libcanberra) michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: michael@0: bool isFile; michael@0: nsresult rv = aURL->SchemeIs("file", &isFile); michael@0: if (NS_SUCCEEDED(rv) && isFile) { michael@0: ca_context* ctx = ca_context_get_default(); michael@0: if (!ctx) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: nsAutoCString spec; michael@0: rv = aURL->GetSpec(spec); michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: gchar *path = g_filename_from_uri(spec.get(), nullptr, nullptr); michael@0: if (!path) { michael@0: return NS_ERROR_FILE_UNRECOGNIZED_PATH; michael@0: } michael@0: michael@0: ca_context_play(ctx, 0, "media.filename", path, nullptr); michael@0: g_free(path); michael@0: } else { michael@0: nsCOMPtr loader; michael@0: rv = NS_NewStreamLoader(getter_AddRefs(loader), aURL, this); michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsSound::PlayEventSound(uint32_t aEventId) michael@0: { michael@0: if (!mInited) michael@0: Init(); michael@0: michael@0: if (!libcanberra) michael@0: return NS_OK; michael@0: michael@0: // Do we even want alert sounds? michael@0: GtkSettings* settings = gtk_settings_get_default(); michael@0: michael@0: if (g_object_class_find_property(G_OBJECT_GET_CLASS(settings), michael@0: "gtk-enable-event-sounds")) { michael@0: gboolean enable_sounds = TRUE; michael@0: g_object_get(settings, "gtk-enable-event-sounds", &enable_sounds, nullptr); michael@0: michael@0: if (!enable_sounds) { michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: ca_context* ctx = ca_context_get_default(); michael@0: if (!ctx) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: switch (aEventId) { michael@0: case EVENT_ALERT_DIALOG_OPEN: michael@0: ca_context_play(ctx, 0, "event.id", "dialog-warning", nullptr); michael@0: break; michael@0: case EVENT_CONFIRM_DIALOG_OPEN: michael@0: ca_context_play(ctx, 0, "event.id", "dialog-question", nullptr); michael@0: break; michael@0: case EVENT_NEW_MAIL_RECEIVED: michael@0: ca_context_play(ctx, 0, "event.id", "message-new-email", nullptr); michael@0: break; michael@0: case EVENT_MENU_EXECUTE: michael@0: ca_context_play(ctx, 0, "event.id", "menu-click", nullptr); michael@0: break; michael@0: case EVENT_MENU_POPUP: michael@0: ca_context_play(ctx, 0, "event.id", "menu-popup", nullptr); michael@0: break; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsSound::PlaySystemSound(const nsAString &aSoundAlias) michael@0: { michael@0: if (NS_IsMozAliasSound(aSoundAlias)) { michael@0: NS_WARNING("nsISound::playSystemSound is called with \"_moz_\" events, they are obsolete, use nsISound::playEventSound instead"); michael@0: uint32_t eventId; michael@0: if (aSoundAlias.Equals(NS_SYSSOUND_ALERT_DIALOG)) michael@0: eventId = EVENT_ALERT_DIALOG_OPEN; michael@0: else if (aSoundAlias.Equals(NS_SYSSOUND_CONFIRM_DIALOG)) michael@0: eventId = EVENT_CONFIRM_DIALOG_OPEN; michael@0: else if (aSoundAlias.Equals(NS_SYSSOUND_MAIL_BEEP)) michael@0: eventId = EVENT_NEW_MAIL_RECEIVED; michael@0: else if (aSoundAlias.Equals(NS_SYSSOUND_MENU_EXECUTE)) michael@0: eventId = EVENT_MENU_EXECUTE; michael@0: else if (aSoundAlias.Equals(NS_SYSSOUND_MENU_POPUP)) michael@0: eventId = EVENT_MENU_POPUP; michael@0: else michael@0: return NS_OK; michael@0: return PlayEventSound(eventId); michael@0: } michael@0: michael@0: nsresult rv; michael@0: nsCOMPtr fileURI; michael@0: michael@0: // create a nsIFile and then a nsIFileURL from that michael@0: nsCOMPtr soundFile; michael@0: rv = NS_NewLocalFile(aSoundAlias, true, michael@0: getter_AddRefs(soundFile)); michael@0: NS_ENSURE_SUCCESS(rv,rv); michael@0: michael@0: rv = NS_NewFileURI(getter_AddRefs(fileURI), soundFile); michael@0: NS_ENSURE_SUCCESS(rv,rv); michael@0: michael@0: nsCOMPtr fileURL = do_QueryInterface(fileURI,&rv); michael@0: NS_ENSURE_SUCCESS(rv,rv); michael@0: michael@0: rv = Play(fileURL); michael@0: michael@0: return rv; michael@0: }