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"
26 #include <dali/public-api/object/any.h>
27 #include <dali/public-api/object/type-registry.h>
28 #include <dali/integration-api/debug.h>
31 #ifdef DALI_ELDBUS_AVAILABLE
33 #endif // DALI_ELDBUS_AVAILABLE
34 #include <Ecore_Wl2.h>
37 #include <dali/internal/system/common/singleton-service-impl.h>
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
45 ///////////////////////////////////////////////////////////////////////////////////////////////////
47 ///////////////////////////////////////////////////////////////////////////////////////////////////
58 struct Clipboard::Impl
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);
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);
77 eldbus_connection_unref(cbhm_conn);
80 Eldbus_Proxy* cbhm_proxy_get()
85 Eldbus_Connection* cbhm_connection_get()
90 void SetItem( const std::string &itemData )
92 const char *types[10] = {0, };
95 if (itemData.length() == 0)
99 mSendBuffer = itemData;
101 // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
102 types[++i] = "CLIPBOARD_BEGIN";
104 types[++i] = "text/plain;charset=utf-8";
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);
113 ecore_wl2_dnd_selection_get(ecore_wl2_input);
116 char *ExcuteSend( void *event )
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();
124 while (len_written < len_buf)
126 ret = write(ev->fd, buf, len_remained);
127 if (ret == -1) break;
136 char *ExcuteReceive( void *event )
138 Ecore_Wl2_Event_Selection_Data_Ready *ev = (Ecore_Wl2_Event_Selection_Data_Ready *)event;
140 return (char *)ev->data;
145 Eldbus_Message *reply, *req;
146 const char *errname = NULL, *errmsg = NULL;
149 if (!(req = eldbus_proxy_method_call_new(eldbus_proxy, "CbhmGetCount")))
151 DALI_LOG_ERROR("Failed to create method call on org.freedesktop.DBus.Properties.Get");
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))
160 DALI_LOG_ERROR("Unable to call method org.freedesktop.DBus.Properties.Get: %s %s",
162 eldbus_message_unref(req);
166 if (!eldbus_message_arguments_get(reply, "i", &count))
168 DALI_LOG_ERROR("Cannot get arguments from eldbus");
169 eldbus_message_unref(req);
173 eldbus_message_unref(req);
174 DALI_LOG_ERROR("cbhm item count(%d)", count);
180 eldbus_proxy_call(cbhm_proxy_get(), "CbhmShow", NULL, NULL, -1, "s", "0");
181 mIsFirstTimeHidden = true;
185 void HideClipboard( bool skipFirstHide )
187 if ( skipFirstHide && mIsFirstTimeHidden )
189 mIsFirstTimeHidden = false;
192 eldbus_proxy_call(cbhm_proxy_get(), "CbhmHide", NULL, NULL, -1, "");
193 mIsFirstTimeHidden = false;
197 bool IsVisible() const
202 static void _on_item_clicked(void *data, const Eldbus_Message *msg EINA_UNUSED)
204 static_cast<Clipboard::Impl*>(data)->RequestItem();
207 Eldbus_Proxy *eldbus_proxy;
208 Eldbus_Connection *cbhm_conn;
210 std::string mSendBuffer;
212 bool mIsFirstTimeHidden;
214 Ecore_Wl2_Display* ecore_wl2_display;
215 Ecore_Wl2_Input* ecore_wl2_input;
218 Clipboard::Clipboard(Impl* impl)
223 Clipboard::~Clipboard()
228 Dali::Clipboard Clipboard::Get()
230 Dali::Clipboard clipboard;
232 Dali::SingletonService service( SingletonService::Get() );
235 // Check whether the singleton is already created
236 Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::Clipboard ) );
239 // If so, downcast the handle
240 clipboard = Dali::Clipboard( dynamic_cast< Clipboard* >( handle.GetObjectPtr() ) );
244 Clipboard::Impl* impl( new Clipboard::Impl() );
245 clipboard = Dali::Clipboard( new Clipboard(impl) );
246 service.Register( typeid(Dali::Clipboard), clipboard );
253 bool Clipboard::SetItem(const std::string &itemData )
255 mImpl->SetItem( itemData );
260 * Request clipboard service to give an item
262 void Clipboard::RequestItem()
264 mImpl->RequestItem();
268 * Get number of items in clipboard
270 unsigned int Clipboard::NumberOfItems()
272 return mImpl->GetCount();
275 void Clipboard::ShowClipboard()
277 mImpl->ShowClipboard();
280 void Clipboard::HideClipboard(bool skipFirstHide)
282 mImpl->HideClipboard(skipFirstHide);
285 bool Clipboard::IsVisible() const
287 return mImpl->IsVisible();
290 char* Clipboard::ExcuteBuffered( bool type, void *event )
292 return (type ? mImpl->ExcuteSend( event ) : mImpl->ExcuteReceive( event ));
295 } // namespace Adaptor
297 } // namespace Internal
301 #pragma GCC diagnostic pop