| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2011 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/backlight.h" | 5 #include "power_manager/backlight.h" |
| 6 | 6 |
| 7 #include <dirent.h> | 7 #include <dirent.h> |
| 8 #include <glib.h> | 8 #include <glib.h> |
| 9 #include <inttypes.h> | 9 #include <inttypes.h> |
| 10 #include <sys/types.h> | 10 #include <sys/types.h> |
| 11 #include <unistd.h> | 11 #include <unistd.h> |
| 12 | 12 |
| 13 #include <string> | 13 #include <string> |
| 14 | 14 |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/file_util.h" | 16 #include "base/file_util.h" |
| 17 #include "base/string_number_conversions.h" | 17 #include "base/string_number_conversions.h" |
| 18 #include "base/string_util.h" | 18 #include "base/string_util.h" |
| 19 | 19 |
| 20 namespace power_manager { | 20 namespace power_manager { |
| 21 | 21 |
| 22 // Gradually change backlight level to new brightness by breaking up the | 22 // Gradually change backlight level to new brightness by breaking up the |
| 23 // transition into N steps, where N = kBacklightNumSteps. | 23 // transition into N steps, where N = kBacklightNumSteps. |
| 24 const int kBacklightNumSteps = 8; | 24 const int kBacklightNumSteps = 8; |
| 25 // Time between backlight adjustment steps, in milliseconds. | 25 // Time between backlight adjustment steps, in milliseconds. |
| 26 const int kBacklightStepTimeMs = 30; | 26 const int kBacklightStepTimeMs = 30; |
| 27 | 27 |
| 28 Backlight::Backlight() |
| 29 : turn_screen_off_data_(NULL), |
| 30 turn_screen_off_func_(NULL) {} |
| 31 |
| 28 bool Backlight::Init() { | 32 bool Backlight::Init() { |
| 29 FilePath base_path("/sys/class/backlight"); | 33 FilePath base_path("/sys/class/backlight"); |
| 30 DIR* dir = opendir(base_path.value().c_str()); | 34 DIR* dir = opendir(base_path.value().c_str()); |
| 31 | 35 |
| 32 #if !defined(OS_LINUX) && !defined(OS_MACOSX) && !defined(OS_FREEBSD) | 36 #if !defined(OS_LINUX) && !defined(OS_MACOSX) && !defined(OS_FREEBSD) |
| 33 #error Port warning: depending on the definition of struct dirent, \ | 37 #error Port warning: depending on the definition of struct dirent, \ |
| 34 additional space for pathname may be needed | 38 additional space for pathname may be needed |
| 35 #endif | 39 #endif |
| 36 | 40 |
| 37 if (dir) { | 41 if (dir) { |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 int64 step_level = current_level + diff * (i + 1) / kBacklightNumSteps; | 148 int64 step_level = current_level + diff * (i + 1) / kBacklightNumSteps; |
| 145 if (step_level == previous_level) | 149 if (step_level == previous_level) |
| 146 continue; | 150 continue; |
| 147 g_timeout_add(i * kBacklightStepTimeMs, SetBrightnessHardThunk, | 151 g_timeout_add(i * kBacklightStepTimeMs, SetBrightnessHardThunk, |
| 148 CreateSetBrightnessHardArgs(this, step_level, target_level)); | 152 CreateSetBrightnessHardArgs(this, step_level, target_level)); |
| 149 previous_level = step_level; | 153 previous_level = step_level; |
| 150 } | 154 } |
| 151 return true; | 155 return true; |
| 152 } | 156 } |
| 153 | 157 |
| 158 void Backlight::SetScreenOffFunc(SIGNAL_CALLBACK_PTR(void, func), void *data) { |
| 159 turn_screen_off_func_ = func; |
| 160 turn_screen_off_data_ = data; |
| 161 } |
| 162 |
| 154 bool Backlight::GetTransitionParams(int* num_steps, int* step_time_ms) | 163 bool Backlight::GetTransitionParams(int* num_steps, int* step_time_ms) |
| 155 { | 164 { |
| 156 if (num_steps == NULL) | 165 if (num_steps == NULL) |
| 157 return false; | 166 return false; |
| 158 if (step_time_ms == NULL) | 167 if (step_time_ms == NULL) |
| 159 return false; | 168 return false; |
| 160 *num_steps = kBacklightNumSteps; | 169 *num_steps = kBacklightNumSteps; |
| 161 *step_time_ms = kBacklightStepTimeMs; | 170 *step_time_ms = kBacklightStepTimeMs; |
| 162 return true; | 171 return true; |
| 163 } | 172 } |
| 164 | 173 |
| 165 gboolean Backlight::SetBrightnessHard(int64 level, int64 target_level) { | 174 gboolean Backlight::SetBrightnessHard(int64 level, int64 target_level) { |
| 166 // If the target brightness of this call does not match the backlight's | 175 // If the target brightness of this call does not match the backlight's |
| 167 // current target brightness, it must be from an earlier backlight adjustment | 176 // current target brightness, it must be from an earlier backlight adjustment |
| 168 // that had a different target brightness. In that case, it is invalidated | 177 // that had a different target brightness. In that case, it is invalidated |
| 169 // so do nothing. | 178 // so do nothing. |
| 170 if (target_brightness_ != target_level) | 179 if (target_brightness_ != target_level) |
| 171 return false; // Return false so glib doesn't repeat. | 180 return false; // Return false so glib doesn't repeat. |
| 172 DLOG(INFO) << "Setting brightness to " << level; | 181 DLOG(INFO) << "Setting brightness to " << level; |
| 173 std::string buf = base::Int64ToString(level); | 182 std::string buf = base::Int64ToString(level); |
| 174 bool ok = (-1 != file_util::WriteFile(brightness_path_, buf.data(), | 183 bool ok = (-1 != file_util::WriteFile(brightness_path_, buf.data(), |
| 175 buf.size())); | 184 buf.size())); |
| 176 LOG_IF(WARNING, !ok) << "Can't set brightness to " << level; | 185 LOG_IF(WARNING, !ok) << "Can't set brightness to " << level; |
| 186 |
| 187 if (level == 0 && |
| 188 target_level == 0 && |
| 189 turn_screen_off_func_ != NULL && |
| 190 turn_screen_off_data_ != NULL) |
| 191 turn_screen_off_func_(turn_screen_off_data_); |
| 177 return false; // Return false so glib doesn't repeat. | 192 return false; // Return false so glib doesn't repeat. |
| 178 } | 193 } |
| 179 | 194 |
| 180 } // namespace power_manager | 195 } // namespace power_manager |
| OLD | NEW |