| 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 #ifndef WINDOW_MANAGER_MOTION_EVENT_COALESCER_H_ | 5 #ifndef WINDOW_MANAGER_MOTION_EVENT_COALESCER_H_ |
| 6 #define WINDOW_MANAGER_MOTION_EVENT_COALESCER_H_ | 6 #define WINDOW_MANAGER_MOTION_EVENT_COALESCER_H_ |
| 7 | 7 |
| 8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
| 9 #include "window_manager/callback.h" | 9 #include "window_manager/callback.h" |
| 10 #include "window_manager/geometry.h" |
| 10 | 11 |
| 11 namespace window_manager { | 12 namespace window_manager { |
| 12 | 13 |
| 13 class EventLoop; | 14 class EventLoop; |
| 14 | 15 |
| 15 // Rate-limits how quickly motion events are processed by saving them as | 16 // Rate-limits how quickly motion events are processed by saving them as |
| 16 // they're generated and then periodically invoking a callback (but only if | 17 // they're generated and then periodically invoking a callback (but only if |
| 17 // new motion events have been received). | 18 // new motion events have been received). |
| 18 class MotionEventCoalescer { | 19 class MotionEventCoalescer { |
| 19 public: | 20 public: |
| 20 // The constructor takes ownership of |cb|. | 21 // The constructor takes ownership of |cb|. |
| 21 MotionEventCoalescer(EventLoop* event_loop, Closure* cb, int timeout_ms); | 22 MotionEventCoalescer(EventLoop* event_loop, Closure* cb, int timeout_ms); |
| 22 ~MotionEventCoalescer(); | 23 ~MotionEventCoalescer(); |
| 23 | 24 |
| 24 int x() const { return x_; } | 25 const Point& position() const { return position_; } |
| 25 int y() const { return y_; } | 26 int x() const { return position_.x; } |
| 27 int y() const { return position_.y; } |
| 26 | 28 |
| 27 void set_synchronous(bool synchronous) { | 29 void set_synchronous(bool synchronous) { |
| 28 synchronous_ = synchronous; | 30 synchronous_ = synchronous; |
| 29 } | 31 } |
| 30 | 32 |
| 31 // Start or stop the timer. | 33 // Start or stop the timer. |
| 32 void Start(); | 34 void Start(); |
| 33 void Stop(); | 35 void Stop(); |
| 34 | 36 |
| 35 // Is the timer currently running? | 37 // Is the timer currently running? |
| 36 bool IsRunning() { | 38 bool IsRunning() { |
| 37 return timeout_id_ >= 0; | 39 return timeout_id_ >= 0; |
| 38 } | 40 } |
| 39 | 41 |
| 40 // Store a position. This should be called in response to each motion | 42 // Store a position. This should be called in response to each motion |
| 41 // event. | 43 // event. |
| 42 void StorePosition(int x, int y); | 44 void StorePosition(const Point& pos); |
| 43 | 45 |
| 44 private: | 46 private: |
| 45 // Invoked by Stop() and by the destructor to remove the timer. If | 47 // Invoked by Stop() and by the destructor to remove the timer. If |
| 46 // |maybe_run_callback| is true, the callback will be invoked one last | 48 // |maybe_run_callback| is true, the callback will be invoked one last |
| 47 // time if a new position has been received but not yet handled (the | 49 // time if a new position has been received but not yet handled (the |
| 48 // destructor passes false here; running the callback may be dangerous if | 50 // destructor passes false here; running the callback may be dangerous if |
| 49 // parts of the owning class have already been destroyed). | 51 // parts of the owning class have already been destroyed). |
| 50 void StopInternal(bool maybe_run_callback); | 52 void StopInternal(bool maybe_run_callback); |
| 51 | 53 |
| 52 // Handle the timer firing. Runs the callback if we have a queued | 54 // Handle the timer firing. Runs the callback if we have a queued |
| 53 // position. | 55 // position. |
| 54 void HandleTimeout(); | 56 void HandleTimeout(); |
| 55 | 57 |
| 56 EventLoop* event_loop_; // not owned | 58 EventLoop* event_loop_; // not owned |
| 57 | 59 |
| 58 // Timeout ID, or -1 if the timeout isn't active. | 60 // Timeout ID, or -1 if the timeout isn't active. |
| 59 int timeout_id_; | 61 int timeout_id_; |
| 60 | 62 |
| 61 // Frequency for invoking the callback, in milliseconds. | 63 // Frequency for invoking the callback, in milliseconds. |
| 62 int timeout_ms_; | 64 int timeout_ms_; |
| 63 | 65 |
| 64 // Have we received a position since the last time the callback was | 66 // Have we received a position since the last time the callback was |
| 65 // invoked? | 67 // invoked? |
| 66 bool have_queued_position_; | 68 bool have_queued_position_; |
| 67 | 69 |
| 68 // The most-recently-received position. | 70 // The most-recently-received position. |
| 69 int x_; | 71 Point position_; |
| 70 int y_; | |
| 71 | 72 |
| 72 // Callback that gets periodically invoked when there's a new position to | 73 // Callback that gets periodically invoked when there's a new position to |
| 73 // handle. | 74 // handle. |
| 74 // TODO: When we're using a callback library that supports parameters, we | 75 // TODO: When we're using a callback library that supports parameters, we |
| 75 // should just pass the position directly to the callback. | 76 // should just pass the position directly to the callback. |
| 76 scoped_ptr<Closure> cb_; | 77 scoped_ptr<Closure> cb_; |
| 77 | 78 |
| 78 // Should we just invoke the callback in response to each StorePosition() | 79 // Should we just invoke the callback in response to each StorePosition() |
| 79 // call instead of using a timer? Useful for tests. | 80 // call instead of using a timer? Useful for tests. |
| 80 bool synchronous_; | 81 bool synchronous_; |
| 81 }; | 82 }; |
| 82 | 83 |
| 83 } // namespace window_manager | 84 } // namespace window_manager |
| 84 | 85 |
| 85 #endif // WINDOW_MANAGER_MOTION_EVENT_COALESCER_H_ | 86 #endif // WINDOW_MANAGER_MOTION_EVENT_COALESCER_H_ |
| OLD | NEW |