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 
20 #include "frameworks/base/log/log_wrapper.h"
21 
22 namespace OHOS::Ace {
23 namespace {
24 #if (defined(__aarch64__) || defined(__x86_64__))
25 const std::string STYLUS_CLIENT_SO_PATH = "/system/lib64/libstylus_innerapi.z.so";
26 #else
27 const std::string STYLUS_CLIENT_SO_PATH = "/system/lib/libstylus_innerapi.z.so";
28 #endif
29 } // namespace
Load()30 std::shared_ptr<StylusDetectorLoader> StylusDetectorLoader::Load()
31 {
32     auto engLib(std::make_shared<StylusDetectorLoader>());
33     auto ret = engLib->Init();
34     if (!ret) {
35         LOGD("Stylus detector loader instance init failed.");
36         return nullptr;
37     }
38     return engLib;
39 }
40 
~StylusDetectorLoader()41 StylusDetectorLoader::~StylusDetectorLoader()
42 {
43     Close();
44 }
45 
Init()46 bool StylusDetectorLoader::Init()
47 {
48     if (stylusSoLoaded_ && libraryHandle_ != nullptr && createStylusDetectorInstance_ != nullptr &&
49         destroyStylusDetectorInstance_ != nullptr) {
50         return true;
51     }
52     libraryHandle_ = dlopen(STYLUS_CLIENT_SO_PATH.c_str(), RTLD_LAZY);
53     if (libraryHandle_ == nullptr) {
54         return false;
55     }
56     createStylusDetectorInstance_ =
57         (StylusDetectorInterface* (*)()) dlsym(libraryHandle_, "CreateStylusDetectorInstance");
58     destroyStylusDetectorInstance_ =
59         (void (*)(StylusDetectorInterface*))dlsym(libraryHandle_, "DestroyStylusDetectorInstance");
60     if (createStylusDetectorInstance_ == nullptr || destroyStylusDetectorInstance_ == nullptr) {
61         TAG_LOGI(AceLogTag::ACE_STYLUS, "Stylus detector loader instance loading failed.");
62         Close();
63         return false;
64     }
65     stylusSoLoaded_ = true;
66     return true;
67 }
68 
CreateStylusDetector()69 StylusDetectorInstance StylusDetectorLoader::CreateStylusDetector()
70 {
71     if (!createStylusDetectorInstance_ || !destroyStylusDetectorInstance_) {
72         return nullptr;
73     }
74 
75     return { createStylusDetectorInstance_(), [lib = shared_from_this(), destroy = destroyStylusDetectorInstance_](
76                                                   StylusDetectorInterface* e) { destroy(e); } };
77 }
78 
79 void StylusDetectorLoader::Close()
80 {
81     if (libraryHandle_ != nullptr) {
82         dlclose(libraryHandle_);
83     }
84     stylusSoLoaded_ = false;
85     libraryHandle_ = nullptr;
86     createStylusDetectorInstance_ = nullptr;
87     destroyStylusDetectorInstance_ = nullptr;
88 }
89 
90 } // namespace OHOS::Ace