x11: Clear X11 selection when we can't bridge the data type
[profile/ivi/weston-ivi-shell.git] / src / compositor.c
1 /*
2  * Copyright © 2010-2011 Intel Corporation
3  * Copyright © 2008-2011 Kristian Høgsberg
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and
6  * its documentation for any purpose is hereby granted without fee, provided
7  * that the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of the copyright holders not be used in
10  * advertising or publicity pertaining to distribution of the software
11  * without specific, written prior permission.  The copyright holders make
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied warranty.
14  *
15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  */
23
24 #define _GNU_SOURCE
25
26 #include "config.h"
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <limits.h>
33 #include <stdarg.h>
34 #include <assert.h>
35 #include <sys/ioctl.h>
36 #include <sys/wait.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <math.h>
42 #include <linux/input.h>
43 #include <dlfcn.h>
44 #include <getopt.h>
45 #include <signal.h>
46
47 #include <wayland-server.h>
48 #include "compositor.h"
49
50 static const char *option_socket_name = NULL;
51
52 static struct wl_list child_process_list;
53
54 static int
55 sigchld_handler(int signal_number, void *data)
56 {
57         struct weston_process *p;
58         int status;
59         pid_t pid;
60
61         pid = wait(&status);
62         wl_list_for_each(p, &child_process_list, link) {
63                 if (p->pid == pid)
64                         break;
65         }
66
67         if (&p->link == &child_process_list) {
68                 fprintf(stderr, "unknown child process exited\n");
69                 return 1;
70         }
71
72         wl_list_remove(&p->link);
73         p->cleanup(p, status);
74
75         return 1;
76 }
77
78 WL_EXPORT void
79 weston_watch_process(struct weston_process *process)
80 {
81         wl_list_insert(&child_process_list, &process->link);
82 }
83
84 static void
85 child_client_exec(int sockfd, const char *path)
86 {
87         int clientfd;
88         char s[32];
89         sigset_t allsigs;
90
91         /* do not give our signal mask to the new process */
92         sigfillset(&allsigs);
93         sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
94
95         /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
96          * non-CLOEXEC fd to pass through exec. */
97         clientfd = dup(sockfd);
98         if (clientfd == -1) {
99                 fprintf(stderr, "compositor: dup failed: %m\n");
100                 return;
101         }
102
103         snprintf(s, sizeof s, "%d", clientfd);
104         setenv("WAYLAND_SOCKET", s, 1);
105
106         if (execl(path, path, NULL) < 0)
107                 fprintf(stderr, "compositor: executing '%s' failed: %m\n",
108                         path);
109 }
110
111 WL_EXPORT struct wl_client *
112 weston_client_launch(struct weston_compositor *compositor,
113                      struct weston_process *proc,
114                      const char *path,
115                      weston_process_cleanup_func_t cleanup)
116 {
117         int sv[2];
118         pid_t pid;
119         struct wl_client *client;
120
121         if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
122                 fprintf(stderr, "weston_client_launch: "
123                         "socketpair failed while launching '%s': %m\n",
124                         path);
125                 return NULL;
126         }
127
128         pid = fork();
129         if (pid == -1) {
130                 close(sv[0]);
131                 close(sv[1]);
132                 fprintf(stderr,  "weston_client_launch: "
133                         "fork failed while launching '%s': %m\n", path);
134                 return NULL;
135         }
136
137         if (pid == 0) {
138                 child_client_exec(sv[1], path);
139                 exit(-1);
140         }
141
142         close(sv[1]);
143
144         client = wl_client_create(compositor->wl_display, sv[0]);
145         if (!client) {
146                 close(sv[0]);
147                 fprintf(stderr, "weston_client_launch: "
148                         "wl_client_create failed while launching '%s'.\n",
149                         path);
150                 return NULL;
151         }
152
153         proc->pid = pid;
154         proc->cleanup = cleanup;
155         weston_watch_process(proc);
156
157         return client;
158 }
159
160 static void
161 surface_handle_buffer_destroy(struct wl_listener *listener,
162                               struct wl_resource *resource, uint32_t time)
163 {
164         struct weston_surface *es =
165                 container_of(listener, struct weston_surface, 
166                              buffer_destroy_listener);
167
168         es->buffer = NULL;
169 }
170
171 static void
172 output_handle_scanout_buffer_destroy(struct wl_listener *listener,
173                                      struct wl_resource *resource,
174                                      uint32_t time)
175 {
176         struct weston_output *output =
177                 container_of(listener, struct weston_output,
178                              scanout_buffer_destroy_listener);
179
180         output->scanout_buffer = NULL;
181
182         if (!output->pending_scanout_buffer)
183                 weston_compositor_schedule_repaint(output->compositor);
184 }
185
186 static void
187 output_handle_pending_scanout_buffer_destroy(struct wl_listener *listener,
188                                              struct wl_resource *resource,
189                                              uint32_t time)
190 {
191         struct weston_output *output =
192                 container_of(listener, struct weston_output,
193                              pending_scanout_buffer_destroy_listener);
194
195         output->pending_scanout_buffer = NULL;
196
197         weston_compositor_schedule_repaint(output->compositor);
198 }
199
200
201 WL_EXPORT struct weston_surface *
202 weston_surface_create(struct weston_compositor *compositor,
203                       int32_t x, int32_t y, int32_t width, int32_t height)
204 {
205         struct weston_surface *surface;
206
207         surface = calloc(1, sizeof *surface);
208         if (surface == NULL)
209                 return NULL;
210
211         wl_list_init(&surface->link);
212         wl_list_init(&surface->buffer_link);
213
214         glGenTextures(1, &surface->texture);
215         glBindTexture(GL_TEXTURE_2D, surface->texture);
216         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
217         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
218
219         surface->surface.resource.client = NULL;
220
221         surface->compositor = compositor;
222         surface->visual = WESTON_NONE_VISUAL;
223         surface->image = EGL_NO_IMAGE_KHR;
224         surface->saved_texture = 0;
225         surface->x = x;
226         surface->y = y;
227         surface->width = width;
228         surface->height = height;
229         surface->alpha = 255;
230
231         surface->fullscreen_output = NULL;
232         surface->buffer = NULL;
233         surface->output = NULL;
234
235         pixman_region32_init(&surface->damage);
236         pixman_region32_init(&surface->opaque);
237         wl_list_init(&surface->frame_callback_list);
238
239         surface->buffer_destroy_listener.func = surface_handle_buffer_destroy;
240
241         surface->transform = NULL;
242
243         return surface;
244 }
245
246 WL_EXPORT void
247 weston_surface_damage_rectangle(struct weston_surface *surface,
248                               int32_t x, int32_t y,
249                               int32_t width, int32_t height)
250 {
251         struct weston_compositor *compositor = surface->compositor;
252
253         pixman_region32_union_rect(&surface->damage,
254                                    &surface->damage,
255                                    surface->x + x, surface->y + y,
256                                    width, height);
257         weston_compositor_schedule_repaint(compositor);
258 }
259
260 WL_EXPORT void
261 weston_surface_damage(struct weston_surface *surface)
262 {
263         weston_surface_damage_rectangle(surface, 0, 0,
264                                       surface->width, surface->height);
265 }
266
267 WL_EXPORT void
268 weston_surface_damage_below(struct weston_surface *surface)
269 {
270         struct weston_surface *below;
271
272         if (surface->output == NULL)
273                 return;
274
275         if (surface->link.next == &surface->compositor->surface_list)
276                 return;
277
278         below = container_of(surface->link.next, struct weston_surface, link);
279
280         pixman_region32_union_rect(&below->damage,
281                                    &below->damage,
282                                    surface->x, surface->y,
283                                    surface->width, surface->height);
284         weston_compositor_schedule_repaint(surface->compositor);
285 }
286
287 WL_EXPORT void
288 weston_surface_configure(struct weston_surface *surface,
289                          int x, int y, int width, int height)
290 {
291         weston_surface_damage_below(surface);
292
293         surface->x = x;
294         surface->y = y;
295         surface->width = width;
296         surface->height = height;
297
298         weston_surface_assign_output(surface);
299         weston_surface_damage(surface);
300
301         pixman_region32_fini(&surface->opaque);
302         if (surface->visual == WESTON_RGB_VISUAL)
303                 pixman_region32_init_rect(&surface->opaque,
304                                           surface->x, surface->y,
305                                           surface->width, surface->height);
306         else
307                 pixman_region32_init(&surface->opaque);
308 }
309
310 WL_EXPORT uint32_t
311 weston_compositor_get_time(void)
312 {
313         struct timeval tv;
314
315         gettimeofday(&tv, NULL);
316
317         return tv.tv_sec * 1000 + tv.tv_usec / 1000;
318 }
319
320 WL_EXPORT void
321 weston_compositor_repick(struct weston_compositor *compositor)
322 {
323         struct weston_input_device *device;
324         struct weston_surface *surface;
325         int32_t sx, sy;
326         uint32_t time;
327
328         if (!compositor->focus)
329                 return;
330
331         time = weston_compositor_get_time();
332         wl_list_for_each(device, &compositor->input_device_list, link) {
333                 if (device->input_device.grab)
334                         continue;
335                 surface = pick_surface(&device->input_device, &sx, &sy);
336                 wl_input_device_set_pointer_focus(&device->input_device,
337                                                   &surface->surface,
338                                                   time,
339                                                   device->input_device.x,
340                                                   device->input_device.y,
341                                                   sx, sy);
342         }
343 }
344
345 static void
346 destroy_surface(struct wl_resource *resource)
347 {
348         struct weston_surface *surface =
349                 container_of(resource,
350                              struct weston_surface, surface.resource);
351         struct weston_compositor *compositor = surface->compositor;
352
353         weston_surface_damage_below(surface);
354
355         wl_list_remove(&surface->link);
356         weston_compositor_repick(compositor);
357
358         if (surface->saved_texture == 0)
359                 glDeleteTextures(1, &surface->texture);
360         else
361                 glDeleteTextures(1, &surface->saved_texture);
362
363         if (surface->buffer)
364                 wl_list_remove(&surface->buffer_destroy_listener.link);
365
366         if (surface->image != EGL_NO_IMAGE_KHR)
367                 compositor->destroy_image(compositor->display,
368                                           surface->image);
369
370         wl_list_remove(&surface->buffer_link);
371
372         free(surface);
373 }
374
375 static void
376 weston_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
377 {
378         struct weston_surface *es = (struct weston_surface *) surface;
379         struct weston_compositor *ec = es->compositor;
380         struct wl_list *surfaces_attached_to;
381
382         if (es->saved_texture != 0)
383                 es->texture = es->saved_texture;
384
385         glBindTexture(GL_TEXTURE_2D, es->texture);
386
387         if (wl_buffer_is_shm(buffer)) {
388                 /* Unbind any EGLImage texture that may be bound, so we don't
389                  * overwrite it.*/
390                 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
391                              0, 0, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
392                 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
393                 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
394                              es->pitch, buffer->height, 0,
395                              GL_BGRA_EXT, GL_UNSIGNED_BYTE,
396                              wl_shm_buffer_get_data(buffer));
397
398                 switch (wl_shm_buffer_get_format(buffer)) {
399                 case WL_SHM_FORMAT_ARGB32:
400                         es->visual = WESTON_ARGB_VISUAL;
401                         break;
402                 case WL_SHM_FORMAT_PREMULTIPLIED_ARGB32:
403                         es->visual = WESTON_PREMUL_ARGB_VISUAL;
404                         break;
405                 case WL_SHM_FORMAT_XRGB32:
406                         es->visual = WESTON_RGB_VISUAL;
407                         break;
408                 }
409
410                 surfaces_attached_to = buffer->user_data;
411
412                 wl_list_remove(&es->buffer_link);
413                 wl_list_insert(surfaces_attached_to, &es->buffer_link);
414         } else {
415                 if (es->image != EGL_NO_IMAGE_KHR)
416                         ec->destroy_image(ec->display, es->image);
417                 es->image = ec->create_image(ec->display, NULL,
418                                              EGL_WAYLAND_BUFFER_WL,
419                                              buffer, NULL);
420                 
421                 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
422
423                 /* FIXME: we need to get the visual from the wl_buffer */
424                 es->visual = WESTON_PREMUL_ARGB_VISUAL;
425                 es->pitch = es->width;
426         }
427 }
428
429 static int
430 texture_region(struct weston_surface *es, pixman_region32_t *region)
431 {
432         struct weston_compositor *ec = es->compositor;
433         GLfloat *v, inv_width, inv_height;
434         pixman_box32_t *rectangles;
435         unsigned int *p;
436         int i, n;
437
438         rectangles = pixman_region32_rectangles(region, &n);
439         v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
440         p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
441         inv_width = 1.0 / es->pitch;
442         inv_height = 1.0 / es->height;
443
444         for (i = 0; i < n; i++, v += 16, p += 6) {
445                 v[ 0] = rectangles[i].x1;
446                 v[ 1] = rectangles[i].y1;
447                 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
448                 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
449
450                 v[ 4] = rectangles[i].x1;
451                 v[ 5] = rectangles[i].y2;
452                 v[ 6] = v[ 2];
453                 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
454
455                 v[ 8] = rectangles[i].x2;
456                 v[ 9] = rectangles[i].y1;
457                 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
458                 v[11] = v[ 3];
459
460                 v[12] = rectangles[i].x2;
461                 v[13] = rectangles[i].y2;
462                 v[14] = v[10];
463                 v[15] = v[ 7];
464
465                 p[0] = i * 4 + 0;
466                 p[1] = i * 4 + 1;
467                 p[2] = i * 4 + 2;
468                 p[3] = i * 4 + 2;
469                 p[4] = i * 4 + 1;
470                 p[5] = i * 4 + 3;
471         }
472
473         return n;
474 }
475
476 static void
477 transform_vertex(struct weston_surface *surface,
478                  GLfloat x, GLfloat y, GLfloat u, GLfloat v, GLfloat *r)
479 {
480         struct weston_vector t;
481
482         t.f[0] = x;
483         t.f[1] = y;
484         t.f[2] = 0.0;
485         t.f[3] = 1.0;
486
487         weston_matrix_transform(&surface->transform->matrix, &t);
488
489         r[ 0] = t.f[0];
490         r[ 1] = t.f[1];
491         r[ 2] = u;
492         r[ 3] = v;
493 }
494
495 static int
496 texture_transformed_surface(struct weston_surface *es)
497 {
498         struct weston_compositor *ec = es->compositor;
499         GLfloat *v;
500         unsigned int *p;
501
502         v = wl_array_add(&ec->vertices, 16 * sizeof *v);
503         p = wl_array_add(&ec->indices, 6 * sizeof *p);
504
505         transform_vertex(es, es->x, es->y, 0.0, 0.0, &v[0]);
506         transform_vertex(es, es->x, es->y + es->height, 0.0, 1.0, &v[4]);
507         transform_vertex(es, es->x + es->width, es->y, 1.0, 0.0, &v[8]);
508         transform_vertex(es, es->x + es->width, es->y + es->height,
509                          1.0, 1.0, &v[12]);
510
511         p[0] = 0;
512         p[1] = 1;
513         p[2] = 2;
514         p[3] = 2;
515         p[4] = 1;
516         p[5] = 3;
517
518         return 1;
519 }
520
521 static void
522 weston_surface_draw(struct weston_surface *es,
523                   struct weston_output *output, pixman_region32_t *clip)
524 {
525         struct weston_compositor *ec = es->compositor;
526         GLfloat *v;
527         pixman_region32_t repaint;
528         GLint filter;
529         int n;
530
531         pixman_region32_init_rect(&repaint,
532                                   es->x, es->y, es->width, es->height);
533         pixman_region32_intersect(&repaint, &repaint, clip);
534         if (!pixman_region32_not_empty(&repaint))
535                 return;
536
537         switch (es->visual) {
538         case WESTON_ARGB_VISUAL:
539                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
540                 glEnable(GL_BLEND);
541                 break;
542         case WESTON_PREMUL_ARGB_VISUAL:
543                 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
544                 glEnable(GL_BLEND);
545                 break;
546         case WESTON_RGB_VISUAL:
547                 glDisable(GL_BLEND);
548                 break;
549         default:
550                 fprintf(stderr, "bogus visual\n");
551                 break;
552         }
553
554         if (es->alpha != ec->current_alpha) {
555                 glUniform1f(ec->texture_shader.alpha_uniform,
556                             es->alpha / 255.0);
557                 ec->current_alpha = es->alpha;
558         }
559
560         if (es->transform == NULL) {
561                 filter = GL_NEAREST;
562                 n = texture_region(es, &repaint);
563         } else {
564                 filter = GL_LINEAR;
565                 n = texture_transformed_surface(es);
566         }
567
568         glBindTexture(GL_TEXTURE_2D, es->texture);
569         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
570         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
571
572         v = ec->vertices.data;
573         glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
574         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
575         glEnableVertexAttribArray(0);
576         glEnableVertexAttribArray(1);
577         glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
578
579         ec->vertices.size = 0;
580         ec->indices.size = 0;
581         pixman_region32_fini(&repaint);
582 }
583
584 static void
585 weston_surface_raise(struct weston_surface *surface)
586 {
587         struct weston_compositor *compositor = surface->compositor;
588
589         wl_list_remove(&surface->link);
590         wl_list_insert(&compositor->surface_list, &surface->link);
591         weston_compositor_repick(compositor);
592         weston_surface_damage(surface);
593 }
594
595 WL_EXPORT void
596 weston_compositor_damage_all(struct weston_compositor *compositor)
597 {
598         struct weston_output *output;
599
600         wl_list_for_each(output, &compositor->output_list, link)
601                 weston_output_damage(output);
602 }
603
604 static inline void
605 weston_buffer_post_release(struct wl_buffer *buffer)
606 {
607         if (--buffer->busy_count > 0)
608                 return;
609
610         assert(buffer->resource.client != NULL);
611         wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
612 }
613
614 WL_EXPORT void
615 weston_output_damage(struct weston_output *output)
616 {
617         struct weston_compositor *compositor = output->compositor;
618         struct weston_surface *es;
619
620         if (wl_list_empty(&compositor->surface_list))
621                 return;
622
623         es = container_of(compositor->surface_list.next,
624                           struct weston_surface, link);
625         pixman_region32_union(&es->damage, &es->damage, &output->region);
626         weston_compositor_schedule_repaint(compositor);
627 }
628
629 static void
630 fade_frame(struct weston_animation *animation,
631            struct weston_output *output, uint32_t msecs)
632 {
633         struct weston_compositor *compositor =
634                 container_of(animation,
635                              struct weston_compositor, fade.animation);
636
637         weston_spring_update(&compositor->fade.spring, msecs);
638         if (weston_spring_done(&compositor->fade.spring)) {
639                 compositor->fade.spring.current =
640                         compositor->fade.spring.target;
641                 wl_list_remove(&animation->link);
642                 wl_list_init(&animation->link);
643
644                 if (compositor->fade.spring.current > 0.999) {
645                         compositor->state = WESTON_COMPOSITOR_SLEEPING;
646                         compositor->shell->lock(compositor->shell);
647                 }
648         }
649
650         weston_output_damage(output);
651 }
652
653 static void
654 fade_output(struct weston_output *output,
655             GLfloat tint, pixman_region32_t *region)
656 {
657         struct weston_compositor *compositor = output->compositor;
658         struct weston_surface surface;
659         GLfloat color[4] = { 0.0, 0.0, 0.0, tint };
660
661         surface.compositor = compositor;
662         surface.x = output->x;
663         surface.y = output->y;
664         surface.pitch = output->current->width;
665         surface.width = output->current->width;
666         surface.height = output->current->height;
667         surface.texture = GL_NONE;
668         surface.transform = NULL;
669         surface.alpha = compositor->current_alpha;
670
671         if (tint <= 1.0)
672                 surface.visual = WESTON_PREMUL_ARGB_VISUAL;
673         else
674                 surface.visual = WESTON_RGB_VISUAL;
675
676         glUseProgram(compositor->solid_shader.program);
677         glUniformMatrix4fv(compositor->solid_shader.proj_uniform,
678                            1, GL_FALSE, output->matrix.d);
679         glUniform4fv(compositor->solid_shader.color_uniform, 1, color);
680         weston_surface_draw(&surface, output, region);
681 }
682
683 static void
684 weston_output_set_cursor(struct weston_output *output,
685                          struct wl_input_device *dev, int force_sw)
686 {
687         struct weston_compositor *ec = output->compositor;
688         struct weston_input_device *device =
689                 (struct weston_input_device *) dev;
690         pixman_region32_t cursor_region;
691         int use_hardware_cursor = 1, prior_was_hardware;
692
693         if (device->sprite == NULL)
694                 return;
695
696         pixman_region32_init_rect(&cursor_region,
697                                   device->sprite->x, device->sprite->y,
698                                   device->sprite->width,
699                                   device->sprite->height);
700
701         pixman_region32_intersect(&cursor_region, &cursor_region, &output->region);
702
703         if (!pixman_region32_not_empty(&cursor_region)) {
704                 output->set_hardware_cursor(output, NULL);
705                 goto out;
706         }
707
708         prior_was_hardware = wl_list_empty(&device->sprite->link);
709         if (force_sw || output->set_hardware_cursor(output, device) < 0) {
710                 if (prior_was_hardware) {
711                         weston_surface_damage(device->sprite);
712                         output->set_hardware_cursor(output, NULL);
713                 }
714                 use_hardware_cursor = 0;
715         } else if (!prior_was_hardware) {
716                 weston_surface_damage_below(device->sprite);
717         }
718
719         /* Remove always to be on top. */
720         wl_list_remove(&device->sprite->link);
721         if (!use_hardware_cursor && ec->focus) {
722                 wl_list_insert(&ec->surface_list, &device->sprite->link);
723                 device->sprite->output = output;
724         } else {
725                 wl_list_init(&device->sprite->link);
726                 device->sprite->output = NULL;
727         }
728
729 out:
730         pixman_region32_fini(&cursor_region);
731 }
732
733 static int
734 setup_scanout_surface(struct weston_output *output, struct weston_surface *es)
735 {
736         if (es->visual != WESTON_RGB_VISUAL ||
737             output->prepare_scanout_surface(output, es) != 0)
738                 return -1;
739
740         /* assert output->pending_scanout_buffer == NULL */
741         output->pending_scanout_buffer = es->buffer;
742         output->pending_scanout_buffer->busy_count++;
743
744         wl_list_insert(output->pending_scanout_buffer->resource.destroy_listener_list.prev,
745                        &output->pending_scanout_buffer_destroy_listener.link);
746
747         return 0;
748 }
749
750 static void
751 weston_output_repaint(struct weston_output *output)
752 {
753         struct weston_compositor *ec = output->compositor;
754         struct weston_surface *es;
755         pixman_region32_t opaque, new_damage, total_damage, repaint;
756
757         output->prepare_render(output);
758
759         glViewport(0, 0, output->current->width, output->current->height);
760
761         glUseProgram(ec->texture_shader.program);
762         glUniformMatrix4fv(ec->texture_shader.proj_uniform,
763                            1, GL_FALSE, output->matrix.d);
764         glUniform1i(ec->texture_shader.tex_uniform, 0);
765
766         weston_output_set_cursor(output, ec->input_device,
767                                ec->fade.spring.current >= 0.001);
768
769         pixman_region32_init(&new_damage);
770         pixman_region32_init(&opaque);
771                                 
772         wl_list_for_each(es, &ec->surface_list, link) {
773                 pixman_region32_subtract(&es->damage, &es->damage, &opaque);
774                 pixman_region32_union(&new_damage, &new_damage, &es->damage);
775                 pixman_region32_union(&opaque, &opaque, &es->opaque);
776         }
777
778         pixman_region32_init(&total_damage);
779         pixman_region32_union(&total_damage, &new_damage,
780                               &output->previous_damage);
781         pixman_region32_intersect(&output->previous_damage,
782                                   &new_damage, &output->region);
783
784         pixman_region32_fini(&opaque);
785         pixman_region32_fini(&new_damage);
786
787         es = container_of(ec->surface_list.next, struct weston_surface, link);
788
789         if (setup_scanout_surface(output, es) == 0)
790                 /* We're drawing nothing, just let the damage accumulate */
791                 return;
792
793         if (es->fullscreen_output == output) {
794                 if (es->width < output->current->width ||
795                     es->height < output->current->height)
796                         glClear(GL_COLOR_BUFFER_BIT);
797                 weston_surface_draw(es, output, &total_damage);
798         } else {
799                 wl_list_for_each(es, &ec->surface_list, link) {
800                         pixman_region32_copy(&es->damage, &total_damage);
801                         pixman_region32_subtract(&total_damage, &total_damage, &es->opaque);
802                 }
803
804                 wl_list_for_each_reverse(es, &ec->surface_list, link) {
805                         pixman_region32_init(&repaint);
806                         pixman_region32_intersect(&repaint, &output->region,
807                                                   &es->damage);
808                         weston_surface_draw(es, output, &repaint);
809                         pixman_region32_subtract(&es->damage,
810                                                  &es->damage, &output->region);
811                 }
812         }
813
814         if (ec->fade.spring.current > 0.001)
815                 fade_output(output, ec->fade.spring.current, &total_damage);
816 }
817
818 struct weston_frame_callback {
819         struct wl_resource resource;
820         struct wl_list link;
821 };
822
823 static void
824 repaint(void *data, int msecs)
825 {
826         struct weston_output *output = data;
827         struct weston_compositor *compositor = output->compositor;
828         struct weston_animation *animation, *next;
829         struct weston_frame_callback *cb, *cnext;
830
831         weston_output_repaint(output);
832         output->repaint_needed = 0;
833         output->repaint_scheduled = 1;
834         output->present(output);
835
836         wl_list_for_each_safe(cb, cnext, &output->frame_callback_list, link) {
837                 wl_resource_post_event(&cb->resource, WL_CALLBACK_DONE, msecs);
838                 wl_resource_destroy(&cb->resource, 0);
839         }
840
841         wl_list_for_each_safe(animation, next,
842                               &compositor->animation_list, link)
843                 animation->frame(animation, output, msecs);
844 }
845
846 static void
847 idle_repaint(void *data)
848 {
849         repaint(data, weston_compositor_get_time());
850 }
851
852 WL_EXPORT void
853 weston_output_finish_frame(struct weston_output *output, int msecs)
854 {
855         if (output->scanout_buffer) {
856                 weston_buffer_post_release(output->scanout_buffer);
857                 wl_list_remove(&output->scanout_buffer_destroy_listener.link);
858                 output->scanout_buffer = NULL;
859         }
860
861         if (output->pending_scanout_buffer) {
862                 output->scanout_buffer = output->pending_scanout_buffer;
863                 wl_list_remove(&output->pending_scanout_buffer_destroy_listener.link);
864                 wl_list_insert(output->scanout_buffer->resource.destroy_listener_list.prev,
865                                &output->scanout_buffer_destroy_listener.link);
866                 output->pending_scanout_buffer = NULL;
867         }
868
869         if (output->repaint_needed)
870                 repaint(output, msecs);
871         else
872                 output->repaint_scheduled = 0;
873 }
874
875 WL_EXPORT void
876 weston_compositor_schedule_repaint(struct weston_compositor *compositor)
877 {
878         struct weston_output *output;
879         struct wl_event_loop *loop;
880
881         if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
882                 return;
883
884         loop = wl_display_get_event_loop(compositor->wl_display);
885         wl_list_for_each(output, &compositor->output_list, link) {
886                 output->repaint_needed = 1;
887                 if (output->repaint_scheduled)
888                         continue;
889
890                 wl_event_loop_add_idle(loop, idle_repaint, output);
891                 output->repaint_scheduled = 1;
892         }
893 }
894
895 WL_EXPORT void
896 weston_compositor_fade(struct weston_compositor *compositor, float tint)
897 {
898         int done;
899
900         done = weston_spring_done(&compositor->fade.spring);
901         compositor->fade.spring.target = tint;
902         if (weston_spring_done(&compositor->fade.spring))
903                 return;
904
905         if (done)
906                 compositor->fade.spring.timestamp =
907                         weston_compositor_get_time();
908
909         weston_compositor_damage_all(compositor);
910         if (wl_list_empty(&compositor->fade.animation.link))
911                 wl_list_insert(compositor->animation_list.prev,
912                                &compositor->fade.animation.link);
913 }
914
915 static void
916 surface_destroy(struct wl_client *client, struct wl_resource *resource)
917 {
918         wl_resource_destroy(resource, weston_compositor_get_time());
919 }
920
921 WL_EXPORT void
922 weston_surface_assign_output(struct weston_surface *es)
923 {
924         struct weston_compositor *ec = es->compositor;
925         struct weston_output *output, *new_output;
926         pixman_region32_t region;
927         uint32_t max, area;
928         pixman_box32_t *e;
929
930         new_output = NULL;
931         max = 0;
932         wl_list_for_each(output, &ec->output_list, link) {
933                 pixman_region32_init_rect(&region,
934                                           es->x, es->y, es->width, es->height);
935                 pixman_region32_intersect(&region, &region, &output->region);
936
937                 e = pixman_region32_extents(&region);
938                 area = (e->x2 - e->x1) * (e->y2 - e->y1);
939
940                 if (area >= max) {
941                         new_output = output;
942                         max = area;
943                 }
944         }
945
946         es->output = new_output;
947         if (!wl_list_empty(&es->frame_callback_list)) {
948                 wl_list_insert_list(new_output->frame_callback_list.prev,
949                                     &es->frame_callback_list);
950                 wl_list_init(&es->frame_callback_list);
951         }
952 }
953
954 static void
955 surface_attach(struct wl_client *client,
956                struct wl_resource *resource,
957                struct wl_resource *buffer_resource, int32_t x, int32_t y)
958 {
959         struct weston_surface *es = resource->data;
960         struct weston_shell *shell = es->compositor->shell;
961         struct wl_buffer *buffer = buffer_resource->data;
962
963         if (es->buffer) {
964                 weston_buffer_post_release(es->buffer);
965                 wl_list_remove(&es->buffer_destroy_listener.link);
966         }
967
968         buffer->busy_count++;
969         es->buffer = buffer;
970         wl_list_insert(es->buffer->resource.destroy_listener_list.prev,
971                        &es->buffer_destroy_listener.link);
972
973         if (es->visual == WESTON_NONE_VISUAL) {
974                 shell->map(shell, es, buffer->width, buffer->height);
975         } else if (x != 0 || y != 0 ||
976                    es->width != buffer->width ||
977                    es->height != buffer->height) {
978                 shell->configure(shell, es, es->x + x, es->y + y,
979                                  buffer->width, buffer->height);
980         }
981
982         weston_buffer_attach(buffer, &es->surface);
983 }
984
985 static void
986 surface_damage(struct wl_client *client,
987                struct wl_resource *resource,
988                int32_t x, int32_t y, int32_t width, int32_t height)
989 {
990         struct weston_surface *es = resource->data;
991
992         weston_surface_damage_rectangle(es, x, y, width, height);
993 }
994
995 static void
996 destroy_frame_callback(struct wl_resource *resource)
997 {
998         struct weston_frame_callback *cb = resource->data;
999
1000         wl_list_remove(&cb->link);
1001         free(cb);
1002 }
1003
1004 static void
1005 surface_frame(struct wl_client *client,
1006               struct wl_resource *resource, uint32_t callback)
1007 {
1008         struct weston_frame_callback *cb;
1009         struct weston_surface *es = resource->data;
1010
1011         cb = malloc(sizeof *cb);
1012         if (cb == NULL) {
1013                 wl_resource_post_no_memory(resource);
1014                 return;
1015         }
1016                 
1017         cb->resource.object.interface = &wl_callback_interface;
1018         cb->resource.object.id = callback;
1019         cb->resource.destroy = destroy_frame_callback;
1020         cb->resource.client = client;
1021         cb->resource.data = cb;
1022
1023         wl_client_add_resource(client, &cb->resource);
1024
1025         if (es->output) {
1026                 wl_list_insert(es->output->frame_callback_list.prev,
1027                                &cb->link);
1028         } else {
1029                 wl_list_insert(es->frame_callback_list.prev, &cb->link);
1030         }
1031 }
1032
1033 const static struct wl_surface_interface surface_interface = {
1034         surface_destroy,
1035         surface_attach,
1036         surface_damage,
1037         surface_frame
1038 };
1039
1040 static void
1041 compositor_create_surface(struct wl_client *client,
1042                           struct wl_resource *resource, uint32_t id)
1043 {
1044         struct weston_compositor *ec = resource->data;
1045         struct weston_surface *surface;
1046
1047         surface = weston_surface_create(ec, 0, 0, 0, 0);
1048         if (surface == NULL) {
1049                 wl_resource_post_no_memory(resource);
1050                 return;
1051         }
1052
1053         surface->surface.resource.destroy = destroy_surface;
1054
1055         surface->surface.resource.object.id = id;
1056         surface->surface.resource.object.interface = &wl_surface_interface;
1057         surface->surface.resource.object.implementation =
1058                 (void (**)(void)) &surface_interface;
1059         surface->surface.resource.data = surface;
1060
1061         wl_client_add_resource(client, &surface->surface.resource);
1062 }
1063
1064 const static struct wl_compositor_interface compositor_interface = {
1065         compositor_create_surface,
1066 };
1067
1068 static void
1069 weston_surface_transform(struct weston_surface *surface,
1070                        int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1071 {
1072         *sx = x - surface->x;
1073         *sy = y - surface->y;
1074 }
1075
1076 static struct weston_surface *
1077 weston_compositor_pick_surface(struct weston_compositor *compositor,
1078                                int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1079 {
1080         struct weston_surface *surface;
1081
1082         wl_list_for_each(surface, &compositor->surface_list, link) {
1083                 if (surface->surface.resource.client == NULL)
1084                         continue;
1085                 weston_surface_transform(surface, x, y, sx, sy);
1086                 if (0 <= *sx && *sx < surface->width &&
1087                     0 <= *sy && *sy < surface->height)
1088                         return surface;
1089         }
1090
1091         return NULL;
1092 }
1093
1094 WL_EXPORT struct weston_surface *
1095 pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
1096 {
1097         struct weston_input_device *wd = (struct weston_input_device *) device;
1098
1099         return weston_compositor_pick_surface(wd->compositor,
1100                                             device->x, device->y, sx, sy);
1101 }
1102
1103
1104 static void
1105 implicit_grab_motion(struct wl_grab *grab,
1106                      uint32_t time, int32_t x, int32_t y)
1107 {
1108         struct weston_input_device *device =
1109                 (struct weston_input_device *) grab->input_device;
1110         struct weston_surface *es =
1111                 (struct weston_surface *) device->input_device.pointer_focus;
1112         int32_t sx, sy;
1113         struct wl_resource *resource;
1114
1115         resource = grab->input_device->pointer_focus_resource;
1116         if (resource) {
1117                 weston_surface_transform(es, x, y, &sx, &sy);
1118                 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
1119                                        time, x, y, sx, sy);
1120         }
1121 }
1122
1123 static void
1124 implicit_grab_button(struct wl_grab *grab,
1125                      uint32_t time, int32_t button, int32_t state)
1126 {
1127         struct wl_resource *resource;
1128
1129         resource = grab->input_device->pointer_focus_resource;
1130         if (resource)
1131                 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
1132                                        time, button, state);
1133 }
1134
1135 static void
1136 implicit_grab_end(struct wl_grab *grab, uint32_t time)
1137 {
1138 }
1139
1140 static const struct wl_grab_interface implicit_grab_interface = {
1141         implicit_grab_motion,
1142         implicit_grab_button,
1143         implicit_grab_end
1144 };
1145
1146 WL_EXPORT void
1147 weston_compositor_wake(struct weston_compositor *compositor)
1148 {
1149         compositor->state = WESTON_COMPOSITOR_ACTIVE;
1150         weston_compositor_fade(compositor, 0.0);
1151
1152         wl_event_source_timer_update(compositor->idle_source,
1153                                      compositor->idle_time * 1000);
1154 }
1155
1156 WL_EXPORT void
1157 weston_compositor_activity(struct weston_compositor *compositor)
1158 {
1159         if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1160                 weston_compositor_wake(compositor);
1161         } else {
1162                 compositor->shell->unlock(compositor->shell);
1163         }
1164 }
1165
1166 static void
1167 weston_compositor_idle_inhibit(struct weston_compositor *compositor)
1168 {
1169         weston_compositor_activity(compositor);
1170         compositor->idle_inhibit++;
1171 }
1172
1173 static void
1174 weston_compositor_idle_release(struct weston_compositor *compositor)
1175 {
1176         compositor->idle_inhibit--;
1177         weston_compositor_activity(compositor);
1178 }
1179
1180 static int
1181 idle_handler(void *data)
1182 {
1183         struct weston_compositor *compositor = data;
1184
1185         if (compositor->idle_inhibit)
1186                 return 1;
1187
1188         weston_compositor_fade(compositor, 1.0);
1189
1190         return 1;
1191 }
1192
1193 WL_EXPORT void
1194 notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
1195 {
1196         struct weston_surface *es;
1197         struct weston_output *output;
1198         const struct wl_grab_interface *interface;
1199         struct weston_input_device *wd = (struct weston_input_device *) device;
1200         struct weston_compositor *ec = wd->compositor;
1201         int32_t sx, sy;
1202         int x_valid = 0, y_valid = 0;
1203         int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
1204
1205         weston_compositor_activity(ec);
1206
1207         wl_list_for_each(output, &ec->output_list, link) {
1208                 if (output->x <= x && x <= output->x + output->current->width)
1209                         x_valid = 1;
1210
1211                 if (output->y <= y && y <= output->y + output->current->height)
1212                         y_valid = 1;
1213
1214                 /* FIXME: calculate this only on output addition/deletion */
1215                 if (output->x < min_x)
1216                         min_x = output->x;
1217                 if (output->y < min_y)
1218                         min_y = output->y;
1219
1220                 if (output->x + output->current->width > max_x)
1221                         max_x = output->x + output->current->width;
1222                 if (output->y + output->current->height > max_y)
1223                         max_y = output->y + output->current->height;
1224         }
1225         
1226         if (!x_valid) {
1227                 if (x < min_x)
1228                         x = min_x;
1229                 else if (x >= max_x)
1230                         x = max_x;
1231         }
1232         if (!y_valid) {
1233                 if (y < min_y)
1234                         y = min_y;
1235                 else  if (y >= max_y)
1236                         y = max_y;
1237         }
1238
1239         device->x = x;
1240         device->y = y;
1241
1242         if (device->grab) {
1243                 interface = device->grab->interface;
1244                 interface->motion(device->grab, time, x, y);
1245         } else {
1246                 es = pick_surface(device, &sx, &sy);
1247                 wl_input_device_set_pointer_focus(device,
1248                                                   &es->surface,
1249                                                   time, x, y, sx, sy);
1250                 if (device->pointer_focus_resource)
1251                         wl_resource_post_event(device->pointer_focus_resource,
1252                                                WL_INPUT_DEVICE_MOTION,
1253                                                time, x, y, sx, sy);
1254         }
1255
1256         if (wd->sprite) {
1257                 weston_surface_damage_below(wd->sprite);
1258
1259                 wd->sprite->x = device->x - wd->hotspot_x;
1260                 wd->sprite->y = device->y - wd->hotspot_y;
1261
1262                 weston_surface_damage(wd->sprite);
1263         }
1264 }
1265
1266 WL_EXPORT void
1267 weston_surface_activate(struct weston_surface *surface,
1268                         struct weston_input_device *device, uint32_t time)
1269 {
1270         weston_surface_raise(surface);
1271         wl_input_device_set_keyboard_focus(&device->input_device,
1272                                            &surface->surface, time);
1273         weston_data_device_set_keyboard_focus(device);
1274 }
1275
1276 WL_EXPORT void
1277 notify_button(struct wl_input_device *device,
1278               uint32_t time, int32_t button, int32_t state)
1279 {
1280         struct weston_input_device *wd = (struct weston_input_device *) device;
1281         struct weston_compositor *compositor = wd->compositor;
1282         struct weston_surface *surface =
1283                 (struct weston_surface *) device->pointer_focus;
1284         int32_t sx, sy;
1285
1286         if (state)
1287                 weston_compositor_idle_inhibit(compositor);
1288         else
1289                 weston_compositor_idle_release(compositor);
1290
1291         weston_compositor_run_binding(compositor, wd, time, 0, button, state);
1292
1293         if (state && surface && device->grab == NULL) {
1294                 wl_input_device_start_grab(device,
1295                                            &device->implicit_grab,
1296                                            button, time);
1297         }
1298
1299         if (device->grab)
1300                 device->grab->interface->button(device->grab, time,
1301                                                 button, state);
1302
1303         if (!state && device->grab && device->grab_button == button) {
1304                 wl_input_device_end_grab(device, time);
1305                 surface = pick_surface(device, &sx, &sy);
1306                 wl_input_device_set_pointer_focus(device, &surface->surface,
1307                                                   time, device->x, device->y,
1308                                                   sx, sy);
1309         }
1310 }
1311
1312 static void
1313 update_modifier_state(struct weston_input_device *device,
1314                       uint32_t key, uint32_t state)
1315 {
1316         uint32_t modifier;
1317
1318         switch (key) {
1319         case KEY_LEFTCTRL:
1320         case KEY_RIGHTCTRL:
1321                 modifier = MODIFIER_CTRL;
1322                 break;
1323
1324         case KEY_LEFTALT:
1325         case KEY_RIGHTALT:
1326                 modifier = MODIFIER_ALT;
1327                 break;
1328
1329         case KEY_LEFTMETA:
1330         case KEY_RIGHTMETA:
1331                 modifier = MODIFIER_SUPER;
1332                 break;
1333
1334         default:
1335                 modifier = 0;
1336                 break;
1337         }
1338
1339         if (state)
1340                 device->modifier_state |= modifier;
1341         else
1342                 device->modifier_state &= ~modifier;
1343 }
1344
1345 WL_EXPORT void
1346 notify_key(struct wl_input_device *device,
1347            uint32_t time, uint32_t key, uint32_t state)
1348 {
1349         struct weston_input_device *wd = (struct weston_input_device *) device;
1350         struct weston_compositor *compositor = wd->compositor;
1351         uint32_t *k, *end;
1352
1353         if (state)
1354                 weston_compositor_idle_inhibit(compositor);
1355         else
1356                 weston_compositor_idle_release(compositor);
1357
1358         weston_compositor_run_binding(compositor, wd, time, key, 0, state);
1359
1360         update_modifier_state(wd, key, state);
1361         end = device->keys.data + device->keys.size;
1362         for (k = device->keys.data; k < end; k++) {
1363                 if (*k == key)
1364                         *k = *--end;
1365         }
1366         device->keys.size = (void *) end - device->keys.data;
1367         if (state) {
1368                 k = wl_array_add(&device->keys, sizeof *k);
1369                 *k = key;
1370         }
1371
1372         if (device->keyboard_focus_resource)
1373                 wl_resource_post_event(device->keyboard_focus_resource,
1374                                        WL_INPUT_DEVICE_KEY, time, key, state);
1375 }
1376
1377 WL_EXPORT void
1378 notify_pointer_focus(struct wl_input_device *device,
1379                      uint32_t time, struct weston_output *output,
1380                      int32_t x, int32_t y)
1381 {
1382         struct weston_input_device *wd = (struct weston_input_device *) device;
1383         struct weston_compositor *compositor = wd->compositor;
1384         struct weston_surface *es;
1385         int32_t sx, sy;
1386
1387         if (output) {
1388                 device->x = x;
1389                 device->y = y;
1390                 es = pick_surface(device, &sx, &sy);
1391                 wl_input_device_set_pointer_focus(device,
1392                                                   &es->surface,
1393                                                   time, x, y, sx, sy);
1394
1395                 compositor->focus = 1;
1396         } else {
1397                 wl_input_device_set_pointer_focus(device, NULL,
1398                                                   time, 0, 0, 0, 0);
1399                 compositor->focus = 0;
1400         }
1401 }
1402
1403 WL_EXPORT void
1404 notify_keyboard_focus(struct wl_input_device *device,
1405                       uint32_t time, struct weston_output *output,
1406                       struct wl_array *keys)
1407 {
1408         struct weston_input_device *wd =
1409                 (struct weston_input_device *) device;
1410         struct weston_compositor *compositor = wd->compositor;
1411         struct weston_surface *es;
1412         uint32_t *k, *end;
1413
1414         if (!wl_list_empty(&compositor->surface_list))
1415                 es = container_of(compositor->surface_list.next,
1416                                   struct weston_surface, link);
1417         else
1418                 es = NULL;
1419
1420         if (output) {
1421                 wl_array_copy(&wd->input_device.keys, keys);
1422                 wd->modifier_state = 0;
1423                 end = device->keys.data + device->keys.size;
1424                 for (k = device->keys.data; k < end; k++) {
1425                         weston_compositor_idle_inhibit(compositor);
1426                         update_modifier_state(wd, *k, 1);
1427                 }
1428
1429                 if (es && es->surface.resource.client)
1430                         wl_input_device_set_keyboard_focus(&wd->input_device,
1431                                                            &es->surface, time);
1432         } else {
1433                 end = device->keys.data + device->keys.size;
1434                 for (k = device->keys.data; k < end; k++)
1435                         weston_compositor_idle_release(compositor);
1436
1437                 wd->modifier_state = 0;
1438                 wl_input_device_set_keyboard_focus(&wd->input_device,
1439                                                    NULL, time);
1440         }
1441 }
1442
1443 /* TODO: share this function with wayland-server.c */
1444 static struct wl_resource *
1445 find_resource_for_surface(struct wl_list *list, struct wl_surface *surface)
1446 {
1447         struct wl_resource *r;
1448
1449         if (!surface)
1450                 return NULL;
1451
1452         wl_list_for_each(r, list, link) {
1453                 if (r->client == surface->resource.client)
1454                         return r;
1455         }
1456
1457         return NULL;
1458 }
1459
1460 static void
1461 lose_touch_focus_resource(struct wl_listener *listener,
1462                           struct wl_resource *resource, uint32_t time)
1463 {
1464         struct weston_input_device *device =
1465                 container_of(listener, struct weston_input_device,
1466                              touch_focus_resource_listener);
1467
1468         device->touch_focus_resource = NULL;
1469 }
1470
1471 static void
1472 lose_touch_focus(struct wl_listener *listener,
1473                  struct wl_resource *resource, uint32_t time)
1474 {
1475         struct weston_input_device *device =
1476                 container_of(listener, struct weston_input_device,
1477                              touch_focus_listener);
1478
1479         device->touch_focus = NULL;
1480 }
1481
1482 static void
1483 touch_set_focus(struct weston_input_device *device,
1484                 struct wl_surface *surface, uint32_t time)
1485 {
1486         struct wl_input_device *input_device = &device->input_device;
1487         struct wl_resource *resource;
1488
1489         if (device->touch_focus == surface)
1490                 return;
1491
1492         if (surface) {
1493                 resource =
1494                         find_resource_for_surface(&input_device->resource_list,
1495                                                   surface);
1496                 if (!resource) {
1497                         fprintf(stderr, "couldn't find resource\n");
1498                         return;
1499                 }
1500
1501                 device->touch_focus_resource_listener.func =
1502                         lose_touch_focus_resource;
1503                 wl_list_insert(resource->destroy_listener_list.prev,
1504                                &device->touch_focus_resource_listener.link);
1505                 device->touch_focus_listener.func = lose_touch_focus;
1506                 wl_list_insert(surface->resource.destroy_listener_list.prev,
1507                                &device->touch_focus_listener.link);
1508
1509                 device->touch_focus = surface;
1510                 device->touch_focus_resource = resource;
1511         } else {
1512                 if (device->touch_focus)
1513                         wl_list_remove(&device->touch_focus_listener.link);
1514                 if (device->touch_focus_resource)
1515                         wl_list_remove(&device->touch_focus_resource_listener.link);
1516                 device->touch_focus = NULL;
1517                 device->touch_focus_resource = NULL;
1518         }
1519 }
1520
1521 /**
1522  * notify_touch - emulates button touches and notifies surfaces accordingly.
1523  *
1524  * It assumes always the correct cycle sequence until it gets here: touch_down
1525  * → touch_update → ... → touch_update → touch_end. The driver is responsible
1526  * for sending along such order.
1527  *
1528  */
1529 WL_EXPORT void
1530 notify_touch(struct wl_input_device *device, uint32_t time, int touch_id,
1531              int x, int y, int touch_type)
1532 {
1533         struct weston_input_device *wd = (struct weston_input_device *) device;
1534         struct weston_compositor *ec = wd->compositor;
1535         struct weston_surface *es;
1536         int32_t sx, sy;
1537
1538         switch (touch_type) {
1539         case WL_INPUT_DEVICE_TOUCH_DOWN:
1540                 weston_compositor_idle_inhibit(ec);
1541
1542                 wd->num_tp++;
1543
1544                 /* the first finger down picks the surface, and all further go
1545                  * to that surface for the remainder of the touch session i.e.
1546                  * until all touch points are up again. */
1547                 if (wd->num_tp == 1) {
1548                         es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
1549                         touch_set_focus(wd, &es->surface, time);
1550                 } else if (wd->touch_focus) {
1551                         es = (struct weston_surface *) wd->touch_focus;
1552                         weston_surface_transform(es, x, y, &sx, &sy);
1553                 }
1554
1555                 if (wd->touch_focus_resource && wd->touch_focus)
1556                         wl_resource_post_event(wd->touch_focus_resource,
1557                                                touch_type, time,
1558                                                wd->touch_focus,
1559                                                touch_id, sx, sy);
1560                 break;
1561         case WL_INPUT_DEVICE_TOUCH_MOTION:
1562                 es = (struct weston_surface *) wd->touch_focus;
1563                 if (!es)
1564                         break;
1565
1566                 weston_surface_transform(es, x, y, &sx, &sy);
1567                 if (wd->touch_focus_resource)
1568                         wl_resource_post_event(wd->touch_focus_resource,
1569                                                touch_type, time,
1570                                                touch_id, sx, sy);
1571                 break;
1572         case WL_INPUT_DEVICE_TOUCH_UP:
1573                 weston_compositor_idle_release(ec);
1574                 wd->num_tp--;
1575
1576                 if (wd->touch_focus_resource)
1577                         wl_resource_post_event(wd->touch_focus_resource,
1578                                                touch_type, time, touch_id);
1579                 if (wd->num_tp == 0)
1580                         touch_set_focus(wd, NULL, time);
1581                 break;
1582         }
1583 }
1584
1585 static void
1586 input_device_attach(struct wl_client *client,
1587                     struct wl_resource *resource,
1588                     uint32_t time,
1589                     struct wl_resource *buffer_resource, int32_t x, int32_t y)
1590 {
1591         struct weston_input_device *device = resource->data;
1592         struct weston_compositor *compositor = device->compositor;
1593         struct wl_buffer *buffer;
1594
1595         if (time < device->input_device.pointer_focus_time)
1596                 return;
1597         if (device->input_device.pointer_focus == NULL)
1598                 return;
1599         if (device->input_device.pointer_focus->resource.client != client)
1600                 return;
1601
1602         if (device->sprite)
1603                 weston_surface_damage_below(device->sprite);
1604
1605         if (!buffer_resource) {
1606                 destroy_surface(&device->sprite->surface.resource);
1607                 device->sprite = NULL;
1608                 return;
1609         }
1610
1611         if (!device->sprite) {
1612                 device->sprite =
1613                         weston_surface_create(compositor,
1614                                             device->input_device.x,
1615                                             device->input_device.y, 32, 32);
1616                 wl_list_init(&device->sprite->link);
1617         }
1618
1619         buffer = buffer_resource->data;
1620         weston_buffer_attach(buffer, &device->sprite->surface);
1621
1622         device->hotspot_x = x;
1623         device->hotspot_y = y;
1624         device->sprite->width = buffer->width;
1625         device->sprite->height = buffer->height;
1626         device->sprite->x = device->input_device.x - device->hotspot_x;
1627         device->sprite->y = device->input_device.y - device->hotspot_y;
1628
1629         weston_surface_damage(device->sprite);
1630 }
1631
1632 const static struct wl_input_device_interface input_device_interface = {
1633         input_device_attach,
1634 };
1635
1636 static void unbind_input_device(struct wl_resource *resource)
1637 {
1638         wl_list_remove(&resource->link);
1639         free(resource);
1640 }
1641
1642 static void
1643 bind_input_device(struct wl_client *client,
1644                   void *data, uint32_t version, uint32_t id)
1645 {
1646         struct wl_input_device *device = data;
1647         struct wl_resource *resource;
1648
1649         resource = wl_client_add_object(client, &wl_input_device_interface,
1650                                         &input_device_interface, id, data);
1651         wl_list_insert(&device->resource_list, &resource->link);
1652         resource->destroy = unbind_input_device;
1653 }
1654
1655 WL_EXPORT void
1656 weston_input_device_init(struct weston_input_device *device,
1657                          struct weston_compositor *ec)
1658 {
1659         wl_input_device_init(&device->input_device);
1660         wl_list_init(&device->drag_resource_list);
1661
1662         wl_display_add_global(ec->wl_display, &wl_input_device_interface,
1663                               device, bind_input_device);
1664
1665         device->sprite = NULL;
1666
1667         device->compositor = ec;
1668         device->hotspot_x = 16;
1669         device->hotspot_y = 16;
1670         device->modifier_state = 0;
1671         device->num_tp = 0;
1672
1673         device->input_device.implicit_grab.interface = &implicit_grab_interface;
1674
1675         wl_list_insert(ec->input_device_list.prev, &device->link);
1676
1677         device->selection_data_source = NULL;
1678 }
1679
1680 static void
1681 bind_output(struct wl_client *client,
1682             void *data, uint32_t version, uint32_t id)
1683 {
1684         struct weston_output *output = data;
1685         struct weston_mode *mode;
1686         struct wl_resource *resource;
1687
1688         resource = wl_client_add_object(client,
1689                                         &wl_output_interface, NULL, id, data);
1690
1691         wl_resource_post_event(resource,
1692                                WL_OUTPUT_GEOMETRY,
1693                                output->x,
1694                                output->y,
1695                                output->mm_width,
1696                                output->mm_height,
1697                                output->subpixel,
1698                                output->make, output->model);
1699
1700         wl_list_for_each (mode, &output->mode_list, link) {
1701                 wl_resource_post_event(resource,
1702                                        WL_OUTPUT_MODE,
1703                                        mode->flags,
1704                                        mode->width,
1705                                        mode->height,
1706                                        mode->refresh);
1707         }
1708 }
1709
1710 static const char vertex_shader[] =
1711         "uniform mat4 proj;\n"
1712         "attribute vec2 position;\n"
1713         "attribute vec2 texcoord;\n"
1714         "varying vec2 v_texcoord;\n"
1715         "void main()\n"
1716         "{\n"
1717         "   gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1718         "   v_texcoord = texcoord;\n"
1719         "}\n";
1720
1721 static const char texture_fragment_shader[] =
1722         "precision mediump float;\n"
1723         "varying vec2 v_texcoord;\n"
1724         "uniform sampler2D tex;\n"
1725         "uniform float alpha;\n"
1726         "void main()\n"
1727         "{\n"
1728         "   gl_FragColor = texture2D(tex, v_texcoord)\n;"
1729         "   gl_FragColor = alpha * gl_FragColor;\n"
1730         "}\n";
1731
1732 static const char solid_fragment_shader[] =
1733         "precision mediump float;\n"
1734         "uniform vec4 color;\n"
1735         "void main()\n"
1736         "{\n"
1737         "   gl_FragColor = color\n;"
1738         "}\n";
1739
1740 static int
1741 compile_shader(GLenum type, const char *source)
1742 {
1743         GLuint s;
1744         char msg[512];
1745         GLint status;
1746
1747         s = glCreateShader(type);
1748         glShaderSource(s, 1, &source, NULL);
1749         glCompileShader(s);
1750         glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1751         if (!status) {
1752                 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1753                 fprintf(stderr, "shader info: %s\n", msg);
1754                 return GL_NONE;
1755         }
1756
1757         return s;
1758 }
1759
1760 static int
1761 weston_shader_init(struct weston_shader *shader,
1762                    const char *vertex_source, const char *fragment_source)
1763 {
1764         char msg[512];
1765         GLint status;
1766
1767         shader->vertex_shader =
1768                 compile_shader(GL_VERTEX_SHADER, vertex_source);
1769         shader->fragment_shader =
1770                 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1771
1772         shader->program = glCreateProgram();
1773         glAttachShader(shader->program, shader->vertex_shader);
1774         glAttachShader(shader->program, shader->fragment_shader);
1775         glBindAttribLocation(shader->program, 0, "position");
1776         glBindAttribLocation(shader->program, 1, "texcoord");
1777
1778         glLinkProgram(shader->program);
1779         glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1780         if (!status) {
1781                 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1782                 fprintf(stderr, "link info: %s\n", msg);
1783                 return -1;
1784         }
1785
1786         shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1787         shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
1788         shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
1789
1790         return 0;
1791 }
1792
1793 static int
1794 init_solid_shader(struct weston_shader *shader,
1795                   GLuint vertex_shader, const char *fragment_source)
1796 {
1797         GLint status;
1798         char msg[512];
1799
1800         shader->vertex_shader = vertex_shader;
1801         shader->fragment_shader =
1802                 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1803
1804         shader->program = glCreateProgram();
1805         glAttachShader(shader->program, shader->vertex_shader);
1806         glAttachShader(shader->program, shader->fragment_shader);
1807         glBindAttribLocation(shader->program, 0, "position");
1808         glBindAttribLocation(shader->program, 1, "texcoord");
1809
1810         glLinkProgram(shader->program);
1811         glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1812         if (!status) {
1813                 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1814                 fprintf(stderr, "link info: %s\n", msg);
1815                 return -1;
1816         }
1817  
1818         shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1819         shader->color_uniform = glGetUniformLocation(shader->program, "color");
1820
1821         return 0;
1822 }
1823
1824 WL_EXPORT void
1825 weston_output_destroy(struct weston_output *output)
1826 {
1827         pixman_region32_fini(&output->region);
1828         pixman_region32_fini(&output->previous_damage);
1829 }
1830
1831 WL_EXPORT void
1832 weston_output_move(struct weston_output *output, int x, int y)
1833 {
1834         int flip;
1835
1836         output->x = x;
1837         output->y = y;
1838
1839         pixman_region32_init(&output->previous_damage);
1840         pixman_region32_init_rect(&output->region, x, y, 
1841                                   output->current->width,
1842                                   output->current->height);
1843
1844         weston_matrix_init(&output->matrix);
1845         weston_matrix_translate(&output->matrix,
1846                               -output->x - output->current->width / 2.0,
1847                               -output->y - output->current->height / 2.0, 0);
1848
1849         flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
1850         weston_matrix_scale(&output->matrix,
1851                           2.0 / output->current->width,
1852                           flip * 2.0 / output->current->height, 1);
1853
1854         weston_output_damage(output);
1855 }
1856
1857 WL_EXPORT void
1858 weston_output_init(struct weston_output *output, struct weston_compositor *c,
1859                    int x, int y, int width, int height, uint32_t flags)
1860 {
1861         output->compositor = c;
1862         output->x = x;
1863         output->y = y;
1864         output->mm_width = width;
1865         output->mm_height = height;
1866
1867         output->flags = flags;
1868         weston_output_move(output, x, y);
1869
1870         output->scanout_buffer_destroy_listener.func =
1871                 output_handle_scanout_buffer_destroy;
1872         output->pending_scanout_buffer_destroy_listener.func =
1873                 output_handle_pending_scanout_buffer_destroy;
1874
1875         wl_list_init(&output->frame_callback_list);
1876
1877         wl_display_add_global(c->wl_display,
1878                               &wl_output_interface, output, bind_output);
1879 }
1880
1881 static void
1882 shm_buffer_created(struct wl_buffer *buffer)
1883 {
1884         struct wl_list *surfaces_attached_to;
1885
1886         surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1887         if (!surfaces_attached_to) {
1888                 buffer->user_data = NULL;
1889                 return;
1890         }
1891
1892         wl_list_init(surfaces_attached_to);
1893
1894         buffer->user_data = surfaces_attached_to;
1895 }
1896
1897 static void
1898 shm_buffer_damaged(struct wl_buffer *buffer,
1899                    int32_t x, int32_t y, int32_t width, int32_t height)
1900 {
1901         struct wl_list *surfaces_attached_to = buffer->user_data;
1902         struct weston_surface *es;
1903         GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
1904
1905         wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1906                 glBindTexture(GL_TEXTURE_2D, es->texture);
1907                 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1908                              tex_width, buffer->height, 0,
1909                              GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1910                              wl_shm_buffer_get_data(buffer));
1911                 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1912                  * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1913         }
1914 }
1915
1916 static void
1917 shm_buffer_destroyed(struct wl_buffer *buffer)
1918 {
1919         struct wl_list *surfaces_attached_to = buffer->user_data;
1920         struct weston_surface *es, *next;
1921
1922         wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
1923                 wl_list_remove(&es->buffer_link);
1924                 wl_list_init(&es->buffer_link);
1925         }
1926
1927         free(surfaces_attached_to);
1928 }
1929
1930 const static struct wl_shm_callbacks shm_callbacks = {
1931         shm_buffer_created,
1932         shm_buffer_damaged,
1933         shm_buffer_destroyed
1934 };
1935
1936 static void
1937 compositor_bind(struct wl_client *client,
1938                 void *data, uint32_t version, uint32_t id)
1939 {
1940         struct weston_compositor *compositor = data;
1941
1942         wl_client_add_object(client, &wl_compositor_interface,
1943                              &compositor_interface, id, compositor);
1944 }
1945
1946 WL_EXPORT int
1947 weston_compositor_init(struct weston_compositor *ec, struct wl_display *display)
1948 {
1949         struct wl_event_loop *loop;
1950         const char *extensions;
1951
1952         ec->wl_display = display;
1953
1954         if (!wl_display_add_global(display, &wl_compositor_interface,
1955                                    ec, compositor_bind))
1956                 return -1;
1957
1958         ec->shm = wl_shm_init(display, &shm_callbacks);
1959
1960         ec->image_target_texture_2d =
1961                 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1962         ec->image_target_renderbuffer_storage = (void *)
1963                 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1964         ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1965         ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1966         ec->bind_display =
1967                 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1968         ec->unbind_display =
1969                 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1970
1971         extensions = (const char *) glGetString(GL_EXTENSIONS);
1972         if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1973                 fprintf(stderr,
1974                         "GL_EXT_texture_format_BGRA8888 not available\n");
1975                 return -1;
1976         }
1977
1978         extensions =
1979                 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
1980         if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1981                 ec->has_bind_display = 1;
1982         if (ec->has_bind_display)
1983                 ec->bind_display(ec->display, ec->wl_display);
1984
1985         wl_list_init(&ec->surface_list);
1986         wl_list_init(&ec->input_device_list);
1987         wl_list_init(&ec->output_list);
1988         wl_list_init(&ec->binding_list);
1989         wl_list_init(&ec->animation_list);
1990         weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
1991         ec->fade.animation.frame = fade_frame;
1992         wl_list_init(&ec->fade.animation.link);
1993
1994         screenshooter_create(ec);
1995
1996         weston_data_device_manager_init(ec);
1997
1998         glActiveTexture(GL_TEXTURE0);
1999
2000         if (weston_shader_init(&ec->texture_shader,
2001                              vertex_shader, texture_fragment_shader) < 0)
2002                 return -1;
2003         if (init_solid_shader(&ec->solid_shader,
2004                               ec->texture_shader.vertex_shader,
2005                               solid_fragment_shader) < 0)
2006                 return -1;
2007
2008         loop = wl_display_get_event_loop(ec->wl_display);
2009         ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2010         wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2011
2012         weston_compositor_schedule_repaint(ec);
2013
2014         return 0;
2015 }
2016
2017 WL_EXPORT void
2018 weston_compositor_shutdown(struct weston_compositor *ec)
2019 {
2020         struct weston_output *output, *next;
2021
2022         /* Destroy all outputs associated with this compositor */
2023         wl_list_for_each_safe(output, next, &ec->output_list, link)
2024                 output->destroy(output);
2025 }
2026
2027 static int on_term_signal(int signal_number, void *data)
2028 {
2029         struct wl_display *display = data;
2030
2031         fprintf(stderr, "caught signal %d\n", signal_number);
2032         wl_display_terminate(display);
2033
2034         return 1;
2035 }
2036
2037 static void *
2038 load_module(const char *name, const char *entrypoint, void **handle)
2039 {
2040         char path[PATH_MAX];
2041         void *module, *init;
2042
2043         if (name[0] != '/')
2044                 snprintf(path, sizeof path, MODULEDIR "/%s", name);
2045         else
2046                 snprintf(path, sizeof path, "%s", name);
2047
2048         module = dlopen(path, RTLD_LAZY);
2049         if (!module) {
2050                 fprintf(stderr,
2051                         "failed to load module: %s\n", dlerror());
2052                 return NULL;
2053         }
2054
2055         init = dlsym(module, entrypoint);
2056         if (!init) {
2057                 fprintf(stderr,
2058                         "failed to lookup init function: %s\n", dlerror());
2059                 return NULL;
2060         }
2061
2062         return init;
2063 }
2064
2065 int main(int argc, char *argv[])
2066 {
2067         struct wl_display *display;
2068         struct weston_compositor *ec;
2069         struct wl_event_loop *loop;
2070         int o, xserver = 0;
2071         void *shell_module, *backend_module;
2072         int (*shell_init)(struct weston_compositor *ec);
2073         struct weston_compositor
2074                 *(*backend_init)(struct wl_display *display, char *options);
2075         char *backend = NULL;
2076         char *backend_options = "";
2077         char *shell = NULL;
2078         char *p;
2079         int option_idle_time = 300;
2080
2081         static const char opts[] = "B:b:o:S:i:s:x";
2082         static const struct option longopts[ ] = {
2083                 { "backend", 1, NULL, 'B' },
2084                 { "backend-options", 1, NULL, 'o' },
2085                 { "socket", 1, NULL, 'S' },
2086                 { "idle-time", 1, NULL, 'i' },
2087                 { "shell", 1, NULL, 's' },
2088                 { "xserver", 0, NULL, 'x' },
2089                 { NULL, }
2090         };
2091
2092         while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
2093                 switch (o) {
2094                 case 'B':
2095                         backend = optarg;
2096                         break;
2097                 case 'o':
2098                         backend_options = optarg;
2099                         break;
2100                 case 'S':
2101                         option_socket_name = optarg;
2102                         break;
2103                 case 'i':
2104                         option_idle_time = strtol(optarg, &p, 0);
2105                         if (*p != '\0') {
2106                                 fprintf(stderr,
2107                                         "invalid idle time option: %s\n",
2108                                         optarg);
2109                                 exit(EXIT_FAILURE);
2110                         }
2111                         break;
2112                 case 's':
2113                         shell = optarg;
2114                         break;
2115                 case 'x':
2116                         xserver = 1;
2117                         break;
2118                 }
2119         }
2120
2121         display = wl_display_create();
2122
2123         loop = wl_display_get_event_loop(display);
2124         wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, display);
2125         wl_event_loop_add_signal(loop, SIGINT, on_term_signal, display);
2126         wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal, display);
2127
2128         wl_list_init(&child_process_list);
2129         wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler, NULL);
2130
2131         if (!backend) {
2132                 if (getenv("WAYLAND_DISPLAY"))
2133                         backend = "wayland-backend.so";
2134                 else if (getenv("DISPLAY"))
2135                         backend = "x11-backend.so";
2136                 else if (getenv("OPENWFD"))
2137                         backend = "openwfd-backend.so";
2138                 else
2139                         backend = "drm-backend.so";
2140         }
2141
2142         if (!shell)
2143                 shell = "desktop-shell.so";
2144
2145         backend_init = load_module(backend, "backend_init", &backend_module);
2146         if (!backend_init)
2147                 exit(EXIT_FAILURE);
2148
2149         shell_init = load_module(shell, "shell_init", &shell_module);
2150         if (!shell_init)
2151                 exit(EXIT_FAILURE);
2152
2153         ec = backend_init(display, backend_options);
2154         if (ec == NULL) {
2155                 fprintf(stderr, "failed to create compositor\n");
2156                 exit(EXIT_FAILURE);
2157         }
2158
2159         ec->option_idle_time = option_idle_time;
2160         ec->idle_time = option_idle_time;
2161
2162         if (shell_init(ec) < 0)
2163                 exit(EXIT_FAILURE);
2164
2165         if (xserver)
2166                 weston_xserver_init(ec);
2167
2168         if (wl_display_add_socket(display, option_socket_name)) {
2169                 fprintf(stderr, "failed to add socket: %m\n");
2170                 exit(EXIT_FAILURE);
2171         }
2172
2173         wl_display_run(display);
2174
2175         if (xserver)
2176                 weston_xserver_destroy(ec);
2177
2178         if (ec->has_bind_display)
2179                 ec->unbind_display(ec->display, display);
2180
2181         ec->destroy(ec);
2182         wl_display_destroy(display);
2183
2184         return 0;
2185 }