2 * Copyright (c) 2018 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/system/common/callback-manager-ecore.h>
22 // Ecore is littered with C style cast
23 #pragma GCC diagnostic push
24 #pragma GCC diagnostic ignored "-Wold-style-cast"
27 #include <dali/integration-api/debug.h>
42 * Structure contains the callback function and control options
50 CallbackData( CallbackBase* callback, bool hasReturnValue )
51 : mCallback( callback ),
52 mRemoveFromContainerFunction( NULL ),
55 mHasReturnValue( hasReturnValue )
64 delete mRemoveFromContainerFunction;
67 CallbackBase* mCallback; ///< call back
68 CallbackBase* mRemoveFromContainerFunction; ///< Called to remove the callbackdata from the callback container
69 Ecore_Idler* mIdler; ///< ecore idler
70 Ecore_Idle_Enterer* mIdleEnterer; ///< ecore idle enterer
71 bool mHasReturnValue; ///< true if the callback function has a return value.
78 * Called from the main thread while idle.
80 Eina_Bool IdleCallback(void *data)
82 Eina_Bool ret = ECORE_CALLBACK_CANCEL; // CALLBACK Cancel will delete the idler so we don't need to call ecore_idler_del
83 CallbackData *callbackData = static_cast< CallbackData * >( data );
85 if( callbackData->mHasReturnValue )
88 bool retValue = CallbackBase::ExecuteReturn< bool >( *callbackData->mCallback );
92 ret = ECORE_CALLBACK_RENEW;
96 // remove callback data from the container
97 CallbackBase::Execute( *callbackData->mRemoveFromContainerFunction, callbackData );
105 // remove callback data from the container
106 CallbackBase::Execute( *callbackData->mRemoveFromContainerFunction, callbackData );
109 CallbackBase::Execute( *callbackData->mCallback );
118 } // unnamed namespace
120 EcoreCallbackManager::EcoreCallbackManager()
126 void EcoreCallbackManager::Start()
128 DALI_ASSERT_DEBUG( mRunning == false );
133 void EcoreCallbackManager::Stop()
135 // make sure we're not called twice
136 DALI_ASSERT_DEBUG( mRunning == true );
138 RemoveAllCallbacks();
144 bool EcoreCallbackManager::AddIdleCallback( CallbackBase* callback, bool hasReturnValue )
151 CallbackData* callbackData = new CallbackData( callback, hasReturnValue );
153 callbackData->mRemoveFromContainerFunction = MakeCallback( this, &EcoreCallbackManager::RemoveCallbackFromContainer );
155 // add the call back to the container
156 mCallbackContainer.push_front(callbackData);
159 callbackData->mIdler = ecore_idler_add( IdleCallback, callbackData);
161 DALI_ASSERT_ALWAYS( ( callbackData->mIdler != NULL ) && "Idle method not created" );
166 void EcoreCallbackManager::RemoveIdleCallback( CallbackBase* callback )
168 for( CallbackList::iterator it = mCallbackContainer.begin(),
169 endIt = mCallbackContainer.end();
173 CallbackData* data = *it;
175 if( data->mCallback == callback )
177 // remove callback data from the container.
178 CallbackBase::Execute( *data->mRemoveFromContainerFunction, data );
180 ecore_idler_del( data->mIdler );
185 bool EcoreCallbackManager::AddIdleEntererCallback( CallbackBase* callback )
192 CallbackData* callbackData = new CallbackData( callback, true );
194 callbackData->mRemoveFromContainerFunction = MakeCallback( this, &EcoreCallbackManager::RemoveCallbackFromContainer );
196 // add the call back to the container
197 mCallbackContainer.push_front( callbackData );
200 callbackData->mIdleEnterer = ecore_idle_enterer_add( IdleCallback, callbackData );
202 DALI_ASSERT_ALWAYS( ( callbackData->mIdleEnterer != NULL ) && "Idle method not created" );
207 void EcoreCallbackManager::RemoveIdleEntererCallback( CallbackBase* callback )
209 for( CallbackList::iterator it = mCallbackContainer.begin(),
210 endIt = mCallbackContainer.end();
214 CallbackData* data = *it;
216 if( data->mCallback == callback )
218 // remove callback data from the container.
219 CallbackBase::Execute( *data->mRemoveFromContainerFunction, data );
221 ecore_idle_enterer_del( data->mIdleEnterer );
226 void EcoreCallbackManager::RemoveCallbackFromContainer(CallbackData *callbackData)
228 mCallbackContainer.remove(callbackData);
231 void EcoreCallbackManager::RemoveAllCallbacks()
233 // always called from main thread
234 for( CallbackList::iterator iter = mCallbackContainer.begin(); iter != mCallbackContainer.end(); ++iter)
236 CallbackData* data = (*iter);
240 ecore_idler_del( data->mIdler );
242 else if( data->mIdleEnterer )
244 ecore_idle_enterer_del( data->mIdleEnterer );
249 mCallbackContainer.clear();
252 // Creates a concrete interface for CallbackManager
253 CallbackManager* CallbackManager::New()
255 return new EcoreCallbackManager;
258 } // namespace Adaptor
260 } // namespace Internal
264 #pragma GCC diagnostic pop