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"
45 struct drm_compositor {
46 struct weston_compositor base;
49 struct wl_event_source *drm_source;
51 struct udev_monitor *udev_monitor;
52 struct wl_event_source *udev_drm_source;
58 struct gbm_device *gbm;
61 uint32_t crtc_allocator;
62 uint32_t connector_allocator;
65 struct gbm_surface *dummy_surface;
66 EGLSurface dummy_egl_surface;
68 struct wl_list sprite_list;
69 int sprites_are_broken;
75 struct weston_mode base;
76 drmModeModeInfo mode_info;
83 struct drm_output *output;
86 struct wl_buffer *buffer;
87 struct wl_listener buffer_destroy_listener;
91 struct weston_output base;
94 uint32_t connector_id;
95 drmModeCrtcPtr original_crtc;
97 struct gbm_surface *surface;
98 struct gbm_bo *cursor_bo[2];
100 EGLSurface egl_surface;
101 struct drm_fb *current, *next;
102 struct backlight *backlight;
106 * An output has a primary display plane plus zero or more sprites for
107 * blending display contents.
113 uint32_t pending_fb_id;
114 struct weston_surface *surface;
115 struct weston_surface *pending_surface;
117 struct drm_compositor *compositor;
119 struct wl_listener destroy_listener;
120 struct wl_listener pending_destroy_listener;
122 uint32_t possible_crtcs;
124 uint32_t count_formats;
126 int32_t src_x, src_y;
127 uint32_t src_w, src_h;
128 uint32_t dest_x, dest_y;
129 uint32_t dest_w, dest_h;
135 surface_is_primary(struct weston_compositor *ec, struct weston_surface *es)
137 struct weston_surface *primary;
139 primary = container_of(ec->surface_list.next, struct weston_surface,
147 drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported)
149 struct weston_compositor *ec = output_base->compositor;
150 struct drm_compositor *c =(struct drm_compositor *) ec;
151 struct drm_output *output = (struct drm_output *) output_base;
154 for (crtc = 0; crtc < c->num_crtcs; crtc++) {
155 if (c->crtcs[crtc] != output->crtc_id)
158 if (supported & (1 << crtc))
166 drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
168 struct drm_fb *fb = data;
169 struct gbm_device *gbm = gbm_bo_get_device(bo);
172 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
175 weston_buffer_post_release(fb->buffer);
176 wl_list_remove(&fb->buffer_destroy_listener.link);
182 static struct drm_fb *
183 drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_output *output)
185 struct drm_fb *fb = gbm_bo_get_user_data(bo);
186 struct drm_compositor *compositor =
187 (struct drm_compositor *) output->base.compositor;
188 uint32_t width, height, stride, handle;
194 fb = malloc(sizeof *fb);
198 fb->is_client_buffer = 0;
201 width = gbm_bo_get_width(bo);
202 height = gbm_bo_get_height(bo);
203 stride = gbm_bo_get_pitch(bo);
204 handle = gbm_bo_get_handle(bo).u32;
206 ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32,
207 stride, handle, &fb->fb_id);
209 weston_log("failed to create kms fb: %m\n");
214 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
220 fb_handle_buffer_destroy(struct wl_listener *listener, void *data)
222 struct drm_fb *fb = container_of(listener, struct drm_fb,
223 buffer_destroy_listener);
227 if (fb == fb->output->next ||
228 (fb == fb->output->current && !fb->output->next))
229 weston_compositor_schedule_repaint(fb->output->base.compositor);
233 drm_output_prepare_scanout_surface(struct drm_output *output)
235 struct drm_compositor *c =
236 (struct drm_compositor *) output->base.compositor;
237 struct weston_surface *es;
240 es = container_of(c->base.surface_list.next,
241 struct weston_surface, link);
243 if (es->geometry.x != output->base.x ||
244 es->geometry.y != output->base.y ||
245 es->geometry.width != output->base.current->width ||
246 es->geometry.height != output->base.current->height ||
247 es->transform.enabled ||
248 es->image == EGL_NO_IMAGE_KHR)
251 bo = gbm_bo_create_from_egl_image(c->gbm,
252 c->base.display, es->image,
257 /* Need to verify output->region contained in surface opaque
258 * region. Or maybe just that format doesn't have alpha.
259 * For now, scanout only if format is XRGB8888. */
260 if (gbm_bo_get_format(bo) != GBM_FORMAT_XRGB8888) {
265 output->next = drm_fb_get_from_bo(bo, output);
271 output->next->is_client_buffer = 1;
272 output->next->buffer = es->buffer;
273 output->next->buffer->busy_count++;
274 output->next->buffer_destroy_listener.notify = fb_handle_buffer_destroy;
276 wl_signal_add(&output->next->buffer->resource.destroy_signal,
277 &output->next->buffer_destroy_listener);
279 pixman_region32_fini(&es->damage);
280 pixman_region32_init(&es->damage);
286 drm_output_render(struct drm_output *output, pixman_region32_t *damage)
288 struct drm_compositor *compositor =
289 (struct drm_compositor *) output->base.compositor;
290 struct weston_surface *surface;
293 if (!eglMakeCurrent(compositor->base.display, output->egl_surface,
294 output->egl_surface, compositor->base.context)) {
295 weston_log("failed to make current\n");
299 wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
300 weston_surface_draw(surface, &output->base, damage);
302 weston_output_do_read_pixels(&output->base);
304 eglSwapBuffers(compositor->base.display, output->egl_surface);
305 bo = gbm_surface_lock_front_buffer(output->surface);
307 weston_log("failed to lock front buffer: %m\n");
311 output->next = drm_fb_get_from_bo(bo, output);
313 weston_log("failed to get drm_fb for bo\n");
314 gbm_surface_release_buffer(output->surface, bo);
320 drm_output_repaint(struct weston_output *output_base,
321 pixman_region32_t *damage)
323 struct drm_output *output = (struct drm_output *) output_base;
324 struct drm_compositor *compositor =
325 (struct drm_compositor *) output->base.compositor;
326 struct drm_sprite *s;
327 struct drm_mode *mode;
330 drm_output_prepare_scanout_surface(output);
332 drm_output_render(output, damage);
336 mode = container_of(output->base.current, struct drm_mode, base);
337 if (!output->current) {
338 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
339 output->next->fb_id, 0, 0,
340 &output->connector_id, 1,
343 weston_log("set mode failed: %m\n");
348 if (drmModePageFlip(compositor->drm.fd, output->crtc_id,
350 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
351 weston_log("queueing pageflip failed: %m\n");
356 * Now, update all the sprite surfaces
358 wl_list_for_each(s, &compositor->sprite_list, link) {
361 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
362 .request.sequence = 1,
365 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
368 ret = drmModeSetPlane(compositor->drm.fd, s->plane_id,
369 output->crtc_id, s->pending_fb_id, flags,
370 s->dest_x, s->dest_y,
371 s->dest_w, s->dest_h,
375 weston_log("setplane failed: %d: %s\n",
376 ret, strerror(errno));
379 * Queue a vblank signal so we know when the surface
380 * becomes active on the display or has been replaced.
382 vbl.request.signal = (unsigned long)s;
383 ret = drmWaitVBlank(compositor->drm.fd, &vbl);
385 weston_log("vblank event request failed: %d: %s\n",
386 ret, strerror(errno));
394 vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
397 struct drm_sprite *s = (struct drm_sprite *)data;
398 struct drm_compositor *c = s->compositor;
401 weston_buffer_post_release(s->surface->buffer);
402 wl_list_remove(&s->destroy_listener.link);
404 drmModeRmFB(c->drm.fd, s->fb_id);
408 if (s->pending_surface) {
409 wl_list_remove(&s->pending_destroy_listener.link);
410 wl_signal_add(&s->pending_surface->buffer->resource.destroy_signal,
411 &s->destroy_listener);
412 s->surface = s->pending_surface;
413 s->pending_surface = NULL;
414 s->fb_id = s->pending_fb_id;
415 s->pending_fb_id = 0;
420 page_flip_handler(int fd, unsigned int frame,
421 unsigned int sec, unsigned int usec, void *data)
423 struct drm_output *output = (struct drm_output *) data;
426 if (output->current) {
427 if (output->current->is_client_buffer)
428 gbm_bo_destroy(output->current->bo);
430 gbm_surface_release_buffer(output->surface,
431 output->current->bo);
434 output->current = output->next;
437 msecs = sec * 1000 + usec / 1000;
438 weston_output_finish_frame(&output->base, msecs);
442 drm_surface_format_supported(struct drm_sprite *s, uint32_t format)
446 for (i = 0; i < s->count_formats; i++)
447 if (s->formats[i] == format)
454 drm_surface_transform_supported(struct weston_surface *es)
456 if (es->transform.enabled)
463 drm_surface_overlap_supported(struct weston_output *output_base,
464 pixman_region32_t *overlap)
466 /* We could potentially use a color key here if the surface left
467 * to display has rectangular regions
469 if (pixman_region32_not_empty(overlap))
475 drm_disable_unused_sprites(struct weston_output *output_base)
477 struct weston_compositor *ec = output_base->compositor;
478 struct drm_compositor *c =(struct drm_compositor *) ec;
479 struct drm_output *output = (struct drm_output *) output_base;
480 struct drm_sprite *s;
483 wl_list_for_each(s, &c->sprite_list, link) {
484 if (s->pending_fb_id)
487 ret = drmModeSetPlane(c->drm.fd, s->plane_id,
488 output->crtc_id, 0, 0,
489 0, 0, 0, 0, 0, 0, 0, 0);
491 weston_log("failed to disable plane: %d: %s\n",
492 ret, strerror(errno));
493 drmModeRmFB(c->drm.fd, s->fb_id);
495 s->pending_surface = NULL;
497 s->pending_fb_id = 0;
502 * This function must take care to damage any previously assigned surface
503 * if the sprite ends up binding to a different surface than in the
507 drm_output_prepare_overlay_surface(struct weston_output *output_base,
508 struct weston_surface *es,
509 pixman_region32_t *overlap)
511 struct weston_compositor *ec = output_base->compositor;
512 struct drm_compositor *c =(struct drm_compositor *) ec;
513 struct drm_sprite *s;
515 EGLint handle, stride;
518 uint32_t handles[4], pitches[4], offsets[4];
520 pixman_region32_t dest_rect, src_rect;
524 if (c->sprites_are_broken)
527 if (surface_is_primary(ec, es))
530 if (es->image == EGL_NO_IMAGE_KHR)
533 if (!drm_surface_transform_supported(es))
536 if (!drm_surface_overlap_supported(output_base, overlap))
539 wl_list_for_each(s, &c->sprite_list, link) {
540 if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs))
543 if (!s->pending_fb_id) {
549 /* No sprites available */
553 bo = gbm_bo_create_from_egl_image(c->gbm, c->base.display, es->image,
554 es->geometry.width, es->geometry.height,
556 format = gbm_bo_get_format(bo);
557 handle = gbm_bo_get_handle(bo).s32;
558 stride = gbm_bo_get_pitch(bo);
562 if (!drm_surface_format_supported(s, format))
572 ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height,
573 format, handles, pitches, offsets,
576 weston_log("addfb2 failed: %d\n", ret);
577 c->sprites_are_broken = 1;
581 if (s->surface && s->surface != es) {
582 struct weston_surface *old_surf = s->surface;
583 pixman_region32_fini(&old_surf->damage);
584 pixman_region32_init_rect(&old_surf->damage,
585 old_surf->geometry.x, old_surf->geometry.y,
586 old_surf->geometry.width, old_surf->geometry.height);
589 s->pending_fb_id = fb_id;
590 s->pending_surface = es;
591 es->buffer->busy_count++;
594 * Calculate the source & dest rects properly based on actual
595 * postion (note the caller has called weston_surface_update_transform()
598 pixman_region32_init(&dest_rect);
599 pixman_region32_intersect(&dest_rect, &es->transform.boundingbox,
600 &output_base->region);
601 pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y);
602 box = pixman_region32_extents(&dest_rect);
605 s->dest_w = box->x2 - box->x1;
606 s->dest_h = box->y2 - box->y1;
607 pixman_region32_fini(&dest_rect);
609 pixman_region32_init(&src_rect);
610 pixman_region32_intersect(&src_rect, &es->transform.boundingbox,
611 &output_base->region);
612 pixman_region32_translate(&src_rect, -es->geometry.x, -es->geometry.y);
613 box = pixman_region32_extents(&src_rect);
614 s->src_x = box->x1 << 16;
615 s->src_y = box->y1 << 16;
616 s->src_w = (box->x2 - box->x1) << 16;
617 s->src_h = (box->y2 - box->y1) << 16;
618 pixman_region32_fini(&src_rect);
620 wl_signal_add(&es->buffer->resource.destroy_signal,
621 &s->pending_destroy_listener);
626 drm_output_set_cursor(struct weston_output *output_base,
627 struct weston_seat *es);
630 weston_output_set_cursor(struct weston_output *output,
631 struct weston_seat *seat,
632 pixman_region32_t *overlap)
634 pixman_region32_t cursor_region;
635 int prior_was_hardware;
637 if (seat->sprite == NULL)
640 pixman_region32_init(&cursor_region);
641 pixman_region32_intersect(&cursor_region,
642 &seat->sprite->transform.boundingbox,
645 if (!pixman_region32_not_empty(&cursor_region)) {
646 drm_output_set_cursor(output, NULL);
650 prior_was_hardware = seat->hw_cursor;
651 if (pixman_region32_not_empty(overlap) ||
652 drm_output_set_cursor(output, seat) < 0) {
653 if (prior_was_hardware) {
654 weston_surface_damage(seat->sprite);
655 drm_output_set_cursor(output, NULL);
659 if (!prior_was_hardware)
660 weston_surface_damage_below(seat->sprite);
661 wl_list_remove(&seat->sprite->link);
666 pixman_region32_fini(&cursor_region);
670 drm_assign_planes(struct weston_output *output)
672 struct weston_compositor *ec = output->compositor;
673 struct weston_surface *es, *next;
674 pixman_region32_t overlap, surface_overlap;
675 struct weston_seat *seat;
678 * Find a surface for each sprite in the output using some heuristics:
680 * 2) frequency of update
681 * 3) opacity (though some hw might support alpha blending)
682 * 4) clipping (this can be fixed with color keys)
684 * The idea is to save on blitting since this should save power.
685 * If we can get a large video surface on the sprite for example,
686 * the main display surface may not need to update at all, and
687 * the client buffer can be used directly for the sprite surface
688 * as we do for flipping full screen surfaces.
690 pixman_region32_init(&overlap);
691 wl_list_for_each_safe(es, next, &ec->surface_list, link) {
693 * FIXME: try to assign hw cursors here too, they're just
696 pixman_region32_init(&surface_overlap);
697 pixman_region32_intersect(&surface_overlap, &overlap,
698 &es->transform.boundingbox);
700 seat = (struct weston_seat *) ec->seat;
701 if (es == seat->sprite) {
702 weston_output_set_cursor(output, seat,
705 if (!seat->hw_cursor)
706 pixman_region32_union(&overlap, &overlap,
707 &es->transform.boundingbox);
708 } else if (!drm_output_prepare_overlay_surface(output, es,
710 pixman_region32_fini(&es->damage);
711 pixman_region32_init(&es->damage);
713 pixman_region32_union(&overlap, &overlap,
714 &es->transform.boundingbox);
716 pixman_region32_fini(&surface_overlap);
718 pixman_region32_fini(&overlap);
720 drm_disable_unused_sprites(output);
724 drm_output_set_cursor(struct weston_output *output_base,
725 struct weston_seat *es)
727 struct drm_output *output = (struct drm_output *) output_base;
728 struct drm_compositor *c =
729 (struct drm_compositor *) output->base.compositor;
730 EGLint handle, stride;
733 uint32_t buf[64 * 64];
734 unsigned char *d, *s, *end;
737 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
741 if (es->sprite->buffer == NULL ||
742 !wl_buffer_is_shm(es->sprite->buffer))
745 if (es->sprite->geometry.width > 64 ||
746 es->sprite->geometry.height > 64)
749 output->current_cursor ^= 1;
750 bo = output->cursor_bo[output->current_cursor];
754 memset(buf, 0, sizeof buf);
755 d = (unsigned char *) buf;
756 stride = wl_shm_buffer_get_stride(es->sprite->buffer);
757 s = wl_shm_buffer_get_data(es->sprite->buffer);
758 end = s + stride * es->sprite->geometry.height;
760 memcpy(d, s, es->sprite->geometry.width * 4);
765 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
768 handle = gbm_bo_get_handle(bo).s32;
769 ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64);
771 weston_log("failed to set cursor: %s\n", strerror(-ret));
775 ret = drmModeMoveCursor(c->drm.fd, output->crtc_id,
776 es->sprite->geometry.x - output->base.x,
777 es->sprite->geometry.y - output->base.y);
779 weston_log("failed to move cursor: %s\n", strerror(-ret));
785 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
790 drm_output_destroy(struct weston_output *output_base)
792 struct drm_output *output = (struct drm_output *) output_base;
793 struct drm_compositor *c =
794 (struct drm_compositor *) output->base.compositor;
795 drmModeCrtcPtr origcrtc = output->original_crtc;
797 if (output->backlight)
798 backlight_destroy(output->backlight);
800 /* Turn off hardware cursor */
801 drm_output_set_cursor(&output->base, NULL);
803 /* Restore original CRTC state */
804 drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
805 origcrtc->x, origcrtc->y,
806 &output->connector_id, 1, &origcrtc->mode);
807 drmModeFreeCrtc(origcrtc);
809 c->crtc_allocator &= ~(1 << output->crtc_id);
810 c->connector_allocator &= ~(1 << output->connector_id);
812 eglDestroySurface(c->base.display, output->egl_surface);
813 gbm_surface_destroy(output->surface);
815 weston_output_destroy(&output->base);
816 wl_list_remove(&output->base.link);
821 static struct drm_mode *
822 choose_mode (struct drm_output *output, struct weston_mode *target_mode)
824 struct drm_mode *tmp_mode = NULL, *mode;
826 if (output->base.current->width == target_mode->width &&
827 output->base.current->height == target_mode->height &&
828 (output->base.current->refresh == target_mode->refresh ||
829 target_mode->refresh == 0))
830 return (struct drm_mode *)output->base.current;
832 wl_list_for_each(mode, &output->base.mode_list, base.link) {
833 if (mode->mode_info.hdisplay == target_mode->width &&
834 mode->mode_info.vdisplay == target_mode->height) {
835 if (mode->mode_info.vrefresh == target_mode->refresh ||
836 target_mode->refresh == 0) {
838 } else if (!tmp_mode)
847 drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
849 struct drm_output *output;
850 struct drm_mode *drm_mode;
852 struct drm_compositor *ec;
853 struct gbm_surface *surface;
854 EGLSurface egl_surface;
856 if (output_base == NULL) {
857 weston_log("output is NULL.\n");
862 weston_log("mode is NULL.\n");
866 ec = (struct drm_compositor *)output_base->compositor;
867 output = (struct drm_output *)output_base;
868 drm_mode = choose_mode (output, mode);
871 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
873 } else if (&drm_mode->base == output->base.current) {
875 } else if (drm_mode->base.width == output->base.current->width &&
876 drm_mode->base.height == output->base.current->height) {
877 /* only change refresh value */
878 ret = drmModeSetCrtc(ec->drm.fd,
880 output->current->fb_id, 0, 0,
881 &output->connector_id, 1, &drm_mode->mode_info);
884 weston_log("failed to set mode (%dx%d) %u Hz\n",
885 drm_mode->base.width,
886 drm_mode->base.height,
887 drm_mode->base.refresh / 1000);
890 output->base.current->flags = 0;
891 output->base.current = &drm_mode->base;
892 drm_mode->base.flags =
893 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
900 drm_mode->base.flags =
901 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
903 surface = gbm_surface_create(ec->gbm,
904 drm_mode->base.width,
905 drm_mode->base.height,
908 GBM_BO_USE_RENDERING);
910 weston_log("failed to create gbm surface\n");
915 eglCreateWindowSurface(ec->base.display,
919 if (egl_surface == EGL_NO_SURFACE) {
920 weston_log("failed to create egl surface\n");
924 ret = drmModeSetCrtc(ec->drm.fd,
926 output->current->fb_id, 0, 0,
927 &output->connector_id, 1, &drm_mode->mode_info);
929 weston_log("failed to set mode\n");
933 /* reset rendering stuff. */
934 if (output->current) {
935 if (output->current->is_client_buffer)
936 gbm_bo_destroy(output->current->bo);
938 gbm_surface_release_buffer(output->surface,
939 output->current->bo);
941 output->current = NULL;
944 if (output->next->is_client_buffer)
945 gbm_bo_destroy(output->next->bo);
947 gbm_surface_release_buffer(output->surface,
952 eglDestroySurface(ec->base.display, output->egl_surface);
953 gbm_surface_destroy(output->surface);
954 output->egl_surface = egl_surface;
955 output->surface = surface;
958 output->base.current = &drm_mode->base;
959 output->base.dirty = 1;
960 weston_output_move(&output->base, output->base.x, output->base.y);
964 eglDestroySurface(ec->base.display, egl_surface);
965 gbm_surface_destroy(surface);
970 on_drm_input(int fd, uint32_t mask, void *data)
972 drmEventContext evctx;
974 memset(&evctx, 0, sizeof evctx);
975 evctx.version = DRM_EVENT_CONTEXT_VERSION;
976 evctx.page_flip_handler = page_flip_handler;
977 evctx.vblank_handler = vblank_handler;
978 drmHandleEvent(fd, &evctx);
984 init_egl(struct drm_compositor *ec, struct udev_device *device)
986 EGLint major, minor, n;
987 const char *filename, *sysnum;
989 static const EGLint context_attribs[] = {
990 EGL_CONTEXT_CLIENT_VERSION, 2,
994 static const EGLint config_attribs[] = {
995 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1000 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1004 sysnum = udev_device_get_sysnum(device);
1006 ec->drm.id = atoi(sysnum);
1007 if (!sysnum || ec->drm.id < 0) {
1008 weston_log("cannot get device sysnum\n");
1012 filename = udev_device_get_devnode(device);
1013 fd = open(filename, O_RDWR | O_CLOEXEC);
1015 /* Probably permissions error */
1016 weston_log("couldn't open %s, skipping\n",
1017 udev_device_get_devnode(device));
1022 ec->gbm = gbm_create_device(ec->drm.fd);
1023 ec->base.display = eglGetDisplay(ec->gbm);
1024 if (ec->base.display == NULL) {
1025 weston_log("failed to create display\n");
1029 if (!eglInitialize(ec->base.display, &major, &minor)) {
1030 weston_log("failed to initialize display\n");
1034 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
1035 weston_log("failed to bind api EGL_OPENGL_ES_API\n");
1039 if (!eglChooseConfig(ec->base.display, config_attribs,
1040 &ec->base.config, 1, &n) || n != 1) {
1041 weston_log("failed to choose config: %d\n", n);
1045 ec->base.context = eglCreateContext(ec->base.display, ec->base.config,
1046 EGL_NO_CONTEXT, context_attribs);
1047 if (ec->base.context == NULL) {
1048 weston_log("failed to create context\n");
1052 ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10,
1053 GBM_FORMAT_XRGB8888,
1054 GBM_BO_USE_RENDERING);
1055 if (!ec->dummy_surface) {
1056 weston_log("failed to create dummy gbm surface\n");
1060 ec->dummy_egl_surface =
1061 eglCreateWindowSurface(ec->base.display, ec->base.config,
1062 ec->dummy_surface, NULL);
1063 if (ec->dummy_egl_surface == EGL_NO_SURFACE) {
1064 weston_log("failed to create egl surface\n");
1068 if (!eglMakeCurrent(ec->base.display, ec->dummy_egl_surface,
1069 ec->dummy_egl_surface, ec->base.context)) {
1070 weston_log("failed to make context current\n");
1077 static drmModeModeInfo builtin_1024x768 = {
1079 1024, 1072, 1176, 1328, 0,
1080 768, 771, 775, 798, 0,
1082 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
1089 drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
1091 struct drm_mode *mode;
1094 mode = malloc(sizeof *mode);
1098 mode->base.flags = 0;
1099 mode->base.width = info->hdisplay;
1100 mode->base.height = info->vdisplay;
1102 /* Calculate higher precision (mHz) refresh rate */
1103 refresh = (info->clock * 1000000LL / info->htotal +
1104 info->vtotal / 2) / info->vtotal;
1106 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1108 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1110 if (info->vscan > 1)
1111 refresh /= info->vscan;
1113 mode->base.refresh = refresh;
1114 mode->mode_info = *info;
1115 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1121 drm_subpixel_to_wayland(int drm_value)
1123 switch (drm_value) {
1125 case DRM_MODE_SUBPIXEL_UNKNOWN:
1126 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1127 case DRM_MODE_SUBPIXEL_NONE:
1128 return WL_OUTPUT_SUBPIXEL_NONE;
1129 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1130 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1131 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1132 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1133 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1134 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1135 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1136 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1141 sprite_handle_buffer_destroy(struct wl_listener *listener, void *data)
1143 struct drm_sprite *sprite =
1144 container_of(listener, struct drm_sprite,
1147 sprite->surface = NULL;
1151 sprite_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
1153 struct drm_sprite *sprite =
1154 container_of(listener, struct drm_sprite,
1155 pending_destroy_listener);
1157 sprite->pending_surface = NULL;
1160 /* returns a value between 0-255 range, where higher is brighter */
1162 drm_get_backlight(struct drm_output *output)
1164 long brightness, max_brightness, norm;
1166 brightness = backlight_get_brightness(output->backlight);
1167 max_brightness = backlight_get_max_brightness(output->backlight);
1169 /* convert it on a scale of 0 to 255 */
1170 norm = (brightness * 255)/(max_brightness);
1172 return (uint32_t) norm;
1175 /* values accepted are between 0-255 range */
1177 drm_set_backlight(struct weston_output *output_base, uint32_t value)
1179 struct drm_output *output = (struct drm_output *) output_base;
1180 long max_brightness, new_brightness;
1182 if (!output->backlight)
1188 max_brightness = backlight_get_max_brightness(output->backlight);
1190 /* get denormalized value */
1191 new_brightness = (value * max_brightness) / 255;
1193 backlight_set_brightness(output->backlight, new_brightness);
1196 static drmModePropertyPtr
1197 drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1199 drmModePropertyPtr props;
1202 for (i = 0; i < connector->count_props; i++) {
1203 props = drmModeGetProperty(fd, connector->props[i]);
1207 if (!strcmp(props->name, name))
1210 drmModeFreeProperty(props);
1217 drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1219 struct drm_output *output = (struct drm_output *) output_base;
1220 struct weston_compositor *ec = output_base->compositor;
1221 struct drm_compositor *c = (struct drm_compositor *) ec;
1222 drmModeConnectorPtr connector;
1223 drmModePropertyPtr prop;
1225 connector = drmModeGetConnector(c->drm.fd, output->connector_id);
1229 prop = drm_get_prop(c->drm.fd, connector, "DPMS");
1231 drmModeFreeConnector(connector);
1235 drmModeConnectorSetProperty(c->drm.fd, connector->connector_id,
1236 prop->prop_id, level);
1237 drmModeFreeProperty(prop);
1238 drmModeFreeConnector(connector);
1242 create_output_for_connector(struct drm_compositor *ec,
1243 drmModeRes *resources,
1244 drmModeConnector *connector,
1245 int x, int y, struct udev_device *drm_device)
1247 struct drm_output *output;
1248 struct drm_mode *drm_mode, *next;
1249 drmModeEncoder *encoder;
1252 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
1253 if (encoder == NULL) {
1254 weston_log("No encoder for connector.\n");
1258 for (i = 0; i < resources->count_crtcs; i++) {
1259 if (encoder->possible_crtcs & (1 << i) &&
1260 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
1263 if (i == resources->count_crtcs) {
1264 weston_log("No usable crtc for encoder.\n");
1265 drmModeFreeEncoder(encoder);
1269 output = malloc(sizeof *output);
1270 if (output == NULL) {
1271 drmModeFreeEncoder(encoder);
1275 memset(output, 0, sizeof *output);
1276 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
1277 output->base.make = "unknown";
1278 output->base.model = "unknown";
1279 wl_list_init(&output->base.mode_list);
1281 output->crtc_id = resources->crtcs[i];
1282 ec->crtc_allocator |= (1 << output->crtc_id);
1283 output->connector_id = connector->connector_id;
1284 ec->connector_allocator |= (1 << output->connector_id);
1286 output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id);
1287 drmModeFreeEncoder(encoder);
1289 for (i = 0; i < connector->count_modes; i++) {
1290 ret = drm_output_add_mode(output, &connector->modes[i]);
1295 if (connector->count_modes == 0) {
1296 ret = drm_output_add_mode(output, &builtin_1024x768);
1301 drm_mode = container_of(output->base.mode_list.next,
1302 struct drm_mode, base.link);
1303 output->base.current = &drm_mode->base;
1304 drm_mode->base.flags =
1305 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1307 output->surface = gbm_surface_create(ec->gbm,
1308 output->base.current->width,
1309 output->base.current->height,
1310 GBM_FORMAT_XRGB8888,
1311 GBM_BO_USE_SCANOUT |
1312 GBM_BO_USE_RENDERING);
1313 if (!output->surface) {
1314 weston_log("failed to create gbm surface\n");
1318 output->egl_surface =
1319 eglCreateWindowSurface(ec->base.display, ec->base.config,
1320 output->surface, NULL);
1321 if (output->egl_surface == EGL_NO_SURFACE) {
1322 weston_log("failed to create egl surface\n");
1326 output->cursor_bo[0] =
1327 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1328 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1329 output->cursor_bo[1] =
1330 gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888,
1331 GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
1333 output->backlight = backlight_init(drm_device,
1334 connector->connector_type);
1335 if (output->backlight) {
1336 output->base.set_backlight = drm_set_backlight;
1337 output->base.backlight_current = drm_get_backlight(output);
1340 weston_output_init(&output->base, &ec->base, x, y,
1341 connector->mmWidth, connector->mmHeight,
1344 wl_list_insert(ec->base.output_list.prev, &output->base.link);
1346 output->base.origin = output->base.current;
1347 output->base.repaint = drm_output_repaint;
1348 output->base.destroy = drm_output_destroy;
1349 output->base.assign_planes = drm_assign_planes;
1350 output->base.set_dpms = drm_set_dpms;
1351 output->base.switch_mode = drm_output_switch_mode;
1356 gbm_surface_destroy(output->surface);
1358 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
1360 wl_list_remove(&drm_mode->base.link);
1364 drmModeFreeCrtc(output->original_crtc);
1365 ec->crtc_allocator &= ~(1 << output->crtc_id);
1366 ec->connector_allocator &= ~(1 << output->connector_id);
1373 create_sprites(struct drm_compositor *ec)
1375 struct drm_sprite *sprite;
1376 drmModePlaneRes *plane_res;
1377 drmModePlane *plane;
1380 plane_res = drmModeGetPlaneResources(ec->drm.fd);
1382 weston_log("failed to get plane resources: %s\n",
1387 for (i = 0; i < plane_res->count_planes; i++) {
1388 plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]);
1392 sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
1393 plane->count_formats));
1395 weston_log("%s: out of memory\n",
1401 memset(sprite, 0, sizeof *sprite);
1403 sprite->possible_crtcs = plane->possible_crtcs;
1404 sprite->plane_id = plane->plane_id;
1405 sprite->surface = NULL;
1406 sprite->pending_surface = NULL;
1408 sprite->pending_fb_id = 0;
1409 sprite->destroy_listener.notify = sprite_handle_buffer_destroy;
1410 sprite->pending_destroy_listener.notify =
1411 sprite_handle_pending_buffer_destroy;
1412 sprite->compositor = ec;
1413 sprite->count_formats = plane->count_formats;
1414 memcpy(sprite->formats, plane->formats,
1415 plane->count_formats * sizeof(plane->formats[0]));
1416 drmModeFreePlane(plane);
1418 wl_list_insert(&ec->sprite_list, &sprite->link);
1421 free(plane_res->planes);
1426 destroy_sprites(struct drm_compositor *compositor)
1428 struct drm_sprite *sprite, *next;
1429 struct drm_output *output;
1431 output = container_of(compositor->base.output_list.next,
1432 struct drm_output, base.link);
1434 wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) {
1435 drmModeSetPlane(compositor->drm.fd,
1437 output->crtc_id, 0, 0,
1438 0, 0, 0, 0, 0, 0, 0, 0);
1439 drmModeRmFB(compositor->drm.fd, sprite->fb_id);
1445 create_outputs(struct drm_compositor *ec, uint32_t option_connector,
1446 struct udev_device *drm_device)
1448 drmModeConnector *connector;
1449 drmModeRes *resources;
1453 resources = drmModeGetResources(ec->drm.fd);
1455 weston_log("drmModeGetResources failed\n");
1459 ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
1461 drmModeFreeResources(resources);
1465 ec->num_crtcs = resources->count_crtcs;
1466 memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs);
1468 for (i = 0; i < resources->count_connectors; i++) {
1469 connector = drmModeGetConnector(ec->drm.fd,
1470 resources->connectors[i]);
1471 if (connector == NULL)
1474 if (connector->connection == DRM_MODE_CONNECTED &&
1475 (option_connector == 0 ||
1476 connector->connector_id == option_connector)) {
1477 if (create_output_for_connector(ec, resources,
1480 drmModeFreeConnector(connector);
1484 x += container_of(ec->base.output_list.prev,
1485 struct weston_output,
1486 link)->current->width;
1489 drmModeFreeConnector(connector);
1492 if (wl_list_empty(&ec->base.output_list)) {
1493 weston_log("No currently active connector found.\n");
1494 drmModeFreeResources(resources);
1498 drmModeFreeResources(resources);
1504 update_outputs(struct drm_compositor *ec, struct udev_device *drm_device)
1506 drmModeConnector *connector;
1507 drmModeRes *resources;
1508 struct drm_output *output, *next;
1510 int x_offset = 0, y_offset = 0;
1511 uint32_t connected = 0, disconnects = 0;
1514 resources = drmModeGetResources(ec->drm.fd);
1516 weston_log("drmModeGetResources failed\n");
1520 /* collect new connects */
1521 for (i = 0; i < resources->count_connectors; i++) {
1522 int connector_id = resources->connectors[i];
1524 connector = drmModeGetConnector(ec->drm.fd, connector_id);
1525 if (connector == NULL)
1528 if (connector->connection != DRM_MODE_CONNECTED) {
1529 drmModeFreeConnector(connector);
1533 connected |= (1 << connector_id);
1535 if (!(ec->connector_allocator & (1 << connector_id))) {
1536 struct weston_output *last =
1537 container_of(ec->base.output_list.prev,
1538 struct weston_output, link);
1540 /* XXX: not yet needed, we die with 0 outputs */
1541 if (!wl_list_empty(&ec->base.output_list))
1542 x = last->x + last->current->width;
1546 create_output_for_connector(ec, resources,
1549 weston_log("connector %d connected\n", connector_id);
1552 drmModeFreeConnector(connector);
1554 drmModeFreeResources(resources);
1556 disconnects = ec->connector_allocator & ~connected;
1558 wl_list_for_each_safe(output, next, &ec->base.output_list,
1560 if (x_offset != 0 || y_offset != 0) {
1561 weston_output_move(&output->base,
1562 output->base.x - x_offset,
1563 output->base.y - y_offset);
1566 if (disconnects & (1 << output->connector_id)) {
1567 disconnects &= ~(1 << output->connector_id);
1568 weston_log("connector %d disconnected\n",
1569 output->connector_id);
1570 x_offset += output->base.current->width;
1571 drm_output_destroy(&output->base);
1576 /* FIXME: handle zero outputs, without terminating */
1577 if (ec->connector_allocator == 0)
1578 wl_display_terminate(ec->base.wl_display);
1582 udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device)
1587 sysnum = udev_device_get_sysnum(device);
1588 if (!sysnum || atoi(sysnum) != ec->drm.id)
1591 val = udev_device_get_property_value(device, "HOTPLUG");
1595 return strcmp(val, "1") == 0;
1599 udev_drm_event(int fd, uint32_t mask, void *data)
1601 struct drm_compositor *ec = data;
1602 struct udev_device *event;
1604 event = udev_monitor_receive_device(ec->udev_monitor);
1606 if (udev_event_is_hotplug(ec, event))
1607 update_outputs(ec, event);
1609 udev_device_unref(event);
1615 drm_destroy(struct weston_compositor *ec)
1617 struct drm_compositor *d = (struct drm_compositor *) ec;
1618 struct weston_seat *seat, *next;
1620 wl_list_for_each_safe(seat, next, &ec->seat_list, link)
1621 evdev_input_destroy(seat);
1623 wl_event_source_remove(d->udev_drm_source);
1624 wl_event_source_remove(d->drm_source);
1626 weston_compositor_shutdown(ec);
1628 /* Work around crash in egl_dri2.c's dri2_make_current() */
1629 eglMakeCurrent(ec->display, EGL_NO_SURFACE, EGL_NO_SURFACE,
1631 eglTerminate(ec->display);
1634 gbm_device_destroy(d->gbm);
1636 if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0)
1637 weston_log("failed to drop master: %m\n");
1638 tty_destroy(d->tty);
1644 drm_compositor_set_modes(struct drm_compositor *compositor)
1646 struct drm_output *output;
1647 struct drm_mode *drm_mode;
1650 wl_list_for_each(output, &compositor->base.output_list, base.link) {
1651 drm_mode = (struct drm_mode *) output->base.current;
1652 ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id,
1653 output->current->fb_id, 0, 0,
1654 &output->connector_id, 1,
1655 &drm_mode->mode_info);
1658 "failed to set mode %dx%d for output at %d,%d: %m\n",
1659 drm_mode->base.width, drm_mode->base.height,
1660 output->base.x, output->base.y);
1666 vt_func(struct weston_compositor *compositor, int event)
1668 struct drm_compositor *ec = (struct drm_compositor *) compositor;
1669 struct weston_output *output;
1670 struct weston_seat *seat;
1671 struct drm_sprite *sprite;
1672 struct drm_output *drm_output;
1676 compositor->focus = 1;
1677 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) {
1678 weston_log("failed to set master: %m\n");
1679 wl_display_terminate(compositor->wl_display);
1681 compositor->state = ec->prev_state;
1682 drm_compositor_set_modes(ec);
1683 weston_compositor_damage_all(compositor);
1684 wl_list_for_each(seat, &compositor->seat_list, link) {
1685 evdev_add_devices(ec->udev, seat);
1686 evdev_enable_udev_monitor(ec->udev, seat);
1690 wl_list_for_each(seat, &compositor->seat_list, link) {
1691 evdev_disable_udev_monitor(seat);
1692 evdev_remove_devices(seat);
1695 compositor->focus = 0;
1696 ec->prev_state = compositor->state;
1697 compositor->state = WESTON_COMPOSITOR_SLEEPING;
1699 /* If we have a repaint scheduled (either from a
1700 * pending pageflip or the idle handler), make sure we
1701 * cancel that so we don't try to pageflip when we're
1702 * vt switched away. The SLEEPING state will prevent
1703 * further attemps at repainting. When we switch
1704 * back, we schedule a repaint, which will process
1705 * pending frame callbacks. */
1707 wl_list_for_each(output, &ec->base.output_list, link) {
1708 output->repaint_needed = 0;
1709 drm_output_set_cursor(output, NULL);
1712 drm_output = container_of(ec->base.output_list.next,
1713 struct drm_output, base.link);
1715 wl_list_for_each(sprite, &ec->sprite_list, link)
1716 drmModeSetPlane(ec->drm.fd,
1718 drm_output->crtc_id, 0, 0,
1719 0, 0, 0, 0, 0, 0, 0, 0);
1721 if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0)
1722 weston_log("failed to drop master: %m\n");
1729 switch_vt_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
1731 struct drm_compositor *ec = data;
1733 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
1736 static const char default_seat[] = "seat0";
1738 static struct weston_compositor *
1739 drm_compositor_create(struct wl_display *display,
1740 int connector, const char *seat, int tty,
1741 int argc, char *argv[], const char *config_file)
1743 struct drm_compositor *ec;
1744 struct udev_enumerate *e;
1745 struct udev_list_entry *entry;
1746 struct udev_device *device, *drm_device;
1747 const char *path, *device_seat;
1748 struct wl_event_loop *loop;
1751 ec = malloc(sizeof *ec);
1755 memset(ec, 0, sizeof *ec);
1756 ec->udev = udev_new();
1757 if (ec->udev == NULL) {
1758 weston_log("failed to initialize udev context\n");
1762 ec->base.wl_display = display;
1763 ec->tty = tty_create(&ec->base, vt_func, tty);
1765 weston_log("failed to initialize tty\n");
1770 e = udev_enumerate_new(ec->udev);
1771 udev_enumerate_add_match_subsystem(e, "drm");
1772 udev_enumerate_add_match_sysname(e, "card[0-9]*");
1774 udev_enumerate_scan_devices(e);
1776 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
1777 path = udev_list_entry_get_name(entry);
1778 device = udev_device_new_from_syspath(ec->udev, path);
1780 udev_device_get_property_value(device, "ID_SEAT");
1782 device_seat = default_seat;
1783 if (strcmp(device_seat, seat) == 0) {
1784 drm_device = device;
1787 udev_device_unref(device);
1790 if (drm_device == NULL) {
1791 weston_log("no drm device found\n");
1795 if (init_egl(ec, drm_device) < 0) {
1796 weston_log("failed to initialize egl\n");
1800 ec->base.destroy = drm_destroy;
1804 ec->prev_state = WESTON_COMPOSITOR_ACTIVE;
1806 /* Can't init base class until we have a current egl context */
1807 if (weston_compositor_init(&ec->base, display, argc, argv,
1811 for (key = KEY_F1; key < KEY_F9; key++)
1812 weston_compositor_add_key_binding(&ec->base, key,
1813 MODIFIER_CTRL | MODIFIER_ALT,
1814 switch_vt_binding, ec);
1816 wl_list_init(&ec->sprite_list);
1819 if (create_outputs(ec, connector, drm_device) < 0) {
1820 weston_log("failed to create output for %s\n", path);
1824 udev_device_unref(drm_device);
1825 udev_enumerate_unref(e);
1828 evdev_input_create(&ec->base, ec->udev, seat);
1830 loop = wl_display_get_event_loop(ec->base.wl_display);
1832 wl_event_loop_add_fd(loop, ec->drm.fd,
1833 WL_EVENT_READABLE, on_drm_input, ec);
1835 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
1836 if (ec->udev_monitor == NULL) {
1837 weston_log("failed to intialize udev monitor\n");
1840 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
1842 ec->udev_drm_source =
1843 wl_event_loop_add_fd(loop,
1844 udev_monitor_get_fd(ec->udev_monitor),
1845 WL_EVENT_READABLE, udev_drm_event, ec);
1847 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
1848 weston_log("failed to enable udev-monitor receiving\n");
1855 WL_EXPORT struct weston_compositor *
1856 backend_init(struct wl_display *display, int argc, char *argv[],
1857 const char *config_file)
1859 int connector = 0, tty = 0;
1860 const char *seat = default_seat;
1862 const struct weston_option drm_options[] = {
1863 { WESTON_OPTION_INTEGER, "connector", 0, &connector },
1864 { WESTON_OPTION_STRING, "seat", 0, &seat },
1865 { WESTON_OPTION_INTEGER, "tty", 0, &tty },
1868 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
1870 return drm_compositor_create(display, connector, seat, tty, argc, argv,