Enhance application device signal
[platform/core/uifw/dali-adaptor.git] / adaptors / ubuntu / framework-ubuntu.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 <Ecore.h>
23 #include <Elementary.h>
24 #include <X11/Xlib.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( CallbackManager::New() )
66   {
67   }
68
69   ~Impl()
70   {
71     delete mAbortCallBack;
72
73     // we're quiting the main loop so
74     // mCallbackManager->RemoveAllCallBacks() does not need to be called
75     // to delete our abort handler
76     delete mCallbackManager;
77   }
78
79   // Data
80
81   CallbackBase* mAbortCallBack;
82   CallbackManager *mCallbackManager;
83   // Static methods
84
85   /**
86    * Called by AppCore on application creation.
87    */
88   static bool AppCreate(void *data)
89   {
90     return static_cast<Framework*>(data)->AppStatusHandler(APP_CREATE, NULL);
91   }
92
93   /**
94    * Called by AppCore when the application should terminate.
95    */
96   static void AppTerminate(void *data)
97   {
98     static_cast<Framework*>(data)->AppStatusHandler(APP_TERMINATE, NULL);
99   }
100
101   /**
102    * Called by AppCore when the application is paused.
103    */
104   static void AppPause(void *data)
105   {
106     static_cast<Framework*>(data)->AppStatusHandler(APP_PAUSE, NULL);
107   }
108
109   /**
110    * Called by AppCore when the application is resumed.
111    */
112   static void AppResume(void *data)
113   {
114     static_cast<Framework*>(data)->AppStatusHandler(APP_RESUME, NULL);
115   }
116
117   /**
118    * Called by AppCore when the language changes on the device.
119    */
120   static void AppLanguageChange(void* data)
121   {
122     static_cast<Framework*>(data)->AppStatusHandler(APP_LANGUAGE_CHANGE, NULL);
123   }
124
125 };
126
127 Framework::Framework( Framework::Observer& observer, int *argc, char ***argv, Type type )
128 : mObserver(observer),
129   mInitialised(false),
130   mRunning(false),
131   mArgc(argc),
132   mArgv(argv),
133   mBundleName(""),
134   mBundleId(""),
135   mAbortHandler( MakeCallback( this, &Framework::AbortCallback ) ),
136   mImpl(NULL)
137 {
138   InitThreads();
139   mImpl = new Impl(this);
140 }
141
142 Framework::~Framework()
143 {
144   if (mRunning)
145   {
146     Quit();
147   }
148
149   delete mImpl;
150 }
151
152 void Framework::Run()
153 {
154   mRunning = true;
155
156   elm_init(*mArgc, *mArgv);
157
158   Impl::AppCreate(this);
159
160   elm_run();
161
162   mRunning = false;
163 }
164
165 void Framework::Quit()
166 {
167   Impl::AppTerminate(this);
168
169   elm_exit();
170 }
171
172 bool Framework::IsMainLoopRunning()
173 {
174   return mRunning;
175 }
176
177 void Framework::AddAbortCallback( CallbackBase* callback )
178 {
179   mImpl->mAbortCallBack = callback;
180 }
181
182 std::string Framework::GetBundleName() const
183 {
184   return mBundleName;
185 }
186
187 void Framework::SetBundleName(const std::string& name)
188 {
189   mBundleName = name;
190 }
191
192 std::string Framework::GetBundleId() const
193 {
194   return mBundleId;
195 }
196
197 std::string Framework::GetResourcePath()
198 {
199   // "DALI_APPLICATION_PACKAGE" is used by Ubuntu specifically to get the already configured Application package path.
200   const char* ubuntuEnvironmentVariable = "DALI_APPLICATION_PACKAGE";
201   char* value = getenv( ubuntuEnvironmentVariable );
202   std::string resourcePath;
203   if ( value != NULL )
204   {
205     resourcePath = value;
206   }
207
208   return resourcePath;
209 }
210
211 void Framework::SetBundleId(const std::string& id)
212 {
213   mBundleId = id;
214 }
215
216 void Framework::AbortCallback( )
217 {
218   // if an abort call back has been installed run it.
219   if (mImpl->mAbortCallBack)
220   {
221     CallbackBase::Execute( *mImpl->mAbortCallBack );
222   }
223   else
224   {
225     Quit();
226   }
227 }
228
229 bool Framework::AppStatusHandler(int type, void *bundleData)
230 {
231   switch (type)
232   {
233     case APP_CREATE:
234     {
235       mInitialised = true;
236
237       mObserver.OnInit();
238       break;
239     }
240
241     case APP_RESET:
242       mObserver.OnReset();
243       break;
244
245     case APP_RESUME:
246       mObserver.OnResume();
247       break;
248
249     case APP_TERMINATE:
250       mObserver.OnTerminate();
251       break;
252
253     case APP_PAUSE:
254       mObserver.OnPause();
255       break;
256
257     case APP_LANGUAGE_CHANGE:
258       mObserver.OnLanguageChanged("");
259       break;
260
261     default:
262       break;
263   }
264
265   return true;
266 }
267
268 void Framework::InitThreads()
269 {
270   XInitThreads();
271 }
272
273 } // namespace Adaptor
274
275 } // namespace Internal
276
277 } // namespace Dali