ForCAX/entry/src/main/cpp/napi_init.cpp
2026-02-20 20:43:25 +08:00

374 lines
11 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "napi/native_api.h"
//#include "OCCTRenderThread/OCCTRenderThread.h"
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <map>
#include <mutex>
//#include <ace/xcomponent/native_interface_xcomponent.h>
//using namespace OCCTRenderer;
//
//// 存储XComponent ID到渲染线程的映射
//static std::map<int64_t, std::shared_ptr<OCCTRenderThread>> renderThreadMap;
//static std::mutex mapMutex;
//
//// XComponent回调
//static void OnSurfaceCreated(XComponent* component, void* window)
//{
// OH_NativeXComponent* nativeXComponent = reinterpret_cast<OH_NativeXComponent*>(component);
// int64_t id = 0;
// OH_NativeXComponent_GetXComponentId(nativeXComponent, &id);
// {
// std::lock_guard<std::mutex> lock(mapMutex);
// if (renderThreadMap.find(id) == renderThreadMap.end()) {
// auto renderThread = std::make_shared<OCCTRenderThread>();
// renderThreadMap[id] = renderThread;
//
// // 启动渲染线程
// renderThread->start(reinterpret_cast<OHNativeWindow*>(window));
// }
// }
//
// printf("Surface created for component id: %lld\n", static_cast<long long>(id));
//}
//
//static void OnSurfaceChanged(XComponent* component, void* window, int32_t width, int32_t height)
//{
// OH_NativeXComponent* nativeXComponent = reinterpret_cast<OH_NativeXComponent*>(component);
// int64_t id = 0;
// OH_NativeXComponent_GetXComponentId(nativeXComponent, &id);
//
// {
// std::lock_guard<std::mutex> lock(mapMutex);
// if (renderThreadMap.find(id) != renderThreadMap.end()) {
// renderThreadMap[id]->resizeWindow(width, height);
// }
// }
//
// printf("Surface changed for component id: %lld, width: %d, height: %d\n",
// static_cast<long long>(id), width, height);
//}
//
//static void OnSurfaceDestroyed(XComponent* component, void* window)
//{
// OH_NativeXComponent* nativeXComponent = reinterpret_cast<OH_NativeXComponent*>(component);
// int64_t id = 0;
// OH_NativeXComponent_GetXComponentId(nativeXComponent, &id);
//
// {
// std::lock_guard<std::mutex> lock(mapMutex);
// if (renderThreadMap.find(id) != renderThreadMap.end()) {
// renderThreadMap[id]->stop();
// renderThreadMap.erase(id);
// }
// }
//
// printf("Surface destroyed for component id: %lld\n", static_cast<long long>(id));
//}
//
//// 渲染完成回调
//static void OnRenderComplete()
//{
// // 这里可以通知JS层渲染完成例如发送事件
//}
//
//// 旋转模型
//static napi_value RotateModel(napi_env env, napi_value info)
//{
// size_t argc = 3;
// napi_value args[3] = {nullptr};
// napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
//
// if (argc < 3) {
// napi_throw_error(env, nullptr, "Wrong number of arguments");
// return nullptr;
// }
//
// int64_t id = 0;
// double xAngle = 0.0;
// double yAngle = 0.0;
//
// napi_get_value_int64(env, args[0], &id);
// napi_get_value_double(env, args[1], &xAngle);
// napi_get_value_double(env, args[2], &yAngle);
//
// {
// std::lock_guard<std::mutex> lock(mapMutex);
// if (renderThreadMap.find(id) != renderThreadMap.end()) {
// renderThreadMap[id]->setRotation(static_cast<float>(xAngle), static_cast<float>(yAngle));
// }
// }
//
// return nullptr;
//}
//
//// 平移模型
//static napi_value TranslateModel(napi_env env, napi_value info)
//{
// size_t argc = 3;
// napi_value args[3] = {nullptr};
// napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
//
// if (argc < 3) {
// napi_throw_error(env, nullptr, "Wrong number of arguments");
// return nullptr;
// }
//
// int64_t id = 0;
// double x = 0.0;
// double y = 0.0;
//
// napi_get_value_int64(env, args[0], &id);
// napi_get_value_double(env, args[1], &x);
// napi_get_value_double(env, args[2], &y);
//
// {
// std::lock_guard<std::mutex> lock(mapMutex);
// if (renderThreadMap.find(id) != renderThreadMap.end()) {
// renderThreadMap[id]->setTranslation(static_cast<float>(x), static_cast<float>(y));
// }
// }
//
// return nullptr;
//}
//
//// 重置视图
//static napi_value ResetView(napi_env env, napi_value info)
//{
// size_t argc = 1;
// napi_value args[1] = {nullptr};
// napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
//
// if (argc < 1) {
// napi_throw_error(env, nullptr, "Wrong number of arguments");
// return nullptr;
// }
//
// int64_t id = 0;
// napi_get_value_int64(env, args[0], &id);
//
// {
// std::lock_guard<std::mutex> lock(mapMutex);
// if (renderThreadMap.find(id) != renderThreadMap.end()) {
// renderThreadMap[id]->resetView();
// }
// }
//
// return nullptr;
//}
//
//// 设置背景颜色
//static napi_value SetClearColor(napi_env env, napi_value info)
//{
// size_t argc = 5;
// napi_value args[5] = {nullptr};
// napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
//
// if (argc < 5) {
// napi_throw_error(env, nullptr, "Wrong number of arguments");
// return nullptr;
// }
//
// int64_t id = 0;
// double r = 0.0, g = 0.0, b = 0.0, a = 0.0;
//
// napi_get_value_int64(env, args[0], &id);
// napi_get_value_double(env, args[1], &r);
// napi_get_value_double(env, args[2], &g);
// napi_get_value_double(env, args[3], &b);
// napi_get_value_double(env, args[4], &a);
//
// {
// std::lock_guard<std::mutex> lock(mapMutex);
// if (renderThreadMap.find(id) != renderThreadMap.end()) {
// renderThreadMap[id]->setClearColor(static_cast<float>(r), static_cast<float>(g),
// static_cast<float>(b), static_cast<float>(a));
// }
// }
//
// return nullptr;
//}
//
//// 加载STEP模型
//static napi_value LoadStepModel(napi_env env, napi_value info)
//{
// size_t argc = 2;
// napi_value args[2] = {nullptr};
// napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
//
// if (argc < 2) {
// napi_throw_error(env, nullptr, "Wrong number of arguments");
// return nullptr;
// }
//
// int64_t id = 0;
// char path[1024] = {0};
// size_t pathLen = 0;
//
// napi_get_value_int64(env, args[0], &id);
// napi_get_value_string_utf8(env, args[1], path, sizeof(path), &pathLen);
//
// {
// std::lock_guard<std::mutex> lock(mapMutex);
// if (renderThreadMap.find(id) != renderThreadMap.end()) {
// renderThreadMap[id]->loadModel(std::string(path));
// }
// }
//
// return nullptr;
//}
//
//// 注册模块
//static napi_value Init(napi_env env, napi_value exports)
//{
// // 注册XComponent回调
// OH_NativeXComponent_Callback callback = {
// .OnSurfaceCreated = OnSurfaceCreated,
// .OnSurfaceChanged = OnSurfaceChanged,
// .OnSurfaceDestroyed = OnSurfaceDestroyed
// };
//
// napi_property_descriptor desc[] = {
// DECLARE_NAPI_FUNCTION("rotateModel", RotateModel),
// DECLARE_NAPI_FUNCTION("translateModel", TranslateModel),
// DECLARE_NAPI_FUNCTION("resetView", ResetView),
// DECLARE_NAPI_FUNCTION("setClearColor", SetClearColor),
// DECLARE_NAPI_FUNCTION("loadStepModel", LoadStepModel)
// };
//
// NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
//
// return exports;
//}
//
//EXTERN_C_START
///*
// * 定义函数用于注册NAPI模块
// */
//static napi_module OCCTModel = {
// .nm_version = 1,
// .nm_flags = 0,
// .nm_filename = nullptr,
// .nm_register_func = Init,
// .nm_modname = "OCCTRender",
// .nm_priv = ((void*)0),
// .reserved = {0}
//};
//
///*
// * 模块注册函数
// */
//extern "C" void NAPI_occtrenderer_GetJSClass(void)
//{
// napi_module_register(&OCCTModel);
//}
//EXTERN_C_END
#include "curl/curl.h"
// [StartExclude curl_request_cpp]
#include "napi/native_api.h"
#include "hilog/log.h"
#include <string>
using namespace std;
// [EndExclude curl_request_cpp]
// Get request and post request data response functions
size_t ReqReply(void *ptr, size_t size, size_t nmemb, void *userdata) {
string *str = reinterpret_cast<string *>(userdata);
(*str).append((char *)ptr, size * nmemb);
return size * nmemb;
}
// http GET Request configuration
CURLcode CurlGetReq(const std::string &url, std::string &response) {
// Curl initialization
CURL *curl = curl_easy_init();
// Curl return value
CURLcode res;
if (curl) {
// Set the request header for Curl
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type:application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// Set the URL address for the request
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
// Receive response header data, 0 represents not receiving, 1 represents receiving
curl_easy_setopt(curl, CURLOPT_HEADER, 1);
// Set data receiving function
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ReqReply);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
// Set to not use any signal/alarm handlers
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
// Set timeout period
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
// Open request
res = curl_easy_perform(curl);
}
// Release curl
curl_easy_cleanup(curl);
return res;
}
static napi_value NatReq(napi_env env, napi_callback_info info) {
string getUrlStr = "http://www.baidu.com";
string getResponseStr;
auto res = CurlGetReq(getUrlStr, getResponseStr);
if (res == CURLE_OK) {
OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "pure", "response: \n%{public}s", getResponseStr.c_str());
}
// [StartExclude curl_request_cpp]
size_t requireArgc = 2;
size_t argc = 2;
napi_value args[2] = {nullptr};
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
napi_valuetype valuetype0;
napi_typeof(env, args[0], &valuetype0);
napi_valuetype valuetype1;
napi_typeof(env, args[1], &valuetype1);
double value0;
napi_get_value_double(env, args[0], &value0);
double value1;
napi_get_value_double(env, args[1], &value1);
napi_value sum;
napi_create_double(env, value0 + value1, &sum);
return sum;
// [EndExclude curl_request_cpp]
}
// [End curl_request_cpp]
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor desc[] = {{"add", nullptr, NatReq, nullptr, nullptr, nullptr, napi_default, nullptr}};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
EXTERN_C_END
static napi_module nativecurl = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "nativecurl",
.nm_priv = ((void *)0),
.reserved = {0},
};
extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&nativecurl); }