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 return handle_.isWorking(capturePid);
82 }
83
RegisterListener(ScreenCaptureCallback callback)84 void InputScreenCaptureAgent::RegisterListener(ScreenCaptureCallback callback)
85 {
86 if (LoadLibrary() != RET_OK) {
87 MMI_HILOGE("LoadLibrary fail");
88 return;
89 }
90 handle_.registerListener(callback);
91 }
92
IsMusicActivate()93 bool InputScreenCaptureAgent::IsMusicActivate()
94 {
95 if (LoadAudioLibrary() != RET_OK) {
96 MMI_HILOGE("LoadLibrary fail");
97 return false;
98 }
99 return handle_.isMusicActivate();
100 }
101
LoadAudioLibrary()102 int32_t InputScreenCaptureAgent::LoadAudioLibrary()
103 {
104 std::lock_guard<std::mutex> guard(agentMutex_);
105 if (handle_.handle != nullptr) {
106 MMI_HILOGD("The library has already been loaded");
107 return RET_OK;
108 }
109 char libRealPath[PATH_MAX] = {};
110 if (realpath(REFENCE_LIB_ABSOLUTE_PATH.c_str(), libRealPath) == nullptr) {
111 MMI_HILOGE("Get file real path fail");
112 return RET_ERR;
113 }
114 handle_.handle = dlopen(libRealPath, RTLD_LAZY);
115 if (handle_.handle == nullptr) {
116 MMI_HILOGE("dlopen failed, reason:%{public}s", dlerror());
117 return RET_ERR;
118 }
119 handle_.isMusicActivate = reinterpret_cast<bool (*)()>(dlsym(handle_.handle, "IsMusicActivate"));
120 if (handle_.isMusicActivate == nullptr) {
121 MMI_HILOGE("dlsym isWorking failed: error:%{public}s", dlerror());
122 handle_.Free();
123 return RET_ERR;
124 }
125 return RET_OK;
126 }
127 }
128 }