2 * Copyright © 2008 Kristian Høgsberg
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 #include "../config.h"
35 #include <glib-object.h>
36 #include <gdk-pixbuf/gdk-pixbuf.h>
39 #include <wayland-egl.h>
43 #include <EGL/eglext.h>
49 #include <X11/extensions/XKBcommon.h>
51 #include <linux/input.h>
52 #include "wayland-util.h"
53 #include "wayland-client.h"
54 #include "wayland-glib.h"
55 #include "cairo-util.h"
60 struct wl_display *display;
61 struct wl_egl_display *native_dpy;
62 struct wl_compositor *compositor;
63 struct wl_shell *shell;
65 struct wl_output *output;
66 struct rectangle screen_allocation;
70 cairo_device_t *device;
74 struct wl_list window_list;
75 struct wl_list input_list;
77 cairo_surface_t *active_frame, *inactive_frame, *shadow;
79 cairo_surface_t **pointer_surfaces;
81 display_global_handler_t global_handler;
83 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
84 PFNEGLCREATEIMAGEKHRPROC create_image;
85 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
89 struct display *display;
90 struct window *parent;
91 struct wl_surface *surface;
93 struct rectangle allocation, saved_allocation, server_allocation;
97 int minimum_width, minimum_height;
101 struct input *grab_device;
102 struct input *keyboard_device;
104 enum window_buffer_type buffer_type;
107 cairo_surface_t *cairo_surface, *pending_surface;
109 window_resize_handler_t resize_handler;
110 window_redraw_handler_t redraw_handler;
111 window_key_handler_t key_handler;
112 window_button_handler_t button_handler;
113 window_keyboard_focus_handler_t keyboard_focus_handler;
114 window_motion_handler_t motion_handler;
121 struct display *display;
122 struct wl_input_device *input_device;
123 struct window *pointer_focus;
124 struct window *keyboard_focus;
125 struct selection_offer *offer;
126 uint32_t current_pointer_image;
128 int32_t x, y, sx, sy;
133 POINTER_DEFAULT = 100,
137 enum window_location {
139 WINDOW_RESIZING_TOP = 1,
140 WINDOW_RESIZING_BOTTOM = 2,
141 WINDOW_RESIZING_LEFT = 4,
142 WINDOW_RESIZING_TOP_LEFT = 5,
143 WINDOW_RESIZING_BOTTOM_LEFT = 6,
144 WINDOW_RESIZING_RIGHT = 8,
145 WINDOW_RESIZING_TOP_RIGHT = 9,
146 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
147 WINDOW_RESIZING_MASK = 15,
148 WINDOW_EXTERIOR = 16,
149 WINDOW_TITLEBAR = 17,
150 WINDOW_CLIENT_AREA = 18,
153 const char *option_xkb_layout = "us";
154 const char *option_xkb_variant = "";
155 const char *option_xkb_options = "";
157 static const GOptionEntry xkb_option_entries[] = {
158 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
159 &option_xkb_layout, "XKB Layout" },
160 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
161 &option_xkb_variant, "XKB Variant" },
162 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
163 &option_xkb_options, "XKB Options" },
168 rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
170 cairo_move_to(cr, x0, y0 + radius);
171 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
172 cairo_line_to(cr, x1 - radius, y0);
173 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
174 cairo_line_to(cr, x1, y1 - radius);
175 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
176 cairo_line_to(cr, x0 + radius, y1);
177 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
178 cairo_close_path(cr);
181 static const cairo_user_data_key_t surface_data_key;
182 struct surface_data {
183 struct wl_buffer *buffer;
186 #define MULT(_d,c,a,t) \
187 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
189 #ifdef HAVE_CAIRO_EGL
191 struct egl_image_surface_data {
192 struct surface_data data;
195 struct display *display;
196 struct wl_egl_pixmap *pixmap;
200 egl_image_surface_data_destroy(void *p)
202 struct egl_image_surface_data *data = p;
203 struct display *d = data->display;
205 cairo_device_acquire(d->device);
206 glDeleteTextures(1, &data->texture);
207 cairo_device_release(d->device);
209 d->destroy_image(d->dpy, data->image);
210 wl_buffer_destroy(data->data.buffer);
211 wl_egl_pixmap_destroy(data->pixmap);
216 display_get_image_for_egl_image_surface(struct display *display,
217 cairo_surface_t *surface)
219 struct egl_image_surface_data *data;
221 data = cairo_surface_get_user_data (surface, &surface_data_key);
226 static cairo_surface_t *
227 display_create_egl_image_surface(struct display *display,
228 struct rectangle *rectangle)
230 struct egl_image_surface_data *data;
231 EGLDisplay dpy = display->dpy;
232 cairo_surface_t *surface;
233 struct wl_visual *visual;
235 data = malloc(sizeof *data);
239 data->display = display;
241 visual = wl_display_get_premultiplied_argb_visual(display->display);
242 data->pixmap =wl_egl_pixmap_create(display->native_dpy,
246 if (data->pixmap == NULL) {
251 data->image = display->create_image(dpy, NULL,
252 EGL_NATIVE_PIXMAP_KHR,
253 (EGLClientBuffer) data->pixmap,
255 if (data->image == EGL_NO_IMAGE_KHR) {
256 wl_egl_pixmap_destroy(data->pixmap);
262 wl_egl_pixmap_create_buffer(display->native_dpy, data->pixmap);
264 cairo_device_acquire(display->device);
265 glGenTextures(1, &data->texture);
266 glBindTexture(GL_TEXTURE_2D, data->texture);
267 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
268 cairo_device_release(display->device);
270 surface = cairo_gl_surface_create_for_texture(display->device,
271 CAIRO_CONTENT_COLOR_ALPHA,
276 cairo_surface_set_user_data (surface, &surface_data_key,
277 data, egl_image_surface_data_destroy);
282 static cairo_surface_t *
283 display_create_egl_image_surface_from_file(struct display *display,
284 const char *filename,
285 struct rectangle *rect)
287 cairo_surface_t *surface;
289 GError *error = NULL;
291 unsigned char *pixels, *p, *end;
292 struct egl_image_surface_data *data;
294 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
295 rect->width, rect->height,
300 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
301 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
302 g_object_unref(pixbuf);
307 stride = gdk_pixbuf_get_rowstride(pixbuf);
308 pixels = gdk_pixbuf_get_pixels(pixbuf);
310 for (i = 0; i < rect->height; i++) {
311 p = pixels + i * stride;
312 end = p + rect->width * 4;
316 MULT(p[0], p[0], p[3], t);
317 MULT(p[1], p[1], p[3], t);
318 MULT(p[2], p[2], p[3], t);
324 surface = display_create_egl_image_surface(display, rect);
325 if (surface == NULL) {
326 g_object_unref(pixbuf);
330 data = cairo_surface_get_user_data(surface, &surface_data_key);
332 cairo_device_acquire(display->device);
333 glBindTexture(GL_TEXTURE_2D, data->texture);
334 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
335 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
336 cairo_device_release(display->device);
338 g_object_unref(pixbuf);
346 display_get_buffer_for_surface(struct display *display,
347 cairo_surface_t *surface)
349 struct surface_data *data;
351 data = cairo_surface_get_user_data (surface, &surface_data_key);
356 struct shm_surface_data {
357 struct surface_data data;
363 shm_surface_data_destroy(void *p)
365 struct shm_surface_data *data = p;
367 wl_buffer_destroy(data->data.buffer);
368 munmap(data->map, data->length);
371 static cairo_surface_t *
372 display_create_shm_surface(struct display *display,
373 struct rectangle *rectangle)
375 struct shm_surface_data *data;
376 cairo_surface_t *surface;
377 struct wl_visual *visual;
379 char filename[] = "/tmp/wayland-shm-XXXXXX";
381 data = malloc(sizeof *data);
385 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
387 data->length = stride * rectangle->height;
388 fd = mkstemp(filename);
390 fprintf(stderr, "open %s failed: %m\n", filename);
393 if (ftruncate(fd, data->length) < 0) {
394 fprintf(stderr, "ftruncate failed: %m\n");
399 data->map = mmap(NULL, data->length,
400 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
403 if (data->map == MAP_FAILED) {
404 fprintf(stderr, "mmap failed: %m\n");
409 surface = cairo_image_surface_create_for_data (data->map,
415 cairo_surface_set_user_data (surface, &surface_data_key,
416 data, shm_surface_data_destroy);
418 visual = wl_display_get_premultiplied_argb_visual(display->display);
419 data->data.buffer = wl_shm_create_buffer(display->shm,
430 static cairo_surface_t *
431 display_create_shm_surface_from_file(struct display *display,
432 const char *filename,
433 struct rectangle *rect)
435 cairo_surface_t *surface;
437 GError *error = NULL;
439 unsigned char *pixels, *p, *end, *dest_data;
443 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
444 rect->width, rect->height,
449 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
450 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
451 g_object_unref(pixbuf);
455 stride = gdk_pixbuf_get_rowstride(pixbuf);
456 pixels = gdk_pixbuf_get_pixels(pixbuf);
458 surface = display_create_shm_surface(display, rect);
459 if (surface == NULL) {
460 g_object_unref(pixbuf);
464 dest_data = cairo_image_surface_get_data (surface);
465 dest_stride = cairo_image_surface_get_stride (surface);
467 for (i = 0; i < rect->height; i++) {
468 d = (uint32_t *) (dest_data + i * dest_stride);
469 p = pixels + i * stride;
470 end = p + rect->width * 4;
473 unsigned char a, r, g, b;
480 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
484 g_object_unref(pixbuf);
490 check_size(struct rectangle *rect)
492 if (rect->width && rect->height)
495 fprintf(stderr, "tried to create surface of "
496 "width: %d, height: %d\n", rect->width, rect->height);
501 display_create_surface(struct display *display,
502 struct rectangle *rectangle)
504 if (check_size(rectangle) < 0)
506 #ifdef HAVE_CAIRO_EGL
508 return display_create_egl_image_surface(display, rectangle);
511 return display_create_shm_surface(display, rectangle);
514 static cairo_surface_t *
515 display_create_surface_from_file(struct display *display,
516 const char *filename,
517 struct rectangle *rectangle)
519 if (check_size(rectangle) < 0)
521 #ifdef HAVE_CAIRO_EGL
523 return display_create_egl_image_surface_from_file(display,
528 return display_create_shm_surface_from_file(display, filename, rectangle);
530 static const struct {
531 const char *filename;
532 int hotspot_x, hotspot_y;
533 } pointer_images[] = {
534 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
535 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
536 { DATADIR "/wayland/bottom_side.png", 16, 20 },
537 { DATADIR "/wayland/grabbing.png", 20, 17 },
538 { DATADIR "/wayland/left_ptr.png", 10, 5 },
539 { DATADIR "/wayland/left_side.png", 10, 20 },
540 { DATADIR "/wayland/right_side.png", 30, 19 },
541 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
542 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
543 { DATADIR "/wayland/top_side.png", 18, 8 },
544 { DATADIR "/wayland/xterm.png", 15, 15 },
545 { DATADIR "/wayland/hand1.png", 18, 11 }
549 create_pointer_surfaces(struct display *display)
552 const int width = 32, height = 32;
553 struct rectangle rect;
555 count = ARRAY_LENGTH(pointer_images);
556 display->pointer_surfaces =
557 malloc(count * sizeof *display->pointer_surfaces);
559 rect.height = height;
560 for (i = 0; i < count; i++) {
561 display->pointer_surfaces[i] =
562 display_create_surface_from_file(display,
563 pointer_images[i].filename,
570 display_get_pointer_surface(struct display *display, int pointer,
571 int *width, int *height,
572 int *hotspot_x, int *hotspot_y)
574 cairo_surface_t *surface;
576 surface = display->pointer_surfaces[pointer];
578 *width = cairo_gl_surface_get_width(surface);
579 *height = cairo_gl_surface_get_height(surface);
581 *width = cairo_image_surface_get_width(surface);
582 *height = cairo_image_surface_get_height(surface);
584 *hotspot_x = pointer_images[pointer].hotspot_x;
585 *hotspot_y = pointer_images[pointer].hotspot_y;
587 return cairo_surface_reference(surface);
592 window_attach_surface(struct window *window);
595 free_surface(void *data)
597 struct window *window = data;
599 cairo_surface_destroy(window->pending_surface);
600 window->pending_surface = NULL;
601 if (window->cairo_surface)
602 window_attach_surface(window);
606 window_attach_surface(struct window *window)
608 struct display *display = window->display;
609 struct wl_buffer *buffer;
612 if (window->pending_surface != NULL)
615 window->pending_surface = window->cairo_surface;
616 window->cairo_surface = NULL;
618 buffer = display_get_buffer_for_surface(display,
619 window->pending_surface);
620 if (window->resize_edges & WINDOW_RESIZING_LEFT)
621 x = window->server_allocation.width -
622 window->allocation.width;
626 if (window->resize_edges & WINDOW_RESIZING_TOP)
627 y = window->server_allocation.height -
628 window->allocation.height;
632 window->server_allocation = window->allocation;
633 window->resize_edges = 0;
634 wl_surface_attach(window->surface, buffer, x, y);
635 wl_display_sync_callback(display->display, free_surface, window);
637 if (window->fullscreen)
638 wl_surface_map_fullscreen(window->surface);
639 else if (!window->parent)
640 wl_surface_map_toplevel(window->surface);
642 wl_surface_map_transient(window->surface,
643 window->parent->surface,
644 window->x, window->y, 0);
646 wl_surface_damage(window->surface, 0, 0,
647 window->allocation.width,
648 window->allocation.height);
652 window_flush(struct window *window)
654 if (window->cairo_surface)
655 window_attach_surface(window);
659 window_set_surface(struct window *window, cairo_surface_t *surface)
661 cairo_surface_reference(surface);
663 if (window->cairo_surface != NULL)
664 cairo_surface_destroy(window->cairo_surface);
666 window->cairo_surface = surface;
670 window_create_surface(struct window *window)
672 cairo_surface_t *surface;
674 switch (window->buffer_type) {
675 #ifdef HAVE_CAIRO_EGL
676 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
677 surface = display_create_surface(window->display,
678 &window->allocation);
681 case WINDOW_BUFFER_TYPE_SHM:
682 surface = display_create_shm_surface(window->display,
683 &window->allocation);
690 window_set_surface(window, surface);
691 cairo_surface_destroy(surface);
695 window_draw_menu(struct window *window)
698 int width, height, r = 5;
700 window_create_surface(window);
702 cr = cairo_create(window->cairo_surface);
703 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
704 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
707 width = window->allocation.width;
708 height = window->allocation.height;
709 rounded_rect(cr, r, r, width - r, height - r, r);
710 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
711 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
717 window_draw_decorations(struct window *window)
720 cairo_text_extents_t extents;
721 cairo_surface_t *frame;
722 int width, height, shadow_dx = 3, shadow_dy = 3;
724 window_create_surface(window);
726 width = window->allocation.width;
727 height = window->allocation.height;
729 cr = cairo_create(window->cairo_surface);
731 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
732 cairo_set_source_rgba(cr, 0, 0, 0, 0);
735 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
736 tile_mask(cr, window->display->shadow,
737 shadow_dx, shadow_dy, width, height,
738 window->margin + 10 - shadow_dx,
739 window->margin + 10 - shadow_dy);
741 if (window->keyboard_device)
742 frame = window->display->active_frame;
744 frame = window->display->inactive_frame;
746 tile_source(cr, frame, 0, 0, width, height,
747 window->margin + 10, window->margin + 50);
749 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
750 cairo_set_font_size(cr, 14);
751 cairo_text_extents(cr, window->title, &extents);
752 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
753 if (window->keyboard_device)
754 cairo_set_source_rgb(cr, 0, 0, 0);
756 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
757 cairo_show_text(cr, window->title);
761 cairo_device_flush (window->display->device);
765 window_destroy(struct window *window)
767 wl_surface_destroy(window->surface);
768 wl_list_remove(&window->link);
773 display_flush_cairo_device(struct display *display)
775 cairo_device_flush (display->device);
779 window_draw(struct window *window)
782 window_draw_menu(window);
783 else if (window->fullscreen || !window->decoration)
784 window_create_surface(window);
786 window_draw_decorations(window);
790 window_get_surface(struct window *window)
792 return cairo_surface_reference(window->cairo_surface);
796 get_pointer_location(struct window *window, int32_t x, int32_t y)
798 int vlocation, hlocation, location;
799 const int grip_size = 8;
801 if (!window->decoration)
802 return WINDOW_CLIENT_AREA;
804 if (x < window->margin)
805 hlocation = WINDOW_EXTERIOR;
806 else if (window->margin <= x && x < window->margin + grip_size)
807 hlocation = WINDOW_RESIZING_LEFT;
808 else if (x < window->allocation.width - window->margin - grip_size)
809 hlocation = WINDOW_INTERIOR;
810 else if (x < window->allocation.width - window->margin)
811 hlocation = WINDOW_RESIZING_RIGHT;
813 hlocation = WINDOW_EXTERIOR;
815 if (y < window->margin)
816 vlocation = WINDOW_EXTERIOR;
817 else if (window->margin <= y && y < window->margin + grip_size)
818 vlocation = WINDOW_RESIZING_TOP;
819 else if (y < window->allocation.height - window->margin - grip_size)
820 vlocation = WINDOW_INTERIOR;
821 else if (y < window->allocation.height - window->margin)
822 vlocation = WINDOW_RESIZING_BOTTOM;
824 vlocation = WINDOW_EXTERIOR;
826 location = vlocation | hlocation;
827 if (location & WINDOW_EXTERIOR)
828 location = WINDOW_EXTERIOR;
829 if (location == WINDOW_INTERIOR && y < window->margin + 50)
830 location = WINDOW_TITLEBAR;
831 else if (location == WINDOW_INTERIOR)
832 location = WINDOW_CLIENT_AREA;
838 set_pointer_image(struct input *input, uint32_t time, int pointer)
840 struct display *display = input->display;
841 struct wl_buffer *buffer;
842 cairo_surface_t *surface;
845 location = get_pointer_location(input->pointer_focus,
846 input->sx, input->sy);
848 case WINDOW_RESIZING_TOP:
849 pointer = POINTER_TOP;
851 case WINDOW_RESIZING_BOTTOM:
852 pointer = POINTER_BOTTOM;
854 case WINDOW_RESIZING_LEFT:
855 pointer = POINTER_LEFT;
857 case WINDOW_RESIZING_RIGHT:
858 pointer = POINTER_RIGHT;
860 case WINDOW_RESIZING_TOP_LEFT:
861 pointer = POINTER_TOP_LEFT;
863 case WINDOW_RESIZING_TOP_RIGHT:
864 pointer = POINTER_TOP_RIGHT;
866 case WINDOW_RESIZING_BOTTOM_LEFT:
867 pointer = POINTER_BOTTOM_LEFT;
869 case WINDOW_RESIZING_BOTTOM_RIGHT:
870 pointer = POINTER_BOTTOM_RIGHT;
872 case WINDOW_EXTERIOR:
873 case WINDOW_TITLEBAR:
874 if (input->current_pointer_image == POINTER_DEFAULT)
877 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
878 input->current_pointer_image = POINTER_DEFAULT;
884 if (pointer == input->current_pointer_image)
887 input->current_pointer_image = pointer;
888 surface = display->pointer_surfaces[pointer];
889 buffer = display_get_buffer_for_surface(display, surface);
890 wl_input_device_attach(input->input_device, time, buffer,
891 pointer_images[pointer].hotspot_x,
892 pointer_images[pointer].hotspot_y);
896 window_handle_motion(void *data, struct wl_input_device *input_device,
898 int32_t x, int32_t y, int32_t sx, int32_t sy)
900 struct input *input = data;
901 struct window *window = input->pointer_focus;
902 int pointer = POINTER_LEFT_PTR;
909 if (window->motion_handler)
910 pointer = (*window->motion_handler)(window, input, time,
914 set_pointer_image(input, time, pointer);
918 window_handle_button(void *data,
919 struct wl_input_device *input_device,
920 uint32_t time, uint32_t button, uint32_t state)
922 struct input *input = data;
923 struct window *window = input->pointer_focus;
926 location = get_pointer_location(window, input->sx, input->sy);
928 if (button == BTN_LEFT && state == 1) {
930 case WINDOW_TITLEBAR:
931 wl_shell_move(window->display->shell,
932 window->surface, input_device, time);
934 case WINDOW_RESIZING_TOP:
935 case WINDOW_RESIZING_BOTTOM:
936 case WINDOW_RESIZING_LEFT:
937 case WINDOW_RESIZING_RIGHT:
938 case WINDOW_RESIZING_TOP_LEFT:
939 case WINDOW_RESIZING_TOP_RIGHT:
940 case WINDOW_RESIZING_BOTTOM_LEFT:
941 case WINDOW_RESIZING_BOTTOM_RIGHT:
942 wl_shell_resize(window->display->shell,
943 window->surface, input_device, time,
946 case WINDOW_CLIENT_AREA:
947 if (window->button_handler)
948 (*window->button_handler)(window,
955 if (window->button_handler)
956 (*window->button_handler)(window,
964 window_handle_key(void *data, struct wl_input_device *input_device,
965 uint32_t time, uint32_t key, uint32_t state)
967 struct input *input = data;
968 struct window *window = input->keyboard_focus;
969 struct display *d = window->display;
970 uint32_t code, sym, level;
972 code = key + d->xkb->min_key_code;
973 if (window->keyboard_device != input)
977 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
978 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
981 sym = XkbKeySymEntry(d->xkb, code, level, 0);
984 input->modifiers |= d->xkb->map->modmap[code];
986 input->modifiers &= ~d->xkb->map->modmap[code];
988 if (window->key_handler)
989 (*window->key_handler)(window, input, time, key, sym, state,
994 window_handle_pointer_focus(void *data,
995 struct wl_input_device *input_device,
996 uint32_t time, struct wl_surface *surface,
997 int32_t x, int32_t y, int32_t sx, int32_t sy)
999 struct input *input = data;
1000 struct window *window;
1004 input->pointer_focus = wl_surface_get_user_data(surface);
1005 window = input->pointer_focus;
1012 pointer = POINTER_LEFT_PTR;
1013 if (window->motion_handler)
1014 pointer = (*window->motion_handler)(window,
1019 set_pointer_image(input, time, pointer);
1021 input->pointer_focus = NULL;
1022 input->current_pointer_image = POINTER_UNSET;
1027 window_handle_keyboard_focus(void *data,
1028 struct wl_input_device *input_device,
1030 struct wl_surface *surface,
1031 struct wl_array *keys)
1033 struct input *input = data;
1034 struct window *window = input->keyboard_focus;
1035 struct display *d = input->display;
1038 window = input->keyboard_focus;
1040 window->keyboard_device = NULL;
1041 if (window->keyboard_focus_handler)
1042 (*window->keyboard_focus_handler)(window, NULL,
1047 input->keyboard_focus = wl_surface_get_user_data(surface);
1049 input->keyboard_focus = NULL;
1051 end = keys->data + keys->size;
1052 input->modifiers = 0;
1053 for (k = keys->data; k < end; k++)
1054 input->modifiers |= d->xkb->map->modmap[*k];
1056 window = input->keyboard_focus;
1058 window->keyboard_device = input;
1059 if (window->keyboard_focus_handler)
1060 (*window->keyboard_focus_handler)(window,
1061 window->keyboard_device,
1066 static const struct wl_input_device_listener input_device_listener = {
1067 window_handle_motion,
1068 window_handle_button,
1070 window_handle_pointer_focus,
1071 window_handle_keyboard_focus,
1075 input_get_position(struct input *input, int32_t *x, int32_t *y)
1081 struct wl_input_device *
1082 input_get_input_device(struct input *input)
1084 return input->input_device;
1088 input_get_modifiers(struct input *input)
1090 return input->modifiers;
1094 window_create_drag(struct window *window)
1096 cairo_device_flush (window->display->device);
1098 return wl_shell_create_drag(window->display->shell);
1102 window_move(struct window *window, struct input *input, uint32_t time)
1104 wl_shell_move(window->display->shell,
1105 window->surface, input->input_device, time);
1109 window_activate_drag(struct wl_drag *drag, struct window *window,
1110 struct input *input, uint32_t time)
1112 wl_drag_activate(drag, window->surface, input->input_device, time);
1116 handle_configure(void *data, struct wl_shell *shell,
1117 uint32_t time, uint32_t edges,
1118 struct wl_surface *surface, int32_t width, int32_t height)
1120 struct window *window = wl_surface_get_user_data(surface);
1121 int32_t child_width, child_height;
1123 /* FIXME: this is probably the wrong place to check for width
1124 * or height <= 0, but it prevents the compositor from crashing
1126 if (width <= 0 || height <= 0)
1129 window->resize_edges = edges;
1131 if (window->resize_handler) {
1132 child_width = width - 20 - window->margin * 2;
1133 child_height = height - 60 - window->margin * 2;
1135 (*window->resize_handler)(window,
1136 child_width, child_height,
1139 window->allocation.width = width;
1140 window->allocation.height = height;
1142 if (window->redraw_handler)
1143 window_schedule_redraw(window);
1147 static const struct wl_shell_listener shell_listener = {
1152 window_get_child_allocation(struct window *window,
1153 struct rectangle *allocation)
1155 if (window->fullscreen || !window->decoration) {
1156 *allocation = window->allocation;
1158 allocation->x = window->margin + 10;
1159 allocation->y = window->margin + 50;
1161 window->allocation.width - 20 - window->margin * 2;
1162 allocation->height =
1163 window->allocation.height - 60 - window->margin * 2;
1168 window_set_child_size(struct window *window, int32_t width, int32_t height)
1170 if (!window->fullscreen) {
1171 window->allocation.x = 20 + window->margin;
1172 window->allocation.y = 60 + window->margin;
1173 window->allocation.width = width + 20 + window->margin * 2;
1174 window->allocation.height = height + 60 + window->margin * 2;
1176 window->allocation.x = 0;
1177 window->allocation.y = 0;
1178 window->allocation.width = width;
1179 window->allocation.height = height;
1184 idle_redraw(void *data)
1186 struct window *window = data;
1188 window->redraw_handler(window, window->user_data);
1190 window->redraw_scheduled = 0;
1196 window_schedule_redraw(struct window *window)
1198 if (!window->redraw_scheduled) {
1199 g_idle_add(idle_redraw, window);
1200 window->redraw_scheduled = 1;
1205 window_set_fullscreen(struct window *window, int fullscreen)
1207 int32_t width, height;
1209 if (window->fullscreen == fullscreen)
1212 window->fullscreen = fullscreen;
1213 if (window->fullscreen) {
1214 window->saved_allocation = window->allocation;
1215 width = window->display->screen_allocation.width;
1216 height = window->display->screen_allocation.height;
1218 width = window->saved_allocation.width - 20 - window->margin * 2;
1219 height = window->saved_allocation.height - 60 - window->margin * 2;
1222 (*window->resize_handler)(window, width, height, window->user_data);
1226 window_set_decoration(struct window *window, int decoration)
1228 window->decoration = decoration;
1232 window_set_user_data(struct window *window, void *data)
1234 window->user_data = data;
1238 window_get_user_data(struct window *window)
1240 return window->user_data;
1244 window_set_resize_handler(struct window *window,
1245 window_resize_handler_t handler)
1247 window->resize_handler = handler;
1251 window_set_redraw_handler(struct window *window,
1252 window_redraw_handler_t handler)
1254 window->redraw_handler = handler;
1258 window_set_key_handler(struct window *window,
1259 window_key_handler_t handler)
1261 window->key_handler = handler;
1265 window_set_button_handler(struct window *window,
1266 window_button_handler_t handler)
1268 window->button_handler = handler;
1272 window_set_motion_handler(struct window *window,
1273 window_motion_handler_t handler)
1275 window->motion_handler = handler;
1279 window_set_keyboard_focus_handler(struct window *window,
1280 window_keyboard_focus_handler_t handler)
1282 window->keyboard_focus_handler = handler;
1286 window_set_title(struct window *window, const char *title)
1288 free(window->title);
1289 window->title = strdup(title);
1293 window_get_title(struct window *window)
1295 return window->title;
1299 window_damage(struct window *window, int32_t x, int32_t y,
1300 int32_t width, int32_t height)
1302 wl_surface_damage(window->surface, x, y, width, height);
1305 static struct window *
1306 window_create_internal(struct display *display, struct window *parent,
1307 int32_t width, int32_t height)
1309 struct window *window;
1311 window = malloc(sizeof *window);
1315 memset(window, 0, sizeof *window);
1316 window->display = display;
1317 window->parent = parent;
1318 window->surface = wl_compositor_create_surface(display->compositor);
1319 window->allocation.x = 0;
1320 window->allocation.y = 0;
1321 window->allocation.width = width;
1322 window->allocation.height = height;
1323 window->saved_allocation = window->allocation;
1324 window->margin = 16;
1325 window->decoration = 1;
1328 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_IMAGE;
1330 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1332 wl_surface_set_user_data(window->surface, window);
1333 wl_list_insert(display->window_list.prev, &window->link);
1339 window_create(struct display *display, int32_t width, int32_t height)
1341 struct window *window;
1343 window = window_create_internal(display, NULL, width, height);
1351 window_create_transient(struct display *display, struct window *parent,
1352 int32_t x, int32_t y, int32_t width, int32_t height)
1354 struct window *window;
1356 window = window_create_internal(parent->display,
1357 parent, width, height);
1368 window_set_buffer_type(struct window *window, enum window_buffer_type type)
1370 window->buffer_type = type;
1374 display_handle_geometry(void *data,
1375 struct wl_output *output,
1376 int32_t x, int32_t y, int32_t width, int32_t height)
1378 struct display *display = data;
1380 display->screen_allocation.x = x;
1381 display->screen_allocation.y = y;
1382 display->screen_allocation.width = width;
1383 display->screen_allocation.height = height;
1386 static const struct wl_output_listener output_listener = {
1387 display_handle_geometry,
1391 display_add_input(struct display *d, uint32_t id)
1393 struct input *input;
1395 input = malloc(sizeof *input);
1399 memset(input, 0, sizeof *input);
1401 input->input_device = wl_input_device_create(d->display, id);
1402 input->pointer_focus = NULL;
1403 input->keyboard_focus = NULL;
1404 wl_list_insert(d->input_list.prev, &input->link);
1406 wl_input_device_add_listener(input->input_device,
1407 &input_device_listener, input);
1408 wl_input_device_set_user_data(input->input_device, input);
1411 struct selection_offer {
1412 struct display *display;
1413 struct wl_selection_offer *offer;
1414 struct wl_array types;
1415 struct input *input;
1419 input_offers_mime_type(struct input *input, const char *type)
1421 struct selection_offer *offer = input->offer;
1427 end = offer->types.data + offer->types.size;
1428 for (p = offer->types.data; p < end; p++)
1429 if (strcmp(*p, type) == 0)
1436 input_receive_mime_type(struct input *input, const char *type, int fd)
1438 struct selection_offer *offer = input->offer;
1440 /* FIXME: A number of things can go wrong here: the object may
1441 * not be the current selection offer any more (which could
1442 * still work, but the source may have gone away or just
1443 * destroyed its wl_selection) or the offer may not have the
1444 * requested type after all (programmer/client error,
1446 wl_selection_offer_receive(offer->offer, type, fd);
1450 selection_offer_offer(void *data,
1451 struct wl_selection_offer *selection_offer,
1454 struct selection_offer *offer = data;
1458 p = wl_array_add(&offer->types, sizeof *p);
1464 selection_offer_keyboard_focus(void *data,
1465 struct wl_selection_offer *selection_offer,
1466 struct wl_input_device *input_device)
1468 struct selection_offer *offer = data;
1469 struct input *input;
1472 if (input_device == NULL) {
1473 printf("selection offer retracted %p\n", selection_offer);
1474 input = offer->input;
1475 input->offer = NULL;
1476 wl_selection_offer_destroy(selection_offer);
1477 wl_array_release(&offer->types);
1482 input = wl_input_device_get_user_data(input_device);
1483 printf("new selection offer %p:", selection_offer);
1485 offer->input = input;
1486 input->offer = offer;
1487 end = offer->types.data + offer->types.size;
1488 for (p = offer->types.data; p < end; p++)
1494 struct wl_selection_offer_listener selection_offer_listener = {
1495 selection_offer_offer,
1496 selection_offer_keyboard_focus
1500 add_selection_offer(struct display *d, uint32_t id)
1502 struct selection_offer *offer;
1504 offer = malloc(sizeof *offer);
1508 offer->offer = wl_selection_offer_create(d->display, id);
1510 wl_array_init(&offer->types);
1511 offer->input = NULL;
1513 wl_selection_offer_add_listener(offer->offer,
1514 &selection_offer_listener, offer);
1518 display_handle_global(struct wl_display *display, uint32_t id,
1519 const char *interface, uint32_t version, void *data)
1521 struct display *d = data;
1523 if (strcmp(interface, "compositor") == 0) {
1524 d->compositor = wl_compositor_create(display, id);
1525 } else if (strcmp(interface, "output") == 0) {
1526 d->output = wl_output_create(display, id);
1527 wl_output_add_listener(d->output, &output_listener, d);
1528 } else if (strcmp(interface, "input_device") == 0) {
1529 display_add_input(d, id);
1530 } else if (strcmp(interface, "shell") == 0) {
1531 d->shell = wl_shell_create(display, id);
1532 wl_shell_add_listener(d->shell, &shell_listener, d);
1533 } else if (strcmp(interface, "shm") == 0) {
1534 d->shm = wl_shm_create(display, id);
1535 } else if (strcmp(interface, "selection_offer") == 0) {
1536 add_selection_offer(d, id);
1537 } else if (d->global_handler) {
1538 d->global_handler(d, interface, id, version);
1543 display_render_frame(struct display *d)
1548 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1549 cr = cairo_create(d->shadow);
1550 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1551 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1552 rounded_rect(cr, 16, 16, 112, 112, radius);
1555 blur_surface(d->shadow, 64);
1558 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1559 cr = cairo_create(d->active_frame);
1560 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1561 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1562 rounded_rect(cr, 16, 16, 112, 112, radius);
1567 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1568 cr = cairo_create(d->inactive_frame);
1569 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1570 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1571 rounded_rect(cr, 16, 16, 112, 112, radius);
1577 init_xkb(struct display *d)
1579 struct xkb_rule_names names;
1581 names.rules = "evdev";
1582 names.model = "pc105";
1583 names.layout = option_xkb_layout;
1584 names.variant = option_xkb_variant;
1585 names.options = option_xkb_options;
1587 d->xkb = xkb_compile_keymap_from_rules(&names);
1589 fprintf(stderr, "Failed to compile keymap\n");
1595 init_egl(struct display *d)
1597 EGLint major, minor;
1599 d->dpy = eglGetDisplay(d->native_dpy);
1600 if (!eglInitialize(d->dpy, &major, &minor)) {
1601 fprintf(stderr, "failed to initialize display\n");
1605 if (!eglBindAPI(EGL_OPENGL_API)) {
1606 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1610 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1611 if (d->ctx == NULL) {
1612 fprintf(stderr, "failed to create context\n");
1616 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1617 fprintf(stderr, "faile to make context current\n");
1621 #ifdef HAVE_CAIRO_EGL
1622 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1623 if (d->device == NULL) {
1624 fprintf(stderr, "failed to get cairo egl device\n");
1633 display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
1636 GOptionContext *context;
1637 GOptionGroup *xkb_option_group;
1642 context = g_option_context_new(NULL);
1644 g_option_context_add_main_entries(context, option_entries, "Wayland View");
1646 xkb_option_group = g_option_group_new("xkb",
1648 "Show all XKB options",
1650 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1651 g_option_context_add_group (context, xkb_option_group);
1653 if (!g_option_context_parse(context, argc, argv, &error)) {
1654 fprintf(stderr, "option parsing failed: %s\n", error->message);
1658 g_option_context_free(context);
1660 d = malloc(sizeof *d);
1664 memset(d, 0, sizeof *d);
1666 d->display = wl_display_connect(NULL);
1667 if (d->display == NULL) {
1668 fprintf(stderr, "failed to create display: %m\n");
1672 wl_list_init(&d->input_list);
1674 /* Set up listener so we'll catch all events. */
1675 wl_display_add_global_listener(d->display,
1676 display_handle_global, d);
1678 d->native_dpy = wl_egl_display_create(d->display);
1680 /* Process connection events. */
1681 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1683 if (init_egl(d) < 0)
1686 d->image_target_texture_2d =
1687 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1688 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1689 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1691 create_pointer_surfaces(d);
1693 display_render_frame(d);
1695 d->loop = g_main_loop_new(NULL, FALSE);
1696 d->source = wl_glib_source_new(d->display);
1697 g_source_attach(d->source, NULL);
1699 wl_list_init(&d->window_list);
1707 display_get_display(struct display *display)
1709 return display->display;
1712 struct wl_compositor *
1713 display_get_compositor(struct display *display)
1715 return display->compositor;
1719 display_get_egl_display(struct display *d)
1725 display_get_shell(struct display *display)
1727 return display->shell;
1731 display_run(struct display *d)
1733 g_main_loop_run(d->loop);
1737 display_set_global_handler(struct display *display,
1738 display_global_handler_t handler)
1740 display->global_handler = handler;