summaryrefslogtreecommitdiff
path: root/src/lib/ecore_drm/ecore_drm_output.c
blob: 396e888aaf4cb8217edea706a42a4ddaf4da8372 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "ecore_drm_private.h"

#define ALEN(array) (sizeof(array) / sizeof(array)[0])

static const char *conn_types[] = 
{
   "None", "VGA", "DVI", "DVI", "DVI",
   "Composite", "TV", "LVDS", "CTV", "DIN",
   "DP", "HDMI", "HDMI", "TV", "eDP",
};

/* local functions */
/* #ifdef HAVE_GBM */
/* static Eina_Bool  */
/* _ecore_drm_output_context_create(Ecore_Drm_Device *dev, EGLSurface surface) */
/* { */
/*    EGLBoolean r; */
/*    static const EGLint attribs[] =  */
/*      { */
/*         EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE */
/*      }; */

/*    if ((!dev->egl.disp) || (!dev->egl.cfg)) return EINA_FALSE; */

/*    if (!eglBindAPI(EGL_OPENGL_ES_API)) */
/*      { */
/*         ERR("Could not bind egl api"); */
/*         return EINA_FALSE; */
/*      } */

/*    dev->egl.ctxt =  */
/*      eglCreateContext(dev->egl.disp, dev->egl.cfg, EGL_NO_CONTEXT, attribs); */
/*    if (!dev->egl.ctxt) */
/*      { */
/*         ERR("Could not create Egl Context"); */
/*         return EINA_FALSE; */
/*      } */

/*    r = eglMakeCurrent(dev->egl.disp, surface, surface, dev->egl.ctxt); */
/*    if (r == EGL_FALSE) */
/*      { */
/*         ERR("Could not make surface current"); */
/*         return EINA_FALSE; */
/*      } */

/*    return EINA_TRUE; */
/* } */

/* static Eina_Bool  */
/* _ecore_drm_output_hardware_setup(Ecore_Drm_Device *dev, Ecore_Drm_Output *output) */
/* { */
/*    unsigned int i = 0; */
/*    int flags = 0; */
/*    int w = 0, h = 0; */

/*    if ((!dev) || (!output)) return EINA_FALSE; */

/*    if (output->current_mode) */
/*      { */
/*         w = output->current_mode->width; */
/*         h = output->current_mode->height; */
/*      } */
/*    else */
/*      { */
/*         w = 1024; */
/*         h = 768; */
/*      } */

/*    flags = (GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); */

/*    if (!(output->surface =  */
/*          gbm_surface_create(dev->gbm, w, h, GBM_FORMAT_ARGB8888, flags))) */
/*      { */
/*         ERR("Could not create output surface"); */
/*         return EINA_FALSE; */
/*      } */

/*    if (!(output->egl.surface =  */
/*          eglCreateWindowSurface(dev->egl.disp, dev->egl.cfg,  */
/*                                 output->surface, NULL))) */
/*      { */
/*         ERR("Could not create output egl surface"); */
/*         gbm_surface_destroy(output->surface); */
/*         return EINA_FALSE; */
/*      } */

/*    if (!dev->egl.ctxt) */
/*      { */
/*         if (!_ecore_drm_output_context_create(dev, output->egl.surface)) */
/*           { */
/*              ERR("Could not create context"); */
/*              gbm_surface_destroy(output->surface); */
/*              return EINA_FALSE; */
/*           } */
/*      } */

/*    flags = (GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE); */
/*    for (i = 0; i < NUM_FRAME_BUFFERS; i++) */
/*      { */
/*         if (output->cursor[i]) continue; */
/*         if (!(output->cursor[i] =  */
/*               gbm_bo_create(dev->gbm, 64, 64, dev->format, flags))) */
/*           { */
/*              continue; */
/*           } */
/*      } */

/*    if ((!output->cursor[0]) || (!output->cursor[1])) */
/*      { */
/*         WRN("Hardware Cursor Buffers not available"); */
/*         dev->cursors_broken = EINA_TRUE; */
/*      } */

/*    return EINA_TRUE; */
/* } */

