/
touch_event.cpp
206 lines (160 loc) · 6.09 KB
/
touch_event.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Copyright © 2016 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 2 or 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Brandon Schaefer <brandon.schaefer@canonical.com>
*/
#include <boost/throw_exception.hpp>
#include "mir/events/touch_event.h"
#include <stdexcept>
MirTouchEvent::MirTouchEvent()
{
event.initInput();
event.getInput().initTouch();
event.getInput().getTouch().initContacts(mir::capnp::TouchScreenEvent::MAX_COUNT);
}
MirTouchEvent::MirTouchEvent(MirInputDeviceId id,
std::chrono::nanoseconds timestamp,
std::vector<uint8_t> const& cookie,
MirInputEventModifiers modifiers,
std::vector<mir::events::ContactState> const& contacts)
: MirInputEvent(id,timestamp, modifiers, cookie)
{
event.getInput().initTouch();
auto tev = event.getInput().getTouch();
tev.initContacts(mir::capnp::TouchScreenEvent::MAX_COUNT);
tev.setCount(contacts.size());
for (size_t i = 0; i < contacts.size(); ++i)
{
using ContactType = mir::capnp::TouchScreenEvent::Contact;
auto& contact = contacts[i];
auto event_contact = tev.getContacts()[i];
event_contact.setId(contact.touch_id);
event_contact.setX(contact.x);
event_contact.setY(contact.y);
event_contact.setPressure(contact.pressure);
event_contact.setTouchMajor(contact.touch_major);
event_contact.setTouchMinor(contact.touch_minor);
event_contact.setOrientation(contact.orientation);
auto const action = static_cast<ContactType::TouchAction>(contact.action);
event_contact.setAction(action);
auto capnp_tool_type = static_cast<ContactType::ToolType>(contact.tooltype);
event_contact.setToolType(capnp_tool_type);
}
}
size_t MirTouchEvent::pointer_count() const
{
return event.asReader().getInput().getTouch().getCount();
}
void MirTouchEvent::set_pointer_count(size_t count)
{
return event.getInput().getTouch().setCount(count);
}
void MirTouchEvent::throw_if_out_of_bounds(size_t index) const
{
if (index > event.asReader().getInput().getTouch().getCount())
BOOST_THROW_EXCEPTION(std::out_of_range("Out of bounds index in pointer coordinates"));
}
int MirTouchEvent::id(size_t index) const
{
throw_if_out_of_bounds(index);
return event.asReader().getInput().getTouch().getContacts()[index].getId();
}
void MirTouchEvent::set_id(size_t index, int id)
{
throw_if_out_of_bounds(index);
event.getInput().getTouch().getContacts()[index].setId(id);
}
float MirTouchEvent::x(size_t index) const
{
throw_if_out_of_bounds(index);
return event.asReader().getInput().getTouch().getContacts()[index].getX();
}
void MirTouchEvent::set_x(size_t index, float x)
{
throw_if_out_of_bounds(index);
event.getInput().getTouch().getContacts()[index].setX(x);
}
float MirTouchEvent::y(size_t index) const
{
throw_if_out_of_bounds(index);
return event.asReader().getInput().getTouch().getContacts()[index].getY();
}
void MirTouchEvent::set_y(size_t index, float y)
{
throw_if_out_of_bounds(index);
event.getInput().getTouch().getContacts()[index].setY(y);
}
float MirTouchEvent::touch_major(size_t index) const
{
throw_if_out_of_bounds(index);
return event.asReader().getInput().getTouch().getContacts()[index].getTouchMajor();
}
void MirTouchEvent::set_touch_major(size_t index, float major)
{
throw_if_out_of_bounds(index);
event.getInput().getTouch().getContacts()[index].setTouchMajor(major);
}
float MirTouchEvent::touch_minor(size_t index) const
{
throw_if_out_of_bounds(index);
return event.asReader().getInput().getTouch().getContacts()[index].getTouchMinor();
}
void MirTouchEvent::set_touch_minor(size_t index, float minor)
{
throw_if_out_of_bounds(index);
event.getInput().getTouch().getContacts()[index].setTouchMinor(minor);
}
float MirTouchEvent::pressure(size_t index) const
{
throw_if_out_of_bounds(index);
return event.asReader().getInput().getTouch().getContacts()[index].getPressure();
}
void MirTouchEvent::set_pressure(size_t index, float pressure)
{
throw_if_out_of_bounds(index);
event.getInput().getTouch().getContacts()[index].setPressure(pressure);
}
float MirTouchEvent::orientation(size_t index) const
{
throw_if_out_of_bounds(index);
return event.asReader().getInput().getTouch().getContacts()[index].getOrientation();
}
void MirTouchEvent::set_orientation(size_t index, float orientation)
{
throw_if_out_of_bounds(index);
event.getInput().getTouch().getContacts()[index].setOrientation(orientation);
}
MirTouchTooltype MirTouchEvent::tool_type(size_t index) const
{
throw_if_out_of_bounds(index);
return static_cast<MirTouchTooltype>(event.asReader().getInput().getTouch().getContacts()[index].getToolType());
}
void MirTouchEvent::set_tool_type(size_t index, MirTouchTooltype tool_type)
{
throw_if_out_of_bounds(index);
auto capnp_tool_type = static_cast<mir::capnp::TouchScreenEvent::Contact::ToolType>(tool_type);
event.getInput().getTouch().getContacts()[index].setToolType(capnp_tool_type);
}
MirTouchAction MirTouchEvent::action(size_t index) const
{
throw_if_out_of_bounds(index);
return static_cast<MirTouchAction>(event.asReader().getInput().getTouch().getContacts()[index].getAction());
}
void MirTouchEvent::set_action(size_t index, MirTouchAction action)
{
throw_if_out_of_bounds(index);
event.getInput().getTouch().getContacts()[index].setAction(
static_cast<mir::capnp::TouchScreenEvent::Contact::TouchAction>(action));
}