1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include "nsIAccessibleEvent.h"
10 #include "nsIGSettingsService.h"
12 #include "nsServiceManagerUtils.h"
16 #ifdef MOZ_ENABLE_DBUS
17 # include <dbus/dbus.h>
21 using namespace mozilla
;
22 using namespace mozilla::a11y
;
24 int atkMajorVersion
= 1, atkMinorVersion
= 12, atkMicroVersion
= 0;
26 GType (*gAtkTableCellGetTypeFunc
)();
29 typedef GType (*AtkGetTypeType
)(void);
30 typedef void (*AtkBridgeAdaptorInit
)(int*, char**[]);
33 static PRLibrary
* sATKLib
= nullptr;
34 static const char sATKLibName
[] = "libatk-1.0.so.0";
35 static const char sATKHyperlinkImplGetTypeSymbol
[] =
36 "atk_hyperlink_impl_get_type";
38 gboolean
toplevel_event_watcher(GSignalInvocationHint
*, guint
, const GValue
*,
40 static bool sToplevel_event_hook_added
= false;
41 static gulong sToplevel_show_hook
= 0;
42 static gulong sToplevel_hide_hook
= 0;
44 GType g_atk_hyperlink_impl_type
= G_TYPE_INVALID
;
46 struct AtkBridgeModule
{
50 AtkBridgeAdaptorInit init
;
53 static AtkBridgeModule sAtkBridge
= {"libatk-bridge-2.0.so.0", nullptr,
54 "atk_bridge_adaptor_init", nullptr};
56 static nsresult
LoadGtkModule(AtkBridgeModule
& aModule
) {
57 NS_ENSURE_ARG(aModule
.libName
);
59 if (!(aModule
.lib
= PR_LoadLibrary(aModule
.libName
))) {
60 return NS_ERROR_FAILURE
;
63 // we have loaded the library, try to get the function ptrs
64 if (!(aModule
.init
= (AtkBridgeAdaptorInit
)PR_FindFunctionSymbol(
65 aModule
.lib
, aModule
.initName
))) {
67 PR_UnloadLibrary(aModule
.lib
);
68 aModule
.lib
= nullptr;
69 return NS_ERROR_FAILURE
;
74 void a11y::PlatformInit() {
75 if (!ShouldA11yBeEnabled()) return;
77 sATKLib
= PR_LoadLibrary(sATKLibName
);
80 AtkGetTypeType pfn_atk_hyperlink_impl_get_type
=
81 (AtkGetTypeType
)PR_FindFunctionSymbol(sATKLib
,
82 sATKHyperlinkImplGetTypeSymbol
);
83 if (pfn_atk_hyperlink_impl_get_type
) {
84 g_atk_hyperlink_impl_type
= pfn_atk_hyperlink_impl_get_type();
87 gAtkTableCellGetTypeFunc
=
88 (GType(*)())PR_FindFunctionSymbol(sATKLib
, "atk_table_cell_get_type");
90 const char* (*atkGetVersion
)() =
91 (const char* (*)())PR_FindFunctionSymbol(sATKLib
, "atk_get_version");
93 const char* version
= atkGetVersion();
95 char* endPtr
= nullptr;
96 atkMajorVersion
= strtol(version
, &endPtr
, 10);
97 if (atkMajorVersion
!= 0L) {
98 atkMinorVersion
= strtol(endPtr
+ 1, &endPtr
, 10);
99 if (atkMinorVersion
!= 0L) {
100 atkMicroVersion
= strtol(endPtr
+ 1, &endPtr
, 10);
106 // Initialize the MAI Utility class, it will overwrite gail_util.
107 g_type_class_unref(g_type_class_ref(mai_util_get_type()));
109 // Init atk-bridge now
110 PR_SetEnv("NO_AT_BRIDGE=0");
111 nsresult rv
= LoadGtkModule(sAtkBridge
);
112 if (NS_SUCCEEDED(rv
)) {
113 (*sAtkBridge
.init
)(nullptr, nullptr);
116 if (!sToplevel_event_hook_added
) {
117 sToplevel_event_hook_added
= true;
118 sToplevel_show_hook
= g_signal_add_emission_hook(
119 g_signal_lookup("show", GTK_TYPE_WINDOW
), 0, toplevel_event_watcher
,
120 reinterpret_cast<gpointer
>(nsIAccessibleEvent::EVENT_SHOW
), nullptr);
121 sToplevel_hide_hook
= g_signal_add_emission_hook(
122 g_signal_lookup("hide", GTK_TYPE_WINDOW
), 0, toplevel_event_watcher
,
123 reinterpret_cast<gpointer
>(nsIAccessibleEvent::EVENT_HIDE
), nullptr);
127 void a11y::PlatformShutdown() {
128 if (sToplevel_event_hook_added
) {
129 sToplevel_event_hook_added
= false;
130 g_signal_remove_emission_hook(g_signal_lookup("show", GTK_TYPE_WINDOW
),
131 sToplevel_show_hook
);
132 g_signal_remove_emission_hook(g_signal_lookup("hide", GTK_TYPE_WINDOW
),
133 sToplevel_hide_hook
);
136 if (sAtkBridge
.lib
) {
137 // Do not shutdown/unload atk-bridge,
138 // an exit function registered will take care of it
139 // PR_UnloadLibrary(sAtkBridge.lib);
140 sAtkBridge
.lib
= nullptr;
141 sAtkBridge
.init
= nullptr;
144 // PR_UnloadLibrary(sATKLib);
145 // sATKLib = nullptr;
149 static const char sAccEnv
[] = "GNOME_ACCESSIBILITY";
150 #ifdef MOZ_ENABLE_DBUS
151 static DBusPendingCall
* sPendingCall
= nullptr;
154 void a11y::PreInit() {
155 #ifdef MOZ_ENABLE_DBUS
156 static bool sChecked
= FALSE
;
157 if (sChecked
) return;
161 // dbus is only checked if GNOME_ACCESSIBILITY is unset
162 // also make sure that a session bus address is available to prevent dbus from
163 // starting a new one. Dbus confuses the test harness when it creates a new
164 // process (see bug 693343)
165 if (PR_GetEnv(sAccEnv
) || !PR_GetEnv("DBUS_SESSION_BUS_ADDRESS")) return;
167 DBusConnection
* bus
= dbus_bus_get(DBUS_BUS_SESSION
, nullptr);
170 dbus_connection_set_exit_on_disconnect(bus
, FALSE
);
172 static const char* iface
= "org.a11y.Status";
173 static const char* member
= "IsEnabled";
174 DBusMessage
* message
;
176 dbus_message_new_method_call("org.a11y.Bus", "/org/a11y/bus",
177 "org.freedesktop.DBus.Properties", "Get");
178 if (!message
) goto dbus_done
;
180 dbus_message_append_args(message
, DBUS_TYPE_STRING
, &iface
, DBUS_TYPE_STRING
,
181 &member
, DBUS_TYPE_INVALID
);
182 dbus_connection_send_with_reply(bus
, message
, &sPendingCall
, 1000);
183 dbus_message_unref(message
);
186 dbus_connection_unref(bus
);
190 bool a11y::ShouldA11yBeEnabled() {
191 static bool sChecked
= false, sShouldEnable
= false;
192 if (sChecked
) return sShouldEnable
;
196 EPlatformDisabledState disabledState
= PlatformDisabledState();
197 if (disabledState
== ePlatformIsDisabled
) {
198 return sShouldEnable
= false;
200 if (disabledState
== ePlatformIsForceEnabled
) {
201 return sShouldEnable
= true;
204 // check if accessibility enabled/disabled by environment variable
205 const char* envValue
= PR_GetEnv(sAccEnv
);
206 if (envValue
) return sShouldEnable
= !!atoi(envValue
);
208 #ifdef MOZ_ENABLE_DBUS
210 bool dbusSuccess
= false;
211 DBusMessage
* reply
= nullptr;
212 if (!sPendingCall
) goto dbus_done
;
214 dbus_pending_call_block(sPendingCall
);
215 reply
= dbus_pending_call_steal_reply(sPendingCall
);
216 dbus_pending_call_unref(sPendingCall
);
217 sPendingCall
= nullptr;
219 dbus_message_get_type(reply
) != DBUS_MESSAGE_TYPE_METHOD_RETURN
||
220 strcmp(dbus_message_get_signature(reply
), DBUS_TYPE_VARIANT_AS_STRING
)) {
224 DBusMessageIter iter
, iter_variant
, iter_struct
;
226 dbus_message_iter_init(reply
, &iter
);
227 dbus_message_iter_recurse(&iter
, &iter_variant
);
228 switch (dbus_message_iter_get_arg_type(&iter_variant
)) {
229 case DBUS_TYPE_STRUCT
:
230 // at-spi2-core 2.2.0-2.2.1 had a bug where it returned a struct
231 dbus_message_iter_recurse(&iter_variant
, &iter_struct
);
232 if (dbus_message_iter_get_arg_type(&iter_struct
) == DBUS_TYPE_BOOLEAN
) {
233 dbus_message_iter_get_basic(&iter_struct
, &dResult
);
234 sShouldEnable
= dResult
;
239 case DBUS_TYPE_BOOLEAN
:
240 dbus_message_iter_get_basic(&iter_variant
, &dResult
);
241 sShouldEnable
= dResult
;
249 if (reply
) dbus_message_unref(reply
);
251 if (dbusSuccess
) return sShouldEnable
;
255 #define GSETINGS_A11Y_INTERFACE "org.gnome.desktop.interface"
256 #define GSETINGS_A11Y_KEY "toolkit-accessibility"
257 nsCOMPtr
<nsIGSettingsService
> gsettings
=
258 do_GetService(NS_GSETTINGSSERVICE_CONTRACTID
);
259 nsCOMPtr
<nsIGSettingsCollection
> a11y_settings
;
262 gsettings
->GetCollectionForSchema(nsLiteralCString(GSETINGS_A11Y_INTERFACE
),
263 getter_AddRefs(a11y_settings
));
265 a11y_settings
->GetBoolean(nsLiteralCString(GSETINGS_A11Y_KEY
),
270 return sShouldEnable
;