2 * Copyright © 2008 Kristian Høgsberg
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.
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.
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.
24 #include <sys/ioctl.h>
28 #include <gdk-pixbuf/gdk-pixbuf.h>
31 #include <linux/input.h>
33 #define GL_GLEXT_PROTOTYPES
34 #define EGL_EGLEXT_PROTOTYPES
35 #include <GLES2/gl2.h>
36 #include <GLES2/gl2ext.h>
38 #include <EGL/eglext.h>
41 #include "wayland-protocol.h"
42 #include "cairo-util.h"
43 #include "compositor.h"
45 const char *option_background = "background.jpg";
46 int option_connector = 0;
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" },
58 wlsc_matrix_init(struct wlsc_matrix *matrix)
60 static const struct wlsc_matrix identity = {
61 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
64 memcpy(matrix, &identity, sizeof identity);
68 wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
70 struct wlsc_matrix tmp;
71 const GLfloat *row, *column;
75 for (i = 0; i < 16; i++) {
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];
83 memcpy(m, &tmp, sizeof tmp);
87 wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
89 struct wlsc_matrix translate = {
90 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
93 wlsc_matrix_multiply(matrix, &translate);
97 wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
99 struct wlsc_matrix scale = {
100 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
103 wlsc_matrix_multiply(matrix, &scale);
107 wlsc_matrix_rotate(struct wlsc_matrix *matrix,
108 GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
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,
119 wlsc_matrix_multiply(matrix, &rotate);
123 wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
126 struct wlsc_vector t;
128 for (i = 0; i < 4; i++) {
130 for (j = 0; j < 4; j++)
131 t.f[i] += v->f[j] * matrix->d[i + j * 4];
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)
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);
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);
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)
159 struct wlsc_surface *es;
163 stride = cairo_image_surface_get_stride(surface);
164 data = cairo_image_surface_get_data(surface);
166 es = malloc(sizeof *es);
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);
184 wlsc_surface_destroy(struct wlsc_surface *surface,
185 struct wlsc_compositor *compositor)
187 struct wlsc_listener *l;
189 wl_list_remove(&surface->link);
190 glDeleteTextures(1, &surface->texture);
191 wl_client_remove_surface(surface->base.client, &surface->base);
193 wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
200 pointer_path(cairo_t *cr, int x, int y)
202 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
203 const int width = 16, height = 16;
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);
215 static struct wlsc_surface *
216 pointer_create(struct wlsc_compositor *ec, int x, int y, int width, int height)
218 struct wlsc_surface *es;
219 const int hotspot_x = 16, hotspot_y = 16;
220 cairo_surface_t *surface;
223 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
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);
232 blur_surface(surface, width);
234 pointer_path(cr, hotspot_x, hotspot_y);
235 cairo_stroke_preserve(cr);
236 cairo_set_source_rgb(cr, 1, 1, 1);
240 es = wlsc_surface_create_from_cairo_surface(ec,
246 cairo_surface_destroy(surface);
251 static struct wlsc_surface *
252 background_create(struct wlsc_output *output, const char *filename)
254 struct wlsc_surface *background;
256 GError *error = NULL;
260 background = malloc(sizeof *background);
261 if (background == NULL)
266 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
275 data = gdk_pixbuf_get_pixels(pixbuf);
277 wlsc_surface_init(background, output->compositor,
278 &output->compositor->rgb_visual,
279 output->x, output->y, output->width, output->height);
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);
287 if (gdk_pixbuf_get_has_alpha(pixbuf))
292 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
293 output->width, output->height, 0,
294 format, GL_UNSIGNED_BYTE, data);
300 wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
302 struct wlsc_compositor *ec = es->compositor;
303 struct wlsc_matrix tmp;
306 wlsc_matrix_multiply(&tmp, &output->matrix);
307 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
308 glUniform1i(ec->tex_uniform, 0);
310 if (es->visual == &ec->argb_visual) {
311 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
313 } else if (es->visual == &ec->premultiplied_argb_visual) {
314 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
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);
333 wlsc_surface_raise(struct wlsc_surface *surface)
335 struct wlsc_compositor *compositor = surface->compositor;
337 wl_list_remove(&surface->link);
338 wl_list_insert(compositor->surface_list.prev, &surface->link);
342 wlsc_surface_lower(struct wlsc_surface *surface)
344 struct wlsc_compositor *compositor = surface->compositor;
346 wl_list_remove(&surface->link);
347 wl_list_insert(&compositor->surface_list, &surface->link);
351 wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
353 wl_display_post_frame(compositor->wl_display,
355 compositor->current_frame, msecs);
357 wl_event_source_timer_update(compositor->timer_source, 5);
358 compositor->current_frame++;
362 wlsc_output_repaint(struct wlsc_output *output)
364 struct wlsc_compositor *ec = output->compositor;
365 struct wlsc_surface *es;
366 struct wlsc_input_device *eid;
368 glViewport(0, 0, output->width, output->height);
370 if (output->background)
371 wlsc_surface_draw(output->background, output);
373 glClear(GL_COLOR_BUFFER_BIT);
375 wl_list_for_each(es, &ec->surface_list, link)
376 wlsc_surface_draw(es, output);
379 wl_list_for_each(eid, &ec->input_device_list, link)
380 wlsc_surface_draw(eid->sprite, output);
386 struct wlsc_compositor *ec = data;
387 struct wlsc_output *output;
389 if (!ec->repaint_needed) {
390 ec->repaint_on_timeout = 0;
394 wl_list_for_each(output, &ec->output_list, link)
395 wlsc_output_repaint(output);
399 ec->repaint_needed = 0;
403 wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
405 compositor->repaint_needed = 1;
406 if (compositor->repaint_on_timeout)
409 wl_event_source_timer_update(compositor->timer_source, 1);
410 compositor->repaint_on_timeout = 1;
414 surface_destroy(struct wl_client *client,
415 struct wl_surface *surface)
417 struct wlsc_surface *es = (struct wlsc_surface *) surface;
418 struct wlsc_compositor *ec = es->compositor;
420 wlsc_surface_destroy(es, ec);
422 wlsc_compositor_schedule_repaint(ec);
426 surface_attach(struct wl_client *client,
427 struct wl_surface *surface, uint32_t name,
428 uint32_t width, uint32_t height, uint32_t stride,
429 struct wl_object *visual)
431 struct wlsc_surface *es = (struct wlsc_surface *) surface;
432 struct wlsc_compositor *ec = es->compositor;
436 EGL_IMAGE_STRIDE_MESA, 0,
437 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
444 if (visual == &ec->argb_visual.base)
445 es->visual = &ec->argb_visual;
446 else if (visual == &ec->premultiplied_argb_visual.base)
447 es->visual = &ec->premultiplied_argb_visual;
448 else if (visual == &ec->rgb_visual.base)
449 es->visual = &ec->rgb_visual;
451 /* FIXME: Smack client with an exception event */;
453 glBindTexture(GL_TEXTURE_2D, es->texture);
454 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
455 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
456 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
457 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
460 eglDestroyImageKHR(ec->display, es->image);
464 attribs[5] = stride / 4;
466 es->image = eglCreateImageKHR(ec->display, ec->context,
468 (EGLClientBuffer) name, attribs);
469 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, es->image);
474 surface_map(struct wl_client *client,
475 struct wl_surface *surface,
476 int32_t x, int32_t y, int32_t width, int32_t height)
478 struct wlsc_surface *es = (struct wlsc_surface *) surface;
480 wlsc_matrix_init(&es->matrix);
481 wlsc_matrix_scale(&es->matrix, width, height, 1);
482 wlsc_matrix_translate(&es->matrix, x, y, 0);
484 wlsc_matrix_init(&es->matrix_inv);
485 wlsc_matrix_translate(&es->matrix_inv, -x, -y, 0);
486 wlsc_matrix_scale(&es->matrix_inv, 1.0 / width, 1.0 / height, 1);
491 surface_damage(struct wl_client *client,
492 struct wl_surface *surface,
493 int32_t x, int32_t y, int32_t width, int32_t height)
495 /* FIXME: This need to take a damage region, of course. */
498 const static struct wl_surface_interface surface_interface = {
506 compositor_create_surface(struct wl_client *client,
507 struct wl_compositor *compositor, uint32_t id)
509 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
510 struct wlsc_surface *surface;
512 surface = malloc(sizeof *surface);
514 /* FIXME: Send OOM event. */
517 wlsc_surface_init(surface, ec, NULL, 0, 0, 0, 0);
519 wl_list_insert(ec->surface_list.prev, &surface->link);
520 wl_client_add_surface(client, &surface->base,
521 &surface_interface, id);
525 compositor_commit(struct wl_client *client,
526 struct wl_compositor *compositor, uint32_t key)
528 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
530 wlsc_compositor_schedule_repaint(ec);
531 wl_client_send_acknowledge(client, compositor, key, ec->current_frame);
534 const static struct wl_compositor_interface compositor_interface = {
535 compositor_create_surface,
540 wlsc_surface_transform(struct wlsc_surface *surface,
541 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
543 struct wlsc_vector v = { { x, y, 0, 1 } };
545 wlsc_matrix_transform(&surface->matrix_inv, &v);
546 *sx = v.f[0] * surface->width;
547 *sy = v.f[1] * surface->height;
551 wlsc_input_device_set_keyboard_focus(struct wlsc_input_device *device,
552 struct wlsc_surface *surface)
554 if (device->keyboard_focus == surface)
557 if (device->keyboard_focus &&
558 (!surface || device->keyboard_focus->base.client != surface->base.client))
559 wl_surface_post_event(&device->keyboard_focus->base,
561 WL_INPUT_KEYBOARD_FOCUS, NULL, &device->keys);
564 wl_surface_post_event(&surface->base,
566 WL_INPUT_KEYBOARD_FOCUS,
567 &surface->base, &device->keys);
569 device->keyboard_focus = surface;
573 wlsc_input_device_set_pointer_focus(struct wlsc_input_device *device,
574 struct wlsc_surface *surface)
576 if (device->pointer_focus == surface)
579 if (device->pointer_focus &&
580 (!surface || device->pointer_focus->base.client != surface->base.client))
581 wl_surface_post_event(&device->pointer_focus->base,
583 WL_INPUT_POINTER_FOCUS, NULL);
585 wl_surface_post_event(&surface->base,
587 WL_INPUT_POINTER_FOCUS, &surface->base);
589 device->pointer_focus = surface;
592 static struct wlsc_surface *
593 pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
595 struct wlsc_compositor *ec = device->ec;
596 struct wlsc_surface *es;
598 if (device->grab > 0) {
599 wlsc_surface_transform(device->grab_surface,
600 device->x, device->y, sx, sy);
601 return device->grab_surface;
604 wl_list_for_each(es, &ec->surface_list, link) {
605 wlsc_surface_transform(es, device->x, device->y, sx, sy);
606 if (0 <= *sx && *sx < es->width &&
607 0 <= *sy && *sy < es->height)
615 notify_motion(struct wlsc_input_device *device, int x, int y)
617 struct wlsc_surface *es;
618 struct wlsc_compositor *ec = device->ec;
619 struct wlsc_output *output;
620 const int hotspot_x = 16, hotspot_y = 16;
623 /* FIXME: We need some multi head love here. */
624 output = container_of(ec->output_list.next, struct wlsc_output, link);
629 if (x >= output->x + output->width)
630 x = output->x + output->width - 1;
631 if (y >= output->y + output->height)
632 y = output->y + output->height - 1;
636 es = pick_surface(device, &sx, &sy);
638 wlsc_input_device_set_pointer_focus(device, es);
641 wl_surface_post_event(&es->base, &device->base,
642 WL_INPUT_MOTION, x, y, sx, sy);
644 wlsc_matrix_init(&device->sprite->matrix);
645 wlsc_matrix_scale(&device->sprite->matrix, 64, 64, 1);
646 wlsc_matrix_translate(&device->sprite->matrix,
647 x - hotspot_x, y - hotspot_y, 0);
649 wlsc_compositor_schedule_repaint(device->ec);
653 notify_button(struct wlsc_input_device *device,
654 int32_t button, int32_t state)
656 struct wlsc_surface *surface;
657 struct wlsc_compositor *compositor = device->ec;
660 surface = pick_surface(device, &sx, &sy);
663 wlsc_surface_raise(surface);
665 device->grab_surface = surface;
666 wlsc_input_device_set_keyboard_focus(device,
672 /* FIXME: Swallow click on raise? */
673 wl_surface_post_event(&surface->base, &device->base,
674 WL_INPUT_BUTTON, button, state,
675 device->x, device->y, sx, sy);
677 wlsc_compositor_schedule_repaint(compositor);
682 notify_key(struct wlsc_input_device *device,
683 uint32_t key, uint32_t state)
685 struct wlsc_compositor *compositor = device->ec;
689 switch (key | compositor->modifier_state) {
690 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
698 modifier = MODIFIER_CTRL;
703 modifier = MODIFIER_ALT;
712 compositor->modifier_state |= modifier;
714 compositor->modifier_state &= ~modifier;
716 end = device->keys.data + device->keys.size;
717 for (k = device->keys.data; k < end; k++) {
721 device->keys.size = (void *) end - device->keys.data;
723 k = wl_array_add(&device->keys, sizeof *k);
727 if (device->keyboard_focus != NULL)
728 wl_surface_post_event(&device->keyboard_focus->base,
730 WL_INPUT_KEY, key, state);
734 handle_surface_destroy(struct wlsc_listener *listener,
735 struct wlsc_surface *surface)
737 struct wlsc_input_device *device =
738 container_of(listener, struct wlsc_input_device, listener);
739 struct wlsc_surface *focus;
742 if (device->grab_surface == surface) {
743 device->grab_surface = NULL;
746 if (device->keyboard_focus == surface)
747 wlsc_input_device_set_keyboard_focus(device, NULL);
748 if (device->pointer_focus == surface) {
749 focus = pick_surface(device, &sx, &sy);
750 wlsc_input_device_set_pointer_focus(device, focus);
751 fprintf(stderr, "lost pointer focus surface, reverting to %p\n", focus);
756 wlsc_input_device_init(struct wlsc_input_device *device,
757 struct wlsc_compositor *ec)
759 device->base.interface = &wl_input_device_interface;
760 device->base.implementation = NULL;
761 wl_display_add_object(ec->wl_display, &device->base);
762 wl_display_add_global(ec->wl_display, &device->base, NULL);
766 device->sprite = pointer_create(ec, device->x, device->y, 64, 64);
768 device->listener.func = handle_surface_destroy;
769 wl_list_insert(ec->surface_destroy_listener_list.prev,
770 &device->listener.link);
771 wl_list_insert(ec->input_device_list.prev, &device->link);
775 wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
777 struct wlsc_output *output =
778 container_of(global, struct wlsc_output, base);
780 wl_client_post_event(client, global,
782 output->width, output->height);
785 static const char vertex_shader[] =
786 "uniform mat4 proj;\n"
787 "attribute vec4 position;\n"
788 "attribute vec2 texcoord;\n"
789 "varying vec2 v_texcoord;\n"
792 " gl_Position = proj * position;\n"
793 " v_texcoord = texcoord;\n"
796 static const char fragment_shader[] =
797 /* "precision mediump float;\n" */
798 "varying vec2 v_texcoord;\n"
799 "uniform sampler2D tex;\n"
802 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
806 init_shaders(struct wlsc_compositor *ec)
808 GLuint v, f, program;
811 GLfloat vertices[4 * 5];
815 v = glCreateShader(GL_VERTEX_SHADER);
816 glShaderSource(v, 1, &p, NULL);
818 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
820 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
821 fprintf(stderr, "vertex shader info: %s\n", msg);
826 f = glCreateShader(GL_FRAGMENT_SHADER);
827 glShaderSource(f, 1, &p, NULL);
829 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
831 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
832 fprintf(stderr, "fragment shader info: %s\n", msg);
836 program = glCreateProgram();
837 glAttachShader(program, v);
838 glAttachShader(program, f);
839 glBindAttribLocation(program, 0, "position");
840 glBindAttribLocation(program, 1, "texcoord");
842 glLinkProgram(program);
843 glGetProgramiv(program, GL_LINK_STATUS, &status);
845 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
846 fprintf(stderr, "link info: %s\n", msg);
850 glUseProgram(program);
851 ec->proj_uniform = glGetUniformLocation(program, "proj");
852 ec->tex_uniform = glGetUniformLocation(program, "tex");
878 glGenBuffers(1, &ec->vbo);
879 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
880 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
885 static const struct wl_interface visual_interface = {
890 add_visuals(struct wlsc_compositor *ec)
892 ec->argb_visual.base.interface = &visual_interface;
893 ec->argb_visual.base.implementation = NULL;
894 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
895 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
897 ec->premultiplied_argb_visual.base.interface = &visual_interface;
898 ec->premultiplied_argb_visual.base.implementation = NULL;
899 wl_display_add_object(ec->wl_display,
900 &ec->premultiplied_argb_visual.base);
901 wl_display_add_global(ec->wl_display,
902 &ec->premultiplied_argb_visual.base, NULL);
904 ec->rgb_visual.base.interface = &visual_interface;
905 ec->rgb_visual.base.implementation = NULL;
906 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
907 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
911 wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
912 int x, int y, int width, int height)
914 output->compositor = c;
917 output->width = width;
918 output->height = height;
921 background_create(output, option_background);
923 wlsc_matrix_init(&output->matrix);
924 wlsc_matrix_translate(&output->matrix,
925 -output->x - output->width / 2.0,
926 -output->y - output->height / 2.0, 0);
927 wlsc_matrix_scale(&output->matrix,
928 2.0 / output->width, 2.0 / output->height, 1);
930 output->base.interface = &wl_output_interface;
931 wl_display_add_object(c->wl_display, &output->base);
932 wl_display_add_global(c->wl_display, &output->base,
933 wlsc_output_post_geometry);
937 wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
939 struct wl_event_loop *loop;
941 ec->wl_display = display;
943 wl_display_set_compositor(display, &ec->base, &compositor_interface);
947 wl_list_init(&ec->surface_list);
948 wl_list_init(&ec->input_device_list);
949 wl_list_init(&ec->output_list);
950 wl_list_init(&ec->surface_destroy_listener_list);
952 screenshooter_create(ec);
954 glGenFramebuffers(1, &ec->fbo);
955 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
956 glActiveTexture(GL_TEXTURE0);
957 if (init_shaders(ec) < 0)
960 loop = wl_display_get_event_loop(ec->wl_display);
961 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
962 wlsc_compositor_schedule_repaint(ec);
967 /* The plan here is to generate a random anonymous socket name and
968 * advertise that through a service on the session dbus.
970 static const char socket_name[] = "\0wayland";
972 int main(int argc, char *argv[])
974 struct wl_display *display;
975 struct wlsc_compositor *ec;
976 GError *error = NULL;
977 GOptionContext *context;
979 context = g_option_context_new(NULL);
980 g_option_context_add_main_entries(context, option_entries, "Wayland");
981 if (!g_option_context_parse(context, &argc, &argv, &error)) {
982 fprintf(stderr, "option parsing failed: %s\n", error->message);
986 display = wl_display_create();
988 if (getenv("DISPLAY"))
989 ec = x11_compositor_create(display);
991 ec = drm_compositor_create(display);
994 fprintf(stderr, "failed to create compositor\n");
998 if (wl_display_add_socket(display, socket_name, sizeof socket_name)) {
999 fprintf(stderr, "failed to add socket: %m\n");
1003 wl_display_run(display);