2 * Copyright (c) 2014 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 "clipboard-impl.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>
29 #ifdef DALI_ELDBUS_AVAILABLE
31 #endif // DALI_ELDBUS_AVAILABLE
34 #include <singleton-service-impl.h>
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
42 ///////////////////////////////////////////////////////////////////////////////////////////////////
44 ///////////////////////////////////////////////////////////////////////////////////////////////////
55 struct Clipboard::Impl
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);
66 mIsFirstTimeHidden = true;
72 eldbus_connection_unref(cbhm_conn);
75 Eldbus_Proxy* cbhm_proxy_get()
80 Eldbus_Connection* cbhm_connection_get()
85 void SetItem( const std::string &itemData )
87 const char *types[10] = {0, };
90 if (itemData.length() == 0)
94 mSendBuffer = itemData;
96 // ELM_SEL_TYPE_CLIPBOARD - To distinguish clipboard selection in cbhm
97 types[++i] = "CLIPBOARD_BEGIN";
99 types[++i] = "application/x-elementary-markup";
100 types[++i] = "text/plain";
101 types[++i] = "text/plain;charset=utf-8";
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);
110 const char *types[10] = {0, };
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);
119 char *ExcuteSend( void *event )
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();
127 while (len_written < len_buf)
129 ret = write(ev->fd, buf, len_remained);
130 if (ret == -1) break;
139 char *ExcuteReceive( void *event )
141 Ecore_Wl_Event_Selection_Data_Ready *ev = (Ecore_Wl_Event_Selection_Data_Ready *)event;
143 return (char *)ev->data;
148 Eldbus_Message *reply, *req;
149 const char *errname = NULL, *errmsg = NULL;
152 if (!(req = eldbus_proxy_method_call_new(eldbus_proxy, "CbhmGetCount")))
154 DALI_LOG_ERROR("Failed to create method call on org.freedesktop.DBus.Properties.Get");
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))
163 DALI_LOG_ERROR("Unable to call method org.freedesktop.DBus.Properties.Get: %s %s",
165 eldbus_message_unref(req);
169 if (!eldbus_message_arguments_get(reply, "i", &count))
171 DALI_LOG_ERROR("Cannot get arguments from eldbus");
172 eldbus_message_unref(req);
176 eldbus_message_unref(req);
177 DALI_LOG_ERROR("cbhm item count(%d)", count);
183 eldbus_proxy_call(cbhm_proxy_get(), "CbhmShow", NULL, NULL, -1, "s", "0");
184 mIsFirstTimeHidden = true;
188 void HideClipboard( bool skipFirstHide )
190 if ( skipFirstHide && mIsFirstTimeHidden )
192 mIsFirstTimeHidden = false;
195 eldbus_proxy_call(cbhm_proxy_get(), "CbhmHide", NULL, NULL, -1, "");
196 mIsFirstTimeHidden = false;
200 bool IsVisible() const
205 static void _on_item_clicked(void *data, const Eldbus_Message *msg EINA_UNUSED)
207 static_cast<Clipboard::Impl*>(data)->RequestItem();
210 Eldbus_Proxy *eldbus_proxy;
211 Eldbus_Connection *cbhm_conn;
213 std::string mSendBuffer;
215 bool mIsFirstTimeHidden;
218 Clipboard::Clipboard(Impl* impl)
223 Clipboard::~Clipboard()
227 Dali::Clipboard Clipboard::Get()
229 Dali::Clipboard clipboard;
231 Dali::SingletonService service( SingletonService::Get() );
234 // Check whether the singleton is already created
235 Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::Clipboard ) );
238 // If so, downcast the handle
239 clipboard = Dali::Clipboard( dynamic_cast< Clipboard* >( handle.GetObjectPtr() ) );
243 Clipboard::Impl* impl( new Clipboard::Impl() );
244 clipboard = Dali::Clipboard( new Clipboard(impl) );
245 service.Register( typeid(Dali::Clipboard), clipboard );
252 bool Clipboard::SetItem(const std::string &itemData )
254 mImpl->SetItem( itemData );
259 * Request clipboard service to give an item
261 void Clipboard::RequestItem()
263 mImpl->RequestItem();
267 * Get number of items in clipboard
269 unsigned int Clipboard::NumberOfItems()
271 return mImpl->GetCount();
274 void Clipboard::ShowClipboard()
276 mImpl->ShowClipboard();
279 void Clipboard::HideClipboard(bool skipFirstHide)
281 mImpl->HideClipboard(skipFirstHide);
284 bool Clipboard::IsVisible() const
286 return mImpl->IsVisible();
289 char* Clipboard::ExcuteBuffered( bool type, void *event )
291 return (type ? mImpl->ExcuteSend( event ) : mImpl->ExcuteReceive( event ));
294 } // namespace Adaptor
296 } // namespace Internal