/* static void  */
/* _ecore_drm_output_hardware_render(Ecore_Drm_Output *output) */
/* { */
/*    struct gbm_bo *bo; */
/*    int ret; */

/*    if (!output) return; */
/*    if (!output->current_mode) return; */

/*    glViewport(output->x, output->y,  */
/*               output->current_mode->width, output->current_mode->height); */

/*    if (eglMakeCurrent(output->dev->egl.disp, output->egl.surface,  */
/*                       output->egl.surface, output->dev->egl.ctxt) == EGL_FALSE) */
/*      return; */

/*    glClearColor(1.0, 1.0, 0.0, 1.0); */
/*    glClear(GL_COLOR_BUFFER_BIT); */
/*    glFlush(); */

/*    eglSwapBuffers(output->dev->egl.disp, output->egl.surface); */

/*    if (!(bo = gbm_surface_lock_front_buffer(output->surface))) */
/*      { */
/*         ERR("Failed to lock front buffer"); */
/*         return; */
/*      } */

/*    if (!(output->next = _ecore_drm_fb_bo_get(output->dev, bo))) */
/*      { */
/*         ERR("Failed to get FB from bo"); */
/*         gbm_surface_release_buffer(output->surface, bo); */
/*      } */
/* } */
/* #endif */

static Eina_Bool 
_ecore_drm_output_software_setup(Ecore_Drm_Device *dev, Ecore_Drm_Output *output)
{
   unsigned int i = 0;
   int w = 0, h = 0;

   if ((!dev) || (!output)) return EINA_FALSE;

   if (output->current_mode)
     {
        w = output->current_mode->width;
        h = output->current_mode->height;
     }
   else
     {
        w = 1024;
        h = 768;
     }

   for (i = 0; i < NUM_FRAME_BUFFERS; i++)
     {
        if (!(output->dumb[i] = ecore_drm_fb_create(dev, w, h)))
          {
             ERR("Could not create dumb framebuffer %d", i);
             goto err;
          }
     }

   return EINA_TRUE;

err:
   for (i = 0; i < NUM_FRAME_BUFFERS; i++)
     {
        if (output->dumb[i]) ecore_drm_fb_destroy(output->dumb[i]);
        output->dumb[i] = NULL;
     }

   return EINA_FALSE;
}

static void 
_ecore_drm_output_software_render(Ecore_Drm_Output *output)
{
   if (!output) return;
   if (!output->current_mode) return;
}

static int
_ecore_drm_output_crtc_find(Ecore_Drm_Device *dev, drmModeRes *res, drmModeConnector *conn)
{
   drmModeEncoder *enc;
   unsigned int p;
   int i, j;
   int crtc = -1;

   /* Trying to first use the currently active encoder and crtc combination to avoid a
    * full modeset */
   if (conn->encoder_id)
     enc = drmModeGetEncoder(dev->drm.fd, conn->encoder_id);
   else
     enc = NULL;

   if ((enc) && (enc->crtc_id))
     {
        crtc = enc->crtc_id;
        drmModeFreeEncoder(enc);
        /* Check if this CRTC is already allocated */
        if (!(dev->crtc_allocator & (1 << crtc)))
          return crtc;
     }

   /* We did not find an existing encoder + crtc combination. Loop through all of them until we
    * find the first working combination */
   for (j = 0; j < conn->count_encoders; j++)
     {
        /* get the encoder on this connector */
        if (!(enc = drmModeGetEncoder(dev->drm.fd, conn->encoders[j])))
          {
             WRN("Failed to get encoder: %m");
             continue;
          }

        p = enc->possible_crtcs;
        drmModeFreeEncoder(enc);

	/* Walk over all CRTCs */
        for (i = 0; i < res->count_crtcs; i++)
          {
             /* Does the CRTC match the list of possible CRTCs from the encoder? */
             if ((p & (1 << i)) &&
                 (!(dev->crtc_allocator & (1 << res->crtcs[i]))))
               {
                  return res->crtcs[i];
               }
          }
     }

   return -1;
}

