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
.radius_x()),
97 radius_y(touch
.radius_y()),
98 pressure(touch
.force()),
99 tracking_id(GetTrackingId(touch
)),
100 source_device(touch
.source_device_id()) {
103 // Populates a dictionary value with all the information about the touch
105 scoped_ptr
<base::DictionaryValue
> GetAsDictionary() const {
106 scoped_ptr
<base::DictionaryValue
> value(new base::DictionaryValue());
108 value
->SetInteger("id", id
);
109 value
->SetString("type", std::string(GetTouchEventLabel(type
)));
110 value
->SetString("location", location
.ToString());
111 value
->SetDouble("timestamp", timestamp
);
112 value
->SetDouble("radius_x", radius_x
);
113 value
->SetDouble("radius_y", radius_y
);
114 value
->SetDouble("pressure", pressure
);
115 value
->SetInteger("tracking_id", tracking_id
);
116 value
->SetInteger("source_device", source_device
);
132 // A TouchTrace keeps track of all the touch events of a single touch point
133 // (starting from a touch-press and ending at a touch-release or touch-cancel).
136 typedef std::vector
<TouchPointLog
>::iterator iterator
;
137 typedef std::vector
<TouchPointLog
>::const_iterator const_iterator
;
138 typedef std::vector
<TouchPointLog
>::reverse_iterator reverse_iterator
;
139 typedef std::vector
<TouchPointLog
>::const_reverse_iterator
140 const_reverse_iterator
;
145 void AddTouchPoint(const ui::TouchEvent
& touch
) {
146 log_
.push_back(TouchPointLog(touch
));
149 const std::vector
<TouchPointLog
>& log() const { return log_
; }
151 bool active() const {
152 return !log_
.empty() && log_
.back().type
!= ui::ET_TOUCH_RELEASED
&&
153 log_
.back().type
!= ui::ET_TOUCH_CANCELLED
;
156 // Returns a list containing data from all events for the touch point.
157 scoped_ptr
<base::ListValue
> GetAsList() const {
158 scoped_ptr
<base::ListValue
> list(new base::ListValue());
159 for (const_iterator i
= log_
.begin(); i
!= log_
.end(); ++i
)
160 list
->Append((*i
).GetAsDictionary().release());
169 std::vector
<TouchPointLog
> log_
;
171 DISALLOW_COPY_AND_ASSIGN(TouchTrace
);
174 // A TouchLog keeps track of all touch events of all touch points.
177 TouchLog() : next_trace_index_(0) {
180 void AddTouchPoint(const ui::TouchEvent
& touch
) {
181 if (touch
.type() == ui::ET_TOUCH_PRESSED
)
187 next_trace_index_
= 0;
188 for (int i
= 0; i
< kMaxPaths
; ++i
)
192 scoped_ptr
<base::ListValue
> GetAsList() const {
193 scoped_ptr
<base::ListValue
> list(new base::ListValue());
194 for (int i
= 0; i
< kMaxPaths
; ++i
) {
195 if (!traces_
[i
].log().empty())
196 list
->Append(traces_
[i
].GetAsList().release());
201 int GetTraceIndex(int touch_id
) const {
202 return touch_id_to_trace_index_
.at(touch_id
);
205 const TouchTrace
* traces() const {
210 void StartTrace(const ui::TouchEvent
& touch
) {
211 // Find the first inactive spot; otherwise, overwrite the one
212 // |next_trace_index_| is pointing to.
213 int old_trace_index
= next_trace_index_
;
215 if (!traces_
[next_trace_index_
].active())
217 next_trace_index_
= (next_trace_index_
+ 1) % kMaxPaths
;
218 } while (next_trace_index_
!= old_trace_index
);
219 int touch_id
= touch
.touch_id();
220 traces_
[next_trace_index_
].Reset();
221 touch_id_to_trace_index_
[touch_id
] = next_trace_index_
;
222 next_trace_index_
= (next_trace_index_
+ 1) % kMaxPaths
;
225 void AddToTrace(const ui::TouchEvent
& touch
) {
226 int touch_id
= touch
.touch_id();
227 int trace_index
= touch_id_to_trace_index_
[touch_id
];
228 traces_
[trace_index
].AddTouchPoint(touch
);
231 TouchTrace traces_
[kMaxPaths
];
232 int next_trace_index_
;
234 std::map
<int, int> touch_id_to_trace_index_
;
236 DISALLOW_COPY_AND_ASSIGN(TouchLog
);
239 // TouchHudCanvas draws touch traces in |FULLSCREEN| and |REDUCED_SCALE| modes.
240 class TouchHudCanvas
: public views::View
{
242 explicit TouchHudCanvas(const TouchLog
& touch_log
)
243 : touch_log_(touch_log
),
245 SetPaintToLayer(true);
246 SetFillsBoundsOpaquely(false);
248 paint_
.setStyle(SkPaint::kFill_Style
);
251 ~TouchHudCanvas() override
{}
253 void SetScale(int scale
) {
257 gfx::Transform transform
;
258 transform
.Scale(1. / scale_
, 1. / scale_
);
259 layer()->SetTransform(transform
);
262 int scale() const { return scale_
; }
264 void TouchPointAdded(int touch_id
) {
265 int trace_index
= touch_log_
.GetTraceIndex(touch_id
);
266 const TouchTrace
& trace
= touch_log_
.traces()[trace_index
];
267 const TouchPointLog
& point
= trace
.log().back();
268 if (point
.type
== ui::ET_TOUCH_PRESSED
)
269 StartedTrace(trace_index
);
270 if (point
.type
!= ui::ET_TOUCH_CANCELLED
)
271 AddedPointToTrace(trace_index
);
275 for (int i
= 0; i
< kMaxPaths
; ++i
)
282 void StartedTrace(int trace_index
) {
283 paths_
[trace_index
].reset();
284 colors_
[trace_index
] = SkColorSetA(kColors
[trace_index
], kAlpha
);
287 void AddedPointToTrace(int trace_index
) {
288 const TouchTrace
& trace
= touch_log_
.traces()[trace_index
];
289 const TouchPointLog
& point
= trace
.log().back();
290 const gfx::Point
& location
= point
.location
;
291 SkScalar x
= SkIntToScalar(location
.x());
292 SkScalar y
= SkIntToScalar(location
.y());
294 if (!paths_
[trace_index
].getLastPt(&last
) || x
!= last
.x() ||
296 paths_
[trace_index
].addCircle(x
, y
, SkIntToScalar(kPointRadius
));
301 // Overridden from views::View.
302 void OnPaint(gfx::Canvas
* canvas
) override
{
303 for (int i
= 0; i
< kMaxPaths
; ++i
) {
304 if (paths_
[i
].countPoints() == 0)
306 paint_
.setColor(colors_
[i
]);
307 canvas
->DrawPath(paths_
[i
], paint_
);
313 const TouchLog
& touch_log_
;
314 SkPath paths_
[kMaxPaths
];
315 SkColor colors_
[kMaxPaths
];
319 DISALLOW_COPY_AND_ASSIGN(TouchHudCanvas
);
322 TouchHudDebug::TouchHudDebug(aura::Window
* initial_root
)
323 : TouchObserverHUD(initial_root
),
325 touch_log_(new TouchLog()),
327 label_container_(NULL
) {
328 const gfx::Display
& display
=
329 Shell::GetInstance()->display_manager()->GetDisplayForId(display_id());
331 views::View
* content
= widget()->GetContentsView();
333 canvas_
= new TouchHudCanvas(*touch_log_
);
334 content
->AddChildView(canvas_
);
336 const gfx::Size
& display_size
= display
.size();
337 canvas_
->SetSize(display_size
);
339 label_container_
= new views::View
;
340 label_container_
->SetLayoutManager(new views::BoxLayout(
341 views::BoxLayout::kVertical
, 0, 0, 0));
343 for (int i
= 0; i
< kMaxTouchPoints
; ++i
) {
344 touch_labels_
[i
] = new views::Label
;
345 touch_labels_
[i
]->SetBackgroundColor(SkColorSetARGB(0, 255, 255, 255));
346 touch_labels_
[i
]->SetShadows(gfx::ShadowValues(
347 1, gfx::ShadowValue(gfx::Vector2d(1, 1), 0, SK_ColorWHITE
)));
348 label_container_
->AddChildView(touch_labels_
[i
]);
350 label_container_
->SetX(0);
351 label_container_
->SetY(display_size
.height() / kReducedScale
);
352 label_container_
->SetSize(label_container_
->GetPreferredSize());
353 label_container_
->SetVisible(false);
354 content
->AddChildView(label_container_
);
357 TouchHudDebug::~TouchHudDebug() {
361 scoped_ptr
<base::DictionaryValue
> TouchHudDebug::GetAllAsDictionary() {
362 scoped_ptr
<base::DictionaryValue
> value(new base::DictionaryValue());
363 aura::Window::Windows roots
= Shell::GetInstance()->GetAllRootWindows();
364 for (aura::Window::Windows::iterator iter
= roots
.begin();
365 iter
!= roots
.end(); ++iter
) {
366 RootWindowController
* controller
= GetRootWindowController(*iter
);
367 TouchHudDebug
* hud
= controller
->touch_hud_debug();
369 scoped_ptr
<base::ListValue
> list
= hud
->GetLogAsList();
371 value
->Set(base::Int64ToString(hud
->display_id()), list
.release());
377 void TouchHudDebug::ChangeToNextMode() {
380 SetMode(REDUCED_SCALE
);
391 scoped_ptr
<base::ListValue
> TouchHudDebug::GetLogAsList() const {
392 return touch_log_
->GetAsList();
395 void TouchHudDebug::Clear() {
396 if (widget()->IsVisible()) {
398 for (int i
= 0; i
< kMaxTouchPoints
; ++i
)
399 touch_labels_
[i
]->SetText(base::string16());
400 label_container_
->SetSize(label_container_
->GetPreferredSize());
404 void TouchHudDebug::SetMode(Mode mode
) {
410 label_container_
->SetVisible(false);
411 canvas_
->SetVisible(true);
412 canvas_
->SetScale(1);
413 canvas_
->SchedulePaint();
417 label_container_
->SetVisible(true);
418 canvas_
->SetVisible(true);
419 canvas_
->SetScale(kReducedScale
);
420 canvas_
->SchedulePaint();
429 void TouchHudDebug::UpdateTouchPointLabel(int index
) {
430 int trace_index
= touch_log_
->GetTraceIndex(index
);
431 const TouchTrace
& trace
= touch_log_
->traces()[trace_index
];
432 TouchTrace::const_reverse_iterator point
= trace
.log().rbegin();
433 ui::EventType touch_status
= point
->type
;
434 float touch_radius
= std::max(point
->radius_x
, point
->radius_y
);
435 while (point
!= trace
.log().rend() && point
->type
== ui::ET_TOUCH_CANCELLED
)
437 DCHECK(point
!= trace
.log().rend());
438 gfx::Point touch_position
= point
->location
;
440 std::string string
= base::StringPrintf("%2d: %s %s (%.4f)",
442 GetTouchEventLabel(touch_status
),
443 touch_position
.ToString().c_str(),
445 touch_labels_
[index
]->SetText(base::UTF8ToUTF16(string
));
448 void TouchHudDebug::OnTouchEvent(ui::TouchEvent
* event
) {
449 if (event
->touch_id() >= kMaxTouchPoints
)
452 touch_log_
->AddTouchPoint(*event
);
453 canvas_
->TouchPointAdded(event
->touch_id());
454 UpdateTouchPointLabel(event
->touch_id());
455 label_container_
->SetSize(label_container_
->GetPreferredSize());
458 void TouchHudDebug::OnDisplayMetricsChanged(const gfx::Display
& display
,
460 TouchObserverHUD::OnDisplayMetricsChanged(display
, metrics
);
462 if (display
.id() != display_id() || !(metrics
& DISPLAY_METRIC_BOUNDS
))
464 const gfx::Size
& size
= display
.size();
465 canvas_
->SetSize(size
);
466 label_container_
->SetY(size
.height() / kReducedScale
);
469 void TouchHudDebug::SetHudForRootWindowController(
470 RootWindowController
* controller
) {
471 controller
->set_touch_hud_debug(this);
474 void TouchHudDebug::UnsetHudForRootWindowController(
475 RootWindowController
* controller
) {
476 controller
->set_touch_hud_debug(NULL
);