Index: src/platform/monitor_reconfig/monitor_reconfigure_main.cc |
diff --git a/src/platform/monitor_reconfig/monitor_reconfigure_main.cc b/src/platform/monitor_reconfig/monitor_reconfigure_main.cc |
index f71f1c7edc6dfa3ccd8257953b5e72d895a1c67a..52f30fa461e851b4d18e1573e5fb50396f910ff4 100644 |
--- a/src/platform/monitor_reconfig/monitor_reconfigure_main.cc |
+++ b/src/platform/monitor_reconfig/monitor_reconfigure_main.cc |
@@ -2,44 +2,58 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "platform/monitor_reconfig/monitor_reconfigure_main.h" |
+#include "monitor_reconfig/monitor_reconfigure_main.h" |
-using namespace std; |
+#include <cstdio> |
+#include <cstdlib> |
-static const char* kDisplay = ":0.0"; |
+#include "base/logging.h" |
-namespace chromeos_monitor_reconfig { |
+using std::map; |
+ |
+namespace monitor_reconfig { |
MonitorReconfigureMain::MonitorReconfigureMain(Display* display, |
- XRRScreenResources* screen_info) |
- : display_(display), screen_info_(screen_info) { |
- // Initialize hash table with modes |
- for (int m = 0; m < screen_info_->nmode; m++) { |
- XRRModeInfo* current_mode = &screen_info_->modes[m]; |
+ XRRScreenResources* screen_info) |
+ : display_(display), |
+ screen_info_(screen_info) { |
+ for (int i = 0; i < screen_info_->nmode; ++i) { |
+ XRRModeInfo* current_mode = &screen_info_->modes[i]; |
mode_map_[current_mode->id] = current_mode; |
} |
DetermineOutputs(); |
} |
void MonitorReconfigureMain::DetermineOutputs() { |
- static const string kNotebookOutputName = "LVDS1"; |
- XRROutputInfo* current_output = XRRGetOutputInfo(display_, screen_info_, |
- screen_info_->outputs[0]); |
- if (strcmp(current_output->name, kNotebookOutputName.c_str()) == 0) { |
- notebook_output_ = current_output; |
- external_output_ = XRRGetOutputInfo(display_, screen_info_, |
- screen_info_->outputs[1]); |
+ XRROutputInfo* first_output = |
+ XRRGetOutputInfo(display_, screen_info_, screen_info_->outputs[0]); |
+ XRROutputInfo* second_output = |
+ XRRGetOutputInfo(display_, screen_info_, screen_info_->outputs[1]); |
+ |
+ static const char* kNotebookOutputName = "LVDS1"; |
+ if (strcmp(first_output->name, kNotebookOutputName) == 0) { |
+ notebook_output_ = first_output; |
+ external_output_ = second_output; |
} else { |
- notebook_output_ = XRRGetOutputInfo(display_, screen_info_, |
- screen_info_->outputs[1]); |
- external_output_ = current_output; |
+ notebook_output_ = second_output; |
+ external_output_ = first_output; |
+ } |
+ |
+ for (int i = 0; i < notebook_output_->nmode; ++i) { |
+ XRRModeInfo* mode = mode_map_[notebook_output_->modes[i]]; |
+ LOG(INFO) << "notebook mode: " << mode->width << "x" << mode->height; |
+ } |
+ for (int i = 0; i < external_output_->nmode; ++i) { |
+ XRRModeInfo* mode = mode_map_[external_output_->modes[i]]; |
+ LOG(INFO) << "external mode: " << mode->width << "x" << mode->height; |
} |
} |
+ |
XRRModeInfo* MonitorReconfigureMain::FindMaxResolution(XRROutputInfo* output) { |
XRRModeInfo* mode_return = NULL; |
- for (int m = 0; m < output->nmode; m++) { |
- XRRModeInfo* current_mode = mode_map_[output->modes[m]]; |
+ for (int i = 0; i < output->nmode; ++i) { |
sosa
2010/04/14 17:20:33
you don't like m's :)?
|
+ XRRModeInfo* current_mode = mode_map_[output->modes[i]]; |
if (mode_return == NULL) { |
mode_return = current_mode; |
} else { |
@@ -53,20 +67,21 @@ XRRModeInfo* MonitorReconfigureMain::FindMaxResolution(XRROutputInfo* output) { |
return mode_return; |
} |
-bool MonitorReconfigureMain::IsEqual(XRRModeInfo* one, |
- XRRModeInfo* two) { |
+bool MonitorReconfigureMain::IsEqual(XRRModeInfo* one, XRRModeInfo* two) { |
return (one->height * one->width) == (two->height * two->width); |
} |
bool MonitorReconfigureMain::IsBiggerOrEqual(XRRModeInfo* target, |
- XRRModeInfo* screen) { |
+ XRRModeInfo* screen) { |
return ((target->width >= screen->width) && |
- (target->height >= screen->height)); |
+ (target->height >= screen->height)); |
} |
bool MonitorReconfigureMain::IsBetterMatching(XRRModeInfo* target, |
- XRRModeInfo* to_match, XRRModeInfo* previous_best) { |
- if (IsEqual(previous_best, to_match)) return false; |
+ XRRModeInfo* to_match, |
+ XRRModeInfo* previous_best) { |
+ if (IsEqual(previous_best, to_match)) |
+ return false; |
// If the current will have some of the display cut off |
// and the new choice doesn't, choose the new one |
if ((!IsBiggerOrEqual(previous_best, to_match)) && |
@@ -75,7 +90,7 @@ bool MonitorReconfigureMain::IsBetterMatching(XRRModeInfo* target, |
// If the current one isn't cropped and the new one would |
// get cropped |
} else if (IsBiggerOrEqual(previous_best, to_match) && |
- (!IsBiggerOrEqual(target, to_match))) { |
+ !IsBiggerOrEqual(target, to_match)) { |
return false; |
// Case if the current is bigger than the matching but the new target falls |
// between the current and the matching (so it's closer to the matching) |
@@ -96,14 +111,13 @@ XRRModeInfo* MonitorReconfigureMain::FindBestMatchingResolution( |
min_mode.width = 0; |
XRRModeInfo* best_mode = &min_mode; |
// Match horizontal if notebook is wider, o/w match vertical |
- for (int m = 0; m < external_output_->nmode; m++) { |
- XRRModeInfo* current_mode = |
- mode_map_[external_output_->modes[m]]; |
- if (IsBetterMatching(current_mode, matching_mode, best_mode)) { |
+ for (int i = 0; i < external_output_->nmode; ++i) { |
+ XRRModeInfo* current_mode = mode_map_[external_output_->modes[i]]; |
+ if (IsBetterMatching(current_mode, matching_mode, best_mode)) |
best_mode = current_mode; |
- } |
} |
- if (best_mode == &min_mode) best_mode = NULL; |
+ if (best_mode == &min_mode) |
+ best_mode = NULL; |
return best_mode; |
} |
@@ -113,16 +127,19 @@ void MonitorReconfigureMain::SetResolutions(XRRModeInfo* notebook_mode, |
// We use xrandr script to set modes |
char buffer[512]; |
snprintf(buffer, sizeof(buffer), "xrandr --output %s --mode %s", |
- external_output_->name, external_mode->name); |
+ external_output_->name, external_mode->name); |
system(buffer); |
snprintf(buffer, sizeof(buffer), "xrandr --output %s --mode %s", |
- notebook_output_->name, notebook_mode->name); |
+ notebook_output_->name, notebook_mode->name); |
system(buffer); |
snprintf(buffer, sizeof(buffer), "xrandr --fb %s", overall_screen_size->name); |
system(buffer); |
} |
void MonitorReconfigureMain::Run() { |
+ if (!IsExternalMonitorConnected()) |
+ return; |
+ |
// Find the max resolution for the notebook |
XRRModeInfo* notebook_mode = FindMaxResolution(notebook_output_); |
// Find the best mode for external output relative to above mode |
@@ -134,24 +151,17 @@ void MonitorReconfigureMain::Run() { |
bool MonitorReconfigureMain::IsExternalMonitorConnected() { |
return (external_output_->connection == RR_Connected); |
} |
-} // end namespace chromeos_monitor_reconfig |
+ |
+} // end namespace monitor_reconfig |
int main(int argc, char** argv) { |
- Display* display = XOpenDisplay(kDisplay); |
- if (display == NULL) { |
- cerr << "Could not open display '" |
- << kDisplay << "'" << endl; |
- return 1; |
- } else { |
- Window window = RootWindow(display, DefaultScreen(display)); |
- XRRScreenResources* screen_info = XRRGetScreenResources(display, window); |
- chromeos_monitor_reconfig::MonitorReconfigureMain |
- main_app(display, screen_info); |
- if (!main_app.IsExternalMonitorConnected()) { |
- return 0; |
- } else { |
- main_app.Run(); |
- return 0; |
- } |
- } |
+ Display* display = XOpenDisplay(NULL); |
+ CHECK(display) << "Could not open display"; |
+ |
+ Window window = RootWindow(display, DefaultScreen(display)); |
+ XRRScreenResources* screen_info = XRRGetScreenResources(display, window); |
+ monitor_reconfig::MonitorReconfigureMain main_app(display, screen_info); |
+ |
+ main_app.Run(); |
+ return 0; |
} |