compositor-drm: Hide hw cursors on vt switch
[profile/ivi/weston.git] / compositor / compositor-drm.c
1 /*
2  * Copyright © 2008-2010 Kristian Høgsberg
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 #define _GNU_SOURCE
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26
27 #include <xf86drm.h>
28 #include <xf86drmMode.h>
29
30 #include "compositor.h"
31
32 struct drm_compositor {
33         struct wlsc_compositor base;
34
35         struct udev *udev;
36         struct wl_event_source *drm_source;
37
38         struct udev_monitor *udev_monitor;
39         struct wl_event_source *udev_drm_source;
40
41         struct {
42                 int fd;
43         } drm;
44         uint32_t crtc_allocator;
45         uint32_t connector_allocator;
46         struct tty *tty;
47
48         PFNEGLCREATEDRMIMAGEMESA create_drm_image;
49         PFNEGLEXPORTDRMIMAGEMESA export_drm_image;
50 };
51
52 struct drm_output {
53         struct wlsc_output   base;
54
55         drmModeModeInfo mode;
56         uint32_t crtc_id;
57         uint32_t connector_id;
58         GLuint rbo[2];
59         uint32_t fb_id[2];
60         EGLImageKHR image[2];
61         uint32_t current;       
62
63         struct wlsc_surface *scanout_surface;
64
65         uint32_t fs_surf_fb_id;
66         uint32_t pending_fs_surf_fb_id;
67 };
68
69 static int
70 drm_output_prepare_render(struct wlsc_output *output_base)
71 {
72         struct drm_output *output = (struct drm_output *) output_base;
73
74         glFramebufferRenderbuffer(GL_FRAMEBUFFER,
75                                   GL_COLOR_ATTACHMENT0,
76                                   GL_RENDERBUFFER,
77                                   output->rbo[output->current]);
78
79         if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
80                 return -1;
81
82         return 0;
83 }
84
85 static int
86 drm_output_present(struct wlsc_output *output_base)
87 {
88         struct drm_output *output = (struct drm_output *) output_base;
89         struct drm_compositor *c =
90                 (struct drm_compositor *) output->base.compositor;
91         uint32_t fb_id = 0;
92
93         if (drm_output_prepare_render(&output->base))
94                 return -1;
95         glFlush();
96
97         output->current ^= 1;
98
99         if (output->scanout_surface != NULL) {
100                 output->scanout_surface = NULL;
101                 fb_id = output->fs_surf_fb_id;
102         } else {
103                 fb_id = output->fb_id[output->current ^ 1];
104         }
105
106         drmModePageFlip(c->drm.fd, output->crtc_id,
107                         fb_id,
108                         DRM_MODE_PAGE_FLIP_EVENT, output);
109
110         return 0;
111 }
112
113 static void
114 page_flip_handler(int fd, unsigned int frame,
115                   unsigned int sec, unsigned int usec, void *data)
116 {
117         struct drm_output *output = (struct drm_output *) data;
118         struct drm_compositor *c =
119                 (struct drm_compositor *) output->base.compositor;
120         uint32_t msecs;
121
122         if (output->pending_fs_surf_fb_id) {
123                 drmModeRmFB(c->drm.fd, output->pending_fs_surf_fb_id);
124                 output->pending_fs_surf_fb_id = 0;
125         }
126
127         if (output->fs_surf_fb_id) {
128                 output->pending_fs_surf_fb_id = output->fs_surf_fb_id;
129                 output->fs_surf_fb_id = 0;
130         }
131
132         msecs = sec * 1000 + usec / 1000;
133         wlsc_output_finish_frame(&output->base, msecs);
134 }
135
136 static int
137 drm_output_prepare_scanout_surface(struct wlsc_output *output_base,
138                                    struct wlsc_surface *es)
139 {
140         struct drm_output *output = (struct drm_output *) output_base;
141         struct drm_compositor *c =
142                 (struct drm_compositor *) output->base.compositor;
143         EGLint handle, stride;
144         int ret;
145         uint32_t fb_id = 0;
146
147         if (es->x != output->base.x ||
148             es->y != output->base.y ||
149             es->width != output->base.width ||
150             es->height != output->base.height ||
151             es->image == EGL_NO_IMAGE_KHR)
152                 return -1;
153
154         c->export_drm_image(c->base.display,
155                             es->image, NULL, &handle, &stride);
156
157         if (handle == 0)
158                 return -1;
159
160         ret = drmModeAddFB(c->drm.fd,
161                            output->base.width, output->base.height,
162                            32, 32, stride, handle, &fb_id);
163
164         if (ret)
165                 return -1;
166
167         output->fs_surf_fb_id = fb_id;
168         output->scanout_surface = es;
169
170         return 0;
171 }
172
173 static int
174 drm_output_set_cursor(struct wlsc_output *output_base,
175                       struct wlsc_input_device *eid)
176 {
177         struct drm_output *output = (struct drm_output *) output_base;
178         struct drm_compositor *c =
179                 (struct drm_compositor *) output->base.compositor;
180         EGLint handle, stride;
181         int ret = -1;
182         pixman_region32_t cursor_region;
183
184         if (eid == NULL) {
185                 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
186                 return 0;
187         }
188
189         pixman_region32_init_rect(&cursor_region,
190                                   eid->sprite->x, eid->sprite->y,
191                                   eid->sprite->width, eid->sprite->height);
192
193         pixman_region32_intersect_rect(&cursor_region, &cursor_region,
194                                        output->base.x, output->base.y,
195                                        output->base.width, output->base.height);
196
197         if (!pixman_region32_not_empty(&cursor_region)) {
198                 ret = 0;
199                 goto out;
200         }
201
202         if (eid->sprite->image == EGL_NO_IMAGE_KHR)
203                 goto out;
204
205         if (eid->sprite->width > 64 || eid->sprite->height > 64)
206                 goto out;
207         
208         c->export_drm_image(c->base.display, eid->sprite->image,
209                             NULL, &handle, &stride);
210
211         if (stride != 64 * 4) {
212                 fprintf(stderr, "info: cursor stride is != 64\n");
213                 goto out;
214         }
215
216         ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64);
217         if (ret) {
218                 fprintf(stderr, "failed to set cursor: %s\n", strerror(-ret));
219                 goto out;
220         }
221
222         ret = drmModeMoveCursor(c->drm.fd, output->crtc_id,
223                                 eid->sprite->x - output->base.x,
224                                 eid->sprite->y - output->base.y);
225         if (ret) {
226                 fprintf(stderr, "failed to move cursor: %s\n", strerror(-ret));
227                 goto out;
228         }
229
230         printf("info: set hardware cursor\n");
231
232 out:
233         pixman_region32_fini(&cursor_region);
234         if (ret)
235                 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
236         return ret;
237 }
238
239 static int
240 on_drm_input(int fd, uint32_t mask, void *data)
241 {
242         drmEventContext evctx;
243
244         memset(&evctx, 0, sizeof evctx);
245         evctx.version = DRM_EVENT_CONTEXT_VERSION;
246         evctx.page_flip_handler = page_flip_handler;
247         drmHandleEvent(fd, &evctx);
248
249         return 1;
250 }
251
252 static int
253 init_egl(struct drm_compositor *ec, struct udev_device *device)
254 {
255         EGLint major, minor;
256         const char *extensions, *filename;
257         int fd;
258         static const EGLint context_attribs[] = {
259                 EGL_CONTEXT_CLIENT_VERSION, 2,
260                 EGL_NONE
261         };
262
263         filename = udev_device_get_devnode(device);
264         fd = open(filename, O_RDWR, O_CLOEXEC);
265         if (fd < 0) {
266                 /* Probably permissions error */
267                 fprintf(stderr, "couldn't open %s, skipping\n",
268                         udev_device_get_devnode(device));
269                 return -1;
270         }
271
272         ec->drm.fd = fd;
273         ec->base.display = eglGetDisplay(FD_TO_EGL_NATIVE_DPY(ec->drm.fd));
274         if (ec->base.display == NULL) {
275                 fprintf(stderr, "failed to create display\n");
276                 return -1;
277         }
278
279         if (!eglInitialize(ec->base.display, &major, &minor)) {
280                 fprintf(stderr, "failed to initialize display\n");
281                 return -1;
282         }
283
284         extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS);
285         if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
286                 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
287                 return -1;
288         }
289
290         if (!eglBindAPI(EGL_OPENGL_ES_API)) {
291                 fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n");
292                 return -1;
293         }
294
295         ec->base.context = eglCreateContext(ec->base.display, NULL,
296                                             EGL_NO_CONTEXT, context_attribs);
297         if (ec->base.context == NULL) {
298                 fprintf(stderr, "failed to create context\n");
299                 return -1;
300         }
301
302         if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE,
303                             EGL_NO_SURFACE, ec->base.context)) {
304                 fprintf(stderr, "failed to make context current\n");
305                 return -1;
306         }
307
308         return 0;
309 }
310
311 static drmModeModeInfo builtin_1024x768 = {
312         63500,                  /* clock */
313         1024, 1072, 1176, 1328, 0,
314         768, 771, 775, 798, 0,
315         59920,
316         DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
317         0,
318         "1024x768"
319 };
320
321 static int
322 create_output_for_connector(struct drm_compositor *ec,
323                             drmModeRes *resources,
324                             drmModeConnector *connector,
325                             int x, int y)
326 {
327         struct drm_output *output;
328         drmModeEncoder *encoder;
329         drmModeModeInfo *mode;
330         int i, ret;
331         EGLint handle, stride, attribs[] = {
332                 EGL_WIDTH,              0,
333                 EGL_HEIGHT,             0,
334                 EGL_DRM_BUFFER_FORMAT_MESA,     EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
335                 EGL_DRM_BUFFER_USE_MESA,        EGL_DRM_BUFFER_USE_SCANOUT_MESA,
336                 EGL_NONE
337         };
338
339         output = malloc(sizeof *output);
340         if (output == NULL)
341                 return -1;
342
343         if (connector->count_modes > 0) 
344                 mode = &connector->modes[0];
345         else
346                 mode = &builtin_1024x768;
347
348         encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
349         if (encoder == NULL) {
350                 fprintf(stderr, "No encoder for connector.\n");
351                 return -1;
352         }
353
354         for (i = 0; i < resources->count_crtcs; i++) {
355                 if (encoder->possible_crtcs & (1 << i) &&
356                     !(ec->crtc_allocator & (1 << resources->crtcs[i])))
357                         break;
358         }
359         if (i == resources->count_crtcs) {
360                 fprintf(stderr, "No usable crtc for encoder.\n");
361                 return -1;
362         }
363
364         memset(output, 0, sizeof *output);
365         wlsc_output_init(&output->base, &ec->base, x, y,
366                          mode->hdisplay, mode->vdisplay, 0);
367
368         output->crtc_id = resources->crtcs[i];
369         ec->crtc_allocator |= (1 << output->crtc_id);
370
371         output->connector_id = connector->connector_id;
372         ec->connector_allocator |= (1 << output->connector_id);
373         output->mode = *mode;
374
375         drmModeFreeEncoder(encoder);
376
377         glGenRenderbuffers(2, output->rbo);
378         for (i = 0; i < 2; i++) {
379                 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
380
381                 attribs[1] = output->base.width;
382                 attribs[3] = output->base.height;
383                 output->image[i] =
384                         ec->create_drm_image(ec->base.display, attribs);
385                 ec->base.image_target_renderbuffer_storage(GL_RENDERBUFFER,
386                                                            output->image[i]);
387                 ec->export_drm_image(ec->base.display, output->image[i],
388                                      NULL, &handle, &stride);
389
390                 ret = drmModeAddFB(ec->drm.fd,
391                                    output->base.width, output->base.height,
392                                    32, 32, stride, handle, &output->fb_id[i]);
393                 if (ret) {
394                         fprintf(stderr, "failed to add fb %d: %m\n", i);
395                         return -1;
396                 }
397         }
398
399         output->current = 0;
400         glFramebufferRenderbuffer(GL_FRAMEBUFFER,
401                                   GL_COLOR_ATTACHMENT0,
402                                   GL_RENDERBUFFER,
403                                   output->rbo[output->current]);
404         ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id,
405                              output->fb_id[output->current ^ 1], 0, 0,
406                              &output->connector_id, 1, &output->mode);
407         if (ret) {
408                 fprintf(stderr, "failed to set mode: %m\n");
409                 return -1;
410         }
411
412         output->scanout_surface = NULL;
413         output->base.prepare_render = drm_output_prepare_render;
414         output->base.present = drm_output_present;
415         output->base.prepare_scanout_surface =
416                 drm_output_prepare_scanout_surface;
417         output->base.set_hardware_cursor = drm_output_set_cursor;
418
419         wl_list_insert(ec->base.output_list.prev, &output->base.link);
420
421         return 0;
422 }
423
424 static int
425 create_outputs(struct drm_compositor *ec, int option_connector)
426 {
427         drmModeConnector *connector;
428         drmModeRes *resources;
429         int i;
430         int x = 0, y = 0;
431
432         resources = drmModeGetResources(ec->drm.fd);
433         if (!resources) {
434                 fprintf(stderr, "drmModeGetResources failed\n");
435                 return -1;
436         }
437
438         for (i = 0; i < resources->count_connectors; i++) {
439                 connector = drmModeGetConnector(ec->drm.fd, resources->connectors[i]);
440                 if (connector == NULL)
441                         continue;
442
443                 if (connector->connection == DRM_MODE_CONNECTED &&
444                     (option_connector == 0 ||
445                      connector->connector_id == option_connector))
446                         if (create_output_for_connector(ec, resources,
447                                                         connector, x, y) < 0)
448                                 return -1;
449
450                 x += container_of(ec->base.output_list.prev, struct wlsc_output,
451                                   link)->width;
452
453                 drmModeFreeConnector(connector);
454         }
455
456         if (wl_list_empty(&ec->base.output_list)) {
457                 fprintf(stderr, "No currently active connector found.\n");
458                 return -1;
459         }
460
461         drmModeFreeResources(resources);
462
463         return 0;
464 }
465
466 static int
467 destroy_output(struct drm_output *output)
468 {
469         struct drm_compositor *ec =
470                 (struct drm_compositor *) output->base.compositor;
471         int i;
472
473         glFramebufferRenderbuffer(GL_FRAMEBUFFER,
474                                   GL_COLOR_ATTACHMENT0,
475                                   GL_RENDERBUFFER,
476                                   0);
477
478         glBindRenderbuffer(GL_RENDERBUFFER, 0);
479         glDeleteRenderbuffers(2, output->rbo);
480
481         for (i = 0; i < 2; i++) {
482                 ec->base.destroy_image(ec->base.display, output->image[i]);
483                 drmModeRmFB(ec->drm.fd, output->fb_id[i]);
484         }
485         
486         ec->crtc_allocator &= ~(1 << output->crtc_id);
487         ec->connector_allocator &= ~(1 << output->connector_id);
488
489         wlsc_output_destroy(&output->base);
490         wl_list_remove(&output->base.link);
491
492         free(output);
493
494         return 0;
495 }
496
497 static void
498 update_outputs(struct drm_compositor *ec)
499 {
500         drmModeConnector *connector;
501         drmModeRes *resources;
502         struct drm_output *output, *next;
503         int x = 0, y = 0;
504         int x_offset = 0, y_offset = 0;
505         uint32_t connected = 0, disconnects = 0;
506         int i;
507
508         resources = drmModeGetResources(ec->drm.fd);
509         if (!resources) {
510                 fprintf(stderr, "drmModeGetResources failed\n");
511                 return;
512         }
513
514         /* collect new connects */
515         for (i = 0; i < resources->count_connectors; i++) {
516                 connector =
517                         drmModeGetConnector(ec->drm.fd,
518                                             resources->connectors[i]);
519                 if (connector == NULL ||
520                     connector->connection != DRM_MODE_CONNECTED)
521                         continue;
522
523                 connected |= (1 << connector->connector_id);
524                 
525                 if (!(ec->connector_allocator & (1 << connector->connector_id))) {
526                         struct wlsc_output *last_output =
527                                 container_of(ec->base.output_list.prev,
528                                              struct wlsc_output, link);
529
530                         /* XXX: not yet needed, we die with 0 outputs */
531                         if (!wl_list_empty(&ec->base.output_list))
532                                 x = last_output->x + last_output->width;
533                         else
534                                 x = 0;
535                         y = 0;
536                         create_output_for_connector(ec, resources,
537                                                     connector, x, y);
538                         printf("connector %d connected\n",
539                                connector->connector_id);
540                                 
541                 }
542                 drmModeFreeConnector(connector);
543         }
544         drmModeFreeResources(resources);
545
546         disconnects = ec->connector_allocator & ~connected;
547         if (disconnects) {
548                 wl_list_for_each_safe(output, next, &ec->base.output_list,
549                                       base.link) {
550                         if (x_offset != 0 || y_offset != 0) {
551                                 wlsc_output_move(&output->base,
552                                                  output->base.x - x_offset,
553                                                  output->base.y - y_offset);
554                         }
555
556                         if (disconnects & (1 << output->connector_id)) {
557                                 disconnects &= ~(1 << output->connector_id);
558                                 printf("connector %d disconnected\n",
559                                        output->connector_id);
560                                 x_offset += output->base.width;
561                                 destroy_output(output);
562                         }
563                 }
564         }
565
566         /* FIXME: handle zero outputs, without terminating */   
567         if (ec->connector_allocator == 0)
568                 wl_display_terminate(ec->base.wl_display);
569 }
570
571 static int
572 udev_event_is_hotplug(struct udev_device *device)
573 {
574         struct udev_list_entry *list, *hotplug_entry;
575         
576         list = udev_device_get_properties_list_entry(device);
577
578         hotplug_entry = udev_list_entry_get_by_name(list, "HOTPLUG");
579         if (hotplug_entry == NULL)
580                 return 0;
581
582         return strcmp(udev_list_entry_get_value(hotplug_entry), "1") == 0;
583 }
584
585 static int
586 udev_drm_event(int fd, uint32_t mask, void *data)
587 {
588         struct drm_compositor *ec = data;
589         struct udev_device *event;
590
591         event = udev_monitor_receive_device(ec->udev_monitor);
592         
593         if (udev_event_is_hotplug(event))
594                 update_outputs(ec);
595
596         udev_device_unref(event);
597
598         return 1;
599 }
600
601 static EGLImageKHR
602 drm_compositor_create_cursor_image(struct wlsc_compositor *ec,
603                                    int32_t width, int32_t height)
604 {
605         static const EGLint image_attribs[] = {
606                 EGL_WIDTH, 64,
607                 EGL_HEIGHT, 64,
608                 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
609                 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_CURSOR_MESA,
610                 EGL_NONE
611         };
612         struct drm_compositor *c = (struct drm_compositor *) ec;
613
614         if (width > 64 || height > 64)
615                 return EGL_NO_IMAGE_KHR;
616
617         return c->create_drm_image(ec->display, image_attribs);
618 }
619
620 static void
621 drm_destroy(struct wlsc_compositor *ec)
622 {
623         struct drm_compositor *d = (struct drm_compositor *) ec;
624
625         tty_destroy(d->tty);
626
627         free(d);
628 }
629
630 static void
631 vt_func(struct wlsc_compositor *compositor, int event)
632 {
633         struct drm_compositor *ec = (struct drm_compositor *) compositor;
634         struct wlsc_output *output;
635
636         switch (event) {
637         case TTY_ENTER_VT:
638                 compositor->focus = 1;
639                 drmSetMaster(ec->drm.fd);
640                 compositor->state = WLSC_COMPOSITOR_ACTIVE;
641                 wlsc_compositor_damage_all(compositor);
642                 break;
643         case TTY_LEAVE_VT:
644                 compositor->focus = 0;
645                 compositor->state = WLSC_COMPOSITOR_SLEEPING;
646                 drmDropMaster(ec->drm.fd);
647
648                 wl_list_for_each(output, &ec->base.output_list, link)
649                         drm_output_set_cursor(output, NULL);
650
651                 break;
652         };
653 }
654
655 static struct wlsc_compositor *
656 drm_compositor_create(struct wl_display *display, int connector)
657 {
658         struct drm_compositor *ec;
659         struct udev_enumerate *e;
660         struct udev_list_entry *entry;
661         struct udev_device *device;
662         const char *path;
663         struct wl_event_loop *loop;
664
665         ec = malloc(sizeof *ec);
666         if (ec == NULL)
667                 return NULL;
668
669         memset(ec, 0, sizeof *ec);
670         ec->udev = udev_new();
671         if (ec->udev == NULL) {
672                 fprintf(stderr, "failed to initialize udev context\n");
673                 return NULL;
674         }
675
676         e = udev_enumerate_new(ec->udev);
677         udev_enumerate_add_match_subsystem(e, "drm");
678         udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
679         udev_enumerate_scan_devices(e);
680         device = NULL;
681         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
682                 path = udev_list_entry_get_name(entry);
683                 device = udev_device_new_from_syspath(ec->udev, path);
684                 break;
685         }
686         udev_enumerate_unref(e);
687
688         if (device == NULL) {
689                 fprintf(stderr, "no drm device found\n");
690                 return NULL;
691         }
692
693         ec->base.wl_display = display;
694         if (init_egl(ec, device) < 0) {
695                 fprintf(stderr, "failed to initialize egl\n");
696                 return NULL;
697         }
698
699         ec->base.destroy = drm_destroy;
700         ec->base.create_cursor_image = drm_compositor_create_cursor_image;
701
702         ec->base.focus = 1;
703
704         glGenFramebuffers(1, &ec->base.fbo);
705         glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo);
706
707         ec->create_drm_image =
708                 (void *) eglGetProcAddress("eglCreateDRMImageMESA");
709         ec->export_drm_image =
710                 (void *) eglGetProcAddress("eglExportDRMImageMESA");
711
712         /* Can't init base class until we have a current egl context */
713         if (wlsc_compositor_init(&ec->base, display) < 0)
714                 return NULL;
715
716         if (create_outputs(ec, connector) < 0) {
717                 fprintf(stderr, "failed to create output for %s\n", path);
718                 return NULL;
719         }
720
721         evdev_input_add_devices(&ec->base, ec->udev);
722
723         loop = wl_display_get_event_loop(ec->base.wl_display);
724         ec->drm_source =
725                 wl_event_loop_add_fd(loop, ec->drm.fd,
726                                      WL_EVENT_READABLE, on_drm_input, ec);
727         ec->tty = tty_create(&ec->base, vt_func);
728
729         ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
730         if (ec->udev_monitor == NULL) {
731                 fprintf(stderr, "failed to intialize udev monitor\n");
732                 return NULL;
733         }
734         udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
735                                                         "drm", NULL);
736         ec->udev_drm_source =
737                 wl_event_loop_add_fd(loop, udev_monitor_get_fd(ec->udev_monitor),
738                                      WL_EVENT_READABLE, udev_drm_event, ec);
739
740         if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
741                 fprintf(stderr, "failed to enable udev-monitor receiving\n");
742                 return NULL;
743         }
744
745         return &ec->base;
746 }
747
748 struct wlsc_compositor *
749 backend_init(struct wl_display *display, char *options);
750
751 WL_EXPORT struct wlsc_compositor *
752 backend_init(struct wl_display *display, char *options)
753 {
754         int connector = 0, i;
755         char *p, *value;
756
757         static char * const tokens[] = { "connector", NULL };
758         
759         p = options;
760         while (i = getsubopt(&p, tokens, &value), i != -1) {
761                 switch (i) {
762                 case 0:
763                         connector = strtol(value, NULL, 0);
764                         break;
765                 }
766         }
767
768         return drm_compositor_create(display, connector);
769 }