| 1 |
// Copyright (C) 2019 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "mockcompositor.h" |
| 5 |
|
| 6 |
#include <qwayland-server-tablet-unstable-v2.h> |
| 7 |
|
| 8 |
#include <QtGui/QRasterWindow> |
| 9 |
|
| 10 |
using namespace MockCompositor; |
| 11 |
|
| 12 |
constexpr int tabletVersion = 1; // protocol VERSION, not the name suffix (_v2) |
| 13 |
|
| 14 |
class TabletManagerV2; |
| 15 |
class TabletSeatV2; |
| 16 |
|
| 17 |
class TabletV2 : public QObject, public QtWaylandServer::zwp_tablet_v2 |
| 18 |
{ |
| 19 |
Q_OBJECT |
| 20 |
public: |
| 21 |
explicit TabletV2(TabletSeatV2 *tabletSeat) |
| 22 |
: m_tabletSeat(tabletSeat) |
| 23 |
{ |
| 24 |
} |
| 25 |
|
| 26 |
void send_removed() = delete; |
| 27 |
void send_removed(struct ::wl_resource *resource) = delete; |
| 28 |
void sendRemoved(); |
| 29 |
|
| 30 |
QPointer<TabletSeatV2> m_tabletSeat; // destroy order is not guaranteed |
| 31 |
protected: |
| 32 |
void zwp_tablet_v2_destroy(Resource *resource) override; |
| 33 |
}; |
| 34 |
|
| 35 |
class TabletToolV2 : public QObject, public QtWaylandServer::zwp_tablet_tool_v2 |
| 36 |
{ |
| 37 |
Q_OBJECT |
| 38 |
public: |
| 39 |
using ToolType = QtWaylandServer::zwp_tablet_tool_v2::type; |
| 40 |
explicit TabletToolV2(TabletSeatV2 *tabletSeat, ToolType toolType, quint64 hardwareSerial) |
| 41 |
: m_tabletSeat(tabletSeat) |
| 42 |
, m_toolType(toolType) |
| 43 |
, m_hardwareSerial(hardwareSerial) |
| 44 |
{ |
| 45 |
} |
| 46 |
|
| 47 |
wl_resource *toolResource() // for convenience |
| 48 |
{ |
| 49 |
Q_ASSERT(resourceMap().size() == 1); |
| 50 |
// Strictly speaking, there may be more than one resource for the tool, for intsance if |
| 51 |
// if there are multiple clients, or a client has called get_tablet_seat multiple times. |
| 52 |
// For now we'll pretend there can only be one resource. |
| 53 |
return resourceMap().first()->handle; |
| 54 |
} |
| 55 |
|
| 56 |
void send_removed() = delete; |
| 57 |
void send_removed(struct ::wl_resource *resource) = delete; |
| 58 |
void sendRemoved(); |
| 59 |
|
| 60 |
uint sendProximityIn(TabletV2 *tablet, Surface *surface); |
| 61 |
void sendProximityOut(); |
| 62 |
void sendMotion(QPointF position) |
| 63 |
{ |
| 64 |
Q_ASSERT(m_proximitySurface); |
| 65 |
for (auto *resource : resourceMap()) |
| 66 |
send_motion(resource->handle, wl_fixed_from_double(position.x()), wl_fixed_from_double(position.y())); |
| 67 |
} |
| 68 |
uint sendDown(); |
| 69 |
void sendUp() { send_up(toolResource()); } |
| 70 |
void sendPressure(uint pressure); |
| 71 |
void sendTilt(qreal tiltX, qreal tiltY) { send_tilt(toolResource(), wl_fixed_from_double(tiltX), wl_fixed_from_double(tiltY)); } |
| 72 |
void sendRotation(qreal rotation) { send_rotation(toolResource(), wl_fixed_from_double(rotation)); } |
| 73 |
uint sendButton(uint button, bool pressed); |
| 74 |
uint sendFrame(); |
| 75 |
|
| 76 |
QPointer<TabletSeatV2> m_tabletSeat; // destruction order is not guaranteed |
| 77 |
ToolType m_toolType = ToolType::type_pen; |
| 78 |
quint64 m_hardwareSerial = 0; |
| 79 |
QPointer<Surface> m_proximitySurface; |
| 80 |
protected: |
| 81 |
void zwp_tablet_tool_v2_destroy(Resource *resource) override; |
| 82 |
}; |
| 83 |
|
| 84 |
class TabletPadV2 : public QObject, public QtWaylandServer::zwp_tablet_pad_v2 |
| 85 |
{ |
| 86 |
Q_OBJECT |
| 87 |
public: |
| 88 |
explicit TabletPadV2(TabletSeatV2 *tabletSeat) |
| 89 |
: m_tabletSeat(tabletSeat) |
| 90 |
{ |
| 91 |
} |
| 92 |
|
| 93 |
void send_removed() = delete; |
| 94 |
void send_removed(struct ::wl_resource *resource) = delete; |
| 95 |
void sendRemoved(); |
| 96 |
|
| 97 |
QPointer<TabletSeatV2> m_tabletSeat; // destroy order is not guaranteed |
| 98 |
protected: |
| 99 |
void zwp_tablet_pad_v2_destroy(Resource *resource) override; |
| 100 |
}; |
| 101 |
|
| 102 |
class TabletSeatV2 : public QObject, public QtWaylandServer::zwp_tablet_seat_v2 |
| 103 |
{ |
| 104 |
Q_OBJECT |
| 105 |
public: |
| 106 |
explicit TabletSeatV2(TabletManagerV2 *manager, Seat *seat) |
| 107 |
: m_manager(manager) |
| 108 |
, m_seat(seat) |
| 109 |
{} |
| 110 |
TabletV2 *addTablet() |
| 111 |
{ |
| 112 |
auto *tablet = new TabletV2(this); |
| 113 |
m_tablets.append(tablet); |
| 114 |
for (auto *resource : resourceMap()) |
| 115 |
sendTabletAdded(resource, tablet); |
| 116 |
return tablet; |
| 117 |
} |
| 118 |
|
| 119 |
void sendTabletAdded(Resource *resource, TabletV2 *tablet) |
| 120 |
{ |
| 121 |
// Although, not necessarily correct, assuming just one tablet_seat per client |
| 122 |
auto *tabletResource = tablet->add(resource->client(), resource->version()); |
| 123 |
zwp_tablet_seat_v2::send_tablet_added(resource->handle, tabletResource->handle); |
| 124 |
// TODO: send extra stuff before done? |
| 125 |
tablet->send_done(tabletResource->handle); |
| 126 |
} |
| 127 |
|
| 128 |
using ToolType = QtWaylandServer::zwp_tablet_tool_v2::type; |
| 129 |
TabletToolV2 *addTool(ToolType toolType = ToolType::type_pen, quint64 hardwareSerial = 0) |
| 130 |
{ |
| 131 |
auto *tool = new TabletToolV2(this, toolType, hardwareSerial); |
| 132 |
m_tools.append(tool); |
| 133 |
for (auto *resource : resourceMap()) |
| 134 |
sendToolAdded(resource, tool); |
| 135 |
return tool; |
| 136 |
} |
| 137 |
|
| 138 |
void sendToolAdded(Resource *resource, TabletToolV2 *tool) |
| 139 |
{ |
| 140 |
// Although, not necessarily correct, assuming just one tablet_seat per client |
| 141 |
auto *toolResource = tool->add(resource->client(), resource->version())->handle; |
| 142 |
zwp_tablet_seat_v2::send_tool_added(resource->handle, toolResource); |
| 143 |
tool->send_type(toolResource, tool->m_toolType); |
| 144 |
if (tool->m_hardwareSerial) { |
| 145 |
const uint hi = tool->m_hardwareSerial >> 32; |
| 146 |
const uint lo = tool->m_hardwareSerial & 0xffffffff; |
| 147 |
tool->send_hardware_serial(toolResource, hi, lo); |
| 148 |
} |
| 149 |
tool->send_done(toolResource); |
| 150 |
} |
| 151 |
|
| 152 |
TabletPadV2 *addPad() |
| 153 |
{ |
| 154 |
auto *pad = new TabletPadV2(this); |
| 155 |
m_pads.append(pad); |
| 156 |
for (auto *resource : resourceMap()) |
| 157 |
sendPadAdded(resource, pad); |
| 158 |
return pad; |
| 159 |
} |
| 160 |
|
| 161 |
void sendPadAdded(Resource *resource, TabletPadV2 *pad) |
| 162 |
{ |
| 163 |
// Although, not necessarily correct, assuming just one tablet_seat per client |
| 164 |
auto *padResource = pad->add(resource->client(), resource->version())->handle; |
| 165 |
zwp_tablet_seat_v2::send_pad_added(resource->handle, padResource); |
| 166 |
pad->send_done(padResource); |
| 167 |
} |
| 168 |
|
| 169 |
void removeAll() |
| 170 |
{ |
| 171 |
const auto tools = m_tools; |
| 172 |
for (auto *tool : tools) |
| 173 |
tool->sendRemoved(); |
| 174 |
|
| 175 |
const auto tablets = m_tablets; |
| 176 |
for (auto *tablet : tablets) |
| 177 |
tablet->sendRemoved(); |
| 178 |
|
| 179 |
const auto pads = m_pads; |
| 180 |
for (auto *pad : pads) |
| 181 |
pad->sendRemoved(); |
| 182 |
} |
| 183 |
|
| 184 |
TabletManagerV2 *m_manager = nullptr; |
| 185 |
Seat *m_seat = nullptr; |
| 186 |
QList<TabletV2 *> m_tablets; |
| 187 |
QList<TabletV2 *> m_tabletsWaitingForDestroy; |
| 188 |
QList<TabletToolV2 *> m_tools; |
| 189 |
QList<TabletToolV2::Resource *> m_toolsWaitingForDestroy; |
| 190 |
QList<TabletPadV2 *> m_pads; |
| 191 |
QList<TabletPadV2::Resource *> m_padsWaitingForDestroy; |
| 192 |
|
| 193 |
protected: |
| 194 |
void zwp_tablet_seat_v2_bind_resource(Resource *resource) override |
| 195 |
{ |
| 196 |
for (auto *tablet : m_tablets) |
| 197 |
sendTabletAdded(resource, tablet); |
| 198 |
for (auto *tool : m_tools) |
| 199 |
sendToolAdded(resource, tool); |
| 200 |
for (auto *pad : m_pads) |
| 201 |
sendPadAdded(resource, pad); |
| 202 |
} |
| 203 |
}; |
| 204 |
|
| 205 |
class TabletManagerV2 : public Global, public QtWaylandServer::zwp_tablet_manager_v2 |
| 206 |
{ |
| 207 |
Q_OBJECT |
| 208 |
public: |
| 209 |
explicit TabletManagerV2(CoreCompositor *compositor, int version = 1) |
| 210 |
: QtWaylandServer::zwp_tablet_manager_v2(compositor->m_display, version) |
| 211 |
, m_version(version) |
| 212 |
{} |
| 213 |
bool isClean() override |
| 214 |
{ |
| 215 |
for (auto *seat : m_tabletSeats) { |
| 216 |
if (!seat->m_tabletsWaitingForDestroy.empty()) |
| 217 |
return false; |
| 218 |
if (!seat->m_toolsWaitingForDestroy.empty()) |
| 219 |
return false; |
| 220 |
if (!seat->m_padsWaitingForDestroy.empty()) |
| 221 |
return false; |
| 222 |
} |
| 223 |
return true; |
| 224 |
} |
| 225 |
|
| 226 |
TabletSeatV2 *tabletSeatFor(Seat *seat) |
| 227 |
{ |
| 228 |
Q_ASSERT(seat); |
| 229 |
if (auto *tabletSeat = m_tabletSeats.value(seat, nullptr)) |
| 230 |
return tabletSeat; |
| 231 |
|
| 232 |
auto *tabletSeat = new TabletSeatV2(this, seat); |
| 233 |
m_tabletSeats[seat] = tabletSeat; |
| 234 |
return tabletSeat; |
| 235 |
} |
| 236 |
|
| 237 |
int m_version = 1; // TODO: Remove on libwayland upgrade |
| 238 |
QMap<Seat *, TabletSeatV2 *> m_tabletSeats; |
| 239 |
|
| 240 |
protected: |
| 241 |
void zwp_tablet_manager_v2_destroy(Resource *resource) override |
| 242 |
{ |
| 243 |
// tablet_seats created from this object are unaffected and should be destroyed separately. |
| 244 |
wl_resource_destroy(resource->handle); |
| 245 |
} |
| 246 |
|
| 247 |
void zwp_tablet_manager_v2_get_tablet_seat(Resource *resource, uint32_t id, ::wl_resource *seatResource) override |
| 248 |
{ |
| 249 |
auto *seat = fromResource<Seat>(seatResource); |
| 250 |
QVERIFY(seat); |
| 251 |
auto *tabletSeat = tabletSeatFor(seat); |
| 252 |
tabletSeat->add(resource->client(), id, resource->version()); |
| 253 |
} |
| 254 |
}; |
| 255 |
|
| 256 |
void TabletV2::sendRemoved() |
| 257 |
{ |
| 258 |
for (auto *resource : resourceMap()) |
| 259 |
zwp_tablet_v2_send_removed(resource->handle); |
| 260 |
bool removed = m_tabletSeat->m_tablets.removeOne(this); |
| 261 |
QVERIFY(removed); |
| 262 |
m_tabletSeat->m_tabletsWaitingForDestroy.append(this); |
| 263 |
} |
| 264 |
|
| 265 |
void TabletV2::zwp_tablet_v2_destroy(QtWaylandServer::zwp_tablet_v2::Resource *resource) |
| 266 |
{ |
| 267 |
Q_UNUSED(resource); |
| 268 |
if (m_tabletSeat) { |
| 269 |
bool removed = m_tabletSeat->m_tabletsWaitingForDestroy.removeOne(this); |
| 270 |
QVERIFY(removed); |
| 271 |
} |
| 272 |
wl_resource_destroy(resource->handle); |
| 273 |
} |
| 274 |
|
| 275 |
void TabletToolV2::sendRemoved() |
| 276 |
{ |
| 277 |
for (auto *resource : resourceMap()) { |
| 278 |
zwp_tablet_tool_v2_send_removed(resource->handle); |
| 279 |
m_tabletSeat->m_toolsWaitingForDestroy.append(resource); |
| 280 |
} |
| 281 |
bool removed = m_tabletSeat->m_tools.removeOne(this); |
| 282 |
QVERIFY(removed); |
| 283 |
} |
| 284 |
|
| 285 |
uint TabletToolV2::sendProximityIn(TabletV2 *tablet, Surface *surface) |
| 286 |
{ |
| 287 |
Q_ASSERT(!m_proximitySurface); |
| 288 |
m_proximitySurface = surface; |
| 289 |
uint serial = m_tabletSeat->m_seat->m_compositor->nextSerial(); |
| 290 |
auto *client = surface->resource()->client(); |
| 291 |
auto tabletResource = tablet->resourceMap().value(client)->handle; |
| 292 |
send_proximity_in(toolResource(), serial, tabletResource, surface->resource()->handle); |
| 293 |
return serial; |
| 294 |
} |
| 295 |
|
| 296 |
void TabletToolV2::sendProximityOut() |
| 297 |
{ |
| 298 |
Q_ASSERT(m_proximitySurface); |
| 299 |
send_proximity_out(toolResource()); |
| 300 |
m_proximitySurface = nullptr; |
| 301 |
} |
| 302 |
|
| 303 |
uint TabletToolV2::sendDown() |
| 304 |
{ |
| 305 |
uint serial = m_tabletSeat->m_seat->m_compositor->nextSerial(); |
| 306 |
send_down(toolResource(), serial); |
| 307 |
return serial; |
| 308 |
} |
| 309 |
|
| 310 |
void TabletToolV2::sendPressure(uint pressure) |
| 311 |
{ |
| 312 |
Q_ASSERT(m_proximitySurface); |
| 313 |
auto *client = m_proximitySurface->resource()->client(); |
| 314 |
auto toolResource = resourceMap().value(client)->handle; |
| 315 |
send_pressure(toolResource, pressure); |
| 316 |
} |
| 317 |
|
| 318 |
uint TabletToolV2::sendButton(uint button, bool pressed) |
| 319 |
{ |
| 320 |
button_state state = pressed ? button_state_pressed : button_state_released; |
| 321 |
uint serial = m_tabletSeat->m_seat->m_compositor->nextSerial(); |
| 322 |
send_button(toolResource(), serial, button, state); |
| 323 |
return serial; |
| 324 |
} |
| 325 |
|
| 326 |
uint TabletToolV2::sendFrame() |
| 327 |
{ |
| 328 |
uint time = m_tabletSeat->m_seat->m_compositor->currentTimeMilliseconds(); |
| 329 |
for (auto *resource : resourceMap()) |
| 330 |
send_frame(resource->handle, time); |
| 331 |
return time; |
| 332 |
} |
| 333 |
|
| 334 |
void TabletToolV2::zwp_tablet_tool_v2_destroy(QtWaylandServer::zwp_tablet_tool_v2::Resource *resource) |
| 335 |
{ |
| 336 |
if (m_tabletSeat) { |
| 337 |
m_tabletSeat->m_toolsWaitingForDestroy.removeOne(resource); |
| 338 |
} |
| 339 |
wl_resource_destroy(resource->handle); |
| 340 |
} |
| 341 |
|
| 342 |
void TabletPadV2::sendRemoved() |
| 343 |
{ |
| 344 |
for (auto *resource : resourceMap()) { |
| 345 |
zwp_tablet_pad_v2_send_removed(resource->handle); |
| 346 |
m_tabletSeat->m_padsWaitingForDestroy.append(resource); |
| 347 |
} |
| 348 |
bool removed = m_tabletSeat->m_pads.removeOne(this); |
| 349 |
QVERIFY(removed); |
| 350 |
} |
| 351 |
|
| 352 |
void TabletPadV2::zwp_tablet_pad_v2_destroy(QtWaylandServer::zwp_tablet_pad_v2::Resource *resource) |
| 353 |
{ |
| 354 |
if (m_tabletSeat) { |
| 355 |
m_tabletSeat->m_padsWaitingForDestroy.removeOne(resource); |
| 356 |
} |
| 357 |
wl_resource_destroy(resource->handle); |
| 358 |
} |
| 359 |
|
| 360 |
class TabletCompositor : public DefaultCompositor { |
| 361 |
public: |
| 362 |
explicit TabletCompositor() |
| 363 |
{ |
| 364 |
exec([this] { |
| 365 |
m_config.autoConfigure = true; |
| 366 |
add<TabletManagerV2>(tabletVersion); |
| 367 |
}); |
| 368 |
} |
| 369 |
TabletSeatV2 *tabletSeat(int i = 0) |
| 370 |
{ |
| 371 |
return get<TabletManagerV2>()->tabletSeatFor(get<Seat>(i)); |
| 372 |
} |
| 373 |
TabletV2 *tablet(int i = 0, int iSeat = 0) |
| 374 |
{ |
| 375 |
if (auto *ts = tabletSeat(iSeat)) |
| 376 |
return ts->m_tablets.value(i, nullptr); |
| 377 |
return nullptr; |
| 378 |
} |
| 379 |
TabletToolV2 *tabletTool(int i = 0, int iSeat = 0) |
| 380 |
{ |
| 381 |
if (auto *ts = tabletSeat(iSeat)) |
| 382 |
return ts->m_tools.value(i, nullptr); |
| 383 |
return nullptr; |
| 384 |
} |
| 385 |
TabletPadV2 *tabletPad(int i = 0, int iSeat = 0) |
| 386 |
{ |
| 387 |
if (auto *ts = tabletSeat(iSeat)) |
| 388 |
return ts->m_pads.value(i, nullptr); |
| 389 |
return nullptr; |
| 390 |
} |
| 391 |
}; |
| 392 |
|
| 393 |
Q_DECLARE_METATYPE(QtWaylandServer::zwp_tablet_tool_v2::type); |
| 394 |
Q_DECLARE_METATYPE(QPointingDevice::PointerType); |
| 395 |
Q_DECLARE_METATYPE(Qt::MouseButton); |
| 396 |
|
| 397 |
class tst_tabletv2 : public QObject, private TabletCompositor |
| 398 |
{ |
| 399 |
using ToolType = QtWaylandServer::zwp_tablet_tool_v2::type; |
| 400 |
Q_OBJECT |
| 401 |
private slots: |
| 402 |
void cleanup(); |
| 403 |
void bindsToManager(); |
| 404 |
void createsTabletSeat(); |
| 405 |
void destroysTablet(); |
| 406 |
void destroysTool(); |
| 407 |
void destroysPad(); |
| 408 |
void removeTabletBeforeTool(); |
| 409 |
void removeTabletBeforePad(); |
| 410 |
void proximityEvents(); |
| 411 |
void moveEvent(); |
| 412 |
void pointerType_data(); |
| 413 |
void pointerType(); |
| 414 |
void hardwareSerial(); |
| 415 |
void buttons_data(); |
| 416 |
void buttons(); |
| 417 |
void tabletEvents(); |
| 418 |
}; |
| 419 |
|
| 420 |
class ProximityFilter : public QObject { |
| 421 |
Q_OBJECT |
| 422 |
public: |
| 423 |
ProximityFilter() { qApp->installEventFilter(this); } |
| 424 |
~ProximityFilter() override { qDeleteAll(m_events); } |
| 425 |
QList<QTabletEvent *> m_events; |
| 426 |
|
| 427 |
int nextEventIndex = 0; |
| 428 |
int numEvents() const { return m_events.size() - nextEventIndex; } |
| 429 |
QTabletEvent *popEvent() |
| 430 |
{ |
| 431 |
auto *event = m_events.value(nextEventIndex, nullptr); |
| 432 |
if (event) |
| 433 |
++nextEventIndex; |
| 434 |
return event; |
| 435 |
} |
| 436 |
|
| 437 |
protected: |
| 438 |
bool eventFilter(QObject *object, QEvent *event) override |
| 439 |
{ |
| 440 |
Q_UNUSED(object); |
| 441 |
switch (event->type()) { |
| 442 |
case QEvent::TabletEnterProximity: |
| 443 |
case QEvent::TabletLeaveProximity: { |
| 444 |
auto *e = static_cast<QTabletEvent *>(event); |
| 445 |
auto *ev = new QTabletEvent(e->type(), e->pointingDevice(), e->position(), e->globalPosition(), |
| 446 |
e->pressure(), e->xTilt(), e->yTilt(), |
| 447 |
e->tangentialPressure(), e->rotation(), e->z(), |
| 448 |
Qt::KeyboardModifier::NoModifier, |
| 449 |
e->button(), e->buttons()); |
| 450 |
m_events << ev; |
| 451 |
break; |
| 452 |
} |
| 453 |
default: |
| 454 |
break; |
| 455 |
} |
| 456 |
return false; |
| 457 |
} |
| 458 |
}; |
| 459 |
|
| 460 |
void tst_tabletv2::cleanup() |
| 461 |
{ |
| 462 |
exec([&] { |
| 463 |
tabletSeat()->removeAll(); |
| 464 |
}); |
| 465 |
QCOMPOSITOR_COMPARE(get<TabletManagerV2>()->m_tabletSeats.size(), 1); |
| 466 |
QCOMPOSITOR_COMPARE(tabletSeat()->m_tablets.size(), 0); |
| 467 |
QCOMPOSITOR_COMPARE(tabletSeat()->m_tools.size(), 0); |
| 468 |
QCOMPOSITOR_COMPARE(tabletSeat()->m_pads.size(), 0); |
| 469 |
|
| 470 |
QTRY_VERIFY2(isClean(), qPrintable(dirtyMessage())); |
| 471 |
} |
| 472 |
|
| 473 |
void tst_tabletv2::bindsToManager() |
| 474 |
{ |
| 475 |
QCOMPOSITOR_TRY_COMPARE(get<TabletManagerV2>()->resourceMap().size(), 1); |
| 476 |
QCOMPOSITOR_TRY_COMPARE(get<TabletManagerV2>()->resourceMap().first()->version(), tabletVersion); |
| 477 |
} |
| 478 |
|
| 479 |
void tst_tabletv2::createsTabletSeat() |
| 480 |
{ |
| 481 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()); |
| 482 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()->resourceMap().contains(client())); |
| 483 |
QCOMPOSITOR_TRY_COMPARE(tabletSeat()->resourceMap().value(client())->version(), tabletVersion); |
| 484 |
//TODO: Maybe also assert some capability reported though qt APIs? |
| 485 |
} |
| 486 |
|
| 487 |
void tst_tabletv2::destroysTablet() |
| 488 |
{ |
| 489 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()); |
| 490 |
exec([&] { |
| 491 |
tabletSeat()->addTablet(); |
| 492 |
}); |
| 493 |
QCOMPOSITOR_TRY_VERIFY(tablet()); |
| 494 |
|
| 495 |
exec([&] { |
| 496 |
tablet()->sendRemoved(); |
| 497 |
}); |
| 498 |
|
| 499 |
QCOMPOSITOR_TRY_VERIFY(!tablet()); |
| 500 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()->m_tabletsWaitingForDestroy.empty()); |
| 501 |
} |
| 502 |
|
| 503 |
void tst_tabletv2::destroysTool() |
| 504 |
{ |
| 505 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()); |
| 506 |
exec([&] { |
| 507 |
tabletSeat()->addTablet(); |
| 508 |
tabletSeat()->addTool(); |
| 509 |
}); |
| 510 |
QCOMPOSITOR_TRY_VERIFY(tabletTool()); |
| 511 |
|
| 512 |
exec([&] { |
| 513 |
tabletTool()->sendRemoved(); |
| 514 |
tablet()->sendRemoved(); |
| 515 |
}); |
| 516 |
|
| 517 |
QCOMPOSITOR_TRY_VERIFY(!tabletTool()); |
| 518 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()->m_toolsWaitingForDestroy.empty()); |
| 519 |
} |
| 520 |
|
| 521 |
void tst_tabletv2::destroysPad() |
| 522 |
{ |
| 523 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()); |
| 524 |
exec([&] { |
| 525 |
tabletSeat()->addPad(); |
| 526 |
}); |
| 527 |
QCOMPOSITOR_TRY_VERIFY(tabletPad()); |
| 528 |
|
| 529 |
exec([&] { |
| 530 |
tabletPad()->sendRemoved(); |
| 531 |
}); |
| 532 |
|
| 533 |
QCOMPOSITOR_TRY_VERIFY(!tabletPad()); |
| 534 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()->m_padsWaitingForDestroy.empty()); |
| 535 |
} |
| 536 |
|
| 537 |
void tst_tabletv2::removeTabletBeforeTool() |
| 538 |
{ |
| 539 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()); |
| 540 |
exec([&] { |
| 541 |
tabletSeat()->addTablet(); |
| 542 |
tabletSeat()->addTool(); |
| 543 |
}); |
| 544 |
QCOMPOSITOR_TRY_VERIFY(tablet()); |
| 545 |
QCOMPOSITOR_TRY_VERIFY(tabletTool()); |
| 546 |
|
| 547 |
exec([&] { tablet()->sendRemoved(); }); |
| 548 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()->m_tabletsWaitingForDestroy.empty()); |
| 549 |
|
| 550 |
exec([&] { tabletTool()->sendRemoved(); }); |
| 551 |
QCOMPOSITOR_TRY_VERIFY(!tabletTool()); |
| 552 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()->m_toolsWaitingForDestroy.empty()); |
| 553 |
} |
| 554 |
|
| 555 |
void tst_tabletv2::removeTabletBeforePad() |
| 556 |
{ |
| 557 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()); |
| 558 |
exec([&] { |
| 559 |
tabletSeat()->addTablet(); |
| 560 |
tabletSeat()->addPad(); |
| 561 |
}); |
| 562 |
QCOMPOSITOR_TRY_VERIFY(tablet()); |
| 563 |
QCOMPOSITOR_TRY_VERIFY(tabletPad()); |
| 564 |
|
| 565 |
exec([&] { tablet()->sendRemoved(); }); |
| 566 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()->m_tabletsWaitingForDestroy.empty()); |
| 567 |
|
| 568 |
exec([&] { tabletPad()->sendRemoved(); }); |
| 569 |
QCOMPOSITOR_TRY_VERIFY(!tabletPad()); |
| 570 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()->m_padsWaitingForDestroy.empty()); |
| 571 |
} |
| 572 |
|
| 573 |
void tst_tabletv2::proximityEvents() |
| 574 |
{ |
| 575 |
ProximityFilter filter; |
| 576 |
|
| 577 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()); |
| 578 |
exec([&] { |
| 579 |
tabletSeat()->addTablet(); |
| 580 |
tabletSeat()->addTool(); |
| 581 |
}); |
| 582 |
|
| 583 |
QRasterWindow window; |
| 584 |
window.resize(64, 64); |
| 585 |
window.show(); |
| 586 |
QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial); |
| 587 |
|
| 588 |
QCOMPOSITOR_TRY_VERIFY(tablet()); |
| 589 |
exec([&] { |
| 590 |
auto *surface = xdgSurface()->m_surface; |
| 591 |
auto *tool = tabletTool(); |
| 592 |
tool->sendProximityIn(tablet(), surface); |
| 593 |
tool->sendFrame(); |
| 594 |
}); |
| 595 |
|
| 596 |
QTRY_COMPARE(filter.numEvents(), 1); |
| 597 |
QTabletEvent *enterEvent = filter.popEvent(); |
| 598 |
QCOMPARE(enterEvent->type(), QEvent::TabletEnterProximity); |
| 599 |
|
| 600 |
exec([&] { |
| 601 |
auto *tool = tabletTool(); |
| 602 |
tool->sendProximityOut(); |
| 603 |
tool->sendFrame(); |
| 604 |
}); |
| 605 |
|
| 606 |
QTRY_COMPARE(filter.numEvents(), 1); |
| 607 |
QTabletEvent *leaveEvent = filter.popEvent(); |
| 608 |
QCOMPARE(leaveEvent->type(), QEvent::TabletLeaveProximity); |
| 609 |
} |
| 610 |
|
| 611 |
class TabletWindow : public QRasterWindow { |
| 612 |
Q_OBJECT |
| 613 |
public: |
| 614 |
~TabletWindow() override { qDeleteAll(m_events); } |
| 615 |
|
| 616 |
void tabletEvent(QTabletEvent *e) override |
| 617 |
{ |
| 618 |
m_events << new QTabletEvent(e->type(), e->pointingDevice(), e->position(), e->globalPosition(), |
| 619 |
e->pressure(), e->xTilt(), e->yTilt(), |
| 620 |
e->tangentialPressure(), e->rotation(), e->z(), |
| 621 |
Qt::KeyboardModifier::NoModifier, |
| 622 |
e->button(), e->buttons()); |
| 623 |
emit tabletEventReceived(m_events.last()); |
| 624 |
} |
| 625 |
int nextEventIndex = 0; |
| 626 |
int numEvents() const { return m_events.size() - nextEventIndex; } |
| 627 |
QTabletEvent *popEvent() |
| 628 |
{ |
| 629 |
auto *event = m_events.value(nextEventIndex, nullptr); |
| 630 |
if (event) |
| 631 |
++nextEventIndex; |
| 632 |
return event; |
| 633 |
} |
| 634 |
|
| 635 |
signals: |
| 636 |
void tabletEventReceived(QTabletEvent *event); |
| 637 |
|
| 638 |
private: |
| 639 |
QList<QTabletEvent *> m_events; |
| 640 |
}; |
| 641 |
|
| 642 |
void tst_tabletv2::moveEvent() |
| 643 |
{ |
| 644 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()); |
| 645 |
exec([&] { |
| 646 |
tabletSeat()->addTablet(); |
| 647 |
tabletSeat()->addTool(); |
| 648 |
}); |
| 649 |
|
| 650 |
TabletWindow window; |
| 651 |
window.resize(64, 64); |
| 652 |
window.show(); |
| 653 |
QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial); |
| 654 |
|
| 655 |
QCOMPOSITOR_TRY_VERIFY(tablet()); |
| 656 |
exec([&] { |
| 657 |
auto *surface = xdgSurface()->m_surface; |
| 658 |
auto *tool = tabletTool(); |
| 659 |
tool->sendProximityIn(tablet(), surface); |
| 660 |
QMargins margins = window.frameMargins(); |
| 661 |
tool->sendMotion(QPointF(12 + margins.left(), 34 + margins.top())); |
| 662 |
tool->sendFrame(); |
| 663 |
}); |
| 664 |
QTRY_VERIFY(window.numEvents()); |
| 665 |
QTabletEvent *event = window.popEvent(); |
| 666 |
QCOMPARE(event->type(), QEvent::TabletMove); |
| 667 |
QCOMPARE(event->pressure(), 0); |
| 668 |
QCOMPARE(event->position(), QPointF(12, 34)); |
| 669 |
} |
| 670 |
|
| 671 |
void tst_tabletv2::pointerType_data() |
| 672 |
{ |
| 673 |
QTest::addColumn<ToolType>("toolType"); |
| 674 |
QTest::addColumn<QPointingDevice::PointerType>("pointerType"); |
| 675 |
QTest::addColumn<QInputDevice::DeviceType>("tabletDevice"); |
| 676 |
|
| 677 |
QTest::newRow("pen") << ToolType::type_pen << QPointingDevice::PointerType::Pen << QInputDevice::DeviceType::Stylus; |
| 678 |
QTest::newRow("eraser") << ToolType::type_eraser << QPointingDevice::PointerType::Eraser << QInputDevice::DeviceType::Stylus; |
| 679 |
QTest::newRow("pencil") << ToolType::type_pencil << QPointingDevice::PointerType::Pen << QInputDevice::DeviceType::Stylus; |
| 680 |
QTest::newRow("airbrush") << ToolType::type_airbrush << QPointingDevice::PointerType::Pen << QInputDevice::DeviceType::Airbrush; |
| 681 |
QTest::newRow("brush") << ToolType::type_brush << QPointingDevice::PointerType::Pen << QInputDevice::DeviceType::Stylus; // TODO: is TabletDevice::Stylus the right thing? |
| 682 |
QTest::newRow("lens") << ToolType::type_lens << QPointingDevice::PointerType::Cursor << QInputDevice::DeviceType::Puck; |
| 683 |
// TODO: also add tests for FourDMouse and RotationStylus (also need to send capabilities) |
| 684 |
|
| 685 |
// TODO: should these rather be mapped to touch/mouse events? |
| 686 |
QTest::newRow("finger") << ToolType::type_finger << QPointingDevice::PointerType::Unknown << QInputDevice::DeviceType::Unknown; |
| 687 |
QTest::newRow("mouse") << ToolType::type_mouse << QPointingDevice::PointerType::Cursor << QInputDevice::DeviceType::Unknown; |
| 688 |
} |
| 689 |
|
| 690 |
void tst_tabletv2::pointerType() |
| 691 |
{ |
| 692 |
using ToolType = QtWaylandServer::zwp_tablet_tool_v2::type; |
| 693 |
QFETCH(ToolType, toolType); |
| 694 |
QFETCH(QPointingDevice::PointerType, pointerType); |
| 695 |
QFETCH(QInputDevice::DeviceType, tabletDevice); |
| 696 |
|
| 697 |
ProximityFilter filter; |
| 698 |
|
| 699 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()); |
| 700 |
exec([&] { |
| 701 |
tabletSeat()->addTablet(); |
| 702 |
tabletSeat()->addTool(toolType); |
| 703 |
}); |
| 704 |
|
| 705 |
TabletWindow window; |
| 706 |
window.resize(64, 64); |
| 707 |
window.show(); |
| 708 |
QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial); |
| 709 |
|
| 710 |
QCOMPOSITOR_TRY_VERIFY(tablet()); |
| 711 |
exec([&] { |
| 712 |
auto *surface = xdgSurface()->m_surface; |
| 713 |
auto *tool = tabletTool(); |
| 714 |
tool->sendProximityIn(tablet(), surface); |
| 715 |
QMargins margins = window.frameMargins(); |
| 716 |
tool->sendMotion(QPointF(12 + margins.left(), 34 + margins.top())); |
| 717 |
tool->sendFrame(); |
| 718 |
}); |
| 719 |
|
| 720 |
QTRY_COMPARE(filter.numEvents(), 1); |
| 721 |
QTabletEvent *event = filter.popEvent(); |
| 722 |
QCOMPARE(event->pointerType(), pointerType); |
| 723 |
QCOMPARE(event->deviceType(), tabletDevice); |
| 724 |
|
| 725 |
QTRY_VERIFY(window.numEvents()); |
| 726 |
event = window.popEvent(); |
| 727 |
QCOMPARE(event->pointerType(), pointerType); |
| 728 |
QCOMPARE(event->deviceType(), tabletDevice); |
| 729 |
|
| 730 |
exec([&] { |
| 731 |
tabletTool()->sendProximityOut(); |
| 732 |
tabletTool()->sendFrame(); |
| 733 |
}); |
| 734 |
|
| 735 |
QTRY_VERIFY(filter.numEvents()); |
| 736 |
event = filter.popEvent(); |
| 737 |
QCOMPARE(event->pointerType(), pointerType); |
| 738 |
QCOMPARE(event->deviceType(), tabletDevice); |
| 739 |
} |
| 740 |
|
| 741 |
void tst_tabletv2::hardwareSerial() |
| 742 |
{ |
| 743 |
ProximityFilter filter; |
| 744 |
const QPointingDeviceUniqueId uid = QPointingDeviceUniqueId::fromNumericId(0xbaba15dead15f00d); |
| 745 |
|
| 746 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()); |
| 747 |
exec([&] { |
| 748 |
tabletSeat()->addTablet(); |
| 749 |
tabletSeat()->addTool(ToolType::type_pen, uid.numericId()); |
| 750 |
}); |
| 751 |
|
| 752 |
TabletWindow window; |
| 753 |
window.resize(64, 64); |
| 754 |
window.show(); |
| 755 |
QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial); |
| 756 |
|
| 757 |
QCOMPOSITOR_TRY_VERIFY(tablet()); |
| 758 |
exec([&] { |
| 759 |
auto *surface = xdgSurface()->m_surface; |
| 760 |
auto *tool = tabletTool(); |
| 761 |
tool->sendProximityIn(tablet(), surface); |
| 762 |
QMargins margins = window.frameMargins(); |
| 763 |
tool->sendMotion(QPointF(12 + margins.left(), 34 + margins.top())); |
| 764 |
tool->sendFrame(); |
| 765 |
}); |
| 766 |
|
| 767 |
QTRY_COMPARE(filter.numEvents(), 1); |
| 768 |
QTabletEvent *event = filter.popEvent(); |
| 769 |
QCOMPARE(event->pointingDevice()->uniqueId(), uid); |
| 770 |
|
| 771 |
QTRY_VERIFY(window.numEvents()); |
| 772 |
event = window.popEvent(); |
| 773 |
QCOMPARE(event->pointingDevice()->uniqueId(), uid); |
| 774 |
|
| 775 |
exec([&] { |
| 776 |
tabletTool()->sendProximityOut(); |
| 777 |
tabletTool()->sendFrame(); |
| 778 |
}); |
| 779 |
|
| 780 |
QTRY_VERIFY(filter.numEvents()); |
| 781 |
event = filter.popEvent(); |
| 782 |
QCOMPARE(event->pointingDevice()->uniqueId(), uid); |
| 783 |
} |
| 784 |
|
| 785 |
// As defined in linux/input-event-codes.h |
| 786 |
#ifndef BTN_STYLUS |
| 787 |
#define BTN_STYLUS 0x14b |
| 788 |
#endif |
| 789 |
#ifndef BTN_STYLUS2 |
| 790 |
#define BTN_STYLUS2 0x14c |
| 791 |
#endif |
| 792 |
|
| 793 |
void tst_tabletv2::buttons_data() |
| 794 |
{ |
| 795 |
QTest::addColumn<uint>("tabletButton"); |
| 796 |
QTest::addColumn<Qt::MouseButton>("mouseButton"); |
| 797 |
|
| 798 |
QTest::newRow("BTN_STYLUS2") << uint(BTN_STYLUS2) << Qt::MouseButton::RightButton; |
| 799 |
QTest::newRow("BTN_STYLUS") << uint(BTN_STYLUS) << Qt::MouseButton::MiddleButton; |
| 800 |
} |
| 801 |
|
| 802 |
void tst_tabletv2::buttons() |
| 803 |
{ |
| 804 |
QFETCH(uint, tabletButton); |
| 805 |
QFETCH(Qt::MouseButton, mouseButton); |
| 806 |
|
| 807 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()); |
| 808 |
exec([&] { |
| 809 |
tabletSeat()->addTablet(); |
| 810 |
tabletSeat()->addTool(); |
| 811 |
}); |
| 812 |
|
| 813 |
TabletWindow window; |
| 814 |
window.resize(64, 64); |
| 815 |
window.show(); |
| 816 |
QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial); |
| 817 |
|
| 818 |
QCOMPOSITOR_TRY_VERIFY(tablet()); |
| 819 |
exec([&] { |
| 820 |
tabletTool()->sendProximityIn(tablet(), xdgSurface()->m_surface); |
| 821 |
QMargins margins = window.frameMargins(); |
| 822 |
tabletTool()->sendMotion(QPointF(12 + margins.left(), 34 + margins.top())); |
| 823 |
tabletTool()->sendFrame(); |
| 824 |
}); |
| 825 |
|
| 826 |
QTRY_VERIFY(window.numEvents()); |
| 827 |
window.popEvent(); |
| 828 |
|
| 829 |
QCOMPOSITOR_TRY_VERIFY(tablet()); |
| 830 |
exec([&] { |
| 831 |
tabletTool()->sendButton(tabletButton, true); |
| 832 |
tabletTool()->sendFrame(); |
| 833 |
tabletTool()->sendButton(tabletButton, false); |
| 834 |
tabletTool()->sendFrame(); |
| 835 |
}); |
| 836 |
|
| 837 |
QTRY_VERIFY(window.numEvents()); |
| 838 |
QTabletEvent *event = window.popEvent(); |
| 839 |
QCOMPARE(event->buttons(), mouseButton); |
| 840 |
|
| 841 |
exec([&] { |
| 842 |
tabletTool()->sendProximityOut(); |
| 843 |
tabletTool()->sendFrame(); |
| 844 |
}); |
| 845 |
} |
| 846 |
|
| 847 |
void tst_tabletv2::tabletEvents() |
| 848 |
{ |
| 849 |
QCOMPOSITOR_TRY_VERIFY(tabletSeat()); |
| 850 |
exec([&] { |
| 851 |
tabletSeat()->addTablet(); |
| 852 |
tabletSeat()->addTool(); |
| 853 |
}); |
| 854 |
|
| 855 |
TabletWindow window; |
| 856 |
window.resize(64, 64); |
| 857 |
window.show(); |
| 858 |
QCOMPOSITOR_TRY_VERIFY(xdgSurface() && xdgSurface()->m_committedConfigureSerial); |
| 859 |
|
| 860 |
const QPointF insideDecorations(window.frameMargins().left(), window.frameMargins().top()); |
| 861 |
|
| 862 |
QCOMPOSITOR_TRY_VERIFY(tablet()); |
| 863 |
exec([&] { |
| 864 |
auto *surface = xdgSurface()->m_surface; |
| 865 |
auto *tool = tabletTool(); |
| 866 |
// TODO: encapsulate this into a helper function? |
| 867 |
tool->sendProximityIn(tablet(), surface); |
| 868 |
tool->sendMotion(QPointF(12, 34) + insideDecorations); |
| 869 |
tool->sendDown(); |
| 870 |
tool->sendPressure(65535); |
| 871 |
tool->sendFrame(); |
| 872 |
}); |
| 873 |
|
| 874 |
QTRY_VERIFY(window.numEvents()); |
| 875 |
QTabletEvent *event = window.popEvent(); |
| 876 |
QCOMPARE(event->type(), QEvent::TabletPress); |
| 877 |
QCOMPARE(event->pressure(), 1.0); |
| 878 |
QCOMPARE(event->position(), QPointF(12, 34)); |
| 879 |
|
| 880 |
// Values we didn't send should be 0 |
| 881 |
QCOMPARE(event->rotation(), 0); |
| 882 |
QCOMPARE(event->xTilt(), 0); |
| 883 |
QCOMPARE(event->yTilt(), 0); |
| 884 |
|
| 885 |
exec([&] { |
| 886 |
tabletTool()->sendMotion(QPointF(45, 56) + insideDecorations); |
| 887 |
tabletTool()->sendPressure(65535/2); |
| 888 |
tabletTool()->sendRotation(90); |
| 889 |
tabletTool()->sendTilt(13, 37); |
| 890 |
tabletTool()->sendFrame(); |
| 891 |
}); |
| 892 |
|
| 893 |
QTRY_VERIFY(window.numEvents()); |
| 894 |
event = window.popEvent(); |
| 895 |
QCOMPARE(event->type(), QEvent::TabletMove); |
| 896 |
QVERIFY(qAbs(event->pressure() - 0.5) < 0.01); |
| 897 |
QVERIFY(qAbs(event->rotation() - 90) < 0.01); |
| 898 |
QVERIFY(qAbs(event->xTilt() - 13) < 0.01); |
| 899 |
QVERIFY(qAbs(event->yTilt() - 37) < 0.01); |
| 900 |
QCOMPARE(event->position(), QPointF(45, 56)); |
| 901 |
|
| 902 |
// Verify that the values stay the same if we don't update them |
| 903 |
exec([&] { |
| 904 |
tabletTool()->sendMotion(QPointF(10, 11) + insideDecorations); // Change position only |
| 905 |
tabletTool()->sendFrame(); |
| 906 |
}); |
| 907 |
QTRY_VERIFY(window.numEvents()); |
| 908 |
event = window.popEvent(); |
| 909 |
QCOMPARE(event->type(), QEvent::TabletMove); |
| 910 |
QVERIFY(qAbs(event->pressure() - 0.5) < 0.01); |
| 911 |
QVERIFY(qAbs(event->rotation() - 90) < 0.01); |
| 912 |
QVERIFY(qAbs(event->xTilt() - 13) < 0.01); |
| 913 |
QVERIFY(qAbs(event->yTilt() - 37) < 0.01); |
| 914 |
QCOMPARE(event->position(), QPointF(10, 11)); |
| 915 |
|
| 916 |
exec([&] { |
| 917 |
tabletTool()->sendPressure(0); |
| 918 |
tabletTool()->sendUp(); |
| 919 |
tabletTool()->sendFrame(); |
| 920 |
|
| 921 |
tabletTool()->sendProximityOut(); |
| 922 |
tabletTool()->sendFrame(); |
| 923 |
}); |
| 924 |
|
| 925 |
QTRY_VERIFY(window.numEvents()); |
| 926 |
event = window.popEvent(); |
| 927 |
QCOMPARE(event->type(), QEvent::TabletRelease); |
| 928 |
QCOMPARE(event->pressure(), 0); |
| 929 |
QCOMPARE(event->position(), QPointF(10, 11)); |
| 930 |
} |
| 931 |
|
| 932 |
QCOMPOSITOR_TEST_MAIN(tst_tabletv2) |
| 933 |
#include "tst_tabletv2.moc" |