Fix build error when using --enable-debug option
[platform/core/uifw/dali-adaptor.git] / adaptors / ecore / wayland / clipboard-impl-ecore-wl.cpp
1 /*
2  * Copyright (c) 2014 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 #include <Ecore.h>
23 #include <Ecore_Wayland.h>
24 #include <dali/public-api/object/any.h>
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/integration-api/debug.h>
27 #include <unistd.h>
28
29 #ifdef DALI_ELDBUS_AVAILABLE
30 #include <Eldbus.h>
31 #endif // DALI_ELDBUS_AVAILABLE
32
33 // INTERNAL INCLUDES
34 #include <singleton-service-impl.h>
35
36 #define CBHM_DBUS_OBJPATH "/org/tizen/cbhm/dbus"
37 #ifndef CBHM_DBUS_INTERFACE
38 #define CBHM_DBUS_INTERFACE "org.tizen.cbhm.dbus"
39 #endif /* CBHM_DBUS_INTERFACE */
40 #define CBHM_COUNT_ALL 0    // ATOM_INDEX_CBHM_COUNT_ALL
41
42 ///////////////////////////////////////////////////////////////////////////////////////////////////
43 // Clipboard
44 ///////////////////////////////////////////////////////////////////////////////////////////////////
45
46 namespace Dali
47 {
48
49 namespace Internal
50 {
51
52 namespace Adaptor
53 {
54
55 struct Clipboard::Impl
56 {
57   Impl()
58   {
59     Eldbus_Object *eldbus_obj;
60     cbhm_conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
61     eldbus_obj = eldbus_object_get(cbhm_conn, CBHM_DBUS_INTERFACE, CBHM_DBUS_OBJPATH);
62     eldbus_proxy = eldbus_proxy_get(eldbus_obj, CBHM_DBUS_INTERFACE);
63     eldbus_name_owner_changed_callback_add(cbhm_conn, CBHM_DBUS_INTERFACE, NULL, cbhm_conn, EINA_TRUE);
64     eldbus_proxy_signal_handler_add(eldbus_proxy, "ItemClicked", _on_item_clicked, this);
65     mVisible = false;
66     mIsFirstTimeHidden = true;
67   }
68
69   ~Impl()
70   {
71     if (cbhm_conn)
72       eldbus_connection_unref(cbhm_conn);
73   }
74
75   Eldbus_Proxy* cbhm_proxy_get()
76   {
77     return eldbus_proxy;
78   }
79
80   Eldbus_Connection* cbhm_connection_get()
81   {
82     return cbhm_conn;
83   }
84
85   void SetItem( const std::string &itemData )
86   {
87     const char *types[10] = {0, };
88     int i = -1;
89
90     if (itemData.length() == 0)
91     {
92       return;
93     }
94     mSendBuffer = itemData;
95
96     // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
97     types[++i] = "CLIPBOARD_BEGIN";
98
99     types[++i] = "application/x-elementary-markup";
100     types[++i] = "text/plain";
101     types[++i] = "text/plain;charset=utf-8";
102
103     // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
104     types[++i] = "CLIPBOARD_END";
105     ecore_wl_dnd_selection_set(ecore_wl_input_get(), types);
106   }
107
108   void RequestItem()
109   {
110     const char *types[10] = {0, };
111     int i = -1;
112
113     types[++i] = "application/x-elementary-markup";
114     types[++i] = "text/plain";
115     types[++i] = "text/plain;charset=utf-8";
116     ecore_wl_dnd_selection_get(ecore_wl_input_get(), *types);
117   }
118
119   char *ExcuteSend( void *event )
120   {
121     Ecore_Wl_Event_Data_Source_Send *ev = (Ecore_Wl_Event_Data_Source_Send *)event;
122     int len_buf = mSendBuffer.length();
123     int len_remained = len_buf;
124     int len_written = 0, ret;
125     const char *buf = mSendBuffer.c_str();
126
127     while (len_written < len_buf)
128     {
129        ret = write(ev->fd, buf, len_remained);
130        if (ret == -1) break;
131        buf += ret;
132        len_written += ret;
133        len_remained -= ret;
134     }
135     close(ev->fd);
136     return NULL;
137   }
138
139   char *ExcuteReceive( void *event )
140   {
141     Ecore_Wl_Event_Selection_Data_Ready *ev = (Ecore_Wl_Event_Selection_Data_Ready *)event;
142
143     return (char *)ev->data;
144   }
145
146   int GetCount()
147   {
148     Eldbus_Message *reply, *req;
149     const char *errname = NULL, *errmsg = NULL;
150     int count = -1;
151
152     if (!(req = eldbus_proxy_method_call_new(eldbus_proxy, "CbhmGetCount")))
153     {
154       DALI_LOG_ERROR("Failed to create method call on org.freedesktop.DBus.Properties.Get");
155       return -1;
156     }
157
158     eldbus_message_ref(req);
159     eldbus_message_arguments_append(req, "i", CBHM_COUNT_ALL) ;
160     reply = eldbus_proxy_send_and_block(eldbus_proxy, req, 100);
161     if (!reply || eldbus_message_error_get(reply, &errname, &errmsg))
162     {
163       DALI_LOG_ERROR("Unable to call method org.freedesktop.DBus.Properties.Get: %s %s",
164       errname, errmsg);
165       eldbus_message_unref(req);
166       return -1;
167     }
168
169     if (!eldbus_message_arguments_get(reply, "i", &count))
170     {
171       DALI_LOG_ERROR("Cannot get arguments from eldbus");
172       eldbus_message_unref(req);
173       return -1;
174     }
175
176     eldbus_message_unref(req);
177     DALI_LOG_ERROR("cbhm item count(%d)", count);
178     return count;
179   }
180
181   void ShowClipboard()
182   {
183     eldbus_proxy_call(cbhm_proxy_get(), "CbhmShow", NULL, NULL, -1, "s", "0");
184     mIsFirstTimeHidden = true;
185     mVisible = true;
186   }
187
188   void HideClipboard( bool skipFirstHide )
189   {
190     if ( skipFirstHide && mIsFirstTimeHidden )
191     {
192       mIsFirstTimeHidden = false;
193       return;
194     }
195     eldbus_proxy_call(cbhm_proxy_get(), "CbhmHide", NULL, NULL, -1, "");
196     mIsFirstTimeHidden = false;
197     mVisible = false;
198   }
199
200   bool IsVisible() const
201   {
202     return mVisible;
203   }
204
205   static void _on_item_clicked(void *data, const Eldbus_Message *msg EINA_UNUSED)
206   {
207     static_cast<Clipboard::Impl*>(data)->RequestItem();
208   }
209
210   Eldbus_Proxy *eldbus_proxy;
211   Eldbus_Connection *cbhm_conn;
212
213   std::string mSendBuffer;
214   bool mVisible;
215   bool mIsFirstTimeHidden;
216 };
217
218 Clipboard::Clipboard(Impl* impl)
219 : mImpl(impl)
220 {
221 }
222
223 Clipboard::~Clipboard()
224 {
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