Create LibUV / X11 profile
[platform/core/uifw/dali-adaptor.git] / dali / internal / adaptor / libuv / framework-libuv.cpp
1 /*
2  * Copyright (c) 2015 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 <X11/Xlib.h>
23 #include <uv.h>
24 #include <cstdio>
25 #include <cstring>
26
27 #include <dali/integration-api/debug.h>
28
29 // INTERNAL INCLUDES
30 #include <dali/internal/system/common/callback-manager.h>
31
32 namespace Dali
33 {
34 namespace Internal
35 {
36 namespace Adaptor
37 {
38 namespace
39 {
40 thread_local uv_loop_t* gUVLoop{nullptr};
41 }
42
43 uv_loop_t* GetUVMainLoop()
44 {
45   return gUVLoop;
46 }
47
48 /**
49  * Impl to hide LibUV data members
50  */
51 struct Framework::Impl
52 {
53   // Constructor
54   Impl(void* data)
55   : mAbortCallBack(nullptr),
56     mCallbackManager(nullptr),
57     mLanguage("NOT_SUPPORTED"),
58     mRegion("NOT_SUPPORTED")
59   {
60     mCallbackManager = CallbackManager::New();
61     mMainLoop        = new uv_loop_t;
62     gUVLoop          = mMainLoop; // "There can be only one!"
63
64     uv_loop_init(mMainLoop);
65   }
66
67   ~Impl()
68   {
69     delete mAbortCallBack;
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     delete mMainLoop;
77   }
78
79   void Run()
80   {
81     uv_run(mMainLoop, UV_RUN_DEFAULT);
82     uv_loop_close(mMainLoop);
83   }
84
85   void Quit()
86   {
87     uv_stop(mMainLoop);
88   }
89
90   // Data
91
92   CallbackBase*    mAbortCallBack;
93   CallbackManager* mCallbackManager;
94   uv_loop_t*       mMainLoop;
95
96   std::string mLanguage;
97   std::string mRegion;
98
99 private:
100   Impl(const Impl& impl) = delete;
101   Impl& operator=(const Impl& impl) = delete;
102 };
103
104 Framework::Framework(Framework::Observer& observer, TaskObserver& taskObserver, int* argc, char*** argv, Type type, bool useUiThread)
105 : mObserver(observer),
106   mTaskObserver(taskObserver),
107   mInitialised(false),
108   mRunning(false),
109   mArgc(argc),
110   mArgv(argv),
111   mBundleName(""),
112   mBundleId(""),
113   mAbortHandler(MakeCallback(this, &Framework::AbortCallback)),
114   mImpl(NULL)
115 {
116   mImpl = new Impl(this);
117 }
118
119 Framework::~Framework()
120 {
121   if(mRunning)
122   {
123     Quit();
124   }
125
126   delete mImpl;
127 }
128
129 void Framework::Run()
130 {
131   mRunning = true;
132   mObserver.OnInit();
133   mImpl->Run();
134
135   mRunning = false;
136 }
137
138 void Framework::Quit()
139 {
140   mObserver.OnTerminate();
141   mImpl->Quit();
142 }
143
144 bool Framework::IsMainLoopRunning()
145 {
146   return mRunning;
147 }
148
149 void Framework::AddAbortCallback(CallbackBase* callback)
150 {
151   mImpl->mAbortCallBack = callback;
152 }
153
154 std::string Framework::GetBundleName() const
155 {
156   return mBundleName;
157 }
158
159 void Framework::SetBundleName(const std::string& name)
160 {
161 }
162
163 std::string Framework::GetBundleId() const
164 {
165   return "";
166 }
167
168 std::string Framework::GetResourcePath()
169 {
170   // "DALI_APPLICATION_PACKAGE" is used by Ubuntu specifically to get the already configured Application package path.
171   const char* ubuntuEnvironmentVariable = "DALI_APPLICATION_PACKAGE";
172   char*       value                     = getenv(ubuntuEnvironmentVariable);
173   std::string resourcePath;
174   if(value != NULL)
175   {
176     resourcePath = value;
177   }
178
179   if(resourcePath.back() != '/')
180   {
181     resourcePath += "/";
182   }
183
184   return resourcePath;
185 }
186
187 void Framework::SetBundleId(const std::string& id)
188 {
189 }
190
191 void Framework::AbortCallback()
192 {
193   // if an abort call back has been installed run it.
194   if(mImpl->mAbortCallBack)
195   {
196     CallbackBase::Execute(*mImpl->mAbortCallBack);
197   }
198   else
199   {
200     Quit();
201   }
202 }
203
204 bool Framework::AppStatusHandler(int type, void* bundleData)
205 {
206   return true;
207 }
208
209 std::string Framework::GetLanguage() const
210 {
211   return mImpl->mLanguage;
212 }
213
214 std::string Framework::GetRegion() const
215 {
216   return mImpl->mRegion;
217 }
218
219 std::string Framework::GetDataPath()
220 {
221   const char* ubuntuEnvironmentVariable = "DALI_APPLICATION_DATA_DIR";
222   char*       value                     = getenv(ubuntuEnvironmentVariable);
223   std::string dataPath;
224   if(value != NULL)
225   {
226     dataPath = value;
227   }
228
229   return dataPath;
230 }
231
232 } // namespace Adaptor
233
234 } // namespace Internal
235
236 } // namespace Dali