• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
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 #include "touch_input.h"
16 #include "gfx_utils/graphic_log.h"
17 #include "graphic_config.h"
18 
19 namespace OHOS
20 {
GetInstance()21 TouchInput *TouchInput::GetInstance()
22 {
23     static TouchInput instance;
24     if (!instance.init) {
25         instance.handle = TouchOpen(0);
26         if (!instance.handle) {
27             GRAPHIC_LOGE("TouchOpen failed");
28             return nullptr;
29         }
30         instance.init = true;
31     }
32     return &instance;
33 }
34 
IsValidTouchMsg(struct touch_msg * msg)35 bool TouchInput::IsValidTouchMsg(struct touch_msg *msg)
36 {
37     if (msg->x >= HORIZONTAL_RESOLUTION || msg->y >= VERTICAL_RESOLUTION)
38         return false;
39 
40     if (msg->event != TOUCH_EVENT_UP && msg->event != TOUCH_EVENT_DOWN && msg->event != TOUCH_EVENT_MOVE)
41         return false;
42 
43     return true;
44 }
45 
Read(DeviceData & data)46 bool TouchInput::Read(DeviceData &data)
47 {
48     // merge msg with the same event
49     struct touch_msg tmp[TOUCH_MSG_MAX] = {0};
50     int i = 0;
51     while (i < TOUCH_MSG_MAX && TouchRead(this->handle, &tmp[i], 0) == 0) {
52         if (!IsValidTouchMsg(&tmp[i]))
53             break;
54 
55         if (tmp[i].event == TOUCH_EVENT_MOVE) {
56             tmp[i].event = TOUCH_EVENT_DOWN;
57         }
58         i++;
59         if (IsValidTouchMsg(&this->msg)) {
60             if (tmp[i - 1].event != this->msg.event) {
61                 break;
62             }
63         } else {
64             if (i > 1 && tmp[i - 1].event != tmp[0].event)
65                 break;
66         }
67     }
68     if (i <= 1) {
69         data.point.x = this->msg.x;
70         data.point.y = this->msg.y;
71         data.state = (this->msg.event == TOUCH_EVENT_DOWN) ? STATE_PRESS : STATE_RELEASE;
72     } else if (i <= TOUCH_MSG_MAX) {
73         data.point.x = tmp[i - 2].x;
74         data.point.y = tmp[i - 2].y;
75         data.state = (tmp[i - 2].event == TOUCH_EVENT_DOWN) ? STATE_PRESS : STATE_RELEASE;
76     }
77     // GRAPHIC_LOGD("Touch {%d, %d} %d", data.point.x, data.point.y, data.state);
78     if (i >= 1 && i <= TOUCH_MSG_MAX && IsValidTouchMsg(&tmp[i - 1]))
79         this->msg = tmp[i - 1];
80 
81     return false;
82 }
83 } // namespace OHOS
84