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.
26 #include <sys/ioctl.h>
29 #include <gdk-pixbuf/gdk-pixbuf.h>
32 #include <linux/input.h>
34 #include "wayland-server-protocol.h"
35 #include "compositor.h"
37 /* The plan here is to generate a random anonymous socket name and
38 * advertise that through a service on the session dbus.
40 static const char *option_socket_name = NULL;
41 static const char *option_background = "background.jpg";
42 static const char *option_geometry = "1024x640";
43 static int option_connector = 0;
45 static const GOptionEntry option_entries[] = {
46 { "background", 'b', 0, G_OPTION_ARG_STRING,
47 &option_background, "Background image" },
48 { "connector", 'c', 0, G_OPTION_ARG_INT,
49 &option_connector, "KMS connector" },
50 { "geometry", 'g', 0, G_OPTION_ARG_STRING,
51 &option_geometry, "Geometry" },
52 { "socket", 's', 0, G_OPTION_ARG_STRING,
53 &option_socket_name, "Socket Name" },
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_transform(struct wlsc_matrix *matrix, struct wlsc_vector *v)
110 struct wlsc_vector t;
112 for (i = 0; i < 4; i++) {
114 for (j = 0; j < 4; j++)
115 t.f[i] += v->f[j] * matrix->d[i + j * 4];
121 static struct wlsc_surface *
122 wlsc_surface_create(struct wlsc_compositor *compositor,
123 struct wl_visual *visual,
124 int32_t x, int32_t y, int32_t width, int32_t height)
126 struct wlsc_surface *surface;
128 surface = malloc(sizeof *surface);
132 glGenTextures(1, &surface->texture);
133 glBindTexture(GL_TEXTURE_2D, surface->texture);
134 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
135 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
136 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
137 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
139 surface->compositor = compositor;
140 surface->visual = visual;
143 surface->width = width;
144 surface->height = height;
145 wlsc_matrix_init(&surface->matrix);
146 wlsc_matrix_scale(&surface->matrix, width, height, 1);
147 wlsc_matrix_translate(&surface->matrix, x, y, 0);
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);
157 destroy_surface(struct wl_resource *resource, struct wl_client *client)
159 struct wlsc_surface *surface =
160 container_of(resource, struct wlsc_surface, base.base);
161 struct wlsc_compositor *compositor = surface->compositor;
162 struct wlsc_listener *l;
164 wl_list_remove(&surface->link);
165 glDeleteTextures(1, &surface->texture);
167 wl_list_for_each(l, &compositor->surface_destroy_listener_list, link)
172 wlsc_compositor_schedule_repaint(compositor);
176 texture_from_png(const char *filename, int width, int height)
179 GError *error = NULL;
183 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
189 data = gdk_pixbuf_get_pixels(pixbuf);
191 if (gdk_pixbuf_get_has_alpha(pixbuf))
196 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
197 format, GL_UNSIGNED_BYTE, data);
199 gdk_pixbuf_unref(pixbuf);
204 static const struct {
205 const char *filename;
206 int hotspot_x, hotspot_y;
207 } pointer_images[] = {
208 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
209 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
210 { DATADIR "/wayland/bottom_side.png", 16, 20 },
211 { DATADIR "/wayland/grabbing.png", 20, 17 },
212 { DATADIR "/wayland/left_ptr.png", 10, 5 },
213 { DATADIR "/wayland/left_side.png", 10, 20 },
214 { DATADIR "/wayland/right_side.png", 30, 19 },
215 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
216 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
217 { DATADIR "/wayland/top_side.png", 18, 8 },
218 { DATADIR "/wayland/xterm.png", 15, 15 }
222 create_pointer_images(struct wlsc_compositor *ec)
226 const int width = 32, height = 32;
228 glGenTextures(1, &texture);
229 glBindTexture(GL_TEXTURE_2D, texture);
230 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
235 count = ARRAY_LENGTH(pointer_images);
236 ec->pointer_buffers = malloc(count * sizeof *ec->pointer_buffers);
237 for (i = 0; i < count; i++) {
238 ec->pointer_buffers[i] =
239 wlsc_drm_buffer_create(ec, width, height,
241 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
242 ec->pointer_buffers[i]->image);
243 texture_from_png(pointer_images[i].filename, width, height);
247 static struct wlsc_surface *
248 background_create(struct wlsc_output *output, const char *filename)
250 struct wlsc_surface *background;
252 GError *error = NULL;
256 background = wlsc_surface_create(output->compositor,
257 &output->compositor->rgb_visual,
258 output->x, output->y,
259 output->width, output->height);
260 if (background == NULL)
263 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
272 data = gdk_pixbuf_get_pixels(pixbuf);
274 if (gdk_pixbuf_get_has_alpha(pixbuf))
279 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
280 output->width, output->height, 0,
281 format, GL_UNSIGNED_BYTE, data);
287 wlsc_surface_draw(struct wlsc_surface *es, struct wlsc_output *output)
289 struct wlsc_compositor *ec = es->compositor;
290 struct wlsc_matrix tmp;
293 wlsc_matrix_multiply(&tmp, &output->matrix);
294 glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, tmp.d);
295 glUniform1i(ec->tex_uniform, 0);
297 if (es->visual == &ec->argb_visual) {
298 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
300 } else if (es->visual == &ec->premultiplied_argb_visual) {
301 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
307 glBindTexture(GL_TEXTURE_2D, es->texture);
308 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
309 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
310 5 * sizeof(GLfloat), NULL);
311 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
312 5 * sizeof(GLfloat), (GLfloat *) 0 + 3);
313 glEnableVertexAttribArray(0);
314 glEnableVertexAttribArray(1);
315 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
319 wlsc_surface_raise(struct wlsc_surface *surface)
321 struct wlsc_compositor *compositor = surface->compositor;
323 wl_list_remove(&surface->link);
324 wl_list_insert(&compositor->surface_list, &surface->link);
328 wlsc_surface_update_matrix(struct wlsc_surface *es)
330 wlsc_matrix_init(&es->matrix);
331 wlsc_matrix_scale(&es->matrix, es->width, es->height, 1);
332 wlsc_matrix_translate(&es->matrix, es->x, es->y, 0);
334 wlsc_matrix_init(&es->matrix_inv);
335 wlsc_matrix_translate(&es->matrix_inv, -es->x, -es->y, 0);
336 wlsc_matrix_scale(&es->matrix_inv,
337 1.0 / es->width, 1.0 / es->height, 1);
341 wlsc_compositor_finish_frame(struct wlsc_compositor *compositor, int msecs)
343 wl_display_post_frame(compositor->wl_display, msecs);
344 wl_event_source_timer_update(compositor->timer_source, 5);
348 wlsc_output_repaint(struct wlsc_output *output)
350 struct wlsc_compositor *ec = output->compositor;
351 struct wlsc_surface *es;
352 struct wlsc_input_device *eid;
354 glViewport(0, 0, output->width, output->height);
356 if (output->background)
357 wlsc_surface_draw(output->background, output);
359 glClear(GL_COLOR_BUFFER_BIT);
361 wl_list_for_each_reverse(es, &ec->surface_list, link)
362 wlsc_surface_draw(es, output);
365 wl_list_for_each(eid, &ec->input_device_list, link)
366 wlsc_surface_draw(eid->sprite, output);
372 struct wlsc_compositor *ec = data;
373 struct wlsc_output *output;
375 if (!ec->repaint_needed) {
376 ec->repaint_on_timeout = 0;
380 wl_list_for_each(output, &ec->output_list, link)
381 wlsc_output_repaint(output);
385 ec->repaint_needed = 0;
389 wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor)
391 compositor->repaint_needed = 1;
392 if (compositor->repaint_on_timeout)
395 wl_event_source_timer_update(compositor->timer_source, 1);
396 compositor->repaint_on_timeout = 1;
400 surface_destroy(struct wl_client *client,
401 struct wl_surface *surface)
403 wl_resource_destroy(&surface->base, client);
407 surface_attach(struct wl_client *client,
408 struct wl_surface *surface, struct wl_buffer *buffer)
410 struct wlsc_surface *es = (struct wlsc_surface *) surface;
412 buffer->attach(buffer, surface);
417 surface_map(struct wl_client *client,
418 struct wl_surface *surface,
419 int32_t x, int32_t y, int32_t width, int32_t height)
421 struct wlsc_surface *es = (struct wlsc_surface *) surface;
428 wlsc_surface_update_matrix(es);
429 wlsc_compositor_schedule_repaint(es->compositor);
433 surface_damage(struct wl_client *client,
434 struct wl_surface *surface,
435 int32_t x, int32_t y, int32_t width, int32_t height)
437 struct wlsc_surface *es = (struct wlsc_surface *) surface;
439 es->buffer->damage(es->buffer, surface, x, y, width, height);
440 wlsc_compositor_schedule_repaint(es->compositor);
443 const static struct wl_surface_interface surface_interface = {
451 wlsc_input_device_attach(struct wlsc_input_device *device,
452 struct wl_buffer *buffer, int x, int y)
454 struct wlsc_compositor *ec = device->ec;
456 buffer->attach(buffer, &device->sprite->base);
457 device->hotspot_x = x;
458 device->hotspot_y = y;
460 device->sprite->x = device->x - device->hotspot_x;
461 device->sprite->y = device->y - device->hotspot_y;
462 device->sprite->width = buffer->width;
463 device->sprite->height = buffer->height;
464 wlsc_surface_update_matrix(device->sprite);
466 wlsc_compositor_schedule_repaint(ec);
471 wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
472 enum wlsc_pointer_type type)
474 struct wlsc_compositor *compositor = device->ec;
476 wlsc_input_device_attach(device,
477 &compositor->pointer_buffers[type]->base,
478 pointer_images[type].hotspot_x,
479 pointer_images[type].hotspot_y);
483 wlsc_input_device_start_grab(struct wlsc_input_device *device,
485 enum wlsc_grab_type grab)
487 struct wlsc_surface *focus =
488 (struct wlsc_surface *) device->base.pointer_focus;
491 device->grab_surface = focus;
492 device->grab_dx = focus->x - device->grab_x;
493 device->grab_dy = focus->y - device->grab_y;
494 device->grab_width = focus->width;
495 device->grab_height = focus->height;
497 wl_input_device_set_pointer_focus(&device->base,
498 &wl_grab_surface, time, 0, 0, 0, 0);
502 shell_move(struct wl_client *client, struct wl_shell *shell,
503 struct wl_surface *surface,
504 struct wl_input_device *device, uint32_t time)
506 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
508 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
509 wd->grab_time != time ||
510 wd->base.pointer_focus != surface)
513 wlsc_input_device_start_grab(wd, time, WLSC_DEVICE_GRAB_MOVE);
514 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
518 shell_resize(struct wl_client *client, struct wl_shell *shell,
519 struct wl_surface *surface,
520 struct wl_input_device *device, uint32_t time, uint32_t edges)
522 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
523 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
525 if (edges == 0 || edges > 15 ||
526 (edges & 3) == 3 || (edges & 12) == 12)
529 if (wd->grab != WLSC_DEVICE_GRAB_MOTION ||
530 wd->grab_time != time ||
531 wd->base.pointer_focus != surface)
535 case WLSC_DEVICE_GRAB_RESIZE_TOP:
536 pointer = WLSC_POINTER_TOP;
538 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
539 pointer = WLSC_POINTER_BOTTOM;
541 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
542 pointer = WLSC_POINTER_LEFT;
544 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
545 pointer = WLSC_POINTER_TOP_LEFT;
547 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
548 pointer = WLSC_POINTER_BOTTOM_LEFT;
550 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
551 pointer = WLSC_POINTER_RIGHT;
553 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
554 pointer = WLSC_POINTER_TOP_RIGHT;
556 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
557 pointer = WLSC_POINTER_BOTTOM_RIGHT;
561 wlsc_input_device_start_grab(wd, time, edges);
562 wlsc_input_device_set_pointer_image(wd, pointer);
570 gettimeofday(&tv, NULL);
572 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
577 struct wlsc_listener listener;
581 wl_drag_set_pointer_focus(struct wl_drag *drag,
582 struct wlsc_surface *surface, uint32_t time,
583 int32_t x, int32_t y, int32_t sx, int32_t sy);
586 destroy_drag(struct wl_resource *resource, struct wl_client *client)
588 struct wlsc_drag *drag =
589 container_of(resource, struct wlsc_drag, drag.resource);
591 wl_list_remove(&drag->listener.link);
596 const static struct wl_drag_interface drag_interface;
599 drag_handle_surface_destroy(struct wlsc_listener *listener,
600 struct wlsc_surface *surface)
602 struct wlsc_drag *drag =
603 container_of(listener, struct wlsc_drag, listener);
604 uint32_t time = get_time();
606 if (drag->drag.pointer_focus == &surface->base)
607 wl_drag_set_pointer_focus(&drag->drag, NULL, time, 0, 0, 0, 0);
611 shell_create_drag(struct wl_client *client,
612 struct wl_shell *shell, uint32_t id)
614 struct wlsc_drag *drag;
615 struct wlsc_compositor *ec =
616 container_of(shell, struct wlsc_compositor, shell);
618 drag = malloc(sizeof *drag);
620 wl_client_post_no_memory(client);
624 memset(drag, 0, sizeof *drag);
625 drag->drag.resource.base.id = id;
626 drag->drag.resource.base.interface = &wl_drag_interface;
627 drag->drag.resource.base.implementation =
628 (void (**)(void)) &drag_interface;
630 drag->drag.resource.destroy = destroy_drag;
632 drag->listener.func = drag_handle_surface_destroy;
633 wl_list_insert(ec->surface_destroy_listener_list.prev,
634 &drag->listener.link);
636 wl_client_add_resource(client, &drag->drag.resource);
639 const static struct wl_shell_interface shell_interface = {
646 compositor_create_surface(struct wl_client *client,
647 struct wl_compositor *compositor, uint32_t id)
649 struct wlsc_compositor *ec = (struct wlsc_compositor *) compositor;
650 struct wlsc_surface *surface;
652 surface = wlsc_surface_create(ec, NULL, 0, 0, 0, 0);
653 if (surface == NULL) {
654 wl_client_post_no_memory(client);
658 wl_list_insert(ec->surface_list.prev, &surface->link);
659 surface->base.base.destroy = destroy_surface;
661 surface->base.base.base.id = id;
662 surface->base.base.base.interface = &wl_surface_interface;
663 surface->base.base.base.implementation =
664 (void (**)(void)) &surface_interface;
665 surface->base.client = client;
667 wl_client_add_resource(client, &surface->base.base);
670 const static struct wl_compositor_interface compositor_interface = {
671 compositor_create_surface,
675 wlsc_surface_transform(struct wlsc_surface *surface,
676 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
678 struct wlsc_vector v = { { x, y, 0, 1 } };
680 wlsc_matrix_transform(&surface->matrix_inv, &v);
681 *sx = v.f[0] * surface->width;
682 *sy = v.f[1] * surface->height;
685 static struct wlsc_surface *
686 pick_surface(struct wlsc_input_device *device, int32_t *sx, int32_t *sy)
688 struct wlsc_compositor *ec = device->ec;
689 struct wlsc_surface *es;
691 wl_list_for_each(es, &ec->surface_list, link) {
692 wlsc_surface_transform(es, device->x, device->y, sx, sy);
693 if (0 <= *sx && *sx < es->width &&
694 0 <= *sy && *sy < es->height)
702 notify_motion(struct wlsc_input_device *device, uint32_t time, int x, int y)
704 struct wlsc_surface *es;
705 struct wlsc_compositor *ec = device->ec;
706 struct wlsc_output *output;
707 int32_t sx, sy, width, height;
709 /* FIXME: We need some multi head love here. */
710 output = container_of(ec->output_list.next, struct wlsc_output, link);
715 if (x >= output->x + output->width)
716 x = output->x + output->width - 1;
717 if (y >= output->y + output->height)
718 y = output->y + output->height - 1;
723 switch (device->grab) {
724 case WLSC_DEVICE_GRAB_NONE:
725 es = pick_surface(device, &sx, &sy);
726 wl_input_device_set_pointer_focus(&device->base, &es->base,
729 wl_client_post_event(es->base.client,
731 WL_INPUT_DEVICE_MOTION,
735 case WLSC_DEVICE_GRAB_MOTION:
736 es = (struct wlsc_surface *) device->base.pointer_focus;
737 wlsc_surface_transform(es, x, y, &sx, &sy);
738 wl_client_post_event(es->base.client,
740 WL_INPUT_DEVICE_MOTION,
744 case WLSC_DEVICE_GRAB_MOVE:
745 es = device->grab_surface;
746 es->x = x + device->grab_dx;
747 es->y = y + device->grab_dy;;
748 wl_client_post_event(es->base.client,
752 &es->base, es->x, es->y,
753 es->width, es->height);
755 wlsc_surface_update_matrix(es);
759 case WLSC_DEVICE_GRAB_RESIZE_TOP:
760 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM:
761 case WLSC_DEVICE_GRAB_RESIZE_LEFT:
762 case WLSC_DEVICE_GRAB_RESIZE_TOP_LEFT:
763 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_LEFT:
764 case WLSC_DEVICE_GRAB_RESIZE_RIGHT:
765 case WLSC_DEVICE_GRAB_RESIZE_TOP_RIGHT:
766 case WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT:
767 case WLSC_DEVICE_GRAB_RESIZE_MASK:
768 es = device->grab_surface;
770 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_LEFT) {
771 sx = x + device->grab_dx;
772 width = device->grab_x - x + device->grab_width;
773 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_RIGHT) {
774 sx = device->grab_x + device->grab_dx;
775 width = x - device->grab_x + device->grab_width;
777 sx = device->grab_x + device->grab_dx;
778 width = device->grab_width;
781 if (device->grab & WLSC_DEVICE_GRAB_RESIZE_TOP) {
782 sy = y + device->grab_dy;
783 height = device->grab_y - y + device->grab_height;
784 } else if (device->grab & WLSC_DEVICE_GRAB_RESIZE_BOTTOM) {
785 sy = device->grab_y + device->grab_dy;
786 height = y - device->grab_y + device->grab_height;
788 sy = device->grab_y + device->grab_dy;
789 height = device->grab_height;
792 wl_client_post_event(es->base.client,
794 WL_SHELL_CONFIGURE, time, device->grab,
795 &es->base, sx, sy, width, height);
798 case WLSC_DEVICE_GRAB_DRAG:
799 es = pick_surface(device, &sx, &sy);
800 wl_drag_set_pointer_focus(device->drag,
801 es, time, x, y, sx, sy);
803 wl_client_post_event(es->base.client,
804 &device->drag->drag_offer.base,
805 WL_DRAG_OFFER_MOTION,
811 device->sprite->x = device->x - device->hotspot_x;
812 device->sprite->y = device->y - device->hotspot_y;
813 wlsc_surface_update_matrix(device->sprite);
815 wlsc_compositor_schedule_repaint(device->ec);
819 wlsc_input_device_end_grab(struct wlsc_input_device *device, uint32_t time)
821 struct wl_drag *drag = device->drag;
822 struct wlsc_surface *es;
825 switch (device->grab) {
826 case WLSC_DEVICE_GRAB_DRAG:
828 wl_client_post_event(drag->target,
829 &drag->drag_offer.base,
831 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
838 device->grab = WLSC_DEVICE_GRAB_NONE;
839 es = pick_surface(device, &sx, &sy);
840 wl_input_device_set_pointer_focus(&device->base, &es->base, time,
841 device->x, device->y, sx, sy);
845 notify_button(struct wlsc_input_device *device,
846 uint32_t time, int32_t button, int32_t state)
848 struct wlsc_surface *surface;
849 struct wlsc_compositor *compositor = device->ec;
851 surface = (struct wlsc_surface *) device->base.pointer_focus;
853 if (state && device->grab == WLSC_DEVICE_GRAB_NONE) {
854 wlsc_surface_raise(surface);
855 device->grab = WLSC_DEVICE_GRAB_MOTION;
856 device->grab_button = button;
857 device->grab_time = time;
858 device->grab_x = device->x;
859 device->grab_y = device->y;
860 wl_input_device_set_keyboard_focus(&device->base,
865 if (state && button == BTN_LEFT &&
866 device->grab == WLSC_DEVICE_GRAB_MOTION &&
867 (device->modifier_state & MODIFIER_SUPER))
868 shell_move(NULL, (struct wl_shell *) &compositor->shell,
869 &surface->base, &device->base, time);
870 else if (state && button == BTN_MIDDLE &&
871 device->grab == WLSC_DEVICE_GRAB_MOTION &&
872 (device->modifier_state & MODIFIER_SUPER))
873 shell_resize(NULL, (struct wl_shell *) &compositor->shell,
875 &surface->base, &device->base, time,
876 WLSC_DEVICE_GRAB_RESIZE_BOTTOM_RIGHT);
877 else if (device->grab == WLSC_DEVICE_GRAB_NONE ||
878 device->grab == WLSC_DEVICE_GRAB_MOTION)
879 wl_client_post_event(surface->base.client,
881 WL_INPUT_DEVICE_BUTTON,
882 time, button, state);
885 device->grab != WLSC_DEVICE_GRAB_NONE &&
886 device->grab_button == button) {
887 wlsc_input_device_end_grab(device, time);
890 wlsc_compositor_schedule_repaint(compositor);
895 notify_key(struct wlsc_input_device *device,
896 uint32_t time, uint32_t key, uint32_t state)
901 switch (key | device->modifier_state) {
902 case KEY_BACKSPACE | MODIFIER_CTRL | MODIFIER_ALT:
903 wl_display_terminate(device->ec->wl_display);
910 modifier = MODIFIER_CTRL;
915 modifier = MODIFIER_ALT;
920 modifier = MODIFIER_SUPER;
929 device->modifier_state |= modifier;
931 device->modifier_state &= ~modifier;
933 end = device->base.keys.data + device->base.keys.size;
934 for (k = device->base.keys.data; k < end; k++) {
938 device->base.keys.size = (void *) end - device->base.keys.data;
940 k = wl_array_add(&device->base.keys, sizeof *k);
944 if (device->base.keyboard_focus != NULL)
945 wl_client_post_event(device->base.keyboard_focus->client,
947 WL_INPUT_DEVICE_KEY, time, key, state);
951 input_device_attach(struct wl_client *client,
952 struct wl_input_device *device_base,
954 struct wl_buffer *buffer, int32_t x, int32_t y)
956 struct wlsc_input_device *device =
957 (struct wlsc_input_device *) device_base;
959 if (time < device->base.pointer_focus_time)
961 if (device->base.pointer_focus == NULL)
964 if (device->base.pointer_focus->client != client &&
965 !(device->base.pointer_focus == &wl_grab_surface &&
966 device->grab_surface->base.client == client))
969 if (buffer == NULL) {
970 wlsc_input_device_set_pointer_image(device,
971 WLSC_POINTER_LEFT_PTR);
975 wlsc_input_device_attach(device, buffer, x, y);
978 const static struct wl_input_device_interface input_device_interface = {
983 handle_surface_destroy(struct wlsc_listener *listener,
984 struct wlsc_surface *surface)
986 struct wlsc_input_device *device =
987 container_of(listener, struct wlsc_input_device, listener);
988 uint32_t time = get_time();
990 if (device->base.keyboard_focus == &surface->base)
991 wl_input_device_set_keyboard_focus(&device->base, NULL, time);
992 if (device->base.pointer_focus == &surface->base)
993 wl_input_device_set_pointer_focus(&device->base, NULL, time,
995 if (device->base.pointer_focus == &surface->base ||
996 (device->base.pointer_focus == &wl_grab_surface &&
997 device->grab_surface == surface))
998 wlsc_input_device_end_grab(device, time);
1002 wl_drag_set_pointer_focus(struct wl_drag *drag,
1003 struct wlsc_surface *surface, uint32_t time,
1004 int32_t x, int32_t y, int32_t sx, int32_t sy)
1008 if (drag->pointer_focus == &surface->base)
1011 if (drag->pointer_focus &&
1012 (!surface || drag->pointer_focus->client != surface->base.client))
1013 wl_client_post_event(drag->pointer_focus->client,
1014 &drag->drag_offer.base,
1015 WL_DRAG_OFFER_POINTER_FOCUS,
1016 time, NULL, 0, 0, 0, 0);
1019 (!drag->pointer_focus ||
1020 drag->pointer_focus->client != surface->base.client)) {
1021 wl_client_post_global(surface->base.client,
1022 &drag->drag_offer.base);
1024 end = drag->types.data + drag->types.size;
1025 for (p = drag->types.data; p < end; p++)
1026 wl_client_post_event(surface->base.client,
1027 &drag->drag_offer.base,
1028 WL_DRAG_OFFER_OFFER, *p);
1032 wl_client_post_event(surface->base.client,
1033 &drag->drag_offer.base,
1034 WL_DRAG_OFFER_POINTER_FOCUS,
1035 time, &surface->base,
1040 drag->pointer_focus = &surface->base;
1041 drag->pointer_focus_time = time;
1042 drag->target = NULL;
1046 drag_offer_accept(struct wl_client *client,
1047 struct wl_drag_offer *offer, uint32_t time, const char *type)
1049 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
1052 /* If the client responds to drag pointer_focus or motion
1053 * events after the pointer has left the surface, we just
1054 * discard the accept requests. The drag source just won't
1055 * get the corresponding 'target' events and eventually the
1056 * next surface/root will start sending events. */
1057 if (time < drag->pointer_focus_time)
1060 drag->target = client;
1062 end = drag->types.data + drag->types.size;
1063 for (p = drag->types.data; p < end; p++)
1064 if (type && strcmp(*p, type) == 0)
1067 wl_client_post_event(drag->source->client, &drag->resource.base,
1068 WL_DRAG_TARGET, drag->type);
1072 drag_offer_receive(struct wl_client *client,
1073 struct wl_drag_offer *offer, int fd)
1075 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
1077 wl_client_post_event(drag->source->client, &drag->resource.base,
1078 WL_DRAG_FINISH, fd);
1083 drag_offer_reject(struct wl_client *client, struct wl_drag_offer *offer)
1085 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
1087 wl_client_post_event(drag->source->client, &drag->resource.base,
1091 static const struct wl_drag_offer_interface drag_offer_interface = {
1098 drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type)
1102 p = wl_array_add(&drag->types, sizeof *p);
1106 wl_client_post_no_memory(client);
1110 drag_activate(struct wl_client *client,
1111 struct wl_drag *drag,
1112 struct wl_surface *surface,
1113 struct wl_input_device *input_device, uint32_t time)
1115 struct wl_display *display = wl_client_get_display (client);
1116 struct wlsc_input_device *device =
1117 (struct wlsc_input_device *) input_device;
1118 struct wlsc_surface *target;
1121 if (device->grab != WLSC_DEVICE_GRAB_MOTION ||
1122 device->base.pointer_focus != surface ||
1123 device->grab_time != time)
1126 drag->source = surface;
1127 drag->input_device = input_device;
1129 drag->drag_offer.base.interface = &wl_drag_offer_interface;
1130 drag->drag_offer.base.implementation =
1131 (void (**)(void)) &drag_offer_interface;
1133 wl_display_add_object(display, &drag->drag_offer.base);
1135 wlsc_input_device_start_grab(device, time,
1136 WLSC_DEVICE_GRAB_DRAG);
1138 device->drag = drag;
1139 target = pick_surface(device, &sx, &sy);
1140 wl_drag_set_pointer_focus(device->drag, target, time,
1141 device->x, device->y, sx, sy);
1145 drag_cancel(struct wl_client *client, struct wl_drag *drag)
1147 struct wlsc_input_device *device =
1148 (struct wlsc_input_device *) drag->input_device;
1150 if (drag->source == NULL || drag->source->client != client)
1153 wlsc_input_device_end_grab(device, get_time());
1154 device->drag = NULL;
1157 static const struct wl_drag_interface drag_interface = {
1164 wlsc_input_device_init(struct wlsc_input_device *device,
1165 struct wlsc_compositor *ec)
1167 device->base.base.interface = &wl_input_device_interface;
1168 device->base.base.implementation =
1169 (void (**)(void)) &input_device_interface;
1170 wl_display_add_object(ec->wl_display, &device->base.base);
1171 wl_display_add_global(ec->wl_display, &device->base.base, NULL);
1176 device->sprite = wlsc_surface_create(ec, &ec->argb_visual,
1177 device->x, device->y, 32, 32);
1178 device->hotspot_x = 16;
1179 device->hotspot_y = 16;
1181 device->listener.func = handle_surface_destroy;
1182 wl_list_insert(ec->surface_destroy_listener_list.prev,
1183 &device->listener.link);
1184 wl_list_insert(ec->input_device_list.prev, &device->link);
1186 wlsc_input_device_set_pointer_image(device, WLSC_POINTER_LEFT_PTR);
1190 wlsc_output_post_geometry(struct wl_client *client, struct wl_object *global)
1192 struct wlsc_output *output =
1193 container_of(global, struct wlsc_output, base);
1195 wl_client_post_event(client, global,
1197 output->width, output->height);
1200 static const char vertex_shader[] =
1201 "uniform mat4 proj;\n"
1202 "attribute vec4 position;\n"
1203 "attribute vec2 texcoord;\n"
1204 "varying vec2 v_texcoord;\n"
1207 " gl_Position = proj * position;\n"
1208 " v_texcoord = texcoord;\n"
1211 static const char fragment_shader[] =
1212 /* "precision mediump float;\n" */
1213 "varying vec2 v_texcoord;\n"
1214 "uniform sampler2D tex;\n"
1217 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
1221 init_shaders(struct wlsc_compositor *ec)
1223 GLuint v, f, program;
1226 GLfloat vertices[4 * 5];
1230 v = glCreateShader(GL_VERTEX_SHADER);
1231 glShaderSource(v, 1, &p, NULL);
1233 glGetShaderiv(v, GL_COMPILE_STATUS, &status);
1235 glGetShaderInfoLog(v, sizeof msg, NULL, msg);
1236 fprintf(stderr, "vertex shader info: %s\n", msg);
1240 p = fragment_shader;
1241 f = glCreateShader(GL_FRAGMENT_SHADER);
1242 glShaderSource(f, 1, &p, NULL);
1244 glGetShaderiv(f, GL_COMPILE_STATUS, &status);
1246 glGetShaderInfoLog(f, sizeof msg, NULL, msg);
1247 fprintf(stderr, "fragment shader info: %s\n", msg);
1251 program = glCreateProgram();
1252 glAttachShader(program, v);
1253 glAttachShader(program, f);
1254 glBindAttribLocation(program, 0, "position");
1255 glBindAttribLocation(program, 1, "texcoord");
1257 glLinkProgram(program);
1258 glGetProgramiv(program, GL_LINK_STATUS, &status);
1260 glGetProgramInfoLog(program, sizeof msg, NULL, msg);
1261 fprintf(stderr, "link info: %s\n", msg);
1265 glUseProgram(program);
1266 ec->proj_uniform = glGetUniformLocation(program, "proj");
1267 ec->tex_uniform = glGetUniformLocation(program, "tex");
1293 glGenBuffers(1, &ec->vbo);
1294 glBindBuffer(GL_ARRAY_BUFFER, ec->vbo);
1295 glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);
1300 static const struct wl_interface visual_interface = {
1305 add_visuals(struct wlsc_compositor *ec)
1307 ec->argb_visual.base.interface = &visual_interface;
1308 ec->argb_visual.base.implementation = NULL;
1309 wl_display_add_object(ec->wl_display, &ec->argb_visual.base);
1310 wl_display_add_global(ec->wl_display, &ec->argb_visual.base, NULL);
1312 ec->premultiplied_argb_visual.base.interface = &visual_interface;
1313 ec->premultiplied_argb_visual.base.implementation = NULL;
1314 wl_display_add_object(ec->wl_display,
1315 &ec->premultiplied_argb_visual.base);
1316 wl_display_add_global(ec->wl_display,
1317 &ec->premultiplied_argb_visual.base, NULL);
1319 ec->rgb_visual.base.interface = &visual_interface;
1320 ec->rgb_visual.base.implementation = NULL;
1321 wl_display_add_object(ec->wl_display, &ec->rgb_visual.base);
1322 wl_display_add_global(ec->wl_display, &ec->rgb_visual.base, NULL);
1326 wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
1327 int x, int y, int width, int height)
1329 output->compositor = c;
1332 output->width = width;
1333 output->height = height;
1335 output->background =
1336 background_create(output, option_background);
1338 wlsc_matrix_init(&output->matrix);
1339 wlsc_matrix_translate(&output->matrix,
1340 -output->x - output->width / 2.0,
1341 -output->y - output->height / 2.0, 0);
1342 wlsc_matrix_scale(&output->matrix,
1343 2.0 / output->width, 2.0 / output->height, 1);
1345 output->base.interface = &wl_output_interface;
1346 wl_display_add_object(c->wl_display, &output->base);
1347 wl_display_add_global(c->wl_display, &output->base,
1348 wlsc_output_post_geometry);
1352 wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
1354 struct wl_event_loop *loop;
1356 ec->wl_display = display;
1358 ec->base.base.interface = &wl_compositor_interface;
1359 ec->base.base.implementation =
1360 (void (**)(void)) &compositor_interface;
1362 wl_display_add_object(display, &ec->base.base);
1363 if (wl_display_add_global(display, &ec->base.base, NULL))
1368 ec->shell.base.interface = &wl_shell_interface;
1369 ec->shell.base.implementation = (void (**)(void)) &shell_interface;
1370 wl_display_add_object(display, &ec->shell.base);
1371 if (wl_display_add_global(display, &ec->shell.base, NULL))
1376 wl_list_init(&ec->surface_list);
1377 wl_list_init(&ec->input_device_list);
1378 wl_list_init(&ec->output_list);
1379 wl_list_init(&ec->surface_destroy_listener_list);
1381 create_pointer_images(ec);
1383 screenshooter_create(ec);
1385 glGenFramebuffers(1, &ec->fbo);
1386 glBindFramebuffer(GL_FRAMEBUFFER, ec->fbo);
1387 glActiveTexture(GL_TEXTURE0);
1388 if (init_shaders(ec) < 0)
1391 loop = wl_display_get_event_loop(ec->wl_display);
1392 ec->timer_source = wl_event_loop_add_timer(loop, repaint, ec);
1393 wlsc_compositor_schedule_repaint(ec);
1398 int main(int argc, char *argv[])
1400 struct wl_display *display;
1401 struct wlsc_compositor *ec;
1402 GError *error = NULL;
1403 GOptionContext *context;
1406 g_type_init(); /* GdkPixbuf needs this, it seems. */
1408 context = g_option_context_new(NULL);
1409 g_option_context_add_main_entries(context, option_entries, "Wayland");
1410 if (!g_option_context_parse(context, &argc, &argv, &error)) {
1411 fprintf(stderr, "option parsing failed: %s\n", error->message);
1414 if (sscanf(option_geometry, "%dx%d", &width, &height) != 2) {
1415 fprintf(stderr, "invalid geometry option: %s \n",
1420 display = wl_display_create();
1422 if (getenv("WAYLAND_DISPLAY"))
1423 ec = wayland_compositor_create(display, width, height);
1424 else if (getenv("DISPLAY"))
1425 ec = x11_compositor_create(display, width, height);
1427 ec = drm_compositor_create(display, option_connector);
1430 fprintf(stderr, "failed to create compositor\n");
1434 if (wl_display_add_socket(display, option_socket_name)) {
1435 fprintf(stderr, "failed to add socket: %m\n");
1439 wl_display_run(display);
1441 wl_display_destroy(display);