Merge branch 'tizen' into devel/new_mesh
[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, const std::string& name)
128 : mObserver(observer),
129   mInitialised(false),
130   mRunning(false),
131   mArgc(argc),
132   mArgv(argv),
133   mName(name),
134   mBundleName(""),
135   mBundleId(""),
136   mAbortHandler( MakeCallback( this, &Framework::AbortCallback ) ),
137   mImpl(NULL)
138 {
139   InitThreads();
140   mImpl = new Impl(this);
141 }
142
143 Framework::~Framework()
144 {
145   if (mRunning)
146   {
147     Quit();
148   }
149
150   delete mImpl;
151 }
152
153 void Framework::Run()
154 {
155   mRunning = true;
156
157   elm_init(*mArgc, *mArgv);
158
159   Impl::AppCreate(this);
160
161   elm_run();
162
163   mRunning = false;
164 }
165
166 void Framework::Quit()
167 {
168   Impl::AppTerminate(this);
169
170   elm_exit();
171 }
172
173 bool Framework::IsMainLoopRunning()
174 {
175   return mRunning;
176 }
177
178 void Framework::AddAbortCallback( CallbackBase* callback )
179 {
180   mImpl->mAbortCallBack = callback;
181 }
182
183 std::string Framework::GetBundleName() const
184 {
185   return mBundleName;
186 }
187
188 void Framework::SetBundleName(const std::string& name)
189 {
190   mBundleName = name;
191 }
192
193 std::string Framework::GetBundleId() const
194 {
195   return mBundleId;
196 }
197
198 void Framework::SetBundleId(const std::string& id)
199 {
200   mBundleId = id;
201 }
202
203 void Framework::AbortCallback( )
204 {
205   // if an abort call back has been installed run it.
206   if (mImpl->mAbortCallBack)
207   {
208     CallbackBase::Execute( *mImpl->mAbortCallBack );
209   }
210   else
211   {
212     Quit();
213   }
214 }
215
216 bool Framework::AppStatusHandler(int type, void *bundleData)
217 {
218   switch (type)
219   {
220     case APP_CREATE:
221     {
222       mInitialised = true;
223
224       // Connect to abnormal exit signals
225       mAbortHandler.AbortOnSignal( SIGINT );
226       mAbortHandler.AbortOnSignal( SIGQUIT );
227       mAbortHandler.AbortOnSignal( SIGKILL );
228
229       mObserver.OnInit();
230       break;
231     }
232
233     case APP_RESET:
234       mObserver.OnReset();
235       break;
236
237     case APP_RESUME:
238       mObserver.OnResume();
239       break;
240
241     case APP_TERMINATE:
242      mObserver.OnTerminate();
243       break;
244
245     case APP_PAUSE:
246       mObserver.OnPause();
247       break;
248
249     case APP_LANGUAGE_CHANGE:
250       mObserver.OnLanguageChanged();
251       break;
252
253     default:
254       break;
255   }
256
257   return true;
258 }
259
260 void Framework::InitThreads()
261 {
262   XInitThreads();
263 }
264
265 } // namespace Adaptor
266
267 } // namespace Internal
268
269 } // namespace Dali