compositor-drm: Drop cursor debug message
[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, struct wlsc_output,
525                                   link)->current->width;
526
527                 drmModeFreeConnector(connector);
528         }
529
530         if (wl_list_empty(&ec->base.output_list)) {
531                 fprintf(stderr, "No currently active connector found.\n");
532                 return -1;
533         }
534
535         drmModeFreeResources(resources);
536
537         return 0;
538 }
539
540 static int
541 destroy_output(struct drm_output *output)
542 {
543         struct drm_compositor *ec =
544                 (struct drm_compositor *) output->base.compositor;
545         int i;
546
547         glFramebufferRenderbuffer(GL_FRAMEBUFFER,
548                                   GL_COLOR_ATTACHMENT0,
549                                   GL_RENDERBUFFER,
550                                   0);
551
552         glBindRenderbuffer(GL_RENDERBUFFER, 0);
553         glDeleteRenderbuffers(2, output->rbo);
554
555         for (i = 0; i < 2; i++) {
556                 ec->base.destroy_image(ec->base.display, output->image[i]);
557                 drmModeRmFB(ec->drm.fd, output->fb_id[i]);
558         }
559         
560         ec->crtc_allocator &= ~(1 << output->crtc_id);
561         ec->connector_allocator &= ~(1 << output->connector_id);
562
563         wlsc_output_destroy(&output->base);
564         wl_list_remove(&output->base.link);
565
566         free(output);
567
568         return 0;
569 }
570
571 static void
572 update_outputs(struct drm_compositor *ec)
573 {
574         drmModeConnector *connector;
575         drmModeRes *resources;
576         struct drm_output *output, *next;
577         int x = 0, y = 0;
578         int x_offset = 0, y_offset = 0;
579         uint32_t connected = 0, disconnects = 0;
580         int i;
581
582         resources = drmModeGetResources(ec->drm.fd);
583         if (!resources) {
584                 fprintf(stderr, "drmModeGetResources failed\n");
585                 return;
586         }
587
588         /* collect new connects */
589         for (i = 0; i < resources->count_connectors; i++) {
590                 connector =
591                         drmModeGetConnector(ec->drm.fd,
592                                             resources->connectors[i]);
593                 if (connector == NULL ||
594                     connector->connection != DRM_MODE_CONNECTED)
595                         continue;
596
597                 connected |= (1 << connector->connector_id);
598                 
599                 if (!(ec->connector_allocator & (1 << connector->connector_id))) {
600                         struct wlsc_output *last_output =
601                                 container_of(ec->base.output_list.prev,
602                                              struct wlsc_output, link);
603
604                         /* XXX: not yet needed, we die with 0 outputs */
605                         if (!wl_list_empty(&ec->base.output_list))
606                                 x = last_output->x + last_output->current->width;
607                         else
608                                 x = 0;
609                         y = 0;
610                         create_output_for_connector(ec, resources,
611                                                     connector, x, y);
612                         printf("connector %d connected\n",
613                                connector->connector_id);
614                                 
615                 }
616                 drmModeFreeConnector(connector);
617         }
618         drmModeFreeResources(resources);
619
620         disconnects = ec->connector_allocator & ~connected;
621         if (disconnects) {
622                 wl_list_for_each_safe(output, next, &ec->base.output_list,
623                                       base.link) {
624                         if (x_offset != 0 || y_offset != 0) {
625                                 wlsc_output_move(&output->base,
626                                                  output->base.x - x_offset,
627                                                  output->base.y - y_offset);
628                         }
629
630                         if (disconnects & (1 << output->connector_id)) {
631                                 disconnects &= ~(1 << output->connector_id);
632                                 printf("connector %d disconnected\n",
633                                        output->connector_id);
634                                 x_offset += output->base.current->width;
635                                 destroy_output(output);
636                         }
637                 }
638         }
639
640         /* FIXME: handle zero outputs, without terminating */   
641         if (ec->connector_allocator == 0)
642                 wl_display_terminate(ec->base.wl_display);
643 }
644
645 static int
646 udev_event_is_hotplug(struct udev_device *device)
647 {
648         struct udev_list_entry *list, *hotplug_entry;
649         
650         list = udev_device_get_properties_list_entry(device);
651
652         hotplug_entry = udev_list_entry_get_by_name(list, "HOTPLUG");
653         if (hotplug_entry == NULL)
654                 return 0;
655
656         return strcmp(udev_list_entry_get_value(hotplug_entry), "1") == 0;
657 }
658
659 static int
660 udev_drm_event(int fd, uint32_t mask, void *data)
661 {
662         struct drm_compositor *ec = data;
663         struct udev_device *event;
664
665         event = udev_monitor_receive_device(ec->udev_monitor);
666         
667         if (udev_event_is_hotplug(event))
668                 update_outputs(ec);
669
670         udev_device_unref(event);
671
672         return 1;
673 }
674
675 static EGLImageKHR
676 drm_compositor_create_cursor_image(struct wlsc_compositor *ec,
677                                    int32_t width, int32_t height)
678 {
679         struct drm_compositor *c = (struct drm_compositor *) ec;
680         struct gbm_bo *bo;
681         EGLImageKHR image;
682
683         if (width > 64 || height > 64)
684                 return EGL_NO_IMAGE_KHR;
685
686         bo = gbm_bo_create(c->gbm,
687                            /* width, height, */ 64, 64,
688                            GBM_BO_FORMAT_ARGB8888,
689                            GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_RENDERING);
690
691         image = ec->create_image(c->base.display, NULL,
692                                  EGL_NATIVE_PIXMAP_KHR, bo, NULL);
693         gbm_bo_destroy(bo);
694
695         return image;
696 }
697
698 static void
699 drm_destroy(struct wlsc_compositor *ec)
700 {
701         struct drm_compositor *d = (struct drm_compositor *) ec;
702
703         tty_destroy(d->tty);
704
705         free(d);
706 }
707
708 static void
709 vt_func(struct wlsc_compositor *compositor, int event)
710 {
711         struct drm_compositor *ec = (struct drm_compositor *) compositor;
712         struct wlsc_output *output;
713
714         switch (event) {
715         case TTY_ENTER_VT:
716                 compositor->focus = 1;
717                 drmSetMaster(ec->drm.fd);
718                 compositor->state = WLSC_COMPOSITOR_ACTIVE;
719                 wlsc_compositor_damage_all(compositor);
720                 break;
721         case TTY_LEAVE_VT:
722                 compositor->focus = 0;
723                 compositor->state = WLSC_COMPOSITOR_SLEEPING;
724                 drmDropMaster(ec->drm.fd);
725
726                 wl_list_for_each(output, &ec->base.output_list, link)
727                         drm_output_set_cursor(output, NULL);
728
729                 break;
730         };
731 }
732
733 static struct wlsc_compositor *
734 drm_compositor_create(struct wl_display *display, int connector)
735 {
736         struct drm_compositor *ec;
737         struct udev_enumerate *e;
738         struct udev_list_entry *entry;
739         struct udev_device *device;
740         const char *path;
741         struct wl_event_loop *loop;
742
743         ec = malloc(sizeof *ec);
744         if (ec == NULL)
745                 return NULL;
746
747         memset(ec, 0, sizeof *ec);
748         ec->udev = udev_new();
749         if (ec->udev == NULL) {
750                 fprintf(stderr, "failed to initialize udev context\n");
751                 return NULL;
752         }
753
754         e = udev_enumerate_new(ec->udev);
755         udev_enumerate_add_match_subsystem(e, "drm");
756         udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
757         udev_enumerate_scan_devices(e);
758         device = NULL;
759         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
760                 path = udev_list_entry_get_name(entry);
761                 device = udev_device_new_from_syspath(ec->udev, path);
762                 break;
763         }
764         udev_enumerate_unref(e);
765
766         if (device == NULL) {
767                 fprintf(stderr, "no drm device found\n");
768                 return NULL;
769         }
770
771         ec->base.wl_display = display;
772         if (init_egl(ec, device) < 0) {
773                 fprintf(stderr, "failed to initialize egl\n");
774                 return NULL;
775         }
776
777         ec->base.destroy = drm_destroy;
778         ec->base.create_cursor_image = drm_compositor_create_cursor_image;
779
780         ec->base.focus = 1;
781
782         glGenFramebuffers(1, &ec->base.fbo);
783         glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo);
784
785         /* Can't init base class until we have a current egl context */
786         if (wlsc_compositor_init(&ec->base, display) < 0)
787                 return NULL;
788
789         if (create_outputs(ec, connector) < 0) {
790                 fprintf(stderr, "failed to create output for %s\n", path);
791                 return NULL;
792         }
793
794         evdev_input_add_devices(&ec->base, ec->udev);
795
796         loop = wl_display_get_event_loop(ec->base.wl_display);
797         ec->drm_source =
798                 wl_event_loop_add_fd(loop, ec->drm.fd,
799                                      WL_EVENT_READABLE, on_drm_input, ec);
800         ec->tty = tty_create(&ec->base, vt_func);
801
802         ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
803         if (ec->udev_monitor == NULL) {
804                 fprintf(stderr, "failed to intialize udev monitor\n");
805                 return NULL;
806         }
807         udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
808                                                         "drm", NULL);
809         ec->udev_drm_source =
810                 wl_event_loop_add_fd(loop, udev_monitor_get_fd(ec->udev_monitor),
811                                      WL_EVENT_READABLE, udev_drm_event, ec);
812
813         if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
814                 fprintf(stderr, "failed to enable udev-monitor receiving\n");
815                 return NULL;
816         }
817
818         return &ec->base;
819 }
820
821 struct wlsc_compositor *
822 backend_init(struct wl_display *display, char *options);
823
824 WL_EXPORT struct wlsc_compositor *
825 backend_init(struct wl_display *display, char *options)
826 {
827         int connector = 0, i;
828         char *p, *value;
829
830         static char * const tokens[] = { "connector", NULL };
831         
832         p = options;
833         while (i = getsubopt(&p, tokens, &value), i != -1) {
834                 switch (i) {
835                 case 0:
836                         connector = strtol(value, NULL, 0);
837                         break;
838                 }
839         }
840
841         return drm_compositor_create(display, connector);
842 }