2 * Copyright (c) 2019 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 #include <dali/internal/system/linux/dali-ecore.h>
25 #include <dali/internal/adaptor/tizen-wayland/dali-ecore-wl2.h>
27 #include <dali/internal/adaptor/tizen-wayland/dali-ecore-wayland.h>
30 #include <dali/public-api/object/any.h>
31 #include <dali/public-api/object/type-registry.h>
32 #include <dali/integration-api/debug.h>
35 #ifdef DALI_ELDBUS_AVAILABLE
37 #endif // DALI_ELDBUS_AVAILABLE
40 #include <dali/internal/system/common/singleton-service-impl.h>
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
48 ///////////////////////////////////////////////////////////////////////////////////////////////////
50 ///////////////////////////////////////////////////////////////////////////////////////////////////
61 struct Clipboard::Impl
65 Eldbus_Object *eldbus_obj;
67 cbhm_conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
68 eldbus_obj = eldbus_object_get(cbhm_conn, CBHM_DBUS_INTERFACE, CBHM_DBUS_OBJPATH);
69 eldbus_proxy = eldbus_proxy_get(eldbus_obj, CBHM_DBUS_INTERFACE);
70 eldbus_name_owner_changed_callback_add(cbhm_conn, CBHM_DBUS_INTERFACE, NULL, cbhm_conn, EINA_TRUE);
71 eldbus_proxy_signal_handler_add(eldbus_proxy, "ItemClicked", _on_item_clicked, this);
73 mIsFirstTimeHidden = true;
79 eldbus_connection_unref(cbhm_conn);
83 Eldbus_Proxy* cbhm_proxy_get()
88 Eldbus_Connection* cbhm_connection_get()
93 void SetItem( const std::string &itemData )
95 const char *types[10] = {0, };
98 if (itemData.length() == 0)
102 mSendBuffer = itemData;
104 // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
105 types[++i] = "CLIPBOARD_BEGIN";
107 types[++i] = "text/plain;charset=utf-8";
109 // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
110 types[++i] = "CLIPBOARD_END";
112 #ifdef ECORE_WAYLAND2
113 Ecore_Wl2_Input* input = ecore_wl2_input_default_input_get( ecore_wl2_connected_display_get( NULL ) );
114 ecore_wl2_dnd_selection_set( input, types );
116 ecore_wl_dnd_selection_set( ecore_wl_input_get(), types );
122 #ifdef ECORE_WAYLAND2
123 Ecore_Wl2_Input* input = ecore_wl2_input_default_input_get( ecore_wl2_connected_display_get( NULL ) );
124 ecore_wl2_dnd_selection_get( input );
126 const char *types[10] = {0, };
129 types[++i] = "text/plain;charset=utf-8";
130 ecore_wl_dnd_selection_get(ecore_wl_input_get(), *types);
134 char *ExcuteSend( void *event )
136 #ifdef ECORE_WAYLAND2
137 Ecore_Wl2_Event_Data_Source_Send *ev = reinterpret_cast<Ecore_Wl2_Event_Data_Source_Send *>( event );
139 Ecore_Wl_Event_Data_Source_Send *ev = reinterpret_cast<Ecore_Wl_Event_Data_Source_Send *>( event );
142 int len_buf = mSendBuffer.length();
143 int len_remained = len_buf;
144 int len_written = 0, ret;
145 const char *buf = mSendBuffer.c_str();
147 while (len_written < len_buf)
149 ret = write(ev->fd, buf, len_remained);
150 if (ret == -1) break;
159 char *ExcuteReceive( void *event )
161 #ifdef ECORE_WAYLAND2
162 Ecore_Wl2_Event_Selection_Data_Ready *ev = reinterpret_cast<Ecore_Wl2_Event_Selection_Data_Ready *>( event );
164 Ecore_Wl_Event_Selection_Data_Ready *ev = reinterpret_cast<Ecore_Wl_Event_Selection_Data_Ready *>( event );
167 return reinterpret_cast<char *>( ev->data );
172 Eldbus_Message *reply, *req;
173 const char *errname = NULL, *errmsg = NULL;
176 if (!(req = eldbus_proxy_method_call_new(eldbus_proxy, "CbhmGetCount")))
178 DALI_LOG_ERROR("Failed to create method call on org.freedesktop.DBus.Properties.Get");
182 eldbus_message_ref(req);
183 eldbus_message_arguments_append(req, "i", CBHM_COUNT_ALL) ;
184 reply = eldbus_proxy_send_and_block(eldbus_proxy, req, 100);
185 if (!reply || eldbus_message_error_get(reply, &errname, &errmsg))
187 DALI_LOG_ERROR("Unable to call method org.freedesktop.DBus.Properties.Get: %s %s",
189 eldbus_message_unref(req);
192 eldbus_message_unref(reply);
197 if (!eldbus_message_arguments_get(reply, "i", &count))
199 DALI_LOG_ERROR("Cannot get arguments from eldbus");
200 eldbus_message_unref(req);
201 eldbus_message_unref(reply);
205 eldbus_message_unref(req);
206 eldbus_message_unref(reply);
207 DALI_LOG_ERROR("cbhm item count(%d)", count);
213 eldbus_proxy_call(cbhm_proxy_get(), "CbhmShow", NULL, NULL, -1, "s", "0");
214 mIsFirstTimeHidden = true;
218 void HideClipboard( bool skipFirstHide )
220 if ( skipFirstHide && mIsFirstTimeHidden )
222 mIsFirstTimeHidden = false;
225 eldbus_proxy_call(cbhm_proxy_get(), "CbhmHide", NULL, NULL, -1, "");
226 mIsFirstTimeHidden = false;
230 bool IsVisible() const
235 static void _on_item_clicked(void *data, const Eldbus_Message *msg EINA_UNUSED)
237 static_cast<Clipboard::Impl*>(data)->RequestItem();
240 Eldbus_Proxy *eldbus_proxy;
241 Eldbus_Connection *cbhm_conn;
243 std::string mSendBuffer;
245 bool mIsFirstTimeHidden;
248 Clipboard::Clipboard(Impl* impl)
253 Clipboard::~Clipboard()
258 Dali::Clipboard Clipboard::Get()
260 Dali::Clipboard clipboard;
262 Dali::SingletonService service( SingletonService::Get() );
265 // Check whether the singleton is already created
266 Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::Clipboard ) );
269 // If so, downcast the handle
270 clipboard = Dali::Clipboard( dynamic_cast< Clipboard* >( handle.GetObjectPtr() ) );
274 Clipboard::Impl* impl( new Clipboard::Impl() );
275 clipboard = Dali::Clipboard( new Clipboard(impl) );
276 service.Register( typeid(Dali::Clipboard), clipboard );
283 bool Clipboard::SetItem(const std::string &itemData )
285 mImpl->SetItem( itemData );
290 * Request clipboard service to give an item
292 void Clipboard::RequestItem()
294 mImpl->RequestItem();
298 * Get number of items in clipboard
300 unsigned int Clipboard::NumberOfItems()
302 return mImpl->GetCount();
305 void Clipboard::ShowClipboard()
307 mImpl->ShowClipboard();
310 void Clipboard::HideClipboard(bool skipFirstHide)
312 mImpl->HideClipboard(skipFirstHide);
315 bool Clipboard::IsVisible() const
317 return mImpl->IsVisible();
320 char* Clipboard::ExcuteBuffered( bool type, void *event )
322 return (type ? mImpl->ExcuteSend( event ) : mImpl->ExcuteReceive( event ));
325 } // namespace Adaptor
327 } // namespace Internal