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 auto r = directReadingClient.method< DBus::ValueOrError< void >( bool ) > ( "PauseResume" ).call( true );
136 LOG() << "Direct reading command failed (" << r.getError().message << ")";
140 void Resume() override
147 auto r = directReadingClient.method< DBus::ValueOrError< void >( bool ) > ( "PauseResume" ).call( false );
150 LOG() << "Direct reading command failed (" << r.getError().message << ")";
154 void Say( const std::string& text, bool discardable, std::function< void(std::string) > callback ) override
161 auto commandId = directReadingClient.method< DBus::ValueOrError< std::string, bool, int32_t >( std::string, bool ) > ( "ReadCommand" ).call( text, discardable );
164 LOG() << "Direct reading command failed (" << commandId.getError().message << ")";
168 directReadingCallbacks.emplace( std::get< 2 >( commandId ), callback);
172 void ForceDown() override
176 if (data->currentlyHighlightedActor && data->highlightActor)
178 data->currentlyHighlightedActor.Remove(data->highlightActor);
180 data->currentlyHighlightedActor = {};
181 data->highlightActor = {};
183 highlightedActor = {};
184 highlightClearAction = {};
185 BridgeAccessible::ForceDown();
187 directReadingClient = {};
188 directReadingCallbacks.clear();
191 void Terminate() override
195 data->currentlyHighlightedActor = {};
196 data->highlightActor = {};
199 listenOnAtspiEnabledSignalClient = {};
204 ForceUpResult ForceUp() override
206 if( BridgeAccessible::ForceUp() == ForceUpResult::ALREADY_UP )
208 return ForceUpResult::ALREADY_UP;
211 BridgeObject::RegisterInterfaces();
212 BridgeAccessible::RegisterInterfaces();
213 BridgeComponent::RegisterInterfaces();
214 BridgeCollection::RegisterInterfaces();
215 BridgeAction::RegisterInterfaces();
216 BridgeValue::RegisterInterfaces();
217 BridgeText::RegisterInterfaces();
218 BridgeEditableText::RegisterInterfaces();
220 RegisterOnBridge( &application );
222 registryClient = { AtspiDbusNameRegistry, AtspiDbusPathDec, AtspiDbusInterfaceDec, con };
223 directReadingClient = DBus::DBusClient{ DirectReadingDBusName, DirectReadingDBusPath, DirectReadingDBusInterface, con };
224 directReadingClient.addSignal< void(int32_t, std::string) >( "ReadingStateChanged", [=]( int32_t id, std::string readingState )
226 auto it = directReadingCallbacks.find( id );
227 if (it != directReadingCallbacks.end())
229 it->second( readingState );
230 if (readingState != "ReadingPaused" && readingState != "ReadingResumed" && readingState != "ReadingStarted")
231 directReadingCallbacks.erase( it );
236 auto proxy = DBus::DBusClient{AtspiDbusNameRegistry, AtspiDbusPathRoot, AtspiDbusInterfaceSocket, con};
237 Address root{"", "root"};
238 auto res = proxy.method< Address( Address ) >( "Embed" ).call( root );
241 LOG() << "Call to Embed failed: " << res.getError().message;
244 application.parent.SetAddress( std::move( std::get< 0 >( res ) ) );
249 return ForceUpResult::JUST_STARTED;
254 auto win = application.getActiveWindow();
257 win->Emit( WindowEvent::ACTIVATE, 0 );
261 void EmitDeactivate()
263 auto win = application.getActiveWindow();
266 win->Emit( WindowEvent::DEACTIVATE, 0 );
270 void ApplicationHidden() override
272 if ( isShown && IsUp() )
279 void ApplicationShown() override
281 if ( !isShown && IsUp() )
288 void Initialize() override
290 auto req = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
291 auto p = req.property< bool >( "ScreenReaderEnabled" ).get();
294 screenReaderEnabled = std::get< 0 >( p );
296 p = req.property< bool >( "IsEnabled" ).get();
299 isEnabled = std::get< 0 >( p );
301 if( screenReaderEnabled || isEnabled )
307 bool GetScreenReaderEnabled()
309 return screenReaderEnabled;
319 Bridge* Bridge::GetCurrentBridge()
321 static BridgeImpl *bridge = new BridgeImpl;