Add clean target for data/ so make clean works again
[profile/ivi/wayland.git] / compositor.c
1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdint.h>
23 #include <stdarg.h>
24 #include <sys/ioctl.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <gdk-pixbuf/gdk-pixbuf.h>
28 #include <math.h>
29 #include <time.h>
30 #include <linux/input.h>
31
32 #include "wayland-server-protocol.h"
33 #include "compositor.h"
34
35 const char *option_background = "background.jpg";
36 int option_connector = 0;
37
38 static const GOptionEntry option_entries[] = {
39         { "background", 'b', 0, G_OPTION_ARG_STRING,
40           &option_background, "Background image" },
41         { "connector", 'c', 0, G_OPTION_ARG_INT,
42           &option_connector, "KMS connector" },
43         { NULL }
44 };
45
46 static void
47 wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
48                                     struct wlsc_surface *surface,
49                                     uint32_t time,
50                                     int32_t x, int32_t y,
51                                     int32_t sx, int32_t sy);
52
53 static void
54 wlsc_matrix_init(struct wlsc_matrix *matrix)
55 {
56         static const struct wlsc_matrix identity = {
57                 { 1, 0, 0, 0,  0, 1, 0, 0,  0, 0, 1, 0,  0, 0, 0, 1 }
58         };
59
60         memcpy(matrix, &identity, sizeof identity);
61 }
62
63 static void
64 wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
65 {
66         struct wlsc_matrix tmp;
67         const GLfloat *row, *column;
68         div_t d;
69         int i, j;
70
71         for (i = 0; i < 16; i++) {
72                 tmp.d[i] = 0;
73                 d = div(i, 4);
74                 row = m->d + d.quot * 4;
75                 column = n->d + d.rem;
76                 for (j = 0; j < 4; j++)
77                         tmp.d[i] += row[j] * column[j * 4];
78         }
79         memcpy(m, &tmp, sizeof tmp);
80 }
81
82 static void
83 wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
84 {
85         struct wlsc_matrix translate = {        
86                 { 1, 0, 0, 0,  0, 1, 0, 0,  0, 0, 1, 0,  x, y, z, 1 }
87         };
88
89         wlsc_matrix_multiply(matrix, &translate);
90 }
91
92 static void
93 wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
94 {
95         struct wlsc_matrix scale = {
96                 { x, 0, 0, 0,  0, y, 0, 0,  0, 0, z, 0,  0, 0, 0, 1 }
97         };
98
99         wlsc_matrix_multiply(matrix, &scale);
100 }
101
102 static void
103 wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
104 {
105         int i, j;
106         struct wlsc_vector t;
107
108         for (i = 0; i < 4; i++) {
109                 t.f[i] = 0;
110                 for (j = 0; j < 4; j++)
111                         t.f[i] += v->f[j] * matrix->d[i + j * 4];
112         }
113         
114         *v = t;
115 }
116
117 static struct wlsc_surface *
118 wlsc_surface_create(struct wlsc_compositor *compositor,
119                     struct wl_visual *visual,
120                     int32_t x, int32_t y, int32_t width, int32_t height)
121 {
122         struct wlsc_surface *surface;
123
124         surface = malloc(sizeof *surface);
125         if (surface == NULL)
126                 return NULL;
127
128         glGenTextures(1, &surface->texture);
129         glBindTexture(GL_TEXTURE_2D, surface->texture);
130         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
131         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
132         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
133         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
134
135         surface->compositor = compositor;
136         surface->visual = visual;
137         surface->x = x;
138         surface->y = y;
139         surface->width = width;
140         surface->height = height;
141         wlsc_matrix_init(&surface->matrix);
142         wlsc_matrix_scale(&surface->matrix, width, height, 1);
143         wlsc_matrix_translate(&surface->matrix, x, y, 0);
144
145         wlsc_matrix_init(&surface->matrix_inv);
146         wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
147         wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
148
149         return surface;
150 }
151
152 static void
153 destroy_surface(struct wl_resource *resource, struct wl_client *client)
154 {
155         struct wlsc_surface *surface =
156                 container_of(resource, struct wlsc_surface, base.base);
157         struct wlsc_compositor *compositor = surface->compositor;
158         struct wlsc_listener *l;
159
160         wl_list_remove(&surface->link);
161         glDeleteTextures(1, &surface->texture);
162
163         wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
164                 l->func(l, surface);
165
166         free(surface);
167
168         wlsc_compositor_schedule_repaint(compositor);
169 }
170
171 static int
172 texture_from_png(const char *filename, int width, int height)
173 {
174         GdkPixbuf *pixbuf;
175         GError *error = NULL;
176         void *data;
177         GLenum format;
178
179         pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
180                                                    width, height,
181                                                    FALSE, &error);
182         if (error != NULL)
183                 return -1;
184
185         data = gdk_pixbuf_get_pixels(pixbuf);
186
187         if (gdk_pixbuf_get_has_alpha(pixbuf))
188                 format = GL_RGBA;
189         else
190                 format = GL_RGB;
191
192         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
193                      width, height, 0, format, GL_UNSIGNED_BYTE, data);
194
195         gdk_pixbuf_unref(pixbuf);
196
197         return 0;
198 }
199
200 static const struct {
201         const char *filename;
202         int hotspot_x, hotspot_y;
203 } pointer_images[] = {
204         { DATADIR "/wayland/bottom_left_corner.png",     6, 30 },
205         { DATADIR "/wayland/bottom_right_corner.png",   28, 28 },
206         { DATADIR "/wayland/bottom_side.png",           16, 20 },
207         { DATADIR "/wayland/grabbing.png",              20, 17 },
208         { DATADIR "/wayland/left_ptr.png",              10,  5 },
209         { DATADIR "/wayland/left_side.png",             10, 20 },
210         { DATADIR "/wayland/right_side.png",            30, 19 },
211         { DATADIR "/wayland/top_left_corner.png",        8,  8 },
212         { DATADIR "/wayland/top_right_corner.png",      26,  8 },
213         { DATADIR "/wayland/top_side.png",              18,  8 },
214         { DATADIR "/wayland/xterm.png",                 15, 15 }
215 };
216
217 static void
218 create_pointer_images(struct wlsc_compositor *ec)
219 {
220         int i, count;
221         GLuint texture;
222         const int width = 32, height = 32;
223
224         EGLint image_attribs[] = {
225                 EGL_WIDTH,              0,
226                 EGL_HEIGHT,             0,
227                 EGL_IMAGE_FORMAT_MESA,  EGL_IMAGE_FORMAT_ARGB8888_MESA,
228                 EGL_IMAGE_USE_MESA,     EGL_IMAGE_USE_SCANOUT_MESA,
229                 EGL_NONE
230         };
231
232         glGenTextures(1, &texture);
233         glBindTexture(GL_TEXTURE_2D, texture);
234         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
235         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
236         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
237         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
238
239         image_attribs[1] = width;
240         image_attribs[3] = height;
241         count = ARRAY_LENGTH(pointer_images);
242         ec->pointer_buffers = malloc(count * sizeof *ec->pointer_buffers);
243         for (i = 0; i < count; i++) {
244                 ec->pointer_buffers[i].image =
245                         eglCreateDRMImageMESA(ec->display, image_attribs);
246                 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
247                                              ec->pointer_buffers[i].image);
248                 texture_from_png(pointer_images[i].filename, width, height);
249                 ec->pointer_buffers[i].visual = &ec->argb_visual;
250                 ec->pointer_buffers[i].width = width;
251                 ec->pointer_buffers[i].height = height;
252         }
253 }
254
255 static struct wlsc_surface *
256 background_create(struct wlsc_output *output, const char *filename)
257 {
258         struct wlsc_surface *background;
259         GdkPixbuf *pixbuf;
260         GError *error = NULL;
261         void *data;
262         GLenum format;
263
264         background = wlsc_surface_create(output->compositor,
265                                          &output->compositor->rgb_visual,
266                                          output->x, output->y,
267                                          output->width, output->height);
268         if (background == NULL)
269                 return NULL;
270
271         pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
272                                                    output->width,
273                                                    output->height,
274                                                    FALSE, &error);
275         if (error != NULL) {
276                 free(background);
277                 return NULL;
278         }
279
280         data = gdk_pixbuf_get_pixels(pixbuf);
281
282         if (gdk_pixbuf_get_has_alpha(pixbuf))
283                 format = GL_RGBA;
284         else
285                 format = GL_RGB;
286
287         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
288                      output->width, output->height, 0,
289                      format, GL_UNSIGNED_BYTE, data);
290
291         return background;
292 }
293
294 static void
295 wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
296 {
297         struct wlsc_compositor *ec = es->compositor;
298         struct wlsc_matrix tmp;
299
300         tmp = es->matrix;
301         wlsc_matrix_multiply(&tmp, &output->matrix);
302         glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
303         glUniform1i(ec->tex_uniform, 0);
304
305         if (es->visual == &ec->argb_visual) {
306                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
307                 glEnable(GL_BLEND);
308         } else if (es->visual == &ec->premultiplied_argb_visual) {
309                 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
310                 glEnable(GL_BLEND);
311         } else {
312                 glDisable(GL_BLEND);
313         }
314
315         glBindTexture(GL_TEXTURE_2D, es->texture);
316         glEnable(GL_TEXTURE_2D);
317         glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
318         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
319                               5 * sizeof(GLfloat), NULL);
320         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
321                               5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
322         glEnableVertexAttribArray(0);
323         glEnableVertexAttribArray(1);
324         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
325 }
326
327 static void
328 wlsc_surface_raise(struct wlsc_surface *surface)
329 {
330         struct wlsc_compositor *compositor = surface->compositor;
331
332         wl_list_remove(&surface->link);
333         wl_list_insert(&compositor->surface_list, &surface->link);
334 }
335
336 static void
337 wlsc_surface_update_matrix(struct wlsc_surface *es)
338 {
339         wlsc_matrix_init(&es->matrix);
340         wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
341         wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
342
343         wlsc_matrix_init(&es->matrix_inv);
344         wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
345         wlsc_matrix_scale(&es->matrix_inv,
346                           1.0 / es->width, 1.0 / es->height, 1);
347 }
348
349 void
350 wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
351 {
352         wl_display_post_frame(compositor->wl_display,
353                               &compositor->base,
354                               compositor->current_frame, msecs);
355
356         wl_event_source_timer_update(compositor->timer_source, 5);
357         compositor->current_frame++;
358 }
359
360 static void
361 wlsc_output_repaint(struct wlsc_output *output)
362 {
363         struct wlsc_compositor *ec = output->compositor;
364         struct wlsc_surface *es;
365         struct wlsc_input_device *eid;
366
367         glViewport(0, 0, output->width, output->height);
368
369         if (output->background)
370                 wlsc_surface_draw(output->background, output);
371         else
372                 glClear(GL_COLOR_BUFFER_BIT);
373
374         wl_list_for_each_reverse(es, &ec->surface_list, link)
375                 wlsc_surface_draw(es, output);
376
377         if (ec->focus)
378                 wl_list_for_each(eid, &ec->input_device_list, link)
379                         wlsc_surface_draw(eid->sprite, output);
380 }
381
382 static void
383 repaint(void *data)
384 {
385         struct wlsc_compositor *ec = data;
386         struct wlsc_output *output;
387
388         if (!ec->repaint_needed) {
389                 ec->repaint_on_timeout = 0;
390                 return;
391         }
392
393         wl_list_for_each(output, &ec->output_list, link)
394                 wlsc_output_repaint(output);
395
396         ec->present(ec);
397
398         ec->repaint_needed = 0;
399 }
400
401 void
402 wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
403 {
404         compositor->repaint_needed = 1;
405         if (compositor->repaint_on_timeout)
406                 return;
407
408         wl_event_source_timer_update(compositor->timer_source, 1);
409         compositor->repaint_on_timeout = 1;
410 }
411
412 static void
413 surface_destroy(struct wl_client *client,
414                 struct wl_surface *surface)
415 {
416         wl_resource_destroy(&surface->base, client);
417 }
418
419 static void
420 surface_attach(struct wl_client *client,
421                struct wl_surface *surface, struct wl_buffer *buffer_base)
422 {
423         struct wlsc_surface *es = (struct wlsc_surface *) surface;
424         struct wlsc_buffer *buffer = (struct wlsc_buffer *) buffer_base;
425
426         glBindTexture(GL_TEXTURE_2D, es->texture);
427         glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, buffer->image);
428         es->visual = buffer->visual;
429 }
430
431 static void
432 surface_map(struct wl_client *client,
433             struct wl_surface *surface,
434             int32_t x, int32_t y, int32_t width, int32_t height)
435 {
436         struct wlsc_surface *es = (struct wlsc_surface *) surface;
437
438         es->x = x;
439         es->y = y;
440         es->width = width;
441         es->height = height;
442
443         wlsc_surface_update_matrix(es);
444 }
445
446 static void
447 surface_damage(struct wl_client *client,
448                struct wl_surface *surface,
449                int32_t x, int32_t y, int32_t width, int32_t height)
450 {
451         /* FIXME: This need to take a damage region, of course. */
452 }
453
454 const static struct wl_surface_interface surface_interface = {
455         surface_destroy,
456         surface_attach,
457         surface_map,
458         surface_damage
459 };
460
461 static void
462 wlsc_input_device_attach(struct wlsc_input_device *device,
463                          struct wlsc_buffer *buffer, int x, int y)
464 {
465         struct wlsc_compositor *ec = device->ec;
466
467         glBindTexture(GL_TEXTURE_2D, device->sprite->texture);
468         glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, buffer->image);
469         device->sprite->visual = buffer->visual;
470         device->hotspot_x = x;
471         device->hotspot_y = y;
472
473         device->sprite->x = device->x - device->hotspot_x;
474         device->sprite->y = device->y - device->hotspot_y;
475         device->sprite->width = buffer->width;
476         device->sprite->height = buffer->height;
477         wlsc_surface_update_matrix(device->sprite);
478
479         wlsc_compositor_schedule_repaint(ec);
480 }
481
482
483 static void
484 wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
485                                     enum wlsc_pointer_type type)
486 {
487         struct wlsc_compositor *compositor = device->ec;
488
489         wlsc_input_device_attach(device,
490                                  &compositor->pointer_buffers[type],
491                                  pointer_images[type].hotspot_x,
492                                  pointer_images[type].hotspot_y);
493 }
494
495 static void
496 wlsc_input_device_start_grab(struct wlsc_input_device *device,
497                              uint32_t time,
498                              enum wlsc_grab_type grab)
499 {
500         device->grab = grab;
501         device->grab_surface = device->pointer_focus;
502         device->grab_dx = device->pointer_focus->x - device->grab_x;
503         device->grab_dy = device->pointer_focus->y - device->grab_y;
504         device->grab_width = device->pointer_focus->width;
505         device->grab_height = device->pointer_focus->height;
506
507         wlsc_input_device_set_pointer_focus(device,
508                                             (struct wlsc_surface *) &wl_grab_surface,
509                                             time, 0, 0, 0, 0);
510 }
511
512 static void
513 shell_move(struct wl_client *client, struct wl_shell *shell,
514            struct wl_surface *surface,
515            struct wl_input_device *device, uint32_t time)
516 {
517         struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
518
519         if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
520             wd->grab_time != time ||
521             &wd->pointer_focus->base != surface)
522                 return;
523
524         wlsc_input_device_start_grab(wd, time, WLSC_DEVICE_GRAB_MOVE);
525         wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
526 }
527
528 static void
529 shell_resize(struct wl_client *client, struct wl_shell *shell,
530              struct wl_surface *surface,
531              struct wl_input_device *device, uint32_t time, uint32_t edges)
532 {
533         struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
534         enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
535
536         if (edges == 0 || edges > 15 ||
537             (edges & 3) == 3 || (edges & 12) == 12)
538                 return;
539
540         if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
541             wd->grab_time != time ||
542             &wd->pointer_focus->base != surface)
543                 return;
544
545         switch (edges) {
546         case WLSC_DEVICE_GRAB_RESIZE_TOP:
547                 pointer = WLSC_POINTER_TOP;
548                 break;
549         case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
550                 pointer = WLSC_POINTER_BOTTOM;
551                 break;
552         case WLSC_DEVICE_GRAB_RESIZE_LEFT:
553                 pointer = WLSC_POINTER_LEFT;
554                 break;
555         case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
556                 pointer = WLSC_POINTER_TOP_LEFT;
557                 break;
558         case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
559                 pointer = WLSC_POINTER_BOTTOM_LEFT;
560                 break;
561         case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
562                 pointer = WLSC_POINTER_RIGHT;
563                 break;
564         case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
565                 pointer = WLSC_POINTER_TOP_RIGHT;
566                 break;
567         case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
568                 pointer = WLSC_POINTER_BOTTOM_RIGHT;
569                 break;
570         }
571
572         wlsc_input_device_start_grab(wd, time, edges);
573         wlsc_input_device_set_pointer_image(wd, pointer);
574 }
575
576 const static struct wl_shell_interface shell_interface = {
577         shell_move,
578         shell_resize
579 };
580
581 static void
582 compositor_create_surface(struct wl_client *client,
583                           struct wl_compositor *compositor, uint32_t id)
584 {
585         struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
586         struct wlsc_surface *surface;
587
588         surface = wlsc_surface_create(ec, NULL, 0, 0, 0, 0);
589         if (surface == NULL) {
590                 wl_client_post_event(client,
591                                      (struct wl_object *) ec->wl_display,
592                                      WL_DISPLAY_NO_MEMORY, 0);
593                 return;
594         }
595
596         wl_list_insert(ec->surface_list.prev, &surface->link);
597         surface->base.base.destroy = destroy_surface;
598         wl_client_add_surface(client, &surface->base,
599                               &surface_interface, id);
600 }
601
602 static void
603 compositor_commit(struct wl_client *client,
604                   struct wl_compositor *compositor, uint32_t key)
605 {
606         struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
607
608         wlsc_compositor_schedule_repaint(ec);
609         wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
610 }
611
612 const static struct wl_compositor_interface compositor_interface = {
613         compositor_create_surface,
614         compositor_commit
615 };
616
617 static void
618 wlsc_surface_transform(struct wlsc_surface *surface,
619                        int32_t x, int32_t y, int32_t *sx, int32_t *sy)
620 {
621         struct wlsc_vector v = { { x, y, 0, 1 } };
622         
623         wlsc_matrix_transform(&surface->matrix_inv, &v);
624         *sx = v.f[0] * surface->width;
625         *sy = v.f[1] * surface->height;
626 }
627
628 static void
629 wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
630                                      struct wlsc_surface *surface,
631                                      uint32_t time)
632 {
633         if (device->keyboard_focus == surface)
634                 return;
635
636         if (device->keyboard_focus &&
637             (!surface || device->keyboard_focus->base.client != surface->base.client))
638                 wl_surface_post_event(&device->keyboard_focus->base,
639                                       &device->base.base,
640                                       WL_INPUT_DEVICE_KEYBOARD_FOCUS,
641                                       time, NULL, &device->keys);
642
643         if (surface)
644                 wl_surface_post_event(&surface->base,
645                                       &device->base.base,
646                                       WL_INPUT_DEVICE_KEYBOARD_FOCUS,
647                                       time, &surface->base, &device->keys);
648
649         device->keyboard_focus = surface;
650 }
651
652 static void
653 wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
654                                     struct wlsc_surface *surface,
655                                     uint32_t time,
656                                     int32_t x, int32_t y,
657                                     int32_t sx, int32_t sy)
658 {
659         if (device->pointer_focus == surface)
660                 return;
661
662         if (device->pointer_focus &&
663             (!surface || device->pointer_focus->base.client != surface->base.client))
664                 wl_surface_post_event(&device->pointer_focus->base,
665                                       &device->base.base,
666                                       WL_INPUT_DEVICE_POINTER_FOCUS,
667                                       time, NULL, 0, 0, 0, 0);
668         if (surface)
669                 wl_surface_post_event(&surface->base,
670                                       &device->base.base,
671                                       WL_INPUT_DEVICE_POINTER_FOCUS,
672                                       time, &surface->base,
673                                       x, y, sx, sy);
674
675         if (!surface)
676                 wlsc_input_device_set_pointer_image(device,
677                                                     WLSC_POINTER_LEFT_PTR);
678
679         device->pointer_focus = surface;
680 }
681
682 static struct wlsc_surface *
683 pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
684 {
685         struct wlsc_compositor *ec = device->ec;
686         struct wlsc_surface *es;
687
688         wl_list_for_each(es, &ec->surface_list, link) {
689                 wlsc_surface_transform(es, device->x, device->y, sx, sy);
690                 if (0 <= *sx && *sx < es->width &&
691                     0 <= *sy && *sy < es->height)
692                         return es;
693         }
694
695         return NULL;
696 }
697
698 static void
699 wl_drag_reset(struct wl_drag *drag);
700 static void
701 wl_drag_set_pointer_focus(struct wl_drag *drag,
702                           struct wlsc_surface *surface, uint32_t time,
703                           int32_t x, int32_t y, int32_t sx, int32_t sy);
704
705 void
706 notify_motion(struct wlsc_input_device *device, uint32_t time, int x, int y)
707 {
708         struct wlsc_surface *es;
709         struct wlsc_compositor *ec = device->ec;
710         struct wlsc_output *output;
711         int32_t sx, sy, width, height;
712
713         /* FIXME: We need some multi head love here. */
714         output = container_of(ec->output_list.next, struct wlsc_output, link);
715         if (x < output->x)
716                 x = 0;
717         if (y < output->y)
718                 y = 0;
719         if (x >= output->x + output->width)
720                 x = output->x + output->width - 1;
721         if (y >= output->y + output->height)
722                 y = output->y + output->height - 1;
723
724         device->x = x;
725         device->y = y;
726
727         switch (device->grab) {
728         case WLSC_DEVICE_GRAB_NONE:
729                 es = pick_surface(device, &sx, &sy);
730                 wlsc_input_device_set_pointer_focus(device, es,
731                                                     time, x, y, sx, sy);
732                 if (es)
733                         wl_surface_post_event(&es->base, &device->base.base,
734                                               WL_INPUT_DEVICE_MOTION,
735                                               time, x, y, sx, sy);
736                 break;
737
738         case WLSC_DEVICE_GRAB_MOTION:
739                 es = device->pointer_focus;
740                 wlsc_surface_transform(es, x, y, &sx, &sy);
741                 wl_surface_post_event(&es->base, &device->base.base,
742                                       WL_INPUT_DEVICE_MOTION,
743                                       time, x, y, sx, sy);
744                 break;
745
746         case WLSC_DEVICE_GRAB_MOVE:
747                 es = device->grab_surface;
748                 es->x = x + device->grab_dx;
749                 es->y = y + device->grab_dy;;
750                 wl_surface_post_event(&es->base, &ec->shell.base,
751                                       WL_SHELL_CONFIGURE,
752                                       time, device->grab,
753                                       &es->base, es->x, es->y,
754                                       es->width, es->height);
755
756                 wlsc_surface_update_matrix(es);
757
758                 break;
759
760         case WLSC_DEVICE_GRAB_RESIZE_TOP:
761         case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
762         case WLSC_DEVICE_GRAB_RESIZE_LEFT:
763         case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
764         case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
765         case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
766         case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
767         case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
768         case WLSC_DEVICE_GRAB_RESIZE_MASK:
769                 es = device->grab_surface;
770
771                 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_LEFT) {
772                         sx = x + device->grab_dx;
773                         width = device->grab_x - x + device->grab_width;
774                 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_RIGHT) {
775                         sx = device->grab_x + device->grab_dx;
776                         width = x - device->grab_x + device->grab_width;
777                 } else {
778                         sx = device->grab_x + device->grab_dx;
779                         width = device->grab_width;
780                 }
781
782                 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_TOP) {
783                         sy = y + device->grab_dy;
784                         height = device->grab_y - y + device->grab_height;
785                 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_BOTTOM) {
786                         sy = device->grab_y + device->grab_dy;
787                         height = y - device->grab_y + device->grab_height;
788                 } else {
789                         sy = device->grab_y + device->grab_dy;
790                         height = device->grab_height;
791                 }
792
793                 wl_surface_post_event(&es->base, &ec->shell.base,
794                                       WL_SHELL_CONFIGURE, time, device->grab,
795                                       &es->base, sx, sy, width, height);
796                 break;
797
798         case WLSC_DEVICE_GRAB_DRAG:
799                 es = pick_surface(device, &sx, &sy);
800                 wl_drag_set_pointer_focus(&device->drag,
801                                           es, time, x, y, sx, sy);
802                 if (es)
803                         wl_surface_post_event(&es->base, &device->drag.base,
804                                               WL_DRAG_MOTION,
805                                               time, x, y, sx, sy);
806                 break;
807
808         }
809
810         device->sprite->x = device->x - device->hotspot_x;
811         device->sprite->y = device->y - device->hotspot_y;
812         wlsc_surface_update_matrix(device->sprite);
813
814         wlsc_compositor_schedule_repaint(device->ec);
815 }
816
817 static void
818 wlsc_input_device_end_grab(struct wlsc_input_device *device, uint32_t time)
819 {
820         struct wl_drag *drag = &device->drag;
821         struct wlsc_surface *es;
822         int32_t sx, sy;
823
824         switch (device->grab) {
825         case WLSC_DEVICE_GRAB_DRAG:
826                 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
827                 wl_surface_post_event(drag->source, &drag->base,
828                                       WL_DRAG_FINISH);
829                 wl_drag_reset(drag);
830                 break;
831         default:
832                 break;
833         }
834
835         device->grab = WLSC_DEVICE_GRAB_NONE;
836         es = pick_surface(device, &sx, &sy);
837         wlsc_input_device_set_pointer_focus(device, es, time,
838                                             device->x, device->y, sx, sy);
839 }
840
841 void
842 notify_button(struct wlsc_input_device *device,
843               uint32_t time, int32_t button, int32_t state)
844 {
845         struct wlsc_surface *surface;
846         struct wlsc_compositor *compositor = device->ec;
847
848         surface = device->pointer_focus;
849         if (surface) {
850                 if (state && device->grab == WLSC_DEVICE_GRAB_NONE) {
851                         wlsc_surface_raise(surface);
852                         device->grab = WLSC_DEVICE_GRAB_MOTION;
853                         device->grab_button = button;
854                         device->grab_time = time;
855                         device->grab_x = device->x;
856                         device->grab_y = device->y;
857                         wlsc_input_device_set_keyboard_focus(device,
858                                                              surface, time);
859                 }
860
861                 if (state && button == BTN_LEFT &&
862                     device->grab == WLSC_DEVICE_GRAB_MOTION &&
863                     (device->modifier_state & MODIFIER_SUPER))
864                         shell_move(NULL, (struct wl_shell *) &compositor->shell,
865                                    &surface->base, &device->base, time);
866                 else if (state && button == BTN_MIDDLE &&
867                            device->grab == WLSC_DEVICE_GRAB_MOTION &&
868                            (device->modifier_state & MODIFIER_SUPER))
869                         shell_resize(NULL, (struct wl_shell *) &compositor->shell,
870
871                                      &surface->base, &device->base, time,
872                                      WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT);
873                 else if (device->grab == WLSC_DEVICE_GRAB_NONE ||
874                          device->grab == WLSC_DEVICE_GRAB_MOTION)
875                         wl_surface_post_event(&surface->base,
876                                               &device->base.base,
877                                               WL_INPUT_DEVICE_BUTTON,
878                                               time, button, state);
879
880                 if (!state &&
881                     device->grab != WLSC_DEVICE_GRAB_NONE &&
882                     device->grab_button == button) {
883                         wlsc_input_device_end_grab(device, time);
884                 }
885
886                 wlsc_compositor_schedule_repaint(compositor);
887         }
888 }
889
890 void
891 notify_key(struct wlsc_input_device *device,
892            uint32_t time, uint32_t key, uint32_t state)
893 {
894         uint32_t *k, *end;
895         uint32_t modifier;
896
897         switch (key | device->modifier_state) {
898         case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
899                 kill(0, SIGTERM);
900                 return;
901         }
902
903         switch (key) {
904         case KEY_LEFTCTRL:
905         case KEY_RIGHTCTRL:
906                 modifier = MODIFIER_CTRL;
907                 break;
908
909         case KEY_LEFTALT:
910         case KEY_RIGHTALT:
911                 modifier = MODIFIER_ALT;
912                 break;
913
914         case KEY_LEFTMETA:
915         case KEY_RIGHTMETA:
916                 modifier = MODIFIER_SUPER;
917                 break;
918
919         default:
920                 modifier = 0;
921                 break;
922         }
923
924         if (state)
925                 device->modifier_state |= modifier;
926         else
927                 device->modifier_state &= ~modifier;
928
929         end = device->keys.data + device->keys.size;
930         for (k = device->keys.data; k < end; k++) {
931                 if (*k == key)
932                         *k = *--end;
933         }
934         device->keys.size = (void *) end - device->keys.data;
935         if (state) {
936                 k = wl_array_add(&device->keys, sizeof *k);
937                 *k = key;
938         }
939
940         if (device->keyboard_focus != NULL)
941                 wl_surface_post_event(&device->keyboard_focus->base,
942                                       &device->base.base,
943                                       WL_INPUT_DEVICE_KEY, time, key, state);
944 }
945
946 static void
947 input_device_attach(struct wl_client *client,
948                     struct wl_input_device *device_base,
949                     struct wl_buffer *buffer_base, int32_t x, int32_t y)
950 {
951         struct wlsc_input_device *device =
952                 (struct wlsc_input_device *) device_base;
953         struct wlsc_buffer *buffer = (struct wlsc_buffer *) buffer_base;
954
955         if (device->pointer_focus == NULL ||
956             device->pointer_focus->base.client != client)
957                 return;
958
959         if (buffer == NULL) {
960                 wlsc_input_device_set_pointer_image(device,
961                                                     WLSC_POINTER_LEFT_PTR);
962                 return;
963         }
964
965         wlsc_input_device_attach(device, buffer, x, y);
966 }
967
968 const static struct wl_input_device_interface input_device_interface = {
969         input_device_attach,
970 };
971
972 static uint32_t
973 get_time(void)
974 {
975         struct timeval tv;
976
977         gettimeofday(&tv, NULL);
978
979         return tv.tv_sec * 1000 + tv.tv_usec / 1000;
980 }
981
982 static void
983 handle_surface_destroy(struct wlsc_listener *listener,
984                        struct wlsc_surface *surface)
985 {
986         struct wlsc_input_device *device =
987                 container_of(listener, struct wlsc_input_device, listener);
988         uint32_t time = get_time();
989
990         if (device->keyboard_focus == surface)
991                 wlsc_input_device_set_keyboard_focus(device, NULL, time);
992         if (device->pointer_focus == surface ||
993             (&device->pointer_focus->base == &wl_grab_surface &&
994              device->grab_surface == surface))
995                 wlsc_input_device_end_grab(device, time);
996 }
997
998 static void
999 wl_drag_set_pointer_focus(struct wl_drag *drag,
1000                           struct wlsc_surface *surface, uint32_t time,
1001                           int32_t x, int32_t y, int32_t sx, int32_t sy)
1002 {
1003         if (drag->pointer_focus == &surface->base)
1004                 return;
1005
1006         if (drag->pointer_focus &&
1007             (!surface || drag->pointer_focus->client != surface->base.client))
1008                 wl_surface_post_event(drag->pointer_focus,
1009                                       &drag->base,
1010                                       WL_DRAG_POINTER_FOCUS,
1011                                       time, NULL, 0, 0, 0, 0);
1012         if (surface)
1013                 wl_surface_post_event(&surface->base,
1014                                       &drag->base,
1015                                       WL_DRAG_POINTER_FOCUS,
1016                                       time, &surface->base,
1017                                       x, y, sx, sy);
1018
1019         drag->pointer_focus = &surface->base;
1020 }
1021
1022 static void
1023 wl_drag_reset(struct wl_drag *drag)
1024 {
1025         char **p, **end;
1026
1027         end = drag->types.data + drag->types.size;
1028         for (p = drag->types.data; p < end; p++)
1029                 free(*p);
1030         wl_array_release(&drag->types);
1031         wl_array_init(&drag->types);
1032
1033         drag->source = NULL;
1034         drag->target = NULL;
1035         drag->time = 0;
1036         drag->pointer_focus = NULL;
1037 }
1038
1039 static void
1040 drag_prepare(struct wl_client *client,
1041              struct wl_drag *drag, struct wl_surface *surface, uint32_t time,
1042              struct wl_buffer *buffer, int32_t hotspot_x, int32_t hotspot_y)
1043 {
1044         struct wlsc_input_device *device =
1045                 (struct wlsc_input_device *) drag->input_device;
1046
1047         if (&device->pointer_focus->base != surface ||
1048             device->grab_time != time)
1049                 return;
1050
1051         wl_drag_reset(drag);
1052         drag->source = surface;
1053         drag->time = time;
1054         drag->buffer = buffer;
1055         drag->hotspot_x = hotspot_x;
1056         drag->hotspot_y = hotspot_y;
1057 }
1058
1059 static void
1060 drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type)
1061 {
1062         struct wl_display *display = wl_client_get_display (client);
1063         struct wlsc_input_device *device =
1064                 (struct wlsc_input_device *) drag->input_device;
1065         char **p;
1066
1067         if (drag->source == NULL ||
1068             drag->source->client != client ||
1069             device->grab != WLSC_DEVICE_GRAB_MOTION ||
1070             &device->pointer_focus->base != drag->source ||
1071             device->grab_time != drag->time)
1072                 return;
1073
1074         p = wl_array_add(&drag->types, sizeof *p);
1075         if (p)
1076                 *p = strdup(type);
1077         if (!p || !*p)
1078                 wl_client_post_event(client,
1079                                      (struct wl_object *) display,
1080                                      WL_DISPLAY_NO_MEMORY);
1081 }
1082
1083 static void
1084 drag_activate(struct wl_client *client,
1085               struct wl_drag *drag)
1086 {
1087         struct wlsc_input_device *device =
1088                 (struct wlsc_input_device *) drag->input_device;
1089         struct wlsc_surface *surface;
1090         int32_t sx, sy;
1091
1092         if (drag->source == NULL ||
1093             drag->source->client != client ||
1094             device->grab != WLSC_DEVICE_GRAB_MOTION ||
1095             &device->pointer_focus->base != drag->source ||
1096             device->grab_time != drag->time)
1097                 return;
1098
1099         wlsc_input_device_start_grab(device, drag->time,
1100                                      WLSC_DEVICE_GRAB_DRAG);
1101         wlsc_input_device_attach(device, (struct wlsc_buffer *) drag->buffer,
1102                                  drag->hotspot_x, drag->hotspot_y);
1103
1104         surface = pick_surface(device, &sx, &sy);
1105         wl_drag_set_pointer_focus(&device->drag, surface, drag->time,
1106                                   device->x, device->y, sx, sy);
1107 }
1108
1109 static void
1110 drag_cancel(struct wl_client *client, struct wl_drag *drag)
1111 {
1112         struct wlsc_input_device *device =
1113                 (struct wlsc_input_device *) drag->input_device;
1114
1115         if (drag->source == NULL ||
1116             drag->source->client != client ||
1117             device->grab != WLSC_DEVICE_GRAB_DRAG)
1118                 return;
1119
1120         wlsc_input_device_end_grab(device, get_time());
1121 }
1122
1123 static void
1124 drag_send(struct wl_client *client,
1125           struct wl_drag *drag, struct wl_array *contents)
1126 {
1127         wl_client_post_event(client, &drag->base, WL_DRAG_DROP, contents);
1128 }
1129
1130 static void
1131 drag_accept(struct wl_client *client,
1132             struct wl_drag *drag, const char *type)
1133 {
1134         char **p, **end;
1135
1136         if (drag->pointer_focus->client != client)
1137                 return;
1138
1139         /* FIXME: We need a serial number here to correlate the accept
1140          * request with a pointer_focus/motion event. */
1141         drag->target = client;
1142
1143         end = drag->types.data + drag->types.size;
1144         for (p = drag->types.data; p < end; p++)
1145                 if (strcmp(*p, type) == 0)
1146                     drag->type = *p;
1147
1148         wl_surface_post_event(drag->source, &drag->base,
1149                               WL_DRAG_TARGET, drag->type);
1150 }
1151
1152 static const struct wl_drag_interface drag_interface = {
1153         drag_prepare,
1154         drag_offer,
1155         drag_activate,
1156         drag_cancel,
1157         drag_send,
1158         drag_accept
1159 };
1160
1161 static void
1162 wl_drag_post_device(struct wl_client *client, struct wl_object *global)
1163 {
1164         struct wl_drag *drag = container_of(global, struct wl_drag, base);
1165
1166         wl_client_post_event(client, global,
1167                              WL_DRAG_DEVICE, drag->input_device);
1168 }
1169
1170 static void
1171 wl_drag_init(struct wl_drag *drag,
1172              struct wl_display *display, struct wl_input_device *input_device)
1173 {
1174         drag->base.interface = &wl_drag_interface;
1175         drag->base.implementation = (void (**)(void))
1176                 &drag_interface;
1177
1178         wl_display_add_object(display, &drag->base);
1179         wl_display_add_global(display, &drag->base, wl_drag_post_device);
1180
1181         drag->source = NULL;
1182         wl_array_init(&drag->types);
1183         drag->input_device = input_device;
1184 }
1185
1186 void
1187 wlsc_input_device_init(struct wlsc_input_device *device,
1188                        struct wlsc_compositor *ec)
1189 {
1190         device->base.base.interface = &wl_input_device_interface;
1191         device->base.base.implementation =
1192                 (void (**)(void)) &input_device_interface;
1193         wl_display_add_object(ec->wl_display, &device->base.base);
1194         wl_display_add_global(ec->wl_display, &device->base.base, NULL);
1195
1196         wl_drag_init(&device->drag, ec->wl_display, &device->base);
1197
1198         device->x = 100;
1199         device->y = 100;
1200         device->ec = ec;
1201         device->sprite = wlsc_surface_create(ec, &ec->argb_visual,
1202                                              device->x, device->y, 32, 32);
1203         device->hotspot_x = 16;
1204         device->hotspot_y = 16;
1205
1206         device->listener.func = handle_surface_destroy;
1207         wl_list_insert(ec->surface_destroy_listener_list.prev,
1208                        &device->listener.link);
1209         wl_list_insert(ec->input_device_list.prev, &device->link);
1210
1211         wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
1212 }
1213
1214 static void
1215 wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
1216 {
1217         struct wlsc_output *output =
1218                 container_of(global, struct wlsc_output, base);
1219
1220         wl_client_post_event(client, global,
1221                              WL_OUTPUT_GEOMETRY,
1222                              output->width, output->height);
1223 }
1224
1225 static const char vertex_shader[] =
1226         "uniform mat4 proj;\n"
1227         "attribute vec4 position;\n"
1228         "attribute vec2 texcoord;\n"
1229         "varying vec2 v_texcoord;\n"
1230         "void main()\n"
1231         "{\n"
1232         "   gl_Position = proj * position;\n"
1233         "   v_texcoord = texcoord;\n"
1234         "}\n";
1235
1236 static const char fragment_shader[] =
1237         /* "precision mediump float;\n" */
1238         "varying vec2 v_texcoord;\n"
1239         "uniform sampler2D tex;\n"
1240         "void main()\n"
1241         "{\n"
1242         "   gl_FragColor = texture2D(tex, v_texcoord)\n;"
1243         "}\n";
1244
1245 static int
1246 init_shaders(struct wlsc_compositor *ec)
1247 {
1248         GLuint v, f, program;
1249         const char *p;
1250         char msg[512];
1251         GLfloat vertices[4 * 5];
1252         GLint status;
1253
1254         p = vertex_shader;
1255         v = glCreateShader(GL_VERTEX_SHADER);
1256         glShaderSource(v, 1, &p, NULL);
1257         glCompileShader(v);
1258         glGetShaderiv(v, GL_COMPILE_STATUS, &status);
1259         if (!status) {
1260                 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
1261                 fprintf(stderr, "vertex shader info: %s\n", msg);
1262                 return -1;
1263         }
1264
1265         p = fragment_shader;
1266         f = glCreateShader(GL_FRAGMENT_SHADER);
1267         glShaderSource(f, 1, &p, NULL);
1268         glCompileShader(f);
1269         glGetShaderiv(f, GL_COMPILE_STATUS, &status);
1270         if (!status) {
1271                 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
1272                 fprintf(stderr, "fragment shader info: %s\n", msg);
1273                 return -1;
1274         }
1275
1276         program = glCreateProgram();
1277         glAttachShader(program, v);
1278         glAttachShader(program, f);
1279         glBindAttribLocation(program, 0, "position");
1280         glBindAttribLocation(program, 1, "texcoord");
1281
1282         glLinkProgram(program);
1283         glGetProgramiv(program, GL_LINK_STATUS, &status);
1284         if (!status) {
1285                 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
1286                 fprintf(stderr, "link info: %s\n", msg);
1287                 return -1;
1288         }
1289
1290         glUseProgram(program);
1291         ec->proj_uniform = glGetUniformLocation(program, "proj");
1292         ec->tex_uniform = glGetUniformLocation(program, "tex");
1293
1294         vertices[ 0] = 0.0;
1295         vertices[ 1] = 0.0;
1296         vertices[ 2] = 0.0;
1297         vertices[ 3] = 0.0;
1298         vertices[ 4] = 0.0;
1299
1300         vertices[ 5] = 0.0;
1301         vertices[ 6] = 1.0;
1302         vertices[ 7] = 0.0;
1303         vertices[ 8] = 0.0;
1304         vertices[ 9] = 1.0;
1305
1306         vertices[10] = 1.0;
1307         vertices[11] = 0.0;
1308         vertices[12] = 0.0;
1309         vertices[13] = 1.0;
1310         vertices[14] = 0.0;
1311
1312         vertices[15] = 1.0;
1313         vertices[16] = 1.0;
1314         vertices[17] = 0.0;
1315         vertices[18] = 1.0;
1316         vertices[19] = 1.0;
1317
1318         glGenBuffers(1, &ec->vbo);
1319         glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
1320         glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
1321
1322         return 0;
1323 }
1324
1325 static const struct wl_interface visual_interface = {
1326         "visual", 1,
1327 };
1328
1329 static void
1330 add_visuals(struct wlsc_compositor *ec)
1331 {
1332         ec->argb_visual.base.interface = &visual_interface;
1333         ec->argb_visual.base.implementation = NULL;
1334         wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
1335         wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
1336
1337         ec->premultiplied_argb_visual.base.interface = &visual_interface;
1338         ec->premultiplied_argb_visual.base.implementation = NULL;
1339         wl_display_add_object(ec->wl_display,
1340                               &ec->premultiplied_argb_visual.base);
1341         wl_display_add_global(ec->wl_display,
1342                               &ec->premultiplied_argb_visual.base, NULL);
1343
1344         ec->rgb_visual.base.interface = &visual_interface;
1345         ec->rgb_visual.base.implementation = NULL;
1346         wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
1347         wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1348 }
1349
1350 void
1351 wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1352                  int x, int y, int width, int height)
1353 {
1354         output->compositor = c;
1355         output->x = x;
1356         output->y = y;
1357         output->width = width;
1358         output->height = height;
1359
1360         output->background =
1361                 background_create(output, option_background);
1362
1363         wlsc_matrix_init(&output->matrix);
1364         wlsc_matrix_translate(&output->matrix,
1365                               -output->x - output->width / 2.0,
1366                               -output->y - output->height / 2.0, 0);
1367         wlsc_matrix_scale(&output->matrix,
1368                           2.0 / output->width, 2.0 / output->height, 1);
1369
1370         output->base.interface = &wl_output_interface;
1371         wl_display_add_object(c->wl_display, &output->base);
1372         wl_display_add_global(c->wl_display, &output->base,
1373                               wlsc_output_post_geometry);
1374 }
1375
1376 int
1377 wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1378 {
1379         struct wl_event_loop *loop;
1380
1381         ec->wl_display = display;
1382
1383         wl_display_set_compositor(display, &ec->base, &compositor_interface); 
1384
1385         ec->shell.base.interface = &wl_shell_interface;
1386         ec->shell.base.implementation = (void (**)(void)) &shell_interface;
1387         wl_display_add_object(display, &ec->shell.base);
1388         if (wl_display_add_global(display, &ec->shell.base, NULL))
1389                 return -1;
1390
1391         add_visuals(ec);
1392
1393         wl_list_init(&ec->surface_list);
1394         wl_list_init(&ec->input_device_list);
1395         wl_list_init(&ec->output_list);
1396         wl_list_init(&ec->surface_destroy_listener_list);
1397
1398         create_pointer_images(ec);
1399
1400         screenshooter_create(ec);
1401
1402         glGenFramebuffers(1, &ec->fbo);
1403         glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1404         glActiveTexture(GL_TEXTURE0);
1405         if (init_shaders(ec) < 0)
1406                 return -1;
1407
1408         loop = wl_display_get_event_loop(ec->wl_display);
1409         ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
1410         wlsc_compositor_schedule_repaint(ec);
1411
1412         return 0;
1413 }
1414
1415 /* The plan here is to generate a random anonymous socket name and
1416  * advertise that through a service on the session dbus.
1417  */
1418 static const char socket_name[] = "\0wayland";
1419
1420 int main(int argc, char *argv[])
1421 {
1422         struct wl_display *display;
1423         struct wlsc_compositor *ec;
1424         GError *error = NULL;
1425         GOptionContext *context;
1426
1427         g_type_init(); /* GdkPixbuf needs this, it seems. */
1428
1429         context = g_option_context_new(NULL);
1430         g_option_context_add_main_entries(context, option_entries, "Wayland");
1431         if (!g_option_context_parse(context, &argc, &argv, &error)) {
1432                 fprintf(stderr, "option parsing failed: %s\n", error->message);
1433                 exit(EXIT_FAILURE);
1434         }
1435
1436         display = wl_display_create();
1437
1438         if (getenv("DISPLAY"))
1439                 ec = x11_compositor_create(display);
1440         else
1441                 ec = drm_compositor_create(display);
1442
1443         if (ec == NULL) {
1444                 fprintf(stderr, "failed to create compositor\n");
1445                 exit(EXIT_FAILURE);
1446         }
1447                 
1448         if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
1449                 fprintf(stderr, "failed to add socket: %m\n");
1450                 exit(EXIT_FAILURE);
1451         }
1452
1453         wl_display_run(display);
1454
1455         return 0;
1456 }