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