Revert "[Tizen](ATSPI) squashed implementation"
[platform/core/uifw/dali-adaptor.git] / dali / internal / clipboard / tizen-wayland / clipboard-impl-ecore-wl.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 #include <dali/internal/clipboard/common/clipboard-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/internal/system/linux/dali-ecore.h>
23
24 #ifdef ECORE_WAYLAND2
25 #include <dali/internal/adaptor/tizen-wayland/dali-ecore-wl2.h>
26 #else
27 #include <dali/internal/adaptor/tizen-wayland/dali-ecore-wayland.h>
28 #endif
29
30 #include <dali/public-api/object/any.h>
31 #include <dali/public-api/object/type-registry.h>
32 #include <dali/integration-api/debug.h>
33 #include <unistd.h>
34
35 #ifdef DALI_ELDBUS_AVAILABLE
36 #include <Eldbus.h>
37 #endif // DALI_ELDBUS_AVAILABLE
38
39 // INTERNAL INCLUDES
40 #include <dali/internal/system/common/singleton-service-impl.h>
41
42 #define CBHM_DBUS_OBJPATH "/org/tizen/cbhm/dbus"
43 #ifndef CBHM_DBUS_INTERFACE
44 #define CBHM_DBUS_INTERFACE "org.tizen.cbhm.dbus"
45 #endif /* CBHM_DBUS_INTERFACE */
46 #define CBHM_COUNT_ALL 0    // ATOM_INDEX_CBHM_COUNT_ALL
47
48 ///////////////////////////////////////////////////////////////////////////////////////////////////
49 // Clipboard
50 ///////////////////////////////////////////////////////////////////////////////////////////////////
51
52 namespace Dali
53 {
54
55 namespace Internal
56 {
57
58 namespace Adaptor
59 {
60
61 struct Clipboard::Impl
62 {
63   Impl()
64   {
65     Eldbus_Object *eldbus_obj;
66     cbhm_conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
67     eldbus_obj = eldbus_object_get(cbhm_conn, CBHM_DBUS_INTERFACE, CBHM_DBUS_OBJPATH);
68     eldbus_proxy = eldbus_proxy_get(eldbus_obj, CBHM_DBUS_INTERFACE);
69     eldbus_name_owner_changed_callback_add(cbhm_conn, CBHM_DBUS_INTERFACE, NULL, cbhm_conn, EINA_TRUE);
70     eldbus_proxy_signal_handler_add(eldbus_proxy, "ItemClicked", _on_item_clicked, this);
71     mVisible = false;
72     mIsFirstTimeHidden = true;
73   }
74
75   ~Impl()
76   {
77     if (cbhm_conn)
78       eldbus_connection_unref(cbhm_conn);
79   }
80
81   Eldbus_Proxy* cbhm_proxy_get()
82   {
83     return eldbus_proxy;
84   }
85
86   Eldbus_Connection* cbhm_connection_get()
87   {
88     return cbhm_conn;
89   }
90
91   void SetItem( const std::string &itemData )
92   {
93     const char *types[10] = {0, };
94     int i = -1;
95
96     if (itemData.length() == 0)
97     {
98       return;
99     }
100     mSendBuffer = itemData;
101
102     // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
103     types[++i] = "CLIPBOARD_BEGIN";
104
105     types[++i] = "text/plain;charset=utf-8";
106
107     // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
108     types[++i] = "CLIPBOARD_END";
109
110 #ifdef ECORE_WAYLAND2
111     Ecore_Wl2_Input* input = ecore_wl2_input_default_input_get( ecore_wl2_connected_display_get( NULL ) );
112     ecore_wl2_dnd_selection_set( input, types );
113 #else
114     ecore_wl_dnd_selection_set( ecore_wl_input_get(), types );
115 #endif
116   }
117
118   void RequestItem()
119   {
120 #ifdef ECORE_WAYLAND2
121     Ecore_Wl2_Input* input = ecore_wl2_input_default_input_get( ecore_wl2_connected_display_get( NULL ) );
122     ecore_wl2_dnd_selection_get( input );
123 #else
124     const char *types[10] = {0, };
125     int i = -1;
126
127     types[++i] = "text/plain;charset=utf-8";
128     ecore_wl_dnd_selection_get(ecore_wl_input_get(), *types);
129 #endif
130   }
131
132   char *ExcuteSend( void *event )
133   {
134 #ifdef ECORE_WAYLAND2
135     Ecore_Wl2_Event_Data_Source_Send *ev = reinterpret_cast<Ecore_Wl2_Event_Data_Source_Send *>( event );
136 #else
137     Ecore_Wl_Event_Data_Source_Send *ev = reinterpret_cast<Ecore_Wl_Event_Data_Source_Send *>( event );
138 #endif
139
140     int len_buf = mSendBuffer.length();
141     int len_remained = len_buf;
142     int len_written = 0, ret;
143     const char *buf = mSendBuffer.c_str();
144
145     while (len_written < len_buf)
146     {
147        ret = write(ev->fd, buf, len_remained);
148        if (ret == -1) break;
149        buf += ret;
150        len_written += ret;
151        len_remained -= ret;
152     }
153     close(ev->fd);
154     return NULL;
155   }
156
157   char *ExcuteReceive( void *event )
158   {
159 #ifdef ECORE_WAYLAND2
160     Ecore_Wl2_Event_Selection_Data_Ready *ev = reinterpret_cast<Ecore_Wl2_Event_Selection_Data_Ready *>( event );
161 #else
162     Ecore_Wl_Event_Selection_Data_Ready *ev = reinterpret_cast<Ecore_Wl_Event_Selection_Data_Ready *>( event );
163 #endif
164
165     return reinterpret_cast<char *>( ev->data );
166   }
167
168   int GetCount()
169   {
170     Eldbus_Message *reply, *req;
171     const char *errname = NULL, *errmsg = NULL;
172     int count = -1;
173
174     if (!(req = eldbus_proxy_method_call_new(eldbus_proxy, "CbhmGetCount")))
175     {
176       DALI_LOG_ERROR("Failed to create method call on org.freedesktop.DBus.Properties.Get");
177       return -1;
178     }
179
180     eldbus_message_ref(req);
181     eldbus_message_arguments_append(req, "i", CBHM_COUNT_ALL) ;
182     reply = eldbus_proxy_send_and_block(eldbus_proxy, req, 100);
183     if (!reply || eldbus_message_error_get(reply, &errname, &errmsg))
184     {
185       DALI_LOG_ERROR("Unable to call method org.freedesktop.DBus.Properties.Get: %s %s",
186       errname, errmsg);
187       eldbus_message_unref(req);
188       if( reply )
189       {
190         eldbus_message_unref(reply);
191       }
192       return -1;
193     }
194
195     if (!eldbus_message_arguments_get(reply, "i", &count))
196     {
197       DALI_LOG_ERROR("Cannot get arguments from eldbus");
198       eldbus_message_unref(req);
199       eldbus_message_unref(reply);
200       return -1;
201     }
202
203     eldbus_message_unref(req);
204     eldbus_message_unref(reply);
205     DALI_LOG_ERROR("cbhm item count(%d)", count);
206     return count;
207   }
208
209   void ShowClipboard()
210   {
211     eldbus_proxy_call(cbhm_proxy_get(), "CbhmShow", NULL, NULL, -1, "s", "0");
212     mIsFirstTimeHidden = true;
213     mVisible = true;
214   }
215
216   void HideClipboard( bool skipFirstHide )
217   {
218     if ( skipFirstHide && mIsFirstTimeHidden )
219     {
220       mIsFirstTimeHidden = false;
221       return;
222     }
223     eldbus_proxy_call(cbhm_proxy_get(), "CbhmHide", NULL, NULL, -1, "");
224     mIsFirstTimeHidden = false;
225     mVisible = false;
226   }
227
228   bool IsVisible() const
229   {
230     return mVisible;
231   }
232
233   static void _on_item_clicked(void *data, const Eldbus_Message *msg EINA_UNUSED)
234   {
235     static_cast<Clipboard::Impl*>(data)->RequestItem();
236   }
237
238   Eldbus_Proxy *eldbus_proxy;
239   Eldbus_Connection *cbhm_conn;
240
241   std::string mSendBuffer;
242   bool mVisible;
243   bool mIsFirstTimeHidden;
244 };
245
246 Clipboard::Clipboard(Impl* impl)
247 : mImpl(impl)
248 {
249 }
250
251 Clipboard::~Clipboard()
252 {
253   delete mImpl;
254 }
255
256 Dali::Clipboard Clipboard::Get()
257 {
258   Dali::Clipboard clipboard;
259
260   Dali::SingletonService service( SingletonService::Get() );
261   if ( service )
262   {
263     // Check whether the singleton is already created
264     Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::Clipboard ) );
265     if(handle)
266     {
267       // If so, downcast the handle
268       clipboard = Dali::Clipboard( dynamic_cast< Clipboard* >( handle.GetObjectPtr() ) );
269     }
270     else
271     {
272       Clipboard::Impl* impl( new Clipboard::Impl() );
273       clipboard = Dali::Clipboard( new Clipboard(impl) );
274       service.Register( typeid(Dali::Clipboard), clipboard );
275     }
276   }
277
278   return clipboard;
279 }
280
281 bool Clipboard::SetItem(const std::string &itemData )
282 {
283   mImpl->SetItem( itemData );
284   return true;
285 }
286
287 /*
288  * Request clipboard service to give an item
289  */
290 void Clipboard::RequestItem()
291 {
292   mImpl->RequestItem();
293 }
294
295 /*
296  * Get number of items in clipboard
297  */
298 unsigned int Clipboard::NumberOfItems()
299 {
300   return mImpl->GetCount();
301 }
302
303 void Clipboard::ShowClipboard()
304 {
305   mImpl->ShowClipboard();
306 }
307
308 void Clipboard::HideClipboard(bool skipFirstHide)
309 {
310   mImpl->HideClipboard(skipFirstHide);
311 }
312
313 bool Clipboard::IsVisible() const
314 {
315   return mImpl->IsVisible();
316 }
317
318 char* Clipboard::ExcuteBuffered( bool type, void *event )
319 {
320   return (type ?  mImpl->ExcuteSend( event ) : mImpl->ExcuteReceive( event ));
321 }
322
323 } // namespace Adaptor
324
325 } // namespace Internal
326
327 } // namespace Dali