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