- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / gtk_window_utils.cc
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.
4
5 #include "content/browser/renderer_host/gtk_window_utils.h"
6
7 #include <X11/Xlib.h>
8 #include <gdk/gdkx.h>
9 #include <gtk/gtk.h>
10
11 #include <vector>
12
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"
18
19 namespace content {
20
21 namespace {
22
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();
27   int desktop = -1;
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);
32   }
33   if (desktop < 0)
34     return gfx::Rect();
35
36   std::vector<int> property;
37   if (!ui::GetIntArrayProperty(root, "_NET_WORKAREA", &property))
38     return gfx::Rect();
39
40   size_t start_index = 4 * desktop;
41   if (property.size() < start_index + 4)
42     return gfx::Rect();
43
44   return gfx::Rect(property[start_index], property[start_index + 1],
45                    property[start_index + 2], property[start_index + 3]);
46 }
47
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);
53   Window root_ret;
54   int x, y;
55   unsigned int width, height, border, depth;
56   XGetGeometry(
57       display, root, &root_ret, &x, &y, &width, &height, &border, &depth);
58
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;
65   return results;
66 }
67
68 }  // namespace
69
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));
75
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);
81
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;
87 }
88
89 }  // namespace content