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