2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 #include <linux/input.h>
34 #include <xf86drmMode.h>
35 #include <drm_fourcc.h>
38 #include <libbacklight.h>
40 #include "compositor.h"
42 #include "launcher-util.h"
46 WESTON_PLANE_DRM_CURSOR = 0x100
49 struct drm_compositor {
50 struct weston_compositor base;
53 struct wl_event_source *drm_source;
55 struct udev_monitor *udev_monitor;
56 struct wl_event_source *udev_drm_source;
62 struct gbm_device *gbm;
65 uint32_t crtc_allocator;
66 uint32_t connector_allocator;
69 struct gbm_surface *dummy_surface;
70 EGLSurface dummy_egl_surface;
72 struct wl_list sprite_list;
73 int sprites_are_broken;
79 struct weston_mode base;
80 drmModeModeInfo mode_info;
87 struct drm_output *output;
90 struct wl_buffer *buffer;
91 struct wl_listener buffer_destroy_listener;
95 struct weston_output base;
98 uint32_t connector_id;
99 drmModeCrtcPtr original_crtc;
101 struct gbm_surface *surface;
102 struct gbm_bo *cursor_bo[2];
104 EGLSurface egl_surface;
105 struct drm_fb *current, *next;
106 struct backlight *backlight;
110 * An output has a primary display plane plus zero or more sprites for
111 * blending display contents.
117 uint32_t pending_fb_id;
118 struct weston_surface *surface;
119 struct weston_surface *pending_surface;
121 struct drm_compositor *compositor;
123 struct wl_listener destroy_listener;
124 struct wl_listener pending_destroy_listener;
126 uint32_t possible_crtcs;
128 uint32_t count_formats;
130 int32_t src_x, src_y;
131 uint32_t src_w, src_h;
132 uint32_t dest_x, dest_y;
133 uint32_t dest_w, dest_h;
139 surface_is_primary(struct weston_compositor *ec, struct weston_surface *es)
141 struct weston_surface *primary;
143 primary = container_of(ec->surface_list.next, struct weston_surface,
151 drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
153 struct weston_compositor *ec = output_base->compositor;
154 struct drm_compositor *c =(struct drm_compositor *) ec;
155 struct drm_output *output = (struct drm_output *) output_base;
158 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
159 if (c->crtcs[crtc] != output->crtc_id)
162 if (supported & (1 << crtc))
170 drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
172 struct drm_fb *fb = data;
173 struct gbm_device *gbm = gbm_bo_get_device(bo);
176 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
179 weston_buffer_post_release(fb->buffer);
180 wl_list_remove(&fb->buffer_destroy_listener.link);
186 static struct drm_fb *
187 drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_output *output)
189 struct drm_fb *fb = gbm_bo_get_user_data(bo);
190 struct drm_compositor *compositor =
191 (struct drm_compositor *) output->base.compositor;
192 uint32_t width, height, stride, handle;
198 fb = malloc(sizeof *fb);
202 fb->is_client_buffer = 0;
205 width = gbm_bo_get_width(bo);
206 height = gbm_bo_get_height(bo);
207 stride = gbm_bo_get_pitch(bo);
208 handle = gbm_bo_get_handle(bo).u32;
210 ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32,
211 stride, handle, &fb->fb_id);
213 weston_log("failed to create kms fb: %m\n");
218 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
224 fb_handle_buffer_destroy(struct wl_listener *listener, void *data)
226 struct drm_fb *fb = container_of(listener, struct drm_fb,
227 buffer_destroy_listener);
231 if (fb == fb->output->next ||
232 (fb == fb->output->current && !fb->output->next))
233 weston_output_schedule_repaint(&fb->output->base);
237 drm_output_prepare_scanout_surface(struct drm_output *output)
239 struct drm_compositor *c =
240 (struct drm_compositor *) output->base.compositor;
241 struct weston_surface *es;
244 es = container_of(c->base.surface_list.next,
245 struct weston_surface, link);
247 if (es->geometry.x != output->base.x ||
248 es->geometry.y != output->base.y ||
249 es->geometry.width != output->base.current->width ||
250 es->geometry.height != output->base.current->height ||
251 es->transform.enabled ||
252 es->image == EGL_NO_IMAGE_KHR)
255 bo = gbm_bo_create_from_egl_image(c->gbm,
262 /* Need to verify output->region contained in surface opaque
263 * region. Or maybe just that format doesn't have alpha.
264 * For now, scanout only if format is XRGB8888. */
265 if (gbm_bo_get_format(bo) != GBM_FORMAT_XRGB8888) {
270 output->next = drm_fb_get_from_bo(bo, output);
276 output->next->is_client_buffer = 1;
277 output->next->buffer = es->buffer;
278 output->next->buffer->busy_count++;
279 output->next->buffer_destroy_listener.notify = fb_handle_buffer_destroy;
281 wl_signal_add(&output->next->buffer->resource.destroy_signal,
282 &output->next->buffer_destroy_listener);
284 pixman_region32_fini(&es->damage);
285 pixman_region32_init(&es->damage);
291 drm_output_render(struct drm_output *output, pixman_region32_t *damage)
293 struct drm_compositor *compositor =
294 (struct drm_compositor *) output->base.compositor;
295 struct weston_surface *surface;
298 if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface,
300 compositor->base.egl_context)) {
301 weston_log("failed to make current\n");
305 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
306 weston_surface_draw(surface, &output->base, damage);
308 wl_signal_emit(&output->base.frame_signal, output);
310 eglSwapBuffers(compositor->base.egl_display, output->egl_surface);
311 bo = gbm_surface_lock_front_buffer(output->surface);
313 weston_log("failed to lock front buffer: %m\n");
317 output->next = drm_fb_get_from_bo(bo, output);
319 weston_log("failed to get drm_fb for bo\n");
320 gbm_surface_release_buffer(output->surface, bo);
326 drm_output_repaint(struct weston_output *output_base,
327 pixman_region32_t *damage)
329 struct drm_output *output = (struct drm_output *) output_base;
330 struct drm_compositor *compositor =
331 (struct drm_compositor *) output->base.compositor;
332 struct drm_sprite *s;
333 struct drm_mode *mode;
336 drm_output_prepare_scanout_surface(output);
338 drm_output_render(output, damage);
342 mode = container_of(output->base.current, struct drm_mode, base);
343 if (!output->current) {
344 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
345 output->next->fb_id, 0, 0,
346 &output->connector_id, 1,
349 weston_log("set mode failed: %m\n");
354 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
356 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
357 weston_log("queueing pageflip failed: %m\n");
362 * Now, update all the sprite surfaces
364 wl_list_for_each(s, &compositor->sprite_list, link) {
367 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
368 .request.sequence = 1,
371 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
374 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
375 output->crtc_id, s->pending_fb_id, flags,
376 s->dest_x, s->dest_y,
377 s->dest_w, s->dest_h,
381 weston_log("setplane failed: %d: %s\n",
382 ret, strerror(errno));
385 * Queue a vblank signal so we know when the surface
386 * becomes active on the display or has been replaced.
388 vbl.request.signal = (unsigned long)s;
389 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
391 weston_log("vblank event request failed: %d: %s\n",
392 ret, strerror(errno));
400 vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
403 struct drm_sprite *s = (struct drm_sprite *)data;
404 struct drm_compositor *c = s->compositor;
407 weston_buffer_post_release(s->surface->buffer);
408 wl_list_remove(&s->destroy_listener.link);
410 drmModeRmFB(c->drm.fd, s->fb_id);
414 if (s->pending_surface) {
415 wl_list_remove(&s->pending_destroy_listener.link);
416 wl_signal_add(&s->pending_surface->buffer->resource.destroy_signal,
417 &s->destroy_listener);
418 s->surface = s->pending_surface;
419 s->pending_surface = NULL;
420 s->fb_id = s->pending_fb_id;
421 s->pending_fb_id = 0;
426 page_flip_handler(int fd, unsigned int frame,
427 unsigned int sec, unsigned int usec, void *data)
429 struct drm_output *output = (struct drm_output *) data;
432 if (output->current) {
433 if (output->current->is_client_buffer)
434 gbm_bo_destroy(output->current->bo);
436 gbm_surface_release_buffer(output->surface,
437 output->current->bo);
440 output->current = output->next;
443 msecs = sec * 1000 + usec / 1000;
444 weston_output_finish_frame(&output->base, msecs);
448 drm_surface_format_supported(struct drm_sprite *s, uint32_t format)
452 for (i = 0; i < s->count_formats; i++)
453 if (s->formats[i] == format)
460 drm_surface_transform_supported(struct weston_surface *es)
462 if (es->transform.enabled)
469 drm_surface_overlap_supported(struct weston_output *output_base,
470 pixman_region32_t *overlap)
472 /* We could potentially use a color key here if the surface left
473 * to display has rectangular regions
475 if (pixman_region32_not_empty(overlap))
481 drm_disable_unused_sprites(struct weston_output *output_base)
483 struct weston_compositor *ec = output_base->compositor;
484 struct drm_compositor *c =(struct drm_compositor *) ec;
485 struct drm_output *output = (struct drm_output *) output_base;
486 struct drm_sprite *s;
489 wl_list_for_each(s, &c->sprite_list, link) {
490 if (s->pending_fb_id)
493 ret = drmModeSetPlane(c->drm.fd, s->plane_id,
494 output->crtc_id, 0, 0,
495 0, 0, 0, 0, 0, 0, 0, 0);
497 weston_log("failed to disable plane: %d: %s\n",
498 ret, strerror(errno));
499 drmModeRmFB(c->drm.fd, s->fb_id);
501 s->pending_surface = NULL;
503 s->pending_fb_id = 0;
508 * This function must take care to damage any previously assigned surface
509 * if the sprite ends up binding to a different surface than in the
513 drm_output_prepare_overlay_surface(struct weston_output *output_base,
514 struct weston_surface *es,
515 pixman_region32_t *overlap)
517 struct weston_compositor *ec = output_base->compositor;
518 struct drm_compositor *c =(struct drm_compositor *) ec;
519 struct drm_sprite *s;
521 EGLint handle, stride;
524 uint32_t handles[4], pitches[4], offsets[4];
526 pixman_region32_t dest_rect, src_rect;
530 if (c->sprites_are_broken)
533 if (es->output_mask != (1u << output_base->id))
536 if (surface_is_primary(ec, es))
539 if (es->image == EGL_NO_IMAGE_KHR)
542 if (!drm_surface_transform_supported(es))
545 if (!drm_surface_overlap_supported(output_base, overlap))
548 wl_list_for_each(s, &c->sprite_list, link) {
549 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
552 if (!s->pending_fb_id) {
558 /* No sprites available */
562 bo = gbm_bo_create_from_egl_image(c->gbm, c->base.egl_display,
563 es->image, es->geometry.width,
566 format = gbm_bo_get_format(bo);
567 handle = gbm_bo_get_handle(bo).s32;
568 stride = gbm_bo_get_pitch(bo);
572 if (!drm_surface_format_supported(s, format))
582 ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height,
583 format, handles, pitches, offsets,
586 weston_log("addfb2 failed: %d\n", ret);
587 c->sprites_are_broken = 1;
591 if (s->surface && s->surface != es) {
592 struct weston_surface *old_surf = s->surface;
593 pixman_region32_fini(&old_surf->damage);
594 pixman_region32_init_rect(&old_surf->damage,
595 old_surf->geometry.x, old_surf->geometry.y,
596 old_surf->geometry.width, old_surf->geometry.height);
599 s->pending_fb_id = fb_id;
600 s->pending_surface = es;
601 es->buffer->busy_count++;
604 * Calculate the source & dest rects properly based on actual
605 * postion (note the caller has called weston_surface_update_transform()
608 pixman_region32_init(&dest_rect);
609 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
610 &output_base->region);
611 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
612 box = pixman_region32_extents(&dest_rect);
615 s->dest_w = box->x2 - box->x1;
616 s->dest_h = box->y2 - box->y1;
617 pixman_region32_fini(&dest_rect);
619 pixman_region32_init(&src_rect);
620 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
621 &output_base->region);
622 pixman_region32_translate(&src_rect, -es->geometry.x, -es->geometry.y);
623 box = pixman_region32_extents(&src_rect);
624 s->src_x = box->x1 << 16;
625 s->src_y = box->y1 << 16;
626 s->src_w = (box->x2 - box->x1) << 16;
627 s->src_h = (box->y2 - box->y1) << 16;
628 pixman_region32_fini(&src_rect);
630 wl_signal_add(&es->buffer->resource.destroy_signal,
631 &s->pending_destroy_listener);
636 drm_output_set_cursor(struct weston_output *output_base,
637 struct weston_seat *es);
640 weston_output_set_cursor(struct weston_output *output,
641 struct weston_seat *seat,
642 pixman_region32_t *overlap)
644 pixman_region32_t cursor_region;
646 if (seat->sprite == NULL)
649 pixman_region32_init(&cursor_region);
650 pixman_region32_intersect(&cursor_region,
651 &seat->sprite->transform.boundingbox,
654 if (!pixman_region32_not_empty(&cursor_region)) {
655 drm_output_set_cursor(output, NULL);
659 if (pixman_region32_not_empty(overlap) ||
660 drm_output_set_cursor(output, seat) < 0) {
661 if (seat->sprite->plane == WESTON_PLANE_DRM_CURSOR) {
662 weston_surface_damage(seat->sprite);
663 drm_output_set_cursor(output, NULL);
665 seat->sprite->plane = WESTON_PLANE_PRIMARY;
667 if (seat->sprite->plane == WESTON_PLANE_PRIMARY)
668 weston_surface_damage_below(seat->sprite);
669 wl_list_remove(&seat->sprite->link);
670 seat->sprite->plane = WESTON_PLANE_DRM_CURSOR;
674 pixman_region32_fini(&cursor_region);
678 drm_assign_planes(struct weston_output *output)
680 struct weston_compositor *ec = output->compositor;
681 struct weston_surface *es, *next;
682 pixman_region32_t overlap, surface_overlap;
683 struct weston_seat *seat;
686 * Find a surface for each sprite in the output using some heuristics:
688 * 2) frequency of update
689 * 3) opacity (though some hw might support alpha blending)
690 * 4) clipping (this can be fixed with color keys)
692 * The idea is to save on blitting since this should save power.
693 * If we can get a large video surface on the sprite for example,
694 * the main display surface may not need to update at all, and
695 * the client buffer can be used directly for the sprite surface
696 * as we do for flipping full screen surfaces.
698 seat = (struct weston_seat *) ec->seat;
699 pixman_region32_init(&overlap);
700 wl_list_for_each_safe(es, next, &ec->surface_list, link) {
702 * FIXME: try to assign hw cursors here too, they're just
705 pixman_region32_init(&surface_overlap);
706 pixman_region32_intersect(&surface_overlap, &overlap,
707 &es->transform.boundingbox);
709 if (es == seat->sprite) {
710 weston_output_set_cursor(output, seat,
713 if (seat->sprite->plane == WESTON_PLANE_PRIMARY)
714 pixman_region32_union(&overlap, &overlap,
715 &es->transform.boundingbox);
716 } else if (!drm_output_prepare_overlay_surface(output, es,
718 pixman_region32_fini(&es->damage);
719 pixman_region32_init(&es->damage);
721 pixman_region32_union(&overlap, &overlap,
722 &es->transform.boundingbox);
724 pixman_region32_fini(&surface_overlap);
726 pixman_region32_fini(&overlap);
728 if (!seat->sprite || !weston_surface_is_mapped(seat->sprite))
729 drm_output_set_cursor(output, NULL);
731 drm_disable_unused_sprites(output);
735 drm_output_set_cursor(struct weston_output *output_base,
736 struct weston_seat *es)
738 struct drm_output *output = (struct drm_output *) output_base;
739 struct drm_compositor *c =
740 (struct drm_compositor *) output->base.compositor;
741 EGLint handle, stride;
744 uint32_t buf[64 * 64];
745 unsigned char *d, *s, *end;
748 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
752 if (es->sprite->buffer == NULL ||
753 !wl_buffer_is_shm(es->sprite->buffer))
756 if (es->sprite->geometry.width > 64 ||
757 es->sprite->geometry.height > 64)
760 output->current_cursor ^= 1;
761 bo = output->cursor_bo[output->current_cursor];
765 memset(buf, 0, sizeof buf);
766 d = (unsigned char *) buf;
767 stride = wl_shm_buffer_get_stride(es->sprite->buffer);
768 s = wl_shm_buffer_get_data(es->sprite->buffer);
769 end = s + stride * es->sprite->geometry.height;
771 memcpy(d, s, es->sprite->geometry.width * 4);
776 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
779 handle = gbm_bo_get_handle(bo).s32;
780 ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64);
782 weston_log("failed to set cursor: %s\n", strerror(-ret));
786 ret = drmModeMoveCursor(c->drm.fd, output->crtc_id,
787 es->sprite->geometry.x - output->base.x,
788 es->sprite->geometry.y - output->base.y);
790 weston_log("failed to move cursor: %s\n", strerror(-ret));
796 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
801 drm_output_destroy(struct weston_output *output_base)
803 struct drm_output *output = (struct drm_output *) output_base;
804 struct drm_compositor *c =
805 (struct drm_compositor *) output->base.compositor;
806 drmModeCrtcPtr origcrtc = output->original_crtc;
808 if (output->backlight)
809 backlight_destroy(output->backlight);
811 /* Turn off hardware cursor */
812 drm_output_set_cursor(&output->base, NULL);
814 /* Restore original CRTC state */
815 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
816 origcrtc->x, origcrtc->y,
817 &output->connector_id, 1, &origcrtc->mode);
818 drmModeFreeCrtc(origcrtc);
820 c->crtc_allocator &= ~(1 << output->crtc_id);
821 c->connector_allocator &= ~(1 << output->connector_id);
823 eglDestroySurface(c->base.egl_display, output->egl_surface);
824 gbm_surface_destroy(output->surface);
826 weston_output_destroy(&output->base);
827 wl_list_remove(&output->base.link);
832 static struct drm_mode *
833 choose_mode (struct drm_output *output, struct weston_mode *target_mode)
835 struct drm_mode *tmp_mode = NULL, *mode;
837 if (output->base.current->width == target_mode->width &&
838 output->base.current->height == target_mode->height &&
839 (output->base.current->refresh == target_mode->refresh ||
840 target_mode->refresh == 0))
841 return (struct drm_mode *)output->base.current;
843 wl_list_for_each(mode, &output->base.mode_list, base.link) {
844 if (mode->mode_info.hdisplay == target_mode->width &&
845 mode->mode_info.vdisplay == target_mode->height) {
846 if (mode->mode_info.vrefresh == target_mode->refresh ||
847 target_mode->refresh == 0) {
849 } else if (!tmp_mode)
858 drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
860 struct drm_output *output;
861 struct drm_mode *drm_mode;
863 struct drm_compositor *ec;
864 struct gbm_surface *surface;
865 EGLSurface egl_surface;
867 if (output_base == NULL) {
868 weston_log("output is NULL.\n");
873 weston_log("mode is NULL.\n");
877 ec = (struct drm_compositor *)output_base->compositor;
878 output = (struct drm_output *)output_base;
879 drm_mode = choose_mode (output, mode);
882 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
884 } else if (&drm_mode->base == output->base.current) {
886 } else if (drm_mode->base.width == output->base.current->width &&
887 drm_mode->base.height == output->base.current->height) {
888 /* only change refresh value */
889 ret = drmModeSetCrtc(ec->drm.fd,
891 output->current->fb_id, 0, 0,
892 &output->connector_id, 1, &drm_mode->mode_info);
895 weston_log("failed to set mode (%dx%d) %u Hz\n",
896 drm_mode->base.width,
897 drm_mode->base.height,
898 drm_mode->base.refresh / 1000);
901 output->base.current->flags = 0;
902 output->base.current = &drm_mode->base;
903 drm_mode->base.flags =
904 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
911 drm_mode->base.flags =
912 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
914 surface = gbm_surface_create(ec->gbm,
915 drm_mode->base.width,
916 drm_mode->base.height,
919 GBM_BO_USE_RENDERING);
921 weston_log("failed to create gbm surface\n");
926 eglCreateWindowSurface(ec->base.egl_display,
930 if (egl_surface == EGL_NO_SURFACE) {
931 weston_log("failed to create egl surface\n");
935 ret = drmModeSetCrtc(ec->drm.fd,
937 output->current->fb_id, 0, 0,
938 &output->connector_id, 1, &drm_mode->mode_info);
940 weston_log("failed to set mode\n");
944 /* reset rendering stuff. */
945 if (output->current) {
946 if (output->current->is_client_buffer)
947 gbm_bo_destroy(output->current->bo);
949 gbm_surface_release_buffer(output->surface,
950 output->current->bo);
952 output->current = NULL;
955 if (output->next->is_client_buffer)
956 gbm_bo_destroy(output->next->bo);
958 gbm_surface_release_buffer(output->surface,
963 eglDestroySurface(ec->base.egl_display, output->egl_surface);
964 gbm_surface_destroy(output->surface);
965 output->egl_surface = egl_surface;
966 output->surface = surface;
969 output->base.current = &drm_mode->base;
970 output->base.dirty = 1;
971 weston_output_move(&output->base, output->base.x, output->base.y);
975 eglDestroySurface(ec->base.egl_display, egl_surface);
976 gbm_surface_destroy(surface);
981 on_drm_input(int fd, uint32_t mask, void *data)
983 drmEventContext evctx;
985 memset(&evctx, 0, sizeof evctx);
986 evctx.version = DRM_EVENT_CONTEXT_VERSION;
987 evctx.page_flip_handler = page_flip_handler;
988 evctx.vblank_handler = vblank_handler;
989 drmHandleEvent(fd, &evctx);
995 init_egl(struct drm_compositor *ec, struct udev_device *device)
997 EGLint major, minor, n;
998 const char *filename, *sysnum;
1000 static const EGLint context_attribs[] = {
1001 EGL_CONTEXT_CLIENT_VERSION, 2,
1005 static const EGLint config_attribs[] = {
1006 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1011 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1015 sysnum = udev_device_get_sysnum(device);
1017 ec->drm.id = atoi(sysnum);
1018 if (!sysnum || ec->drm.id < 0) {
1019 weston_log("cannot get device sysnum\n");
1023 filename = udev_device_get_devnode(device);
1024 fd = open(filename, O_RDWR | O_CLOEXEC);
1026 /* Probably permissions error */
1027 weston_log("couldn't open %s, skipping\n",
1028 udev_device_get_devnode(device));
1032 weston_log("using %s\n", filename);
1035 ec->gbm = gbm_create_device(ec->drm.fd);
1036 ec->base.egl_display = eglGetDisplay(ec->gbm);
1037 if (ec->base.egl_display == NULL) {
1038 weston_log("failed to create display\n");
1042 if (!eglInitialize(ec->base.egl_display, &major, &minor)) {
1043 weston_log("failed to initialize display\n");
1047 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1048 weston_log("failed to bind api EGL_OPENGL_ES_API\n");
1052 if (!eglChooseConfig(ec->base.egl_display, config_attribs,
1053 &ec->base.egl_config, 1, &n) || n != 1) {
1054 weston_log("failed to choose config: %d\n", n);
1058 ec->base.egl_context =
1059 eglCreateContext(ec->base.egl_display, ec->base.egl_config,
1060 EGL_NO_CONTEXT, context_attribs);
1061 if (ec->base.egl_context == NULL) {
1062 weston_log("failed to create context\n");
1066 ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10,
1067 GBM_FORMAT_XRGB8888,
1068 GBM_BO_USE_RENDERING);
1069 if (!ec->dummy_surface) {
1070 weston_log("failed to create dummy gbm surface\n");
1074 ec->dummy_egl_surface =
1075 eglCreateWindowSurface(ec->base.egl_display,
1076 ec->base.egl_config,
1079 if (ec->dummy_egl_surface == EGL_NO_SURFACE) {
1080 weston_log("failed to create egl surface\n");
1084 if (!eglMakeCurrent(ec->base.egl_display, ec->dummy_egl_surface,
1085 ec->dummy_egl_surface, ec->base.egl_context)) {
1086 weston_log("failed to make context current\n");
1093 static drmModeModeInfo builtin_1024x768 = {
1095 1024, 1072, 1176, 1328, 0,
1096 768, 771, 775, 798, 0,
1098 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
1105 drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1107 struct drm_mode *mode;
1110 mode = malloc(sizeof *mode);
1114 mode->base.flags = 0;
1115 mode->base.width = info->hdisplay;
1116 mode->base.height = info->vdisplay;
1118 /* Calculate higher precision (mHz) refresh rate */
1119 refresh = (info->clock * 1000000LL / info->htotal +
1120 info->vtotal / 2) / info->vtotal;
1122 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1124 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1126 if (info->vscan > 1)
1127 refresh /= info->vscan;
1129 mode->base.refresh = refresh;
1130 mode->mode_info = *info;
1131 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1137 drm_subpixel_to_wayland(int drm_value)
1139 switch (drm_value) {
1141 case DRM_MODE_SUBPIXEL_UNKNOWN:
1142 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1143 case DRM_MODE_SUBPIXEL_NONE:
1144 return WL_OUTPUT_SUBPIXEL_NONE;
1145 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1146 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1147 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1148 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1149 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1150 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1151 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1152 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1157 sprite_handle_buffer_destroy(struct wl_listener *listener, void *data)
1159 struct drm_sprite *sprite =
1160 container_of(listener, struct drm_sprite,
1163 sprite->surface = NULL;
1167 sprite_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
1169 struct drm_sprite *sprite =
1170 container_of(listener, struct drm_sprite,
1171 pending_destroy_listener);
1173 sprite->pending_surface = NULL;
1176 /* returns a value between 0-255 range, where higher is brighter */
1178 drm_get_backlight(struct drm_output *output)
1180 long brightness, max_brightness, norm;
1182 brightness = backlight_get_brightness(output->backlight);
1183 max_brightness = backlight_get_max_brightness(output->backlight);
1185 /* convert it on a scale of 0 to 255 */
1186 norm = (brightness * 255)/(max_brightness);
1188 return (uint32_t) norm;
1191 /* values accepted are between 0-255 range */
1193 drm_set_backlight(struct weston_output *output_base, uint32_t value)
1195 struct drm_output *output = (struct drm_output *) output_base;
1196 long max_brightness, new_brightness;
1198 if (!output->backlight)
1204 max_brightness = backlight_get_max_brightness(output->backlight);
1206 /* get denormalized value */
1207 new_brightness = (value * max_brightness) / 255;
1209 backlight_set_brightness(output->backlight, new_brightness);
1212 static drmModePropertyPtr
1213 drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1215 drmModePropertyPtr props;
1218 for (i = 0; i < connector->count_props; i++) {
1219 props = drmModeGetProperty(fd, connector->props[i]);
1223 if (!strcmp(props->name, name))
1226 drmModeFreeProperty(props);
1233 drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1235 struct drm_output *output = (struct drm_output *) output_base;
1236 struct weston_compositor *ec = output_base->compositor;
1237 struct drm_compositor *c = (struct drm_compositor *) ec;
1238 drmModeConnectorPtr connector;
1239 drmModePropertyPtr prop;
1241 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
1245 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1247 drmModeFreeConnector(connector);
1251 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1252 prop->prop_id, level);
1253 drmModeFreeProperty(prop);
1254 drmModeFreeConnector(connector);
1258 create_output_for_connector(struct drm_compositor *ec,
1259 drmModeRes *resources,
1260 drmModeConnector *connector,
1261 int x, int y, struct udev_device *drm_device)
1263 struct drm_output *output;
1264 struct drm_mode *drm_mode, *next;
1265 drmModeEncoder *encoder;
1268 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
1269 if (encoder == NULL) {
1270 weston_log("No encoder for connector.\n");
1274 for (i = 0; i < resources->count_crtcs; i++) {
1275 if (encoder->possible_crtcs & (1 << i) &&
1276 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
1279 if (i == resources->count_crtcs) {
1280 weston_log("No usable crtc for encoder.\n");
1281 drmModeFreeEncoder(encoder);
1285 output = malloc(sizeof *output);
1286 if (output == NULL) {
1287 drmModeFreeEncoder(encoder);
1291 memset(output, 0, sizeof *output);
1292 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1293 output->base.make = "unknown";
1294 output->base.model = "unknown";
1295 wl_list_init(&output->base.mode_list);
1297 output->crtc_id = resources->crtcs[i];
1298 ec->crtc_allocator |= (1 << output->crtc_id);
1299 output->connector_id = connector->connector_id;
1300 ec->connector_allocator |= (1 << output->connector_id);
1302 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
1303 drmModeFreeEncoder(encoder);
1305 for (i = 0; i < connector->count_modes; i++) {
1306 ret = drm_output_add_mode(output, &connector->modes[i]);
1311 if (connector->count_modes == 0) {
1312 ret = drm_output_add_mode(output, &builtin_1024x768);
1317 drm_mode = container_of(output->base.mode_list.next,
1318 struct drm_mode, base.link);
1319 output->base.current = &drm_mode->base;
1320 drm_mode->base.flags =
1321 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1323 output->surface = gbm_surface_create(ec->gbm,
1324 output->base.current->width,
1325 output->base.current->height,
1326 GBM_FORMAT_XRGB8888,
1327 GBM_BO_USE_SCANOUT |
1328 GBM_BO_USE_RENDERING);
1329 if (!output->surface) {
1330 weston_log("failed to create gbm surface\n");
1334 output->egl_surface =
1335 eglCreateWindowSurface(ec->base.egl_display,
1336 ec->base.egl_config,
1339 if (output->egl_surface == EGL_NO_SURFACE) {
1340 weston_log("failed to create egl surface\n");
1344 output->cursor_bo[0] =
1345 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1346 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1347 output->cursor_bo[1] =
1348 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1349 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1351 output->backlight = backlight_init(drm_device,
1352 connector->connector_type);
1353 if (output->backlight) {
1354 output->base.set_backlight = drm_set_backlight;
1355 output->base.backlight_current = drm_get_backlight(output);
1358 weston_output_init(&output->base, &ec->base, x, y,
1359 connector->mmWidth, connector->mmHeight,
1362 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1364 output->base.origin = output->base.current;
1365 output->base.repaint = drm_output_repaint;
1366 output->base.destroy = drm_output_destroy;
1367 output->base.assign_planes = drm_assign_planes;
1368 output->base.set_dpms = drm_set_dpms;
1369 output->base.switch_mode = drm_output_switch_mode;
1371 weston_log("kms connector %d, crtc %d at mode %dx%d@%.1f\n",
1372 output->connector_id, output->crtc_id,
1373 output->base.current->width,
1374 output->base.current->height,
1375 output->base.current->refresh / 1000.0);
1380 gbm_surface_destroy(output->surface);
1382 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1384 wl_list_remove(&drm_mode->base.link);
1388 drmModeFreeCrtc(output->original_crtc);
1389 ec->crtc_allocator &= ~(1 << output->crtc_id);
1390 ec->connector_allocator &= ~(1 << output->connector_id);
1397 create_sprites(struct drm_compositor *ec)
1399 struct drm_sprite *sprite;
1400 drmModePlaneRes *plane_res;
1401 drmModePlane *plane;
1404 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1406 weston_log("failed to get plane resources: %s\n",
1411 for (i = 0; i < plane_res->count_planes; i++) {
1412 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1416 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1417 plane->count_formats));
1419 weston_log("%s: out of memory\n",
1425 memset(sprite, 0, sizeof *sprite);
1427 sprite->possible_crtcs = plane->possible_crtcs;
1428 sprite->plane_id = plane->plane_id;
1429 sprite->surface = NULL;
1430 sprite->pending_surface = NULL;
1432 sprite->pending_fb_id = 0;
1433 sprite->destroy_listener.notify = sprite_handle_buffer_destroy;
1434 sprite->pending_destroy_listener.notify =
1435 sprite_handle_pending_buffer_destroy;
1436 sprite->compositor = ec;
1437 sprite->count_formats = plane->count_formats;
1438 memcpy(sprite->formats, plane->formats,
1439 plane->count_formats * sizeof(plane->formats[0]));
1440 drmModeFreePlane(plane);
1442 wl_list_insert(&ec->sprite_list, &sprite->link);
1445 free(plane_res->planes);
1450 destroy_sprites(struct drm_compositor *compositor)
1452 struct drm_sprite *sprite, *next;
1453 struct drm_output *output;
1455 output = container_of(compositor->base.output_list.next,
1456 struct drm_output, base.link);
1458 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1459 drmModeSetPlane(compositor->drm.fd,
1461 output->crtc_id, 0, 0,
1462 0, 0, 0, 0, 0, 0, 0, 0);
1463 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1469 create_outputs(struct drm_compositor *ec, uint32_t option_connector,
1470 struct udev_device *drm_device)
1472 drmModeConnector *connector;
1473 drmModeRes *resources;
1477 resources = drmModeGetResources(ec->drm.fd);
1479 weston_log("drmModeGetResources failed\n");
1483 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
1485 drmModeFreeResources(resources);
1489 ec->num_crtcs = resources->count_crtcs;
1490 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1492 for (i = 0; i < resources->count_connectors; i++) {
1493 connector = drmModeGetConnector(ec->drm.fd,
1494 resources->connectors[i]);
1495 if (connector == NULL)
1498 if (connector->connection == DRM_MODE_CONNECTED &&
1499 (option_connector == 0 ||
1500 connector->connector_id == option_connector)) {
1501 if (create_output_for_connector(ec, resources,
1504 drmModeFreeConnector(connector);
1508 x += container_of(ec->base.output_list.prev,
1509 struct weston_output,
1510 link)->current->width;
1513 drmModeFreeConnector(connector);
1516 if (wl_list_empty(&ec->base.output_list)) {
1517 weston_log("No currently active connector found.\n");
1518 drmModeFreeResources(resources);
1522 drmModeFreeResources(resources);
1528 update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
1530 drmModeConnector *connector;
1531 drmModeRes *resources;
1532 struct drm_output *output, *next;
1534 int x_offset = 0, y_offset = 0;
1535 uint32_t connected = 0, disconnects = 0;
1538 resources = drmModeGetResources(ec->drm.fd);
1540 weston_log("drmModeGetResources failed\n");
1544 /* collect new connects */
1545 for (i = 0; i < resources->count_connectors; i++) {
1546 int connector_id = resources->connectors[i];
1548 connector = drmModeGetConnector(ec->drm.fd, connector_id);
1549 if (connector == NULL)
1552 if (connector->connection != DRM_MODE_CONNECTED) {
1553 drmModeFreeConnector(connector);
1557 connected |= (1 << connector_id);
1559 if (!(ec->connector_allocator & (1 << connector_id))) {
1560 struct weston_output *last =
1561 container_of(ec->base.output_list.prev,
1562 struct weston_output, link);
1564 /* XXX: not yet needed, we die with 0 outputs */
1565 if (!wl_list_empty(&ec->base.output_list))
1566 x = last->x + last->current->width;
1570 create_output_for_connector(ec, resources,
1573 weston_log("connector %d connected\n", connector_id);
1576 drmModeFreeConnector(connector);
1578 drmModeFreeResources(resources);
1580 disconnects = ec->connector_allocator & ~connected;
1582 wl_list_for_each_safe(output, next, &ec->base.output_list,
1584 if (x_offset != 0 || y_offset != 0) {
1585 weston_output_move(&output->base,
1586 output->base.x - x_offset,
1587 output->base.y - y_offset);
1590 if (disconnects & (1 << output->connector_id)) {
1591 disconnects &= ~(1 << output->connector_id);
1592 weston_log("connector %d disconnected\n",
1593 output->connector_id);
1594 x_offset += output->base.current->width;
1595 drm_output_destroy(&output->base);
1600 /* FIXME: handle zero outputs, without terminating */
1601 if (ec->connector_allocator == 0)
1602 wl_display_terminate(ec->base.wl_display);
1606 udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
1611 sysnum = udev_device_get_sysnum(device);
1612 if (!sysnum || atoi(sysnum) != ec->drm.id)
1615 val = udev_device_get_property_value(device, "HOTPLUG");
1619 return strcmp(val, "1") == 0;
1623 udev_drm_event(int fd, uint32_t mask, void *data)
1625 struct drm_compositor *ec = data;
1626 struct udev_device *event;
1628 event = udev_monitor_receive_device(ec->udev_monitor);
1630 if (udev_event_is_hotplug(ec, event))
1631 update_outputs(ec, event);
1633 udev_device_unref(event);
1639 drm_destroy(struct weston_compositor *ec)
1641 struct drm_compositor *d = (struct drm_compositor *) ec;
1642 struct weston_seat *seat, *next;
1644 wl_list_for_each_safe(seat, next, &ec->seat_list, link)
1645 evdev_input_destroy(seat);
1647 wl_event_source_remove(d->udev_drm_source);
1648 wl_event_source_remove(d->drm_source);
1650 weston_compositor_shutdown(ec);
1652 /* Work around crash in egl_dri2.c's dri2_make_current() */
1653 eglMakeCurrent(ec->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
1655 eglTerminate(ec->egl_display);
1658 gbm_device_destroy(d->gbm);
1660 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
1661 weston_log("failed to drop master: %m\n");
1662 tty_destroy(d->tty);
1668 drm_compositor_set_modes(struct drm_compositor *compositor)
1670 struct drm_output *output;
1671 struct drm_mode *drm_mode;
1674 wl_list_for_each(output, &compositor->base.output_list, base.link) {
1675 drm_mode = (struct drm_mode *) output->base.current;
1676 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
1677 output->current->fb_id, 0, 0,
1678 &output->connector_id, 1,
1679 &drm_mode->mode_info);
1682 "failed to set mode %dx%d for output at %d,%d: %m\n",
1683 drm_mode->base.width, drm_mode->base.height,
1684 output->base.x, output->base.y);
1690 vt_func(struct weston_compositor *compositor, int event)
1692 struct drm_compositor *ec = (struct drm_compositor *) compositor;
1693 struct weston_output *output;
1694 struct weston_seat *seat;
1695 struct drm_sprite *sprite;
1696 struct drm_output *drm_output;
1700 compositor->focus = 1;
1701 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) {
1702 weston_log("failed to set master: %m\n");
1703 wl_display_terminate(compositor->wl_display);
1705 compositor->state = ec->prev_state;
1706 drm_compositor_set_modes(ec);
1707 weston_compositor_damage_all(compositor);
1708 wl_list_for_each(seat, &compositor->seat_list, link) {
1709 evdev_add_devices(ec->udev, seat);
1710 evdev_enable_udev_monitor(ec->udev, seat);
1714 wl_list_for_each(seat, &compositor->seat_list, link) {
1715 evdev_disable_udev_monitor(seat);
1716 evdev_remove_devices(seat);
1719 compositor->focus = 0;
1720 ec->prev_state = compositor->state;
1721 compositor->state = WESTON_COMPOSITOR_SLEEPING;
1723 /* If we have a repaint scheduled (either from a
1724 * pending pageflip or the idle handler), make sure we
1725 * cancel that so we don't try to pageflip when we're
1726 * vt switched away. The SLEEPING state will prevent
1727 * further attemps at repainting. When we switch
1728 * back, we schedule a repaint, which will process
1729 * pending frame callbacks. */
1731 wl_list_for_each(output, &ec->base.output_list, link) {
1732 output->repaint_needed = 0;
1733 drm_output_set_cursor(output, NULL);
1736 drm_output = container_of(ec->base.output_list.next,
1737 struct drm_output, base.link);
1739 wl_list_for_each(sprite, &ec->sprite_list, link)
1740 drmModeSetPlane(ec->drm.fd,
1742 drm_output->crtc_id, 0, 0,
1743 0, 0, 0, 0, 0, 0, 0, 0);
1745 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
1746 weston_log("failed to drop master: %m\n");
1753 switch_vt_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
1755 struct drm_compositor *ec = data;
1757 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
1760 static const char default_seat[] = "seat0";
1762 static struct weston_compositor *
1763 drm_compositor_create(struct wl_display *display,
1764 int connector, const char *seat, int tty,
1765 int argc, char *argv[], const char *config_file)
1767 struct drm_compositor *ec;
1768 struct udev_enumerate *e;
1769 struct udev_list_entry *entry;
1770 struct udev_device *device, *drm_device;
1771 const char *path, *device_seat;
1772 struct wl_event_loop *loop;
1773 struct weston_seat *weston_seat, *next;
1776 weston_log("initializing drm backend\n");
1778 ec = malloc(sizeof *ec);
1781 memset(ec, 0, sizeof *ec);
1783 if (weston_compositor_init(&ec->base, display, argc, argv,
1785 weston_log("weston_compositor_init failed\n");
1789 ec->udev = udev_new();
1790 if (ec->udev == NULL) {
1791 weston_log("failed to initialize udev context\n");
1792 goto err_compositor;
1795 ec->base.wl_display = display;
1796 ec->tty = tty_create(&ec->base, vt_func, tty);
1798 weston_log("failed to initialize tty\n");
1802 e = udev_enumerate_new(ec->udev);
1803 udev_enumerate_add_match_subsystem(e, "drm");
1804 udev_enumerate_add_match_sysname(e, "card[0-9]*");
1806 udev_enumerate_scan_devices(e);
1808 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1809 path = udev_list_entry_get_name(entry);
1810 device = udev_device_new_from_syspath(ec->udev, path);
1812 udev_device_get_property_value(device, "ID_SEAT");
1814 device_seat = default_seat;
1815 if (strcmp(device_seat, seat) == 0) {
1816 drm_device = device;
1819 udev_device_unref(device);
1822 if (drm_device == NULL) {
1823 weston_log("no drm device found\n");
1827 if (init_egl(ec, drm_device) < 0) {
1828 weston_log("failed to initialize egl\n");
1832 ec->base.destroy = drm_destroy;
1836 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
1838 if (weston_compositor_init_gl(&ec->base) < 0)
1841 for (key = KEY_F1; key < KEY_F9; key++)
1842 weston_compositor_add_key_binding(&ec->base, key,
1843 MODIFIER_CTRL | MODIFIER_ALT,
1844 switch_vt_binding, ec);
1846 wl_list_init(&ec->sprite_list);
1849 if (create_outputs(ec, connector, drm_device) < 0) {
1850 weston_log("failed to create output for %s\n", path);
1856 evdev_input_create(&ec->base, ec->udev, seat);
1858 loop = wl_display_get_event_loop(ec->base.wl_display);
1860 wl_event_loop_add_fd(loop, ec->drm.fd,
1861 WL_EVENT_READABLE, on_drm_input, ec);
1863 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
1864 if (ec->udev_monitor == NULL) {
1865 weston_log("failed to intialize udev monitor\n");
1866 goto err_drm_source;
1868 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
1870 ec->udev_drm_source =
1871 wl_event_loop_add_fd(loop,
1872 udev_monitor_get_fd(ec->udev_monitor),
1873 WL_EVENT_READABLE, udev_drm_event, ec);
1875 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
1876 weston_log("failed to enable udev-monitor receiving\n");
1877 goto err_udev_monitor;
1880 udev_device_unref(drm_device);
1881 udev_enumerate_unref(e);
1886 wl_event_source_remove(ec->udev_drm_source);
1887 udev_monitor_unref(ec->udev_monitor);
1889 wl_event_source_remove(ec->drm_source);
1890 wl_list_for_each_safe(weston_seat, next, &ec->base.seat_list, link)
1891 evdev_input_destroy(weston_seat);
1893 destroy_sprites(ec);
1895 eglMakeCurrent(ec->base.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
1897 eglTerminate(ec->base.egl_display);
1899 gbm_device_destroy(ec->gbm);
1901 udev_device_unref(drm_device);
1903 udev_enumerate_unref(e);
1904 tty_destroy(ec->tty);
1906 udev_unref(ec->udev);
1908 weston_compositor_shutdown(&ec->base);
1914 WL_EXPORT struct weston_compositor *
1915 backend_init(struct wl_display *display, int argc, char *argv[],
1916 const char *config_file)
1918 int connector = 0, tty = 0;
1919 const char *seat = default_seat;
1921 const struct weston_option drm_options[] = {
1922 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
1923 { WESTON_OPTION_STRING, "seat", 0, &seat },
1924 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
1927 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
1929 return drm_compositor_create(display, connector, seat, tty, argc, argv,