• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 #include "include/GameEvent.h"
16 #include "interface/GameServicePlugin.h"
17 
18 #include <string>
19 #include <map>
20 #include "include/service_plugin.h"
21 #include <sp_log.h>
22 #include <dlfcn.h>
23 #include <memory>
24 
25 namespace OHOS {
26 namespace SmartPerf {
OnGameEvent(int32_t type,std::map<std::string,std::string> & params)27 void GameEventCallbackImpl::OnGameEvent(int32_t type, std::map<std::string, std::string> &params)
28 {
29     for (auto &item : params) {
30         GameEvent::GetInstance().GetGameEventItemData()[item.first] = item.second;
31     }
32 }
33 
ItemData()34 std::map<std::string, std::string> GameEvent::ItemData()
35 {
36     LOGI("GameEvent:ItemData map size(%u)", gameEventItemData.size());
37     return gameEventItemData;
38 }
39 
GetGameEventItemData()40 std::map<std::string, std::string> &GameEvent::GetGameEventItemData()
41 {
42     return gameEventItemData;
43 }
44 
StartExecutionOnce(bool isPause)45 void GameEvent::StartExecutionOnce(bool isPause)
46 {
47     if (isPause) {
48         return;
49     }
50     RegisterGameEvent();
51 }
52 
FinishtExecutionOnce(bool isPause)53 void GameEvent::FinishtExecutionOnce(bool isPause)
54 {
55     if (isPause) {
56         return;
57     }
58     UnregisterGameEvent();
59 }
60 
RegisterGameEvent()61 int GameEvent::RegisterGameEvent()
62 {
63     if (isRegister) {
64         WLOGI("GameEvent::RegisterGameEvent, already register");
65         return 0;
66     }
67     WLOGI("GameEvent::RegisterGameEvent");
68     std::unique_ptr<GameEventCallback> gameEventCallback = std::make_unique<GameEventCallbackImpl>();
69     ServicePluginHandler &servicePluginHandler = ServicePluginHandler::GetInstance();
70     void* handle = servicePluginHandler.GetSoHandler(ServicePluginHandler::ServicePluginType::GAME_PLUGIN);
71     if (!handle) {
72         WLOGE("Get service plugin handler failed.");
73         return -1;
74     }
75     typedef GameServicePlugin *(*GetServicePlugin)();
76     GetServicePlugin servicePlugin = (GetServicePlugin)dlsym(handle, createPlugin.c_str());
77     if (!servicePlugin) {
78         WLOGE("GameServicePlugin Error loading symbol");
79         return -1;
80     }
81 
82     int ret = servicePlugin()->RegisterGameEventListener(std::move(gameEventCallback));
83     if (ret == 0) {
84         isRegister = true;
85         WLOGI("GameEvent::ItemData, RegisterGameEventListener success");
86     } else {
87         WLOGE("GameEvent::ItemData, RegisterGameEventListener failed");
88     }
89     return ret;
90 }
91 
UnregisterGameEvent()92 int GameEvent::UnregisterGameEvent()
93 {
94     if (!isRegister) {
95         return 0;
96     }
97     WLOGI("GameEvent::UnregisterGameEvent");
98     ServicePluginHandler &servicePluginHandler = ServicePluginHandler::GetInstance();
99     void* handle = servicePluginHandler.GetSoHandler(ServicePluginHandler::ServicePluginType::GAME_PLUGIN);
100     if (!handle) {
101         WLOGE("Get service plugin handler failed.");
102         return -1;
103     }
104     typedef GameServicePlugin *(*GetServicePlugin)();
105     GetServicePlugin servicePlugin = (GetServicePlugin)dlsym(handle, createPlugin.c_str());
106     if (!servicePlugin) {
107         WLOGE("GameServicePlugin Error loading symbol");
108         return -1;
109     }
110 
111     int ret = servicePlugin()->UnregisterGameEventListener();
112     if (ret == 0) {
113         isRegister = false;
114         WLOGI("GameEvent::ItemData, UnregisterGameEventListener success");
115     } else {
116         WLOGE("GameEvent::ItemData, UnregisterGameEventListener failed");
117     }
118     return ret;
119 }
120 
121 } // namespace SmartPerf
122 } // namespace OHOS