static Ecore_Drm_Output_Mode *
_ecore_drm_output_mode_add(Ecore_Drm_Output *output, drmModeModeInfo *info)
{
   Ecore_Drm_Output_Mode *mode;
   uint64_t refresh;

   /* try to allocate space for mode */
   if (!(mode = malloc(sizeof(Ecore_Drm_Output_Mode))))
     {
        ERR("Could not allocate space for mode");
        return NULL;
     }

   mode->flags = 0;
   mode->width = info->hdisplay;
   mode->height = info->vdisplay;

   refresh = (info->clock * 1000000LL / info->htotal + info->vtotal / 2) / info->vtotal;
   if (info->flags & DRM_MODE_FLAG_INTERLACE)
     refresh *= 2;
   if (info->flags & DRM_MODE_FLAG_DBLSCAN)
     refresh /= 2;
   if (info->vscan > 1)
     refresh /= info->vscan;

   mode->refresh = refresh;
   mode->info = *info;

   /* DBG("Added Mode: %dx%d@%d to Output %d",  */
   /*     mode->width, mode->height, mode->refresh, output->crtc_id); */

   output->modes = eina_list_append(output->modes, mode);

   return mode;
}

static double
_ecore_drm_output_brightness_get(Ecore_Drm_Backlight *backlight)
{
   const char *brightness = NULL;
   double ret;

   if (!(backlight) || !(backlight->device))
     return 0;

   brightness = eeze_udev_syspath_get_sysattr(backlight->device, "brightness");
   if (!brightness) return 0;

   ret = strtod(brightness, NULL);
   if (ret < 0)
     ret = 0;

   return ret;
}

static double
_ecore_drm_output_actual_brightness_get(Ecore_Drm_Backlight *backlight)
{
   const char *brightness = NULL;
   double ret;

   if (!(backlight) || !(backlight->device))
     return 0;

   brightness = eeze_udev_syspath_get_sysattr(backlight->device, "actual_brightness");
   if (!brightness) return 0;

   ret = strtod(brightness, NULL);
   if (ret < 0)
     ret = 0;

   return ret;
}

static double
_ecore_drm_output_max_brightness_get(Ecore_Drm_Backlight *backlight)
{
   const char *brightness = NULL;
   double ret;

   if (!(backlight) || !(backlight->device))
     return 0;

   brightness = eeze_udev_syspath_get_sysattr(backlight->device, "max_brightness");
   if (!brightness) return 0;

   ret = strtod(brightness, NULL);
   if (ret < 0)
     ret = 0;

   return ret;
}

static double
_ecore_drm_output_brightness_set(Ecore_Drm_Backlight *backlight, double brightness_val)
{
   Eina_Bool ret = EINA_FALSE;

   if (!(backlight) || !(backlight->device))
     return ret;

   ret = eeze_udev_syspath_set_sysattr(backlight->device, "brightness", brightness_val);

   return ret;
}

static Ecore_Drm_Backlight *
_ecore_drm_output_backlight_init(Ecore_Drm_Output *output EINA_UNUSED, uint32_t conn_type)
{
   Ecore_Drm_Backlight *backlight = NULL;
   Ecore_Drm_Backlight_Type type = 0;
   Eina_List *devs, *l;
   Eina_Bool found = EINA_FALSE;
   const char *device, *devtype;

   if (!(devs = eeze_udev_find_by_type(EEZE_UDEV_TYPE_BACKLIGHT, NULL)))
     devs = eeze_udev_find_by_type(EEZE_UDEV_TYPE_LEDS, NULL);

   if (!devs) return NULL;

   EINA_LIST_FOREACH(devs, l, device)
     {
        if (!(devtype = eeze_udev_syspath_get_sysattr(device, "type")))
          continue;

        if (!strcmp(devtype, "raw"))
          type = ECORE_DRM_BACKLIGHT_RAW;
        else if (!strcmp(devtype, "platform"))
          type = ECORE_DRM_BACKLIGHT_PLATFORM;
        else if (!strcmp(devtype, "firmware"))
          type = ECORE_DRM_BACKLIGHT_FIRMWARE;

        if ((conn_type != DRM_MODE_CONNECTOR_LVDS) && 
            (conn_type != DRM_MODE_CONNECTOR_eDP))
          {
             if (type != ECORE_DRM_BACKLIGHT_RAW) goto cont;
          }

        found = EINA_TRUE;
cont:
        eina_stringshare_del(devtype);
        if (found)
          {
             break;
          }
     }

   if (!found) goto out;

   if ((backlight = calloc(1, sizeof(Ecore_Drm_Backlight))))
     {
        backlight->type = type;
        backlight->device = eina_stringshare_add(device);
     }

out:
   EINA_LIST_FREE(devs, device);
      eina_stringshare_del(device);

   return backlight;
}

