| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 | 11 |
| 12 namespace { |
| 13 |
| 14 bool GetScreenWorkArea(gfx::Rect* out_rect) { |
| 15 gboolean ok; |
| 16 guchar* raw_data = NULL; |
| 17 gint data_len = 0; |
| 18 ok = gdk_property_get(gdk_get_default_root_window(), // a gdk window |
| 19 gdk_atom_intern("_NET_WORKAREA", FALSE), // property |
| 20 gdk_atom_intern("CARDINAL", FALSE), // property type |
| 21 0, // byte offset into property |
| 22 0xff, // property length to retrieve |
| 23 false, // delete property after retrieval? |
| 24 NULL, // returned property type |
| 25 NULL, // returned data format |
| 26 &data_len, // returned data len |
| 27 &raw_data); // returned data |
| 28 if (!ok) |
| 29 return false; |
| 30 |
| 31 // We expect to get four longs back: x, y, width, height. |
| 32 if (data_len < static_cast<gint>(4 * sizeof(glong))) { |
| 33 NOTREACHED(); |
| 34 g_free(raw_data); |
| 35 return false; |
| 36 } |
| 37 |
| 38 glong* data = reinterpret_cast<glong*>(raw_data); |
| 39 gint x = data[0]; |
| 40 gint y = data[1]; |
| 41 gint width = data[2]; |
| 42 gint height = data[3]; |
| 43 g_free(raw_data); |
| 44 |
| 45 out_rect->SetRect(x, y, width, height); |
| 46 return true; |
| 47 } |
| 48 |
| 49 } // namespace |
| 50 |
| 12 namespace gfx { | 51 namespace gfx { |
| 13 | 52 |
| 14 // static | 53 // static |
| 15 gfx::Point Screen::GetCursorScreenPoint() { | 54 gfx::Point Screen::GetCursorScreenPoint() { |
| 16 gint x, y; | 55 gint x, y; |
| 17 gdk_display_get_pointer(gdk_display_get_default(), NULL, &x, &y, NULL); | 56 gdk_display_get_pointer(gdk_display_get_default(), NULL, &x, &y, NULL); |
| 18 return gfx::Point(x, y); | 57 return gfx::Point(x, y); |
| 19 } | 58 } |
| 20 | 59 |
| 21 // static | 60 // static |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 // static | 96 // static |
| 58 gfx::Rect Screen::GetMonitorAreaNearestPoint(const gfx::Point& point) { | 97 gfx::Rect Screen::GetMonitorAreaNearestPoint(const gfx::Point& point) { |
| 59 GdkScreen* screen = gdk_screen_get_default(); | 98 GdkScreen* screen = gdk_screen_get_default(); |
| 60 gint monitor = gdk_screen_get_monitor_at_point(screen, point.x(), point.y()); | 99 gint monitor = gdk_screen_get_monitor_at_point(screen, point.x(), point.y()); |
| 61 GdkRectangle bounds; | 100 GdkRectangle bounds; |
| 62 gdk_screen_get_monitor_geometry(screen, monitor, &bounds); | 101 gdk_screen_get_monitor_geometry(screen, monitor, &bounds); |
| 63 return gfx::Rect(bounds); | 102 return gfx::Rect(bounds); |
| 64 } | 103 } |
| 65 | 104 |
| 66 // static | 105 // static |
| 106 gfx::Rect Screen::GetPrimaryMonitorWorkArea() { |
| 107 gfx::Rect rect; |
| 108 if (GetScreenWorkArea(&rect)) |
| 109 return rect.Intersect(GetPrimaryMonitorBounds()); |
| 110 |
| 111 // Return the best we've got. |
| 112 return GetPrimaryMonitorBounds(); |
| 113 } |
| 114 |
| 115 // static |
| 116 gfx::Rect Screen::GetPrimaryMonitorBounds() { |
| 117 GdkScreen* screen = gdk_screen_get_default(); |
| 118 GdkRectangle rect; |
| 119 gdk_screen_get_monitor_geometry(screen, 0, &rect); |
| 120 return gfx::Rect(rect); |
| 121 } |
| 122 |
| 123 // static |
| 124 gfx::Rect Screen::GetMonitorWorkAreaMatching(const gfx::Rect& match_rect) { |
| 125 // TODO(thestig) Implement multi-monitor support. |
| 126 return GetPrimaryMonitorWorkArea(); |
| 127 } |
| 128 |
| 129 // static |
| 67 gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() { | 130 gfx::NativeWindow Screen::GetWindowAtCursorScreenPoint() { |
| 68 GdkWindow* window = gdk_window_at_pointer(NULL, NULL); | 131 GdkWindow* window = gdk_window_at_pointer(NULL, NULL); |
| 69 if (!window) | 132 if (!window) |
| 70 return NULL; | 133 return NULL; |
| 71 | 134 |
| 72 gpointer data = NULL; | 135 gpointer data = NULL; |
| 73 gdk_window_get_user_data(window, &data); | 136 gdk_window_get_user_data(window, &data); |
| 74 GtkWidget* widget = reinterpret_cast<GtkWidget*>(data); | 137 GtkWidget* widget = reinterpret_cast<GtkWidget*>(data); |
| 75 if (!widget) | 138 if (!widget) |
| 76 return NULL; | 139 return NULL; |
| 77 widget = gtk_widget_get_toplevel(widget); | 140 widget = gtk_widget_get_toplevel(widget); |
| 78 return GTK_IS_WINDOW(widget) ? GTK_WINDOW(widget) : NULL; | 141 return GTK_IS_WINDOW(widget) ? GTK_WINDOW(widget) : NULL; |
| 79 } | 142 } |
| 80 | 143 |
| 81 // static | 144 // static |
| 82 gfx::Size Screen::GetPrimaryMonitorSize() { | 145 gfx::Size Screen::GetPrimaryMonitorSize() { |
| 83 GdkScreen* screen = gdk_screen_get_default(); | 146 GdkScreen* screen = gdk_screen_get_default(); |
| 84 return gfx::Size(gdk_screen_get_width(screen), gdk_screen_get_height(screen)); | 147 return gfx::Size(gdk_screen_get_width(screen), gdk_screen_get_height(screen)); |
| 85 } | 148 } |
| 86 | 149 |
| 87 // static | 150 // static |
| 88 int Screen::GetNumMonitors() { | 151 int Screen::GetNumMonitors() { |
| 89 // 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? |
| 90 // 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. |
| 91 GdkScreen* screen = gdk_screen_get_default(); | 154 GdkScreen* screen = gdk_screen_get_default(); |
| 92 return gdk_screen_get_n_monitors(screen); | 155 return gdk_screen_get_n_monitors(screen); |
| 93 } | 156 } |
| 94 | 157 |
| 95 } // namespace gfx | 158 } // namespace gfx |
| OLD | NEW |