Add power management state to the output object

Signed-off-by: Jim Ramsay <i.am@jimramsay.com>
pull/199/head
Jim Ramsay 2022-11-29 18:46:43 -05:00 committed by Andri Yngvason
parent 9639740bc8
commit 308308b63a
5 changed files with 239 additions and 5 deletions

View File

@ -21,10 +21,20 @@
#include <stdbool.h> #include <stdbool.h>
struct zxdg_output_v1; struct zxdg_output_v1;
struct zwlr_output_power_v1;
enum output_power_state {
OUTPUT_POWER_UNKNOWN = 0,
OUTPUT_POWER_OFF,
OUTPUT_POWER_ON,
};
const char* output_power_state_name(enum output_power_state state);
struct output { struct output {
struct wl_output* wl_output; struct wl_output* wl_output;
struct zxdg_output_v1* xdg_output; struct zxdg_output_v1* xdg_output;
struct zwlr_output_power_v1* wlr_output_power;
struct wl_list link; struct wl_list link;
uint32_t id; uint32_t id;
@ -41,12 +51,14 @@ struct output {
char model[256]; char model[256];
char name[256]; char name[256];
char description[256]; char description[256];
enum output_power_state power;
bool is_dimension_changed; bool is_dimension_changed;
bool is_transform_changed; bool is_transform_changed;
void (*on_dimension_change)(struct output*); void (*on_dimension_change)(struct output*);
void (*on_transform_change)(struct output*); void (*on_transform_change)(struct output*);
void (*on_power_change)(struct output*);
void* userdata; void* userdata;
}; };
@ -55,6 +67,8 @@ struct output* output_new(struct wl_output* wl_output, uint32_t id);
void output_destroy(struct output* output); void output_destroy(struct output* output);
void output_set_xdg_output(struct output* output, void output_set_xdg_output(struct output* output,
struct zxdg_output_v1* xdg_output); struct zxdg_output_v1* xdg_output);
void output_set_wlr_output_power(struct output* output,
struct zwlr_output_power_v1* wlr_output_power);
void output_list_destroy(struct wl_list* list); void output_list_destroy(struct wl_list* list);
struct output* output_find_by_id(struct wl_list* list, uint32_t id); struct output* output_find_by_id(struct wl_list* list, uint32_t id);
struct output* output_find_by_name(struct wl_list* list, const char* name); struct output* output_find_by_name(struct wl_list* list, const char* name);

View File

@ -21,6 +21,7 @@ client_protocols = [
'xdg-output-unstable-v1.xml', 'xdg-output-unstable-v1.xml',
'linux-dmabuf-unstable-v1.xml', 'linux-dmabuf-unstable-v1.xml',
'wlr-data-control-unstable-v1.xml', 'wlr-data-control-unstable-v1.xml',
'wlr-output-power-management-unstable-v1.xml',
] ]
client_protos_src = [] client_protos_src = []

View File

@ -0,0 +1,128 @@
<?xml version="1.0" encoding="UTF-8"?>
<protocol name="wlr_output_power_management_unstable_v1">
<copyright>
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.
</copyright>
<description summary="Control power management modes of outputs">
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.
</description>
<interface name="zwlr_output_power_manager_v1" version="1">
<description summary="manager to create per-output power management">
This interface is a manager that allows creating per-output power
management mode controls.
</description>
<request name="get_output_power">
<description summary="get a power management for an output">
Create a output power management mode control that can be used to
adjust the power management mode for a given output.
</description>
<arg name="id" type="new_id" interface="zwlr_output_power_v1"/>
<arg name="output" type="object" interface="wl_output"/>
</request>
<request name="destroy" type="destructor">
<description summary="destroy the manager">
All objects created by the manager will still remain valid, until their
appropriate destroy request has been called.
</description>
</request>
</interface>
<interface name="zwlr_output_power_v1" version="1">
<description summary="adjust power management mode for an output">
This object offers requests to set the power management mode of
an output.
</description>
<enum name="mode">
<entry name="off" value="0"
summary="Output is turned off."/>
<entry name="on" value="1"
summary="Output is turned on, no power saving"/>
</enum>
<enum name="error">
<entry name="invalid_mode" value="1" summary="inexistent power save mode"/>
</enum>
<request name="set_mode">
<description summary="Set an outputs power save mode">
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.
</description>
<arg name="mode" type="uint" enum="mode" summary="the power save mode to set"/>
</request>
<event name="mode">
<description summary="Report a power management mode change">
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.
</description>
<arg name="mode" type="uint" enum="mode"
summary="the output's new power management mode"/>
</event>
<event name="failed">
<description summary="object no longer valid">
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.
</description>
</event>
<request name="destroy" type="destructor">
<description summary="destroy this power management">
Destroys the output power management mode control object.
</description>
</request>
</interface>
</protocol>

