wayland_touch_delegate.cc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/exo/wayland/wayland_touch_delegate.h"
  5. #include <wayland-server-core.h>
  6. #include <wayland-server-protocol-core.h>
  7. #include "components/exo/touch.h"
  8. #include "components/exo/wayland/serial_tracker.h"
  9. #include "components/exo/wayland/server_util.h"
  10. namespace exo {
  11. namespace wayland {
  12. WaylandTouchDelegate::WaylandTouchDelegate(wl_resource* touch_resource,
  13. SerialTracker* serial_tracker)
  14. : touch_resource_(touch_resource), serial_tracker_(serial_tracker) {}
  15. void WaylandTouchDelegate::OnTouchDestroying(Touch* touch) {
  16. delete this;
  17. }
  18. bool WaylandTouchDelegate::CanAcceptTouchEventsForSurface(
  19. Surface* surface) const {
  20. wl_resource* surface_resource = GetSurfaceResource(surface);
  21. // We can accept events for this surface if the client is the same as the
  22. // touch resource.
  23. return surface_resource &&
  24. wl_resource_get_client(surface_resource) == client();
  25. }
  26. void WaylandTouchDelegate::OnTouchDown(Surface* surface,
  27. base::TimeTicks time_stamp,
  28. int id,
  29. const gfx::PointF& location) {
  30. wl_resource* surface_resource = GetSurfaceResource(surface);
  31. DCHECK(surface_resource);
  32. SendTimestamp(time_stamp);
  33. wl_touch_send_down(
  34. touch_resource_,
  35. serial_tracker_->GetNextSerial(SerialTracker::EventType::TOUCH_DOWN),
  36. TimeTicksToMilliseconds(time_stamp), surface_resource, id,
  37. wl_fixed_from_double(location.x()), wl_fixed_from_double(location.y()));
  38. }
  39. void WaylandTouchDelegate::OnTouchUp(base::TimeTicks time_stamp, int id) {
  40. SendTimestamp(time_stamp);
  41. wl_touch_send_up(
  42. touch_resource_,
  43. serial_tracker_->GetNextSerial(SerialTracker::EventType::TOUCH_UP),
  44. TimeTicksToMilliseconds(time_stamp), id);
  45. }
  46. void WaylandTouchDelegate::OnTouchMotion(base::TimeTicks time_stamp,
  47. int id,
  48. const gfx::PointF& location) {
  49. SendTimestamp(time_stamp);
  50. wl_touch_send_motion(touch_resource_, TimeTicksToMilliseconds(time_stamp), id,
  51. wl_fixed_from_double(location.x()),
  52. wl_fixed_from_double(location.y()));
  53. }
  54. void WaylandTouchDelegate::OnTouchShape(int id, float major, float minor) {
  55. if (wl_resource_get_version(touch_resource_) >=
  56. WL_TOUCH_SHAPE_SINCE_VERSION) {
  57. wl_touch_send_shape(touch_resource_, id, wl_fixed_from_double(major),
  58. wl_fixed_from_double(minor));
  59. }
  60. }
  61. void WaylandTouchDelegate::OnTouchFrame() {
  62. if (wl_resource_get_version(touch_resource_) >=
  63. WL_TOUCH_FRAME_SINCE_VERSION) {
  64. wl_touch_send_frame(touch_resource_);
  65. }
  66. wl_client_flush(client());
  67. }
  68. void WaylandTouchDelegate::OnTouchCancel() {
  69. wl_touch_send_cancel(touch_resource_);
  70. serial_tracker_->ResetTouchDownSerial();
  71. }
  72. wl_client* WaylandTouchDelegate::client() const {
  73. return wl_resource_get_client(touch_resource_);
  74. }
  75. } // namespace wayland
  76. } // namespace exo