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

Side by Side Diff: ui/aura/monitor_change_observer_x11.cc

Issue 9808010: Keep the mouse cursor within desktop (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 9 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) 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 NotifyMonitorChange();
45 }
46
47 MonitorChangeObserverX11::~MonitorChangeObserverX11() {
48 }
49
50 bool MonitorChangeObserverX11::Dispatch(const XEvent* event) {
51 if (event->type - xrandr_event_base_ == RRScreenChangeNotify) {
52 NotifyMonitorChange();
53 return true;
54 }
55 return false;
56 }
57
58 void MonitorChangeObserverX11::NotifyMonitorChange() {
59 if (!MonitorManager::use_fullscreen_host_window())
60 return; // Use the default monitor that monitor manager determined.
61
62 XRRScreenResources* screen_resources =
63 XRRGetScreenResources(xdisplay_, x_root_window_);
64 std::map<XID, XRRCrtcInfo*> crtc_info_map;
65
66 for (int c = 0; c < screen_resources->ncrtc; c++) {
67 XID crtc_id = screen_resources->crtcs[c];
68 XRRCrtcInfo *crtc_info =
69 XRRGetCrtcInfo(xdisplay_, screen_resources, crtc_id);
70 crtc_info_map[crtc_id] = crtc_info;
71 }
72
73 std::vector<const Monitor*> monitors;
74 std::set<int> y_coords;
75 for (int o = 0; o < screen_resources->noutput; o++) {
76 XRROutputInfo *output_info =
77 XRRGetOutputInfo(xdisplay_,
78 screen_resources,
79 screen_resources->outputs[o]);
80 if (output_info->connection != RR_Connected) {
81 XRRFreeOutputInfo(output_info);
82 continue;
83 }
84 XRRCrtcInfo* crtc_info = crtc_info_map[output_info->crtc];
85 if (!crtc_info) {
86 LOG(WARNING) << "Crtc not found for output";
87 continue;
88 }
89 XRRModeInfo* mode = FindMode(screen_resources, crtc_info->mode);
90 CHECK(mode);
91 // Mirrored monitors have the same y coordinates.
92 if (y_coords.find(crtc_info->y) != y_coords.end())
93 continue;
94 Monitor* monitor = new Monitor;
95 monitor->set_bounds(gfx::Rect(crtc_info->x, crtc_info->y,
96 mode->width, mode->height));
97 monitors.push_back(monitor);
98 y_coords.insert(crtc_info->y);
99 XRRFreeOutputInfo(output_info);
100 }
101
102 // Free all allocated resources.
103 for (std::map<XID, XRRCrtcInfo*>::const_iterator iter = crtc_info_map.begin();
104 iter != crtc_info_map.end(); ++iter) {
105 XRRFreeCrtcInfo(iter->second);
106 }
107 XRRFreeScreenResources(screen_resources);
108
109 // PowerManager lays out the outputs vertically. Sort them by Y
110 // coordinates.
111 std::sort(monitors.begin(), monitors.end(), CompareMonitorY);
112 aura::Env::GetInstance()->monitor_manager()
113 ->OnNativeMonitorsChanged(monitors);
114 STLDeleteContainerPointers(monitors.begin(), monitors.end());
115 }
116
117 } // namespace aura
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698