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