michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ 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 "Platform.h" michael@0: michael@0: #include "nsIAccessibleEvent.h" michael@0: #include "nsIGConfService.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsMai.h" michael@0: #include "AtkSocketAccessible.h" michael@0: #include "prenv.h" michael@0: #include "prlink.h" michael@0: michael@0: #ifdef MOZ_ENABLE_DBUS michael@0: #include michael@0: #endif michael@0: #include michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::a11y; michael@0: michael@0: int atkMajorVersion = 1, atkMinorVersion = 12; michael@0: michael@0: extern "C" { michael@0: typedef GType (* AtkGetTypeType) (void); michael@0: typedef void (*GnomeAccessibilityInit) (void); michael@0: typedef void (*GnomeAccessibilityShutdown) (void); michael@0: } michael@0: michael@0: static PRLibrary* sATKLib = nullptr; michael@0: static const char sATKLibName[] = "libatk-1.0.so.0"; michael@0: static const char sATKHyperlinkImplGetTypeSymbol[] = michael@0: "atk_hyperlink_impl_get_type"; michael@0: michael@0: gboolean toplevel_event_watcher(GSignalInvocationHint*, guint, const GValue*, michael@0: gpointer); michael@0: static bool sToplevel_event_hook_added = false; michael@0: static gulong sToplevel_show_hook = 0; michael@0: static gulong sToplevel_hide_hook = 0; michael@0: michael@0: GType g_atk_hyperlink_impl_type = G_TYPE_INVALID; michael@0: michael@0: struct GnomeAccessibilityModule michael@0: { michael@0: const char *libName; michael@0: PRLibrary *lib; michael@0: const char *initName; michael@0: GnomeAccessibilityInit init; michael@0: const char *shutdownName; michael@0: GnomeAccessibilityShutdown shutdown; michael@0: }; michael@0: michael@0: static GnomeAccessibilityModule sAtkBridge = { michael@0: #ifdef AIX michael@0: "libatk-bridge.a(libatk-bridge.so.0)", nullptr, michael@0: #else michael@0: "libatk-bridge.so", nullptr, michael@0: #endif michael@0: "gnome_accessibility_module_init", nullptr, michael@0: "gnome_accessibility_module_shutdown", nullptr michael@0: }; michael@0: michael@0: static GnomeAccessibilityModule sGail = { michael@0: "libgail.so", nullptr, michael@0: "gnome_accessibility_module_init", nullptr, michael@0: "gnome_accessibility_module_shutdown", nullptr michael@0: }; michael@0: michael@0: static nsresult michael@0: LoadGtkModule(GnomeAccessibilityModule& aModule) michael@0: { michael@0: NS_ENSURE_ARG(aModule.libName); michael@0: michael@0: if (!(aModule.lib = PR_LoadLibrary(aModule.libName))) { michael@0: //try to load the module with "gtk-2.0/modules" appended michael@0: char *curLibPath = PR_GetLibraryPath(); michael@0: nsAutoCString libPath(curLibPath); michael@0: #if defined(LINUX) && defined(__x86_64__) michael@0: libPath.Append(":/usr/lib64:/usr/lib"); michael@0: #else michael@0: libPath.Append(":/usr/lib"); michael@0: #endif michael@0: PR_FreeLibraryName(curLibPath); michael@0: michael@0: int16_t loc1 = 0, loc2 = 0; michael@0: int16_t subLen = 0; michael@0: while (loc2 >= 0) { michael@0: loc2 = libPath.FindChar(':', loc1); michael@0: if (loc2 < 0) michael@0: subLen = libPath.Length() - loc1; michael@0: else michael@0: subLen = loc2 - loc1; michael@0: nsAutoCString sub(Substring(libPath, loc1, subLen)); michael@0: sub.Append("/gtk-2.0/modules/"); michael@0: sub.Append(aModule.libName); michael@0: aModule.lib = PR_LoadLibrary(sub.get()); michael@0: if (aModule.lib) michael@0: break; michael@0: michael@0: loc1 = loc2+1; michael@0: } michael@0: if (!aModule.lib) michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: //we have loaded the library, try to get the function ptrs michael@0: if (!(aModule.init = PR_FindFunctionSymbol(aModule.lib, michael@0: aModule.initName)) || michael@0: !(aModule.shutdown = PR_FindFunctionSymbol(aModule.lib, michael@0: aModule.shutdownName))) { michael@0: michael@0: //fail, :( michael@0: PR_UnloadLibrary(aModule.lib); michael@0: aModule.lib = nullptr; michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: a11y::PlatformInit() michael@0: { michael@0: if (!ShouldA11yBeEnabled()) michael@0: return; michael@0: michael@0: sATKLib = PR_LoadLibrary(sATKLibName); michael@0: if (!sATKLib) michael@0: return; michael@0: michael@0: AtkGetTypeType pfn_atk_hyperlink_impl_get_type = michael@0: (AtkGetTypeType) PR_FindFunctionSymbol(sATKLib, sATKHyperlinkImplGetTypeSymbol); michael@0: if (pfn_atk_hyperlink_impl_get_type) michael@0: g_atk_hyperlink_impl_type = pfn_atk_hyperlink_impl_get_type(); michael@0: michael@0: AtkGetTypeType pfn_atk_socket_get_type = (AtkGetTypeType) michael@0: PR_FindFunctionSymbol(sATKLib, AtkSocketAccessible::sATKSocketGetTypeSymbol); michael@0: if (pfn_atk_socket_get_type) { michael@0: AtkSocketAccessible::g_atk_socket_type = pfn_atk_socket_get_type(); michael@0: AtkSocketAccessible::g_atk_socket_embed = (AtkSocketEmbedType) michael@0: PR_FindFunctionSymbol(sATKLib, AtkSocketAccessible ::sATKSocketEmbedSymbol); michael@0: AtkSocketAccessible::gCanEmbed = michael@0: AtkSocketAccessible::g_atk_socket_type != G_TYPE_INVALID && michael@0: AtkSocketAccessible::g_atk_socket_embed; michael@0: } michael@0: michael@0: const char* (*atkGetVersion)() = michael@0: (const char* (*)()) PR_FindFunctionSymbol(sATKLib, "atk_get_version"); michael@0: if (atkGetVersion) { michael@0: const char* version = atkGetVersion(); michael@0: if (version) { michael@0: char* endPtr = nullptr; michael@0: atkMajorVersion = strtol(version, &endPtr, 10); michael@0: if (*endPtr == '.') michael@0: atkMinorVersion = strtol(endPtr + 1, &endPtr, 10); michael@0: } michael@0: } michael@0: michael@0: // Load and initialize gail library. michael@0: nsresult rv = LoadGtkModule(sGail); michael@0: if (NS_SUCCEEDED(rv)) michael@0: (*sGail.init)(); michael@0: michael@0: // Initialize the MAI Utility class, it will overwrite gail_util. michael@0: g_type_class_unref(g_type_class_ref(mai_util_get_type())); michael@0: michael@0: // Init atk-bridge now michael@0: PR_SetEnv("NO_AT_BRIDGE=0"); michael@0: rv = LoadGtkModule(sAtkBridge); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: (*sAtkBridge.init)(); michael@0: } michael@0: michael@0: if (!sToplevel_event_hook_added) { michael@0: sToplevel_event_hook_added = true; michael@0: sToplevel_show_hook = michael@0: g_signal_add_emission_hook(g_signal_lookup("show", GTK_TYPE_WINDOW), michael@0: 0, toplevel_event_watcher, michael@0: reinterpret_cast(nsIAccessibleEvent::EVENT_SHOW), michael@0: nullptr); michael@0: sToplevel_hide_hook = michael@0: g_signal_add_emission_hook(g_signal_lookup("hide", GTK_TYPE_WINDOW), 0, michael@0: toplevel_event_watcher, michael@0: reinterpret_cast(nsIAccessibleEvent::EVENT_HIDE), michael@0: nullptr); michael@0: } michael@0: } michael@0: michael@0: void michael@0: a11y::PlatformShutdown() michael@0: { michael@0: if (sToplevel_event_hook_added) { michael@0: sToplevel_event_hook_added = false; michael@0: g_signal_remove_emission_hook(g_signal_lookup("show", GTK_TYPE_WINDOW), michael@0: sToplevel_show_hook); michael@0: g_signal_remove_emission_hook(g_signal_lookup("hide", GTK_TYPE_WINDOW), michael@0: sToplevel_hide_hook); michael@0: } michael@0: michael@0: if (sAtkBridge.lib) { michael@0: // Do not shutdown/unload atk-bridge, michael@0: // an exit function registered will take care of it michael@0: // if (sAtkBridge.shutdown) michael@0: // (*sAtkBridge.shutdown)(); michael@0: // PR_UnloadLibrary(sAtkBridge.lib); michael@0: sAtkBridge.lib = nullptr; michael@0: sAtkBridge.init = nullptr; michael@0: sAtkBridge.shutdown = nullptr; michael@0: } michael@0: if (sGail.lib) { michael@0: // Do not shutdown gail because michael@0: // 1) Maybe it's not init-ed by us. e.g. GtkEmbed michael@0: // 2) We need it to avoid assert in spi_atk_tidy_windows michael@0: // if (sGail.shutdown) michael@0: // (*sGail.shutdown)(); michael@0: // PR_UnloadLibrary(sGail.lib); michael@0: sGail.lib = nullptr; michael@0: sGail.init = nullptr; michael@0: sGail.shutdown = nullptr; michael@0: } michael@0: // if (sATKLib) { michael@0: // PR_UnloadLibrary(sATKLib); michael@0: // sATKLib = nullptr; michael@0: // } michael@0: } michael@0: michael@0: static const char sAccEnv [] = "GNOME_ACCESSIBILITY"; michael@0: #ifdef MOZ_ENABLE_DBUS michael@0: static DBusPendingCall *sPendingCall = nullptr; michael@0: #endif michael@0: michael@0: void michael@0: a11y::PreInit() michael@0: { michael@0: #ifdef MOZ_ENABLE_DBUS michael@0: static bool sChecked = FALSE; michael@0: if (sChecked) michael@0: return; michael@0: michael@0: sChecked = TRUE; michael@0: michael@0: // dbus is only checked if GNOME_ACCESSIBILITY is unset michael@0: // also make sure that a session bus address is available to prevent dbus from michael@0: // starting a new one. Dbus confuses the test harness when it creates a new michael@0: // process (see bug 693343) michael@0: if (PR_GetEnv(sAccEnv) || !PR_GetEnv("DBUS_SESSION_BUS_ADDRESS")) michael@0: return; michael@0: michael@0: DBusConnection* bus = dbus_bus_get(DBUS_BUS_SESSION, nullptr); michael@0: if (!bus) michael@0: return; michael@0: michael@0: dbus_connection_set_exit_on_disconnect(bus, FALSE); michael@0: michael@0: static const char* iface = "org.a11y.Status"; michael@0: static const char* member = "IsEnabled"; michael@0: DBusMessage *message; michael@0: message = dbus_message_new_method_call("org.a11y.Bus", "/org/a11y/bus", michael@0: "org.freedesktop.DBus.Properties", michael@0: "Get"); michael@0: if (!message) michael@0: goto dbus_done; michael@0: michael@0: dbus_message_append_args(message, DBUS_TYPE_STRING, &iface, michael@0: DBUS_TYPE_STRING, &member, DBUS_TYPE_INVALID); michael@0: dbus_connection_send_with_reply(bus, message, &sPendingCall, 1000); michael@0: dbus_message_unref(message); michael@0: michael@0: dbus_done: michael@0: dbus_connection_unref(bus); michael@0: #endif michael@0: } michael@0: michael@0: bool michael@0: a11y::ShouldA11yBeEnabled() michael@0: { michael@0: static bool sChecked = false, sShouldEnable = false; michael@0: if (sChecked) michael@0: return sShouldEnable; michael@0: michael@0: sChecked = true; michael@0: michael@0: EPlatformDisabledState disabledState = PlatformDisabledState(); michael@0: if (disabledState == ePlatformIsDisabled) michael@0: return sShouldEnable = false; michael@0: michael@0: // check if accessibility enabled/disabled by environment variable michael@0: const char* envValue = PR_GetEnv(sAccEnv); michael@0: if (envValue) michael@0: return sShouldEnable = !!atoi(envValue); michael@0: michael@0: #ifdef MOZ_ENABLE_DBUS michael@0: PreInit(); michael@0: bool dbusSuccess = false; michael@0: DBusMessage *reply = nullptr; michael@0: if (!sPendingCall) michael@0: goto dbus_done; michael@0: michael@0: dbus_pending_call_block(sPendingCall); michael@0: reply = dbus_pending_call_steal_reply(sPendingCall); michael@0: dbus_pending_call_unref(sPendingCall); michael@0: sPendingCall = nullptr; michael@0: if (!reply || michael@0: dbus_message_get_type(reply) != DBUS_MESSAGE_TYPE_METHOD_RETURN || michael@0: strcmp(dbus_message_get_signature (reply), DBUS_TYPE_VARIANT_AS_STRING)) michael@0: goto dbus_done; michael@0: michael@0: DBusMessageIter iter, iter_variant, iter_struct; michael@0: dbus_bool_t dResult; michael@0: dbus_message_iter_init(reply, &iter); michael@0: dbus_message_iter_recurse (&iter, &iter_variant); michael@0: switch (dbus_message_iter_get_arg_type(&iter_variant)) { michael@0: case DBUS_TYPE_STRUCT: michael@0: // at-spi2-core 2.2.0-2.2.1 had a bug where it returned a struct michael@0: dbus_message_iter_recurse(&iter_variant, &iter_struct); michael@0: if (dbus_message_iter_get_arg_type(&iter_struct) == DBUS_TYPE_BOOLEAN) { michael@0: dbus_message_iter_get_basic(&iter_struct, &dResult); michael@0: sShouldEnable = dResult; michael@0: dbusSuccess = true; michael@0: } michael@0: michael@0: break; michael@0: case DBUS_TYPE_BOOLEAN: michael@0: dbus_message_iter_get_basic(&iter_variant, &dResult); michael@0: sShouldEnable = dResult; michael@0: dbusSuccess = true; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: dbus_done: michael@0: if (reply) michael@0: dbus_message_unref(reply); michael@0: michael@0: if (dbusSuccess) michael@0: return sShouldEnable; michael@0: #endif michael@0: michael@0: //check gconf-2 setting michael@0: static const char sGconfAccessibilityKey[] = michael@0: "/desktop/gnome/interface/accessibility"; michael@0: nsresult rv = NS_OK; michael@0: nsCOMPtr gconf = michael@0: do_GetService(NS_GCONFSERVICE_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv) && gconf) michael@0: gconf->GetBool(NS_LITERAL_CSTRING(sGconfAccessibilityKey), &sShouldEnable); michael@0: michael@0: return sShouldEnable; michael@0: }