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 pthread_mutex_lock(&lock_);
48 if (clientInfoMap_.size() >= MAX_CLIENT_SIZE) {
49 pthread_mutex_unlock(&lock_);
50 GRAPHIC_LOGE("Exceeded the maximum number!");
51 return;
52 }
53 pthread_mutex_unlock(&lock_);
54 pid_t pid = GetCallingPid();
55 SvcIdentity svc = {0};
56 bool ret = ReadRemoteObject(req, &svc);
57 bool alwaysInvoke;
58 ReadBool(req, &alwaysInvoke);
59 if (!ret) {
60 GRAPHIC_LOGE("ReadRemoteObject failed.");
61 return;
62 }
63 uint32_t cbId = 0;
64 if (AddDeathRecipient(svc, DeathCallback, nullptr, &cbId) != 0) {
65 GRAPHIC_LOGE("Register death callback failed!");
66 return;
67 }
68 struct ClientInfo clientInfo = { svc, cbId, alwaysInvoke };
69 pthread_mutex_lock(&lock_);
70 clientInfoMap_.insert(std::make_pair(pid, clientInfo));
71 pthread_mutex_unlock(&lock_);
72 }
73
DeathCallback(void * origin)74 void InputEventClientProxy::DeathCallback(void* origin)
75 {
76 InputEventClientProxy::GetInstance()->RemoveListener(origin, nullptr, nullptr);
77 }
78
RemoveListener(const void * origin,IpcIo * req,IpcIo * reply)79 void InputEventClientProxy::RemoveListener(const void* origin, IpcIo* req, IpcIo* reply)
80 {
81 pid_t pid = GetCallingPid();
82 pthread_mutex_lock(&lock_);
83 if (clientInfoMap_.count(pid) > 0) {
84 ReleaseSvc(clientInfoMap_[pid].svc);
85 clientInfoMap_.erase(pid);
86 }
87 pthread_mutex_unlock(&lock_);
88 }
89
OnRawEvent(const RawEvent & event)90 void InputEventClientProxy::OnRawEvent(const RawEvent& event)
91 {
92 IpcIo io;
93 uint8_t tmpData[IMS_DEFAULT_IPC_SIZE];
94 IpcIoInit(&io, tmpData, IMS_DEFAULT_IPC_SIZE, 1);
95 WriteRawData(&io, static_cast<const void*>(&event), sizeof(RawEvent));
96 pthread_mutex_lock(&lock_);
97 std::map<pid_t, ClientInfo>::iterator it;
98 for (it = clientInfoMap_.begin(); it != clientInfoMap_.end(); ++it) {
99 if (it->second.alwaysInvoke || (event.state != lastState_)) {
100 MessageOption option;
101 MessageOptionInit(&option);
102 option.flags = TF_OP_ASYNC;
103 SendRequest(it->second.svc, 0, &io, nullptr, option, nullptr);
104 }
105 }
106 lastState_ = event.state;
107 pthread_mutex_unlock(&lock_);
108 }
109 } // namespace OHOS
110