From d821f6b0dc6a26054064e58ad893f7e98456f527 Mon Sep 17 00:00:00 2001 From: Marten Ringwelski Date: Tue, 27 Oct 2020 11:17:19 +0100 Subject: [PATCH] Implement wlr-output-power-management-unstable-v1 protocol --- build.zig | 2 + ...lr-output-power-management-unstable-v1.xml | 128 ++++++++++++++++++ river/Output.zig | 6 + river/Server.zig | 33 +++++ river/c.zig | 1 + 5 files changed, 170 insertions(+) create mode 100644 protocol/wlr-output-power-management-unstable-v1.xml diff --git a/build.zig b/build.zig index cca6b87..332f11c 100644 --- a/build.zig +++ b/build.zig @@ -153,6 +153,7 @@ const ScanProtocolsStep = struct { const protocol_dir_paths = [_][]const []const u8{ &[_][]const u8{ protocol_dir, "stable/xdg-shell/xdg-shell.xml" }, &[_][]const u8{ "protocol", "wlr-layer-shell-unstable-v1.xml" }, + &[_][]const u8{ "protocol", "wlr-output-power-management-unstable-v1.xml" }, &[_][]const u8{ "protocol", "river-control-unstable-v1.xml" }, &[_][]const u8{ "protocol", "river-status-unstable-v1.xml" }, }; @@ -160,6 +161,7 @@ const ScanProtocolsStep = struct { const server_protocols = [_][]const u8{ "xdg-shell", "wlr-layer-shell-unstable-v1", + "wlr-output-power-management-unstable-v1", "river-control-unstable-v1", "river-status-unstable-v1", }; diff --git a/protocol/wlr-output-power-management-unstable-v1.xml b/protocol/wlr-output-power-management-unstable-v1.xml new file mode 100644 index 0000000..20dbb77 --- /dev/null +++ b/protocol/wlr-output-power-management-unstable-v1.xml @@ -0,0 +1,128 @@ + + + + Copyright © 2019 Purism SPC + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice (including the next + paragraph) shall be included in all copies or substantial portions of the + Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. + + + + This protocol allows clients to control power management modes + of outputs that are currently part of the compositor space. The + intent is to allow special clients like desktop shells to power + down outputs when the system is idle. + + To modify outputs not currently part of the compositor space see + wlr-output-management. + + Warning! The protocol described in this file is experimental and + backward incompatible changes may be made. Backward compatible changes + may be added together with the corresponding interface version bump. + Backward incompatible changes are done by bumping the version number in + the protocol and interface names and resetting the interface version. + Once the protocol is to be declared stable, the 'z' prefix and the + version number in the protocol and interface names are removed and the + interface version number is reset. + + + + + This interface is a manager that allows creating per-output power + management mode controls. + + + + + Create an output power management mode control that can be used to + adjust the power management mode for a given output. + + + + + + + + All objects created by the manager will still remain valid, until their + appropriate destroy request has been called. + + + + + + + This object offers requests to set the power management mode of + an output. + + + + + + + + + + + + + + Set an output's power save mode to the given mode. The mode change + is effective immediately. If the output does not support the given + mode a failed event is sent. + + + + + + + Report the power management mode change of an output. + + The mode event is sent after an output changed its power + management mode. The reason can be a client using set_mode or the + compositor deciding to change an output's mode. + This event is also sent immediately when the object is created + so the client is informed about the current power management mode. + + + + + + + This event indicates that the output power management mode control + is no longer valid. This can happen for a number of reasons, + including: + - The output doesn't support power management + - Another client already has exclusive power management mode control + for this output + - The output disappeared + + Upon receiving this event, the client should destroy this object. + + + + + + Destroys the output power management mode control object. + + + + diff --git a/river/Output.zig b/river/Output.zig index 124d8b1..248d0fe 100644 --- a/river/Output.zig +++ b/river/Output.zig @@ -2,6 +2,7 @@ // // Copyright 2020 Isaac Freund // Copyright 2020 Leon Henrik Plickat +// Copyright 2020 Marten Ringwelski // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -600,6 +601,11 @@ fn handleFrame(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { // This function is called every time an output is ready to display a frame, // generally at the output's refresh rate (e.g. 60Hz). const self = @fieldParentPtr(Self, "listen_frame", listener.?); + + // TODO(wlroots) Remove this check when we update wlroots 0.11 to 0.12 + // wlroots fix: https://github.com/swaywm/wlroots/commit/85757665e6e1393773b36282aa244feb10b7a5fe + if (!self.wlr_output.enabled) return; + render.renderOutput(self); } diff --git a/river/Server.zig b/river/Server.zig index 31cd0e9..318e16f 100644 --- a/river/Server.zig +++ b/river/Server.zig @@ -51,6 +51,9 @@ listen_new_xdg_surface: c.wl_listener, wlr_layer_shell: *c.wlr_layer_shell_v1, listen_new_layer_surface: c.wl_listener, +wlr_output_power_manager: *c.wlr_output_power_manager_v1, +listen_output_power_manager_set_mode: c.wl_listener, + wlr_xwayland: if (build_options.xwayland) *c.wlr_xwayland else void, listen_new_xwayland_surface: if (build_options.xwayland) c.wl_listener else void, @@ -100,6 +103,11 @@ pub fn init(self: *Self) !void { const wlr_compositor = c.wlr_compositor_create(self.wl_display, wlr_renderer) orelse return error.OutOfMemory; + // Set up output power manager + self.wlr_output_power_manager = c.wlr_output_power_manager_v1_create(self.wl_display); + self.listen_output_power_manager_set_mode.notify = handleOutputPowerManagementSetMode; + c.wl_signal_add(&self.wlr_output_power_manager.events.set_mode, &self.listen_output_power_manager_set_mode); + // Set up xdg shell self.wlr_xdg_shell = c.wlr_xdg_shell_create(self.wl_display) orelse return error.OutOfMemory; self.listen_new_xdg_surface.notify = handleNewXdgSurface; @@ -284,3 +292,28 @@ fn handleNewXwaylandSurface(listener: ?*c.wl_listener, data: ?*c_void) callconv( const node = util.gpa.create(ViewStack(View).Node) catch return; node.view.init(output, output.current.tags, wlr_xwayland_surface); } + +fn handleOutputPowerManagementSetMode(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { + const self = @fieldParentPtr(Self, "listen_output_power_manager_set_mode", listener.?); + const mode_event = util.voidCast(c.wlr_output_power_v1_set_mode_event, data.?); + const wlr_output: *c.wlr_output = mode_event.output; + + const enable = mode_event.mode == .ZWLR_OUTPUT_POWER_V1_MODE_ON; + + const log_text = if (enable) "Enabling" else "Disabling"; + log.debug( + .server, + "{} dpms for output {}", + .{log_text, wlr_output.name}, + ); + + c.wlr_output_enable(wlr_output, enable); + if (!c.wlr_output_commit(wlr_output)) { + log.err( + .server, + "wlr_output_commit failed for {}", + .{wlr_output.name}, + ); + } + +} diff --git a/river/c.zig b/river/c.zig index eedb9d3..1a05c44 100644 --- a/river/c.zig +++ b/river/c.zig @@ -47,6 +47,7 @@ pub usingnamespace @cImport({ @cInclude("wlr/types/wlr_matrix.h"); @cInclude("wlr/types/wlr_output.h"); @cInclude("wlr/types/wlr_output_layout.h"); + @cInclude("wlr/types/wlr_output_power_management_v1.h"); @cInclude("wlr/types/wlr_pointer.h"); @cInclude("wlr/types/wlr_primary_selection.h"); @cInclude("wlr/types/wlr_primary_selection_v1.h");