|
| 1 | +#include "napi/native_api.h" |
| 2 | +#include <algorithm> |
| 3 | +#include <array> |
| 4 | +#include <assert.h> |
| 5 | +#include <cstdint> |
| 6 | +#include <deque> |
| 7 | +#include <fcntl.h> |
| 8 | +#include <map> |
| 9 | +#include <poll.h> |
| 10 | +#include <pthread.h> |
| 11 | +#include <pty.h> |
| 12 | +#include <set> |
| 13 | +#include <stdint.h> |
| 14 | +#include <stdlib.h> |
| 15 | +#include <string> |
| 16 | +#include <sys/time.h> |
| 17 | +#include <unistd.h> |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +static int width = 127; |
| 21 | +static int height = 36; |
| 22 | + |
| 23 | +static int fd = -1; |
| 24 | + |
| 25 | +static napi_value Add(napi_env env, napi_callback_info info) { |
| 26 | + size_t argc = 2; |
| 27 | + napi_value args[2] = {nullptr}; |
| 28 | + |
| 29 | + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); |
| 30 | + |
| 31 | + napi_valuetype valuetype0; |
| 32 | + napi_typeof(env, args[0], &valuetype0); |
| 33 | + |
| 34 | + napi_valuetype valuetype1; |
| 35 | + napi_typeof(env, args[1], &valuetype1); |
| 36 | + |
| 37 | + double value0; |
| 38 | + napi_get_value_double(env, args[0], &value0); |
| 39 | + |
| 40 | + double value1; |
| 41 | + napi_get_value_double(env, args[1], &value1); |
| 42 | + |
| 43 | + napi_value sum; |
| 44 | + napi_create_double(env, value0 + value1, &sum); |
| 45 | + |
| 46 | + return sum; |
| 47 | +} |
| 48 | + |
| 49 | +static void *terminal_worker(void *) {} |
| 50 | + |
| 51 | +static napi_value Run(napi_env env, napi_callback_info info) { |
| 52 | + |
| 53 | + size_t argc = 2; |
| 54 | + napi_value args[2] = {nullptr}; |
| 55 | + |
| 56 | + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); |
| 57 | + |
| 58 | + napi_valuetype valuetype0; |
| 59 | + napi_typeof(env, args[0], &valuetype0); |
| 60 | + |
| 61 | + napi_valuetype valuetype1; |
| 62 | + napi_typeof(env, args[1], &valuetype1); |
| 63 | + |
| 64 | + double value0; |
| 65 | + napi_get_value_double(env, args[0], &value0); |
| 66 | + |
| 67 | + double value1; |
| 68 | + napi_get_value_double(env, args[1], &value1); |
| 69 | + |
| 70 | + width = value0; |
| 71 | + height = value1; |
| 72 | + |
| 73 | + if (!fd) { |
| 74 | + struct winsize ws = {}; |
| 75 | + ws.ws_col = width; |
| 76 | + ws.ws_row = height; |
| 77 | + int pid = forkpty(&fd, nullptr, nullptr, &ws); |
| 78 | + |
| 79 | + if (!pid) { |
| 80 | + const char *home = "/storage/Users/currentUser"; |
| 81 | + setenv("HOME", home, 1); |
| 82 | + setenv("PWD", home, 1); |
| 83 | + |
| 84 | + chdir(home); |
| 85 | + execl("/bin/sh", "/bin/sh", nullptr); |
| 86 | + } |
| 87 | + |
| 88 | + // set as non blocking |
| 89 | + int res = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); |
| 90 | + assert(res == 0); |
| 91 | + |
| 92 | + // start terminal worker in another thread |
| 93 | + pthread_t terminal_thread; |
| 94 | + pthread_create(&terminal_thread, NULL, terminal_worker, nullptr); |
| 95 | + } |
| 96 | + |
| 97 | + return nullptr; |
| 98 | +} |
| 99 | + |
| 100 | +// send data to terminal |
| 101 | +static napi_value send(napi_env env, napi_callback_info info) { |
| 102 | + |
| 103 | + size_t argc = 1; |
| 104 | + napi_value args[1] = {nullptr}; |
| 105 | + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); |
| 106 | + |
| 107 | + void *data; |
| 108 | + size_t length; |
| 109 | + napi_status ret = napi_get_arraybuffer_info(env, args[0], &data, &length); |
| 110 | + assert(ret == napi_ok); |
| 111 | + |
| 112 | +// SendData((uint8_t *)data, length); |
| 113 | + return nullptr; |
| 114 | +} |
| 115 | + |
| 116 | +EXTERN_C_START |
| 117 | +static napi_value Init(napi_env env, napi_value exports) { |
| 118 | + napi_property_descriptor desc[] = {{"add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr}, |
| 119 | + {"run", nullptr, Run, nullptr, nullptr, nullptr, napi_default, nullptr}, |
| 120 | + {"send", nullptr, send, nullptr, nullptr, nullptr, napi_default, nullptr}}; |
| 121 | + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); |
| 122 | + return exports; |
| 123 | +} |
| 124 | +EXTERN_C_END |
| 125 | + |
| 126 | +static napi_module demoModule = { |
| 127 | + .nm_version = 1, |
| 128 | + .nm_flags = 0, |
| 129 | + .nm_filename = nullptr, |
| 130 | + .nm_register_func = Init, |
| 131 | + .nm_modname = "entry", |
| 132 | + .nm_priv = ((void *)0), |
| 133 | + .reserved = {0}, |
| 134 | +}; |
| 135 | + |
| 136 | +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } |
0 commit comments