2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/clipboard/common/clipboard-impl.h>
22 // Ecore is littered with C style cast
23 #pragma GCC diagnostic push
24 #pragma GCC diagnostic ignored "-Wold-style-cast"
28 #include <Ecore_Wl2.h>
30 #include <Ecore_Wayland.h>
33 #include <dali/public-api/object/any.h>
34 #include <dali/public-api/object/type-registry.h>
35 #include <dali/integration-api/debug.h>
38 #ifdef DALI_ELDBUS_AVAILABLE
40 #endif // DALI_ELDBUS_AVAILABLE
43 #include <dali/internal/system/common/singleton-service-impl.h>
45 #define CBHM_DBUS_OBJPATH "/org/tizen/cbhm/dbus"
46 #ifndef CBHM_DBUS_INTERFACE
47 #define CBHM_DBUS_INTERFACE "org.tizen.cbhm.dbus"
48 #endif /* CBHM_DBUS_INTERFACE */
49 #define CBHM_COUNT_ALL 0 // ATOM_INDEX_CBHM_COUNT_ALL
51 ///////////////////////////////////////////////////////////////////////////////////////////////////
53 ///////////////////////////////////////////////////////////////////////////////////////////////////
64 struct Clipboard::Impl
68 Eldbus_Object *eldbus_obj;
69 cbhm_conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
70 eldbus_obj = eldbus_object_get(cbhm_conn, CBHM_DBUS_INTERFACE, CBHM_DBUS_OBJPATH);
71 eldbus_proxy = eldbus_proxy_get(eldbus_obj, CBHM_DBUS_INTERFACE);
72 eldbus_name_owner_changed_callback_add(cbhm_conn, CBHM_DBUS_INTERFACE, NULL, cbhm_conn, EINA_TRUE);
73 eldbus_proxy_signal_handler_add(eldbus_proxy, "ItemClicked", _on_item_clicked, this);
75 mIsFirstTimeHidden = true;
81 eldbus_connection_unref(cbhm_conn);
84 Eldbus_Proxy* cbhm_proxy_get()
89 Eldbus_Connection* cbhm_connection_get()
94 void SetItem( const std::string &itemData )
96 const char *types[10] = {0, };
99 if (itemData.length() == 0)
103 mSendBuffer = itemData;
105 // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
106 types[++i] = "CLIPBOARD_BEGIN";
108 types[++i] = "text/plain;charset=utf-8";
110 // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
111 types[++i] = "CLIPBOARD_END";
113 #ifdef ECORE_WAYLAND2
114 Ecore_Wl2_Input* input = ecore_wl2_input_default_input_get( ecore_wl2_connected_display_get( NULL ) );
115 ecore_wl2_dnd_selection_set( input, types );
117 ecore_wl_dnd_selection_set( ecore_wl_input_get(), types );
123 #ifdef ECORE_WAYLAND2
124 Ecore_Wl2_Input* input = ecore_wl2_input_default_input_get( ecore_wl2_connected_display_get( NULL ) );
125 ecore_wl2_dnd_selection_get( input );
127 const char *types[10] = {0, };
130 types[++i] = "text/plain;charset=utf-8";
131 ecore_wl_dnd_selection_get(ecore_wl_input_get(), *types);
135 char *ExcuteSend( void *event )
137 #ifdef ECORE_WAYLAND2
138 Ecore_Wl2_Event_Data_Source_Send *ev = (Ecore_Wl2_Event_Data_Source_Send *)event;
140 Ecore_Wl_Event_Data_Source_Send *ev = (Ecore_Wl_Event_Data_Source_Send *)event;
143 int len_buf = mSendBuffer.length();
144 int len_remained = len_buf;
145 int len_written = 0, ret;
146 const char *buf = mSendBuffer.c_str();
148 while (len_written < len_buf)
150 ret = write(ev->fd, buf, len_remained);
151 if (ret == -1) break;
160 char *ExcuteReceive( void *event )
162 #ifdef ECORE_WAYLAND2
163 Ecore_Wl2_Event_Selection_Data_Ready *ev = (Ecore_Wl2_Event_Selection_Data_Ready *)event;
165 Ecore_Wl_Event_Selection_Data_Ready *ev = (Ecore_Wl_Event_Selection_Data_Ready *)event;
168 return (char *)ev->data;
173 Eldbus_Message *reply, *req;
174 const char *errname = NULL, *errmsg = NULL;
177 if (!(req = eldbus_proxy_method_call_new(eldbus_proxy, "CbhmGetCount")))
179 DALI_LOG_ERROR("Failed to create method call on org.freedesktop.DBus.Properties.Get");
183 eldbus_message_ref(req);
184 eldbus_message_arguments_append(req, "i", CBHM_COUNT_ALL) ;
185 reply = eldbus_proxy_send_and_block(eldbus_proxy, req, 100);
186 if (!reply || eldbus_message_error_get(reply, &errname, &errmsg))
188 DALI_LOG_ERROR("Unable to call method org.freedesktop.DBus.Properties.Get: %s %s",
190 eldbus_message_unref(req);
193 eldbus_message_unref(reply);
198 if (!eldbus_message_arguments_get(reply, "i", &count))
200 DALI_LOG_ERROR("Cannot get arguments from eldbus");
201 eldbus_message_unref(req);
202 eldbus_message_unref(reply);
206 eldbus_message_unref(req);
207 eldbus_message_unref(reply);
208 DALI_LOG_ERROR("cbhm item count(%d)", count);
214 eldbus_proxy_call(cbhm_proxy_get(), "CbhmShow", NULL, NULL, -1, "s", "0");
215 mIsFirstTimeHidden = true;
219 void HideClipboard( bool skipFirstHide )
221 if ( skipFirstHide && mIsFirstTimeHidden )
223 mIsFirstTimeHidden = false;
226 eldbus_proxy_call(cbhm_proxy_get(), "CbhmHide", NULL, NULL, -1, "");
227 mIsFirstTimeHidden = false;
231 bool IsVisible() const
236 static void _on_item_clicked(void *data, const Eldbus_Message *msg EINA_UNUSED)
238 static_cast<Clipboard::Impl*>(data)->RequestItem();
241 Eldbus_Proxy *eldbus_proxy;
242 Eldbus_Connection *cbhm_conn;
244 std::string mSendBuffer;
246 bool mIsFirstTimeHidden;
249 Clipboard::Clipboard(Impl* impl)
254 Clipboard::~Clipboard()
259 Dali::Clipboard Clipboard::Get()
261 Dali::Clipboard clipboard;
263 Dali::SingletonService service( SingletonService::Get() );
266 // Check whether the singleton is already created
267 Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::Clipboard ) );
270 // If so, downcast the handle
271 clipboard = Dali::Clipboard( dynamic_cast< Clipboard* >( handle.GetObjectPtr() ) );
275 Clipboard::Impl* impl( new Clipboard::Impl() );
276 clipboard = Dali::Clipboard( new Clipboard(impl) );
277 service.Register( typeid(Dali::Clipboard), clipboard );
284 bool Clipboard::SetItem(const std::string &itemData )
286 mImpl->SetItem( itemData );
291 * Request clipboard service to give an item
293 void Clipboard::RequestItem()
295 mImpl->RequestItem();
299 * Get number of items in clipboard
301 unsigned int Clipboard::NumberOfItems()
303 return mImpl->GetCount();
306 void Clipboard::ShowClipboard()
308 mImpl->ShowClipboard();
311 void Clipboard::HideClipboard(bool skipFirstHide)
313 mImpl->HideClipboard(skipFirstHide);
316 bool Clipboard::IsVisible() const
318 return mImpl->IsVisible();
321 char* Clipboard::ExcuteBuffered( bool type, void *event )
323 return (type ? mImpl->ExcuteSend( event ) : mImpl->ExcuteReceive( event ));
326 } // namespace Adaptor
328 } // namespace Internal
332 #pragma GCC diagnostic pop