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.
5 #include "ui/gfx/screen.h"
10 #include "base/logging.h"
11 #include "ui/gfx/display.h"
15 bool GetScreenWorkArea(gfx::Rect* out_rect) {
17 guchar* raw_data = NULL;
19 ok = gdk_property_get(gdk_get_default_root_window(), // a gdk window
20 gdk_atom_intern("_NET_WORKAREA", FALSE), // property
21 gdk_atom_intern("CARDINAL", FALSE), // property type
22 0, // byte offset into property
23 0xff, // property length to retrieve
24 false, // delete property after retrieval?
25 NULL, // returned property type
26 NULL, // returned data format
27 &data_len, // returned data len
28 &raw_data); // returned data
32 // We expect to get four longs back: x, y, width, height.
33 if (data_len < static_cast<gint>(4 * sizeof(glong))) {
39 glong* data = reinterpret_cast<glong*>(raw_data);
43 gint height = data[3];
46 out_rect->SetRect(x, y, width, height);
50 gfx::Display GetDisplayForMonitorNum(GdkScreen* screen, gint monitor_num) {
52 gdk_screen_get_monitor_geometry(screen, monitor_num, &bounds);
53 // Use |monitor_num| as display id.
54 gfx::Display display(monitor_num, gfx::Rect(bounds));
55 if (gdk_screen_get_primary_monitor(screen) == monitor_num) {
57 if (GetScreenWorkArea(&rect))
58 display.set_work_area(gfx::IntersectRects(rect, display.bounds()));
63 gfx::Display GetMonitorAreaNearestWindow(gfx::NativeView view) {
64 GdkScreen* screen = gdk_screen_get_default();
66 if (view && GTK_IS_WINDOW(view)) {
67 GtkWidget* top_level = gtk_widget_get_toplevel(view);
68 DCHECK(GTK_IS_WINDOW(top_level));
69 GtkWindow* window = GTK_WINDOW(top_level);
70 screen = gtk_window_get_screen(window);
71 monitor_num = gdk_screen_get_monitor_at_window(
73 gtk_widget_get_window(top_level));
75 return GetDisplayForMonitorNum(screen, monitor_num);
78 class ScreenGtk : public gfx::Screen {
83 virtual ~ScreenGtk() {
86 virtual bool IsDIPEnabled() OVERRIDE {
90 virtual gfx::Point GetCursorScreenPoint() OVERRIDE {
92 gdk_display_get_pointer(gdk_display_get_default(), NULL, &x, &y, NULL);
93 return gfx::Point(x, y);
96 // Returns the window under the cursor.
97 virtual gfx::NativeWindow GetWindowUnderCursor() OVERRIDE {
98 GdkWindow* window = gdk_window_at_pointer(NULL, NULL);
102 gpointer data = NULL;
103 gdk_window_get_user_data(window, &data);
104 GtkWidget* widget = reinterpret_cast<GtkWidget*>(data);
107 widget = gtk_widget_get_toplevel(widget);
108 return GTK_IS_WINDOW(widget) ? GTK_WINDOW(widget) : NULL;
111 virtual gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point)
117 // Returns the number of displays.
118 // Mirrored displays are excluded; this method is intended to return the
119 // number of distinct, usable displays.
120 virtual int GetNumDisplays() const OVERRIDE {
121 // This query is kinda bogus for Linux -- do we want number of X screens?
122 // The number of monitors Xinerama has? We'll just use whatever GDK uses.
123 GdkScreen* screen = gdk_screen_get_default();
124 return gdk_screen_get_n_monitors(screen);
127 virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE {
128 GdkScreen* screen = gdk_screen_get_default();
129 gint num_of_displays = gdk_screen_get_n_monitors(screen);
130 std::vector<gfx::Display> all_displays;
131 for (gint i = 0; i < num_of_displays; ++i)
132 all_displays.push_back(GetDisplayForMonitorNum(screen, i));
136 // Returns the display nearest the specified window.
137 virtual gfx::Display GetDisplayNearestWindow(
138 gfx::NativeView view) const OVERRIDE {
139 // Do not use the _NET_WORKAREA here, this is supposed to be an area on a
140 // specific monitor, and _NET_WORKAREA is a hint from the WM that
141 // generally spans across all monitors. This would make the work area
142 // larger than the monitor.
143 // TODO(danakj) This is a work-around as there is no standard way to get
144 // this area, but it is a rect that we should be computing. The standard
145 // means to compute this rect would be to watch all windows with
146 // _NET_WM_STRUT(_PARTIAL) hints, and subtract their space from the
147 // physical area of the display to construct a work area.
148 // TODO(oshima): Implement Observer.
149 return GetMonitorAreaNearestWindow(view);
152 // Returns the the display nearest the specified point.
153 virtual gfx::Display GetDisplayNearestPoint(
154 const gfx::Point& point) const OVERRIDE {
155 GdkScreen* screen = gdk_screen_get_default();
156 gint monitor = gdk_screen_get_monitor_at_point(
157 screen, point.x(), point.y());
158 // TODO(oshima): Implement Observer.
159 return GetDisplayForMonitorNum(screen, monitor);
162 // Returns the display that most closely intersects the provided bounds.
163 virtual gfx::Display GetDisplayMatching(
164 const gfx::Rect& match_rect) const OVERRIDE {
165 std::vector<gfx::Display> displays = GetAllDisplays();
166 gfx::Display maxIntersectDisplay;
167 gfx::Rect maxIntersection;
168 for (std::vector<gfx::Display>::iterator it = displays.begin();
169 it != displays.end(); ++it) {
170 gfx::Rect displayIntersection = it->bounds();
171 displayIntersection.Intersect(match_rect);
172 if (displayIntersection.size().GetArea() >
173 maxIntersection.size().GetArea()) {
174 maxIntersectDisplay = *it;
175 maxIntersection = displayIntersection;
178 return maxIntersectDisplay.is_valid() ?
179 maxIntersectDisplay : GetPrimaryDisplay();
182 // Returns the primary display.
183 virtual gfx::Display GetPrimaryDisplay() const OVERRIDE {
184 GdkScreen* screen = gdk_screen_get_default();
185 gint primary_monitor_index = gdk_screen_get_primary_monitor(screen);
186 // TODO(oshima): Implement Observer.
187 return GetDisplayForMonitorNum(screen, primary_monitor_index);
190 virtual void AddObserver(gfx::DisplayObserver* observer) OVERRIDE {
191 // TODO(oshima): crbug.com/122863.
194 virtual void RemoveObserver(gfx::DisplayObserver* observer) OVERRIDE {
195 // TODO(oshima): crbug.com/122863.
199 DISALLOW_COPY_AND_ASSIGN(ScreenGtk);
206 Screen* CreateNativeScreen() {
207 return new ScreenGtk;