Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / fullscreen_aurax11.cc
1 // Copyright 2014 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 "chrome/browser/fullscreen.h"
6
7 #include <X11/Xlib.h>
8 #include "base/basictypes.h"
9 #include "base/logging.h"
10 #include "ui/base/x/x11_util.h"
11 #include "ui/gfx/rect.h"
12
13 namespace {
14
15 void EnumerateAllChildWindows(ui::EnumerateWindowsDelegate* delegate,
16                               XID window) {
17   std::vector<XID> windows;
18
19   if (!ui::GetXWindowStack(window, &windows)) {
20     // Window Manager doesn't support _NET_CLIENT_LIST_STACKING, so fall back
21     // to old school enumeration of all X windows.
22     XID root, parent, *children;
23     unsigned int num_children;
24     int status = XQueryTree(gfx::GetXDisplay(), window, &root, &parent,
25                             &children, &num_children);
26     if (status) {
27       for (long i = static_cast<long>(num_children) - 1; i >= 0; i--)
28         windows.push_back(children[i]);
29       XFree(children);
30     }
31   }
32
33   std::vector<XID>::iterator iter;
34   for (iter = windows.begin(); iter != windows.end(); iter++) {
35     if (delegate->ShouldStopIterating(*iter))
36       return;
37   }
38 }
39
40 // To find the top-most window:
41 // 1) Enumerate all top-level windows from the top to the bottom.
42 // 2) For each window:
43 //    2.1) If it is hidden, continue the iteration.
44 //    2.2) If it is managed by the Window Manager (has a WM_STATE property).
45 //         Return this window as the top-most window.
46 //    2.3) Enumerate all its child windows. If there is a child window that is
47 //         managed by the Window Manager (has a WM_STATE property). Return this
48 //         child window as the top-most window.
49 //    2.4) Otherwise, continue the iteration.
50
51 class WindowManagerWindowFinder : public ui::EnumerateWindowsDelegate {
52  public:
53   WindowManagerWindowFinder() : window_(None) { }
54
55   XID window() const { return window_; }
56
57  protected:
58   virtual bool ShouldStopIterating(XID window) OVERRIDE {
59     if (ui::PropertyExists(window, "WM_STATE")) {
60       window_ = window;
61       return true;
62     }
63     return false;
64   }
65
66  private:
67   XID window_;
68
69   DISALLOW_COPY_AND_ASSIGN(WindowManagerWindowFinder);
70 };
71
72 class TopMostWindowFinder : public ui::EnumerateWindowsDelegate {
73  public:
74   TopMostWindowFinder()
75       : top_most_window_(None) {}
76
77   XID top_most_window() const { return top_most_window_; }
78
79  protected:
80   virtual bool ShouldStopIterating(XID window) OVERRIDE {
81      if (!ui::IsWindowVisible(window))
82        return false;
83      if (ui::PropertyExists(window, "WM_STATE")) {
84       top_most_window_ = window;
85       return true;
86      }
87
88      WindowManagerWindowFinder child_finder;
89      EnumerateAllChildWindows(&child_finder, window);
90      XID child_window = child_finder.window();
91      if (child_window == None)
92        return false;
93      top_most_window_ = child_window;
94      return true;
95   }
96
97  private:
98   XID top_most_window_;
99
100   DISALLOW_COPY_AND_ASSIGN(TopMostWindowFinder);
101 };
102
103 bool IsTopMostWindowFullScreen() {
104   // Find the topmost window
105   TopMostWindowFinder finder;
106   EnumerateAllChildWindows(&finder, ui::GetX11RootWindow());
107   XID window = finder.top_most_window();
108   if (window == None)
109     return false;
110
111   // Make sure it is not the desktop window.
112   int window_desktop;
113   if (!ui::GetWindowDesktop(window, &window_desktop))
114     return false;
115
116   return ui::IsX11WindowFullScreen(window);
117 }
118
119 }  // namespace
120
121 bool IsFullScreenMode() {
122   return IsTopMostWindowFullScreen();
123 }