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