1 // Copyright 2013 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.
5 #include "ash/touch/touch_hud_debug.h"
7 #include "ash/display/display_manager.h"
8 #include "ash/root_window_controller.h"
10 #include "base/json/json_string_value_serializer.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "third_party/skia/include/core/SkPath.h"
15 #include "ui/aura/window_event_dispatcher.h"
16 #include "ui/events/event.h"
17 #include "ui/gfx/animation/animation_delegate.h"
18 #include "ui/gfx/canvas.h"
19 #include "ui/gfx/display.h"
20 #include "ui/gfx/geometry/size.h"
21 #include "ui/gfx/geometry/vector2d.h"
22 #include "ui/gfx/transform.h"
23 #include "ui/views/controls/label.h"
24 #include "ui/views/layout/box_layout.h"
25 #include "ui/views/widget/widget.h"
28 #include <X11/extensions/XInput2.h>
31 #include "ui/events/devices/x11/device_data_manager_x11.h"
36 const int kPointRadius
= 20;
37 const SkColor kColors
[] = {
47 SkColorSetRGB(0xFF, 0x8C, 0x00),
48 SkColorSetRGB(0x8B, 0x45, 0x13),
49 SkColorSetRGB(0xFF, 0xDE, 0xAD),
51 const int kAlpha
= 0x60;
52 const int kMaxPaths
= arraysize(kColors
);
53 const int kReducedScale
= 10;
55 const char* GetTouchEventLabel(ui::EventType type
) {
59 case ui::ET_TOUCH_PRESSED
:
61 case ui::ET_TOUCH_MOVED
:
63 case ui::ET_TOUCH_RELEASED
:
65 case ui::ET_TOUCH_CANCELLED
:
73 int GetTrackingId(const ui::TouchEvent
& event
) {
74 if (!event
.HasNativeEvent())
77 ui::DeviceDataManagerX11
* manager
= ui::DeviceDataManagerX11::GetInstance();
79 if (manager
->GetEventData(*event
.native_event(),
80 ui::DeviceDataManagerX11::DT_TOUCH_TRACKING_ID
,
82 return static_cast<int>(tracking_id
);
88 // A TouchPointLog represents a single touch-event of a touch point.
89 struct TouchPointLog
{
91 explicit TouchPointLog(const ui::TouchEvent
& touch
)
92 : id(touch
.touch_id()),
94 location(touch
.root_location()),
95 timestamp(touch
.time_stamp().InMillisecondsF()),
96 radius_x(touch
.pointer_details().radius_x()),
97 radius_y(touch
.pointer_details().radius_y()),
98 pressure(touch
.pointer_details().force()),
99 tracking_id(GetTrackingId(touch
)),
100 source_device(touch
.source_device_id()) {}
102 // Populates a dictionary value with all the information about the touch
104 scoped_ptr
<base::DictionaryValue
> GetAsDictionary() const {
105 scoped_ptr
<base::DictionaryValue
> value(new base::DictionaryValue());
107 value
->SetInteger("id", id
);
108 value
->SetString("type", std::string(GetTouchEventLabel(type
)));
109 value
->SetString("location", location
.ToString());
110 value
->SetDouble("timestamp", timestamp
);
111 value
->SetDouble("radius_x", radius_x
);
112 value
->SetDouble("radius_y", radius_y
);
113 value
->SetDouble("pressure", pressure
);
114 value
->SetInteger("tracking_id", tracking_id
);
115 value
->SetInteger("source_device", source_device
);
131 // A TouchTrace keeps track of all the touch events of a single touch point
132 // (starting from a touch-press and ending at a touch-release or touch-cancel).
135 typedef std::vector
<TouchPointLog
>::iterator iterator
;
136 typedef std::vector
<TouchPointLog
>::const_iterator const_iterator
;
137 typedef std::vector
<TouchPointLog
>::reverse_iterator reverse_iterator
;
138 typedef std::vector
<TouchPointLog
>::const_reverse_iterator
139 const_reverse_iterator
;
144 void AddTouchPoint(const ui::TouchEvent
& touch
) {
145 log_
.push_back(TouchPointLog(touch
));
148 const std::vector
<TouchPointLog
>& log() const { return log_
; }
150 bool active() const {
151 return !log_
.empty() && log_
.back().type
!= ui::ET_TOUCH_RELEASED
&&
152 log_
.back().type
!= ui::ET_TOUCH_CANCELLED
;
155 // Returns a list containing data from all events for the touch point.
156 scoped_ptr
<base::ListValue
> GetAsList() const {
157 scoped_ptr
<base::ListValue
> list(new base::ListValue());
158 for (const_iterator i
= log_
.begin(); i
!= log_
.end(); ++i
)
159 list
->Append((*i
).GetAsDictionary().release());
168 std::vector
<TouchPointLog
> log_
;
170 DISALLOW_COPY_AND_ASSIGN(TouchTrace
);
173 // A TouchLog keeps track of all touch events of all touch points.
176 TouchLog() : next_trace_index_(0) {
179 void AddTouchPoint(const ui::TouchEvent
& touch
) {
180 if (touch
.type() == ui::ET_TOUCH_PRESSED
)
186 next_trace_index_
= 0;
187 for (int i
= 0; i
< kMaxPaths
; ++i
)
191 scoped_ptr
<base::ListValue
> GetAsList() const {
192 scoped_ptr
<base::ListValue
> list(new base::ListValue());
193 for (int i
= 0; i
< kMaxPaths
; ++i
) {
194 if (!traces_
[i
].log().empty())
195 list
->Append(traces_
[i
].GetAsList().release());
200 int GetTraceIndex(int touch_id
) const {
201 return touch_id_to_trace_index_
.at(touch_id
);
204 const TouchTrace
* traces() const {
209 void StartTrace(const ui::TouchEvent
& touch
) {
210 // Find the first inactive spot; otherwise, overwrite the one
211 // |next_trace_index_| is pointing to.
212 int old_trace_index
= next_trace_index_
;
214 if (!traces_
[next_trace_index_
].active())
216 next_trace_index_
= (next_trace_index_
+ 1) % kMaxPaths
;
217 } while (next_trace_index_
!= old_trace_index
);
218 int touch_id
= touch
.touch_id();
219 traces_
[next_trace_index_
].Reset();
220 touch_id_to_trace_index_
[touch_id
] = next_trace_index_
;
221 next_trace_index_
= (next_trace_index_
+ 1) % kMaxPaths
;
224 void AddToTrace(const ui::TouchEvent
& touch
) {
225 int touch_id
= touch
.touch_id();
226 int trace_index
= touch_id_to_trace_index_
[touch_id
];
227 traces_
[trace_index
].AddTouchPoint(touch
);
230 TouchTrace traces_
[kMaxPaths
];
231 int next_trace_index_
;
233 std::map
<int, int> touch_id_to_trace_index_
;
235 DISALLOW_COPY_AND_ASSIGN(TouchLog
);
238 // TouchHudCanvas draws touch traces in |FULLSCREEN| and |REDUCED_SCALE| modes.
239 class TouchHudCanvas
: public views::View
{
241 explicit TouchHudCanvas(const TouchLog
& touch_log
)
242 : touch_log_(touch_log
),
244 SetPaintToLayer(true);
245 SetFillsBoundsOpaquely(false);
247 paint_
.setStyle(SkPaint::kFill_Style
);
250 ~TouchHudCanvas() override
{}
252 void SetScale(int scale
) {
256 gfx::Transform transform
;
257 transform
.Scale(1. / scale_
, 1. / scale_
);
258 layer()->SetTransform(transform
);
261 int scale() const { return scale_
; }
263 void TouchPointAdded(int touch_id
) {
264 int trace_index
= touch_log_
.GetTraceIndex(touch_id
);
265 const TouchTrace
& trace
= touch_log_
.traces()[trace_index
];
266 const TouchPointLog
& point
= trace
.log().back();
267 if (point
.type
== ui::ET_TOUCH_PRESSED
)
268 StartedTrace(trace_index
);
269 if (point
.type
!= ui::ET_TOUCH_CANCELLED
)
270 AddedPointToTrace(trace_index
);
274 for (int i
= 0; i
< kMaxPaths
; ++i
)
281 void StartedTrace(int trace_index
) {
282 paths_
[trace_index
].reset();
283 colors_
[trace_index
] = SkColorSetA(kColors
[trace_index
], kAlpha
);
286 void AddedPointToTrace(int trace_index
) {
287 const TouchTrace
& trace
= touch_log_
.traces()[trace_index
];
288 const TouchPointLog
& point
= trace
.log().back();
289 const gfx::Point
& location
= point
.location
;
290 SkScalar x
= SkIntToScalar(location
.x());
291 SkScalar y
= SkIntToScalar(location
.y());
293 if (!paths_
[trace_index
].getLastPt(&last
) || x
!= last
.x() ||
295 paths_
[trace_index
].addCircle(x
, y
, SkIntToScalar(kPointRadius
));
300 // Overridden from views::View.
301 void OnPaint(gfx::Canvas
* canvas
) override
{
302 for (int i
= 0; i
< kMaxPaths
; ++i
) {
303 if (paths_
[i
].countPoints() == 0)
305 paint_
.setColor(colors_
[i
]);
306 canvas
->DrawPath(paths_
[i
], paint_
);
312 const TouchLog
& touch_log_
;
313 SkPath paths_
[kMaxPaths
];
314 SkColor colors_
[kMaxPaths
];
318 DISALLOW_COPY_AND_ASSIGN(TouchHudCanvas
);
321 TouchHudDebug::TouchHudDebug(aura::Window
* initial_root
)
322 : TouchObserverHUD(initial_root
),
324 touch_log_(new TouchLog()),
326 label_container_(NULL
) {
327 const gfx::Display
& display
=
328 Shell::GetInstance()->display_manager()->GetDisplayForId(display_id());
330 views::View
* content
= widget()->GetContentsView();
332 canvas_
= new TouchHudCanvas(*touch_log_
);
333 content
->AddChildView(canvas_
);
335 const gfx::Size
& display_size
= display
.size();
336 canvas_
->SetSize(display_size
);
338 label_container_
= new views::View
;
339 label_container_
->SetLayoutManager(new views::BoxLayout(
340 views::BoxLayout::kVertical
, 0, 0, 0));
342 for (int i
= 0; i
< kMaxTouchPoints
; ++i
) {
343 touch_labels_
[i
] = new views::Label
;
344 touch_labels_
[i
]->SetBackgroundColor(SkColorSetARGB(0, 255, 255, 255));
345 touch_labels_
[i
]->SetShadows(gfx::ShadowValues(
346 1, gfx::ShadowValue(gfx::Vector2d(1, 1), 0, SK_ColorWHITE
)));
347 label_container_
->AddChildView(touch_labels_
[i
]);
349 label_container_
->SetX(0);
350 label_container_
->SetY(display_size
.height() / kReducedScale
);
351 label_container_
->SetSize(label_container_
->GetPreferredSize());
352 label_container_
->SetVisible(false);
353 content
->AddChildView(label_container_
);
356 TouchHudDebug::~TouchHudDebug() {
360 scoped_ptr
<base::DictionaryValue
> TouchHudDebug::GetAllAsDictionary() {
361 scoped_ptr
<base::DictionaryValue
> value(new base::DictionaryValue());
362 aura::Window::Windows roots
= Shell::GetInstance()->GetAllRootWindows();
363 for (aura::Window::Windows::iterator iter
= roots
.begin();
364 iter
!= roots
.end(); ++iter
) {
365 RootWindowController
* controller
= GetRootWindowController(*iter
);
366 TouchHudDebug
* hud
= controller
->touch_hud_debug();
368 scoped_ptr
<base::ListValue
> list
= hud
->GetLogAsList();
370 value
->Set(base::Int64ToString(hud
->display_id()), list
.release());
376 void TouchHudDebug::ChangeToNextMode() {
379 SetMode(REDUCED_SCALE
);
390 scoped_ptr
<base::ListValue
> TouchHudDebug::GetLogAsList() const {
391 return touch_log_
->GetAsList();
394 void TouchHudDebug::Clear() {
395 if (widget()->IsVisible()) {
397 for (int i
= 0; i
< kMaxTouchPoints
; ++i
)
398 touch_labels_
[i
]->SetText(base::string16());
399 label_container_
->SetSize(label_container_
->GetPreferredSize());
403 void TouchHudDebug::SetMode(Mode mode
) {
409 label_container_
->SetVisible(false);
410 canvas_
->SetVisible(true);
411 canvas_
->SetScale(1);
412 canvas_
->SchedulePaint();
416 label_container_
->SetVisible(true);
417 canvas_
->SetVisible(true);
418 canvas_
->SetScale(kReducedScale
);
419 canvas_
->SchedulePaint();
428 void TouchHudDebug::UpdateTouchPointLabel(int index
) {
429 int trace_index
= touch_log_
->GetTraceIndex(index
);
430 const TouchTrace
& trace
= touch_log_
->traces()[trace_index
];
431 TouchTrace::const_reverse_iterator point
= trace
.log().rbegin();
432 ui::EventType touch_status
= point
->type
;
433 float touch_radius
= std::max(point
->radius_x
, point
->radius_y
);
434 while (point
!= trace
.log().rend() && point
->type
== ui::ET_TOUCH_CANCELLED
)
436 DCHECK(point
!= trace
.log().rend());
437 gfx::Point touch_position
= point
->location
;
439 std::string string
= base::StringPrintf("%2d: %s %s (%.4f)",
441 GetTouchEventLabel(touch_status
),
442 touch_position
.ToString().c_str(),
444 touch_labels_
[index
]->SetText(base::UTF8ToUTF16(string
));
447 void TouchHudDebug::OnTouchEvent(ui::TouchEvent
* event
) {
448 if (event
->touch_id() >= kMaxTouchPoints
)
451 touch_log_
->AddTouchPoint(*event
);
452 canvas_
->TouchPointAdded(event
->touch_id());
453 UpdateTouchPointLabel(event
->touch_id());
454 label_container_
->SetSize(label_container_
->GetPreferredSize());
457 void TouchHudDebug::OnDisplayMetricsChanged(const gfx::Display
& display
,
459 TouchObserverHUD::OnDisplayMetricsChanged(display
, metrics
);
461 if (display
.id() != display_id() || !(metrics
& DISPLAY_METRIC_BOUNDS
))
463 const gfx::Size
& size
= display
.size();
464 canvas_
->SetSize(size
);
465 label_container_
->SetY(size
.height() / kReducedScale
);
468 void TouchHudDebug::SetHudForRootWindowController(
469 RootWindowController
* controller
) {
470 controller
->set_touch_hud_debug(this);
473 void TouchHudDebug::UnsetHudForRootWindowController(
474 RootWindowController
* controller
) {
475 controller
->set_touch_hud_debug(NULL
);