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