Fix create return condition
[platform/core/appfw/app-core.git] / legacy / src / legacy / appcore_efl.cc
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd. All rights reserved.
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 <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include <list>
22 #include <memory>
23
24 #include "app_core_efl_base.hh"
25 #include "appcore-efl.h"
26 #include "common/log_private.hh"
27
28 namespace {
29
30 using namespace tizen_cpp;
31
32 class EflAppContext : public AppCoreEflBase {
33  public:
34   enum AppState {
35     APP_STATE_NOT_RUNNING,
36     APP_STATE_CREATING,
37     APP_STATE_RUNNING,
38   };
39
40   EflAppContext(struct appcore_ops* callback, unsigned int hint)
41       : AppCoreEflBase(hint), callback_(callback) {
42     SetAppState(APP_STATE_CREATING);
43   }
44
45   void Run(int argc, char** argv) override {
46     SetAppState(APP_STATE_RUNNING);
47     AppCoreEflBase::Run(argc, argv);
48     SetAppState(APP_STATE_NOT_RUNNING);
49   }
50
51   int OnCreate() override {
52     AppCoreEflBase::OnCreate();
53     if (callback_->create == nullptr ||
54         callback_->create(callback_->data) < 0)
55       return -1;
56
57     return 0;
58   }
59
60   int OnControl(tizen_base::Bundle b) override {
61     AppCoreEflBase::OnControl(b);
62
63     if (callback_->reset)
64       callback_->reset(b.GetHandle(), callback_->data);
65
66     return 0;
67   }
68
69   int OnTerminate() override {
70     if (callback_->terminate)
71       callback_->terminate(callback_->data);
72
73     return 0;
74   }
75
76   int OnPause() override {
77     AppCoreEflBase::OnPause();
78
79     if (callback_->pause)
80       callback_->pause(callback_->data);
81
82     return 0;
83   }
84
85   int OnResume() override {
86     AppCoreEflBase::OnResume();
87
88     if (callback_->resume)
89       callback_->resume(callback_->data);
90
91     return 0;
92   }
93
94   AppState GetAppState() const { return state_; }
95
96   void SetAppState(AppState state) { state_ = state; }
97
98  private:
99   struct appcore_ops* callback_;
100   AppState state_ = APP_STATE_NOT_RUNNING;
101 };
102
103 std::unique_ptr<EflAppContext> __context;
104
105 }  // namespace
106
107 EXPORT_API int appcore_efl_init(const char* name,
108                                 int* argc,
109                                 char*** argv,
110                                 struct appcore_ops* ops) {
111   if (*argc < 1 || *argv == nullptr || ops == nullptr) {
112     _E("Invalid parameter");
113     return -1;
114   }
115
116   if (ops->create == nullptr) {
117     _E("app_create_cb() callback MUST be registerted");
118     return -1;
119   }
120
121   if (__context.get() != nullptr &&
122       __context->GetAppState() != EflAppContext::APP_STATE_NOT_RUNNING) {
123     _E("Already running");
124     return -1;
125   }
126
127   unsigned int hint = AppCoreEflBase::HINT_WINDOW_GROUP_CONTROL |
128                       AppCoreEflBase::HINT_WINDOW_STACK_CONTROL |
129                       AppCoreEflBase::HINT_BG_LAUNCH_CONTROL |
130                       AppCoreEflBase::HINT_HW_ACC_CONTROL |
131                       AppCoreEflBase::HINT_WINDOW_AUTO_CONTROL |
132                       AppCoreEflBase::HINT_LEGACY_CONTROL |
133                       AppCoreEflBase::HINT_WINDOW_ID_CONTROL;
134
135   LOGW("appcore_efl_init()");
136   try {
137     __context = std::make_unique<EflAppContext>(ops, hint);
138     __context->Run(*argc, *argv);
139   } catch (std::runtime_error& e) {
140     __context->SetAppState(EflAppContext::APP_STATE_NOT_RUNNING);
141   }
142
143   return 0;
144 }
145
146 EXPORT_API void appcore_efl_fini(void) {
147   LOGW("appcore_efl_fini()");
148   if (__context.get() &&
149       __context->GetAppState() == EflAppContext::APP_STATE_RUNNING)
150     __context->Exit();
151 }
152
153 EXPORT_API int appcore_efl_main(const char* name,
154                                 int* argc,
155                                 char*** argv,
156                                 struct appcore_ops* ops) {
157   int r = appcore_efl_init(name, argc, argv, ops);
158   if (r < 0)
159     return r;
160
161   appcore_efl_fini();
162   return 0;
163 }
164
165 EXPORT_API void appcore_group_attach() {
166   if (__context == nullptr)
167     return;
168
169   __context->GroupAdd();
170 }
171
172 EXPORT_API void appcore_group_lower() {
173   if (__context == nullptr)
174     return;
175
176   __context->GroupRemove();
177 }
178
179 EXPORT_API unsigned int appcore_get_main_window(void) {
180   if (__context == nullptr)
181     return 0;
182
183   return __context->GetMainWindow();
184 }
185
186 EXPORT_API unsigned int appcore_get_main_surface(void) {
187   if (__context == nullptr)
188     return 0;
189
190   return __context->GetMainSurface();
191 }
192
193 EXPORT_API int appcore_set_system_resource_reclaiming(bool enable) {
194   if (__context == nullptr)
195     return -1;
196
197   __context->SetSystemResourceReclaiming(enable);
198   return 0;
199 }