| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium 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 "ui/aura/monitor_change_observer_x11.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <map> |
| 9 #include <set> |
| 10 #include <vector> |
| 11 |
| 12 #include <X11/extensions/Xrandr.h> |
| 13 |
| 14 #include "base/message_pump_x.h" |
| 15 #include "base/stl_util.h" |
| 16 #include "ui/aura/env.h" |
| 17 #include "ui/aura/monitor.h" |
| 18 #include "ui/aura/monitor_manager.h" |
| 19 |
| 20 namespace aura { |
| 21 namespace { |
| 22 XRRModeInfo* FindMode(XRRScreenResources* screen_resources, XID current_mode) { |
| 23 for (int m = 0; m < screen_resources->nmode; m++) { |
| 24 XRRModeInfo *mode = &screen_resources->modes[m]; |
| 25 if (mode->id == current_mode) |
| 26 return mode; |
| 27 } |
| 28 return NULL; |
| 29 } |
| 30 |
| 31 bool CompareMonitorY(const Monitor* lhs, const Monitor* rhs) { |
| 32 return lhs->bounds().y() < rhs->bounds().y(); |
| 33 } |
| 34 |
| 35 } // namespace internal |
| 36 |
| 37 MonitorChangeObserverX11::MonitorChangeObserverX11() |
| 38 : xdisplay_(base::MessagePumpX::GetDefaultXDisplay()), |
| 39 x_root_window_(DefaultRootWindow(xdisplay_)), |
| 40 xrandr_event_base_(0) { |
| 41 XRRSelectInput(xdisplay_, x_root_window_, RRScreenChangeNotifyMask); |
| 42 int error_base_ignored; |
| 43 XRRQueryExtension(xdisplay_, &xrandr_event_base_, &error_base_ignored); |
| 44 } |
| 45 |
| 46 MonitorChangeObserverX11::~MonitorChangeObserverX11() { |
| 47 } |
| 48 |
| 49 bool MonitorChangeObserverX11::Dispatch(const XEvent* event) { |
| 50 if (event->type - xrandr_event_base_ == RRScreenChangeNotify) { |
| 51 NotifyMonitorChange(); |
| 52 return true; |
| 53 } |
| 54 return false; |
| 55 } |
| 56 |
| 57 void MonitorChangeObserverX11::NotifyMonitorChange() { |
| 58 if (!MonitorManager::use_fullscreen_host_window()) |
| 59 return; // Use the default monitor that monitor manager determined. |
| 60 |
| 61 XRRScreenResources* screen_resources = |
| 62 XRRGetScreenResources(xdisplay_, x_root_window_); |
| 63 std::map<XID, XRRCrtcInfo*> crtc_info_map; |
| 64 |
| 65 for (int c = 0; c < screen_resources->ncrtc; c++) { |
| 66 XID crtc_id = screen_resources->crtcs[c]; |
| 67 XRRCrtcInfo *crtc_info = |
| 68 XRRGetCrtcInfo(xdisplay_, screen_resources, crtc_id); |
| 69 crtc_info_map[crtc_id] = crtc_info; |
| 70 } |
| 71 |
| 72 std::vector<const Monitor*> monitors; |
| 73 std::set<int> y_coords; |
| 74 for (int o = 0; o < screen_resources->noutput; o++) { |
| 75 XRROutputInfo *output_info = |
| 76 XRRGetOutputInfo(xdisplay_, |
| 77 screen_resources, |
| 78 screen_resources->outputs[o]); |
| 79 if (output_info->connection != RR_Connected) { |
| 80 XRRFreeOutputInfo(output_info); |
| 81 continue; |
| 82 } |
| 83 XRRCrtcInfo* crtc_info = crtc_info_map[output_info->crtc]; |
| 84 if (!crtc_info) { |
| 85 LOG(WARNING) << "Crtc not found for output"; |
| 86 continue; |
| 87 } |
| 88 XRRModeInfo* mode = FindMode(screen_resources, crtc_info->mode); |
| 89 CHECK(mode); |
| 90 // Mirrored monitors have the same y coordinates. |
| 91 if (y_coords.find(crtc_info->y) != y_coords.end()) |
| 92 continue; |
| 93 Monitor* monitor = new Monitor; |
| 94 monitor->set_bounds(gfx::Rect(crtc_info->x, crtc_info->y, |
| 95 mode->width, mode->height)); |
| 96 monitors.push_back(monitor); |
| 97 y_coords.insert(crtc_info->y); |
| 98 XRRFreeOutputInfo(output_info); |
| 99 } |
| 100 |
| 101 // Free all allocated resources. |
| 102 for (std::map<XID, XRRCrtcInfo*>::const_iterator iter = crtc_info_map.begin(); |
| 103 iter != crtc_info_map.end(); ++iter) { |
| 104 XRRFreeCrtcInfo(iter->second); |
| 105 } |
| 106 XRRFreeScreenResources(screen_resources); |
| 107 |
| 108 // PowerManager lays out the outputs vertically. Sort them by Y |
| 109 // coordinates. |
| 110 std::sort(monitors.begin(), monitors.end(), CompareMonitorY); |
| 111 aura::Env::GetInstance()->monitor_manager() |
| 112 ->OnNativeMonitorsChanged(monitors); |
| 113 STLDeleteContainerPointers(monitors.begin(), monitors.end()); |
| 114 } |
| 115 |
| 116 } // namespace aura |
| OLD | NEW |