1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "core/common/stylus/stylus_detector_loader.h"
17 
18 #include <dlfcn.h>
19 #include <memory>
20 
21 #include "frameworks/base/log/log_wrapper.h"
22 
23 namespace OHOS::Ace {
24 namespace {
25 #if (defined(__aarch64__) || defined(__x86_64__))
26 const std::string STYLUS_CLIENT_SO_PATH = "/system/lib64/libstylus_innerapi.z.so";
27 #else
28 const std::string STYLUS_CLIENT_SO_PATH = "/system/lib/libstylus_innerapi.z.so";
29 #endif
30 } // namespace
Load()31 std::shared_ptr<StylusDetectorLoader> StylusDetectorLoader::Load()
32 {
33     auto engLib(std::make_shared<StylusDetectorLoader>());
34     auto ret = engLib->Init();
35     if (!ret) {
36         LOGD("Stylus detector loader instance init failed.");
37         return nullptr;
38     }
39     return engLib;
40 }
41 
~StylusDetectorLoader()42 StylusDetectorLoader::~StylusDetectorLoader()
43 {
44     Close();
45 }
46 
Init()47 bool StylusDetectorLoader::Init()
48 {
49     if (stylusSoLoaded_ && libraryHandle_ != nullptr && createStylusDetectorInstance_ != nullptr &&
50         destroyStylusDetectorInstance_ != nullptr) {
51         return true;
52     }
53     libraryHandle_ = dlopen(STYLUS_CLIENT_SO_PATH.c_str(), RTLD_LAZY);
54     if (libraryHandle_ == nullptr) {
55         return false;
56     }
57     createStylusDetectorInstance_ =
58         (StylusDetectorInterface* (*)()) dlsym(libraryHandle_, "CreateStylusDetectorInstance");
59     destroyStylusDetectorInstance_ =
60         (void (*)(StylusDetectorInterface*))dlsym(libraryHandle_, "DestroyStylusDetectorInstance");
61     if (createStylusDetectorInstance_ == nullptr || destroyStylusDetectorInstance_ == nullptr) {
62         TAG_LOGI(AceLogTag::ACE_STYLUS, "Stylus detector loader instance loading failed.");
63         Close();
64         return false;
65     }
66     stylusSoLoaded_ = true;
67     return true;
68 }
69 
CreateStylusDetector()70 StylusDetectorInstance StylusDetectorLoader::CreateStylusDetector()
71 {
72     if (!createStylusDetectorInstance_ || !destroyStylusDetectorInstance_) {
73         return nullptr;
74     }
75 
76     return { createStylusDetectorInstance_(), [lib = shared_from_this(), destroy = destroyStylusDetectorInstance_](
77                                                   StylusDetectorInterface* e) { destroy(e); } };
78 }
79 
Close()80 void StylusDetectorLoader::Close()
81 {
82     if (libraryHandle_ != nullptr) {
83         dlclose(libraryHandle_);
84     }
85     stylusSoLoaded_ = false;
86     libraryHandle_ = nullptr;
87     createStylusDetectorInstance_ = nullptr;
88     destroyStylusDetectorInstance_ = nullptr;
89 }
90 
91 } // namespace OHOS::Ace