compositor-drm: Pass physical size to output_init
[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.subpixel = drm_subpixel_to_wayland(connector->subpixel);
420         output->base.make = "unknown";
421         output->base.model = "unknown";
422         wl_list_init(&output->base.mode_list);
423
424         output->crtc_id = resources->crtcs[i];
425         ec->crtc_allocator |= (1 << output->crtc_id);
426         output->connector_id = connector->connector_id;
427         ec->connector_allocator |= (1 << output->connector_id);
428
429         for (i = 0; i < connector->count_modes; i++)
430                 drm_output_add_mode(output, &connector->modes[i]);
431         if (connector->count_modes == 0)
432                 drm_output_add_mode(output, &builtin_1024x768);
433
434         drm_mode = container_of(output->base.mode_list.next,
435                                 struct drm_mode, base.link);
436         output->base.current = &drm_mode->base;
437         drm_mode->base.flags =
438                 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
439
440         wlsc_output_init(&output->base, &ec->base, x, y,
441                          connector->mmWidth, connector->mmHeight, 0);
442
443         wl_list_insert(ec->base.output_list.prev, &output->base.link);
444
445         drmModeFreeEncoder(encoder);
446
447         glGenRenderbuffers(2, output->rbo);
448         for (i = 0; i < 2; i++) {
449                 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
450
451                 output->bo[i] =
452                         gbm_bo_create(ec->gbm,
453                                       output->base.current->width,
454                                       output->base.current->height,
455                                       GBM_BO_FORMAT_XRGB8888,
456                                       GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
457                 output->image[i] = ec->base.create_image(ec->base.display,
458                                                          NULL,
459                                                          EGL_NATIVE_PIXMAP_KHR,
460                                                          output->bo[i], NULL);
461
462
463                 ec->base.image_target_renderbuffer_storage(GL_RENDERBUFFER,
464                                                            output->image[i]);
465                 stride = gbm_bo_get_pitch(output->bo[i]);
466                 handle = gbm_bo_get_handle(output->bo[i]).u32;
467
468                 ret = drmModeAddFB(ec->drm.fd,
469                                    output->base.current->width,
470                                    output->base.current->height,
471                                    32, 32, stride, handle, &output->fb_id[i]);
472                 if (ret) {
473                         fprintf(stderr, "failed to add fb %d: %m\n", i);
474                         return -1;
475                 }
476         }
477
478         output->current = 0;
479         glFramebufferRenderbuffer(GL_FRAMEBUFFER,
480                                   GL_COLOR_ATTACHMENT0,
481                                   GL_RENDERBUFFER,
482                                   output->rbo[output->current]);
483         ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id,
484                              output->fb_id[output->current ^ 1], 0, 0,
485                              &output->connector_id, 1,
486                              &drm_mode->mode_info);
487         if (ret) {
488                 fprintf(stderr, "failed to set mode: %m\n");
489                 return -1;
490         }
491
492         output->scanout_surface = NULL;
493         output->base.prepare_render = drm_output_prepare_render;
494         output->base.present = drm_output_present;
495         output->base.prepare_scanout_surface =
496                 drm_output_prepare_scanout_surface;
497         output->base.set_hardware_cursor = drm_output_set_cursor;
498
499         return 0;
500 }
501
502 static int
503 create_outputs(struct drm_compositor *ec, int option_connector)
504 {
505         drmModeConnector *connector;
506         drmModeRes *resources;
507         int i;
508         int x = 0, y = 0;
509
510         resources = drmModeGetResources(ec->drm.fd);
511         if (!resources) {
512                 fprintf(stderr, "drmModeGetResources failed\n");
513                 return -1;
514         }
515
516         for (i = 0; i < resources->count_connectors; i++) {
517                 connector = drmModeGetConnector(ec->drm.fd, resources->connectors[i]);
518                 if (connector == NULL)
519                         continue;
520
521                 if (connector->connection == DRM_MODE_CONNECTED &&
522                     (option_connector == 0 ||
523                      connector->connector_id == option_connector))
524                         if (create_output_for_connector(ec, resources,
525                                                         connector, x, y) < 0)
526                                 return -1;
527
528                 x += container_of(ec->base.output_list.prev, struct wlsc_output,
529                                   link)->current->width;
530
531                 drmModeFreeConnector(connector);
532         }
533
534         if (wl_list_empty(&ec->base.output_list)) {
535                 fprintf(stderr, "No currently active connector found.\n");
536                 return -1;
537         }
538
539         drmModeFreeResources(resources);
540
541         return 0;
542 }
543
544 static int
545 destroy_output(struct drm_output *output)
546 {
547         struct drm_compositor *ec =
548                 (struct drm_compositor *) output->base.compositor;
549         int i;
550
551         glFramebufferRenderbuffer(GL_FRAMEBUFFER,
552                                   GL_COLOR_ATTACHMENT0,
553                                   GL_RENDERBUFFER,
554                                   0);
555
556         glBindRenderbuffer(GL_RENDERBUFFER, 0);
557         glDeleteRenderbuffers(2, output->rbo);
558
559         for (i = 0; i < 2; i++) {
560                 ec->base.destroy_image(ec->base.display, output->image[i]);
561                 drmModeRmFB(ec->drm.fd, output->fb_id[i]);
562         }
563         
564         ec->crtc_allocator &= ~(1 << output->crtc_id);
565         ec->connector_allocator &= ~(1 << output->connector_id);
566
567         wlsc_output_destroy(&output->base);
568         wl_list_remove(&output->base.link);
569
570         free(output);
571
572         return 0;
573 }
574
575 static void
576 update_outputs(struct drm_compositor *ec)
577 {
578         drmModeConnector *connector;
579         drmModeRes *resources;
580         struct drm_output *output, *next;
581         int x = 0, y = 0;
582         int x_offset = 0, y_offset = 0;
583         uint32_t connected = 0, disconnects = 0;
584         int i;
585
586         resources = drmModeGetResources(ec->drm.fd);
587         if (!resources) {
588                 fprintf(stderr, "drmModeGetResources failed\n");
589                 return;
590         }
591
592         /* collect new connects */
593         for (i = 0; i < resources->count_connectors; i++) {
594                 connector =
595                         drmModeGetConnector(ec->drm.fd,
596                                             resources->connectors[i]);
597                 if (connector == NULL ||
598                     connector->connection != DRM_MODE_CONNECTED)
599                         continue;
600
601                 connected |= (1 << connector->connector_id);
602                 
603                 if (!(ec->connector_allocator & (1 << connector->connector_id))) {
604                         struct wlsc_output *last_output =
605                                 container_of(ec->base.output_list.prev,
606                                              struct wlsc_output, link);
607
608                         /* XXX: not yet needed, we die with 0 outputs */
609                         if (!wl_list_empty(&ec->base.output_list))
610                                 x = last_output->x + last_output->current->width;
611                         else
612                                 x = 0;
613                         y = 0;
614                         create_output_for_connector(ec, resources,
615                                                     connector, x, y);
616                         printf("connector %d connected\n",
617                                connector->connector_id);
618                                 
619                 }
620                 drmModeFreeConnector(connector);
621         }
622         drmModeFreeResources(resources);
623
624         disconnects = ec->connector_allocator & ~connected;
625         if (disconnects) {
626                 wl_list_for_each_safe(output, next, &ec->base.output_list,
627                                       base.link) {
628                         if (x_offset != 0 || y_offset != 0) {
629                                 wlsc_output_move(&output->base,
630                                                  output->base.x - x_offset,
631                                                  output->base.y - y_offset);
632                         }
633
634                         if (disconnects & (1 << output->connector_id)) {
635                                 disconnects &= ~(1 << output->connector_id);
636                                 printf("connector %d disconnected\n",
637                                        output->connector_id);
638                                 x_offset += output->base.current->width;
639                                 destroy_output(output);
640                         }
641                 }
642         }
643
644         /* FIXME: handle zero outputs, without terminating */   
645         if (ec->connector_allocator == 0)
646                 wl_display_terminate(ec->base.wl_display);
647 }
648
649 static int
650 udev_event_is_hotplug(struct udev_device *device)
651 {
652         struct udev_list_entry *list, *hotplug_entry;
653         
654         list = udev_device_get_properties_list_entry(device);
655
656         hotplug_entry = udev_list_entry_get_by_name(list, "HOTPLUG");
657         if (hotplug_entry == NULL)
658                 return 0;
659
660         return strcmp(udev_list_entry_get_value(hotplug_entry), "1") == 0;
661 }
662
663 static int
664 udev_drm_event(int fd, uint32_t mask, void *data)
665 {
666         struct drm_compositor *ec = data;
667         struct udev_device *event;
668
669         event = udev_monitor_receive_device(ec->udev_monitor);
670         
671         if (udev_event_is_hotplug(event))
672                 update_outputs(ec);
673
674         udev_device_unref(event);
675
676         return 1;
677 }
678
679 static EGLImageKHR
680 drm_compositor_create_cursor_image(struct wlsc_compositor *ec,
681                                    int32_t width, int32_t height)
682 {
683         struct drm_compositor *c = (struct drm_compositor *) ec;
684         struct gbm_bo *bo;
685         EGLImageKHR image;
686
687         if (width > 64 || height > 64)
688                 return EGL_NO_IMAGE_KHR;
689
690         bo = gbm_bo_create(c->gbm,
691                            /* width, height, */ 64, 64,
692                            GBM_BO_FORMAT_ARGB8888,
693                            GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_RENDERING);
694
695         image = ec->create_image(c->base.display, NULL,
696                                  EGL_NATIVE_PIXMAP_KHR, bo, NULL);
697         gbm_bo_destroy(bo);
698
699         return image;
700 }
701
702 static void
703 drm_destroy(struct wlsc_compositor *ec)
704 {
705         struct drm_compositor *d = (struct drm_compositor *) ec;
706
707         tty_destroy(d->tty);
708
709         free(d);
710 }
711
712 static void
713 vt_func(struct wlsc_compositor *compositor, int event)
714 {
715         struct drm_compositor *ec = (struct drm_compositor *) compositor;
716         struct wlsc_output *output;
717
718         switch (event) {
719         case TTY_ENTER_VT:
720                 compositor->focus = 1;
721                 drmSetMaster(ec->drm.fd);
722                 compositor->state = WLSC_COMPOSITOR_ACTIVE;
723                 wlsc_compositor_damage_all(compositor);
724                 break;
725         case TTY_LEAVE_VT:
726                 compositor->focus = 0;
727                 compositor->state = WLSC_COMPOSITOR_SLEEPING;
728                 drmDropMaster(ec->drm.fd);
729
730                 wl_list_for_each(output, &ec->base.output_list, link)
731                         drm_output_set_cursor(output, NULL);
732
733                 break;
734         };
735 }
736
737 static struct wlsc_compositor *
738 drm_compositor_create(struct wl_display *display, int connector)
739 {
740         struct drm_compositor *ec;
741         struct udev_enumerate *e;
742         struct udev_list_entry *entry;
743         struct udev_device *device;
744         const char *path;
745         struct wl_event_loop *loop;
746
747         ec = malloc(sizeof *ec);
748         if (ec == NULL)
749                 return NULL;
750
751         memset(ec, 0, sizeof *ec);
752         ec->udev = udev_new();
753         if (ec->udev == NULL) {
754                 fprintf(stderr, "failed to initialize udev context\n");
755                 return NULL;
756         }
757
758         e = udev_enumerate_new(ec->udev);
759         udev_enumerate_add_match_subsystem(e, "drm");
760         udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
761         udev_enumerate_scan_devices(e);
762         device = NULL;
763         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
764                 path = udev_list_entry_get_name(entry);
765                 device = udev_device_new_from_syspath(ec->udev, path);
766                 break;
767         }
768         udev_enumerate_unref(e);
769
770         if (device == NULL) {
771                 fprintf(stderr, "no drm device found\n");
772                 return NULL;
773         }
774
775         ec->base.wl_display = display;
776         if (init_egl(ec, device) < 0) {
777                 fprintf(stderr, "failed to initialize egl\n");
778                 return NULL;
779         }
780
781         ec->base.destroy = drm_destroy;
782         ec->base.create_cursor_image = drm_compositor_create_cursor_image;
783
784         ec->base.focus = 1;
785
786         glGenFramebuffers(1, &ec->base.fbo);
787         glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo);
788
789         /* Can't init base class until we have a current egl context */
790         if (wlsc_compositor_init(&ec->base, display) < 0)
791                 return NULL;
792
793         if (create_outputs(ec, connector) < 0) {
794                 fprintf(stderr, "failed to create output for %s\n", path);
795                 return NULL;
796         }
797
798         evdev_input_add_devices(&ec->base, ec->udev);
799
800         loop = wl_display_get_event_loop(ec->base.wl_display);
801         ec->drm_source =
802                 wl_event_loop_add_fd(loop, ec->drm.fd,
803                                      WL_EVENT_READABLE, on_drm_input, ec);
804         ec->tty = tty_create(&ec->base, vt_func);
805
806         ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
807         if (ec->udev_monitor == NULL) {
808                 fprintf(stderr, "failed to intialize udev monitor\n");
809                 return NULL;
810         }
811         udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
812                                                         "drm", NULL);
813         ec->udev_drm_source =
814                 wl_event_loop_add_fd(loop, udev_monitor_get_fd(ec->udev_monitor),
815                                      WL_EVENT_READABLE, udev_drm_event, ec);
816
817         if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
818                 fprintf(stderr, "failed to enable udev-monitor receiving\n");
819                 return NULL;
820         }
821
822         return &ec->base;
823 }
824
825 struct wlsc_compositor *
826 backend_init(struct wl_display *display, char *options);
827
828 WL_EXPORT struct wlsc_compositor *
829 backend_init(struct wl_display *display, char *options)
830 {
831         int connector = 0, i;
832         char *p, *value;
833
834         static char * const tokens[] = { "connector", NULL };
835         
836         p = options;
837         while (i = getsubopt(&p, tokens, &value), i != -1) {
838                 switch (i) {
839                 case 0:
840                         connector = strtol(value, NULL, 0);
841                         break;
842                 }
843         }
844
845         return drm_compositor_create(display, connector);
846 }