From: Youngsun Suh Date: Mon, 20 Jan 2025 02:26:10 +0000 (+0900) Subject: Add C Bindings for rust link X-Git-Tag: accepted/tizen/unified/20250124.043542~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F19%2F318519%2F1;p=platform%2Fcore%2Fuifw%2Faurum.git Add C Bindings for rust link Change-Id: I96dae3ed444c58901a6fde73953c07b8be266a0c --- diff --git a/libaurum/inc/AurumCBindings.h b/libaurum/inc/AurumCBindings.h new file mode 100644 index 0000000..d940599 --- /dev/null +++ b/libaurum/inc/AurumCBindings.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2025 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 AURUM_C_BINDINGS_H +#define AURUM_C_BINDINGS_H + +#ifdef __cplusplus +extern "C" { +#endif + // Initialize the Aurum library. + void aurum_init(); + // Dump current screen as JSON string. + const char* dump_screen(); + // Free the allocated string memory. + void free_string_result(const char* ptr); +#ifdef __cplusplus +} +#endif + +#endif // AURUM_C_BINDINGS_H \ No newline at end of file diff --git a/libaurum/meson.build b/libaurum/meson.build index daa5648..7f199a1 100644 --- a/libaurum/meson.build +++ b/libaurum/meson.build @@ -86,3 +86,9 @@ libaurum = declare_dependency(link_with: libaurum_lib, install_headers(libaurum_install_inc, ) + +if get_option('build_c_bindings') == true + c_bindings_dir = dir_include + '/c_bindings' + message('install c bindings header to ' + c_bindings_dir) + install_headers('./inc/AurumCBindings.h', install_dir: c_bindings_dir,) +endif diff --git a/libaurum/src/c_bindings/AurumCBindings.cc b/libaurum/src/c_bindings/AurumCBindings.cc new file mode 100644 index 0000000..9aa581f --- /dev/null +++ b/libaurum/src/c_bindings/AurumCBindings.cc @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2025 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 "AurumCBindings.h" +#include "Aurum.h" +#include + +using namespace Aurum; + +namespace +{ + constexpr const char WINDOWS_KEY[] = "\"windows\""; +} + +void aurum_init() +{ + LOGI("aurum_init"); + AccessibleWatcher::getInstance(); +} + +const char* dump_screen() +{ + LOGI("dump_screen"); + auto device = UiDevice::getInstance(); + auto windowRoots = device->getWindowRoot(); + std::string dumpResult{WINDOWS_KEY}; + dumpResult += " : ["; + + for (const auto& root: windowRoots) { + // TODO: implement dump logic + // root->dump(); + dumpResult += "{},"; + } + + // remove last comma if exists. + if(dumpResult.back() == ',') { + dumpResult.pop_back(); + } + + dumpResult += "]"; + char* cResult = new char[dumpResult.size() + 1]; // Consumers must free this memory later + std::strcpy(cResult, dumpResult.c_str()); + return cResult; + +} + +void free_string_result(const char* ptr) +{ + LOGI("free_string_result"); + delete[] ptr; +} \ No newline at end of file diff --git a/libaurum/src/c_bindings/meson.build b/libaurum/src/c_bindings/meson.build new file mode 100644 index 0000000..dce0bab --- /dev/null +++ b/libaurum/src/c_bindings/meson.build @@ -0,0 +1,3 @@ +libaurum_src += [ + files('AurumCBindings.cc'), +] diff --git a/libaurum/src/meson.build b/libaurum/src/meson.build index 17595be..261bc74 100644 --- a/libaurum/src/meson.build +++ b/libaurum/src/meson.build @@ -16,3 +16,7 @@ libaurum_src += [ subdir('Accessibility') subdir('Impl') subdir('Runnable') + +if get_option('build_c_bindings') == true + subdir('c_bindings') +endif diff --git a/meson_options.txt b/meson_options.txt index 3ca0306..386c0c1 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -32,3 +32,9 @@ option('libaurum_only', value: false, description: 'skip building grpc and protobuf protocol' ) + +option('build_c_bindings', + type: 'boolean', + value: false, + description: 'build c bindings to be called from rust code' +) \ No newline at end of file diff --git a/tests/Test_CBindings.cc b/tests/Test_CBindings.cc new file mode 100644 index 0000000..564c7b7 --- /dev/null +++ b/tests/Test_CBindings.cc @@ -0,0 +1,16 @@ +#include + +#include + +TEST(CBindingsTest, DumpScreen) +{ + aurum_init(); + const char *c_str = dump_screen(); + EXPECT_NE(c_str, nullptr); + free_string_result(c_str); +} + +TEST(CBindingsTest, FreeStringResultWorksWithNull) +{ + free_string_result(nullptr); +} \ No newline at end of file diff --git a/tests/meson.build b/tests/meson.build index 7d668fe..1e52a62 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -6,6 +6,10 @@ test_src = [ ['test_uiselector', files('Test_UiSelector.cc')], ] +if get_option('build_c_bindings') == true + test_src += [['Test_CBindings', files('Test_CBindings.cc')]] +endif + test_inc = [ include_directories('./') ]