[dali_1.0.4] Merge branch 'tizen'
[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 <boost/bind.hpp>
25 #include <X11/Xlib.h>
26
27 #include <dali/integration-api/debug.h>
28
29 // INTERNAL INCLUDES
30 #include <callback-manager.h>
31
32 namespace Dali
33 {
34
35 namespace Internal
36 {
37
38 namespace Adaptor
39 {
40
41 namespace
42 {
43
44 /// Application Status Enum
45 enum
46 {
47   APP_CREATE,
48   APP_TERMINATE,
49   APP_PAUSE,
50   APP_RESUME,
51   APP_RESET,
52   APP_LANGUAGE_CHANGE,
53 };
54
55 } // Unnamed namespace
56
57 /**
58  * Impl to hide EFL data members
59  */
60 struct Framework::Impl
61 {
62   // Constructor
63
64   Impl(void* data)
65   {
66     mCallbackManager = CallbackManager::New();
67   }
68
69   ~Impl()
70   {
71     // we're quiting the main loop so
72     // mCallbackManager->RemoveAllCallBacks() does not need to be called
73     // to delete our abort handler
74     delete mCallbackManager;
75   }
76
77   // Data
78
79   boost::function<void(void)> mAbortCallBack;
80   CallbackManager *mCallbackManager;
81   // Static methods
82
83   /**
84    * Called by AppCore on application creation.
85    */
86   static bool AppCreate(void *data)
87   {
88     return static_cast<Framework*>(data)->SlpAppStatusHandler(APP_CREATE);
89   }
90
91   /**
92    * Called by AppCore when the application should terminate.
93    */
94   static void AppTerminate(void *data)
95   {
96     static_cast<Framework*>(data)->SlpAppStatusHandler(APP_TERMINATE);
97   }
98
99   /**
100    * Called by AppCore when the application is paused.
101    */
102   static void AppPause(void *data)
103   {
104     static_cast<Framework*>(data)->SlpAppStatusHandler(APP_PAUSE);
105   }
106
107   /**
108    * Called by AppCore when the application is resumed.
109    */
110   static void AppResume(void *data)
111   {
112     static_cast<Framework*>(data)->SlpAppStatusHandler(APP_RESUME);
113   }
114
115   /**
116    * Called by AppCore when the language changes on the device.
117    */
118   static void AppLanguageChange(void* data)
119   {
120     static_cast<Framework*>(data)->SlpAppStatusHandler(APP_LANGUAGE_CHANGE);
121   }
122
123 };
124
125 Framework::Framework(Framework::Observer& observer, int *argc, char ***argv, const std::string& name)
126 : mObserver(observer),
127   mInitialised(false),
128   mRunning(false),
129   mArgc(argc),
130   mArgv(argv),
131   mName(name),
132   mBundleName(""),
133   mBundleId(""),
134   mAbortHandler(boost::bind(&Framework::AbortCallback, this)),
135   mImpl(NULL)
136 {
137   InitThreads();
138   mImpl = new Impl(this);
139 }
140
141 Framework::~Framework()
142 {
143   if (mRunning)
144   {
145     Quit();
146   }
147
148   delete mImpl;
149 }
150
151 void Framework::Run()
152 {
153   mRunning = true;
154
155   elm_init(*mArgc, *mArgv);
156
157   Impl::AppCreate(this);
158
159   elm_run();
160
161   mRunning = false;
162 }
163
164 void Framework::Quit()
165 {
166   Impl::AppTerminate(this);
167
168   elm_exit();
169 }
170
171 bool Framework::IsMainLoopRunning()
172 {
173   return mRunning;
174 }
175
176 void Framework::AddAbortCallback(boost::function<void(void)> callBack)
177 {
178   mImpl->mAbortCallBack = callBack;
179 }
180
181 std::string Framework::GetBundleName() const
182 {
183   return mBundleName;
184 }
185
186 void Framework::SetBundleName(const std::string& name)
187 {
188   mBundleName = name;
189 }
190
191 std::string Framework::GetBundleId() const
192 {
193   return mBundleId;
194 }
195
196 void Framework::SetBundleId(const std::string& id)
197 {
198   mBundleId = id;
199 }
200
201 void Framework::AbortCallback( )
202 {
203   // if an abort call back has been installed run it.
204   if (mImpl->mAbortCallBack)
205   {
206     mImpl->mAbortCallBack();
207   }
208   else
209   {
210     Quit();
211   }
212 }
213
214 bool Framework::SlpAppStatusHandler(int type)
215 {
216   switch (type)
217   {
218     case APP_CREATE:
219     {
220       mInitialised = true;
221
222       // Connect to abnormal exit signals
223       mAbortHandler.AbortOnSignal( SIGINT );
224       mAbortHandler.AbortOnSignal( SIGQUIT );
225       mAbortHandler.AbortOnSignal( SIGKILL );
226
227       mObserver.OnInit();
228       break;
229     }
230
231     case APP_RESET:
232       mObserver.OnReset();
233       break;
234
235     case APP_RESUME:
236       mObserver.OnResume();
237       break;
238
239     case APP_TERMINATE:
240      mObserver.OnTerminate();
241       break;
242
243     case APP_PAUSE:
244       mObserver.OnPause();
245       break;
246
247     case APP_LANGUAGE_CHANGE:
248       mObserver.OnLanguageChanged();
249       break;
250
251     default:
252       break;
253   }
254
255   return true;
256 }
257
258 void Framework::InitThreads()
259 {
260   XInitThreads();
261 }
262
263 } // namespace Adaptor
264
265 } // namespace Internal
266
267 } // namespace Dali