1 /*
2 * Copyright (c) 2023 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_operate.h"
17 #include <set>
18 #include "hdf_log.h"
19
20 #define HDF_LOG_TAG InputEventOperateTag
21
22 namespace OHOS {
23 namespace Input {
TransformEvent(IfInputEvent & inputEvent,libinput_event * libinputEvent)24 RetStatus TransformEvent(IfInputEvent &inputEvent, libinput_event *libinputEvent)
25 {
26 if (libinputEvent == nullptr) {
27 HDF_LOGE("libinputEvent is nullptr");
28 return INPUT_FAILURE;
29 }
30 auto type = libinput_event_get_type(libinputEvent);
31 if (IsTouchEvent(type)) {
32 return CopyTouchEvent(inputEvent, libinputEvent);
33 }
34 HDF_LOGE("unsupported event type: %{public}d", type);
35 return INPUT_FAILURE;
36 }
37
CopyTouchEvent(IfInputEvent & inputEvent,libinput_event * libinputEvent)38 RetStatus CopyTouchEvent(IfInputEvent &inputEvent, libinput_event *libinputEvent)
39 {
40 if (libinputEvent == nullptr) {
41 HDF_LOGE("libinputEvent is nullptr");
42 return INPUT_FAILURE;
43 }
44
45 auto libinputTouchEvent = libinput_event_get_touch_event(libinputEvent);
46
47 IfTouchEvent touchEvent;
48 touchEvent.time = libinput_event_touch_get_time_usec(libinputTouchEvent);
49 touchEvent.pressure = libinput_event_touch_get_pressure(libinputTouchEvent);
50 touchEvent.seatSlot = libinput_event_touch_get_seat_slot(libinputTouchEvent);
51 touchEvent.axis.major = libinput_event_get_touch_contact_long_axis(libinputTouchEvent);
52 touchEvent.axis.minor = libinput_event_get_touch_contact_short_axis(libinputTouchEvent);
53 touchEvent.toolType = libinput_event_touch_get_tool_type(libinputTouchEvent);
54 touchEvent.sensorTime = libinput_event_get_sensortime(libinputEvent);
55
56 IfEventData eventData;
57 eventData.touchEvent = touchEvent;
58
59 inputEvent.eventType = libinput_event_get_type(libinputEvent);
60 inputEvent.data = eventData;
61 return INPUT_SUCCESS;
62 }
63
IsTouchEvent(libinput_event_type type)64 bool IsTouchEvent(libinput_event_type type)
65 {
66 std::set<libinput_event_type> touchTypeSet = {
67 LIBINPUT_EVENT_TOUCH_DOWN,
68 LIBINPUT_EVENT_TOUCH_UP,
69 LIBINPUT_EVENT_TOUCH_MOTION,
70 LIBINPUT_EVENT_TOUCH_CANCEL,
71 LIBINPUT_EVENT_TOUCH_FRAME
72 };
73 if (touchTypeSet.find(type) == touchTypeSet.end()) {
74 return false;
75 }
76 return true;
77 }
78 }
79 }
80