1 /*
2 * Copyright (c) 2020-2021 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_event_client_proxy.h"
17 #include "gfx_utils/graphic_log.h"
18 #include "samgr_lite.h"
19
20 namespace OHOS {
21 pthread_mutex_t InputEventClientProxy::lock_;
GetInstance()22 InputEventClientProxy* InputEventClientProxy::GetInstance()
23 {
24 static InputEventClientProxy ims;
25 return &ims;
26 }
27
ClientRequestHandle(int funcId,void * origin,IpcIo * req,IpcIo * reply)28 void InputEventClientProxy::ClientRequestHandle(int funcId, void* origin, IpcIo* req, IpcIo* reply)
29 {
30 switch (funcId) {
31 case LITEIMS_CLIENT_REGISTER: {
32 InputEventClientProxy::GetInstance()->AddListener(origin, req, reply);
33 break;
34 }
35 case LITEIMS_CLIENT_UNREGISTER: {
36 InputEventClientProxy::GetInstance()->RemoveListener(origin, req, reply);
37 break;
38 }
39 default: {
40 break;
41 }
42 }
43 }
44
AddListener(const void * origin,IpcIo * req,IpcIo * reply)45 void InputEventClientProxy::AddListener(const void* origin, IpcIo* req, IpcIo* reply)
46 {
47 if (clientInfoMap_.size() >= MAX_CLIENT_SIZE) {
48 GRAPHIC_LOGE("Exceeded the maximum number!");
49 return;
50 }
51 pid_t pid = GetCallingPid();
52 SvcIdentity svc = {0};
53 bool ret = ReadRemoteObject(req, &svc);
54 bool alwaysInvoke;
55 ReadBool(req, &alwaysInvoke);
56 if (!ret) {
57 GRAPHIC_LOGE("ReadRemoteObject failed.");
58 return;
59 }
60 uint32_t cbId = 0;
61 if (AddDeathRecipient(svc, DeathCallback, nullptr, &cbId) != 0) {
62 GRAPHIC_LOGE("Register death callback failed!");
63 return;
64 }
65 struct ClientInfo clientInfo = { svc, cbId, alwaysInvoke };
66 pthread_mutex_lock(&lock_);
67 clientInfoMap_.insert(std::make_pair(pid, clientInfo));
68 pthread_mutex_unlock(&lock_);
69 }
70
DeathCallback(void * origin)71 void InputEventClientProxy::DeathCallback(void* origin)
72 {
73 InputEventClientProxy::GetInstance()->RemoveListener(origin, nullptr, nullptr);
74 }
75
RemoveListener(const void * origin,IpcIo * req,IpcIo * reply)76 void InputEventClientProxy::RemoveListener(const void* origin, IpcIo* req, IpcIo* reply)
77 {
78 pid_t pid = GetCallingPid();
79 if (clientInfoMap_.count(pid) > 0) {
80 ReleaseSvc(clientInfoMap_[pid].svc);
81 pthread_mutex_lock(&lock_);
82 clientInfoMap_.erase(pid);
83 pthread_mutex_unlock(&lock_);
84 }
85 }
86
OnRawEvent(const RawEvent & event)87 void InputEventClientProxy::OnRawEvent(const RawEvent& event)
88 {
89 IpcIo io;
90 uint8_t tmpData[IMS_DEFAULT_IPC_SIZE];
91 IpcIoInit(&io, tmpData, IMS_DEFAULT_IPC_SIZE, 1);
92 WriteRawData(&io, static_cast<const void*>(&event), sizeof(RawEvent));
93 pthread_mutex_lock(&lock_);
94 std::map<pid_t, ClientInfo>::iterator it;
95 for (it = clientInfoMap_.begin(); it != clientInfoMap_.end(); ++it) {
96 if (it->second.alwaysInvoke || (event.state != lastState_)) {
97 MessageOption option;
98 MessageOptionInit(&option);
99 option.flags = TF_OP_ASYNC;
100 SendRequest(it->second.svc, 0, &io, nullptr, option, nullptr);
101 }
102 }
103 lastState_ = event.state;
104 pthread_mutex_unlock(&lock_);
105 }
106 } // namespace OHOS
107