static void
_ecore_drm_output_backlight_shutdown(Ecore_Drm_Backlight *backlight)
{
   if (!backlight)
     return;

   if (backlight->device)
     eina_stringshare_del(backlight->device);

   free(backlight);
}

static Ecore_Drm_Output *
_ecore_drm_output_create(Ecore_Drm_Device *dev, drmModeRes *res, drmModeConnector *conn, int x, int y)
{
   Ecore_Drm_Output *output;
   Ecore_Drm_Output_Mode *mode;
   const char *conn_name;
   char name[32];
   int i = 0;
   drmModeEncoder *enc;
   drmModeModeInfo crtc_mode;
   drmModeCrtc *crtc;
   Eina_List *l;

   i = _ecore_drm_output_crtc_find(dev, res, conn);
   if (i < 0)
     {
        ERR("Could not find crtc or encoder for connector");
        return NULL;
     }

   /* try to allocate space for output */
   if (!(output = calloc(1, sizeof(Ecore_Drm_Output))))
     {
        ERR("Could not allocate space for output");
        return NULL;
     }

   output->dev = dev;
   output->x = x;
   output->y = y;

   output->subpixel = conn->subpixel;
   output->make = eina_stringshare_add("unknown");
   output->model = eina_stringshare_add("unknown");

   if (conn->connector_type < ALEN(conn_types))
     conn_name = conn_types[conn->connector_type];
   else
     conn_name = "UNKNOWN";

   snprintf(name, sizeof(name), "%s%d", conn_name, conn->connector_type_id);
   output->name = eina_stringshare_add(name);

   output->crtc_id = res->crtcs[i];
   dev->crtc_allocator |= (1 << output->crtc_id);
   output->conn_id = conn->connector_id;
   output->crtc = drmModeGetCrtc(dev->drm.fd, output->crtc_id);

   memset(&mode, 0, sizeof(mode));
   if ((enc = drmModeGetEncoder(dev->drm.fd, conn->encoder_id)))
     {
        crtc = drmModeGetCrtc(dev->drm.fd, enc->crtc_id);
        drmModeFreeEncoder(enc);
        if (!crtc) goto mode_err;
        if (crtc->mode_valid) crtc_mode = crtc->mode;
        drmModeFreeCrtc(crtc);
     }

   for (i = 0; i < conn->count_modes; i++)
     {
        if (!(mode = _ecore_drm_output_mode_add(output, &conn->modes[i])))
          {
             ERR("Failed to add mode to output");
             goto mode_err;
          }
     }

   EINA_LIST_REVERSE_FOREACH(output->modes, l, mode)
     {
        if (!memcmp(&crtc_mode, &mode->info, sizeof(crtc_mode)))
          {
             output->current_mode = mode;
             break;
          }
     }

   if ((!output->current_mode) && (crtc_mode.clock != 0))
     {
        output->current_mode = _ecore_drm_output_mode_add(output, &crtc_mode);
        if (!output->current_mode) goto mode_err;
     }

/* #ifdef HAVE_GBM */
/*    if ((dev->use_hw_accel) && (dev->gbm)) */
/*      { */
/*         if (!_ecore_drm_output_hardware_setup(dev, output)) */
/*           { */
/*              ERR("Could not setup output for hardware acceleration"); */
/*              dev->use_hw_accel = EINA_FALSE; */
/*              if (!_ecore_drm_output_software_setup(dev, output)) */
/*                goto mode_err; */
/*              else */
/*                DBG("Setup Output %d for Software Rendering", output->crtc_id); */
/*           } */
/*         else */
/*           DBG("Setup Output %d for Hardware Acceleration", output->crtc_id); */
/*      } */
/*    else */
/* #endif */
     {
        dev->use_hw_accel = EINA_FALSE;
        if (!_ecore_drm_output_software_setup(dev, output))
          goto mode_err;
        else
          DBG("Setup Output %d for Software Rendering", output->crtc_id);
     }

   output->backlight = _ecore_drm_output_backlight_init(output, conn->connector_type);

   return output;

mode_err:
   eina_stringshare_del(output->make);
   eina_stringshare_del(output->model);
   eina_stringshare_del(output->name);
   EINA_LIST_FREE(output->modes, mode)
     free(mode);
   drmModeFreeCrtc(output->crtc);
   dev->crtc_allocator &= ~(1 << output->crtc_id);
   free(output);
   return NULL;
}

