view: add proper abstraction with interface

This commit is contained in:
Jente Hidskes 2019-01-30 17:01:16 +01:00
parent 24517922cc
commit 64b971a665
7 changed files with 201 additions and 121 deletions

53
view.c
View file

@ -24,14 +24,14 @@
char *
view_get_title(struct cg_view *view)
{
const char *title = view->get_title(view);
const char *title = view->impl->get_title(view);
return strndup(title, strlen(title));
}
void
view_activate(struct cg_view *view, bool activate)
{
view->activate(view, activate);
view->impl->activate(view, activate);
}
static void
@ -41,7 +41,7 @@ view_maximize(struct cg_view *view)
int output_width, output_height;
wlr_output_transformed_resolution(output->wlr_output, &output_width, &output_height);
view->maximize(view, output_width, output_height);
view->impl->maximize(view, output_width, output_height);
}
static void
@ -53,29 +53,28 @@ view_center(struct cg_view *view)
wlr_output_transformed_resolution(output, &output_width, &output_height);
int width, height;
view->get_geometry(view, &width, &height);
view->impl->get_geometry(view, &width, &height);
view->x = (output_width - width) / 2;
view->y = (output_height - height) / 2;
}
void
view_for_each_surface(struct cg_view *view, wlr_surface_iterator_func_t iterator,
void *data)
view_for_each_surface(struct cg_view *view, wlr_surface_iterator_func_t iterator, void *data)
{
view->for_each_surface(view, iterator, data);
view->impl->for_each_surface(view, iterator, data);
}
struct wlr_surface *
view_wlr_surface_at(struct cg_view *view, double sx, double sy, double *sub_x, double *sub_y)
{
return view->wlr_surface_at(view, sx, sy, sub_x, sub_y);
return view->impl->wlr_surface_at(view, sx, sy, sub_x, sub_y);
}
bool
view_is_primary(struct cg_view *view)
{
return view->is_primary(view);
return view->impl->is_primary(view);
}
bool
@ -83,7 +82,7 @@ view_has_children(struct cg_server *server, struct cg_view *parent)
{
struct cg_view *child;
wl_list_for_each(child, &server->views, link) {
if (parent != child && parent->is_parent(parent, child)) {
if (parent != child && parent->impl->is_parent(parent, child)) {
return true;
}
}
@ -122,50 +121,32 @@ void
view_destroy(struct cg_view *view)
{
struct cg_server *server = view->server;
bool mapped = true;
#if CAGE_HAS_XWAYLAND
/* Some applications that aren't yet Wayland-native or
otherwise "special" (e.g. Firefox Nightly and Google
Chrome/Chromium) spawn an XWayland surface upon startup
that is almost immediately closed again. This makes Cage
think there are no views left, which results in it
exiting. However, after this initial (unmapped) surface,
the "real" application surface is opened. This leads to
these applications' startup sequences being interrupted by
Cage exiting. Hence, to work around this issue, Cage checks
whether an XWayland surface has been mapped and exits only
if 1) the XWayland surface has been mapped and 2) this was
the last surface Cage manages. */
if (view->type == CAGE_XWAYLAND_VIEW) {
mapped = view->xwayland_surface->mapped;
}
#endif
if (view->wlr_surface != NULL) {
view_unmap(view);
}
free(view);
view->impl->destroy(view);
/* If there is a previous view in the list, focus that. */
bool empty = wl_list_empty(&server->views);
if (!empty) {
struct cg_view *prev = wl_container_of(server->views.next, prev, link);
seat_set_focus(server->seat, prev);
} else if (mapped) {
} else {
/* The list is empty and the last view has been
mapped, so we can safely exit. */
wl_display_terminate(server->wl_display);
}
}
struct cg_view *
view_create(struct cg_server *server)
void
view_init(struct cg_view *view, struct cg_server *server, enum cg_view_type type,
const struct cg_view_impl *impl)
{
struct cg_view *view = calloc(1, sizeof(struct cg_view));
view->server = server;
return view;
view->type = type;
view->impl = impl;
}
struct cg_view *