main neuswc / libswc / subcompositor.c
  1/* swc: libswc/subcompositor.c
  2 *
  3 * Copyright (c) 2015-2020 Michael Forney
  4 *
  5 * Permission is hereby granted, free of charge, to any person obtaining a copy
  6 * of this software and associated documentation files (the "Software"), to deal
  7 * in the Software without restriction, including without limitation the rights
  8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9 * copies of the Software, and to permit persons to whom the Software is
 10 * furnished to do so, subject to the following conditions:
 11 *
 12 * The above copyright notice and this permission notice shall be included in
 13 * all copies or substantial portions of the Software.
 14 *
 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 21 * SOFTWARE.
 22 */
 23
 24#include "subcompositor.h"
 25#include "internal.h"
 26#include "subsurface.h"
 27#include "surface.h"
 28#include "swc.h"
 29#include "util.h"
 30
 31static bool
 32is_descendant_of(struct surface *ancestor, struct surface *surface)
 33{
 34	while (surface && surface->subsurface) {
 35		surface = surface->subsurface->parent;
 36		if (surface == ancestor) {
 37			return true;
 38		}
 39	}
 40
 41	return false;
 42}
 43
 44static void
 45get_subsurface(struct wl_client *client, struct wl_resource *resource,
 46               uint32_t id, struct wl_resource *surface_resource,
 47               struct wl_resource *parent_resource)
 48{
 49	struct subsurface *subsurface;
 50	struct surface *surface = wl_resource_get_user_data(surface_resource);
 51	struct surface *parent = wl_resource_get_user_data(parent_resource);
 52
 53	if (!surface || !parent) {
 54		wl_resource_post_error(resource, WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
 55		                       "invalid surface");
 56		return;
 57	}
 58
 59	if (surface == parent || is_descendant_of(surface, parent)) {
 60		wl_resource_post_error(resource, WL_SUBCOMPOSITOR_ERROR_BAD_PARENT,
 61		                       "invalid parent surface");
 62		return;
 63	}
 64
 65	if (surface->subsurface) {
 66		wl_resource_post_error(resource, WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
 67		                       "surface already has a subsurface role");
 68		return;
 69	}
 70
 71	subsurface = subsurface_new(client, wl_resource_get_version(resource), id,
 72	                            surface, parent);
 73
 74	if (!subsurface) {
 75		wl_resource_post_no_memory(resource);
 76		return;
 77	}
 78	surface->subsurface = subsurface;
 79}
 80
 81static const struct wl_subcompositor_interface subcompositor_impl = {
 82    .destroy = destroy_resource,
 83    .get_subsurface = get_subsurface,
 84};
 85
 86static void
 87bind_subcompositor(struct wl_client *client, void *data, uint32_t version,
 88                   uint32_t id)
 89{
 90	struct wl_resource *resource;
 91
 92	resource =
 93	    wl_resource_create(client, &wl_subcompositor_interface, version, id);
 94	if (!resource) {
 95		wl_client_post_no_memory(client);
 96		return;
 97	}
 98	wl_resource_set_implementation(resource, &subcompositor_impl, NULL, NULL);
 99}
100
101struct wl_global *
102subcompositor_create(struct wl_display *display)
103{
104	return wl_global_create(display, &wl_subcompositor_interface, 1, NULL,
105	                        &bind_subcompositor);
106}