static void 
_ecore_drm_output_free(Ecore_Drm_Output *output)
{
   Ecore_Drm_Output_Mode *mode;

   if (!output) return;

   eina_stringshare_del(output->make);
   eina_stringshare_del(output->model);
   eina_stringshare_del(output->name);

   EINA_LIST_FREE(output->modes, mode)
     free(mode);

   drmModeFreeCrtc(output->crtc);

   free(output);
}

void 
_ecore_drm_output_frame_finish(Ecore_Drm_Output *output)
{
   if (!output) return;

   if (output->need_repaint) ecore_drm_output_repaint(output);

   output->repaint_scheduled = EINA_FALSE;
}

void 
_ecore_drm_output_fb_release(Ecore_Drm_Output *output, Ecore_Drm_Fb *fb)
{
   if ((!output) || (!fb)) return;

   if ((fb->mmap) && (fb != output->dumb[0]) && (fb != output->dumb[1]))
     ecore_drm_fb_destroy(fb);
/* #ifdef HAVE_GBM */
/*    else if (fb->bo) */
/*      gbm_bo_destroy(fb->bo); */
/* #endif */
}

void 
_ecore_drm_output_repaint_start(Ecore_Drm_Output *output)
{
   unsigned int fb;

   /* DBG("Output Repaint Start"); */

   if (!output) return;

   if (!output->current)
     {
        /* DBG("\tNo Current FB"); */
        goto finish;
     }

   fb = output->current->id;

   if (drmModePageFlip(output->dev->drm.fd, output->crtc_id, fb, 
                       DRM_MODE_PAGE_FLIP_EVENT, output) < 0)
     {
        ERR("Could not schedule output page flip event");
        goto finish;
     }

   return;

finish:
   _ecore_drm_output_frame_finish(output);
}

static void
_ecore_drm_output_event(const char *device, Eeze_Udev_Event event, void *data, Eeze_Udev_Watch *watch EINA_UNUSED)
{
   Ecore_Drm_Output *output;

   if (!(output = data)) return;

   /* TODO: Check if device is hotplug and update outputs */
}

/**
 * @defgroup Ecore_Drm_Output_Group Ecore DRM Output
 * 
 * Functions to manage DRM outputs.
 * 
 */

/* TODO: DOXY !! */

