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