View File

@ -39,6 +39,7 @@
#include "wlr-virtual-pointer-unstable-v1.h" #include "wlr-virtual-pointer-unstable-v1.h"
#include "virtual-keyboard-unstable-v1.h" #include "virtual-keyboard-unstable-v1.h"
#include "xdg-output-unstable-v1.h" #include "xdg-output-unstable-v1.h"
#include "wlr-output-power-management-unstable-v1.h"
#include "linux-dmabuf-unstable-v1.h" #include "linux-dmabuf-unstable-v1.h"
#include "screencopy.h" #include "screencopy.h"
#include "data-control.h" #include "data-control.h"
@ -78,6 +79,7 @@ struct wayvnc {
struct cfg cfg; struct cfg cfg;
struct zxdg_output_manager_v1* xdg_output_manager; struct zxdg_output_manager_v1* xdg_output_manager;
struct zwlr_output_power_manager_v1* wlr_output_power_manager;
struct zwp_virtual_keyboard_manager_v1* keyboard_manager; struct zwp_virtual_keyboard_manager_v1* keyboard_manager;
struct zwlr_virtual_pointer_manager_v1* pointer_manager; struct zwlr_virtual_pointer_manager_v1* pointer_manager;
struct zwlr_data_control_manager_v1* data_control_manager; struct zwlr_data_control_manager_v1* data_control_manager;
@ -208,6 +210,13 @@ static void registry_add(void* data, struct wl_registry* registry,
return; return;
} }
if (strcmp(interface, zwlr_output_power_manager_v1_interface.name) == 0) {
self->wlr_output_power_manager =
wl_registry_bind(registry, id,
&zwlr_output_power_manager_v1_interface, 1);
return;
}
if (strcmp(interface, zwlr_screencopy_manager_v1_interface.name) == 0) { if (strcmp(interface, zwlr_screencopy_manager_v1_interface.name) == 0) {
self->screencopy.manager = self->screencopy.manager =
wl_registry_bind(registry, id, wl_registry_bind(registry, id,
@ -320,6 +329,10 @@ void wayvnc_destroy(struct wayvnc* self)
zxdg_output_manager_v1_destroy(self->xdg_output_manager); zxdg_output_manager_v1_destroy(self->xdg_output_manager);
if (self->wlr_output_power_manager)
zwlr_output_power_manager_v1_destroy(
self->wlr_output_power_manager);
wl_shm_destroy(wl_shm); wl_shm_destroy(wl_shm);
if (self->keyboard_manager) if (self->keyboard_manager)
@ -344,15 +357,22 @@ void wayvnc_destroy(struct wayvnc* self)
wl_display_disconnect(self->display); wl_display_disconnect(self->display);
} }
static void init_xdg_outputs(struct wayvnc* self) static void init_outputs(struct wayvnc* self)
{ {
struct output* output; struct output* output;
wl_list_for_each(output, &self->outputs, link) { wl_list_for_each(output, &self->outputs, link) {
struct zxdg_output_v1* xdg_output = struct zxdg_output_v1* xdg_output =
zxdg_output_manager_v1_get_xdg_output( zxdg_output_manager_v1_get_xdg_output(
self->xdg_output_manager, output->wl_output); self->xdg_output_manager, output->wl_output);
output_set_xdg_output(output, xdg_output); output_set_xdg_output(output, xdg_output);
if (self->wlr_output_power_manager) {
struct zwlr_output_power_v1* wlr_output_power =
zwlr_output_power_manager_v1_get_output_power(
self->wlr_output_power_manager,
output->wl_output);
output_set_wlr_output_power(output, wlr_output_power);
}
} }
} }
@ -379,7 +399,7 @@ static int init_wayland(struct wayvnc* self)
wl_display_dispatch(self->display); wl_display_dispatch(self->display);
wl_display_roundtrip(self->display); wl_display_roundtrip(self->display);
init_xdg_outputs(self); init_outputs(self);
if (!self->pointer_manager && !self->disable_input) { if (!self->pointer_manager && !self->disable_input) {
nvnc_log(NVNC_LOG_ERROR, "Virtual Pointer protocol not supported by compositor."); nvnc_log(NVNC_LOG_ERROR, "Virtual Pointer protocol not supported by compositor.");
@ -771,6 +791,14 @@ void on_output_dimension_change(struct output* output)
wayvnc_start_capture_immediate(self); wayvnc_start_capture_immediate(self);
} }
static void on_output_power_change(struct output* output)
{
struct wayvnc* self = output->userdata;
assert(self->selected_output == output);
nvnc_trace("Output %s power state changed to %s", output->name, output_power_state_name(output->power));
}
static uint32_t calculate_region_area(struct pixman_region16* region) static uint32_t calculate_region_area(struct pixman_region16* region)
{ {
uint32_t area = 0; uint32_t area = 0;
@ -1107,11 +1135,12 @@ void log_selected_output(struct wayvnc* self)
struct output* output; struct output* output;
wl_list_for_each(output, &self->outputs, link) { wl_list_for_each(output, &self->outputs, link) {
bool this_output = (output->id == self->selected_output->id); bool this_output = (output->id == self->selected_output->id);
nvnc_log(NVNC_LOG_INFO, "%s %s %dx%d+%dx%d", nvnc_log(NVNC_LOG_INFO, "%s %s %dx%d+%dx%d Power:%s",
this_output ? ">>" : "--", this_output ? ">>" : "--",
output->description, output->description,
output->width, output->height, output->width, output->height,
output->x, output->y); output->x, output->y,
output_power_state_name(output->power));
} }
} }
@ -1122,6 +1151,7 @@ void set_selected_output(struct wayvnc* self, struct output* output) {
self->selected_output = output; self->selected_output = output;
self->screencopy.wl_output = output->wl_output; self->screencopy.wl_output = output->wl_output;
output->on_dimension_change = on_output_dimension_change; output->on_dimension_change = on_output_dimension_change;
output->on_power_change = on_output_power_change;
output->userdata = self; output->userdata = self;
if (self->ctl) if (self->ctl)
ctl_server_event_capture_changed(self->ctl, output->name); ctl_server_event_capture_changed(self->ctl, output->name);

View File

@ -27,6 +27,7 @@
#include "strlcpy.h" #include "strlcpy.h"
#include "xdg-output-unstable-v1.h" #include "xdg-output-unstable-v1.h"
#include "wlr-output-power-management-unstable-v1.h"
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b))
@ -179,6 +180,8 @@ static const struct wl_output_listener output_listener = {
void output_destroy(struct output* output) void output_destroy(struct output* output)
{ {
zxdg_output_v1_destroy(output->xdg_output); zxdg_output_v1_destroy(output->xdg_output);
if (output->wlr_output_power)
zwlr_output_power_v1_destroy(output->wlr_output_power);
wl_output_destroy(output->wl_output); wl_output_destroy(output->wl_output);
free(output); free(output);
} }
@ -204,6 +207,7 @@ struct output* output_new(struct wl_output* wl_output, uint32_t id)
output->wl_output = wl_output; output->wl_output = wl_output;
output->id = id; output->id = id;
output->power = OUTPUT_POWER_UNKNOWN;
wl_output_add_listener(output->wl_output, &output_listener, wl_output_add_listener(output->wl_output, &output_listener,
output); output);
@ -254,6 +258,63 @@ void output_set_xdg_output(struct output* self,
self); self);
} }
const char* output_power_state_name(enum output_power_state state)
{
switch(state) {
case OUTPUT_POWER_ON:
return "ON";
case OUTPUT_POWER_OFF:
return "OFF";
case OUTPUT_POWER_UNKNOWN:
return "??";
}
}
static void output_power_mode(void *data,
struct zwlr_output_power_v1 *zwlr_output_power_v1,
uint32_t mode)
{
struct output* self = data;
nvnc_trace("Output %s power state changed to %s", self->name,
(mode == ZWLR_OUTPUT_POWER_V1_MODE_ON) ? "ON" : "OFF");
enum output_power_state old = self->power;
switch (mode) {
case ZWLR_OUTPUT_POWER_V1_MODE_OFF:
self->power = OUTPUT_POWER_OFF;
break;
case ZWLR_OUTPUT_POWER_V1_MODE_ON:
self->power = OUTPUT_POWER_ON;
break;
}
if (old != self->power && self->on_power_change)
self->on_power_change(self);
}
static void output_power_failed(void *data,
struct zwlr_output_power_v1 *zwlr_output_power_v1)
{
struct output* self = data;
nvnc_log(NVNC_LOG_WARNING, "Output %s power state failure", self->name);
self->power = OUTPUT_POWER_UNKNOWN;
zwlr_output_power_v1_destroy(self->wlr_output_power);
self->wlr_output_power = NULL;
}
static const struct zwlr_output_power_v1_listener wlr_output_power_listener = {
.mode = output_power_mode,
.failed = output_power_failed,
};
void output_set_wlr_output_power(struct output* self,
struct zwlr_output_power_v1* wlr_output_power)
{
self->wlr_output_power = wlr_output_power;
zwlr_output_power_v1_add_listener(self->wlr_output_power,
&wlr_output_power_listener, self);
}
struct output* output_find_by_id(struct wl_list* list, uint32_t id) struct output* output_find_by_id(struct wl_list* list, uint32_t id)
{ {
struct output* output; struct output* output;