feat: add minimal HarmonyOS native napi skeleton

This commit is contained in:
OpenClaw committed 2026-03-09 17:05:17 +08:00
1 parent e328780ee5
commit 8b6e24fa61
11 files changed
+565

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.5)
project(mrp_napi)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_library(mrp_napi SHARED
napi/module_init.cpp
napi/facade_binding.cpp
platform/MrpFacade.cpp
runtime/MrpRuntime.cpp
render/FrameBuffer.cpp
input/InputDispatcher.cpp
)
target_include_directories(mrp_napi PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/platform
${CMAKE_CURRENT_SOURCE_DIR}/runtime
${CMAKE_CURRENT_SOURCE_DIR}/render
${CMAKE_CURRENT_SOURCE_DIR}/input
${CMAKE_CURRENT_SOURCE_DIR}/napi
)
@@ -0,0 +1,7 @@
#include "InputEvent.h"
bool DispatchInput(const MrpInputEvent &event)
{
(void)event;
return true;
}
+18
View File
@@ -0,0 +1,18 @@
#ifndef MRP_INPUT_INPUT_EVENT_H
#define MRP_INPUT_INPUT_EVENT_H
#include <cstdint>
#include <string>
struct MrpInputEvent {
std::string type = "key";
std::string action = "down";
int32_t keyCode = 0;
int32_t x = 0;
int32_t y = 0;
int64_t timestamp = 0;
};
bool DispatchInput(const MrpInputEvent &event);
#endif
+224
View File
@@ -0,0 +1,224 @@
#include <node_api.h>
#include <cstring>
#include <string>
#include <vector>
#include "../input/InputEvent.h"
#include "../platform/MrpFacade.h"
namespace {
MrpFacade g_facade;
napi_value MakeString(napi_env env, const std::string &value)
{
napi_value result;
napi_create_string_utf8(env, value.c_str(), value.size(), &result);
return result;
}
napi_value MakeCommonResult(napi_env env, const CommonResult &result)
{
napi_value obj;
napi_create_object(env, &obj);
napi_value ok;
napi_get_boolean(env, result.ok, &ok);
napi_set_named_property(env, obj, "ok", ok);
napi_value errorCode;
napi_create_int32(env, result.errorCode, &errorCode);
napi_set_named_property(env, obj, "errorCode", errorCode);
napi_set_named_property(env, obj, "errorMessage", MakeString(env, result.errorMessage));
return obj;
}
std::string GetStringProperty(napi_env env, napi_value obj, const char *name, const std::string &fallback)
{
bool hasProperty = false;
napi_has_named_property(env, obj, name, &hasProperty);
if (!hasProperty) {
return fallback;
}
napi_value value;
napi_get_named_property(env, obj, name, &value);
size_t length = 0;
napi_get_value_string_utf8(env, value, nullptr, 0, &length);
std::string result(length, '\0');
napi_get_value_string_utf8(env, value, result.data(), length + 1, &length);
return result;
}
int32_t GetIntProperty(napi_env env, napi_value obj, const char *name, int32_t fallback)
{
bool hasProperty = false;
napi_has_named_property(env, obj, name, &hasProperty);
if (!hasProperty) {
return fallback;
}
napi_value value;
napi_get_named_property(env, obj, name, &value);
int32_t result = fallback;
napi_get_value_int32(env, value, &result);
return result;
}
bool GetBoolProperty(napi_env env, napi_value obj, const char *name, bool fallback)
{
bool hasProperty = false;
napi_has_named_property(env, obj, name, &hasProperty);
if (!hasProperty) {
return fallback;
}
napi_value value;
napi_get_named_property(env, obj, name, &value);
bool result = fallback;
napi_get_value_bool(env, value, &result);
return result;
}
napi_value Init(napi_env env, napi_callback_info info)
{
size_t argc = 1;
napi_value argv[1] = {nullptr};
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
InitOptions options;
if (argc == 1 && argv[0] != nullptr) {
options.workDir = GetStringProperty(env, argv[0], "workDir", "");
options.width = GetIntProperty(env, argv[0], "width", 240);
options.height = GetIntProperty(env, argv[0], "height", 320);
options.debug = GetBoolProperty(env, argv[0], "debug", false);
}
return MakeCommonResult(env, g_facade.Init(options));
}
napi_value LoadPackage(napi_env env, napi_callback_info info)
{
size_t argc = 1;
napi_value argv[1] = {nullptr};
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
std::string packagePath;
if (argc == 1 && argv[0] != nullptr) {
size_t length = 0;
napi_get_value_string_utf8(env, argv[0], nullptr, 0, &length);
packagePath.resize(length);
napi_get_value_string_utf8(env, argv[0], packagePath.data(), length + 1, &length);
}
return MakeCommonResult(env, g_facade.LoadPackage(packagePath));
}
napi_value Start(napi_env env, napi_callback_info info)
{
(void)info;
return MakeCommonResult(env, g_facade.Start());
}
napi_value Pause(napi_env env, napi_callback_info info)
{
(void)info;
return MakeCommonResult(env, g_facade.Pause());
}
napi_value Resume(napi_env env, napi_callback_info info)
{
(void)info;
return MakeCommonResult(env, g_facade.Resume());
}
napi_value SendInput(napi_env env, napi_callback_info info)
{
size_t argc = 1;
napi_value argv[1] = {nullptr};
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
MrpInputEvent event;
if (argc == 1 && argv[0] != nullptr) {
event.type = GetStringProperty(env, argv[0], "type", "key");
event.action = GetStringProperty(env, argv[0], "action", "down");
event.keyCode = GetIntProperty(env, argv[0], "keyCode", 0);
event.x = GetIntProperty(env, argv[0], "x", 0);
event.y = GetIntProperty(env, argv[0], "y", 0);
event.timestamp = static_cast<int64_t>(GetIntProperty(env, argv[0], "timestamp", 0));
}
return MakeCommonResult(env, g_facade.SendInput(event));
}
napi_value PullFrame(napi_env env, napi_callback_info info)
{
(void)info;
FrameResult frame = g_facade.PullFrame();
napi_value obj;
napi_create_object(env, &obj);
napi_value ok;
napi_get_boolean(env, frame.ok, &ok);
napi_set_named_property(env, obj, "ok", ok);
napi_value hasNewFrame;
napi_get_boolean(env, frame.hasNewFrame, &hasNewFrame);
napi_set_named_property(env, obj, "hasNewFrame", hasNewFrame);
napi_value width;
napi_create_int32(env, frame.width, &width);
napi_set_named_property(env, obj, "width", width);
napi_value height;
napi_create_int32(env, frame.height, &height);
napi_set_named_property(env, obj, "height", height);
napi_set_named_property(env, obj, "pixelFormat", MakeString(env, frame.pixelFormat));
void *data = nullptr;
napi_value buffer;
napi_create_arraybuffer(env, frame.buffer.size(), &data, &buffer);
if (data != nullptr && !frame.buffer.empty()) {
std::memcpy(data, frame.buffer.data(), frame.buffer.size());
}
napi_set_named_property(env, obj, "buffer", buffer);
napi_value frameId;
napi_create_int64(env, frame.frameId, &frameId);
napi_set_named_property(env, obj, "frameId", frameId);
napi_value errorCode;
napi_create_int32(env, frame.errorCode, &errorCode);
napi_set_named_property(env, obj, "errorCode", errorCode);
napi_set_named_property(env, obj, "errorMessage", MakeString(env, frame.errorMessage));
return obj;
}
napi_value Release(napi_env env, napi_callback_info info)
{
(void)info;
return MakeCommonResult(env, g_facade.Release());
}
} // namespace
napi_value ExportMrpFacade(napi_env env, napi_value exports)
{
napi_property_descriptor descriptors[] = {
{"init", nullptr, Init, nullptr, nullptr, nullptr, napi_default, nullptr},
{"loadPackage", nullptr, LoadPackage, nullptr, nullptr, nullptr, napi_default, nullptr},
{"start", nullptr, Start, nullptr, nullptr, nullptr, napi_default, nullptr},
{"pause", nullptr, Pause, nullptr, nullptr, nullptr, napi_default, nullptr},
{"resume", nullptr, Resume, nullptr, nullptr, nullptr, napi_default, nullptr},
{"sendInput", nullptr, SendInput, nullptr, nullptr, nullptr, napi_default, nullptr},
{"pullFrame", nullptr, PullFrame, nullptr, nullptr, nullptr, napi_default, nullptr},
{"release", nullptr, Release, nullptr, nullptr, nullptr, napi_default, nullptr},
};
napi_define_properties(env, exports, sizeof(descriptors) / sizeof(descriptors[0]), descriptors);
return exports;
}
+23
View File
@@ -0,0 +1,23 @@
#include <node_api.h>
napi_value ExportMrpFacade(napi_env env, napi_value exports);
static napi_value InitModule(napi_env env, napi_value exports)
{
return ExportMrpFacade(env, exports);
}
static napi_module g_mrpModule = {
1,
0,
nullptr,
InitModule,
"mrp_napi",
nullptr,
{0},
};
extern "C" __attribute__((constructor)) void RegisterMrpModule(void)
{
napi_module_register(&g_mrpModule);
}
+63
View File
@@ -0,0 +1,63 @@
#include "MrpFacade.h"
#include "../render/FrameBuffer.h"
#include "../runtime/MrpRuntime.h"
namespace {
MrpRuntime g_runtime;
}
CommonResult MrpFacade::Init(const InitOptions &options)
{
options_ = options;
return g_runtime.Init(options.width, options.height, options.debug);
}
CommonResult MrpFacade::LoadPackage(const std::string &packagePath)
{
packagePath_ = packagePath;
return g_runtime.LoadPackage(packagePath);
}
CommonResult MrpFacade::Start()
{
return g_runtime.Start();
}
CommonResult MrpFacade::Pause()
{
return g_runtime.Pause();
}
CommonResult MrpFacade::Resume()
{
return g_runtime.Resume();
}
CommonResult MrpFacade::SendInput(const MrpInputEvent &event)
{
DispatchInput(event);
return CommonResult{true, 0, ""};
}
FrameResult MrpFacade::PullFrame()
{
FrameBuffer frameBuffer(g_runtime.Width(), g_runtime.Height());
frameBuffer.FillTestPattern(g_runtime.NextFrameId());
FrameResult result;
result.ok = true;
result.hasNewFrame = true;
result.width = g_runtime.Width();
result.height = g_runtime.Height();
result.pixelFormat = "RGBA_8888";
result.buffer = frameBuffer.Data();
result.frameId = g_runtime.CurrentFrameId();
return result;
}
CommonResult MrpFacade::Release()
{
packagePath_.clear();
return g_runtime.Release();
}
+51
View File
@@ -0,0 +1,51 @@
#ifndef MRP_PLATFORM_MRP_FACADE_H
#define MRP_PLATFORM_MRP_FACADE_H
#include <cstdint>
#include <string>
#include <vector>
#include "../input/InputEvent.h"
struct InitOptions {
std::string workDir;
int32_t width = 240;
int32_t height = 320;
bool debug = false;
};
struct CommonResult {
bool ok = false;
int32_t errorCode = 0;
std::string errorMessage;
};
struct FrameResult {
bool ok = false;
bool hasNewFrame = false;
int32_t width = 0;
int32_t height = 0;
std::string pixelFormat = "RGBA_8888";
std::vector<uint8_t> buffer;
int64_t frameId = 0;
int32_t errorCode = 0;
std::string errorMessage;
};
class MrpFacade {
public:
CommonResult Init(const InitOptions &options);
CommonResult LoadPackage(const std::string &packagePath);
CommonResult Start();
CommonResult Pause();
CommonResult Resume();
CommonResult SendInput(const MrpInputEvent &event);
FrameResult PullFrame();
CommonResult Release();
private:
InitOptions options_;
std::string packagePath_;
};
#endif
+24
View File
@@ -0,0 +1,24 @@
#include "FrameBuffer.h"
FrameBuffer::FrameBuffer(int32_t width, int32_t height)
: width_(width), height_(height), rgba_(static_cast<size_t>(width) * static_cast<size_t>(height) * 4U, 0)
{
}
void FrameBuffer::FillTestPattern(int64_t frameId)
{
for (int32_t y = 0; y < height_; ++y) {
for (int32_t x = 0; x < width_; ++x) {
const size_t index = static_cast<size_t>(y * width_ + x) * 4U;
rgba_[index] = static_cast<uint8_t>((x + frameId) % 255);
rgba_[index + 1] = static_cast<uint8_t>((y + frameId) % 255);
rgba_[index + 2] = static_cast<uint8_t>((x + y) % 255);
rgba_[index + 3] = 255;
}
}
}
const std::vector<uint8_t> &FrameBuffer::Data() const
{
return rgba_;
}
+20
View File
@@ -0,0 +1,20 @@
#ifndef MRP_RENDER_FRAME_BUFFER_H
#define MRP_RENDER_FRAME_BUFFER_H
#include <cstdint>
#include <vector>
class FrameBuffer {
public:
FrameBuffer(int32_t width, int32_t height);
void FillTestPattern(int64_t frameId);
const std::vector<uint8_t> &Data() const;
private:
int32_t width_;
int32_t height_;
std::vector<uint8_t> rgba_;
};
#endif
+75
View File
@@ -0,0 +1,75 @@
#include "MrpRuntime.h"
CommonResult MrpRuntime::Init(int32_t width, int32_t height, bool debug)
{
width_ = width > 0 ? width : 240;
height_ = height > 0 ? height : 320;
debug_ = debug;
frameId_ = 0;
state_ = State::Initialized;
return CommonResult{true, 0, ""};
}
CommonResult MrpRuntime::LoadPackage(const std::string &packagePath)
{
if (state_ != State::Initialized && state_ != State::Released) {
return CommonResult{false, 1001, "runtime not initialized"};
}
(void)packagePath;
state_ = State::Loaded;
return CommonResult{true, 0, ""};
}
CommonResult MrpRuntime::Start()
{
if (state_ != State::Loaded && state_ != State::Paused) {
return CommonResult{false, 1002, "runtime not ready to start"};
}
state_ = State::Running;
return CommonResult{true, 0, ""};
}
CommonResult MrpRuntime::Pause()
{
if (state_ != State::Running) {
return CommonResult{false, 1003, "runtime not running"};
}
state_ = State::Paused;
return CommonResult{true, 0, ""};
}
CommonResult MrpRuntime::Resume()
{
if (state_ != State::Paused) {
return CommonResult{false, 1004, "runtime not paused"};
}
state_ = State::Running;
return CommonResult{true, 0, ""};
}
CommonResult MrpRuntime::Release()
{
state_ = State::Released;
frameId_ = 0;
return CommonResult{true, 0, ""};
}
int32_t MrpRuntime::Width() const
{
return width_;
}
int32_t MrpRuntime::Height() const
{
return height_;
}
int64_t MrpRuntime::NextFrameId()
{
return ++frameId_;
}
int64_t MrpRuntime::CurrentFrameId() const
{
return frameId_;
}
+37
View File
@@ -0,0 +1,37 @@
#ifndef MRP_RUNTIME_MRP_RUNTIME_H
#define MRP_RUNTIME_MRP_RUNTIME_H
#include "../platform/MrpFacade.h"
class MrpRuntime {
public:
CommonResult Init(int32_t width, int32_t height, bool debug);
CommonResult LoadPackage(const std::string &packagePath);
CommonResult Start();
CommonResult Pause();
CommonResult Resume();
CommonResult Release();
int32_t Width() const;
int32_t Height() const;
int64_t NextFrameId();
int64_t CurrentFrameId() const;
private:
enum class State {
Idle,
Initialized,
Loaded,
Running,
Paused,
Released,
};
State state_ = State::Idle;
int32_t width_ = 240;
int32_t height_ = 320;
bool debug_ = false;
int64_t frameId_ = 0;
};
#endif