2023-03-30 22:19:05 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include "common/mem.h"
|
|
|
|
|
#include "labwc.h"
|
|
|
|
|
|
|
|
|
|
struct session_lock_output {
|
|
|
|
|
struct wlr_scene_tree *tree;
|
|
|
|
|
struct wlr_scene_rect *background;
|
2024-05-30 00:13:34 +09:00
|
|
|
struct session_lock_manager *manager;
|
2023-03-30 22:19:05 +01:00
|
|
|
struct output *output;
|
|
|
|
|
struct wlr_session_lock_surface_v1 *surface;
|
2024-05-30 00:29:59 +09:00
|
|
|
struct wl_event_source *blank_timer;
|
2023-03-30 22:19:05 +01:00
|
|
|
|
2024-05-30 00:13:34 +09:00
|
|
|
struct wl_list link; /* session_lock_manager.outputs */
|
2023-03-30 22:19:05 +01:00
|
|
|
|
|
|
|
|
struct wl_listener destroy;
|
|
|
|
|
struct wl_listener commit;
|
|
|
|
|
struct wl_listener surface_destroy;
|
|
|
|
|
struct wl_listener surface_map;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void
|
2024-05-30 00:13:34 +09:00
|
|
|
focus_surface(struct session_lock_manager *manager, struct wlr_surface *focused)
|
2023-03-30 22:19:05 +01:00
|
|
|
{
|
2024-05-30 00:13:34 +09:00
|
|
|
manager->focused = focused;
|
|
|
|
|
seat_focus_lock_surface(&manager->server->seat, focused);
|
2023-03-30 22:19:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
refocus_output(struct session_lock_output *output)
|
|
|
|
|
{
|
|
|
|
|
/* Try to focus another session-lock surface */
|
2024-05-30 00:13:34 +09:00
|
|
|
if (output->manager->focused != output->surface->surface) {
|
2023-03-30 22:19:05 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct session_lock_output *iter;
|
2024-05-30 00:13:34 +09:00
|
|
|
wl_list_for_each(iter, &output->manager->session_lock_outputs, link) {
|
2023-06-15 02:35:43 -07:00
|
|
|
if (iter == output || !iter->surface || !iter->surface->surface) {
|
2023-03-30 22:19:05 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
2023-06-15 02:35:43 -07:00
|
|
|
if (iter->surface->surface->mapped) {
|
2024-05-30 00:13:34 +09:00
|
|
|
focus_surface(output->manager, iter->surface->surface);
|
2023-03-30 22:19:05 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-30 00:13:34 +09:00
|
|
|
focus_surface(output->manager, NULL);
|
2023-03-30 22:19:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
handle_surface_map(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
2024-05-30 00:13:34 +09:00
|
|
|
struct session_lock_output *output =
|
|
|
|
|
wl_container_of(listener, output, surface_map);
|
|
|
|
|
if (!output->manager->focused) {
|
|
|
|
|
focus_surface(output->manager, output->surface->surface);
|
2023-03-30 22:19:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
handle_surface_destroy(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
|
|
|
|
struct session_lock_output *output =
|
|
|
|
|
wl_container_of(listener, output, surface_destroy);
|
|
|
|
|
refocus_output(output);
|
|
|
|
|
|
|
|
|
|
assert(output->surface);
|
|
|
|
|
output->surface = NULL;
|
|
|
|
|
wl_list_remove(&output->surface_destroy.link);
|
|
|
|
|
wl_list_remove(&output->surface_map.link);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
lock_output_reconfigure(struct session_lock_output *output)
|
|
|
|
|
{
|
|
|
|
|
struct wlr_box box;
|
2024-05-30 00:13:34 +09:00
|
|
|
wlr_output_layout_get_box(output->manager->server->output_layout,
|
|
|
|
|
output->output->wlr_output, &box);
|
2023-03-30 22:19:05 +01:00
|
|
|
wlr_scene_rect_set_size(output->background, box.width, box.height);
|
|
|
|
|
if (output->surface) {
|
2024-05-30 00:13:34 +09:00
|
|
|
wlr_session_lock_surface_v1_configure(
|
|
|
|
|
output->surface, box.width, box.height);
|
2023-03-30 22:19:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
handle_new_surface(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
2024-05-30 00:13:34 +09:00
|
|
|
struct session_lock_manager *manager =
|
|
|
|
|
wl_container_of(listener, manager, lock_new_surface);
|
2023-03-30 22:19:05 +01:00
|
|
|
struct wlr_session_lock_surface_v1 *lock_surface = data;
|
|
|
|
|
struct output *output = lock_surface->output->data;
|
|
|
|
|
struct session_lock_output *lock_output;
|
2024-05-30 00:13:34 +09:00
|
|
|
wl_list_for_each(lock_output, &manager->session_lock_outputs, link) {
|
2023-03-30 22:19:05 +01:00
|
|
|
if (lock_output->output == output) {
|
|
|
|
|
goto found_lock_output;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
wlr_log(WLR_ERROR, "new lock surface, but no output");
|
|
|
|
|
/* TODO: Consider improving security by handling this better */
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
found_lock_output:
|
|
|
|
|
lock_output->surface = lock_surface;
|
|
|
|
|
wlr_scene_subsurface_tree_create(lock_output->tree, lock_surface->surface);
|
|
|
|
|
|
|
|
|
|
lock_output->surface_destroy.notify = handle_surface_destroy;
|
|
|
|
|
wl_signal_add(&lock_surface->events.destroy, &lock_output->surface_destroy);
|
|
|
|
|
|
|
|
|
|
lock_output->surface_map.notify = handle_surface_map;
|
2023-06-15 02:35:43 -07:00
|
|
|
wl_signal_add(&lock_surface->surface->events.map, &lock_output->surface_map);
|
2023-03-30 22:19:05 +01:00
|
|
|
|
|
|
|
|
lock_output_reconfigure(lock_output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
session_lock_output_destroy(struct session_lock_output *output)
|
|
|
|
|
{
|
|
|
|
|
if (output->surface) {
|
|
|
|
|
refocus_output(output);
|
|
|
|
|
wl_list_remove(&output->surface_destroy.link);
|
|
|
|
|
wl_list_remove(&output->surface_map.link);
|
|
|
|
|
}
|
|
|
|
|
wl_list_remove(&output->commit.link);
|
|
|
|
|
wl_list_remove(&output->destroy.link);
|
|
|
|
|
wl_list_remove(&output->link);
|
2024-05-30 00:29:59 +09:00
|
|
|
wl_event_source_remove(output->blank_timer);
|
2023-03-30 22:19:05 +01:00
|
|
|
free(output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2024-05-30 00:13:34 +09:00
|
|
|
handle_output_destroy(struct wl_listener *listener, void *data)
|
2023-03-30 22:19:05 +01:00
|
|
|
{
|
|
|
|
|
struct session_lock_output *output = wl_container_of(listener, output, destroy);
|
|
|
|
|
session_lock_output_destroy(output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2024-05-30 00:13:34 +09:00
|
|
|
handle_output_commit(struct wl_listener *listener, void *data)
|
2023-03-30 22:19:05 +01:00
|
|
|
{
|
|
|
|
|
struct wlr_output_event_commit *event = data;
|
|
|
|
|
struct session_lock_output *output = wl_container_of(listener, output, commit);
|
|
|
|
|
uint32_t require_reconfigure = WLR_OUTPUT_STATE_MODE
|
|
|
|
|
| WLR_OUTPUT_STATE_SCALE | WLR_OUTPUT_STATE_TRANSFORM;
|
2023-10-06 19:47:40 +02:00
|
|
|
if (event->state->committed & require_reconfigure) {
|
2023-03-30 22:19:05 +01:00
|
|
|
lock_output_reconfigure(output);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-04 23:54:27 -04:00
|
|
|
static void
|
|
|
|
|
align_session_lock_tree(struct output *output)
|
|
|
|
|
{
|
|
|
|
|
struct wlr_box box;
|
2024-05-30 00:13:34 +09:00
|
|
|
wlr_output_layout_get_box(output->server->output_layout,
|
|
|
|
|
output->wlr_output, &box);
|
2023-10-04 23:54:27 -04:00
|
|
|
wlr_scene_node_set_position(&output->session_lock_tree->node, box.x, box.y);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-30 00:29:59 +09:00
|
|
|
static int
|
|
|
|
|
handle_output_blank_timeout(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct session_lock_output *lock_output = data;
|
|
|
|
|
wlr_scene_node_set_enabled(&lock_output->background->node, true);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-30 22:19:05 +01:00
|
|
|
void
|
2024-05-30 00:13:34 +09:00
|
|
|
session_lock_output_create(struct session_lock_manager *manager, struct output *output)
|
2023-03-30 22:19:05 +01:00
|
|
|
{
|
|
|
|
|
struct session_lock_output *lock_output = znew(*lock_output);
|
|
|
|
|
|
|
|
|
|
struct wlr_scene_tree *tree = wlr_scene_tree_create(output->session_lock_tree);
|
|
|
|
|
if (!tree) {
|
|
|
|
|
wlr_log(WLR_ERROR, "session-lock: wlr_scene_tree_create()");
|
|
|
|
|
free(lock_output);
|
|
|
|
|
goto exit_session;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* The ext-session-lock protocol says that the compositor should blank
|
|
|
|
|
* all outputs with an opaque color such that their normal content is
|
|
|
|
|
* fully hidden
|
|
|
|
|
*/
|
|
|
|
|
float *black = (float[4]) { 0.f, 0.f, 0.f, 1.f };
|
|
|
|
|
struct wlr_scene_rect *background = wlr_scene_rect_create(tree, 0, 0, black);
|
|
|
|
|
if (!background) {
|
|
|
|
|
wlr_log(WLR_ERROR, "session-lock: wlr_scene_rect_create()");
|
|
|
|
|
wlr_scene_node_destroy(&tree->node);
|
|
|
|
|
free(lock_output);
|
|
|
|
|
goto exit_session;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-30 00:29:59 +09:00
|
|
|
/* Delay blanking output by 100ms to prevent flashing */
|
|
|
|
|
lock_output->blank_timer =
|
|
|
|
|
wl_event_loop_add_timer(manager->server->wl_event_loop,
|
|
|
|
|
handle_output_blank_timeout, lock_output);
|
|
|
|
|
if (!manager->locked) {
|
|
|
|
|
wlr_scene_node_set_enabled(&background->node, false);
|
|
|
|
|
wl_event_source_timer_update(lock_output->blank_timer, 100);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-04 23:54:27 -04:00
|
|
|
align_session_lock_tree(output);
|
2023-03-30 22:19:05 +01:00
|
|
|
|
|
|
|
|
lock_output->output = output;
|
|
|
|
|
lock_output->tree = tree;
|
|
|
|
|
lock_output->background = background;
|
2024-05-30 00:13:34 +09:00
|
|
|
lock_output->manager = manager;
|
2023-03-30 22:19:05 +01:00
|
|
|
|
2024-05-30 00:13:34 +09:00
|
|
|
lock_output->destroy.notify = handle_output_destroy;
|
2023-03-30 22:19:05 +01:00
|
|
|
wl_signal_add(&tree->node.events.destroy, &lock_output->destroy);
|
|
|
|
|
|
2024-05-30 00:13:34 +09:00
|
|
|
lock_output->commit.notify = handle_output_commit;
|
2023-03-30 22:19:05 +01:00
|
|
|
wl_signal_add(&output->wlr_output->events.commit, &lock_output->commit);
|
|
|
|
|
|
|
|
|
|
lock_output_reconfigure(lock_output);
|
|
|
|
|
|
2024-05-30 00:13:34 +09:00
|
|
|
wl_list_insert(&manager->session_lock_outputs, &lock_output->link);
|
2023-03-30 22:19:05 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
exit_session:
|
|
|
|
|
/* TODO: Consider a better - but secure - way to deal with this */
|
|
|
|
|
wlr_log(WLR_ERROR, "out of memory");
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2024-05-30 00:13:34 +09:00
|
|
|
session_lock_destroy(struct session_lock_manager *manager)
|
2023-03-30 22:19:05 +01:00
|
|
|
{
|
|
|
|
|
struct session_lock_output *lock_output, *next;
|
2024-05-30 00:13:34 +09:00
|
|
|
wl_list_for_each_safe(lock_output, next, &manager->session_lock_outputs, link) {
|
2023-03-30 22:19:05 +01:00
|
|
|
wlr_scene_node_destroy(&lock_output->tree->node);
|
|
|
|
|
}
|
2024-05-30 00:13:34 +09:00
|
|
|
if (manager->lock) {
|
|
|
|
|
wl_list_remove(&manager->lock_destroy.link);
|
|
|
|
|
wl_list_remove(&manager->lock_unlock.link);
|
|
|
|
|
wl_list_remove(&manager->lock_new_surface.link);
|
2023-03-30 22:19:05 +01:00
|
|
|
}
|
2024-05-30 00:13:34 +09:00
|
|
|
manager->lock = NULL;
|
2023-03-30 22:19:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2024-05-30 00:13:34 +09:00
|
|
|
handle_lock_unlock(struct wl_listener *listener, void *data)
|
2023-03-30 22:19:05 +01:00
|
|
|
{
|
2024-05-30 00:13:34 +09:00
|
|
|
struct session_lock_manager *manager =
|
|
|
|
|
wl_container_of(listener, manager, lock_unlock);
|
|
|
|
|
session_lock_destroy(manager);
|
|
|
|
|
manager->locked = false;
|
|
|
|
|
desktop_focus_topmost_view(manager->server);
|
2023-03-30 22:19:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2024-05-30 00:13:34 +09:00
|
|
|
handle_lock_destroy(struct wl_listener *listener, void *data)
|
2023-03-30 22:19:05 +01:00
|
|
|
{
|
2024-05-30 00:13:34 +09:00
|
|
|
struct session_lock_manager *manager =
|
|
|
|
|
wl_container_of(listener, manager, lock_destroy);
|
2023-03-30 22:19:05 +01:00
|
|
|
|
|
|
|
|
float *black = (float[4]) { 0.f, 0.f, 0.f, 1.f };
|
|
|
|
|
struct session_lock_output *lock_output;
|
2024-05-30 00:13:34 +09:00
|
|
|
wl_list_for_each(lock_output, &manager->session_lock_outputs, link) {
|
2023-03-30 22:19:05 +01:00
|
|
|
wlr_scene_rect_set_color(lock_output->background, black);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-30 00:13:34 +09:00
|
|
|
wl_list_remove(&manager->lock_destroy.link);
|
|
|
|
|
wl_list_remove(&manager->lock_unlock.link);
|
|
|
|
|
wl_list_remove(&manager->lock_new_surface.link);
|
2023-03-30 22:19:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
handle_new_session_lock(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
2024-05-30 00:13:34 +09:00
|
|
|
struct session_lock_manager *manager =
|
|
|
|
|
wl_container_of(listener, manager, new_lock);
|
2023-03-30 22:19:05 +01:00
|
|
|
struct wlr_session_lock_v1 *lock = data;
|
|
|
|
|
|
2024-05-30 00:13:34 +09:00
|
|
|
if (manager->lock) {
|
|
|
|
|
wlr_log(WLR_ERROR, "session already locked");
|
2023-03-30 22:19:05 +01:00
|
|
|
wlr_session_lock_v1_destroy(lock);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-05-30 00:13:34 +09:00
|
|
|
if (manager->locked) {
|
|
|
|
|
wlr_log(WLR_INFO, "replacing abandoned lock");
|
|
|
|
|
session_lock_destroy(manager);
|
|
|
|
|
}
|
|
|
|
|
assert(wl_list_empty(&manager->session_lock_outputs));
|
2023-03-30 22:19:05 +01:00
|
|
|
|
|
|
|
|
struct output *output;
|
2024-05-30 00:13:34 +09:00
|
|
|
wl_list_for_each(output, &manager->server->outputs, link) {
|
|
|
|
|
session_lock_output_create(manager, output);
|
2023-03-30 22:19:05 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-30 00:13:34 +09:00
|
|
|
manager->lock_new_surface.notify = handle_new_surface;
|
|
|
|
|
wl_signal_add(&lock->events.new_surface, &manager->lock_new_surface);
|
2023-03-30 22:19:05 +01:00
|
|
|
|
2024-05-30 00:13:34 +09:00
|
|
|
manager->lock_unlock.notify = handle_lock_unlock;
|
|
|
|
|
wl_signal_add(&lock->events.unlock, &manager->lock_unlock);
|
2023-03-30 22:19:05 +01:00
|
|
|
|
2024-05-30 00:13:34 +09:00
|
|
|
manager->lock_destroy.notify = handle_lock_destroy;
|
|
|
|
|
wl_signal_add(&lock->events.destroy, &manager->lock_destroy);
|
2023-03-30 22:19:05 +01:00
|
|
|
|
2024-05-30 00:13:34 +09:00
|
|
|
manager->locked = true;
|
2023-03-30 22:19:05 +01:00
|
|
|
wlr_session_lock_v1_send_locked(lock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
handle_manager_destroy(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
2024-05-30 00:13:34 +09:00
|
|
|
struct session_lock_manager *manager =
|
|
|
|
|
wl_container_of(listener, manager, destroy);
|
|
|
|
|
session_lock_destroy(manager);
|
|
|
|
|
wl_list_remove(&manager->new_lock.link);
|
|
|
|
|
wl_list_remove(&manager->destroy.link);
|
|
|
|
|
manager->server->session_lock_manager = NULL;
|
|
|
|
|
free(manager);
|
2023-03-30 22:19:05 +01:00
|
|
|
}
|
|
|
|
|
|
2023-10-04 23:54:27 -04:00
|
|
|
void
|
|
|
|
|
session_lock_init(struct server *server)
|
2023-03-30 22:19:05 +01:00
|
|
|
{
|
2024-05-30 00:13:34 +09:00
|
|
|
struct session_lock_manager *manager = znew(*manager);
|
|
|
|
|
server->session_lock_manager = manager;
|
|
|
|
|
manager->server = server;
|
|
|
|
|
manager->wlr_manager = wlr_session_lock_manager_v1_create(server->wl_display);
|
|
|
|
|
wl_list_init(&manager->session_lock_outputs);
|
2023-03-30 22:19:05 +01:00
|
|
|
|
2024-05-30 00:13:34 +09:00
|
|
|
manager->new_lock.notify = handle_new_session_lock;
|
|
|
|
|
wl_signal_add(&manager->wlr_manager->events.new_lock, &manager->new_lock);
|
2023-03-30 22:19:05 +01:00
|
|
|
|
2024-05-30 00:13:34 +09:00
|
|
|
manager->destroy.notify = handle_manager_destroy;
|
|
|
|
|
wl_signal_add(&manager->wlr_manager->events.destroy, &manager->destroy);
|
2023-03-30 22:19:05 +01:00
|
|
|
}
|
2023-10-04 23:54:27 -04:00
|
|
|
|
|
|
|
|
void
|
2024-05-30 00:13:34 +09:00
|
|
|
session_lock_update_for_layout_change(struct server *server)
|
2023-10-04 23:54:27 -04:00
|
|
|
{
|
2024-05-30 00:13:34 +09:00
|
|
|
if (!server->session_lock_manager->locked) {
|
2023-10-04 23:54:27 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct output *output;
|
2024-05-30 00:13:34 +09:00
|
|
|
wl_list_for_each(output, &server->outputs, link) {
|
2023-10-04 23:54:27 -04:00
|
|
|
align_session_lock_tree(output);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-30 00:13:34 +09:00
|
|
|
struct session_lock_manager *manager = server->session_lock_manager;
|
2023-10-04 23:54:27 -04:00
|
|
|
struct session_lock_output *lock_output;
|
2024-05-30 00:13:34 +09:00
|
|
|
wl_list_for_each(lock_output, &manager->session_lock_outputs, link) {
|
2023-10-04 23:54:27 -04:00
|
|
|
lock_output_reconfigure(lock_output);
|
|
|
|
|
}
|
|
|
|
|
}
|