Added initial support for testing harness
authorMarius Vlad <marius.vlad@collabora.com>
Tue, 13 Apr 2021 10:24:35 +0000 (13:24 +0300)
committerMarius Vlad <marius.vlad@collabora.com>
Fri, 16 Apr 2021 12:49:04 +0000 (15:49 +0300)
Signed-off-by: Marius Vlad <marius.vlad@collabora.com>
Change-Id: I4ba36795c88c1814e352460a44ca7e7303bb4d8c

16 files changed:
meson.build
protocol/weston-test.xml [new file with mode: 0644]
src/compositor.c [new file with mode: 0644]
src/main.c
src/policy-default.c
src/systemd-notify.h [new file with mode: 0644]
tests/agl-shell-test.c [new file with mode: 0644]
tests/meson.build [new file with mode: 0644]
tests/testsuite-util.c [new file with mode: 0644]
tests/weston-test-client-helper.c [new file with mode: 0644]
tests/weston-test-client-helper.h [new file with mode: 0644]
tests/weston-test-fixture-compositor.c [new file with mode: 0644]
tests/weston-test-fixture-compositor.h [new file with mode: 0644]
tests/weston-test-runner.c [new file with mode: 0644]
tests/weston-test-runner.h [new file with mode: 0644]
tests/weston-testsuite-data.h [new file with mode: 0644]

index 081b127..d678c34 100644 (file)
@@ -13,12 +13,14 @@ config_h = configuration_data()
 agl_compositor_version = '0.0.19'
 libweston_version = 'libweston-9'
 pkgconfig = import('pkgconfig')
+env_modmap = ''
 
 cc = meson.get_compiler('c')
 cxx = meson.get_compiler('cpp')
 
 add_project_arguments(
   cc.get_supported_arguments([
+    '-Wno-missing-field-initializers',
     '-Wno-unused-parameter',
     '-Wno-pedantic',
     '-Wextra',
@@ -93,7 +95,9 @@ protocols = [
   { 'name': 'agl-shell', 'source': 'internal' },
   { 'name': 'agl-shell-desktop', 'source': 'internal' },
   { 'name': 'agl-screenshooter', 'source': 'internal' },
+  { 'name': 'weston-test', 'source': 'internal' },
   { 'name': 'xdg-shell', 'source': 'wp-stable' },
+  { 'name': 'viewporter', 'source': 'wp-stable' },
   { 'name': 'xdg-output', 'source': 'unstable', 'version': 'v1' },
 ]
 
@@ -159,7 +163,7 @@ deps_libweston = [
 ]
 
 srcs_agl_compositor = [
-       'src/main.c',
+       'src/compositor.c',
        'src/desktop.c',
        'src/layout.c',
        'src/policy.c',
@@ -177,6 +181,7 @@ srcs_agl_compositor = [
        xdg_shell_protocol_c,
 ]
 
+
 policy_to_install = get_option('policy-default')
 if policy_to_install == 'auto' or policy_to_install == 'allow-all'
   srcs_agl_compositor += 'src/policy-default.c'
@@ -215,7 +220,6 @@ endif
 
 if dep_libsystemd.found()
   config_h.set('HAVE_SYSTEMD', 1)
-
   srcs_agl_compositor += 'src/systemd-notify.c'
   deps_libweston += dep_libsystemd
 
@@ -239,10 +243,26 @@ endif
 
 configure_file(output: 'config.h', configuration: config_h)
 
+libexec_compositor = shared_library(
+        'exec_compositor',
+        sources: srcs_agl_compositor,
+        dependencies: deps_libweston,
+#install_dir: dir_module_weston,
+        install: true,
+        version: '0.0.0',
+        soversion: 0
+)
+
+dep_libexec_compositor = declare_dependency(
+        link_with: libexec_compositor,
+        include_directories: [ include_directories('.') ],
+        dependencies: deps_libweston,
+)
+
 exe_agl_compositor = executable(
        'agl-compositor',
-       srcs_agl_compositor,
-       dependencies: deps_libweston,
+       'src/main.c',
+       dependencies: dep_libexec_compositor,
        install: true
 )
 
@@ -265,3 +285,4 @@ install_data(
 
 common_inc = [ include_directories('src'), include_directories('.') ]
 subdir('clients')
+subdir('tests')
diff --git a/protocol/weston-test.xml b/protocol/weston-test.xml
new file mode 100644 (file)
index 0000000..00b7185
--- /dev/null
@@ -0,0 +1,144 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<protocol name="weston_test">
+
+  <copyright>
+    Copyright © 2012 Intel Corporation
+
+    Permission is hereby granted, free of charge, to any person obtaining a
+    copy of this software and associated documentation files (the "Software"),
+    to deal in the Software without restriction, including without limitation
+    the rights to use, copy, modify, merge, publish, distribute, sublicense,
+    and/or sell copies of the Software, and to permit persons to whom the
+    Software is furnished to do so, subject to the following conditions:
+
+    The above copyright notice and this permission notice (including the next
+    paragraph) shall be included in all copies or substantial portions of the
+    Software.
+
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+    DEALINGS IN THE SOFTWARE.
+  </copyright>
+
+  <interface name="weston_test" version="1">
+    <description summary="weston internal testing">
+      Internal testing facilities for the weston compositor.
+
+      It can't be stressed enough that these should never ever be used
+      outside of running weston's tests.  The weston-test.so module should
+      never be installed.
+
+      These requests may allow clients to do very bad things.
+    </description>
+    <request name="move_surface">
+      <arg name="surface" type="object" interface="wl_surface"/>
+      <arg name="x" type="int"/>
+      <arg name="y" type="int"/>
+    </request>
+    <request name="move_pointer">
+      <arg name="tv_sec_hi" type="uint"/>
+      <arg name="tv_sec_lo" type="uint"/>
+      <arg name="tv_nsec" type="uint"/>
+      <arg name="x" type="int"/>
+      <arg name="y" type="int"/>
+    </request>
+    <request name="send_button">
+      <arg name="tv_sec_hi" type="uint"/>
+      <arg name="tv_sec_lo" type="uint"/>
+      <arg name="tv_nsec" type="uint"/>
+      <arg name="button" type="int"/>
+      <arg name="state" type="uint"/>
+    </request>
+    <request name="send_axis">
+      <arg name="tv_sec_hi" type="uint"/>
+      <arg name="tv_sec_lo" type="uint"/>
+      <arg name="tv_nsec" type="uint"/>
+      <arg name="axis" type="uint"/>
+      <arg name="value" type="fixed"/>
+    </request>
+    <request name="activate_surface">
+      <arg name="surface" type="object" interface="wl_surface" allow-null="true"/>
+    </request>
+    <request name="send_key">
+      <arg name="tv_sec_hi" type="uint"/>
+      <arg name="tv_sec_lo" type="uint"/>
+      <arg name="tv_nsec" type="uint"/>
+      <arg name="key" type="uint"/>
+      <arg name="state" type="uint"/>
+    </request>
+    <request name="device_release">
+      <arg name="device" type="string"/>
+    </request>
+    <request name="device_add">
+      <arg name="device" type="string"/>
+    </request>
+    <event name="pointer_position">
+      <arg name="x" type="fixed"/>
+      <arg name="y" type="fixed"/>
+    </event>
+    <request name="capture_screenshot">
+      <description summary="records current screen image">
+        Records an image of what is currently displayed on a given
+        display output, returning the image as an event.
+      </description>
+      <arg name="output" type="object" interface="wl_output"
+           summary="output to capture from"/>
+      <arg name="buffer" type="object" interface="wl_buffer"
+           summary="buffer for returning screenshots to the test client"/>
+    </request>
+    <event name="capture_screenshot_done">
+         <description summary="screenshot capture is done">
+               The capture_screenshot_done signal is sent when a screenshot has been copied into the
+               provided buffer.
+         </description>
+    </event>
+    <request name="send_touch">
+      <arg name="tv_sec_hi" type="uint"/>
+      <arg name="tv_sec_lo" type="uint"/>
+      <arg name="tv_nsec" type="uint"/>
+      <arg name="touch_id" type="int"/>
+      <arg name="x" type="fixed"/>
+      <arg name="y" type="fixed"/>
+      <arg name="touch_type" type="uint"/>
+    </request>
+  </interface>
+
+  <interface name="weston_test_runner" version="1">
+    <description summary="weston internal testing">
+      This is a global singleton interface for Weston internal tests.
+
+      This interface allows a test client to trigger compositor-side
+      test procedures. This is useful for cases, where the actual tests
+      are in compositor plugins, but they also require the presence of
+      a particular client.
+
+      This interface is implemented by the compositor plugins containing
+      the testing code.
+
+      A test client starts a test with the "run" request. It must not send
+      another "run" request until receiving the "finished" event. If the
+      compositor-side test succeeds, the "finished" event is sent. If the
+      compositor-side test fails, the compositor should send the protocol
+      error "test_failed", but it may also exit with an error (e.g. SEGV).
+
+      Unknown test name will raise "unknown_test" protocol error.
+    </description>
+
+    <enum name="error">
+      <entry name="test_failed" value="0" summary="compositor test failed"/>
+      <entry name="unknown_test" value="1" summary="unrecognized test name"/>
+    </enum>
+
+    <request name="destroy" type="destructor"/>
+
+    <request name="run">
+      <arg name="test_name" type="string"/>
+    </request>
+
+    <event name="finished"/>
+  </interface>
+</protocol>
diff --git a/src/compositor.c b/src/compositor.c
new file mode 100644 (file)
index 0000000..614c847
--- /dev/null
@@ -0,0 +1,1635 @@
+/*
+ * Copyright © 2012-2021 Collabora, Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "ivi-compositor.h"
+#include "policy.h"
+
+#include <assert.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <linux/input.h>
+
+#include <libweston/backend-drm.h>
+#include <libweston/backend-wayland.h>
+#ifdef HAVE_BACKEND_X11
+#include <libweston/backend-x11.h>
+#endif
+#include <libweston/libweston.h>
+#include <libweston/windowed-output-api.h>
+#include <libweston/config-parser.h>
+#include <libweston/weston-log.h>
+#include <weston/weston.h>
+
+#include "shared/os-compatibility.h"
+#include "shared/helpers.h"
+
+#include "agl-shell-server-protocol.h"
+
+#ifdef HAVE_REMOTING
+#include "remote.h"
+#endif
+
+#ifdef HAVE_WALTHAM
+#include <waltham-transmitter/transmitter_api.h>
+#endif
+
+static int cached_tm_mday = -1;
+static struct weston_log_scope *log_scope;
+
+struct ivi_compositor *
+to_ivi_compositor(struct weston_compositor *ec)
+{
+       return weston_compositor_get_user_data(ec);
+}
+
+static void
+handle_output_destroy(struct wl_listener *listener, void *data)
+{
+       struct ivi_output *output;
+
+       output = wl_container_of(listener, output, output_destroy);
+       assert(output->output == data);
+
+       if (output->fullscreen_view.fs->view) {
+               weston_surface_destroy(output->fullscreen_view.fs->view->surface);
+               output->fullscreen_view.fs->view = NULL;
+       }
+
+       output->output = NULL;
+       wl_list_remove(&output->output_destroy.link);
+}
+
+struct ivi_output *
+to_ivi_output(struct weston_output *o)
+{
+       struct wl_listener *listener;
+       struct ivi_output *output;
+
+       listener = weston_output_get_destroy_listener(o, handle_output_destroy);
+       output = wl_container_of(listener, output, output_destroy);
+
+       return output;
+}
+
+static void
+ivi_output_configure_app_id(struct ivi_output *ivi_output)
+{
+       if (ivi_output->config) {
+               if (ivi_output->app_id != NULL)
+                       return;
+
+               weston_config_section_get_string(ivi_output->config,
+                                                "agl-shell-app-id",
+                                                &ivi_output->app_id,
+                                                NULL);
+
+               if (ivi_output->app_id == NULL)
+                       return;
+
+               weston_log("Will place app_id %s on output %s\n",
+                               ivi_output->app_id, ivi_output->name);
+       }
+}
+
+static struct ivi_output *
+ivi_ensure_output(struct ivi_compositor *ivi, char *name,
+                 struct weston_config_section *config)
+{
+       struct ivi_output *output = NULL;
+       wl_list_for_each(output, &ivi->outputs, link) {
+               if (strcmp(output->name, name) == 0) {
+                       free(name);
+                       return output;
+               }
+       }
+
+       output = zalloc(sizeof *output);
+       if (!output) {
+               free(name);
+               return NULL;
+       }
+
+       output->ivi = ivi;
+       output->name = name;
+       output->config = config;
+
+       output->output = weston_compositor_create_output(ivi->compositor, name);
+       if (!output->output) {
+               free(output->name);
+               free(output);
+               return NULL;
+       }
+
+       output->output_destroy.notify = handle_output_destroy;
+       weston_output_add_destroy_listener(output->output,
+                                          &output->output_destroy);
+
+       wl_list_insert(&ivi->outputs, &output->link);
+       ivi_output_configure_app_id(output);
+       return output;
+}
+
+static int
+count_heads(struct weston_output *output)
+{
+       struct weston_head *iter = NULL;
+       int n = 0;
+
+       while ((iter = weston_output_iterate_heads(output, iter)))
+               ++n;
+
+       return n;
+}
+
+static void
+handle_head_destroy(struct wl_listener *listener, void *data)
+{
+       struct weston_head *head = data;
+       struct weston_output *output;
+
+       wl_list_remove(&listener->link);
+       free(listener);
+
+       output = weston_head_get_output(head);
+
+       /* On shutdown path, the output might be already gone. */
+       if (!output)
+               return;
+
+       /* We're the last head */
+       if (count_heads(output) <= 1)
+               weston_output_destroy(output);
+}
+
+static void
+add_head_destroyed_listener(struct weston_head *head)
+{
+       /* We already have a destroy listener */
+       if (weston_head_get_destroy_listener(head, handle_head_destroy))
+               return;
+
+       struct wl_listener *listener = zalloc(sizeof *listener);
+       if (!listener)
+               return;
+
+       listener->notify = handle_head_destroy;
+       weston_head_add_destroy_listener(head, listener);
+}
+
+static int
+drm_configure_output(struct ivi_output *output)
+{
+       struct ivi_compositor *ivi = output->ivi;
+       struct weston_config_section *section = output->config;
+       enum weston_drm_backend_output_mode mode =
+               WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
+       char *modeline = NULL;
+       char *gbm_format = NULL;
+       char *seat = NULL;
+
+       if (section) {
+               char *m;
+               weston_config_section_get_string(section, "mode", &m, "preferred");
+
+               /* This should have been handled earlier */
+               assert(strcmp(m, "off") != 0);
+
+               if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
+                       mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
+               } else if (strcmp(m, "preferred") != 0) {
+                       modeline = m;
+                       m = NULL;
+               }
+               free(m);
+
+               weston_config_section_get_string(section, "gbm-format",
+                                                &gbm_format, NULL);
+
+               weston_config_section_get_string(section, "seat", &seat, "");
+       }
+
+       if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
+               weston_log("Cannot configure output using weston_drm_output_api.\n");
+               free(modeline);
+               return -1;
+       }
+       free(modeline);
+
+       ivi->drm_api->set_gbm_format(output->output, gbm_format);
+       free(gbm_format);
+
+       ivi->drm_api->set_seat(output->output, seat);
+       free(seat);
+
+       return 0;
+}
+
+#define WINDOWED_DEFAULT_WIDTH 1024
+#define WINDOWED_DEFAULT_HEIGHT 768
+
+static int
+windowed_configure_output(struct ivi_output *output)
+{
+       struct ivi_compositor *ivi = output->ivi;
+       struct weston_config_section *section = output->config;
+       int width = WINDOWED_DEFAULT_WIDTH;
+       int height = WINDOWED_DEFAULT_HEIGHT;
+
+       if (section) {
+               char *mode;
+
+               weston_config_section_get_string(section, "mode", &mode, NULL);
+               if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
+                       weston_log("Invalid mode for output %s. Using defaults.\n",
+                                  output->name);
+                       width = WINDOWED_DEFAULT_WIDTH;
+                       height = WINDOWED_DEFAULT_HEIGHT;
+               }
+               free(mode);
+       }
+
+       if (ivi->cmdline.width)
+               width = ivi->cmdline.width;
+       if (ivi->cmdline.height)
+               height = ivi->cmdline.height;
+       if (ivi->cmdline.scale)
+               weston_output_set_scale(output->output, ivi->cmdline.scale);
+
+       if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
+               weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
+                          output->name);
+               return -1;
+       }
+
+       return 0;
+}
+
+static int
+parse_transform(const char *transform, uint32_t *out)
+{
+       static const struct { const char *name; uint32_t token; } transforms[] = {
+               { "normal",     WL_OUTPUT_TRANSFORM_NORMAL },
+               { "90",         WL_OUTPUT_TRANSFORM_90 },
+               { "180",        WL_OUTPUT_TRANSFORM_180 },
+               { "270",        WL_OUTPUT_TRANSFORM_270 },
+               { "flipped",    WL_OUTPUT_TRANSFORM_FLIPPED },
+               { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
+               { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
+               { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
+       };
+
+       for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
+               if (strcmp(transforms[i].name, transform) == 0) {
+                       *out = transforms[i].token;
+                       return 0;
+               }
+
+       *out = WL_OUTPUT_TRANSFORM_NORMAL;
+       return -1;
+}
+
+static int
+configure_output(struct ivi_output *output)
+{
+       struct ivi_compositor *ivi = output->ivi;
+       struct weston_config_section *section = output->config;
+       int32_t scale = 1;
+       uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
+
+       /*
+        * This can happen with the wayland backend with 'sprawl'. The config
+        * is hard-coded, so we don't need to do anything.
+        */
+       if (!ivi->drm_api && !ivi->window_api)
+               return 0;
+
+       if (section) {
+               char *t;
+
+               weston_config_section_get_int(section, "scale", &scale, 1);
+               weston_config_section_get_string(section, "transform", &t, "normal");
+               if (parse_transform(t, &transform) < 0)
+                       weston_log("Invalid transform \"%s\" for output %s\n",
+                                  t, output->name);
+               free(t);
+       }
+
+       weston_output_set_scale(output->output, scale);
+       weston_output_set_transform(output->output, transform);
+
+       if (ivi->drm_api)
+               return drm_configure_output(output);
+       else
+               return windowed_configure_output(output);
+}
+
+/*
+ * Reorgainizes the output's add array into two sections.
+ * add[0..ret-1] are the heads that failed to get attached.
+ * add[ret..add_len] are the heads that were successfully attached.
+ *
+ * The order between elements in each section is stable.
+ */
+static size_t
+try_attach_heads(struct ivi_output *output)
+{
+       size_t fail_len = 0;
+
+       for (size_t i = 0; i < output->add_len; ++i) {
+               if (weston_output_attach_head(output->output, output->add[i]) < 0) {
+                       struct weston_head *tmp = output->add[i];
+                       memmove(&output->add[fail_len + 1], output->add[fail_len],
+                               sizeof output->add[0] * (i - fail_len));
+                       output->add[fail_len++] = tmp;
+               }
+       }
+
+       return fail_len;
+}
+
+/*
+ * Like try_attach_heads, this reorganizes the output's add array into a failed
+ * and successful section.
+ * i is the number of heads that already failed the previous step.
+ */
+static size_t
+try_enable_output(struct ivi_output *output, size_t i)
+{
+       for (; i < output->add_len; ++i) {
+               struct weston_head *head;
+
+               if (weston_output_enable(output->output) == 0)
+                       break;
+
+               head = output->add[output->add_len - 1];
+               memmove(&output->add[i + 1], &output->add[i],
+                       sizeof output->add[0] * (output->add_len - i));
+               output->add[i] = head;
+
+               weston_head_detach(head);
+       }
+
+       return i;
+}
+
+static int
+try_attach_enable_heads(struct ivi_output *output)
+{
+       size_t fail_len;
+       assert(!output->output->enabled);
+
+       fail_len = try_attach_heads(output);
+
+       if (configure_output(output) < 0)
+               return -1;
+
+       fail_len = try_enable_output(output, fail_len);
+
+       /* All heads failed to be attached */
+       if (fail_len == output->add_len)
+               return -1;
+
+       /* For each successful head attached */
+       for (size_t i = fail_len; i < output->add_len; ++i)
+               add_head_destroyed_listener(output->add[i]);
+
+       output->add_len = fail_len;
+       return 0;
+}
+
+static int
+process_output(struct ivi_output *output)
+{
+       if (output->output->enabled) {
+               output->add_len = try_attach_heads(output);
+               return output->add_len == 0 ? 0 : -1;
+       }
+
+       return try_attach_enable_heads(output);
+}
+
+static void
+head_disable(struct ivi_compositor *ivi, struct weston_head *head)
+{
+       struct weston_output *output;
+       struct ivi_output *ivi_output;
+       struct wl_listener *listener;
+
+       output = weston_head_get_output(head);
+       assert(output);
+
+       listener = weston_output_get_destroy_listener(output,
+                                                     handle_output_destroy);
+       assert(listener);
+
+       ivi_output = wl_container_of(listener, ivi_output, output_destroy);
+       assert(ivi_output->output == output);
+
+       weston_head_detach(head);
+       if (count_heads(ivi_output->output) == 0) {
+               weston_output_disable(ivi_output->output);
+       }
+}
+
+static struct weston_config_section *
+find_controlling_output_config(struct weston_config *config,
+                              const char *name)
+{
+       struct weston_config_section *section;
+       char *same_as;
+       int depth = 0;
+
+       same_as = strdup(name);
+       do {
+               section = weston_config_get_section(config, "output",
+                                                   "name", same_as);
+               if (!section && depth > 0)
+                       weston_log("Configuration error: output section reffered"
+                                  "to by same-as=%s not found.\n", same_as);
+               free(same_as);
+
+               if (!section)
+                       return NULL;
+
+               if (depth++ > 8) {
+                       weston_log("Configuration error: same-as nested too "
+                                  "deep for output '%s'.\n", name);
+                       return NULL;
+               }
+
+               weston_config_section_get_string(section, "same-as",
+                                                &same_as, NULL);
+       } while (same_as);
+
+       return section;
+}
+
+static void
+head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
+{
+       const char *name = weston_head_get_name(head);
+       struct weston_config_section *section;
+       struct ivi_output *output;
+       char *output_name = NULL;
+
+       section = find_controlling_output_config(ivi->config, name);
+       if (section) {
+               char *mode;
+
+               weston_config_section_get_string(section, "mode", &mode, NULL);
+               if (mode && strcmp(mode, "off") == 0) {
+                       free(mode);
+                       return;
+               }
+               free(mode);
+
+               weston_config_section_get_string(section, "name",
+                                                &output_name, NULL);
+       } else {
+               output_name = strdup(name);
+       }
+
+       if (!output_name)
+               return;
+
+       output = ivi_ensure_output(ivi, output_name, section);
+       if (!output)
+               return;
+
+       if (output->add_len >= ARRAY_LENGTH(output->add))
+               return;
+
+       output->add[output->add_len++] = head;
+}
+
+static void
+heads_changed(struct wl_listener *listener, void *arg)
+{
+       struct weston_compositor *compositor = arg;
+       struct weston_head *head = NULL;
+       struct ivi_compositor *ivi = to_ivi_compositor(compositor);
+       struct ivi_output *output;
+
+       while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
+               bool connected = weston_head_is_connected(head);
+               bool enabled = weston_head_is_enabled(head);
+               bool changed = weston_head_is_device_changed(head);
+               bool non_desktop = weston_head_is_non_desktop(head);
+
+               if (connected && !enabled && !non_desktop)
+                       head_prepare_enable(ivi, head);
+               else if (!connected && enabled)
+                       head_disable(ivi, head);
+               else if (enabled && changed)
+                       weston_log("Detected a monitor change on head '%s', "
+                                  "not bothering to do anything about it.\n",
+                                  weston_head_get_name(head));
+
+               weston_head_reset_device_changed(head);
+       }
+
+       wl_list_for_each(output, &ivi->outputs, link) {
+               if (output->add_len == 0)
+                       continue;
+
+               if (process_output(output) < 0) {
+                       output->add_len = 0;
+                       ivi->init_failed = true;
+               }
+       }
+}
+
+#ifdef HAVE_WALTHAM
+static int
+load_waltham_plugin(struct ivi_compositor *ivi, struct weston_config *config)
+{
+       struct weston_compositor *compositor = ivi->compositor;
+       int (*module_init)(struct weston_compositor *wc);
+
+       module_init = weston_load_module("waltham-transmitter.so",
+                                        "wet_module_init");
+       if (!module_init)
+               return -1;
+
+       if (module_init(compositor) < 0)
+               return -1;
+
+       ivi->waltham_transmitter_api = weston_get_transmitter_api(compositor);
+       if (!ivi->waltham_transmitter_api) {
+               weston_log("Failed to load waltham-transmitter plugin.\n");
+               return -1;
+       }
+
+       weston_log("waltham-transmitter plug-in loaded\n");
+       return 0;
+}
+#else
+static int
+load_waltham_plugin(struct ivi_compositor *ivi, struct weston_config *config)
+{
+       return -1;
+}
+#endif
+
+#ifdef HAVE_REMOTING
+static int
+drm_backend_remoted_output_configure(struct weston_output *output,
+                                    struct weston_config_section *section,
+                                    char *modeline,
+                                    const struct weston_remoting_api *api)
+{
+       char *gbm_format = NULL;
+       char *seat = NULL;
+       char *host = NULL;
+       char *pipeline = NULL;
+       int port, ret;
+       int32_t scale = 1;
+       uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
+       char *trans;
+
+       ret = api->set_mode(output, modeline);
+       if (ret < 0) {
+               weston_log("Cannot configure an output \"%s\" using "
+                               "weston_remoting_api. Invalid mode\n",
+                               output->name);
+               return -1;
+       }
+
+       weston_config_section_get_int(section, "scale", &scale, 1);
+       weston_output_set_scale(output, scale);
+
+       weston_config_section_get_string(section, "transform", &trans, "normal");
+       if (parse_transform(trans, &transform) < 0) {
+               weston_log("Invalid transform \"%s\" for output %s\n",
+                          trans, output->name);
+       }
+       weston_output_set_transform(output, transform);
+
+       weston_config_section_get_string(section, "gbm-format",
+                                        &gbm_format, NULL);
+       api->set_gbm_format(output, gbm_format);
+       free(gbm_format);
+
+       weston_config_section_get_string(section, "seat", &seat, "");
+
+       api->set_seat(output, seat);
+       free(seat);
+
+       weston_config_section_get_string(section, "gst-pipeline", &pipeline,
+                       NULL);
+       if (pipeline) {
+               api->set_gst_pipeline(output, pipeline);
+               free(pipeline);
+               return 0;
+       }
+
+       weston_config_section_get_string(section, "host", &host, NULL);
+       weston_config_section_get_int(section, "port", &port, 0);
+       if (!host || port <= 0 || 65533 < port) {
+               weston_log("Cannot configure an output \"%s\". "
+                               "Need to specify gst-pipeline or "
+                               "host and port (1-65533).\n", output->name);
+       }
+       api->set_host(output, host);
+       free(host);
+       api->set_port(output, port);
+
+       return 0;
+}
+
+
+static int
+remote_output_init(struct ivi_output *ivi_output,
+                  struct weston_compositor *compositor,
+                  struct weston_config_section *section,
+                  const struct weston_remoting_api *api)
+{
+       char *output_name, *modeline = NULL;
+       int ret = -1;
+
+       weston_config_section_get_string(section, "name", &output_name, NULL);
+       if (!output_name)
+               return ret;
+
+       weston_config_section_get_string(section, "mode", &modeline, "off");
+       if (strcmp(modeline, "off") == 0)
+               goto err;
+
+       ivi_output->output = api->create_output(compositor, output_name);
+       if (!ivi_output->output) {
+               weston_log("Cannot create remoted output \"%s\".\n",
+                               output_name);
+               goto err;
+       }
+
+       ret = drm_backend_remoted_output_configure(ivi_output->output, section,
+                                                  modeline, api);
+       if (ret < 0) {
+               weston_log("Cannot configure remoted output \"%s\".\n",
+                               output_name);
+               goto err;
+       }
+
+       if (weston_output_enable(ivi_output->output) < 0) {
+               weston_log("Enabling remoted output \"%s\" failed.\n",
+                               output_name);
+               goto err;
+       }
+
+       free(modeline);
+       free(output_name);
+       weston_log("remoted output '%s' enabled\n", ivi_output->output->name);
+
+       return 0;
+
+err:
+       free(modeline);
+       free(output_name);
+       if (ivi_output->output)
+               weston_output_destroy(ivi_output->output);
+
+       return ret;
+}
+
+static void
+ivi_enable_remote_outputs(struct ivi_compositor *ivi)
+{
+       struct weston_config_section *remote_section = NULL;
+       const char *section_name;
+       struct weston_config *config = ivi->config;
+
+       while (weston_config_next_section(config, &remote_section, &section_name)) {
+               if (strcmp(section_name, "remote-output"))
+                       continue;
+
+               struct ivi_output *ivi_output = NULL;
+               bool output_found = false;
+               char *_name = NULL;
+
+               weston_config_section_get_string(remote_section,
+                                                "name", &_name, NULL);
+               wl_list_for_each(ivi_output, &ivi->outputs, link) {
+                       if (!strcmp(ivi_output->name, _name)) {
+                               output_found = true;
+                               break;
+                       }
+               }
+
+               if (output_found) {
+                       free(_name);
+                       continue;
+               }
+
+               ivi_output = zalloc(sizeof(*ivi_output));
+
+               ivi_output->ivi = ivi;
+               ivi_output->name = _name;
+               ivi_output->config = remote_section;
+               ivi_output->type = OUTPUT_REMOTE;
+
+               if (remote_output_init(ivi_output, ivi->compositor,
+                                      remote_section, ivi->remoting_api)) {
+                       free(ivi_output->name);
+                       free(ivi_output);
+                       continue;
+               }
+
+               ivi_output->output_destroy.notify = handle_output_destroy;
+               weston_output_add_destroy_listener(ivi_output->output,
+                                                  &ivi_output->output_destroy);
+
+               wl_list_insert(&ivi->outputs, &ivi_output->link);
+               ivi_output_configure_app_id(ivi_output);
+       }
+}
+
+static void
+ivi_enable_waltham_outputs(struct ivi_compositor *ivi)
+{
+       struct weston_config_section *transmitter_section = NULL;
+       const char *sect_name;
+       struct weston_config *config = ivi->config;
+
+       while (weston_config_next_section(config, &transmitter_section, &sect_name)) {
+               if (strcmp(sect_name, "transmitter-output"))
+                       continue;
+
+               struct ivi_output *ivi_output = NULL;
+               bool output_found = false;
+               char *_name = NULL;
+
+               weston_config_section_get_string(transmitter_section,
+                               "name", &_name, NULL);
+               wl_list_for_each(ivi_output, &ivi->outputs, link) {
+                       if (!strcmp(ivi_output->name, _name)) {
+                               output_found = true;
+                               break;
+                       }
+               }
+
+               if (output_found) {
+                       free(_name);
+                       continue;
+               }
+
+               ivi_output = zalloc(sizeof(*ivi_output));
+
+               ivi_output->ivi = ivi;
+               ivi_output->name = _name;
+               ivi_output->config = transmitter_section;
+
+               if (remote_output_init(ivi_output, ivi->compositor,
+                                       transmitter_section, ivi->remoting_api)) {
+                       free(ivi_output->name);
+                       free(ivi_output);
+                       continue;
+               }
+
+               ivi_output->type = OUTPUT_WALTHAM;
+               ivi_output->output_destroy.notify = handle_output_destroy;
+               weston_output_add_destroy_listener(ivi_output->output,
+                               &ivi_output->output_destroy);
+
+               wl_list_insert(&ivi->outputs, &ivi_output->link);
+               ivi_output_configure_app_id(ivi_output);
+       }
+}
+
+static int
+load_remoting_plugin(struct ivi_compositor *ivi, struct weston_config *config)
+{
+       struct weston_compositor *compositor = ivi->compositor;
+       int (*module_init)(struct weston_compositor *wc);
+
+       module_init = weston_load_module("remoting-plugin.so",
+                                        "weston_module_init");
+       if (!module_init)
+               return -1;
+
+       if (module_init(compositor) < 0)
+               return -1;
+
+       ivi->remoting_api = weston_remoting_get_api(compositor);
+       if (!ivi->remoting_api)
+               return -1;
+       return 0;
+}
+#else
+static int
+load_remoting_plugin(struct weston_compositor *compositor, struct weston_config *config)
+{
+       return -1;
+}
+#endif
+
+static int
+load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
+{
+       struct weston_drm_backend_config config = {
+               .base = {
+                       .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
+                       .struct_size = sizeof config,
+               },
+       };
+       struct weston_config_section *section;
+       int use_current_mode = 0;
+       int use_pixman = 0;
+       bool use_shadow;
+       int ret;
+
+       const struct weston_option options[] = {
+               { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
+               { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
+               { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
+               { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
+               { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
+       };
+
+       parse_options(options, ARRAY_LENGTH(options), argc, argv);
+       config.use_pixman = use_pixman;
+       ivi->cmdline.use_current_mode = use_current_mode;
+
+       section = weston_config_get_section(ivi->config, "core", NULL, NULL);
+       weston_config_section_get_string(section, "gbm-format",
+                                        &config.gbm_format, NULL);
+       weston_config_section_get_uint(section, "pageflip-timeout",
+                                      &config.pageflip_timeout, 0);
+       weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
+       config.use_pixman_shadow = use_shadow;
+
+       ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
+                                            &config.base);
+       if (ret < 0)
+               return ret;
+
+       ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
+       if (!ivi->drm_api) {
+               weston_log("Cannot use drm output api.\n");
+               ret = -1;
+               goto error;
+       }
+
+       load_remoting_plugin(ivi, ivi->config);
+       load_waltham_plugin(ivi, ivi->config);
+
+error:
+       free(config.gbm_format);
+       free(config.seat_id);
+       return ret;
+}
+
+static void
+windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
+                             bool *use_pixman, bool *fullscreen, int *output_count)
+{
+       struct weston_config_section *section;
+       bool pixman;
+       int fs = 0;
+
+       const struct weston_option options[] = {
+               { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
+               { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
+               { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
+               { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
+               { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
+               { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
+       };
+
+       section = weston_config_get_section(ivi->config, "core", NULL, NULL);
+       weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
+
+       *output_count = 1;
+       parse_options(options, ARRAY_LENGTH(options), argc, argv);
+       *use_pixman = pixman;
+       *fullscreen = fs;
+}
+
+static int
+windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
+                       const char *match_prefix, const char *name_prefix)
+{
+       struct weston_config_section *section = NULL;
+       const char *section_name;
+       char *default_output = NULL;
+       int i = 0;
+       size_t match_len = strlen(match_prefix);
+
+       while (weston_config_next_section(ivi->config, &section, &section_name)) {
+               char *output_name;
+
+               if (i >= output_count)
+                       break;
+
+               if (strcmp(section_name, "output") != 0)
+                       continue;
+
+               weston_config_section_get_string(section, "name", &output_name, NULL);
+               if (output_name == NULL)
+                       continue;
+               if (strncmp(output_name, match_prefix, match_len) != 0) {
+                       free(output_name);
+                       continue;
+               }
+
+               if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
+                       free(output_name);
+                       return -1;
+               }
+
+               free(output_name);
+               ++i;
+       }
+
+       for (; i < output_count; ++i) {
+               if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
+                       return -1;
+
+               if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
+                       free(default_output);
+                       return -1;
+               }
+
+               free(default_output);
+       }
+
+       return 0;
+}
+
+static int
+load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
+{
+       struct weston_wayland_backend_config config = {
+               .base = {
+                       .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
+                       .struct_size = sizeof config,
+               },
+       };
+       struct weston_config_section *section;
+       int sprawl = 0;
+       int output_count;
+       int ret;
+
+       const struct weston_option options[] = {
+               { WESTON_OPTION_STRING, "display", 0, &config.display_name },
+               { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
+       };
+
+       windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
+                                     &config.fullscreen, &output_count);
+
+       parse_options(options, ARRAY_LENGTH(options), argc, argv);
+       config.sprawl = sprawl;
+
+       section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
+       weston_config_section_get_string(section, "cursor-theme",
+                                        &config.cursor_theme, NULL);
+       weston_config_section_get_int(section, "cursor-size",
+                                     &config.cursor_size, 32);
+
+       ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
+                                            &config.base);
+
+       free(config.cursor_theme);
+       free(config.display_name);
+
+       if (ret < 0)
+               return ret;
+
+       ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
+
+       /*
+        * We will just assume if load_backend() finished cleanly and
+        * windowed_output_api is not present that wayland backend is started
+        * with --sprawl or runs on fullscreen-shell. In this case, all values
+        * are hardcoded, so nothing can be configured; simply create and
+        * enable an output.
+        */
+       if (ivi->window_api == NULL)
+               return 0;
+
+       return windowed_create_outputs(ivi, output_count, "WL", "wayland");
+}
+
+#ifdef HAVE_BACKEND_X11
+static int
+load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
+{
+       struct weston_x11_backend_config config = {
+               .base = {
+                       .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
+                       .struct_size = sizeof config,
+               },
+       };
+       int no_input = 0;
+       int output_count;
+       int ret;
+
+       const struct weston_option options[] = {
+              { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
+       };
+
+       windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
+                                     &config.fullscreen, &output_count);
+
+       parse_options(options, ARRAY_LENGTH(options), argc, argv);
+       config.no_input = no_input;
+
+       ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
+                                            &config.base);
+
+       if (ret < 0)
+               return ret;
+
+       ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
+       if (!ivi->window_api) {
+               weston_log("Cannot use weston_windowed_output_api.\n");
+               return -1;
+       }
+
+       return windowed_create_outputs(ivi, output_count, "X", "screen");
+}
+#else
+static int
+load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
+{
+       return -1;
+}
+#endif
+
+static int
+load_backend(struct ivi_compositor *ivi, const char *backend,
+            int *argc, char *argv[])
+{
+       if (strcmp(backend, "drm-backend.so") == 0) {
+               return load_drm_backend(ivi, argc, argv);
+       } else if (strcmp(backend, "wayland-backend.so") == 0) {
+               return load_wayland_backend(ivi, argc, argv);
+       } else if (strcmp(backend, "x11-backend.so") == 0) {
+               return load_x11_backend(ivi, argc, argv);
+       }
+
+       weston_log("fatal: unknown backend '%s'.\n", backend);
+       return -1;
+}
+
+static char *
+choose_default_backend(void)
+{
+       char *backend = NULL;
+
+       if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
+               backend = strdup("wayland-backend.so");
+       else if (getenv("DISPLAY"))
+               backend = strdup("x11-backend.so");
+       else
+               backend = strdup("drm-backend.so");
+
+       return backend;
+}
+
+static int
+compositor_init_config(struct weston_compositor *compositor,
+                      struct weston_config *config)
+{
+       struct xkb_rule_names xkb_names;
+       struct weston_config_section *section;
+       int repaint_msec;
+       bool vt_switching;
+       bool require_input;
+
+       /* agl-compositor.ini [keyboard] */
+       section = weston_config_get_section(config, "keyboard", NULL, NULL);
+       weston_config_section_get_string(section, "keymap_rules",
+                                        (char **) &xkb_names.rules, NULL);
+       weston_config_section_get_string(section, "keymap_model",
+                                        (char **) &xkb_names.model, NULL);
+       weston_config_section_get_string(section, "keymap_layout",
+                                        (char **) &xkb_names.layout, NULL);
+       weston_config_section_get_string(section, "keymap_variant",
+                                        (char **) &xkb_names.variant, NULL);
+       weston_config_section_get_string(section, "keymap_options",
+                                        (char **) &xkb_names.options, NULL);
+
+       if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
+               return -1;
+
+       weston_config_section_get_int(section, "repeat-rate",
+                                     &compositor->kb_repeat_rate, 40);
+       weston_config_section_get_int(section, "repeat-delay",
+                                     &compositor->kb_repeat_delay, 400);
+
+       weston_config_section_get_bool(section, "vt-switching",
+                                      &vt_switching, true);
+       compositor->vt_switching = vt_switching;
+
+       /* agl-compositor.ini [core] */
+       section = weston_config_get_section(config, "core", NULL, NULL);
+
+       weston_config_section_get_bool(section, "require-input", &require_input, true);
+       compositor->require_input = require_input;
+
+       weston_config_section_get_int(section, "repaint-window", &repaint_msec,
+                                     compositor->repaint_msec);
+       if (repaint_msec < -10 || repaint_msec > 1000) {
+               weston_log("Invalid repaint_window value in config: %d\n",
+                          repaint_msec);
+       } else {
+               compositor->repaint_msec = repaint_msec;
+       }
+       weston_log("Output repaint window is %d ms maximum.\n",
+                  compositor->repaint_msec);
+
+       return 0;
+}
+
+struct ivi_surface *
+to_ivi_surface(struct weston_surface *surface)
+{
+       struct weston_desktop_surface *dsurface;
+
+       dsurface = weston_surface_get_desktop_surface(surface);
+       if (!dsurface)
+               return NULL;
+
+       return weston_desktop_surface_get_user_data(dsurface);
+}
+
+static void
+activate_binding(struct weston_seat *seat,
+                struct weston_view *focus_view)
+{
+       struct weston_surface *focus = focus_view->surface;
+       struct weston_surface *main_surface =
+               weston_surface_get_main_surface(focus);
+       struct ivi_surface *surface;
+
+       surface = to_ivi_surface(main_surface);
+       if (!surface)
+               return;
+
+       weston_seat_set_keyboard_focus(seat, focus);
+}
+
+static void
+click_to_activate_binding(struct weston_pointer *pointer,
+                         const struct timespec *time,
+                         uint32_t button, void *data)
+{
+       if (pointer->grab != &pointer->default_grab)
+               return;
+       if (pointer->focus == NULL)
+               return;
+
+       activate_binding(pointer->seat, pointer->focus);
+}
+
+static void
+touch_to_activate_binding(struct weston_touch *touch,
+                         const struct timespec *time,
+                         void *data)
+{
+       if (touch->grab != &touch->default_grab)
+               return;
+       if (touch->focus == NULL)
+               return;
+
+       activate_binding(touch->seat, touch->focus);
+}
+
+static void
+add_bindings(struct weston_compositor *compositor)
+{
+       weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
+                                            click_to_activate_binding,
+                                            NULL);
+       weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
+                                            click_to_activate_binding,
+                                            NULL);
+       weston_compositor_add_touch_binding(compositor, 0,
+                                           touch_to_activate_binding,
+                                           NULL);
+}
+
+static int
+create_listening_socket(struct wl_display *display, const char *socket_name)
+{
+       if (socket_name) {
+               if (wl_display_add_socket(display, socket_name)) {
+                       weston_log("fatal: failed to add socket: %s\n",
+                                  strerror(errno));
+                       return -1;
+               }
+       } else {
+               socket_name = wl_display_add_socket_auto(display);
+               if (!socket_name) {
+                       weston_log("fatal: failed to add socket: %s\n",
+                                  strerror(errno));
+                       return -1;
+               }
+       }
+
+       setenv("WAYLAND_DISPLAY", socket_name, 1);
+
+       return 0;
+}
+
+static bool
+global_filter(const struct wl_client *client, const struct wl_global *global,
+             void *data)
+{
+       return true;
+}
+
+static int
+load_config(struct weston_config **config, bool no_config,
+           const char *config_file)
+{
+       const char *file = "agl-compositor.ini";
+       const char *full_path;
+
+       if (config_file)
+               file = config_file;
+
+       if (!no_config)
+               *config = weston_config_parse(file);
+
+       if (*config) {
+               full_path = weston_config_get_full_path(*config);
+
+               weston_log("Using config file '%s'.\n", full_path);
+               setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
+
+               return 0;
+       }
+
+       if (config_file && !no_config) {
+               weston_log("fatal: error opening or reading config file '%s'.\n",
+                       config_file);
+
+               return -1;
+       }
+
+       weston_log("Starting with no config file.\n");
+       setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
+
+       return 0;
+}
+
+static FILE *logfile;
+
+static char *
+log_timestamp(char *buf, size_t len)
+{
+       struct timeval tv;
+       struct tm *brokendown_time;
+       char datestr[128];
+       char timestr[128];
+
+       gettimeofday(&tv, NULL);
+
+       brokendown_time = localtime(&tv.tv_sec);
+       if (brokendown_time == NULL) {
+               snprintf(buf, len, "%s", "[(NULL)localtime] ");
+               return buf;
+       }
+
+       memset(datestr, 0, sizeof(datestr));
+       if (brokendown_time->tm_mday != cached_tm_mday) {
+               strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n",
+                               brokendown_time);
+               cached_tm_mday = brokendown_time->tm_mday;
+       }
+
+       strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time);
+       /* if datestr is empty it prints only timestr*/
+       snprintf(buf, len, "%s[%s.%03li]", datestr,
+                       timestr, (tv.tv_usec / 1000));
+
+       return buf;
+}
+
+static void
+custom_handler(const char *fmt, va_list arg)
+{
+       char timestr[512];
+
+       weston_log_scope_printf(log_scope, "%s libwayland: ",
+                       log_timestamp(timestr, sizeof(timestr)));
+       weston_log_scope_vprintf(log_scope, fmt, arg);
+}
+
+static void
+log_file_open(const char *filename)
+{
+       wl_log_set_handler_server(custom_handler);
+
+       if (filename)
+               logfile = fopen(filename, "a");
+
+       if (!logfile) {
+               logfile = stderr;
+       } else {
+               os_fd_set_cloexec(fileno(logfile));
+               setvbuf(logfile, NULL, _IOLBF, 256);
+       }
+}
+
+static void
+log_file_close(void)
+{
+       if (logfile && logfile != stderr)
+               fclose(logfile);
+       logfile = stderr;
+}
+
+static int
+vlog(const char *fmt, va_list ap)
+{
+       const char *oom = "Out of memory";
+       char timestr[128];
+       int len = 0;
+       char *str;
+
+       if (weston_log_scope_is_enabled(log_scope)) {
+               int len_va;
+               char *xlog_timestamp = log_timestamp(timestr, sizeof(timestr));
+               len_va = vasprintf(&str, fmt, ap);
+               if (len_va >= 0) {
+                       len = weston_log_scope_printf(log_scope, "%s %s",
+                                       xlog_timestamp, str);
+                       free(str);
+               } else {
+                       len = weston_log_scope_printf(log_scope, "%s %s",
+                                       xlog_timestamp, oom);
+               }
+       }
+
+       return len;
+}
+
+
+static int
+vlog_continue(const char *fmt, va_list ap)
+{
+       return weston_log_scope_vprintf(log_scope, fmt, ap);
+}
+
+static int
+on_term_signal(int signo, void *data)
+{
+       struct wl_display *display = data;
+
+       weston_log("caught signal %d\n", signo);
+       wl_display_terminate(display);
+
+       return 1;
+}
+
+static void
+handle_exit(struct weston_compositor *compositor)
+{
+       wl_display_terminate(compositor->wl_display);
+}
+
+static void
+usage(int error_code)
+{
+       FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
+       fprintf(out,
+               "Usage: agl-compositor [OPTIONS]\n"
+               "\n"
+               "This is " PACKAGE_STRING ", the reference compositor for\n"
+               "Automotive Grade Linux. Weston-ivi supports multiple backends, and depending\n"
+               "on which backend is in use different options will be accepted.\n"
+               "\n"
+               "Core options:\n"
+               "\n"
+               "  --version\t\tPrint agl-compositor version\n"
+               "  -B, --backend=MODULE\tBackend module, one of\n"
+                       "\t\t\t\tdrm-backend.so\n"
+                       "\t\t\t\twayland-backend.so\n"
+                       "\t\t\t\tx11-backend.so\n"
+               "  -S, --socket=NAME\tName of socket to listen on\n"
+               "  --log=FILE\t\tLog to the given file\n"
+               "  -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
+               "  --no-config\t\tDo not read agl-compositor.ini\n"
+               "  --debug\t\tEnable debug extension(s)\n"
+               "  -h, --help\t\tThis help message\n"
+               "\n");
+       exit(error_code);
+}
+
+WL_EXPORT
+int wet_main(int argc, char *argv[])
+{
+       struct ivi_compositor ivi = { 0 };
+       struct wl_display *display = NULL;
+       struct wl_event_loop *loop;
+       struct wl_event_source *signals[3] = { 0 };
+       struct weston_config_section *section;
+       /* Command line options */
+       char *backend = NULL;
+       char *socket_name = NULL;
+       char *log = NULL;
+       int help = 0;
+       int version = 0;
+       int no_config = 0;
+       int debug = 0;
+       char *config_file = NULL;
+       struct weston_log_context *log_ctx = NULL;
+       struct weston_log_subscriber *logger;
+       int ret = EXIT_FAILURE;
+
+       const struct weston_option core_options[] = {
+               { WESTON_OPTION_STRING, "backend", 'B', &backend },
+               { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
+               { WESTON_OPTION_STRING, "log", 0, &log },
+               { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
+               { WESTON_OPTION_BOOLEAN, "version", 0, &version },
+               { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
+               { WESTON_OPTION_BOOLEAN, "debug", 0, &debug },
+               { WESTON_OPTION_STRING, "config", 'c', &config_file },
+       };
+
+       wl_list_init(&ivi.outputs);
+       wl_list_init(&ivi.surfaces);
+       wl_list_init(&ivi.pending_surfaces);
+       wl_list_init(&ivi.popup_pending_apps);
+       wl_list_init(&ivi.fullscreen_pending_apps);
+       wl_list_init(&ivi.split_pending_apps);
+       wl_list_init(&ivi.remote_pending_apps);
+       wl_list_init(&ivi.desktop_clients);
+
+       /* Prevent any clients we spawn getting our stdin */
+       os_fd_set_cloexec(STDIN_FILENO);
+
+       parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
+
+       if (help)
+               usage(EXIT_SUCCESS);
+
+       if (version) {
+               printf(PACKAGE_STRING "\n");
+               return EXIT_SUCCESS;
+       }
+
+       log_ctx = weston_log_ctx_create();
+       if (!log_ctx) {
+               fprintf(stderr, "Failed to initialize weston debug framework.\n");
+               return ret;
+       }
+
+        log_scope = weston_log_ctx_add_log_scope(log_ctx, "log",
+                                                "agl-compositor log\n",
+                                                 NULL, NULL, NULL);
+
+       log_file_open(log);
+       weston_log_set_handler(vlog, vlog_continue);
+
+       logger = weston_log_subscriber_create_log(logfile);
+       weston_log_subscribe(log_ctx, logger, "log");
+
+       if (load_config(&ivi.config, no_config, config_file) < 0)
+               goto error_signals;
+       section = weston_config_get_section(ivi.config, "core", NULL, NULL);
+       if (!backend) {
+               weston_config_section_get_string(section, "backend", &backend,
+                                                NULL);
+               if (!backend)
+                       backend = choose_default_backend();
+       }
+       /* from [core] */
+       weston_config_section_get_bool(section, "hide-cursor", &ivi.hide_cursor, false);
+       weston_config_section_get_bool(section, "activate-by-default", &ivi.activate_by_default, true);
+
+       display = wl_display_create();
+       loop = wl_display_get_event_loop(display);
+
+       wl_display_set_global_filter(display,
+                                    global_filter, &ivi);
+
+       /* Register signal handlers so we shut down cleanly */
+
+       signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
+                                             display);
+       signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
+                                             display);
+       signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
+                                             display);
+
+       for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
+               if (!signals[i])
+                       goto error_signals;
+
+       ivi.compositor = weston_compositor_create(display, log_ctx, &ivi);
+       if (!ivi.compositor) {
+               weston_log("fatal: failed to create compositor.\n");
+               goto error_signals;
+       }
+
+       if (compositor_init_config(ivi.compositor, ivi.config) < 0)
+               goto error_compositor;
+
+       if (load_backend(&ivi, backend, &argc, argv) < 0) {
+               weston_log("fatal: failed to create compositor backend.\n");
+               goto error_compositor;
+       }
+
+       ivi.heads_changed.notify = heads_changed;
+       weston_compositor_add_heads_changed_listener(ivi.compositor,
+                                                    &ivi.heads_changed);
+
+       if (ivi_desktop_init(&ivi) < 0)
+               goto error_compositor;
+
+       ivi_seat_init(&ivi);
+
+       if (ivi_policy_init(&ivi) < 0)
+               goto error_compositor;
+
+       if (ivi_shell_init(&ivi) < 0)
+               goto error_compositor;
+
+       add_bindings(ivi.compositor);
+
+       weston_compositor_flush_heads_changed(ivi.compositor);
+
+       if (ivi.remoting_api)
+               ivi_enable_remote_outputs(&ivi);
+
+       if (ivi.waltham_transmitter_api)
+               ivi_enable_waltham_outputs(&ivi);
+
+       if (create_listening_socket(display, socket_name) < 0)
+               goto error_compositor;
+
+       ivi_shell_init_black_fs(&ivi);
+
+       ivi.compositor->exit = handle_exit;
+
+       weston_compositor_wake(ivi.compositor);
+
+       ivi_shell_create_global(&ivi);
+       ivi_launch_shell_client(&ivi);
+       if (debug)
+               ivi_screenshooter_create(&ivi);
+       ivi_agl_systemd_notify(&ivi);
+
+       wl_display_run(display);
+
+       ret = ivi.compositor->exit_code;
+
+       wl_display_destroy_clients(display);
+
+error_compositor:
+       weston_compositor_destroy(ivi.compositor);
+
+       weston_log_scope_destroy(log_scope);
+       log_scope = NULL;
+
+       weston_log_subscriber_destroy(logger);
+       weston_log_ctx_destroy(log_ctx);
+
+       ivi_policy_destroy(ivi.policy);
+
+error_signals:
+       for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
+               if (signals[i])
+                       wl_event_source_remove(signals[i]);
+
+       wl_display_destroy(display);
+
+       log_file_close();
+       if (ivi.config)
+               weston_config_destroy(ivi.config);
+
+       return ret;
+}
index 4111784..5d192b0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2012-2019 Collabora, Ltd.
+ * Copyright © 2012-2021 Collabora, Ltd.
  *
  * Permission is hereby granted, free of charge, to any person obtaining
  * a copy of this software and associated documentation files (the
  * SOFTWARE.
  */
 
-#include "ivi-compositor.h"
-#include "policy.h"
-
-#include <assert.h>
-#include <errno.h>
-#include <signal.h>
-#include <stdarg.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <linux/input.h>
-
-#include <libweston/backend-drm.h>
-#include <libweston/backend-wayland.h>
-#ifdef HAVE_BACKEND_X11
-#include <libweston/backend-x11.h>
-#endif
-#include <libweston/libweston.h>
-#include <libweston/windowed-output-api.h>
-#include <libweston/config-parser.h>
-#include <libweston/weston-log.h>
+#include "config.h"
 #include <weston/weston.h>
 
-#include "shared/os-compatibility.h"
-#include "shared/helpers.h"
-
-#include "agl-shell-server-protocol.h"
-
-#ifdef HAVE_REMOTING
-#include "remote.h"
-#endif
-
-#ifdef HAVE_WALTHAM
-#include <waltham-transmitter/transmitter_api.h>
-#endif
-
-static int cached_tm_mday = -1;
-static struct weston_log_scope *log_scope;
-
-struct ivi_compositor *
-to_ivi_compositor(struct weston_compositor *ec)
-{
-       return weston_compositor_get_user_data(ec);
-}
-
-static void
-handle_output_destroy(struct wl_listener *listener, void *data)
-{
-       struct ivi_output *output;
-
-       output = wl_container_of(listener, output, output_destroy);
-       assert(output->output == data);
-
-       if (output->fullscreen_view.fs->view) {
-               weston_surface_destroy(output->fullscreen_view.fs->view->surface);
-               output->fullscreen_view.fs->view = NULL;
-       }
-
-       output->output = NULL;
-       wl_list_remove(&output->output_destroy.link);
-}
-
-struct ivi_output *
-to_ivi_output(struct weston_output *o)
-{
-       struct wl_listener *listener;
-       struct ivi_output *output;
-
-       listener = weston_output_get_destroy_listener(o, handle_output_destroy);
-       output = wl_container_of(listener, output, output_destroy);
-
-       return output;
-}
-
-static void
-ivi_output_configure_app_id(struct ivi_output *ivi_output)
-{
-       if (ivi_output->config) {
-               if (ivi_output->app_id != NULL)
-                       return;
-
-               weston_config_section_get_string(ivi_output->config,
-                                                "agl-shell-app-id",
-                                                &ivi_output->app_id,
-                                                NULL);
-
-               if (ivi_output->app_id == NULL)
-                       return;
-
-               weston_log("Will place app_id %s on output %s\n",
-                               ivi_output->app_id, ivi_output->name);
-       }
-}
-
-static struct ivi_output *
-ivi_ensure_output(struct ivi_compositor *ivi, char *name,
-                 struct weston_config_section *config)
-{
-       struct ivi_output *output = NULL;
-       wl_list_for_each(output, &ivi->outputs, link) {
-               if (strcmp(output->name, name) == 0) {
-                       free(name);
-                       return output;
-               }
-       }
-
-       output = zalloc(sizeof *output);
-       if (!output) {
-               free(name);
-               return NULL;
-       }
-
-       output->ivi = ivi;
-       output->name = name;
-       output->config = config;
-
-       output->output = weston_compositor_create_output(ivi->compositor, name);
-       if (!output->output) {
-               free(output->name);
-               free(output);
-               return NULL;
-       }
-
-       output->output_destroy.notify = handle_output_destroy;
-       weston_output_add_destroy_listener(output->output,
-                                          &output->output_destroy);
-
-       wl_list_insert(&ivi->outputs, &output->link);
-       ivi_output_configure_app_id(output);
-       return output;
-}
-
-static int
-count_heads(struct weston_output *output)
-{
-       struct weston_head *iter = NULL;
-       int n = 0;
-
-       while ((iter = weston_output_iterate_heads(output, iter)))
-               ++n;
-
-       return n;
-}
-
-static void
-handle_head_destroy(struct wl_listener *listener, void *data)
-{
-       struct weston_head *head = data;
-       struct weston_output *output;
-
-       wl_list_remove(&listener->link);
-       free(listener);
-
-       output = weston_head_get_output(head);
-
-       /* On shutdown path, the output might be already gone. */
-       if (!output)
-               return;
-
-       /* We're the last head */
-       if (count_heads(output) <= 1)
-               weston_output_destroy(output);
-}
-
-static void
-add_head_destroyed_listener(struct weston_head *head)
-{
-       /* We already have a destroy listener */
-       if (weston_head_get_destroy_listener(head, handle_head_destroy))
-               return;
-
-       struct wl_listener *listener = zalloc(sizeof *listener);
-       if (!listener)
-               return;
-
-       listener->notify = handle_head_destroy;
-       weston_head_add_destroy_listener(head, listener);
-}
-
-static int
-drm_configure_output(struct ivi_output *output)
-{
-       struct ivi_compositor *ivi = output->ivi;
-       struct weston_config_section *section = output->config;
-       enum weston_drm_backend_output_mode mode =
-               WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
-       char *modeline = NULL;
-       char *gbm_format = NULL;
-       char *seat = NULL;
-
-       if (section) {
-               char *m;
-               weston_config_section_get_string(section, "mode", &m, "preferred");
-
-               /* This should have been handled earlier */
-               assert(strcmp(m, "off") != 0);
-
-               if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
-                       mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
-               } else if (strcmp(m, "preferred") != 0) {
-                       modeline = m;
-                       m = NULL;
-               }
-               free(m);
-
-               weston_config_section_get_string(section, "gbm-format",
-                                                &gbm_format, NULL);
-
-               weston_config_section_get_string(section, "seat", &seat, "");
-       }
-
-       if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
-               weston_log("Cannot configure output using weston_drm_output_api.\n");
-               free(modeline);
-               return -1;
-       }
-       free(modeline);
-
-       ivi->drm_api->set_gbm_format(output->output, gbm_format);
-       free(gbm_format);
-
-       ivi->drm_api->set_seat(output->output, seat);
-       free(seat);
-
-       return 0;
-}
-
-#define WINDOWED_DEFAULT_WIDTH 1024
-#define WINDOWED_DEFAULT_HEIGHT 768
-
-static int
-windowed_configure_output(struct ivi_output *output)
-{
-       struct ivi_compositor *ivi = output->ivi;
-       struct weston_config_section *section = output->config;
-       int width = WINDOWED_DEFAULT_WIDTH;
-       int height = WINDOWED_DEFAULT_HEIGHT;
-
-       if (section) {
-               char *mode;
-
-               weston_config_section_get_string(section, "mode", &mode, NULL);
-               if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
-                       weston_log("Invalid mode for output %s. Using defaults.\n",
-                                  output->name);
-                       width = WINDOWED_DEFAULT_WIDTH;
-                       height = WINDOWED_DEFAULT_HEIGHT;
-               }
-               free(mode);
-       }
-
-       if (ivi->cmdline.width)
-               width = ivi->cmdline.width;
-       if (ivi->cmdline.height)
-               height = ivi->cmdline.height;
-       if (ivi->cmdline.scale)
-               weston_output_set_scale(output->output, ivi->cmdline.scale);
-
-       if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
-               weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
-                          output->name);
-               return -1;
-       }
-
-       return 0;
-}
-
-static int
-parse_transform(const char *transform, uint32_t *out)
-{
-       static const struct { const char *name; uint32_t token; } transforms[] = {
-               { "normal",     WL_OUTPUT_TRANSFORM_NORMAL },
-               { "90",         WL_OUTPUT_TRANSFORM_90 },
-               { "180",        WL_OUTPUT_TRANSFORM_180 },
-               { "270",        WL_OUTPUT_TRANSFORM_270 },
-               { "flipped",    WL_OUTPUT_TRANSFORM_FLIPPED },
-               { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
-               { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
-               { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
-       };
-
-       for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
-               if (strcmp(transforms[i].name, transform) == 0) {
-                       *out = transforms[i].token;
-                       return 0;
-               }
-
-       *out = WL_OUTPUT_TRANSFORM_NORMAL;
-       return -1;
-}
-
-static int
-configure_output(struct ivi_output *output)
-{
-       struct ivi_compositor *ivi = output->ivi;
-       struct weston_config_section *section = output->config;
-       int32_t scale = 1;
-       uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
-
-       /*
-        * This can happen with the wayland backend with 'sprawl'. The config
-        * is hard-coded, so we don't need to do anything.
-        */
-       if (!ivi->drm_api && !ivi->window_api)
-               return 0;
-
-       if (section) {
-               char *t;
-
-               weston_config_section_get_int(section, "scale", &scale, 1);
-               weston_config_section_get_string(section, "transform", &t, "normal");
-               if (parse_transform(t, &transform) < 0)
-                       weston_log("Invalid transform \"%s\" for output %s\n",
-                                  t, output->name);
-               free(t);
-       }
-
-       weston_output_set_scale(output->output, scale);
-       weston_output_set_transform(output->output, transform);
-
-       if (ivi->drm_api)
-               return drm_configure_output(output);
-       else
-               return windowed_configure_output(output);
-}
-
-/*
- * Reorgainizes the output's add array into two sections.
- * add[0..ret-1] are the heads that failed to get attached.
- * add[ret..add_len] are the heads that were successfully attached.
- *
- * The order between elements in each section is stable.
- */
-static size_t
-try_attach_heads(struct ivi_output *output)
-{
-       size_t fail_len = 0;
-
-       for (size_t i = 0; i < output->add_len; ++i) {
-               if (weston_output_attach_head(output->output, output->add[i]) < 0) {
-                       struct weston_head *tmp = output->add[i];
-                       memmove(&output->add[fail_len + 1], output->add[fail_len],
-                               sizeof output->add[0] * (i - fail_len));
-                       output->add[fail_len++] = tmp;
-               }
-       }
-
-       return fail_len;
-}
-
-/*
- * Like try_attach_heads, this reorganizes the output's add array into a failed
- * and successful section.
- * i is the number of heads that already failed the previous step.
- */
-static size_t
-try_enable_output(struct ivi_output *output, size_t i)
-{
-       for (; i < output->add_len; ++i) {
-               struct weston_head *head;
-
-               if (weston_output_enable(output->output) == 0)
-                       break;
-
-               head = output->add[output->add_len - 1];
-               memmove(&output->add[i + 1], &output->add[i],
-                       sizeof output->add[0] * (output->add_len - i));
-               output->add[i] = head;
-
-               weston_head_detach(head);
-       }
-
-       return i;
-}
-
-static int
-try_attach_enable_heads(struct ivi_output *output)
-{
-       size_t fail_len;
-       assert(!output->output->enabled);
-
-       fail_len = try_attach_heads(output);
-
-       if (configure_output(output) < 0)
-               return -1;
-
-       fail_len = try_enable_output(output, fail_len);
-
-       /* All heads failed to be attached */
-       if (fail_len == output->add_len)
-               return -1;
-
-       /* For each successful head attached */
-       for (size_t i = fail_len; i < output->add_len; ++i)
-               add_head_destroyed_listener(output->add[i]);
-
-       output->add_len = fail_len;
-       return 0;
-}
-
-static int
-process_output(struct ivi_output *output)
-{
-       if (output->output->enabled) {
-               output->add_len = try_attach_heads(output);
-               return output->add_len == 0 ? 0 : -1;
-       }
-
-       return try_attach_enable_heads(output);
-}
-
-static void
-head_disable(struct ivi_compositor *ivi, struct weston_head *head)
-{
-       struct weston_output *output;
-       struct ivi_output *ivi_output;
-       struct wl_listener *listener;
-
-       output = weston_head_get_output(head);
-       assert(output);
-
-       listener = weston_output_get_destroy_listener(output,
-                                                     handle_output_destroy);
-       assert(listener);
-
-       ivi_output = wl_container_of(listener, ivi_output, output_destroy);
-       assert(ivi_output->output == output);
-
-       weston_head_detach(head);
-       if (count_heads(ivi_output->output) == 0) {
-               weston_output_disable(ivi_output->output);
-       }
-}
-
-static struct weston_config_section *
-find_controlling_output_config(struct weston_config *config,
-                              const char *name)
-{
-       struct weston_config_section *section;
-       char *same_as;
-       int depth = 0;
-
-       same_as = strdup(name);
-       do {
-               section = weston_config_get_section(config, "output",
-                                                   "name", same_as);
-               if (!section && depth > 0)
-                       weston_log("Configuration error: output section reffered"
-                                  "to by same-as=%s not found.\n", same_as);
-               free(same_as);
-
-               if (!section)
-                       return NULL;
-
-               if (depth++ > 8) {
-                       weston_log("Configuration error: same-as nested too "
-                                  "deep for output '%s'.\n", name);
-                       return NULL;
-               }
-
-               weston_config_section_get_string(section, "same-as",
-                                                &same_as, NULL);
-       } while (same_as);
-
-       return section;
-}
-
-static void
-head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
-{
-       const char *name = weston_head_get_name(head);
-       struct weston_config_section *section;
-       struct ivi_output *output;
-       char *output_name = NULL;
-
-       section = find_controlling_output_config(ivi->config, name);
-       if (section) {
-               char *mode;
-
-               weston_config_section_get_string(section, "mode", &mode, NULL);
-               if (mode && strcmp(mode, "off") == 0) {
-                       free(mode);
-                       return;
-               }
-               free(mode);
-
-               weston_config_section_get_string(section, "name",
-                                                &output_name, NULL);
-       } else {
-               output_name = strdup(name);
-       }
-
-       if (!output_name)
-               return;
-
-       output = ivi_ensure_output(ivi, output_name, section);
-       if (!output)
-               return;
-
-       if (output->add_len >= ARRAY_LENGTH(output->add))
-               return;
-
-       output->add[output->add_len++] = head;
-}
-
-static void
-heads_changed(struct wl_listener *listener, void *arg)
-{
-       struct weston_compositor *compositor = arg;
-       struct weston_head *head = NULL;
-       struct ivi_compositor *ivi = to_ivi_compositor(compositor);
-       struct ivi_output *output;
-
-       while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
-               bool connected = weston_head_is_connected(head);
-               bool enabled = weston_head_is_enabled(head);
-               bool changed = weston_head_is_device_changed(head);
-               bool non_desktop = weston_head_is_non_desktop(head);
-
-               if (connected && !enabled && !non_desktop)
-                       head_prepare_enable(ivi, head);
-               else if (!connected && enabled)
-                       head_disable(ivi, head);
-               else if (enabled && changed)
-                       weston_log("Detected a monitor change on head '%s', "
-                                  "not bothering to do anything about it.\n",
-                                  weston_head_get_name(head));
-
-               weston_head_reset_device_changed(head);
-       }
-
-       wl_list_for_each(output, &ivi->outputs, link) {
-               if (output->add_len == 0)
-                       continue;
-
-               if (process_output(output) < 0) {
-                       output->add_len = 0;
-                       ivi->init_failed = true;
-               }
-       }
-}
-
-#ifdef HAVE_WALTHAM
-static int
-load_waltham_plugin(struct ivi_compositor *ivi, struct weston_config *config)
-{
-       struct weston_compositor *compositor = ivi->compositor;
-       int (*module_init)(struct weston_compositor *wc);
-
-       module_init = weston_load_module("waltham-transmitter.so",
-                                        "wet_module_init");
-       if (!module_init)
-               return -1;
-
-       if (module_init(compositor) < 0)
-               return -1;
-
-       ivi->waltham_transmitter_api = weston_get_transmitter_api(compositor);
-       if (!ivi->waltham_transmitter_api) {
-               weston_log("Failed to load waltham-transmitter plugin.\n");
-               return -1;
-       }
-
-       weston_log("waltham-transmitter plug-in loaded\n");
-       return 0;
-}
-#else
-static int
-load_waltham_plugin(struct ivi_compositor *ivi, struct weston_config *config)
-{
-       return -1;
-}
-#endif
-
-#ifdef HAVE_REMOTING
-static int
-drm_backend_remoted_output_configure(struct weston_output *output,
-                                    struct weston_config_section *section,
-                                    char *modeline,
-                                    const struct weston_remoting_api *api)
-{
-       char *gbm_format = NULL;
-       char *seat = NULL;
-       char *host = NULL;
-       char *pipeline = NULL;
-       int port, ret;
-       int32_t scale = 1;
-       uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
-       char *trans;
-
-       ret = api->set_mode(output, modeline);
-       if (ret < 0) {
-               weston_log("Cannot configure an output \"%s\" using "
-                               "weston_remoting_api. Invalid mode\n",
-                               output->name);
-               return -1;
-       }
-
-       weston_config_section_get_int(section, "scale", &scale, 1);
-       weston_output_set_scale(output, scale);
-
-       weston_config_section_get_string(section, "transform", &trans, "normal");
-       if (parse_transform(trans, &transform) < 0) {
-               weston_log("Invalid transform \"%s\" for output %s\n",
-                          trans, output->name);
-       }
-       weston_output_set_transform(output, transform);
-
-       weston_config_section_get_string(section, "gbm-format",
-                                        &gbm_format, NULL);
-       api->set_gbm_format(output, gbm_format);
-       free(gbm_format);
-
-       weston_config_section_get_string(section, "seat", &seat, "");
-
-       api->set_seat(output, seat);
-       free(seat);
-
-       weston_config_section_get_string(section, "gst-pipeline", &pipeline,
-                       NULL);
-       if (pipeline) {
-               api->set_gst_pipeline(output, pipeline);
-               free(pipeline);
-               return 0;
-       }
-
-       weston_config_section_get_string(section, "host", &host, NULL);
-       weston_config_section_get_int(section, "port", &port, 0);
-       if (!host || port <= 0 || 65533 < port) {
-               weston_log("Cannot configure an output \"%s\". "
-                               "Need to specify gst-pipeline or "
-                               "host and port (1-65533).\n", output->name);
-       }
-       api->set_host(output, host);
-       free(host);
-       api->set_port(output, port);
-
-       return 0;
-}
-
-
-static int
-remote_output_init(struct ivi_output *ivi_output,
-                  struct weston_compositor *compositor,
-                  struct weston_config_section *section,
-                  const struct weston_remoting_api *api)
-{
-       char *output_name, *modeline = NULL;
-       int ret = -1;
-
-       weston_config_section_get_string(section, "name", &output_name, NULL);
-       if (!output_name)
-               return ret;
-
-       weston_config_section_get_string(section, "mode", &modeline, "off");
-       if (strcmp(modeline, "off") == 0)
-               goto err;
-
-       ivi_output->output = api->create_output(compositor, output_name);
-       if (!ivi_output->output) {
-               weston_log("Cannot create remoted output \"%s\".\n",
-                               output_name);
-               goto err;
-       }
-
-       ret = drm_backend_remoted_output_configure(ivi_output->output, section,
-                                                  modeline, api);
-       if (ret < 0) {
-               weston_log("Cannot configure remoted output \"%s\".\n",
-                               output_name);
-               goto err;
-       }
-
-       if (weston_output_enable(ivi_output->output) < 0) {
-               weston_log("Enabling remoted output \"%s\" failed.\n",
-                               output_name);
-               goto err;
-       }
-
-       free(modeline);
-       free(output_name);
-       weston_log("remoted output '%s' enabled\n", ivi_output->output->name);
-
-       return 0;
-
-err:
-       free(modeline);
-       free(output_name);
-       if (ivi_output->output)
-               weston_output_destroy(ivi_output->output);
-
-       return ret;
-}
-
-static void
-ivi_enable_remote_outputs(struct ivi_compositor *ivi)
-{
-       struct weston_config_section *remote_section = NULL;
-       const char *section_name;
-       struct weston_config *config = ivi->config;
-
-       while (weston_config_next_section(config, &remote_section, &section_name)) {
-               if (strcmp(section_name, "remote-output"))
-                       continue;
-
-               struct ivi_output *ivi_output = NULL;
-               bool output_found = false;
-               char *_name = NULL;
-
-               weston_config_section_get_string(remote_section,
-                                                "name", &_name, NULL);
-               wl_list_for_each(ivi_output, &ivi->outputs, link) {
-                       if (!strcmp(ivi_output->name, _name)) {
-                               output_found = true;
-                               break;
-                       }
-               }
-
-               if (output_found) {
-                       free(_name);
-                       continue;
-               }
-
-               ivi_output = zalloc(sizeof(*ivi_output));
-
-               ivi_output->ivi = ivi;
-               ivi_output->name = _name;
-               ivi_output->config = remote_section;
-               ivi_output->type = OUTPUT_REMOTE;
-
-               if (remote_output_init(ivi_output, ivi->compositor,
-                                      remote_section, ivi->remoting_api)) {
-                       free(ivi_output->name);
-                       free(ivi_output);
-                       continue;
-               }
-
-               ivi_output->output_destroy.notify = handle_output_destroy;
-               weston_output_add_destroy_listener(ivi_output->output,
-                                                  &ivi_output->output_destroy);
-
-               wl_list_insert(&ivi->outputs, &ivi_output->link);
-               ivi_output_configure_app_id(ivi_output);
-       }
-}
-
-static void
-ivi_enable_waltham_outputs(struct ivi_compositor *ivi)
-{
-       struct weston_config_section *transmitter_section = NULL;
-       const char *sect_name;
-       struct weston_config *config = ivi->config;
-
-       while (weston_config_next_section(config, &transmitter_section, &sect_name)) {
-               if (strcmp(sect_name, "transmitter-output"))
-                       continue;
-
-               struct ivi_output *ivi_output = NULL;
-               bool output_found = false;
-               char *_name = NULL;
-
-               weston_config_section_get_string(transmitter_section,
-                               "name", &_name, NULL);
-               wl_list_for_each(ivi_output, &ivi->outputs, link) {
-                       if (!strcmp(ivi_output->name, _name)) {
-                               output_found = true;
-                               break;
-                       }
-               }
-
-               if (output_found) {
-                       free(_name);
-                       continue;
-               }
-
-               ivi_output = zalloc(sizeof(*ivi_output));
-
-               ivi_output->ivi = ivi;
-               ivi_output->name = _name;
-               ivi_output->config = transmitter_section;
-
-               if (remote_output_init(ivi_output, ivi->compositor,
-                                       transmitter_section, ivi->remoting_api)) {
-                       free(ivi_output->name);
-                       free(ivi_output);
-                       continue;
-               }
-
-               ivi_output->type = OUTPUT_WALTHAM;
-               ivi_output->output_destroy.notify = handle_output_destroy;
-               weston_output_add_destroy_listener(ivi_output->output,
-                               &ivi_output->output_destroy);
-
-               wl_list_insert(&ivi->outputs, &ivi_output->link);
-               ivi_output_configure_app_id(ivi_output);
-       }
-}
-
-static int
-load_remoting_plugin(struct ivi_compositor *ivi, struct weston_config *config)
-{
-       struct weston_compositor *compositor = ivi->compositor;
-       int (*module_init)(struct weston_compositor *wc);
-
-       module_init = weston_load_module("remoting-plugin.so",
-                                        "weston_module_init");
-       if (!module_init)
-               return -1;
-
-       if (module_init(compositor) < 0)
-               return -1;
-
-       ivi->remoting_api = weston_remoting_get_api(compositor);
-       if (!ivi->remoting_api)
-               return -1;
-       return 0;
-}
-#else
-static int
-load_remoting_plugin(struct weston_compositor *compositor, struct weston_config *config)
-{
-       return -1;
-}
-#endif
-
-static int
-load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
-{
-       struct weston_drm_backend_config config = {
-               .base = {
-                       .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
-                       .struct_size = sizeof config,
-               },
-       };
-       struct weston_config_section *section;
-       int use_current_mode = 0;
-       int use_pixman = 0;
-       bool use_shadow;
-       int ret;
-
-       const struct weston_option options[] = {
-               { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
-               { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
-               { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
-               { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
-               { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
-       };
-
-       parse_options(options, ARRAY_LENGTH(options), argc, argv);
-       config.use_pixman = use_pixman;
-       ivi->cmdline.use_current_mode = use_current_mode;
-
-       section = weston_config_get_section(ivi->config, "core", NULL, NULL);
-       weston_config_section_get_string(section, "gbm-format",
-                                        &config.gbm_format, NULL);
-       weston_config_section_get_uint(section, "pageflip-timeout",
-                                      &config.pageflip_timeout, 0);
-       weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
-       config.use_pixman_shadow = use_shadow;
-
-       ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
-                                            &config.base);
-       if (ret < 0)
-               return ret;
-
-       ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
-       if (!ivi->drm_api) {
-               weston_log("Cannot use drm output api.\n");
-               ret = -1;
-               goto error;
-       }
-
-       load_remoting_plugin(ivi, ivi->config);
-       load_waltham_plugin(ivi, ivi->config);
-
-error:
-       free(config.gbm_format);
-       free(config.seat_id);
-       return ret;
-}
-
-static void
-windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
-                             bool *use_pixman, bool *fullscreen, int *output_count)
-{
-       struct weston_config_section *section;
-       bool pixman;
-       int fs = 0;
-
-       const struct weston_option options[] = {
-               { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
-               { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
-               { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
-               { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
-               { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
-               { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
-       };
-
-       section = weston_config_get_section(ivi->config, "core", NULL, NULL);
-       weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
-
-       *output_count = 1;
-       parse_options(options, ARRAY_LENGTH(options), argc, argv);
-       *use_pixman = pixman;
-       *fullscreen = fs;
-}
-
-static int
-windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
-                       const char *match_prefix, const char *name_prefix)
-{
-       struct weston_config_section *section = NULL;
-       const char *section_name;
-       char *default_output = NULL;
-       int i = 0;
-       size_t match_len = strlen(match_prefix);
-
-       while (weston_config_next_section(ivi->config, &section, &section_name)) {
-               char *output_name;
-
-               if (i >= output_count)
-                       break;
-
-               if (strcmp(section_name, "output") != 0)
-                       continue;
-
-               weston_config_section_get_string(section, "name", &output_name, NULL);
-               if (output_name == NULL)
-                       continue;
-               if (strncmp(output_name, match_prefix, match_len) != 0) {
-                       free(output_name);
-                       continue;
-               }
-
-               if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
-                       free(output_name);
-                       return -1;
-               }
-
-               free(output_name);
-               ++i;
-       }
-
-       for (; i < output_count; ++i) {
-               if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
-                       return -1;
-
-               if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
-                       free(default_output);
-                       return -1;
-               }
-
-               free(default_output);
-       }
-
-       return 0;
-}
-
-static int
-load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
-{
-       struct weston_wayland_backend_config config = {
-               .base = {
-                       .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
-                       .struct_size = sizeof config,
-               },
-       };
-       struct weston_config_section *section;
-       int sprawl = 0;
-       int output_count;
-       int ret;
-
-       const struct weston_option options[] = {
-               { WESTON_OPTION_STRING, "display", 0, &config.display_name },
-               { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
-       };
-
-       windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
-                                     &config.fullscreen, &output_count);
-
-       parse_options(options, ARRAY_LENGTH(options), argc, argv);
-       config.sprawl = sprawl;
-
-       section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
-       weston_config_section_get_string(section, "cursor-theme",
-                                        &config.cursor_theme, NULL);
-       weston_config_section_get_int(section, "cursor-size",
-                                     &config.cursor_size, 32);
-
-       ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
-                                            &config.base);
-
-       free(config.cursor_theme);
-       free(config.display_name);
-
-       if (ret < 0)
-               return ret;
-
-       ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
-
-       /*
-        * We will just assume if load_backend() finished cleanly and
-        * windowed_output_api is not present that wayland backend is started
-        * with --sprawl or runs on fullscreen-shell. In this case, all values
-        * are hardcoded, so nothing can be configured; simply create and
-        * enable an output.
-        */
-       if (ivi->window_api == NULL)
-               return 0;
-
-       return windowed_create_outputs(ivi, output_count, "WL", "wayland");
-}
-
-#ifdef HAVE_BACKEND_X11
-static int
-load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
-{
-       struct weston_x11_backend_config config = {
-               .base = {
-                       .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
-                       .struct_size = sizeof config,
-               },
-       };
-       int no_input = 0;
-       int output_count;
-       int ret;
-
-       const struct weston_option options[] = {
-              { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
-       };
-
-       windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
-                                     &config.fullscreen, &output_count);
-
-       parse_options(options, ARRAY_LENGTH(options), argc, argv);
-       config.no_input = no_input;
-
-       ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
-                                            &config.base);
-
-       if (ret < 0)
-               return ret;
-
-       ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
-       if (!ivi->window_api) {
-               weston_log("Cannot use weston_windowed_output_api.\n");
-               return -1;
-       }
-
-       return windowed_create_outputs(ivi, output_count, "X", "screen");
-}
-#else
-static int
-load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
-{
-       return -1;
-}
-#endif
-
-static int
-load_backend(struct ivi_compositor *ivi, const char *backend,
-            int *argc, char *argv[])
-{
-       if (strcmp(backend, "drm-backend.so") == 0) {
-               return load_drm_backend(ivi, argc, argv);
-       } else if (strcmp(backend, "wayland-backend.so") == 0) {
-               return load_wayland_backend(ivi, argc, argv);
-       } else if (strcmp(backend, "x11-backend.so") == 0) {
-               return load_x11_backend(ivi, argc, argv);
-       }
-
-       weston_log("fatal: unknown backend '%s'.\n", backend);
-       return -1;
-}
-
-static char *
-choose_default_backend(void)
-{
-       char *backend = NULL;
-
-       if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
-               backend = strdup("wayland-backend.so");
-       else if (getenv("DISPLAY"))
-               backend = strdup("x11-backend.so");
-       else
-               backend = strdup("drm-backend.so");
-
-       return backend;
-}
-
-static int
-compositor_init_config(struct weston_compositor *compositor,
-                      struct weston_config *config)
-{
-       struct xkb_rule_names xkb_names;
-       struct weston_config_section *section;
-       int repaint_msec;
-       bool vt_switching;
-       bool require_input;
-
-       /* agl-compositor.ini [keyboard] */
-       section = weston_config_get_section(config, "keyboard", NULL, NULL);
-       weston_config_section_get_string(section, "keymap_rules",
-                                        (char **) &xkb_names.rules, NULL);
-       weston_config_section_get_string(section, "keymap_model",
-                                        (char **) &xkb_names.model, NULL);
-       weston_config_section_get_string(section, "keymap_layout",
-                                        (char **) &xkb_names.layout, NULL);
-       weston_config_section_get_string(section, "keymap_variant",
-                                        (char **) &xkb_names.variant, NULL);
-       weston_config_section_get_string(section, "keymap_options",
-                                        (char **) &xkb_names.options, NULL);
-
-       if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
-               return -1;
-
-       weston_config_section_get_int(section, "repeat-rate",
-                                     &compositor->kb_repeat_rate, 40);
-       weston_config_section_get_int(section, "repeat-delay",
-                                     &compositor->kb_repeat_delay, 400);
-
-       weston_config_section_get_bool(section, "vt-switching",
-                                      &vt_switching, true);
-       compositor->vt_switching = vt_switching;
-
-       /* agl-compositor.ini [core] */
-       section = weston_config_get_section(config, "core", NULL, NULL);
-
-       weston_config_section_get_bool(section, "require-input", &require_input, true);
-       compositor->require_input = require_input;
-
-       weston_config_section_get_int(section, "repaint-window", &repaint_msec,
-                                     compositor->repaint_msec);
-       if (repaint_msec < -10 || repaint_msec > 1000) {
-               weston_log("Invalid repaint_window value in config: %d\n",
-                          repaint_msec);
-       } else {
-               compositor->repaint_msec = repaint_msec;
-       }
-       weston_log("Output repaint window is %d ms maximum.\n",
-                  compositor->repaint_msec);
-
-       return 0;
-}
-
-struct ivi_surface *
-to_ivi_surface(struct weston_surface *surface)
-{
-       struct weston_desktop_surface *dsurface;
-
-       dsurface = weston_surface_get_desktop_surface(surface);
-       if (!dsurface)
-               return NULL;
-
-       return weston_desktop_surface_get_user_data(dsurface);
-}
-
-static void
-activate_binding(struct weston_seat *seat,
-                struct weston_view *focus_view)
-{
-       struct weston_surface *focus = focus_view->surface;
-       struct weston_surface *main_surface =
-               weston_surface_get_main_surface(focus);
-       struct ivi_surface *surface;
-
-       surface = to_ivi_surface(main_surface);
-       if (!surface)
-               return;
-
-       weston_seat_set_keyboard_focus(seat, focus);
-}
-
-static void
-click_to_activate_binding(struct weston_pointer *pointer,
-                         const struct timespec *time,
-                         uint32_t button, void *data)
-{
-       if (pointer->grab != &pointer->default_grab)
-               return;
-       if (pointer->focus == NULL)
-               return;
-
-       activate_binding(pointer->seat, pointer->focus);
-}
-
-static void
-touch_to_activate_binding(struct weston_touch *touch,
-                         const struct timespec *time,
-                         void *data)
-{
-       if (touch->grab != &touch->default_grab)
-               return;
-       if (touch->focus == NULL)
-               return;
-
-       activate_binding(touch->seat, touch->focus);
-}
-
-static void
-add_bindings(struct weston_compositor *compositor)
-{
-       weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
-                                            click_to_activate_binding,
-                                            NULL);
-       weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
-                                            click_to_activate_binding,
-                                            NULL);
-       weston_compositor_add_touch_binding(compositor, 0,
-                                           touch_to_activate_binding,
-                                           NULL);
-}
-
-static int
-create_listening_socket(struct wl_display *display, const char *socket_name)
-{
-       if (socket_name) {
-               if (wl_display_add_socket(display, socket_name)) {
-                       weston_log("fatal: failed to add socket: %s\n",
-                                  strerror(errno));
-                       return -1;
-               }
-       } else {
-               socket_name = wl_display_add_socket_auto(display);
-               if (!socket_name) {
-                       weston_log("fatal: failed to add socket: %s\n",
-                                  strerror(errno));
-                       return -1;
-               }
-       }
-
-       setenv("WAYLAND_DISPLAY", socket_name, 1);
-
-       return 0;
-}
-
-static bool
-global_filter(const struct wl_client *client, const struct wl_global *global,
-             void *data)
-{
-       return true;
-}
-
-static int
-load_config(struct weston_config **config, bool no_config,
-           const char *config_file)
-{
-       const char *file = "agl-compositor.ini";
-       const char *full_path;
-
-       if (config_file)
-               file = config_file;
-
-       if (!no_config)
-               *config = weston_config_parse(file);
-
-       if (*config) {
-               full_path = weston_config_get_full_path(*config);
-
-               weston_log("Using config file '%s'.\n", full_path);
-               setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
-
-               return 0;
-       }
-
-       if (config_file && !no_config) {
-               weston_log("fatal: error opening or reading config file '%s'.\n",
-                       config_file);
-
-               return -1;
-       }
-
-       weston_log("Starting with no config file.\n");
-       setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
-
-       return 0;
-}
-
-static FILE *logfile;
-
-static char *
-log_timestamp(char *buf, size_t len)
-{
-       struct timeval tv;
-       struct tm *brokendown_time;
-       char datestr[128];
-       char timestr[128];
-
-       gettimeofday(&tv, NULL);
-
-       brokendown_time = localtime(&tv.tv_sec);
-       if (brokendown_time == NULL) {
-               snprintf(buf, len, "%s", "[(NULL)localtime] ");
-               return buf;
-       }
-
-       memset(datestr, 0, sizeof(datestr));
-       if (brokendown_time->tm_mday != cached_tm_mday) {
-               strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n",
-                               brokendown_time);
-               cached_tm_mday = brokendown_time->tm_mday;
-       }
-
-       strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time);
-       /* if datestr is empty it prints only timestr*/
-       snprintf(buf, len, "%s[%s.%03li]", datestr,
-                       timestr, (tv.tv_usec / 1000));
-
-       return buf;
-}
-
-static void
-custom_handler(const char *fmt, va_list arg)
-{
-       char timestr[512];
-
-       weston_log_scope_printf(log_scope, "%s libwayland: ",
-                       log_timestamp(timestr, sizeof(timestr)));
-       weston_log_scope_vprintf(log_scope, fmt, arg);
-}
-
-static void
-log_file_open(const char *filename)
-{
-       wl_log_set_handler_server(custom_handler);
-
-       if (filename)
-               logfile = fopen(filename, "a");
-
-       if (!logfile) {
-               logfile = stderr;
-       } else {
-               os_fd_set_cloexec(fileno(logfile));
-               setvbuf(logfile, NULL, _IOLBF, 256);
-       }
-}
-
-static void
-log_file_close(void)
-{
-       if (logfile && logfile != stderr)
-               fclose(logfile);
-       logfile = stderr;
-}
-
-static int
-vlog(const char *fmt, va_list ap)
-{
-       const char *oom = "Out of memory";
-       char timestr[128];
-       int len = 0;
-       char *str;
-
-       if (weston_log_scope_is_enabled(log_scope)) {
-               int len_va;
-               char *xlog_timestamp = log_timestamp(timestr, sizeof(timestr));
-               len_va = vasprintf(&str, fmt, ap);
-               if (len_va >= 0) {
-                       len = weston_log_scope_printf(log_scope, "%s %s",
-                                       xlog_timestamp, str);
-                       free(str);
-               } else {
-                       len = weston_log_scope_printf(log_scope, "%s %s",
-                                       xlog_timestamp, oom);
-               }
-       }
-
-       return len;
-}
-
-
-static int
-vlog_continue(const char *fmt, va_list ap)
-{
-       return weston_log_scope_vprintf(log_scope, fmt, ap);
-}
-
-static int
-on_term_signal(int signo, void *data)
-{
-       struct wl_display *display = data;
-
-       weston_log("caught signal %d\n", signo);
-       wl_display_terminate(display);
-
-       return 1;
-}
-
-static void
-handle_exit(struct weston_compositor *compositor)
-{
-       wl_display_terminate(compositor->wl_display);
-}
-
-static void
-usage(int error_code)
-{
-       FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
-       fprintf(out,
-               "Usage: agl-compositor [OPTIONS]\n"
-               "\n"
-               "This is " PACKAGE_STRING ", the reference compositor for\n"
-               "Automotive Grade Linux. Weston-ivi supports multiple backends, and depending\n"
-               "on which backend is in use different options will be accepted.\n"
-               "\n"
-               "Core options:\n"
-               "\n"
-               "  --version\t\tPrint agl-compositor version\n"
-               "  -B, --backend=MODULE\tBackend module, one of\n"
-                       "\t\t\t\tdrm-backend.so\n"
-                       "\t\t\t\twayland-backend.so\n"
-                       "\t\t\t\tx11-backend.so\n"
-               "  -S, --socket=NAME\tName of socket to listen on\n"
-               "  --log=FILE\t\tLog to the given file\n"
-               "  -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
-               "  --no-config\t\tDo not read agl-compositor.ini\n"
-               "  --debug\t\tEnable debug extension(s)\n"
-               "  -h, --help\t\tThis help message\n"
-               "\n");
-       exit(error_code);
-}
-
 int main(int argc, char *argv[])
 {
-       struct ivi_compositor ivi = { 0 };
-       struct wl_display *display = NULL;
-       struct wl_event_loop *loop;
-       struct wl_event_source *signals[3] = { 0 };
-       struct weston_config_section *section;
-       /* Command line options */
-       char *backend = NULL;
-       char *socket_name = NULL;
-       char *log = NULL;
-       int help = 0;
-       int version = 0;
-       int no_config = 0;
-       int debug = 0;
-       char *config_file = NULL;
-       struct weston_log_context *log_ctx = NULL;
-       struct weston_log_subscriber *logger;
-       int ret = EXIT_FAILURE;
-
-       const struct weston_option core_options[] = {
-               { WESTON_OPTION_STRING, "backend", 'B', &backend },
-               { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
-               { WESTON_OPTION_STRING, "log", 0, &log },
-               { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
-               { WESTON_OPTION_BOOLEAN, "version", 0, &version },
-               { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
-               { WESTON_OPTION_BOOLEAN, "debug", 0, &debug },
-               { WESTON_OPTION_STRING, "config", 'c', &config_file },
-       };
-
-       wl_list_init(&ivi.outputs);
-       wl_list_init(&ivi.surfaces);
-       wl_list_init(&ivi.pending_surfaces);
-       wl_list_init(&ivi.popup_pending_apps);
-       wl_list_init(&ivi.fullscreen_pending_apps);
-       wl_list_init(&ivi.split_pending_apps);
-       wl_list_init(&ivi.remote_pending_apps);
-       wl_list_init(&ivi.desktop_clients);
-
-       /* Prevent any clients we spawn getting our stdin */
-       os_fd_set_cloexec(STDIN_FILENO);
-
-       parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
-
-       if (help)
-               usage(EXIT_SUCCESS);
-
-       if (version) {
-               printf(PACKAGE_STRING "\n");
-               return EXIT_SUCCESS;
-       }
-
-       log_ctx = weston_log_ctx_create();
-       if (!log_ctx) {
-               fprintf(stderr, "Failed to initialize weston debug framework.\n");
-               return ret;
-       }
-
-        log_scope = weston_log_ctx_add_log_scope(log_ctx, "log",
-                                                "agl-compositor log\n",
-                                                 NULL, NULL, NULL);
-
-       log_file_open(log);
-       weston_log_set_handler(vlog, vlog_continue);
-
-       logger = weston_log_subscriber_create_log(logfile);
-       weston_log_subscribe(log_ctx, logger, "log");
-
-       if (load_config(&ivi.config, no_config, config_file) < 0)
-               goto error_signals;
-       section = weston_config_get_section(ivi.config, "core", NULL, NULL);
-       if (!backend) {
-               weston_config_section_get_string(section, "backend", &backend,
-                                                NULL);
-               if (!backend)
-                       backend = choose_default_backend();
-       }
-       /* from [core] */
-       weston_config_section_get_bool(section, "hide-cursor", &ivi.hide_cursor, false);
-       weston_config_section_get_bool(section, "activate-by-default", &ivi.activate_by_default, true);
-
-       display = wl_display_create();
-       loop = wl_display_get_event_loop(display);
-
-       wl_display_set_global_filter(display,
-                                    global_filter, &ivi);
-
-       /* Register signal handlers so we shut down cleanly */
-
-       signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
-                                             display);
-       signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
-                                             display);
-       signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
-                                             display);
-
-       for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
-               if (!signals[i])
-                       goto error_signals;
-
-       ivi.compositor = weston_compositor_create(display, log_ctx, &ivi);
-       if (!ivi.compositor) {
-               weston_log("fatal: failed to create compositor.\n");
-               goto error_signals;
-       }
-
-       if (compositor_init_config(ivi.compositor, ivi.config) < 0)
-               goto error_compositor;
-
-       if (load_backend(&ivi, backend, &argc, argv) < 0) {
-               weston_log("fatal: failed to create compositor backend.\n");
-               goto error_compositor;
-       }
-
-       ivi.heads_changed.notify = heads_changed;
-       weston_compositor_add_heads_changed_listener(ivi.compositor,
-                                                    &ivi.heads_changed);
-
-       if (ivi_desktop_init(&ivi) < 0)
-               goto error_compositor;
-
-       ivi_seat_init(&ivi);
-
-       if (ivi_policy_init(&ivi) < 0)
-               goto error_compositor;
-
-       if (ivi_shell_init(&ivi) < 0)
-               goto error_compositor;
-
-       add_bindings(ivi.compositor);
-
-       weston_compositor_flush_heads_changed(ivi.compositor);
-
-       if (ivi.remoting_api)
-               ivi_enable_remote_outputs(&ivi);
-
-       if (ivi.waltham_transmitter_api)
-               ivi_enable_waltham_outputs(&ivi);
-
-       if (create_listening_socket(display, socket_name) < 0)
-               goto error_compositor;
-
-       ivi_shell_init_black_fs(&ivi);
-
-       ivi.compositor->exit = handle_exit;
-
-       weston_compositor_wake(ivi.compositor);
-
-       ivi_shell_create_global(&ivi);
-       ivi_launch_shell_client(&ivi);
-       if (debug)
-               ivi_screenshooter_create(&ivi);
-       ivi_agl_systemd_notify(&ivi);
-
-       wl_display_run(display);
-
-       ret = ivi.compositor->exit_code;
-
-       wl_display_destroy_clients(display);
-
-error_compositor:
-       weston_compositor_destroy(ivi.compositor);
-
-       weston_log_scope_destroy(log_scope);
-       log_scope = NULL;
-
-       weston_log_subscriber_destroy(logger);
-       weston_log_ctx_destroy(log_ctx);
-
-       ivi_policy_destroy(ivi.policy);
-
-error_signals:
-       for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
-               if (signals[i])
-                       wl_event_source_remove(signals[i]);
-
-       wl_display_destroy(display);
-
-       log_file_close();
-       if (ivi.config)
-               weston_config_destroy(ivi.config);
-
-       return ret;
+       return wet_main(argc, argv);
 }
index 8d70684..8a631d6 100644 (file)
@@ -133,7 +133,7 @@ static const struct ivi_policy_api policy_api = {
        .policy_rule_try_event = ivi_policy_default_try_event,
 };
 
-int
+WL_EXPORT int
 ivi_policy_init(struct ivi_compositor *ivi)
 {
        ivi->policy = ivi_policy_create(ivi, &policy_api, ivi);
diff --git a/src/systemd-notify.h b/src/systemd-notify.h
new file mode 100644 (file)
index 0000000..9f97221
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef __SYSTEMD_NOTIFY
+#define __SYSTEMD_NOTIFY
+
+int
+ivi_agl_systemd_notify(struct ivi_compositor *ivi);
+
+#endif
diff --git a/tests/agl-shell-test.c b/tests/agl-shell-test.c
new file mode 100644 (file)
index 0000000..6ece318
--- /dev/null
@@ -0,0 +1,44 @@
+#include "config.h"
+
+#include <stdint.h>
+#include <stdio.h>
+
+#include "weston-test-client-helper.h"
+#include "weston-test-fixture-compositor.h"
+#include "test-config.h"
+
+static enum test_result_code
+fixture_setup(struct weston_test_harness *harness)
+{
+       struct compositor_setup setup;
+
+       compositor_setup_defaults(&setup);
+       setup.renderer = RENDERER_PIXMAN;
+       setup.width = 320;
+       setup.height = 240;
+       setup.shell = SHELL_DESKTOP;
+
+       return weston_test_harness_execute_as_client(harness, &setup);
+}
+
+DECLARE_FIXTURE_SETUP(fixture_setup);
+
+
+TEST(agl_shell)
+{
+       struct client *client;
+       struct wl_surface *surface;
+
+       /* Create the client */
+       testlog("Creating client for test\n");
+
+       client = create_client_and_test_surface(100, 100, 100, 100);
+       assert(client);
+
+       surface = client->surface->wl_surface;
+       (void) surface;
+
+       testlog("Test complete\n");
+
+       client_destroy(client);
+}
diff --git a/tests/meson.build b/tests/meson.build
new file mode 100644 (file)
index 0000000..ea2f068
--- /dev/null
@@ -0,0 +1,89 @@
+dep_pixman = dependency('pixman-1', version: '>= 0.25.2')
+
+lib_test_runner = static_library(
+       'test-runner',
+       'weston-test-runner.c',
+       dependencies: [
+               dep_wayland_client,
+               libweston_dep,
+       ],
+       include_directories: common_inc,
+       install: false,
+)
+
+dep_test_runner = declare_dependency(
+       dependencies: dep_wayland_client,
+       link_with: lib_test_runner
+)
+
+lib_test_client = static_library(
+       'test-client',
+       [
+               'weston-test-client-helper.c',
+               'weston-test-fixture-compositor.c',
+               'testsuite-util.c',
+               '../shared/os-compatibility.c',
+               '../shared/xalloc.c',
+               weston_test_client_protocol_h,
+               weston_test_protocol_c,
+               viewporter_client_protocol_h,
+               viewporter_protocol_c,
+       ],
+       include_directories: common_inc,
+       dependencies: [
+               dep_wayland_client,
+               deps_libweston,
+               dep_libexec_compositor,
+               dep_pixman,
+               dependency('cairo'),
+       ],
+       install: false,
+)
+dep_test_client = declare_dependency(
+       link_with: lib_test_client,
+       sources: [
+               viewporter_client_protocol_h,
+       ],
+       dependencies: [
+               dep_wayland_client,
+               dep_test_runner,
+               dep_pixman,
+               dependency('libudev', version: '>= 136'),
+       ]
+)
+
+tests = [
+       { 'name': 'agl-shell', },
+]
+
+test_config_h = configuration_data()
+test_config_h.set_quoted('WESTON_TEST_REFERENCE_PATH', meson.current_source_dir() + '/reference')
+test_config_h.set_quoted('WESTON_MODULE_MAP', env_modmap)
+test_config_h.set_quoted('WESTON_DATA_DIR', join_paths(meson.current_source_dir(), '..', 'data'))
+message('Test suite plug-in path is ' + libweston_dep.get_pkgconfig_variable('libdir'))
+test_config_h.set_quoted('TESTSUITE_PLUGIN_PATH', libweston_dep.get_pkgconfig_variable('libdir') + '/' + 'test-plugin.so')
+configure_file(output: 'test-config.h', configuration: test_config_h)
+
+foreach t : tests
+       t_name = 'test-' + t.get('name')
+       t_sources = t.get('sources', [t.get('name') + '-test.c'])
+       t_sources += weston_test_client_protocol_h
+
+       t_deps = [ dep_test_client, libweston_dep ]
+       t_deps += t.get('dep_objs', [])
+
+       t_exe = executable(
+               t_name,
+               t_sources,
+               c_args: [
+                       '-DUNIT_TEST',
+                       '-DTHIS_TEST_NAME="' + t_name + '"',
+               ],
+               build_by_default: true,
+               include_directories: common_inc,
+               dependencies: t_deps,
+               install: false,
+       )
+
+       test(t.get('name'), t_exe, depends: t.get('test_deps', []))
+endforeach
diff --git a/tests/testsuite-util.c b/tests/testsuite-util.c
new file mode 100644 (file)
index 0000000..a4cd83e
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2019 Collabora, Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <wayland-util.h>
+#include <weston/weston.h>
+
+static struct wet_testsuite_data *wet_testsuite_data_global;
+
+/** Set global test suite data
+ *
+ * \param data Custom test suite data.
+ *
+ * The type struct wet_testsuite_data is free to be defined by any test suite
+ * in any way they want. This function stores a single pointer to that data
+ * in a global variable.
+ *
+ * The data is expected to be fetched from a test suite specific plugin that
+ * knows how to interpret it.
+ *
+ * \sa wet_testsuite_data_get
+ */
+WL_EXPORT void
+wet_testsuite_data_set(struct wet_testsuite_data *data)
+{
+       wet_testsuite_data_global = data;
+}
+
+/** Get global test suite data
+ *
+ * \return Custom test suite data.
+ *
+ * Returns the value last set with wet_testsuite_data_set().
+ */
+WL_EXPORT struct wet_testsuite_data *
+wet_testsuite_data_get(void)
+{
+       return wet_testsuite_data_global;
+}
diff --git a/tests/weston-test-client-helper.c b/tests/weston-test-client-helper.c
new file mode 100644 (file)
index 0000000..94b93ea
--- /dev/null
@@ -0,0 +1,1899 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ * Copyright © 2015 Samsung Electronics Co., Ltd
+ * Copyright 2016, 2017 Collabora, Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/mman.h>
+#include <cairo.h>
+
+#include "test-config.h"
+#include "shared/os-compatibility.h"
+#include "shared/xalloc.h"
+#include <libweston/zalloc.h>
+#include "weston-test-client-helper.h"
+
+#define max(a, b) (((a) > (b)) ? (a) : (b))
+#define min(a, b) (((a) > (b)) ? (b) : (a))
+#define clip(x, a, b)  min(max(x, a), b)
+
+int
+surface_contains(struct surface *surface, int x, int y)
+{
+       /* test whether a global x,y point is contained in the surface */
+       int sx = surface->x;
+       int sy = surface->y;
+       int sw = surface->width;
+       int sh = surface->height;
+       return x >= sx && y >= sy && x < sx + sw && y < sy + sh;
+}
+
+static void
+frame_callback_handler(void *data, struct wl_callback *callback, uint32_t time)
+{
+       int *done = data;
+
+       *done = 1;
+
+       wl_callback_destroy(callback);
+}
+
+static const struct wl_callback_listener frame_listener = {
+       frame_callback_handler
+};
+
+struct wl_callback *
+frame_callback_set(struct wl_surface *surface, int *done)
+{
+       struct wl_callback *callback;
+
+       *done = 0;
+       callback = wl_surface_frame(surface);
+       wl_callback_add_listener(callback, &frame_listener, done);
+
+       return callback;
+}
+
+int
+frame_callback_wait_nofail(struct client *client, int *done)
+{
+       while (!*done) {
+               if (wl_display_dispatch(client->wl_display) < 0)
+                       return 0;
+       }
+
+       return 1;
+}
+
+void
+move_client(struct client *client, int x, int y)
+{
+       struct surface *surface = client->surface;
+       int done;
+
+       client->surface->x = x;
+       client->surface->y = y;
+       weston_test_move_surface(client->test->weston_test, surface->wl_surface,
+                            surface->x, surface->y);
+       /* The attach here is necessary because commit() will call configure
+        * only on surfaces newly attached, and the one that sets the surface
+        * position is the configure. */
+       wl_surface_attach(surface->wl_surface, surface->buffer->proxy, 0, 0);
+       wl_surface_damage(surface->wl_surface, 0, 0, surface->width,
+                         surface->height);
+
+       frame_callback_set(surface->wl_surface, &done);
+
+       wl_surface_commit(surface->wl_surface);
+
+       frame_callback_wait(client, &done);
+}
+
+static void
+pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
+                    uint32_t serial, struct wl_surface *wl_surface,
+                    wl_fixed_t x, wl_fixed_t y)
+{
+       struct pointer *pointer = data;
+
+       if (wl_surface)
+               pointer->focus = wl_surface_get_user_data(wl_surface);
+       else
+               pointer->focus = NULL;
+
+       pointer->x = wl_fixed_to_int(x);
+       pointer->y = wl_fixed_to_int(y);
+
+       testlog("test-client: got pointer enter %d %d, surface %p\n",
+               pointer->x, pointer->y, pointer->focus);
+}
+
+static void
+pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
+                    uint32_t serial, struct wl_surface *wl_surface)
+{
+       struct pointer *pointer = data;
+
+       pointer->focus = NULL;
+
+       testlog("test-client: got pointer leave, surface %p\n",
+               wl_surface ? wl_surface_get_user_data(wl_surface) : NULL);
+}
+
+static void
+pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
+                     uint32_t time_msec, wl_fixed_t x, wl_fixed_t y)
+{
+       struct pointer *pointer = data;
+
+       pointer->x = wl_fixed_to_int(x);
+       pointer->y = wl_fixed_to_int(y);
+       pointer->motion_time_msec = time_msec;
+       pointer->motion_time_timespec = pointer->input_timestamp;
+       pointer->input_timestamp = (struct timespec) { 0 };
+
+       testlog("test-client: got pointer motion %d %d\n",
+               pointer->x, pointer->y);
+}
+
+static void
+pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
+                     uint32_t serial, uint32_t time_msec, uint32_t button,
+                     uint32_t state)
+{
+       struct pointer *pointer = data;
+
+       pointer->button = button;
+       pointer->state = state;
+       pointer->button_time_msec = time_msec;
+       pointer->button_time_timespec = pointer->input_timestamp;
+       pointer->input_timestamp = (struct timespec) { 0 };
+
+       testlog("test-client: got pointer button %u %u\n", button, state);
+}
+
+static void
+pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
+                   uint32_t time_msec, uint32_t axis, wl_fixed_t value)
+{
+       struct pointer *pointer = data;
+
+       pointer->axis = axis;
+       pointer->axis_value = wl_fixed_to_double(value);
+       pointer->axis_time_msec = time_msec;
+       pointer->axis_time_timespec = pointer->input_timestamp;
+       pointer->input_timestamp = (struct timespec) { 0 };
+
+       testlog("test-client: got pointer axis %u %f\n",
+               axis, wl_fixed_to_double(value));
+}
+
+static void
+pointer_handle_frame(void *data, struct wl_pointer *wl_pointer)
+{
+       testlog("test-client: got pointer frame\n");
+}
+
+static void
+pointer_handle_axis_source(void *data, struct wl_pointer *wl_pointer,
+                            uint32_t source)
+{
+       testlog("test-client: got pointer axis source %u\n", source);
+}
+
+static void
+pointer_handle_axis_stop(void *data, struct wl_pointer *wl_pointer,
+                        uint32_t time_msec, uint32_t axis)
+{
+       struct pointer *pointer = data;
+
+       pointer->axis = axis;
+       pointer->axis_stop_time_msec = time_msec;
+       pointer->axis_stop_time_timespec = pointer->input_timestamp;
+       pointer->input_timestamp = (struct timespec) { 0 };
+
+       testlog("test-client: got pointer axis stop %u\n", axis);
+}
+
+static void
+pointer_handle_axis_discrete(void *data, struct wl_pointer *wl_pointer,
+                            uint32_t axis, int32_t value)
+{
+       testlog("test-client: got pointer axis discrete %u %d\n", axis, value);
+}
+
+static const struct wl_pointer_listener pointer_listener = {
+       pointer_handle_enter,
+       pointer_handle_leave,
+       pointer_handle_motion,
+       pointer_handle_button,
+       pointer_handle_axis,
+       pointer_handle_frame,
+       pointer_handle_axis_source,
+       pointer_handle_axis_stop,
+       pointer_handle_axis_discrete,
+};
+
+static void
+keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
+                      uint32_t format, int fd, uint32_t size)
+{
+       close(fd);
+
+       testlog("test-client: got keyboard keymap\n");
+}
+
+static void
+keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard,
+                     uint32_t serial, struct wl_surface *wl_surface,
+                     struct wl_array *keys)
+{
+       struct keyboard *keyboard = data;
+
+       if (wl_surface)
+               keyboard->focus = wl_surface_get_user_data(wl_surface);
+       else
+               keyboard->focus = NULL;
+
+       testlog("test-client: got keyboard enter, surface %p\n",
+               keyboard->focus);
+}
+
+static void
+keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
+                     uint32_t serial, struct wl_surface *wl_surface)
+{
+       struct keyboard *keyboard = data;
+
+       keyboard->focus = NULL;
+
+       testlog("test-client: got keyboard leave, surface %p\n",
+               wl_surface ? wl_surface_get_user_data(wl_surface) : NULL);
+}
+
+static void
+keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard,
+                   uint32_t serial, uint32_t time_msec, uint32_t key,
+                   uint32_t state)
+{
+       struct keyboard *keyboard = data;
+
+       keyboard->key = key;
+       keyboard->state = state;
+       keyboard->key_time_msec = time_msec;
+       keyboard->key_time_timespec = keyboard->input_timestamp;
+       keyboard->input_timestamp = (struct timespec) { 0 };
+
+       testlog("test-client: got keyboard key %u %u\n", key, state);
+}
+
+static void
+keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
+                         uint32_t serial, uint32_t mods_depressed,
+                         uint32_t mods_latched, uint32_t mods_locked,
+                         uint32_t group)
+{
+       struct keyboard *keyboard = data;
+
+       keyboard->mods_depressed = mods_depressed;
+       keyboard->mods_latched = mods_latched;
+       keyboard->mods_locked = mods_locked;
+       keyboard->group = group;
+
+       testlog("test-client: got keyboard modifiers %u %u %u %u\n",
+               mods_depressed, mods_latched, mods_locked, group);
+}
+
+static void
+keyboard_handle_repeat_info(void *data, struct wl_keyboard *wl_keyboard,
+                           int32_t rate, int32_t delay)
+{
+       struct keyboard *keyboard = data;
+
+       keyboard->repeat_info.rate = rate;
+       keyboard->repeat_info.delay = delay;
+
+       testlog("test-client: got keyboard repeat_info %d %d\n", rate, delay);
+}
+
+static const struct wl_keyboard_listener keyboard_listener = {
+       keyboard_handle_keymap,
+       keyboard_handle_enter,
+       keyboard_handle_leave,
+       keyboard_handle_key,
+       keyboard_handle_modifiers,
+       keyboard_handle_repeat_info,
+};
+
+static void
+touch_handle_down(void *data, struct wl_touch *wl_touch,
+                 uint32_t serial, uint32_t time_msec,
+                 struct wl_surface *surface, int32_t id,
+                 wl_fixed_t x_w, wl_fixed_t y_w)
+{
+       struct touch *touch = data;
+
+       touch->down_x = wl_fixed_to_int(x_w);
+       touch->down_y = wl_fixed_to_int(y_w);
+       touch->id = id;
+       touch->down_time_msec = time_msec;
+       touch->down_time_timespec = touch->input_timestamp;
+       touch->input_timestamp = (struct timespec) { 0 };
+
+       testlog("test-client: got touch down %d %d, surf: %p, id: %d\n",
+               touch->down_x, touch->down_y, surface, id);
+}
+
+static void
+touch_handle_up(void *data, struct wl_touch *wl_touch,
+               uint32_t serial, uint32_t time_msec, int32_t id)
+{
+       struct touch *touch = data;
+       touch->up_id = id;
+       touch->up_time_msec = time_msec;
+       touch->up_time_timespec = touch->input_timestamp;
+       touch->input_timestamp = (struct timespec) { 0 };
+
+       testlog("test-client: got touch up, id: %d\n", id);
+}
+
+static void
+touch_handle_motion(void *data, struct wl_touch *wl_touch,
+                   uint32_t time_msec, int32_t id,
+                   wl_fixed_t x_w, wl_fixed_t y_w)
+{
+       struct touch *touch = data;
+       touch->x = wl_fixed_to_int(x_w);
+       touch->y = wl_fixed_to_int(y_w);
+       touch->motion_time_msec = time_msec;
+       touch->motion_time_timespec = touch->input_timestamp;
+       touch->input_timestamp = (struct timespec) { 0 };
+
+       testlog("test-client: got touch motion, %d %d, id: %d\n",
+               touch->x, touch->y, id);
+}
+
+static void
+touch_handle_frame(void *data, struct wl_touch *wl_touch)
+{
+       struct touch *touch = data;
+
+       ++touch->frame_no;
+
+       testlog("test-client: got touch frame (%d)\n", touch->frame_no);
+}
+
+static void
+touch_handle_cancel(void *data, struct wl_touch *wl_touch)
+{
+       struct touch *touch = data;
+
+       ++touch->cancel_no;
+
+       testlog("test-client: got touch cancel (%d)\n", touch->cancel_no);
+}
+
+static const struct wl_touch_listener touch_listener = {
+       touch_handle_down,
+       touch_handle_up,
+       touch_handle_motion,
+       touch_handle_frame,
+       touch_handle_cancel,
+};
+
+static void
+surface_enter(void *data,
+             struct wl_surface *wl_surface, struct wl_output *output)
+{
+       struct surface *surface = data;
+
+       surface->output = wl_output_get_user_data(output);
+
+       testlog("test-client: got surface enter output %p\n", surface->output);
+}
+
+static void
+surface_leave(void *data,
+             struct wl_surface *wl_surface, struct wl_output *output)
+{
+       struct surface *surface = data;
+
+       surface->output = NULL;
+
+       testlog("test-client: got surface leave output %p\n",
+               wl_output_get_user_data(output));
+}
+
+static const struct wl_surface_listener surface_listener = {
+       surface_enter,
+       surface_leave
+};
+
+static struct buffer *
+create_shm_buffer(struct client *client, int width, int height,
+                 pixman_format_code_t format, uint32_t wlfmt)
+{
+       struct wl_shm *shm = client->wl_shm;
+       struct buffer *buf;
+       size_t stride_bytes;
+       struct wl_shm_pool *pool;
+       int fd;
+       void *data;
+       size_t bytes_pp;
+
+       assert(width > 0);
+       assert(height > 0);
+
+       buf = xzalloc(sizeof *buf);
+
+       bytes_pp = PIXMAN_FORMAT_BPP(format) / 8;
+       stride_bytes = width * bytes_pp;
+       /* round up to multiple of 4 bytes for Pixman */
+       stride_bytes = (stride_bytes + 3) & ~3u;
+       assert(stride_bytes / bytes_pp >= (unsigned)width);
+
+       buf->len = stride_bytes * height;
+       assert(buf->len / stride_bytes == (unsigned)height);
+
+       fd = os_create_anonymous_file(buf->len);
+       assert(fd >= 0);
+
+       data = mmap(NULL, buf->len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+       if (data == MAP_FAILED) {
+               close(fd);
+               assert(data != MAP_FAILED);
+       }
+
+       pool = wl_shm_create_pool(shm, fd, buf->len);
+       buf->proxy = wl_shm_pool_create_buffer(pool, 0, width, height,
+                                              stride_bytes, wlfmt);
+       wl_shm_pool_destroy(pool);
+       close(fd);
+
+       buf->image = pixman_image_create_bits(format, width, height,
+                                             data, stride_bytes);
+
+       assert(buf->proxy);
+       assert(buf->image);
+
+       return buf;
+}
+
+struct buffer *
+create_shm_buffer_a8r8g8b8(struct client *client, int width, int height)
+{
+       assert(client->has_argb);
+
+       return create_shm_buffer(client, width, height,
+                                PIXMAN_a8r8g8b8, WL_SHM_FORMAT_ARGB8888);
+}
+
+void
+buffer_destroy(struct buffer *buf)
+{
+       void *pixels;
+
+       pixels = pixman_image_get_data(buf->image);
+
+       if (buf->proxy) {
+               wl_buffer_destroy(buf->proxy);
+               assert(munmap(pixels, buf->len) == 0);
+       }
+
+       assert(pixman_image_unref(buf->image));
+
+       free(buf);
+}
+
+static void
+shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
+{
+       struct client *client = data;
+
+       if (format == WL_SHM_FORMAT_ARGB8888)
+               client->has_argb = 1;
+}
+
+struct wl_shm_listener shm_listener = {
+       shm_format
+};
+
+static void
+test_handle_pointer_position(void *data, struct weston_test *weston_test,
+                            wl_fixed_t x, wl_fixed_t y)
+{
+       struct test *test = data;
+       test->pointer_x = wl_fixed_to_int(x);
+       test->pointer_y = wl_fixed_to_int(y);
+
+       testlog("test-client: got global pointer %d %d\n",
+               test->pointer_x, test->pointer_y);
+}
+
+static void
+test_handle_capture_screenshot_done(void *data, struct weston_test *weston_test)
+{
+       struct test *test = data;
+
+       testlog("Screenshot has been captured\n");
+       test->buffer_copy_done = 1;
+}
+
+static const struct weston_test_listener test_listener = {
+       test_handle_pointer_position,
+       test_handle_capture_screenshot_done,
+};
+
+static void
+input_destroy(struct input *inp)
+{
+       if (inp->pointer) {
+               wl_pointer_release(inp->pointer->wl_pointer);
+               free(inp->pointer);
+       }
+       if (inp->keyboard) {
+               wl_keyboard_release(inp->keyboard->wl_keyboard);
+               free(inp->keyboard);
+       }
+       if (inp->touch) {
+               wl_touch_release(inp->touch->wl_touch);
+               free(inp->touch);
+       }
+       wl_list_remove(&inp->link);
+       wl_seat_release(inp->wl_seat);
+       free(inp->seat_name);
+       free(inp);
+}
+
+static void
+input_update_devices(struct input *input)
+{
+       struct pointer *pointer;
+       struct keyboard *keyboard;
+       struct touch *touch;
+
+       struct wl_seat *seat = input->wl_seat;
+       enum wl_seat_capability caps = input->caps;
+
+       if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
+               pointer = xzalloc(sizeof *pointer);
+               pointer->wl_pointer = wl_seat_get_pointer(seat);
+               wl_pointer_set_user_data(pointer->wl_pointer, pointer);
+               wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener,
+                                       pointer);
+               input->pointer = pointer;
+       } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
+               wl_pointer_destroy(input->pointer->wl_pointer);
+               free(input->pointer);
+               input->pointer = NULL;
+       }
+
+       if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
+               keyboard = xzalloc(sizeof *keyboard);
+               keyboard->wl_keyboard = wl_seat_get_keyboard(seat);
+               wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard);
+               wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener,
+                                        keyboard);
+               input->keyboard = keyboard;
+       } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
+               wl_keyboard_destroy(input->keyboard->wl_keyboard);
+               free(input->keyboard);
+               input->keyboard = NULL;
+       }
+
+       if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
+               touch = xzalloc(sizeof *touch);
+               touch->wl_touch = wl_seat_get_touch(seat);
+               wl_touch_set_user_data(touch->wl_touch, touch);
+               wl_touch_add_listener(touch->wl_touch, &touch_listener,
+                                        touch);
+               input->touch = touch;
+       } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
+               wl_touch_destroy(input->touch->wl_touch);
+               free(input->touch);
+               input->touch = NULL;
+       }
+}
+
+static void
+seat_handle_capabilities(void *data, struct wl_seat *seat,
+                        enum wl_seat_capability caps)
+{
+       struct input *input = data;
+
+       input->caps = caps;
+
+       /* we will create/update the devices only with the right (test) seat.
+        * If we haven't discovered which seat is the test seat, just
+        * store capabilities and bail out */
+       if (input->seat_name && strcmp(input->seat_name, "test-seat") == 0)
+               input_update_devices(input);
+
+       testlog("test-client: got seat %p capabilities: %x\n", input, caps);
+}
+
+static void
+seat_handle_name(void *data, struct wl_seat *seat, const char *name)
+{
+       struct input *input = data;
+
+       input->seat_name = strdup(name);
+       assert(input->seat_name && "No memory");
+
+       /* We only update the devices and set client input for the test seat */
+       if (strcmp(name, "test-seat") == 0) {
+               assert(!input->client->input &&
+                      "Multiple test seats detected!");
+
+               input_update_devices(input);
+               input->client->input = input;
+       }
+
+       testlog("test-client: got seat %p name: \'%s\'\n", input, name);
+}
+
+static const struct wl_seat_listener seat_listener = {
+       seat_handle_capabilities,
+       seat_handle_name,
+};
+
+static void
+output_handle_geometry(void *data,
+                      struct wl_output *wl_output,
+                      int x, int y,
+                      int physical_width,
+                      int physical_height,
+                      int subpixel,
+                      const char *make,
+                      const char *model,
+                      int32_t transform)
+{
+       struct output *output = data;
+
+       output->x = x;
+       output->y = y;
+}
+
+static void
+output_handle_mode(void *data,
+                  struct wl_output *wl_output,
+                  uint32_t flags,
+                  int width,
+                  int height,
+                  int refresh)
+{
+       struct output *output = data;
+
+       if (flags & WL_OUTPUT_MODE_CURRENT) {
+               output->width = width;
+               output->height = height;
+       }
+}
+
+static void
+output_handle_scale(void *data,
+                   struct wl_output *wl_output,
+                   int scale)
+{
+       struct output *output = data;
+
+       output->scale = scale;
+}
+
+static void
+output_handle_done(void *data,
+                  struct wl_output *wl_output)
+{
+       struct output *output = data;
+
+       output->initialized = 1;
+}
+
+static const struct wl_output_listener output_listener = {
+       output_handle_geometry,
+       output_handle_mode,
+       output_handle_done,
+       output_handle_scale,
+};
+
+static void
+output_destroy(struct output *output)
+{
+       assert(wl_proxy_get_version((struct wl_proxy *)output->wl_output) >= 3);
+       wl_output_release(output->wl_output);
+       wl_list_remove(&output->link);
+       free(output);
+}
+
+static void
+handle_global(void *data, struct wl_registry *registry,
+             uint32_t id, const char *interface, uint32_t version)
+{
+       struct client *client = data;
+       struct output *output;
+       struct test *test;
+       struct global *global;
+       struct input *input;
+
+       global = xzalloc(sizeof *global);
+       global->name = id;
+       global->interface = strdup(interface);
+       assert(interface);
+       global->version = version;
+       wl_list_insert(client->global_list.prev, &global->link);
+
+       /* We deliberately bind all globals with the maximum (advertised)
+        * version, because this test suite must be kept up-to-date with
+        * Weston. We must always implement at least the version advertised
+        * by Weston. This is not ok for normal clients, but it is ok in
+        * this test suite.
+        */
+
+       if (strcmp(interface, "wl_compositor") == 0) {
+               client->wl_compositor =
+                       wl_registry_bind(registry, id,
+                                        &wl_compositor_interface, version);
+       } else if (strcmp(interface, "wl_seat") == 0) {
+               input = xzalloc(sizeof *input);
+               input->client = client;
+               input->global_name = global->name;
+               input->wl_seat =
+                       wl_registry_bind(registry, id,
+                                        &wl_seat_interface, version);
+               wl_seat_add_listener(input->wl_seat, &seat_listener, input);
+               wl_list_insert(&client->inputs, &input->link);
+       } else if (strcmp(interface, "wl_shm") == 0) {
+               client->wl_shm =
+                       wl_registry_bind(registry, id,
+                                        &wl_shm_interface, version);
+               wl_shm_add_listener(client->wl_shm, &shm_listener, client);
+       } else if (strcmp(interface, "wl_output") == 0) {
+               output = xzalloc(sizeof *output);
+               output->wl_output =
+                       wl_registry_bind(registry, id,
+                                        &wl_output_interface, version);
+               wl_output_add_listener(output->wl_output,
+                                      &output_listener, output);
+               wl_list_insert(&client->output_list, &output->link);
+               client->output = output;
+       } else if (strcmp(interface, "weston_test") == 0) {
+               test = xzalloc(sizeof *test);
+               test->weston_test =
+                       wl_registry_bind(registry, id,
+                                        &weston_test_interface, version);
+               weston_test_add_listener(test->weston_test, &test_listener, test);
+               client->test = test;
+       } else if (strcmp(interface, "wl_drm") == 0) {
+               client->has_wl_drm = true;
+       }
+}
+
+static struct global *
+client_find_global_with_name(struct client *client, uint32_t name)
+{
+       struct global *global;
+
+       wl_list_for_each(global, &client->global_list, link) {
+               if (global->name == name)
+                       return global;
+       }
+
+       return NULL;
+}
+
+static struct input *
+client_find_input_with_name(struct client *client, uint32_t name)
+{
+       struct input *input;
+
+       wl_list_for_each(input, &client->inputs, link) {
+               if (input->global_name == name)
+                       return input;
+       }
+
+       return NULL;
+}
+
+static void
+global_destroy(struct global *global)
+{
+       wl_list_remove(&global->link);
+       free(global->interface);
+       free(global);
+}
+
+static void
+handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
+{
+       struct client *client = data;
+       struct global *global;
+       struct input *input;
+
+       global = client_find_global_with_name(client, name);
+       assert(global && "Request to remove unknown global");
+
+       if (strcmp(global->interface, "wl_seat") == 0) {
+               input = client_find_input_with_name(client, name);
+               if (input) {
+                       if (client->input == input)
+                               client->input = NULL;
+                       input_destroy(input);
+               }
+       }
+
+       /* XXX: handle wl_output */
+
+       global_destroy(global);
+}
+
+static const struct wl_registry_listener registry_listener = {
+       handle_global,
+       handle_global_remove,
+};
+
+void
+skip(const char *fmt, ...)
+{
+       va_list argp;
+
+       va_start(argp, fmt);
+       vfprintf(stderr, fmt, argp);
+       va_end(argp);
+
+       /* automake tests uses exit code 77. weston-test-runner will see
+        * this and use it, and then weston-test's sigchld handler (in the
+        * weston process) will use that as an exit status, which is what
+        * ninja will see in the end. */
+       exit(77);
+}
+
+void
+expect_protocol_error(struct client *client,
+                     const struct wl_interface *intf,
+                     uint32_t code)
+{
+       int err;
+       uint32_t errcode, failed = 0;
+       const struct wl_interface *interface;
+       unsigned int id;
+
+       /* if the error has not come yet, make it happen */
+       wl_display_roundtrip(client->wl_display);
+
+       err = wl_display_get_error(client->wl_display);
+
+       assert(err && "Expected protocol error but nothing came");
+       assert(err == EPROTO && "Expected protocol error but got local error");
+
+       errcode = wl_display_get_protocol_error(client->wl_display,
+                                               &interface, &id);
+
+       /* check error */
+       if (errcode != code) {
+               testlog("Should get error code %d but got %d\n", code, errcode);
+               failed = 1;
+       }
+
+       /* this should be definitely set */
+       assert(interface);
+
+       if (strcmp(intf->name, interface->name) != 0) {
+               testlog("Should get interface '%s' but got '%s'\n",
+                       intf->name, interface->name);
+               failed = 1;
+       }
+
+       if (failed) {
+               testlog("Expected other protocol error\n");
+               abort();
+       }
+
+       /* all OK */
+       testlog("Got expected protocol error on '%s' (object id: %d) "
+               "with code %d\n", interface->name, id, errcode);
+}
+
+static void
+log_handler(const char *fmt, va_list args)
+{
+       fprintf(stderr, "libwayland: ");
+       vfprintf(stderr, fmt, args);
+}
+
+struct client *
+create_client(void)
+{
+       struct client *client;
+
+       wl_log_set_handler_client(log_handler);
+
+       /* connect to display */
+       client = xzalloc(sizeof *client);
+       client->wl_display = wl_display_connect(NULL);
+       assert(client->wl_display);
+       wl_list_init(&client->global_list);
+       wl_list_init(&client->inputs);
+       wl_list_init(&client->output_list);
+
+       /* setup registry so we can bind to interfaces */
+       client->wl_registry = wl_display_get_registry(client->wl_display);
+       wl_registry_add_listener(client->wl_registry, &registry_listener,
+                                client);
+
+       /* this roundtrip makes sure we have all globals and we bound to them */
+       client_roundtrip(client);
+       /* this roundtrip makes sure we got all wl_shm.format and wl_seat.*
+        * events */
+       client_roundtrip(client);
+
+       /* must have WL_SHM_FORMAT_ARGB32 */
+       assert(client->has_argb);
+
+       /* must have weston_test interface */
+       assert(client->test);
+
+       /* must have an output */
+       assert(client->output);
+
+       /* the output must be initialized */
+       assert(client->output->initialized == 1);
+
+       /* must have seat set */
+       assert(client->input);
+
+       return client;
+}
+
+struct surface *
+create_test_surface(struct client *client)
+{
+       struct surface *surface;
+
+       surface = xzalloc(sizeof *surface);
+
+       surface->wl_surface =
+               wl_compositor_create_surface(client->wl_compositor);
+       assert(surface->wl_surface);
+
+       wl_surface_add_listener(surface->wl_surface, &surface_listener,
+                               surface);
+
+       wl_surface_set_user_data(surface->wl_surface, surface);
+
+       return surface;
+}
+
+void
+surface_destroy(struct surface *surface)
+{
+       if (surface->wl_surface)
+               wl_surface_destroy(surface->wl_surface);
+       if (surface->buffer)
+               buffer_destroy(surface->buffer);
+       free(surface);
+}
+
+struct client *
+create_client_and_test_surface(int x, int y, int width, int height)
+{
+       struct client *client;
+       struct surface *surface;
+       pixman_color_t color = { 16384, 16384, 16384, 16384 }; /* uint16_t */
+       pixman_image_t *solid;
+
+       client = create_client();
+
+       /* initialize the client surface */
+       surface = create_test_surface(client);
+       client->surface = surface;
+
+       surface->width = width;
+       surface->height = height;
+       surface->buffer = create_shm_buffer_a8r8g8b8(client, width, height);
+
+       solid = pixman_image_create_solid_fill(&color);
+       pixman_image_composite32(PIXMAN_OP_SRC,
+                                solid, /* src */
+                                NULL, /* mask */
+                                surface->buffer->image, /* dst */
+                                0, 0, /* src x,y */
+                                0, 0, /* mask x,y */
+                                0, 0, /* dst x,y */
+                                width, height);
+       pixman_image_unref(solid);
+
+       move_client(client, x, y);
+
+       return client;
+}
+
+void
+client_destroy(struct client *client)
+{
+       if (client->surface)
+               surface_destroy(client->surface);
+
+       while (!wl_list_empty(&client->inputs)) {
+               input_destroy(container_of(client->inputs.next,
+                                          struct input, link));
+       }
+
+       while (!wl_list_empty(&client->output_list)) {
+               output_destroy(container_of(client->output_list.next,
+                                           struct output, link));
+       }
+
+       while (!wl_list_empty(&client->global_list)) {
+               global_destroy(container_of(client->global_list.next,
+                                           struct global, link));
+       }
+
+       if (client->test) {
+               weston_test_destroy(client->test->weston_test);
+               free(client->test);
+       }
+
+       if (client->wl_shm)
+               wl_shm_destroy(client->wl_shm);
+       if (client->wl_compositor)
+               wl_compositor_destroy(client->wl_compositor);
+       if (client->wl_registry)
+               wl_registry_destroy(client->wl_registry);
+
+       client_roundtrip(client);
+
+       if (client->wl_display)
+               wl_display_disconnect(client->wl_display);
+       free(client);
+}
+
+static const char*
+output_path(void)
+{
+       char *path = getenv("WESTON_TEST_OUTPUT_PATH");
+
+       if (!path)
+               return ".";
+
+       return path;
+}
+
+char*
+screenshot_output_filename(const char *basename, uint32_t seq)
+{
+       char *filename;
+
+       if (asprintf(&filename, "%s/%s-%02d.png",
+                                output_path(), basename, seq) < 0)
+               return NULL;
+       return filename;
+}
+
+static const char*
+reference_path(void)
+{
+       char *path = getenv("WESTON_TEST_REFERENCE_PATH");
+
+       if (!path)
+               return WESTON_TEST_REFERENCE_PATH;
+       return path;
+}
+
+char*
+screenshot_reference_filename(const char *basename, uint32_t seq)
+{
+       char *filename;
+
+       if (asprintf(&filename, "%s/%s-%02d.png",
+                                reference_path(), basename, seq) < 0)
+               return NULL;
+       return filename;
+}
+
+char *
+image_filename(const char *basename)
+{
+       char *filename;
+
+       if (asprintf(&filename, "%s/%s.png", reference_path(), basename) < 0)
+               assert(0);
+       return filename;
+}
+
+struct format_map_entry {
+       cairo_format_t cairo;
+       pixman_format_code_t pixman;
+};
+
+static const struct format_map_entry format_map[] = {
+       { CAIRO_FORMAT_ARGB32,    PIXMAN_a8r8g8b8 },
+       { CAIRO_FORMAT_RGB24,     PIXMAN_x8r8g8b8 },
+       { CAIRO_FORMAT_A8,        PIXMAN_a8 },
+       { CAIRO_FORMAT_RGB16_565, PIXMAN_r5g6b5 },
+};
+
+static pixman_format_code_t
+format_cairo2pixman(cairo_format_t fmt)
+{
+       unsigned i;
+
+       for (i = 0; i < ARRAY_LENGTH(format_map); i++)
+               if (format_map[i].cairo == fmt)
+                       return format_map[i].pixman;
+
+       assert(0 && "unknown Cairo pixel format");
+}
+
+static cairo_format_t
+format_pixman2cairo(pixman_format_code_t fmt)
+{
+       unsigned i;
+
+       for (i = 0; i < ARRAY_LENGTH(format_map); i++)
+               if (format_map[i].pixman == fmt)
+                       return format_map[i].cairo;
+
+       assert(0 && "unknown Pixman pixel format");
+}
+
+/**
+ * Validate range
+ *
+ * \param r Range to validate or NULL.
+ * \return The given range, or {0, 0} for NULL.
+ *
+ * Will abort if range is invalid, that is a > b.
+ */
+static struct range
+range_get(const struct range *r)
+{
+       if (!r)
+               return (struct range){ 0, 0 };
+
+       assert(r->a <= r->b);
+       return *r;
+}
+
+/**
+ * Compute the ROI for image comparisons
+ *
+ * \param img_a An image.
+ * \param img_b Another image.
+ * \param clip_rect Explicit ROI, or NULL for using the whole
+ * image area.
+ *
+ * \return The region of interest (ROI) that is guaranteed to be inside both
+ * images.
+ *
+ * If clip_rect is given, it must fall inside of both images.
+ * If clip_rect is NULL, the images must be of the same size.
+ * If any precondition is violated, this function aborts with an error.
+ *
+ * The ROI is given as pixman_box32_t, where x2,y2 are non-inclusive.
+ */
+static pixman_box32_t
+image_check_get_roi(pixman_image_t *img_a, pixman_image_t *img_b,
+                  const struct rectangle *clip_rect)
+{
+       int width_a;
+       int width_b;
+       int height_a;
+       int height_b;
+       pixman_box32_t box;
+
+       width_a = pixman_image_get_width(img_a);
+       height_a = pixman_image_get_height(img_a);
+
+       width_b = pixman_image_get_width(img_b);
+       height_b = pixman_image_get_height(img_b);
+
+       if (clip_rect) {
+               box.x1 = clip_rect->x;
+               box.y1 = clip_rect->y;
+               box.x2 = clip_rect->x + clip_rect->width;
+               box.y2 = clip_rect->y + clip_rect->height;
+       } else {
+               box.x1 = 0;
+               box.y1 = 0;
+               box.x2 = max(width_a, width_b);
+               box.y2 = max(height_a, height_b);
+       }
+
+       assert(box.x1 >= 0);
+       assert(box.y1 >= 0);
+       assert(box.x2 > box.x1);
+       assert(box.y2 > box.y1);
+       assert(box.x2 <= width_a);
+       assert(box.x2 <= width_b);
+       assert(box.y2 <= height_a);
+       assert(box.y2 <= height_b);
+
+       return box;
+}
+
+struct image_iterator {
+       char *data;
+       int stride; /* bytes */
+};
+
+static void
+image_iter_init(struct image_iterator *it, pixman_image_t *image)
+{
+       pixman_format_code_t fmt;
+
+       it->stride = pixman_image_get_stride(image);
+       it->data = (void *)pixman_image_get_data(image);
+
+       fmt = pixman_image_get_format(image);
+       assert(PIXMAN_FORMAT_BPP(fmt) == 32);
+}
+
+static uint32_t *
+image_iter_get_row(struct image_iterator *it, int y)
+{
+       return (uint32_t *)(it->data + y * it->stride);
+}
+
+struct pixel_diff_stat {
+       struct pixel_diff_stat_channel {
+               int min_diff;
+               int max_diff;
+       } ch[4];
+};
+
+static void
+testlog_pixel_diff_stat(const struct pixel_diff_stat *stat)
+{
+       int i;
+
+       testlog("Image difference statistics:\n");
+       for (i = 0; i < 4; i++) {
+               testlog("\tch %d: [%d, %d]\n",
+                       i, stat->ch[i].min_diff, stat->ch[i].max_diff);
+       }
+}
+
+static bool
+fuzzy_match_pixels(uint32_t pix_a, uint32_t pix_b,
+                  const struct range *fuzz,
+                  struct pixel_diff_stat *stat)
+{
+       bool ret = true;
+       int shift;
+       int i;
+
+       for (shift = 0, i = 0; i < 4; shift += 8, i++) {
+               int val_a = (pix_a >> shift) & 0xffu;
+               int val_b = (pix_b >> shift) & 0xffu;
+               int d = val_b - val_a;
+
+               stat->ch[i].min_diff = min(stat->ch[i].min_diff, d);
+               stat->ch[i].max_diff = max(stat->ch[i].max_diff, d);
+
+               if (d < fuzz->a || d > fuzz->b)
+                       ret = false;
+       }
+
+       return ret;
+}
+
+/**
+ * Test if a given region within two images are pixel-identical
+ *
+ * Returns true if the two images pixel-wise identical, and false otherwise.
+ *
+ * \param img_a First image.
+ * \param img_b Second image.
+ * \param clip_rect The region of interest, or NULL for comparing the whole
+ * images.
+ * \param prec Per-channel allowed difference, or NULL for identical match
+ * required.
+ *
+ * This function hard-fails if clip_rect is not inside both images. If clip_rect
+ * is given, the images do not have to match in size, otherwise size mismatch
+ * will be a hard failure.
+ *
+ * The per-pixel, per-channel difference is computed as img_b - img_a which is
+ * required to be in the range [prec->a, prec->b] inclusive. The difference is
+ * signed. All four channels are compared the same way, without any special
+ * meaning on alpha channel.
+ */
+bool
+check_images_match(pixman_image_t *img_a, pixman_image_t *img_b,
+                  const struct rectangle *clip_rect, const struct range *prec)
+{
+       struct range fuzz = range_get(prec);
+       struct pixel_diff_stat diffstat = {};
+       struct image_iterator it_a;
+       struct image_iterator it_b;
+       pixman_box32_t box;
+       int x, y;
+       uint32_t *pix_a;
+       uint32_t *pix_b;
+
+       box = image_check_get_roi(img_a, img_b, clip_rect);
+
+       image_iter_init(&it_a, img_a);
+       image_iter_init(&it_b, img_b);
+
+       for (y = box.y1; y < box.y2; y++) {
+               pix_a = image_iter_get_row(&it_a, y) + box.x1;
+               pix_b = image_iter_get_row(&it_b, y) + box.x1;
+
+               for (x = box.x1; x < box.x2; x++) {
+                       if (!fuzzy_match_pixels(*pix_a, *pix_b,
+                                               &fuzz, &diffstat))
+                               return false;
+
+                       pix_a++;
+                       pix_b++;
+               }
+       }
+
+       return true;
+}
+
+/**
+ * Tint a color
+ *
+ * \param src Source pixel as x8r8g8b8.
+ * \param add The tint as x8r8g8b8, x8 must be zero; r8, g8 and b8 must be
+ * no greater than 0xc0 to avoid overflow to another channel.
+ * \return The tinted pixel color as x8r8g8b8, x8 guaranteed to be 0xff.
+ *
+ * The source pixel RGB values are divided by 4, and then the tint is added.
+ * To achieve colors outside of the range of src, a tint color channel must be
+ * at least 0x40. (0xff / 4 = 0x3f, 0xff - 0x3f = 0xc0)
+ */
+static uint32_t
+tint(uint32_t src, uint32_t add)
+{
+       uint32_t v;
+
+       v = ((src & 0xfcfcfcfc) >> 2) | 0xff000000;
+
+       return v + add;
+}
+
+/**
+ * Create a visualization of image differences.
+ *
+ * \param img_a First image, which is used as the basis for the output.
+ * \param img_b Second image.
+ * \param clip_rect The region of interest, or NULL for comparing the whole
+ * images.
+ * \param prec Per-channel allowed difference, or NULL for identical match
+ * required.
+ * \return A new image with the differences highlighted.
+ *
+ * Regions outside of the region of interest are shaded with black, matching
+ * pixels are shaded with green, and differing pixels are shaded with
+ * bright red.
+ *
+ * This function hard-fails if clip_rect is not inside both images. If clip_rect
+ * is given, the images do not have to match in size, otherwise size mismatch
+ * will be a hard failure.
+ *
+ * The per-pixel, per-channel difference is computed as img_b - img_a which is
+ * required to be in the range [prec->a, prec->b] inclusive. The difference is
+ * signed. All four channels are compared the same way, without any special
+ * meaning on alpha channel.
+ */
+pixman_image_t *
+visualize_image_difference(pixman_image_t *img_a, pixman_image_t *img_b,
+                          const struct rectangle *clip_rect,
+                          const struct range *prec)
+{
+       struct range fuzz = range_get(prec);
+       struct pixel_diff_stat diffstat = {};
+       pixman_image_t *diffimg;
+       pixman_image_t *shade;
+       struct image_iterator it_a;
+       struct image_iterator it_b;
+       struct image_iterator it_d;
+       int width;
+       int height;
+       pixman_box32_t box;
+       int x, y;
+       uint32_t *pix_a;
+       uint32_t *pix_b;
+       uint32_t *pix_d;
+       pixman_color_t shade_color = { 0, 0, 0, 32768 };
+
+       width = pixman_image_get_width(img_a);
+       height = pixman_image_get_height(img_a);
+       box = image_check_get_roi(img_a, img_b, clip_rect);
+
+       diffimg = pixman_image_create_bits_no_clear(PIXMAN_x8r8g8b8,
+                                                   width, height, NULL, 0);
+
+       /* Fill diffimg with a black-shaded copy of img_a, and then fill
+        * the clip_rect area with original img_a.
+        */
+       shade = pixman_image_create_solid_fill(&shade_color);
+       pixman_image_composite32(PIXMAN_OP_SRC, img_a, shade, diffimg,
+                                0, 0, 0, 0, 0, 0, width, height);
+       pixman_image_unref(shade);
+       pixman_image_composite32(PIXMAN_OP_SRC, img_a, NULL, diffimg,
+                                box.x1, box.y1, 0, 0, box.x1, box.y1,
+                                box.x2 - box.x1, box.y2 - box.y1);
+
+       image_iter_init(&it_a, img_a);
+       image_iter_init(&it_b, img_b);
+       image_iter_init(&it_d, diffimg);
+
+       for (y = box.y1; y < box.y2; y++) {
+               pix_a = image_iter_get_row(&it_a, y) + box.x1;
+               pix_b = image_iter_get_row(&it_b, y) + box.x1;
+               pix_d = image_iter_get_row(&it_d, y) + box.x1;
+
+               for (x = box.x1; x < box.x2; x++) {
+                       if (fuzzy_match_pixels(*pix_a, *pix_b,
+                                              &fuzz, &diffstat))
+                               *pix_d = tint(*pix_d, 0x00008000); /* green */
+                       else
+                               *pix_d = tint(*pix_d, 0x00c00000); /* red */
+
+                       pix_a++;
+                       pix_b++;
+                       pix_d++;
+               }
+       }
+
+       testlog_pixel_diff_stat(&diffstat);
+
+       return diffimg;
+}
+
+/**
+ * Write an image into a PNG file.
+ *
+ * \param image The image.
+ * \param fname The name and path for the file.
+ *
+ * \returns true if successfully saved file; false otherwise.
+ *
+ * \note Only image formats directly supported by Cairo are accepted, not all
+ * Pixman formats.
+ */
+bool
+write_image_as_png(pixman_image_t *image, const char *fname)
+{
+       cairo_surface_t *cairo_surface;
+       cairo_status_t status;
+       cairo_format_t fmt;
+
+       fmt = format_pixman2cairo(pixman_image_get_format(image));
+
+       cairo_surface = cairo_image_surface_create_for_data(
+                       (void *)pixman_image_get_data(image),
+                       fmt,
+                       pixman_image_get_width(image),
+                       pixman_image_get_height(image),
+                       pixman_image_get_stride(image));
+
+       status = cairo_surface_write_to_png(cairo_surface, fname);
+       if (status != CAIRO_STATUS_SUCCESS) {
+               testlog("Failed to save image '%s': %s\n", fname,
+                       cairo_status_to_string(status));
+
+               return false;
+       }
+
+       cairo_surface_destroy(cairo_surface);
+
+       return true;
+}
+
+static pixman_image_t *
+image_convert_to_a8r8g8b8(pixman_image_t *image)
+{
+       pixman_image_t *ret;
+       int width;
+       int height;
+
+       if (pixman_image_get_format(image) == PIXMAN_a8r8g8b8)
+               return pixman_image_ref(image);
+
+       width = pixman_image_get_width(image);
+       height = pixman_image_get_height(image);
+
+       ret = pixman_image_create_bits_no_clear(PIXMAN_a8r8g8b8, width, height,
+                                               NULL, 0);
+       assert(ret);
+
+       pixman_image_composite32(PIXMAN_OP_SRC, image, NULL, ret,
+                                0, 0, 0, 0, 0, 0, width, height);
+
+       return ret;
+}
+
+static void
+destroy_cairo_surface(pixman_image_t *image, void *data)
+{
+       cairo_surface_t *surface = data;
+
+       cairo_surface_destroy(surface);
+}
+
+/**
+ * Load an image from a PNG file
+ *
+ * Reads a PNG image from disk using the given filename (and path)
+ * and returns as a Pixman image. Use pixman_image_unref() to free it.
+ *
+ * The returned image is always in PIXMAN_a8r8g8b8 format.
+ *
+ * @returns Pixman image, or NULL in case of error.
+ */
+pixman_image_t *
+load_image_from_png(const char *fname)
+{
+       pixman_image_t *image;
+       pixman_image_t *converted;
+       cairo_format_t cairo_fmt;
+       pixman_format_code_t pixman_fmt;
+       cairo_surface_t *reference_cairo_surface;
+       cairo_status_t status;
+       int width;
+       int height;
+       int stride;
+       void *data;
+
+       reference_cairo_surface = cairo_image_surface_create_from_png(fname);
+       cairo_surface_flush(reference_cairo_surface);
+       status = cairo_surface_status(reference_cairo_surface);
+       if (status != CAIRO_STATUS_SUCCESS) {
+               testlog("Could not open %s: %s\n", fname,
+                       cairo_status_to_string(status));
+               cairo_surface_destroy(reference_cairo_surface);
+               return NULL;
+       }
+
+       cairo_fmt = cairo_image_surface_get_format(reference_cairo_surface);
+       pixman_fmt = format_cairo2pixman(cairo_fmt);
+
+       width = cairo_image_surface_get_width(reference_cairo_surface);
+       height = cairo_image_surface_get_height(reference_cairo_surface);
+       stride = cairo_image_surface_get_stride(reference_cairo_surface);
+       data = cairo_image_surface_get_data(reference_cairo_surface);
+
+       /* The Cairo surface will own the data, so we keep it around. */
+       image = pixman_image_create_bits_no_clear(pixman_fmt,
+                                                 width, height, data, stride);
+       assert(image);
+
+       pixman_image_set_destroy_function(image, destroy_cairo_surface,
+                                         reference_cairo_surface);
+
+       converted = image_convert_to_a8r8g8b8(image);
+       pixman_image_unref(image);
+
+       return converted;
+}
+
+/**
+ * Take screenshot of a single output
+ *
+ * Requests a screenshot from the server of the output that the
+ * client appears on. This implies that the compositor goes through an output
+ * repaint to provide the screenshot before this function returns. This
+ * function is therefore both a server roundtrip and a wait for a repaint.
+ *
+ * @returns A new buffer object, that should be freed with buffer_destroy().
+ */
+struct buffer *
+capture_screenshot_of_output(struct client *client)
+{
+       struct buffer *buffer;
+
+       buffer = create_shm_buffer_a8r8g8b8(client,
+                                           client->output->width,
+                                           client->output->height);
+
+       client->test->buffer_copy_done = 0;
+       weston_test_capture_screenshot(client->test->weston_test,
+                                      client->output->wl_output,
+                                      buffer->proxy);
+       while (client->test->buffer_copy_done == 0)
+               if (wl_display_dispatch(client->wl_display) < 0)
+                       break;
+
+       /* FIXME: Document somewhere the orientation the screenshot is taken
+        * and how the clip coords are interpreted, in case of scaling/transform.
+        * If we're using read_pixels() just make sure it is documented somewhere.
+        * Protocol docs in the XML, comparison function docs in Doxygen style.
+        */
+
+       return buffer;
+}
+
+static void
+write_visual_diff(pixman_image_t *ref_image,
+                 struct buffer *shot,
+                 const struct rectangle *clip,
+                 const char *test_name,
+                 int seq_no,
+                 const struct range *fuzz)
+{
+       char *fname;
+       char *ext_test_name;
+       pixman_image_t *diff;
+       int ret;
+
+       ret = asprintf(&ext_test_name, "%s-diff", test_name);
+       assert(ret >= 0);
+
+       fname = screenshot_output_filename(ext_test_name, seq_no);
+       diff = visualize_image_difference(ref_image, shot->image, clip, fuzz);
+       write_image_as_png(diff, fname);
+
+       pixman_image_unref(diff);
+       free(fname);
+       free(ext_test_name);
+}
+
+/**
+ * Take a screenshot and verify its contents
+ *
+ * Takes a screenshot and writes the image into a PNG file named with
+ * get_test_name() and seq_no. Compares the contents to the given reference
+ * image over the given clip rectangle, reports whether they match to the
+ * test log, and if they do not match writes a visual diff into a PNG file.
+ *
+ * The compositor output size and the reference image size must both contain
+ * the clip rectangle.
+ *
+ * This function uses the pixel value allowed fuzz approriate for GL-renderer
+ * with 8 bits per channel data.
+ *
+ * \param client The client, for connecting to the compositor.
+ * \param ref_image The reference image file basename, without sequence number
+ * and .png suffix.
+ * \param ref_seq_no The reference image sequence number.
+ * \param clip The region of interest, or NULL for comparing the whole
+ * images.
+ * \param seq_no Test sequence number, for writing output files.
+ * \return True if the screen contents matches the reference image,
+ * false otherwise.
+ *
+ * For bootstrapping, ref_image can be NULL or the file can be missing.
+ * In that case the screenshot file is written but no comparison is performed,
+ * and false is returned.
+ */
+bool
+verify_screen_content(struct client *client,
+                     const char *ref_image,
+                     int ref_seq_no,
+                     const struct rectangle *clip,
+                     int seq_no)
+{
+       const char *test_name = get_test_name();
+       const struct range gl_fuzz = { -3, 4 };
+       struct buffer *shot;
+       pixman_image_t *ref = NULL;
+       char *ref_fname = NULL;
+       char *shot_fname;
+       bool match;
+
+       shot = capture_screenshot_of_output(client);
+       assert(shot);
+       shot_fname = screenshot_output_filename(test_name, seq_no);
+       write_image_as_png(shot->image, shot_fname);
+
+       if (ref_image) {
+               ref_fname = screenshot_reference_filename(ref_image, ref_seq_no);
+               ref = load_image_from_png(ref_fname);
+       }
+
+       if (ref) {
+               match = check_images_match(ref, shot->image, clip, &gl_fuzz);
+               testlog("Verify reference image %s vs. shot %s: %s\n",
+                       ref_fname, shot_fname, match ? "PASS" : "FAIL");
+
+               if (!match) {
+                       write_visual_diff(ref, shot, clip,
+                                         test_name, seq_no, &gl_fuzz);
+               }
+
+               pixman_image_unref(ref);
+       } else {
+               testlog("No reference image, shot %s: FAIL\n", shot_fname);
+               match = false;
+       }
+
+       free(ref_fname);
+       buffer_destroy(shot);
+       free(shot_fname);
+
+       return match;
+}
+
+/**
+ * Create a wl_buffer from a PNG file
+ *
+ * Loads the named PNG file from the directory of reference images,
+ * creates a wl_buffer with scale times the image dimensions in pixels,
+ * and copies the image content into the buffer using nearest-neighbor filter.
+ *
+ * \param client The client, for the Wayland connection.
+ * \param basename The PNG file name without .png suffix.
+ * \param scale Upscaling factor >= 1.
+ */
+struct buffer *
+client_buffer_from_image_file(struct client *client,
+                             const char *basename,
+                             int scale)
+{
+       struct buffer *buf;
+       char *fname;
+       pixman_image_t *img;
+       int buf_w, buf_h;
+       pixman_transform_t scaling;
+
+       assert(scale >= 1);
+
+       fname = image_filename(basename);
+       img = load_image_from_png(fname);
+       free(fname);
+       assert(img);
+
+       buf_w = scale * pixman_image_get_width(img);
+       buf_h = scale * pixman_image_get_height(img);
+       buf = create_shm_buffer_a8r8g8b8(client, buf_w, buf_h);
+
+       pixman_transform_init_scale(&scaling,
+                                   pixman_fixed_1 / scale,
+                                   pixman_fixed_1 / scale);
+       pixman_image_set_transform(img, &scaling);
+       pixman_image_set_filter(img, PIXMAN_FILTER_NEAREST, NULL, 0);
+
+       pixman_image_composite32(PIXMAN_OP_SRC,
+                                img, /* src */
+                                NULL, /* mask */
+                                buf->image, /* dst */
+                                0, 0, /* src x,y */
+                                0, 0, /* mask x,y */
+                                0, 0, /* dst x,y */
+                                buf_w, buf_h);
+       pixman_image_unref(img);
+
+       return buf;
+}
+
+/**
+ * Bind to a singleton global in wl_registry
+ *
+ * \param client Client whose registry and globals to use.
+ * \param iface The Wayland interface to look for.
+ * \param version The version to bind the interface with.
+ * \return A struct wl_proxy, which you need to cast to the proper type.
+ *
+ * Asserts that the global being searched for is a singleton and is found.
+ *
+ * Binds with the exact version given, does not take compositor interface
+ * version into account.
+ */
+void *
+bind_to_singleton_global(struct client *client,
+                        const struct wl_interface *iface,
+                        int version)
+{
+       struct global *tmp;
+       struct global *g = NULL;
+       struct wl_proxy *proxy;
+
+       wl_list_for_each(tmp, &client->global_list, link) {
+               if (strcmp(tmp->interface, iface->name))
+                       continue;
+
+               assert(!g && "multiple singleton objects");
+               g = tmp;
+       }
+
+       assert(g && "singleton not found");
+
+       proxy = wl_registry_bind(client->wl_registry, g->name, iface, version);
+       assert(proxy);
+
+       return proxy;
+}
+
+/**
+ * Create a wp_viewport for the client surface
+ *
+ * \param client The client->surface to use.
+ * \return A fresh viewport object.
+ */
+struct wp_viewport *
+client_create_viewport(struct client *client)
+{
+       struct wp_viewporter *viewporter;
+       struct wp_viewport *viewport;
+
+       viewporter = bind_to_singleton_global(client,
+                                             &wp_viewporter_interface, 1);
+       viewport = wp_viewporter_get_viewport(viewporter,
+                                             client->surface->wl_surface);
+       assert(viewport);
+       wp_viewporter_destroy(viewporter);
+
+       return viewport;
+}
+
+/**
+ * Fill the image with the given color
+ *
+ * \param image The image to write to.
+ * \param color The color to use.
+ */
+void
+fill_image_with_color(pixman_image_t *image, pixman_color_t *color)
+{
+       pixman_image_t *solid;
+       int width;
+       int height;
+
+       width = pixman_image_get_width(image);
+       height = pixman_image_get_height(image);
+
+       solid = pixman_image_create_solid_fill(color);
+       pixman_image_composite32(PIXMAN_OP_SRC,
+                                solid, /* src */
+                                NULL, /* mask */
+                                image, /* dst */
+                                0, 0, /* src x,y */
+                                0, 0, /* mask x,y */
+                                0, 0, /* dst x,y */
+                                width, height);
+       pixman_image_unref(solid);
+}
+
+/**
+ * Convert 8-bit RGB to opaque Pixman color
+ *
+ * \param tmp Pixman color struct to fill in.
+ * \param r Red value, 0 - 255.
+ * \param g Green value, 0 - 255.
+ * \param b Blue value, 0 - 255.
+ * \return tmp
+ */
+pixman_color_t *
+color_rgb888(pixman_color_t *tmp, uint8_t r, uint8_t g, uint8_t b)
+{
+       tmp->alpha = 65535;
+       tmp->red = (r << 8) + r;
+       tmp->green = (g << 8) + g;
+       tmp->blue = (b << 8) + b;
+
+       return tmp;
+}
diff --git a/tests/weston-test-client-helper.h b/tests/weston-test-client-helper.h
new file mode 100644 (file)
index 0000000..bda330e
--- /dev/null
@@ -0,0 +1,287 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef _WESTON_TEST_CLIENT_HELPER_H_
+#define _WESTON_TEST_CLIENT_HELPER_H_
+
+#include "config.h"
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <time.h>
+#include <pixman.h>
+
+#include <wayland-client-protocol.h>
+#include "weston-test-runner.h"
+#include "weston-test-client-protocol.h"
+#include "viewporter-client-protocol.h"
+
+struct client {
+       struct wl_display *wl_display;
+       struct wl_registry *wl_registry;
+       struct wl_compositor *wl_compositor;
+       struct wl_shm *wl_shm;
+       struct test *test;
+       /* the seat that is actually used for input events */
+       struct input *input;
+       /* server can have more wl_seats. We need keep them all until we
+        * find the one that we need. After that, the others
+        * will be destroyed, so this list will have the length of 1.
+        * If some day in the future we will need the other seats,
+        * we can just keep them here. */
+       struct wl_list inputs;
+       struct output *output;
+       struct surface *surface;
+       int has_argb;
+       struct wl_list global_list;
+       bool has_wl_drm;
+       struct wl_list output_list; /* struct output::link */
+};
+
+struct global {
+       uint32_t name;
+       char *interface;
+       uint32_t version;
+       struct wl_list link;
+};
+
+struct test {
+       struct weston_test *weston_test;
+       int pointer_x;
+       int pointer_y;
+       uint32_t n_egl_buffers;
+       int buffer_copy_done;
+};
+
+struct input {
+       struct client *client;
+       uint32_t global_name;
+       struct wl_seat *wl_seat;
+       struct pointer *pointer;
+       struct keyboard *keyboard;
+       struct touch *touch;
+       char *seat_name;
+       enum wl_seat_capability caps;
+       struct wl_list link;
+};
+
+struct pointer {
+       struct wl_pointer *wl_pointer;
+       struct surface *focus;
+       int x;
+       int y;
+       uint32_t button;
+       uint32_t state;
+       uint32_t axis;
+       double axis_value;
+       uint32_t motion_time_msec;
+       uint32_t button_time_msec;
+       uint32_t axis_time_msec;
+       uint32_t axis_stop_time_msec;
+       struct timespec input_timestamp;
+       struct timespec motion_time_timespec;
+       struct timespec button_time_timespec;
+       struct timespec axis_time_timespec;
+       struct timespec axis_stop_time_timespec;
+};
+
+struct keyboard {
+       struct wl_keyboard *wl_keyboard;
+       struct surface *focus;
+       uint32_t key;
+       uint32_t state;
+       uint32_t mods_depressed;
+       uint32_t mods_latched;
+       uint32_t mods_locked;
+       uint32_t group;
+       struct {
+               int rate;
+               int delay;
+       } repeat_info;
+       uint32_t key_time_msec;
+       struct timespec input_timestamp;
+       struct timespec key_time_timespec;
+};
+
+struct touch {
+       struct wl_touch *wl_touch;
+       int down_x;
+       int down_y;
+       int x;
+       int y;
+       int id;
+       int up_id; /* id of last wl_touch.up event */
+       int frame_no;
+       int cancel_no;
+       uint32_t down_time_msec;
+       uint32_t up_time_msec;
+       uint32_t motion_time_msec;
+       struct timespec input_timestamp;
+       struct timespec down_time_timespec;
+       struct timespec up_time_timespec;
+       struct timespec motion_time_timespec;
+};
+
+struct output {
+       struct wl_output *wl_output;
+       struct wl_list link; /* struct client::output_list */
+       int x;
+       int y;
+       int width;
+       int height;
+       int scale;
+       int initialized;
+};
+
+struct buffer {
+       struct wl_buffer *proxy;
+       size_t len;
+       pixman_image_t *image;
+};
+
+struct surface {
+       struct wl_surface *wl_surface;
+       struct output *output; /* not owned */
+       int x;
+       int y;
+       int width;
+       int height;
+       struct buffer *buffer;
+};
+
+struct rectangle {
+       int x;
+       int y;
+       int width;
+       int height;
+};
+
+struct range {
+       int a;
+       int b;
+};
+
+struct client *
+create_client(void);
+
+void
+client_destroy(struct client *client);
+
+struct surface *
+create_test_surface(struct client *client);
+
+void
+surface_destroy(struct surface *surface);
+
+struct client *
+create_client_and_test_surface(int x, int y, int width, int height);
+
+struct buffer *
+create_shm_buffer_a8r8g8b8(struct client *client, int width, int height);
+
+void
+buffer_destroy(struct buffer *buf);
+
+int
+surface_contains(struct surface *surface, int x, int y);
+
+void
+move_client(struct client *client, int x, int y);
+
+#define client_roundtrip(c) do { \
+       assert(wl_display_roundtrip((c)->wl_display) >= 0); \
+} while (0)
+
+struct wl_callback *
+frame_callback_set(struct wl_surface *surface, int *done);
+
+int
+frame_callback_wait_nofail(struct client *client, int *done);
+
+#define frame_callback_wait(c, d) assert(frame_callback_wait_nofail((c), (d)))
+
+void
+skip(const char *fmt, ...);
+
+void
+expect_protocol_error(struct client *client,
+                     const struct wl_interface *intf, uint32_t code);
+
+char *
+screenshot_output_filename(const char *basename, uint32_t seq);
+
+char *
+screenshot_reference_filename(const char *basename, uint32_t seq);
+
+char *
+image_filename(const char *basename);
+
+bool
+check_images_match(pixman_image_t *img_a, pixman_image_t *img_b,
+                  const struct rectangle *clip,
+                  const struct range *prec);
+
+pixman_image_t *
+visualize_image_difference(pixman_image_t *img_a, pixman_image_t *img_b,
+                          const struct rectangle *clip_rect,
+                          const struct range *prec);
+
+bool
+write_image_as_png(pixman_image_t *image, const char *fname);
+
+pixman_image_t *
+load_image_from_png(const char *fname);
+
+struct buffer *
+capture_screenshot_of_output(struct client *client);
+
+bool
+verify_screen_content(struct client *client,
+                     const char *ref_image,
+                     int ref_seq_no,
+                     const struct rectangle *clip,
+                     int seq_no);
+
+struct buffer *
+client_buffer_from_image_file(struct client *client,
+                             const char *basename,
+                             int scale);
+
+void *
+bind_to_singleton_global(struct client *client,
+                        const struct wl_interface *iface,
+                        int version);
+
+struct wp_viewport *
+client_create_viewport(struct client *client);
+
+void
+fill_image_with_color(pixman_image_t *image, pixman_color_t *color);
+
+pixman_color_t *
+color_rgb888(pixman_color_t *tmp, uint8_t r, uint8_t g, uint8_t b);
+
+#endif
diff --git a/tests/weston-test-fixture-compositor.c b/tests/weston-test-fixture-compositor.c
new file mode 100644 (file)
index 0000000..249f5dc
--- /dev/null
@@ -0,0 +1,427 @@
+/*
+ * Copyright 2019 Collabora, Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <string.h>
+#include <assert.h>
+#include <libudev.h>
+#include <unistd.h>
+#include <sys/file.h>
+#include <errno.h>
+
+#include "shared/helpers.h"
+#include "weston-test-fixture-compositor.h"
+#include <weston/weston.h>
+#include "test-config.h"
+
+struct prog_args {
+       int argc;
+       char **argv;
+       char **saved;
+       int alloc;
+};
+
+static void
+prog_args_init(struct prog_args *p)
+{
+       memset(p, 0, sizeof(*p));
+}
+
+static void
+prog_args_take(struct prog_args *p, char *arg)
+{
+       assert(arg);
+
+       if (p->argc == p->alloc) {
+               p->alloc += 10;
+               p->argv = realloc(p->argv, sizeof(char *) * p->alloc);
+               assert(p->argv);
+       }
+
+       p->argv[p->argc++] = arg;
+}
+
+/*
+ * The program to be executed will trample on argv, hence we need a copy to
+ * be able to free all our args.
+ */
+static void
+prog_args_save(struct prog_args *p)
+{
+       assert(p->saved == NULL);
+
+       p->saved = calloc(p->argc, sizeof(char *));
+       assert(p->saved);
+
+       memcpy(p->saved, p->argv, sizeof(char *) * p->argc);
+}
+
+static void
+prog_args_fini(struct prog_args *p)
+{
+       int i;
+
+       assert(p->saved);
+
+       for (i = 0; i < p->argc; i++)
+               free(p->saved[i]);
+       free(p->saved);
+       free(p->argv);
+       prog_args_init(p);
+}
+
+static char *
+get_lock_path(void)
+{
+       const char *env_path, *suffix;
+       char *lock_path;
+
+       suffix = "weston-test-suite-drm-lock";
+       env_path = getenv("XDG_RUNTIME_DIR");
+       if (!env_path) {
+               fprintf(stderr, "Failed to compute lock file path. " \
+                       "XDG_RUNTIME_DIR is not set.\n");
+               return NULL;
+       }
+
+       if (asprintf(&lock_path, "%s/%s", env_path, suffix) == -1)
+               return NULL;
+
+       return lock_path;
+}
+
+/*
+ * DRM-backend tests need to be run sequentially, since there can only be
+ * one user at a time with master status in a DRM KMS device. Since the
+ * test suite runs the tests in parallel, there's a mechanism to assure
+ * only one DRM-backend test is running at a time: tests of this type keep
+ * waiting until they acquire a lock (which is hold until they end).
+ *
+ * Returns -1 in failure and fd in success.
+ */
+static int
+wait_for_lock(void)
+{
+       char *lock_path;
+       int fd;
+
+       lock_path = get_lock_path();
+       if (!lock_path)
+               goto err_path;
+
+       fd = open(lock_path, O_RDWR | O_CLOEXEC | O_CREAT, 00700);
+       if (fd == -1) {
+               fprintf(stderr, "Could not open lock file %s: %s\n", lock_path, strerror(errno));
+               goto err_open;
+       }
+       fprintf(stderr, "Waiting for lock on %s...\n", lock_path);
+
+       /* The call is blocking, so we don't need a 'while'. Also, as we
+        * have a timeout for each test, this won't get stuck waiting. */
+       if (flock(fd, LOCK_EX) == -1) {
+               fprintf(stderr, "Could not lock %s: %s\n", lock_path, strerror(errno));
+               goto err_lock;
+       }
+       fprintf(stderr, "Lock %s acquired.\n", lock_path);
+       free(lock_path);
+
+       return fd;
+
+err_lock:
+       close(fd);
+err_open:
+       free(lock_path);
+err_path:
+       return -1;
+}
+
+/** Initialize part of compositor setup
+ *
+ * \param setup The variable to initialize.
+ * \param testset_name Value for testset_name member.
+ *
+ * \ingroup testharness_private
+ */
+void
+compositor_setup_defaults_(struct compositor_setup *setup,
+                          const char *testset_name)
+{
+       *setup = (struct compositor_setup) {
+               .backend = WESTON_BACKEND_HEADLESS,
+               .renderer = RENDERER_NOOP,
+               .shell = SHELL_DESKTOP,
+               .xwayland = false,
+               .width = 320,
+               .height = 240,
+               .scale = 1,
+               .transform = WL_OUTPUT_TRANSFORM_NORMAL,
+               .config_file = NULL,
+               .extra_module = NULL,
+               .logging_scopes = NULL,
+               .testset_name = testset_name,
+       };
+}
+
+static const char *
+backend_to_str(enum weston_compositor_backend b)
+{
+       static const char * const names[] = {
+               [WESTON_BACKEND_DRM] = "drm-backend.so",
+               [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
+               [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
+               [WESTON_BACKEND_RDP] = "rdp-backend.so",
+               [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
+               [WESTON_BACKEND_X11] = "X11-backend.so",
+       };
+       assert(b >= 0 && b < ARRAY_LENGTH(names));
+       return names[b];
+}
+
+static const char *
+renderer_to_arg(enum weston_compositor_backend b, enum renderer_type r)
+{
+       static const char * const headless_names[] = {
+               [RENDERER_NOOP] = NULL,
+               [RENDERER_PIXMAN] = "--use-pixman",
+               [RENDERER_GL] = "--use-gl",
+       };
+       static const char * const drm_names[] = {
+               [RENDERER_PIXMAN] = "--use-pixman",
+               [RENDERER_GL] = NULL,
+       };
+
+       switch (b) {
+       case WESTON_BACKEND_HEADLESS:
+               assert(r >= RENDERER_NOOP && r <= RENDERER_GL);
+               return headless_names[r];
+       case WESTON_BACKEND_DRM:
+               assert(r >= RENDERER_PIXMAN && r <= RENDERER_GL);
+               return drm_names[r];
+       default:
+               assert(0 && "renderer_to_str() does not know the backend");
+       }
+
+       return NULL;
+}
+
+static const char *
+shell_to_str(enum shell_type t)
+{
+       static const char * const names[] = {
+               [SHELL_TEST_DESKTOP] = "weston-test-desktop-shell.so",
+               [SHELL_DESKTOP] = "desktop-shell.so",
+               [SHELL_FULLSCREEN] = "fullscreen-shell.so",
+               [SHELL_IVI] = "ivi-shell.so",
+       };
+       assert(t >= 0 && t < ARRAY_LENGTH(names));
+       return names[t];
+}
+
+static const char *
+transform_to_str(enum wl_output_transform t)
+{
+       static const char * const names[] = {
+               [WL_OUTPUT_TRANSFORM_NORMAL] = "normal",
+               [WL_OUTPUT_TRANSFORM_90] = "rotate-90",
+               [WL_OUTPUT_TRANSFORM_180] = "rotate-180",
+               [WL_OUTPUT_TRANSFORM_270] = "rotate-270",
+               [WL_OUTPUT_TRANSFORM_FLIPPED] = "flipped",
+               [WL_OUTPUT_TRANSFORM_FLIPPED_90] = "flipped-rotate-90",
+               [WL_OUTPUT_TRANSFORM_FLIPPED_180] = "flipped-rotate-180",
+               [WL_OUTPUT_TRANSFORM_FLIPPED_270] = "flipped-rotate-270",
+       };
+
+       assert(t < ARRAY_LENGTH(names) && names[t]);
+       return names[t];
+}
+
+/** Execute compositor
+ *
+ * Manufactures the compositor command line and calls wet_main().
+ *
+ * Returns RESULT_SKIP if the given setup contains features that were disabled
+ * in the build, e.g. GL-renderer or DRM-backend.
+ *
+ * \ingroup testharness_private
+ */
+int
+execute_compositor(const struct compositor_setup *setup,
+                  struct wet_testsuite_data *data)
+{
+       struct prog_args args;
+       char *tmp;
+       const char *ctmp, *drm_device;
+       int ret, lock_fd = -1;
+
+       if (setenv("WESTON_MODULE_MAP", WESTON_MODULE_MAP, 0) < 0 ||
+           setenv("WESTON_DATA_DIR", WESTON_DATA_DIR, 0) < 0) {
+               fprintf(stderr, "Error: environment setup failed.\n");
+               return RESULT_HARD_ERROR;
+       }
+
+#ifndef BUILD_DRM_COMPOSITOR
+       if (setup->backend == WESTON_BACKEND_DRM) {
+               fprintf(stderr, "DRM-backend required but not built, skipping.\n");
+               return RESULT_SKIP;
+       }
+#endif
+
+#ifndef BUILD_FBDEV_COMPOSITOR
+       if (setup->backend == WESTON_BACKEND_FBDEV) {
+               fprintf(stderr, "fbdev-backend required but not built, skipping.\n");
+               return RESULT_SKIP;
+       }
+#endif
+
+#ifndef BUILD_RDP_COMPOSITOR
+       if (setup->backend == WESTON_BACKEND_RDP) {
+               fprintf(stderr, "RDP-backend required but not built, skipping.\n");
+               return RESULT_SKIP;
+       }
+#endif
+
+#ifndef BUILD_WAYLAND_COMPOSITOR
+       if (setup->backend == WESTON_BACKEND_WAYLAND) {
+               fprintf(stderr, "wayland-backend required but not built, skipping.\n");
+               return RESULT_SKIP;
+       }
+#endif
+
+#ifndef BUILD_X11_COMPOSITOR
+       if (setup->backend == WESTON_BACKEND_X11) {
+               fprintf(stderr, "X11-backend required but not built, skipping.\n");
+               return RESULT_SKIP;
+       }
+#endif
+
+#ifndef ENABLE_EGL
+       if (setup->renderer == RENDERER_GL) {
+               fprintf(stderr, "GL-renderer required but not built, skipping.\n");
+               return RESULT_SKIP;
+       }
+#endif
+
+#if !TEST_GL_RENDERER
+       if (setup->renderer == RENDERER_GL) {
+               fprintf(stderr, "GL-renderer disabled for tests, skipping.\n");
+               return RESULT_SKIP;
+       }
+#endif
+
+       prog_args_init(&args);
+
+       /* argv[0] */
+       asprintf(&tmp, "agl-%s", setup->testset_name);
+       prog_args_take(&args, tmp);
+
+       asprintf(&tmp, "--backend=%s", backend_to_str(setup->backend));
+       prog_args_take(&args, tmp);
+
+       if (setup->backend == WESTON_BACKEND_DRM) {
+
+               drm_device = getenv("WESTON_TEST_SUITE_DRM_DEVICE");
+               if (!drm_device) {
+                       fprintf(stderr, "Skipping DRM-backend tests because " \
+                               "WESTON_TEST_SUITE_DRM_DEVICE is not set. " \
+                               "See test suite documentation to learn how " \
+                               "to run them.\n");
+                       return RESULT_SKIP;
+               }
+               asprintf(&tmp, "--drm-device=%s", drm_device);
+               prog_args_take(&args, tmp);
+
+               prog_args_take(&args, strdup("--seat=weston-test-seat"));
+
+               prog_args_take(&args, strdup("--continue-without-input"));
+
+               lock_fd = wait_for_lock();
+               if (lock_fd == -1)
+                       return RESULT_FAIL;
+       }
+
+       asprintf(&tmp, "--socket=%s", setup->testset_name);
+       prog_args_take(&args, tmp);
+
+       asprintf(&tmp, "--modules=%s%s%s", TESTSUITE_PLUGIN_PATH,
+                setup->extra_module ? "," : "",
+                setup->extra_module ? setup->extra_module : "");
+       prog_args_take(&args, tmp);
+
+       if (setup->backend != WESTON_BACKEND_DRM &&
+           setup->backend != WESTON_BACKEND_FBDEV) {
+               asprintf(&tmp, "--width=%d", setup->width);
+               prog_args_take(&args, tmp);
+
+               asprintf(&tmp, "--height=%d", setup->height);
+               prog_args_take(&args, tmp);
+       }
+
+       if (setup->scale != 1) {
+               asprintf(&tmp, "--scale=%d", setup->scale);
+               prog_args_take(&args, tmp);
+       }
+
+       if (setup->transform != WL_OUTPUT_TRANSFORM_NORMAL) {
+               asprintf(&tmp, "--transform=%s",
+                        transform_to_str(setup->transform));
+               prog_args_take(&args, tmp);
+       }
+
+       if (setup->config_file) {
+               asprintf(&tmp, "--config=%s", setup->config_file);
+               prog_args_take(&args, tmp);
+       } else {
+               prog_args_take(&args, strdup("--no-config"));
+       }
+
+       ctmp = renderer_to_arg(setup->backend, setup->renderer);
+       if (ctmp)
+               prog_args_take(&args, strdup(ctmp));
+
+       asprintf(&tmp, "--shell=%s", shell_to_str(setup->shell));
+       prog_args_take(&args, tmp);
+
+       if (setup->logging_scopes) {
+               asprintf(&tmp, "--logger-scopes=%s", setup->logging_scopes);
+               prog_args_take(&args, tmp);
+       }
+
+       if (setup->xwayland)
+               prog_args_take(&args, strdup("--xwayland"));
+
+       wet_testsuite_data_set(data);
+       prog_args_save(&args);
+       ret = wet_main(args.argc, args.argv);
+
+       prog_args_fini(&args);
+
+       /* We acquired a lock (if this is a DRM-backend test) and now we can
+        * close its fd and release it, as it has already been run. */
+       if (lock_fd != -1)
+               close(lock_fd);
+
+       return ret;
+}
diff --git a/tests/weston-test-fixture-compositor.h b/tests/weston-test-fixture-compositor.h
new file mode 100644 (file)
index 0000000..fd8d0e5
--- /dev/null
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2019 Collabora, Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef WESTON_TEST_FIXTURE_COMPOSITOR_H
+#define WESTON_TEST_FIXTURE_COMPOSITOR_H
+
+#include <wayland-client-protocol.h>
+#include <libweston/libweston.h>
+
+#include "weston-testsuite-data.h"
+
+/** Weston renderer type
+ *
+ * \sa compositor_setup
+ * \ingroup testharness
+ */
+enum renderer_type {
+       /** Dummy renderer that does nothing. */
+       RENDERER_NOOP = 0,
+       /** Pixman-renderer */
+       RENDERER_PIXMAN,
+       /** GL-renderer */
+       RENDERER_GL
+};
+
+/** Weston shell plugin
+ *
+ * \sa compositor_setup
+ * \ingroup testharness
+ */
+enum shell_type {
+       /** Desktop test-shell with predictable window placement and
+        * no helper clients */
+       SHELL_TEST_DESKTOP = 0,
+       /** The full desktop shell. */
+       SHELL_DESKTOP,
+       /** The ivi-shell. */
+       SHELL_IVI,
+       /** The fullscreen-shell. */
+       SHELL_FULLSCREEN
+};
+
+/** Weston compositor configuration
+ *
+ * This structure determines the Weston compositor command line arguments.
+ * You should always use compositor_setup_defaults() to initialize this, then
+ * override any members you need with assignments.
+ *
+ * \ingroup testharness
+ */
+struct compositor_setup {
+       /** The backend to use. */
+       enum weston_compositor_backend backend;
+       /** The renderer to use. */
+       enum renderer_type renderer;
+       /** The shell plugin to use. */
+       enum shell_type shell;
+       /** Whether to enable xwayland support. */
+       bool xwayland;
+       /** Default output width. */
+       unsigned width;
+       /** Default output height. */
+       unsigned height;
+       /** Default output scale. */
+       int scale;
+       /** Default output transform, one of WL_OUTPUT_TRANSFORM_*. */
+       enum wl_output_transform transform;
+       /** The absolute path to \c weston.ini to use,
+        * or NULL for \c --no-config . */
+       const char *config_file;
+       /** Full path to an extra plugin to load, or NULL for none. */
+       const char *extra_module;
+       /** Debug scopes for the compositor log,
+        * or NULL for compositor defaults. */
+       const char *logging_scopes;
+       /** The name of this test program, used as a unique identifier. */
+       const char *testset_name;
+};
+
+void
+compositor_setup_defaults_(struct compositor_setup *setup,
+                          const char *testset_name);
+
+/** Initialize compositor setup to defaults
+ *
+ * \param s The variable to initialize.
+ *
+ * The defaults are:
+ * - backend: headless
+ * - renderer: noop
+ * - shell: desktop shell
+ * - xwayland: no
+ * - width: 320
+ * - height: 240
+ * - scale: 1
+ * - transform: WL_OUTPUT_TRANSFORM_NORMAL
+ * - config_file: none
+ * - extra_module: none
+ * - logging_scopes: compositor defaults
+ * - testset_name: the test name from meson.build
+ *
+ * \ingroup testharness
+ */
+#define compositor_setup_defaults(s) do {\
+       compositor_setup_defaults_(s, THIS_TEST_NAME); \
+} while (0)
+
+int
+execute_compositor(const struct compositor_setup *setup,
+                  struct wet_testsuite_data *data);
+
+#endif /* WESTON_TEST_FIXTURE_COMPOSITOR_H */
diff --git a/tests/weston-test-runner.c b/tests/weston-test-runner.c
new file mode 100644 (file)
index 0000000..8e92170
--- /dev/null
@@ -0,0 +1,620 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+#include <signal.h>
+#include <getopt.h>
+
+#include "weston-test-runner.h"
+#include "weston-testsuite-data.h"
+#include "shared/string-helpers.h"
+
+/**
+ * \defgroup testharness Test harness
+ * \defgroup testharness_private Test harness private
+ */
+
+extern const struct weston_test_entry __start_test_section, __stop_test_section;
+
+struct weston_test_run_info {
+       char name[512];
+       int fixture_nr;
+};
+
+static const struct weston_test_run_info *test_run_info_;
+
+/** Get the test name string with counter
+ *
+ * \return The test name with fixture number \c -f%%d added. For an array
+ * driven test, e.g. defined with TEST_P(), the name has also a \c -e%%d
+ * suffix to indicate the array element number.
+ *
+ * This is only usable from code paths inside TEST(), TEST_P(), PLUGIN_TEST()
+ * etc. defined functions.
+ *
+ * \ingroup testharness
+ */
+const char *
+get_test_name(void)
+{
+       return test_run_info_->name;
+}
+
+/** Get the current fixture index
+ *
+ * Returns the current fixture index which can be used directly as an index
+ * into the array passed as an argument to DECLARE_FIXTURE_SETUP_WITH_ARG().
+ *
+ * This is only usable from code paths inside TEST(), TEST_P(), PLUGIN_TEST()
+ * etc. defined functions.
+ *
+ * \ingroup testharness
+ */
+int
+get_test_fixture_index(void)
+{
+       return test_run_info_->fixture_nr - 1;
+}
+
+/** Print into test log
+ *
+ * This is exactly like printf() except the output goes to the test log,
+ * which is at stderr.
+ *
+ * \param fmt printf format string
+ *
+ * \ingroup testharness
+ */
+void
+testlog(const char *fmt, ...)
+{
+       va_list argp;
+
+       va_start(argp, fmt);
+       vfprintf(stderr, fmt, argp);
+       va_end(argp);
+}
+
+static const struct weston_test_entry *
+find_test(const char *name)
+{
+       const struct weston_test_entry *t;
+
+       for (t = &__start_test_section; t < &__stop_test_section; t++)
+               if (strcmp(t->name, name) == 0)
+                       return t;
+
+       return NULL;
+}
+
+static enum test_result_code
+run_test(int fixture_nr, const struct weston_test_entry *t, void *data,
+        int iteration)
+{
+       struct weston_test_run_info info;
+
+       if (data) {
+               snprintf(info.name, sizeof(info.name), "%s-f%02d-e%02d",
+                        t->name, fixture_nr, iteration);
+       } else {
+               snprintf(info.name, sizeof(info.name), "%s-f%02d",
+                        t->name, fixture_nr);
+       }
+       info.fixture_nr = fixture_nr;
+
+       test_run_info_ = &info;
+       t->run(data);
+       test_run_info_ = NULL;
+
+       /*
+        * XXX: We should return t->run(data); but that requires changing
+        * the function signature and stop using assert() in tests.
+        * https://gitlab.freedesktop.org/wayland/weston/issues/311
+        */
+       return RESULT_OK;
+}
+
+static void
+list_tests(void)
+{
+       const struct fixture_setup_array *fsa;
+       const struct weston_test_entry *t;
+
+       fsa = fixture_setup_array_get_();
+
+       printf("Fixture setups: %d\n", fsa->n_elements);
+
+       for (t = &__start_test_section; t < &__stop_test_section; t++) {
+               printf("  %s\n", t->name);
+               if (t->n_elements > 1)
+                       printf("    with array of %d cases\n", t->n_elements);
+       }
+}
+
+struct weston_test_harness {
+       int32_t fixt_ind;
+       char *chosen_testname;
+       int32_t case_ind;
+
+       struct wet_testsuite_data data;
+};
+
+typedef void (*weston_test_cb)(struct wet_testsuite_data *suite_data,
+                              const struct weston_test_entry *t,
+                              const void *test_data,
+                              int iteration);
+
+static void
+for_each_test_case(struct wet_testsuite_data *data, weston_test_cb cb)
+{
+       unsigned i;
+
+       for (i = 0; i < data->tests_count; i++) {
+               const struct weston_test_entry *t = &data->tests[i];
+               const void *current_test_data = t->table_data;
+               int elem;
+               int elem_end;
+
+               if (data->case_index == -1) {
+                       elem = 0;
+                       elem_end = t->n_elements;
+               } else {
+                       elem = data->case_index;
+                       elem_end = elem + 1;
+               }
+
+               for (; elem < elem_end; elem++) {
+                       current_test_data = (char *)t->table_data +
+                                           elem * t->element_size;
+                       cb(data, t, current_test_data, elem);
+                }
+       }
+}
+
+static const char *
+result_to_str(enum test_result_code ret)
+{
+       static const char *names[] = {
+               [RESULT_FAIL] = "fail",
+               [RESULT_HARD_ERROR] = "hard error",
+               [RESULT_OK] = "ok",
+               [RESULT_SKIP] = "skip",
+       };
+
+       assert(ret >= 0 && ret < ARRAY_LENGTH(names));
+       return names[ret];
+}
+
+static void
+run_case(struct wet_testsuite_data *suite_data,
+        const struct weston_test_entry *t,
+        const void *test_data,
+        int iteration)
+{
+       enum test_result_code ret;
+       const char *fail = "";
+       const char *skip = "";
+       int fixture_nr = suite_data->fixture_iteration + 1;
+       int iteration_nr = iteration + 1;
+
+       testlog("*** Run fixture %d, %s/%d\n",
+               fixture_nr, t->name, iteration_nr);
+
+       if (suite_data->type == TEST_TYPE_PLUGIN) {
+               ret = run_test(fixture_nr, t, suite_data->compositor,
+                              iteration);
+       } else {
+               ret = run_test(fixture_nr, t, (void *)test_data, iteration);
+       }
+
+       switch (ret) {
+       case RESULT_OK:
+               suite_data->passed++;
+               break;
+       case RESULT_FAIL:
+       case RESULT_HARD_ERROR:
+               suite_data->failed++;
+               fail = "not ";
+               break;
+       case RESULT_SKIP:
+               suite_data->skipped++;
+               skip = " # SKIP";
+               break;
+       }
+
+       testlog("*** Result fixture %d, %s/%d: %s\n",
+               fixture_nr, t->name, iteration_nr, result_to_str(ret));
+
+       suite_data->counter++;
+       printf("%sok %d fixture %d %s/%d%s\n", fail, suite_data->counter,
+              fixture_nr, t->name, iteration_nr, skip);
+}
+
+/* This function might run in a new thread */
+static void
+testsuite_run(struct wet_testsuite_data *data)
+{
+       for_each_test_case(data, run_case);
+}
+
+static void
+count_case(struct wet_testsuite_data *suite_data,
+          const struct weston_test_entry *t,
+          const void *test_data,
+          int iteration)
+{
+       suite_data->total++;
+}
+
+static void
+tap_plan(struct wet_testsuite_data *data, int count_fixtures)
+{
+       data->total = 0;
+       for_each_test_case(data, count_case);
+
+       printf("1..%d\n", data->total * count_fixtures);
+}
+
+static void
+skip_case(struct wet_testsuite_data *suite_data,
+         const struct weston_test_entry *t,
+         const void *test_data,
+         int iteration)
+{
+       int fixture_nr = suite_data->fixture_iteration + 1;
+       int iteration_nr = iteration + 1;
+
+       suite_data->counter++;
+       printf("ok %d fixture %d %s/%d # SKIP fixture\n", suite_data->counter,
+              fixture_nr, t->name, iteration_nr);
+}
+
+static void
+tap_skip_fixture(struct wet_testsuite_data *data)
+{
+       for_each_test_case(data, skip_case);
+}
+
+static void
+help(const char *exe)
+{
+       printf(
+               "Usage: %s [options] [testname [index]]\n"
+               "\n"
+               "This is a Weston test suite executable that runs some tests.\n"
+               "Options:\n"
+               "  -f, --fixture N  Run only fixture index N. Indices start from 1.\n"
+               "  -h, --help       Print this help and exit with success.\n"
+               "  -l, --list       List all tests in this executable and exit with success.\n"
+               "testname:          Optional; name of the test to execute instead of all tests.\n"
+               "index:             Optional; for a multi-case test, run the given case only.\n",
+               exe);
+}
+
+static void
+parse_command_line(struct weston_test_harness *harness, int argc, char **argv)
+{
+       int c;
+       static const struct option opts[] = {
+               { "fixture", required_argument, NULL,      'f' },
+               { "help",    no_argument,       NULL,      'h' },
+               { "list",    no_argument,       NULL,      'l' },
+               { 0,         0,                 NULL,      0  }
+       };
+
+       while ((c = getopt_long(argc, argv, "f:hl", opts, NULL)) != -1) {
+               switch (c) {
+               case 'f':
+                       if (!safe_strtoint(optarg, &harness->fixt_ind)) {
+                               fprintf(stderr,
+                                       "Error: '%s' does not look like a number (command line).\n",
+                                       optarg);
+                               exit(RESULT_HARD_ERROR);
+                       }
+                       harness->fixt_ind--; /* convert base-1 to base 0 */
+                       break;
+               case 'h':
+                       help(argv[0]);
+                       exit(RESULT_OK);
+               case 'l':
+                       list_tests();
+                       exit(RESULT_OK);
+               case 0:
+                       break;
+               default:
+                       exit(RESULT_HARD_ERROR);
+               }
+       }
+
+       if (optind < argc)
+               harness->chosen_testname = argv[optind++];
+
+       if (optind < argc) {
+               if (!safe_strtoint(argv[optind], &harness->case_ind)) {
+                       fprintf(stderr,
+                               "Error: '%s' does not look like a number (command line).\n",
+                               argv[optind]);
+                       exit(RESULT_HARD_ERROR);
+               }
+               harness->case_ind--; /* convert base-1 to base 0 */
+               optind++;
+       }
+
+       if (optind < argc) {
+               fprintf(stderr, "Unexpected extra arguments given (command line).\n\n");
+               help(argv[0]);
+               exit(RESULT_HARD_ERROR);
+       }
+}
+
+static struct weston_test_harness *
+weston_test_harness_create(int argc, char **argv)
+{
+       const struct fixture_setup_array *fsa;
+       struct weston_test_harness *harness;
+
+       harness = zalloc(sizeof(*harness));
+       assert(harness);
+
+       harness->fixt_ind = -1;
+       harness->case_ind = -1;
+       parse_command_line(harness, argc, argv);
+
+       fsa = fixture_setup_array_get_();
+       if (harness->fixt_ind < -1 || harness->fixt_ind >= fsa->n_elements) {
+               fprintf(stderr,
+                       "Error: fixture index %d (command line) is invalid for this program.\n",
+                       harness->fixt_ind + 1);
+               exit(RESULT_HARD_ERROR);
+       }
+
+       if (harness->chosen_testname) {
+               const struct weston_test_entry *t;
+
+               t = find_test(harness->chosen_testname);
+               if (!t) {
+                       fprintf(stderr,
+                               "Error: test '%s' not found (command line).\n",
+                               harness->chosen_testname);
+                       exit(RESULT_HARD_ERROR);
+               }
+
+               if (harness->case_ind < -1 ||
+                   harness->case_ind >= t->n_elements) {
+                       fprintf(stderr,
+                               "Error: case index %d (command line) is invalid for this test.\n",
+                               harness->case_ind + 1);
+                       exit(RESULT_HARD_ERROR);
+               }
+
+               harness->data.tests = t;
+               harness->data.tests_count = 1;
+               harness->data.case_index = harness->case_ind;
+       } else {
+               harness->data.tests = &__start_test_section;
+               harness->data.tests_count =
+                       &__stop_test_section - &__start_test_section;
+               harness->data.case_index = -1;
+       }
+
+       harness->data.run = testsuite_run;
+
+       return harness;
+}
+
+static void
+weston_test_harness_destroy(struct weston_test_harness *harness)
+{
+       free(harness);
+}
+
+static enum test_result_code
+counts_to_result(const struct wet_testsuite_data *data)
+{
+       /* RESULT_SKIP is reserved for fixture setup itself skipping everything */
+       if (data->total == data->passed + data->skipped)
+               return RESULT_OK;
+       return RESULT_FAIL;
+}
+
+/** Execute all tests as client tests
+ *
+ * \param harness The test harness context.
+ * \param setup The compositor configuration.
+ *
+ * Initializes the compositor with the given setup and executes the compositor.
+ * The compositor creates a new thread where all tests in the test program are
+ * serially executed. Once the thread finishes, the compositor returns from its
+ * event loop and cleans up.
+ *
+ * Returns RESULT_SKIP if the requested compositor features, e.g. GL-renderer,
+ * are not built.
+ *
+ * \sa DECLARE_FIXTURE_SETUP(), DECLARE_FIXTURE_SETUP_WITH_ARG()
+ * \ingroup testharness
+ */
+enum test_result_code
+weston_test_harness_execute_as_client(struct weston_test_harness *harness,
+                                     const struct compositor_setup *setup)
+{
+       struct wet_testsuite_data *data = &harness->data;
+
+       data->type = TEST_TYPE_CLIENT;
+       return execute_compositor(setup, data);
+}
+
+/** Execute all tests as plugin tests
+ *
+ * \param harness The test harness context.
+ * \param setup The compositor configuration.
+ *
+ * Initializes the compositor with the given setup and executes the compositor.
+ * The compositor executes all tests in the test program serially from an idle
+ * handler, then returns from its event loop and cleans up.
+ *
+ * Returns RESULT_SKIP if the requested compositor features, e.g. GL-renderer,
+ * are not built.
+ *
+ * \sa DECLARE_FIXTURE_SETUP(), DECLARE_FIXTURE_SETUP_WITH_ARG()
+ * \ingroup testharness
+ */
+enum test_result_code
+weston_test_harness_execute_as_plugin(struct weston_test_harness *harness,
+                                     const struct compositor_setup *setup)
+{
+       struct wet_testsuite_data *data = &harness->data;
+
+       data->type = TEST_TYPE_PLUGIN;
+       return execute_compositor(setup, data);
+}
+
+/** Execute all tests as standalone tests
+ *
+ * \param harness The test harness context.
+ *
+ * Executes all tests in the test program serially without any further setup,
+ * particularly without any compositor instance created.
+ *
+ * \sa DECLARE_FIXTURE_SETUP(), DECLARE_FIXTURE_SETUP_WITH_ARG()
+ * \ingroup testharness
+ */
+enum test_result_code
+weston_test_harness_execute_standalone(struct weston_test_harness *harness)
+{
+       struct wet_testsuite_data *data = &harness->data;
+
+       data->type = TEST_TYPE_STANDALONE;
+       data->run(data);
+
+       return RESULT_OK;
+}
+
+/** Fixture data array getter method
+ *
+ * DECLARE_FIXTURE_SETUP_WITH_ARG() overrides this in test programs.
+ * The default implementation has no data and makes the tests run once.
+ *
+ * \ingroup testharness
+ */
+__attribute__((weak)) const struct fixture_setup_array *
+fixture_setup_array_get_(void)
+{
+       /* A dummy fixture without a data array. */
+       static const struct fixture_setup_array default_fsa = {
+               .array = NULL,
+               .element_size = 0,
+               .n_elements = 1,
+       };
+
+       return &default_fsa;
+}
+
+/** Fixture setup function
+ *
+ * DECLARE_FIXTURE_SETUP() and DECLARE_FIXTURE_SETUP_WITH_ARG() override
+ * this in test programs.
+ * The default implementation just calls
+ * weston_test_harness_execute_standalone().
+ *
+ * \ingroup testharness
+ */
+__attribute__((weak)) enum test_result_code
+fixture_setup_run_(struct weston_test_harness *harness, const void *arg_)
+{
+       return weston_test_harness_execute_standalone(harness);
+}
+
+static void
+fixture_report(const struct wet_testsuite_data *d, enum test_result_code ret)
+{
+       int fixture_nr = d->fixture_iteration + 1;
+
+       testlog("--- Fixture %d %s: passed %d, skipped %d, failed %d, total %d\n",
+               fixture_nr, result_to_str(ret),
+               d->passed, d->skipped, d->failed, d->total);
+}
+
+int
+main(int argc, char *argv[])
+{
+       struct weston_test_harness *harness;
+       enum test_result_code ret;
+       enum test_result_code result = RESULT_OK;
+       const struct fixture_setup_array *fsa;
+       const char *array_data;
+       int fi;
+       int fi_end;
+
+       harness = weston_test_harness_create(argc, argv);
+
+       fsa = fixture_setup_array_get_();
+       array_data = fsa->array;
+
+       if (harness->fixt_ind == -1) {
+               fi = 0;
+               fi_end = fsa->n_elements;
+       } else {
+               fi = harness->fixt_ind;
+               fi_end = fi + 1;
+       }
+
+       tap_plan(&harness->data, fi_end - fi);
+       testlog("Iterating through %d fixtures.\n", fi_end - fi);
+
+       for (; fi < fi_end; fi++) {
+               const void *arg = array_data + fi * fsa->element_size;
+
+               testlog("--- Fixture %d...\n", fi + 1);
+               harness->data.fixture_iteration = fi;
+               harness->data.passed = 0;
+               harness->data.skipped = 0;
+               harness->data.failed = 0;
+
+               ret = fixture_setup_run_(harness, arg);
+               fixture_report(&harness->data, ret);
+
+               if (ret == RESULT_SKIP) {
+                       tap_skip_fixture(&harness->data);
+                       continue;
+               }
+
+               if (ret != RESULT_OK && result != RESULT_HARD_ERROR)
+                       result = ret;
+               else if (counts_to_result(&harness->data) != RESULT_OK)
+                       result = RESULT_FAIL;
+       }
+
+       weston_test_harness_destroy(harness);
+
+       return result;
+}
diff --git a/tests/weston-test-runner.h b/tests/weston-test-runner.h
new file mode 100644 (file)
index 0000000..9e545cd
--- /dev/null
@@ -0,0 +1,259 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ * Copyright © 2013 Sam Spilsbury <smspillaz@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef _WESTON_TEST_RUNNER_H_
+#define _WESTON_TEST_RUNNER_H_
+
+#include "config.h"
+
+#include <stdlib.h>
+
+#include <wayland-util.h>
+#include "shared/helpers.h"
+#include "weston-test-fixture-compositor.h"
+#include "weston-testsuite-data.h"
+
+#ifdef NDEBUG
+#error "Tests must not be built with NDEBUG defined, they rely on assert()."
+#endif
+
+/** Test program entry
+ *
+ * Each invocation of TEST(), TEST_P(), or PLUGIN_TEST() will create one
+ * more weston_test_entry in a custom named section in the final binary.
+ * Iterating through the section then allows to iterate through all
+ * the defined tests.
+ *
+ * \ingroup testharness_private
+ */
+struct weston_test_entry {
+       const char *name;
+       void (*run)(void *);
+       const void *table_data;
+       size_t element_size;
+       int n_elements;
+} __attribute__ ((aligned (32)));
+
+#define TEST_BEGIN(name, arg)                                          \
+       static void name(arg)
+
+#define TEST_COMMON(func, name, data, size, n_elem)                    \
+       static void func(void *);                                       \
+                                                                       \
+       const struct weston_test_entry test##name                       \
+               __attribute__ ((used, section ("test_section"))) =      \
+       {                                                               \
+               #name, func, data, size, n_elem                         \
+       };
+
+#define NO_ARG_TEST(name)                                              \
+       TEST_COMMON(wrap##name, name, NULL, 0, 1)                       \
+       static void name(void);                                         \
+       static void wrap##name(void *data)                              \
+       {                                                               \
+               (void) data;                                            \
+               name();                                                 \
+       }                                                               \
+                                                                       \
+       TEST_BEGIN(name, void)
+
+#define ARG_TEST(name, test_data)                                      \
+       TEST_COMMON(name, name, test_data,                              \
+                   sizeof(test_data[0]),                               \
+                   ARRAY_LENGTH(test_data))                            \
+       TEST_BEGIN(name, void *data)                                    \
+
+/** Add a test with no parameters
+ *
+ * This defines one test as a new function. Use this macro in place of the
+ * function signature and put the function body after this.
+ *
+ * \param name Name for the test, must be a valid function name.
+ *
+ * \ingroup testharness
+ */
+#define TEST(name) NO_ARG_TEST(name)
+
+/** Add an array driven test with a parameter
+ *
+ * This defines an array of tests as a new function. Use this macro in place
+ * of the function signature and put the function body after this. The function
+ * will be executed once for each element in \c data_array, passing the
+ * element as the argument <tt>void *data</tt> to the function.
+ *
+ * This macro is not usable if fixture setup is using
+ * weston_test_harness_execute_as_plugin().
+ *
+ * \param name Name for the test, must be a valid function name.
+ * \param data_array A static const array of any type. The length will be
+ * recorded automatically.
+ *
+ * \ingroup testharness
+ */
+#define TEST_P(name, data_array) ARG_TEST(name, data_array)
+
+/** Add a test with weston_compositor argument
+ *
+ * This defines one test as a new function. Use this macro in place of the
+ * function signature and put the function body after this. The function
+ * will have one argument <tt>struct weston_compositor *compositor</tt>.
+ *
+ * This macro is only usable if fixture setup is using
+ * weston_test_harness_execute_as_plugin().
+ *
+ * \param name Name for the test, must be a valid function name.
+ *
+ * \ingroup testharness
+ */
+#define PLUGIN_TEST(name)                                              \
+       TEST_COMMON(wrap##name, name, NULL, 0, 1)                       \
+       static void name(struct weston_compositor *);                   \
+       static void wrap##name(void *compositor)                        \
+       {                                                               \
+               name(compositor);                                       \
+       }                                                               \
+       TEST_BEGIN(name, struct weston_compositor *compositor)
+
+void
+testlog(const char *fmt, ...) WL_PRINTF(1, 2);
+
+const char *
+get_test_name(void);
+
+int
+get_test_fixture_index(void);
+
+/** Fixture setup array record
+ *
+ * Helper to store the attributes of the data array passed in to
+ * DECLARE_FIXTURE_SETUP_WITH_ARG().
+ *
+ * \ingroup testharness_private
+ */
+struct fixture_setup_array {
+       const void *array;
+       size_t element_size;
+       int n_elements;
+};
+
+const struct fixture_setup_array *
+fixture_setup_array_get_(void);
+
+/** Test harness context
+ *
+ * \ingroup testharness
+ */
+struct weston_test_harness;
+
+enum test_result_code
+fixture_setup_run_(struct weston_test_harness *harness, const void *arg_);
+
+/** Register a fixture setup function
+ *
+ * This registers the given (preferably static) function to be used for setting
+ * up any fixtures you might need. The function must have the signature:
+ *
+ * \code
+ * enum test_result_code func_(struct weston_test_harness *harness)
+ * \endcode
+ *
+ * The function must call one of weston_test_harness_execute_standalone(),
+ * weston_test_harness_execute_as_plugin() or
+ * weston_test_harness_execute_as_client() passing in the \c harness argument,
+ * and return the return value from that call. The function can also return a
+ * test_result_code on its own if it does not want to run the tests,
+ * e.g. RESULT_SKIP or RESULT_HARD_ERROR.
+ *
+ * The function will be called once to run all tests.
+ *
+ * \param func_ The function to be used as fixture setup.
+ *
+ * \ingroup testharness
+ */
+#define DECLARE_FIXTURE_SETUP(func_)                                   \
+       enum test_result_code                                           \
+       fixture_setup_run_(struct weston_test_harness *harness,         \
+                          const void *arg_)                            \
+       {                                                               \
+               return func_(harness);                                  \
+       }
+
+/** Register a fixture setup function with a data array
+ *
+ * This registers the given (preferably static) function to be used for setting
+ * up any fixtures you might need. The function must have the signature:
+ *
+ * \code
+ * enum test_result_code func_(struct weston_test_harness *harness, typeof(array_[0]) *arg)
+ * \endcode
+ *
+ * The function must call one of weston_test_harness_execute_standalone(),
+ * weston_test_harness_execute_as_plugin() or
+ * weston_test_harness_execute_as_client() passing in the \c harness argument,
+ * and return the return value from that call. The function can also return a
+ * test_result_code on its own if it does not want to run the tests,
+ * e.g. RESULT_SKIP or RESULT_HARD_ERROR.
+ *
+ * The function will be called once with each element of the array pointed to
+ * by \c arg, so that all tests would be repeated for each element in turn.
+ *
+ * \param func_ The function to be used as fixture setup.
+ * \param array_ A static const array of arbitrary type.
+ *
+ * \ingroup testharness
+ */
+#define DECLARE_FIXTURE_SETUP_WITH_ARG(func_, array_)                  \
+       const struct fixture_setup_array *                              \
+       fixture_setup_array_get_(void)                                  \
+       {                                                               \
+               static const struct fixture_setup_array arr = {         \
+                       .array = array_,                                \
+                       .element_size = sizeof(array_[0]),              \
+                       .n_elements = ARRAY_LENGTH(array_)              \
+               };                                                      \
+               return &arr;                                            \
+       }                                                               \
+                                                                       \
+       enum test_result_code                                           \
+       fixture_setup_run_(struct weston_test_harness *harness,         \
+                          const void *arg_)                            \
+       {                                                               \
+               typeof(array_[0]) *arg = arg_;                          \
+               return func_(harness, arg);                             \
+       }
+
+enum test_result_code
+weston_test_harness_execute_as_client(struct weston_test_harness *harness,
+                                     const struct compositor_setup *setup);
+
+enum test_result_code
+weston_test_harness_execute_as_plugin(struct weston_test_harness *harness,
+                                     const struct compositor_setup *setup);
+
+enum test_result_code
+weston_test_harness_execute_standalone(struct weston_test_harness *harness);
+
+#endif
diff --git a/tests/weston-testsuite-data.h b/tests/weston-testsuite-data.h
new file mode 100644 (file)
index 0000000..06c35bd
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2019 Collabora, Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef WESTON_TESTSUITE_DATA_H
+#define WESTON_TESTSUITE_DATA_H
+
+/** Standard return codes
+ *
+ * Both Autotools and Meson use these codes as test program exit codes
+ * to denote the test result for the whole process.
+ *
+ * \ingroup testharness
+ */
+enum test_result_code {
+       RESULT_OK = 0,
+       RESULT_SKIP = 77,
+       RESULT_FAIL = 1,
+       RESULT_HARD_ERROR = 99,
+};
+
+struct weston_test;
+struct weston_compositor;
+
+/** Weston test types
+ *
+ * \sa weston_test_harness_execute_standalone
+ * weston_test_harness_execute_as_plugin
+ * weston_test_harness_execute_as_client
+ *
+ * \ingroup testharness_private
+ */
+enum test_type {
+       TEST_TYPE_STANDALONE,
+       TEST_TYPE_PLUGIN,
+       TEST_TYPE_CLIENT,
+};
+
+/** Test harness specific data for running tests
+ *
+ * \ingroup testharness_private
+ */
+struct wet_testsuite_data {
+       void (*run)(struct wet_testsuite_data *);
+
+       /* test definitions */
+       const struct weston_test_entry *tests;
+       unsigned tests_count;
+       int case_index;
+       enum test_type type;
+       struct weston_compositor *compositor;
+
+       /* client thread control */
+       int thread_event_pipe;
+
+       /* informational run state */
+       int fixture_iteration;
+
+       /* test counts */
+       unsigned counter;
+       unsigned passed;
+       unsigned skipped;
+       unsigned failed;
+       unsigned total;
+};
+
+#endif /* WESTON_TESTSUITE_DATA_H */