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