Add sharing AppCoreBase context
[platform/core/appfw/app-core.git] / legacy / src / legacy / appcore_base.cc
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 #include "appcore_base.h"
18
19 #include <aul.h>
20 #include <aul_app_lifecycle.h>
21 #include <dlog.h>
22
23 #include <list>
24 #include <memory>
25 #include <string>
26
27 #include "app_core_base.hh"
28
29 #undef EXPORT_API
30 #define EXPORT_API __attribute__ ((visibility("default")))
31
32 #undef DEPRECATED
33 #define DEPRECATED __attribute__((__deprecated__))
34
35 namespace tizen_cpp {
36
37 namespace {
38
39 class AppCore : public AppCoreBase {
40  public:
41   class AppCoreEvent : public AppCoreBase::EventBase {
42    public:
43     AppCoreEvent(appcore_base_event type, appcore_base_event_cb cb, void* data)
44         : EventBase(static_cast<Type>(type)),
45           cb_(cb),
46           data_(data) {}
47
48     void OnEvent(const std::string& value) override {
49       if (cb_ != nullptr)
50         cb_(reinterpret_cast<void*>(const_cast<char*>(value.c_str())), data_);
51     }
52
53     void OnEvent(int value) override {
54       if (cb_ != nullptr)
55         cb_(&value, data_);
56     }
57
58    private:
59     appcore_base_event_cb cb_;
60     void* data_;
61   };
62
63   AppCore(appcore_base_ops ops, void* data) : ops_(ops), data_(data) {}
64
65   void OnLoopInit(int argc, char** argv) override {
66     if (ops_.init)
67       ops_.init(argc, argv, data_);
68   }
69
70   void OnLoopFinish() override {
71     if (ops_.finish)
72       ops_.finish();
73   }
74
75   void OnLoopRun() override {
76     if (ops_.run)
77       ops_.run(data_);
78   }
79
80   void OnLoopExit() override {
81     if (ops_.exit)
82       ops_.exit(data_);
83   }
84
85   int OnReceive(aul_type type, tizen_base::Bundle b) override {
86     if (ops_.receive)
87       return ops_.receive(type, b.GetHandle(), data_);
88     return -1;
89   }
90
91   int OnCreate() override {
92     if (ops_.create)
93       return ops_.create(data_);
94     return -1;
95   }
96
97   int OnControl(tizen_base::Bundle b) override {
98     if (ops_.control)
99       return ops_.control(b.GetHandle(), data_);
100     return -1;
101   }
102
103   int OnTerminate() override {
104     if (ops_.terminate)
105       return ops_.terminate(data_);
106     return -1;
107   }
108
109   int OnSetI18n() override {
110     if (ops_.set_i18n)
111       return ops_.set_i18n(data_);
112     return -1;
113   }
114
115   int OnSetEvent(IEvent::Type event) override {
116     if (!ops_.set_event)
117       return -1;
118
119     ops_.set_event((appcore_base_event)event, data_);
120     return 0;
121   }
122
123   int OnUnsetEvent(IEvent::Type event) override {
124     if (!ops_.unset_event)
125       return -1;
126
127     ops_.unset_event((appcore_base_event)event, data_);
128     return 0;
129   }
130
131   int OnTrimMemory() override {
132     if (!ops_.trim_memory)
133       return -1;
134
135     ops_.trim_memory(data_);
136     return 0;
137   }
138
139  private:
140   appcore_base_ops ops_;
141   void* data_;
142 };
143
144 std::list<std::shared_ptr<AppCoreBase::EventBase>> __event_list;
145
146 int __on_control(bundle* b, void* data) {
147   return appcore_base_on_control(b);
148 }
149
150 int __on_set_i18n(void* data) {
151   return appcore_base_on_set_i18n();
152 }
153
154 void __on_set_event(enum appcore_base_event event, void* data) {
155   appcore_base_on_set_event(event);
156 }
157
158 void __on_unset_event(enum appcore_base_event event, void* data) {
159   appcore_base_on_unset_event(event);
160 }
161
162 void __on_trim_memory(void* data) {
163   appcore_base_on_trim_memory();
164 }
165
166 int __on_receive(aul_type type, bundle* b, void* data) {
167   return appcore_base_on_receive(type, b);
168 }
169
170 int __on_create(void* data) {
171   return appcore_base_on_create();
172 }
173
174 int __on_terminate(void* data) {
175   return appcore_base_on_terminate();
176 }
177
178 }  // namespace
179 }  // namespace tizen_cpp
180
181 using namespace tizen_cpp;
182
183 extern "C" EXPORT_API int appcore_base_on_receive(aul_type type, bundle* b) {
184   auto* context = AppCoreBase::GetContext();
185   if (!context)
186     return -1;
187
188   return context->AppCoreBase::OnReceive(type, tizen_base::Bundle(b));
189 }
190
191 extern "C" EXPORT_API int appcore_base_on_create(void) {
192   auto* context = AppCoreBase::GetContext();
193   if (!context)
194     return -1;
195
196   return context->AppCoreBase::OnCreate();
197 }
198
199 extern "C" EXPORT_API int appcore_base_on_control(bundle* b) {
200   auto* context = AppCoreBase::GetContext();
201   if (!context)
202     return -1;
203
204   return context->AppCoreBase::OnControl(
205       b ? tizen_base::Bundle(b) : tizen_base::Bundle());
206 }
207
208 extern "C" EXPORT_API int appcore_base_on_terminate(void) {
209   auto* context = AppCoreBase::GetContext();
210   if (!context)
211     return -1;
212
213   return context->AppCoreBase::OnTerminate();
214 }
215
216 extern "C" EXPORT_API int appcore_base_on_set_i18n(void) {
217   auto* context = AppCoreBase::GetContext();
218   if (!context)
219     return -1;
220
221   return context->AppCoreBase::OnSetI18n();
222 }
223
224 extern "C" EXPORT_API void appcore_base_on_set_event(
225     enum appcore_base_event event) {
226   auto* context = AppCoreBase::GetContext();
227   if (!context)
228     return;
229
230   context->AppCoreBase::OnSetEvent(
231       static_cast<AppCoreBase::IEvent::Type>(event));
232 }
233
234 extern "C" EXPORT_API void appcore_base_on_unset_event(
235     enum appcore_base_event event) {
236   auto* context = AppCoreBase::GetContext();
237   if (!context)
238     return;
239
240   context->AppCoreBase::OnUnsetEvent(
241       static_cast<AppCoreBase::IEvent::Type>(event));
242 }
243
244 extern "C" EXPORT_API int appcore_base_on_trim_memory(void) {
245   auto* context = AppCoreBase::GetContext();
246   if (!context)
247     return -1;
248
249   return context->AppCoreBase::OnTrimMemory();
250 }
251
252 static std::unique_ptr<AppCore> context;
253
254 extern "C" EXPORT_API int appcore_base_init(appcore_base_ops ops,
255     int argc, char** argv, void* data) {
256   context.reset(new AppCore(ops, data));
257   try {
258     for (auto& event : __event_list)
259       context->AddEvent(std::move(event));
260
261     __event_list.clear();
262     context->Init(argc, argv);
263   } catch (const std::runtime_error&) {
264     return -1;
265   }
266
267   return 0;
268 }
269
270 extern "C" EXPORT_API void appcore_base_fini(void) {
271   if (!context)
272     return;
273   context->Fini();
274   context.reset();
275 }
276
277 extern "C" EXPORT_API appcore_base_ops appcore_base_get_default_ops(void) {
278   appcore_base_ops ops;
279
280   ops.create = __on_create;
281   ops.control = __on_control;
282   ops.terminate = __on_terminate;
283   ops.receive = __on_receive;
284   ops.set_i18n = __on_set_i18n;
285   ops.init = nullptr;
286   ops.finish = nullptr;
287   ops.run = nullptr;
288   ops.exit = nullptr;
289   ops.set_event = __on_set_event;
290   ops.unset_event = __on_unset_event;
291   ops.trim_memory = __on_trim_memory;
292
293   return ops;
294 }
295
296 extern "C" EXPORT_API appcore_base_event_h appcore_base_add_event(
297     enum appcore_base_event event,
298     appcore_base_event_cb cb,
299     void* data) {
300   auto* ev = new std::shared_ptr<AppCore::AppCoreEvent>(
301       std::make_shared<AppCore::AppCoreEvent>(event, cb, data));
302   if (ev == nullptr)
303     return nullptr;
304
305   auto* context = AppCoreBase::GetContext();
306   if (!context) {
307     __event_list.push_back(*ev);
308   } else {
309     context->AddEvent(*ev);
310   }
311
312   return static_cast<appcore_base_event_h>(ev);
313 }
314
315 extern "C" EXPORT_API int appcore_base_remove_event(
316     appcore_base_event_h handle) {
317   auto* ev = static_cast<std::shared_ptr<AppCoreBase::EventBase>*>(handle);
318   if (ev == nullptr)
319     return -1;
320
321   int ret;
322   auto* context = AppCoreBase::GetContext();
323   if (!context) {
324     ret = -1;
325
326     for (auto it = __event_list.begin(); it != __event_list.end(); ++it) {
327       if (*it == *ev) {
328         it = __event_list.erase(it);
329         ret = 0;
330         break;
331       }
332     }
333   } else {
334     ret = (context->RemoveEvent(*ev) ? 0 : -1);
335   }
336
337   delete ev;
338   return ret;
339 }
340
341 extern "C" DEPRECATED EXPORT_API int appcore_base_raise_event(
342     void* event,
343     enum appcore_base_event type) {
344   LOGE("This api was deprecated since 7.0");
345   auto* context = AppCoreBase::GetContext();
346   if (!context)
347     return -1;
348
349   return 0;
350 }
351
352 extern "C" EXPORT_API int appcore_base_flush_memory(void) {
353   auto* context = AppCoreBase::GetContext();
354   if (!context)
355     return -1;
356
357   context->FlushMemory();
358   return 0;
359 }
360
361 extern "C" DEPRECATED EXPORT_API int appcore_base_get_rotation_state(
362     enum appcore_base_rm* curr) {
363   LOGE("This api was deprecated since 7.0");
364   auto* context = AppCoreBase::GetContext();
365   if (!context)
366     return -1;
367
368   return 0;
369 }
370
371 extern "C" EXPORT_API bool appcore_base_is_bg_allowed(void) {
372   auto* context = AppCoreBase::GetContext();
373   if (!context)
374     return false;
375
376   return context->IsBgAllowed();
377 }
378
379 extern "C" EXPORT_API bool appcore_base_is_suspended(void) {
380   auto* context = AppCoreBase::GetContext();
381   if (!context)
382     return false;
383
384   return context->IsSuspended();
385 }
386
387 extern "C" EXPORT_API void appcore_base_toggle_suspended_state(void) {
388   auto* context = AppCoreBase::GetContext();
389   if (!context)
390     return;
391
392   context->ToggleSuspendedState();
393 }
394
395 extern "C" EXPORT_API int appcore_base_set_i18n(const char* domain_name,
396                                                 const char* dir_name) {
397   auto* context = AppCoreBase::GetContext();
398   if (!context || domain_name == nullptr || dir_name == nullptr)
399     return -1;
400
401   return context->SetI18n(domain_name, dir_name);
402 }
403
404 extern "C" EXPORT_API void appcore_base_exit(void) {
405   aul_status_update(STATUS_DYING);
406   auto* context = AppCoreBase::GetContext();
407   if (!context)
408     return;
409
410   context->Exit();
411 }
412
413 extern "C" EXPORT_API void appcore_base_add_suspend_timer(void) {
414   auto* context = AppCoreBase::GetContext();
415   if (!context)
416     return;
417
418   context->AddSuspendTimer();
419 }
420
421 extern "C" EXPORT_API void appcore_base_remove_suspend_timer(void) {
422   auto* context = AppCoreBase::GetContext();
423   if (!context)
424     return;
425
426   context->RemoveSuspendTimer();
427 }
428
429 extern "C" EXPORT_API void appcore_base_set_display_state(int display_state) {
430   auto* context = AppCoreBase::GetContext();
431   if (!context)
432     return;
433
434   context->SetDisplayState(
435       static_cast<AppCoreBase::DisplayState>(display_state));
436 }
437
438 extern "C" EXPORT_API int appcore_base_get_display_state(void) {
439   auto* context = AppCoreBase::GetContext();
440   if (!context)
441     return -1;
442
443   return context->GetDisplayState();
444 }