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, isEnabled = false, isShown = false;
50 std::unordered_map <int32_t, std::function<void(std::string)>> directReadingCallbacks;
51 Dali::Actor highlightedActor;
52 std::function<void(Dali::Actor)> highlightClearAction;
56 listenOnAtspiEnabledSignalClient = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface,
57 DBus::ConnectionType::SESSION};
58 listenOnAtspiEnabledSignalClient.addPropertyChangedEvent< bool >( "ScreenReaderEnabled", [this]( bool res ) {
59 screenReaderEnabled = res;
60 if( screenReaderEnabled || isEnabled )
65 listenOnAtspiEnabledSignalClient.addPropertyChangedEvent< bool >( "IsEnabled", [this]( bool res ) {
67 if( screenReaderEnabled || isEnabled )
74 Consumed Emit( KeyEventType type, unsigned int keyCode, const std::string& keyName, unsigned int timeStamp, bool isText ) override
76 if (!IsUp()) return Consumed::NO;
77 unsigned int evType = 0;
81 case KeyEventType::KEY_PRESSED:
86 case KeyEventType::KEY_RELEASED:
96 auto m = registryClient.method< bool( std::tuple< uint32_t, int32_t, int32_t, int32_t, int32_t, std::string, bool > ) >( "NotifyListenersSync" );
97 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} );
100 LOG() << result.getError().message;
103 return std::get< 0 >( result ) ? Consumed::YES : Consumed::NO;
106 void PauseResume( bool pause ) override
108 auto r = directReadingClient.method< DBus::ValueOrError< void >( bool ) > ( "PauseResume" ).call( pause );
110 LOG() << "Direct reading command failed (" << r.getError().message << ")";
114 void Say( const std::string& text, bool discardable, std::function< void(std::string) > callback ) override
116 auto commandId = directReadingClient.method< DBus::ValueOrError< std::string, bool, int32_t >( std::string, bool ) > ( "ReadCommand" ).call( text, discardable );
118 LOG() << "Direct reading command failed (" << commandId.getError().message << ")";
119 } else if( callback ) {
120 directReadingCallbacks.emplace( std::get< 2 >( commandId ), callback);
124 void ForceDown() override
127 if (data->currentlyHighlightedActor && data->highlightActor) {
128 data->currentlyHighlightedActor.Remove(data->highlightActor);
130 data->currentlyHighlightedActor = {};
131 data->highlightActor = {};
133 highlightedActor = {};
134 highlightClearAction = {};
135 BridgeAccessible::ForceDown();
137 directReadingClient = {};
138 directReadingCallbacks.clear();
140 void Terminate() override
143 data->currentlyHighlightedActor = {};
144 data->highlightActor = {};
147 listenOnAtspiEnabledSignalClient = {};
152 ForceUpResult ForceUp() override
154 if( BridgeAccessible::ForceUp() == ForceUpResult::ALREADY_UP )
155 return ForceUpResult::ALREADY_UP;
157 BridgeObject::RegisterInterfaces();
158 BridgeAccessible::RegisterInterfaces();
159 BridgeComponent::RegisterInterfaces();
160 BridgeCollection::RegisterInterfaces();
161 BridgeAction::RegisterInterfaces();
162 BridgeValue::RegisterInterfaces();
163 BridgeText::RegisterInterfaces();
164 BridgeEditableText::RegisterInterfaces();
166 RegisterOnBridge( &application );
168 registryClient = { AtspiDbusNameRegistry, AtspiDbusPathDec, AtspiDbusInterfaceDec, con };
169 directReadingClient = DBus::DBusClient{ DirectReadingDBusName, DirectReadingDBusPath, DirectReadingDBusInterface, con };
170 directReadingClient.addSignal< void(int32_t, std::string) >( "ReadingStateChanged", [=]( int32_t id, std::string readingState ) {
171 auto it = directReadingCallbacks.find( id );
172 if (it != directReadingCallbacks.end())
174 it->second( readingState );
175 directReadingCallbacks.erase( it );
178 auto proxy = DBus::DBusClient{AtspiDbusNameRegistry, AtspiDbusPathRoot, AtspiDbusInterfaceSocket, con};
179 Address root{"", "root"};
180 auto res = proxy.method< Address( Address ) >( "Embed" ).call( root );
182 LOG() << "Call to Embed failed: " << res.getError().message;
184 application.parent.SetAddress( std::move( std::get< 0 >( res ) ) );
188 return ForceUpResult::JUST_STARTED;
193 auto win = application.getActiveWindow();
195 win->Emit( WindowEvent::ACTIVATE, 0 );
198 void EmitDeactivate()
200 auto win = application.getActiveWindow();
202 win->Emit( WindowEvent::DEACTIVATE, 0 );
205 void ApplicationHidden() override
207 if ( isShown && IsUp() )
211 void ApplicationShown() override
213 if ( !isShown && IsUp() )
217 void Initialize() override
219 auto req = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
220 auto p = req.property< bool >( "ScreenReaderEnabled" ).get();
222 screenReaderEnabled = std::get< 0 >( p );
223 p = req.property< bool >( "IsEnabled" ).get();
225 isEnabled = std::get< 0 >( p );
226 if( screenReaderEnabled || isEnabled )
231 Bridge* Bridge::GetCurrentBridge()
233 static BridgeImpl *bridge = new BridgeImpl;