/* public functions */
EAPI Eina_Bool 
ecore_drm_outputs_create(Ecore_Drm_Device *dev)
{
   Eina_Bool ret = EINA_TRUE;
   Ecore_Drm_Output *output = NULL;
   drmModeConnector *conn;
   drmModeRes *res;
   drmModeCrtc *crtc;
   int i = 0, x = 0, y = 0;

   /* DBG("Create outputs for %d", dev->drm.fd); */

   /* get the resources */
   if (!(res = drmModeGetResources(dev->drm.fd)))
     {
        ERR("Could not get resources for drm card: %m");
        return EINA_FALSE;
     }

   if (!(dev->crtcs = calloc(res->count_crtcs, sizeof(unsigned int))))
     {
        ERR("Could not allocate space for crtcs");
        /* free resources */
        drmModeFreeResources(res);
        return EINA_FALSE;
     }

   dev->crtc_count = res->count_crtcs;
   memcpy(dev->crtcs, res->crtcs, sizeof(unsigned int) * res->count_crtcs);

   dev->min_width = res->min_width;
   dev->min_height = res->min_height;
   dev->max_width = res->max_width;
   dev->max_height = res->max_height;

   for (i = 0; i < res->count_connectors; i++)
     {
        /* get the connector */
        if (!(conn = drmModeGetConnector(dev->drm.fd, res->connectors[i])))
          continue;

        if ((conn->connection == DRM_MODE_CONNECTED) && 
            (conn->count_modes > 0))
          {
             drmModeEncoder *enc;

             /* create output for this connector */
             if (!(output = _ecore_drm_output_create(dev, res, conn, x, y)))
               {
                  /* free the connector */
                  drmModeFreeConnector(conn);
                  _ecore_drm_output_free(output);
                  continue;
               }

             output->drm_fd = dev->drm.fd;

             if (!(enc = drmModeGetEncoder(dev->drm.fd, conn->encoder_id)))
               {
                  drmModeFreeConnector(conn);
                  _ecore_drm_output_free(output);
                  continue;
               }

             if (!(crtc = drmModeGetCrtc(dev->drm.fd, enc->crtc_id)))
               {
                  drmModeFreeEncoder(enc);
                  drmModeFreeConnector(conn);
                  _ecore_drm_output_free(output);
                  continue;
               }

             x += crtc->width;

             drmModeFreeCrtc(crtc);
             drmModeFreeEncoder(enc);

             dev->outputs = eina_list_append(dev->outputs, output);
          }

        /* free the connector */
        drmModeFreeConnector(conn);
     }

   ret = EINA_TRUE;
   if (eina_list_count(dev->outputs) < 1) 
     {
        ret = EINA_FALSE;
        free(dev->crtcs);
     }

   /* free resources */
   drmModeFreeResources(res);

   if ((output) && (!output->watch))
     {
        int events = 0;

        events = (EEZE_UDEV_EVENT_ADD | EEZE_UDEV_EVENT_REMOVE);
        if (!(output->watch =
                eeze_udev_watch_add(EEZE_UDEV_TYPE_DRM, events,
                                    _ecore_drm_output_event, output)))
        {
           ERR("Could not create Eeze_Udev_Watch for drm output");
           return EINA_FALSE;
        }
     }

   return ret;
}

EAPI void 
ecore_drm_output_free(Ecore_Drm_Output *output)
{
   Ecore_Drm_Output_Mode *mode;

   /* check for valid output */
   if (!output) return;

   /* free modes */
   EINA_LIST_FREE(output->modes, mode)
     free(mode);

   /* free strings */
   if (output->name) eina_stringshare_del(output->name);
   if (output->model) eina_stringshare_del(output->model);
   if (output->make) eina_stringshare_del(output->make);

   free(output);
}

EAPI void 
ecore_drm_output_cursor_size_set(Ecore_Drm_Output *output, int handle, int w, int h)
{
   if (!output) return;
   drmModeSetCursor(output->drm_fd, output->crtc_id, handle, w, h);
}

EAPI Eina_Bool 
ecore_drm_output_enable(Ecore_Drm_Output *output)
{
   Ecore_Drm_Output_Mode *mode;

   if ((!output) || (!output->current)) return EINA_FALSE;

   mode = output->current_mode;
   if (drmModeSetCrtc(output->drm_fd, output->crtc_id, output->current->id, 
                      0, 0, &output->conn_id, 1, &mode->info) < 0)
     {
        ERR("Could not set output crtc: %m");
        return EINA_FALSE;
     }

   return EINA_TRUE;
}

