Merge "Direct Rendering" into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / adaptor / macos / framework-mac.mm
1 /*
2  * Copyright (c) 2020 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 #import <Cocoa/Cocoa.h>
19
20 #include "extern-definitions.h"
21
22 // CLASS HEADER
23 #include <dali/internal/adaptor/common/framework.h>
24
25 // EXTERNAL INCLUDES
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 WindowsSystem data members
58  */
59 struct Framework::Impl
60 {
61   // Constructor
62
63   Impl(void* data)
64   : mAbortCallBack( nullptr ),
65     mLanguage( "NOT_SUPPORTED" ),
66     mRegion( "NOT_SUPPORTED" )
67   {
68   }
69
70   ~Impl()
71   {
72     delete mAbortCallBack;
73   }
74
75   std::string GetLanguage() const
76   {
77     return mLanguage;
78   }
79
80   std::string GetRegion() const
81   {
82     return mRegion;
83   }
84
85   void SetAbortCallback( CallbackBase *base )
86   {
87     mAbortCallBack = base;
88   }
89
90   bool ExecuteCallback()
91   {
92     if( nullptr != mAbortCallBack )
93     {
94       CallbackBase::Execute( *mAbortCallBack );
95       return true;
96     }
97
98     return false;
99   }
100
101 private:
102   // Undefined
103   Impl( const Impl& impl ) = delete;
104
105   // Undefined
106   Impl& operator=( const Impl& impl ) = delete;
107
108 private:
109   // Data
110   CallbackBase* mAbortCallBack;
111   std::string mLanguage;
112   std::string mRegion;
113 };
114
115 Framework::Framework(Framework::Observer& observer, Framework::TaskObserver& taskObserver, int* argc, char*** argv, Type type, bool useUiThread)
116 : mObserver(observer),
117   mTaskObserver(taskObserver),
118   mInitialised(false),
119   mPaused(false),
120   mRunning(false),
121   mArgc(argc),
122   mArgv(argv),
123   mBundleName(""),
124   mBundleId(""),
125   mAbortHandler( MakeCallback( this, &Framework::AbortCallback ) ),
126   mImpl(NULL)
127 {
128     InitThreads();
129     mImpl = new Impl(this);
130
131     // ensures the NSApp global object is initialized
132     [NSApplication sharedApplication];
133
134     // this is needed for applications without a bundle
135     [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
136
137     // make sure we can become the key window
138     [NSApp activateIgnoringOtherApps:YES];
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   AppStatusHandler(APP_CREATE, nullptr);
155   [NSApp run];
156   mRunning = false;
157 }
158
159 void Framework::Quit()
160 {
161   AppStatusHandler(APP_TERMINATE, nullptr);
162 }
163
164 bool Framework::IsMainLoopRunning()
165 {
166   return mRunning;
167 }
168
169 void Framework::AddAbortCallback( CallbackBase* callback )
170 {
171   mImpl->SetAbortCallback( callback );
172 }
173
174 std::string Framework::GetBundleName() const
175 {
176   return mBundleName;
177 }
178
179 void Framework::SetBundleName(const std::string& name)
180 {
181   mBundleName = name;
182 }
183
184 std::string Framework::GetBundleId() const
185 {
186   return mBundleId;
187 }
188
189 std::string Framework::GetResourcePath()
190 {
191   // "DALI_APPLICATION_PACKAGE" is used by macOS specifically to get the already configured Application package path.
192   const char* macEnvironmentVariable = "DALI_APPLICATION_PACKAGE";
193   char* value = getenv( macEnvironmentVariable );
194
195   std::string resourcePath;
196   if ( value != NULL )
197   {
198     resourcePath = value;
199   }
200
201   if( resourcePath.back() != '/' )
202   {
203     resourcePath+="/";
204   }
205
206   return resourcePath;
207 }
208
209 std::string Framework::GetDataPath()
210 {
211   return app_get_data_path();
212 }
213
214 void Framework::SetBundleId(const std::string& id)
215 {
216   mBundleId = id;
217 }
218
219 void Framework::AbortCallback( )
220 {
221   // if an abort call back has been installed run it.
222   if( false == mImpl->ExecuteCallback() )
223   {
224     Quit();
225   }
226 }
227
228 bool Framework::AppStatusHandler(int type, void *)
229 {
230   switch (type)
231   {
232     case APP_CREATE:
233     {
234       mInitialised = true;
235       mObserver.OnInit();
236       break;
237     }
238
239     case APP_RESET:
240       mObserver.OnReset();
241       break;
242
243     case APP_RESUME:
244       mObserver.OnResume();
245       break;
246
247     case APP_TERMINATE:
248       mObserver.OnTerminate();
249       break;
250
251     case APP_PAUSE:
252       mObserver.OnPause();
253       break;
254
255     case APP_LANGUAGE_CHANGE:
256       mObserver.OnLanguageChanged();
257       break;
258
259     default:
260       break;
261   }
262
263   return true;
264 }
265
266 void Framework::InitThreads()
267 {
268 }
269
270 std::string Framework::GetLanguage() const
271 {
272   return mImpl->GetLanguage();
273 }
274
275 std::string Framework::GetRegion() const
276 {
277   return mImpl->GetRegion();
278 }
279
280 } // namespace Adaptor
281
282 } // namespace Internal
283
284 } // namespace Dali