2 * Copyright (c) 2021 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-editable-text.h>
31 #include <dali/internal/accessibility/bridge/bridge-object.h>
32 #include <dali/internal/accessibility/bridge/bridge-text.h>
33 #include <dali/internal/accessibility/bridge/bridge-value.h>
34 #include <dali/internal/accessibility/bridge/dummy-atspi.h>
36 using namespace Dali::Accessibility;
38 class BridgeImpl : public virtual BridgeBase,
39 public BridgeAccessible,
41 public BridgeComponent,
42 public BridgeCollection,
46 public BridgeEditableText
48 DBus::DBusClient listenOnAtspiEnabledSignalClient;
49 DBus::DBusClient registryClient, directReadingClient;
50 bool screenReaderEnabled = false;
51 bool isEnabled = false;
53 std::unordered_map<int32_t, std::function<void(std::string)>> directReadingCallbacks;
54 Dali::Actor highlightedActor;
55 std::function<void(Dali::Actor)> highlightClearAction;
60 listenOnAtspiEnabledSignalClient = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
62 listenOnAtspiEnabledSignalClient.addPropertyChangedEvent<bool>("ScreenReaderEnabled", [this](bool res) {
63 screenReaderEnabled = res;
64 if(screenReaderEnabled || isEnabled)
74 listenOnAtspiEnabledSignalClient.addPropertyChangedEvent<bool>("IsEnabled", [this](bool res) {
76 if(screenReaderEnabled || isEnabled)
87 Consumed Emit(KeyEventType type, unsigned int keyCode, const std::string& keyName, unsigned int timeStamp, bool isText) override
94 unsigned int evType = 0;
98 case KeyEventType::KEY_PRESSED:
103 case KeyEventType::KEY_RELEASED:
113 auto m = registryClient.method<bool(std::tuple<uint32_t, int32_t, int32_t, int32_t, int32_t, std::string, bool>)>("NotifyListenersSync");
114 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});
117 LOG() << result.getError().message;
120 return std::get<0>(result) ? Consumed::YES : Consumed::NO;
123 void Pause() override
130 directReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
133 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
139 void Resume() override
146 directReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
149 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
155 void Say(const std::string& text, bool discardable, std::function<void(std::string)> callback) override
162 directReadingClient.method<DBus::ValueOrError<std::string, bool, int32_t>(std::string, bool)>("ReadCommand").asyncCall([=](DBus::ValueOrError<std::string, bool, int32_t> msg) {
165 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
169 directReadingCallbacks.emplace(std::get<2>(msg), callback);
176 void ForceDown() override
180 if(data->currentlyHighlightedActor && data->highlightActor)
182 data->currentlyHighlightedActor.Remove(data->highlightActor);
184 data->currentlyHighlightedActor = {};
185 data->highlightActor = {};
187 highlightedActor = {};
188 highlightClearAction = {};
189 BridgeAccessible::ForceDown();
191 directReadingClient = {};
192 directReadingCallbacks.clear();
195 void Terminate() override
199 data->currentlyHighlightedActor = {};
200 data->highlightActor = {};
203 listenOnAtspiEnabledSignalClient = {};
208 ForceUpResult ForceUp() override
210 if(BridgeAccessible::ForceUp() == ForceUpResult::ALREADY_UP)
212 return ForceUpResult::ALREADY_UP;
215 BridgeObject::RegisterInterfaces();
216 BridgeAccessible::RegisterInterfaces();
217 BridgeComponent::RegisterInterfaces();
218 BridgeCollection::RegisterInterfaces();
219 BridgeAction::RegisterInterfaces();
220 BridgeValue::RegisterInterfaces();
221 BridgeText::RegisterInterfaces();
222 BridgeEditableText::RegisterInterfaces();
224 RegisterOnBridge(&application);
226 registryClient = {AtspiDbusNameRegistry, AtspiDbusPathDec, AtspiDbusInterfaceDec, con};
227 directReadingClient = DBus::DBusClient{DirectReadingDBusName, DirectReadingDBusPath, DirectReadingDBusInterface, con};
228 directReadingClient.addSignal<void(int32_t, std::string)>("ReadingStateChanged", [=](int32_t id, std::string readingState) {
229 auto it = directReadingCallbacks.find(id);
230 if(it != directReadingCallbacks.end())
232 it->second(readingState);
233 if(readingState != "ReadingPaused" && readingState != "ReadingResumed" && readingState != "ReadingStarted")
234 directReadingCallbacks.erase(it);
238 auto proxy = DBus::DBusClient{AtspiDbusNameRegistry, AtspiDbusPathRoot, AtspiDbusInterfaceSocket, con};
239 Address root{"", "root"};
240 auto res = proxy.method<Address(Address)>("Embed").call(root);
243 LOG() << "Call to Embed failed: " << res.getError().message;
246 application.parent.SetAddress(std::move(std::get<0>(res)));
251 return ForceUpResult::JUST_STARTED;
256 auto win = application.getActiveWindow();
259 win->Emit(WindowEvent::ACTIVATE, 0);
263 void EmitDeactivate()
265 auto win = application.getActiveWindow();
268 win->Emit(WindowEvent::DEACTIVATE, 0);
272 void ApplicationHidden() override
274 if(isShown && IsUp())
281 void ApplicationShown() override
283 if(!isShown && IsUp())
290 void Initialize() override
292 auto req = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
293 auto p = req.property<bool>("ScreenReaderEnabled").get();
296 screenReaderEnabled = std::get<0>(p);
298 p = req.property<bool>("IsEnabled").get();
301 isEnabled = std::get<0>(p);
303 if(screenReaderEnabled || isEnabled)
309 bool GetScreenReaderEnabled()
311 return screenReaderEnabled;
320 static Bridge* CreateBridge()
324 return new BridgeImpl;
326 catch (const std::exception&)
328 DALI_LOG_ERROR("Failed to initialize AT-SPI bridge");
329 return Dali::Accessibility::DummyBridge::GetInstance();
333 Bridge* Bridge::GetCurrentBridge()
335 static Bridge* bridge = CreateBridge();