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>
40 #define EGL_EGLEXT_PROTOTYPES 1
41 #define GL_GLEXT_PROTOTYPES 1
44 #include <EGL/eglext.h>
50 #include <X11/extensions/XKBcommon.h>
52 #include <linux/input.h>
53 #include "wayland-util.h"
54 #include "wayland-client.h"
55 #include "wayland-glib.h"
56 #include "cairo-util.h"
61 struct wl_display *display;
62 struct wl_compositor *compositor;
63 struct wl_shell *shell;
66 struct wl_output *output;
67 struct rectangle screen_allocation;
71 cairo_device_t *device;
75 struct wl_list window_list;
76 struct wl_list input_list;
78 cairo_surface_t *active_frame, *inactive_frame, *shadow;
80 cairo_surface_t **pointer_surfaces;
82 display_drag_offer_handler_t drag_offer_handler;
83 display_global_handler_t global_handler;
87 struct display *display;
88 struct wl_surface *surface;
90 struct rectangle allocation, saved_allocation, server_allocation;
93 int minimum_width, minimum_height;
97 struct input *grab_device;
98 struct input *keyboard_device;
100 enum window_buffer_type buffer_type;
104 cairo_surface_t *cairo_surface, *pending_surface;
106 window_resize_handler_t resize_handler;
107 window_redraw_handler_t redraw_handler;
108 window_key_handler_t key_handler;
109 window_button_handler_t button_handler;
110 window_keyboard_focus_handler_t keyboard_focus_handler;
111 window_motion_handler_t motion_handler;
118 struct display *display;
119 struct wl_input_device *input_device;
120 struct window *pointer_focus;
121 struct window *keyboard_focus;
122 uint32_t current_pointer_image;
124 int32_t x, y, sx, sy;
129 POINTER_DEFAULT = 100,
133 enum window_location {
135 WINDOW_RESIZING_TOP = 1,
136 WINDOW_RESIZING_BOTTOM = 2,
137 WINDOW_RESIZING_LEFT = 4,
138 WINDOW_RESIZING_TOP_LEFT = 5,
139 WINDOW_RESIZING_BOTTOM_LEFT = 6,
140 WINDOW_RESIZING_RIGHT = 8,
141 WINDOW_RESIZING_TOP_RIGHT = 9,
142 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
143 WINDOW_RESIZING_MASK = 15,
144 WINDOW_EXTERIOR = 16,
145 WINDOW_TITLEBAR = 17,
146 WINDOW_CLIENT_AREA = 18,
149 const char *option_xkb_layout = "us";
150 const char *option_xkb_variant = "";
151 const char *option_xkb_options = "";
153 static const GOptionEntry xkb_option_entries[] = {
154 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
155 &option_xkb_layout, "XKB Layout" },
156 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
157 &option_xkb_variant, "XKB Variant" },
158 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
159 &option_xkb_options, "XKB Options" },
164 rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
166 cairo_move_to(cr, x0, y0 + radius);
167 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
168 cairo_line_to(cr, x1 - radius, y0);
169 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
170 cairo_line_to(cr, x1, y1 - radius);
171 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
172 cairo_line_to(cr, x0 + radius, y1);
173 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
174 cairo_close_path(cr);
177 static const cairo_user_data_key_t surface_data_key;
178 struct surface_data {
179 struct wl_buffer *buffer;
182 #define MULT(_d,c,a,t) \
183 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
185 #ifdef HAVE_CAIRO_EGL
187 struct drm_surface_data {
188 struct surface_data data;
191 struct display *display;
195 drm_surface_data_destroy(void *p)
197 struct drm_surface_data *data = p;
198 struct display *d = data->display;
200 cairo_device_acquire(d->device);
201 glDeleteTextures(1, &data->texture);
202 cairo_device_release(d->device);
204 eglDestroyImageKHR(d->dpy, data->image);
205 wl_buffer_destroy(data->data.buffer);
209 display_get_image_for_drm_surface(struct display *display,
210 cairo_surface_t *surface)
212 struct drm_surface_data *data;
214 data = cairo_surface_get_user_data (surface, &surface_data_key);
219 static cairo_surface_t *
220 display_create_drm_surface(struct display *display,
221 struct rectangle *rectangle)
223 struct drm_surface_data *data;
224 EGLDisplay dpy = display->dpy;
225 cairo_surface_t *surface;
226 struct wl_visual *visual;
229 EGLint image_attribs[] = {
232 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
233 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
237 data = malloc(sizeof *data);
241 data->display = display;
243 image_attribs[1] = rectangle->width;
244 image_attribs[3] = rectangle->height;
245 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
247 cairo_device_acquire(display->device);
248 glGenTextures(1, &data->texture);
249 glBindTexture(GL_TEXTURE_2D, data->texture);
250 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
251 cairo_device_release(display->device);
253 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
255 visual = wl_display_get_premultiplied_argb_visual(display->display);
257 wl_drm_create_buffer(display->drm, name, rectangle->width,
258 rectangle->height, stride, visual);
260 surface = cairo_gl_surface_create_for_texture(display->device,
261 CAIRO_CONTENT_COLOR_ALPHA,
266 cairo_surface_set_user_data (surface, &surface_data_key,
267 data, drm_surface_data_destroy);
272 static cairo_surface_t *
273 display_create_drm_surface_from_file(struct display *display,
274 const char *filename,
275 struct rectangle *rect)
277 cairo_surface_t *surface;
279 GError *error = NULL;
281 unsigned char *pixels, *p, *end;
282 struct drm_surface_data *data;
284 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
285 rect->width, rect->height,
290 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
291 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
292 gdk_pixbuf_unref(pixbuf);
297 stride = gdk_pixbuf_get_rowstride(pixbuf);
298 pixels = gdk_pixbuf_get_pixels(pixbuf);
300 for (i = 0; i < rect->height; i++) {
301 p = pixels + i * stride;
302 end = p + rect->width * 4;
306 MULT(p[0], p[0], p[3], t);
307 MULT(p[1], p[1], p[3], t);
308 MULT(p[2], p[2], p[3], t);
314 surface = display_create_drm_surface(display, rect);
315 data = cairo_surface_get_user_data(surface, &surface_data_key);
317 cairo_device_acquire(display->device);
318 glBindTexture(GL_TEXTURE_2D, data->texture);
319 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
320 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
321 cairo_device_release(display->device);
323 gdk_pixbuf_unref(pixbuf);
331 display_get_buffer_for_surface(struct display *display,
332 cairo_surface_t *surface)
334 struct surface_data *data;
336 data = cairo_surface_get_user_data (surface, &surface_data_key);
341 struct shm_surface_data {
342 struct surface_data data;
348 shm_surface_data_destroy(void *p)
350 struct shm_surface_data *data = p;
352 wl_buffer_destroy(data->data.buffer);
353 munmap(data->map, data->length);
356 static cairo_surface_t *
357 display_create_shm_surface(struct display *display,
358 struct rectangle *rectangle)
360 struct shm_surface_data *data;
361 cairo_surface_t *surface;
362 struct wl_visual *visual;
364 char filename[] = "/tmp/wayland-shm-XXXXXX";
366 data = malloc(sizeof *data);
370 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
372 data->length = stride * rectangle->height;
373 fd = mkstemp(filename);
375 fprintf(stderr, "open %s failed: %m", filename);
378 if (ftruncate(fd, data->length) < 0) {
379 fprintf(stderr, "ftruncate failed: %m");
384 data->map = mmap(NULL, data->length,
385 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
388 if (data->map == MAP_FAILED) {
389 fprintf(stderr, "mmap failed: %m");
394 surface = cairo_image_surface_create_for_data (data->map,
400 cairo_surface_set_user_data (surface, &surface_data_key,
401 data, shm_surface_data_destroy);
403 visual = wl_display_get_premultiplied_argb_visual(display->display);
404 data->data.buffer = wl_shm_create_buffer(display->shm,
415 static cairo_surface_t *
416 display_create_shm_surface_from_file(struct display *display,
417 const char *filename,
418 struct rectangle *rect)
420 cairo_surface_t *surface;
422 GError *error = NULL;
424 unsigned char *pixels, *p, *end, *dest_data;
428 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
429 rect->width, rect->height,
434 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
435 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
436 gdk_pixbuf_unref(pixbuf);
440 stride = gdk_pixbuf_get_rowstride(pixbuf);
441 pixels = gdk_pixbuf_get_pixels(pixbuf);
443 surface = display_create_shm_surface(display, rect);
444 dest_data = cairo_image_surface_get_data (surface);
445 dest_stride = cairo_image_surface_get_stride (surface);
447 for (i = 0; i < rect->height; i++) {
448 d = (uint32_t *) (dest_data + i * dest_stride);
449 p = pixels + i * stride;
450 end = p + rect->width * 4;
453 unsigned char a, r, g, b;
460 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
464 gdk_pixbuf_unref(pixbuf);
470 display_create_surface(struct display *display,
471 struct rectangle *rectangle)
473 #ifdef HAVE_CAIRO_EGL
475 return display_create_drm_surface(display, rectangle);
478 return display_create_shm_surface(display, rectangle);
481 static cairo_surface_t *
482 display_create_surface_from_file(struct display *display,
483 const char *filename,
484 struct rectangle *rectangle)
486 #ifdef HAVE_CAIRO_EGL
488 return display_create_drm_surface_from_file(display, filename, rectangle);
491 return display_create_shm_surface_from_file(display, filename, rectangle);
493 static const struct {
494 const char *filename;
495 int hotspot_x, hotspot_y;
496 } pointer_images[] = {
497 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
498 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
499 { DATADIR "/wayland/bottom_side.png", 16, 20 },
500 { DATADIR "/wayland/grabbing.png", 20, 17 },
501 { DATADIR "/wayland/left_ptr.png", 10, 5 },
502 { DATADIR "/wayland/left_side.png", 10, 20 },
503 { DATADIR "/wayland/right_side.png", 30, 19 },
504 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
505 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
506 { DATADIR "/wayland/top_side.png", 18, 8 },
507 { DATADIR "/wayland/xterm.png", 15, 15 },
508 { DATADIR "/wayland/hand1.png", 18, 11 }
512 create_pointer_surfaces(struct display *display)
515 const int width = 32, height = 32;
516 struct rectangle rect;
518 count = ARRAY_LENGTH(pointer_images);
519 display->pointer_surfaces =
520 malloc(count * sizeof *display->pointer_surfaces);
522 rect.height = height;
523 for (i = 0; i < count; i++) {
524 display->pointer_surfaces[i] =
525 display_create_surface_from_file(display,
526 pointer_images[i].filename,
533 display_get_pointer_surface(struct display *display, int pointer,
534 int *width, int *height,
535 int *hotspot_x, int *hotspot_y)
537 cairo_surface_t *surface;
539 surface = display->pointer_surfaces[pointer];
541 *width = cairo_gl_surface_get_width(surface);
542 *height = cairo_gl_surface_get_height(surface);
544 *width = cairo_image_surface_get_width(surface);
545 *height = cairo_image_surface_get_height(surface);
547 *hotspot_x = pointer_images[pointer].hotspot_x;
548 *hotspot_y = pointer_images[pointer].hotspot_y;
550 return cairo_surface_reference(surface);
555 window_attach_surface(struct window *window);
558 free_surface(void *data)
560 struct window *window = data;
562 cairo_surface_destroy(window->pending_surface);
563 window->pending_surface = NULL;
564 if (window->cairo_surface)
565 window_attach_surface(window);
569 window_attach_surface(struct window *window)
571 struct display *display = window->display;
572 struct wl_buffer *buffer;
575 if (window->pending_surface != NULL)
578 window->pending_surface = window->cairo_surface;
579 window->cairo_surface = NULL;
581 buffer = display_get_buffer_for_surface(display,
582 window->pending_surface);
583 if (window->resize_edges & WINDOW_RESIZING_LEFT)
584 x = window->server_allocation.width -
585 window->allocation.width;
589 if (window->resize_edges & WINDOW_RESIZING_TOP)
590 y = window->server_allocation.height -
591 window->allocation.height;
595 window->server_allocation = window->allocation;
596 window->resize_edges = 0;
597 wl_surface_attach(window->surface, buffer, x, y);
598 wl_display_sync_callback(display->display, free_surface, window);
600 if (!window->mapped) {
601 wl_surface_map_toplevel(window->surface);
605 wl_surface_damage(window->surface, 0, 0,
606 window->allocation.width,
607 window->allocation.height);
611 window_flush(struct window *window)
613 if (window->cairo_surface)
614 window_attach_surface(window);
618 window_set_surface(struct window *window, cairo_surface_t *surface)
620 cairo_surface_reference(surface);
622 if (window->cairo_surface != NULL)
623 cairo_surface_destroy(window->cairo_surface);
625 window->cairo_surface = surface;
629 window_create_surface(struct window *window)
631 cairo_surface_t *surface;
633 switch (window->buffer_type) {
634 #ifdef HAVE_CAIRO_EGL
635 case WINDOW_BUFFER_TYPE_DRM:
636 surface = display_create_surface(window->display,
637 &window->allocation);
640 case WINDOW_BUFFER_TYPE_SHM:
641 surface = display_create_shm_surface(window->display,
642 &window->allocation);
649 window_set_surface(window, surface);
650 cairo_surface_destroy(surface);
654 window_draw_decorations(struct window *window)
657 cairo_text_extents_t extents;
658 cairo_pattern_t *outline, *bright, *dim;
659 cairo_surface_t *frame;
660 int width, height, shadow_dx = 3, shadow_dy = 3;
662 window_create_surface(window);
664 width = window->allocation.width;
665 height = window->allocation.height;
667 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
668 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
669 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
671 cr = cairo_create(window->cairo_surface);
673 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
674 cairo_set_source_rgba(cr, 0, 0, 0, 0);
677 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
678 tile_mask(cr, window->display->shadow,
679 shadow_dx, shadow_dy, width, height,
680 window->margin + 10 - shadow_dx,
681 window->margin + 10 - shadow_dy);
683 if (window->keyboard_device)
684 frame = window->display->active_frame;
686 frame = window->display->inactive_frame;
688 tile_source(cr, frame, 0, 0, width, height,
689 window->margin + 10, window->margin + 50);
691 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
692 cairo_set_font_size(cr, 14);
693 cairo_text_extents(cr, window->title, &extents);
694 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
695 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
696 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
697 cairo_set_line_width (cr, 4);
698 if (window->keyboard_device)
699 cairo_set_source_rgb(cr, 0, 0, 0);
701 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
702 cairo_show_text(cr, window->title);
706 cairo_device_flush (window->display->device);
710 display_flush_cairo_device(struct display *display)
712 cairo_device_flush (display->device);
716 window_draw(struct window *window)
718 if (window->fullscreen || !window->decoration)
719 window_create_surface(window);
721 window_draw_decorations(window);
725 window_get_surface(struct window *window)
727 return cairo_surface_reference(window->cairo_surface);
731 get_pointer_location(struct window *window, int32_t x, int32_t y)
733 int vlocation, hlocation, location;
734 const int grip_size = 8;
736 if (!window->decoration)
737 return WINDOW_CLIENT_AREA;
739 if (x < window->margin)
740 hlocation = WINDOW_EXTERIOR;
741 else if (window->margin <= x && x < window->margin + grip_size)
742 hlocation = WINDOW_RESIZING_LEFT;
743 else if (x < window->allocation.width - window->margin - grip_size)
744 hlocation = WINDOW_INTERIOR;
745 else if (x < window->allocation.width - window->margin)
746 hlocation = WINDOW_RESIZING_RIGHT;
748 hlocation = WINDOW_EXTERIOR;
750 if (y < window->margin)
751 vlocation = WINDOW_EXTERIOR;
752 else if (window->margin <= y && y < window->margin + grip_size)
753 vlocation = WINDOW_RESIZING_TOP;
754 else if (y < window->allocation.height - window->margin - grip_size)
755 vlocation = WINDOW_INTERIOR;
756 else if (y < window->allocation.height - window->margin)
757 vlocation = WINDOW_RESIZING_BOTTOM;
759 vlocation = WINDOW_EXTERIOR;
761 location = vlocation | hlocation;
762 if (location & WINDOW_EXTERIOR)
763 location = WINDOW_EXTERIOR;
764 if (location == WINDOW_INTERIOR && y < window->margin + 50)
765 location = WINDOW_TITLEBAR;
766 else if (location == WINDOW_INTERIOR)
767 location = WINDOW_CLIENT_AREA;
773 set_pointer_image(struct input *input, uint32_t time, int pointer)
775 struct display *display = input->display;
776 struct wl_buffer *buffer;
777 cairo_surface_t *surface;
780 location = get_pointer_location(input->pointer_focus,
781 input->sx, input->sy);
783 case WINDOW_RESIZING_TOP:
784 pointer = POINTER_TOP;
786 case WINDOW_RESIZING_BOTTOM:
787 pointer = POINTER_BOTTOM;
789 case WINDOW_RESIZING_LEFT:
790 pointer = POINTER_LEFT;
792 case WINDOW_RESIZING_RIGHT:
793 pointer = POINTER_RIGHT;
795 case WINDOW_RESIZING_TOP_LEFT:
796 pointer = POINTER_TOP_LEFT;
798 case WINDOW_RESIZING_TOP_RIGHT:
799 pointer = POINTER_TOP_RIGHT;
801 case WINDOW_RESIZING_BOTTOM_LEFT:
802 pointer = POINTER_BOTTOM_LEFT;
804 case WINDOW_RESIZING_BOTTOM_RIGHT:
805 pointer = POINTER_BOTTOM_RIGHT;
807 case WINDOW_EXTERIOR:
808 case WINDOW_TITLEBAR:
809 if (input->current_pointer_image == POINTER_DEFAULT)
812 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
813 input->current_pointer_image = POINTER_DEFAULT;
819 if (pointer == input->current_pointer_image)
822 input->current_pointer_image = pointer;
823 surface = display->pointer_surfaces[pointer];
824 buffer = display_get_buffer_for_surface(display, surface);
825 wl_input_device_attach(input->input_device, time, buffer,
826 pointer_images[pointer].hotspot_x,
827 pointer_images[pointer].hotspot_y);
831 window_handle_motion(void *data, struct wl_input_device *input_device,
833 int32_t x, int32_t y, int32_t sx, int32_t sy)
835 struct input *input = data;
836 struct window *window = input->pointer_focus;
837 int location, pointer = POINTER_LEFT_PTR;
844 location = get_pointer_location(window, input->sx, input->sy);
846 if (window->motion_handler)
847 pointer = (*window->motion_handler)(window, input, time,
851 set_pointer_image(input, time, pointer);
855 window_handle_button(void *data,
856 struct wl_input_device *input_device,
857 uint32_t time, uint32_t button, uint32_t state)
859 struct input *input = data;
860 struct window *window = input->pointer_focus;
863 location = get_pointer_location(window, input->sx, input->sy);
865 if (button == BTN_LEFT && state == 1) {
867 case WINDOW_TITLEBAR:
868 wl_shell_move(window->display->shell,
869 window->surface, input_device, time);
871 case WINDOW_RESIZING_TOP:
872 case WINDOW_RESIZING_BOTTOM:
873 case WINDOW_RESIZING_LEFT:
874 case WINDOW_RESIZING_RIGHT:
875 case WINDOW_RESIZING_TOP_LEFT:
876 case WINDOW_RESIZING_TOP_RIGHT:
877 case WINDOW_RESIZING_BOTTOM_LEFT:
878 case WINDOW_RESIZING_BOTTOM_RIGHT:
879 wl_shell_resize(window->display->shell,
880 window->surface, input_device, time,
883 case WINDOW_CLIENT_AREA:
884 if (window->button_handler)
885 (*window->button_handler)(window,
892 if (window->button_handler)
893 (*window->button_handler)(window,
901 window_handle_key(void *data, struct wl_input_device *input_device,
902 uint32_t time, uint32_t key, uint32_t state)
904 struct input *input = data;
905 struct window *window = input->keyboard_focus;
906 struct display *d = window->display;
907 uint32_t code, sym, level;
909 code = key + d->xkb->min_key_code;
910 if (window->keyboard_device != input)
914 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
915 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
918 sym = XkbKeySymEntry(d->xkb, code, level, 0);
921 input->modifiers |= d->xkb->map->modmap[code];
923 input->modifiers &= ~d->xkb->map->modmap[code];
925 if (window->key_handler)
926 (*window->key_handler)(window, key, sym, state,
927 input->modifiers, window->user_data);
931 window_handle_pointer_focus(void *data,
932 struct wl_input_device *input_device,
933 uint32_t time, struct wl_surface *surface,
934 int32_t x, int32_t y, int32_t sx, int32_t sy)
936 struct input *input = data;
937 struct window *window;
941 input->pointer_focus = wl_surface_get_user_data(surface);
942 window = input->pointer_focus;
944 pointer = POINTER_LEFT_PTR;
945 if (window->motion_handler)
946 pointer = (*window->motion_handler)(window,
951 set_pointer_image(input, time, pointer);
953 input->pointer_focus = NULL;
954 input->current_pointer_image = POINTER_UNSET;
959 window_handle_keyboard_focus(void *data,
960 struct wl_input_device *input_device,
962 struct wl_surface *surface,
963 struct wl_array *keys)
965 struct input *input = data;
966 struct window *window = input->keyboard_focus;
967 struct display *d = input->display;
970 window = input->keyboard_focus;
972 window->keyboard_device = NULL;
973 if (window->keyboard_focus_handler)
974 (*window->keyboard_focus_handler)(window, NULL,
979 input->keyboard_focus = wl_surface_get_user_data(surface);
981 input->keyboard_focus = NULL;
983 end = keys->data + keys->size;
984 for (k = keys->data; k < end; k++)
985 input->modifiers |= d->xkb->map->modmap[*k];
987 window = input->keyboard_focus;
989 window->keyboard_device = input;
990 if (window->keyboard_focus_handler)
991 (*window->keyboard_focus_handler)(window,
992 window->keyboard_device,
997 static const struct wl_input_device_listener input_device_listener = {
998 window_handle_motion,
999 window_handle_button,
1001 window_handle_pointer_focus,
1002 window_handle_keyboard_focus,
1006 input_get_position(struct input *input, int32_t *x, int32_t *y)
1012 struct wl_input_device *
1013 input_get_input_device(struct input *input)
1015 return input->input_device;
1019 window_create_drag(struct window *window)
1021 cairo_device_flush (window->display->device);
1023 return wl_shell_create_drag(window->display->shell);
1027 window_move(struct window *window, struct input *input, uint32_t time)
1029 wl_shell_move(window->display->shell,
1030 window->surface, input->input_device, time);
1034 window_activate_drag(struct wl_drag *drag, struct window *window,
1035 struct input *input, uint32_t time)
1037 wl_drag_activate(drag, window->surface, input->input_device, time);
1041 handle_configure(void *data, struct wl_shell *shell,
1042 uint32_t time, uint32_t edges,
1043 struct wl_surface *surface, int32_t width, int32_t height)
1045 struct window *window = wl_surface_get_user_data(surface);
1046 int32_t child_width, child_height;
1048 if (window->resize_handler) {
1049 child_width = width - 20 - window->margin * 2;
1050 child_height = height - 60 - window->margin * 2;
1052 (*window->resize_handler)(window,
1053 child_width, child_height,
1056 window->resize_edges = edges;
1057 window->allocation.width = width;
1058 window->allocation.height = height;
1060 if (window->redraw_handler)
1061 window_schedule_redraw(window);
1065 static const struct wl_shell_listener shell_listener = {
1070 window_get_child_allocation(struct window *window,
1071 struct rectangle *allocation)
1073 if (window->fullscreen && !window->decoration) {
1074 *allocation = window->allocation;
1076 allocation->x = window->margin + 10;
1077 allocation->y = window->margin + 50;
1079 window->allocation.width - 20 - window->margin * 2;
1080 allocation->height =
1081 window->allocation.height - 60 - window->margin * 2;
1086 window_set_child_size(struct window *window, int32_t width, int32_t height)
1088 if (!window->fullscreen) {
1089 window->allocation.width = width + 20 + window->margin * 2;
1090 window->allocation.height = height + 60 + window->margin * 2;
1095 window_copy_image(struct window *window,
1096 struct rectangle *rectangle, EGLImageKHR image)
1098 /* set image as read buffer, copy pixels or something... */
1102 window_copy_surface(struct window *window,
1103 struct rectangle *rectangle,
1104 cairo_surface_t *surface)
1108 cr = cairo_create (window->cairo_surface);
1110 cairo_set_source_surface (cr,
1112 rectangle->x, rectangle->y);
1119 idle_redraw(void *data)
1121 struct window *window = data;
1123 window->redraw_handler(window, window->user_data);
1125 window->redraw_scheduled = 0;
1131 window_schedule_redraw(struct window *window)
1133 if (!window->redraw_scheduled) {
1134 g_idle_add(idle_redraw, window);
1135 window->redraw_scheduled = 1;
1140 window_set_fullscreen(struct window *window, int fullscreen)
1142 window->fullscreen = fullscreen;
1143 if (window->fullscreen) {
1144 window->saved_allocation = window->allocation;
1145 window->allocation = window->display->screen_allocation;
1147 window->allocation = window->saved_allocation;
1152 window_set_decoration(struct window *window, int decoration)
1154 window->decoration = decoration;
1158 window_set_user_data(struct window *window, void *data)
1160 window->user_data = data;
1164 window_get_user_data(struct window *window)
1166 return window->user_data;
1170 window_set_resize_handler(struct window *window,
1171 window_resize_handler_t handler)
1173 window->resize_handler = handler;
1177 window_set_redraw_handler(struct window *window,
1178 window_redraw_handler_t handler)
1180 window->redraw_handler = handler;
1184 window_set_key_handler(struct window *window,
1185 window_key_handler_t handler)
1187 window->key_handler = handler;
1191 window_set_button_handler(struct window *window,
1192 window_button_handler_t handler)
1194 window->button_handler = handler;
1198 window_set_motion_handler(struct window *window,
1199 window_motion_handler_t handler)
1201 window->motion_handler = handler;
1205 window_set_keyboard_focus_handler(struct window *window,
1206 window_keyboard_focus_handler_t handler)
1208 window->keyboard_focus_handler = handler;
1212 window_damage(struct window *window, int32_t x, int32_t y,
1213 int32_t width, int32_t height)
1215 wl_surface_damage(window->surface, x, y, width, height);
1219 window_create(struct display *display, const char *title,
1220 int32_t width, int32_t height)
1222 struct window *window;
1224 window = malloc(sizeof *window);
1228 memset(window, 0, sizeof *window);
1229 window->display = display;
1230 window->title = strdup(title);
1231 window->surface = wl_compositor_create_surface(display->compositor);
1232 window->allocation.x = 0;
1233 window->allocation.y = 0;
1234 window->allocation.width = width;
1235 window->allocation.height = height;
1236 window->saved_allocation = window->allocation;
1237 window->margin = 16;
1238 window->decoration = 1;
1241 window->buffer_type = WINDOW_BUFFER_TYPE_DRM;
1243 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1245 wl_surface_set_user_data(window->surface, window);
1246 wl_list_insert(display->window_list.prev, &window->link);
1252 window_set_buffer_type(struct window *window, enum window_buffer_type type)
1254 window->buffer_type = type;
1258 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
1260 struct display *d = data;
1262 d->device_name = strdup(device);
1265 static void drm_handle_authenticated(void *data, struct wl_drm *drm)
1267 struct display *d = data;
1269 d->authenticated = 1;
1272 static const struct wl_drm_listener drm_listener = {
1274 drm_handle_authenticated
1278 display_handle_geometry(void *data,
1279 struct wl_output *output,
1280 int32_t x, int32_t y, int32_t width, int32_t height)
1282 struct display *display = data;
1284 display->screen_allocation.x = x;
1285 display->screen_allocation.y = y;
1286 display->screen_allocation.width = width;
1287 display->screen_allocation.height = height;
1290 static const struct wl_output_listener output_listener = {
1291 display_handle_geometry,
1295 display_add_input(struct display *d, uint32_t id)
1297 struct input *input;
1299 input = malloc(sizeof *input);
1303 memset(input, 0, sizeof *input);
1305 input->input_device = wl_input_device_create(d->display, id);
1306 input->pointer_focus = NULL;
1307 input->keyboard_focus = NULL;
1308 wl_list_insert(d->input_list.prev, &input->link);
1310 wl_input_device_add_listener(input->input_device,
1311 &input_device_listener, input);
1312 wl_input_device_set_user_data(input->input_device, input);
1316 display_handle_global(struct wl_display *display, uint32_t id,
1317 const char *interface, uint32_t version, void *data)
1319 struct display *d = data;
1320 struct wl_drag_offer *offer;
1322 if (strcmp(interface, "compositor") == 0) {
1323 d->compositor = wl_compositor_create(display, id);
1324 } else if (strcmp(interface, "output") == 0) {
1325 d->output = wl_output_create(display, id);
1326 wl_output_add_listener(d->output, &output_listener, d);
1327 } else if (strcmp(interface, "input_device") == 0) {
1328 display_add_input(d, id);
1329 } else if (strcmp(interface, "shell") == 0) {
1330 d->shell = wl_shell_create(display, id);
1331 wl_shell_add_listener(d->shell, &shell_listener, d);
1332 } else if (strcmp(interface, "drm") == 0) {
1333 d->drm = wl_drm_create(display, id);
1334 wl_drm_add_listener(d->drm, &drm_listener, d);
1335 } else if (strcmp(interface, "shm") == 0) {
1336 d->shm = wl_shm_create(display, id);
1337 } else if (strcmp(interface, "drag_offer") == 0) {
1338 if (d->drag_offer_handler) {
1339 offer = wl_drag_offer_create(display, id);
1340 d->drag_offer_handler(offer, d);
1342 } else if (d->global_handler) {
1343 d->global_handler(d, interface, version);
1348 display_render_frame(struct display *d)
1353 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1354 cr = cairo_create(d->shadow);
1355 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1356 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1357 rounded_rect(cr, 16, 16, 112, 112, radius);
1360 blur_surface(d->shadow, 64);
1363 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1364 cr = cairo_create(d->active_frame);
1365 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1366 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1367 rounded_rect(cr, 16, 16, 112, 112, radius);
1372 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1373 cr = cairo_create(d->inactive_frame);
1374 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1375 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1376 rounded_rect(cr, 16, 16, 112, 112, radius);
1382 init_xkb(struct display *d)
1384 struct xkb_rule_names names;
1386 names.rules = "evdev";
1387 names.model = "pc105";
1388 names.layout = option_xkb_layout;
1389 names.variant = option_xkb_variant;
1390 names.options = option_xkb_options;
1392 d->xkb = xkb_compile_keymap_from_rules(&names);
1394 fprintf(stderr, "Failed to compile keymap\n");
1400 init_drm(struct display *d)
1402 EGLint major, minor;
1406 fd = open(d->device_name, O_RDWR);
1408 fprintf(stderr, "drm open failed: %m\n");
1412 if (drmGetMagic(fd, &magic)) {
1413 fprintf(stderr, "DRI2: failed to get drm magic");
1417 /* Wait for authenticated event */
1418 wl_drm_authenticate(d->drm, magic);
1419 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1420 while (!d->authenticated)
1421 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1423 d->dpy = eglGetDRMDisplayMESA(fd);
1424 if (!eglInitialize(d->dpy, &major, &minor)) {
1425 fprintf(stderr, "failed to initialize display\n");
1429 if (!eglBindAPI(EGL_OPENGL_API)) {
1430 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1434 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1435 if (d->ctx == NULL) {
1436 fprintf(stderr, "failed to create context\n");
1440 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1441 fprintf(stderr, "faile to make context current\n");
1445 #ifdef HAVE_CAIRO_EGL
1446 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1447 if (d->device == NULL) {
1448 fprintf(stderr, "failed to get cairo drm device\n");
1457 display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
1460 GOptionContext *context;
1461 GOptionGroup *xkb_option_group;
1466 context = g_option_context_new(NULL);
1468 g_option_context_add_main_entries(context, option_entries, "Wayland View");
1470 xkb_option_group = g_option_group_new("xkb",
1472 "Show all XKB options",
1474 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1475 g_option_context_add_group (context, xkb_option_group);
1477 if (!g_option_context_parse(context, argc, argv, &error)) {
1478 fprintf(stderr, "option parsing failed: %s\n", error->message);
1483 d = malloc(sizeof *d);
1487 d->display = wl_display_connect(NULL);
1488 if (d->display == NULL) {
1489 fprintf(stderr, "failed to create display: %m\n");
1493 wl_list_init(&d->input_list);
1495 /* Set up listener so we'll catch all events. */
1496 wl_display_add_global_listener(d->display,
1497 display_handle_global, d);
1499 /* Process connection events. */
1500 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1502 if (d->device_name && init_drm(d) < 0)
1505 create_pointer_surfaces(d);
1507 display_render_frame(d);
1509 d->loop = g_main_loop_new(NULL, FALSE);
1510 d->source = wl_glib_source_new(d->display);
1511 g_source_attach(d->source, NULL);
1513 wl_list_init(&d->window_list);
1521 display_get_display(struct display *display)
1523 return display->display;
1526 struct wl_compositor *
1527 display_get_compositor(struct display *display)
1529 return display->compositor;
1533 display_get_egl_display(struct display *d)
1539 display_run(struct display *d)
1541 g_main_loop_run(d->loop);
1545 display_set_drag_offer_handler(struct display *display,
1546 display_drag_offer_handler_t handler)
1548 display->drag_offer_handler = handler;
1552 display_set_global_handler(struct display *display,
1553 display_global_handler_t handler)
1555 display->global_handler = handler;