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_listener_proxy.h"
17 #include "gfx_utils/graphic_log.h"
18 #include "samgr_lite.h"
19 
20 namespace OHOS {
21 static IpcObjectStub objectStub;
22 InputEventListenerProxy::RawEventListener* InputEventListenerProxy::listener_ = nullptr;
~InputEventListenerProxy()23 InputEventListenerProxy::~InputEventListenerProxy()
24 {
25     if (proxy_ != nullptr) {
26         UnregisterInputEventListener();
27         proxy_ = nullptr;
28     }
29 }
30 
GetInstance()31 InputEventListenerProxy* InputEventListenerProxy::GetInstance()
32 {
33     static InputEventListenerProxy client;
34     return &client;
35 }
36 
GetIClientProxy()37 bool InputEventListenerProxy::GetIClientProxy()
38 {
39     if (proxy_ == nullptr) {
40         IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(IMS_SERVICE_NAME);
41         if (iUnknown == nullptr) {
42             GRAPHIC_LOGE("iUnknown is NULL");
43             return false;
44         }
45         (void)iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&proxy_);
46         if (proxy_ == nullptr) {
47             GRAPHIC_LOGE("QueryInterface failed, IClientProxy is empty!");
48             return false;
49         }
50     }
51     return true;
52 }
53 
ReceiveMsgHandler(uint32_t code,IpcIo * io,IpcIo * reply,MessageOption option)54 int32_t InputEventListenerProxy::ReceiveMsgHandler(uint32_t code, IpcIo* io, IpcIo* reply, MessageOption option)
55 {
56     if (listener_ == nullptr) {
57         return -1;
58     }
59     RawEvent* eventTemp = static_cast<RawEvent*>(ReadRawData(io, sizeof(RawEvent)));
60     if (eventTemp == nullptr) {
61         GRAPHIC_LOGE("pop raw event failed.");
62         return -1;
63     }
64     RawEvent event = *eventTemp;
65     listener_->OnRawEvent(event);
66     return 0;
67 }
68 
RegisterInputEventListener(RawEventListener * listener)69 bool InputEventListenerProxy::RegisterInputEventListener(RawEventListener* listener)
70 {
71     if (listener == nullptr) {
72         GRAPHIC_LOGE("Input event listener is empty.");
73         return false;
74     }
75 
76     if (!GetIClientProxy()) {
77         GRAPHIC_LOGE("Get input event client proxy failed.");
78         return false;
79     }
80     IpcIo io;
81     uint8_t tmpData[IMS_DEFAULT_IPC_SIZE];
82     IpcIoInit(&io, tmpData, IMS_DEFAULT_IPC_SIZE, 1);
83 
84     SvcIdentity svc;
85     objectStub.func = ReceiveMsgHandler;
86     objectStub.args = nullptr;
87     objectStub.isRemote = false;
88     svc.handle = IPC_INVALID_HANDLE;
89     svc.token = SERVICE_TYPE_ANONYMOUS;
90     svc.cookie = reinterpret_cast<uintptr_t>(&objectStub);
91     WriteRemoteObject(&io, &svc);
92     WriteBool(&io, listener->IsAlwaysInvoke());
93     int32_t ret = proxy_->Invoke(proxy_, LITEIMS_CLIENT_REGISTER, &io, NULL, NULL);
94     if (ret != 0) {
95         GRAPHIC_LOGE("Client register failed, ret=%d", ret);
96         return false;
97     }
98     listener_ = listener;
99     return true;
100 }
101 
UnregisterInputEventListener()102 bool InputEventListenerProxy::UnregisterInputEventListener()
103 {
104     if (proxy_ != nullptr) {
105         IpcIo io;
106         uint8_t tmpData[IMS_DEFAULT_IPC_SIZE];
107         IpcIoInit(&io, tmpData, IMS_DEFAULT_IPC_SIZE, 1);
108         int32_t ret = proxy_->Invoke(proxy_, LITEIMS_CLIENT_UNREGISTER, &io, NULL, NULL);
109         if (ret == 0) {
110             listener_ = nullptr;
111             return true;
112         }
113     }
114     return false;
115 }
116 } // namespace OHOS
117