diyac/session.c

278 lines
9.1 KiB
C

#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;
static struct diyac_server *g_server;
static void focus_output(struct diyac_output *output)
{
g_server->lock->focused = NULL;
if (output)
{
g_server->lock->focused = output->lock_handle->surface->surface;
}
diyac_seat_focus_lock_surface(&g_server->seat, g_server->lock->focused);
}
static void session_lock_update_geometry(struct diyac_output *output, bool align)
{
struct wlr_box box;
wlr_output_layout_get_box(g_server->output_layout, output->wlr_output, &box);
if (align)
{
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);
}
wlr_scene_rect_set_size(output->lock_handle->background, box.width, box.height);
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);
}
}
static void handle_surface_map(struct wl_listener *listener, void *data)
{
struct diyac_output_lock_handle *handle = wl_container_of(listener, handle, surface_map);
if (!g_server->lock->focused)
{
focus_output(handle->output);
}
wlr_log(WLR_INFO, "handle_surface_map: surface is mapped");
}
static void handle_surface_destroy(struct wl_listener *listener, void *data)
{
struct diyac_output_lock_handle *handle = wl_container_of(listener, handle, surface_destroy);
if (g_server->lock->focused == handle->surface->surface)
{
struct diyac_output *output;
wl_list_for_each(output, &g_server->outputs, link)
{
if (output == handle->output ||
!output->lock_handle ||
!output->lock_handle->surface ||
!output->lock_handle->surface->surface)
{
continue;
}
if (output->lock_handle->surface->surface->mapped)
{
// focus surface
focus_output(output);
break;
}
}
focus_output(NULL);
}
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);
}
static void handle_new_surface(struct wl_listener *listener, void *data)
{
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");
assert(output->lock_handle);
output->lock_handle->surface = lock_surface;
wlr_scene_subsurface_tree_create(output->lock_handle->tree, lock_surface->surface);
output->lock_handle->surface_destroy.notify = handle_surface_destroy;
wl_signal_add(&lock_surface->events.destroy, &output->lock_handle->surface_destroy);
output->lock_handle->surface_map.notify = handle_surface_map;
wl_signal_add(&lock_surface->surface->events.map, &output->lock_handle->surface_map);
session_lock_update_geometry(output, false);
}
static void session_lock_destroy(struct diyac_session_lock *lock)
{
struct diyac_output *output;
wl_list_for_each(output, &g_server->outputs, link)
{
diyac_session_unlock_output(output);
}
if (g_server->lock == lock)
{
g_server->lock = NULL;
}
if (!lock->abandoned)
{
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);
}
static void handle_unlock(struct wl_listener *listener, void *data)
{
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);
}
static void handle_session_lock_destroy(struct wl_listener *listener, void *data)
{
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);
}
static void handle_new_session_lock(struct wl_listener *listener, void *data)
{
struct wlr_session_lock_v1 *lock = data;
if (g_server->lock)
{
if (g_server->lock->abandoned)
{
session_lock_destroy(g_server->lock);
}
else
{
wlr_log(WLR_ERROR, "handle_new_session_lock: session already locked");
return;
}
}
struct diyac_session_lock *session = malloc(sizeof(struct diyac_session_lock));
if (!session)
{
wlr_log(WLR_ERROR, "handle_new_session_lock: out of memory");
wlr_session_lock_v1_destroy(lock);
return;
}
session->abandoned = false;
session->wlr_session_lock = lock;
session->focused = NULL;
g_server->lock = session;
struct diyac_output *output;
wl_list_for_each(output, &g_server->outputs, link)
{
diyac_session_lock_output(output);
}
session->new_surface.notify = handle_new_surface;
wl_signal_add(&lock->events.new_surface, &session->new_surface);
session->unlock.notify = handle_unlock;
wl_signal_add(&lock->events.unlock, &session->unlock);
session->destroy.notify = handle_session_lock_destroy;
wl_signal_add(&lock->events.destroy, &session->destroy);
// session->new_output.notify = handle_new_output;
// wl_signal_add(&server.backend->events.new_output, &session->new_output);
wlr_session_lock_v1_send_locked(lock);
}
static void handle_commit(struct wl_listener *listener, void *data)
{
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)
{
session_lock_update_geometry(handle->output, false);
}
}
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;
}
void diyac_init_session_lock(struct diyac_server *server)
{
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;
}
void diyac_session_lock_output(struct diyac_output *output)
{
if (output->lock_handle)
{
diyac_session_unlock_output(output);
}
struct diyac_output_lock_handle *handle = malloc(sizeof(struct diyac_output_lock_handle));
if (!handle)
{
wlr_log(WLR_ERROR, "diyac_session_lock_output: out of memory");
exit(EXIT_FAILURE);
}
handle->tree = wlr_scene_tree_create(output->scenes.session);
if (!handle->tree)
{
free(handle);
wlr_log(WLR_ERROR, "diyac_session_lock_output:Unable to create lock tree");
exit(EXIT_FAILURE);
}
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)
{
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;
handle->commit.notify = handle_commit;
wl_signal_add(&output->wlr_output->events.commit, &handle->commit);
output->lock_handle = handle;
session_lock_update_geometry(output, true);
}
void diyac_session_unlock_output(struct diyac_output *output)
{
if (output->lock_handle)
{
wlr_scene_node_destroy(&output->lock_handle->tree->node);
if (output->lock_handle->surface)
{
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;
}
}