• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "input_screen_capture_agent.h"
17 
18 #undef MMI_LOG_TAG
19 #define MMI_LOG_TAG "InputScreenCaptureAgent"
20 
21 namespace OHOS {
22 namespace MMI {
23 namespace {
24 #ifdef __aarch64__
25 const std::string REFERENCE_LIB_PATH = "/system/lib64/platformsdk";
26 #else
27 const std::string REFERENCE_LIB_PATH = "/system/lib/platformsdk";
28 #endif
29 const std::string FILESEPARATOR = "/";
30 const std::string REFERENCE_LIB_NAME = "libmmi-screen_capture.z.so";
31 const std::string REFENCE_LIB_ABSOLUTE_PATH = REFERENCE_LIB_PATH + FILESEPARATOR + REFERENCE_LIB_NAME;
32 }
33 
~InputScreenCaptureAgent()34 InputScreenCaptureAgent::~InputScreenCaptureAgent()
35 {
36     std::lock_guard<std::mutex> guard(agentMutex_);
37     if (handle_.handle != nullptr) {
38         handle_.Free();
39     }
40 }
41 
LoadLibrary()42 int32_t InputScreenCaptureAgent::LoadLibrary()
43 {
44     std::lock_guard<std::mutex> guard(agentMutex_);
45     if (handle_.handle != nullptr) {
46         MMI_HILOGD("The library has already been loaded");
47         return RET_OK;
48     }
49     char libRealPath[PATH_MAX] = {};
50     if (realpath(REFENCE_LIB_ABSOLUTE_PATH.c_str(), libRealPath) == nullptr) {
51         MMI_HILOGE("Get file real path fail");
52         return RET_ERR;
53     }
54     handle_.handle = dlopen(libRealPath, RTLD_LAZY);
55     if (handle_.handle == nullptr) {
56         MMI_HILOGE("dlopen failed, reason:%{public}s", dlerror());
57         return RET_ERR;
58     }
59     handle_.isWorking = reinterpret_cast<int32_t (*)(int32_t)>(dlsym(handle_.handle, "IsScreenCaptureWorking"));
60     if (handle_.isWorking == nullptr) {
61         MMI_HILOGE("dlsym isWorking failed: error:%{public}s", dlerror());
62         handle_.Free();
63         return RET_ERR;
64     }
65     handle_.registerListener = reinterpret_cast<void (*)(ScreenCaptureCallback)>(
66         dlsym(handle_.handle, "RegisterListener"));
67     if (handle_.registerListener == nullptr) {
68         MMI_HILOGE("dlsym registerListener failed: error:%{public}s", dlerror());
69         handle_.Free();
70         return RET_ERR;
71     }
72     return RET_OK;
73 }
74 
IsScreenCaptureWorking(int32_t capturePid)75 bool InputScreenCaptureAgent::IsScreenCaptureWorking(int32_t capturePid)
76 {
77     if (LoadLibrary() != RET_OK) {
78         MMI_HILOGE("LoadLibrary fail");
79         return {};
80     }
81     std::lock_guard<std::mutex> guard(agentMutex_);
82     if (handle_.isWorking == nullptr) {
83         MMI_HILOGE("isWorking is null");
84         return {};
85     }
86     return handle_.isWorking(capturePid);
87 }
88 
RegisterListener(ScreenCaptureCallback callback)89 void InputScreenCaptureAgent::RegisterListener(ScreenCaptureCallback callback)
90 {
91     if (LoadLibrary() != RET_OK) {
92         MMI_HILOGE("LoadLibrary fail");
93         return;
94     }
95     std::lock_guard<std::mutex> guard(agentMutex_);
96     if (handle_.registerListener == nullptr) {
97         MMI_HILOGE("registerListener is null");
98         return;
99     }
100     handle_.registerListener(callback);
101 }
102 
IsMusicActivate()103 bool InputScreenCaptureAgent::IsMusicActivate()
104 {
105     if (LoadAudioLibrary() != RET_OK) {
106         MMI_HILOGE("LoadLibrary fail");
107         return false;
108     }
109     std::lock_guard<std::mutex> guard(agentMutex_);
110     if (handle_.isMusicActivate == nullptr) {
111         MMI_HILOGE("isMusicActivate is null");
112         return false;
113     }
114     return handle_.isMusicActivate();
115 }
116 
LoadAudioLibrary()117 int32_t InputScreenCaptureAgent::LoadAudioLibrary()
118 {
119     std::lock_guard<std::mutex> guard(agentMutex_);
120     if (handle_.handle != nullptr) {
121         MMI_HILOGD("The library has already been loaded");
122         return RET_OK;
123     }
124     char libRealPath[PATH_MAX] = {};
125     if (realpath(REFENCE_LIB_ABSOLUTE_PATH.c_str(), libRealPath) == nullptr) {
126         MMI_HILOGE("Get file real path fail");
127         return RET_ERR;
128     }
129     handle_.handle = dlopen(libRealPath, RTLD_LAZY);
130     if (handle_.handle == nullptr) {
131         MMI_HILOGE("dlopen failed, reason:%{public}s", dlerror());
132         return RET_ERR;
133     }
134     handle_.isMusicActivate = reinterpret_cast<bool (*)()>(dlsym(handle_.handle, "IsMusicActivate"));
135     if (handle_.isMusicActivate == nullptr) {
136         MMI_HILOGE("dlsym isWorking failed: error:%{public}s", dlerror());
137         handle_.Free();
138         return RET_ERR;
139     }
140     return RET_OK;
141 }
142 }
143 }