Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / framework-tizen.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 "framework.h"
20
21 // EXTERNAL INCLUDES
22 #include <app.h>
23 #include <bundle.h>
24 #include <Ecore.h>
25
26 #include <dali/integration-api/debug.h>
27
28 // INTERNAL INCLUDES
29 #include <callback-manager.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace Adaptor
38 {
39
40 namespace
41 {
42
43 /// Application Status Enum
44 enum
45 {
46   APP_CREATE,
47   APP_TERMINATE,
48   APP_PAUSE,
49   APP_RESUME,
50   APP_RESET,
51   APP_LANGUAGE_CHANGE,
52 };
53
54 } // Unnamed namespace
55
56 /**
57  * Impl to hide EFL data members
58  */
59 struct Framework::Impl
60 {
61   // Constructor
62
63   Impl(void* data)
64   : mAbortCallBack( NULL ),
65     mCallbackManager( NULL )
66   {
67     mEventCallback.create = AppCreate;
68     mEventCallback.terminate = AppTerminate;
69     mEventCallback.pause = AppPause;
70     mEventCallback.resume = AppResume;
71 #if defined(TIZEN_SDK_2_3)
72     mEventCallback.app_control = AppControl;
73 #else
74     mEventCallback.service = AppService;
75 #endif
76     mEventCallback.low_memory = NULL;
77     mEventCallback.low_battery = NULL;
78     mEventCallback.device_orientation = DeviceRotated;
79     mEventCallback.language_changed = AppLanguageChange;
80     mEventCallback.region_format_changed = NULL;
81
82     mCallbackManager = CallbackManager::New();
83   }
84
85   ~Impl()
86   {
87     delete mAbortCallBack;
88
89     // we're quiting the main loop so
90     // mCallbackManager->RemoveAllCallBacks() does not need to be called
91     // to delete our abort handler
92     delete mCallbackManager;
93   }
94
95   // Data
96
97   CallbackBase* mAbortCallBack;
98   app_event_callback_s mEventCallback;
99   CallbackManager *mCallbackManager;
100   // Static methods
101
102   /**
103    * Called by AppCore on application creation.
104    */
105   static bool AppCreate(void *data)
106   {
107     return static_cast<Framework*>(data)->SlpAppStatusHandler(APP_CREATE);
108   }
109
110   /**
111    * Called by AppCore when the application should terminate.
112    */
113   static void AppTerminate(void *data)
114   {
115     static_cast<Framework*>(data)->SlpAppStatusHandler(APP_TERMINATE);
116   }
117
118   /**
119    * Called by AppCore when the application is paused.
120    */
121   static void AppPause(void *data)
122   {
123     static_cast<Framework*>(data)->SlpAppStatusHandler(APP_PAUSE);
124   }
125
126   /**
127    * Called by AppCore when the application is resumed.
128    */
129   static void AppResume(void *data)
130   {
131     static_cast<Framework*>(data)->SlpAppStatusHandler(APP_RESUME);
132   }
133
134   /**
135    * Called by AppCore when the application is launched from another module (e.g. homescreen).
136    * @param[in] b the bundle data which the launcher module sent
137    */
138
139 #if defined(TIZEN_SDK_2_3)
140   static void AppControl(app_control_h app_control, void *data)
141 #else
142   static void AppService(service_h service, void *data)
143 #endif
144   {
145     Framework* framework = static_cast<Framework*>(data);
146
147     if(framework)
148     {
149       bundle *bundleData = NULL;
150 #if defined(TIZEN_SDK_2_3)
151       app_control_to_bundle(app_control, &bundleData);
152 #else
153       service_to_bundle(service, &bundleData);
154 #endif
155
156       if(bundleData)
157       {
158         // get bundle name
159         char* bundleName = const_cast<char*>(bundle_get_val(bundleData, "name"));
160         if(bundleName != NULL)
161         {
162           framework->SetBundleName(bundleName);
163         }
164
165         // get bundle id
166         char* bundleId = const_cast<char*>(bundle_get_val(bundleData, "id"));
167         if(bundleId != NULL)
168         {
169           framework->SetBundleId(bundleId);
170         }
171       }
172       framework->SlpAppStatusHandler(APP_RESET);
173     }
174   }
175
176   /**
177    * Called by AppCore when the language changes on the device.
178    */
179   static void AppLanguageChange(void* data)
180   {
181     static_cast<Framework*>(data)->SlpAppStatusHandler(APP_LANGUAGE_CHANGE);
182   }
183
184   static void DeviceRotated(app_device_orientation_e orientation, void *user_data)
185   {
186     switch(orientation)
187     {
188       case APP_DEVICE_ORIENTATION_0:
189         break;
190       case APP_DEVICE_ORIENTATION_90:
191         break;
192       case APP_DEVICE_ORIENTATION_180:
193         break;
194       case APP_DEVICE_ORIENTATION_270:
195         break;
196     }
197   }
198
199 };
200
201 Framework::Framework(Framework::Observer& observer, int *argc, char ***argv, const std::string& name)
202 : mObserver(observer),
203   mInitialised(false),
204   mRunning(false),
205   mArgc(argc),
206   mArgv(argv),
207   mName(name),
208   mBundleName(""),
209   mBundleId(""),
210   mAbortHandler( MakeCallback( this, &Framework::AbortCallback ) ),
211   mImpl(NULL)
212 {
213   InitThreads();
214   mImpl = new Impl(this);
215 }
216
217 Framework::~Framework()
218 {
219   if (mRunning)
220   {
221     Quit();
222   }
223
224   delete mImpl;
225 }
226
227 void Framework::Run()
228 {
229   mRunning = true;
230
231   app_efl_main(mArgc, mArgv, &mImpl->mEventCallback, this);
232
233   mRunning = false;
234 }
235
236 void Framework::Quit()
237 {
238   app_efl_exit();
239 }
240
241 bool Framework::IsMainLoopRunning()
242 {
243   return mRunning;
244 }
245
246 void Framework::AddAbortCallback( CallbackBase* callback )
247 {
248   mImpl->mAbortCallBack = callback;
249 }
250
251 std::string Framework::GetBundleName() const
252 {
253   return mBundleName;
254 }
255
256 void Framework::SetBundleName(const std::string& name)
257 {
258   mBundleName = name;
259 }
260
261 std::string Framework::GetBundleId() const
262 {
263   return mBundleId;
264 }
265
266 void Framework::SetBundleId(const std::string& id)
267 {
268   mBundleId = id;
269 }
270
271 void Framework::AbortCallback( )
272 {
273   // if an abort call back has been installed run it.
274   if (mImpl->mAbortCallBack)
275   {
276     CallbackBase::Execute( *mImpl->mAbortCallBack );
277   }
278   else
279   {
280     Quit();
281   }
282 }
283
284 bool Framework::SlpAppStatusHandler(int type)
285 {
286   switch (type)
287   {
288     case APP_CREATE:
289     {
290       mInitialised = true;
291
292       // Connect to abnormal exit signals
293       mAbortHandler.AbortOnSignal( SIGINT );
294       mAbortHandler.AbortOnSignal( SIGQUIT );
295       mAbortHandler.AbortOnSignal( SIGKILL );
296
297       mObserver.OnInit();
298       break;
299     }
300
301     case APP_RESET:
302       mObserver.OnReset();
303       break;
304
305     case APP_RESUME:
306       mObserver.OnResume();
307       break;
308
309     case APP_TERMINATE:
310      mObserver.OnTerminate();
311       break;
312
313     case APP_PAUSE:
314       mObserver.OnPause();
315       break;
316
317     case APP_LANGUAGE_CHANGE:
318       mObserver.OnLanguageChanged();
319       break;
320
321     default:
322       break;
323   }
324
325   return true;
326 }
327
328 } // namespace Adaptor
329
330 } // namespace Internal
331
332 } // namespace Dali