EAPI void 
ecore_drm_output_fb_release(Ecore_Drm_Output *output, Ecore_Drm_Fb *fb)
{
   if ((!output) || (!fb)) return;

   if ((fb->mmap) && (fb != output->dumb[0]) && (fb != output->dumb[1]))
     ecore_drm_fb_destroy(fb);
/* #ifdef HAVE_GBM */
/*    else if (fb->bo) */
/*      { */
/*         if (fb->from_client) */
/*           gbm_bo_destroy(fb->bo); */
/*         else */
/*           gbm_surface_release_buffer(output->surface, fb->bo); */
/*      } */
/* #endif */
}

EAPI void 
ecore_drm_output_repaint(Ecore_Drm_Output *output)
{
   Eina_List *l;
   Ecore_Drm_Sprite *sprite;
   int ret = 0;

   if (!output) return;

   /* DBG("Output Repaint: %d %d", output->crtc_id, output->conn_id); */

   /* TODO: assign planes ? */

   if (!output->next)
     {
/* #ifdef HAVE_GBM */
/*         if (output->dev->use_hw_accel) */
/*           { */
/*              _ecore_drm_output_hardware_render(output); */
/*           } */
/*         else */
/* #endif */
          {
             _ecore_drm_output_software_render(output);
          }
     }

   if (!output->next)
     {
        /* DBG("\tNo Next Fb"); */
        return;
     }

   output->need_repaint = EINA_FALSE;

   if (!output->current)
     {
        Ecore_Drm_Output_Mode *mode;

        mode = output->current_mode;

        ret = drmModeSetCrtc(output->dev->drm.fd, output->crtc_id, 
                             output->next->id, 0, 0, &output->conn_id, 1, 
                             &mode->info);
        if (ret)
          {
             /* ERR("Setting output mode failed"); */
             goto err;
          }
     }

   if (drmModePageFlip(output->dev->drm.fd, output->crtc_id, output->next->id,
                       DRM_MODE_PAGE_FLIP_EVENT, output) < 0)
     {
        /* ERR("Scheduling pageflip failed"); */
        goto err;
     }

   output->pending_flip = EINA_TRUE;

   EINA_LIST_FOREACH(output->dev->sprites, l, sprite)
     {
        unsigned int flags = 0, id = 0;
        drmVBlank vbl = 
          {
             .request.type = (DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT),
             .request.sequence = 1,
          };

        if (((!sprite->current_fb) && (!sprite->next_fb)) || 
            (!ecore_drm_sprites_crtc_supported(output, sprite->crtcs)))
          continue;

        if ((sprite->next_fb) && (!output->dev->cursors_broken))
          id = sprite->next_fb->id;

        ecore_drm_sprites_fb_set(sprite, id, flags);

        vbl.request.signal = (unsigned long)sprite;
        ret = drmWaitVBlank(output->dev->drm.fd, &vbl);
        if (ret) ERR("Error Wait VBlank: %m");

        sprite->output = output;
        output->pending_vblank = EINA_TRUE;
     }

   return;

err:
   if (output->next)
     {
        ecore_drm_output_fb_release(output, output->next);
        output->next = NULL;
     }
}

EAPI void 
ecore_drm_output_size_get(Ecore_Drm_Device *dev, int output, int *w, int *h)
{
   drmModeFB *fb;

   if (w) *w = 0;
   if (h) *h = 0;
   if (!dev) return;

   if (!(fb = drmModeGetFB(dev->drm.fd, output))) return;
   if (w) *w = fb->width;
   if (h) *h = fb->height;
   drmModeFreeFB(fb);
}

EAPI void 
ecore_drm_outputs_geometry_get(Ecore_Drm_Device *dev, int *x, int *y, int *w, int *h)
{
   Ecore_Drm_Output *output;
   Eina_List *l;
   int ox = 0, oy = 0, ow = 0, oh = 0;

   if (x) *x = 0;
   if (y) *y = 0;
   if (w) *w = 0;
   if (h) *h = 0;
   if (!dev) return;

   EINA_LIST_FOREACH(dev->outputs, l, output)
     {
        ox += output->x;
        oy += output->y;
        ow += MAX(ow, output->current_mode->width);
        oh += MAX(oh, output->current_mode->height);
     }

   if (x) *x = ox;
   if (y) *y = oy;
   if (w) *w = ow;
   if (h) *h = oh;
}