From aed2c8f1c790aa3b9cbdc71ca669130bc220d95e Mon Sep 17 00:00:00 2001 From: "jh9216.park" Date: Tue, 13 Jul 2021 00:00:25 -0400 Subject: [PATCH 1/1] Add internal API for c++ Change-Id: I3c57fa79d63863cc17fc30e5559a90177b6d30b7 Signed-off-by: jh9216.park --- include/widget_app.hpp | 198 +++++++++++++++++++++++++++++++++ packaging/appcore-widget.spec | 1 + src/efl_base/CMakeLists.txt | 2 +- test/unit_tests/test_widget_app_cpp.cc | 56 ++++++++++ 4 files changed, 256 insertions(+), 1 deletion(-) create mode 100644 include/widget_app.hpp create mode 100644 test/unit_tests/test_widget_app_cpp.cc diff --git a/include/widget_app.hpp b/include/widget_app.hpp new file mode 100644 index 0000000..fb9a05d --- /dev/null +++ b/include/widget_app.hpp @@ -0,0 +1,198 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TIZEN_APPFW_WIDGET_APP_HPP_ +#define TIZEN_APPFW_WIDGET_APP_HPP_ + +#include +#include + +#include +#include +#include + +#include +#include + +namespace tizen_appfw { + +class WidgetAppBase : public app_common::AppBase< + decltype(widget_app_add_event_handler)*, + decltype(widget_app_remove_event_handler)*> { + public: + using Remover = decltype(widget_app_remove_event_handler)*; + + WidgetAppBase() + : AppBase(widget_app_add_event_handler, + widget_app_remove_event_handler) {} + + virtual void OnCreate() {} + virtual void OnTerminate() {} + + class InstanceBase { + public: + class Factory { + public: + ~Factory() = default; + virtual std::unique_ptr Create(widget_context_h h) = 0; + }; + + enum class DestroyType { + PERMANENT = 0x00, + TEMPORARY = 0x01, + }; + + InstanceBase(widget_context_h h) : handle_(h) {} + virtual ~InstanceBase() = default; + + virtual void OnCreate(const tizen_base::Bundle& content, int w, int h) = 0; + virtual void OnDestroy(DestroyType reason, tizen_base::Bundle* content) = 0; + virtual void OnPause() = 0; + virtual void OnResume() = 0; + virtual void OnResize(int w, int h) = 0; + virtual void OnUpdate(const tizen_base::Bundle& content, bool force) = 0; + widget_context_h GetHandle() const { + return handle_; + } + + void Finish() { + widget_app_terminate_context(handle_); + } + + std::string GetId() const { + return std::string(widget_app_get_id(handle_)); + } + + void SetContextInfo(const tizen_base::Bundle& info) { + widget_app_context_set_content_info(handle_, info.GetHandle()); + } + + void SetTitle(const std::string& title) { + widget_app_context_set_title(handle_, title.c_str()); + } + + Evas_Object* GetElmWin() const { + Evas_Object* win = nullptr; + widget_app_get_elm_win(handle_, &win); + return win; + } + + private: + widget_context_h handle_; + }; + + int Run(int argc, char** argv, + std::unique_ptr factory) { + factory_ = std::move(factory); + widget_app_lifecycle_callback_s callback = { + create : [](void *user_data) -> widget_class_h { + WidgetAppBase* b = static_cast(user_data); + b->OnCreate(); + widget_instance_lifecycle_callback_s callback = { + create : [](widget_context_h context, bundle* content, int w, int h, + void* user_data) -> int { + WidgetAppBase* b = static_cast(user_data); + auto i = b->factory_->Create(context); + i->OnCreate(tizen_base::Bundle(content, false, false), w, h); + b->instances_.push_back(std::move(i)); + return 0; + }, + destroy : [](widget_context_h context, + widget_app_destroy_type_e reason, bundle* content, + void* user_data) -> int { + WidgetAppBase* b = static_cast(user_data); + + for (auto& i : b->instances_) { + if (i->GetHandle() == context) { + tizen_base::Bundle bd(content, false, false); + i->OnDestroy( + static_cast(reason), &bd); + b->instances_.remove(i); + break; + } + } + + return 0; + }, + pause : [](widget_context_h context, void* user_data) -> int { + WidgetAppBase* b = static_cast(user_data); + auto* i = b->FindInst(context); + if (!i) + return -1; + i->OnPause(); + return 0; + }, + resume : [](widget_context_h context, void* user_data) -> int { + WidgetAppBase* b = static_cast(user_data); + auto* i = b->FindInst(context); + if (!i) + return -1; + i->OnResume(); + return 0; + }, + resize : [](widget_context_h context, int w, int h, + void* user_data) -> int { + WidgetAppBase* b = static_cast(user_data); + auto* i = b->FindInst(context); + if (!i) + return -1; + i->OnResize(w, h); + return 0; + }, + update : [](widget_context_h context, bundle* content, int force, + void* user_data) -> int { + WidgetAppBase* b = static_cast(user_data); + auto* i = b->FindInst(context); + if (!i) + return -1; + i->OnUpdate(tizen_base::Bundle(content), force); + return 0; + } + }; + auto h = widget_app_class_create(callback, user_data); + return h; + }, + terminate : [](void* user_data) { + WidgetAppBase* b = static_cast(user_data); + b->OnTerminate(); + } + }; + + return widget_app_main(argc, argv, &callback, this); + } + + void Exit() noexcept { + widget_app_exit(); + } + + private: + InstanceBase* FindInst(widget_context_h h) { + for (auto& i : instances_) { + if (i->GetHandle() == h) { + return i.get(); + } + } + + return nullptr; + } + + std::unique_ptr factory_; + std::list> instances_; +}; + +} // namespace tizen_appfw + +#endif // TIZEN_APPFW_WIDGET_APP_HPP_ diff --git a/packaging/appcore-widget.spec b/packaging/appcore-widget.spec index 1c4ac32..de8148a 100644 --- a/packaging/appcore-widget.spec +++ b/packaging/appcore-widget.spec @@ -136,6 +136,7 @@ install -m 0644 appcore-widget.zip %{buildroot}%{_datadir}/gcov/ %files -n capi-appfw-widget-application-devel /usr/include/appfw/widget_app.h +/usr/include/appfw/widget_app.hpp /usr/include/appfw/widget_app_efl.h /usr/include/appfw/widget_app_internal.h %{_libdir}/pkgconfig/capi-appfw-widget-application.pc diff --git a/src/efl_base/CMakeLists.txt b/src/efl_base/CMakeLists.txt index e9e7311..610bb00 100644 --- a/src/efl_base/CMakeLists.txt +++ b/src/efl_base/CMakeLists.txt @@ -22,5 +22,5 @@ INSTALL(TARGETS ${TARGET_WIDGET_APPLICATION} DESTINATION ${LIB_INSTALL_DIR}) INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/../../capi-appfw-widget-application.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/include/ DESTINATION include/appfw/ FILES_MATCHING - PATTERN "*.h" + PATTERN "*.h*" ) diff --git a/test/unit_tests/test_widget_app_cpp.cc b/test/unit_tests/test_widget_app_cpp.cc new file mode 100644 index 0000000..b41c249 --- /dev/null +++ b/test/unit_tests/test_widget_app_cpp.cc @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +namespace { + +class WidgetApp : public tizen_appfw::WidgetAppBase { + public: + class Instance : public InstanceBase { + public: + class Factory : public InstanceBase::Factory { + public: + std::unique_ptr Create(widget_context_h h) override { + return std::unique_ptr(new Instance(h)); + } + }; + + Instance(widget_context_h h) : InstanceBase(h) {} + void OnCreate(const tizen_base::Bundle& content, int w, int h) override {} + void OnDestroy(DestroyType reason, tizen_base::Bundle* content) override {} + void OnPause() override {} + void OnResume() override {} + void OnResize(int w, int h) override {} + void OnUpdate(const tizen_base::Bundle& content, bool force) override {} + }; + + WidgetApp() {} + void OnCreate() override {} + void OnTerminate() override {} +}; + +} // namespace + +TEST(WidgetAppCppTest, Run_InvalidParameter) { + WidgetApp app; + std::unique_ptr factory( + new WidgetApp::Instance::Factory()); + + int ret = app.Run(0, nullptr, std::move(factory)); + EXPECT_EQ(ret, APP_ERROR_INVALID_PARAMETER); +} \ No newline at end of file -- 2.7.4