| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "window_manager/motion_event_coalescer.h" | 5 #include "window_manager/motion_event_coalescer.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "window_manager/event_loop.h" | 8 #include "window_manager/event_loop.h" |
| 9 | 9 |
| 10 namespace window_manager { | 10 namespace window_manager { |
| 11 | 11 |
| 12 MotionEventCoalescer::MotionEventCoalescer(EventLoop* event_loop, | 12 MotionEventCoalescer::MotionEventCoalescer(EventLoop* event_loop, |
| 13 Closure* cb, | 13 Closure* cb, |
| 14 int timeout_ms) | 14 int timeout_ms) |
| 15 : event_loop_(event_loop), | 15 : event_loop_(event_loop), |
| 16 timeout_id_(-1), | 16 timeout_id_(-1), |
| 17 timeout_ms_(timeout_ms), | 17 timeout_ms_(timeout_ms), |
| 18 have_queued_position_(false), | 18 have_queued_position_(false), |
| 19 x_(-1), | 19 position_(-1, -1), |
| 20 y_(-1), | |
| 21 cb_(cb), | 20 cb_(cb), |
| 22 synchronous_(false) { | 21 synchronous_(false) { |
| 23 CHECK(cb); | 22 CHECK(cb); |
| 24 CHECK(timeout_ms > 0); | 23 CHECK(timeout_ms > 0); |
| 25 } | 24 } |
| 26 | 25 |
| 27 MotionEventCoalescer::~MotionEventCoalescer() { | 26 MotionEventCoalescer::~MotionEventCoalescer() { |
| 28 if (IsRunning()) | 27 if (IsRunning()) |
| 29 StopInternal(false); | 28 StopInternal(false); |
| 30 } | 29 } |
| 31 | 30 |
| 32 void MotionEventCoalescer::Start() { | 31 void MotionEventCoalescer::Start() { |
| 33 if (timeout_id_ >= 0) { | 32 if (timeout_id_ >= 0) { |
| 34 LOG(WARNING) << "Ignoring request to start coalescer while timer " | 33 LOG(WARNING) << "Ignoring request to start coalescer while timer " |
| 35 << "is already running"; | 34 << "is already running"; |
| 36 return; | 35 return; |
| 37 } | 36 } |
| 38 if (!synchronous_) { | 37 if (!synchronous_) { |
| 39 timeout_id_ = event_loop_->AddTimeout( | 38 timeout_id_ = event_loop_->AddTimeout( |
| 40 NewPermanentCallback(this, &MotionEventCoalescer::HandleTimeout), | 39 NewPermanentCallback(this, &MotionEventCoalescer::HandleTimeout), |
| 41 0, timeout_ms_); | 40 0, timeout_ms_); |
| 42 } | 41 } |
| 43 have_queued_position_ = false; | 42 have_queued_position_ = false; |
| 44 x_ = -1; | 43 position_.reset(-1, -1); |
| 45 y_ = -1; | |
| 46 } | 44 } |
| 47 | 45 |
| 48 void MotionEventCoalescer::Stop() { | 46 void MotionEventCoalescer::Stop() { |
| 49 if (!synchronous_) | 47 if (!synchronous_) |
| 50 StopInternal(true); | 48 StopInternal(true); |
| 51 } | 49 } |
| 52 | 50 |
| 53 void MotionEventCoalescer::StorePosition(int x, int y) { | 51 void MotionEventCoalescer::StorePosition(const Point& pos) { |
| 54 if (x == x_ && y == y_) | 52 if (pos == position_) |
| 55 return; | 53 return; |
| 56 x_ = x; | 54 position_ = pos; |
| 57 y_ = y; | |
| 58 have_queued_position_ = true; | 55 have_queued_position_ = true; |
| 59 if (synchronous_) | 56 if (synchronous_) |
| 60 HandleTimeout(); | 57 HandleTimeout(); |
| 61 } | 58 } |
| 62 | 59 |
| 63 void MotionEventCoalescer::StopInternal(bool maybe_run_callback) { | 60 void MotionEventCoalescer::StopInternal(bool maybe_run_callback) { |
| 64 if (timeout_id_ < 0) { | 61 if (timeout_id_ < 0) { |
| 65 LOG(WARNING) << "Ignoring request to stop coalescer while timer " | 62 LOG(WARNING) << "Ignoring request to stop coalescer while timer " |
| 66 << "isn't running"; | 63 << "isn't running"; |
| 67 return; | 64 return; |
| 68 } | 65 } |
| 69 event_loop_->RemoveTimeout(timeout_id_); | 66 event_loop_->RemoveTimeout(timeout_id_); |
| 70 timeout_id_ = -1; | 67 timeout_id_ = -1; |
| 71 | 68 |
| 72 // Invoke the handler one last time to catch any events that came in | 69 // Invoke the handler one last time to catch any events that came in |
| 73 // after the final run. | 70 // after the final run. |
| 74 if (maybe_run_callback) | 71 if (maybe_run_callback) |
| 75 HandleTimeout(); | 72 HandleTimeout(); |
| 76 } | 73 } |
| 77 | 74 |
| 78 void MotionEventCoalescer::HandleTimeout() { | 75 void MotionEventCoalescer::HandleTimeout() { |
| 79 if (have_queued_position_) { | 76 if (have_queued_position_) { |
| 80 cb_->Run(); | 77 cb_->Run(); |
| 81 have_queued_position_ = false; | 78 have_queued_position_ = false; |
| 82 } | 79 } |
| 83 } | 80 } |
| 84 | 81 |
| 85 } // namespace window_manager | 82 } // namespace window_manager |
| OLD | NEW |