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 "content/browser/renderer_host/gtk_window_utils.h"
13 #include "third_party/WebKit/public/web/WebScreenInfo.h"
14 #include "ui/base/x/x11_util.h"
15 #include "ui/gfx/gtk_compat.h"
16 #include "ui/gfx/rect.h"
17 #include "ui/gfx/x/x11_types.h"
23 // Returns the region of |window|'s desktop that isn't occupied by docks or
24 // panels. Returns an empty rect on failure.
25 gfx::Rect GetWorkArea(Window window) {
26 Window root = ui::GetX11RootWindow();
28 if (!ui::GetIntProperty(window, "_NET_WM_DESKTOP", &desktop)) {
29 // Hack for ion3: _NET_WM_DESKTOP doesn't get set on individual windows, but
30 // _NET_CURRENT_DESKTOP and _NET_WORKAREA do get set.
31 ui::GetIntProperty(root, "_NET_CURRENT_DESKTOP", &desktop);
36 std::vector<int> property;
37 if (!ui::GetIntArrayProperty(root, "_NET_WORKAREA", &property))
40 size_t start_index = 4 * desktop;
41 if (property.size() < start_index + 4)
44 return gfx::Rect(property[start_index], property[start_index + 1],
45 property[start_index + 2], property[start_index + 3]);
48 WebKit::WebScreenInfo GetScreenInfo(XDisplay* display, int screenNumber) {
49 // XDisplayWidth() and XDisplayHeight() return cached values. To ensure that
50 // we return the correct dimensions after the screen is resized, query the
51 // root window's geometry each time.
52 Window root = RootWindow(display, screenNumber);
55 unsigned int width, height, border, depth;
57 display, root, &root_ret, &x, &y, &width, &height, &border, &depth);
59 WebKit::WebScreenInfo results;
60 results.depthPerComponent = 8; // Assume 8bpp, which is usually right.
61 results.depth = depth;
62 results.isMonochrome = depth == 1;
63 results.rect = WebKit::WebRect(x, y, width, height);
64 results.availableRect = results.rect;
70 void GetScreenInfoFromNativeWindow(
71 GdkWindow* gdk_window, WebKit::WebScreenInfo* results) {
72 GdkScreen* screen = gdk_window_get_screen(gdk_window);
73 *results = GetScreenInfo(gdk_x11_drawable_get_xdisplay(gdk_window),
74 gdk_x11_screen_get_screen_number(screen));
76 int monitor_number = gdk_screen_get_monitor_at_window(screen, gdk_window);
77 GdkRectangle monitor_rect;
78 gdk_screen_get_monitor_geometry(screen, monitor_number, &monitor_rect);
79 results->rect = WebKit::WebRect(monitor_rect.x, monitor_rect.y,
80 monitor_rect.width, monitor_rect.height);
82 gfx::Rect available_rect = results->rect;
83 gfx::Rect work_area = GetWorkArea(GDK_WINDOW_XID(gdk_window));
84 if (!work_area.IsEmpty())
85 available_rect.Intersect(work_area);
86 results->availableRect = available_rect;
89 } // namespace content