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;
86 struct display *display;
87 struct wl_surface *surface;
89 struct rectangle allocation, saved_allocation, pending_allocation;
92 int minimum_width, minimum_height;
96 struct input *grab_device;
97 struct input *keyboard_device;
99 enum window_buffer_type buffer_type;
102 cairo_surface_t *cairo_surface, *pending_surface;
104 window_resize_handler_t resize_handler;
105 window_redraw_handler_t redraw_handler;
106 window_key_handler_t key_handler;
107 window_button_handler_t button_handler;
108 window_keyboard_focus_handler_t keyboard_focus_handler;
109 window_motion_handler_t motion_handler;
116 struct display *display;
117 struct wl_input_device *input_device;
118 struct window *pointer_focus;
119 struct window *keyboard_focus;
120 uint32_t current_pointer_image;
122 int32_t x, y, sx, sy;
127 POINTER_DEFAULT = 100,
131 const char *option_xkb_layout = "us";
132 const char *option_xkb_variant = "";
133 const char *option_xkb_options = "";
135 static const GOptionEntry xkb_option_entries[] = {
136 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
137 &option_xkb_layout, "XKB Layout" },
138 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
139 &option_xkb_variant, "XKB Variant" },
140 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
141 &option_xkb_options, "XKB Options" },
146 rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
148 cairo_move_to(cr, x0, y0 + radius);
149 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
150 cairo_line_to(cr, x1 - radius, y0);
151 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
152 cairo_line_to(cr, x1, y1 - radius);
153 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
154 cairo_line_to(cr, x0 + radius, y1);
155 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
156 cairo_close_path(cr);
159 static const cairo_user_data_key_t surface_data_key;
160 struct surface_data {
161 struct wl_buffer *buffer;
166 struct drm_surface_data {
167 struct surface_data data;
174 drm_surface_data_destroy(void *p)
176 struct drm_surface_data *data = p;
178 glDeleteTextures(1, &data->texture);
179 eglDestroyImageKHR(data->dpy, data->image);
180 wl_buffer_destroy(data->data.buffer);
184 display_create_drm_surface(struct display *display,
185 struct rectangle *rectangle)
187 struct drm_surface_data *data;
188 EGLDisplay dpy = display->dpy;
189 cairo_surface_t *surface;
190 struct wl_visual *visual;
191 struct wl_buffer *buffer;
194 EGLint image_attribs[] = {
197 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
198 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
202 data = malloc(sizeof *data);
206 image_attribs[1] = rectangle->width;
207 image_attribs[3] = rectangle->height;
208 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
209 glGenTextures(1, &data->texture);
211 glBindTexture(GL_TEXTURE_2D, data->texture);
212 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
214 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
216 visual = wl_display_get_premultiplied_argb_visual(display->display);
218 wl_drm_create_buffer(display->drm, name, rectangle->width,
219 rectangle->height, stride, visual);
221 surface = cairo_gl_surface_create_for_texture(display->device,
222 CAIRO_CONTENT_COLOR_ALPHA,
227 cairo_surface_set_user_data (surface, &surface_data_key,
228 data, drm_surface_data_destroy);
234 display_create_drm_surface_from_file(struct display *display,
235 const char *filename,
236 struct rectangle *rect)
238 cairo_surface_t *surface;
240 GError *error = NULL;
242 unsigned char *pixels, *p, *end;
244 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
245 rect->width, rect->height,
250 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
251 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
252 gdk_pixbuf_unref(pixbuf);
257 stride = gdk_pixbuf_get_rowstride(pixbuf);
258 pixels = gdk_pixbuf_get_pixels(pixbuf);
260 for (i = 0; i < rect->height; i++) {
261 p = pixels + i * stride;
262 end = p + rect->width * 4;
266 #define MULT(d,c,a,t) \
267 do { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } while (0)
269 MULT(p[0], p[0], p[3], t);
270 MULT(p[1], p[1], p[3], t);
271 MULT(p[2], p[2], p[3], t);
277 surface = display_create_drm_surface(display, rect);
278 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
279 rect->width, rect->height,
280 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
282 gdk_pixbuf_unref(pixbuf);
290 display_get_buffer_for_surface(struct display *display,
291 cairo_surface_t *surface)
293 struct surface_data *data;
295 data = cairo_surface_get_user_data (surface, &surface_data_key);
300 struct shm_surface_data {
301 struct surface_data data;
307 shm_surface_data_destroy(void *p)
309 struct shm_surface_data *data = p;
311 wl_buffer_destroy(data->data.buffer);
312 munmap(data->map, data->length);
316 display_create_shm_surface(struct display *display,
317 struct rectangle *rectangle)
319 struct shm_surface_data *data;
320 cairo_surface_t *surface;
321 struct wl_visual *visual;
322 int stride, alloc, fd;
323 char filename[] = "/tmp/wayland-shm-XXXXXX";
325 data = malloc(sizeof *data);
329 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
331 data->length = stride * rectangle->height;
332 fd = mkstemp(filename);
334 fprintf(stderr, "open %s failed: %m", filename);
337 if (ftruncate(fd, data->length) < 0) {
338 fprintf(stderr, "ftruncate failed: %m");
343 data->map = mmap(NULL, data->length,
344 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
347 if (data->map == MAP_FAILED) {
348 fprintf(stderr, "mmap failed: %m");
353 surface = cairo_image_surface_create_for_data (data->map,
359 cairo_surface_set_user_data (surface, &surface_data_key,
360 data, shm_surface_data_destroy);
362 visual = wl_display_get_premultiplied_argb_visual(display->display);
363 data->data.buffer = wl_shm_create_buffer(display->shm,
375 display_create_shm_surface_from_file(struct display *display,
376 const char *filename,
377 struct rectangle *rect)
379 cairo_surface_t *surface;
381 GError *error = NULL;
383 unsigned char *pixels, *p, *end, *dest_data;
387 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
388 rect->width, rect->height,
393 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
394 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
395 gdk_pixbuf_unref(pixbuf);
399 stride = gdk_pixbuf_get_rowstride(pixbuf);
400 pixels = gdk_pixbuf_get_pixels(pixbuf);
402 surface = display_create_shm_surface(display, rect);
403 dest_data = cairo_image_surface_get_data (surface);
404 dest_stride = cairo_image_surface_get_stride (surface);
406 for (i = 0; i < rect->height; i++) {
407 d = (uint32_t *) (dest_data + i * dest_stride);
408 p = pixels + i * stride;
409 end = p + rect->width * 4;
412 unsigned char a, r, g, b;
414 #define MULT(_d,c,a,t) \
415 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
422 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
426 gdk_pixbuf_unref(pixbuf);
432 display_create_surface(struct display *display,
433 struct rectangle *rectangle)
436 display_create_drm_surface(display, rectangle);
438 display_create_shm_surface(display, rectangle);
443 display_create_surface_from_file(struct display *display,
444 const char *filename,
445 struct rectangle *rectangle)
448 display_create_drm_surface_from_file(display, filename, rectangle);
450 display_create_shm_surface_from_file(display, filename, rectangle);
454 static const struct {
455 const char *filename;
456 int hotspot_x, hotspot_y;
457 } pointer_images[] = {
458 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
459 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
460 { DATADIR "/wayland/bottom_side.png", 16, 20 },
461 { DATADIR "/wayland/grabbing.png", 20, 17 },
462 { DATADIR "/wayland/left_ptr.png", 10, 5 },
463 { DATADIR "/wayland/left_side.png", 10, 20 },
464 { DATADIR "/wayland/right_side.png", 30, 19 },
465 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
466 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
467 { DATADIR "/wayland/top_side.png", 18, 8 },
468 { DATADIR "/wayland/xterm.png", 15, 15 },
469 { DATADIR "/wayland/hand1.png", 18, 11 }
473 create_pointer_surfaces(struct display *display)
476 const int width = 32, height = 32;
477 struct rectangle rect;
479 count = ARRAY_LENGTH(pointer_images);
480 display->pointer_surfaces =
481 malloc(count * sizeof *display->pointer_surfaces);
483 rect.height = height;
484 for (i = 0; i < count; i++) {
485 display->pointer_surfaces[i] =
486 display_create_surface_from_file(display,
487 pointer_images[i].filename,
494 display_get_pointer_surface(struct display *display, int pointer,
495 int *width, int *height,
496 int *hotspot_x, int *hotspot_y)
498 cairo_surface_t *surface;
500 surface = display->pointer_surfaces[pointer];
501 *width = cairo_gl_surface_get_width(surface);
502 *height = cairo_gl_surface_get_height(surface);
503 *hotspot_x = pointer_images[pointer].hotspot_x;
504 *hotspot_y = pointer_images[pointer].hotspot_y;
506 return cairo_surface_reference(surface);
511 window_attach_surface(struct window *window);
514 free_surface(void *data)
516 struct window *window = data;
518 cairo_surface_destroy(window->pending_surface);
519 window->pending_surface = NULL;
520 if (window->cairo_surface)
521 window_attach_surface(window);
525 window_attach_surface(struct window *window)
527 struct display *display = window->display;
528 struct wl_buffer *buffer;
530 if (window->pending_surface != NULL)
533 window->pending_surface = window->cairo_surface;
534 window->cairo_surface = NULL;
536 buffer = display_get_buffer_for_surface(display,
537 window->pending_surface);
538 wl_surface_attach(window->surface, buffer);
540 wl_surface_map(window->surface,
541 window->allocation.x,
542 window->allocation.y,
543 window->allocation.width,
544 window->allocation.height);
546 wl_display_sync_callback(display->display, free_surface, window);
550 window_flush(struct window *window)
552 if (window->cairo_surface)
553 window_attach_surface(window);
557 window_create_surface(struct window *window, struct rectangle *allocation)
559 switch (window->buffer_type) {
561 case WINDOW_BUFFER_TYPE_DRM:
562 window->cairo_surface =
563 display_create_surface(window->display, allocation);
566 case WINDOW_BUFFER_TYPE_SHM:
567 window->cairo_surface =
568 display_create_shm_surface(window->display, allocation);
574 window_draw_decorations(struct window *window)
577 cairo_text_extents_t extents;
578 cairo_pattern_t *outline, *bright, *dim;
579 cairo_surface_t *frame;
580 int width, height, shadow_dx = 3, shadow_dy = 3;
582 window_create_surface(window, &window->allocation);
584 width = window->allocation.width;
585 height = window->allocation.height;
587 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
588 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
589 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
591 cr = cairo_create(window->cairo_surface);
593 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
594 cairo_set_source_rgba(cr, 0, 0, 0, 0);
597 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
598 tile_mask(cr, window->display->shadow,
599 shadow_dx, shadow_dy, width, height,
600 window->margin + 10 - shadow_dx,
601 window->margin + 10 - shadow_dy);
603 if (window->keyboard_device)
604 frame = window->display->active_frame;
606 frame = window->display->inactive_frame;
608 tile_source(cr, frame, 0, 0, width, height,
609 window->margin + 10, window->margin + 50);
611 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
612 cairo_set_font_size(cr, 14);
613 cairo_text_extents(cr, window->title, &extents);
614 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
615 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
616 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
617 cairo_set_line_width (cr, 4);
618 if (window->keyboard_device)
619 cairo_set_source_rgb(cr, 0, 0, 0);
621 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
622 cairo_show_text(cr, window->title);
626 cairo_device_flush (window->display->device);
630 display_flush_cairo_device(struct display *display)
632 cairo_device_flush (display->device);
636 window_draw_fullscreen(struct window *window)
638 window_create_surface(window, &window->allocation);
642 window_draw(struct window *window)
644 if (window->cairo_surface != NULL)
645 cairo_surface_destroy(window->cairo_surface);
647 if (window->fullscreen || !window->decoration)
648 window_draw_fullscreen(window);
650 window_draw_decorations(window);
654 window_get_surface(struct window *window)
656 return window->cairo_surface;
659 enum window_location {
661 WINDOW_RESIZING_TOP = 1,
662 WINDOW_RESIZING_BOTTOM = 2,
663 WINDOW_RESIZING_LEFT = 4,
664 WINDOW_RESIZING_TOP_LEFT = 5,
665 WINDOW_RESIZING_BOTTOM_LEFT = 6,
666 WINDOW_RESIZING_RIGHT = 8,
667 WINDOW_RESIZING_TOP_RIGHT = 9,
668 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
669 WINDOW_RESIZING_MASK = 15,
670 WINDOW_EXTERIOR = 16,
671 WINDOW_TITLEBAR = 17,
672 WINDOW_CLIENT_AREA = 18,
676 get_pointer_location(struct window *window, int32_t x, int32_t y)
678 int vlocation, hlocation, location;
679 const int grip_size = 8;
681 if (x < window->margin)
682 hlocation = WINDOW_EXTERIOR;
683 else if (window->margin <= x && x < window->margin + grip_size)
684 hlocation = WINDOW_RESIZING_LEFT;
685 else if (x < window->allocation.width - window->margin - grip_size)
686 hlocation = WINDOW_INTERIOR;
687 else if (x < window->allocation.width - window->margin)
688 hlocation = WINDOW_RESIZING_RIGHT;
690 hlocation = WINDOW_EXTERIOR;
692 if (y < window->margin)
693 vlocation = WINDOW_EXTERIOR;
694 else if (window->margin <= y && y < window->margin + grip_size)
695 vlocation = WINDOW_RESIZING_TOP;
696 else if (y < window->allocation.height - window->margin - grip_size)
697 vlocation = WINDOW_INTERIOR;
698 else if (y < window->allocation.height - window->margin)
699 vlocation = WINDOW_RESIZING_BOTTOM;
701 vlocation = WINDOW_EXTERIOR;
703 location = vlocation | hlocation;
704 if (location & WINDOW_EXTERIOR)
705 location = WINDOW_EXTERIOR;
706 if (location == WINDOW_INTERIOR && y < window->margin + 50)
707 location = WINDOW_TITLEBAR;
708 else if (location == WINDOW_INTERIOR)
709 location = WINDOW_CLIENT_AREA;
715 set_pointer_image(struct input *input, uint32_t time, int pointer)
717 struct display *display = input->display;
718 struct wl_buffer *buffer;
719 cairo_surface_t *surface;
722 location = get_pointer_location(input->pointer_focus,
723 input->sx, input->sy);
725 case WINDOW_RESIZING_TOP:
726 pointer = POINTER_TOP;
728 case WINDOW_RESIZING_BOTTOM:
729 pointer = POINTER_BOTTOM;
731 case WINDOW_RESIZING_LEFT:
732 pointer = POINTER_LEFT;
734 case WINDOW_RESIZING_RIGHT:
735 pointer = POINTER_RIGHT;
737 case WINDOW_RESIZING_TOP_LEFT:
738 pointer = POINTER_TOP_LEFT;
740 case WINDOW_RESIZING_TOP_RIGHT:
741 pointer = POINTER_TOP_RIGHT;
743 case WINDOW_RESIZING_BOTTOM_LEFT:
744 pointer = POINTER_BOTTOM_LEFT;
746 case WINDOW_RESIZING_BOTTOM_RIGHT:
747 pointer = POINTER_BOTTOM_RIGHT;
749 case WINDOW_EXTERIOR:
750 case WINDOW_TITLEBAR:
751 if (input->current_pointer_image == POINTER_DEFAULT)
754 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
755 input->current_pointer_image = POINTER_DEFAULT;
761 if (pointer == input->current_pointer_image)
764 input->current_pointer_image = pointer;
765 surface = display->pointer_surfaces[pointer];
766 buffer = display_get_buffer_for_surface(display, surface);
767 wl_input_device_attach(input->input_device, time, buffer,
768 pointer_images[pointer].hotspot_x,
769 pointer_images[pointer].hotspot_y);
773 window_handle_motion(void *data, struct wl_input_device *input_device,
775 int32_t x, int32_t y, int32_t sx, int32_t sy)
777 struct input *input = data;
778 struct window *window = input->pointer_focus;
779 int location, pointer = POINTER_LEFT_PTR;
786 location = get_pointer_location(window, input->sx, input->sy);
788 if (window->motion_handler)
789 pointer = (*window->motion_handler)(window, input, time,
793 set_pointer_image(input, time, pointer);
797 window_handle_button(void *data,
798 struct wl_input_device *input_device,
799 uint32_t time, uint32_t button, uint32_t state)
801 struct input *input = data;
802 struct window *window = input->pointer_focus;
805 location = get_pointer_location(window, input->sx, input->sy);
807 if (button == BTN_LEFT && state == 1) {
809 case WINDOW_TITLEBAR:
810 wl_shell_move(window->display->shell,
811 window->surface, input_device, time);
813 case WINDOW_RESIZING_TOP:
814 case WINDOW_RESIZING_BOTTOM:
815 case WINDOW_RESIZING_LEFT:
816 case WINDOW_RESIZING_RIGHT:
817 case WINDOW_RESIZING_TOP_LEFT:
818 case WINDOW_RESIZING_TOP_RIGHT:
819 case WINDOW_RESIZING_BOTTOM_LEFT:
820 case WINDOW_RESIZING_BOTTOM_RIGHT:
821 wl_shell_resize(window->display->shell,
822 window->surface, input_device, time,
825 case WINDOW_CLIENT_AREA:
826 if (window->button_handler)
827 (*window->button_handler)(window,
834 if (window->button_handler)
835 (*window->button_handler)(window,
843 window_handle_key(void *data, struct wl_input_device *input_device,
844 uint32_t time, uint32_t key, uint32_t state)
846 struct input *input = data;
847 struct window *window = input->keyboard_focus;
848 struct display *d = window->display;
849 uint32_t code, sym, level;
851 code = key + d->xkb->min_key_code;
852 if (window->keyboard_device != input)
856 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
857 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
860 sym = XkbKeySymEntry(d->xkb, code, level, 0);
863 input->modifiers |= d->xkb->map->modmap[code];
865 input->modifiers &= ~d->xkb->map->modmap[code];
867 if (window->key_handler)
868 (*window->key_handler)(window, key, sym, state,
869 input->modifiers, window->user_data);
873 window_handle_pointer_focus(void *data,
874 struct wl_input_device *input_device,
875 uint32_t time, struct wl_surface *surface,
876 int32_t x, int32_t y, int32_t sx, int32_t sy)
878 struct input *input = data;
879 struct window *window;
883 input->pointer_focus = wl_surface_get_user_data(surface);
884 window = input->pointer_focus;
886 pointer = POINTER_LEFT_PTR;
887 if (window->motion_handler)
888 pointer = (*window->motion_handler)(window,
893 set_pointer_image(input, time, pointer);
895 input->pointer_focus = NULL;
896 input->current_pointer_image = POINTER_UNSET;
901 window_handle_keyboard_focus(void *data,
902 struct wl_input_device *input_device,
904 struct wl_surface *surface,
905 struct wl_array *keys)
907 struct input *input = data;
908 struct window *window = input->keyboard_focus;
909 struct display *d = input->display;
912 window = input->keyboard_focus;
914 window->keyboard_device = NULL;
915 if (window->keyboard_focus_handler)
916 (*window->keyboard_focus_handler)(window, NULL,
921 input->keyboard_focus = wl_surface_get_user_data(surface);
923 input->keyboard_focus = NULL;
925 end = keys->data + keys->size;
926 for (k = keys->data; k < end; k++)
927 input->modifiers |= d->xkb->map->modmap[*k];
929 window = input->keyboard_focus;
931 window->keyboard_device = input;
932 if (window->keyboard_focus_handler)
933 (*window->keyboard_focus_handler)(window,
934 window->keyboard_device,
939 static const struct wl_input_device_listener input_device_listener = {
940 window_handle_motion,
941 window_handle_button,
943 window_handle_pointer_focus,
944 window_handle_keyboard_focus,
948 input_get_position(struct input *input, int32_t *x, int32_t *y)
954 struct wl_input_device *
955 input_get_input_device(struct input *input)
957 return input->input_device;
961 window_start_drag(struct window *window, struct input *input, uint32_t time,
962 const struct wl_drag_listener *listener, void *data)
964 struct wl_drag *drag;
966 cairo_device_flush (window->display->device);
968 drag = wl_shell_create_drag(window->display->shell);
969 wl_drag_offer(drag, "text/plain");
970 wl_drag_offer(drag, "text/html");
971 wl_drag_activate(drag, window->surface, input->input_device, time);
972 wl_drag_add_listener(drag, listener, data);
978 handle_configure(void *data, struct wl_shell *shell,
979 uint32_t time, uint32_t edges,
980 struct wl_surface *surface,
981 int32_t x, int32_t y, int32_t width, int32_t height)
983 struct window *window = wl_surface_get_user_data(surface);
985 window->resize_edges = edges;
986 window->pending_allocation.x = x;
987 window->pending_allocation.y = y;
988 window->pending_allocation.width = width;
989 window->pending_allocation.height = height;
994 if (window->resize_handler)
995 (*window->resize_handler)(window,
997 else if (window->redraw_handler)
998 window_schedule_redraw(window);
1001 static const struct wl_shell_listener shell_listener = {
1006 window_get_child_rectangle(struct window *window,
1007 struct rectangle *rectangle)
1009 if (window->fullscreen && !window->decoration) {
1010 *rectangle = window->allocation;
1012 rectangle->x = window->margin + 10;
1013 rectangle->y = window->margin + 50;
1014 rectangle->width = window->allocation.width - 20 - window->margin * 2;
1015 rectangle->height = window->allocation.height - 60 - window->margin * 2;
1020 window_set_child_size(struct window *window,
1021 struct rectangle *rectangle)
1023 int32_t width, height;
1025 if (!window->fullscreen) {
1026 width = rectangle->width + 20 + window->margin * 2;
1027 height = rectangle->height + 60 + window->margin * 2;
1029 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1030 window->allocation.x +=
1031 window->allocation.width - width;
1032 if (window->resize_edges & WINDOW_RESIZING_TOP)
1033 window->allocation.y +=
1034 window->allocation.height - height;
1036 window->allocation.width = width;
1037 window->allocation.height = height;
1042 window_copy_image(struct window *window,
1043 struct rectangle *rectangle, EGLImageKHR image)
1045 /* set image as read buffer, copy pixels or something... */
1049 window_copy_surface(struct window *window,
1050 struct rectangle *rectangle,
1051 cairo_surface_t *surface)
1055 cr = cairo_create (window->cairo_surface);
1057 cairo_set_source_surface (cr,
1059 rectangle->x, rectangle->y);
1066 idle_redraw(void *data)
1068 struct window *window = data;
1070 if (window->resize_edges)
1071 window->allocation = window->pending_allocation;
1073 window->redraw_handler(window, window->user_data);
1075 window->redraw_scheduled = 0;
1076 window->resize_edges = 0;
1082 window_schedule_redraw(struct window *window)
1084 if (!window->redraw_scheduled) {
1085 g_idle_add(idle_redraw, window);
1086 window->redraw_scheduled = 1;
1091 window_set_fullscreen(struct window *window, int fullscreen)
1093 window->fullscreen = fullscreen;
1094 if (window->fullscreen) {
1095 window->saved_allocation = window->allocation;
1096 window->allocation = window->display->screen_allocation;
1098 window->allocation = window->saved_allocation;
1103 window_set_decoration(struct window *window, int decoration)
1105 window->decoration = decoration;
1109 window_set_user_data(struct window *window, void *data)
1111 window->user_data = data;
1115 window_get_user_data(struct window *window)
1117 return window->user_data;
1121 window_set_resize_handler(struct window *window,
1122 window_resize_handler_t handler)
1124 window->resize_handler = handler;
1128 window_set_redraw_handler(struct window *window,
1129 window_redraw_handler_t handler)
1131 window->redraw_handler = handler;
1135 window_set_key_handler(struct window *window,
1136 window_key_handler_t handler)
1138 window->key_handler = handler;
1142 window_set_button_handler(struct window *window,
1143 window_button_handler_t handler)
1145 window->button_handler = handler;
1149 window_set_motion_handler(struct window *window,
1150 window_motion_handler_t handler)
1152 window->motion_handler = handler;
1156 window_set_keyboard_focus_handler(struct window *window,
1157 window_keyboard_focus_handler_t handler)
1159 window->keyboard_focus_handler = handler;
1163 window_move(struct window *window, int32_t x, int32_t y)
1165 window->allocation.x = x;
1166 window->allocation.y = y;
1168 wl_surface_map(window->surface,
1169 window->allocation.x - window->margin,
1170 window->allocation.y - window->margin,
1171 window->allocation.width,
1172 window->allocation.height);
1176 window_damage(struct window *window, int32_t x, int32_t y,
1177 int32_t width, int32_t height)
1179 wl_surface_damage(window->surface, x, y, width, height);
1183 window_create(struct display *display, const char *title,
1184 int32_t x, int32_t y, int32_t width, int32_t height)
1186 struct window *window;
1188 window = malloc(sizeof *window);
1192 memset(window, 0, sizeof *window);
1193 window->display = display;
1194 window->title = strdup(title);
1195 window->surface = wl_compositor_create_surface(display->compositor);
1196 window->allocation.x = x;
1197 window->allocation.y = y;
1198 window->allocation.width = width;
1199 window->allocation.height = height;
1200 window->saved_allocation = window->allocation;
1201 window->margin = 16;
1202 window->decoration = 1;
1204 #ifdef HAVE_CAIRO_GL
1205 window->buffer_type = WINDOW_BUFFER_TYPE_DRM;
1207 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1210 wl_surface_set_user_data(window->surface, window);
1211 wl_list_insert(display->window_list.prev, &window->link);
1217 window_set_buffer_type(struct window *window, enum window_buffer_type type)
1219 window->buffer_type = type;
1223 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
1225 struct display *d = data;
1227 d->device_name = strdup(device);
1230 static void drm_handle_authenticated(void *data, struct wl_drm *drm)
1232 struct display *d = data;
1234 d->authenticated = 1;
1237 static const struct wl_drm_listener drm_listener = {
1239 drm_handle_authenticated
1243 display_handle_geometry(void *data,
1244 struct wl_output *output,
1245 int32_t width, int32_t height)
1247 struct display *display = data;
1249 display->screen_allocation.x = 0;
1250 display->screen_allocation.y = 0;
1251 display->screen_allocation.width = width;
1252 display->screen_allocation.height = height;
1255 static const struct wl_output_listener output_listener = {
1256 display_handle_geometry,
1260 display_add_input(struct display *d, uint32_t id)
1262 struct input *input;
1264 input = malloc(sizeof *input);
1268 memset(input, 0, sizeof *input);
1270 input->input_device = wl_input_device_create(d->display, id);
1271 input->pointer_focus = NULL;
1272 input->keyboard_focus = NULL;
1273 wl_list_insert(d->input_list.prev, &input->link);
1275 wl_input_device_add_listener(input->input_device,
1276 &input_device_listener, input);
1277 wl_input_device_set_user_data(input->input_device, input);
1281 display_handle_global(struct wl_display *display, uint32_t id,
1282 const char *interface, uint32_t version, void *data)
1284 struct display *d = data;
1285 struct wl_drag_offer *offer;
1287 if (strcmp(interface, "compositor") == 0) {
1288 d->compositor = wl_compositor_create(display, id);
1289 } else if (strcmp(interface, "output") == 0) {
1290 d->output = wl_output_create(display, id);
1291 wl_output_add_listener(d->output, &output_listener, d);
1292 } else if (strcmp(interface, "input_device") == 0) {
1293 display_add_input(d, id);
1294 } else if (strcmp(interface, "shell") == 0) {
1295 d->shell = wl_shell_create(display, id);
1296 wl_shell_add_listener(d->shell, &shell_listener, d);
1297 } else if (strcmp(interface, "drm") == 0) {
1298 d->drm = wl_drm_create(display, id);
1299 wl_drm_add_listener(d->drm, &drm_listener, d);
1300 } else if (strcmp(interface, "shm") == 0) {
1301 d->shm = wl_shm_create(display, id);
1302 } else if (strcmp(interface, "drag_offer") == 0) {
1303 offer = wl_drag_offer_create(display, id);
1304 d->drag_offer_handler(offer, d);
1308 static const char socket_name[] = "\0wayland";
1311 display_render_frame(struct display *d)
1316 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1317 cr = cairo_create(d->shadow);
1318 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1319 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1320 rounded_rect(cr, 16, 16, 112, 112, radius);
1323 blur_surface(d->shadow, 64);
1326 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1327 cr = cairo_create(d->active_frame);
1328 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1329 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1330 rounded_rect(cr, 16, 16, 112, 112, radius);
1335 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1336 cr = cairo_create(d->inactive_frame);
1337 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1338 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1339 rounded_rect(cr, 16, 16, 112, 112, radius);
1345 init_xkb(struct display *d)
1347 struct xkb_rule_names names;
1349 names.rules = "evdev";
1350 names.model = "pc105";
1351 names.layout = option_xkb_layout;
1352 names.variant = option_xkb_variant;
1353 names.options = option_xkb_options;
1355 d->xkb = xkb_compile_keymap_from_rules(&names);
1357 fprintf(stderr, "Failed to compile keymap\n");
1363 display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
1366 EGLint major, minor;
1368 GOptionContext *context;
1369 GOptionGroup *xkb_option_group;
1375 context = g_option_context_new(NULL);
1377 g_option_context_add_main_entries(context, option_entries, "Wayland View");
1379 xkb_option_group = g_option_group_new("xkb",
1381 "Show all XKB options",
1383 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1384 g_option_context_add_group (context, xkb_option_group);
1386 if (!g_option_context_parse(context, argc, argv, &error)) {
1387 fprintf(stderr, "option parsing failed: %s\n", error->message);
1392 d = malloc(sizeof *d);
1396 d->display = wl_display_create(socket_name, sizeof socket_name);
1397 if (d->display == NULL) {
1398 fprintf(stderr, "failed to create display: %m\n");
1402 wl_list_init(&d->input_list);
1404 /* Set up listener so we'll catch all events. */
1405 wl_display_add_global_listener(d->display,
1406 display_handle_global, d);
1408 /* Process connection events. */
1409 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1411 fd = open(d->device_name, O_RDWR);
1413 fprintf(stderr, "drm open failed: %m\n");
1417 if (drmGetMagic(fd, &magic)) {
1418 fprintf(stderr, "DRI2: failed to get drm magic");
1422 /* Wait for authenticated event */
1423 wl_drm_authenticate(d->drm, magic);
1424 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1425 while (!d->authenticated)
1426 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1428 d->dpy = eglGetDRMDisplayMESA(fd);
1429 if (!eglInitialize(d->dpy, &major, &minor)) {
1430 fprintf(stderr, "failed to initialize display\n");
1434 eglBindAPI(EGL_OPENGL_API);
1436 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1437 if (d->ctx == NULL) {
1438 fprintf(stderr, "failed to create context\n");
1442 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1443 fprintf(stderr, "faile to make context current\n");
1447 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1448 if (d->device == NULL) {
1449 fprintf(stderr, "failed to get cairo drm device\n");
1453 create_pointer_surfaces(d);
1455 display_render_frame(d);
1457 d->loop = g_main_loop_new(NULL, FALSE);
1458 d->source = wl_glib_source_new(d->display);
1459 g_source_attach(d->source, NULL);
1461 wl_list_init(&d->window_list);
1469 display_get_display(struct display *display)
1471 return display->display;
1474 struct wl_compositor *
1475 display_get_compositor(struct display *display)
1477 return display->compositor;
1481 display_get_egl_display(struct display *d)
1487 display_run(struct display *d)
1489 g_main_loop_run(d->loop);
1493 display_set_drag_offer_handler(struct display *display,
1494 display_drag_offer_handler_t handler)
1496 display->drag_offer_handler = handler;