• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "nweb_input_event_consumer.h"
17 
18 #include <cstdint>
19 #include <memory>
20 
21 #include "nweb_log.h"
22 
23 namespace OHOS::NWeb {
NWebInputEventConsumer(std::shared_ptr<NWeb> nweb)24 NWebInputEventConsumer::NWebInputEventConsumer(std::shared_ptr<NWeb> nweb)
25     : nwebweak_(nweb) {}
26 
OnInputEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent) const27 bool NWebInputEventConsumer::OnInputEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent) const
28 {
29     if (pointerEvent == nullptr) {
30         WVLOG_E("pointerEvent is invalid.");
31         return false;
32     }
33     DispatchPointerEvent(pointerEvent);
34     pointerEvent->MarkProcessed();
35     return true;
36 }
37 
OnInputEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent) const38 bool NWebInputEventConsumer::OnInputEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent) const
39 {
40     if (keyEvent == nullptr) {
41         WVLOG_E("keyEvent is invalid.");
42         return false;
43     }
44     DispatchKeyEvent(keyEvent);
45     keyEvent->MarkProcessed();
46     return true;
47 }
48 
OnInputEvent(const std::shared_ptr<MMI::AxisEvent> & axisEvent) const49 bool NWebInputEventConsumer::OnInputEvent(const std::shared_ptr<MMI::AxisEvent>& axisEvent) const
50 {
51     // do nothing
52     return false;
53 }
54 
DispatchPointerEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const55 void NWebInputEventConsumer::DispatchPointerEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const
56 {
57     if (nwebweak_.expired()) {
58         WVLOG_W("nweb instance has expired");
59         return;
60     }
61     auto nweb = nwebweak_.lock();
62     if (pointerEvent == nullptr) {
63         WVLOG_E("pointerEvent is invalid.");
64         return;
65     }
66     int32_t pointerID = pointerEvent->GetPointerId();
67     MMI::PointerEvent::PointerItem item;
68     bool ret = pointerEvent->GetPointerItem(pointerID, item);
69     if (!ret) {
70         WVLOG_E("get pointer item failed.");
71         return;
72     }
73     int32_t action = pointerEvent->GetPointerAction();
74     switch (action) {
75         case MMI::PointerEvent::POINTER_ACTION_DOWN: {
76             WVLOG_I("nweb receive pointer touch down event");
77             nweb->OnTouchPress(pointerID, item.GetWindowX(), item.GetWindowY());
78             break;
79         }
80         case MMI::PointerEvent::POINTER_ACTION_UP: {
81             WVLOG_I("nweb receive pointer touch up event");
82             nweb->OnTouchRelease(pointerID);
83             break;
84         }
85         case MMI::PointerEvent::POINTER_ACTION_MOVE: {
86             WVLOG_I("nweb receive pointer touch move event");
87             nweb->OnTouchMove(pointerID, item.GetWindowX(), item.GetWindowY());
88             break;
89         }
90         case MMI::PointerEvent::POINTER_ACTION_CANCEL: {
91             WVLOG_I("nweb receive pointer touch cancel event");
92             nweb->OnTouchCancel();
93             break;
94         }
95         default: {
96             WVLOG_W("unsupported PointerEvent action[%{public}d] received", action);
97             break;
98         }
99     }
100 }
101 
DispatchKeyEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const102 void NWebInputEventConsumer::DispatchKeyEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const
103 {
104     if (nwebweak_.expired()) {
105         WVLOG_W("nweb instance has expired");
106         return;
107     }
108     auto nweb = nwebweak_.lock();
109     if (keyEvent == nullptr) {
110         WVLOG_E("keyEvent is invalid.");
111         return;
112     }
113     int32_t keyCode = keyEvent->GetKeyCode();
114     if (keyCode == MMI::KeyEvent::KEYCODE_BACK) {
115         WVLOG_I("nweb receive key back event");
116         nweb->OnNavigateBack();
117     } else {
118         WVLOG_W("unsupported KeyEvent code[%{public}d] received", keyCode);
119     }
120 }
121 } // namespace OHOS::NWeb
122