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 "ecore-callback-manager.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 )
51 : mCallback(callback),
52 mRemoveFromContainerFunction(NULL),
62 delete mRemoveFromContainerFunction;
65 CallbackBase* mCallback; ///< call back
66 CallbackBase* mRemoveFromContainerFunction; ///< Called to remove the callbackdata from the callback container
67 Ecore_Idler* mIdler; ///< ecore idler
74 * Called from the main thread while idle.
76 Eina_Bool IdleCallback(void *data)
78 CallbackData *callbackData = static_cast< CallbackData * >( data );
80 // remove callback data from the container
81 CallbackBase::Execute( *callbackData->mRemoveFromContainerFunction, callbackData );
84 CallbackBase::Execute( *callbackData->mCallback );
89 // CALLBACK Cancel will delete the idler so we don't need to call ecore_idler_del
90 return ECORE_CALLBACK_CANCEL;
93 } // unnamed namespace
95 EcoreCallbackManager::EcoreCallbackManager()
101 void EcoreCallbackManager::Start()
103 DALI_ASSERT_DEBUG( mRunning == false );
108 void EcoreCallbackManager::Stop()
110 // make sure we're not called twice
111 DALI_ASSERT_DEBUG( mRunning == true );
113 RemoveAllCallbacks();
119 bool EcoreCallbackManager::AddIdleCallback( CallbackBase* callback )
126 CallbackData* callbackData = new CallbackData( callback );
128 callbackData->mRemoveFromContainerFunction = MakeCallback( this, &EcoreCallbackManager::RemoveCallbackFromContainer );
130 // add the call back to the container
131 mCallbackContainer.push_front(callbackData);
134 callbackData->mIdler = ecore_idler_add( IdleCallback, callbackData);
136 DALI_ASSERT_ALWAYS( ( callbackData->mIdler != NULL ) && "Idle method not created" );
141 void EcoreCallbackManager::RemoveIdleCallback( CallbackBase* callback )
143 CallbackList::iterator it = mCallbackContainer.begin();
145 while( mCallbackContainer.end() != it )
147 CallbackData* data = *it;
149 if( data->mCallback == callback )
151 it = mCallbackContainer.erase( it );
152 ecore_idler_del( data->mIdler );
161 void EcoreCallbackManager::RemoveCallbackFromContainer(CallbackData *callbackData)
163 mCallbackContainer.remove(callbackData);
166 void EcoreCallbackManager::RemoveAllCallbacks()
168 // always called from main thread
169 for( CallbackList::iterator iter = mCallbackContainer.begin(); iter != mCallbackContainer.end(); ++iter)
171 CallbackData* data = (*iter);
173 ecore_idler_del( data->mIdler );
178 mCallbackContainer.clear();
181 // Creates a concrete interface for CallbackManager
182 CallbackManager* CallbackManager::New()
184 return new EcoreCallbackManager;
187 } // namespace Adaptor
189 } // namespace Internal
193 #pragma GCC diagnostic pop