screenshooter: Grab pixel data directly before buffer swap
[profile/ivi/weston-ivi-shell.git] / src / compositor.c
index f2ae2f6..2b46fbc 100644 (file)
@@ -59,7 +59,10 @@ sigchld_handler(int signal_number, void *data)
        int status;
        pid_t pid;
 
-       pid = wait(&status);
+       pid = waitpid(-1, &status, WNOHANG);
+       if (!pid)
+               return 1;
+
        wl_list_for_each(p, &child_process_list, link) {
                if (p->pid == pid)
                        break;
@@ -76,6 +79,15 @@ sigchld_handler(int signal_number, void *data)
        return 1;
 }
 
+WL_EXPORT int
+weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode)
+{
+       if (!output->switch_mode)
+               return -1;
+
+       return output->switch_mode(output, mode);
+}
+
 WL_EXPORT void
 weston_watch_process(struct weston_process *process)
 {
@@ -162,8 +174,7 @@ weston_client_launch(struct weston_compositor *compositor,
 }
 
 static void
-surface_handle_buffer_destroy(struct wl_listener *listener,
-                             struct wl_resource *resource, uint32_t time)
+surface_handle_buffer_destroy(struct wl_listener *listener, void *data)
 {
        struct weston_surface *es =
                container_of(listener, struct weston_surface, 
@@ -205,21 +216,22 @@ weston_surface_create(struct weston_compositor *compositor)
        if (surface == NULL)
                return NULL;
 
-       wl_list_init(&surface->surface.resource.destroy_listener_list);
+       wl_signal_init(&surface->surface.resource.destroy_signal);
 
        wl_list_init(&surface->link);
        wl_list_init(&surface->layer_link);
-       wl_list_init(&surface->buffer_link);
 
        surface->surface.resource.client = NULL;
 
        surface->compositor = compositor;
        surface->image = EGL_NO_IMAGE_KHR;
        surface->alpha = 255;
+       surface->brightness = 255;
+       surface->saturation = 255;
+       surface->pitch = 1;
 
        surface->buffer = NULL;
        surface->output = NULL;
-       surface->force_configure = 0;
 
        pixman_region32_init(&surface->damage);
        pixman_region32_init(&surface->opaque);
@@ -228,7 +240,8 @@ weston_surface_create(struct weston_compositor *compositor)
        pixman_region32_init(&surface->transform.opaque);
        wl_list_init(&surface->frame_callback_list);
 
-       surface->buffer_destroy_listener.func = surface_handle_buffer_destroy;
+       surface->buffer_destroy_listener.notify =
+               surface_handle_buffer_destroy;
 
        wl_list_init(&surface->geometry.transformation_list);
        wl_list_insert(&surface->geometry.transformation_list,
@@ -415,21 +428,28 @@ weston_surface_update_transform(struct weston_surface *surface)
        pixman_region32_union(&surface->damage, &surface->damage,
                              &surface->transform.boundingbox);
 
-       if (surface->output)
+       if (weston_surface_is_mapped(surface))
                weston_surface_assign_output(surface);
 
        weston_compositor_schedule_repaint(surface->compositor);
 }
 
 WL_EXPORT void
+weston_surface_to_global_float(struct weston_surface *surface,
+                              int32_t sx, int32_t sy, GLfloat *x, GLfloat *y)
+{
+       weston_surface_update_transform(surface);
+
+       surface_to_global_float(surface, sx, sy, x, y);
+}
+
+WL_EXPORT void
 weston_surface_to_global(struct weston_surface *surface,
                         int32_t sx, int32_t sy, int32_t *x, int32_t *y)
 {
        GLfloat xf, yf;
 
-       weston_surface_update_transform(surface);
-
-       surface_to_global_float(surface, sx, sy, &xf, &yf);
+       weston_surface_to_global_float(surface, sx, sy, &xf, &yf);
        *x = floorf(xf);
        *y = floorf(yf);
 }
@@ -527,14 +547,23 @@ weston_surface_set_position(struct weston_surface *surface,
        surface->geometry.dirty = 1;
 }
 
+WL_EXPORT int
+weston_surface_is_mapped(struct weston_surface *surface)
+{
+       if (surface->output)
+               return 1;
+       else
+               return 0;
+}
+
 WL_EXPORT uint32_t
 weston_compositor_get_time(void)
 {
-       struct timeval tv;
+       struct timeval tv;
 
-       gettimeofday(&tv, NULL);
+       gettimeofday(&tv, NULL);
 
-       return tv.tv_sec * 1000 + tv.tv_usec / 1000;
+       return tv.tv_sec * 1000 + tv.tv_usec / 1000;
 }
 
 static struct weston_surface *
@@ -554,7 +583,7 @@ weston_compositor_pick_surface(struct weston_compositor *compositor,
 }
 
 static void
-weston_device_repick(struct wl_input_device *device, uint32_t time)
+weston_device_repick(struct wl_input_device *device)
 {
        struct weston_input_device *wd = (struct weston_input_device *) device;
        const struct wl_pointer_grab_interface *interface;
@@ -567,7 +596,7 @@ weston_device_repick(struct wl_input_device *device, uint32_t time)
 
        if (&surface->surface != device->current) {
                interface = device->pointer_grab->interface;
-               interface->focus(device->pointer_grab, time, &surface->surface,
+               interface->focus(device->pointer_grab, &surface->surface,
                                 device->current_x, device->current_y);
                device->current = &surface->surface;
        }
@@ -582,14 +611,12 @@ WL_EXPORT void
 weston_compositor_repick(struct weston_compositor *compositor)
 {
        struct weston_input_device *device;
-       uint32_t time;
 
        if (!compositor->focus)
                return;
 
-       time = weston_compositor_get_time();
        wl_list_for_each(device, &compositor->input_device_list, link)
-               weston_device_repick(&device->input_device, time);
+               weston_device_repick(&device->input_device);
 }
 
 static void
@@ -603,11 +630,9 @@ weston_surface_unmap(struct weston_surface *surface)
        wl_list_remove(&surface->layer_link);
 
        if (device->keyboard_focus == &surface->surface)
-               wl_input_device_set_keyboard_focus(device, NULL,
-                                                  weston_compositor_get_time());
+               wl_input_device_set_keyboard_focus(device, NULL);
        if (device->pointer_focus == &surface->surface)
-               wl_input_device_set_pointer_focus(device, NULL, 0, 0,
-                                                 weston_compositor_get_time());
+               wl_input_device_set_pointer_focus(device, NULL, 0, 0);
 
        weston_compositor_schedule_repaint(surface->compositor);
 }
@@ -620,7 +645,7 @@ destroy_surface(struct wl_resource *resource)
                             struct weston_surface, surface.resource);
        struct weston_compositor *compositor = surface->compositor;
 
-       if (surface->output)
+       if (weston_surface_is_mapped(surface))
                weston_surface_unmap(surface);
 
        if (surface->texture)
@@ -633,8 +658,6 @@ destroy_surface(struct wl_resource *resource)
                compositor->destroy_image(compositor->display,
                                          surface->image);
 
-       wl_list_remove(&surface->buffer_link);
-
        pixman_region32_fini(&surface->transform.boundingbox);
        pixman_region32_fini(&surface->damage);
        pixman_region32_fini(&surface->opaque);
@@ -655,11 +678,34 @@ weston_surface_destroy(struct weston_surface *surface)
 }
 
 static void
-weston_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
+weston_surface_attach(struct wl_surface *surface, struct wl_buffer *buffer)
 {
        struct weston_surface *es = (struct weston_surface *) surface;
        struct weston_compositor *ec = es->compositor;
-       struct wl_list *surfaces_attached_to;
+
+       if (es->buffer) {
+               weston_buffer_post_release(es->buffer);
+               wl_list_remove(&es->buffer_destroy_listener.link);
+       }
+
+       es->buffer = buffer;
+
+       if (!buffer) {
+               if (weston_surface_is_mapped(es))
+                       weston_surface_unmap(es);
+               return;
+       }
+
+       buffer->busy_count++;
+       wl_signal_add(&es->buffer->resource.destroy_signal,
+                     &es->buffer_destroy_listener);
+
+       if (es->geometry.width != buffer->width ||
+           es->geometry.height != buffer->height) {
+               undef_region(&es->input);
+               pixman_region32_fini(&es->opaque);
+               pixman_region32_init(&es->opaque);
+       }
 
        if (!es->texture) {
                glGenTextures(1, &es->texture);
@@ -676,24 +722,18 @@ weston_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
        if (wl_buffer_is_shm(buffer)) {
                es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
                glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
-                            es->pitch, buffer->height, 0,
-                            GL_BGRA_EXT, GL_UNSIGNED_BYTE,
-                            wl_shm_buffer_get_data(buffer));
-
-               surfaces_attached_to = buffer->user_data;
-
-               wl_list_remove(&es->buffer_link);
-               wl_list_insert(surfaces_attached_to, &es->buffer_link);
+                            es->pitch, es->buffer->height, 0,
+                            GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
        } else {
                if (es->image != EGL_NO_IMAGE_KHR)
                        ec->destroy_image(ec->display, es->image);
                es->image = ec->create_image(ec->display, NULL,
                                             EGL_WAYLAND_BUFFER_WL,
                                             buffer, NULL);
-               
+
                ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
 
-               es->pitch = es->geometry.width;
+               es->pitch = buffer->width;
        }
 }
 
@@ -784,6 +824,8 @@ weston_surface_draw(struct weston_surface *es, struct weston_output *output,
        glUniform1i(es->shader->tex_uniform, 0);
        glUniform4fv(es->shader->color_uniform, 1, es->color);
        glUniform1f(es->shader->alpha_uniform, es->alpha / 255.0);
+       glUniform1f(es->shader->brightness_uniform, es->brightness / 255.0);
+       glUniform1f(es->shader->saturation_uniform, es->saturation / 255.0);
        glUniform1f(es->shader->texwidth_uniform,
                    (GLfloat)es->geometry.width / es->pitch);
 
@@ -880,7 +922,7 @@ fade_frame(struct weston_animation *animation,
                        compositor->fade.surface = NULL;
                } else if (compositor->fade.spring.current > 0.999) {
                        compositor->state = WESTON_COMPOSITOR_SLEEPING;
-                       compositor->shell->lock(compositor->shell);
+                       wl_signal_emit(&compositor->lock_signal, compositor);
                }
        }
 }
@@ -965,7 +1007,7 @@ weston_output_repaint(struct weston_output *output, int msecs)
 
        wl_list_for_each_safe(cb, cnext, &output->frame_callback_list, link) {
                wl_callback_send_done(&cb->resource, msecs);
-               wl_resource_destroy(&cb->resource, 0);
+               wl_resource_destroy(&cb->resource);
        }
 
        wl_list_for_each_safe(animation, next, &ec->animation_list, link)
@@ -1080,7 +1122,48 @@ weston_compositor_fade(struct weston_compositor *compositor, float tint)
 static void
 surface_destroy(struct wl_client *client, struct wl_resource *resource)
 {
-       wl_resource_destroy(resource, weston_compositor_get_time());
+       wl_resource_destroy(resource);
+}
+
+static struct wl_resource *
+find_resource_for_client(struct wl_list *list, struct wl_client *client)
+{
+        struct wl_resource *r;
+
+        wl_list_for_each(r, list, link) {
+                if (r->client == client)
+                        return r;
+        }
+
+        return NULL;
+}
+
+static void
+weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
+{
+       uint32_t different = es->output_mask ^ mask;
+       uint32_t entered = mask & different;
+       uint32_t left = es->output_mask & different;
+       struct weston_output *output;
+       struct wl_resource *resource;
+       struct wl_client *client = es->surface.resource.client;
+
+       if (es->surface.resource.client == NULL)
+               return;
+       if (different == 0)
+               return;
+
+       es->output_mask = mask;
+       wl_list_for_each(output, &es->compositor->output_list, link) {
+               if (1 << output->id & different)
+                       resource =
+                               find_resource_for_client(&output->resource_list,
+                                                        client);
+               if (1 << output->id & entered)
+                       wl_surface_send_enter(&es->surface.resource, resource);
+               if (1 << output->id & left)
+                       wl_surface_send_leave(&es->surface.resource, resource);
+       }
 }
 
 WL_EXPORT void
@@ -1089,13 +1172,14 @@ weston_surface_assign_output(struct weston_surface *es)
        struct weston_compositor *ec = es->compositor;
        struct weston_output *output, *new_output;
        pixman_region32_t region;
-       uint32_t max, area;
+       uint32_t max, area, mask;
        pixman_box32_t *e;
 
        weston_surface_update_transform(es);
 
        new_output = NULL;
        max = 0;
+       mask = 0;
        pixman_region32_init(&region);
        wl_list_for_each(output, &ec->output_list, link) {
                pixman_region32_intersect(&region, &es->transform.boundingbox,
@@ -1104,6 +1188,9 @@ weston_surface_assign_output(struct weston_surface *es)
                e = pixman_region32_extents(&region);
                area = (e->x2 - e->x1) * (e->y2 - e->y1);
 
+               if (area > 0)
+                       mask |= 1 << output->id;
+
                if (area >= max) {
                        new_output = output;
                        max = area;
@@ -1112,6 +1199,8 @@ weston_surface_assign_output(struct weston_surface *es)
        pixman_region32_fini(&region);
 
        es->output = new_output;
+       weston_surface_update_output_mask(es, mask);
+
        if (!wl_list_empty(&es->frame_callback_list)) {
                wl_list_insert_list(new_output->frame_callback_list.prev,
                                    &es->frame_callback_list);
@@ -1125,54 +1214,42 @@ surface_attach(struct wl_client *client,
               struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
 {
        struct weston_surface *es = resource->data;
-       struct weston_shell *shell = es->compositor->shell;
-       struct wl_buffer *buffer;
+       struct wl_buffer *buffer = NULL;
 
-       if (!buffer_resource && !es->output)
-               return;
+       if (buffer_resource)
+               buffer = buffer_resource->data;
 
-       if (es->buffer) {
-               weston_buffer_post_release(es->buffer);
-               wl_list_remove(&es->buffer_destroy_listener.link);
-       }
+       weston_surface_attach(&es->surface, buffer);
 
-       if (!buffer_resource && es->output) {
-               weston_surface_unmap(es);
-               es->buffer = NULL;
-               return;
-       }
+       if (buffer && es->configure)
+               es->configure(es, sx, sy);
+}
 
-       buffer = buffer_resource->data;
-       buffer->busy_count++;
-       es->buffer = buffer;
-       wl_list_insert(es->buffer->resource.destroy_listener_list.prev,
-                      &es->buffer_destroy_listener.link);
+static void
+texture_set_subimage(struct weston_surface *surface,
+                    int32_t x, int32_t y, int32_t width, int32_t height)
+{
+       glBindTexture(GL_TEXTURE_2D, surface->texture);
 
-       if (es->geometry.width != buffer->width ||
-           es->geometry.height != buffer->height) {
-               undef_region(&es->input);
-               pixman_region32_fini(&es->opaque);
-               pixman_region32_init(&es->opaque);
-       }
+#ifdef GL_UNPACK_ROW_LENGTH
+       /* Mesa does not define GL_EXT_unpack_subimage */
 
-       if (es->output == NULL) {
-               shell->map(shell, es, buffer->width, buffer->height, sx, sy);
-       } else if (es->force_configure || sx != 0 || sy != 0 ||
-                  es->geometry.width != buffer->width ||
-                  es->geometry.height != buffer->height) {
-               GLfloat from_x, from_y;
-               GLfloat to_x, to_y;
+       if (surface->compositor->has_unpack_subimage) {
+               glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
+               glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
+               glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
 
-               surface_to_global_float(es, 0, 0, &from_x, &from_y);
-               surface_to_global_float(es, sx, sy, &to_x, &to_y);
-               shell->configure(shell, es,
-                                es->geometry.x + to_x - from_x,
-                                es->geometry.y + to_y - from_y,
-                                buffer->width, buffer->height);
-               es->force_configure = 0;
+               glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height,
+                               GL_BGRA_EXT, GL_UNSIGNED_BYTE,
+                               wl_shm_buffer_get_data(surface->buffer));
+               return;
        }
+#endif
 
-       weston_buffer_attach(buffer, &es->surface);
+       glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
+                    surface->pitch, surface->buffer->height, 0,
+                    GL_BGRA_EXT, GL_UNSIGNED_BYTE,
+                    wl_shm_buffer_get_data(surface->buffer));
 }
 
 static void
@@ -1183,6 +1260,9 @@ surface_damage(struct wl_client *client,
        struct weston_surface *es = resource->data;
 
        weston_surface_damage_rectangle(es, x, y, width, height);
+
+       if (es->buffer && wl_buffer_is_shm(es->buffer))
+               texture_set_subimage(es, x, y, width, height);
 }
 
 static void
@@ -1271,7 +1351,7 @@ surface_set_input_region(struct wl_client *client,
        weston_compositor_schedule_repaint(surface->compositor);
 }
 
-const static struct wl_surface_interface surface_interface = {
+static const struct wl_surface_interface surface_interface = {
        surface_destroy,
        surface_attach,
        surface_damage,
@@ -1317,7 +1397,7 @@ destroy_region(struct wl_resource *resource)
 static void
 region_destroy(struct wl_client *client, struct wl_resource *resource)
 {
-       wl_resource_destroy(resource, weston_compositor_get_time());
+       wl_resource_destroy(resource);
 }
 
 static void
@@ -1373,7 +1453,7 @@ compositor_create_region(struct wl_client *client,
        wl_client_add_resource(client, &region->resource);
 }
 
-const static struct wl_compositor_interface compositor_interface = {
+static const struct wl_compositor_interface compositor_interface = {
        compositor_create_surface,
        compositor_create_region
 };
@@ -1405,7 +1485,7 @@ weston_compositor_activity(struct weston_compositor *compositor)
                weston_compositor_wake(compositor);
        } else {
                weston_compositor_dpms_on(compositor);
-               compositor->shell->unlock(compositor->shell);
+               wl_signal_emit(&compositor->unlock_signal, compositor);
        }
 }
 
@@ -1495,7 +1575,7 @@ notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
                    pixman_region32_contains_point(&output->region, x, y, NULL))
                        weston_output_update_zoom(output, x, y);
 
-       weston_device_repick(device, time);
+       weston_device_repick(device);
        interface = device->pointer_grab->interface;
        interface->motion(device->pointer_grab, time,
                          device->pointer_grab->x, device->pointer_grab->y);
@@ -1511,11 +1591,15 @@ notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
 
 WL_EXPORT void
 weston_surface_activate(struct weston_surface *surface,
-                       struct weston_input_device *device, uint32_t time)
+                       struct weston_input_device *device)
 {
+       struct weston_compositor *compositor = device->compositor;
+
        wl_input_device_set_keyboard_focus(&device->input_device,
-                                          &surface->surface, time);
+                                          &surface->surface);
        wl_data_device_set_keyboard_focus(&device->input_device);
+
+       wl_signal_emit(&compositor->activate_signal, surface);
 }
 
 WL_EXPORT void
@@ -1524,8 +1608,12 @@ notify_button(struct wl_input_device *device,
 {
        struct weston_input_device *wd = (struct weston_input_device *) device;
        struct weston_compositor *compositor = wd->compositor;
+       struct weston_surface *focus = (struct weston_surface *) device->pointer_focus;
+       uint32_t serial = wl_display_next_serial(compositor->wl_display);
 
        if (state) {
+               if (compositor->ping_handler && focus)
+                       compositor->ping_handler(focus, serial);
                weston_compositor_idle_inhibit(compositor);
                if (device->button_count == 0) {
                        device->grab_button = button;
@@ -1539,10 +1627,38 @@ notify_button(struct wl_input_device *device,
                device->button_count--;
        }
 
-       weston_compositor_run_binding(compositor, wd, time, 0, button, state);
+       weston_compositor_run_binding(compositor, wd, time, 0, button, 0, state);
 
        device->pointer_grab->interface->button(device->pointer_grab, time, button, state);
 
+       if (device->button_count == 1)
+               device->grab_serial =
+                       wl_display_get_serial(compositor->wl_display);
+}
+
+WL_EXPORT void
+notify_axis(struct wl_input_device *device,
+             uint32_t time, uint32_t axis, int32_t value)
+{
+       struct weston_input_device *wd = (struct weston_input_device *) device;
+       struct weston_compositor *compositor = wd->compositor;
+       struct weston_surface *focus = (struct weston_surface *) device->pointer_focus;
+       uint32_t serial = wl_display_next_serial(compositor->wl_display);
+
+       if (compositor->ping_handler && focus)
+               compositor->ping_handler(focus, serial);
+
+       weston_compositor_activity(compositor);
+
+       if (value)
+               weston_compositor_run_binding(compositor, wd,
+                                               time, 0, 0, axis, value);
+       else
+               return;
+
+       if (device->pointer_focus_resource)
+               wl_resource_post_event(device->pointer_focus_resource,
+                               WL_INPUT_DEVICE_AXIS, time, axis, value);
 }
 
 static void
@@ -1584,9 +1700,14 @@ notify_key(struct wl_input_device *device,
 {
        struct weston_input_device *wd = (struct weston_input_device *) device;
        struct weston_compositor *compositor = wd->compositor;
+       struct weston_surface *focus = (struct weston_surface *) device->pointer_focus;
+       uint32_t serial = wl_display_next_serial(compositor->wl_display);
        uint32_t *k, *end;
 
        if (state) {
+               if (compositor->ping_handler && focus)
+                       compositor->ping_handler(focus, serial);
+
                weston_compositor_idle_inhibit(compositor);
                device->grab_key = key;
                device->grab_time = time;
@@ -1608,7 +1729,7 @@ notify_key(struct wl_input_device *device,
 
        if (device->keyboard_grab == &device->default_keyboard_grab)
                weston_compositor_run_binding(compositor, wd,
-                                             time, key, 0, state);
+                                             time, key, 0, 0, state);
 
        device->keyboard_grab->interface->key(device->keyboard_grab,
                                              time, key, state);
@@ -1616,8 +1737,7 @@ notify_key(struct wl_input_device *device,
 
 WL_EXPORT void
 notify_pointer_focus(struct wl_input_device *device,
-                    uint32_t time, struct weston_output *output,
-                    int32_t x, int32_t y)
+                    struct weston_output *output, int32_t x, int32_t y)
 {
        struct weston_input_device *wd = (struct weston_input_device *) device;
        struct weston_compositor *compositor = wd->compositor;
@@ -1636,24 +1756,27 @@ notify_pointer_focus(struct wl_input_device *device,
        }
 }
 
+static void
+destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
+{
+       struct weston_input_device *wd;
+
+       wd = container_of(listener, struct weston_input_device,
+                         saved_kbd_focus_listener);
+
+       wd->saved_kbd_focus = NULL;
+}
+
 WL_EXPORT void
-notify_keyboard_focus(struct wl_input_device *device,
-                     uint32_t time, struct weston_output *output,
-                     struct wl_array *keys)
+notify_keyboard_focus(struct wl_input_device *device, struct wl_array *keys)
 {
        struct weston_input_device *wd =
                (struct weston_input_device *) device;
        struct weston_compositor *compositor = wd->compositor;
-       struct weston_surface *es;
+       struct wl_surface *surface;
        uint32_t *k;
 
-       if (!wl_list_empty(&compositor->surface_list))
-               es = container_of(compositor->surface_list.next,
-                                 struct weston_surface, link);
-       else
-               es = NULL;
-
-       if (output) {
+       if (keys) {
                wl_array_copy(&wd->input_device.keys, keys);
                wd->modifier_state = 0;
                wl_array_for_each(k, &device->keys) {
@@ -1661,39 +1784,40 @@ notify_keyboard_focus(struct wl_input_device *device,
                        update_modifier_state(wd, *k, 1);
                }
 
-               if (es && es->surface.resource.client)
+               surface = wd->saved_kbd_focus;
+
+               if (surface) {
+                       wl_list_remove(&wd->saved_kbd_focus_listener.link);
                        wl_input_device_set_keyboard_focus(&wd->input_device,
-                                                          &es->surface, time);
+                                                          surface);
+                       wd->saved_kbd_focus = NULL;
+               }
        } else {
                wl_array_for_each(k, &device->keys)
                        weston_compositor_idle_release(compositor);
 
                wd->modifier_state = 0;
-               wl_input_device_set_keyboard_focus(&wd->input_device,
-                                                  NULL, time);
-       }
-}
-
-/* TODO: share this function with wayland-server.c */
-static struct wl_resource *
-find_resource_for_surface(struct wl_list *list, struct wl_surface *surface)
-{
-        struct wl_resource *r;
 
-        if (!surface)
-                return NULL;
+               surface = wd->input_device.keyboard_focus;
 
-        wl_list_for_each(r, list, link) {
-                if (r->client == surface->resource.client)
-                        return r;
-        }
+               if (surface) {
+                       wd->saved_kbd_focus = surface;
+                       wd->saved_kbd_focus_listener.notify =
+                               destroy_device_saved_kbd_focus;
+                       wl_signal_add(&surface->resource.destroy_signal,
+                                     &wd->saved_kbd_focus_listener);
+               }
 
-        return NULL;
+               wl_input_device_set_keyboard_focus(&wd->input_device, NULL);
+               /* FIXME: We really need keyboard grab cancel here to
+                * let the grab shut down properly.  As it is we leak
+                * the grab data. */
+               wl_input_device_end_keyboard_grab(&wd->input_device);
+       }
 }
 
 static void
-lose_touch_focus_resource(struct wl_listener *listener,
-                         struct wl_resource *resource, uint32_t time)
+lose_touch_focus_resource(struct wl_listener *listener, void *data)
 {
        struct weston_input_device *device =
                container_of(listener, struct weston_input_device,
@@ -1703,8 +1827,7 @@ lose_touch_focus_resource(struct wl_listener *listener,
 }
 
 static void
-lose_touch_focus(struct wl_listener *listener,
-                struct wl_resource *resource, uint32_t time)
+lose_touch_focus(struct wl_listener *listener, void *data)
 {
        struct weston_input_device *device =
                container_of(listener, struct weston_input_device,
@@ -1715,7 +1838,7 @@ lose_touch_focus(struct wl_listener *listener,
 
 static void
 touch_set_focus(struct weston_input_device *device,
-               struct wl_surface *surface, uint32_t time)
+               struct wl_surface *surface)
 {
        struct wl_input_device *input_device = &device->input_device;
        struct wl_resource *resource;
@@ -1725,20 +1848,20 @@ touch_set_focus(struct weston_input_device *device,
 
        if (surface) {
                resource =
-                       find_resource_for_surface(&input_device->resource_list,
-                                                 surface);
+                       find_resource_for_client(&input_device->resource_list,
+                                                surface->resource.client);
                if (!resource) {
                        fprintf(stderr, "couldn't find resource\n");
                        return;
                }
 
-               device->touch_focus_resource_listener.func =
+               device->touch_focus_resource_listener.notify =
                        lose_touch_focus_resource;
-               wl_list_insert(resource->destroy_listener_list.prev,
-                              &device->touch_focus_resource_listener.link);
-               device->touch_focus_listener.func = lose_touch_focus;
-               wl_list_insert(surface->resource.destroy_listener_list.prev,
-                              &device->touch_focus_listener.link);
+               wl_signal_add(&resource->destroy_signal,
+                             &device->touch_focus_resource_listener);
+               device->touch_focus_listener.notify = lose_touch_focus;
+               wl_signal_add(&surface->resource.destroy_signal,
+                              &device->touch_focus_listener);
 
                device->touch_focus = surface;
                device->touch_focus_resource = resource;
@@ -1768,6 +1891,7 @@ notify_touch(struct wl_input_device *device, uint32_t time, int touch_id,
        struct weston_compositor *ec = wd->compositor;
        struct weston_surface *es;
        int32_t sx, sy;
+       uint32_t serial = 0;
 
        switch (touch_type) {
        case WL_INPUT_DEVICE_TOUCH_DOWN:
@@ -1780,7 +1904,7 @@ notify_touch(struct wl_input_device *device, uint32_t time, int touch_id,
                 * until all touch points are up again. */
                if (wd->num_tp == 1) {
                        es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
-                       touch_set_focus(wd, &es->surface, time);
+                       touch_set_focus(wd, &es->surface);
                } else if (wd->touch_focus) {
                        es = (struct weston_surface *) wd->touch_focus;
                        weston_surface_from_global(es, x, y, &sx, &sy);
@@ -1788,7 +1912,7 @@ notify_touch(struct wl_input_device *device, uint32_t time, int touch_id,
 
                if (wd->touch_focus_resource && wd->touch_focus)
                        wl_input_device_send_touch_down(wd->touch_focus_resource,
-                                                       time, &wd->touch_focus->resource,
+                                                       serial, time, &wd->touch_focus->resource,
                                                        touch_id, sx, sy);
                break;
        case WL_INPUT_DEVICE_TOUCH_MOTION:
@@ -1807,9 +1931,9 @@ notify_touch(struct wl_input_device *device, uint32_t time, int touch_id,
 
                if (wd->touch_focus_resource)
                        wl_input_device_send_touch_up(wd->touch_focus_resource,
-                                                     time, touch_id);
+                                                     serial, time, touch_id);
                if (wd->num_tp == 0)
-                       touch_set_focus(wd, NULL, time);
+                       touch_set_focus(wd, NULL);
                break;
        }
 }
@@ -1817,34 +1941,35 @@ notify_touch(struct wl_input_device *device, uint32_t time, int touch_id,
 static void
 input_device_attach(struct wl_client *client,
                    struct wl_resource *resource,
-                   uint32_t time,
+                   uint32_t serial,
                    struct wl_resource *buffer_resource, int32_t x, int32_t y)
 {
        struct weston_input_device *device = resource->data;
        struct weston_compositor *compositor = device->compositor;
-       struct wl_buffer *buffer;
+       struct wl_buffer *buffer = NULL;
 
-       if (time < device->input_device.pointer_focus_time)
+       if (serial < device->input_device.pointer_focus_serial)
                return;
        if (device->input_device.pointer_focus == NULL)
                return;
        if (device->input_device.pointer_focus->resource.client != client)
                return;
 
-       if (!buffer_resource && device->sprite->output) {
-               wl_list_remove(&device->sprite->link);
-               wl_list_remove(&device->sprite->layer_link);
-               device->sprite->output = NULL;
+       if (buffer_resource)
+               buffer = buffer_resource->data;
+
+       weston_surface_attach(&device->sprite->surface, buffer);
+
+       if (!buffer)
                return;
-       }
 
-       if (!device->sprite->output) {
+       if (!weston_surface_is_mapped(device->sprite)) {
                wl_list_insert(&compositor->cursor_layer.surface_list,
                               &device->sprite->layer_link);
                weston_surface_assign_output(device->sprite);
        }
 
-       buffer = buffer_resource->data;
+
        device->hotspot_x = x;
        device->hotspot_y = y;
        weston_surface_configure(device->sprite,
@@ -1852,17 +1977,16 @@ input_device_attach(struct wl_client *client,
                                 device->input_device.y - device->hotspot_y,
                                 buffer->width, buffer->height);
 
-       weston_buffer_attach(buffer, &device->sprite->surface);
-       weston_surface_damage(device->sprite);
+       surface_damage(NULL, &device->sprite->surface.resource,
+                      0, 0, buffer->width, buffer->height);
 }
 
-const static struct wl_input_device_interface input_device_interface = {
+static const struct wl_input_device_interface input_device_interface = {
        input_device_attach,
 };
 
 static void
-handle_drag_surface_destroy(struct wl_listener *listener,
-                           struct wl_resource *resource, uint32_t time)
+handle_drag_surface_destroy(struct wl_listener *listener, void *data)
 {
        struct weston_input_device *device;
 
@@ -1872,7 +1996,7 @@ handle_drag_surface_destroy(struct wl_listener *listener,
        device->drag_surface = NULL;
 }
 
-static void unbind_input_device(struct wl_resource *resource)
+static void unbind_resource(struct wl_resource *resource)
 {
        wl_list_remove(&resource->link);
        free(resource);
@@ -1888,7 +2012,18 @@ bind_input_device(struct wl_client *client,
        resource = wl_client_add_object(client, &wl_input_device_interface,
                                        &input_device_interface, id, data);
        wl_list_insert(&device->resource_list, &resource->link);
-       resource->destroy = unbind_input_device;
+       resource->destroy = unbind_resource;
+}
+
+static void
+device_handle_new_drag_icon(struct wl_listener *listener, void *data)
+{
+       struct weston_input_device *device;
+
+       device = container_of(listener, struct weston_input_device,
+                             new_drag_icon_listener);
+
+       weston_input_update_drag_surface(&device->input_device, 0, 0);
 }
 
 WL_EXPORT void
@@ -1901,6 +2036,7 @@ weston_input_device_init(struct weston_input_device *device,
                              device, bind_input_device);
 
        device->sprite = weston_surface_create(ec);
+       device->sprite->surface.resource.data = device->sprite;
 
        device->compositor = ec;
        device->hotspot_x = 16;
@@ -1908,9 +2044,14 @@ weston_input_device_init(struct weston_input_device *device,
        device->modifier_state = 0;
        device->num_tp = 0;
 
-       device->drag_surface_destroy_listener.func = handle_drag_surface_destroy;
+       device->drag_surface_destroy_listener.notify =
+               handle_drag_surface_destroy;
 
        wl_list_insert(ec->input_device_list.prev, &device->link);
+
+       device->new_drag_icon_listener.notify = device_handle_new_drag_icon;
+       wl_signal_add(&device->input_device.drag_icon_signal,
+                     &device->new_drag_icon_listener);
 }
 
 WL_EXPORT void
@@ -1926,23 +2067,43 @@ weston_input_device_release(struct weston_input_device *device)
 }
 
 static void
+drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
+{
+       weston_surface_configure(es,
+                                es->geometry.x + sx, es->geometry.y + sy,
+                                es->buffer->width, es->buffer->height);
+}
+
+static int
 device_setup_new_drag_surface(struct weston_input_device *device,
                              struct weston_surface *surface)
 {
        struct wl_input_device *input_device = &device->input_device;
 
+       if (surface->configure) {
+               wl_resource_post_error(&surface->surface.resource,
+                                      WL_DISPLAY_ERROR_INVALID_OBJECT,
+                                      "surface->configure already set");
+               return 0;
+       }
+
        device->drag_surface = surface;
 
        weston_surface_set_position(device->drag_surface,
                                    input_device->x, input_device->y);
 
-       wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
-                      &device->drag_surface_destroy_listener.link);
+       surface->configure = drag_surface_configure;
+
+       wl_signal_add(&surface->surface.resource.destroy_signal,
+                      &device->drag_surface_destroy_listener);
+
+       return 1;
 }
 
 static void
 device_release_drag_surface(struct weston_input_device *device)
 {
+       device->drag_surface->configure = NULL;
        undef_region(&device->drag_surface->input);
        wl_list_remove(&device->drag_surface_destroy_listener.link);
        device->drag_surface = NULL;
@@ -1951,7 +2112,7 @@ device_release_drag_surface(struct weston_input_device *device)
 static void
 device_map_drag_surface(struct weston_input_device *device)
 {
-       if (device->drag_surface->output ||
+       if (weston_surface_is_mapped(device->drag_surface) ||
            !device->drag_surface->buffer)
                return;
 
@@ -1987,7 +2148,8 @@ weston_input_update_drag_surface(struct wl_input_device *input_device,
        if (!device->drag_surface || surface_changed) {
                struct weston_surface *surface = (struct weston_surface *)
                        input_device->drag_surface;
-               device_setup_new_drag_surface(device, surface);
+               if (!device_setup_new_drag_surface(device, surface))
+                       return;
        }
 
        /* the client may not have attached a buffer to the drag surface
@@ -2024,6 +2186,9 @@ bind_output(struct wl_client *client,
        resource = wl_client_add_object(client,
                                        &wl_output_interface, NULL, id, data);
 
+       wl_list_insert(&output->resource_list, &resource->link);
+       resource->destroy = unbind_resource;
+
        wl_output_send_geometry(resource,
                                output->x,
                                output->y,
@@ -2057,6 +2222,8 @@ static const char texture_fragment_shader[] =
        "varying vec2 v_texcoord;\n"
        "uniform sampler2D tex;\n"
        "uniform float alpha;\n"
+       "uniform float bright;\n"
+       "uniform float saturation;\n"
        "uniform float texwidth;\n"
        "void main()\n"
        "{\n"
@@ -2064,15 +2231,20 @@ static const char texture_fragment_shader[] =
        "       v_texcoord.y < 0.0 || v_texcoord.y > 1.0)\n"
        "      discard;\n"
        "   gl_FragColor = texture2D(tex, v_texcoord)\n;"
+       "   float gray = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114));\n"
+       "   vec3 range = (gl_FragColor.rgb - vec3 (gray, gray, gray)) * saturation;\n"
+       "   gl_FragColor = vec4(vec3(gray + range), gl_FragColor.a);\n"
+       "   gl_FragColor = vec4(vec3(bright, bright, bright) * gl_FragColor.rgb, gl_FragColor.a);\n"
        "   gl_FragColor = alpha * gl_FragColor;\n"
        "}\n";
 
 static const char solid_fragment_shader[] =
        "precision mediump float;\n"
        "uniform vec4 color;\n"
+       "uniform float alpha;\n"
        "void main()\n"
        "{\n"
-       "   gl_FragColor = color\n;"
+       "   gl_FragColor = alpha * color\n;"
        "}\n";
 
 static int
@@ -2124,6 +2296,8 @@ weston_shader_init(struct weston_shader *shader,
        shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
        shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
        shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
+       shader->brightness_uniform = glGetUniformLocation(shader->program, "bright");
+       shader->saturation_uniform = glGetUniformLocation(shader->program, "saturation");
        shader->color_uniform = glGetUniformLocation(shader->program, "color");
        shader->texwidth_uniform = glGetUniformLocation(shader->program,
                                                        "texwidth");
@@ -2134,8 +2308,13 @@ weston_shader_init(struct weston_shader *shader,
 WL_EXPORT void
 weston_output_destroy(struct weston_output *output)
 {
+       struct weston_compositor *c = output->compositor;
+
        pixman_region32_fini(&output->region);
        pixman_region32_fini(&output->previous_damage);
+       output->compositor->output_id_pool &= ~(1 << output->id);
+
+       wl_display_remove_global(c->wl_display, output->global);
 }
 
 WL_EXPORT void
@@ -2210,6 +2389,7 @@ weston_output_init(struct weston_output *output, struct weston_compositor *c,
        output->mm_width = width;
        output->mm_height = height;
        output->dirty = 1;
+       wl_list_init(&output->read_pixels_list);
 
        output->zoom.active = 0;
        output->zoom.increment = 0.05;
@@ -2220,68 +2400,33 @@ weston_output_init(struct weston_output *output, struct weston_compositor *c,
 
        output->flags = flags;
        weston_output_move(output, x, y);
+       weston_output_damage(output);
 
        wl_list_init(&output->frame_callback_list);
+       wl_list_init(&output->resource_list);
 
-       wl_display_add_global(c->wl_display,
-                             &wl_output_interface, output, bind_output);
-}
-
-static void
-shm_buffer_created(struct wl_buffer *buffer)
-{
-       struct wl_list *surfaces_attached_to;
-
-       surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
-       if (!surfaces_attached_to) {
-               buffer->user_data = NULL;
-               return;
-       }
-
-       wl_list_init(surfaces_attached_to);
+       output->id = ffs(~output->compositor->output_id_pool) - 1;
+       output->compositor->output_id_pool |= 1 << output->id;
 
-       buffer->user_data = surfaces_attached_to;
+       output->global =
+               wl_display_add_global(c->wl_display, &wl_output_interface,
+                                     output, bind_output);
 }
 
-static void
-shm_buffer_damaged(struct wl_buffer *buffer,
-                  int32_t x, int32_t y, int32_t width, int32_t height)
-{
-       struct wl_list *surfaces_attached_to = buffer->user_data;
-       struct weston_surface *es;
-       GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
-
-       wl_list_for_each(es, surfaces_attached_to, buffer_link) {
-               glBindTexture(GL_TEXTURE_2D, es->texture);
-               glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
-                            tex_width, buffer->height, 0,
-                            GL_BGRA_EXT, GL_UNSIGNED_BYTE,
-                            wl_shm_buffer_get_data(buffer));
-               /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
-                * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
-       }
-}
-
-static void
-shm_buffer_destroyed(struct wl_buffer *buffer)
+WL_EXPORT void
+weston_output_do_read_pixels(struct weston_output *output)
 {
-       struct wl_list *surfaces_attached_to = buffer->user_data;
-       struct weston_surface *es, *next;
+       struct weston_read_pixels *r, *next;
 
-       wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
-               wl_list_remove(&es->buffer_link);
-               wl_list_init(&es->buffer_link);
+       glPixelStorei(GL_PACK_ALIGNMENT, 1);
+       wl_list_for_each_safe(r, next, &output->read_pixels_list, link) {
+               glReadPixels(r->x, r->y, r->width, r->height,
+                            output->compositor->read_format,
+                            GL_UNSIGNED_BYTE, r->data);
+               r->done(r, output);
        }
-
-       free(surfaces_attached_to);
 }
 
-const static struct wl_shm_callbacks shm_callbacks = {
-       shm_buffer_created,
-       shm_buffer_damaged,
-       shm_buffer_destroyed
-};
-
 static void
 compositor_bind(struct wl_client *client,
                void *data, uint32_t version, uint32_t id)
@@ -2299,12 +2444,19 @@ weston_compositor_init(struct weston_compositor *ec, struct wl_display *display)
        const char *extensions;
 
        ec->wl_display = display;
+       wl_signal_init(&ec->destroy_signal);
+       wl_signal_init(&ec->activate_signal);
+       wl_signal_init(&ec->lock_signal);
+       wl_signal_init(&ec->unlock_signal);
+       ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
+
+       ec->output_id_pool = 0;
 
        if (!wl_display_add_global(display, &wl_compositor_interface,
                                   ec, compositor_bind))
                return -1;
 
-       ec->shm = wl_shm_init(display, &shm_callbacks);
+       wl_display_init_shm(display);
 
        ec->image_target_texture_2d =
                (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
@@ -2318,14 +2470,32 @@ weston_compositor_init(struct weston_compositor *ec, struct wl_display *display)
                (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
 
        extensions = (const char *) glGetString(GL_EXTENSIONS);
+       if (!extensions) {
+               fprintf(stderr, "Retrieving GL extension string failed.\n");
+               return -1;
+       }
+
        if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
                fprintf(stderr,
                        "GL_EXT_texture_format_BGRA8888 not available\n");
                return -1;
        }
 
+       if (strstr(extensions, "GL_EXT_read_format_bgra"))
+               ec->read_format = GL_BGRA_EXT;
+       else
+               ec->read_format = GL_RGBA;
+
+       if (strstr(extensions, "GL_EXT_unpack_subimage"))
+               ec->has_unpack_subimage = 1;
+
        extensions =
                (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
+       if (!extensions) {
+               fprintf(stderr, "Retrieving EGL extension string failed.\n");
+               return -1;
+       }
+
        if (strstr(extensions, "EGL_WL_bind_wayland_display"))
                ec->has_bind_display = 1;
        if (ec->has_bind_display)
@@ -2344,7 +2514,9 @@ weston_compositor_init(struct weston_compositor *ec, struct wl_display *display)
        weston_layer_init(&ec->fade_layer, &ec->layer_list);
        weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
 
-       ec->screenshooter = screenshooter_create(ec);
+       screenshooter_create(ec);
+
+       ec->ping_handler = NULL;
 
        wl_data_device_manager_init(ec->wl_display);
 
@@ -2374,9 +2546,8 @@ weston_compositor_shutdown(struct weston_compositor *ec)
        struct weston_output *output, *next;
 
        wl_event_source_remove(ec->idle_source);
-
-       if (ec->screenshooter)
-               screenshooter_destroy(ec->screenshooter);
+       if (ec->input_loop_source)
+               wl_event_source_remove(ec->input_loop_source);
 
        /* Destroy all outputs associated with this compositor */
        wl_list_for_each_safe(output, next, &ec->output_list, link)
@@ -2384,10 +2555,10 @@ weston_compositor_shutdown(struct weston_compositor *ec)
 
        weston_binding_list_destroy_all(&ec->binding_list);
 
-       wl_shm_finish(ec->shm);
-
        wl_array_release(&ec->vertices);
        wl_array_release(&ec->indices);
+
+       wl_event_loop_destroy(ec->input_loop);
 }
 
 static int on_term_signal(int signal_number, void *data)
@@ -2457,8 +2628,9 @@ int main(int argc, char *argv[])
        struct wl_event_source *signals[4];
        struct wl_event_loop *loop;
        struct sigaction segv_action;
-       void *shell_module, *backend_module;
+       void *shell_module, *backend_module, *xserver_module;
        int (*shell_init)(struct weston_compositor *ec);
+       int (*xserver_init)(struct weston_compositor *ec);
        struct weston_compositor
                *(*backend_init)(struct wl_display *display,
                                 int argc, char *argv[]);
@@ -2468,12 +2640,21 @@ int main(int argc, char *argv[])
        int32_t idle_time = 300;
        int32_t xserver;
        char *socket_name = NULL;
+       char *config_file;
+
+       const struct config_key shell_config_keys[] = {
+               { "type", CONFIG_KEY_STRING, &shell },
+       };
+
+       const struct config_section cs[] = {
+               { "shell",
+                 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
+       };
 
        const struct weston_option core_options[] = {
                { WESTON_OPTION_STRING, "backend", 'B', &backend },
                { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
                { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
-               { WESTON_OPTION_STRING, "shell", 's', &shell },
                { WESTON_OPTION_BOOLEAN, "xserver", 0, &xserver },
        };
 
@@ -2510,6 +2691,10 @@ int main(int argc, char *argv[])
                        backend = "drm-backend.so";
        }
 
+       config_file = config_file_path("weston.ini");
+       parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
+       free(config_file);
+
        if (!shell)
                shell = "desktop-shell.so";
 
@@ -2535,10 +2720,13 @@ int main(int argc, char *argv[])
        ec->option_idle_time = idle_time;
        ec->idle_time = idle_time;
 
-#ifdef BUILD_XSERVER_LAUNCHER
+       xserver_init = NULL;
        if (xserver)
-               weston_xserver_init(ec);
-#endif
+               xserver_init = load_module("xserver-launcher.so",
+                                          "weston_xserver_init",
+                                          &xserver_module);
+       if (xserver_init)
+               xserver_init(ec);
 
        if (shell_init(ec) < 0)
                exit(EXIT_FAILURE);
@@ -2556,12 +2744,7 @@ int main(int argc, char *argv[])
        /* prevent further rendering while shutting down */
        ec->state = WESTON_COMPOSITOR_SLEEPING;
 
-#ifdef BUILD_XSERVER_LAUNCHER
-       if (xserver)
-               weston_xserver_destroy(ec);
-#endif
-
-       ec->shell->destroy(ec->shell);
+       wl_signal_emit(&ec->destroy_signal, ec);
 
        if (ec->has_bind_display)
                ec->unbind_display(ec->display, display);