mirror of
https://github.com/cage-kiosk/cage.git
synced 2025-10-29 05:40:19 -04:00
This function can get called on a surface that is not actually a view. We need the view for the output, but we can pass that as user_data instead when the iterator gets called for all surfaces in a view.
197 lines
4.1 KiB
C
197 lines
4.1 KiB
C
/*
|
|
* Cage: A Wayland kiosk.
|
|
*
|
|
* Copyright (C) 2018-2020 Jente Hidskes
|
|
*
|
|
* See the LICENSE file accompanying this file.
|
|
*/
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <assert.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
#include <wlr/types/wlr_surface.h>
|
|
#include <wlr/util/region.h>
|
|
|
|
#include "output.h"
|
|
#include "view.h"
|
|
|
|
static bool
|
|
cage_view_extends_output(struct cg_view *view, struct wlr_box *output_box)
|
|
{
|
|
assert(view->impl->get_geometry != NULL);
|
|
|
|
int width, height;
|
|
view->impl->get_geometry(view, &width, &height);
|
|
|
|
return (output_box->height < height || output_box->width < width);
|
|
}
|
|
|
|
bool
|
|
cage_view_is_primary(struct cg_view *view)
|
|
{
|
|
assert(view != NULL);
|
|
assert(view->impl->is_primary != NULL);
|
|
return view->impl->is_primary(view);
|
|
}
|
|
|
|
static void
|
|
cage_view_maximize(struct cg_view *view, struct wlr_box *output_box)
|
|
{
|
|
assert(view->impl->maximize != NULL);
|
|
|
|
view->impl->maximize(view, output_box->width, output_box->height);
|
|
}
|
|
|
|
static void
|
|
cage_view_center(struct cg_view *view, struct wlr_box *output_box)
|
|
{
|
|
// No-op placeholder
|
|
}
|
|
|
|
void
|
|
cage_view_position(struct cg_view *view)
|
|
{
|
|
assert(view != NULL);
|
|
|
|
struct wlr_box output_box = {0};
|
|
cage_output_get_geometry(view->output, &output_box);
|
|
|
|
if (cage_view_is_primary(view) || cage_view_extends_output(view, &output_box)) {
|
|
cage_view_maximize(view, &output_box);
|
|
} else {
|
|
cage_view_center(view, &output_box);
|
|
}
|
|
}
|
|
|
|
static void
|
|
damage_surface_iterator(struct wlr_surface *surface, int sx, int sy, void *user_data)
|
|
{
|
|
struct cg_output *output = (struct cg_output *) user_data;
|
|
cage_output_damage_surface(output, surface, sx, sy);
|
|
}
|
|
|
|
void
|
|
cage_view_for_each_surface(struct cg_view *view, wlr_surface_iterator_func_t iterator, void *user_data)
|
|
{
|
|
assert(view != NULL);
|
|
assert(view->impl->for_each_surface != NULL);
|
|
view->impl->for_each_surface(view, iterator, user_data);
|
|
}
|
|
|
|
char *
|
|
cage_view_get_title(struct cg_view *view)
|
|
{
|
|
assert(view != NULL);
|
|
assert(view->impl->get_title != NULL);
|
|
|
|
const char *title = view->impl->get_title(view);
|
|
if (!title) {
|
|
return NULL;
|
|
}
|
|
return strndup(title, strlen(title));
|
|
}
|
|
|
|
void
|
|
cage_view_damage_whole(struct cg_view *view)
|
|
{
|
|
assert(view != NULL);
|
|
assert(view->impl->get_geometry != NULL);
|
|
struct wlr_box box = {0};
|
|
|
|
view->impl->get_geometry(view, &box.width, &box.height);
|
|
|
|
cage_output_damage_region(view->output, &box);
|
|
}
|
|
|
|
void
|
|
cage_view_damage_part(struct cg_view *view)
|
|
{
|
|
assert(view != NULL);
|
|
cage_view_for_each_surface(view, damage_surface_iterator, (void *) view->output);
|
|
}
|
|
|
|
struct wlr_surface *
|
|
cage_view_wlr_surface_at(struct cg_view *view, double sx, double sy, double *sub_x, double *sub_y)
|
|
{
|
|
assert(view != NULL);
|
|
assert(sub_x != NULL);
|
|
assert(sub_y != NULL);
|
|
assert(view->impl->wlr_surface_at != NULL);
|
|
|
|
return view->impl->wlr_surface_at(view, sx, sy, sub_x, sub_y);
|
|
}
|
|
|
|
void
|
|
cage_view_activate(struct cg_view *view, bool activate)
|
|
{
|
|
assert(view != NULL);
|
|
assert(view->impl->activate != NULL);
|
|
view->impl->activate(view, activate);
|
|
}
|
|
|
|
bool
|
|
cage_view_is_mapped(struct cg_view *view)
|
|
{
|
|
assert(view != NULL);
|
|
return view->wlr_surface != NULL;
|
|
}
|
|
|
|
void
|
|
cage_view_unmap(struct cg_view *view)
|
|
{
|
|
assert(view != NULL);
|
|
assert(cage_view_is_mapped(view));
|
|
|
|
wl_signal_emit(&view->events.unmap, view);
|
|
|
|
wl_list_remove(&view->link);
|
|
wl_list_init(&view->link);
|
|
|
|
view->wlr_surface->data = NULL;
|
|
view->wlr_surface = NULL;
|
|
|
|
assert(!cage_view_is_mapped(view));
|
|
}
|
|
|
|
void
|
|
cage_view_map(struct cg_view *view, struct wlr_surface *surface)
|
|
{
|
|
assert(view != NULL);
|
|
assert(surface != NULL);
|
|
assert(!cage_view_is_mapped(view));
|
|
|
|
view->wlr_surface = surface;
|
|
surface->data = view;
|
|
|
|
wl_list_insert(&view->output->views, &view->link);
|
|
cage_view_position(view);
|
|
|
|
wl_signal_emit(&view->events.map, view);
|
|
|
|
assert(cage_view_is_mapped(view));
|
|
}
|
|
|
|
void
|
|
cage_view_fini(struct cg_view *view)
|
|
{
|
|
assert(view != NULL);
|
|
assert(!cage_view_is_mapped(view));
|
|
view->output = NULL;
|
|
}
|
|
|
|
void
|
|
cage_view_init(struct cg_view *view, enum cg_view_type type, const struct cg_view_impl *impl, struct cg_output *output)
|
|
{
|
|
assert(view != NULL);
|
|
assert(impl != NULL);
|
|
assert(output != NULL);
|
|
|
|
view->type = type;
|
|
view->impl = impl;
|
|
view->output = output;
|
|
|
|
wl_signal_init(&view->events.map);
|
|
wl_signal_init(&view->events.unmap);
|
|
}
|