2024-04-15 17:32:53 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <wlr/util/log.h>
|
|
|
|
#include "session.h"
|
|
|
|
#include "view.h"
|
|
|
|
#include "seat.h"
|
|
|
|
|
|
|
|
static struct wl_listener g_lock_new;
|
|
|
|
static struct wl_listener g_lock_manager_destroy;
|
|
|
|
static struct wlr_session_lock_manager_v1 *g_session_lock_manager;
|
2024-04-17 01:08:26 +02:00
|
|
|
static struct diyac_server *g_server;
|
2024-04-15 17:32:53 +02:00
|
|
|
|
2024-04-17 01:08:26 +02:00
|
|
|
static void focus_output(struct diyac_output *output)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
|
|
|
g_server->lock->focused = NULL;
|
2024-04-17 01:08:26 +02:00
|
|
|
if (output)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
|
|
|
g_server->lock->focused = output->lock_handle->surface->surface;
|
|
|
|
}
|
|
|
|
diyac_seat_focus_lock_surface(&g_server->seat, g_server->lock->focused);
|
|
|
|
}
|
|
|
|
|
2024-04-17 01:08:26 +02:00
|
|
|
static void session_lock_update_geometry(struct diyac_output *output, bool align)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
|
|
|
struct wlr_box box;
|
2024-04-17 01:08:26 +02:00
|
|
|
wlr_output_layout_get_box(g_server->output_layout, output->wlr_output, &box);
|
|
|
|
if (align)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
2024-04-17 01:08:26 +02:00
|
|
|
wlr_scene_node_set_position(&output->scenes.session->node, box.x, box.y);
|
|
|
|
wlr_log(WLR_INFO, "session_lock_update_geometry: Align lock screen on %d, %d", box.x, box.y);
|
2024-04-15 17:32:53 +02:00
|
|
|
}
|
|
|
|
wlr_scene_rect_set_size(output->lock_handle->background, box.width, box.height);
|
2024-04-17 01:08:26 +02:00
|
|
|
if (output->lock_handle->surface)
|
|
|
|
{
|
|
|
|
wlr_log(WLR_INFO, "session_lock_update_geometry: configure lock screen %d, %d", box.width, box.height);
|
|
|
|
wlr_session_lock_surface_v1_configure(output->lock_handle->surface, box.width, box.height);
|
|
|
|
}
|
2024-04-15 17:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_surface_map(struct wl_listener *listener, void *data)
|
|
|
|
{
|
2024-04-17 01:08:26 +02:00
|
|
|
struct diyac_output_lock_handle *handle = wl_container_of(listener, handle, surface_map);
|
|
|
|
if (!g_server->lock->focused)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
|
|
|
focus_output(handle->output);
|
2024-04-17 01:08:26 +02:00
|
|
|
}
|
|
|
|
wlr_log(WLR_INFO, "handle_surface_map: surface is mapped");
|
2024-04-15 17:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_surface_destroy(struct wl_listener *listener, void *data)
|
|
|
|
{
|
2024-04-17 01:08:26 +02:00
|
|
|
struct diyac_output_lock_handle *handle = wl_container_of(listener, handle, surface_destroy);
|
|
|
|
|
|
|
|
if (g_server->lock->focused == handle->surface->surface)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
|
|
|
struct diyac_output *output;
|
|
|
|
wl_list_for_each(output, &g_server->outputs, link)
|
|
|
|
{
|
2024-04-17 01:08:26 +02:00
|
|
|
if (output == handle->output ||
|
2024-04-15 17:32:53 +02:00
|
|
|
!output->lock_handle ||
|
|
|
|
!output->lock_handle->surface ||
|
|
|
|
!output->lock_handle->surface->surface)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2024-04-17 01:08:26 +02:00
|
|
|
if (output->lock_handle->surface->surface->mapped)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
|
|
|
// focus surface
|
|
|
|
focus_output(output);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
focus_output(NULL);
|
|
|
|
}
|
2024-04-17 01:08:26 +02:00
|
|
|
wlr_log(WLR_INFO, "handle_surface_destroy: Lock surface destroyed");
|
|
|
|
assert(handle->surface);
|
|
|
|
handle->surface = NULL;
|
|
|
|
wl_list_remove(&handle->surface_destroy.link);
|
|
|
|
wl_list_remove(&handle->surface_map.link);
|
2024-04-15 17:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_new_surface(struct wl_listener *listener, void *data)
|
|
|
|
{
|
2024-04-17 01:08:26 +02:00
|
|
|
struct diyac_session_lock *lock = wl_container_of(listener, lock, new_surface);
|
|
|
|
struct wlr_session_lock_surface_v1 *lock_surface = data;
|
|
|
|
struct diyac_output *output = lock_surface->output->data;
|
|
|
|
wlr_log(WLR_INFO, "handle_new_surface: New lock surface requested");
|
2024-04-15 17:32:53 +02:00
|
|
|
assert(output->lock_handle);
|
|
|
|
|
2024-04-17 01:08:26 +02:00
|
|
|
output->lock_handle->surface = lock_surface;
|
2024-04-15 17:32:53 +02:00
|
|
|
|
2024-04-17 01:08:26 +02:00
|
|
|
wlr_scene_subsurface_tree_create(output->lock_handle->tree, lock_surface->surface);
|
2024-04-15 17:32:53 +02:00
|
|
|
|
2024-04-17 01:08:26 +02:00
|
|
|
output->lock_handle->surface_destroy.notify = handle_surface_destroy;
|
|
|
|
wl_signal_add(&lock_surface->events.destroy, &output->lock_handle->surface_destroy);
|
2024-04-15 17:32:53 +02:00
|
|
|
|
2024-04-17 01:08:26 +02:00
|
|
|
output->lock_handle->surface_map.notify = handle_surface_map;
|
|
|
|
wl_signal_add(&lock_surface->surface->events.map, &output->lock_handle->surface_map);
|
2024-04-15 17:32:53 +02:00
|
|
|
|
2024-04-17 01:08:26 +02:00
|
|
|
session_lock_update_geometry(output, false);
|
2024-04-15 17:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void session_lock_destroy(struct diyac_session_lock *lock)
|
|
|
|
{
|
2024-04-17 01:08:26 +02:00
|
|
|
struct diyac_output *output;
|
|
|
|
wl_list_for_each(output, &g_server->outputs, link)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
|
|
|
diyac_session_unlock_output(output);
|
2024-04-17 01:08:26 +02:00
|
|
|
}
|
|
|
|
if (g_server->lock == lock)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
2024-04-17 01:08:26 +02:00
|
|
|
g_server->lock = NULL;
|
|
|
|
}
|
|
|
|
if (!lock->abandoned)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
2024-04-17 01:08:26 +02:00
|
|
|
wl_list_remove(&lock->destroy.link);
|
|
|
|
wl_list_remove(&lock->unlock.link);
|
|
|
|
wl_list_remove(&lock->new_surface.link);
|
|
|
|
}
|
|
|
|
wlr_log(WLR_INFO, "session_lock_destroy");
|
|
|
|
free(lock);
|
2024-04-15 17:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_unlock(struct wl_listener *listener, void *data)
|
|
|
|
{
|
2024-04-17 01:08:26 +02:00
|
|
|
struct diyac_session_lock *lock = wl_container_of(listener, lock, unlock);
|
|
|
|
wlr_log(WLR_INFO, "handle_unlock: Lock session is unlocked");
|
|
|
|
session_lock_destroy(lock);
|
|
|
|
diyac_focus_topmost_view(g_server, true);
|
2024-04-15 17:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_session_lock_destroy(struct wl_listener *listener, void *data)
|
|
|
|
{
|
2024-04-17 01:08:26 +02:00
|
|
|
struct diyac_session_lock *lock = wl_container_of(listener, lock, destroy);
|
|
|
|
lock->abandoned = true;
|
|
|
|
wlr_log(WLR_INFO, "handle_session_lock_destroy: Lock session is destroyed without unlocking, session abandoned");
|
|
|
|
wl_list_remove(&lock->destroy.link);
|
|
|
|
wl_list_remove(&lock->unlock.link);
|
|
|
|
wl_list_remove(&lock->new_surface.link);
|
2024-04-15 17:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_new_session_lock(struct wl_listener *listener, void *data)
|
|
|
|
{
|
|
|
|
struct wlr_session_lock_v1 *lock = data;
|
2024-04-17 01:08:26 +02:00
|
|
|
if (g_server->lock)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
2024-04-17 01:08:26 +02:00
|
|
|
if (g_server->lock->abandoned)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
|
|
|
session_lock_destroy(g_server->lock);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-04-17 01:08:26 +02:00
|
|
|
wlr_log(WLR_ERROR, "handle_new_session_lock: session already locked");
|
2024-04-15 17:32:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
struct diyac_session_lock *session = malloc(sizeof(struct diyac_session_lock));
|
2024-04-17 01:08:26 +02:00
|
|
|
if (!session)
|
|
|
|
{
|
|
|
|
wlr_log(WLR_ERROR, "handle_new_session_lock: out of memory");
|
|
|
|
wlr_session_lock_v1_destroy(lock);
|
|
|
|
return;
|
|
|
|
}
|
2024-04-15 17:32:53 +02:00
|
|
|
session->abandoned = false;
|
|
|
|
session->wlr_session_lock = lock;
|
|
|
|
session->focused = NULL;
|
|
|
|
g_server->lock = session;
|
|
|
|
|
|
|
|
struct diyac_output *output;
|
2024-04-17 01:08:26 +02:00
|
|
|
wl_list_for_each(output, &g_server->outputs, link)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
2024-04-17 01:08:26 +02:00
|
|
|
diyac_session_lock_output(output);
|
|
|
|
}
|
2024-04-15 17:32:53 +02:00
|
|
|
|
|
|
|
session->new_surface.notify = handle_new_surface;
|
2024-04-17 01:08:26 +02:00
|
|
|
wl_signal_add(&lock->events.new_surface, &session->new_surface);
|
2024-04-15 17:32:53 +02:00
|
|
|
|
2024-04-17 01:08:26 +02:00
|
|
|
session->unlock.notify = handle_unlock;
|
|
|
|
wl_signal_add(&lock->events.unlock, &session->unlock);
|
2024-04-15 17:32:53 +02:00
|
|
|
|
2024-04-17 01:08:26 +02:00
|
|
|
session->destroy.notify = handle_session_lock_destroy;
|
|
|
|
wl_signal_add(&lock->events.destroy, &session->destroy);
|
2024-04-15 17:32:53 +02:00
|
|
|
|
2024-04-17 01:08:26 +02:00
|
|
|
// session->new_output.notify = handle_new_output;
|
|
|
|
// wl_signal_add(&server.backend->events.new_output, &session->new_output);
|
2024-04-15 17:32:53 +02:00
|
|
|
|
2024-04-17 01:08:26 +02:00
|
|
|
wlr_session_lock_v1_send_locked(lock);
|
2024-04-15 17:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_commit(struct wl_listener *listener, void *data)
|
|
|
|
{
|
2024-04-17 01:08:26 +02:00
|
|
|
struct wlr_output_event_commit *event = data;
|
|
|
|
struct diyac_output_lock_handle *handle = wl_container_of(listener, handle, commit);
|
|
|
|
uint32_t require_reconfigure = WLR_OUTPUT_STATE_MODE | WLR_OUTPUT_STATE_SCALE | WLR_OUTPUT_STATE_TRANSFORM;
|
|
|
|
if (event->state->committed & require_reconfigure)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
2024-04-17 01:08:26 +02:00
|
|
|
session_lock_update_geometry(handle->output, false);
|
|
|
|
}
|
2024-04-15 17:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_lock_manager_destroy(struct wl_listener *listener, void *data)
|
|
|
|
{
|
|
|
|
if (g_server->lock)
|
|
|
|
{
|
|
|
|
session_lock_destroy(g_server->lock);
|
|
|
|
g_server->lock = NULL;
|
|
|
|
}
|
|
|
|
wl_list_remove(&g_lock_new.link);
|
|
|
|
wl_list_remove(&g_lock_manager_destroy.link);
|
|
|
|
g_session_lock_manager = NULL;
|
|
|
|
}
|
2024-04-17 01:08:26 +02:00
|
|
|
void diyac_init_session_lock(struct diyac_server *server)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
|
|
|
g_session_lock_manager = wlr_session_lock_manager_v1_create(server->wl_display);
|
|
|
|
|
|
|
|
g_lock_new.notify = handle_new_session_lock;
|
|
|
|
wl_signal_add(&g_session_lock_manager->events.new_lock, &g_lock_new);
|
|
|
|
|
|
|
|
g_lock_manager_destroy.notify = handle_lock_manager_destroy;
|
|
|
|
wl_signal_add(&g_session_lock_manager->events.destroy, &g_lock_manager_destroy);
|
|
|
|
|
|
|
|
g_server = server;
|
|
|
|
}
|
|
|
|
|
2024-04-17 01:08:26 +02:00
|
|
|
void diyac_session_lock_output(struct diyac_output *output)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
2024-04-17 01:08:26 +02:00
|
|
|
if (output->lock_handle)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
|
|
|
diyac_session_unlock_output(output);
|
|
|
|
}
|
2024-04-17 01:08:26 +02:00
|
|
|
struct diyac_output_lock_handle *handle = malloc(sizeof(struct diyac_output_lock_handle));
|
|
|
|
if (!handle)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
|
|
|
wlr_log(WLR_ERROR, "diyac_session_lock_output: out of memory");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
handle->tree = wlr_scene_tree_create(output->scenes.session);
|
2024-04-17 01:08:26 +02:00
|
|
|
if (!handle->tree)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
|
|
|
free(handle);
|
|
|
|
wlr_log(WLR_ERROR, "diyac_session_lock_output:Unable to create lock tree");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2024-04-17 01:08:26 +02:00
|
|
|
float *black = (float[4]){0.f, 0.f, 0.f, 1.f};
|
|
|
|
handle->background = wlr_scene_rect_create(handle->tree, 0, 0, black);
|
|
|
|
if (!handle->background)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
|
|
|
free(handle);
|
|
|
|
wlr_log(WLR_ERROR, "diyac_session_lock_output:Unable to create lock background");
|
|
|
|
wlr_scene_node_destroy(&handle->tree->node);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
handle->surface = NULL;
|
|
|
|
handle->output = output;
|
|
|
|
|
2024-04-17 01:08:26 +02:00
|
|
|
handle->commit.notify = handle_commit;
|
|
|
|
wl_signal_add(&output->wlr_output->events.commit, &handle->commit);
|
2024-04-15 17:32:53 +02:00
|
|
|
|
|
|
|
output->lock_handle = handle;
|
|
|
|
|
|
|
|
session_lock_update_geometry(output, true);
|
|
|
|
}
|
|
|
|
|
2024-04-17 01:08:26 +02:00
|
|
|
void diyac_session_unlock_output(struct diyac_output *output)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
2024-04-17 01:08:26 +02:00
|
|
|
if (output->lock_handle)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
|
|
|
wlr_scene_node_destroy(&output->lock_handle->tree->node);
|
2024-04-17 01:08:26 +02:00
|
|
|
if (output->lock_handle->surface)
|
2024-04-15 17:32:53 +02:00
|
|
|
{
|
|
|
|
wl_list_remove(&output->lock_handle->surface_destroy.link);
|
|
|
|
wl_list_remove(&output->lock_handle->surface_map.link);
|
|
|
|
}
|
|
|
|
wl_list_remove(&output->lock_handle->commit.link);
|
|
|
|
free(output->lock_handle);
|
|
|
|
output->lock_handle = NULL;
|
|
|
|
}
|
|
|
|
}
|