OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "platform/monitor_reconfig/monitor_reconfigure_main.h" | 5 #include "monitor_reconfig/monitor_reconfigure_main.h" |
6 | 6 |
7 using namespace std; | 7 #include <cstdio> |
8 #include <cstdlib> | |
8 | 9 |
9 static const char* kDisplay = ":0.0"; | 10 #include "base/logging.h" |
10 | 11 |
11 namespace chromeos_monitor_reconfig { | 12 using std::map; |
13 | |
14 namespace monitor_reconfig { | |
12 | 15 |
13 MonitorReconfigureMain::MonitorReconfigureMain(Display* display, | 16 MonitorReconfigureMain::MonitorReconfigureMain(Display* display, |
14 XRRScreenResources* screen_info) | 17 XRRScreenResources* screen_info) |
15 : display_(display), screen_info_(screen_info) { | 18 : display_(display), |
16 // Initialize hash table with modes | 19 screen_info_(screen_info) { |
17 for (int m = 0; m < screen_info_->nmode; m++) { | 20 for (int i = 0; i < screen_info_->nmode; ++i) { |
18 XRRModeInfo* current_mode = &screen_info_->modes[m]; | 21 XRRModeInfo* current_mode = &screen_info_->modes[i]; |
19 mode_map_[current_mode->id] = current_mode; | 22 mode_map_[current_mode->id] = current_mode; |
20 } | 23 } |
21 DetermineOutputs(); | 24 DetermineOutputs(); |
22 } | 25 } |
23 | 26 |
24 void MonitorReconfigureMain::DetermineOutputs() { | 27 void MonitorReconfigureMain::DetermineOutputs() { |
25 static const string kNotebookOutputName = "LVDS1"; | 28 XRROutputInfo* first_output = |
26 XRROutputInfo* current_output = XRRGetOutputInfo(display_, screen_info_, | 29 XRRGetOutputInfo(display_, screen_info_, screen_info_->outputs[0]); |
27 screen_info_->outputs[0]); | 30 XRROutputInfo* second_output = |
28 if (strcmp(current_output->name, kNotebookOutputName.c_str()) == 0) { | 31 XRRGetOutputInfo(display_, screen_info_, screen_info_->outputs[1]); |
29 notebook_output_ = current_output; | 32 |
30 external_output_ = XRRGetOutputInfo(display_, screen_info_, | 33 static const char* kNotebookOutputName = "LVDS1"; |
31 screen_info_->outputs[1]); | 34 if (strcmp(first_output->name, kNotebookOutputName) == 0) { |
35 notebook_output_ = first_output; | |
36 external_output_ = second_output; | |
32 } else { | 37 } else { |
33 notebook_output_ = XRRGetOutputInfo(display_, screen_info_, | 38 notebook_output_ = second_output; |
34 screen_info_->outputs[1]); | 39 external_output_ = first_output; |
35 external_output_ = current_output; | 40 } |
41 | |
42 for (int i = 0; i < notebook_output_->nmode; ++i) { | |
43 XRRModeInfo* mode = mode_map_[notebook_output_->modes[i]]; | |
44 LOG(INFO) << "notebook mode: " << mode->width << "x" << mode->height; | |
45 } | |
46 for (int i = 0; i < external_output_->nmode; ++i) { | |
47 XRRModeInfo* mode = mode_map_[external_output_->modes[i]]; | |
48 LOG(INFO) << "external mode: " << mode->width << "x" << mode->height; | |
36 } | 49 } |
37 } | 50 } |
38 | 51 |
52 | |
39 XRRModeInfo* MonitorReconfigureMain::FindMaxResolution(XRROutputInfo* output) { | 53 XRRModeInfo* MonitorReconfigureMain::FindMaxResolution(XRROutputInfo* output) { |
40 XRRModeInfo* mode_return = NULL; | 54 XRRModeInfo* mode_return = NULL; |
41 for (int m = 0; m < output->nmode; m++) { | 55 for (int i = 0; i < output->nmode; ++i) { |
sosa
2010/04/14 17:20:33
you don't like m's :)?
| |
42 XRRModeInfo* current_mode = mode_map_[output->modes[m]]; | 56 XRRModeInfo* current_mode = mode_map_[output->modes[i]]; |
43 if (mode_return == NULL) { | 57 if (mode_return == NULL) { |
44 mode_return = current_mode; | 58 mode_return = current_mode; |
45 } else { | 59 } else { |
46 int n_size = mode_return->height * mode_return->width; | 60 int n_size = mode_return->height * mode_return->width; |
47 int c_size = current_mode->height * current_mode->width; | 61 int c_size = current_mode->height * current_mode->width; |
48 if (c_size > n_size) { | 62 if (c_size > n_size) { |
49 mode_return = current_mode; | 63 mode_return = current_mode; |
50 } | 64 } |
51 } | 65 } |
52 } | 66 } |
53 return mode_return; | 67 return mode_return; |
54 } | 68 } |
55 | 69 |
56 bool MonitorReconfigureMain::IsEqual(XRRModeInfo* one, | 70 bool MonitorReconfigureMain::IsEqual(XRRModeInfo* one, XRRModeInfo* two) { |
57 XRRModeInfo* two) { | |
58 return (one->height * one->width) == (two->height * two->width); | 71 return (one->height * one->width) == (two->height * two->width); |
59 } | 72 } |
60 | 73 |
61 bool MonitorReconfigureMain::IsBiggerOrEqual(XRRModeInfo* target, | 74 bool MonitorReconfigureMain::IsBiggerOrEqual(XRRModeInfo* target, |
62 XRRModeInfo* screen) { | 75 XRRModeInfo* screen) { |
63 return ((target->width >= screen->width) && | 76 return ((target->width >= screen->width) && |
64 (target->height >= screen->height)); | 77 (target->height >= screen->height)); |
65 } | 78 } |
66 | 79 |
67 bool MonitorReconfigureMain::IsBetterMatching(XRRModeInfo* target, | 80 bool MonitorReconfigureMain::IsBetterMatching(XRRModeInfo* target, |
68 XRRModeInfo* to_match, XRRModeInfo* previous_best) { | 81 XRRModeInfo* to_match, |
69 if (IsEqual(previous_best, to_match)) return false; | 82 XRRModeInfo* previous_best) { |
83 if (IsEqual(previous_best, to_match)) | |
84 return false; | |
70 // If the current will have some of the display cut off | 85 // If the current will have some of the display cut off |
71 // and the new choice doesn't, choose the new one | 86 // and the new choice doesn't, choose the new one |
72 if ((!IsBiggerOrEqual(previous_best, to_match)) && | 87 if ((!IsBiggerOrEqual(previous_best, to_match)) && |
73 (IsBiggerOrEqual(target, to_match))) { | 88 (IsBiggerOrEqual(target, to_match))) { |
74 return true; | 89 return true; |
75 // If the current one isn't cropped and the new one would | 90 // If the current one isn't cropped and the new one would |
76 // get cropped | 91 // get cropped |
77 } else if (IsBiggerOrEqual(previous_best, to_match) && | 92 } else if (IsBiggerOrEqual(previous_best, to_match) && |
78 (!IsBiggerOrEqual(target, to_match))) { | 93 !IsBiggerOrEqual(target, to_match)) { |
79 return false; | 94 return false; |
80 // Case if the current is bigger than the matching but the new target falls | 95 // Case if the current is bigger than the matching but the new target falls |
81 // between the current and the matching (so it's closer to the matching) | 96 // between the current and the matching (so it's closer to the matching) |
82 } else if (IsBiggerOrEqual(previous_best, to_match)) { | 97 } else if (IsBiggerOrEqual(previous_best, to_match)) { |
83 return !IsBiggerOrEqual(target, previous_best); | 98 return !IsBiggerOrEqual(target, previous_best); |
84 } else { | 99 } else { |
85 // Final case, we know the current is smaller than the matching | 100 // Final case, we know the current is smaller than the matching |
86 // We just need to check if the new will bring us closer to the matching | 101 // We just need to check if the new will bring us closer to the matching |
87 return IsBiggerOrEqual(target, previous_best); | 102 return IsBiggerOrEqual(target, previous_best); |
88 } | 103 } |
89 } | 104 } |
90 | 105 |
91 XRRModeInfo* MonitorReconfigureMain::FindBestMatchingResolution( | 106 XRRModeInfo* MonitorReconfigureMain::FindBestMatchingResolution( |
92 XRRModeInfo* matching_mode) { | 107 XRRModeInfo* matching_mode) { |
93 // Need a min mode to increase from | 108 // Need a min mode to increase from |
94 XRRModeInfo min_mode; | 109 XRRModeInfo min_mode; |
95 min_mode.height = 0; | 110 min_mode.height = 0; |
96 min_mode.width = 0; | 111 min_mode.width = 0; |
97 XRRModeInfo* best_mode = &min_mode; | 112 XRRModeInfo* best_mode = &min_mode; |
98 // Match horizontal if notebook is wider, o/w match vertical | 113 // Match horizontal if notebook is wider, o/w match vertical |
99 for (int m = 0; m < external_output_->nmode; m++) { | 114 for (int i = 0; i < external_output_->nmode; ++i) { |
100 XRRModeInfo* current_mode = | 115 XRRModeInfo* current_mode = mode_map_[external_output_->modes[i]]; |
101 mode_map_[external_output_->modes[m]]; | 116 if (IsBetterMatching(current_mode, matching_mode, best_mode)) |
102 if (IsBetterMatching(current_mode, matching_mode, best_mode)) { | |
103 best_mode = current_mode; | 117 best_mode = current_mode; |
104 } | |
105 } | 118 } |
106 if (best_mode == &min_mode) best_mode = NULL; | 119 if (best_mode == &min_mode) |
120 best_mode = NULL; | |
107 return best_mode; | 121 return best_mode; |
108 } | 122 } |
109 | 123 |
110 void MonitorReconfigureMain::SetResolutions(XRRModeInfo* notebook_mode, | 124 void MonitorReconfigureMain::SetResolutions(XRRModeInfo* notebook_mode, |
111 XRRModeInfo* external_mode, | 125 XRRModeInfo* external_mode, |
112 XRRModeInfo* overall_screen_size) { | 126 XRRModeInfo* overall_screen_size) { |
113 // We use xrandr script to set modes | 127 // We use xrandr script to set modes |
114 char buffer[512]; | 128 char buffer[512]; |
115 snprintf(buffer, sizeof(buffer), "xrandr --output %s --mode %s", | 129 snprintf(buffer, sizeof(buffer), "xrandr --output %s --mode %s", |
116 external_output_->name, external_mode->name); | 130 external_output_->name, external_mode->name); |
117 system(buffer); | 131 system(buffer); |
118 snprintf(buffer, sizeof(buffer), "xrandr --output %s --mode %s", | 132 snprintf(buffer, sizeof(buffer), "xrandr --output %s --mode %s", |
119 notebook_output_->name, notebook_mode->name); | 133 notebook_output_->name, notebook_mode->name); |
120 system(buffer); | 134 system(buffer); |
121 snprintf(buffer, sizeof(buffer), "xrandr --fb %s", overall_screen_size->name); | 135 snprintf(buffer, sizeof(buffer), "xrandr --fb %s", overall_screen_size->name); |
122 system(buffer); | 136 system(buffer); |
123 } | 137 } |
124 | 138 |
125 void MonitorReconfigureMain::Run() { | 139 void MonitorReconfigureMain::Run() { |
140 if (!IsExternalMonitorConnected()) | |
141 return; | |
142 | |
126 // Find the max resolution for the notebook | 143 // Find the max resolution for the notebook |
127 XRRModeInfo* notebook_mode = FindMaxResolution(notebook_output_); | 144 XRRModeInfo* notebook_mode = FindMaxResolution(notebook_output_); |
128 // Find the best mode for external output relative to above mode | 145 // Find the best mode for external output relative to above mode |
129 XRRModeInfo* external_mode = FindBestMatchingResolution(notebook_mode); | 146 XRRModeInfo* external_mode = FindBestMatchingResolution(notebook_mode); |
130 // Set the resolutions accordingly | 147 // Set the resolutions accordingly |
131 SetResolutions(notebook_mode, external_mode, notebook_mode); | 148 SetResolutions(notebook_mode, external_mode, notebook_mode); |
132 } | 149 } |
133 | 150 |
134 bool MonitorReconfigureMain::IsExternalMonitorConnected() { | 151 bool MonitorReconfigureMain::IsExternalMonitorConnected() { |
135 return (external_output_->connection == RR_Connected); | 152 return (external_output_->connection == RR_Connected); |
136 } | 153 } |
137 } // end namespace chromeos_monitor_reconfig | 154 |
155 } // end namespace monitor_reconfig | |
138 | 156 |
139 int main(int argc, char** argv) { | 157 int main(int argc, char** argv) { |
140 Display* display = XOpenDisplay(kDisplay); | 158 Display* display = XOpenDisplay(NULL); |
141 if (display == NULL) { | 159 CHECK(display) << "Could not open display"; |
142 cerr << "Could not open display '" | 160 |
143 << kDisplay << "'" << endl; | 161 Window window = RootWindow(display, DefaultScreen(display)); |
144 return 1; | 162 XRRScreenResources* screen_info = XRRGetScreenResources(display, window); |
145 } else { | 163 monitor_reconfig::MonitorReconfigureMain main_app(display, screen_info); |
146 Window window = RootWindow(display, DefaultScreen(display)); | 164 |
147 XRRScreenResources* screen_info = XRRGetScreenResources(display, window); | 165 main_app.Run(); |
148 chromeos_monitor_reconfig::MonitorReconfigureMain | 166 return 0; |
149 main_app(display, screen_info); | |
150 if (!main_app.IsExternalMonitorConnected()) { | |
151 return 0; | |
152 } else { | |
153 main_app.Run(); | |
154 return 0; | |
155 } | |
156 } | |
157 } | 167 } |
OLD | NEW |