compositor-drm: Ignore disconnected connectors width
[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 <gbm.h>
31
32 #include "compositor.h"
33
34 struct drm_compositor {
35         struct wlsc_compositor base;
36
37         struct udev *udev;
38         struct wl_event_source *drm_source;
39
40         struct udev_monitor *udev_monitor;
41         struct wl_event_source *udev_drm_source;
42
43         struct {
44                 int fd;
45         } drm;
46         struct gbm_device *gbm;
47         uint32_t crtc_allocator;
48         uint32_t connector_allocator;
49         struct tty *tty;
50 };
51
52 struct drm_mode {
53         struct wlsc_mode base;
54         drmModeModeInfo mode_info;
55 };
56
57 struct drm_output {
58         struct wlsc_output   base;
59
60         uint32_t crtc_id;
61         uint32_t connector_id;
62         GLuint rbo[2];
63         uint32_t fb_id[2];
64         EGLImageKHR image[2];
65         struct gbm_bo *bo[2];
66         uint32_t current;       
67
68         struct wlsc_surface *scanout_surface;
69
70         uint32_t fs_surf_fb_id;
71         uint32_t pending_fs_surf_fb_id;
72 };
73
74 static int
75 drm_output_prepare_render(struct wlsc_output *output_base)
76 {
77         struct drm_output *output = (struct drm_output *) output_base;
78
79         glFramebufferRenderbuffer(GL_FRAMEBUFFER,
80                                   GL_COLOR_ATTACHMENT0,
81                                   GL_RENDERBUFFER,
82                                   output->rbo[output->current]);
83
84         if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
85                 return -1;
86
87         return 0;
88 }
89
90 static int
91 drm_output_present(struct wlsc_output *output_base)
92 {
93         struct drm_output *output = (struct drm_output *) output_base;
94         struct drm_compositor *c =
95                 (struct drm_compositor *) output->base.compositor;
96         uint32_t fb_id = 0;
97
98         if (drm_output_prepare_render(&output->base))
99                 return -1;
100         glFlush();
101
102         output->current ^= 1;
103
104         if (output->scanout_surface != NULL) {
105                 output->scanout_surface = NULL;
106                 fb_id = output->fs_surf_fb_id;
107         } else {
108                 fb_id = output->fb_id[output->current ^ 1];
109         }
110
111         drmModePageFlip(c->drm.fd, output->crtc_id,
112                         fb_id,
113                         DRM_MODE_PAGE_FLIP_EVENT, output);
114
115         return 0;
116 }
117
118 static void
119 page_flip_handler(int fd, unsigned int frame,
120                   unsigned int sec, unsigned int usec, void *data)
121 {
122         struct drm_output *output = (struct drm_output *) data;
123         struct drm_compositor *c =
124                 (struct drm_compositor *) output->base.compositor;
125         uint32_t msecs;
126
127         if (output->pending_fs_surf_fb_id) {
128                 drmModeRmFB(c->drm.fd, output->pending_fs_surf_fb_id);
129                 output->pending_fs_surf_fb_id = 0;
130         }
131
132         if (output->fs_surf_fb_id) {
133                 output->pending_fs_surf_fb_id = output->fs_surf_fb_id;
134                 output->fs_surf_fb_id = 0;
135         }
136
137         msecs = sec * 1000 + usec / 1000;
138         wlsc_output_finish_frame(&output->base, msecs);
139 }
140
141 static int
142 drm_output_prepare_scanout_surface(struct wlsc_output *output_base,
143                                    struct wlsc_surface *es)
144 {
145         struct drm_output *output = (struct drm_output *) output_base;
146         struct drm_compositor *c =
147                 (struct drm_compositor *) output->base.compositor;
148         EGLint handle, stride;
149         int ret;
150         uint32_t fb_id = 0;
151         struct gbm_bo *bo;
152
153         if (es->x != output->base.x ||
154             es->y != output->base.y ||
155             es->width != output->base.current->width ||
156             es->height != output->base.current->height ||
157             es->image == EGL_NO_IMAGE_KHR)
158                 return -1;
159
160         bo = gbm_bo_create_from_egl_image(c->gbm,
161                                           c->base.display, es->image,
162                                           es->width, es->height,
163                                           GBM_BO_USE_SCANOUT);
164
165         handle = gbm_bo_get_handle(bo).s32;
166         stride = gbm_bo_get_pitch(bo);
167
168         gbm_bo_destroy(bo);
169
170         if (handle == 0)
171                 return -1;
172
173         ret = drmModeAddFB(c->drm.fd,
174                            output->base.current->width,
175                            output->base.current->height,
176                            32, 32, stride, handle, &fb_id);
177
178         if (ret)
179                 return -1;
180
181         output->fs_surf_fb_id = fb_id;
182         output->scanout_surface = es;
183
184         return 0;
185 }
186
187 static int
188 drm_output_set_cursor(struct wlsc_output *output_base,
189                       struct wlsc_input_device *eid)
190 {
191         struct drm_output *output = (struct drm_output *) output_base;
192         struct drm_compositor *c =
193                 (struct drm_compositor *) output->base.compositor;
194         EGLint handle, stride;
195         int ret = -1;
196         pixman_region32_t cursor_region;
197         struct gbm_bo *bo;
198
199         if (eid == NULL) {
200                 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
201                 return 0;
202         }
203
204         pixman_region32_init_rect(&cursor_region,
205                                   eid->sprite->x, eid->sprite->y,
206                                   eid->sprite->width, eid->sprite->height);
207
208         pixman_region32_intersect_rect(&cursor_region, &cursor_region,
209                                        output->base.x, output->base.y,
210                                        output->base.current->width,
211                                        output->base.current->height);
212
213         if (!pixman_region32_not_empty(&cursor_region))
214                 goto out;
215
216         if (eid->sprite->image == EGL_NO_IMAGE_KHR)
217                 goto out;
218
219         if (eid->sprite->width > 64 || eid->sprite->height > 64)
220                 goto out;
221         
222         bo = gbm_bo_create_from_egl_image(c->gbm,
223                                           c->base.display,
224                                           eid->sprite->image, 64, 64,
225                                           GBM_BO_USE_CURSOR_64X64);
226
227         handle = gbm_bo_get_handle(bo).s32;
228         stride = gbm_bo_get_pitch(bo);
229
230         gbm_bo_destroy(bo);
231
232         if (stride != 64 * 4) {
233                 fprintf(stderr, "info: cursor stride is != 64\n");
234                 goto out;
235         }
236
237         ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64);
238         if (ret) {
239                 fprintf(stderr, "failed to set cursor: %s\n", strerror(-ret));
240                 goto out;
241         }
242
243         ret = drmModeMoveCursor(c->drm.fd, output->crtc_id,
244                                 eid->sprite->x - output->base.x,
245                                 eid->sprite->y - output->base.y);
246         if (ret) {
247                 fprintf(stderr, "failed to move cursor: %s\n", strerror(-ret));
248                 goto out;
249         }
250
251 out:
252         pixman_region32_fini(&cursor_region);
253         if (ret)
254                 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
255         return ret;
256 }
257
258 static int
259 on_drm_input(int fd, uint32_t mask, void *data)
260 {
261         drmEventContext evctx;
262
263         memset(&evctx, 0, sizeof evctx);
264         evctx.version = DRM_EVENT_CONTEXT_VERSION;
265         evctx.page_flip_handler = page_flip_handler;
266         drmHandleEvent(fd, &evctx);
267
268         return 1;
269 }
270
271 static int
272 init_egl(struct drm_compositor *ec, struct udev_device *device)
273 {
274         EGLint major, minor;
275         const char *extensions, *filename;
276         int fd;
277         static const EGLint context_attribs[] = {
278                 EGL_CONTEXT_CLIENT_VERSION, 2,
279                 EGL_NONE
280         };
281
282         filename = udev_device_get_devnode(device);
283         fd = open(filename, O_RDWR, O_CLOEXEC);
284         if (fd < 0) {
285                 /* Probably permissions error */
286                 fprintf(stderr, "couldn't open %s, skipping\n",
287                         udev_device_get_devnode(device));
288                 return -1;
289         }
290
291         setenv("EGL_PLATFORM", "drm", 1);
292         ec->drm.fd = fd;
293         ec->gbm = gbm_create_device(ec->drm.fd);
294         ec->base.display = eglGetDisplay(ec->gbm);
295         if (ec->base.display == NULL) {
296                 fprintf(stderr, "failed to create display\n");
297                 return -1;
298         }
299
300         if (!eglInitialize(ec->base.display, &major, &minor)) {
301                 fprintf(stderr, "failed to initialize display\n");
302                 return -1;
303         }
304
305         extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS);
306         if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
307                 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
308                 return -1;
309         }
310
311         if (!eglBindAPI(EGL_OPENGL_ES_API)) {
312                 fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n");
313                 return -1;
314         }
315
316         ec->base.context = eglCreateContext(ec->base.display, NULL,
317                                             EGL_NO_CONTEXT, context_attribs);
318         if (ec->base.context == NULL) {
319                 fprintf(stderr, "failed to create context\n");
320                 return -1;
321         }
322
323         if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE,
324                             EGL_NO_SURFACE, ec->base.context)) {
325                 fprintf(stderr, "failed to make context current\n");
326                 return -1;
327         }
328
329         return 0;
330 }
331
332 static drmModeModeInfo builtin_1024x768 = {
333         63500,                  /* clock */
334         1024, 1072, 1176, 1328, 0,
335         768, 771, 775, 798, 0,
336         59920,
337         DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
338         0,
339         "1024x768"
340 };
341
342
343 static int
344 drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info)
345 {
346         struct drm_mode *mode;
347
348         mode = malloc(sizeof *mode);
349         if (mode == NULL)
350                 return -1;
351
352         mode->base.flags = 0;
353         mode->base.width = info->hdisplay;
354         mode->base.height = info->vdisplay;
355         mode->base.refresh = info->vrefresh;
356         mode->mode_info = *info;
357         wl_list_insert(output->base.mode_list.prev, &mode->base.link);
358
359         return 0;
360 }
361
362 static int
363 drm_subpixel_to_wayland(int drm_value)
364 {
365         switch (drm_value) {
366         default:
367         case DRM_MODE_SUBPIXEL_UNKNOWN:
368                 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
369         case DRM_MODE_SUBPIXEL_NONE:
370                 return WL_OUTPUT_SUBPIXEL_NONE;
371         case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
372                 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
373         case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
374                 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
375         case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
376                 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
377         case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
378                 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
379         }
380 }
381
382 static int
383 create_output_for_connector(struct drm_compositor *ec,
384                             drmModeRes *resources,
385                             drmModeConnector *connector,
386                             int x, int y)
387 {
388         struct drm_output *output;
389         struct drm_mode *drm_mode;
390         drmModeEncoder *encoder;
391         int i, ret;
392         unsigned handle, stride;
393
394         encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
395         if (encoder == NULL) {
396                 fprintf(stderr, "No encoder for connector.\n");
397                 return -1;
398         }
399
400         for (i = 0; i < resources->count_crtcs; i++) {
401                 if (encoder->possible_crtcs & (1 << i) &&
402                     !(ec->crtc_allocator & (1 << resources->crtcs[i])))
403                         break;
404         }
405         if (i == resources->count_crtcs) {
406                 fprintf(stderr, "No usable crtc for encoder.\n");
407                 return -1;
408         }
409
410         output = malloc(sizeof *output);
411         if (output == NULL)
412                 return -1;
413
414         memset(output, 0, sizeof *output);
415         output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
416         output->base.make = "unknown";
417         output->base.model = "unknown";
418         wl_list_init(&output->base.mode_list);
419
420         output->crtc_id = resources->crtcs[i];
421         ec->crtc_allocator |= (1 << output->crtc_id);
422         output->connector_id = connector->connector_id;
423         ec->connector_allocator |= (1 << output->connector_id);
424
425         for (i = 0; i < connector->count_modes; i++)
426                 drm_output_add_mode(output, &connector->modes[i]);
427         if (connector->count_modes == 0)
428                 drm_output_add_mode(output, &builtin_1024x768);
429
430         drm_mode = container_of(output->base.mode_list.next,
431                                 struct drm_mode, base.link);
432         output->base.current = &drm_mode->base;
433         drm_mode->base.flags =
434                 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
435
436         wlsc_output_init(&output->base, &ec->base, x, y,
437                          connector->mmWidth, connector->mmHeight, 0);
438
439         wl_list_insert(ec->base.output_list.prev, &output->base.link);
440
441         drmModeFreeEncoder(encoder);
442
443         glGenRenderbuffers(2, output->rbo);
444         for (i = 0; i < 2; i++) {
445                 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
446
447                 output->bo[i] =
448                         gbm_bo_create(ec->gbm,
449                                       output->base.current->width,
450                                       output->base.current->height,
451                                       GBM_BO_FORMAT_XRGB8888,
452                                       GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
453                 output->image[i] = ec->base.create_image(ec->base.display,
454                                                          NULL,
455                                                          EGL_NATIVE_PIXMAP_KHR,
456                                                          output->bo[i], NULL);
457
458
459                 ec->base.image_target_renderbuffer_storage(GL_RENDERBUFFER,
460                                                            output->image[i]);
461                 stride = gbm_bo_get_pitch(output->bo[i]);
462                 handle = gbm_bo_get_handle(output->bo[i]).u32;
463
464                 ret = drmModeAddFB(ec->drm.fd,
465                                    output->base.current->width,
466                                    output->base.current->height,
467                                    32, 32, stride, handle, &output->fb_id[i]);
468                 if (ret) {
469                         fprintf(stderr, "failed to add fb %d: %m\n", i);
470                         return -1;
471                 }
472         }
473
474         output->current = 0;
475         glFramebufferRenderbuffer(GL_FRAMEBUFFER,
476                                   GL_COLOR_ATTACHMENT0,
477                                   GL_RENDERBUFFER,
478                                   output->rbo[output->current]);
479         ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id,
480                              output->fb_id[output->current ^ 1], 0, 0,
481                              &output->connector_id, 1,
482                              &drm_mode->mode_info);
483         if (ret) {
484                 fprintf(stderr, "failed to set mode: %m\n");
485                 return -1;
486         }
487
488         output->scanout_surface = NULL;
489         output->base.prepare_render = drm_output_prepare_render;
490         output->base.present = drm_output_present;
491         output->base.prepare_scanout_surface =
492                 drm_output_prepare_scanout_surface;
493         output->base.set_hardware_cursor = drm_output_set_cursor;
494
495         return 0;
496 }
497
498 static int
499 create_outputs(struct drm_compositor *ec, int option_connector)
500 {
501         drmModeConnector *connector;
502         drmModeRes *resources;
503         int i;
504         int x = 0, y = 0;
505
506         resources = drmModeGetResources(ec->drm.fd);
507         if (!resources) {
508                 fprintf(stderr, "drmModeGetResources failed\n");
509                 return -1;
510         }
511
512         for (i = 0; i < resources->count_connectors; i++) {
513                 connector = drmModeGetConnector(ec->drm.fd, resources->connectors[i]);
514                 if (connector == NULL)
515                         continue;
516
517                 if (connector->connection == DRM_MODE_CONNECTED &&
518                     (option_connector == 0 ||
519                      connector->connector_id == option_connector)) {
520                         if (create_output_for_connector(ec, resources,
521                                                         connector, x, y) < 0)
522                                 return -1;
523
524                         x += container_of(ec->base.output_list.prev,
525                                           struct wlsc_output,
526                                           link)->current->width;
527                 }
528
529                 drmModeFreeConnector(connector);
530         }
531
532         if (wl_list_empty(&ec->base.output_list)) {
533                 fprintf(stderr, "No currently active connector found.\n");
534                 return -1;
535         }
536
537         drmModeFreeResources(resources);
538
539         return 0;
540 }
541
542 static int
543 destroy_output(struct drm_output *output)
544 {
545         struct drm_compositor *ec =
546                 (struct drm_compositor *) output->base.compositor;
547         int i;
548
549         glFramebufferRenderbuffer(GL_FRAMEBUFFER,
550                                   GL_COLOR_ATTACHMENT0,
551                                   GL_RENDERBUFFER,
552                                   0);
553
554         glBindRenderbuffer(GL_RENDERBUFFER, 0);
555         glDeleteRenderbuffers(2, output->rbo);
556
557         for (i = 0; i < 2; i++) {
558                 ec->base.destroy_image(ec->base.display, output->image[i]);
559                 drmModeRmFB(ec->drm.fd, output->fb_id[i]);
560         }
561         
562         ec->crtc_allocator &= ~(1 << output->crtc_id);
563         ec->connector_allocator &= ~(1 << output->connector_id);
564
565         wlsc_output_destroy(&output->base);
566         wl_list_remove(&output->base.link);
567
568         free(output);
569
570         return 0;
571 }
572
573 static void
574 update_outputs(struct drm_compositor *ec)
575 {
576         drmModeConnector *connector;
577         drmModeRes *resources;
578         struct drm_output *output, *next;
579         int x = 0, y = 0;
580         int x_offset = 0, y_offset = 0;
581         uint32_t connected = 0, disconnects = 0;
582         int i;
583
584         resources = drmModeGetResources(ec->drm.fd);
585         if (!resources) {
586                 fprintf(stderr, "drmModeGetResources failed\n");
587                 return;
588         }
589
590         /* collect new connects */
591         for (i = 0; i < resources->count_connectors; i++) {
592                 connector =
593                         drmModeGetConnector(ec->drm.fd,
594                                             resources->connectors[i]);
595                 if (connector == NULL ||
596                     connector->connection != DRM_MODE_CONNECTED)
597                         continue;
598
599                 connected |= (1 << connector->connector_id);
600                 
601                 if (!(ec->connector_allocator & (1 << connector->connector_id))) {
602                         struct wlsc_output *last_output =
603                                 container_of(ec->base.output_list.prev,
604                                              struct wlsc_output, link);
605
606                         /* XXX: not yet needed, we die with 0 outputs */
607                         if (!wl_list_empty(&ec->base.output_list))
608                                 x = last_output->x + last_output->current->width;
609                         else
610                                 x = 0;
611                         y = 0;
612                         create_output_for_connector(ec, resources,
613                                                     connector, x, y);
614                         printf("connector %d connected\n",
615                                connector->connector_id);
616                                 
617                 }
618                 drmModeFreeConnector(connector);
619         }
620         drmModeFreeResources(resources);
621
622         disconnects = ec->connector_allocator & ~connected;
623         if (disconnects) {
624                 wl_list_for_each_safe(output, next, &ec->base.output_list,
625                                       base.link) {
626                         if (x_offset != 0 || y_offset != 0) {
627                                 wlsc_output_move(&output->base,
628                                                  output->base.x - x_offset,
629                                                  output->base.y - y_offset);
630                         }
631
632                         if (disconnects & (1 << output->connector_id)) {
633                                 disconnects &= ~(1 << output->connector_id);
634                                 printf("connector %d disconnected\n",
635                                        output->connector_id);
636                                 x_offset += output->base.current->width;
637                                 destroy_output(output);
638                         }
639                 }
640         }
641
642         /* FIXME: handle zero outputs, without terminating */   
643         if (ec->connector_allocator == 0)
644                 wl_display_terminate(ec->base.wl_display);
645 }
646
647 static int
648 udev_event_is_hotplug(struct udev_device *device)
649 {
650         struct udev_list_entry *list, *hotplug_entry;
651         
652         list = udev_device_get_properties_list_entry(device);
653
654         hotplug_entry = udev_list_entry_get_by_name(list, "HOTPLUG");
655         if (hotplug_entry == NULL)
656                 return 0;
657
658         return strcmp(udev_list_entry_get_value(hotplug_entry), "1") == 0;
659 }
660
661 static int
662 udev_drm_event(int fd, uint32_t mask, void *data)
663 {
664         struct drm_compositor *ec = data;
665         struct udev_device *event;
666
667         event = udev_monitor_receive_device(ec->udev_monitor);
668         
669         if (udev_event_is_hotplug(event))
670                 update_outputs(ec);
671
672         udev_device_unref(event);
673
674         return 1;
675 }
676
677 static EGLImageKHR
678 drm_compositor_create_cursor_image(struct wlsc_compositor *ec,
679                                    int32_t width, int32_t height)
680 {
681         struct drm_compositor *c = (struct drm_compositor *) ec;
682         struct gbm_bo *bo;
683         EGLImageKHR image;
684
685         if (width > 64 || height > 64)
686                 return EGL_NO_IMAGE_KHR;
687
688         bo = gbm_bo_create(c->gbm,
689                            /* width, height, */ 64, 64,
690                            GBM_BO_FORMAT_ARGB8888,
691                            GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_RENDERING);
692
693         image = ec->create_image(c->base.display, NULL,
694                                  EGL_NATIVE_PIXMAP_KHR, bo, NULL);
695         gbm_bo_destroy(bo);
696
697         return image;
698 }
699
700 static void
701 drm_destroy(struct wlsc_compositor *ec)
702 {
703         struct drm_compositor *d = (struct drm_compositor *) ec;
704
705         tty_destroy(d->tty);
706
707         free(d);
708 }
709
710 static void
711 vt_func(struct wlsc_compositor *compositor, int event)
712 {
713         struct drm_compositor *ec = (struct drm_compositor *) compositor;
714         struct wlsc_output *output;
715
716         switch (event) {
717         case TTY_ENTER_VT:
718                 compositor->focus = 1;
719                 drmSetMaster(ec->drm.fd);
720                 compositor->state = WLSC_COMPOSITOR_ACTIVE;
721                 wlsc_compositor_damage_all(compositor);
722                 break;
723         case TTY_LEAVE_VT:
724                 compositor->focus = 0;
725                 compositor->state = WLSC_COMPOSITOR_SLEEPING;
726                 drmDropMaster(ec->drm.fd);
727
728                 wl_list_for_each(output, &ec->base.output_list, link)
729                         drm_output_set_cursor(output, NULL);
730
731                 break;
732         };
733 }
734
735 static const char default_seat[] = "seat0";
736
737 static struct wlsc_compositor *
738 drm_compositor_create(struct wl_display *display,
739                       int connector, const char *seat)
740 {
741         struct drm_compositor *ec;
742         struct udev_enumerate *e;
743         struct udev_list_entry *entry;
744         struct udev_device *device, *drm_device;
745         const char *path, *device_seat;
746         struct wl_event_loop *loop;
747
748         ec = malloc(sizeof *ec);
749         if (ec == NULL)
750                 return NULL;
751
752         memset(ec, 0, sizeof *ec);
753         ec->udev = udev_new();
754         if (ec->udev == NULL) {
755                 fprintf(stderr, "failed to initialize udev context\n");
756                 return NULL;
757         }
758
759         e = udev_enumerate_new(ec->udev);
760         udev_enumerate_add_match_subsystem(e, "drm");
761
762         udev_enumerate_scan_devices(e);
763         drm_device = NULL;
764         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
765                 path = udev_list_entry_get_name(entry);
766                 device = udev_device_new_from_syspath(ec->udev, path);
767                 device_seat =
768                         udev_device_get_property_value(device, "ID_SEAT");
769                 if (!device_seat)
770                         device_seat = default_seat;
771                 if (strcmp(device_seat, seat) == 0) {
772                         drm_device = device;
773                         break;
774                 }
775                 udev_device_unref(device);
776         }
777
778         udev_enumerate_unref(e);
779
780         if (drm_device == NULL) {
781                 fprintf(stderr, "no drm device found\n");
782                 return NULL;
783         }
784
785         ec->base.wl_display = display;
786         if (init_egl(ec, drm_device) < 0) {
787                 fprintf(stderr, "failed to initialize egl\n");
788                 return NULL;
789         }
790
791         udev_device_unref(drm_device);
792
793         ec->base.destroy = drm_destroy;
794         ec->base.create_cursor_image = drm_compositor_create_cursor_image;
795
796         ec->base.focus = 1;
797
798         glGenFramebuffers(1, &ec->base.fbo);
799         glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo);
800
801         /* Can't init base class until we have a current egl context */
802         if (wlsc_compositor_init(&ec->base, display) < 0)
803                 return NULL;
804
805         if (create_outputs(ec, connector) < 0) {
806                 fprintf(stderr, "failed to create output for %s\n", path);
807                 return NULL;
808         }
809
810         evdev_input_add_devices(&ec->base, ec->udev, seat);
811
812         loop = wl_display_get_event_loop(ec->base.wl_display);
813         ec->drm_source =
814                 wl_event_loop_add_fd(loop, ec->drm.fd,
815                                      WL_EVENT_READABLE, on_drm_input, ec);
816         ec->tty = tty_create(&ec->base, vt_func);
817
818         ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
819         if (ec->udev_monitor == NULL) {
820                 fprintf(stderr, "failed to intialize udev monitor\n");
821                 return NULL;
822         }
823         udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
824                                                         "drm", NULL);
825         ec->udev_drm_source =
826                 wl_event_loop_add_fd(loop, udev_monitor_get_fd(ec->udev_monitor),
827                                      WL_EVENT_READABLE, udev_drm_event, ec);
828
829         if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
830                 fprintf(stderr, "failed to enable udev-monitor receiving\n");
831                 return NULL;
832         }
833
834         return &ec->base;
835 }
836
837 struct wlsc_compositor *
838 backend_init(struct wl_display *display, char *options);
839
840 WL_EXPORT struct wlsc_compositor *
841 backend_init(struct wl_display *display, char *options)
842 {
843         int connector = 0, i;
844         const char *seat;
845         char *p, *value;
846
847         static char * const tokens[] = { "connector", "seat", NULL };
848         
849         p = options;
850         seat = default_seat;
851         while (i = getsubopt(&p, tokens, &value), i != -1) {
852                 switch (i) {
853                 case 0:
854                         connector = strtol(value, NULL, 0);
855                         break;
856                 case 1:
857                         seat = value;
858                         break;
859                 }
860         }
861
862         return drm_compositor_create(display, connector, seat);
863 }