<commit_before>/*
 * Westford Wayland Compositor.
 * Copyright (C) 2016  <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.westford.compositor.protocol;

import org.freedesktop.wayland.server.Client;
import org.freedesktop.wayland.server.Display;
import org.freedesktop.wayland.server.Global;
import org.freedesktop.wayland.server.WlCompositorRequestsV4;
import org.freedesktop.wayland.server.WlCompositorResource;
import org.freedesktop.wayland.server.WlSurfaceResource;
import org.westford.compositor.core.Compositor;
import org.westford.compositor.core.Renderer;
import org.westford.compositor.core.Scene;
import org.westford.compositor.core.Sibling;
import org.westford.compositor.core.Surface;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Collections;
import java.util.Set;
import java.util.WeakHashMap;

@Singleton
public class WlCompositor extends Global<WlCompositorResource> implements WlCompositorRequestsV4, ProtocolObject<WlCompositorResource> {

    private final Set<WlCompositorResource> resources = Collections.newSetFromMap(new WeakHashMap<>());

    @Nonnull
    private final WlSurfaceFactory                                 wlSurfaceFactory;
    @Nonnull
    private final WlRegionFactory                                  wlRegionFactory;
    @Nonnull
    private final org.westford.compositor.core.FiniteRegionFactory finiteRegionFactory;
    @Nonnull
    private final org.westford.compositor.core.SurfaceFactory      surfaceFactory;
    @Nonnull
    private final Compositor                                       compositor;
    @Nonnull
    private final Scene                                            scene;
    @Nonnull
    private final Renderer                                         renderer;

    @Inject
    WlCompositor(@Nonnull final Display display,
                 @Nonnull final WlSurfaceFactory wlSurfaceFactory,
                 @Nonnull final WlRegionFactory wlRegionFactory,
                 @Nonnull final org.westford.compositor.core.FiniteRegionFactory finiteRegionFactory,
                 @Nonnull final org.westford.compositor.core.SurfaceFactory surfaceFactory,
                 @Nonnull final Compositor compositor,
                 @Nonnull final Scene scene,
                 @Nonnull final Renderer renderer) {
        super(display,
              WlCompositorResource.class,
              VERSION);
        this.wlSurfaceFactory = wlSurfaceFactory;
        this.wlRegionFactory = wlRegionFactory;
        this.finiteRegionFactory = finiteRegionFactory;
        this.surfaceFactory = surfaceFactory;
        this.compositor = compositor;
        this.scene = scene;
        this.renderer = renderer;
    }

    @Override
    public WlCompositorResource onBindClient(final Client client,
                                             final int version,
                                             final int id) {
        return add(client,
                   version,
                   id);
    }

    @Override
    public void createSurface(final WlCompositorResource compositorResource,
                              final int id) {
        final Surface   surface   = this.surfaceFactory.create();
        final WlSurface wlSurface = this.wlSurfaceFactory.create(surface);

        final WlSurfaceResource wlSurfaceResource = wlSurface.add(compositorResource.getClient(),
                                                                  compositorResource.getVersion(),
                                                                  id);
        surface.getSiblings()
               .add(Sibling.create(wlSurfaceResource));

        //TODO unit test destroy handler
        wlSurfaceResource.register(() -> {
            //FIXME
            this.scene.getSurfacesStack()
                      .remove(wlSurfaceResource);<commit_msg>create view & add surface to scene when a new surface is created<commit_after>/*
 * Westford Wayland Compositor.
 * Copyright (C) 2016  <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.westford.compositor.protocol;

import org.freedesktop.wayland.server.Client;
import org.freedesktop.wayland.server.Display;
import org.freedesktop.wayland.server.Global;
import org.freedesktop.wayland.server.WlCompositorRequestsV4;
import org.freedesktop.wayland.server.WlCompositorResource;
import org.freedesktop.wayland.server.WlSurfaceResource;
import org.westford.compositor.core.Compositor;
import org.westford.compositor.core.Point;
import org.westford.compositor.core.Renderer;
import org.westford.compositor.core.Scene;
import org.westford.compositor.core.Sibling;
import org.westford.compositor.core.Surface;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Collections;
import java.util.Set;
import java.util.WeakHashMap;

@Singleton
public class WlCompositor extends Global<WlCompositorResource> implements WlCompositorRequestsV4, ProtocolObject<WlCompositorResource> {

    private final Set<WlCompositorResource> resources = Collections.newSetFromMap(new WeakHashMap<>());

    @Nonnull
    private final WlSurfaceFactory                                 wlSurfaceFactory;
    @Nonnull
    private final WlRegionFactory                                  wlRegionFactory;
    @Nonnull
    private final org.westford.compositor.core.FiniteRegionFactory finiteRegionFactory;
    @Nonnull
    private final org.westford.compositor.core.SurfaceFactory      surfaceFactory;
    @Nonnull
    private final Compositor                                       compositor;
    @Nonnull
    private final Scene                                            scene;
    @Nonnull
    private final Renderer                                         renderer;

    @Inject
    WlCompositor(@Nonnull final Display display,
                 @Nonnull final WlSurfaceFactory wlSurfaceFactory,
                 @Nonnull final WlRegionFactory wlRegionFactory,
                 @Nonnull final org.westford.compositor.core.FiniteRegionFactory finiteRegionFactory,
                 @Nonnull final org.westford.compositor.core.SurfaceFactory surfaceFactory,
                 @Nonnull final Compositor compositor,
                 @Nonnull final Scene scene,
                 @Nonnull final Renderer renderer) {
        super(display,
              WlCompositorResource.class,
              VERSION);
        this.wlSurfaceFactory = wlSurfaceFactory;
        this.wlRegionFactory = wlRegionFactory;
        this.finiteRegionFactory = finiteRegionFactory;
        this.surfaceFactory = surfaceFactory;
        this.compositor = compositor;
        this.scene = scene;
        this.renderer = renderer;
    }

    @Override
    public WlCompositorResource onBindClient(final Client client,
                                             final int version,
                                             final int id) {
        return add(client,
                   version,
                   id);
    }

    @Override
    public void createSurface(final WlCompositorResource compositorResource,
                              final int id) {
        final Surface   surface   = this.surfaceFactory.create();
        final WlSurface wlSurface = this.wlSurfaceFactory.create(surface);

        final WlSurfaceResource wlSurfaceResource = wlSurface.add(compositorResource.getClient(),
                                                                  compositorResource.getVersion(),
                                                                  id);
        //TODO we might want to move view creation to role object
        surface.createView(wlSurfaceResource,
                           Point.ZERO);
        surface.getSiblings()
               .add(Sibling.create(wlSurfaceResource));

        //adding a surface to the scene should be done when we add/create a new role
        this.scene.getSurfacesStack()
                  .add(wlSurfaceResource);

        //TODO unit test destroy handler
        wlSurfaceResource.register(() -> {
            //FIXME
            this.scene.getSurfacesStack()
                      .remove(wlSurfaceResource);
{'fraction_non_alphanumeric': 0.06500895585291329, 'fraction_numerical': 0.001475081656305974, 'mean_word_length': 2.2629769680302507, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3230589', 'n_tokens_mistral': 2369, 'n_tokens_neox': 2321, 'n_words': 638}