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.
5 #include "chrome/browser/fullscreen.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"
15 void EnumerateAllChildWindows(ui::EnumerateWindowsDelegate* delegate,
17 std::vector<XID> windows;
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);
27 for (long i = static_cast<long>(num_children) - 1; i >= 0; i--)
28 windows.push_back(children[i]);
33 std::vector<XID>::iterator iter;
34 for (iter = windows.begin(); iter != windows.end(); iter++) {
35 if (delegate->ShouldStopIterating(*iter))
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.
51 class WindowManagerWindowFinder : public ui::EnumerateWindowsDelegate {
53 WindowManagerWindowFinder() : window_(None) { }
55 XID window() const { return window_; }
58 virtual bool ShouldStopIterating(XID window) OVERRIDE {
59 if (ui::PropertyExists(window, "WM_STATE")) {
69 DISALLOW_COPY_AND_ASSIGN(WindowManagerWindowFinder);
72 class TopMostWindowFinder : public ui::EnumerateWindowsDelegate {
75 : top_most_window_(None) {}
77 XID top_most_window() const { return top_most_window_; }
80 virtual bool ShouldStopIterating(XID window) OVERRIDE {
81 if (!ui::IsWindowVisible(window))
83 if (ui::PropertyExists(window, "WM_STATE")) {
84 top_most_window_ = window;
88 WindowManagerWindowFinder child_finder;
89 EnumerateAllChildWindows(&child_finder, window);
90 XID child_window = child_finder.window();
91 if (child_window == None)
93 top_most_window_ = child_window;
100 DISALLOW_COPY_AND_ASSIGN(TopMostWindowFinder);
103 bool IsTopMostWindowFullScreen() {
104 // Find the topmost window
105 TopMostWindowFinder finder;
106 EnumerateAllChildWindows(&finder, ui::GetX11RootWindow());
107 XID window = finder.top_most_window();
111 // Make sure it is not the desktop window.
113 if (!ui::GetWindowDesktop(window, &window_desktop))
116 return ui::IsX11WindowFullScreen(window);
121 bool IsFullScreenMode() {
122 return IsTopMostWindowFullScreen();