Skip to content

Commit 6e9370c

Browse files
committed
support resize terminal
1 parent 4608d05 commit 6e9370c

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

entry/src/main/cpp/napi_init.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828
static int fd = -1;
2929

30-
static napi_value add(napi_env env, napi_callback_info info) {
30+
static napi_value resize(napi_env env, napi_callback_info info) {
31+
3132
size_t argc = 2;
3233
napi_value args[2] = {nullptr};
3334

@@ -45,10 +46,20 @@ static napi_value add(napi_env env, napi_callback_info info) {
4546
double value1;
4647
napi_get_value_double(env, args[1], &value1);
4748

48-
napi_value sum;
49-
napi_create_double(env, value0 + value1, &sum);
49+
int width = value0;
50+
int height = value1;
51+
52+
if (fd > 0) {
53+
struct winsize ws = {};
54+
ws.ws_col = width;
55+
ws.ws_row = height;
56+
int ret = ioctl(fd, TIOCSWINSZ, &ws);
57+
assert(ret == 0);
58+
59+
OH_LOG_INFO(LOG_APP, "Resize: %{public}d, %{public}d", width, height);
60+
}
5061

51-
return sum;
62+
return nullptr;
5263
}
5364

5465
napi_threadsafe_function registered_callback = nullptr;
@@ -83,6 +94,7 @@ static void *terminal_worker(void *) {
8394
}
8495
}
8596

97+
// call callback registered by ArkTS
8698
if (hex.length() > 0 && registered_callback != nullptr) {
8799
data_buffer *pbuf = new data_buffer{.buf = new char[hex.length()], .size = (size_t)hex.length()};
88100
memcpy(pbuf->buf, &hex[0], hex.length());
@@ -99,7 +111,7 @@ static void *terminal_worker(void *) {
99111
}
100112

101113
static napi_value run(napi_env env, napi_callback_info info) {
102-
114+
103115
size_t argc = 2;
104116
napi_value args[2] = {nullptr};
105117

@@ -131,6 +143,7 @@ static napi_value run(napi_env env, napi_callback_info info) {
131143
const char *home = "/storage/Users/currentUser";
132144
setenv("HOME", home, 1);
133145
setenv("PWD", home, 1);
146+
setenv("PATH", "/bin", 1);
134147

135148
chdir(home);
136149
execl("/bin/sh", "/bin/sh", nullptr);
@@ -222,7 +235,7 @@ static napi_value register_callback(napi_env env, napi_callback_info info) {
222235
EXTERN_C_START
223236
static napi_value Init(napi_env env, napi_value exports) {
224237
napi_property_descriptor desc[] = {
225-
{"add", nullptr, add, nullptr, nullptr, nullptr, napi_default, nullptr},
238+
{"resize", nullptr, resize, nullptr, nullptr, nullptr, napi_default, nullptr},
226239
{"run", nullptr, run, nullptr, nullptr, nullptr, napi_default, nullptr},
227240
{"send", nullptr, send, nullptr, nullptr, nullptr, napi_default, nullptr},
228241
{"subscribe", nullptr, register_callback, nullptr, nullptr, nullptr, napi_default, nullptr}};
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const add: (a: number, b: number) => number;
21
export const run: (w: number, h: number) => void;
32
export const send: (content: ArrayBuffer) => void;
4-
export const subscribe: (cb: (ArrayBuffer) => void) => void;
3+
export const subscribe: (cb: (ArrayBuffer) => void) => void;
4+
export const resize: (w: number, h: number) => void;

entry/src/main/ets/pages/Index.ets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class NativeProxy {
3636
async resize(): Promise<void> {
3737
const s = await this.getSize();
3838
hilog.info(DOMAIN, 'WebTerminal', '%{public}s', 'resize: ' + s.width + ", " + s.height)
39+
napi.resize(s.width, s.height)
3940
}
4041

4142
focus(): void {
@@ -92,7 +93,6 @@ struct Index {
9293

9394
aboutToAppear(): void {
9495
webview.WebviewController.setWebDebuggingAccess(true)
95-
hilog.info(DOMAIN, 'testTag', 'Test NAPI 2 + 3 = %{public}d', napi.add(2, 3));
9696
}
9797

9898
build() {

0 commit comments

Comments
 (0)