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 "ecore-callback-manager.h"
24 #include <dali/integration-api/debug.h>
39 * Structure contains the callback function and control options
47 CallbackData( CallbackBase* callback )
48 : mCallback(callback),
49 mRemoveFromContainerFunction(NULL),
59 delete mRemoveFromContainerFunction;
62 CallbackBase* mCallback; ///< call back
63 CallbackBase* mRemoveFromContainerFunction; ///< Called to remove the callbackdata from the callback container
64 Ecore_Idler* mIdler; ///< ecore idler
71 * Called from the main thread while idle.
73 Eina_Bool IdleCallback(void *data)
75 CallbackData *callbackData = static_cast< CallbackData * >( data );
77 // remove callback data from the container
78 CallbackBase::Execute( *callbackData->mRemoveFromContainerFunction, callbackData );
81 CallbackBase::Execute( *callbackData->mCallback );
86 // CALLBACK Cancel will delete the idler so we don't need to call ecore_idler_del
87 return ECORE_CALLBACK_CANCEL;
90 } // unnamed namespace
92 EcoreCallbackManager::EcoreCallbackManager()
98 void EcoreCallbackManager::Start()
100 DALI_ASSERT_DEBUG( mRunning == false );
105 void EcoreCallbackManager::Stop()
107 // make sure we're not called twice
108 DALI_ASSERT_DEBUG( mRunning == true );
110 RemoveAllCallbacks();
116 bool EcoreCallbackManager::AddIdleCallback( CallbackBase* callback )
123 CallbackData *callbackData = new CallbackData( callback );
125 callbackData->mRemoveFromContainerFunction = MakeCallback( this, &EcoreCallbackManager::RemoveCallbackFromContainer );
127 // add the call back to the container
128 mCallbackContainer.push_front(callbackData);
131 callbackData->mIdler = ecore_idler_add( IdleCallback, callbackData);
133 DALI_ASSERT_ALWAYS( ( callbackData->mIdler != NULL ) && "Idle method not created" );
138 void EcoreCallbackManager::RemoveCallbackFromContainer(CallbackData *callbackData)
140 mCallbackContainer.remove(callbackData);
143 void EcoreCallbackManager::RemoveAllCallbacks()
145 // always called from main thread
146 for( CallbackList::iterator iter = mCallbackContainer.begin(); iter != mCallbackContainer.end(); ++iter)
148 CallbackData* data = (*iter);
150 ecore_idler_del( data->mIdler );
155 mCallbackContainer.clear();
158 // Creates a concrete interface for CallbackManager
159 CallbackManager* CallbackManager::New()
161 return new EcoreCallbackManager;
164 } // namespace Adaptor
166 } // namespace Internal