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