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