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
33 #include <glib-object.h>
34 #include <gdk-pixbuf/gdk-pixbuf.h>
37 #define EGL_EGLEXT_PROTOTYPES 1
38 #define GL_GLEXT_PROTOTYPES 1
41 #include <EGL/eglext.h>
44 #include <X11/extensions/XKBcommon.h>
46 #include <linux/input.h>
47 #include "wayland-util.h"
48 #include "wayland-client.h"
49 #include "wayland-glib.h"
50 #include "cairo-util.h"
55 struct wl_display *display;
56 struct wl_compositor *compositor;
57 struct wl_shell *shell;
59 struct wl_output *output;
60 struct rectangle screen_allocation;
64 cairo_device_t *device;
68 struct wl_list window_list;
69 struct wl_list input_list;
71 cairo_surface_t *active_frame, *inactive_frame, *shadow;
73 cairo_surface_t **pointer_surfaces;
77 struct display *display;
78 struct wl_surface *surface;
80 struct rectangle allocation, saved_allocation, pending_allocation;
83 int minimum_width, minimum_height;
87 struct input *grab_device;
88 struct input *keyboard_device;
92 cairo_surface_t *cairo_surface, *pending_surface;
94 window_resize_handler_t resize_handler;
95 window_redraw_handler_t redraw_handler;
96 window_key_handler_t key_handler;
97 window_button_handler_t button_handler;
98 window_keyboard_focus_handler_t keyboard_focus_handler;
99 window_acknowledge_handler_t acknowledge_handler;
100 window_frame_handler_t frame_handler;
101 window_motion_handler_t motion_handler;
108 struct display *display;
109 struct wl_input_device *input_device;
110 struct wl_drag *drag;
111 struct window *pointer_focus;
112 struct window *keyboard_focus;
114 int32_t x, y, sx, sy;
119 rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
121 cairo_move_to(cr, x0, y0 + radius);
122 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
123 cairo_line_to(cr, x1 - radius, y0);
124 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
125 cairo_line_to(cr, x1, y1 - radius);
126 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
127 cairo_line_to(cr, x0 + radius, y1);
128 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
129 cairo_close_path(cr);
133 texture_from_png(const char *filename, int width, int height)
136 GError *error = NULL;
138 unsigned char *pixels, *p, *end;
140 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
146 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
147 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
148 gdk_pixbuf_unref(pixbuf);
153 stride = gdk_pixbuf_get_rowstride(pixbuf);
154 pixels = gdk_pixbuf_get_pixels(pixbuf);
156 for (i = 0; i < height; i++) {
157 p = pixels + i * stride;
162 #define MULT(d,c,a,t) \
163 do { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } while (0)
165 MULT(p[0], p[0], p[3], t);
166 MULT(p[1], p[1], p[3], t);
167 MULT(p[2], p[2], p[3], t);
173 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
174 width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
176 gdk_pixbuf_unref(pixbuf);
181 static const struct {
182 const char *filename;
183 int hotspot_x, hotspot_y;
184 } pointer_images[] = {
185 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
186 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
187 { DATADIR "/wayland/bottom_side.png", 16, 20 },
188 { DATADIR "/wayland/grabbing.png", 20, 17 },
189 { DATADIR "/wayland/left_ptr.png", 10, 5 },
190 { DATADIR "/wayland/left_side.png", 10, 20 },
191 { DATADIR "/wayland/right_side.png", 30, 19 },
192 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
193 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
194 { DATADIR "/wayland/top_side.png", 18, 8 },
195 { DATADIR "/wayland/xterm.png", 15, 15 },
196 { DATADIR "/wayland/hand1.png", 18, 11 }
200 create_pointer_surfaces(struct display *display)
203 const int width = 32, height = 32;
204 struct rectangle rect;
206 count = ARRAY_LENGTH(pointer_images);
207 display->pointer_surfaces =
208 malloc(count * sizeof *display->pointer_surfaces);
210 rect.height = height;
211 for (i = 0; i < count; i++) {
212 display->pointer_surfaces[i] =
213 display_create_surface(display, &rect);
214 texture_from_png(pointer_images[i].filename, width, height);
219 static const cairo_user_data_key_t surface_data_key;
220 struct surface_data {
224 struct wl_buffer *buffer;
228 surface_data_destroy(void *p)
230 struct surface_data *data = p;
232 glDeleteTextures(1, &data->texture);
233 eglDestroyImageKHR(data->dpy, data->image);
235 wl_buffer_destroy(data->buffer);
239 display_create_surface(struct display *display,
240 struct rectangle *rectangle)
242 struct surface_data *data;
243 EGLDisplay dpy = display->dpy;
244 cairo_surface_t *surface;
246 EGLint image_attribs[] = {
249 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
250 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
254 data = malloc(sizeof *data);
255 image_attribs[1] = rectangle->width;
256 image_attribs[3] = rectangle->height;
257 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
258 glGenTextures(1, &data->texture);
260 glBindTexture(GL_TEXTURE_2D, data->texture);
261 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
264 surface = cairo_gl_surface_create_for_texture(display->device,
265 CAIRO_CONTENT_COLOR_ALPHA,
270 cairo_surface_set_user_data (surface, &surface_data_key,
271 data, surface_data_destroy);
277 display_get_buffer_for_surface(struct display *display,
278 cairo_surface_t *surface)
280 struct surface_data *data;
281 struct wl_visual *visual;
282 struct wl_buffer *buffer;
286 data = cairo_surface_get_user_data (surface, &surface_data_key);
290 width = cairo_gl_surface_get_width (surface);
291 height = cairo_gl_surface_get_height (surface);
293 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
295 visual = wl_display_get_premultiplied_argb_visual(display->display);
296 buffer = wl_drm_create_buffer(display->drm,
297 name, width, height, stride, visual);
298 data->buffer = buffer;
304 display_get_pointer_surface(struct display *display, int pointer,
305 int *width, int *height,
306 int *hotspot_x, int *hotspot_y)
308 cairo_surface_t *surface;
310 surface = display->pointer_surfaces[pointer];
311 *width = cairo_gl_surface_get_width(surface);
312 *height = cairo_gl_surface_get_height(surface);
313 *hotspot_x = pointer_images[pointer].hotspot_x;
314 *hotspot_y = pointer_images[pointer].hotspot_y;
316 return cairo_surface_reference(surface);
320 window_attach_surface(struct window *window)
322 struct display *display = window->display;
323 struct wl_buffer *buffer;
325 if (window->pending_surface != NULL)
328 window->pending_surface = window->cairo_surface;
329 window->cairo_surface = NULL;
331 buffer = display_get_buffer_for_surface(display,
332 window->pending_surface);
333 wl_surface_attach(window->surface, buffer);
335 wl_surface_map(window->surface,
336 window->allocation.x,
337 window->allocation.y,
338 window->allocation.width,
339 window->allocation.height);
341 wl_compositor_commit(window->display->compositor, 0);
345 window_commit(struct window *window, uint32_t key)
347 if (window->cairo_surface) {
348 window_attach_surface(window);
350 wl_compositor_commit(window->display->compositor, key);
355 window_draw_decorations(struct window *window)
358 cairo_text_extents_t extents;
359 cairo_pattern_t *outline, *bright, *dim;
360 cairo_surface_t *frame;
361 int width, height, shadow_dx = 3, shadow_dy = 3;
363 window->cairo_surface =
364 display_create_surface(window->display, &window->allocation);
365 width = window->allocation.width;
366 height = window->allocation.height;
368 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
369 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
370 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
372 cr = cairo_create(window->cairo_surface);
374 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
375 cairo_set_source_rgba(cr, 0, 0, 0, 0);
378 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
379 tile_mask(cr, window->display->shadow,
380 shadow_dx, shadow_dy, width, height,
381 window->margin + 10 - shadow_dx,
382 window->margin + 10 - shadow_dy);
384 if (window->keyboard_device)
385 frame = window->display->active_frame;
387 frame = window->display->inactive_frame;
389 tile_source(cr, frame, 0, 0, width, height,
390 window->margin + 10, window->margin + 50);
392 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
393 cairo_set_font_size(cr, 14);
394 cairo_text_extents(cr, window->title, &extents);
395 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
396 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
397 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
398 cairo_set_line_width (cr, 4);
399 if (window->keyboard_device)
400 cairo_set_source_rgb(cr, 0, 0, 0);
402 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
403 cairo_show_text(cr, window->title);
407 cairo_device_flush (window->display->device);
411 display_flush_cairo_device(struct display *display)
413 cairo_device_flush (display->device);
417 window_draw_fullscreen(struct window *window)
419 window->cairo_surface =
420 display_create_surface(window->display, &window->allocation);
424 window_draw(struct window *window)
426 if (window->cairo_surface != NULL)
427 cairo_surface_destroy(window->cairo_surface);
429 if (window->fullscreen || !window->decoration)
430 window_draw_fullscreen(window);
432 window_draw_decorations(window);
436 window_get_surface(struct window *window)
438 return window->cairo_surface;
441 enum window_location {
443 WINDOW_RESIZING_TOP = 1,
444 WINDOW_RESIZING_BOTTOM = 2,
445 WINDOW_RESIZING_LEFT = 4,
446 WINDOW_RESIZING_TOP_LEFT = 5,
447 WINDOW_RESIZING_BOTTOM_LEFT = 6,
448 WINDOW_RESIZING_RIGHT = 8,
449 WINDOW_RESIZING_TOP_RIGHT = 9,
450 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
451 WINDOW_RESIZING_MASK = 15,
452 WINDOW_EXTERIOR = 16,
453 WINDOW_TITLEBAR = 17,
454 WINDOW_CLIENT_AREA = 18,
458 get_pointer_location(struct window *window, int32_t x, int32_t y)
460 int vlocation, hlocation, location;
461 const int grip_size = 8;
463 if (x < window->margin)
464 hlocation = WINDOW_EXTERIOR;
465 else if (window->margin <= x && x < window->margin + grip_size)
466 hlocation = WINDOW_RESIZING_LEFT;
467 else if (x < window->allocation.width - window->margin - grip_size)
468 hlocation = WINDOW_INTERIOR;
469 else if (x < window->allocation.width - window->margin)
470 hlocation = WINDOW_RESIZING_RIGHT;
472 hlocation = WINDOW_EXTERIOR;
474 if (y < window->margin)
475 vlocation = WINDOW_EXTERIOR;
476 else if (window->margin <= y && y < window->margin + grip_size)
477 vlocation = WINDOW_RESIZING_TOP;
478 else if (y < window->allocation.height - window->margin - grip_size)
479 vlocation = WINDOW_INTERIOR;
480 else if (y < window->allocation.height - window->margin)
481 vlocation = WINDOW_RESIZING_BOTTOM;
483 vlocation = WINDOW_EXTERIOR;
485 location = vlocation | hlocation;
486 if (location & WINDOW_EXTERIOR)
487 location = WINDOW_EXTERIOR;
488 if (location == WINDOW_INTERIOR && y < window->margin + 50)
489 location = WINDOW_TITLEBAR;
490 else if (location == WINDOW_INTERIOR)
491 location = WINDOW_CLIENT_AREA;
497 set_pointer_image(struct input *input, int pointer)
499 struct display *display = input->display;
500 struct wl_buffer *buffer;
501 cairo_surface_t *surface;
504 location = get_pointer_location(input->pointer_focus,
505 input->sx, input->sy);
507 case WINDOW_RESIZING_TOP:
508 pointer = POINTER_TOP;
510 case WINDOW_RESIZING_BOTTOM:
511 pointer = POINTER_BOTTOM;
513 case WINDOW_RESIZING_LEFT:
514 pointer = POINTER_LEFT;
516 case WINDOW_RESIZING_RIGHT:
517 pointer = POINTER_RIGHT;
519 case WINDOW_RESIZING_TOP_LEFT:
520 pointer = POINTER_TOP_LEFT;
522 case WINDOW_RESIZING_TOP_RIGHT:
523 pointer = POINTER_TOP_RIGHT;
525 case WINDOW_RESIZING_BOTTOM_LEFT:
526 pointer = POINTER_BOTTOM_LEFT;
528 case WINDOW_RESIZING_BOTTOM_RIGHT:
529 pointer = POINTER_BOTTOM_RIGHT;
531 case WINDOW_EXTERIOR:
532 case WINDOW_TITLEBAR:
533 wl_input_device_attach(input->input_device, NULL, 0, 0);
539 surface = display->pointer_surfaces[pointer];
540 buffer = display_get_buffer_for_surface(display, surface);
541 wl_input_device_attach(input->input_device, buffer,
542 pointer_images[pointer].hotspot_x,
543 pointer_images[pointer].hotspot_y);
547 window_handle_motion(void *data, struct wl_input_device *input_device,
549 int32_t x, int32_t y, int32_t sx, int32_t sy)
551 struct input *input = data;
552 struct window *window = input->pointer_focus;
553 int location, pointer = POINTER_LEFT_PTR;
560 location = get_pointer_location(window, input->sx, input->sy);
562 if (window->motion_handler)
563 pointer = (*window->motion_handler)(window, input, time,
567 set_pointer_image(input, pointer);
571 window_handle_button(void *data,
572 struct wl_input_device *input_device,
573 uint32_t time, uint32_t button, uint32_t state)
575 struct input *input = data;
576 struct window *window = input->pointer_focus;
579 location = get_pointer_location(window, input->sx, input->sy);
581 if (button == BTN_LEFT && state == 1) {
583 case WINDOW_TITLEBAR:
584 wl_shell_move(window->display->shell,
585 window->surface, input_device, time);
587 case WINDOW_RESIZING_TOP:
588 case WINDOW_RESIZING_BOTTOM:
589 case WINDOW_RESIZING_LEFT:
590 case WINDOW_RESIZING_RIGHT:
591 case WINDOW_RESIZING_TOP_LEFT:
592 case WINDOW_RESIZING_TOP_RIGHT:
593 case WINDOW_RESIZING_BOTTOM_LEFT:
594 case WINDOW_RESIZING_BOTTOM_RIGHT:
595 wl_shell_resize(window->display->shell,
596 window->surface, input_device, time,
599 case WINDOW_CLIENT_AREA:
600 if (window->button_handler)
601 (*window->button_handler)(window,
608 if (window->button_handler)
609 (*window->button_handler)(window,
617 window_handle_key(void *data, struct wl_input_device *input_device,
618 uint32_t time, uint32_t key, uint32_t state)
620 struct input *input = data;
621 struct window *window = input->keyboard_focus;
622 struct display *d = window->display;
623 uint32_t code, sym, level;
625 code = key + d->xkb->min_key_code;
626 if (window->keyboard_device != input)
630 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
631 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
634 sym = XkbKeySymEntry(d->xkb, code, level, 0);
637 input->modifiers |= d->xkb->map->modmap[code];
639 input->modifiers &= ~d->xkb->map->modmap[code];
641 if (window->key_handler)
642 (*window->key_handler)(window, key, sym, state,
643 input->modifiers, window->user_data);
647 window_handle_pointer_focus(void *data,
648 struct wl_input_device *input_device,
649 uint32_t time, struct wl_surface *surface,
650 int32_t x, int32_t y, int32_t sx, int32_t sy)
652 struct input *input = data;
653 struct window *window;
657 input->pointer_focus = wl_surface_get_user_data(surface);
658 window = input->pointer_focus;
660 pointer = POINTER_LEFT_PTR;
661 if (window->motion_handler)
662 pointer = (*window->motion_handler)(window,
667 set_pointer_image(input, pointer);
669 input->pointer_focus = NULL;
674 window_handle_keyboard_focus(void *data,
675 struct wl_input_device *input_device,
677 struct wl_surface *surface,
678 struct wl_array *keys)
680 struct input *input = data;
681 struct window *window = input->keyboard_focus;
682 struct display *d = input->display;
685 window = input->keyboard_focus;
687 window->keyboard_device = NULL;
688 if (window->keyboard_focus_handler)
689 (*window->keyboard_focus_handler)(window, NULL,
694 input->keyboard_focus = wl_surface_get_user_data(surface);
696 input->keyboard_focus = NULL;
698 end = keys->data + keys->size;
699 for (k = keys->data; k < end; k++)
700 input->modifiers |= d->xkb->map->modmap[*k];
702 window = input->keyboard_focus;
704 window->keyboard_device = input;
705 if (window->keyboard_focus_handler)
706 (*window->keyboard_focus_handler)(window,
707 window->keyboard_device,
712 static const struct wl_input_device_listener input_device_listener = {
713 window_handle_motion,
714 window_handle_button,
716 window_handle_pointer_focus,
717 window_handle_keyboard_focus,
721 input_get_position(struct input *input, int32_t *x, int32_t *y)
727 struct wl_input_device *
728 input_get_input_device(struct input *input)
730 return input->input_device;
734 display_add_drag_listener(struct display *display,
735 const struct wl_drag_listener *drag_listener,
740 wl_list_for_each(input, &display->input_list, link)
741 wl_drag_add_listener(input->drag, drag_listener, data);
745 window_start_drag(struct window *window, struct input *input, uint32_t time)
747 cairo_device_flush (window->display->device);
749 wl_drag_prepare(input->drag, window->surface, time);
750 wl_drag_offer(input->drag, "text/plain");
751 wl_drag_activate(input->drag);
755 handle_configure(void *data, struct wl_shell *shell,
756 uint32_t time, uint32_t edges,
757 struct wl_surface *surface,
758 int32_t x, int32_t y, int32_t width, int32_t height)
760 struct window *window = wl_surface_get_user_data(surface);
762 window->resize_edges = edges;
763 window->pending_allocation.x = x;
764 window->pending_allocation.y = y;
765 window->pending_allocation.width = width;
766 window->pending_allocation.height = height;
771 if (window->resize_handler)
772 (*window->resize_handler)(window,
774 else if (window->redraw_handler)
775 window_schedule_redraw(window);
778 static const struct wl_shell_listener shell_listener = {
783 window_get_child_rectangle(struct window *window,
784 struct rectangle *rectangle)
786 if (window->fullscreen && !window->decoration) {
787 *rectangle = window->allocation;
789 rectangle->x = window->margin + 10;
790 rectangle->y = window->margin + 50;
791 rectangle->width = window->allocation.width - 20 - window->margin * 2;
792 rectangle->height = window->allocation.height - 60 - window->margin * 2;
797 window_set_child_size(struct window *window,
798 struct rectangle *rectangle)
800 int32_t width, height;
802 if (!window->fullscreen) {
803 width = rectangle->width + 20 + window->margin * 2;
804 height = rectangle->height + 60 + window->margin * 2;
806 if (window->resize_edges & WINDOW_RESIZING_LEFT)
807 window->allocation.x +=
808 window->allocation.width - width;
809 if (window->resize_edges & WINDOW_RESIZING_TOP)
810 window->allocation.y +=
811 window->allocation.height - height;
813 window->allocation.width = width;
814 window->allocation.height = height;
819 window_copy_image(struct window *window,
820 struct rectangle *rectangle, EGLImageKHR image)
822 /* set image as read buffer, copy pixels or something... */
826 window_copy_surface(struct window *window,
827 struct rectangle *rectangle,
828 cairo_surface_t *surface)
832 cr = cairo_create (window->cairo_surface);
834 cairo_set_source_surface (cr,
836 rectangle->x, rectangle->y);
843 idle_redraw(void *data)
845 struct window *window = data;
847 if (window->resize_edges)
848 window->allocation = window->pending_allocation;
850 window->redraw_handler(window, window->user_data);
852 window->redraw_scheduled = 0;
853 window->resize_edges = 0;
859 window_schedule_redraw(struct window *window)
861 if (!window->redraw_scheduled) {
862 g_idle_add(idle_redraw, window);
863 window->redraw_scheduled = 1;
868 window_set_fullscreen(struct window *window, int fullscreen)
870 window->fullscreen = fullscreen;
871 if (window->fullscreen) {
872 window->saved_allocation = window->allocation;
873 window->allocation = window->display->screen_allocation;
875 window->allocation = window->saved_allocation;
880 window_set_decoration(struct window *window, int decoration)
882 window->decoration = decoration;
886 window_set_user_data(struct window *window, void *data)
888 window->user_data = data;
892 window_set_resize_handler(struct window *window,
893 window_resize_handler_t handler)
895 window->resize_handler = handler;
899 window_set_redraw_handler(struct window *window,
900 window_redraw_handler_t handler)
902 window->redraw_handler = handler;
906 window_set_key_handler(struct window *window,
907 window_key_handler_t handler)
909 window->key_handler = handler;
913 window_set_button_handler(struct window *window,
914 window_button_handler_t handler)
916 window->button_handler = handler;
920 window_set_acknowledge_handler(struct window *window,
921 window_acknowledge_handler_t handler)
923 window->acknowledge_handler = handler;
927 window_set_frame_handler(struct window *window,
928 window_frame_handler_t handler)
930 window->frame_handler = handler;
934 window_set_motion_handler(struct window *window,
935 window_motion_handler_t handler)
937 window->motion_handler = handler;
941 window_set_keyboard_focus_handler(struct window *window,
942 window_keyboard_focus_handler_t handler)
944 window->keyboard_focus_handler = handler;
948 window_move(struct window *window, int32_t x, int32_t y)
950 window->allocation.x = x;
951 window->allocation.y = y;
953 wl_surface_map(window->surface,
954 window->allocation.x - window->margin,
955 window->allocation.y - window->margin,
956 window->allocation.width,
957 window->allocation.height);
961 window_create(struct display *display, const char *title,
962 int32_t x, int32_t y, int32_t width, int32_t height)
964 struct window *window;
966 window = malloc(sizeof *window);
970 memset(window, 0, sizeof *window);
971 window->display = display;
972 window->title = strdup(title);
973 window->surface = wl_compositor_create_surface(display->compositor);
974 window->allocation.x = x;
975 window->allocation.y = y;
976 window->allocation.width = width;
977 window->allocation.height = height;
978 window->saved_allocation = window->allocation;
980 window->decoration = 1;
982 wl_surface_set_user_data(window->surface, window);
983 wl_list_insert(display->window_list.prev, &window->link);
989 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
991 struct display *d = data;
993 d->device_name = strdup(device);
996 static void drm_handle_authenticated(void *data, struct wl_drm *drm)
998 struct display *d = data;
1000 d->authenticated = 1;
1003 static const struct wl_drm_listener drm_listener = {
1005 drm_handle_authenticated
1009 display_handle_acknowledge(void *data,
1010 struct wl_compositor *compositor,
1011 uint32_t key, uint32_t frame)
1013 struct display *d = data;
1014 struct window *window;
1016 /* The acknowledge event means that the server processed our
1017 * last commit request and we can now safely free the old
1018 * window buffer if we resized and render the next frame into
1019 * our back buffer.. */
1020 wl_list_for_each(window, &d->window_list, link) {
1021 cairo_surface_destroy(window->pending_surface);
1022 window->pending_surface = NULL;
1023 if (window->cairo_surface)
1024 window_attach_surface(window);
1025 if (window->acknowledge_handler)
1026 (*window->acknowledge_handler)(window, key, frame, window->user_data);
1031 display_handle_frame(void *data,
1032 struct wl_compositor *compositor,
1033 uint32_t frame, uint32_t timestamp)
1035 struct display *d = data;
1036 struct window *window;
1038 wl_list_for_each(window, &d->window_list, link) {
1039 if (window->frame_handler)
1040 (*window->frame_handler)(window, frame,
1041 timestamp, window->user_data);
1045 static const struct wl_compositor_listener compositor_listener = {
1046 display_handle_acknowledge,
1047 display_handle_frame,
1051 display_handle_geometry(void *data,
1052 struct wl_output *output,
1053 int32_t width, int32_t height)
1055 struct display *display = data;
1057 display->screen_allocation.x = 0;
1058 display->screen_allocation.y = 0;
1059 display->screen_allocation.width = width;
1060 display->screen_allocation.height = height;
1063 static const struct wl_output_listener output_listener = {
1064 display_handle_geometry,
1068 display_add_input(struct display *d, uint32_t id)
1070 struct input *input;
1072 input = malloc(sizeof *input);
1076 memset(input, 0, sizeof *input);
1078 input->input_device = wl_input_device_create(d->display, id);
1079 input->pointer_focus = NULL;
1080 input->keyboard_focus = NULL;
1081 wl_list_insert(d->input_list.prev, &input->link);
1083 wl_input_device_add_listener(input->input_device,
1084 &input_device_listener, input);
1085 wl_input_device_set_user_data(input->input_device, input);
1089 drag_handle_device(void *data,
1090 struct wl_drag *drag, struct wl_input_device *device)
1092 struct input *input;
1093 fprintf(stderr, "device for drag %p: %p\n", drag, device);
1095 input = wl_input_device_get_user_data(device);
1097 wl_drag_set_user_data(drag, input);
1101 drag_pointer_focus(void *data,
1102 struct wl_drag *drag,
1103 uint32_t time, struct wl_surface *surface,
1104 int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
1109 drag_offer(void *data,
1110 struct wl_drag *drag, const char *type)
1115 drag_motion(void *data,
1116 struct wl_drag *drag,
1118 int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
1123 drag_target(void *data,
1124 struct wl_drag *drag, const char *mime_type)
1129 drag_drop(void *data, struct wl_drag *drag)
1134 drag_finish(void *data, struct wl_drag *drag, int fd)
1138 static const struct wl_drag_listener drag_listener = {
1149 display_handle_global(struct wl_display *display, uint32_t id,
1150 const char *interface, uint32_t version, void *data)
1152 struct display *d = data;
1153 struct wl_drag *drag;
1155 if (strcmp(interface, "compositor") == 0) {
1156 d->compositor = wl_compositor_create(display, id);
1157 wl_compositor_add_listener(d->compositor,
1158 &compositor_listener, d);
1159 } else if (strcmp(interface, "output") == 0) {
1160 d->output = wl_output_create(display, id);
1161 wl_output_add_listener(d->output, &output_listener, d);
1162 } else if (strcmp(interface, "input_device") == 0) {
1163 display_add_input(d, id);
1164 } else if (strcmp(interface, "shell") == 0) {
1165 d->shell = wl_shell_create(display, id);
1166 wl_shell_add_listener(d->shell, &shell_listener, d);
1167 } else if (strcmp(interface, "drm") == 0) {
1168 d->drm = wl_drm_create(display, id);
1169 wl_drm_add_listener(d->drm, &drm_listener, d);
1170 } else if (strcmp(interface, "drag") == 0) {
1171 drag = wl_drag_create(display, id);
1172 wl_drag_add_listener(drag, &drag_listener, NULL);
1176 static const char socket_name[] = "\0wayland";
1179 display_render_frame(struct display *d)
1184 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1185 cr = cairo_create(d->shadow);
1186 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1187 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1188 rounded_rect(cr, 16, 16, 112, 112, radius);
1191 blur_surface(d->shadow, 64);
1194 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1195 cr = cairo_create(d->active_frame);
1196 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1197 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1198 rounded_rect(cr, 16, 16, 112, 112, radius);
1203 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1204 cr = cairo_create(d->inactive_frame);
1205 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1206 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1207 rounded_rect(cr, 16, 16, 112, 112, radius);
1213 init_xkb(struct display *d)
1215 struct xkb_rule_names names;
1217 names.rules = "evdev";
1218 names.model = "pc105";
1219 names.layout = "us";
1223 d->xkb = xkb_compile_keymap_from_rules(&names);
1225 fprintf(stderr, "Failed to compile keymap\n");
1231 display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
1234 EGLint major, minor;
1236 GOptionContext *context;
1242 context = g_option_context_new(NULL);
1243 if (option_entries) {
1244 g_option_context_add_main_entries(context, option_entries, "Wayland View");
1245 if (!g_option_context_parse(context, argc, argv, &error)) {
1246 fprintf(stderr, "option parsing failed: %s\n", error->message);
1251 d = malloc(sizeof *d);
1255 d->display = wl_display_create(socket_name, sizeof socket_name);
1256 if (d->display == NULL) {
1257 fprintf(stderr, "failed to create display: %m\n");
1261 wl_list_init(&d->input_list);
1263 /* Set up listener so we'll catch all events. */
1264 wl_display_add_global_listener(d->display,
1265 display_handle_global, d);
1267 /* Process connection events. */
1268 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1270 fd = open(d->device_name, O_RDWR);
1272 fprintf(stderr, "drm open failed: %m\n");
1276 if (drmGetMagic(fd, &magic)) {
1277 fprintf(stderr, "DRI2: failed to get drm magic");
1281 /* Wait for authenticated event */
1282 wl_drm_authenticate(d->drm, magic);
1283 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1284 while (!d->authenticated)
1285 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1287 d->dpy = eglGetDRMDisplayMESA(fd);
1288 if (!eglInitialize(d->dpy, &major, &minor)) {
1289 fprintf(stderr, "failed to initialize display\n");
1293 eglBindAPI(EGL_OPENGL_API);
1295 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1296 if (d->ctx == NULL) {
1297 fprintf(stderr, "failed to create context\n");
1301 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1302 fprintf(stderr, "faile to make context current\n");
1306 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1307 if (d->device == NULL) {
1308 fprintf(stderr, "failed to get cairo drm device\n");
1312 create_pointer_surfaces(d);
1314 display_render_frame(d);
1316 d->loop = g_main_loop_new(NULL, FALSE);
1317 d->source = wl_glib_source_new(d->display);
1318 g_source_attach(d->source, NULL);
1320 wl_list_init(&d->window_list);
1327 struct wl_compositor *
1328 display_get_compositor(struct display *display)
1330 return display->compositor;
1334 display_get_egl_display(struct display *d)
1340 display_run(struct display *d)
1342 g_main_loop_run(d->loop);