[dali_2.3.26] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / internal / clipboard / ubuntu-x11 / clipboard-impl-x.cpp
1 /*
2  * Copyright (c) 2023 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 <dali/internal/clipboard/common/clipboard-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/internal/system/linux/dali-ecore-x.h>
24 #include <dali/public-api/adaptor-framework/timer.h>
25 #include <dali/public-api/object/any.h>
26 #include <dali/public-api/object/type-registry.h>
27
28 // INTERNAL INCLUDES
29 #include <dali/devel-api/common/singleton-service.h>
30 #include <dali/internal/adaptor/common/adaptor-impl.h>
31 #include <dali/internal/window-system/ubuntu-x11/window-interface-ecore-x.h>
32
33 namespace Dali
34 {
35 namespace Internal
36 {
37 namespace Adaptor
38 {
39 struct Clipboard::Impl
40 {
41   Impl(Ecore_X_Window ecoreXwin)
42   {
43     mApplicationWindow = ecoreXwin;
44   }
45
46   bool HasType(const std::string& mimeType)
47   {
48     return mMimeType == mimeType ? true : false;
49   }
50
51   bool SetData(const Dali::Clipboard::ClipData& clipData)
52   {
53     mMimeType = clipData.GetMimeType();
54     mData     = clipData.GetData();
55
56     if(mData.empty())
57     {
58       return false;
59     }
60
61     mDataSentSignal.Emit(mMimeType.c_str(), mData.c_str());
62     mDataSelectedSignal.Emit(mMimeType.c_str());
63
64     return true;
65   }
66
67   uint32_t GetData(const std::string &mimeType)
68   {
69     if(!mMimeType.compare(mimeType.c_str()))
70     {
71       mDataId++;
72       // For consistency of operation with tizen Wl2, a fake callback is occurs using a timer.
73       if(mDataReceiveTimer.IsRunning())
74       {
75         mDataReceiveTimer.Stop();
76       }
77       mDataReceiveTimer.Start();
78       DALI_LOG_RELEASE_INFO("request data, id:%u, request type:%s\n", mDataId, mimeType.c_str());
79       return mDataId;
80     }
81     return 0u;
82   }
83
84   bool OnReceiveData()
85   {
86     DALI_LOG_RELEASE_INFO("receive data, success signal emit, id:%u, type:%s, data:%s\n", mDataId, mMimeType.c_str(), mData.c_str());
87     mDataReceivedSignal.Emit(mDataId, mMimeType.c_str(), mData.c_str());
88     return false;
89   }
90
91   Ecore_X_Window mApplicationWindow;
92   std::string    mMimeType;
93   std::string    mData;
94   uint32_t       mDataId{0};
95
96   Dali::Clipboard::DataSentSignalType     mDataSentSignal;
97   Dali::Clipboard::DataReceivedSignalType mDataReceivedSignal;
98   Dali::Clipboard::DataSelectedSignalType mDataSelectedSignal;
99
100   Dali::Timer mDataReceiveTimer;
101 };
102
103 Clipboard::Clipboard(Impl* impl)
104 : mImpl(impl)
105 {
106   mImpl->mDataReceiveTimer = Dali::Timer::New(10);
107   mImpl->mDataReceiveTimer.TickSignal().Connect(this, &Clipboard::OnReceiveData);
108 }
109
110 Clipboard::~Clipboard()
111 {
112   delete mImpl;
113 }
114
115 Dali::Clipboard Clipboard::Get()
116 {
117   Dali::Clipboard clipboard;
118
119   Dali::SingletonService service(SingletonService::Get());
120   if(service)
121   {
122     // Check whether the singleton is already created
123     Dali::BaseHandle handle = service.GetSingleton(typeid(Dali::Clipboard));
124     if(handle)
125     {
126       // If so, downcast the handle
127       clipboard = Dali::Clipboard(dynamic_cast<Clipboard*>(handle.GetObjectPtr()));
128     }
129     else
130     {
131       Adaptor& adaptorImpl(Adaptor::GetImplementation(Adaptor::Get()));
132       Any      nativewindow = adaptorImpl.GetNativeWindowHandle();
133
134       // The Ecore_X_Window needs to use the Clipboard.
135       // Only when the render surface is window, we can get the Ecore_X_Window.
136       Ecore_X_Window ecoreXwin(AnyCast<Ecore_X_Window>(nativewindow));
137       if(ecoreXwin)
138       {
139         // If we fail to get Ecore_X_Window, we can't use the Clipboard correctly.
140         // Thus you have to call "ecore_imf_context_client_window_set" somewhere.
141         // In EvasPlugIn, this function is called in EvasPlugin::ConnectEcoreEvent().
142         Clipboard::Impl* impl(new Clipboard::Impl(ecoreXwin));
143         clipboard = Dali::Clipboard(new Clipboard(impl));
144         service.Register(typeid(clipboard), clipboard);
145       }
146     }
147   }
148
149   return clipboard;
150 }
151
152 bool Clipboard::IsAvailable()
153 {
154   Dali::SingletonService service(SingletonService::Get());
155   if(service)
156   {
157     Dali::BaseHandle handle = service.GetSingleton(typeid(Dali::Clipboard));
158     if(handle)
159     {
160       return true;
161     }
162   }
163   return false;
164 }
165
166 Dali::Clipboard::DataSentSignalType& Clipboard::DataSentSignal()
167 {
168   return mImpl->mDataSentSignal;
169 }
170
171 Dali::Clipboard::DataReceivedSignalType& Clipboard::DataReceivedSignal()
172 {
173   return mImpl->mDataReceivedSignal;
174 }
175
176 Dali::Clipboard::DataSelectedSignalType& Clipboard::DataSelectedSignal()
177 {
178   return mImpl->mDataSelectedSignal;
179 }
180
181 bool Clipboard::HasType(const std::string& mimeType)
182 {
183   return mImpl->HasType(mimeType);
184 }
185
186 bool Clipboard::SetData(const Dali::Clipboard::ClipData& clipData)
187 {
188   return mImpl->SetData(clipData);
189 }
190
191 uint32_t Clipboard::GetData(const std::string &mimeType)
192 {
193   return mImpl->GetData(mimeType);
194 }
195
196 size_t Clipboard::NumberOfItems()
197 {
198   bool isItem = HasType(MIME_TYPE_TEXT_PLAIN) || HasType(MIME_TYPE_HTML) || HasType(MIME_TYPE_TEXT_URI);
199   return isItem ? 1u : 0u;
200 }
201
202 void Clipboard::ShowClipboard()
203 {
204 }
205
206 void Clipboard::HideClipboard(bool skipFirstHide)
207 {
208 }
209
210 bool Clipboard::IsVisible() const
211 {
212   return false;
213 }
214
215 bool Clipboard::OnReceiveData()
216 {
217   return mImpl->OnReceiveData();
218 }
219
220 } // namespace Adaptor
221
222 } // namespace Internal
223
224 } // namespace Dali