File: touch.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (241 lines) | stat: -rw-r--r-- 7,965 bytes parent folder | download | duplicates (6)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/exo/touch.h"

#include <algorithm>

#include "base/trace_event/trace_event.h"
#include "components/exo/input_trace.h"
#include "components/exo/seat.h"
#include "components/exo/shell_surface_util.h"
#include "components/exo/surface.h"
#include "components/exo/touch_delegate.h"
#include "components/exo/touch_stylus_delegate.h"
#include "components/exo/wm_helper.h"
#include "ui/aura/window.h"
#include "ui/compositor/layer.h"
#include "ui/events/event.h"
#include "ui/wm/core/capture_controller.h"
#include "ui/wm/core/window_util.h"

namespace exo {
namespace {

gfx::PointF EventLocationInWindow(ui::TouchEvent* event, aura::Window* window) {
  ui::Layer* root = window->GetRootWindow()->layer();
  ui::Layer* target = window->layer();

  gfx::Transform transform;
  target->GetTargetTransformRelativeTo(root, &transform);
  gfx::PointF point = event->root_location_f();
  return transform.InverseMapPoint(point).value_or(point);
}

}  // namespace

////////////////////////////////////////////////////////////////////////////////
// Touch, public:

Touch::Touch(TouchDelegate* delegate, Seat* seat)
    : delegate_(delegate), seat_(seat) {
  ash::Shell::Get()->AddShellObserver(this);
  for (aura::Window* root : ash::Shell::GetAllRootWindows()) {
    root->AddPreTargetHandler(this);
  }
}

Touch::~Touch() {
  ash::Shell::Get()->RemoveShellObserver(this);
  for (aura::Window* root : ash::Shell::GetAllRootWindows()) {
    root->RemovePreTargetHandler(this);
  }
  delegate_->OnTouchDestroying(this);
  if (HasStylusDelegate())
    stylus_delegate_->OnTouchDestroying(this);
  CancelAllTouches();
}

void Touch::SetStylusDelegate(TouchStylusDelegate* delegate) {
  stylus_delegate_ = delegate;
}

bool Touch::HasStylusDelegate() const {
  return !!stylus_delegate_;
}

////////////////////////////////////////////////////////////////////////////////
// ui::EventHandler overrides:

void Touch::OnTouchEvent(ui::TouchEvent* event) {
  if (seat_->was_shutdown() || event->handled()) {
    return;
  }

  // TODO(crbug.com/40061238): Investigate if we need to do something similar to
  // the filter in `Pointer::OnMouseEvent` when dragging. (not sending touch
  // events during drag)

  bool send_details = false;

  auto event_type = event->type();
  if ((event->flags() & ui::EF_RESERVED_FOR_GESTURE) != 0) {
    event_type = ui::EventType::kTouchCancelled;
  }

  const int touch_pointer_id = event->pointer_details().id;
  switch (event_type) {
    case ui::EventType::kTouchPressed: {
      // Early out if event doesn't contain a valid target for touch device.
      // TODO(b/147848270): Verify GetEffectiveTargetForEvent gets the correct
      // surface when input is captured.
      Surface* target = GetEffectiveTargetForEvent(event);
      if (!target)
        return;

      TRACE_EXO_INPUT_EVENT(event);
      DCHECK(touch_points_surface_map_.find(touch_pointer_id) ==
             touch_points_surface_map_.end());

      touch_points_surface_map_.emplace(touch_pointer_id, target);

      // Update the count of pointers on the target surface.
      auto it = surface_touch_count_map_.find(target);
      if (it == surface_touch_count_map_.end()) {
        target->AddSurfaceObserver(this);
        surface_touch_count_map_.emplace(target, 1);
      } else {
        it->second++;
      }

      // Convert location to target surface coordinate space.
      const gfx::PointF location =
          EventLocationInWindow(event, target->window());

      // Generate a touch down event for the target surface.
      delegate_->OnTouchDown(target, event->time_stamp(), touch_pointer_id,
                             location);
      if (stylus_delegate_ && event->pointer_details().pointer_type !=
                                  ui::EventPointerType::kTouch) {
        stylus_delegate_->OnTouchTool(touch_pointer_id,
                                      event->pointer_details().pointer_type);
      }
      send_details = true;
    } break;
    case ui::EventType::kTouchReleased: {
      auto it = touch_points_surface_map_.find(touch_pointer_id);
      if (it == touch_points_surface_map_.end())
        return;

      Surface* target = it->second;
      DCHECK(target);

      TRACE_EXO_INPUT_EVENT(event);

      touch_points_surface_map_.erase(it);

      // Update the count of pointers on the target surface.
      auto count_it = surface_touch_count_map_.find(target);
      if (count_it == surface_touch_count_map_.end())
        return;
      if ((--count_it->second) <= 0) {
        surface_touch_count_map_.erase(target);
        target->RemoveSurfaceObserver(this);
      }

      delegate_->OnTouchUp(event->time_stamp(), touch_pointer_id);
      seat_->AbortPendingDragOperation();
    } break;
    case ui::EventType::kTouchMoved: {
      auto it = touch_points_surface_map_.find(touch_pointer_id);
      if (it == touch_points_surface_map_.end())
        return;

      Surface* target = it->second;
      DCHECK(target);

      TRACE_EXO_INPUT_EVENT(event);

      // Convert location to focus surface coordinate space.
      gfx::PointF location = EventLocationInWindow(event, target->window());
      delegate_->OnTouchMotion(event->time_stamp(), touch_pointer_id, location);
      send_details = true;
    } break;
    case ui::EventType::kTouchCancelled: {
      TRACE_EXO_INPUT_EVENT(event);
      // Cancel the full set of touch sequences as soon as one is canceled.
      CancelAllTouches();
      delegate_->OnTouchCancel();

      seat_->AbortPendingDragOperation();
    } break;
    default:
      NOTREACHED();
  }
  if (send_details) {
    // Some devices do not report radius_y/minor. We assume a circular shape
    // in that case.
    float major = event->pointer_details().radius_x * 2.0f;
    float minor = event->pointer_details().radius_y * 2.0f;
    if (!minor)
      minor = major;
    delegate_->OnTouchShape(touch_pointer_id, major, minor);

    if (stylus_delegate_ &&
        event->pointer_details().pointer_type != ui::EventPointerType::kTouch) {
      if (!std::isnan(event->pointer_details().force)) {
        stylus_delegate_->OnTouchForce(event->time_stamp(), touch_pointer_id,
                                       event->pointer_details().force);
      }
      stylus_delegate_->OnTouchTilt(
          event->time_stamp(), touch_pointer_id,
          gfx::Vector2dF(event->pointer_details().tilt_x,
                         event->pointer_details().tilt_y));
    }
  }
  // TODO(denniskempin): Extend ui::TouchEvent to signal end of sequence of
  // touch events to send TouchFrame once after all touches have been updated.
  delegate_->OnTouchFrame();
}

////////////////////////////////////////////////////////////////////////////////
// SurfaceObserver overrides:

void Touch::OnSurfaceDestroying(Surface* surface) {
  // TODO(b/147848407): Do not cancel touches on surfaces of different clients
  // when this surface dies.
  CancelAllTouches();
  delegate_->OnTouchCancel();
}

// ash::ShellObserver:
void Touch::OnRootWindowAdded(aura::Window* root_window) {
  root_window->AddPreTargetHandler(this);
}

void Touch::OnRootWindowWillShutdown(aura::Window* root_window) {
  root_window->RemovePreTargetHandler(this);
}

////////////////////////////////////////////////////////////////////////////////
// Touch, private:

Surface* Touch::GetEffectiveTargetForEvent(ui::LocatedEvent* event) const {
  Surface* target = GetTargetSurfaceForLocatedEvent(event);

  if (!target)
    return nullptr;

  return delegate_->CanAcceptTouchEventsForSurface(target) ? target : nullptr;
}

void Touch::CancelAllTouches() {
  std::ranges::for_each(surface_touch_count_map_, [this](auto& it) {
    it.first->RemoveSurfaceObserver(this);
  });
  touch_points_surface_map_.clear();
  surface_touch_count_map_.clear();
}

}  // namespace exo