Turn off hardware cursor when screen fade begins.
[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                         output->set_hardware_cursor(output, NULL);
818                 }
819                 use_hardware_cursor = 0;
820         } else if (!prior_was_hardware) {
821                 wlsc_surface_damage_below(device->sprite);
822         }
823
824         /* Remove always to be on top. */
825         wl_list_remove(&device->sprite->link);
826         if (!use_hardware_cursor && ec->focus)
827                 wl_list_insert(&ec->surface_list, &device->sprite->link);
828         else
829                 wl_list_init(&device->sprite->link);
830
831 out:
832         pixman_region32_fini(&cursor_region);
833 }
834
835 static void
836 wlsc_output_repaint(struct wlsc_output *output)
837 {
838         struct wlsc_compositor *ec = output->compositor;
839         struct wlsc_surface *es;
840         pixman_region32_t opaque, new_damage, total_damage, repaint;
841
842         output->prepare_render(output);
843
844         glViewport(0, 0, output->current->width, output->current->height);
845
846         glUseProgram(ec->texture_shader.program);
847         glUniformMatrix4fv(ec->texture_shader.proj_uniform,
848                            1, GL_FALSE, output->matrix.d);
849         glUniform1i(ec->texture_shader.tex_uniform, 0);
850
851         wlsc_output_set_cursor(output, ec->input_device,
852                                ec->fade.spring.current >= 0.001);
853
854         pixman_region32_init(&new_damage);
855         pixman_region32_copy(&new_damage, &ec->damage);
856         pixman_region32_init(&opaque);
857                                 
858         wl_list_for_each(es, &ec->surface_list, link) {
859                 pixman_region32_subtract(&es->damage, &es->damage, &opaque);
860                 pixman_region32_union(&new_damage, &new_damage, &es->damage);
861                 pixman_region32_union(&opaque, &opaque, &es->opaque);
862         }
863
864         pixman_region32_subtract(&ec->damage, &ec->damage, &output->region);
865
866         pixman_region32_init(&total_damage);
867         pixman_region32_union(&total_damage, &new_damage,
868                               &output->previous_damage);
869         pixman_region32_intersect(&output->previous_damage,
870                                   &new_damage, &output->region);
871
872         pixman_region32_fini(&opaque);
873         pixman_region32_fini(&new_damage);
874
875         es = container_of(ec->surface_list.next, struct wlsc_surface, link);
876
877         if (es->visual == WLSC_RGB_VISUAL &&
878             output->prepare_scanout_surface(output, es) == 0) {
879                 /* We're drawing nothing now,
880                  * draw the damaged regions later. */
881                 pixman_region32_union(&ec->damage, &ec->damage, &total_damage);
882
883                 output->scanout_buffer = es->buffer;
884                 output->scanout_buffer->busy_count++;
885
886                 wl_list_remove(&output->scanout_buffer_destroy_listener.link);
887                 wl_list_insert(output->scanout_buffer->resource.destroy_listener_list.prev,
888                                &output->scanout_buffer_destroy_listener.link);
889
890                 return;
891         }
892
893         if (es->fullscreen_output == output) {
894                 if (es->width < output->current->width ||
895                     es->height < output->current->height)
896                         glClear(GL_COLOR_BUFFER_BIT);
897                 wlsc_surface_draw(es, output, &total_damage);
898         } else {
899                 wl_list_for_each(es, &ec->surface_list, link) {
900                         pixman_region32_copy(&es->damage, &total_damage);
901                         pixman_region32_subtract(&total_damage, &total_damage, &es->opaque);
902                 }
903
904                 wl_list_for_each_reverse(es, &ec->surface_list, link) {
905                         pixman_region32_init(&repaint);
906                         pixman_region32_intersect(&repaint, &output->region,
907                                                   &es->damage);
908                         wlsc_surface_draw(es, output, &repaint);
909                         pixman_region32_subtract(&es->damage,
910                                                  &es->damage, &output->region);
911                 }
912         }
913
914         if (ec->fade.spring.current > 0.001)
915                 fade_output(output, ec->fade.spring.current, &total_damage);
916 }
917
918 struct wlsc_frame_callback {
919         struct wl_resource resource;
920         struct wl_list link;
921 };
922
923 static void
924 repaint(void *data, int msecs)
925 {
926         struct wlsc_output *output = data;
927         struct wlsc_compositor *compositor = output->compositor;
928         struct wlsc_animation *animation, *next;
929         struct wlsc_frame_callback *cb, *cnext;
930
931         wlsc_output_repaint(output);
932         output->repaint_needed = 0;
933         output->repaint_scheduled = 1;
934         output->present(output);
935
936         wl_list_for_each_safe(cb, cnext, &output->frame_callback_list, link) {
937                 wl_resource_post_event(&cb->resource, WL_CALLBACK_DONE, msecs);
938                 wl_resource_destroy(&cb->resource, 0);
939         }
940
941         wl_list_for_each_safe(animation, next,
942                               &compositor->animation_list, link)
943                 animation->frame(animation, output, msecs);
944 }
945
946 static void
947 idle_repaint(void *data)
948 {
949         repaint(data, wlsc_compositor_get_time());
950 }
951
952 WL_EXPORT void
953 wlsc_output_finish_frame(struct wlsc_output *output, int msecs)
954 {
955         wlsc_buffer_post_release(output->scanout_buffer);
956         output->scanout_buffer = NULL;
957         output->repaint_scheduled = 0;
958
959         if (output->repaint_needed)
960                 repaint(output, msecs);
961 }
962
963 WL_EXPORT void
964 wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
965 {
966         struct wlsc_output *output;
967         struct wl_event_loop *loop;
968
969         if (compositor->state == WLSC_COMPOSITOR_SLEEPING)
970                 return;
971
972         loop = wl_display_get_event_loop(compositor->wl_display);
973         wl_list_for_each(output, &compositor->output_list, link) {
974                 output->repaint_needed = 1;
975                 if (output->repaint_scheduled)
976                         continue;
977
978                 wl_event_loop_add_idle(loop, idle_repaint, output);
979                 output->repaint_scheduled = 1;
980         }
981 }
982
983 WL_EXPORT void
984 wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint)
985 {
986         int done;
987
988         done = wlsc_spring_done(&compositor->fade.spring);
989         compositor->fade.spring.target = tint;
990         if (wlsc_spring_done(&compositor->fade.spring))
991                 return;
992
993         if (done)
994                 compositor->fade.spring.timestamp =
995                         wlsc_compositor_get_time();
996
997         wlsc_compositor_damage_all(compositor);
998         if (wl_list_empty(&compositor->fade.animation.link))
999                 wl_list_insert(compositor->animation_list.prev,
1000                                &compositor->fade.animation.link);
1001 }
1002
1003 static void
1004 surface_destroy(struct wl_client *client, struct wl_resource *resource)
1005 {
1006         wl_resource_destroy(resource, wlsc_compositor_get_time());
1007 }
1008
1009 WL_EXPORT void
1010 wlsc_surface_assign_output(struct wlsc_surface *es)
1011 {
1012         struct wlsc_compositor *ec = es->compositor;
1013         struct wlsc_output *output;
1014         pixman_region32_t region;
1015         uint32_t max, area;
1016         pixman_box32_t *e;
1017
1018         max = 0;
1019         wl_list_for_each(output, &ec->output_list, link) {
1020                 pixman_region32_init_rect(&region,
1021                                           es->x, es->y, es->width, es->height);
1022                 pixman_region32_intersect(&region, &region, &output->region);
1023
1024                 e = pixman_region32_extents(&region);
1025                 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1026
1027                 if (area >= max) {
1028                         es->output = output;
1029                         max = area;
1030                 }
1031         }
1032 }
1033
1034 static void
1035 surface_attach(struct wl_client *client,
1036                struct wl_resource *resource,
1037                struct wl_resource *buffer_resource, int32_t x, int32_t y)
1038 {
1039         struct wlsc_surface *es = resource->data;
1040         struct wl_buffer *buffer = buffer_resource->data;
1041
1042         buffer->busy_count++;
1043         wlsc_buffer_post_release(es->buffer);
1044
1045         es->buffer = buffer;
1046         wl_list_remove(&es->buffer_destroy_listener.link);
1047         wl_list_insert(es->buffer->resource.destroy_listener_list.prev,
1048                        &es->buffer_destroy_listener.link);
1049
1050         if (es->visual == WLSC_NONE_VISUAL)
1051                 wl_list_insert(&es->compositor->surface_list, &es->link);
1052
1053         if (x != 0 || y != 0 ||
1054             es->width != buffer->width || es->height != buffer->height)
1055                 wlsc_surface_configure(es, es->x + x, es->y + y,
1056                                        buffer->width, buffer->height);
1057
1058         wlsc_buffer_attach(buffer, &es->surface);
1059
1060         es->compositor->shell->attach(es->compositor->shell, es);
1061 }
1062
1063 static void
1064 surface_damage(struct wl_client *client,
1065                struct wl_resource *resource,
1066                int32_t x, int32_t y, int32_t width, int32_t height)
1067 {
1068         struct wlsc_surface *es = resource->data;
1069
1070         wlsc_surface_damage_rectangle(es, x, y, width, height);
1071 }
1072
1073 static void
1074 destroy_frame_callback(struct wl_resource *resource)
1075 {
1076         struct wlsc_frame_callback *cb = resource->data;
1077
1078         wl_list_remove(&cb->link);
1079         free(resource);
1080 }
1081
1082 static void
1083 surface_frame(struct wl_client *client,
1084               struct wl_resource *resource, uint32_t callback)
1085 {
1086         struct wlsc_frame_callback *cb;
1087         struct wlsc_surface *es = resource->data;
1088
1089         cb = malloc(sizeof *cb);
1090         if (cb == NULL) {
1091                 wl_client_post_no_memory(client);
1092                 return;
1093         }
1094                 
1095         cb->resource.object.interface = &wl_callback_interface;
1096         cb->resource.object.id = callback;
1097         cb->resource.destroy = destroy_frame_callback;
1098         cb->resource.client = client;
1099         cb->resource.data = cb;
1100
1101         wl_client_add_resource(client, &cb->resource);
1102
1103         if (es->output) {
1104                 wl_list_insert(es->output->frame_callback_list.prev,
1105                                &cb->link);
1106         } else {
1107                 wl_list_init(&cb->link);
1108                 wl_resource_destroy(&cb->resource, 0);
1109         }
1110 }
1111
1112 const static struct wl_surface_interface surface_interface = {
1113         surface_destroy,
1114         surface_attach,
1115         surface_damage,
1116         surface_frame
1117 };
1118
1119 static void
1120 wlsc_input_device_attach(struct wlsc_input_device *device,
1121                          int x, int y, int width, int height)
1122 {
1123         wlsc_surface_damage_below(device->sprite);
1124
1125         device->hotspot_x = x;
1126         device->hotspot_y = y;
1127
1128         device->sprite->x = device->input_device.x - device->hotspot_x;
1129         device->sprite->y = device->input_device.y - device->hotspot_y;
1130         device->sprite->width = width;
1131         device->sprite->height = height;
1132
1133         wlsc_surface_damage(device->sprite);
1134 }
1135
1136 static void
1137 wlsc_input_device_attach_buffer(struct wlsc_input_device *device,
1138                                 struct wl_buffer *buffer, int x, int y)
1139 {
1140         wlsc_buffer_attach(buffer, &device->sprite->surface);
1141         wlsc_input_device_attach(device, x, y, buffer->width, buffer->height);
1142 }
1143
1144 static void
1145 wlsc_input_device_attach_sprite(struct wlsc_input_device *device,
1146                                 struct wlsc_sprite *sprite, int x, int y)
1147 {
1148         wlsc_sprite_attach(sprite, &device->sprite->surface);
1149         wlsc_input_device_attach(device, x, y, sprite->width, sprite->height);
1150 }
1151
1152 WL_EXPORT void
1153 wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
1154                                     enum wlsc_pointer_type type)
1155 {
1156         struct wlsc_compositor *compositor =
1157                 (struct wlsc_compositor *) device->input_device.compositor;
1158
1159         wlsc_input_device_attach_sprite(device,
1160                                         compositor->pointer_sprites[type],
1161                                         pointer_images[type].hotspot_x,
1162                                         pointer_images[type].hotspot_y);
1163 }
1164
1165 static void
1166 compositor_create_surface(struct wl_client *client,
1167                           struct wl_resource *resource, uint32_t id)
1168 {
1169         struct wlsc_compositor *ec = resource->data;
1170         struct wlsc_surface *surface;
1171
1172         surface = wlsc_surface_create(ec, 0, 0, 0, 0);
1173         if (surface == NULL) {
1174                 wl_client_post_no_memory(client);
1175                 return;
1176         }
1177
1178         surface->surface.resource.destroy = destroy_surface;
1179
1180         surface->surface.resource.object.id = id;
1181         surface->surface.resource.object.interface = &wl_surface_interface;
1182         surface->surface.resource.object.implementation =
1183                 (void (**)(void)) &surface_interface;
1184         surface->surface.resource.data = surface;
1185
1186         wl_client_add_resource(client, &surface->surface.resource);
1187 }
1188
1189 const static struct wl_compositor_interface compositor_interface = {
1190         compositor_create_surface,
1191 };
1192
1193 static void
1194 wlsc_surface_transform(struct wlsc_surface *surface,
1195                        int32_t x, int32_t y, int32_t *sx, int32_t *sy)
1196 {
1197         *sx = x - surface->x;
1198         *sy = y - surface->y;
1199 }
1200
1201 WL_EXPORT struct wlsc_surface *
1202 pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
1203 {
1204         struct wlsc_compositor *ec =
1205                 (struct wlsc_compositor *) device->compositor;
1206         struct wlsc_surface *es;
1207
1208         wl_list_for_each(es, &ec->surface_list, link) {
1209                 if (es->surface.resource.client == NULL)
1210                         continue;
1211                 wlsc_surface_transform(es, device->x, device->y, sx, sy);
1212                 if (0 <= *sx && *sx < es->width &&
1213                     0 <= *sy && *sy < es->height)
1214                         return es;
1215         }
1216
1217         return NULL;
1218 }
1219
1220
1221 static void
1222 motion_grab_motion(struct wl_grab *grab,
1223                    uint32_t time, int32_t x, int32_t y)
1224 {
1225         struct wlsc_input_device *device =
1226                 (struct wlsc_input_device *) grab->input_device;
1227         struct wlsc_surface *es =
1228                 (struct wlsc_surface *) device->input_device.pointer_focus;
1229         int32_t sx, sy;
1230         struct wl_resource *resource;
1231
1232         resource = grab->input_device->pointer_focus_resource;
1233         if (resource) {
1234                 wlsc_surface_transform(es, x, y, &sx, &sy);
1235                 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
1236                                        time, x, y, sx, sy);
1237         }
1238 }
1239
1240 static void
1241 motion_grab_button(struct wl_grab *grab,
1242                    uint32_t time, int32_t button, int32_t state)
1243 {
1244         struct wl_resource *resource;
1245
1246         resource = grab->input_device->pointer_focus_resource;
1247         if (resource)
1248                 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
1249                                        time, button, state);
1250 }
1251
1252 static void
1253 motion_grab_end(struct wl_grab *grab, uint32_t time)
1254 {
1255 }
1256
1257 static const struct wl_grab_interface motion_grab_interface = {
1258         motion_grab_motion,
1259         motion_grab_button,
1260         motion_grab_end
1261 };
1262
1263 WL_EXPORT void
1264 wlsc_compositor_wake(struct wlsc_compositor *compositor)
1265 {
1266         if (compositor->idle_inhibit)
1267                 return;
1268
1269         wlsc_compositor_fade(compositor, 0.0);
1270         compositor->state = WLSC_COMPOSITOR_ACTIVE;
1271
1272         wl_event_source_timer_update(compositor->idle_source,
1273                                      option_idle_time * 1000);
1274 }
1275
1276 static void
1277 wlsc_compositor_idle_inhibit(struct wlsc_compositor *compositor)
1278 {
1279         wlsc_compositor_wake(compositor);
1280         compositor->idle_inhibit++;
1281 }
1282
1283 static void
1284 wlsc_compositor_idle_release(struct wlsc_compositor *compositor)
1285 {
1286         compositor->idle_inhibit--;
1287         wlsc_compositor_wake(compositor);
1288 }
1289
1290 static int
1291 idle_handler(void *data)
1292 {
1293         struct wlsc_compositor *compositor = data;
1294
1295         if (compositor->idle_inhibit)
1296                 return 1;
1297
1298         wlsc_compositor_fade(compositor, 1.0);
1299
1300         return 1;
1301 }
1302
1303 WL_EXPORT void
1304 notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
1305 {
1306         struct wlsc_surface *es;
1307         struct wlsc_compositor *ec =
1308                 (struct wlsc_compositor *) device->compositor;
1309         struct wlsc_output *output;
1310         const struct wl_grab_interface *interface;
1311         struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1312         int32_t sx, sy;
1313         int x_valid = 0, y_valid = 0;
1314         int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
1315
1316         wlsc_compositor_wake(ec);
1317
1318         wl_list_for_each(output, &ec->output_list, link) {
1319                 if (output->x <= x && x <= output->x + output->current->width)
1320                         x_valid = 1;
1321
1322                 if (output->y <= y && y <= output->y + output->current->height)
1323                         y_valid = 1;
1324
1325                 /* FIXME: calculate this only on output addition/deletion */
1326                 if (output->x < min_x)
1327                         min_x = output->x;
1328                 if (output->y < min_y)
1329                         min_y = output->y;
1330
1331                 if (output->x + output->current->width > max_x)
1332                         max_x = output->x + output->current->width;
1333                 if (output->y + output->current->height > max_y)
1334                         max_y = output->y + output->current->height;
1335         }
1336         
1337         if (!x_valid) {
1338                 if (x < min_x)
1339                         x = min_x;
1340                 else if (x >= max_x)
1341                         x = max_x;
1342         }
1343         if (!y_valid) {
1344                 if (y < min_y)
1345                         y = min_y;
1346                 else  if (y >= max_y)
1347                         y = max_y;
1348         }
1349
1350         device->x = x;
1351         device->y = y;
1352
1353         if (device->grab) {
1354                 interface = device->grab->interface;
1355                 interface->motion(device->grab, time, x, y);
1356         } else {
1357                 es = pick_surface(device, &sx, &sy);
1358                 wl_input_device_set_pointer_focus(device,
1359                                                   &es->surface,
1360                                                   time, x, y, sx, sy);
1361                 if (device->pointer_focus_resource)
1362                         wl_resource_post_event(device->pointer_focus_resource,
1363                                                WL_INPUT_DEVICE_MOTION,
1364                                                time, x, y, sx, sy);
1365         }
1366
1367         wlsc_surface_damage_below(wd->sprite);
1368
1369         wd->sprite->x = device->x - wd->hotspot_x;
1370         wd->sprite->y = device->y - wd->hotspot_y;
1371
1372         wlsc_surface_damage(wd->sprite);
1373 }
1374
1375 WL_EXPORT void
1376 wlsc_surface_activate(struct wlsc_surface *surface,
1377                       struct wlsc_input_device *device, uint32_t time)
1378 {
1379         struct wlsc_shell *shell = surface->compositor->shell;
1380
1381         wlsc_surface_raise(surface);
1382         if (device->selection)
1383                 shell->set_selection_focus(shell,
1384                                            device->selection,
1385                                            &surface->surface, time);
1386
1387         wl_input_device_set_keyboard_focus(&device->input_device,
1388                                            &surface->surface,
1389                                            time);
1390 }
1391
1392 struct wlsc_binding {
1393         uint32_t key;
1394         uint32_t button;
1395         uint32_t modifier;
1396         wlsc_binding_handler_t handler;
1397         void *data;
1398         struct wl_list link;
1399 };
1400
1401 WL_EXPORT void
1402 notify_button(struct wl_input_device *device,
1403               uint32_t time, int32_t button, int32_t state)
1404 {
1405         struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1406         struct wlsc_compositor *compositor =
1407                 (struct wlsc_compositor *) device->compositor;
1408         struct wlsc_binding *b;
1409         struct wlsc_surface *surface =
1410                 (struct wlsc_surface *) device->pointer_focus;
1411
1412         if (state)
1413                 wlsc_compositor_idle_inhibit(compositor);
1414         else
1415                 wlsc_compositor_idle_release(compositor);
1416
1417         if (state && surface && device->grab == NULL) {
1418                 wlsc_surface_activate(surface, wd, time);
1419                 wl_input_device_start_grab(device,
1420                                            &device->motion_grab,
1421                                            button, time);
1422         }
1423
1424         wl_list_for_each(b, &compositor->binding_list, link) {
1425                 if (b->button == button &&
1426                     b->modifier == wd->modifier_state && state) {
1427                         b->handler(&wd->input_device,
1428                                    time, 0, button, state, b->data);
1429                         break;
1430                 }
1431         }
1432
1433         if (device->grab)
1434                 device->grab->interface->button(device->grab, time,
1435                                                 button, state);
1436
1437         if (!state && device->grab && device->grab_button == button)
1438                 wl_input_device_end_grab(device, time);
1439 }
1440
1441 static void
1442 terminate_binding(struct wl_input_device *device, uint32_t time,
1443                   uint32_t key, uint32_t button, uint32_t state, void *data)
1444 {
1445         struct wlsc_compositor *compositor = data;
1446
1447         if (state)
1448                 wl_display_terminate(compositor->wl_display);
1449 }
1450
1451 WL_EXPORT struct wlsc_binding *
1452 wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
1453                             uint32_t key, uint32_t button, uint32_t modifier,
1454                             wlsc_binding_handler_t handler, void *data)
1455 {
1456         struct wlsc_binding *binding;
1457
1458         binding = malloc(sizeof *binding);
1459         if (binding == NULL)
1460                 return NULL;
1461
1462         binding->key = key;
1463         binding->button = button;
1464         binding->modifier = modifier;
1465         binding->handler = handler;
1466         binding->data = data;
1467         wl_list_insert(compositor->binding_list.prev, &binding->link);
1468
1469         return binding;
1470 }
1471
1472 WL_EXPORT void
1473 wlsc_binding_destroy(struct wlsc_binding *binding)
1474 {
1475         wl_list_remove(&binding->link);
1476         free(binding);
1477 }
1478
1479 static void
1480 update_modifier_state(struct wlsc_input_device *device,
1481                       uint32_t key, uint32_t state)
1482 {
1483         uint32_t modifier;
1484
1485         switch (key) {
1486         case KEY_LEFTCTRL:
1487         case KEY_RIGHTCTRL:
1488                 modifier = MODIFIER_CTRL;
1489                 break;
1490
1491         case KEY_LEFTALT:
1492         case KEY_RIGHTALT:
1493                 modifier = MODIFIER_ALT;
1494                 break;
1495
1496         case KEY_LEFTMETA:
1497         case KEY_RIGHTMETA:
1498                 modifier = MODIFIER_SUPER;
1499                 break;
1500
1501         default:
1502                 modifier = 0;
1503                 break;
1504         }
1505
1506         if (state)
1507                 device->modifier_state |= modifier;
1508         else
1509                 device->modifier_state &= ~modifier;
1510 }
1511
1512 WL_EXPORT void
1513 notify_key(struct wl_input_device *device,
1514            uint32_t time, uint32_t key, uint32_t state)
1515 {
1516         struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1517         struct wlsc_compositor *compositor =
1518                 (struct wlsc_compositor *) device->compositor;
1519         uint32_t *k, *end;
1520         struct wlsc_binding *b;
1521
1522         if (state)
1523                 wlsc_compositor_idle_inhibit(compositor);
1524         else
1525                 wlsc_compositor_idle_release(compositor);
1526
1527         wl_list_for_each(b, &compositor->binding_list, link) {
1528                 if (b->key == key &&
1529                     b->modifier == wd->modifier_state) {
1530                         b->handler(&wd->input_device,
1531                                    time, key, 0, state, b->data);
1532                         break;
1533                 }
1534         }
1535
1536         update_modifier_state(wd, key, state);
1537         end = device->keys.data + device->keys.size;
1538         for (k = device->keys.data; k < end; k++) {
1539                 if (*k == key)
1540                         *k = *--end;
1541         }
1542         device->keys.size = (void *) end - device->keys.data;
1543         if (state) {
1544                 k = wl_array_add(&device->keys, sizeof *k);
1545                 *k = key;
1546         }
1547
1548         if (device->keyboard_focus_resource)
1549                 wl_resource_post_event(device->keyboard_focus_resource,
1550                                        WL_INPUT_DEVICE_KEY, time, key, state);
1551 }
1552
1553 WL_EXPORT void
1554 notify_pointer_focus(struct wl_input_device *device,
1555                      uint32_t time, struct wlsc_output *output,
1556                      int32_t x, int32_t y)
1557 {
1558         struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1559         struct wlsc_compositor *compositor =
1560                 (struct wlsc_compositor *) device->compositor;
1561         struct wlsc_surface *es;
1562         int32_t sx, sy;
1563
1564         if (output) {
1565                 device->x = x;
1566                 device->y = y;
1567                 es = pick_surface(device, &sx, &sy);
1568                 wl_input_device_set_pointer_focus(device,
1569                                                   &es->surface,
1570                                                   time, x, y, sx, sy);
1571
1572                 compositor->focus = 1;
1573
1574                 wd->sprite->x = device->x - wd->hotspot_x;
1575                 wd->sprite->y = device->y - wd->hotspot_y;
1576         } else {
1577                 wl_input_device_set_pointer_focus(device, NULL,
1578                                                   time, 0, 0, 0, 0);
1579                 compositor->focus = 0;
1580         }
1581
1582         wlsc_surface_damage(wd->sprite);
1583 }
1584
1585 WL_EXPORT void
1586 notify_keyboard_focus(struct wl_input_device *device,
1587                       uint32_t time, struct wlsc_output *output,
1588                       struct wl_array *keys)
1589 {
1590         struct wlsc_input_device *wd =
1591                 (struct wlsc_input_device *) device;
1592         struct wlsc_compositor *compositor =
1593                 (struct wlsc_compositor *) device->compositor;
1594         struct wlsc_surface *es;
1595         uint32_t *k, *end;
1596
1597         if (!wl_list_empty(&compositor->surface_list))
1598                 es = container_of(compositor->surface_list.next,
1599                                   struct wlsc_surface, link);
1600         else
1601                 es = NULL;
1602
1603         if (output) {
1604                 wl_array_copy(&wd->input_device.keys, keys);
1605                 wd->modifier_state = 0;
1606                 end = device->keys.data + device->keys.size;
1607                 for (k = device->keys.data; k < end; k++) {
1608                         wlsc_compositor_idle_inhibit(compositor);
1609                         update_modifier_state(wd, *k, 1);
1610                 }
1611
1612                 if (es->surface.resource.client)
1613                         wl_input_device_set_keyboard_focus(&wd->input_device,
1614                                                            &es->surface, time);
1615         } else {
1616                 end = device->keys.data + device->keys.size;
1617                 for (k = device->keys.data; k < end; k++)
1618                         wlsc_compositor_idle_release(compositor);
1619
1620                 wd->modifier_state = 0;
1621                 wl_input_device_set_keyboard_focus(&wd->input_device,
1622                                                    NULL, time);
1623         }
1624 }
1625
1626
1627 static void
1628 input_device_attach(struct wl_client *client,
1629                     struct wl_resource *resource,
1630                     uint32_t time,
1631                     struct wl_resource *buffer_resource, int32_t x, int32_t y)
1632 {
1633         struct wlsc_input_device *device = resource->data;
1634         struct wl_buffer *buffer;
1635
1636         if (time < device->input_device.pointer_focus_time)
1637                 return;
1638         if (device->input_device.pointer_focus == NULL)
1639                 return;
1640         if (device->input_device.pointer_focus->resource.client != client)
1641                 return;
1642
1643         if (buffer_resource) {
1644                 buffer = buffer_resource->data;
1645                 wlsc_input_device_attach_buffer(device, buffer, x, y);
1646         } else {
1647                 wlsc_input_device_set_pointer_image(device,
1648                                                     WLSC_POINTER_LEFT_PTR);
1649                 return;
1650         }
1651 }
1652
1653 const static struct wl_input_device_interface input_device_interface = {
1654         input_device_attach,
1655 };
1656
1657 static void
1658 bind_input_device(struct wl_client *client,
1659                   void *data, uint32_t version, uint32_t id)
1660 {
1661         struct wl_input_device *device = data;
1662         struct wl_resource *resource;
1663
1664         resource = wl_client_add_object(client, &wl_input_device_interface,
1665                                         &input_device_interface, id, data);
1666         wl_list_insert(&device->resource_list, &resource->link);
1667 }
1668
1669 WL_EXPORT void
1670 wlsc_input_device_init(struct wlsc_input_device *device,
1671                        struct wlsc_compositor *ec)
1672 {
1673         wl_input_device_init(&device->input_device, &ec->compositor);
1674
1675         wl_display_add_global(ec->wl_display, &wl_input_device_interface,
1676                               device, bind_input_device);
1677
1678         device->sprite = wlsc_surface_create(ec,
1679                                              device->input_device.x,
1680                                              device->input_device.y, 32, 32);
1681         wl_list_insert(&ec->surface_list, &device->sprite->link);
1682
1683         device->hotspot_x = 16;
1684         device->hotspot_y = 16;
1685         device->modifier_state = 0;
1686
1687         device->input_device.motion_grab.interface = &motion_grab_interface;
1688
1689         wl_list_insert(ec->input_device_list.prev, &device->link);
1690
1691         wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
1692 }
1693
1694 static void
1695 bind_output(struct wl_client *client,
1696             void *data, uint32_t version, uint32_t id)
1697 {
1698         struct wlsc_output *output = data;
1699         struct wlsc_mode *mode;
1700         struct wl_resource *resource;
1701
1702         resource = wl_client_add_object(client,
1703                                         &wl_output_interface, NULL, id, data);
1704
1705         wl_resource_post_event(resource,
1706                                WL_OUTPUT_GEOMETRY,
1707                                output->x,
1708                                output->y,
1709                                output->mm_width,
1710                                output->mm_height,
1711                                output->subpixel,
1712                                output->make, output->model);
1713
1714         wl_list_for_each (mode, &output->mode_list, link) {
1715                 wl_resource_post_event(resource,
1716                                        WL_OUTPUT_MODE,
1717                                        mode->flags,
1718                                        mode->width,
1719                                        mode->height,
1720                                        mode->refresh);
1721         }
1722 }
1723
1724 static const char vertex_shader[] =
1725         "uniform mat4 proj;\n"
1726         "attribute vec2 position;\n"
1727         "attribute vec2 texcoord;\n"
1728         "varying vec2 v_texcoord;\n"
1729         "void main()\n"
1730         "{\n"
1731         "   gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1732         "   v_texcoord = texcoord;\n"
1733         "}\n";
1734
1735 static const char texture_fragment_shader[] =
1736         "precision mediump float;\n"
1737         "varying vec2 v_texcoord;\n"
1738         "uniform sampler2D tex;\n"
1739         "void main()\n"
1740         "{\n"
1741         "   gl_FragColor = texture2D(tex, v_texcoord)\n;"
1742         "}\n";
1743
1744 static const char solid_fragment_shader[] =
1745         "precision mediump float;\n"
1746         "uniform vec4 color;\n"
1747         "void main()\n"
1748         "{\n"
1749         "   gl_FragColor = color\n;"
1750         "}\n";
1751
1752 static int
1753 compile_shader(GLenum type, const char *source)
1754 {
1755         GLuint s;
1756         char msg[512];
1757         GLint status;
1758
1759         s = glCreateShader(type);
1760         glShaderSource(s, 1, &source, NULL);
1761         glCompileShader(s);
1762         glGetShaderiv(s, GL_COMPILE_STATUS, &status);
1763         if (!status) {
1764                 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
1765                 fprintf(stderr, "shader info: %s\n", msg);
1766                 return GL_NONE;
1767         }
1768
1769         return s;
1770 }
1771
1772 static int
1773 wlsc_shader_init(struct wlsc_shader *shader,
1774                  const char *vertex_source, const char *fragment_source)
1775 {
1776         char msg[512];
1777         GLint status;
1778
1779         shader->vertex_shader =
1780                 compile_shader(GL_VERTEX_SHADER, vertex_source);
1781         shader->fragment_shader =
1782                 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1783
1784         shader->program = glCreateProgram();
1785         glAttachShader(shader->program, shader->vertex_shader);
1786         glAttachShader(shader->program, shader->fragment_shader);
1787         glBindAttribLocation(shader->program, 0, "position");
1788         glBindAttribLocation(shader->program, 1, "texcoord");
1789
1790         glLinkProgram(shader->program);
1791         glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1792         if (!status) {
1793                 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1794                 fprintf(stderr, "link info: %s\n", msg);
1795                 return -1;
1796         }
1797
1798         shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1799         shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
1800
1801         return 0;
1802 }
1803
1804 static int
1805 init_solid_shader(struct wlsc_shader *shader,
1806                   GLuint vertex_shader, const char *fragment_source)
1807 {
1808         GLint status;
1809         char msg[512];
1810
1811         shader->vertex_shader = vertex_shader;
1812         shader->fragment_shader =
1813                 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
1814
1815         shader->program = glCreateProgram();
1816         glAttachShader(shader->program, shader->vertex_shader);
1817         glAttachShader(shader->program, shader->fragment_shader);
1818         glBindAttribLocation(shader->program, 0, "position");
1819         glBindAttribLocation(shader->program, 1, "texcoord");
1820
1821         glLinkProgram(shader->program);
1822         glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1823         if (!status) {
1824                 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
1825                 fprintf(stderr, "link info: %s\n", msg);
1826                 return -1;
1827         }
1828  
1829         shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
1830         shader->color_uniform = glGetUniformLocation(shader->program, "color");
1831
1832         return 0;
1833 }
1834
1835 WL_EXPORT void
1836 wlsc_output_destroy(struct wlsc_output *output)
1837 {
1838         pixman_region32_fini(&output->region);
1839         pixman_region32_fini(&output->previous_damage);
1840         destroy_surface(&output->background->surface.resource);
1841 }
1842
1843 WL_EXPORT void
1844 wlsc_output_move(struct wlsc_output *output, int x, int y)
1845 {
1846         struct wlsc_compositor *c = output->compositor;
1847         int flip;
1848
1849         output->x = x;
1850         output->y = y;
1851
1852         if (output->background) {
1853                 output->background->x = x;
1854                 output->background->y = y;
1855         }
1856
1857         pixman_region32_init(&output->previous_damage);
1858         pixman_region32_init_rect(&output->region, x, y, 
1859                                   output->current->width,
1860                                   output->current->height);
1861
1862         wlsc_matrix_init(&output->matrix);
1863         wlsc_matrix_translate(&output->matrix,
1864                               -output->x - output->current->width / 2.0,
1865                               -output->y - output->current->height / 2.0, 0);
1866
1867         flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
1868         wlsc_matrix_scale(&output->matrix,
1869                           2.0 / output->current->width,
1870                           flip * 2.0 / output->current->height, 1);
1871
1872         pixman_region32_union(&c->damage, &c->damage, &output->region);
1873 }
1874
1875 WL_EXPORT void
1876 wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1877                  int x, int y, int width, int height, uint32_t flags)
1878 {
1879         output->compositor = c;
1880         output->x = x;
1881         output->y = y;
1882         output->mm_width = width;
1883         output->mm_height = height;
1884
1885         output->background =
1886                 background_create(output, option_background);
1887  
1888         if (output->background != NULL)
1889                 wl_list_insert(c->surface_list.prev,
1890                                &output->background->link);
1891
1892         output->flags = flags;
1893         wlsc_output_move(output, x, y);
1894
1895         output->scanout_buffer_destroy_listener.func =
1896                 output_handle_scanout_buffer_destroy;
1897         wl_list_init(&output->scanout_buffer_destroy_listener.link);
1898         wl_list_init(&output->frame_callback_list);
1899
1900         output->resource.object.interface = &wl_output_interface;
1901         wl_display_add_global(c->wl_display,
1902                               &wl_output_interface, output, bind_output);
1903 }
1904
1905 static void
1906 shm_buffer_created(struct wl_buffer *buffer)
1907 {
1908         struct wl_list *surfaces_attached_to;
1909
1910         surfaces_attached_to = malloc(sizeof *surfaces_attached_to);
1911         if (!surfaces_attached_to) {
1912                 buffer->user_data = NULL;
1913                 return;
1914         }
1915
1916         wl_list_init(surfaces_attached_to);
1917
1918         buffer->user_data = surfaces_attached_to;
1919 }
1920
1921 static void
1922 shm_buffer_damaged(struct wl_buffer *buffer,
1923                    int32_t x, int32_t y, int32_t width, int32_t height)
1924 {
1925         struct wl_list *surfaces_attached_to = buffer->user_data;
1926         struct wlsc_surface *es;
1927         GLsizei tex_width = wl_shm_buffer_get_stride(buffer) / 4;
1928
1929         wl_list_for_each(es, surfaces_attached_to, buffer_link) {
1930                 glBindTexture(GL_TEXTURE_2D, es->texture);
1931                 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1932                              tex_width, buffer->height, 0,
1933                              GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1934                              wl_shm_buffer_get_data(buffer));
1935                 /* Hmm, should use glTexSubImage2D() here but GLES2 doesn't
1936                  * support any unpack attributes except GL_UNPACK_ALIGNMENT. */
1937         }
1938 }
1939
1940 static void
1941 shm_buffer_destroyed(struct wl_buffer *buffer)
1942 {
1943         struct wl_list *surfaces_attached_to = buffer->user_data;
1944         struct wlsc_surface *es, *next;
1945
1946         wl_list_for_each_safe(es, next, surfaces_attached_to, buffer_link) {
1947                 wl_list_remove(&es->buffer_link);
1948                 wl_list_init(&es->buffer_link);
1949         }
1950
1951         free(surfaces_attached_to);
1952 }
1953
1954 const static struct wl_shm_callbacks shm_callbacks = {
1955         shm_buffer_created,
1956         shm_buffer_damaged,
1957         shm_buffer_destroyed
1958 };
1959
1960 WL_EXPORT int
1961 wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1962 {
1963         struct wl_event_loop *loop;
1964         const char *extensions;
1965
1966         ec->wl_display = display;
1967
1968         wl_compositor_init(&ec->compositor, &compositor_interface, display);
1969
1970         ec->shm = wl_shm_init(display, &shm_callbacks);
1971
1972         ec->image_target_texture_2d =
1973                 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1974         ec->image_target_renderbuffer_storage = (void *)
1975                 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
1976         ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1977         ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1978         ec->bind_display =
1979                 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
1980         ec->unbind_display =
1981                 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
1982
1983         extensions = (const char *) glGetString(GL_EXTENSIONS);
1984         if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1985                 fprintf(stderr,
1986                         "GL_EXT_texture_format_BGRA8888 not available\n");
1987                 return -1;
1988         }
1989
1990         extensions =
1991                 (const char *) eglQueryString(ec->display, EGL_EXTENSIONS);
1992         if (strstr(extensions, "EGL_WL_bind_wayland_display"))
1993                 ec->has_bind_display = 1;
1994         if (ec->has_bind_display)
1995                 ec->bind_display(ec->display, ec->wl_display);
1996
1997         wl_list_init(&ec->surface_list);
1998         wl_list_init(&ec->input_device_list);
1999         wl_list_init(&ec->output_list);
2000         wl_list_init(&ec->binding_list);
2001         wl_list_init(&ec->animation_list);
2002         wlsc_spring_init(&ec->fade.spring, 0.8, 0.0, 0.0);
2003         ec->fade.animation.frame = fade_frame;
2004         wl_list_init(&ec->fade.animation.link);
2005
2006         wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
2007                                     MODIFIER_CTRL | MODIFIER_ALT,
2008                                     terminate_binding, ec);
2009
2010         create_pointer_images(ec);
2011
2012         screenshooter_create(ec);
2013
2014         glActiveTexture(GL_TEXTURE0);
2015
2016         if (wlsc_shader_init(&ec->texture_shader,
2017                              vertex_shader, texture_fragment_shader) < 0)
2018                 return -1;
2019         if (init_solid_shader(&ec->solid_shader,
2020                               ec->texture_shader.vertex_shader,
2021                               solid_fragment_shader) < 0)
2022                 return -1;
2023
2024         loop = wl_display_get_event_loop(ec->wl_display);
2025         ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2026         wl_event_source_timer_update(ec->idle_source, option_idle_time * 1000);
2027
2028         pixman_region32_init(&ec->damage);
2029         wlsc_compositor_schedule_repaint(ec);
2030
2031         return 0;
2032 }
2033
2034 WL_EXPORT void
2035 wlsc_compositor_shutdown(struct wlsc_compositor *ec)
2036 {
2037         struct wlsc_output *output;
2038
2039         /* Destroy all outputs associated with this compositor */
2040         wl_list_for_each(output, &ec->output_list, link)
2041                 output->destroy(output);
2042 }
2043
2044 static int on_term_signal(int signal_number, void *data)
2045 {
2046         struct wl_display *display = data;
2047
2048         fprintf(stderr, "caught signal %d\n", signal_number);
2049         wl_display_terminate(display);
2050
2051         return 1;
2052 }
2053
2054 static void *
2055 load_module(const char *name, const char *entrypoint, void **handle)
2056 {
2057         char path[PATH_MAX];
2058         void *module, *init;
2059
2060         if (name[0] != '/')
2061                 snprintf(path, sizeof path, MODULEDIR "/%s", name);
2062         else
2063                 snprintf(path, sizeof path, "%s", name);
2064
2065         module = dlopen(path, RTLD_LAZY);
2066         if (!module) {
2067                 fprintf(stderr,
2068                         "failed to load module: %s\n", dlerror());
2069                 return NULL;
2070         }
2071
2072         init = dlsym(module, entrypoint);
2073         if (!init) {
2074                 fprintf(stderr,
2075                         "failed to lookup init function: %s\n", dlerror());
2076                 return NULL;
2077         }
2078
2079         return init;
2080 }
2081
2082 int main(int argc, char *argv[])
2083 {
2084         struct wl_display *display;
2085         struct wlsc_compositor *ec;
2086         struct wl_event_loop *loop;
2087         int o, xserver = 0;
2088         void *shell_module, *backend_module;
2089         int (*shell_init)(struct wlsc_compositor *ec);
2090         struct wlsc_compositor
2091                 *(*backend_init)(struct wl_display *display, char *options);
2092         char *backend = NULL;
2093         char *backend_options = "";
2094         char *shell = NULL;
2095         char *p;
2096
2097         static const char opts[] = "B:b:o:S:i:s:x";
2098         static const struct option longopts[ ] = {
2099                 { "backend", 1, NULL, 'B' },
2100                 { "backend-options", 1, NULL, 'o' },
2101                 { "background", 1, NULL, 'b' },
2102                 { "socket", 1, NULL, 'S' },
2103                 { "idle-time", 1, NULL, 'i' },
2104                 { "shell", 1, NULL, 's' },
2105                 { "xserver", 0, NULL, 'x' },
2106                 { NULL, }
2107         };
2108
2109         while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
2110                 switch (o) {
2111                 case 'b':
2112                         option_background = optarg;
2113                         break;
2114                 case 'B':
2115                         backend = optarg;
2116                         break;
2117                 case 'o':
2118                         backend_options = optarg;
2119                         break;
2120                 case 'S':
2121                         option_socket_name = optarg;
2122                         break;
2123                 case 'i':
2124                         option_idle_time = strtol(optarg, &p, 0);
2125                         if (*p != '\0') {
2126                                 fprintf(stderr,
2127                                         "invalid idle time option: %s\n",
2128                                         optarg);
2129                                 exit(EXIT_FAILURE);
2130                         }
2131                         break;
2132                 case 's':
2133                         shell = optarg;
2134                         break;
2135                 case 'x':
2136                         xserver = 1;
2137                         break;
2138                 }
2139         }
2140
2141         display = wl_display_create();
2142
2143         loop = wl_display_get_event_loop(display);
2144         wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, display);
2145         wl_event_loop_add_signal(loop, SIGINT, on_term_signal, display);
2146         wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal, display);
2147
2148         wl_list_init(&child_process_list);
2149         wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler, NULL);
2150
2151         if (!backend) {
2152                 if (getenv("WAYLAND_DISPLAY"))
2153                         backend = "wayland-backend.so";
2154                 else if (getenv("DISPLAY"))
2155                         backend = "x11-backend.so";
2156                 else if (getenv("OPENWFD"))
2157                         backend = "openwfd-backend.so";
2158                 else
2159                         backend = "drm-backend.so";
2160         }
2161
2162         if (!shell)
2163                 shell = "desktop-shell.so";
2164
2165         backend_init = load_module(backend, "backend_init", &backend_module);
2166         if (!backend_init)
2167                 exit(EXIT_FAILURE);
2168
2169         shell_init = load_module(shell, "shell_init", &shell_module);
2170         if (!shell_init)
2171                 exit(EXIT_FAILURE);
2172
2173         ec = backend_init(display, backend_options);
2174         if (ec == NULL) {
2175                 fprintf(stderr, "failed to create compositor\n");
2176                 exit(EXIT_FAILURE);
2177         }
2178
2179         if (shell_init(ec) < 0)
2180                 exit(EXIT_FAILURE);
2181
2182         if (xserver)
2183                 wlsc_xserver_init(ec);
2184
2185         if (wl_display_add_socket(display, option_socket_name)) {
2186                 fprintf(stderr, "failed to add socket: %m\n");
2187                 exit(EXIT_FAILURE);
2188         }
2189
2190         wl_display_run(display);
2191
2192         if (xserver)
2193                 wlsc_xserver_destroy(ec);
2194
2195         if (ec->has_bind_display)
2196                 ec->unbind_display(ec->display, display);
2197         wl_display_destroy(display);
2198
2199         ec->destroy(ec);
2200
2201         return 0;
2202 }