1885b47fbSopenharmony_ci/*
2885b47fbSopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3885b47fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4885b47fbSopenharmony_ci * you may not use this file except in compliance with the License.
5885b47fbSopenharmony_ci * You may obtain a copy of the License at
6885b47fbSopenharmony_ci *
7885b47fbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8885b47fbSopenharmony_ci *
9885b47fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10885b47fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11885b47fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12885b47fbSopenharmony_ci * See the License for the specific language governing permissions and
13885b47fbSopenharmony_ci * limitations under the License.
14885b47fbSopenharmony_ci */
15885b47fbSopenharmony_ci
16885b47fbSopenharmony_ci#include <cstdio>
17885b47fbSopenharmony_ci#include <cstring>
18885b47fbSopenharmony_ci#include <pthread.h>
19885b47fbSopenharmony_ci#include <unistd.h>
20885b47fbSopenharmony_ci
21885b47fbSopenharmony_ci#include "hilog_wrapper.h"
22885b47fbSopenharmony_ci#include "napi_accessibility_event_info.h"
23885b47fbSopenharmony_ci#include "napi_accessibility_system_ability_client.h"
24885b47fbSopenharmony_ci#include "napi/native_api.h"
25885b47fbSopenharmony_ci#include "napi/native_node_api.h"
26885b47fbSopenharmony_ci
27885b47fbSopenharmony_ciEXTERN_C_START
28885b47fbSopenharmony_cistatic void Cleanup(void *data)
29885b47fbSopenharmony_ci{
30885b47fbSopenharmony_ci    HILOG_INFO("cleanup hook");
31885b47fbSopenharmony_ci    if (NAccessibilityClient::accessibilityStateListeners_) {
32885b47fbSopenharmony_ci        NAccessibilityClient::accessibilityStateListeners_->UnsubscribeFromFramework();
33885b47fbSopenharmony_ci    }
34885b47fbSopenharmony_ci    if (NAccessibilityClient::touchGuideStateListeners_) {
35885b47fbSopenharmony_ci        NAccessibilityClient::touchGuideStateListeners_->UnsubscribeFromFramework();
36885b47fbSopenharmony_ci    }
37885b47fbSopenharmony_ci    if (NAccessibilityClient::captionListeners_) {
38885b47fbSopenharmony_ci        NAccessibilityClient::captionListeners_->UnsubscribeFromFramework();
39885b47fbSopenharmony_ci    }
40885b47fbSopenharmony_ci}
41885b47fbSopenharmony_ci/*
42885b47fbSopenharmony_ci * function for module exports
43885b47fbSopenharmony_ci */
44885b47fbSopenharmony_cistatic napi_value Init(napi_env env, napi_value exports)
45885b47fbSopenharmony_ci{
46885b47fbSopenharmony_ci    napi_property_descriptor desc[] = {
47885b47fbSopenharmony_ci        DECLARE_NAPI_FUNCTION("isOpenAccessibility", NAccessibilityClient::IsOpenAccessibility),
48885b47fbSopenharmony_ci        DECLARE_NAPI_FUNCTION("isOpenAccessibilitySync", NAccessibilityClient::IsOpenAccessibilitySync),
49885b47fbSopenharmony_ci        DECLARE_NAPI_FUNCTION("isOpenTouchGuide", NAccessibilityClient::IsOpenTouchExploration),
50885b47fbSopenharmony_ci        DECLARE_NAPI_FUNCTION("isOpenTouchGuideSync", NAccessibilityClient::IsOpenTouchExplorationSync),
51885b47fbSopenharmony_ci        DECLARE_NAPI_FUNCTION("getAbilityLists", NAccessibilityClient::GetAbilityList),
52885b47fbSopenharmony_ci        DECLARE_NAPI_FUNCTION("getAccessibilityExtensionList", NAccessibilityClient::GetAccessibilityExtensionList),
53885b47fbSopenharmony_ci        DECLARE_NAPI_FUNCTION("getAccessibilityExtensionListSync",
54885b47fbSopenharmony_ci            NAccessibilityClient::GetAccessibilityExtensionListSync),
55885b47fbSopenharmony_ci        DECLARE_NAPI_FUNCTION("on", NAccessibilityClient::SubscribeState),
56885b47fbSopenharmony_ci        DECLARE_NAPI_FUNCTION("off", NAccessibilityClient::UnsubscribeState),
57885b47fbSopenharmony_ci        DECLARE_NAPI_FUNCTION("sendEvent", NAccessibilityClient::SendEvent),
58885b47fbSopenharmony_ci        DECLARE_NAPI_FUNCTION("sendAccessibilityEvent", NAccessibilityClient::SendAccessibilityEvent),
59885b47fbSopenharmony_ci        DECLARE_NAPI_FUNCTION("getCaptionsManager", NAccessibilityClient::GetCaptionsManager),
60885b47fbSopenharmony_ci    };
61885b47fbSopenharmony_ci
62885b47fbSopenharmony_ci    NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
63885b47fbSopenharmony_ci
64885b47fbSopenharmony_ci    auto &instance = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance();
65885b47fbSopenharmony_ci    (void)instance.InitializeContext();
66885b47fbSopenharmony_ci    NAccessibilityClient::DefineJSCaptionsManager(env);
67885b47fbSopenharmony_ci    NAccessibilityClient::DefineJSCaptionsStyle(env);
68885b47fbSopenharmony_ci    NAccessibilityEventInfo::DefineJSAccessibilityEventInfo(env, exports);
69885b47fbSopenharmony_ci    NAccessibilityClient::accessibilityStateListeners_->SubscribeToFramework();
70885b47fbSopenharmony_ci    NAccessibilityClient::touchGuideStateListeners_->SubscribeToFramework();
71885b47fbSopenharmony_ci    NAccessibilityClient::captionListeners_->SubscribeToFramework();
72885b47fbSopenharmony_ci    napi_status status = napi_add_env_cleanup_hook(env, Cleanup, nullptr);
73885b47fbSopenharmony_ci    if (status != napi_ok) {
74885b47fbSopenharmony_ci        HILOG_WARN("add cleanup hook failed %{public}d", status);
75885b47fbSopenharmony_ci    }
76885b47fbSopenharmony_ci    HILOG_INFO("-----Init end------");
77885b47fbSopenharmony_ci    return exports;
78885b47fbSopenharmony_ci}
79885b47fbSopenharmony_ciEXTERN_C_END
80885b47fbSopenharmony_ci
81885b47fbSopenharmony_ci/*
82885b47fbSopenharmony_ci * Module define
83885b47fbSopenharmony_ci */
84885b47fbSopenharmony_cistatic napi_module _module = {
85885b47fbSopenharmony_ci    .nm_version = 1,
86885b47fbSopenharmony_ci    .nm_flags = 0,
87885b47fbSopenharmony_ci    .nm_filename = nullptr,
88885b47fbSopenharmony_ci    .nm_register_func = Init,
89885b47fbSopenharmony_ci    .nm_modname = "accessibility",
90885b47fbSopenharmony_ci    .nm_priv = ((void*)0),
91885b47fbSopenharmony_ci    .reserved = {0},
92885b47fbSopenharmony_ci};
93885b47fbSopenharmony_ci/*
94885b47fbSopenharmony_ci * Module register function
95885b47fbSopenharmony_ci */
96885b47fbSopenharmony_ciextern "C" __attribute__((constructor)) void RegisterNapiAccessibilityModule(void)
97885b47fbSopenharmony_ci{
98885b47fbSopenharmony_ci    napi_module_register(&_module);
99885b47fbSopenharmony_ci}
100