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;
75 display_drag_offer_handler_t drag_offer_handler;
79 struct display *display;
80 struct wl_surface *surface;
82 struct rectangle allocation, saved_allocation, pending_allocation;
85 int minimum_width, minimum_height;
89 struct input *grab_device;
90 struct input *keyboard_device;
94 cairo_surface_t *cairo_surface, *pending_surface;
96 window_resize_handler_t resize_handler;
97 window_redraw_handler_t redraw_handler;
98 window_key_handler_t key_handler;
99 window_button_handler_t button_handler;
100 window_keyboard_focus_handler_t keyboard_focus_handler;
101 window_motion_handler_t motion_handler;
108 struct display *display;
109 struct wl_input_device *input_device;
110 struct window *pointer_focus;
111 struct window *keyboard_focus;
112 uint32_t current_pointer_image;
114 int32_t x, y, sx, sy;
119 POINTER_DEFAULT = 100,
123 const char *option_xkb_layout = "us";
124 const char *option_xkb_variant = "";
125 const char *option_xkb_options = "";
127 static const GOptionEntry xkb_option_entries[] = {
128 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
129 &option_xkb_layout, "XKB Layout" },
130 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
131 &option_xkb_variant, "XKB Variant" },
132 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
133 &option_xkb_options, "XKB Options" },
138 rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
140 cairo_move_to(cr, x0, y0 + radius);
141 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
142 cairo_line_to(cr, x1 - radius, y0);
143 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
144 cairo_line_to(cr, x1, y1 - radius);
145 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
146 cairo_line_to(cr, x0 + radius, y1);
147 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
148 cairo_close_path(cr);
152 texture_from_png(const char *filename, int width, int height)
155 GError *error = NULL;
157 unsigned char *pixels, *p, *end;
159 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
165 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
166 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
167 gdk_pixbuf_unref(pixbuf);
172 stride = gdk_pixbuf_get_rowstride(pixbuf);
173 pixels = gdk_pixbuf_get_pixels(pixbuf);
175 for (i = 0; i < height; i++) {
176 p = pixels + i * stride;
181 #define MULT(d,c,a,t) \
182 do { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } while (0)
184 MULT(p[0], p[0], p[3], t);
185 MULT(p[1], p[1], p[3], t);
186 MULT(p[2], p[2], p[3], t);
192 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
193 width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
195 gdk_pixbuf_unref(pixbuf);
200 static const struct {
201 const char *filename;
202 int hotspot_x, hotspot_y;
203 } pointer_images[] = {
204 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
205 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
206 { DATADIR "/wayland/bottom_side.png", 16, 20 },
207 { DATADIR "/wayland/grabbing.png", 20, 17 },
208 { DATADIR "/wayland/left_ptr.png", 10, 5 },
209 { DATADIR "/wayland/left_side.png", 10, 20 },
210 { DATADIR "/wayland/right_side.png", 30, 19 },
211 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
212 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
213 { DATADIR "/wayland/top_side.png", 18, 8 },
214 { DATADIR "/wayland/xterm.png", 15, 15 },
215 { DATADIR "/wayland/hand1.png", 18, 11 }
219 create_pointer_surfaces(struct display *display)
222 const int width = 32, height = 32;
223 struct rectangle rect;
225 count = ARRAY_LENGTH(pointer_images);
226 display->pointer_surfaces =
227 malloc(count * sizeof *display->pointer_surfaces);
229 rect.height = height;
230 for (i = 0; i < count; i++) {
231 display->pointer_surfaces[i] =
232 display_create_surface(display, &rect);
233 texture_from_png(pointer_images[i].filename, width, height);
238 static const cairo_user_data_key_t surface_data_key;
239 struct surface_data {
243 struct wl_buffer *buffer;
247 surface_data_destroy(void *p)
249 struct surface_data *data = p;
251 glDeleteTextures(1, &data->texture);
252 eglDestroyImageKHR(data->dpy, data->image);
254 wl_buffer_destroy(data->buffer);
258 display_create_surface(struct display *display,
259 struct rectangle *rectangle)
261 struct surface_data *data;
262 EGLDisplay dpy = display->dpy;
263 cairo_surface_t *surface;
265 EGLint image_attribs[] = {
268 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
269 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
273 data = malloc(sizeof *data);
274 image_attribs[1] = rectangle->width;
275 image_attribs[3] = rectangle->height;
276 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
277 glGenTextures(1, &data->texture);
279 glBindTexture(GL_TEXTURE_2D, data->texture);
280 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
283 surface = cairo_gl_surface_create_for_texture(display->device,
284 CAIRO_CONTENT_COLOR_ALPHA,
289 cairo_surface_set_user_data (surface, &surface_data_key,
290 data, surface_data_destroy);
296 display_get_buffer_for_surface(struct display *display,
297 cairo_surface_t *surface)
299 struct surface_data *data;
300 struct wl_visual *visual;
301 struct wl_buffer *buffer;
305 data = cairo_surface_get_user_data (surface, &surface_data_key);
309 width = cairo_gl_surface_get_width (surface);
310 height = cairo_gl_surface_get_height (surface);
312 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
314 visual = wl_display_get_premultiplied_argb_visual(display->display);
315 buffer = wl_drm_create_buffer(display->drm,
316 name, width, height, stride, visual);
317 data->buffer = buffer;
323 display_get_pointer_surface(struct display *display, int pointer,
324 int *width, int *height,
325 int *hotspot_x, int *hotspot_y)
327 cairo_surface_t *surface;
329 surface = display->pointer_surfaces[pointer];
330 *width = cairo_gl_surface_get_width(surface);
331 *height = cairo_gl_surface_get_height(surface);
332 *hotspot_x = pointer_images[pointer].hotspot_x;
333 *hotspot_y = pointer_images[pointer].hotspot_y;
335 return cairo_surface_reference(surface);
340 window_attach_surface(struct window *window);
343 free_surface(void *data)
345 struct window *window = data;
347 cairo_surface_destroy(window->pending_surface);
348 window->pending_surface = NULL;
349 if (window->cairo_surface)
350 window_attach_surface(window);
354 window_attach_surface(struct window *window)
356 struct display *display = window->display;
357 struct wl_buffer *buffer;
359 if (window->pending_surface != NULL)
362 window->pending_surface = window->cairo_surface;
363 window->cairo_surface = NULL;
365 buffer = display_get_buffer_for_surface(display,
366 window->pending_surface);
367 wl_surface_attach(window->surface, buffer);
369 wl_surface_map(window->surface,
370 window->allocation.x,
371 window->allocation.y,
372 window->allocation.width,
373 window->allocation.height);
375 wl_display_sync_callback(display->display, free_surface, window);
379 window_flush(struct window *window)
381 if (window->cairo_surface)
382 window_attach_surface(window);
386 window_draw_decorations(struct window *window)
389 cairo_text_extents_t extents;
390 cairo_pattern_t *outline, *bright, *dim;
391 cairo_surface_t *frame;
392 int width, height, shadow_dx = 3, shadow_dy = 3;
394 window->cairo_surface =
395 display_create_surface(window->display, &window->allocation);
396 width = window->allocation.width;
397 height = window->allocation.height;
399 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
400 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
401 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
403 cr = cairo_create(window->cairo_surface);
405 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
406 cairo_set_source_rgba(cr, 0, 0, 0, 0);
409 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
410 tile_mask(cr, window->display->shadow,
411 shadow_dx, shadow_dy, width, height,
412 window->margin + 10 - shadow_dx,
413 window->margin + 10 - shadow_dy);
415 if (window->keyboard_device)
416 frame = window->display->active_frame;
418 frame = window->display->inactive_frame;
420 tile_source(cr, frame, 0, 0, width, height,
421 window->margin + 10, window->margin + 50);
423 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
424 cairo_set_font_size(cr, 14);
425 cairo_text_extents(cr, window->title, &extents);
426 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
427 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
428 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
429 cairo_set_line_width (cr, 4);
430 if (window->keyboard_device)
431 cairo_set_source_rgb(cr, 0, 0, 0);
433 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
434 cairo_show_text(cr, window->title);
438 cairo_device_flush (window->display->device);
442 display_flush_cairo_device(struct display *display)
444 cairo_device_flush (display->device);
448 window_draw_fullscreen(struct window *window)
450 window->cairo_surface =
451 display_create_surface(window->display, &window->allocation);
455 window_draw(struct window *window)
457 if (window->cairo_surface != NULL)
458 cairo_surface_destroy(window->cairo_surface);
460 if (window->fullscreen || !window->decoration)
461 window_draw_fullscreen(window);
463 window_draw_decorations(window);
467 window_get_surface(struct window *window)
469 return window->cairo_surface;
472 enum window_location {
474 WINDOW_RESIZING_TOP = 1,
475 WINDOW_RESIZING_BOTTOM = 2,
476 WINDOW_RESIZING_LEFT = 4,
477 WINDOW_RESIZING_TOP_LEFT = 5,
478 WINDOW_RESIZING_BOTTOM_LEFT = 6,
479 WINDOW_RESIZING_RIGHT = 8,
480 WINDOW_RESIZING_TOP_RIGHT = 9,
481 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
482 WINDOW_RESIZING_MASK = 15,
483 WINDOW_EXTERIOR = 16,
484 WINDOW_TITLEBAR = 17,
485 WINDOW_CLIENT_AREA = 18,
489 get_pointer_location(struct window *window, int32_t x, int32_t y)
491 int vlocation, hlocation, location;
492 const int grip_size = 8;
494 if (x < window->margin)
495 hlocation = WINDOW_EXTERIOR;
496 else if (window->margin <= x && x < window->margin + grip_size)
497 hlocation = WINDOW_RESIZING_LEFT;
498 else if (x < window->allocation.width - window->margin - grip_size)
499 hlocation = WINDOW_INTERIOR;
500 else if (x < window->allocation.width - window->margin)
501 hlocation = WINDOW_RESIZING_RIGHT;
503 hlocation = WINDOW_EXTERIOR;
505 if (y < window->margin)
506 vlocation = WINDOW_EXTERIOR;
507 else if (window->margin <= y && y < window->margin + grip_size)
508 vlocation = WINDOW_RESIZING_TOP;
509 else if (y < window->allocation.height - window->margin - grip_size)
510 vlocation = WINDOW_INTERIOR;
511 else if (y < window->allocation.height - window->margin)
512 vlocation = WINDOW_RESIZING_BOTTOM;
514 vlocation = WINDOW_EXTERIOR;
516 location = vlocation | hlocation;
517 if (location & WINDOW_EXTERIOR)
518 location = WINDOW_EXTERIOR;
519 if (location == WINDOW_INTERIOR && y < window->margin + 50)
520 location = WINDOW_TITLEBAR;
521 else if (location == WINDOW_INTERIOR)
522 location = WINDOW_CLIENT_AREA;
528 set_pointer_image(struct input *input, int pointer)
530 struct display *display = input->display;
531 struct wl_buffer *buffer;
532 cairo_surface_t *surface;
535 location = get_pointer_location(input->pointer_focus,
536 input->sx, input->sy);
538 case WINDOW_RESIZING_TOP:
539 pointer = POINTER_TOP;
541 case WINDOW_RESIZING_BOTTOM:
542 pointer = POINTER_BOTTOM;
544 case WINDOW_RESIZING_LEFT:
545 pointer = POINTER_LEFT;
547 case WINDOW_RESIZING_RIGHT:
548 pointer = POINTER_RIGHT;
550 case WINDOW_RESIZING_TOP_LEFT:
551 pointer = POINTER_TOP_LEFT;
553 case WINDOW_RESIZING_TOP_RIGHT:
554 pointer = POINTER_TOP_RIGHT;
556 case WINDOW_RESIZING_BOTTOM_LEFT:
557 pointer = POINTER_BOTTOM_LEFT;
559 case WINDOW_RESIZING_BOTTOM_RIGHT:
560 pointer = POINTER_BOTTOM_RIGHT;
562 case WINDOW_EXTERIOR:
563 case WINDOW_TITLEBAR:
564 if (input->current_pointer_image == POINTER_DEFAULT)
567 wl_input_device_attach(input->input_device, NULL, 0, 0);
568 input->current_pointer_image = POINTER_DEFAULT;
574 if (pointer == input->current_pointer_image)
577 input->current_pointer_image = pointer;
578 surface = display->pointer_surfaces[pointer];
579 buffer = display_get_buffer_for_surface(display, surface);
580 wl_input_device_attach(input->input_device, buffer,
581 pointer_images[pointer].hotspot_x,
582 pointer_images[pointer].hotspot_y);
586 window_handle_motion(void *data, struct wl_input_device *input_device,
588 int32_t x, int32_t y, int32_t sx, int32_t sy)
590 struct input *input = data;
591 struct window *window = input->pointer_focus;
592 int location, pointer = POINTER_LEFT_PTR;
599 location = get_pointer_location(window, input->sx, input->sy);
601 if (window->motion_handler)
602 pointer = (*window->motion_handler)(window, input, time,
606 set_pointer_image(input, pointer);
610 window_handle_button(void *data,
611 struct wl_input_device *input_device,
612 uint32_t time, uint32_t button, uint32_t state)
614 struct input *input = data;
615 struct window *window = input->pointer_focus;
618 location = get_pointer_location(window, input->sx, input->sy);
620 if (button == BTN_LEFT && state == 1) {
622 case WINDOW_TITLEBAR:
623 wl_shell_move(window->display->shell,
624 window->surface, input_device, time);
626 case WINDOW_RESIZING_TOP:
627 case WINDOW_RESIZING_BOTTOM:
628 case WINDOW_RESIZING_LEFT:
629 case WINDOW_RESIZING_RIGHT:
630 case WINDOW_RESIZING_TOP_LEFT:
631 case WINDOW_RESIZING_TOP_RIGHT:
632 case WINDOW_RESIZING_BOTTOM_LEFT:
633 case WINDOW_RESIZING_BOTTOM_RIGHT:
634 wl_shell_resize(window->display->shell,
635 window->surface, input_device, time,
638 case WINDOW_CLIENT_AREA:
639 if (window->button_handler)
640 (*window->button_handler)(window,
647 if (window->button_handler)
648 (*window->button_handler)(window,
656 window_handle_key(void *data, struct wl_input_device *input_device,
657 uint32_t time, uint32_t key, uint32_t state)
659 struct input *input = data;
660 struct window *window = input->keyboard_focus;
661 struct display *d = window->display;
662 uint32_t code, sym, level;
664 code = key + d->xkb->min_key_code;
665 if (window->keyboard_device != input)
669 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
670 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
673 sym = XkbKeySymEntry(d->xkb, code, level, 0);
676 input->modifiers |= d->xkb->map->modmap[code];
678 input->modifiers &= ~d->xkb->map->modmap[code];
680 if (window->key_handler)
681 (*window->key_handler)(window, key, sym, state,
682 input->modifiers, window->user_data);
686 window_handle_pointer_focus(void *data,
687 struct wl_input_device *input_device,
688 uint32_t time, struct wl_surface *surface,
689 int32_t x, int32_t y, int32_t sx, int32_t sy)
691 struct input *input = data;
692 struct window *window;
696 input->pointer_focus = wl_surface_get_user_data(surface);
697 window = input->pointer_focus;
699 pointer = POINTER_LEFT_PTR;
700 if (window->motion_handler)
701 pointer = (*window->motion_handler)(window,
706 set_pointer_image(input, pointer);
708 input->pointer_focus = NULL;
709 input->current_pointer_image = POINTER_UNSET;
714 window_handle_keyboard_focus(void *data,
715 struct wl_input_device *input_device,
717 struct wl_surface *surface,
718 struct wl_array *keys)
720 struct input *input = data;
721 struct window *window = input->keyboard_focus;
722 struct display *d = input->display;
725 window = input->keyboard_focus;
727 window->keyboard_device = NULL;
728 if (window->keyboard_focus_handler)
729 (*window->keyboard_focus_handler)(window, NULL,
734 input->keyboard_focus = wl_surface_get_user_data(surface);
736 input->keyboard_focus = NULL;
738 end = keys->data + keys->size;
739 for (k = keys->data; k < end; k++)
740 input->modifiers |= d->xkb->map->modmap[*k];
742 window = input->keyboard_focus;
744 window->keyboard_device = input;
745 if (window->keyboard_focus_handler)
746 (*window->keyboard_focus_handler)(window,
747 window->keyboard_device,
752 static const struct wl_input_device_listener input_device_listener = {
753 window_handle_motion,
754 window_handle_button,
756 window_handle_pointer_focus,
757 window_handle_keyboard_focus,
761 input_get_position(struct input *input, int32_t *x, int32_t *y)
767 struct wl_input_device *
768 input_get_input_device(struct input *input)
770 return input->input_device;
774 window_start_drag(struct window *window, struct input *input, uint32_t time,
775 const struct wl_drag_listener *listener, void *data)
777 struct wl_drag *drag;
779 cairo_device_flush (window->display->device);
781 drag = wl_shell_create_drag(window->display->shell);
782 wl_drag_offer(drag, "text/plain");
783 wl_drag_offer(drag, "text/html");
784 wl_drag_activate(drag, window->surface, input->input_device, time);
785 wl_drag_add_listener(drag, listener, data);
791 handle_configure(void *data, struct wl_shell *shell,
792 uint32_t time, uint32_t edges,
793 struct wl_surface *surface,
794 int32_t x, int32_t y, int32_t width, int32_t height)
796 struct window *window = wl_surface_get_user_data(surface);
798 window->resize_edges = edges;
799 window->pending_allocation.x = x;
800 window->pending_allocation.y = y;
801 window->pending_allocation.width = width;
802 window->pending_allocation.height = height;
807 if (window->resize_handler)
808 (*window->resize_handler)(window,
810 else if (window->redraw_handler)
811 window_schedule_redraw(window);
814 static const struct wl_shell_listener shell_listener = {
819 window_get_child_rectangle(struct window *window,
820 struct rectangle *rectangle)
822 if (window->fullscreen && !window->decoration) {
823 *rectangle = window->allocation;
825 rectangle->x = window->margin + 10;
826 rectangle->y = window->margin + 50;
827 rectangle->width = window->allocation.width - 20 - window->margin * 2;
828 rectangle->height = window->allocation.height - 60 - window->margin * 2;
833 window_set_child_size(struct window *window,
834 struct rectangle *rectangle)
836 int32_t width, height;
838 if (!window->fullscreen) {
839 width = rectangle->width + 20 + window->margin * 2;
840 height = rectangle->height + 60 + window->margin * 2;
842 if (window->resize_edges & WINDOW_RESIZING_LEFT)
843 window->allocation.x +=
844 window->allocation.width - width;
845 if (window->resize_edges & WINDOW_RESIZING_TOP)
846 window->allocation.y +=
847 window->allocation.height - height;
849 window->allocation.width = width;
850 window->allocation.height = height;
855 window_copy_image(struct window *window,
856 struct rectangle *rectangle, EGLImageKHR image)
858 /* set image as read buffer, copy pixels or something... */
862 window_copy_surface(struct window *window,
863 struct rectangle *rectangle,
864 cairo_surface_t *surface)
868 cr = cairo_create (window->cairo_surface);
870 cairo_set_source_surface (cr,
872 rectangle->x, rectangle->y);
879 idle_redraw(void *data)
881 struct window *window = data;
883 if (window->resize_edges)
884 window->allocation = window->pending_allocation;
886 window->redraw_handler(window, window->user_data);
888 window->redraw_scheduled = 0;
889 window->resize_edges = 0;
895 window_schedule_redraw(struct window *window)
897 if (!window->redraw_scheduled) {
898 g_idle_add(idle_redraw, window);
899 window->redraw_scheduled = 1;
904 window_set_fullscreen(struct window *window, int fullscreen)
906 window->fullscreen = fullscreen;
907 if (window->fullscreen) {
908 window->saved_allocation = window->allocation;
909 window->allocation = window->display->screen_allocation;
911 window->allocation = window->saved_allocation;
916 window_set_decoration(struct window *window, int decoration)
918 window->decoration = decoration;
922 window_set_user_data(struct window *window, void *data)
924 window->user_data = data;
928 window_get_user_data(struct window *window)
930 return window->user_data;
934 window_set_resize_handler(struct window *window,
935 window_resize_handler_t handler)
937 window->resize_handler = handler;
941 window_set_redraw_handler(struct window *window,
942 window_redraw_handler_t handler)
944 window->redraw_handler = handler;
948 window_set_key_handler(struct window *window,
949 window_key_handler_t handler)
951 window->key_handler = handler;
955 window_set_button_handler(struct window *window,
956 window_button_handler_t handler)
958 window->button_handler = handler;
962 window_set_motion_handler(struct window *window,
963 window_motion_handler_t handler)
965 window->motion_handler = handler;
969 window_set_keyboard_focus_handler(struct window *window,
970 window_keyboard_focus_handler_t handler)
972 window->keyboard_focus_handler = handler;
976 window_move(struct window *window, int32_t x, int32_t y)
978 window->allocation.x = x;
979 window->allocation.y = y;
981 wl_surface_map(window->surface,
982 window->allocation.x - window->margin,
983 window->allocation.y - window->margin,
984 window->allocation.width,
985 window->allocation.height);
989 window_create(struct display *display, const char *title,
990 int32_t x, int32_t y, int32_t width, int32_t height)
992 struct window *window;
994 window = malloc(sizeof *window);
998 memset(window, 0, sizeof *window);
999 window->display = display;
1000 window->title = strdup(title);
1001 window->surface = wl_compositor_create_surface(display->compositor);
1002 window->allocation.x = x;
1003 window->allocation.y = y;
1004 window->allocation.width = width;
1005 window->allocation.height = height;
1006 window->saved_allocation = window->allocation;
1007 window->margin = 16;
1008 window->decoration = 1;
1010 wl_surface_set_user_data(window->surface, window);
1011 wl_list_insert(display->window_list.prev, &window->link);
1017 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
1019 struct display *d = data;
1021 d->device_name = strdup(device);
1024 static void drm_handle_authenticated(void *data, struct wl_drm *drm)
1026 struct display *d = data;
1028 d->authenticated = 1;
1031 static const struct wl_drm_listener drm_listener = {
1033 drm_handle_authenticated
1037 display_handle_geometry(void *data,
1038 struct wl_output *output,
1039 int32_t width, int32_t height)
1041 struct display *display = data;
1043 display->screen_allocation.x = 0;
1044 display->screen_allocation.y = 0;
1045 display->screen_allocation.width = width;
1046 display->screen_allocation.height = height;
1049 static const struct wl_output_listener output_listener = {
1050 display_handle_geometry,
1054 display_add_input(struct display *d, uint32_t id)
1056 struct input *input;
1058 input = malloc(sizeof *input);
1062 memset(input, 0, sizeof *input);
1064 input->input_device = wl_input_device_create(d->display, id);
1065 input->pointer_focus = NULL;
1066 input->keyboard_focus = NULL;
1067 wl_list_insert(d->input_list.prev, &input->link);
1069 wl_input_device_add_listener(input->input_device,
1070 &input_device_listener, input);
1071 wl_input_device_set_user_data(input->input_device, input);
1075 display_handle_global(struct wl_display *display, uint32_t id,
1076 const char *interface, uint32_t version, void *data)
1078 struct display *d = data;
1079 struct wl_drag_offer *offer;
1081 if (strcmp(interface, "compositor") == 0) {
1082 d->compositor = wl_compositor_create(display, id);
1083 } else if (strcmp(interface, "output") == 0) {
1084 d->output = wl_output_create(display, id);
1085 wl_output_add_listener(d->output, &output_listener, d);
1086 } else if (strcmp(interface, "input_device") == 0) {
1087 display_add_input(d, id);
1088 } else if (strcmp(interface, "shell") == 0) {
1089 d->shell = wl_shell_create(display, id);
1090 wl_shell_add_listener(d->shell, &shell_listener, d);
1091 } else if (strcmp(interface, "drm") == 0) {
1092 d->drm = wl_drm_create(display, id);
1093 wl_drm_add_listener(d->drm, &drm_listener, d);
1094 } else if (strcmp(interface, "drag_offer") == 0) {
1095 offer = wl_drag_offer_create(display, id);
1096 d->drag_offer_handler(offer, d);
1100 static const char socket_name[] = "\0wayland";
1103 display_render_frame(struct display *d)
1108 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1109 cr = cairo_create(d->shadow);
1110 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1111 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1112 rounded_rect(cr, 16, 16, 112, 112, radius);
1115 blur_surface(d->shadow, 64);
1118 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1119 cr = cairo_create(d->active_frame);
1120 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1121 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1122 rounded_rect(cr, 16, 16, 112, 112, radius);
1127 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1128 cr = cairo_create(d->inactive_frame);
1129 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1130 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1131 rounded_rect(cr, 16, 16, 112, 112, radius);
1137 init_xkb(struct display *d)
1139 struct xkb_rule_names names;
1141 names.rules = "evdev";
1142 names.model = "pc105";
1143 names.layout = option_xkb_layout;
1144 names.variant = option_xkb_variant;
1145 names.options = option_xkb_options;
1147 d->xkb = xkb_compile_keymap_from_rules(&names);
1149 fprintf(stderr, "Failed to compile keymap\n");
1155 display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
1158 EGLint major, minor;
1160 GOptionContext *context;
1161 GOptionGroup *xkb_option_group;
1167 context = g_option_context_new(NULL);
1169 g_option_context_add_main_entries(context, option_entries, "Wayland View");
1171 xkb_option_group = g_option_group_new("xkb",
1173 "Show all XKB options",
1175 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1176 g_option_context_add_group (context, xkb_option_group);
1178 if (!g_option_context_parse(context, argc, argv, &error)) {
1179 fprintf(stderr, "option parsing failed: %s\n", error->message);
1184 d = malloc(sizeof *d);
1188 d->display = wl_display_create(socket_name, sizeof socket_name);
1189 if (d->display == NULL) {
1190 fprintf(stderr, "failed to create display: %m\n");
1194 wl_list_init(&d->input_list);
1196 /* Set up listener so we'll catch all events. */
1197 wl_display_add_global_listener(d->display,
1198 display_handle_global, d);
1200 /* Process connection events. */
1201 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1203 fd = open(d->device_name, O_RDWR);
1205 fprintf(stderr, "drm open failed: %m\n");
1209 if (drmGetMagic(fd, &magic)) {
1210 fprintf(stderr, "DRI2: failed to get drm magic");
1214 /* Wait for authenticated event */
1215 wl_drm_authenticate(d->drm, magic);
1216 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1217 while (!d->authenticated)
1218 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1220 d->dpy = eglGetDRMDisplayMESA(fd);
1221 if (!eglInitialize(d->dpy, &major, &minor)) {
1222 fprintf(stderr, "failed to initialize display\n");
1226 eglBindAPI(EGL_OPENGL_API);
1228 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1229 if (d->ctx == NULL) {
1230 fprintf(stderr, "failed to create context\n");
1234 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1235 fprintf(stderr, "faile to make context current\n");
1239 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1240 if (d->device == NULL) {
1241 fprintf(stderr, "failed to get cairo drm device\n");
1245 create_pointer_surfaces(d);
1247 display_render_frame(d);
1249 d->loop = g_main_loop_new(NULL, FALSE);
1250 d->source = wl_glib_source_new(d->display);
1251 g_source_attach(d->source, NULL);
1253 wl_list_init(&d->window_list);
1261 display_get_display(struct display *display)
1263 return display->display;
1266 struct wl_compositor *
1267 display_get_compositor(struct display *display)
1269 return display->compositor;
1273 display_get_egl_display(struct display *d)
1279 display_run(struct display *d)
1281 g_main_loop_run(d->loop);
1285 display_set_drag_offer_handler(struct display *display,
1286 display_drag_offer_handler_t handler)
1288 display->drag_offer_handler = handler;