Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 | |
| 5 #include "remoting/client/plugin/touch_input_scaler.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "remoting/proto/event.pb.h" | |
| 9 | |
| 10 namespace remoting { | |
| 11 | |
| 12 using protocol::TouchEvent; | |
| 13 using protocol::TouchEventPoint; | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // |value| is the number to be scaled. |output_max| is the output desktop's max | |
| 18 // height or width. |input_max| is the input desktop's max height or width. | |
| 19 float Scale(float value, int output_max, int input_max) { | |
| 20 DCHECK_GT(output_max, 0); | |
| 21 DCHECK_GT(input_max, 0); | |
| 22 value *= output_max; | |
| 23 value /= input_max; | |
| 24 return value; | |
| 25 } | |
| 26 | |
| 27 // Same as Scale() but |value| will be scaled and clamped using |output_max| and | |
| 28 // |input_max|. | |
| 29 float ScaleAndClamp(float value, int output_max, int input_max) { | |
| 30 value = Scale(value, output_max, input_max); | |
| 31 return std::max(0.0f, std::min(static_cast<float>(output_max), value)); | |
| 32 } | |
| 33 | |
| 34 } // namespace | |
| 35 | |
| 36 TouchInputScaler::TouchInputScaler(InputStub* input_stub) | |
| 37 : InputFilter(input_stub) {} | |
| 38 | |
| 39 TouchInputScaler::~TouchInputScaler() {} | |
| 40 | |
| 41 void TouchInputScaler::InjectTouchEvent(const TouchEvent& event) { | |
| 42 if (input_size_.is_empty() || output_size_.is_empty()) | |
| 43 return; | |
| 44 | |
| 45 // We scale based on the maximum input & output coordinates, rather than the | |
| 46 // input and output sizes, so that it's possible to reach the edge of the | |
| 47 // output when up-scaling. We also take care to round up or down correctly, | |
| 48 // which is important when down-scaling. | |
| 49 TouchEvent out_event(event); | |
| 50 for (int i = 0; i < out_event.touch_points().size(); ++i) { | |
| 51 TouchEventPoint* point = out_event.mutable_touch_points(i); | |
| 52 if (point->has_x() || point->has_y()) { | |
| 53 DCHECK(point->has_x() && point->has_y()); | |
| 54 point->set_x( | |
| 55 ScaleAndClamp(point->x(), output_size_.width(), input_size_.width())); | |
| 56 point->set_y(ScaleAndClamp(point->y(), output_size_.height(), | |
| 57 input_size_.height())); | |
| 58 } | |
| 59 | |
| 60 // Also scale the touch radii (size). When the output desktop and the input | |
| 61 // desktop sizes do not match, the touch size should be scaled so that it | |
| 62 // does not look too big or too small on the output desktop, compared to the | |
|
Wez
2015/02/13 16:51:46
nit: The normal impact of this will not be on the
Rintaro Kuroiwa
2015/02/17 19:52:11
Done.
| |
| 63 // input size (e.g. touch area of a finger). | |
| 64 // TODO(rkuroiwa): Find out whether clamping should be done here or by the | |
| 65 // host. Windows' touch injection fails when part of a touch point falls | |
| 66 // outside the screen, but other platforms might handle it differently. | |
| 67 // Also reduce the radii if the ellipse completely encloses the desktop. | |
|
Wez
2015/02/13 16:51:46
Based on the Windows case, I'd suggest choosing be
Rintaro Kuroiwa
2015/02/17 19:52:11
I changed the TODO so that we can iterate on this.
Wez
2015/02/23 23:24:07
Note that we usually file bugs for improvements of
Rintaro Kuroiwa
2015/02/24 23:43:10
Done. Added a link to this todo.
| |
| 68 if (point->has_radius_x() || point->has_radius_y()) { | |
| 69 DCHECK(point->has_radius_x() && point->has_radius_y()); | |
| 70 point->set_radius_x( | |
| 71 Scale(point->radius_x(), output_size_.width(), input_size_.width())); | |
| 72 point->set_radius_y(Scale(point->radius_y(), output_size_.height(), | |
| 73 input_size_.height())); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 InputFilter::InjectTouchEvent(out_event); | |
| 78 } | |
| 79 | |
| 80 } // namespace remoting | |
| OLD | NEW |