Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "power_manager/backlight_controller.h" | |
|
tfarina
2011/04/14 18:54:18
While you are here, move this include down, to lin
marcheu
2011/04/14 19:01:51
Done.
| |
| 6 #include "power_manager/monitor_reconfigure.h" | |
| 7 | |
| 8 #include <algorithm> | |
| 9 #include <cstdio> | |
| 10 #include <cstdlib> | |
| 11 | |
| 12 #include "base/command_line.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/string_util.h" | |
| 15 #include "power_manager/udev_listener.h" | |
| 16 | |
| 17 using std::map; | |
| 18 using std::sort; | |
| 19 using std::string; | |
| 20 using std::vector; | |
| 21 | |
| 22 namespace { | |
| 23 // Namf of the internal output. | |
|
tfarina
2011/04/14 18:54:18
please, no two spaces inside this namespace. Also,
marcheu
2011/04/14 19:01:51
Done.
| |
| 24 const char kLcdOutputName[] = "LVDS1"; | |
| 25 // The screen DPI we return to X11. | |
|
tfarina
2011/04/14 18:54:18
I'd suggest: "we pass" or "we send back"
marcheu
2011/04/14 19:01:51
Done.
| |
| 26 const float kScreenDpi = 96.0; | |
| 27 // An inch in mm. | |
| 28 const float kInchInMm = 25.4; | |
| 29 } | |
| 30 namespace power_manager { | |
|
tfarina
2011/04/14 18:54:18
add a blank line above.
marcheu
2011/04/14 19:01:51
Done.
| |
| 31 | |
| 32 void MonitorReconfigureCallback::Run(GIOChannel* /* source */, | |
| 33 GIOCondition /* condition */) { | |
| 34 monitor_reconfigure_->Run(); | |
| 35 } | |
| 36 | |
| 37 MonitorReconfigureMain::MonitorReconfigureMain() | |
| 38 : backlight_ctl_(NULL), | |
| 39 callback_(NULL), | |
| 40 listener_(NULL) {} | |
| 41 | |
| 42 MonitorReconfigureMain::MonitorReconfigureMain( | |
| 43 BacklightController* backlight_ctl) | |
| 44 : backlight_ctl_(backlight_ctl), | |
| 45 callback_(NULL), | |
| 46 listener_(NULL) {} | |
| 47 | |
| 48 MonitorReconfigureMain::~MonitorReconfigureMain() { | |
| 49 } | |
| 50 | |
| 51 bool MonitorReconfigureMain::Init() { | |
| 52 callback_ = new MonitorReconfigureCallback(this); | |
| 53 listener_ = new UdevListener(callback_, "drm"); | |
| 54 return listener_->Init(); | |
| 55 } | |
| 56 | |
| 57 void MonitorReconfigureMain::Run() { | |
| 58 lcd_output_ = 0; | |
| 59 lcd_output_info_ = NULL; | |
| 60 external_output_ = 0; | |
| 61 external_output_info_ = NULL; | |
| 62 display_ = XOpenDisplay(NULL); | |
| 63 CHECK(display_) << "Could not open display"; | |
| 64 | |
| 65 window_ = RootWindow(display_, DefaultScreen(display_)); | |
| 66 screen_info_ = XRRGetScreenResources(display_, window_); | |
| 67 | |
| 68 for (int i = 0; i < screen_info_->nmode; ++i) { | |
| 69 XRRModeInfo* current_mode = &screen_info_->modes[i]; | |
| 70 mode_map_[current_mode->id] = current_mode; | |
| 71 } | |
| 72 DetermineOutputs(); | |
| 73 vector<ResolutionSelector::Mode> lcd_modes; | |
| 74 SortModesByResolution(lcd_output_, &lcd_modes); | |
| 75 DCHECK(!lcd_modes.empty()); | |
| 76 | |
| 77 vector<ResolutionSelector::Mode> external_modes; | |
| 78 if (IsExternalMonitorConnected()) { | |
| 79 SortModesByResolution(external_output_, &external_modes); | |
| 80 DCHECK(!external_modes.empty()); | |
| 81 } | |
| 82 | |
| 83 ResolutionSelector selector; | |
| 84 ResolutionSelector::Mode lcd_resolution; | |
| 85 ResolutionSelector::Mode external_resolution; | |
| 86 ResolutionSelector::Mode screen_resolution; | |
| 87 | |
| 88 CHECK(selector.FindBestResolutions(lcd_modes, | |
| 89 external_modes, | |
| 90 &lcd_resolution, | |
| 91 &external_resolution, | |
| 92 &screen_resolution)); | |
| 93 CHECK(!lcd_resolution.name.empty() || !external_resolution.name.empty()); | |
| 94 | |
| 95 // Disable the backlight before resizing anything on the screen. | |
| 96 if (lcd_resolution.name.empty()) | |
| 97 backlight_ctl_->SetPowerState(BACKLIGHT_ACTIVE_OFF); | |
| 98 | |
| 99 // Grab the server during mode changes. | |
| 100 XGrabServer(display_); | |
| 101 | |
| 102 // Disable the LCD if we were told to do so (because we're using a higher | |
| 103 // resolution that'd be clipped on the LCD). | |
| 104 // Otherwise enable the LCD if appropriate. | |
| 105 if (lcd_resolution.name.empty()) | |
| 106 DisableDevice(lcd_output_info_); | |
| 107 else | |
| 108 // FIXME: here set the gamma ramp. | |
| 109 SetDeviceResolution(lcd_output_, lcd_output_info_, lcd_resolution); | |
| 110 | |
| 111 // If there's no external output connected, disable the device before we try | |
| 112 // to set the screen resolution; otherwise xrandr will complain if we're | |
| 113 // trying to set the screen to a smaller size than what the now-unplugged | |
| 114 // device was using. | |
| 115 // Otherwise enable the external device if appropriate. | |
| 116 if (external_resolution.name.empty()) | |
| 117 DisableDevice(external_output_info_); | |
| 118 else | |
| 119 SetDeviceResolution(external_output_, external_output_info_, | |
| 120 external_resolution); | |
| 121 | |
| 122 // Set the fb resolution. | |
| 123 SetScreenResolution(screen_resolution); | |
| 124 | |
| 125 // Now let the server go and sync all changes. | |
| 126 XUngrabServer(display_); | |
| 127 XSync(display_, False); | |
| 128 | |
| 129 XRRFreeOutputInfo(lcd_output_info_); | |
| 130 XRRFreeOutputInfo(external_output_info_); | |
| 131 XRRFreeScreenResources(screen_info_); | |
| 132 | |
| 133 // Enable the backlight. We do not want to do this before the resize is | |
| 134 // done so that we can hide the resize when going off->on. | |
| 135 if (!lcd_resolution.name.empty()) | |
| 136 backlight_ctl_->SetPowerState(BACKLIGHT_ACTIVE_ON); | |
| 137 } | |
| 138 | |
| 139 void MonitorReconfigureMain::DetermineOutputs() { | |
| 140 CHECK(screen_info_->noutput > 1) << "Expected at least two outputs"; | |
| 141 CHECK(!lcd_output_info_); | |
| 142 CHECK(!external_output_info_); | |
| 143 | |
| 144 RROutput first_output = screen_info_->outputs[0]; | |
| 145 RROutput second_output = screen_info_->outputs[1]; | |
| 146 | |
| 147 XRROutputInfo* first_output_info = | |
| 148 XRRGetOutputInfo(display_, screen_info_, first_output); | |
| 149 XRROutputInfo* second_output_info = | |
| 150 XRRGetOutputInfo(display_, screen_info_, second_output); | |
| 151 | |
| 152 if (strcmp(first_output_info->name, kLcdOutputName) == 0) { | |
| 153 lcd_output_ = first_output; | |
| 154 lcd_output_info_ = first_output_info; | |
| 155 external_output_ = second_output; | |
| 156 external_output_info_ = second_output_info; | |
| 157 } else { | |
| 158 lcd_output_ = second_output; | |
| 159 lcd_output_info_ = second_output_info; | |
| 160 external_output_ = first_output; | |
| 161 external_output_info_ = first_output_info; | |
| 162 } | |
| 163 | |
| 164 LOG(INFO) << "LCD name: " << lcd_output_info_->name << " (xid " | |
| 165 << lcd_output_ << ")"; | |
| 166 for (int i = 0; i < lcd_output_info_->nmode; ++i) { | |
| 167 XRRModeInfo* mode = mode_map_[lcd_output_info_->modes[i]]; | |
| 168 LOG(INFO) << " Mode: " << mode->width << "x" << mode->height | |
| 169 << " (xid " << mode->id << ")"; | |
| 170 } | |
| 171 | |
| 172 LOG(INFO) << "External name: " << external_output_info_->name | |
| 173 << " (xid " << external_output_ << ")"; | |
| 174 for (int i = 0; i < external_output_info_->nmode; ++i) { | |
| 175 XRRModeInfo* mode = mode_map_[external_output_info_->modes[i]]; | |
| 176 LOG(INFO) << " Mode: " << mode->width << "x" << mode->height | |
| 177 << " (xid " << mode->id << ")"; | |
| 178 } | |
| 179 } | |
| 180 | |
| 181 bool MonitorReconfigureMain::IsExternalMonitorConnected() { | |
| 182 return (external_output_info_->connection == RR_Connected); | |
| 183 } | |
| 184 | |
| 185 void MonitorReconfigureMain::SortModesByResolution( | |
| 186 RROutput output, | |
| 187 vector<ResolutionSelector::Mode>* modes_out) { | |
| 188 modes_out->clear(); | |
| 189 | |
| 190 XRROutputInfo* output_info = XRRGetOutputInfo(display_, screen_info_, output); | |
| 191 for (int i = 0; i < output_info->nmode; ++i) { | |
| 192 const XRRModeInfo* mode = mode_map_[output_info->modes[i]]; | |
| 193 DCHECK(mode); | |
| 194 LOG(INFO) << "Adding mode " << mode->width << " " << mode->height | |
| 195 << " (xid " << mode->id << ")"; | |
| 196 modes_out->push_back( | |
| 197 ResolutionSelector::Mode(mode->width, mode->height, mode->name, | |
| 198 mode->id)); | |
| 199 } | |
| 200 | |
| 201 sort(modes_out->begin(), modes_out->end(), | |
| 202 ResolutionSelector::ModeResolutionComparator()); | |
| 203 | |
| 204 XRRFreeOutputInfo(output_info); | |
| 205 } | |
| 206 | |
| 207 bool MonitorReconfigureMain::SetDeviceResolution( | |
| 208 RROutput output, | |
| 209 const XRROutputInfo* output_info, | |
| 210 const ResolutionSelector::Mode& resolution) { | |
| 211 Status s = XRRSetCrtcConfig(display_, screen_info_, output_info->crtcs[0], | |
| 212 CurrentTime, 0, 0, resolution.id, RR_Rotate_0, | |
| 213 &output, 1); | |
| 214 return (s == RRSetConfigSuccess); | |
| 215 } | |
| 216 | |
| 217 bool MonitorReconfigureMain::SetScreenResolution( | |
| 218 const ResolutionSelector::Mode& resolution) { | |
| 219 LOG(INFO) << "Setting resolution " << resolution.name.c_str() << " " | |
| 220 << resolution.width << "x" << resolution.height; | |
| 221 // Do not switch resolutions if we don't need to, this avoids blinking. | |
| 222 int screen = DefaultScreen(display_); | |
| 223 if ( (resolution.width != DisplayWidth(display_, screen))|| | |
| 224 (resolution.height != DisplayHeight(display_, screen)) ) | |
| 225 XRRSetScreenSize(display_, window_, | |
| 226 resolution.width, | |
| 227 resolution.height, | |
| 228 resolution.width * kInchInMm / kScreenDpi, | |
| 229 resolution.height * kInchInMm / kScreenDpi); | |
| 230 | |
| 231 return true; | |
| 232 } | |
| 233 | |
| 234 bool MonitorReconfigureMain::DisableDevice(const XRROutputInfo* output_info) { | |
| 235 if (!output_info->crtc) | |
| 236 return true; | |
| 237 LOG(INFO) << "Disabling output " << output_info->name; | |
| 238 Status s = XRRSetCrtcConfig(display_, screen_info_, output_info->crtc, | |
| 239 CurrentTime, 0, 0, None, RR_Rotate_0, NULL, 0); | |
| 240 return (s == RRSetConfigSuccess); | |
| 241 } | |
| 242 | |
| 243 } // namespace power_manager | |
| OLD | NEW |