44daa00f057a1a1f6092e5f1a22119a85ddefba7
[platform/core/uifw/dali-adaptor.git] / dali / internal / accessibility / tizen-wayland / atspi / bridge-impl.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19
20 // EXTERNAL INCLUDES
21 #include <dali/integration-api/debug.h>
22 #include <iostream>
23 #include <unordered_map>
24
25 // INTERNAL INCLUDES
26 #include <dali/internal/accessibility/tizen-wayland/atspi/bridge-accessible.h>
27 #include <dali/internal/accessibility/tizen-wayland/atspi/bridge-action.h>
28 #include <dali/internal/accessibility/tizen-wayland/atspi/bridge-collection.h>
29 #include <dali/internal/accessibility/tizen-wayland/atspi/bridge-component.h>
30 #include <dali/internal/accessibility/tizen-wayland/atspi/bridge-object.h>
31 #include <dali/internal/accessibility/tizen-wayland/atspi/bridge-value.h>
32 #include <dali/internal/accessibility/tizen-wayland/atspi/bridge-text.h>
33 #include <dali/internal/accessibility/tizen-wayland/atspi/bridge-editable-text.h>
34
35 using namespace Dali::Accessibility;
36
37 class BridgeImpl : public virtual BridgeBase,
38                    public BridgeAccessible,
39                    public BridgeObject,
40                    public BridgeComponent,
41                    public BridgeCollection,
42                    public BridgeAction,
43                    public BridgeValue,
44                    public BridgeText,
45                    public BridgeEditableText
46 {
47   DBus::DBusClient listenOnAtspiEnabledSignalClient;
48   DBus::DBusClient registryClient, directReadingClient;
49   bool screenReaderEnabled = false;
50   bool isEnabled = false;
51   bool isShown = false;
52   std::unordered_map <int32_t, std::function<void(std::string)>> directReadingCallbacks;
53   Dali::Actor highlightedActor;
54   std::function<void(Dali::Actor)> highlightClearAction;
55
56 public:
57   BridgeImpl()
58   {
59     listenOnAtspiEnabledSignalClient = DBus::DBusClient{ A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION };
60
61     listenOnAtspiEnabledSignalClient.addPropertyChangedEvent< bool >( "ScreenReaderEnabled", [this]( bool res )
62       {
63         screenReaderEnabled = res;
64         if( screenReaderEnabled || isEnabled )
65         {
66           ForceUp();
67         }
68         else
69         {
70           ForceDown();
71         }
72       }
73     );
74
75     listenOnAtspiEnabledSignalClient.addPropertyChangedEvent< bool >( "IsEnabled", [this]( bool res )
76       {
77         isEnabled = res;
78         if( screenReaderEnabled || isEnabled )
79         {
80           ForceUp();
81         }
82         else
83         {
84           ForceDown();
85         }
86       }
87     );
88   }
89
90   Consumed Emit( KeyEventType type, unsigned int keyCode, const std::string& keyName, unsigned int timeStamp, bool isText ) override
91   {
92     if (!IsUp())
93     {
94       return Consumed::NO;
95     }
96
97     unsigned int evType = 0;
98
99     switch( type )
100     {
101       case KeyEventType::KEY_PRESSED:
102       {
103         evType = 0;
104         break;
105       }
106       case KeyEventType::KEY_RELEASED:
107       {
108         evType = 1;
109         break;
110       }
111       default:
112       {
113         return Consumed::NO;
114       }
115     }
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} );
118     if( !result )
119     {
120       LOG() << result.getError().message;
121       return Consumed::NO;
122     }
123     return std::get< 0 >( result ) ? Consumed::YES : Consumed::NO;
124   }
125
126   void Pause() override
127   {
128     auto r = directReadingClient.method< DBus::ValueOrError< void >( bool ) > ( "PauseResume" ).call( true );
129     if (!r)
130     {
131       LOG() << "Direct reading command failed (" << r.getError().message << ")";
132     }
133   }
134
135   void Resume() override
136   {
137     auto r = directReadingClient.method< DBus::ValueOrError< void >( bool ) > ( "PauseResume" ).call( false );
138     if (!r)
139     {
140       LOG() << "Direct reading command failed (" << r.getError().message << ")";
141     }
142   }
143
144   void Say( const std::string& text, bool discardable, std::function< void(std::string) > callback ) override
145   {
146     auto commandId = directReadingClient.method< DBus::ValueOrError< std::string, bool, int32_t >( std::string, bool ) > ( "ReadCommand" ).call( text, discardable );
147     if ( !commandId )
148     {
149       LOG() << "Direct reading command failed (" << commandId.getError().message << ")";
150     }
151     else if( callback )
152     {
153       directReadingCallbacks.emplace( std::get< 2 >( commandId ), callback);
154     }
155   }
156
157   void ForceDown() override
158   {
159     if (data)
160     {
161       if (data->currentlyHighlightedActor && data->highlightActor)
162       {
163         data->currentlyHighlightedActor.Remove(data->highlightActor);
164       }
165       data->currentlyHighlightedActor = {};
166       data->highlightActor = {};
167     }
168     highlightedActor = {};
169     highlightClearAction = {};
170     BridgeAccessible::ForceDown();
171     registryClient = {};
172     directReadingClient = {};
173     directReadingCallbacks.clear();
174   }
175
176   void Terminate() override
177   {
178     if (data)
179     {
180       data->currentlyHighlightedActor = {};
181       data->highlightActor = {};
182     }
183     ForceDown();
184     listenOnAtspiEnabledSignalClient = {};
185     dbusServer = {};
186     con = {};
187   }
188
189   ForceUpResult ForceUp() override
190   {
191     if( BridgeAccessible::ForceUp() == ForceUpResult::ALREADY_UP )
192     {
193       return ForceUpResult::ALREADY_UP;
194     }
195
196     BridgeObject::RegisterInterfaces();
197     BridgeAccessible::RegisterInterfaces();
198     BridgeComponent::RegisterInterfaces();
199     BridgeCollection::RegisterInterfaces();
200     BridgeAction::RegisterInterfaces();
201     BridgeValue::RegisterInterfaces();
202     BridgeText::RegisterInterfaces();
203     BridgeEditableText::RegisterInterfaces();
204
205     RegisterOnBridge( &application );
206
207     registryClient = { AtspiDbusNameRegistry, AtspiDbusPathDec, AtspiDbusInterfaceDec, con };
208     directReadingClient = DBus::DBusClient{ DirectReadingDBusName, DirectReadingDBusPath, DirectReadingDBusInterface, con };
209     directReadingClient.addSignal< void(int32_t, std::string) >( "ReadingStateChanged", [=]( int32_t id, std::string readingState )
210       {
211         auto it = directReadingCallbacks.find( id );
212         if (it != directReadingCallbacks.end())
213         {
214           it->second( readingState );
215           directReadingCallbacks.erase( it );
216         }
217       }
218     );
219
220     auto proxy = DBus::DBusClient{AtspiDbusNameRegistry, AtspiDbusPathRoot, AtspiDbusInterfaceSocket, con};
221     Address root{"", "root"};
222     auto res = proxy.method< Address( Address ) >( "Embed" ).call( root );
223     if (!res)
224     {
225       LOG() << "Call to Embed failed: " << res.getError().message;
226     }
227     assert( res );
228     application.parent.SetAddress( std::move( std::get< 0 >( res ) ) );
229     if (isShown)
230     {
231       EmitActivate();
232     }
233     return ForceUpResult::JUST_STARTED;
234   }
235
236   void EmitActivate()
237   {
238     auto win = application.getActiveWindow();
239     if (win)
240     {
241       win->Emit( WindowEvent::ACTIVATE, 0 );
242     }
243   }
244
245   void EmitDeactivate()
246   {
247     auto win = application.getActiveWindow();
248     if (win)
249     {
250       win->Emit( WindowEvent::DEACTIVATE, 0 );
251     }
252   }
253
254   void ApplicationHidden() override
255   {
256     if ( isShown && IsUp() )
257     {
258       EmitDeactivate();
259     }
260     isShown = false;
261   }
262
263   void ApplicationShown() override
264   {
265     if ( !isShown && IsUp() )
266     {
267       EmitActivate();
268     }
269     isShown = true;
270   }
271
272   void Initialize() override
273   {
274     auto req = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
275     auto p = req.property< bool >( "ScreenReaderEnabled" ).get();
276     if( p )
277     {
278       screenReaderEnabled = std::get< 0 >( p );
279     }
280     p = req.property< bool >( "IsEnabled" ).get();
281     if( p )
282     {
283       isEnabled = std::get< 0 >( p );
284     }
285     if( screenReaderEnabled || isEnabled )
286     {
287       ForceUp();
288     }
289   }
290
291   bool GetScreenReaderEnabled()
292   {
293     return screenReaderEnabled;
294   }
295
296   bool GetIsEnabled()
297   {
298     return isEnabled;
299   }
300
301 };
302
303 Bridge* Bridge::GetCurrentBridge()
304 {
305   static BridgeImpl *bridge = new BridgeImpl;
306   return bridge;
307 }
308