| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ui/gfx/screen.h" | 5 #include "ui/gfx/screen.h" |
| 6 | 6 |
| 7 #include <gdk/gdkx.h> | 7 #include <gdk/gdkx.h> |
| 8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "ui/gfx/monitor.h" |
| 11 | 12 |
| 12 namespace { | 13 namespace { |
| 13 | 14 |
| 14 bool GetScreenWorkArea(gfx::Rect* out_rect) { | 15 bool GetScreenWorkArea(gfx::Rect* out_rect) { |
| 15 gboolean ok; | 16 gboolean ok; |
| 16 guchar* raw_data = NULL; | 17 guchar* raw_data = NULL; |
| 17 gint data_len = 0; | 18 gint data_len = 0; |
| 18 ok = gdk_property_get(gdk_get_default_root_window(), // a gdk window | 19 ok = gdk_property_get(gdk_get_default_root_window(), // a gdk window |
| 19 gdk_atom_intern("_NET_WORKAREA", FALSE), // property | 20 gdk_atom_intern("_NET_WORKAREA", FALSE), // property |
| 20 gdk_atom_intern("CARDINAL", FALSE), // property type | 21 gdk_atom_intern("CARDINAL", FALSE), // property type |
| (...skipping 18 matching lines...) Expand all Loading... |
| 39 gint x = data[0]; | 40 gint x = data[0]; |
| 40 gint y = data[1]; | 41 gint y = data[1]; |
| 41 gint width = data[2]; | 42 gint width = data[2]; |
| 42 gint height = data[3]; | 43 gint height = data[3]; |
| 43 g_free(raw_data); | 44 g_free(raw_data); |
| 44 | 45 |
| 45 out_rect->SetRect(x, y, width, height); | 46 out_rect->SetRect(x, y, width, height); |
| 46 return true; | 47 return true; |
| 47 } | 48 } |
| 48 | 49 |
| 49 } // namespace | 50 gfx::Rect NativePrimaryMonitorBounds() { |
| 50 | 51 GdkScreen* screen = gdk_screen_get_default(); |
| 51 namespace gfx { | 52 GdkRectangle rect; |
| 52 | 53 gdk_screen_get_monitor_geometry(screen, 0, &rect); |
| 53 // static | 54 return gfx::Rect(rect); |
| 54 gfx::Point Screen::GetCursorScreenPoint() { | |
| 55 gint x, y; | |
| 56 gdk_display_get_pointer(gdk_display_get_default(), NULL, &x, &y, NULL); | |
| 57 return gfx::Point(x, y); | |
| 58 } | 55 } |
| 59 | 56 |
| 60 // static | 57 gfx::Rect GetMonitorAreaNearestWindow(gfx::NativeView view) { |
| 61 gfx::Rect Screen::GetMonitorWorkAreaNearestWindow(gfx::NativeView view) { | |
| 62 // Do not use the _NET_WORKAREA here, this is supposed to be an area on a | |
| 63 // specific monitor, and _NET_WORKAREA is a hint from the WM that generally | |
| 64 // spans across all monitors. This would make the work area larger than the | |
| 65 // monitor. | |
| 66 // TODO(danakj) This is a work-around as there is no standard way to get this | |
| 67 // area, but it is a rect that we should be computing. The standard means | |
| 68 // to compute this rect would be to watch all windows with | |
| 69 // _NET_WM_STRUT(_PARTIAL) hints, and subtract their space from the physical | |
| 70 // area of the monitor to construct a work area. | |
| 71 return GetMonitorAreaNearestWindow(view); | |
| 72 } | |
| 73 | |
| 74 // static | |
| 75 gfx::Rect Screen::GetMonitorAreaNearestWindow(gfx::NativeView view) { | |
| 76 GdkScreen* screen = gdk_screen_get_default(); | 58 GdkScreen* screen = gdk_screen_get_default(); |
| 77 gint monitor_num = 0; | 59 gint monitor_num = 0; |
| 78 if (view) { | 60 if (view) { |
| 79 GtkWidget* top_level = gtk_widget_get_toplevel(view); | 61 GtkWidget* top_level = gtk_widget_get_toplevel(view); |
| 80 DCHECK(GTK_IS_WINDOW(top_level)); | 62 DCHECK(GTK_IS_WINDOW(top_level)); |
| 81 GtkWindow* window = GTK_WINDOW(top_level); | 63 GtkWindow* window = GTK_WINDOW(top_level); |
| 82 screen = gtk_window_get_screen(window); | 64 screen = gtk_window_get_screen(window); |
| 83 monitor_num = gdk_screen_get_monitor_at_window( | 65 monitor_num = gdk_screen_get_monitor_at_window( |
| 84 screen, | 66 screen, |
| 85 gtk_widget_get_window(top_level)); | 67 gtk_widget_get_window(top_level)); |
| 86 } | 68 } |
| 87 GdkRectangle bounds; | 69 GdkRectangle bounds; |
| 88 gdk_screen_get_monitor_geometry(screen, monitor_num, &bounds); | 70 gdk_screen_get_monitor_geometry(screen, monitor_num, &bounds); |
| 89 return gfx::Rect(bounds); | 71 return gfx::Rect(bounds); |
| 90 } | 72 } |
| 91 | 73 |
| 74 } // namespace |
| 75 |
| 76 namespace gfx { |
| 77 |
| 92 // static | 78 // static |
| 93 gfx::Rect Screen::GetMonitorWorkAreaNearestPoint(const gfx::Point& point) { | 79 gfx::Point Screen::GetCursorScreenPoint() { |
| 94 // TODO(jamiewalch): Restrict this to the work area of the monitor. | 80 gint x, y; |
| 95 return GetMonitorAreaNearestPoint(point); | 81 gdk_display_get_pointer(gdk_display_get_default(), NULL, &x, &y, NULL); |
| 82 return gfx::Point(x, y); |
| 96 } | 83 } |
| 97 | 84 |
| 98 // static | 85 // static |
| 99 gfx::Rect Screen::GetMonitorAreaNearestPoint(const gfx::Point& point) { | |
| 100 GdkScreen* screen = gdk_screen_get_default(); | |
| 101 gint monitor = gdk_screen_get_monitor_at_point(screen, point.x(), point.y()); | |
| 102 GdkRectangle bounds; | |
| 103 gdk_screen_get_monitor_geometry(screen, monitor, &bounds); | |
| 104 return gfx::Rect(bounds); | |
| 105 } | |
| 106 | |
| 107 // static | |
| 108 gfx::Rect Screen::GetPrimaryMonitorWorkArea() { | |
| 109 gfx::Rect rect; | |
| 110 if (GetScreenWorkArea(&rect)) | |
| 111 return rect.Intersect(GetPrimaryMonitorBounds()); | |
| 112 | |
| 113 // Return the best we've got. | |
| 114 return GetPrimaryMonitorBounds(); | |
| 115 } | |
| 116 | |
| 117 // static | |
| 118 gfx::Rect Screen::GetPrimaryMonitorBounds() { | |
| 119 GdkScreen* screen = gdk_screen_get_default(); | |
| 120 GdkRectangle rect; | |
| 121 gdk_screen_get_monitor_geometry(screen, 0, &rect); | |
| 122 return gfx::Rect(rect); | |
| 123 } | |
| 124 | |
| 125 // static | |
| 126 gfx::Rect Screen::GetMonitorWorkAreaMatching(const gfx::Rect& match_rect) { | |
| 127 // TODO(thestig) Implement multi-monitor support. | |
| 128 return GetPrimaryMonitorWorkArea(); | |
| 129 } | |
| 130 | |
| 131 // static | |
| 132 gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() { | 86 gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() { |
| 133 GdkWindow* window = gdk_window_at_pointer(NULL, NULL); | 87 GdkWindow* window = gdk_window_at_pointer(NULL, NULL); |
| 134 if (!window) | 88 if (!window) |
| 135 return NULL; | 89 return NULL; |
| 136 | 90 |
| 137 gpointer data = NULL; | 91 gpointer data = NULL; |
| 138 gdk_window_get_user_data(window, &data); | 92 gdk_window_get_user_data(window, &data); |
| 139 GtkWidget* widget = reinterpret_cast<GtkWidget*>(data); | 93 GtkWidget* widget = reinterpret_cast<GtkWidget*>(data); |
| 140 if (!widget) | 94 if (!widget) |
| 141 return NULL; | 95 return NULL; |
| 142 widget = gtk_widget_get_toplevel(widget); | 96 widget = gtk_widget_get_toplevel(widget); |
| 143 return GTK_IS_WINDOW(widget) ? GTK_WINDOW(widget) : NULL; | 97 return GTK_IS_WINDOW(widget) ? GTK_WINDOW(widget) : NULL; |
| 144 } | 98 } |
| 145 | 99 |
| 146 // static | 100 // static |
| 147 gfx::Size Screen::GetPrimaryMonitorSize() { | 101 void Screen::GetMonitorNearestWindow(gfx::NativeView view, |
| 148 GdkScreen* screen = gdk_screen_get_default(); | 102 gfx::Monitor* monitor_out) { |
| 149 return gfx::Size(gdk_screen_get_width(screen), gdk_screen_get_height(screen)); | 103 gfx::Rect bounds = GetMonitorAreaNearestWindow(view); |
| 104 monitor_out->set_bounds(bounds); |
| 105 // Do not use the _NET_WORKAREA here, this is supposed to be an area on a |
| 106 // specific monitor, and _NET_WORKAREA is a hint from the WM that generally |
| 107 // spans across all monitors. This would make the work area larger than the |
| 108 // monitor. |
| 109 // TODO(danakj) This is a work-around as there is no standard way to get this |
| 110 // area, but it is a rect that we should be computing. The standard means |
| 111 // to compute this rect would be to watch all windows with |
| 112 // _NET_WM_STRUT(_PARTIAL) hints, and subtract their space from the physical |
| 113 // area of the monitor to construct a work area. |
| 114 monitor_out->set_work_area(bounds); |
| 150 } | 115 } |
| 151 | 116 |
| 152 // static | 117 // static |
| 118 void Screen::GetMonitorNearestPoint(const gfx::Point& point, |
| 119 gfx::Monitor* monitor_out) { |
| 120 GdkScreen* screen = gdk_screen_get_default(); |
| 121 gint monitor = gdk_screen_get_monitor_at_point(screen, point.x(), point.y()); |
| 122 GdkRectangle bounds; |
| 123 gdk_screen_get_monitor_geometry(screen, monitor, &bounds); |
| 124 |
| 125 monitor_out->set_bounds(gfx::Rect(bounds)); |
| 126 // TODO(jamiewalch): Restrict this to the work area of the monitor. |
| 127 monitor_out->set_work_area(gfx::Rect(bounds)); |
| 128 } |
| 129 |
| 130 // static |
| 131 void Screen::GetPrimaryMonitor(gfx::Monitor* monitor_out) { |
| 132 gfx::Rect bounds = NativePrimaryMonitorBounds(); |
| 133 monitor_out->set_bounds(bounds); |
| 134 gfx::Rect rect; |
| 135 if (GetScreenWorkArea(&rect)) { |
| 136 monitor_out->set_work_area(rect.Intersect(bounds)); |
| 137 } else { |
| 138 // Return the best we've got. |
| 139 monitor_out->set_work_area(bounds); |
| 140 } |
| 141 } |
| 142 |
| 143 // static |
| 144 void Screen::GetMonitorMatching(const gfx::Rect& match_rect, |
| 145 gfx::Monitor* monitor_out) { |
| 146 // TODO(thestig) Implement multi-monitor support. |
| 147 GetPrimaryMonitor(monitor_out); |
| 148 } |
| 149 |
| 150 // static |
| 153 int Screen::GetNumMonitors() { | 151 int Screen::GetNumMonitors() { |
| 154 // This query is kinda bogus for Linux -- do we want number of X screens? | 152 // This query is kinda bogus for Linux -- do we want number of X screens? |
| 155 // The number of monitors Xinerama has? We'll just use whatever GDK uses. | 153 // The number of monitors Xinerama has? We'll just use whatever GDK uses. |
| 156 GdkScreen* screen = gdk_screen_get_default(); | 154 GdkScreen* screen = gdk_screen_get_default(); |
| 157 return gdk_screen_get_n_monitors(screen); | 155 return gdk_screen_get_n_monitors(screen); |
| 158 } | 156 } |
| 159 | 157 |
| 160 } // namespace gfx | 158 } // namespace gfx |
| OLD | NEW |