blob: 1813d9ab560475fb16c0b3cd806de22be3a7aff9 [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/base/idle/screensaver_window_finder_x11.h"
#include "base/command_line.h"
#include "ui/base/x/x11_util.h"
#include "ui/gfx/switches.h"
#include "ui/gfx/x/connection.h"
#include "ui/gfx/x/screensaver.h"
#include "ui/gfx/x/x11.h"
#include "ui/gfx/x/x11_atom_cache.h"
#include "ui/gfx/x/x11_error_tracker.h"
namespace ui {
ScreensaverWindowFinder::ScreensaverWindowFinder() : exists_(false) {}
bool ScreensaverWindowFinder::ScreensaverWindowExists() {
// Avoid calling into potentially missing X11 APIs in headless mode.
if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kHeadless))
return false;
auto* connection = x11::Connection::Get();
// Let the server know the client version before making any requests.
connection->screensaver().QueryVersion(
{x11::ScreenSaver::major_version, x11::ScreenSaver::minor_version});
auto reply =
connection->screensaver().QueryInfo({connection->default_root()}).Sync();
if (reply && static_cast<x11::ScreenSaver::State>(reply->state) ==
x11::ScreenSaver::State::On) {
return true;
}
// Ironically, xscreensaver does not conform to the XScreenSaver protocol, so
// info.state == ScreenSaverOff or info.state == ScreenSaverDisabled does not
// necessarily mean that a screensaver is not active, so add a special check
// for xscreensaver.
x11::Atom lock_atom = gfx::GetAtom("LOCK");
std::vector<int> atom_properties;
if (GetIntArrayProperty(GetX11RootWindow(), "_SCREENSAVER_STATUS",
&atom_properties) &&
atom_properties.size() > 0) {
if (atom_properties[0] == static_cast<int>(lock_atom)) {
return true;
}
}
// Also check the top level windows to see if any of them are screensavers.
gfx::X11ErrorTracker err_tracker;
ScreensaverWindowFinder finder;
ui::EnumerateTopLevelWindows(&finder);
return finder.exists_ && !err_tracker.FoundNewError();
}
bool ScreensaverWindowFinder::ShouldStopIterating(x11::Window window) {
if (!ui::IsWindowVisible(window) || !IsScreensaverWindow(window))
return false;
exists_ = true;
return true;
}
bool ScreensaverWindowFinder::IsScreensaverWindow(x11::Window window) const {
// It should occupy the full screen.
if (!ui::IsX11WindowFullScreen(window))
return false;
// For xscreensaver, the window should have _SCREENSAVER_VERSION property.
if (ui::PropertyExists(window, "_SCREENSAVER_VERSION"))
return true;
// For all others, like gnome-screensaver, the window's WM_CLASS property
// should contain "screensaver".
std::string value;
if (!ui::GetStringProperty(window, "WM_CLASS", &value))
return false;
return value.find("screensaver") != std::string::npos;
}
} // namespace ui