Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(220)

Side by Side Diff: monitor_reconfigure.cc

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

Powered by Google App Engine
This is Rietveld 408576698