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>
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) {
62 screenReaderEnabled = res;
63 if(screenReaderEnabled || isEnabled)
73 listenOnAtspiEnabledSignalClient.addPropertyChangedEvent<bool>("IsEnabled", [this](bool res) {
75 if(screenReaderEnabled || isEnabled)
86 Consumed Emit(KeyEventType type, unsigned int keyCode, const std::string& keyName, unsigned int timeStamp, bool isText) override
93 unsigned int evType = 0;
97 case KeyEventType::KEY_PRESSED:
102 case KeyEventType::KEY_RELEASED:
112 auto m = registryClient.method<bool(std::tuple<uint32_t, int32_t, int32_t, int32_t, int32_t, std::string, bool>)>("NotifyListenersSync");
113 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});
116 LOG() << result.getError().message;
119 return std::get<0>(result) ? Consumed::YES : Consumed::NO;
122 void Pause() override
129 directReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
132 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
138 void Resume() override
145 directReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
148 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
154 void Say(const std::string& text, bool discardable, std::function<void(std::string)> callback) override
161 directReadingClient.method<DBus::ValueOrError<std::string, bool, int32_t>(std::string, bool)>("ReadCommand").asyncCall([=](DBus::ValueOrError<std::string, bool, int32_t> msg) {
164 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
168 directReadingCallbacks.emplace(std::get<2>(msg), callback);
175 void ForceDown() override
179 if(data->currentlyHighlightedActor && data->highlightActor)
181 data->currentlyHighlightedActor.Remove(data->highlightActor);
183 data->currentlyHighlightedActor = {};
184 data->highlightActor = {};
186 highlightedActor = {};
187 highlightClearAction = {};
188 BridgeAccessible::ForceDown();
190 directReadingClient = {};
191 directReadingCallbacks.clear();
194 void Terminate() override
198 data->currentlyHighlightedActor = {};
199 data->highlightActor = {};
202 listenOnAtspiEnabledSignalClient = {};
207 ForceUpResult ForceUp() override
209 if(BridgeAccessible::ForceUp() == ForceUpResult::ALREADY_UP)
211 return ForceUpResult::ALREADY_UP;
214 BridgeObject::RegisterInterfaces();
215 BridgeAccessible::RegisterInterfaces();
216 BridgeComponent::RegisterInterfaces();
217 BridgeCollection::RegisterInterfaces();
218 BridgeAction::RegisterInterfaces();
219 BridgeValue::RegisterInterfaces();
220 BridgeText::RegisterInterfaces();
221 BridgeEditableText::RegisterInterfaces();
223 RegisterOnBridge(&application);
225 registryClient = {AtspiDbusNameRegistry, AtspiDbusPathDec, AtspiDbusInterfaceDec, con};
226 directReadingClient = DBus::DBusClient{DirectReadingDBusName, DirectReadingDBusPath, DirectReadingDBusInterface, con};
227 directReadingClient.addSignal<void(int32_t, std::string)>("ReadingStateChanged", [=](int32_t id, std::string readingState) {
228 auto it = directReadingCallbacks.find(id);
229 if(it != directReadingCallbacks.end())
231 it->second(readingState);
232 if(readingState != "ReadingPaused" && readingState != "ReadingResumed" && readingState != "ReadingStarted")
233 directReadingCallbacks.erase(it);
237 auto proxy = DBus::DBusClient{AtspiDbusNameRegistry, AtspiDbusPathRoot, AtspiDbusInterfaceSocket, con};
238 Address root{"", "root"};
239 auto res = proxy.method<Address(Address)>("Embed").call(root);
242 LOG() << "Call to Embed failed: " << res.getError().message;
245 application.parent.SetAddress(std::move(std::get<0>(res)));
250 return ForceUpResult::JUST_STARTED;
255 auto win = application.getActiveWindow();
258 win->Emit(WindowEvent::ACTIVATE, 0);
262 void EmitDeactivate()
264 auto win = application.getActiveWindow();
267 win->Emit(WindowEvent::DEACTIVATE, 0);
271 void ApplicationHidden() override
273 if(isShown && IsUp())
280 void ApplicationShown() override
282 if(!isShown && IsUp())
289 void Initialize() override
291 auto req = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
292 auto p = req.property<bool>("ScreenReaderEnabled").get();
295 screenReaderEnabled = std::get<0>(p);
297 p = req.property<bool>("IsEnabled").get();
300 isEnabled = std::get<0>(p);
302 if(screenReaderEnabled || isEnabled)
308 bool GetScreenReaderEnabled()
310 return screenReaderEnabled;
319 Bridge* Bridge::GetCurrentBridge()
321 static BridgeImpl* bridge = new BridgeImpl;