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