Merge "Add support for new accessibility actions" into devel/master
[platform/core/uifw/dali-adaptor.git] / adaptors / x11 / clipboard-impl-x.cpp
1 /*
2  * Copyright (c) 2014 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 "clipboard-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <Ecore_X.h>
23 #include <dali/public-api/object/any.h>
24 #include <dali/public-api/object/type-registry.h>
25 #include <dali/integration-api/debug.h>
26
27 // INTERNAL INCLUDES
28 #include <adaptor-impl.h>
29 #include <ecore-x-window-interface.h>
30 #include <singleton-service-impl.h>
31
32 namespace //unnamed namespace
33 {
34 const char* const CBHM_WINDOW = "CBHM_XWIN";
35 const char* const CBHM_MSG = "CBHM_MSG";
36 const char* const CBHM_ITEM = "CBHM_ITEM";
37 const char* const CBHM_cCOUNT = "CBHM_cCOUNT";
38 const char* const CBHM_ERROR = "CBHM_ERROR";
39 const char* const SET_ITEM = "SET_ITEM";
40 const char* const SHOW = "show0";
41 const char* const HIDE = "cbhm_hide";
42 }
43
44 ///////////////////////////////////////////////////////////////////////////////////////////////////
45 // Clipboard
46 ///////////////////////////////////////////////////////////////////////////////////////////////////
47
48 namespace Dali
49 {
50
51 namespace Internal
52 {
53
54 namespace Adaptor
55 {
56
57 namespace
58 {
59 BaseHandle Create()
60 {
61   BaseHandle handle( Clipboard::Get() );
62
63   if ( !handle && Adaptor::IsAvailable() )
64   {
65     Dali::SingletonService service( SingletonService::Get() );
66     if ( service )
67     {
68       Adaptor& adaptorImpl( Adaptor::GetImplementation( Adaptor::Get() ) );
69       Any nativewindow = adaptorImpl.GetNativeWindowHandle();
70
71       // The Ecore_X_Window needs to use the Clipboard.
72       // Only when the render surface is window, we can get the Ecore_X_Window.
73       Ecore_X_Window ecoreXwin( AnyCast<Ecore_X_Window>(nativewindow) );
74       if (ecoreXwin)
75       {
76         // If we fail to get Ecore_X_Window, we can't use the Clipboard correctly.
77         // Thus you have to call "ecore_imf_context_client_window_set" somewhere.
78         // In EvasPlugIn, this function is called in EvasPlugin::ConnectEcoreEvent().
79         Dali::Clipboard clipboard = Dali::Clipboard( new Clipboard( ecoreXwin ) );
80         service.Register( typeid( clipboard ), clipboard );
81         handle = clipboard;
82       }
83       else
84       {
85         DALI_LOG_ERROR("Failed to get native window handle");
86       }
87     }
88   }
89
90   return handle;
91 }
92 TypeRegistration CLIPBOARD_TYPE( typeid(Dali::Clipboard), typeid(Dali::BaseHandle), Create, true /* Create Instance At Startup */ );
93
94 } // unnamed namespace
95
96 Clipboard::Clipboard( Ecore_X_Window ecoreXwin)
97 {
98   mApplicationWindow = ecoreXwin;
99 }
100
101 Clipboard::~Clipboard()
102 {
103 }
104
105 Dali::Clipboard Clipboard::Get()
106 {
107   Dali::Clipboard clipboard;
108
109   Dali::SingletonService service( SingletonService::Get() );
110   if ( service )
111   {
112     // Check whether the singleton is already created
113     Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::Clipboard ) );
114     if(handle)
115     {
116       // If so, downcast the handle
117       clipboard = Dali::Clipboard( dynamic_cast< Clipboard* >( handle.GetObjectPtr() ) );
118     }
119   }
120
121   return clipboard;
122 }
123 bool Clipboard::SetItem(const std::string &itemData )
124 {
125   Ecore_X_Window cbhmWin = ECore::WindowInterface::GetWindow();
126   Ecore_X_Atom atomCbhmItem = ecore_x_atom_get( CBHM_ITEM );
127   Ecore_X_Atom dataType = ECORE_X_ATOM_STRING;
128
129   // Set item (property) to send
130   ecore_x_window_prop_property_set( cbhmWin, atomCbhmItem, dataType, 8, const_cast<char*>( itemData.c_str() ), itemData.length() + 1 );
131   ecore_x_sync();
132
133   // Trigger sending of item (property)
134   Ecore_X_Atom atomCbhmMsg = ecore_x_atom_get( CBHM_MSG );
135   ECore::WindowInterface::SendXEvent(ecore_x_display_get(), cbhmWin, False, NoEventMask, atomCbhmMsg, 8, SET_ITEM );
136   return true;
137 }
138
139 /*
140  * Get string at given index of clipboard
141  */
142 std::string Clipboard::GetItem( unsigned int index )  // change string to a Dali::Text object.
143 {
144   if ( index >= NumberOfItems() )
145   {
146     return "";
147   }
148
149   std::string emptyString( "" );
150   char sendBuf[20];
151
152   snprintf( sendBuf, 20,  "%s%d", CBHM_ITEM, index );
153   Ecore_X_Atom xAtomCbhmItem = ecore_x_atom_get( sendBuf );
154   Ecore_X_Atom xAtomItemType = 0;
155
156   std::string clipboardString( ECore::WindowInterface::GetWindowProperty(xAtomCbhmItem, &xAtomItemType, index ) );
157   if ( !clipboardString.empty() )
158   {
159     Ecore_X_Atom xAtomCbhmError = ecore_x_atom_get( CBHM_ERROR );
160     if ( xAtomItemType != xAtomCbhmError )
161     {
162       return clipboardString;
163     }
164    }
165    return emptyString;
166 }
167
168 /*
169  * Get number of items in clipboard
170  */
171 unsigned int Clipboard::NumberOfItems()
172 {
173   Ecore_X_Atom xAtomCbhmCountGet = ecore_x_atom_get( CBHM_cCOUNT );
174
175   std::string ret( ECore::WindowInterface::GetWindowProperty( xAtomCbhmCountGet, NULL, 0 ) );
176   int count = 0;
177
178   if ( !ret.empty() )
179   {
180     count = atoi( ret.c_str() );
181   }
182
183   return count;
184 }
185
186 /**
187  * Show clipboard window
188  * Function to send message to show the Clipboard (cbhm) as no direct API available
189  * Reference elementary/src/modules/ctxpopup_copypasteUI/cbhm_helper.c
190  */
191 void Clipboard::ShowClipboard()
192 {
193   // Claim the ownership of the SECONDARY selection.
194   ecore_x_selection_secondary_set(mApplicationWindow, "", 1);
195   Ecore_X_Window cbhmWin = ECore::WindowInterface::GetWindow();
196
197   // Launch the clipboard window
198   Ecore_X_Atom atomCbhmMsg = ecore_x_atom_get( CBHM_MSG );
199   ECore::WindowInterface::SendXEvent( ecore_x_display_get(), cbhmWin, False, NoEventMask, atomCbhmMsg, 8, SHOW );
200 }
201
202 void Clipboard::HideClipboard()
203 {
204   Ecore_X_Window cbhmWin = ECore::WindowInterface::GetWindow();
205   // Launch the clipboard window
206   Ecore_X_Atom atomCbhmMsg = ecore_x_atom_get( CBHM_MSG );
207   ECore::WindowInterface::SendXEvent( ecore_x_display_get(), cbhmWin, False, NoEventMask, atomCbhmMsg, 8, HIDE );
208
209   // release the ownership of SECONDARY selection
210   ecore_x_selection_secondary_clear();
211 }
212
213
214 } // namespace Adaptor
215
216 } // namespace Internal
217
218 } // namespace Dali