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