Merge "Add more Preedit enumerations for custom styles" into devel/master
[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   mPaused(false),
145   mRunning(false),
146   mArgc(argc),
147   mArgv(argv),
148   mBundleName(""),
149   mBundleId(""),
150   mAbortHandler( MakeCallback( this, &Framework::AbortCallback ) ),
151   mImpl(NULL)
152 {
153   InitThreads();
154   mImpl = new Impl(this);
155 }
156
157 Framework::~Framework()
158 {
159   if (mRunning)
160   {
161     Quit();
162   }
163
164   delete mImpl;
165 }
166
167 void Framework::Run()
168 {
169   mRunning = true;
170
171   elm_init( mArgc ? *mArgc : 0, mArgv ? *mArgv : nullptr );
172
173   Impl::AppCreate(this);
174
175   elm_run();
176
177   mRunning = false;
178 }
179
180 void Framework::Quit()
181 {
182   Impl::AppTerminate(this);
183
184   elm_exit();
185 }
186
187 bool Framework::IsMainLoopRunning()
188 {
189   return mRunning;
190 }
191
192 void Framework::AddAbortCallback( CallbackBase* callback )
193 {
194   mImpl->mAbortCallBack = callback;
195 }
196
197 std::string Framework::GetBundleName() const
198 {
199   return mBundleName;
200 }
201
202 void Framework::SetBundleName(const std::string& name)
203 {
204   mBundleName = name;
205 }
206
207 std::string Framework::GetBundleId() const
208 {
209   return mBundleId;
210 }
211
212 std::string Framework::GetResourcePath()
213 {
214   // "DALI_APPLICATION_PACKAGE" is used by Ubuntu specifically to get the already configured Application package path.
215   const char* ubuntuEnvironmentVariable = "DALI_APPLICATION_PACKAGE";
216   char* value = getenv( ubuntuEnvironmentVariable );
217   std::string resourcePath;
218   if ( value != NULL )
219   {
220     resourcePath = value;
221   }
222
223   if( resourcePath.back() != '/' )
224   {
225     resourcePath+="/";
226   }
227
228   return resourcePath;
229 }
230
231 std::string Framework::GetDataPath()
232 {
233   const char* ubuntuEnvironmentVariable = "DALI_APPLICATION_DATA_DIR";
234   char* value = getenv( ubuntuEnvironmentVariable );
235   std::string dataPath;
236   if ( value != NULL )
237   {
238     dataPath = value;
239   }
240
241   return dataPath;
242 }
243
244 void Framework::SetBundleId(const std::string& id)
245 {
246   mBundleId = id;
247 }
248
249 void Framework::AbortCallback( )
250 {
251   // if an abort call back has been installed run it.
252   if (mImpl->mAbortCallBack)
253   {
254     CallbackBase::Execute( *mImpl->mAbortCallBack );
255   }
256   else
257   {
258     Quit();
259   }
260 }
261
262 bool Framework::AppStatusHandler(int type, void *bundleData)
263 {
264   switch (type)
265   {
266     case APP_CREATE:
267     {
268       mInitialised = true;
269
270       mObserver.OnInit();
271       break;
272     }
273
274     case APP_RESET:
275       mObserver.OnReset();
276       break;
277
278     case APP_RESUME:
279       mObserver.OnResume();
280       break;
281
282     case APP_TERMINATE:
283       mObserver.OnTerminate();
284       break;
285
286     case APP_PAUSE:
287       mObserver.OnPause();
288       break;
289
290     case APP_LANGUAGE_CHANGE:
291       mObserver.OnLanguageChanged();
292       break;
293
294     default:
295       break;
296   }
297
298   return true;
299 }
300
301 void Framework::InitThreads()
302 {
303   XInitThreads();
304 }
305
306 std::string Framework::GetLanguage() const
307 {
308   return mImpl->GetLanguage();
309 }
310
311 std::string Framework::GetRegion() const
312 {
313   return mImpl->GetRegion();
314 }
315
316 } // namespace Adaptor
317
318 } // namespace Internal
319
320 } // namespace Dali