1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "common/vsoc/lib/input_events_region_view.h"
17
18 #include <linux/input.h>
19 #include <linux/uinput.h>
20
21 #include "common/vsoc/lib/circqueue_impl.h"
22
23 using vsoc::layout::input_events::InputEventsLayout;
24
25 namespace vsoc {
26 namespace input_events {
27
28 namespace {
InitInputEvent(InputEvent * evt,uint16_t type,uint16_t code,uint32_t value)29 void InitInputEvent(InputEvent* evt, uint16_t type, uint16_t code, uint32_t value) {
30 evt->type = type;
31 evt->code = code;
32 evt->value = value;
33 }
34 } // namespace
35
36 const int InputEventsRegionView::kMaxEventsPerPacket = 4;
37
HandleSingleTouchEvent(bool down,int x,int y)38 bool InputEventsRegionView::HandleSingleTouchEvent(bool down, int x, int y) {
39 // TODO(jemoreira): Use multitouch when available
40 InputEvent events[4];
41 // Make sure to modify kMaxEventPerPacket if more events are sent.
42 InitInputEvent(&events[0], EV_ABS, ABS_X, x);
43 InitInputEvent(&events[1], EV_ABS, ABS_Y, y);
44 InitInputEvent(&events[2], EV_KEY, BTN_TOUCH, down);
45 InitInputEvent(&events[3], EV_SYN, 0, 0);
46 return 0 <
47 data()->touch_screen_queue.Write(
48 this, reinterpret_cast<char*>(&events[0]), sizeof(events), true);
49 }
50
HandlePowerButtonEvent(bool down)51 bool InputEventsRegionView::HandlePowerButtonEvent(bool down) {
52 InputEvent events[2];
53 InitInputEvent(&events[0], EV_KEY, KEY_POWER, down);
54 InitInputEvent(&events[1], EV_SYN, 0, 0);
55 return 0 < data()->power_button_queue.Write(
56 this, reinterpret_cast<char*>(&events[0]),
57 sizeof(events), true);
58 }
59
HandleKeyboardEvent(bool down,uint16_t key_code)60 bool InputEventsRegionView::HandleKeyboardEvent(bool down, uint16_t key_code) {
61 InputEvent events[2];
62 InitInputEvent(&events[0], EV_KEY, key_code, down);
63 InitInputEvent(&events[1], EV_SYN, 0, 0);
64 return 0 <
65 data()->keyboard_queue.Write(this, reinterpret_cast<char*>(&events[0]),
66 sizeof(events), true);
67 }
68
GetScreenEventsOrWait(InputEvent * evt,int max_event_count)69 intptr_t InputEventsRegionView::GetScreenEventsOrWait(InputEvent* evt,
70 int max_event_count) {
71 intptr_t ret = this->data()->touch_screen_queue.Read(
72 this, reinterpret_cast<char*>(evt), sizeof(InputEvent) * max_event_count);
73 if (ret < 0) {
74 return ret;
75 }
76 return ret / sizeof(InputEvent);
77 }
78
GetKeyboardEventsOrWait(InputEvent * evt,int max_event_count)79 intptr_t InputEventsRegionView::GetKeyboardEventsOrWait(InputEvent* evt,
80 int max_event_count) {
81 intptr_t ret = this->data()->keyboard_queue.Read(
82 this, reinterpret_cast<char*>(evt), sizeof(InputEvent) * max_event_count);
83 if (ret < 0) {
84 return ret;
85 }
86 return ret / sizeof(InputEvent);
87 }
88
GetPowerButtonEventsOrWait(InputEvent * evt,int max_event_count)89 intptr_t InputEventsRegionView::GetPowerButtonEventsOrWait(
90 InputEvent* evt, int max_event_count) {
91 intptr_t ret = this->data()->power_button_queue.Read(
92 this, reinterpret_cast<char*>(evt), sizeof(InputEvent) * max_event_count);
93 if (ret < 0) {
94 return ret;
95 }
96 return ret / sizeof(InputEvent);
97 }
98
99 } // namespace input_events
100 } // namespace vsoc
101