2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <dali/integration-api/debug.h>
23 #include <unordered_map>
26 #include <dali/internal/accessibility/bridge/bridge-accessible.h>
27 #include <dali/internal/accessibility/bridge/bridge-action.h>
28 #include <dali/internal/accessibility/bridge/bridge-collection.h>
29 #include <dali/internal/accessibility/bridge/bridge-component.h>
30 #include <dali/internal/accessibility/bridge/bridge-object.h>
31 #include <dali/internal/accessibility/bridge/bridge-value.h>
32 #include <dali/internal/accessibility/bridge/bridge-text.h>
33 #include <dali/internal/accessibility/bridge/bridge-editable-text.h>
35 using namespace Dali::Accessibility;
37 class BridgeImpl : public virtual BridgeBase,
38 public BridgeAccessible,
40 public BridgeComponent,
41 public BridgeCollection,
45 public BridgeEditableText
47 DBus::DBusClient listenOnAtspiEnabledSignalClient;
48 DBus::DBusClient registryClient, directReadingClient;
49 bool screenReaderEnabled = false;
50 bool isEnabled = false;
52 std::unordered_map <int32_t, std::function<void(std::string)>> directReadingCallbacks;
53 Dali::Actor highlightedActor;
54 std::function<void(Dali::Actor)> highlightClearAction;
59 listenOnAtspiEnabledSignalClient = DBus::DBusClient{ A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION };
61 listenOnAtspiEnabledSignalClient.addPropertyChangedEvent< bool >( "ScreenReaderEnabled", [this]( bool res )
63 screenReaderEnabled = res;
64 if( screenReaderEnabled || isEnabled )
75 listenOnAtspiEnabledSignalClient.addPropertyChangedEvent< bool >( "IsEnabled", [this]( bool res )
78 if( screenReaderEnabled || isEnabled )
90 Consumed Emit( KeyEventType type, unsigned int keyCode, const std::string& keyName, unsigned int timeStamp, bool isText ) override
97 unsigned int evType = 0;
101 case KeyEventType::KEY_PRESSED:
106 case KeyEventType::KEY_RELEASED:
116 auto m = registryClient.method< bool( std::tuple< uint32_t, int32_t, int32_t, int32_t, int32_t, std::string, bool > ) >( "NotifyListenersSync" );
117 auto result = m.call( std::tuple< uint32_t, int32_t, int32_t, int32_t, int32_t, std::string, bool >{evType, 0, static_cast< int32_t >( keyCode ), 0, static_cast< int32_t >( timeStamp ), keyName, isText ? 1 : 0} );
120 LOG() << result.getError().message;
123 return std::get< 0 >( result ) ? Consumed::YES : Consumed::NO;
126 void Pause() override
133 directReadingClient.method< DBus::ValueOrError< void >( bool ) > ( "PauseResume" ).asyncCall(
134 []( DBus::ValueOrError< void > msg ) {
137 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
143 void Resume() override
150 directReadingClient.method< DBus::ValueOrError< void >( bool ) > ( "PauseResume" ).asyncCall(
151 []( DBus::ValueOrError< void > msg) {
154 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
160 void Say( const std::string& text, bool discardable, std::function< void(std::string) > callback ) override
167 directReadingClient.method< DBus::ValueOrError< std::string, bool, int32_t >( std::string, bool ) > ( "ReadCommand" ).asyncCall(
168 [=]( DBus::ValueOrError<std::string, bool, int32_t> msg ) {
171 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
175 directReadingCallbacks.emplace( std::get< 2 >( msg ), callback);
182 void ForceDown() override
186 if (data->currentlyHighlightedActor && data->highlightActor)
188 data->currentlyHighlightedActor.Remove(data->highlightActor);
190 data->currentlyHighlightedActor = {};
191 data->highlightActor = {};
193 highlightedActor = {};
194 highlightClearAction = {};
195 BridgeAccessible::ForceDown();
197 directReadingClient = {};
198 directReadingCallbacks.clear();
201 void Terminate() override
205 data->currentlyHighlightedActor = {};
206 data->highlightActor = {};
209 listenOnAtspiEnabledSignalClient = {};
214 ForceUpResult ForceUp() override
216 if( BridgeAccessible::ForceUp() == ForceUpResult::ALREADY_UP )
218 return ForceUpResult::ALREADY_UP;
221 BridgeObject::RegisterInterfaces();
222 BridgeAccessible::RegisterInterfaces();
223 BridgeComponent::RegisterInterfaces();
224 BridgeCollection::RegisterInterfaces();
225 BridgeAction::RegisterInterfaces();
226 BridgeValue::RegisterInterfaces();
227 BridgeText::RegisterInterfaces();
228 BridgeEditableText::RegisterInterfaces();
230 RegisterOnBridge( &application );
232 registryClient = { AtspiDbusNameRegistry, AtspiDbusPathDec, AtspiDbusInterfaceDec, con };
233 directReadingClient = DBus::DBusClient{ DirectReadingDBusName, DirectReadingDBusPath, DirectReadingDBusInterface, con };
234 directReadingClient.addSignal< void(int32_t, std::string) >( "ReadingStateChanged", [=]( int32_t id, std::string readingState )
236 auto it = directReadingCallbacks.find( id );
237 if (it != directReadingCallbacks.end())
239 it->second( readingState );
240 if (readingState != "ReadingPaused" && readingState != "ReadingResumed" && readingState != "ReadingStarted")
241 directReadingCallbacks.erase( it );
246 auto proxy = DBus::DBusClient{AtspiDbusNameRegistry, AtspiDbusPathRoot, AtspiDbusInterfaceSocket, con};
247 Address root{"", "root"};
248 auto res = proxy.method< Address( Address ) >( "Embed" ).call( root );
251 LOG() << "Call to Embed failed: " << res.getError().message;
254 application.parent.SetAddress( std::move( std::get< 0 >( res ) ) );
259 return ForceUpResult::JUST_STARTED;
264 auto win = application.getActiveWindow();
267 win->Emit( WindowEvent::ACTIVATE, 0 );
271 void EmitDeactivate()
273 auto win = application.getActiveWindow();
276 win->Emit( WindowEvent::DEACTIVATE, 0 );
280 void ApplicationHidden() override
282 if ( isShown && IsUp() )
289 void ApplicationShown() override
291 if ( !isShown && IsUp() )
298 void Initialize() override
300 auto req = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
301 auto p = req.property< bool >( "ScreenReaderEnabled" ).get();
304 screenReaderEnabled = std::get< 0 >( p );
306 p = req.property< bool >( "IsEnabled" ).get();
309 isEnabled = std::get< 0 >( p );
311 if( screenReaderEnabled || isEnabled )
317 bool GetScreenReaderEnabled()
319 return screenReaderEnabled;
329 Bridge* Bridge::GetCurrentBridge()
331 static BridgeImpl *bridge = new BridgeImpl;