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_global_handler_t global_handler;
86 struct display *display;
87 struct wl_surface *surface;
89 struct rectangle allocation, saved_allocation, server_allocation;
92 int minimum_width, minimum_height;
96 struct input *grab_device;
97 struct input *keyboard_device;
99 enum window_buffer_type buffer_type;
103 cairo_surface_t *cairo_surface, *pending_surface;
105 window_resize_handler_t resize_handler;
106 window_redraw_handler_t redraw_handler;
107 window_key_handler_t key_handler;
108 window_button_handler_t button_handler;
109 window_keyboard_focus_handler_t keyboard_focus_handler;
110 window_motion_handler_t motion_handler;
117 struct display *display;
118 struct wl_input_device *input_device;
119 struct window *pointer_focus;
120 struct window *keyboard_focus;
121 uint32_t current_pointer_image;
123 int32_t x, y, sx, sy;
128 POINTER_DEFAULT = 100,
132 enum window_location {
134 WINDOW_RESIZING_TOP = 1,
135 WINDOW_RESIZING_BOTTOM = 2,
136 WINDOW_RESIZING_LEFT = 4,
137 WINDOW_RESIZING_TOP_LEFT = 5,
138 WINDOW_RESIZING_BOTTOM_LEFT = 6,
139 WINDOW_RESIZING_RIGHT = 8,
140 WINDOW_RESIZING_TOP_RIGHT = 9,
141 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
142 WINDOW_RESIZING_MASK = 15,
143 WINDOW_EXTERIOR = 16,
144 WINDOW_TITLEBAR = 17,
145 WINDOW_CLIENT_AREA = 18,
148 const char *option_xkb_layout = "us";
149 const char *option_xkb_variant = "";
150 const char *option_xkb_options = "";
152 static const GOptionEntry xkb_option_entries[] = {
153 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
154 &option_xkb_layout, "XKB Layout" },
155 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
156 &option_xkb_variant, "XKB Variant" },
157 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
158 &option_xkb_options, "XKB Options" },
163 rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
165 cairo_move_to(cr, x0, y0 + radius);
166 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
167 cairo_line_to(cr, x1 - radius, y0);
168 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
169 cairo_line_to(cr, x1, y1 - radius);
170 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
171 cairo_line_to(cr, x0 + radius, y1);
172 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
173 cairo_close_path(cr);
176 static const cairo_user_data_key_t surface_data_key;
177 struct surface_data {
178 struct wl_buffer *buffer;
181 #define MULT(_d,c,a,t) \
182 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
184 #ifdef HAVE_CAIRO_EGL
186 struct drm_surface_data {
187 struct surface_data data;
190 struct display *display;
194 drm_surface_data_destroy(void *p)
196 struct drm_surface_data *data = p;
197 struct display *d = data->display;
199 cairo_device_acquire(d->device);
200 glDeleteTextures(1, &data->texture);
201 cairo_device_release(d->device);
203 eglDestroyImageKHR(d->dpy, data->image);
204 wl_buffer_destroy(data->data.buffer);
208 display_get_image_for_drm_surface(struct display *display,
209 cairo_surface_t *surface)
211 struct drm_surface_data *data;
213 data = cairo_surface_get_user_data (surface, &surface_data_key);
218 static cairo_surface_t *
219 display_create_drm_surface(struct display *display,
220 struct rectangle *rectangle)
222 struct drm_surface_data *data;
223 EGLDisplay dpy = display->dpy;
224 cairo_surface_t *surface;
225 struct wl_visual *visual;
228 EGLint image_attribs[] = {
231 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
232 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
236 data = malloc(sizeof *data);
240 data->display = display;
242 image_attribs[1] = rectangle->width;
243 image_attribs[3] = rectangle->height;
244 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
246 cairo_device_acquire(display->device);
247 glGenTextures(1, &data->texture);
248 glBindTexture(GL_TEXTURE_2D, data->texture);
249 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
250 cairo_device_release(display->device);
252 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
254 visual = wl_display_get_premultiplied_argb_visual(display->display);
256 wl_drm_create_buffer(display->drm, name, rectangle->width,
257 rectangle->height, stride, visual);
259 surface = cairo_gl_surface_create_for_texture(display->device,
260 CAIRO_CONTENT_COLOR_ALPHA,
265 cairo_surface_set_user_data (surface, &surface_data_key,
266 data, drm_surface_data_destroy);
271 static cairo_surface_t *
272 display_create_drm_surface_from_file(struct display *display,
273 const char *filename,
274 struct rectangle *rect)
276 cairo_surface_t *surface;
278 GError *error = NULL;
280 unsigned char *pixels, *p, *end;
281 struct drm_surface_data *data;
283 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
284 rect->width, rect->height,
289 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
290 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
291 gdk_pixbuf_unref(pixbuf);
296 stride = gdk_pixbuf_get_rowstride(pixbuf);
297 pixels = gdk_pixbuf_get_pixels(pixbuf);
299 for (i = 0; i < rect->height; i++) {
300 p = pixels + i * stride;
301 end = p + rect->width * 4;
305 MULT(p[0], p[0], p[3], t);
306 MULT(p[1], p[1], p[3], t);
307 MULT(p[2], p[2], p[3], t);
313 surface = display_create_drm_surface(display, rect);
314 data = cairo_surface_get_user_data(surface, &surface_data_key);
316 cairo_device_acquire(display->device);
317 glBindTexture(GL_TEXTURE_2D, data->texture);
318 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
319 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
320 cairo_device_release(display->device);
322 gdk_pixbuf_unref(pixbuf);
330 display_get_buffer_for_surface(struct display *display,
331 cairo_surface_t *surface)
333 struct surface_data *data;
335 data = cairo_surface_get_user_data (surface, &surface_data_key);
340 struct shm_surface_data {
341 struct surface_data data;
347 shm_surface_data_destroy(void *p)
349 struct shm_surface_data *data = p;
351 wl_buffer_destroy(data->data.buffer);
352 munmap(data->map, data->length);
355 static cairo_surface_t *
356 display_create_shm_surface(struct display *display,
357 struct rectangle *rectangle)
359 struct shm_surface_data *data;
360 cairo_surface_t *surface;
361 struct wl_visual *visual;
363 char filename[] = "/tmp/wayland-shm-XXXXXX";
365 data = malloc(sizeof *data);
369 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
371 data->length = stride * rectangle->height;
372 fd = mkstemp(filename);
374 fprintf(stderr, "open %s failed: %m", filename);
377 if (ftruncate(fd, data->length) < 0) {
378 fprintf(stderr, "ftruncate failed: %m");
383 data->map = mmap(NULL, data->length,
384 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
387 if (data->map == MAP_FAILED) {
388 fprintf(stderr, "mmap failed: %m");
393 surface = cairo_image_surface_create_for_data (data->map,
399 cairo_surface_set_user_data (surface, &surface_data_key,
400 data, shm_surface_data_destroy);
402 visual = wl_display_get_premultiplied_argb_visual(display->display);
403 data->data.buffer = wl_shm_create_buffer(display->shm,
414 static cairo_surface_t *
415 display_create_shm_surface_from_file(struct display *display,
416 const char *filename,
417 struct rectangle *rect)
419 cairo_surface_t *surface;
421 GError *error = NULL;
423 unsigned char *pixels, *p, *end, *dest_data;
427 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
428 rect->width, rect->height,
433 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
434 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
435 gdk_pixbuf_unref(pixbuf);
439 stride = gdk_pixbuf_get_rowstride(pixbuf);
440 pixels = gdk_pixbuf_get_pixels(pixbuf);
442 surface = display_create_shm_surface(display, rect);
443 dest_data = cairo_image_surface_get_data (surface);
444 dest_stride = cairo_image_surface_get_stride (surface);
446 for (i = 0; i < rect->height; i++) {
447 d = (uint32_t *) (dest_data + i * dest_stride);
448 p = pixels + i * stride;
449 end = p + rect->width * 4;
452 unsigned char a, r, g, b;
459 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
463 gdk_pixbuf_unref(pixbuf);
469 display_create_surface(struct display *display,
470 struct rectangle *rectangle)
472 #ifdef HAVE_CAIRO_EGL
474 return display_create_drm_surface(display, rectangle);
477 return display_create_shm_surface(display, rectangle);
480 static cairo_surface_t *
481 display_create_surface_from_file(struct display *display,
482 const char *filename,
483 struct rectangle *rectangle)
485 #ifdef HAVE_CAIRO_EGL
487 return display_create_drm_surface_from_file(display, filename, rectangle);
490 return display_create_shm_surface_from_file(display, filename, rectangle);
492 static const struct {
493 const char *filename;
494 int hotspot_x, hotspot_y;
495 } pointer_images[] = {
496 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
497 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
498 { DATADIR "/wayland/bottom_side.png", 16, 20 },
499 { DATADIR "/wayland/grabbing.png", 20, 17 },
500 { DATADIR "/wayland/left_ptr.png", 10, 5 },
501 { DATADIR "/wayland/left_side.png", 10, 20 },
502 { DATADIR "/wayland/right_side.png", 30, 19 },
503 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
504 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
505 { DATADIR "/wayland/top_side.png", 18, 8 },
506 { DATADIR "/wayland/xterm.png", 15, 15 },
507 { DATADIR "/wayland/hand1.png", 18, 11 }
511 create_pointer_surfaces(struct display *display)
514 const int width = 32, height = 32;
515 struct rectangle rect;
517 count = ARRAY_LENGTH(pointer_images);
518 display->pointer_surfaces =
519 malloc(count * sizeof *display->pointer_surfaces);
521 rect.height = height;
522 for (i = 0; i < count; i++) {
523 display->pointer_surfaces[i] =
524 display_create_surface_from_file(display,
525 pointer_images[i].filename,
532 display_get_pointer_surface(struct display *display, int pointer,
533 int *width, int *height,
534 int *hotspot_x, int *hotspot_y)
536 cairo_surface_t *surface;
538 surface = display->pointer_surfaces[pointer];
540 *width = cairo_gl_surface_get_width(surface);
541 *height = cairo_gl_surface_get_height(surface);
543 *width = cairo_image_surface_get_width(surface);
544 *height = cairo_image_surface_get_height(surface);
546 *hotspot_x = pointer_images[pointer].hotspot_x;
547 *hotspot_y = pointer_images[pointer].hotspot_y;
549 return cairo_surface_reference(surface);
554 window_attach_surface(struct window *window);
557 free_surface(void *data)
559 struct window *window = data;
561 cairo_surface_destroy(window->pending_surface);
562 window->pending_surface = NULL;
563 if (window->cairo_surface)
564 window_attach_surface(window);
568 window_attach_surface(struct window *window)
570 struct display *display = window->display;
571 struct wl_buffer *buffer;
574 if (window->pending_surface != NULL)
577 window->pending_surface = window->cairo_surface;
578 window->cairo_surface = NULL;
580 buffer = display_get_buffer_for_surface(display,
581 window->pending_surface);
582 if (window->resize_edges & WINDOW_RESIZING_LEFT)
583 x = window->server_allocation.width -
584 window->allocation.width;
588 if (window->resize_edges & WINDOW_RESIZING_TOP)
589 y = window->server_allocation.height -
590 window->allocation.height;
594 window->server_allocation = window->allocation;
595 window->resize_edges = 0;
596 wl_surface_attach(window->surface, buffer, x, y);
597 wl_display_sync_callback(display->display, free_surface, window);
599 if (!window->mapped) {
600 wl_surface_map_toplevel(window->surface);
604 wl_surface_damage(window->surface, 0, 0,
605 window->allocation.width,
606 window->allocation.height);
610 window_flush(struct window *window)
612 if (window->cairo_surface)
613 window_attach_surface(window);
617 window_set_surface(struct window *window, cairo_surface_t *surface)
619 cairo_surface_reference(surface);
621 if (window->cairo_surface != NULL)
622 cairo_surface_destroy(window->cairo_surface);
624 window->cairo_surface = surface;
628 window_create_surface(struct window *window)
630 cairo_surface_t *surface;
632 switch (window->buffer_type) {
633 #ifdef HAVE_CAIRO_EGL
634 case WINDOW_BUFFER_TYPE_DRM:
635 surface = display_create_surface(window->display,
636 &window->allocation);
639 case WINDOW_BUFFER_TYPE_SHM:
640 surface = display_create_shm_surface(window->display,
641 &window->allocation);
648 window_set_surface(window, surface);
649 cairo_surface_destroy(surface);
653 window_draw_decorations(struct window *window)
656 cairo_text_extents_t extents;
657 cairo_pattern_t *outline, *bright, *dim;
658 cairo_surface_t *frame;
659 int width, height, shadow_dx = 3, shadow_dy = 3;
661 window_create_surface(window);
663 width = window->allocation.width;
664 height = window->allocation.height;
666 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
667 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
668 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
670 cr = cairo_create(window->cairo_surface);
672 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
673 cairo_set_source_rgba(cr, 0, 0, 0, 0);
676 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
677 tile_mask(cr, window->display->shadow,
678 shadow_dx, shadow_dy, width, height,
679 window->margin + 10 - shadow_dx,
680 window->margin + 10 - shadow_dy);
682 if (window->keyboard_device)
683 frame = window->display->active_frame;
685 frame = window->display->inactive_frame;
687 tile_source(cr, frame, 0, 0, width, height,
688 window->margin + 10, window->margin + 50);
690 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
691 cairo_set_font_size(cr, 14);
692 cairo_text_extents(cr, window->title, &extents);
693 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
694 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
695 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
696 cairo_set_line_width (cr, 4);
697 if (window->keyboard_device)
698 cairo_set_source_rgb(cr, 0, 0, 0);
700 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
701 cairo_show_text(cr, window->title);
705 cairo_device_flush (window->display->device);
709 display_flush_cairo_device(struct display *display)
711 cairo_device_flush (display->device);
715 window_draw(struct window *window)
717 if (window->fullscreen || !window->decoration)
718 window_create_surface(window);
720 window_draw_decorations(window);
724 window_get_surface(struct window *window)
726 return cairo_surface_reference(window->cairo_surface);
730 get_pointer_location(struct window *window, int32_t x, int32_t y)
732 int vlocation, hlocation, location;
733 const int grip_size = 8;
735 if (!window->decoration)
736 return WINDOW_CLIENT_AREA;
738 if (x < window->margin)
739 hlocation = WINDOW_EXTERIOR;
740 else if (window->margin <= x && x < window->margin + grip_size)
741 hlocation = WINDOW_RESIZING_LEFT;
742 else if (x < window->allocation.width - window->margin - grip_size)
743 hlocation = WINDOW_INTERIOR;
744 else if (x < window->allocation.width - window->margin)
745 hlocation = WINDOW_RESIZING_RIGHT;
747 hlocation = WINDOW_EXTERIOR;
749 if (y < window->margin)
750 vlocation = WINDOW_EXTERIOR;
751 else if (window->margin <= y && y < window->margin + grip_size)
752 vlocation = WINDOW_RESIZING_TOP;
753 else if (y < window->allocation.height - window->margin - grip_size)
754 vlocation = WINDOW_INTERIOR;
755 else if (y < window->allocation.height - window->margin)
756 vlocation = WINDOW_RESIZING_BOTTOM;
758 vlocation = WINDOW_EXTERIOR;
760 location = vlocation | hlocation;
761 if (location & WINDOW_EXTERIOR)
762 location = WINDOW_EXTERIOR;
763 if (location == WINDOW_INTERIOR && y < window->margin + 50)
764 location = WINDOW_TITLEBAR;
765 else if (location == WINDOW_INTERIOR)
766 location = WINDOW_CLIENT_AREA;
772 set_pointer_image(struct input *input, uint32_t time, int pointer)
774 struct display *display = input->display;
775 struct wl_buffer *buffer;
776 cairo_surface_t *surface;
779 location = get_pointer_location(input->pointer_focus,
780 input->sx, input->sy);
782 case WINDOW_RESIZING_TOP:
783 pointer = POINTER_TOP;
785 case WINDOW_RESIZING_BOTTOM:
786 pointer = POINTER_BOTTOM;
788 case WINDOW_RESIZING_LEFT:
789 pointer = POINTER_LEFT;
791 case WINDOW_RESIZING_RIGHT:
792 pointer = POINTER_RIGHT;
794 case WINDOW_RESIZING_TOP_LEFT:
795 pointer = POINTER_TOP_LEFT;
797 case WINDOW_RESIZING_TOP_RIGHT:
798 pointer = POINTER_TOP_RIGHT;
800 case WINDOW_RESIZING_BOTTOM_LEFT:
801 pointer = POINTER_BOTTOM_LEFT;
803 case WINDOW_RESIZING_BOTTOM_RIGHT:
804 pointer = POINTER_BOTTOM_RIGHT;
806 case WINDOW_EXTERIOR:
807 case WINDOW_TITLEBAR:
808 if (input->current_pointer_image == POINTER_DEFAULT)
811 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
812 input->current_pointer_image = POINTER_DEFAULT;
818 if (pointer == input->current_pointer_image)
821 input->current_pointer_image = pointer;
822 surface = display->pointer_surfaces[pointer];
823 buffer = display_get_buffer_for_surface(display, surface);
824 wl_input_device_attach(input->input_device, time, buffer,
825 pointer_images[pointer].hotspot_x,
826 pointer_images[pointer].hotspot_y);
830 window_handle_motion(void *data, struct wl_input_device *input_device,
832 int32_t x, int32_t y, int32_t sx, int32_t sy)
834 struct input *input = data;
835 struct window *window = input->pointer_focus;
836 int location, pointer = POINTER_LEFT_PTR;
843 location = get_pointer_location(window, input->sx, input->sy);
845 if (window->motion_handler)
846 pointer = (*window->motion_handler)(window, input, time,
850 set_pointer_image(input, time, pointer);
854 window_handle_button(void *data,
855 struct wl_input_device *input_device,
856 uint32_t time, uint32_t button, uint32_t state)
858 struct input *input = data;
859 struct window *window = input->pointer_focus;
862 location = get_pointer_location(window, input->sx, input->sy);
864 if (button == BTN_LEFT && state == 1) {
866 case WINDOW_TITLEBAR:
867 wl_shell_move(window->display->shell,
868 window->surface, input_device, time);
870 case WINDOW_RESIZING_TOP:
871 case WINDOW_RESIZING_BOTTOM:
872 case WINDOW_RESIZING_LEFT:
873 case WINDOW_RESIZING_RIGHT:
874 case WINDOW_RESIZING_TOP_LEFT:
875 case WINDOW_RESIZING_TOP_RIGHT:
876 case WINDOW_RESIZING_BOTTOM_LEFT:
877 case WINDOW_RESIZING_BOTTOM_RIGHT:
878 wl_shell_resize(window->display->shell,
879 window->surface, input_device, time,
882 case WINDOW_CLIENT_AREA:
883 if (window->button_handler)
884 (*window->button_handler)(window,
891 if (window->button_handler)
892 (*window->button_handler)(window,
900 window_handle_key(void *data, struct wl_input_device *input_device,
901 uint32_t time, uint32_t key, uint32_t state)
903 struct input *input = data;
904 struct window *window = input->keyboard_focus;
905 struct display *d = window->display;
906 uint32_t code, sym, level;
908 code = key + d->xkb->min_key_code;
909 if (window->keyboard_device != input)
913 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
914 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
917 sym = XkbKeySymEntry(d->xkb, code, level, 0);
920 input->modifiers |= d->xkb->map->modmap[code];
922 input->modifiers &= ~d->xkb->map->modmap[code];
924 if (window->key_handler)
925 (*window->key_handler)(window, key, sym, state,
926 input->modifiers, window->user_data);
930 window_handle_pointer_focus(void *data,
931 struct wl_input_device *input_device,
932 uint32_t time, struct wl_surface *surface,
933 int32_t x, int32_t y, int32_t sx, int32_t sy)
935 struct input *input = data;
936 struct window *window;
940 input->pointer_focus = wl_surface_get_user_data(surface);
941 window = input->pointer_focus;
943 pointer = POINTER_LEFT_PTR;
944 if (window->motion_handler)
945 pointer = (*window->motion_handler)(window,
950 set_pointer_image(input, time, pointer);
952 input->pointer_focus = NULL;
953 input->current_pointer_image = POINTER_UNSET;
958 window_handle_keyboard_focus(void *data,
959 struct wl_input_device *input_device,
961 struct wl_surface *surface,
962 struct wl_array *keys)
964 struct input *input = data;
965 struct window *window = input->keyboard_focus;
966 struct display *d = input->display;
969 window = input->keyboard_focus;
971 window->keyboard_device = NULL;
972 if (window->keyboard_focus_handler)
973 (*window->keyboard_focus_handler)(window, NULL,
978 input->keyboard_focus = wl_surface_get_user_data(surface);
980 input->keyboard_focus = NULL;
982 end = keys->data + keys->size;
983 for (k = keys->data; k < end; k++)
984 input->modifiers |= d->xkb->map->modmap[*k];
986 window = input->keyboard_focus;
988 window->keyboard_device = input;
989 if (window->keyboard_focus_handler)
990 (*window->keyboard_focus_handler)(window,
991 window->keyboard_device,
996 static const struct wl_input_device_listener input_device_listener = {
997 window_handle_motion,
998 window_handle_button,
1000 window_handle_pointer_focus,
1001 window_handle_keyboard_focus,
1005 input_get_position(struct input *input, int32_t *x, int32_t *y)
1011 struct wl_input_device *
1012 input_get_input_device(struct input *input)
1014 return input->input_device;
1018 window_create_drag(struct window *window)
1020 cairo_device_flush (window->display->device);
1022 return wl_shell_create_drag(window->display->shell);
1026 window_move(struct window *window, struct input *input, uint32_t time)
1028 wl_shell_move(window->display->shell,
1029 window->surface, input->input_device, time);
1033 window_activate_drag(struct wl_drag *drag, struct window *window,
1034 struct input *input, uint32_t time)
1036 wl_drag_activate(drag, window->surface, input->input_device, time);
1040 handle_configure(void *data, struct wl_shell *shell,
1041 uint32_t time, uint32_t edges,
1042 struct wl_surface *surface, int32_t width, int32_t height)
1044 struct window *window = wl_surface_get_user_data(surface);
1045 int32_t child_width, child_height;
1047 if (window->resize_handler) {
1048 child_width = width - 20 - window->margin * 2;
1049 child_height = height - 60 - window->margin * 2;
1051 (*window->resize_handler)(window,
1052 child_width, child_height,
1055 window->resize_edges = edges;
1056 window->allocation.width = width;
1057 window->allocation.height = height;
1059 if (window->redraw_handler)
1060 window_schedule_redraw(window);
1064 static const struct wl_shell_listener shell_listener = {
1069 window_get_child_allocation(struct window *window,
1070 struct rectangle *allocation)
1072 if (window->fullscreen && !window->decoration) {
1073 *allocation = window->allocation;
1075 allocation->x = window->margin + 10;
1076 allocation->y = window->margin + 50;
1078 window->allocation.width - 20 - window->margin * 2;
1079 allocation->height =
1080 window->allocation.height - 60 - window->margin * 2;
1085 window_set_child_size(struct window *window, int32_t width, int32_t height)
1087 if (!window->fullscreen) {
1088 window->allocation.width = width + 20 + window->margin * 2;
1089 window->allocation.height = height + 60 + window->margin * 2;
1094 window_copy_image(struct window *window,
1095 struct rectangle *rectangle, EGLImageKHR image)
1097 /* set image as read buffer, copy pixels or something... */
1101 window_copy_surface(struct window *window,
1102 struct rectangle *rectangle,
1103 cairo_surface_t *surface)
1107 cr = cairo_create (window->cairo_surface);
1109 cairo_set_source_surface (cr,
1111 rectangle->x, rectangle->y);
1118 idle_redraw(void *data)
1120 struct window *window = data;
1122 window->redraw_handler(window, window->user_data);
1124 window->redraw_scheduled = 0;
1130 window_schedule_redraw(struct window *window)
1132 if (!window->redraw_scheduled) {
1133 g_idle_add(idle_redraw, window);
1134 window->redraw_scheduled = 1;
1139 window_set_fullscreen(struct window *window, int fullscreen)
1141 window->fullscreen = fullscreen;
1142 if (window->fullscreen) {
1143 window->saved_allocation = window->allocation;
1144 window->allocation = window->display->screen_allocation;
1146 window->allocation = window->saved_allocation;
1151 window_set_decoration(struct window *window, int decoration)
1153 window->decoration = decoration;
1157 window_set_user_data(struct window *window, void *data)
1159 window->user_data = data;
1163 window_get_user_data(struct window *window)
1165 return window->user_data;
1169 window_set_resize_handler(struct window *window,
1170 window_resize_handler_t handler)
1172 window->resize_handler = handler;
1176 window_set_redraw_handler(struct window *window,
1177 window_redraw_handler_t handler)
1179 window->redraw_handler = handler;
1183 window_set_key_handler(struct window *window,
1184 window_key_handler_t handler)
1186 window->key_handler = handler;
1190 window_set_button_handler(struct window *window,
1191 window_button_handler_t handler)
1193 window->button_handler = handler;
1197 window_set_motion_handler(struct window *window,
1198 window_motion_handler_t handler)
1200 window->motion_handler = handler;
1204 window_set_keyboard_focus_handler(struct window *window,
1205 window_keyboard_focus_handler_t handler)
1207 window->keyboard_focus_handler = handler;
1211 window_damage(struct window *window, int32_t x, int32_t y,
1212 int32_t width, int32_t height)
1214 wl_surface_damage(window->surface, x, y, width, height);
1218 window_create(struct display *display, const char *title,
1219 int32_t width, int32_t height)
1221 struct window *window;
1223 window = malloc(sizeof *window);
1227 memset(window, 0, sizeof *window);
1228 window->display = display;
1229 window->title = strdup(title);
1230 window->surface = wl_compositor_create_surface(display->compositor);
1231 window->allocation.x = 0;
1232 window->allocation.y = 0;
1233 window->allocation.width = width;
1234 window->allocation.height = height;
1235 window->saved_allocation = window->allocation;
1236 window->margin = 16;
1237 window->decoration = 1;
1240 window->buffer_type = WINDOW_BUFFER_TYPE_DRM;
1242 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1244 wl_surface_set_user_data(window->surface, window);
1245 wl_list_insert(display->window_list.prev, &window->link);
1251 window_set_buffer_type(struct window *window, enum window_buffer_type type)
1253 window->buffer_type = type;
1257 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
1259 struct display *d = data;
1261 d->device_name = strdup(device);
1264 static void drm_handle_authenticated(void *data, struct wl_drm *drm)
1266 struct display *d = data;
1268 d->authenticated = 1;
1271 static const struct wl_drm_listener drm_listener = {
1273 drm_handle_authenticated
1277 display_handle_geometry(void *data,
1278 struct wl_output *output,
1279 int32_t x, int32_t y, int32_t width, int32_t height)
1281 struct display *display = data;
1283 display->screen_allocation.x = x;
1284 display->screen_allocation.y = y;
1285 display->screen_allocation.width = width;
1286 display->screen_allocation.height = height;
1289 static const struct wl_output_listener output_listener = {
1290 display_handle_geometry,
1294 display_add_input(struct display *d, uint32_t id)
1296 struct input *input;
1298 input = malloc(sizeof *input);
1302 memset(input, 0, sizeof *input);
1304 input->input_device = wl_input_device_create(d->display, id);
1305 input->pointer_focus = NULL;
1306 input->keyboard_focus = NULL;
1307 wl_list_insert(d->input_list.prev, &input->link);
1309 wl_input_device_add_listener(input->input_device,
1310 &input_device_listener, input);
1311 wl_input_device_set_user_data(input->input_device, input);
1315 display_handle_global(struct wl_display *display, uint32_t id,
1316 const char *interface, uint32_t version, void *data)
1318 struct display *d = data;
1320 if (strcmp(interface, "compositor") == 0) {
1321 d->compositor = wl_compositor_create(display, id);
1322 } else if (strcmp(interface, "output") == 0) {
1323 d->output = wl_output_create(display, id);
1324 wl_output_add_listener(d->output, &output_listener, d);
1325 } else if (strcmp(interface, "input_device") == 0) {
1326 display_add_input(d, id);
1327 } else if (strcmp(interface, "shell") == 0) {
1328 d->shell = wl_shell_create(display, id);
1329 wl_shell_add_listener(d->shell, &shell_listener, d);
1330 } else if (strcmp(interface, "drm") == 0) {
1331 d->drm = wl_drm_create(display, id);
1332 wl_drm_add_listener(d->drm, &drm_listener, d);
1333 } else if (strcmp(interface, "shm") == 0) {
1334 d->shm = wl_shm_create(display, id);
1335 } else if (d->global_handler) {
1336 d->global_handler(d, interface, id, version);
1341 display_render_frame(struct display *d)
1346 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1347 cr = cairo_create(d->shadow);
1348 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1349 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1350 rounded_rect(cr, 16, 16, 112, 112, radius);
1353 blur_surface(d->shadow, 64);
1356 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1357 cr = cairo_create(d->active_frame);
1358 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1359 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1360 rounded_rect(cr, 16, 16, 112, 112, radius);
1365 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1366 cr = cairo_create(d->inactive_frame);
1367 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1368 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1369 rounded_rect(cr, 16, 16, 112, 112, radius);
1375 init_xkb(struct display *d)
1377 struct xkb_rule_names names;
1379 names.rules = "evdev";
1380 names.model = "pc105";
1381 names.layout = option_xkb_layout;
1382 names.variant = option_xkb_variant;
1383 names.options = option_xkb_options;
1385 d->xkb = xkb_compile_keymap_from_rules(&names);
1387 fprintf(stderr, "Failed to compile keymap\n");
1393 init_drm(struct display *d)
1395 EGLint major, minor;
1399 fd = open(d->device_name, O_RDWR);
1401 fprintf(stderr, "drm open failed: %m\n");
1405 if (drmGetMagic(fd, &magic)) {
1406 fprintf(stderr, "DRI2: failed to get drm magic");
1410 /* Wait for authenticated event */
1411 wl_drm_authenticate(d->drm, magic);
1412 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1413 while (!d->authenticated)
1414 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1416 d->dpy = eglGetDRMDisplayMESA(fd);
1417 if (!eglInitialize(d->dpy, &major, &minor)) {
1418 fprintf(stderr, "failed to initialize display\n");
1422 if (!eglBindAPI(EGL_OPENGL_API)) {
1423 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1427 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1428 if (d->ctx == NULL) {
1429 fprintf(stderr, "failed to create context\n");
1433 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1434 fprintf(stderr, "faile to make context current\n");
1438 #ifdef HAVE_CAIRO_EGL
1439 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1440 if (d->device == NULL) {
1441 fprintf(stderr, "failed to get cairo drm device\n");
1450 display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
1453 GOptionContext *context;
1454 GOptionGroup *xkb_option_group;
1459 context = g_option_context_new(NULL);
1461 g_option_context_add_main_entries(context, option_entries, "Wayland View");
1463 xkb_option_group = g_option_group_new("xkb",
1465 "Show all XKB options",
1467 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1468 g_option_context_add_group (context, xkb_option_group);
1470 if (!g_option_context_parse(context, argc, argv, &error)) {
1471 fprintf(stderr, "option parsing failed: %s\n", error->message);
1476 d = malloc(sizeof *d);
1480 d->display = wl_display_connect(NULL);
1481 if (d->display == NULL) {
1482 fprintf(stderr, "failed to create display: %m\n");
1486 wl_list_init(&d->input_list);
1488 /* Set up listener so we'll catch all events. */
1489 wl_display_add_global_listener(d->display,
1490 display_handle_global, d);
1492 /* Process connection events. */
1493 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1495 if (d->device_name && init_drm(d) < 0)
1498 create_pointer_surfaces(d);
1500 display_render_frame(d);
1502 d->loop = g_main_loop_new(NULL, FALSE);
1503 d->source = wl_glib_source_new(d->display);
1504 g_source_attach(d->source, NULL);
1506 wl_list_init(&d->window_list);
1514 display_get_display(struct display *display)
1516 return display->display;
1519 struct wl_compositor *
1520 display_get_compositor(struct display *display)
1522 return display->compositor;
1526 display_get_egl_display(struct display *d)
1532 display_run(struct display *d)
1534 g_main_loop_run(d->loop);
1538 display_set_global_handler(struct display *display,
1539 display_global_handler_t handler)
1541 display->global_handler = handler;