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.
29 #include <sys/ioctl.h>
32 #include <gdk-pixbuf/gdk-pixbuf.h>
34 #include <linux/input.h>
36 #include "wayland-server.h"
37 #include "compositor.h"
39 struct wlsc_switcher {
40 struct wlsc_compositor *compositor;
41 struct wlsc_surface *current;
42 struct wl_listener listener;
45 /* The plan here is to generate a random anonymous socket name and
46 * advertise that through a service on the session dbus.
48 static const char *option_socket_name = NULL;
49 static const char *option_background = "background.jpg";
50 static const char *option_geometry = "1024x640";
51 static int option_connector = 0;
53 static const GOptionEntry option_entries[] = {
54 { "background", 'b', 0, G_OPTION_ARG_STRING,
55 &option_background, "Background image" },
56 { "connector", 'c', 0, G_OPTION_ARG_INT,
57 &option_connector, "KMS connector" },
58 { "geometry", 'g', 0, G_OPTION_ARG_STRING,
59 &option_geometry, "Geometry" },
60 { "socket", 's', 0, G_OPTION_ARG_STRING,
61 &option_socket_name, "Socket Name" },
66 wlsc_matrix_init(struct wlsc_matrix *matrix)
68 static const struct wlsc_matrix identity = {
69 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
72 memcpy(matrix, &identity, sizeof identity);
76 wlsc_matrix_multiply(struct wlsc_matrix *m, const struct wlsc_matrix *n)
78 struct wlsc_matrix tmp;
79 const GLfloat *row, *column;
83 for (i = 0; i < 16; i++) {
86 row = m->d + d.quot * 4;
87 column = n->d + d.rem;
88 for (j = 0; j < 4; j++)
89 tmp.d[i] += row[j] * column[j * 4];
91 memcpy(m, &tmp, sizeof tmp);
95 wlsc_matrix_translate(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
97 struct wlsc_matrix translate = {
98 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
101 wlsc_matrix_multiply(matrix, &translate);
105 wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
107 struct wlsc_matrix scale = {
108 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
111 wlsc_matrix_multiply(matrix, &scale);
115 wlsc_matrix_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
118 struct wlsc_vector t;
120 for (i = 0; i < 4; i++) {
122 for (j = 0; j < 4; j++)
123 t.f[i] += v->f[j] * matrix->d[i + j * 4];
129 static struct wlsc_surface *
130 wlsc_surface_create(struct wlsc_compositor *compositor,
131 int32_t x, int32_t y, int32_t width, int32_t height)
133 struct wlsc_surface *surface;
135 surface = malloc(sizeof *surface);
139 wl_list_init(&surface->surface.destroy_listener_list);
140 wl_list_init(&surface->link);
141 surface->map_type = WLSC_SURFACE_MAP_UNMAPPED;
143 glGenTextures(1, &surface->texture);
144 glBindTexture(GL_TEXTURE_2D, surface->texture);
145 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
146 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
147 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
148 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
150 surface->compositor = compositor;
151 surface->visual = NULL;
154 surface->width = width;
155 surface->height = height;
156 wlsc_matrix_init(&surface->matrix);
157 wlsc_matrix_scale(&surface->matrix, width, height, 1);
158 wlsc_matrix_translate(&surface->matrix, x, y, 0);
160 wlsc_matrix_init(&surface->matrix_inv);
161 wlsc_matrix_translate(&surface->matrix_inv, -x, -y, 0);
162 wlsc_matrix_scale(&surface->matrix_inv, 1.0 / width, 1.0 / height, 1);
168 wlsc_surface_damage_rectangle(struct wlsc_surface *surface,
169 int32_t x, int32_t y,
170 int32_t width, int32_t height)
172 struct wlsc_compositor *compositor = surface->compositor;
174 pixman_region32_union_rect(&compositor->damage_region,
175 &compositor->damage_region,
176 surface->x + x, surface->y + y,
178 wlsc_compositor_schedule_repaint(compositor);
182 wlsc_surface_damage(struct wlsc_surface *surface)
184 wlsc_surface_damage_rectangle(surface, 0, 0,
185 surface->width, surface->height);
193 gettimeofday(&tv, NULL);
195 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
199 destroy_surface(struct wl_resource *resource, struct wl_client *client)
201 struct wlsc_surface *surface =
202 container_of(resource, struct wlsc_surface, surface.resource);
203 struct wl_listener *l, *next;
206 wlsc_surface_damage(surface);
208 wl_list_remove(&surface->link);
209 glDeleteTextures(1, &surface->texture);
212 wl_list_for_each_safe(l, next,
213 &surface->surface.destroy_listener_list, link)
214 l->func(l, &surface->surface, time);
220 wlsc_load_image(const char *filename, int width, int height)
223 GError *error = NULL;
224 int stride, i, n_channels;
225 unsigned char *pixels, *end, *argb_pixels, *s, *d;
227 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
231 fprintf(stderr, "failed to load image: %s\n", error->message);
236 stride = gdk_pixbuf_get_rowstride(pixbuf);
237 pixels = gdk_pixbuf_get_pixels(pixbuf);
238 n_channels = gdk_pixbuf_get_n_channels(pixbuf);
240 argb_pixels = malloc (height * width * 4);
241 if (argb_pixels == NULL) {
242 g_object_unref(pixbuf);
246 if (n_channels == 4) {
247 for (i = 0; i < height; i++) {
248 s = pixels + i * stride;
250 d = argb_pixels + i * width * 4;
254 #define MULT(_d,c,a,t) \
255 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
257 MULT(d[0], s[2], s[3], t);
258 MULT(d[1], s[1], s[3], t);
259 MULT(d[2], s[0], s[3], t);
265 } else if (n_channels == 3) {
266 for (i = 0; i < height; i++) {
267 s = pixels + i * stride;
269 d = argb_pixels + i * width * 4;
281 g_object_unref(pixbuf);
283 return (uint32_t *) argb_pixels;
287 wlsc_buffer_attach(struct wl_buffer *buffer, struct wl_surface *surface)
289 struct wlsc_surface *es = (struct wlsc_surface *) surface;
290 struct wlsc_compositor *ec = es->compositor;
293 if (buffer->attach) {
294 buffer->attach(buffer, surface);
296 image = eglCreateImageKHR(ec->display, ec->context,
297 EGL_WAYLAND_BUFFER_WL,
300 glBindTexture(GL_TEXTURE_2D, es->texture);
301 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image);
302 es->visual = buffer->visual;
303 eglDestroyImageKHR(ec->display, image);
307 static struct wl_buffer *
308 create_buffer_from_png(struct wlsc_compositor *ec,
309 const char *filename, int width, int height)
312 struct wl_buffer *buffer;
314 pixels = wlsc_load_image(filename, width, height);
318 buffer = ec->create_buffer(ec, width, height, width * 4,
319 &ec->compositor.premultiplied_argb_visual,
327 static const struct {
328 const char *filename;
329 int hotspot_x, hotspot_y;
330 } pointer_images[] = {
331 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
332 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
333 { DATADIR "/wayland/bottom_side.png", 16, 20 },
334 { DATADIR "/wayland/grabbing.png", 20, 17 },
335 { DATADIR "/wayland/left_ptr.png", 10, 5 },
336 { DATADIR "/wayland/left_side.png", 10, 20 },
337 { DATADIR "/wayland/right_side.png", 30, 19 },
338 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
339 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
340 { DATADIR "/wayland/top_side.png", 18, 8 },
341 { DATADIR "/wayland/xterm.png", 15, 15 }
345 create_pointer_images(struct wlsc_compositor *ec)
348 const int width = 32, height = 32;
350 count = ARRAY_LENGTH(pointer_images);
351 ec->pointer_buffers = malloc(count * sizeof *ec->pointer_buffers);
352 for (i = 0; i < count; i++) {
353 ec->pointer_buffers[i] =
354 create_buffer_from_png(ec,
355 pointer_images[i].filename,
360 static struct wlsc_surface *
361 background_create(struct wlsc_output *output, const char *filename)
363 struct wlsc_surface *background;
364 struct wl_buffer *buffer;
366 background = wlsc_surface_create(output->compositor,
367 output->x, output->y,
368 output->width, output->height);
369 if (background == NULL)
372 buffer = create_buffer_from_png(output->compositor,
374 output->width, output->height);
375 if (buffer == NULL) {
380 wlsc_buffer_attach(buffer, &background->surface);
386 wlsc_surface_draw(struct wlsc_surface *es,
387 struct wlsc_output *output, pixman_region32_t *clip)
389 struct wlsc_compositor *ec = es->compositor;
390 GLfloat *v, inv_width, inv_height;
392 pixman_region32_t repaint;
393 pixman_box32_t *rectangles;
396 pixman_region32_init_rect(&repaint,
397 es->x, es->y, es->width, es->height);
398 pixman_region32_intersect(&repaint, &repaint, clip);
399 if (!pixman_region32_not_empty(&repaint))
402 if (es->visual == &ec->compositor.argb_visual) {
403 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
405 } else if (es->visual == &ec->compositor.premultiplied_argb_visual) {
406 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
412 rectangles = pixman_region32_rectangles(&repaint, &n);
413 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
414 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
415 inv_width = 1.0 / es->width;
416 inv_height = 1.0 / es->height;
417 for (i = 0; i < n; i++, v += 16, p += 6) {
418 v[ 0] = rectangles[i].x1;
419 v[ 1] = rectangles[i].y1;
420 v[ 2] = (GLfloat) (rectangles[i].x1 - es->x) * inv_width;
421 v[ 3] = (GLfloat) (rectangles[i].y1 - es->y) * inv_height;
423 v[ 4] = rectangles[i].x1;
424 v[ 5] = rectangles[i].y2;
426 v[ 7] = (GLfloat) (rectangles[i].y2 - es->y) * inv_height;
428 v[ 8] = rectangles[i].x2;
429 v[ 9] = rectangles[i].y1;
430 v[10] = (GLfloat) (rectangles[i].x2 - es->x) * inv_width;
433 v[12] = rectangles[i].x2;
434 v[13] = rectangles[i].y2;
446 glBindTexture(GL_TEXTURE_2D, es->texture);
447 v = ec->vertices.data;
448 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
449 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
450 glEnableVertexAttribArray(0);
451 glEnableVertexAttribArray(1);
452 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
454 ec->vertices.size = 0;
455 ec->indices.size = 0;
456 pixman_region32_fini(&repaint);
460 wlsc_surface_raise(struct wlsc_surface *surface)
462 struct wlsc_compositor *compositor = surface->compositor;
464 wl_list_remove(&surface->link);
465 wl_list_insert(&compositor->surface_list, &surface->link);
469 wlsc_surface_update_matrix(struct wlsc_surface *es)
471 wlsc_matrix_init(&es->matrix);
472 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
473 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
475 wlsc_matrix_init(&es->matrix_inv);
476 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
477 wlsc_matrix_scale(&es->matrix_inv,
478 1.0 / es->width, 1.0 / es->height, 1);
482 wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
484 wl_display_post_frame(compositor->wl_display, msecs);
485 wl_event_source_timer_update(compositor->timer_source, 5);
486 compositor->repaint_on_timeout = 1;
490 wlsc_output_repaint(struct wlsc_output *output)
492 struct wlsc_compositor *ec = output->compositor;
493 struct wlsc_surface *es;
494 struct wlsc_input_device *eid;
495 pixman_region32_t new_damage, total_damage;
497 output->prepare_render(output);
499 glViewport(0, 0, output->width, output->height);
501 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, output->matrix.d);
502 glUniform1i(ec->tex_uniform, 0);
504 pixman_region32_init(&new_damage);
505 pixman_region32_init(&total_damage);
506 pixman_region32_intersect_rect(&new_damage,
508 output->x, output->y,
509 output->width, output->height);
510 pixman_region32_subtract(&ec->damage_region,
511 &ec->damage_region, &new_damage);
512 pixman_region32_union(&total_damage, &new_damage,
513 &output->previous_damage_region);
514 pixman_region32_copy(&output->previous_damage_region, &new_damage);
516 es = container_of(ec->surface_list.next, struct wlsc_surface, link);
517 if (es->map_type == WLSC_SURFACE_MAP_FULLSCREEN &&
518 es->fullscreen_output == output) {
519 if (es->width < output->width || es->height < output->height)
520 glClear(GL_COLOR_BUFFER_BIT);
521 wlsc_surface_draw(es, output, &total_damage);
523 if (output->background)
524 wlsc_surface_draw(output->background,
525 output, &total_damage);
527 glClear(GL_COLOR_BUFFER_BIT);
529 wl_list_for_each_reverse(es, &ec->surface_list, link) {
531 ec->switcher->current == es)
534 wlsc_surface_draw(es, output, &total_damage);
539 wlsc_surface_draw(ec->switcher->current,
540 output, &total_damage);
543 wl_list_for_each(eid, &ec->input_device_list, link)
544 wlsc_surface_draw(eid->sprite, output, &total_damage);
550 struct wlsc_compositor *ec = data;
551 struct wlsc_output *output;
553 if (!ec->repaint_needed) {
554 ec->repaint_on_timeout = 0;
558 wl_list_for_each(output, &ec->output_list, link)
559 wlsc_output_repaint(output);
563 ec->repaint_needed = 0;
567 wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
569 compositor->repaint_needed = 1;
570 if (compositor->repaint_on_timeout)
573 wl_event_source_timer_update(compositor->timer_source, 1);
574 compositor->repaint_on_timeout = 1;
578 surface_destroy(struct wl_client *client,
579 struct wl_surface *surface)
581 wl_resource_destroy(&surface->resource, client);
585 surface_attach(struct wl_client *client,
586 struct wl_surface *surface, struct wl_buffer *buffer,
587 int32_t x, int32_t y)
589 struct wlsc_surface *es = (struct wlsc_surface *) surface;
591 /* FIXME: This damages the entire old surface, but we should
592 * really just damage the part that's no longer covered by the
593 * surface. Anything covered by the new surface will be
594 * damaged by the client. */
595 wlsc_surface_damage(es);
597 wlsc_buffer_attach(buffer, surface);
602 es->width = buffer->width;
603 es->height = buffer->height;
604 wlsc_surface_update_matrix(es);
608 surface_map_toplevel(struct wl_client *client,
609 struct wl_surface *surface)
611 struct wlsc_surface *es = (struct wlsc_surface *) surface;
613 switch (es->map_type) {
614 case WLSC_SURFACE_MAP_UNMAPPED:
615 es->x = 10 + random() % 400;
616 es->y = 10 + random() % 400;
617 wlsc_surface_update_matrix(es);
618 wl_list_insert(&es->compositor->surface_list, &es->link);
620 case WLSC_SURFACE_MAP_TOPLEVEL:
622 case WLSC_SURFACE_MAP_FULLSCREEN:
623 es->fullscreen_output = NULL;
626 wlsc_surface_update_matrix(es);
632 wlsc_surface_damage(es);
633 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
637 surface_map_transient(struct wl_client *client,
638 struct wl_surface *surface, struct wl_surface *parent,
639 int x, int y, uint32_t flags)
641 struct wlsc_surface *es = (struct wlsc_surface *) surface;
642 struct wlsc_surface *pes = (struct wlsc_surface *) parent;
644 switch (es->map_type) {
645 case WLSC_SURFACE_MAP_UNMAPPED:
646 wl_list_insert(&es->compositor->surface_list, &es->link);
648 case WLSC_SURFACE_MAP_FULLSCREEN:
649 es->fullscreen_output = NULL;
658 wlsc_surface_update_matrix(es);
659 wlsc_surface_damage(es);
660 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
664 surface_map_fullscreen(struct wl_client *client, struct wl_surface *surface)
666 struct wlsc_surface *es = (struct wlsc_surface *) surface;
667 struct wlsc_output *output;
669 switch (es->map_type) {
670 case WLSC_SURFACE_MAP_UNMAPPED:
671 es->x = 10 + random() % 400;
672 es->y = 10 + random() % 400;
673 wl_list_insert(&es->compositor->surface_list, &es->link);
675 case WLSC_SURFACE_MAP_FULLSCREEN:
681 /* FIXME: Fullscreen on first output */
682 /* FIXME: Handle output going away */
683 output = container_of(es->compositor->output_list.next,
684 struct wlsc_output, link);
688 es->x = (output->width - es->width) / 2;
689 es->y = (output->height - es->height) / 2;
690 es->fullscreen_output = output;
691 wlsc_surface_update_matrix(es);
692 wlsc_surface_damage(es);
693 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
697 surface_damage(struct wl_client *client,
698 struct wl_surface *surface,
699 int32_t x, int32_t y, int32_t width, int32_t height)
701 struct wlsc_surface *es = (struct wlsc_surface *) surface;
703 es->buffer->damage(es->buffer, surface, x, y, width, height);
705 wlsc_surface_damage_rectangle(es, x, y, width, height);
708 const static struct wl_surface_interface surface_interface = {
711 surface_map_toplevel,
712 surface_map_transient,
713 surface_map_fullscreen,
718 wlsc_input_device_attach(struct wlsc_input_device *device,
719 struct wl_buffer *buffer, int x, int y)
721 wlsc_surface_damage(device->sprite);
723 wlsc_buffer_attach(buffer, &device->sprite->surface);
724 device->hotspot_x = x;
725 device->hotspot_y = y;
727 device->sprite->x = device->input_device.x - device->hotspot_x;
728 device->sprite->y = device->input_device.y - device->hotspot_y;
729 device->sprite->width = buffer->width;
730 device->sprite->height = buffer->height;
731 wlsc_surface_update_matrix(device->sprite);
733 wlsc_surface_damage(device->sprite);
738 wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
739 enum wlsc_pointer_type type)
741 struct wlsc_compositor *compositor =
742 (struct wlsc_compositor *) device->input_device.compositor;
744 wlsc_input_device_attach(device,
745 compositor->pointer_buffers[type],
746 pointer_images[type].hotspot_x,
747 pointer_images[type].hotspot_y);
751 compositor_create_surface(struct wl_client *client,
752 struct wl_compositor *compositor, uint32_t id)
754 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
755 struct wlsc_surface *surface;
757 surface = wlsc_surface_create(ec, 0, 0, 0, 0);
758 if (surface == NULL) {
759 wl_client_post_no_memory(client);
763 surface->surface.resource.destroy = destroy_surface;
765 surface->surface.resource.object.id = id;
766 surface->surface.resource.object.interface = &wl_surface_interface;
767 surface->surface.resource.object.implementation =
768 (void (**)(void)) &surface_interface;
769 surface->surface.client = client;
771 wl_client_add_resource(client, &surface->surface.resource);
774 const static struct wl_compositor_interface compositor_interface = {
775 compositor_create_surface,
779 wlsc_surface_transform(struct wlsc_surface *surface,
780 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
782 struct wlsc_vector v = { { x, y, 0, 1 } };
784 wlsc_matrix_transform(&surface->matrix_inv, &v);
785 *sx = v.f[0] * surface->width;
786 *sy = v.f[1] * surface->height;
789 struct wlsc_surface *
790 pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy)
792 struct wlsc_compositor *ec =
793 (struct wlsc_compositor *) device->compositor;
794 struct wlsc_surface *es;
796 wl_list_for_each(es, &ec->surface_list, link) {
797 wlsc_surface_transform(es, device->x, device->y, sx, sy);
798 if (0 <= *sx && *sx < es->width &&
799 0 <= *sy && *sy < es->height)
808 motion_grab_motion(struct wl_grab *grab,
809 uint32_t time, int32_t x, int32_t y)
811 struct wlsc_input_device *device =
812 (struct wlsc_input_device *) grab->input_device;
813 struct wlsc_surface *es =
814 (struct wlsc_surface *) device->input_device.pointer_focus;
817 wlsc_surface_transform(es, x, y, &sx, &sy);
818 wl_client_post_event(es->surface.client,
819 &device->input_device.object,
820 WL_INPUT_DEVICE_MOTION,
825 motion_grab_button(struct wl_grab *grab,
826 uint32_t time, int32_t button, int32_t state)
828 wl_client_post_event(grab->input_device->pointer_focus->client,
829 &grab->input_device->object,
830 WL_INPUT_DEVICE_BUTTON,
831 time, button, state);
835 motion_grab_end(struct wl_grab *grab, uint32_t time)
839 static const struct wl_grab_interface motion_grab_interface = {
846 notify_motion(struct wl_input_device *device, uint32_t time, int x, int y)
848 struct wlsc_surface *es;
849 struct wlsc_compositor *ec =
850 (struct wlsc_compositor *) device->compositor;
851 struct wlsc_output *output;
852 const struct wl_grab_interface *interface;
853 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
855 int x_valid = 0, y_valid = 0;
856 int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN;
858 wl_list_for_each(output, &ec->output_list, link) {
859 if (output->x <= x && x <= output->x + output->width)
862 if (output->y <= y && y <= output->y + output->height)
865 /* FIXME: calculate this only on output addition/deletion */
866 if (output->x < min_x)
868 if (output->y < min_y)
871 if (output->x + output->width > max_x)
872 max_x = output->x + output->width;
873 if (output->y + output->height > max_y)
874 max_y = output->y + output->height;
894 interface = device->grab->interface;
895 interface->motion(device->grab, time, x, y);
897 es = pick_surface(device, &sx, &sy);
898 wl_input_device_set_pointer_focus(device,
902 wl_client_post_event(es->surface.client,
904 WL_INPUT_DEVICE_MOTION,
908 wlsc_surface_damage(wd->sprite);
910 wd->sprite->x = device->x - wd->hotspot_x;
911 wd->sprite->y = device->y - wd->hotspot_y;
912 wlsc_surface_update_matrix(wd->sprite);
914 wlsc_surface_damage(wd->sprite);
918 wlsc_surface_activate(struct wlsc_surface *surface,
919 struct wlsc_input_device *device, uint32_t time)
921 wlsc_surface_raise(surface);
922 if (device->selection)
923 wlsc_selection_set_focus(device->selection,
924 &surface->surface, time);
926 wl_input_device_set_keyboard_focus(&device->input_device,
932 notify_button(struct wl_input_device *device,
933 uint32_t time, int32_t button, int32_t state)
935 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
936 struct wlsc_surface *surface;
937 struct wlsc_compositor *compositor =
938 (struct wlsc_compositor *) device->compositor;
940 surface = (struct wlsc_surface *) device->pointer_focus;
944 if (state && surface && device->grab == NULL) {
945 wlsc_surface_activate(surface, wd, time);
946 wl_input_device_start_grab(device,
947 &device->motion_grab,
951 if (state && surface && button == BTN_LEFT &&
952 (wd->modifier_state & MODIFIER_SUPER))
954 (struct wl_shell *) &compositor->shell,
955 &surface->surface, device, time);
956 else if (state && surface && button == BTN_MIDDLE &&
957 (wd->modifier_state & MODIFIER_SUPER)) {
959 x = device->grab_x - surface->x;
960 y = device->grab_y - surface->y;
962 if (x < surface->width / 3)
963 edges |= WL_SHELL_RESIZE_LEFT;
964 else if (x < 2 * surface->width / 3)
967 edges |= WL_SHELL_RESIZE_RIGHT;
969 if (y < surface->height / 3)
970 edges |= WL_SHELL_RESIZE_TOP;
971 else if (y < 2 * surface->height / 3)
974 edges |= WL_SHELL_RESIZE_BOTTOM;
977 (struct wl_shell *) &compositor->shell,
978 &surface->surface, device, time, edges);
982 device->grab->interface->button(device->grab, time,
985 if (!state && device->grab && device->grab_button == button)
986 wl_input_device_end_grab(device, time);
990 wlsc_switcher_next(struct wlsc_switcher *switcher)
994 wlsc_surface_damage(switcher->current);
995 l = switcher->current->link.next;
996 if (l == &switcher->compositor->surface_list)
997 l = switcher->compositor->surface_list.next;
998 switcher->current = container_of(l, struct wlsc_surface, link);
999 wl_list_remove(&switcher->listener.link);
1000 wl_list_insert(switcher->current->surface.destroy_listener_list.prev,
1001 &switcher->listener.link);
1002 wlsc_surface_damage(switcher->current);
1006 switcher_handle_surface_destroy(struct wl_listener *listener,
1007 struct wl_surface *surface, uint32_t time)
1009 struct wlsc_switcher *switcher =
1010 container_of(listener, struct wlsc_switcher, listener);
1012 wlsc_switcher_next(switcher);
1015 static struct wlsc_switcher *
1016 wlsc_switcher_create(struct wlsc_compositor *compositor)
1018 struct wlsc_switcher *switcher;
1020 switcher = malloc(sizeof *switcher);
1021 switcher->compositor = compositor;
1022 switcher->current = container_of(compositor->surface_list.next,
1023 struct wlsc_surface, link);
1024 switcher->listener.func = switcher_handle_surface_destroy;
1025 wl_list_init(&switcher->listener.link);
1031 wlsc_switcher_destroy(struct wlsc_switcher *switcher)
1033 wl_list_remove(&switcher->listener.link);
1038 notify_key(struct wl_input_device *device,
1039 uint32_t time, uint32_t key, uint32_t state)
1041 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1042 struct wlsc_compositor *compositor =
1043 (struct wlsc_compositor *) device->compositor;
1047 switch (key | wd->modifier_state) {
1048 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
1049 wl_display_terminate(compositor->wl_display);
1052 case KEY_TAB | MODIFIER_SUPER:
1055 if (wl_list_empty(&compositor->surface_list))
1057 if (compositor->switcher == NULL)
1058 compositor->switcher = wlsc_switcher_create(compositor);
1060 wlsc_switcher_next(compositor->switcher);
1063 case KEY_LEFTMETA | MODIFIER_SUPER:
1064 case KEY_RIGHTMETA | MODIFIER_SUPER:
1065 if (compositor->switcher && !state) {
1066 wlsc_surface_activate(compositor->switcher->current,
1068 wlsc_switcher_destroy(compositor->switcher);
1069 compositor->switcher = NULL;
1077 modifier = MODIFIER_CTRL;
1082 modifier = MODIFIER_ALT;
1087 modifier = MODIFIER_SUPER;
1096 wd->modifier_state |= modifier;
1098 wd->modifier_state &= ~modifier;
1100 end = device->keys.data + device->keys.size;
1101 for (k = device->keys.data; k < end; k++) {
1105 device->keys.size = (void *) end - device->keys.data;
1107 k = wl_array_add(&device->keys, sizeof *k);
1111 if (device->keyboard_focus != NULL)
1112 wl_client_post_event(device->keyboard_focus->client,
1114 WL_INPUT_DEVICE_KEY, time, key, state);
1118 notify_pointer_focus(struct wl_input_device *device,
1119 uint32_t time, struct wlsc_output *output,
1120 int32_t x, int32_t y)
1122 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
1123 struct wlsc_compositor *compositor =
1124 (struct wlsc_compositor *) device->compositor;
1125 struct wlsc_surface *es;
1131 es = pick_surface(device, &sx, &sy);
1132 wl_input_device_set_pointer_focus(device,
1134 time, x, y, sx, sy);
1136 compositor->focus = 1;
1138 wd->sprite->x = device->x - wd->hotspot_x;
1139 wd->sprite->y = device->y - wd->hotspot_y;
1140 wlsc_surface_update_matrix(wd->sprite);
1142 wl_input_device_set_pointer_focus(device, NULL,
1144 compositor->focus = 0;
1147 wlsc_surface_damage(wd->sprite);
1151 notify_keyboard_focus(struct wl_input_device *device,
1152 uint32_t time, struct wlsc_output *output,
1153 struct wl_array *keys)
1155 struct wlsc_input_device *wd =
1156 (struct wlsc_input_device *) device;
1157 struct wlsc_compositor *compositor =
1158 (struct wlsc_compositor *) device->compositor;
1159 struct wlsc_surface *es;
1161 if (!wl_list_empty(&compositor->surface_list))
1162 es = container_of(compositor->surface_list.next,
1163 struct wlsc_surface, link);
1168 wl_array_copy(&wd->input_device.keys, keys);
1169 wl_input_device_set_keyboard_focus(&wd->input_device,
1170 &es->surface, time);
1172 wl_input_device_set_keyboard_focus(&wd->input_device,
1179 input_device_attach(struct wl_client *client,
1180 struct wl_input_device *device_base,
1182 struct wl_buffer *buffer, int32_t x, int32_t y)
1184 struct wlsc_input_device *device =
1185 (struct wlsc_input_device *) device_base;
1187 if (time < device->input_device.pointer_focus_time)
1189 if (device->input_device.pointer_focus == NULL)
1191 if (device->input_device.pointer_focus->client != client)
1194 if (buffer == NULL) {
1195 wlsc_input_device_set_pointer_image(device,
1196 WLSC_POINTER_LEFT_PTR);
1200 wlsc_input_device_attach(device, buffer, x, y);
1203 const static struct wl_input_device_interface input_device_interface = {
1204 input_device_attach,
1208 wlsc_input_device_init(struct wlsc_input_device *device,
1209 struct wlsc_compositor *ec)
1211 wl_input_device_init(&device->input_device, &ec->compositor);
1213 device->input_device.object.interface = &wl_input_device_interface;
1214 device->input_device.object.implementation =
1215 (void (**)(void)) &input_device_interface;
1216 wl_display_add_object(ec->wl_display, &device->input_device.object);
1217 wl_display_add_global(ec->wl_display, &device->input_device.object, NULL);
1219 device->sprite = wlsc_surface_create(ec,
1220 device->input_device.x,
1221 device->input_device.y, 32, 32);
1222 device->hotspot_x = 16;
1223 device->hotspot_y = 16;
1225 device->input_device.motion_grab.interface = &motion_grab_interface;
1227 wl_list_insert(ec->input_device_list.prev, &device->link);
1229 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
1233 wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
1235 struct wlsc_output *output =
1236 container_of(global, struct wlsc_output, object);
1238 wl_client_post_event(client, global,
1240 output->x, output->y,
1241 output->width, output->height);
1244 static const char vertex_shader[] =
1245 "uniform mat4 proj;\n"
1246 "attribute vec2 position;\n"
1247 "attribute vec2 texcoord;\n"
1248 "varying vec2 v_texcoord;\n"
1251 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
1252 " v_texcoord = texcoord;\n"
1255 static const char fragment_shader[] =
1256 "precision mediump float;\n"
1257 "varying vec2 v_texcoord;\n"
1258 "uniform sampler2D tex;\n"
1261 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1265 init_shaders(struct wlsc_compositor *ec)
1267 GLuint v, f, program;
1273 v = glCreateShader(GL_VERTEX_SHADER);
1274 glShaderSource(v, 1, &p, NULL);
1276 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
1278 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
1279 fprintf(stderr, "vertex shader info: %s\n", msg);
1283 p = fragment_shader;
1284 f = glCreateShader(GL_FRAGMENT_SHADER);
1285 glShaderSource(f, 1, &p, NULL);
1287 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
1289 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
1290 fprintf(stderr, "fragment shader info: %s\n", msg);
1294 program = glCreateProgram();
1295 glAttachShader(program, v);
1296 glAttachShader(program, f);
1297 glBindAttribLocation(program, 0, "position");
1298 glBindAttribLocation(program, 1, "texcoord");
1300 glLinkProgram(program);
1301 glGetProgramiv(program, GL_LINK_STATUS, &status);
1303 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
1304 fprintf(stderr, "link info: %s\n", msg);
1308 glUseProgram(program);
1309 ec->proj_uniform = glGetUniformLocation(program, "proj");
1310 ec->tex_uniform = glGetUniformLocation(program, "tex");
1316 wlsc_output_destroy(struct wlsc_output *output)
1318 destroy_surface(&output->background->surface.resource, NULL);
1322 wlsc_output_move(struct wlsc_output *output, int x, int y)
1324 struct wlsc_compositor *c = output->compositor;
1330 if (output->background) {
1331 output->background->x = x;
1332 output->background->y = y;
1333 wlsc_surface_update_matrix(output->background);
1336 pixman_region32_init(&output->previous_damage_region);
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);
1343 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
1344 wlsc_matrix_scale(&output->matrix,
1345 2.0 / output->width,
1346 flip * 2.0 / output->height, 1);
1348 pixman_region32_union_rect(&c->damage_region,
1350 x, y, output->width, output->height);
1354 wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1355 int x, int y, int width, int height, uint32_t flags)
1357 output->compositor = c;
1360 output->width = width;
1361 output->height = height;
1363 output->background =
1364 background_create(output, option_background);
1366 output->flags = flags;
1367 wlsc_output_move(output, x, y);
1369 output->object.interface = &wl_output_interface;
1370 wl_display_add_object(c->wl_display, &output->object);
1371 wl_display_add_global(c->wl_display, &output->object,
1372 wlsc_output_post_geometry);
1376 wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1378 struct wl_event_loop *loop;
1379 const char *extensions;
1381 ec->wl_display = display;
1383 wl_compositor_init(&ec->compositor, &compositor_interface, display);
1386 eglBindWaylandDisplayWL(ec->display, ec->wl_display);
1388 wlsc_shell_init(ec);
1390 wl_list_init(&ec->surface_list);
1391 wl_list_init(&ec->input_device_list);
1392 wl_list_init(&ec->output_list);
1394 create_pointer_images(ec);
1396 screenshooter_create(ec);
1398 extensions = (const char *) glGetString(GL_EXTENSIONS);
1399 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
1401 "GL_EXT_texture_format_BGRA8888 not available\n");
1405 glActiveTexture(GL_TEXTURE0);
1406 if (init_shaders(ec) < 0)
1409 loop = wl_display_get_event_loop(ec->wl_display);
1410 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
1411 pixman_region32_init(&ec->damage_region);
1412 wlsc_compositor_schedule_repaint(ec);
1417 static void on_term_signal(int signal_number, void *data)
1419 struct wlsc_compositor *ec = data;
1421 wl_display_terminate(ec->wl_display);
1424 int main(int argc, char *argv[])
1426 struct wl_display *display;
1427 struct wlsc_compositor *ec;
1428 struct wl_event_loop *loop;
1429 GError *error = NULL;
1430 GOptionContext *context;
1433 g_type_init(); /* GdkPixbuf needs this, it seems. */
1435 context = g_option_context_new(NULL);
1436 g_option_context_add_main_entries(context, option_entries, "Wayland");
1437 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1438 fprintf(stderr, "option parsing failed: %s\n", error->message);
1441 if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) {
1442 fprintf(stderr, "invalid geometry option: %s \n",
1447 display = wl_display_create();
1451 #if BUILD_WAYLAND_COMPOSITOR
1452 if (getenv("WAYLAND_DISPLAY"))
1453 ec = wayland_compositor_create(display, width, height);
1456 #if BUILD_X11_COMPOSITOR
1457 if (ec == NULL && getenv("DISPLAY"))
1458 ec = x11_compositor_create(display, width, height);
1461 #if BUILD_DRM_COMPOSITOR
1463 ec = drm_compositor_create(display, option_connector);
1467 fprintf(stderr, "failed to create compositor\n");
1471 if (wl_display_add_socket(display, option_socket_name)) {
1472 fprintf(stderr, "failed to add socket: %m\n");
1476 loop = wl_display_get_event_loop(ec->wl_display);
1477 wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, ec);
1478 wl_event_loop_add_signal(loop, SIGINT, on_term_signal, ec);
1480 wl_display_run(display);
1482 eglUnbindWaylandDisplayWL(ec->display, display);
1483 wl_display_destroy(display);