Commit 3062e1be authored by Alexander Dunaev's avatar Alexander Dunaev Committed by Commit Bot

[ozone/x11] Implemented the power save blocker.

Blocking the power save mode has platform-specific bits.  Ozone didn't
have any support for that.

This CL introduces the concept of a power save blocker for Ozone, and
implements the X11-specific part of it.

Bug: 1121926
Change-Id: Ic60210b55808cb30e27b0811e5fade69ac01512e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2378071
Commit-Queue: Alexander Dunaev <adunaev@igalia.com>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarRobert Kroeger <rjkroege@chromium.org>
Reviewed-by: default avatarThomas Anderson <thomasanderson@chromium.org>
Cr-Commit-Position: refs/heads/master@{#812580}
parent 10f6d504
......@@ -49,9 +49,13 @@ source_set("power_save_blocker") {
if (use_x11) {
deps += [
"//ui/base:features",
"//ui/base/x:x",
"//ui/gfx/x",
]
}
if (use_ozone) {
deps += [ "//ui/display:display" ]
}
} else if (is_mac) {
sources += [ "power_save_blocker_mac.cc" ]
frameworks = [
......
......@@ -5,4 +5,6 @@ include_rules = [
"+ui/android",
"+ui/gfx",
"+ui/base/ui_base_features.h",
"+ui/base/x",
"+ui/display",
]
......@@ -25,13 +25,21 @@
#include "dbus/object_proxy.h"
#include "ui/gfx/switches.h"
#if defined(USE_X11)
#if defined(USE_X11) || defined(USE_OZONE)
#include "ui/base/ui_base_features.h" // nogncheck
#endif
#if defined(USE_X11)
#include "ui/base/x/x11_util.h" // nogncheck
#include "ui/gfx/x/connection.h" // nogncheck
#include "ui/gfx/x/screensaver.h" // nogncheck
#include "ui/gfx/x/x11_types.h" // nogncheck
#endif
#if defined(USE_OZONE)
#include "ui/display/screen.h"
#endif
namespace device {
namespace {
......@@ -132,36 +140,21 @@ void GetDbusStringsForApi(DBusAPI api,
NOTREACHED();
}
#if defined(USE_X11)
// Check whether the X11 Screen Saver Extension can be used to disable the
// screen saver. Must be called on the UI thread.
bool X11ScreenSaverAvailable() {
// X Screen Saver isn't accessible in headless mode.
if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kHeadless))
return false;
auto* connection = x11::Connection::Get();
auto version = connection->screensaver()
.QueryVersion({x11::ScreenSaver::major_version,
x11::ScreenSaver::minor_version})
.Sync();
return version && (version->server_major_version > 1 ||
(version->server_major_version == 1 &&
version->server_minor_version >= 1));
}
// Wrapper for XScreenSaverSuspend. Checks whether the X11 Screen Saver
// Extension is available first. If it isn't, this is a no-op. Must be called
// on the UI thread.
void X11ScreenSaverSuspendSet(bool suspend) {
if (!X11ScreenSaverAvailable())
void SetScreenSaverSuspended(bool suspend) {
#if defined(USE_OZONE)
if (features::IsUsingOzonePlatform()) {
auto* const screen = display::Screen::GetScreen();
// The screen can be nullptr in tests.
if (!screen)
return;
screen->SetScreenSaverSuspended(suspend);
return;
auto* connection = x11::Connection::Get();
connection->screensaver().Suspend({suspend});
}
}
#endif
#if defined(USE_X11)
ui::SuspendX11ScreenSaver(suspend);
#endif
}
} // namespace
......@@ -242,12 +235,8 @@ void PowerSaveBlocker::Delegate::Init() {
FROM_HERE, base::BindOnce(&Delegate::ApplyBlock, this));
}
#if defined(USE_X11)
if (!features::IsUsingOzonePlatform()) {
ui_task_runner_->PostTask(FROM_HERE,
base::BindOnce(X11ScreenSaverSuspendSet, true));
}
#endif
ui_task_runner_->PostTask(FROM_HERE,
base::BindOnce(SetScreenSaverSuspended, true));
}
void PowerSaveBlocker::Delegate::CleanUp() {
......@@ -256,12 +245,8 @@ void PowerSaveBlocker::Delegate::CleanUp() {
FROM_HERE, base::BindOnce(&Delegate::RemoveBlock, this));
}
#if defined(USE_X11)
if (!features::IsUsingOzonePlatform()) {
ui_task_runner_->PostTask(FROM_HERE,
base::BindOnce(X11ScreenSaverSuspendSet, false));
}
#endif
ui_task_runner_->PostTask(FROM_HERE,
base::BindOnce(SetScreenSaverSuspended, false));
}
bool PowerSaveBlocker::Delegate::ShouldBlock() const {
......
......@@ -85,6 +85,10 @@ display::Display ScreenOzone::GetPrimaryDisplay() const {
return platform_screen_->GetPrimaryDisplay();
}
void ScreenOzone::SetScreenSaverSuspended(bool suspend) {
platform_screen_->SetScreenSaverSuspended(suspend);
}
void ScreenOzone::AddObserver(display::DisplayObserver* observer) {
platform_screen_->AddObserver(observer);
}
......
......@@ -41,6 +41,7 @@ class AURA_EXPORT ScreenOzone : public display::Screen {
display::Display GetDisplayMatching(
const gfx::Rect& match_rect) const override;
display::Display GetPrimaryDisplay() const override;
void SetScreenSaverSuspended(bool suspend) override;
void AddObserver(display::DisplayObserver* observer) override;
void RemoveObserver(display::DisplayObserver* observer) override;
std::string GetCurrentWorkspace() override;
......
......@@ -64,6 +64,7 @@
#include "ui/gfx/skia_util.h"
#include "ui/gfx/switches.h"
#include "ui/gfx/x/connection.h"
#include "ui/gfx/x/screensaver.h"
#include "ui/gfx/x/shm.h"
#include "ui/gfx/x/sync.h"
#include "ui/gfx/x/x11.h"
......@@ -169,6 +170,24 @@ bool GetWindowManagerName(std::string* wm_name) {
return !err_tracker.FoundNewError() && result;
}
// Returns whether the X11 Screen Saver Extension can be used to disable the
// screen saver.
bool IsX11ScreenSaverAvailable() {
// X Screen Saver isn't accessible in headless mode.
if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kHeadless))
return false;
auto version = x11::Connection::Get()
->screensaver()
.QueryVersion({x11::ScreenSaver::major_version,
x11::ScreenSaver::minor_version})
.Sync();
return version && (version->server_major_version > 1 ||
(version->server_major_version == 1 &&
version->server_minor_version >= 1));
}
} // namespace
void DeleteProperty(x11::Window window, x11::Atom name) {
......@@ -1021,6 +1040,14 @@ bool IsX11WindowFullScreen(x11::Window window) {
return window_rect.size() == gfx::Size(width, height);
}
void SuspendX11ScreenSaver(bool suspend) {
static const bool kScreenSaverAvailable = IsX11ScreenSaverAvailable();
if (!kScreenSaverAvailable)
return;
x11::Connection::Get()->screensaver().Suspend({suspend});
}
bool WmSupportsHint(x11::Atom atom) {
if (!SupportsEWMH())
return false;
......
......@@ -523,6 +523,9 @@ COMPONENT_EXPORT(UI_BASE_X) void SetDefaultX11ErrorHandlers();
// Returns true if a given window is in full-screen mode.
COMPONENT_EXPORT(UI_BASE_X) bool IsX11WindowFullScreen(x11::Window window);
// Suspends or resumes the X screen saver. Must be called on the UI thread.
COMPONENT_EXPORT(UI_BASE_X) void SuspendX11ScreenSaver(bool suspend);
// Returns true if the window manager supports the given hint.
COMPONENT_EXPORT(UI_BASE_X) bool WmSupportsHint(x11::Atom atom);
......
......@@ -63,6 +63,10 @@ void Screen::SetDisplayForNewWindows(int64_t display_id) {
display_id_for_new_windows_ = display_id;
}
void Screen::SetScreenSaverSuspended(bool suspend) {
NOTIMPLEMENTED_LOG_ONCE();
}
gfx::Rect Screen::ScreenToDIPRectInWindow(gfx::NativeWindow window,
const gfx::Rect& screen_rect) const {
float scale = GetDisplayNearestWindow(window).device_scale_factor();
......
......@@ -96,6 +96,9 @@ class DISPLAY_EXPORT Screen {
// Sets the suggested display to use when creating a new window.
void SetDisplayForNewWindows(int64_t display_id);
// Suspends the platform-specific screensaver, if applicable.
virtual void SetScreenSaverSuspended(bool suspend);
// Adds/Removes display observers.
virtual void AddObserver(DisplayObserver* observer) = 0;
virtual void RemoveObserver(DisplayObserver* observer) = 0;
......
......@@ -120,6 +120,10 @@ display::Display X11ScreenOzone::GetDisplayMatching(
return matching_display ? *matching_display : GetPrimaryDisplay();
}
void X11ScreenOzone::SetScreenSaverSuspended(bool suspend) {
SuspendX11ScreenSaver(suspend);
}
void X11ScreenOzone::AddObserver(display::DisplayObserver* observer) {
x11_display_manager_->AddObserver(observer);
}
......
......@@ -47,6 +47,7 @@ class X11ScreenOzone : public PlatformScreen,
const gfx::Point& point) const override;
display::Display GetDisplayMatching(
const gfx::Rect& match_rect_in_pixels) const override;
void SetScreenSaverSuspended(bool suspend) override;
void AddObserver(display::DisplayObserver* observer) override;
void RemoveObserver(display::DisplayObserver* observer) override;
std::string GetCurrentWorkspace() override;
......
......@@ -23,4 +23,8 @@ std::string PlatformScreen::GetCurrentWorkspace() {
return {};
}
void PlatformScreen::SetScreenSaverSuspended(bool suspend) {
NOTIMPLEMENTED_LOG_ONCE();
}
} // namespace ui
......@@ -77,6 +77,9 @@ class COMPONENT_EXPORT(OZONE_BASE) PlatformScreen {
virtual display::Display GetDisplayMatching(
const gfx::Rect& match_rect) const = 0;
// Suspends the platform-specific screensaver, if applicable.
virtual void SetScreenSaverSuspended(bool suspend);
// Adds/Removes display observers.
virtual void AddObserver(display::DisplayObserver* observer) = 0;
virtual void RemoveObserver(display::DisplayObserver* observer) = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment