downstream: Multiseat support for drm/wayland backends
[profile/ivi/weston-ivi-shell.git] / src / compositor-drm.c
index 3c44f7a..d558f7d 100644 (file)
  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#define _GNU_SOURCE
+#include "config.h"
 
 #include <errno.h>
 #include <stdlib.h>
+#include <ctype.h>
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <linux/input.h>
+#include <linux/vt.h>
 #include <assert.h>
 #include <sys/mman.h>
+#include <dlfcn.h>
+#include <time.h>
 
 #include <xf86drm.h>
 #include <xf86drmMode.h>
 #include <drm_fourcc.h>
 
 #include <gbm.h>
-#include <libbacklight.h>
 #include <libudev.h>
 
+#include "libbacklight.h"
 #include "compositor.h"
 #include "gl-renderer.h"
 #include "pixman-renderer.h"
-#include "udev-seat.h"
+#include "udev-input.h"
 #include "launcher-util.h"
+#include "vaapi-recorder.h"
+
+#ifndef DRM_CAP_TIMESTAMP_MONOTONIC
+#define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
+#endif
+
+#ifndef DRM_CAP_CURSOR_WIDTH
+#define DRM_CAP_CURSOR_WIDTH 0x8
+#endif
+
+#ifndef DRM_CAP_CURSOR_HEIGHT
+#define DRM_CAP_CURSOR_HEIGHT 0x9
+#endif
+
+#ifndef GBM_BO_USE_CURSOR
+#define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
+#endif
 
 static int option_current_mode = 0;
-static char *output_name;
-static char *output_mode;
-static char *output_transform;
-static struct wl_list configured_output_list;
 
 enum output_config {
        OUTPUT_CONFIG_INVALID = 0,
@@ -61,16 +78,6 @@ enum output_config {
        OUTPUT_CONFIG_MODELINE
 };
 
-struct drm_configured_output {
-       char *name;
-       char *mode;
-       uint32_t transform;
-       int32_t width, height;
-       drmModeModeInfo crtc_mode;
-       enum output_config config;
-       struct wl_list link;
-};
-
 struct drm_compositor {
        struct weston_compositor base;
 
@@ -83,13 +90,15 @@ struct drm_compositor {
        struct {
                int id;
                int fd;
+               char *filename;
        } drm;
        struct gbm_device *gbm;
        uint32_t *crtcs;
        int num_crtcs;
        uint32_t crtc_allocator;
        uint32_t connector_allocator;
-       struct tty *tty;
+       struct wl_listener session_listener;
+       uint32_t format;
 
        /* we need these parameters in order to not fail drmModeAddFB2()
         * due to out of bounds dimensions, and then mistakenly set
@@ -108,6 +117,13 @@ struct drm_compositor {
        int use_pixman;
 
        uint32_t prev_state;
+
+       clockid_t clock;
+       struct udev_input input;
+       char *main_seat;
+
+       uint32_t cursor_width;
+       uint32_t cursor_height;
 };
 
 struct drm_mode {
@@ -131,23 +147,34 @@ struct drm_fb {
        void *map;
 };
 
+struct drm_edid {
+       char eisa_id[13];
+       char monitor_name[13];
+       char pnp_id[5];
+       char serial_number[13];
+};
+
 struct drm_output {
        struct weston_output   base;
 
-       char *name;
        uint32_t crtc_id;
        int pipe;
        uint32_t connector_id;
        drmModeCrtcPtr original_crtc;
+       struct drm_edid edid;
+       drmModePropertyPtr dpms_prop;
+       uint32_t format;
 
        int vblank_pending;
        int page_flip_pending;
+       int destroy_pending;
+       int force_modeset;
 
        struct gbm_surface *surface;
        struct gbm_bo *cursor_bo[2];
        struct weston_plane cursor_plane;
        struct weston_plane fb_plane;
-       struct weston_surface *cursor_surface;
+       struct weston_view *cursor_view;
        int current_cursor;
        struct drm_fb *current, *next;
        struct backlight *backlight;
@@ -156,6 +183,9 @@ struct drm_output {
        pixman_image_t *image[2];
        int current_image;
        pixman_region32_t previous_damage;
+
+       struct vaapi_recorder *recorder;
+       struct wl_listener recorder_frame_listener;
 };
 
 /*
@@ -183,6 +213,15 @@ struct drm_sprite {
        uint32_t formats[];
 };
 
+struct drm_parameters {
+       int connector;
+       int tty;
+       int use_pixman;
+       const char *seat_id;
+};
+
+static struct gl_renderer_interface *gl_renderer;
+
 static const char default_seat[] = "seat0";
 
 static void
@@ -231,10 +270,11 @@ drm_fb_create_dumb(struct drm_compositor *ec, unsigned width, unsigned height)
        struct drm_mode_destroy_dumb destroy_arg;
        struct drm_mode_map_dumb map_arg;
 
-       fb = calloc(1, sizeof *fb);
+       fb = zalloc(sizeof *fb);
        if (!fb)
                return NULL;
 
+       memset(&create_arg, 0, sizeof create_arg);
        create_arg.bpp = 32;
        create_arg.width = width;
        create_arg.height = height;
@@ -253,10 +293,9 @@ drm_fb_create_dumb(struct drm_compositor *ec, unsigned width, unsigned height)
        if (ret)
                goto err_bo;
 
-       memset(&map_arg, 0, sizeof(map_arg));
+       memset(&map_arg, 0, sizeof map_arg);
        map_arg.handle = fb->handle;
-       drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
-
+       ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
        if (ret)
                goto err_add_fb;
 
@@ -368,7 +407,7 @@ err_free:
 }
 
 static void
-drm_fb_set_buffer(struct drm_fb *fb, struct wl_buffer *buffer)
+drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
 {
        assert(fb->buffer_ref.buffer == NULL);
 
@@ -391,7 +430,7 @@ drm_output_release_fb(struct drm_output *output, struct drm_fb *fb)
                        gbm_bo_destroy(fb->bo);
                else
                        gbm_surface_release_buffer(output->surface,
-                                                  output->current->bo);
+                                                  fb->bo);
        }
 }
 
@@ -404,57 +443,56 @@ drm_output_check_scanout_format(struct drm_output *output,
 
        format = gbm_bo_get_format(bo);
 
-       switch (format) {
-       case GBM_FORMAT_XRGB8888:
-               return format;
-       case GBM_FORMAT_ARGB8888:
-               /* We can only scanout an ARGB buffer if the surface's
-                * opaque region covers the whole output */
-               pixman_region32_init(&r);
-               pixman_region32_subtract(&r, &output->base.region,
-                                        &es->opaque);
+       if (format == GBM_FORMAT_ARGB8888) {
+               /* We can scanout an ARGB buffer if the surface's
+                * opaque region covers the whole output, but we have
+                * to use XRGB as the KMS format code. */
+               pixman_region32_init_rect(&r, 0, 0,
+                                         output->base.width,
+                                         output->base.height);
+               pixman_region32_subtract(&r, &r, &es->opaque);
 
                if (!pixman_region32_not_empty(&r))
                        format = GBM_FORMAT_XRGB8888;
-               else
-                       format = 0;
 
                pixman_region32_fini(&r);
+       }
 
+       if (output->format == format)
                return format;
-       default:
-               return 0;
-       }
+
+       return 0;
 }
 
 static struct weston_plane *
-drm_output_prepare_scanout_surface(struct weston_output *_output,
-                                  struct weston_surface *es)
+drm_output_prepare_scanout_view(struct weston_output *_output,
+                               struct weston_view *ev)
 {
        struct drm_output *output = (struct drm_output *) _output;
        struct drm_compositor *c =
                (struct drm_compositor *) output->base.compositor;
-       struct wl_buffer *buffer = es->buffer_ref.buffer;
+       struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
+       struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
        struct gbm_bo *bo;
        uint32_t format;
 
-       if (es->geometry.x != output->base.x ||
-           es->geometry.y != output->base.y ||
+       if (ev->geometry.x != output->base.x ||
+           ev->geometry.y != output->base.y ||
            buffer == NULL || c->gbm == NULL ||
-           buffer->width != output->base.current->width ||
-           buffer->height != output->base.current->height ||
-           output->base.transform != es->buffer_transform ||
-           es->transform.enabled)
+           buffer->width != output->base.current_mode->width ||
+           buffer->height != output->base.current_mode->height ||
+           output->base.transform != viewport->buffer.transform ||
+           ev->transform.enabled)
                return NULL;
 
        bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
-                          buffer, GBM_BO_USE_SCANOUT);
+                          buffer->resource, GBM_BO_USE_SCANOUT);
 
        /* Unable to use the buffer for scanout */
        if (!bo)
                return NULL;
 
-       format = drm_output_check_scanout_format(output, es, bo);
+       format = drm_output_check_scanout_format(output, ev->surface, bo);
        if (format == 0) {
                gbm_bo_destroy(bo);
                return NULL;
@@ -486,7 +524,7 @@ drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
                return;
        }
 
-       output->next = drm_fb_get_from_bo(bo, c, GBM_FORMAT_XRGB8888);
+       output->next = drm_fb_get_from_bo(bo, c, output->format);
        if (!output->next) {
                weston_log("failed to get drm_fb for bo\n");
                gbm_surface_release_buffer(output->surface, bo);
@@ -536,6 +574,27 @@ drm_output_render(struct drm_output *output, pixman_region32_t *damage)
 }
 
 static void
+drm_output_set_gamma(struct weston_output *output_base,
+                    uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
+{
+       int rc;
+       struct drm_output *output = (struct drm_output *) output_base;
+       struct drm_compositor *compositor = (struct drm_compositor *) output->base.compositor;
+
+       /* check */
+       if (output_base->gamma_size != size)
+               return;
+       if (!output->original_crtc)
+               return;
+
+       rc = drmModeCrtcSetGamma(compositor->drm.fd,
+                                output->crtc_id,
+                                size, r, g, b);
+       if (rc)
+               weston_log("set gamma failed: %m\n");
+}
+
+static int
 drm_output_repaint(struct weston_output *output_base,
                   pixman_region32_t *damage)
 {
@@ -546,28 +605,35 @@ drm_output_repaint(struct weston_output *output_base,
        struct drm_mode *mode;
        int ret = 0;
 
+       if (output->destroy_pending)
+               return -1;
+
        if (!output->next)
                drm_output_render(output, damage);
        if (!output->next)
-               return;
+               return -1;
 
-       mode = container_of(output->base.current, struct drm_mode, base);
-       if (!output->current) {
+       mode = container_of(output->base.current_mode, struct drm_mode, base);
+       if (!output->current ||
+           output->current->stride != output->next->stride ||
+           output->force_modeset) {
                ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
                                     output->next->fb_id, 0, 0,
                                     &output->connector_id, 1,
                                     &mode->mode_info);
                if (ret) {
                        weston_log("set mode failed: %m\n");
-                       return;
+                       goto err_pageflip;
                }
+               output_base->set_dpms(output_base, WESTON_DPMS_ON);
+               output->force_modeset = 0;
        }
 
        if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
                            output->next->fb_id,
                            DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
                weston_log("queueing pageflip failed: %m\n");
-               return;
+               goto err_pageflip;
        }
 
        output->page_flip_pending = 1;
@@ -619,7 +685,51 @@ drm_output_repaint(struct weston_output *output_base,
                output->vblank_pending = 1;
        }
 
+       return 0;
+
+err_pageflip:
+       output->cursor_view = NULL;
+       if (output->next) {
+               drm_output_release_fb(output, output->next);
+               output->next = NULL;
+       }
+
+       return -1;
+}
+
+static void
+drm_output_start_repaint_loop(struct weston_output *output_base)
+{
+       struct drm_output *output = (struct drm_output *) output_base;
+       struct drm_compositor *compositor = (struct drm_compositor *)
+               output_base->compositor;
+       uint32_t fb_id;
+       uint32_t msec;
+       struct timespec ts;
+
+       if (output->destroy_pending)
+               return;
+
+       if (!output->current) {
+               /* We can't page flip if there's no mode set */
+               goto finish_frame;
+       }
+
+       fb_id = output->current->fb_id;
+
+       if (drmModePageFlip(compositor->drm.fd, output->crtc_id, fb_id,
+                           DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
+               weston_log("queueing pageflip failed: %m\n");
+               goto finish_frame;
+       }
+
        return;
+
+finish_frame:
+       /* if we cannot page-flip, immediately finish frame */
+       clock_gettime(compositor->clock, &ts);
+       msec = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
+       weston_output_finish_frame(output_base, msec);
 }
 
 static void
@@ -643,27 +753,42 @@ vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
 }
 
 static void
+drm_output_destroy(struct weston_output *output_base);
+
+static void
 page_flip_handler(int fd, unsigned int frame,
                  unsigned int sec, unsigned int usec, void *data)
 {
        struct drm_output *output = (struct drm_output *) data;
        uint32_t msecs;
 
-       output->page_flip_pending = 0;
+       /* We don't set page_flip_pending on start_repaint_loop, in that case
+        * we just want to page flip to the current buffer to get an accurate
+        * timestamp */
+       if (output->page_flip_pending) {
+               drm_output_release_fb(output, output->current);
+               output->current = output->next;
+               output->next = NULL;
+       }
 
-       drm_output_release_fb(output, output->current);
-       output->current = output->next;
-       output->next = NULL;
+       output->page_flip_pending = 0;
 
-       if (!output->vblank_pending) {
+       if (output->destroy_pending)
+               drm_output_destroy(&output->base);
+       else if (!output->vblank_pending) {
                msecs = sec * 1000 + usec / 1000;
                weston_output_finish_frame(&output->base, msecs);
+
+               /* We can't call this from frame_notify, because the output's
+                * repaint needed flag is cleared just after that */
+               if (output->recorder)
+                       weston_output_schedule_repaint(&output->base);
        }
 }
 
 static uint32_t
 drm_output_check_sprite_format(struct drm_sprite *s,
-                              struct weston_surface *es, struct gbm_bo *bo)
+                              struct weston_view *ev, struct gbm_bo *bo)
 {
        uint32_t i, format;
 
@@ -672,9 +797,10 @@ drm_output_check_sprite_format(struct drm_sprite *s,
        if (format == GBM_FORMAT_ARGB8888) {
                pixman_region32_t r;
 
-               pixman_region32_init(&r);
-               pixman_region32_subtract(&r, &es->transform.boundingbox,
-                                        &es->transform.opaque);
+               pixman_region32_init_rect(&r, 0, 0,
+                                         ev->surface->width,
+                                         ev->surface->height);
+               pixman_region32_subtract(&r, &r, &ev->surface->opaque);
 
                if (!pixman_region32_not_empty(&r))
                        format = GBM_FORMAT_XRGB8888;
@@ -690,18 +816,19 @@ drm_output_check_sprite_format(struct drm_sprite *s,
 }
 
 static int
-drm_surface_transform_supported(struct weston_surface *es)
+drm_view_transform_supported(struct weston_view *ev)
 {
-       return !es->transform.enabled ||
-               (es->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
+       return !ev->transform.enabled ||
+               (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
 }
 
 static struct weston_plane *
-drm_output_prepare_overlay_surface(struct weston_output *output_base,
-                                  struct weston_surface *es)
+drm_output_prepare_overlay_view(struct weston_output *output_base,
+                               struct weston_view *ev)
 {
        struct weston_compositor *ec = output_base->compositor;
        struct drm_compositor *c =(struct drm_compositor *) ec;
+       struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
        struct drm_sprite *s;
        int found = 0;
        struct gbm_bo *bo;
@@ -713,25 +840,28 @@ drm_output_prepare_overlay_surface(struct weston_output *output_base,
        if (c->gbm == NULL)
                return NULL;
 
-       if (es->buffer_transform != output_base->transform)
+       if (viewport->buffer.transform != output_base->transform)
+               return NULL;
+
+       if (viewport->buffer.scale != output_base->current_scale)
                return NULL;
 
        if (c->sprites_are_broken)
                return NULL;
 
-       if (es->output_mask != (1u << output_base->id))
+       if (ev->output_mask != (1u << output_base->id))
                return NULL;
 
-       if (es->buffer_ref.buffer == NULL)
+       if (ev->surface->buffer_ref.buffer == NULL)
                return NULL;
 
-       if (es->alpha != 1.0f)
+       if (ev->alpha != 1.0f)
                return NULL;
 
-       if (wl_buffer_is_shm(es->buffer_ref.buffer))
+       if (wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource))
                return NULL;
 
-       if (!drm_surface_transform_supported(es))
+       if (!drm_view_transform_supported(ev))
                return NULL;
 
        wl_list_for_each(s, &c->sprite_list, link) {
@@ -749,11 +879,12 @@ drm_output_prepare_overlay_surface(struct weston_output *output_base,
                return NULL;
 
        bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER,
-                          es->buffer_ref.buffer, GBM_BO_USE_SCANOUT);
+                          ev->surface->buffer_ref.buffer->resource,
+                          GBM_BO_USE_SCANOUT);
        if (!bo)
                return NULL;
 
-       format = drm_output_check_sprite_format(s, es, bo);
+       format = drm_output_check_sprite_format(s, ev, bo);
        if (format == 0) {
                gbm_bo_destroy(bo);
                return NULL;
@@ -765,25 +896,27 @@ drm_output_prepare_overlay_surface(struct weston_output *output_base,
                return NULL;
        }
 
-       drm_fb_set_buffer(s->next, es->buffer_ref.buffer);
+       drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer);
 
-       box = pixman_region32_extents(&es->transform.boundingbox);
+       box = pixman_region32_extents(&ev->transform.boundingbox);
        s->plane.x = box->x1;
        s->plane.y = box->y1;
 
        /*
         * Calculate the source & dest rects properly based on actual
-        * position (note the caller has called weston_surface_update_transform()
+        * position (note the caller has called weston_view_update_transform()
         * for us already).
         */
        pixman_region32_init(&dest_rect);
-       pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
+       pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
                                  &output_base->region);
        pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
        box = pixman_region32_extents(&dest_rect);
        tbox = weston_transformed_rect(output_base->width,
                                       output_base->height,
-                                      output_base->transform, *box);
+                                      output_base->transform,
+                                      output_base->current_scale,
+                                      *box);
        s->dest_x = tbox.x1;
        s->dest_y = tbox.y1;
        s->dest_w = tbox.x2 - tbox.x1;
@@ -791,36 +924,38 @@ drm_output_prepare_overlay_surface(struct weston_output *output_base,
        pixman_region32_fini(&dest_rect);
 
        pixman_region32_init(&src_rect);
-       pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
+       pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
                                  &output_base->region);
        box = pixman_region32_extents(&src_rect);
 
-       weston_surface_from_global_fixed(es,
-                                        wl_fixed_from_int(box->x1),
-                                        wl_fixed_from_int(box->y1),
-                                        &sx1, &sy1);
-       weston_surface_from_global_fixed(es,
-                                        wl_fixed_from_int(box->x2),
-                                        wl_fixed_from_int(box->y2),
-                                        &sx2, &sy2);
+       weston_view_from_global_fixed(ev,
+                                     wl_fixed_from_int(box->x1),
+                                     wl_fixed_from_int(box->y1),
+                                     &sx1, &sy1);
+       weston_view_from_global_fixed(ev,
+                                     wl_fixed_from_int(box->x2),
+                                     wl_fixed_from_int(box->y2),
+                                     &sx2, &sy2);
 
        if (sx1 < 0)
                sx1 = 0;
        if (sy1 < 0)
                sy1 = 0;
-       if (sx2 > wl_fixed_from_int(es->geometry.width))
-               sx2 = wl_fixed_from_int(es->geometry.width);
-       if (sy2 > wl_fixed_from_int(es->geometry.height))
-               sy2 = wl_fixed_from_int(es->geometry.height);
+       if (sx2 > wl_fixed_from_int(ev->surface->width))
+               sx2 = wl_fixed_from_int(ev->surface->width);
+       if (sy2 > wl_fixed_from_int(ev->surface->height))
+               sy2 = wl_fixed_from_int(ev->surface->height);
 
        tbox.x1 = sx1;
        tbox.y1 = sy1;
        tbox.x2 = sx2;
        tbox.y2 = sy2;
 
-       tbox = weston_transformed_rect(wl_fixed_from_int(es->geometry.width),
-                                      wl_fixed_from_int(es->geometry.height),
-                                      es->buffer_transform, tbox);
+       tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
+                                      wl_fixed_from_int(ev->surface->height),
+                                      viewport->buffer.transform,
+                                      viewport->buffer.scale,
+                                      tbox);
 
        s->src_x = tbox.x1 << 8;
        s->src_y = tbox.y1 << 8;
@@ -832,29 +967,32 @@ drm_output_prepare_overlay_surface(struct weston_output *output_base,
 }
 
 static struct weston_plane *
-drm_output_prepare_cursor_surface(struct weston_output *output_base,
-                                 struct weston_surface *es)
+drm_output_prepare_cursor_view(struct weston_output *output_base,
+                              struct weston_view *ev)
 {
        struct drm_compositor *c =
                (struct drm_compositor *) output_base->compositor;
+       struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
        struct drm_output *output = (struct drm_output *) output_base;
 
        if (c->gbm == NULL)
                return NULL;
        if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
                return NULL;
-       if (output->cursor_surface)
+       if (viewport->buffer.scale != output_base->current_scale)
                return NULL;
-       if (es->output_mask != (1u << output_base->id))
+       if (output->cursor_view)
+               return NULL;
+       if (ev->output_mask != (1u << output_base->id))
                return NULL;
        if (c->cursors_are_broken)
                return NULL;
-       if (es->buffer_ref.buffer == NULL ||
-           !wl_buffer_is_shm(es->buffer_ref.buffer) ||
-           es->geometry.width > 64 || es->geometry.height > 64)
+       if (ev->surface->buffer_ref.buffer == NULL ||
+           !wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource) ||
+           ev->surface->width > 64 || ev->surface->height > 64)
                return NULL;
 
-       output->cursor_surface = es;
+       output->cursor_view = ev;
 
        return &output->cursor_plane;
 }
@@ -862,47 +1000,52 @@ drm_output_prepare_cursor_surface(struct weston_output *output_base,
 static void
 drm_output_set_cursor(struct drm_output *output)
 {
-       struct weston_surface *es = output->cursor_surface;
+       struct weston_view *ev = output->cursor_view;
+       struct weston_buffer *buffer;
        struct drm_compositor *c =
                (struct drm_compositor *) output->base.compositor;
        EGLint handle, stride;
        struct gbm_bo *bo;
-       uint32_t buf[64 * 64];
+       uint32_t buf[c->cursor_width * c->cursor_height];
        unsigned char *s;
        int i, x, y;
 
-       output->cursor_surface = NULL;
-       if (es == NULL) {
+       output->cursor_view = NULL;
+       if (ev == NULL) {
                drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
                return;
        }
 
-       if (es->buffer_ref.buffer &&
+       buffer = ev->surface->buffer_ref.buffer;
+
+       if (buffer &&
            pixman_region32_not_empty(&output->cursor_plane.damage)) {
                pixman_region32_fini(&output->cursor_plane.damage);
                pixman_region32_init(&output->cursor_plane.damage);
                output->current_cursor ^= 1;
                bo = output->cursor_bo[output->current_cursor];
                memset(buf, 0, sizeof buf);
-               stride = wl_shm_buffer_get_stride(es->buffer_ref.buffer);
-               s = wl_shm_buffer_get_data(es->buffer_ref.buffer);
-               for (i = 0; i < es->geometry.height; i++)
-                       memcpy(buf + i * 64, s + i * stride,
-                              es->geometry.width * 4);
+               stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
+               s = wl_shm_buffer_get_data(buffer->shm_buffer);
+               wl_shm_buffer_begin_access(buffer->shm_buffer);
+               for (i = 0; i < ev->surface->height; i++)
+                       memcpy(buf + i * c->cursor_width, s + i * stride,
+                              ev->surface->width * 4);
+               wl_shm_buffer_end_access(buffer->shm_buffer);
 
                if (gbm_bo_write(bo, buf, sizeof buf) < 0)
                        weston_log("failed update cursor: %m\n");
 
                handle = gbm_bo_get_handle(bo).s32;
-               if (drmModeSetCursor(c->drm.fd,
-                                    output->crtc_id, handle, 64, 64)) {
+               if (drmModeSetCursor(c->drm.fd, output->crtc_id, handle,
+                               c->cursor_width, c->cursor_height)) {
                        weston_log("failed to set cursor: %m\n");
                        c->cursors_are_broken = 1;
                }
        }
 
-       x = es->geometry.x - output->base.x;
-       y = es->geometry.y - output->base.y;
+       x = (ev->geometry.x - output->base.x) * output->base.current_scale;
+       y = (ev->geometry.y - output->base.y) * output->base.current_scale;
        if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
                if (drmModeMoveCursor(c->drm.fd, output->crtc_id, x, y)) {
                        weston_log("failed to move cursor: %m\n");
@@ -919,26 +1062,10 @@ drm_assign_planes(struct weston_output *output)
 {
        struct drm_compositor *c =
                (struct drm_compositor *) output->compositor;
-       struct drm_output *drm_output = (struct drm_output *) output;
-       struct drm_sprite *s;
-       struct weston_surface *es, *next;
+       struct weston_view *ev, *next;
        pixman_region32_t overlap, surface_overlap;
        struct weston_plane *primary, *next_plane;
 
-       /* Reset the opaque region of the planes */
-       pixman_region32_fini(&drm_output->cursor_plane.opaque);
-       pixman_region32_init(&drm_output->cursor_plane.opaque);
-       pixman_region32_fini(&drm_output->fb_plane.opaque);
-       pixman_region32_init(&drm_output->fb_plane.opaque);
-
-       wl_list_for_each (s, &c->sprite_list, link) {
-               if (!drm_sprite_crtc_supported(output, s->possible_crtcs))
-                       continue;
-
-               pixman_region32_fini(&s->plane.opaque);
-               pixman_region32_init(&s->plane.opaque);
-       }
-
        /*
         * Find a surface for each sprite in the output using some heuristics:
         * 1) size
@@ -954,36 +1081,45 @@ drm_assign_planes(struct weston_output *output)
         */
        pixman_region32_init(&overlap);
        primary = &c->base.primary_plane;
-       wl_list_for_each_safe(es, next, &c->base.surface_list, link) {
-               /* test whether this buffer can ever go into a plane:
-                * non-shm, or small enough to be a cursor
+
+       wl_list_for_each_safe(ev, next, &c->base.view_list, link) {
+               struct weston_surface *es = ev->surface;
+
+               /* Test whether this buffer can ever go into a plane:
+                * non-shm, or small enough to be a cursor.
+                *
+                * Also, keep a reference when using the pixman renderer.
+                * That makes it possible to do a seamless switch to the GL
+                * renderer and since the pixman renderer keeps a reference
+                * to the buffer anyway, there is no side effects.
                 */
-               if ((es->buffer_ref.buffer &&
-                    !wl_buffer_is_shm(es->buffer_ref.buffer)) ||
-                   (es->geometry.width <= 64 && es->geometry.height <= 64))
+               if (c->use_pixman ||
+                   (es->buffer_ref.buffer &&
+                   (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
+                    (ev->surface->width <= 64 && ev->surface->height <= 64))))
                        es->keep_buffer = 1;
                else
                        es->keep_buffer = 0;
 
                pixman_region32_init(&surface_overlap);
                pixman_region32_intersect(&surface_overlap, &overlap,
-                                         &es->transform.boundingbox);
+                                         &ev->transform.boundingbox);
 
                next_plane = NULL;
                if (pixman_region32_not_empty(&surface_overlap))
                        next_plane = primary;
                if (next_plane == NULL)
-                       next_plane = drm_output_prepare_cursor_surface(output, es);
+                       next_plane = drm_output_prepare_cursor_view(output, ev);
                if (next_plane == NULL)
-                       next_plane = drm_output_prepare_scanout_surface(output, es);
+                       next_plane = drm_output_prepare_scanout_view(output, ev);
                if (next_plane == NULL)
-                       next_plane = drm_output_prepare_overlay_surface(output, es);
+                       next_plane = drm_output_prepare_overlay_view(output, ev);
                if (next_plane == NULL)
                        next_plane = primary;
-               weston_surface_move_to_plane(es, next_plane);
+               weston_view_move_to_plane(ev, next_plane);
                if (next_plane == primary)
                        pixman_region32_union(&overlap, &overlap,
-                                             &es->transform.boundingbox);
+                                             &ev->transform.boundingbox);
 
                pixman_region32_fini(&surface_overlap);
        }
@@ -1001,9 +1137,17 @@ drm_output_destroy(struct weston_output *output_base)
                (struct drm_compositor *) output->base.compositor;
        drmModeCrtcPtr origcrtc = output->original_crtc;
 
+       if (output->page_flip_pending) {
+               output->destroy_pending = 1;
+               weston_log("destroy output while page flip pending\n");
+               return;
+       }
+
        if (output->backlight)
                backlight_destroy(output->backlight);
 
+       drmModeFreeProperty(output->dpms_prop);
+
        /* Turn off hardware cursor */
        drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
 
@@ -1019,7 +1163,7 @@ drm_output_destroy(struct weston_output *output_base)
        if (c->use_pixman) {
                drm_output_fini_pixman(output);
        } else {
-               gl_renderer_output_destroy(output_base);
+               gl_renderer->output_destroy(output_base);
                gbm_surface_destroy(output->surface);
        }
 
@@ -1027,9 +1171,7 @@ drm_output_destroy(struct weston_output *output_base)
        weston_plane_release(&output->cursor_plane);
 
        weston_output_destroy(&output->base);
-       wl_list_remove(&output->base.link);
 
-       free(output->name);
        free(output);
 }
 
@@ -1038,11 +1180,11 @@ choose_mode (struct drm_output *output, struct weston_mode *target_mode)
 {
        struct drm_mode *tmp_mode = NULL, *mode;
 
-       if (output->base.current->width == target_mode->width && 
-           output->base.current->height == target_mode->height &&
-           (output->base.current->refresh == target_mode->refresh ||
+       if (output->base.current_mode->width == target_mode->width &&
+           output->base.current_mode->height == target_mode->height &&
+           (output->base.current_mode->refresh == target_mode->refresh ||
             target_mode->refresh == 0))
-               return (struct drm_mode *)output->base.current;
+               return (struct drm_mode *)output->base.current_mode;
 
        wl_list_for_each(mode, &output->base.mode_list, base.link) {
                if (mode->mode_info.hdisplay == target_mode->width &&
@@ -1089,13 +1231,13 @@ drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mo
                return -1;
        }
 
-       if (&drm_mode->base == output->base.current)
+       if (&drm_mode->base == output->base.current_mode)
                return 0;
 
-       output->base.current->flags = 0;
+       output->base.current_mode->flags = 0;
 
-       output->base.current = &drm_mode->base;
-       output->base.current->flags =
+       output->base.current_mode = &drm_mode->base;
+       output->base.current_mode->flags =
                WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
 
        /* reset rendering stuff. */
@@ -1111,7 +1253,7 @@ drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mo
                        return -1;
                }
        } else {
-               gl_renderer_output_destroy(&output->base);
+               gl_renderer->output_destroy(&output->base);
                gbm_surface_destroy(output->surface);
 
                if (drm_output_init_egl(output, ec) < 0) {
@@ -1142,7 +1284,8 @@ static int
 init_drm(struct drm_compositor *ec, struct udev_device *device)
 {
        const char *filename, *sysnum;
-       int fd;
+       uint64_t cap;
+       int fd, ret;
 
        sysnum = udev_device_get_sysnum(device);
        if (sysnum)
@@ -1153,7 +1296,7 @@ init_drm(struct drm_compositor *ec, struct udev_device *device)
        }
 
        filename = udev_device_get_devnode(device);
-       fd = open(filename, O_RDWR | O_CLOEXEC);
+       fd = weston_launcher_open(ec->base.launcher, filename, O_RDWR);
        if (fd < 0) {
                /* Probably permissions error */
                weston_log("couldn't open %s, skipping\n",
@@ -1164,7 +1307,61 @@ init_drm(struct drm_compositor *ec, struct udev_device *device)
        weston_log("using %s\n", filename);
 
        ec->drm.fd = fd;
+       ec->drm.filename = strdup(filename);
 
+       ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
+       if (ret == 0 && cap == 1)
+               ec->clock = CLOCK_MONOTONIC;
+       else
+               ec->clock = CLOCK_REALTIME;
+
+       ret = drmGetCap(fd, DRM_CAP_CURSOR_WIDTH, &cap);
+       if (ret == 0)
+               ec->cursor_width = cap;
+       else
+               ec->cursor_width = 64;
+
+       ret = drmGetCap(fd, DRM_CAP_CURSOR_HEIGHT, &cap);
+       if (ret == 0)
+               ec->cursor_height = cap;
+       else
+               ec->cursor_height = 64;
+
+       return 0;
+}
+
+static struct gbm_device *
+create_gbm_device(int fd)
+{
+       struct gbm_device *gbm;
+
+       gl_renderer = weston_load_module("gl-renderer.so",
+                                        "gl_renderer_interface");
+       if (!gl_renderer)
+               return NULL;
+
+       /* GBM will load a dri driver, but even though they need symbols from
+        * libglapi, in some version of Mesa they are not linked to it. Since
+        * only the gl-renderer module links to it, the call above won't make
+        * these symbols globally available, and loading the DRI driver fails.
+        * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
+       dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
+
+       gbm = gbm_create_device(fd);
+
+       return gbm;
+}
+
+static int
+drm_compositor_create_gl_renderer(struct drm_compositor *ec)
+{
+       EGLint format;
+
+       format = ec->format;
+       if (gl_renderer->create(&ec->base, ec->gbm,
+                              gl_renderer->opaque_attribs, &format) < 0) {
+               return -1;
+       }
 
        return 0;
 }
@@ -1172,10 +1369,12 @@ init_drm(struct drm_compositor *ec, struct udev_device *device)
 static int
 init_egl(struct drm_compositor *ec)
 {
-       ec->gbm = gbm_create_device(ec->drm.fd);
+       ec->gbm = create_gbm_device(ec->drm.fd);
+
+       if (!ec->gbm)
+               return -1;
 
-       if (gl_renderer_create(&ec->base, ec->gbm, gl_renderer_opaque_attribs,
-                       NULL) < 0) {
+       if (drm_compositor_create_gl_renderer(ec) < 0) {
                gbm_device_destroy(ec->gbm);
                return -1;
        }
@@ -1307,23 +1506,12 @@ drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
        struct drm_output *output = (struct drm_output *) output_base;
        struct weston_compositor *ec = output_base->compositor;
        struct drm_compositor *c = (struct drm_compositor *) ec;
-       drmModeConnectorPtr connector;
-       drmModePropertyPtr prop;
 
-       connector = drmModeGetConnector(c->drm.fd, output->connector_id);
-       if (!connector)
+       if (!output->dpms_prop)
                return;
 
-       prop = drm_get_prop(c->drm.fd, connector, "DPMS");
-       if (!prop) {
-               drmModeFreeConnector(connector);
-               return;
-       }
-
-       drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
-                                   prop->prop_id, level);
-       drmModeFreeProperty(prop);
-       drmModeFreeConnector(connector);
+       drmModeConnectorSetProperty(c->drm.fd, output->connector_id,
+                                   output->dpms_prop->prop_id, level);
 }
 
 static const char *connector_type_names[] = {
@@ -1375,12 +1563,13 @@ find_crtc_for_connector(struct drm_compositor *ec,
 static int
 drm_output_init_egl(struct drm_output *output, struct drm_compositor *ec)
 {
+       EGLint format = output->format;
        int i, flags;
 
        output->surface = gbm_surface_create(ec->gbm,
-                                            output->base.current->width,
-                                            output->base.current->height,
-                                            GBM_FORMAT_XRGB8888,
+                                            output->base.current_mode->width,
+                                            output->base.current_mode->height,
+                                            format,
                                             GBM_BO_USE_SCANOUT |
                                             GBM_BO_USE_RENDERING);
        if (!output->surface) {
@@ -1388,21 +1577,23 @@ drm_output_init_egl(struct drm_output *output, struct drm_compositor *ec)
                return -1;
        }
 
-       if (gl_renderer_output_create(&output->base, output->surface) < 0) {
+       if (gl_renderer->output_create(&output->base, output->surface,
+                                      gl_renderer->opaque_attribs,
+                                      &format) < 0) {
                weston_log("failed to create gl renderer output state\n");
                gbm_surface_destroy(output->surface);
                return -1;
        }
 
-       flags = GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE;
+       flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
 
        for (i = 0; i < 2; i++) {
                if (output->cursor_bo[i])
                        continue;
 
                output->cursor_bo[i] =
-                       gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
-                                     flags);
+                       gbm_bo_create(ec->gbm, ec->cursor_width, ec->cursor_height,
+                               GBM_FORMAT_ARGB8888, flags);
        }
 
        if (output->cursor_bo[0] == NULL || output->cursor_bo[1] == NULL) {
@@ -1416,8 +1607,8 @@ drm_output_init_egl(struct drm_output *output, struct drm_compositor *ec)
 static int
 drm_output_init_pixman(struct drm_output *output, struct drm_compositor *c)
 {
-       int w = output->base.current->width;
-       int h = output->base.current->height;
+       int w = output->base.current_mode->width;
+       int h = output->base.current_mode->height;
        unsigned int i;
 
        /* FIXME error checking */
@@ -1439,7 +1630,7 @@ drm_output_init_pixman(struct drm_output *output, struct drm_compositor *c)
                goto err;
 
        pixman_region32_init_rect(&output->previous_damage,
-                                 output->base.x, output->base.y, w, h);
+                                 output->base.x, output->base.y, output->base.width, output->base.height);
 
        return 0;
 
@@ -1473,6 +1664,268 @@ drm_output_fini_pixman(struct drm_output *output)
        }
 }
 
+static void
+edid_parse_string(const uint8_t *data, char text[])
+{
+       int i;
+       int replaced = 0;
+
+       /* this is always 12 bytes, but we can't guarantee it's null
+        * terminated or not junk. */
+       strncpy(text, (const char *) data, 12);
+
+       /* remove insane chars */
+       for (i = 0; text[i] != '\0'; i++) {
+               if (text[i] == '\n' ||
+                   text[i] == '\r') {
+                       text[i] = '\0';
+                       break;
+               }
+       }
+
+       /* ensure string is printable */
+       for (i = 0; text[i] != '\0'; i++) {
+               if (!isprint(text[i])) {
+                       text[i] = '-';
+                       replaced++;
+               }
+       }
+
+       /* if the string is random junk, ignore the string */
+       if (replaced > 4)
+               text[0] = '\0';
+}
+
+#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING       0xfe
+#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME           0xfc
+#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER  0xff
+#define EDID_OFFSET_DATA_BLOCKS                                0x36
+#define EDID_OFFSET_LAST_BLOCK                         0x6c
+#define EDID_OFFSET_PNPID                              0x08
+#define EDID_OFFSET_SERIAL                             0x0c
+
+static int
+edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
+{
+       int i;
+       uint32_t serial_number;
+
+       /* check header */
+       if (length < 128)
+               return -1;
+       if (data[0] != 0x00 || data[1] != 0xff)
+               return -1;
+
+       /* decode the PNP ID from three 5 bit words packed into 2 bytes
+        * /--08--\/--09--\
+        * 7654321076543210
+        * |\---/\---/\---/
+        * R  C1   C2   C3 */
+       edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
+       edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
+       edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
+       edid->pnp_id[3] = '\0';
+
+       /* maybe there isn't a ASCII serial number descriptor, so use this instead */
+       serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
+       serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
+       serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
+       serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
+       if (serial_number > 0)
+               sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
+
+       /* parse EDID data */
+       for (i = EDID_OFFSET_DATA_BLOCKS;
+            i <= EDID_OFFSET_LAST_BLOCK;
+            i += 18) {
+               /* ignore pixel clock data */
+               if (data[i] != 0)
+                       continue;
+               if (data[i+2] != 0)
+                       continue;
+
+               /* any useful blocks? */
+               if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
+                       edid_parse_string(&data[i+5],
+                                         edid->monitor_name);
+               } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
+                       edid_parse_string(&data[i+5],
+                                         edid->serial_number);
+               } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
+                       edid_parse_string(&data[i+5],
+                                         edid->eisa_id);
+               }
+       }
+       return 0;
+}
+
+static void
+find_and_parse_output_edid(struct drm_compositor *ec,
+                          struct drm_output *output,
+                          drmModeConnector *connector)
+{
+       drmModePropertyBlobPtr edid_blob = NULL;
+       drmModePropertyPtr property;
+       int i;
+       int rc;
+
+       for (i = 0; i < connector->count_props && !edid_blob; i++) {
+               property = drmModeGetProperty(ec->drm.fd, connector->props[i]);
+               if (!property)
+                       continue;
+               if ((property->flags & DRM_MODE_PROP_BLOB) &&
+                   !strcmp(property->name, "EDID")) {
+                       edid_blob = drmModeGetPropertyBlob(ec->drm.fd,
+                                                          connector->prop_values[i]);
+               }
+               drmModeFreeProperty(property);
+       }
+       if (!edid_blob)
+               return;
+
+       rc = edid_parse(&output->edid,
+                       edid_blob->data,
+                       edid_blob->length);
+       if (!rc) {
+               weston_log("EDID data '%s', '%s', '%s'\n",
+                          output->edid.pnp_id,
+                          output->edid.monitor_name,
+                          output->edid.serial_number);
+               if (output->edid.pnp_id[0] != '\0')
+                       output->base.make = output->edid.pnp_id;
+               if (output->edid.monitor_name[0] != '\0')
+                       output->base.model = output->edid.monitor_name;
+               if (output->edid.serial_number[0] != '\0')
+                       output->base.serial_number = output->edid.serial_number;
+       }
+       drmModeFreePropertyBlob(edid_blob);
+}
+
+
+
+static int
+parse_modeline(const char *s, drmModeModeInfo *mode)
+{
+       char hsync[16];
+       char vsync[16];
+       float fclock;
+
+       mode->type = DRM_MODE_TYPE_USERDEF;
+       mode->hskew = 0;
+       mode->vscan = 0;
+       mode->vrefresh = 0;
+       mode->flags = 0;
+
+       if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
+                  &fclock,
+                  &mode->hdisplay,
+                  &mode->hsync_start,
+                  &mode->hsync_end,
+                  &mode->htotal,
+                  &mode->vdisplay,
+                  &mode->vsync_start,
+                  &mode->vsync_end,
+                  &mode->vtotal, hsync, vsync) != 11)
+               return -1;
+
+       mode->clock = fclock * 1000;
+       if (strcmp(hsync, "+hsync") == 0)
+               mode->flags |= DRM_MODE_FLAG_PHSYNC;
+       else if (strcmp(hsync, "-hsync") == 0)
+               mode->flags |= DRM_MODE_FLAG_NHSYNC;
+       else
+               return -1;
+
+       if (strcmp(vsync, "+vsync") == 0)
+               mode->flags |= DRM_MODE_FLAG_PVSYNC;
+       else if (strcmp(vsync, "-vsync") == 0)
+               mode->flags |= DRM_MODE_FLAG_NVSYNC;
+       else
+               return -1;
+
+       return 0;
+}
+
+static uint32_t
+parse_transform(const char *transform, const char *output_name)
+{
+       static const struct { const char *name; uint32_t token; } names[] = {
+               { "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 },
+       };
+       unsigned int i;
+
+       for (i = 0; i < ARRAY_LENGTH(names); i++)
+               if (strcmp(names[i].name, transform) == 0)
+                       return names[i].token;
+
+       weston_log("Invalid transform \"%s\" for output %s\n",
+                  transform, output_name);
+
+       return WL_OUTPUT_TRANSFORM_NORMAL;
+}
+
+static void
+setup_output_seat_constraint(struct drm_compositor *ec,
+                            struct weston_output *output,
+                            const char *s)
+{
+       if (strcmp(s, "") != 0) {
+               struct udev_seat *seat;
+
+               seat = udev_seat_get_named(&ec->base, s);
+               if (seat) {
+                       udev_seat_link_output(seat, output);
+#if HAVE_MULTISEAT
+                       if (!seat->input.libinput)
+                               udev_input_init(&seat->input, &ec->base,
+                                               ec->udev, s);
+                       else if (seat->input.suspended)
+                               udev_input_enable(&seat->input);
+#endif
+                       if (seat->base.pointer)
+                               weston_pointer_clamp(seat->base.pointer,
+                                            &seat->base.pointer->x,
+                                            &seat->base.pointer->y);
+               }
+       }
+}
+
+static int
+get_gbm_format_from_section(struct weston_config_section *section,
+                           uint32_t default_value,
+                           uint32_t *format)
+{
+       char *s;
+       int ret = 0;
+
+       weston_config_section_get_string(section,
+                                        "gbm-format", &s, NULL);
+
+       if (s == NULL)
+               *format = default_value;
+       else if (strcmp(s, "xrgb8888") == 0)
+               *format = GBM_FORMAT_XRGB8888;
+       else if (strcmp(s, "rgb565") == 0)
+               *format = GBM_FORMAT_RGB565;
+       else if (strcmp(s, "xrgb2101010") == 0)
+               *format = GBM_FORMAT_XRGB2101010;
+       else {
+               weston_log("fatal: unrecognized pixel format: %s\n", s);
+               ret = -1;
+       }
+
+       free(s);
+
+       return ret;
+}
+
 static int
 create_output_for_connector(struct drm_compositor *ec,
                            drmModeRes *resources,
@@ -1480,15 +1933,18 @@ create_output_for_connector(struct drm_compositor *ec,
                            int x, int y, struct udev_device *drm_device)
 {
        struct drm_output *output;
-       struct drm_mode *drm_mode, *next, *preferred, *current, *configured;
+       struct drm_mode *drm_mode, *next, *preferred, *current, *configured, *best;
        struct weston_mode *m;
-       struct drm_configured_output *o = NULL, *temp;
+       struct weston_config_section *section;
        drmModeEncoder *encoder;
-       drmModeModeInfo crtc_mode;
+       drmModeModeInfo crtc_mode, modeline;
        drmModeCrtc *crtc;
-       int i;
-       char name[32];
+       int i, width, height, scale;
+       char name[32], *s;
        const char *type_name;
+       enum output_config config;
+       uint32_t transform;
+       int default_output;
 
        i = find_crtc_for_connector(ec, resources, connector);
        if (i < 0) {
@@ -1496,14 +1952,14 @@ create_output_for_connector(struct drm_compositor *ec,
                return -1;
        }
 
-       output = malloc(sizeof *output);
+       output = zalloc(sizeof *output);
        if (output == NULL)
                return -1;
 
-       memset(output, 0, sizeof *output);
        output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
        output->base.make = "unknown";
        output->base.model = "unknown";
+       output->base.serial_number = "unknown";
        wl_list_init(&output->base.mode_list);
 
        if (connector->connector_type < ARRAY_LENGTH(connector_type_names))
@@ -1511,7 +1967,46 @@ create_output_for_connector(struct drm_compositor *ec,
        else
                type_name = "UNKNOWN";
        snprintf(name, 32, "%s%d", type_name, connector->connector_type_id);
-       output->name = strdup(name);
+       output->base.name = strdup(name);
+
+       section = weston_config_get_section(ec->base.config, "output", "name",
+                                           output->base.name);
+       weston_config_section_get_string(section, "mode", &s, "preferred");
+       if (strcmp(s, "off") == 0)
+               config = OUTPUT_CONFIG_OFF;
+       else if (strcmp(s, "preferred") == 0)
+               config = OUTPUT_CONFIG_PREFERRED;
+       else if (strcmp(s, "current") == 0)
+               config = OUTPUT_CONFIG_CURRENT;
+       else if (sscanf(s, "%dx%d", &width, &height) == 2)
+               config = OUTPUT_CONFIG_MODE;
+       else if (parse_modeline(s, &modeline) == 0)
+               config = OUTPUT_CONFIG_MODELINE;
+       else {
+               weston_log("Invalid mode \"%s\" for output %s\n",
+                          s, output->base.name);
+               config = OUTPUT_CONFIG_PREFERRED;
+       }
+       free(s);
+
+       weston_config_section_get_int(section, "scale", &scale, 1);
+       weston_config_section_get_string(section, "transform", &s, "normal");
+       transform = parse_transform(s, output->base.name);
+       free(s);
+       weston_config_section_get_int(section, "default_output",
+                                     &default_output, 0);
+
+       if (get_gbm_format_from_section(section,
+                                       ec->format,
+                                       &output->format) == -1)
+               output->format = ec->format;
+
+       weston_config_section_get_string(section, "seat", &s, "");
+       output->base.seat_data.seatname = strdup(s);
+       weston_log("output %p belongs to seat '%s'\n", output,
+                       output->base.seat_data.seatname);
+       setup_output_seat_constraint(ec, &output->base, s);
+       free(s);
 
        output->crtc_id = resources->crtcs[i];
        output->pipe = i;
@@ -1520,6 +2015,7 @@ create_output_for_connector(struct drm_compositor *ec,
        ec->connector_allocator |= (1 << output->connector_id);
 
        output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
+       output->dpms_prop = drm_get_prop(ec->drm.fd, connector, "DPMS");
 
        /* Get the current mode on the crtc that's currently driving
         * this connector. */
@@ -1541,44 +2037,34 @@ create_output_for_connector(struct drm_compositor *ec,
                        goto err_free;
        }
 
-       preferred = NULL;
-       current = NULL;
-       configured = NULL;
-
-       wl_list_for_each(temp, &configured_output_list, link) {
-               if (strcmp(temp->name, output->name) == 0) {
-                       if (temp->mode)
-                               weston_log("%s mode \"%s\" in config\n",
-                                                       temp->name, temp->mode);
-                       o = temp;
-                       break;
-               }
-       }
-
-       if (o && o->config == OUTPUT_CONFIG_OFF) {
-               weston_log("Disabling output %s\n", o->name);
-
+       if (config == OUTPUT_CONFIG_OFF) {
+               weston_log("Disabling output %s\n", output->base.name);
                drmModeSetCrtc(ec->drm.fd, output->crtc_id,
-                                                       0, 0, 0, 0, 0, NULL);
+                              0, 0, 0, 0, 0, NULL);
                goto err_free;
        }
 
-       wl_list_for_each(drm_mode, &output->base.mode_list, base.link) {
-               if (o && o->config == OUTPUT_CONFIG_MODE &&
-                       o->width == drm_mode->base.width &&
-                       o->height == drm_mode->base.height)
+       preferred = NULL;
+       current = NULL;
+       configured = NULL;
+       best = NULL;
+
+       wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
+               if (config == OUTPUT_CONFIG_MODE &&
+                   width == drm_mode->base.width &&
+                   height == drm_mode->base.height)
                        configured = drm_mode;
                if (!memcmp(&crtc_mode, &drm_mode->mode_info, sizeof crtc_mode))
                        current = drm_mode;
                if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
                        preferred = drm_mode;
+               best = drm_mode;
        }
 
-       if (o && o->config == OUTPUT_CONFIG_MODELINE) {
-               configured = drm_output_add_mode(output, &o->crtc_mode);
+       if (config == OUTPUT_CONFIG_MODELINE) {
+               configured = drm_output_add_mode(output, &modeline);
                if (!configured)
                        goto err_free;
-               current = configured;
        }
 
        if (current == NULL && crtc_mode.clock != 0) {
@@ -1587,28 +2073,30 @@ create_output_for_connector(struct drm_compositor *ec,
                        goto err_free;
        }
 
-       if (o && o->config == OUTPUT_CONFIG_CURRENT)
+       if (config == OUTPUT_CONFIG_CURRENT)
                configured = current;
 
        if (option_current_mode && current)
-               output->base.current = &current->base;
+               output->base.current_mode = &current->base;
        else if (configured)
-               output->base.current = &configured->base;
+               output->base.current_mode = &configured->base;
        else if (preferred)
-               output->base.current = &preferred->base;
+               output->base.current_mode = &preferred->base;
        else if (current)
-               output->base.current = &current->base;
+               output->base.current_mode = &current->base;
+       else if (best)
+               output->base.current_mode = &best->base;
 
-       if (output->base.current == NULL) {
-               weston_log("no available modes for %s\n", output->name);
+       if (output->base.current_mode == NULL) {
+               weston_log("no available modes for %s\n", output->base.name);
                goto err_free;
        }
 
-       output->base.current->flags |= WL_OUTPUT_MODE_CURRENT;
+       output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
 
        weston_output_init(&output->base, &ec->base, x, y,
                           connector->mmWidth, connector->mmHeight,
-                          o ? o->transform : WL_OUTPUT_TRANSFORM_NORMAL);
+                          transform, scale);
 
        if (ec->use_pixman) {
                if (drm_output_init_pixman(output, ec) < 0) {
@@ -1623,26 +2111,43 @@ create_output_for_connector(struct drm_compositor *ec,
        output->backlight = backlight_init(drm_device,
                                           connector->connector_type);
        if (output->backlight) {
+               weston_log("Initialized backlight, device %s\n",
+                          output->backlight->path);
                output->base.set_backlight = drm_set_backlight;
                output->base.backlight_current = drm_get_backlight(output);
+       } else {
+               weston_log("Failed to initialize backlight\n");
        }
 
        wl_list_insert(ec->base.output_list.prev, &output->base.link);
+       if (default_output)
+               ec->base.default_output = &output->base;
+
+       find_and_parse_output_edid(ec, output, connector);
+       if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
+               output->base.connection_internal = 1;
 
-       output->base.origin = output->base.current;
+       output->base.start_repaint_loop = drm_output_start_repaint_loop;
        output->base.repaint = drm_output_repaint;
        output->base.destroy = drm_output_destroy;
        output->base.assign_planes = drm_assign_planes;
        output->base.set_dpms = drm_set_dpms;
        output->base.switch_mode = drm_output_switch_mode;
 
-       weston_plane_init(&output->cursor_plane, 0, 0);
-       weston_plane_init(&output->fb_plane, 0, 0);
+       output->base.gamma_size = output->original_crtc->gamma_size;
+       output->base.set_gamma = drm_output_set_gamma;
+
+       weston_plane_init(&output->cursor_plane, &ec->base, 0, 0);
+       weston_plane_init(&output->fb_plane, &ec->base, 0, 0);
+
+       weston_compositor_stack_plane(&ec->base, &output->cursor_plane, NULL);
+       weston_compositor_stack_plane(&ec->base, &output->fb_plane,
+                                     &ec->base.primary_plane);
 
        weston_log("Output %s, (connector %d, crtc %d)\n",
-                  output->name, output->connector_id, output->crtc_id);
+                  output->base.name, output->connector_id, output->crtc_id);
        wl_list_for_each(m, &output->base.mode_list, link)
-               weston_log_continue("  mode %dx%d@%.1f%s%s%s\n",
+               weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
                                    m->width, m->height, m->refresh / 1000.0,
                                    m->flags & WL_OUTPUT_MODE_PREFERRED ?
                                    ", preferred" : "",
@@ -1665,7 +2170,6 @@ err_free:
        drmModeFreeCrtc(output->original_crtc);
        ec->crtc_allocator &= ~(1 << output->crtc_id);
        ec->connector_allocator &= ~(1 << output->connector_id);
-       free(output->name);
        free(output);
 
        return -1;
@@ -1691,17 +2195,15 @@ create_sprites(struct drm_compositor *ec)
                if (!plane)
                        continue;
 
-               sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
+               sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
                                                   plane->count_formats));
                if (!sprite) {
                        weston_log("%s: out of memory\n",
                                __func__);
-                       free(plane);
+                       drmModeFreePlane(plane);
                        continue;
                }
 
-               memset(sprite, 0, sizeof *sprite);
-
                sprite->possible_crtcs = plane->possible_crtcs;
                sprite->plane_id = plane->plane_id;
                sprite->current = NULL;
@@ -1711,13 +2213,14 @@ create_sprites(struct drm_compositor *ec)
                memcpy(sprite->formats, plane->formats,
                       plane->count_formats * sizeof(plane->formats[0]));
                drmModeFreePlane(plane);
-               weston_plane_init(&sprite->plane, 0, 0);
+               weston_plane_init(&sprite->plane, &ec->base, 0, 0);
+               weston_compositor_stack_plane(&ec->base, &sprite->plane,
+                                             &ec->base.primary_plane);
 
                wl_list_insert(&ec->sprite_list, &sprite->link);
        }
 
-       free(plane_res->planes);
-       free(plane_res);
+       drmModeFreePlaneResources(plane_res);
 }
 
 static void
@@ -1812,7 +2315,6 @@ update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
        drmModeRes *resources;
        struct drm_output *output, *next;
        int x = 0, y = 0;
-       int x_offset = 0, y_offset = 0;
        uint32_t connected = 0, disconnects = 0;
        int i;
 
@@ -1862,17 +2364,10 @@ update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
        if (disconnects) {
                wl_list_for_each_safe(output, next, &ec->base.output_list,
                                      base.link) {
-                       if (x_offset != 0 || y_offset != 0) {
-                               weston_output_move(&output->base,
-                                                output->base.x - x_offset,
-                                                output->base.y - y_offset);
-                       }
-
                        if (disconnects & (1 << output->connector_id)) {
                                disconnects &= ~(1 << output->connector_id);
                                weston_log("connector %d disconnected\n",
                                       output->connector_id);
-                               x_offset += output->base.width;
                                drm_output_destroy(&output->base);
                        }
                }
@@ -1919,49 +2414,28 @@ udev_drm_event(int fd, uint32_t mask, void *data)
 static void
 drm_restore(struct weston_compositor *ec)
 {
-       struct drm_compositor *d = (struct drm_compositor *) ec;
-
-       if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
-               weston_log("failed to drop master: %m\n");
-       tty_reset(d->tty);
-}
-
-static void
-drm_free_configured_output(struct drm_configured_output *output)
-{
-       free(output->name);
-       free(output->mode);
-       free(output);
+       weston_launcher_restore(ec->launcher);
 }
 
 static void
 drm_destroy(struct weston_compositor *ec)
 {
        struct drm_compositor *d = (struct drm_compositor *) ec;
-       struct udev_seat *seat, *next;
-       struct drm_configured_output *o, *n;
-
-       wl_list_for_each_safe(seat, next, &ec->seat_list, base.link)
-               udev_seat_destroy(seat);
-       wl_list_for_each_safe(o, n, &configured_output_list, link)
-               drm_free_configured_output(o);
 
        wl_event_source_remove(d->udev_drm_source);
        wl_event_source_remove(d->drm_source);
 
-       weston_compositor_shutdown(ec);
-
-       ec->renderer->destroy(ec);
-
        destroy_sprites(d);
 
+       weston_compositor_shutdown(ec);
+
        if (d->gbm)
                gbm_device_destroy(d->gbm);
 
-       if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
-               weston_log("failed to drop master: %m\n");
-       tty_destroy(d->tty);
+       weston_launcher_destroy(d->base.launcher);
 
+       close(d->drm.fd);
+       free (d->main_seat);
        free(d);
 }
 
@@ -1983,7 +2457,7 @@ drm_compositor_set_modes(struct drm_compositor *compositor)
                        continue;
                }
 
-               drm_mode = (struct drm_mode *) output->base.current;
+               drm_mode = (struct drm_mode *) output->base.current_mode;
                ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
                                     output->current->fb_id, 0, 0,
                                     &output->connector_id, 1,
@@ -1998,40 +2472,38 @@ drm_compositor_set_modes(struct drm_compositor *compositor)
 }
 
 static void
-vt_func(struct weston_compositor *compositor, int event)
+session_notify(struct wl_listener *listener, void *data)
 {
-       struct drm_compositor *ec = (struct drm_compositor *) compositor;
-       struct udev_seat *seat;
+       struct weston_compositor *compositor = data;
+       struct drm_compositor *ec = data;
        struct drm_sprite *sprite;
        struct drm_output *output;
+       struct udev_seat *useat;
+       struct weston_seat *seat, *next;
 
-       switch (event) {
-       case TTY_ENTER_VT:
-               weston_log("entering VT\n");
-               compositor->focus = 1;
-               if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) {
-                       weston_log("failed to set master: %m\n");
-                       wl_display_terminate(compositor->wl_display);
-               }
+       if (ec->base.session_active) {
+               weston_log("activating session\n");
                compositor->state = ec->prev_state;
                drm_compositor_set_modes(ec);
                weston_compositor_damage_all(compositor);
-               wl_list_for_each(seat, &compositor->seat_list, base.link)
-                       udev_seat_enable(seat, ec->udev);
-               break;
-       case TTY_LEAVE_VT:
-               weston_log("leaving VT\n");
-               wl_list_for_each(seat, &compositor->seat_list, base.link)
-                       udev_seat_disable(seat);
+               wl_list_for_each_safe(seat, next, &ec->base.seat_list, link) {
+                   useat = container_of(seat, struct udev_seat, base);
+                   udev_input_enable(&useat->input);
+               }
+       } else {
+               weston_log("deactivating session\n");
+               wl_list_for_each_safe(seat, next, &ec->base.seat_list, link) {
+                   useat = container_of(seat, struct udev_seat, base);
+                   udev_input_disable(&useat->input);
+               }
 
-               compositor->focus = 0;
                ec->prev_state = compositor->state;
-               compositor->state = WESTON_COMPOSITOR_SLEEPING;
+               weston_compositor_offscreen(compositor);
 
                /* If we have a repaint scheduled (either from a
                 * pending pageflip or the idle handler), make sure we
                 * cancel that so we don't try to pageflip when we're
-                * vt switched away.  The SLEEPING state will prevent
+                * vt switched away.  The OFFSCREEN state will prevent
                 * further attemps at repainting.  When we switch
                 * back, we schedule a repaint, which will process
                 * pending frame callbacks. */
@@ -2049,20 +2521,15 @@ vt_func(struct weston_compositor *compositor, int event)
                                        sprite->plane_id,
                                        output->crtc_id, 0, 0,
                                        0, 0, 0, 0, 0, 0, 0, 0);
-
-               if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
-                       weston_log("failed to drop master: %m\n");
-
-               break;
        };
 }
 
 static void
-switch_vt_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
+switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
 {
-       struct drm_compositor *ec = data;
+       struct weston_compositor *compositor = data;
 
-       tty_activate_vt(ec->tty, key - KEY_F1 + 1);
+       weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
 }
 
 /*
@@ -2122,7 +2589,7 @@ find_primary_gpu(struct drm_compositor *ec, const char *seat)
 }
 
 static void
-planes_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
+planes_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
 {
        struct drm_compositor *c = data;
 
@@ -2141,54 +2608,240 @@ planes_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
        }
 }
 
+#ifdef BUILD_VAAPI_RECORDER
+static void
+recorder_destroy(struct drm_output *output)
+{
+       vaapi_recorder_destroy(output->recorder);
+       output->recorder = NULL;
+
+       output->base.disable_planes--;
+
+       wl_list_remove(&output->recorder_frame_listener.link);
+       weston_log("[libva recorder] done\n");
+}
+
+static void
+recorder_frame_notify(struct wl_listener *listener, void *data)
+{
+       struct drm_output *output;
+       struct drm_compositor *c;
+       int fd, ret;
+
+       output = container_of(listener, struct drm_output,
+                             recorder_frame_listener);
+       c = (struct drm_compositor *) output->base.compositor;
+
+       if (!output->recorder)
+               return;
+
+       ret = drmPrimeHandleToFD(c->drm.fd, output->current->handle,
+                                DRM_CLOEXEC, &fd);
+       if (ret) {
+               weston_log("[libva recorder] "
+                          "failed to create prime fd for front buffer\n");
+               return;
+       }
+
+       ret = vaapi_recorder_frame(output->recorder, fd,
+                                  output->current->stride);
+       if (ret < 0) {
+               weston_log("[libva recorder] aborted: %m\n");
+               recorder_destroy(output);
+       }
+}
+
+static void *
+create_recorder(struct drm_compositor *c, int width, int height,
+               const char *filename)
+{
+       int fd;
+       drm_magic_t magic;
+
+       fd = open(c->drm.filename, O_RDWR | O_CLOEXEC);
+       if (fd < 0)
+               return NULL;
+
+       drmGetMagic(fd, &magic);
+       drmAuthMagic(c->drm.fd, magic);
+
+       return vaapi_recorder_create(fd, width, height, filename);
+}
+
+static void
+recorder_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
+                void *data)
+{
+       struct drm_compositor *c = data;
+       struct drm_output *output;
+       int width, height;
+
+       output = container_of(c->base.output_list.next,
+                             struct drm_output, base.link);
+
+       if (!output->recorder) {
+               if (output->format != GBM_FORMAT_XRGB8888) {
+                       weston_log("failed to start vaapi recorder: "
+                                  "output format not supported\n");
+                       return;
+               }
+
+               width = output->base.current_mode->width;
+               height = output->base.current_mode->height;
+
+               output->recorder =
+                       create_recorder(c, width, height, "capture.h264");
+               if (!output->recorder) {
+                       weston_log("failed to create vaapi recorder\n");
+                       return;
+               }
+
+               output->base.disable_planes++;
+
+               output->recorder_frame_listener.notify = recorder_frame_notify;
+               wl_signal_add(&output->base.frame_signal,
+                             &output->recorder_frame_listener);
+
+               weston_output_schedule_repaint(&output->base);
+
+               weston_log("[libva recorder] initialized\n");
+       } else {
+               recorder_destroy(output);
+       }
+}
+#else
+static void
+recorder_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
+                void *data)
+{
+       weston_log("Compiled without libva support\n");
+}
+#endif
+
+static void
+switch_to_gl_renderer(struct drm_compositor *c)
+{
+       struct drm_output *output;
+
+       if (!c->use_pixman)
+               return;
+
+       weston_log("Switching to GL renderer\n");
+
+       c->gbm = create_gbm_device(c->drm.fd);
+       if (!c->gbm) {
+               weston_log("Failed to create gbm device. "
+                          "Aborting renderer switch\n");
+               return;
+       }
+
+       wl_list_for_each(output, &c->base.output_list, base.link)
+               pixman_renderer_output_destroy(&output->base);
+
+       c->base.renderer->destroy(&c->base);
+
+       if (drm_compositor_create_gl_renderer(c) < 0) {
+               gbm_device_destroy(c->gbm);
+               weston_log("Failed to create GL renderer. Quitting.\n");
+               /* FIXME: we need a function to shutdown cleanly */
+               assert(0);
+       }
+
+       wl_list_for_each(output, &c->base.output_list, base.link) {
+               /* Workaround page flip not setting the tiling mode on BYT */
+               output->force_modeset = 1;
+               drm_output_init_egl(output, c);
+       }
+
+       c->use_pixman = 0;
+}
+
+static void
+renderer_switch_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
+                       void *data)
+{
+       struct drm_compositor *c = (struct drm_compositor *) seat->compositor;
+
+       switch_to_gl_renderer(c);
+}
+
+static int
+create_seats(struct drm_compositor *ec, int connector,
+               struct udev_device *drm_device)
+{
+       struct udev_seat *seat = udev_seat_get_named(&ec->base, ec->main_seat);
+       if (seat && udev_input_init(&seat->input, &ec->base,
+           ec->udev, ec->main_seat) < 0) {
+               weston_log("failed to create input devices\n");
+               return -1;
+       }
+
+       if (create_outputs(ec, connector, drm_device) < 0)
+               return -1;
+
+       return 0;
+}
+
 static struct weston_compositor *
 drm_compositor_create(struct wl_display *display,
-                     int connector, const char *seat, int tty, int pixman,
-                     int *argc, char *argv[], const char *config_file)
+                     struct drm_parameters *param,
+                     int *argc, char *argv[],
+                     struct weston_config *config)
 {
        struct drm_compositor *ec;
+       struct weston_config_section *section;
        struct udev_device *drm_device;
        struct wl_event_loop *loop;
-       struct udev_seat *udev_seat, *next;
        const char *path;
        uint32_t key;
 
        weston_log("initializing drm backend\n");
 
-       ec = malloc(sizeof *ec);
+       ec = zalloc(sizeof *ec);
        if (ec == NULL)
                return NULL;
-       memset(ec, 0, sizeof *ec);
 
        /* KMS support for sprites is not complete yet, so disable the
         * functionality for now. */
        ec->sprites_are_broken = 1;
 
-       ec->use_pixman = pixman;
+       section = weston_config_get_section(config, "core", NULL, NULL);
+       if (get_gbm_format_from_section(section,
+                                       GBM_FORMAT_XRGB8888,
+                                       &ec->format) == -1)
+               goto err_base;
+
+       ec->use_pixman = param->use_pixman;
 
        if (weston_compositor_init(&ec->base, display, argc, argv,
-                                  config_file) < 0) {
+                                  config) < 0) {
                weston_log("%s failed\n", __func__);
                goto err_base;
        }
 
+       /* Check if we run drm-backend using weston-launch */
+       ec->base.launcher = weston_launcher_connect(&ec->base, param->tty,
+                                                   param->seat_id);
+       if (ec->base.launcher == NULL) {
+               weston_log("fatal: drm backend should be run "
+                          "using weston-launch binary or as root\n");
+               goto err_compositor;
+       }
+
        ec->udev = udev_new();
        if (ec->udev == NULL) {
                weston_log("failed to initialize udev context\n");
-               goto err_compositor;
+               goto err_launcher;
        }
 
        ec->base.wl_display = display;
-       ec->tty = tty_create(&ec->base, vt_func, tty);
-       if (!ec->tty) {
-               weston_log("failed to initialize tty\n");
-               goto err_udev;
-       }
+       ec->session_listener.notify = session_notify;
+       wl_signal_add(&ec->base.session_signal, &ec->session_listener);
 
-       drm_device = find_primary_gpu(ec, seat);
+       drm_device = find_primary_gpu(ec, param->seat_id);
        if (drm_device == NULL) {
                weston_log("no drm device found\n");
-               goto err_tty;
+               goto err_udev;
        }
        path = udev_device_get_syspath(drm_device);
 
@@ -2212,8 +2865,6 @@ drm_compositor_create(struct wl_display *display,
        ec->base.destroy = drm_destroy;
        ec->base.restore = drm_restore;
 
-       ec->base.focus = 1;
-
        ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
 
        for (key = KEY_F1; key < KEY_F9; key++)
@@ -2224,17 +2875,18 @@ drm_compositor_create(struct wl_display *display,
        wl_list_init(&ec->sprite_list);
        create_sprites(ec);
 
-       if (create_outputs(ec, connector, drm_device) < 0) {
+       ec->main_seat = strdup(param->seat_id);
+       if (create_seats(ec, param->connector, drm_device) < 0) {
                weston_log("failed to create output for %s\n", path);
                goto err_sprite;
        }
 
-       path = NULL;
+       /* A this point we have some idea of whether or not we have a working
+        * cursor plane. */
+       if (!ec->cursors_are_broken)
+               ec->base.capabilities |= WESTON_CAP_CURSOR_PLANE;
 
-       if (udev_seat_create(&ec->base, ec->udev, seat) == NULL) {
-               weston_log("failed to create input devices\n");
-               goto err_sprite;
-       }
+       path = NULL;
 
        loop = wl_display_get_event_loop(ec->base.wl_display);
        ec->drm_source =
@@ -2266,6 +2918,10 @@ drm_compositor_create(struct wl_display *display,
                                            planes_binding, ec);
        weston_compositor_add_debug_binding(&ec->base, KEY_V,
                                            planes_binding, ec);
+       weston_compositor_add_debug_binding(&ec->base, KEY_Q,
+                                           recorder_binding, ec);
+       weston_compositor_add_debug_binding(&ec->base, KEY_W,
+                                           renderer_switch_binding, ec);
 
        return &ec->base;
 
@@ -2274,204 +2930,41 @@ err_udev_monitor:
        udev_monitor_unref(ec->udev_monitor);
 err_drm_source:
        wl_event_source_remove(ec->drm_source);
-       wl_list_for_each_safe(udev_seat, next, &ec->base.seat_list, base.link)
-               udev_seat_destroy(udev_seat);
 err_sprite:
        ec->base.renderer->destroy(&ec->base);
        gbm_device_destroy(ec->gbm);
        destroy_sprites(ec);
 err_udev_dev:
        udev_device_unref(drm_device);
-err_tty:
-       if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
-               weston_log("failed to drop master: %m\n");
-       tty_destroy(ec->tty);
+err_launcher:
+       weston_launcher_destroy(ec->base.launcher);
 err_udev:
        udev_unref(ec->udev);
 err_compositor:
        weston_compositor_shutdown(&ec->base);
 err_base:
+       free(ec->main_seat);
        free(ec);
        return NULL;
 }
 
-static int
-set_sync_flags(drmModeModeInfo *mode, char *hsync, char *vsync)
-{
-       mode->flags = 0;
-
-       if (strcmp(hsync, "+hsync") == 0)
-               mode->flags |= DRM_MODE_FLAG_PHSYNC;
-       else if (strcmp(hsync, "-hsync") == 0)
-               mode->flags |= DRM_MODE_FLAG_NHSYNC;
-       else
-               return -1;
-
-       if (strcmp(vsync, "+vsync") == 0)
-               mode->flags |= DRM_MODE_FLAG_PVSYNC;
-       else if (strcmp(vsync, "-vsync") == 0)
-               mode->flags |= DRM_MODE_FLAG_NVSYNC;
-       else
-               return -1;
-
-       return 0;
-}
-
-static int
-check_for_modeline(struct drm_configured_output *output)
-{
-       drmModeModeInfo mode;
-       char hsync[16];
-       char vsync[16];
-       char mode_name[16];
-       float fclock;
-
-       mode.type = DRM_MODE_TYPE_USERDEF;
-       mode.hskew = 0;
-       mode.vscan = 0;
-       mode.vrefresh = 0;
-
-       if (sscanf(output_mode, "%f %hd %hd %hd %hd %hd %hd %hd %hd %s %s",
-                                               &fclock, &mode.hdisplay,
-                                               &mode.hsync_start,
-                                               &mode.hsync_end, &mode.htotal,
-                                               &mode.vdisplay,
-                                               &mode.vsync_start,
-                                               &mode.vsync_end, &mode.vtotal,
-                                               hsync, vsync) == 11) {
-               if (set_sync_flags(&mode, hsync, vsync))
-                       return -1;
-
-               sprintf(mode_name, "%dx%d", mode.hdisplay, mode.vdisplay);
-               strcpy(mode.name, mode_name);
-
-               mode.clock = fclock * 1000;
-       } else
-               return -1;
-
-       output->crtc_mode = mode;
-
-       return 0;
-}
-
-static void
-drm_output_set_transform(struct drm_configured_output *output)
-{
-       if (!output_transform) {
-               output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
-               return;
-       }
-
-       if (!strcmp(output_transform, "normal"))
-               output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
-       else if (!strcmp(output_transform, "90"))
-               output->transform = WL_OUTPUT_TRANSFORM_90;
-       else if (!strcmp(output_transform, "180"))
-               output->transform = WL_OUTPUT_TRANSFORM_180;
-       else if (!strcmp(output_transform, "270"))
-               output->transform = WL_OUTPUT_TRANSFORM_270;
-       else if (!strcmp(output_transform, "flipped"))
-               output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
-       else if (!strcmp(output_transform, "flipped-90"))
-               output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
-       else if (!strcmp(output_transform, "flipped-180"))
-               output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
-       else if (!strcmp(output_transform, "flipped-270"))
-               output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
-       else {
-               weston_log("Invalid transform \"%s\" for output %s\n",
-                                               output_transform, output_name);
-               output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
-       }
-
-       free(output_transform);
-       output_transform = NULL;
-}
-
-static void
-output_section_done(void *data)
-{
-       struct drm_configured_output *output;
-
-       output = malloc(sizeof *output);
-
-       if (!output || !output_name || (output_name[0] == 'X') ||
-                                       (!output_mode && !output_transform)) {
-               free(output_name);
-               free(output_mode);
-               free(output_transform);
-               free(output);
-               output_name = NULL;
-               output_mode = NULL;
-               output_transform = NULL;
-               return;
-       }
-
-       output->config = OUTPUT_CONFIG_INVALID;
-       output->name = output_name;
-       output->mode = output_mode;
-
-       if (output_mode) {
-               if (strcmp(output_mode, "off") == 0)
-                       output->config = OUTPUT_CONFIG_OFF;
-               else if (strcmp(output_mode, "preferred") == 0)
-                       output->config = OUTPUT_CONFIG_PREFERRED;
-               else if (strcmp(output_mode, "current") == 0)
-                       output->config = OUTPUT_CONFIG_CURRENT;
-               else if (sscanf(output_mode, "%dx%d",
-                                       &output->width, &output->height) == 2)
-                       output->config = OUTPUT_CONFIG_MODE;
-               else if (check_for_modeline(output) == 0)
-                       output->config = OUTPUT_CONFIG_MODELINE;
-
-               if (output->config == OUTPUT_CONFIG_INVALID)
-                       weston_log("Invalid mode \"%s\" for output %s\n",
-                                                       output_mode, output_name);
-               output_mode = NULL;
-       }
-
-       drm_output_set_transform(output);
-
-       wl_list_insert(&configured_output_list, &output->link);
-
-       if (output_transform)
-               free(output_transform);
-       output_transform = NULL;
-}
-
 WL_EXPORT struct weston_compositor *
 backend_init(struct wl_display *display, int *argc, char *argv[],
-            const char *config_file)
+            struct weston_config *config)
 {
-       int connector = 0, tty = 0, use_pixman = 0;
-       const char *seat = default_seat;
+       struct drm_parameters param = { 0, };
 
        const struct weston_option drm_options[] = {
-               { WESTON_OPTION_INTEGER, "connector", 0, &connector },
-               { WESTON_OPTION_STRING, "seat", 0, &seat },
-               { WESTON_OPTION_INTEGER, "tty", 0, &tty },
+               { WESTON_OPTION_INTEGER, "connector", 0, &param.connector },
+               { WESTON_OPTION_STRING, "seat", 0, &param.seat_id },
+               { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
                { WESTON_OPTION_BOOLEAN, "current-mode", 0, &option_current_mode },
-               { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
+               { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &param.use_pixman },
        };
 
-       parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
+       param.seat_id = default_seat;
 
-       wl_list_init(&configured_output_list);
-
-       const struct config_key drm_config_keys[] = {
-               { "name", CONFIG_KEY_STRING, &output_name },
-               { "mode", CONFIG_KEY_STRING, &output_mode },
-               { "transform", CONFIG_KEY_STRING, &output_transform },
-       };
-
-       const struct config_section config_section[] = {
-               { "output", drm_config_keys,
-               ARRAY_LENGTH(drm_config_keys), output_section_done },
-       };
-
-       parse_config_file(config_file, config_section,
-                               ARRAY_LENGTH(config_section), NULL);
+       parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
 
-       return drm_compositor_create(display, connector, seat, tty, use_pixman,
-                                    argc, argv, config_file);
+       return drm_compositor_create(display, &param, argc, argv, config);
 }