Split native drm part of compositor out
[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 <cairo.h>
28 #include <gdk-pixbuf/gdk-pixbuf.h>
29 #include <math.h>
30 #include <time.h>
31 #include <linux/input.h>
32
33 #define GL_GLEXT_PROTOTYPES
34 #define EGL_EGLEXT_PROTOTYPES
35 #include <GLES2/gl2.h>
36 #include <GLES2/gl2ext.h>
37 #include <EGL/egl.h>
38 #include <EGL/eglext.h>
39
40 #include "wayland.h"
41 #include "wayland-protocol.h"
42 #include "cairo-util.h"
43 #include "compositor.h"
44
45 const char *option_background = "background.jpg";
46 int option_connector = 0;
47
48 static const GOptionEntry option_entries[] = {
49         { "background", 'b', 0, G_OPTION_ARG_STRING,
50           &option_background, "Background image" },
51         { "connector", 'c', 0, G_OPTION_ARG_INT,
52           &option_connector, "KMS connector" },
53         { NULL }
54 };
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_rotate(struct wlsc_matrix *matrix,
108                    GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
109 {
110         GLfloat c = cos(angle);
111         GLfloat s = sin(angle);
112         struct wlsc_matrix rotate = {
113                 { x * x * (1 - c) + c,     y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
114                   x * y * (1 - c) - z * s, y * y * (1 - c) + c,     y * z * (1 - c) + x * s, 0, 
115                   x * z * (1 - c) + y * s, y * z * (1 - c) - x * s, z * z * (1 - c) + c,     0,
116                   0, 0, 0, 1 }
117         };
118
119         wlsc_matrix_multiply(matrix, &rotate);
120 }
121
122 static void
123 wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
124 {
125         int i, j;
126         struct wlsc_vector t;
127
128         for (i = 0; i < 4; i++) {
129                 t.f[i] = 0;
130                 for (j = 0; j < 4; j++)
131                         t.f[i] += v->f[j] * matrix->d[i + j * 4];
132         }
133         
134         *v = t;
135 }
136
137 static void
138 wlsc_surface_init(struct wlsc_surface *surface,
139                   struct wlsc_compositor *compositor, struct wl_visual *visual,
140                   int32_t x, int32_t y, int32_t width, int32_t height)
141 {
142         glGenTextures(1, &surface->texture);
143         surface->compositor = compositor;
144         surface->visual = visual;
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
154 static struct wlsc_surface *
155 wlsc_surface_create_from_cairo_surface(struct wlsc_compositor *ec,
156                                       cairo_surface_t *surface,
157                                       int x, int y, int width, int height)
158 {
159         struct wlsc_surface *es;
160         int stride;
161         void *data;
162
163         stride = cairo_image_surface_get_stride(surface);
164         data = cairo_image_surface_get_data(surface);
165
166         es = malloc(sizeof *es);
167         if (es == NULL)
168                 return NULL;
169
170         wlsc_surface_init(es, ec, &ec->premultiplied_argb_visual,
171                           x, y, width, height);
172         glBindTexture(GL_TEXTURE_2D, es->texture);
173         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
174         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
175         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
176         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
177         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
178                      GL_RGBA, GL_UNSIGNED_BYTE, data);
179
180         return es;
181 }
182
183 static void
184 wlsc_surface_destroy(struct wlsc_surface *surface,
185                      struct wlsc_compositor *compositor)
186 {
187         struct wlsc_listener *l;
188
189         wl_list_remove(&surface->link);
190         glDeleteTextures(1, &surface->texture);
191         wl_client_remove_surface(surface->base.client, &surface->base);
192
193         wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
194                 l->func(l, surface);
195
196         free(surface);
197 }
198
199 static void
200 pointer_path(cairo_t *cr, int x, int y)
201 {
202         const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
203         const int width = 16, height = 16;
204
205         cairo_move_to(cr, x, y);
206         cairo_line_to(cr, x + tx, y + ty);
207         cairo_line_to(cr, x + dx, y + dy);
208         cairo_line_to(cr, x + width - end, y + height);
209         cairo_line_to(cr, x + width, y + height - end);
210         cairo_line_to(cr, x + dy, y + dx);
211         cairo_line_to(cr, x + ty, y + tx);
212         cairo_close_path(cr);
213 }
214
215 static struct wlsc_surface *
216 pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
217 {
218         struct wlsc_surface *es;
219         const int hotspot_x = 16, hotspot_y = 16;
220         cairo_surface_t *surface;
221         cairo_t *cr;
222
223         surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
224                                              width, height);
225
226         cr = cairo_create(surface);
227         pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
228         cairo_set_line_width(cr, 2);
229         cairo_set_source_rgb(cr, 0, 0, 0);
230         cairo_stroke_preserve(cr);
231         cairo_fill(cr);
232         blur_surface(surface, width);
233
234         pointer_path(cr, hotspot_x, hotspot_y);
235         cairo_stroke_preserve(cr);
236         cairo_set_source_rgb(cr, 1, 1, 1);
237         cairo_fill(cr);
238         cairo_destroy(cr);
239
240         es = wlsc_surface_create_from_cairo_surface(ec,
241                                                    surface,
242                                                    x - hotspot_x,
243                                                    y - hotspot_y,
244                                                    width, height);
245         
246         cairo_surface_destroy(surface);
247
248         return es;
249 }
250
251 static struct wlsc_surface *
252 background_create(struct wlsc_output *output, const char *filename)
253 {
254         struct wlsc_surface *background;
255         GdkPixbuf *pixbuf;
256         GError *error = NULL;
257         void *data;
258         GLenum format;
259
260         background = malloc(sizeof *background);
261         if (background == NULL)
262                 return NULL;
263         
264         g_type_init();
265
266         pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
267                                                    output->width,
268                                                    output->height,
269                                                    FALSE, &error);
270         if (error != NULL) {
271                 free(background);
272                 return NULL;
273         }
274
275         data = gdk_pixbuf_get_pixels(pixbuf);
276
277         wlsc_surface_init(background, output->compositor,
278                           &output->compositor->rgb_visual,
279                           output->x, output->y, output->width, output->height);
280
281         glBindTexture(GL_TEXTURE_2D, background->texture);
282         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
283         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
284         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
285         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
286
287         if (gdk_pixbuf_get_has_alpha(pixbuf))
288                 format = GL_RGBA;
289         else
290                 format = GL_RGB;
291
292         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
293                      output->width, output->height, 0,
294                      format, GL_UNSIGNED_BYTE, data);
295
296         return background;
297 }
298
299 static void
300 wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
301 {
302         struct wlsc_compositor *ec = es->compositor;
303         struct wlsc_matrix tmp;
304
305         tmp = es->matrix;
306         wlsc_matrix_multiply(&tmp, &output->matrix);
307         glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
308         glUniform1i(ec->tex_uniform, 0);
309
310         if (es->visual == &ec->argb_visual) {
311                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
312                 glEnable(GL_BLEND);
313         } else if (es->visual == &ec->premultiplied_argb_visual) {
314                 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
315                 glEnable(GL_BLEND);
316         } else {
317                 glDisable(GL_BLEND);
318         }
319
320         glBindTexture(GL_TEXTURE_2D, es->texture);
321         glEnable(GL_TEXTURE_2D);
322         glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
323         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
324                               5 * sizeof(GLfloat), NULL);
325         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
326                               5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
327         glEnableVertexAttribArray(0);
328         glEnableVertexAttribArray(1);
329         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
330 }
331
332 static void
333 wlsc_surface_raise(struct wlsc_surface *surface)
334 {
335         struct wlsc_compositor *compositor = surface->compositor;
336
337         wl_list_remove(&surface->link);
338         wl_list_insert(compositor->surface_list.prev, &surface->link);
339 }
340
341 static void
342 wlsc_surface_lower(struct wlsc_surface *surface)
343 {
344         struct wlsc_compositor *compositor = surface->compositor;
345
346         wl_list_remove(&surface->link);
347         wl_list_insert(&compositor->surface_list, &surface->link);
348 }
349
350 void
351 wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
352 {
353         wl_display_post_frame(compositor->wl_display,
354                               &compositor->base,
355                               compositor->current_frame, msecs);
356
357         wl_event_source_timer_update(compositor->timer_source, 5);
358         compositor->repaint_on_timeout = 1;
359
360         compositor->current_frame++;
361 }
362
363 static void
364 wlsc_output_repaint(struct wlsc_output *output)
365 {
366         struct wlsc_compositor *ec = output->compositor;
367         struct wlsc_surface *es;
368         struct wlsc_input_device *eid;
369
370         glViewport(0, 0, output->width, output->height);
371
372         if (output->background)
373                 wlsc_surface_draw(output->background, output);
374         else
375                 glClear(GL_COLOR_BUFFER_BIT);
376
377         wl_list_for_each(es, &ec->surface_list, link)
378                 wlsc_surface_draw(es, output);
379
380         wl_list_for_each(eid, &ec->input_device_list, link)
381                 wlsc_surface_draw(eid->sprite, output);
382
383         output->current ^= 1;
384
385         glFramebufferRenderbuffer(GL_FRAMEBUFFER,
386                                   GL_COLOR_ATTACHMENT0,
387                                   GL_RENDERBUFFER,
388                                   output->rbo[output->current]);
389 }
390
391 static void
392 repaint(void *data)
393 {
394         struct wlsc_compositor *ec = data;
395         struct wlsc_output *output;
396
397         if (!ec->repaint_needed) {
398                 ec->repaint_on_timeout = 0;
399                 return;
400         }
401
402         wl_list_for_each(output, &ec->output_list, link)
403                 wlsc_output_repaint(output);
404
405         wlsc_compositor_present_drm(ec);
406
407         ec->repaint_needed = 0;
408 }
409
410 static void
411 wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
412 {
413         compositor->repaint_needed = 1;
414         if (compositor->repaint_on_timeout)
415                 return;
416
417         wl_event_source_timer_update(compositor->timer_source, 1);
418         compositor->repaint_on_timeout = 1;
419 }
420
421 static void
422 surface_destroy(struct wl_client *client,
423                 struct wl_surface *surface)
424 {
425         struct wlsc_surface *es = (struct wlsc_surface *) surface;
426         struct wlsc_compositor *ec = es->compositor;
427
428         wlsc_surface_destroy(es, ec);
429
430         wlsc_compositor_schedule_repaint(ec);
431 }
432
433 static void
434 surface_attach(struct wl_client *client,
435                struct wl_surface *surface, uint32_t name, 
436                uint32_t width, uint32_t height, uint32_t stride,
437                struct wl_object *visual)
438 {
439         struct wlsc_surface *es = (struct wlsc_surface *) surface;
440         struct wlsc_compositor *ec = es->compositor;
441         EGLint attribs[] = {
442                 EGL_WIDTH,              0,
443                 EGL_HEIGHT,             0,
444                 EGL_IMAGE_STRIDE_MESA,  0,
445                 EGL_IMAGE_FORMAT_MESA,  EGL_IMAGE_FORMAT_ARGB8888_MESA,
446                 EGL_NONE
447         };
448
449         es->width = width;
450         es->height = height;
451
452         if (visual == &ec->argb_visual.base)
453                 es->visual = &ec->argb_visual;
454         else if (visual == &ec->premultiplied_argb_visual.base)
455                 es->visual = &ec->premultiplied_argb_visual;
456         else if (visual == &ec->rgb_visual.base)
457                 es->visual = &ec->rgb_visual;
458         else
459                 /* FIXME: Smack client with an exception event */;
460
461         glBindTexture(GL_TEXTURE_2D, es->texture);
462         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
463         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
464         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
465         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
466
467         if (es->image)
468                 eglDestroyImageKHR(ec->display, es->image);
469
470         attribs[1] = width;
471         attribs[3] = height;
472         attribs[5] = stride / 4;
473
474         es->image = eglCreateImageKHR(ec->display, ec->context,
475                                       EGL_DRM_IMAGE_MESA,
476                                       (EGLClientBuffer) name, attribs);
477         glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
478         
479 }
480
481 static void
482 surface_map(struct wl_client *client,
483             struct wl_surface *surface,
484             int32_t x, int32_t y, int32_t width, int32_t height)
485 {
486         struct wlsc_surface *es = (struct wlsc_surface *) surface;
487
488         wlsc_matrix_init(&es->matrix);
489         wlsc_matrix_scale(&es->matrix, width, height, 1);
490         wlsc_matrix_translate(&es->matrix, x, y, 0);
491
492         wlsc_matrix_init(&es->matrix_inv);
493         wlsc_matrix_translate(&es->matrix_inv, -x, -y, 0);
494         wlsc_matrix_scale(&es->matrix_inv, 1.0 / width, 1.0 / height, 1);
495
496 }
497
498 static void
499 surface_damage(struct wl_client *client,
500                struct wl_surface *surface,
501                int32_t x, int32_t y, int32_t width, int32_t height)
502 {
503         /* FIXME: This need to take a damage region, of course. */
504 }
505
506 const static struct wl_surface_interface surface_interface = {
507         surface_destroy,
508         surface_attach,
509         surface_map,
510         surface_damage
511 };
512
513 static void
514 compositor_create_surface(struct wl_client *client,
515                           struct wl_compositor *compositor, uint32_t id)
516 {
517         struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
518         struct wlsc_surface *surface;
519
520         surface = malloc(sizeof *surface);
521         if (surface == NULL)
522                 /* FIXME: Send OOM event. */
523                 return;
524
525         wlsc_surface_init(surface, ec, NULL, 0, 0, 0, 0);
526
527         wl_list_insert(ec->surface_list.prev, &surface->link);
528         wl_client_add_surface(client, &surface->base,
529                               &surface_interface, id);
530 }
531
532 static void
533 compositor_commit(struct wl_client *client,
534                   struct wl_compositor *compositor, uint32_t key)
535 {
536         struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
537
538         wlsc_compositor_schedule_repaint(ec);
539         wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
540 }
541
542 const static struct wl_compositor_interface compositor_interface = {
543         compositor_create_surface,
544         compositor_commit
545 };
546
547 static void
548 wlsc_surface_transform(struct wlsc_surface *surface,
549                        int32_t x, int32_t y, int32_t *sx, int32_t *sy)
550 {
551         struct wlsc_vector v = { { x, y, 0, 1 } };
552         
553         wlsc_matrix_transform(&surface->matrix_inv, &v);
554         *sx = v.f[0] * surface->width;
555         *sy = v.f[1] * surface->height;
556 }
557
558 static void
559 wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
560                                      struct wlsc_surface *surface)
561 {
562         if (device->keyboard_focus == surface)
563                 return;
564
565         if (device->keyboard_focus &&
566             (!surface || device->keyboard_focus->base.client != surface->base.client))
567                 wl_surface_post_event(&device->keyboard_focus->base,
568                                       &device->base,
569                                       WL_INPUT_KEYBOARD_FOCUS, NULL, &device->keys);
570
571         if (surface)
572                 wl_surface_post_event(&surface->base,
573                                       &device->base,
574                                       WL_INPUT_KEYBOARD_FOCUS,
575                                       &surface->base, &device->keys);
576
577         device->keyboard_focus = surface;
578 }
579
580 static void
581 wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
582                                     struct wlsc_surface *surface)
583 {
584         if (device->pointer_focus == surface)
585                 return;
586
587         if (device->pointer_focus &&
588             (!surface || device->pointer_focus->base.client != surface->base.client))
589                 wl_surface_post_event(&device->pointer_focus->base,
590                                       &device->base,
591                                       WL_INPUT_POINTER_FOCUS, NULL);
592         if (surface)
593                 wl_surface_post_event(&surface->base,
594                                       &device->base,
595                                       WL_INPUT_POINTER_FOCUS, &surface->base);
596
597         device->pointer_focus = surface;
598 }
599
600 static struct wlsc_surface *
601 pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
602 {
603         struct wlsc_compositor *ec = device->ec;
604         struct wlsc_surface *es;
605
606         if (device->grab > 0) {
607                 wlsc_surface_transform(device->grab_surface,
608                                        device->x, device->y, sx, sy);
609                 return device->grab_surface;
610         }
611
612         wl_list_for_each(es, &ec->surface_list, link) {
613                 wlsc_surface_transform(es, device->x, device->y, sx, sy);
614                 if (0 <= *sx && *sx < es->width &&
615                     0 <= *sy && *sy < es->height)
616                         return es;
617         }
618
619         return NULL;
620 }
621
622 void
623 notify_motion(struct wlsc_input_device *device, int x, int y)
624 {
625         struct wlsc_surface *es;
626         struct wlsc_compositor *ec = device->ec;
627         struct wlsc_output *output;
628         const int hotspot_x = 16, hotspot_y = 16;
629         int32_t sx, sy;
630
631         if (!ec->vt_active)
632                 return;
633
634         /* FIXME: We need some multi head love here. */
635         output = container_of(ec->output_list.next, struct wlsc_output, link);
636         if (x < output->x)
637                 x = 0;
638         if (y < output->y)
639                 y = 0;
640         if (x >= output->x + output->width)
641                 x = output->x + output->width - 1;
642         if (y >= output->y + output->height)
643                 y = output->y + output->height - 1;
644
645         device->x = x;
646         device->y = y;
647         es = pick_surface(device, &sx, &sy);
648
649         wlsc_input_device_set_pointer_focus(device, es);
650                 
651         if (es)
652                 wl_surface_post_event(&es->base, &device->base,
653                                       WL_INPUT_MOTION, x, y, sx, sy);
654
655         wlsc_matrix_init(&device->sprite->matrix);
656         wlsc_matrix_scale(&device->sprite->matrix, 64, 64, 1);
657         wlsc_matrix_translate(&device->sprite->matrix,
658                               x - hotspot_x, y - hotspot_y, 0);
659
660         wlsc_compositor_schedule_repaint(device->ec);
661 }
662
663 void
664 notify_button(struct wlsc_input_device *device,
665               int32_t button, int32_t state)
666 {
667         struct wlsc_surface *surface;
668         struct wlsc_compositor *compositor = device->ec;
669         int32_t sx, sy;
670
671         if (!compositor->vt_active)
672                 return;
673
674         surface = pick_surface(device, &sx, &sy);
675         if (surface) {
676                 if (state) {
677                         wlsc_surface_raise(surface);
678                         device->grab++;
679                         device->grab_surface = surface;
680                         wlsc_input_device_set_keyboard_focus(device,
681                                                              surface);
682                 } else {
683                         device->grab--;
684                 }
685
686                 /* FIXME: Swallow click on raise? */
687                 wl_surface_post_event(&surface->base, &device->base,
688                                       WL_INPUT_BUTTON, button, state,
689                                       device->x, device->y, sx, sy);
690
691                 wlsc_compositor_schedule_repaint(compositor);
692         }
693 }
694
695 void
696 notify_key(struct wlsc_input_device *device,
697            uint32_t key, uint32_t state)
698 {
699         struct wlsc_compositor *compositor = device->ec;
700         uint32_t *k, *end;
701         uint32_t modifier;
702
703         if (!compositor->vt_active)
704                 return;
705
706         switch (key | compositor->modifier_state) {
707         case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
708                 kill(0, SIGTERM);
709                 return;
710         }
711
712         switch (key) {
713         case KEY_LEFTCTRL:
714         case KEY_RIGHTCTRL:
715                 modifier = MODIFIER_CTRL;
716                 break;
717
718         case KEY_LEFTALT:
719         case KEY_RIGHTALT:
720                 modifier = MODIFIER_ALT;
721                 break;
722
723         default:
724                 modifier = 0;
725                 break;
726         }
727
728         if (state)
729                 compositor->modifier_state |= modifier;
730         else
731                 compositor->modifier_state &= ~modifier;
732
733         end = device->keys.data + device->keys.size;
734         for (k = device->keys.data; k < end; k++) {
735                 if (*k == key)
736                         *k = *--end;
737         }
738         device->keys.size = (void *) end - device->keys.data;
739         if (state) {
740                 k = wl_array_add(&device->keys, sizeof *k);
741                 *k = key;
742         }
743
744         if (device->keyboard_focus != NULL)
745                 wl_surface_post_event(&device->keyboard_focus->base,
746                                       &device->base, 
747                                       WL_INPUT_KEY, key, state);
748 }
749
750 static void
751 handle_surface_destroy(struct wlsc_listener *listener,
752                        struct wlsc_surface *surface)
753 {
754         struct wlsc_input_device *device =
755                 container_of(listener, struct wlsc_input_device, listener);
756         struct wlsc_surface *focus;
757         int32_t sx, sy;
758
759         if (device->grab_surface == surface) {
760                 device->grab_surface = NULL;
761                 device->grab = 0;
762         }
763         if (device->keyboard_focus == surface)
764                 wlsc_input_device_set_keyboard_focus(device, NULL);
765         if (device->pointer_focus == surface) {
766                 focus = pick_surface(device, &sx, &sy);
767                 wlsc_input_device_set_pointer_focus(device, focus);
768                 fprintf(stderr, "lost pointer focus surface, reverting to %p\n", focus);
769         }
770 }
771
772 struct wlsc_input_device *
773 wlsc_input_device_create(struct wlsc_compositor *ec)
774 {
775         struct wlsc_input_device *device;
776
777         device = malloc(sizeof *device);
778         if (device == NULL)
779                 return NULL;
780
781         memset(device, 0, sizeof *device);
782         device->base.interface = &wl_input_device_interface;
783         device->base.implementation = NULL;
784         wl_display_add_object(ec->wl_display, &device->base);
785         wl_display_add_global(ec->wl_display, &device->base, NULL);
786         device->x = 100;
787         device->y = 100;
788         device->ec = ec;
789
790         device->listener.func = handle_surface_destroy;
791         wl_list_insert(ec->surface_destroy_listener_list.prev,
792                        &device->listener.link);
793         wl_list_insert(ec->input_device_list.prev, &device->link);
794
795         return device;
796 }
797
798 static void
799 wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
800 {
801         struct wlsc_output *output =
802                 container_of(global, struct wlsc_output, base);
803
804         wl_client_post_event(client, global,
805                              WL_OUTPUT_GEOMETRY,
806                              output->width, output->height);
807 }
808
809 static const char vertex_shader[] =
810         "uniform mat4 proj;\n"
811         "attribute vec4 position;\n"
812         "attribute vec2 texcoord;\n"
813         "varying vec2 v_texcoord;\n"
814         "void main()\n"
815         "{\n"
816         "   gl_Position = proj * position;\n"
817         "   v_texcoord = texcoord;\n"
818         "}\n";
819
820 static const char fragment_shader[] =
821         /* "precision mediump float;\n" */
822         "varying vec2 v_texcoord;\n"
823         "uniform sampler2D tex;\n"
824         "void main()\n"
825         "{\n"
826         "   gl_FragColor = texture2D(tex, v_texcoord)\n;"
827         "}\n";
828
829 static void
830 init_shaders(struct wlsc_compositor *ec)
831 {
832    GLuint v, f, program;
833    const char *p;
834    char msg[512];
835    GLfloat vertices[4 * 5];
836
837    p = vertex_shader;
838    v = glCreateShader(GL_VERTEX_SHADER);
839    glShaderSource(v, 1, &p, NULL);
840    glCompileShader(v);
841    glGetShaderInfoLog(v, sizeof msg, NULL, msg);
842    printf("vertex shader info: %s\n", msg);
843
844    p = fragment_shader;
845    f = glCreateShader(GL_FRAGMENT_SHADER);
846    glShaderSource(f, 1, &p, NULL);
847    glCompileShader(f);
848    glGetShaderInfoLog(f, sizeof msg, NULL, msg);
849    printf("fragment shader info: %s\n", msg);
850
851    program = glCreateProgram();
852    glAttachShader(program, v);
853    glAttachShader(program, f);
854    glBindAttribLocation(program, 0, "position");
855    glBindAttribLocation(program, 1, "texcoord");
856
857    glLinkProgram(program);
858    glGetProgramInfoLog(program, sizeof msg, NULL, msg);
859    printf("info: %s\n", msg);
860
861    glUseProgram(program);
862    ec->proj_uniform = glGetUniformLocation(program, "proj");
863    ec->tex_uniform = glGetUniformLocation(program, "tex");
864
865    vertices[ 0] = 0.0;
866    vertices[ 1] = 0.0;
867    vertices[ 2] = 0.0;
868    vertices[ 3] = 0.0;
869    vertices[ 4] = 0.0;
870
871    vertices[ 5] = 0.0;
872    vertices[ 6] = 1.0;
873    vertices[ 7] = 0.0;
874    vertices[ 8] = 0.0;
875    vertices[ 9] = 1.0;
876
877    vertices[10] = 1.0;
878    vertices[11] = 0.0;
879    vertices[12] = 0.0;
880    vertices[13] = 1.0;
881    vertices[14] = 0.0;
882
883    vertices[15] = 1.0;
884    vertices[16] = 1.0;
885    vertices[17] = 0.0;
886    vertices[18] = 1.0;
887    vertices[19] = 1.0;
888
889    glGenBuffers(1, &ec->vbo);
890    glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
891    glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
892 }
893
894 static const struct wl_interface visual_interface = {
895         "visual", 1,
896 };
897
898 static void
899 add_visuals(struct wlsc_compositor *ec)
900 {
901         ec->argb_visual.base.interface = &visual_interface;
902         ec->argb_visual.base.implementation = NULL;
903         wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
904         wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
905
906         ec->premultiplied_argb_visual.base.interface = &visual_interface;
907         ec->premultiplied_argb_visual.base.implementation = NULL;
908         wl_display_add_object(ec->wl_display,
909                               &ec->premultiplied_argb_visual.base);
910         wl_display_add_global(ec->wl_display,
911                               &ec->premultiplied_argb_visual.base, NULL);
912
913         ec->rgb_visual.base.interface = &visual_interface;
914         ec->rgb_visual.base.implementation = NULL;
915         wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
916         wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
917 }
918
919 static struct wlsc_compositor *
920 wlsc_compositor_create(struct wl_display *display)
921 {
922         struct wlsc_compositor *ec;
923         struct wl_event_loop *loop;
924         struct wlsc_output *output;
925
926         ec = malloc(sizeof *ec);
927         if (ec == NULL)
928                 return NULL;
929
930         memset(ec, 0, sizeof *ec);
931         ec->wl_display = display;
932
933         wl_display_set_compositor(display, &ec->base, &compositor_interface); 
934
935         add_visuals(ec);
936
937         wl_list_init(&ec->surface_list);
938         wl_list_init(&ec->input_device_list);
939         wl_list_init(&ec->output_list);
940         wl_list_init(&ec->surface_destroy_listener_list);
941
942         screenshooter_create(ec);
943
944         if (wlsc_compositor_init_drm(ec) < 0) {
945                 fprintf(stderr, "failed to initialize devices\n");
946                 return NULL;
947         }
948
949         /* Create the pointer and background surfaces now that we have
950          * a current EGL context. */
951         ec->input_device->sprite =
952                 pointer_create(ec, 
953                                ec->input_device->x,
954                                ec->input_device->y, 64, 64);
955         wl_list_for_each(output, &ec->output_list, link) {
956                 output->background = background_create(output,
957                                                        option_background);
958
959                 wlsc_matrix_init(&output->matrix);
960
961                 wlsc_matrix_translate(&output->matrix,
962                                       -output->x - output->width / 2.0,
963                                       -output->y - output->height / 2.0, 0);
964                 wlsc_matrix_scale(&output->matrix,
965                                   2.0 / output->width, 2.0 / output->height, 1);
966                 output->base.interface = &wl_output_interface;
967                 wl_display_add_object(ec->wl_display, &output->base);
968                 wl_display_add_global(ec->wl_display, &output->base,
969                                       wlsc_output_post_geometry);
970         }
971
972         glGenFramebuffers(1, &ec->fbo);
973         glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
974         glActiveTexture(GL_TEXTURE0);
975         init_shaders(ec);
976
977         loop = wl_display_get_event_loop(ec->wl_display);
978         ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
979         wlsc_compositor_schedule_repaint(ec);
980
981         return ec;
982 }
983
984 /* The plan here is to generate a random anonymous socket name and
985  * advertise that through a service on the session dbus.
986  */
987 static const char socket_name[] = "\0wayland";
988
989 int main(int argc, char *argv[])
990 {
991         struct wl_display *display;
992         struct wlsc_compositor *ec;
993         GError *error = NULL;
994         GOptionContext *context;
995
996         context = g_option_context_new(NULL);
997         g_option_context_add_main_entries(context, option_entries, "Wayland");
998         if (!g_option_context_parse(context, &argc, &argv, &error)) {
999                 fprintf(stderr, "option parsing failed: %s\n", error->message);
1000                 exit(EXIT_FAILURE);
1001         }
1002
1003         display = wl_display_create();
1004
1005         ec = wlsc_compositor_create(display);
1006         if (ec == NULL) {
1007                 fprintf(stderr, "failed to create compositor\n");
1008                 exit(EXIT_FAILURE);
1009         }
1010                 
1011         if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
1012                 fprintf(stderr, "failed to add socket: %m\n");
1013                 exit(EXIT_FAILURE);
1014         }
1015
1016         wl_display_run(display);
1017
1018         return 0;
1019 }