Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "power_manager/xidle.h" | 5 #include "power_manager/xidle.h" |
| 6 #include <inttypes.h> | 6 #include <inttypes.h> |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 namespace power_manager { | 9 namespace power_manager { |
| 10 | 10 |
| 11 static inline int64 XSyncValueToInt64(XSyncValue value) { | 11 static inline int64 XSyncValueToInt64(XSyncValue value) { |
| 12 return (static_cast<int64>(XSyncValueHigh32(value)) << 32 | | 12 return (static_cast<int64>(XSyncValueHigh32(value)) << 32 | |
| 13 static_cast<int64>(XSyncValueLow32(value))); | 13 static_cast<int64>(XSyncValueLow32(value))); |
| 14 } | 14 } |
| 15 | 15 |
| 16 static inline void XSyncInt64ToValue(XSyncValue* xvalue, int64 value) { | 16 static inline void XSyncInt64ToValue(XSyncValue* xvalue, int64 value) { |
| 17 XSyncIntsToValue(xvalue, value, value >> 32); | 17 XSyncIntsToValue(xvalue, value, value >> 32); |
| 18 } | 18 } |
| 19 | 19 |
| 20 XIdle::XIdle() | 20 XIdle::XIdle() |
| 21 : idle_counter_(0), | 21 : idle_counter_(0), |
| 22 min_timeout_(kint64max) { | 22 min_timeout_(kint64max), |
| 23 display_(NULL) { | |
| 23 } | 24 } |
| 24 | 25 |
| 25 XIdle::~XIdle() { | 26 XIdle::~XIdle() { |
| 26 ClearTimeouts(); | 27 ClearTimeouts(); |
| 27 if (display_) | |
| 28 XCloseDisplay(display_); | |
| 29 } | 28 } |
| 30 | 29 |
| 31 bool XIdle::Init() { | 30 bool XIdle::Init(Display* display) { |
| 32 int major_version, minor_version; | 31 int major_version, minor_version; |
| 33 display_ = XOpenDisplay(NULL); | 32 CHECK(display); |
| 34 if (display_ && | 33 display_ = display; |
| 35 XSyncQueryExtension(display_, &event_base_, &error_base_) && | 34 if (XSyncQueryExtension(display_, &event_base_, &error_base_) && |
| 36 XSyncInitialize(display_, &major_version, &minor_version)) { | 35 XSyncInitialize(display_, &major_version, &minor_version)) { |
| 37 XSyncSystemCounter* counters; | 36 XSyncSystemCounter* counters; |
| 38 int ncounters; | 37 int ncounters; |
| 39 counters = XSyncListSystemCounters(display_, &ncounters); | 38 counters = XSyncListSystemCounters(display_, &ncounters); |
| 40 | 39 |
| 41 if (counters) { | 40 if (counters) { |
| 42 for (int i = 0; i < ncounters; i++) { | 41 for (int i = 0; i < ncounters; i++) { |
| 43 if (counters[i].name && strcmp(counters[i].name, "IDLETIME") == 0) { | 42 if (counters[i].name && strcmp(counters[i].name, "IDLETIME") == 0) { |
| 44 idle_counter_ = counters[i].counter; | 43 idle_counter_ = counters[i].counter; |
| 45 break; | 44 break; |
| 46 } | 45 } |
| 47 } | 46 } |
| 48 XSyncFreeSystemCounterList(counters); | 47 XSyncFreeSystemCounterList(counters); |
| 49 } | 48 } |
| 50 } | 49 } |
| 51 return idle_counter_ != 0; | 50 return idle_counter_ != 0; |
| 52 } | 51 } |
| 53 | 52 |
| 54 bool XIdle::AddIdleTimeout(int64 idle_timeout_ms) { | 53 bool XIdle::AddIdleTimeout(int64 idle_timeout_ms) { |
| 55 DCHECK_NE(idle_counter_, 0); | 54 DCHECK_NE(idle_counter_, 0); |
| 55 DCHECK_GT(idle_timeout_ms, 1); | |
| 56 | 56 |
| 57 if (idle_timeout_ms < min_timeout_) { | 57 if (idle_timeout_ms < min_timeout_) { |
| 58 min_timeout_ = idle_timeout_ms; | 58 min_timeout_ = idle_timeout_ms; |
| 59 XSyncAlarm alarm = CreateIdleAlarm(min_timeout_, XSyncNegativeTransition); | 59 XSyncAlarm alarm = CreateIdleAlarm(min_timeout_ - 1, |
|
Daniel Erat
2010/03/16 21:40:01
it'd be good to add a comment here describing the
| |
| 60 XSyncNegativeTransition); | |
| 60 if (!alarm) | 61 if (!alarm) |
| 61 return false; | 62 return false; |
| 62 if (!alarms_.empty()) { | 63 if (!alarms_.empty()) { |
| 63 XSyncDestroyAlarm(display_, alarms_.front()); | 64 XSyncDestroyAlarm(display_, alarms_.front()); |
| 64 alarms_.pop_front(); | 65 alarms_.pop_front(); |
| 65 } | 66 } |
| 66 alarms_.push_front(alarm); | 67 alarms_.push_front(alarm); |
| 67 } | 68 } |
| 68 | 69 |
| 69 XSyncAlarm alarm = CreateIdleAlarm(idle_timeout_ms, XSyncPositiveTransition); | 70 XSyncAlarm alarm = CreateIdleAlarm(idle_timeout_ms, XSyncPositiveTransition); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 120 XSyncCADelta; | 121 XSyncCADelta; |
| 121 XSyncAlarmAttributes attr; | 122 XSyncAlarmAttributes attr; |
| 122 attr.trigger.counter = idle_counter_; | 123 attr.trigger.counter = idle_counter_; |
| 123 attr.trigger.test_type = test_type; | 124 attr.trigger.test_type = test_type; |
| 124 XSyncInt64ToValue(&attr.trigger.wait_value, idle_timeout_ms); | 125 XSyncInt64ToValue(&attr.trigger.wait_value, idle_timeout_ms); |
| 125 XSyncIntToValue(&attr.delta, 0); | 126 XSyncIntToValue(&attr.delta, 0); |
| 126 return XSyncCreateAlarm(display_, mask, &attr); | 127 return XSyncCreateAlarm(display_, mask, &attr); |
| 127 } | 128 } |
| 128 | 129 |
| 129 } // namespace power_manager | 130 } // namespace power_manager |
| OLD | NEW |