| 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_POINTER_POSITION_WATCHER_H_ | 5 #ifndef WINDOW_MANAGER_POINTER_POSITION_WATCHER_H_ |
| 6 #define WINDOW_MANAGER_POINTER_POSITION_WATCHER_H_ | 6 #define WINDOW_MANAGER_POINTER_POSITION_WATCHER_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 class XConnection; | 15 class XConnection; |
| 15 | 16 |
| 16 // This class periodically queries the mouse pointer's position and invokes | 17 // This class periodically queries the mouse pointer's position and invokes |
| 17 // a callback once the pointer has moved into or out of a target rectangle. | 18 // a callback once the pointer has moved into or out of a target rectangle. |
| 18 // | 19 // |
| 19 // This is primarily useful for: | 20 // This is primarily useful for: |
| 20 // a) avoiding race conditions in cases where we want to open a new window | 21 // a) avoiding race conditions in cases where we want to open a new window |
| 21 // under the pointer and then do something when the pointer leaves the | 22 // under the pointer and then do something when the pointer leaves the |
| 22 // window -- it's possible that the pointer will have already been moved | 23 // window -- it's possible that the pointer will have already been moved |
| 23 // away by the time that window is created | 24 // away by the time that window is created |
| 24 // b) getting notified when the pointer enters or leaves a region without | 25 // b) getting notified when the pointer enters or leaves a region without |
| 25 // creating a window that will steal events from windows underneath it | 26 // creating a window that will steal events from windows underneath it |
| 26 // | 27 // |
| 27 // With that being said, repeatedly waking up to poll the X server over | 28 // With that being said, repeatedly waking up to poll the X server over |
| 28 // long periods of time is a bad idea from a power consumption perspective, | 29 // long periods of time is a bad idea from a power consumption perspective, |
| 29 // so this should only be used in cases where the user is likely to | 30 // so this should only be used in cases where the user is likely to |
| 30 // enter/leave the target region soon. | 31 // enter/leave the target region soon. |
| 31 class PointerPositionWatcher { | 32 class PointerPositionWatcher { |
| 32 public: | 33 public: |
| 33 // The constructor takes ownership of |cb|. | 34 // The constructor takes ownership of |cb|. |
| 34 PointerPositionWatcher( | 35 PointerPositionWatcher( |
| 35 EventLoop* event_loop, | 36 EventLoop* event_loop, |
| 36 XConnection* xconn, | 37 XConnection* xconn, |
| 37 Closure* cb, | 38 Closure* cb, |
| 38 bool watch_for_entering_target, // as opposed to leaving it | 39 bool watch_for_entering_target, // as opposed to leaving it |
| 39 int target_x, int target_y, int target_width, int target_height); | 40 const Rect& target_bounds); |
| 40 ~PointerPositionWatcher(); | 41 ~PointerPositionWatcher(); |
| 41 | 42 |
| 42 // Useful for testing. | 43 // Useful for testing. |
| 43 int timeout_id() const { return timeout_id_; } | 44 int timeout_id() const { return timeout_id_; } |
| 44 | 45 |
| 45 // Invoke RunCallbackIfConditionIsSatisfied() and remove the current | 46 // Invoke RunCallbackIfConditionIsSatisfied() and remove the current |
| 46 // timeout if needed. | 47 // timeout if needed. |
| 47 void TriggerTimeout(); | 48 void TriggerTimeout(); |
| 48 | 49 |
| 49 private: | 50 private: |
| 50 // If |timeout_id_| is set, clear it and remove the timeout. | 51 // If |timeout_id_| is set, clear it and remove the timeout. |
| 51 void CancelTimeoutIfActive(); | 52 void CancelTimeoutIfActive(); |
| 52 | 53 |
| 53 // Check the pointer's position, running the callback and removing the | 54 // Check the pointer's position, running the callback and removing the |
| 54 // timeout if the condition has been satisfied. | 55 // timeout if the condition has been satisfied. |
| 55 void HandleTimeout(); | 56 void HandleTimeout(); |
| 56 | 57 |
| 57 EventLoop* event_loop_; // not owned | 58 EventLoop* event_loop_; // not owned |
| 58 XConnection* xconn_; // not owned | 59 XConnection* xconn_; // not owned |
| 59 | 60 |
| 60 // Callback that gets invoked when the pointer enters/exits the target | 61 // Callback that gets invoked when the pointer enters/exits the target |
| 61 // rectangle. | 62 // rectangle. |
| 62 scoped_ptr<Closure> cb_; | 63 scoped_ptr<Closure> cb_; |
| 63 | 64 |
| 64 // Should we watch for the pointer entering the target rectangle, as | 65 // Should we watch for the pointer entering the target rectangle, as |
| 65 // opposed to leaving it? | 66 // opposed to leaving it? |
| 66 bool watch_for_entering_target_; | 67 bool watch_for_entering_target_; |
| 67 | 68 |
| 68 // Target rectangle. | 69 // Target rectangle. |
| 69 int target_x_; | 70 Rect target_bounds_; |
| 70 int target_y_; | |
| 71 int target_width_; | |
| 72 int target_height_; | |
| 73 | 71 |
| 74 // Timeout ID, or -1 if the timeout isn't active. | 72 // Timeout ID, or -1 if the timeout isn't active. |
| 75 int timeout_id_; | 73 int timeout_id_; |
| 76 }; | 74 }; |
| 77 | 75 |
| 78 } // namespace window_manager | 76 } // namespace window_manager |
| 79 | 77 |
| 80 #endif // WINDOW_MANAGER_MOTION_POINTER_POSITION_WATCHER_H_ | 78 #endif // WINDOW_MANAGER_MOTION_POINTER_POSITION_WATCHER_H_ |
| OLD | NEW |