aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ecore/src/lib/ecore_x/xlib/ecore_x_randr_12.c
blob: ec23b3c502f8942e22537b36377219a8a25716fc (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
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
/*
 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "ecore_x_private.h"
#include "ecore_x_randr.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define Ecore_X_Randr_None  (Ecore_X_Randr_Crtc)0
#define Ecore_X_Randr_Unset (Ecore_X_Randr_Crtc) - 1

#ifdef ECORE_XRANDR

#define RANDR_1_2           ((1 << 16) | 2)

#define RANDR_VALIDATE_ROOT(screen, root) \
  ((screen = XRRRootToScreen(_ecore_x_disp, root)) != -1)

#define RANDR_CHECK_1_2_RET(ret) if (_randr_version < RANDR_1_2) \
    return ret

#define RANDR_PROPERTY_EDID               "EDID"
#define RANDR_PROPERTY_BACKLIGHT          "Backlight"
#define RANDR_PROPERTY_SIGNAL_FORMAT      "SignalFormat"
#define RANDR_PROPERTY_SIGNAL_PROPERTIES  "SignalProperties"
#define RANDR_PROPERTY_CONNECTOR_TYPE     "ConnectorType"
#define RANDR_PROPERTY_CONNECTOR_NUMBER   "ConnectorNumber"
#define RANDR_PROPERTY_COMPATIBILITY_LIST "CompatibilityList"
#define RANDR_PROPERTY_CLONE_LIST         "CloneList"

extern XRRScreenResources *(*_ecore_x_randr_get_screen_resources)(Display *
                                                                  dpy,
                                                                  Window
                                                                  window);
extern int _randr_version;
#endif

/**
 * @brief enable event selection. This enables basic interaction with
 * output/crtc events and requires RRandR >= 1.2.
 * @param win select this window's properties for RandRR events
 * @param on enable/disable selecting
 */
EAPI void
ecore_x_randr_events_select(Ecore_X_Window win,
                            Eina_Bool on)
{
#ifdef ECORE_XRANDR
   int mask;

   LOGFN(__FILE__, __LINE__, __FUNCTION__);
   if (!on)
     mask = 0;
   else
     {
        mask = RRScreenChangeNotifyMask;
        if (_randr_version >= RANDR_1_2)
          mask |= (RRCrtcChangeNotifyMask |
                   RROutputChangeNotifyMask |
                   RROutputPropertyNotifyMask);
     }

   XRRSelectInput(_ecore_x_disp, win, mask);
#endif
}

/**
 * @brief validates a CRTC for a given root window's screen.
 * @param root the window which's default display will be queried
 * @param crtc the CRTC to be validated.
 * @return in case it is found EINA_TRUE will be returned. Else EINA_FALSE is returned.
 */
static inline Eina_Bool
_ecore_x_randr_crtc_validate(Ecore_X_Window root,
                             Ecore_X_Randr_Crtc crtc)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(EINA_FALSE);

   XRRScreenResources *res = NULL;
   int i;
   Eina_Bool ret = EINA_FALSE;

   if ((crtc == Ecore_X_Randr_None) ||
       (crtc == Ecore_X_Randr_Unset))
     return ret;

   if (_ecore_x_randr_root_validate(root) && crtc &&
       (res = _ecore_x_randr_get_screen_resources (_ecore_x_disp, root)))
     {
        for (i = 0; i < res->ncrtc; i++)
          {
             if (res->crtcs[i] == crtc)
               {
                  ret = EINA_TRUE;
                  break;
               }
          }
        XRRFreeScreenResources(res);
     }

   return ret;
#else
   return EINA_FALSE;
#endif
}

Eina_Bool
_ecore_x_randr_output_validate(Ecore_X_Window root,
                               Ecore_X_Randr_Output output)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(EINA_FALSE);

   Eina_Bool ret = EINA_FALSE;
   XRRScreenResources *res = NULL;
   int i;

   if (_ecore_x_randr_root_validate(root) && output &&
       (res = _ecore_x_randr_get_screen_resources (_ecore_x_disp, root)))
     {
        for (i = 0; i < res->noutput; i++)
          {
             if (res->outputs[i] == output)
               {
                  ret = EINA_TRUE;
                  break;
               }
          }
        XRRFreeScreenResources(res);
     }

   return ret;
#else
   return EINA_FALSE;
#endif
}

static inline Eina_Bool
_ecore_x_randr_mode_validate(Ecore_X_Window root,
                             Ecore_X_Randr_Mode mode)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(EINA_FALSE);

   Eina_Bool ret = EINA_FALSE;
   XRRScreenResources *res = NULL;
   int i;

   if (_ecore_x_randr_root_validate(root) && mode &&
       (res = _ecore_x_randr_get_screen_resources (_ecore_x_disp, root)))
     {
        for (i = 0; i < res->nmode; i++)
          {
             if (res->modes[i].id == mode)
               {
                  ret = EINA_TRUE;
                  break;
               }
          }
        XRRFreeScreenResources(res);
     }

   return ret;
#else
   return EINA_FALSE;
#endif
}

/*
 * @param w width of screen in px
 * @param h height of screen in px
 */
EAPI void
ecore_x_randr_screen_current_size_get(Ecore_X_Window root,
                                      int *w,
                                      int *h,
                                      int *w_mm,
                                      int *h_mm)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET();
   Ecore_X_Randr_Screen scr;

   if (!RANDR_VALIDATE_ROOT(scr, root))
     return;

   if (w)
     *w = DisplayWidth(_ecore_x_disp, scr);

   if (h)
     *h = DisplayHeight(_ecore_x_disp, scr);

   if (w_mm)
     *w_mm = DisplayWidthMM(_ecore_x_disp, scr);

   if (h_mm)
     *h_mm = DisplayHeightMM(_ecore_x_disp, scr);

#endif
}

/*
 * @param root window which's screen will be queried
 * @param wmin minimum width the screen can be set to
 * @param hmin minimum height the screen can be set to
 * @param wmax maximum width the screen can be set to
 * @param hmax maximum height the screen can be set to
 */
EAPI void
ecore_x_randr_screen_size_range_get(Ecore_X_Window root,
                                    int *wmin,
                                    int *hmin,
                                    int *wmax,
                                    int *hmax)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET();
   int twmin, thmin, twmax, thmax;
   if (XRRGetScreenSizeRange (_ecore_x_disp, root, &twmin, &thmin, &twmax,
                              &thmax))
     {
        if (wmin)
          *wmin = twmin;

        if (hmin)
          *hmin = thmin;

        if (wmax)
          *wmax = twmax;

        if (hmax)
          *hmax = thmax;
     }

#endif
}

/*
 * @param root window which's screen's size should be set. If invalid (e.g. NULL) no action is taken.
 * @param w width in px the screen should be set to. If out of valid boundaries, current value is assumed.
 * @param h height in px the screen should be set to. If out of valid boundaries, current value is assumed.
 * @param w_mm width in mm the screen should be set to. If 0, current aspect is assumed.
 * @param h_mm height in mm the screen should be set to. If 0, current aspect is assumed.
 * @return EINA_TRUE if request was successfully sent or screen is already in
 * requested size, EINA_FALSE if parameters are invalid
 */
EAPI Eina_Bool
ecore_x_randr_screen_current_size_set(Ecore_X_Window root,
                                      int w,
                                      int h,
                                      int w_mm,
                                      int h_mm)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(EINA_FALSE);

   Ecore_X_Randr_Screen scr;
   int w_c, h_c, w_mm_c, h_mm_c, twmin, thmin, twmax, thmax;

   if (!RANDR_VALIDATE_ROOT(scr, root))
     return EINA_FALSE;

   ecore_x_randr_screen_current_size_get(root, &w_c, &h_c, &w_mm_c, &h_mm_c);
   if ((w == w_c) && (h == h_c) && (w_mm_c == w_mm) && (h_mm_c == h_mm))
     return EINA_TRUE;

   ecore_x_randr_screen_size_range_get(root, &twmin, &thmin, &twmax, &thmax);

   if (((w != Ecore_X_Randr_None) &&
        ((w < twmin) ||
         (w > twmax))) ||
       ((h != Ecore_X_Randr_None) && ((h < thmin) || (h > thmax))))
     return EINA_FALSE;

   if (w <= 0)
     w = DisplayWidth(_ecore_x_disp, scr);

   if (h <= 0)
     h = DisplayHeight(_ecore_x_disp, scr);

   if (w_mm <= 0)
     w_mm =
       (int)(((double)(DisplayWidthMM(_ecore_x_disp,
                                      scr) /
                       (double)DisplayWidth(_ecore_x_disp,
                                            scr))) * (double)w);

   if (h_mm <= 0)
     h_mm =
       (int)(((double)(DisplayHeightMM(_ecore_x_disp,
                                       scr) /
                       (double)DisplayHeight(_ecore_x_disp,
                                             scr))) * (double)h);

   XRRSetScreenSize (_ecore_x_disp, root, w, h, w_mm, h_mm);
   return EINA_TRUE;
#else
   return EINA_FALSE;
#endif
}

/*
 * @brief get detailed information for all modes related to a root window's screen
 * @param root window which's screen's ressources are queried
 * @param num number of modes returned
 * @return modes' information
 */
EAPI Ecore_X_Randr_Mode_Info **
ecore_x_randr_modes_info_get(Ecore_X_Window root,
                             int *num)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(NULL);
   XRRScreenResources *res = NULL;
   Ecore_X_Randr_Mode_Info **ret = NULL;
   int i;

   if (_ecore_x_randr_root_validate(root) &&
       (res = _ecore_x_randr_get_screen_resources (_ecore_x_disp, root)))
     {
        if ((ret =
               (Ecore_X_Randr_Mode_Info **)malloc(sizeof(
                                                    Ecore_X_Randr_Mode_Info *)
                                                  *
                                                  res->nmode)))
          {
             for (i = 0; i < res->nmode; i++)
               {
                  if ((ret[i] = malloc(sizeof(Ecore_X_Randr_Mode_Info))))
                    {
                       ret[i]->xid = res->modes[i].id;
                       ret[i]->width = res->modes[i].width;
                       ret[i]->height = res->modes[i].height;
                       ret[i]->dotClock = res->modes[i].dotClock;
                       ret[i]->hSyncStart = res->modes[i].hSyncStart;
                       ret[i]->hSyncEnd = res->modes[i].hSyncEnd;
                       ret[i]->hTotal = res->modes[i].hTotal;
                       ret[i]->hSkew = res->modes[i].hSkew;
                       ret[i]->vSyncStart = res->modes[i].vSyncStart;
                       ret[i]->vSyncEnd = res->modes[i].vSyncEnd;
                       ret[i]->vTotal = res->modes[i].vTotal;
                       if ((ret[i]->name = (malloc(res->modes[i].nameLength + 1))))
                         strncpy(ret[i]->name, res->modes[i].name,
                                 (res->modes[i].nameLength + 1));
                       else
                         ret[i]->name = NULL;

                       ret[i]->nameLength = res->modes[i].nameLength;
                       ret[i]->modeFlags = res->modes[i].modeFlags;
                    }
                  else
                    {
                       while (i > 0)
                         free(ret[--i]);
                       free(ret);
                       ret = NULL;
                       break;
                    }
               }
          }

        if (ret && num)
          *num = res->nmode;

        XRRFreeScreenResources(res);
     }

   return ret;
#else
   return NULL;
#endif
}

/*
 * @brief add a mode to a display
 * @param root window to which's screen's ressources are added
 * @param mode_info
 * @return Ecore_X_Randr_Mode of the added mode. Ecore_X_Randr_None if mode
 * adding failed.
 * @since 1.2.0
 */
EAPI Ecore_X_Randr_Mode
ecore_x_randr_mode_info_add(Ecore_X_Window root,
                            Ecore_X_Randr_Mode_Info *mode_info)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(EINA_FALSE);
   Ecore_X_Randr_Mode mode = Ecore_X_Randr_None;

   if (_ecore_x_randr_root_validate(root) && mode_info)
     mode = XRRCreateMode(_ecore_x_disp, root, (XRRModeInfo*)mode_info);

   return mode;
#else
   return Ecore_X_Randr_None;
#endif
}

/*
 * @brief delete a mode from the display
 * @param mode_info
 * @since 1.2.0
 */
EAPI void
ecore_x_randr_mode_del(Ecore_X_Randr_Mode mode)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET();

   XRRDestroyMode(_ecore_x_disp, mode);
#else
   return;
#endif
}

/*
 * @brief get detailed information for a given mode id
 * @param root window which's screen's ressources are queried
 * @param mode the XID which identifies the mode of interest
 * @return mode's detailed information
 */
EAPI Ecore_X_Randr_Mode_Info *
ecore_x_randr_mode_info_get(Ecore_X_Window root,
                            Ecore_X_Randr_Mode mode)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(NULL);
   XRRScreenResources *res = NULL;
   Ecore_X_Randr_Mode_Info *ret = NULL;
   int i;

   if (_ecore_x_randr_root_validate(root) &&
       (res = _ecore_x_randr_get_screen_resources(_ecore_x_disp, root)))
     {
        for (i = 0; i < res->nmode; i++)
          {
             if ((res->modes[i].id == mode) &&
                 (ret = malloc(sizeof(Ecore_X_Randr_Mode_Info))))
               {
                  ret->xid = res->modes[i].id;
                  ret->width = res->modes[i].width;
                  ret->height = res->modes[i].height;
                  ret->dotClock = res->modes[i].dotClock;
                  ret->hSyncStart = res->modes[i].hSyncStart;
                  ret->hSyncEnd = res->modes[i].hSyncEnd;
                  ret->hTotal = res->modes[i].hTotal;
                  ret->hSkew = res->modes[i].hSkew;
                  ret->vSyncStart = res->modes[i].vSyncStart;
                  ret->vSyncEnd = res->modes[i].vSyncEnd;
                  ret->vTotal = res->modes[i].vTotal;
                  ret->name = NULL;
                  ret->nameLength = 0;
                  if (res->modes[i].nameLength > 0)
                    {
                       ret->nameLength = res->modes[i].nameLength;
                       ret->name = malloc(res->modes[i].nameLength + 1);
                       if (ret->name)
                         memcpy(ret->name, res->modes[i].name,
                                res->modes[i].nameLength + 1);
                    }
                  ret->modeFlags = res->modes[i].modeFlags;
                  break;
               }
          }
        XRRFreeScreenResources(res);
     }

   return ret;
#else
   return NULL;
#endif
}

/*
 * @brief free detailed mode information. The pointer handed in will be set to
 * NULL after freeing the memory.
 * @param mode_info the mode information that should be freed
 */
EAPI void
ecore_x_randr_mode_info_free(Ecore_X_Randr_Mode_Info *mode_info)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET();
   if (!mode_info)
     return;

   if (mode_info->name)
     free(mode_info->name);

   free(mode_info);
   mode_info = NULL;
#endif
}

/*
 * @brief get all known CRTCs related to a root window's screen
 * @param root window which's screen's ressources are queried
 * @param num number of CRTCs returned
 * @return CRTC IDs
 */
EAPI Ecore_X_Randr_Crtc *
ecore_x_randr_crtcs_get(Ecore_X_Window root,
                        int *num)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(NULL);
   XRRScreenResources *res = NULL;
   Ecore_X_Randr_Crtc *ret = NULL;

   if (num && root &&
       (res = _ecore_x_randr_get_screen_resources (_ecore_x_disp, root)))
     {
        if ((ret = malloc(sizeof(Ecore_X_Randr_Crtc) * res->ncrtc)))
          {
             memcpy(ret, res->crtcs, (sizeof(Ecore_X_Randr_Crtc) * res->ncrtc));
             *num = res->ncrtc;
          }

        XRRFreeScreenResources(res);
     }

   return ret;
#else
   return NULL;
#endif
}

/*
 * @deprecated bad naming. Use ecore_x_randr_window_crtcs_get instead.
 * @brief get the CRTCs, which display a certain window
 * @param window window the displaying crtcs shall be found for
 * @param num the number of crtcs displaying the window
 * @return array of crtcs that display a certain window. NULL if no crtcs
 * was found that displays the specified window.
 */
EAPI Ecore_X_Randr_Crtc *
ecore_x_randr_current_crtc_get(Ecore_X_Window window,
                               int *num)
{
   return ecore_x_randr_window_crtcs_get(window, num);
}

/*
 * @brief get the CRTCs, which display a certain window
 * @param window window the displaying crtcs shall be found for
 * @param num the number of crtcs displaying the window
 * @return array of crtcs that display a certain window. NULL if no crtcs
 * was found that displays the specified window.
 * @since 1.2.0
 */
EAPI Ecore_X_Randr_Crtc *
ecore_x_randr_window_crtcs_get(Ecore_X_Window window,
                               int *num)
{
#ifdef ECORE_XRANDR
   Ecore_X_Window root;
   Eina_Rectangle w_geo, c_geo;
   Ecore_X_Randr_Crtc *crtcs;
   Ecore_X_Randr_Mode mode;
   Ecore_X_Randr_Output *ret = NULL;
   Window tw;
   int ncrtcs, i, nret = 0, rx = 0, ry = 0;

   if (_randr_version < RANDR_1_2) goto _ecore_x_randr_window_crtcs_get_fail;

   ecore_x_window_geometry_get(window,
                               &w_geo.x, &w_geo.y,
                               &w_geo.w, &w_geo.h);

   root = ecore_x_window_root_get(window);
   crtcs = ecore_x_randr_crtcs_get(root, &ncrtcs);
   if (!crtcs) goto _ecore_x_randr_window_crtcs_get_fail;

   /* now get window RELATIVE to root window - thats what matters. */
   XTranslateCoordinates(_ecore_x_disp, window, root, 0, 0, &rx, &ry, &tw);
   w_geo.x = rx;
   w_geo.y = ry;

   for (i = 0, nret = 0; i < ncrtcs; i++)
     {
        /* if crtc is not enabled, don't bother about it any further */
        mode = ecore_x_randr_crtc_mode_get(root, crtcs[i]);
        if (mode == Ecore_X_Randr_None) continue;

        ecore_x_randr_crtc_geometry_get(root, crtcs[i],
                                        &c_geo.x, &c_geo.y,
                                        &c_geo.w, &c_geo.h);
        if (eina_rectangles_intersect(&w_geo, &c_geo))
          {
             ret = realloc(ret, (sizeof(Ecore_X_Randr_Crtc) * ++nret));
             ret[nret] = crtcs[i];
          }
     }
   free(crtcs);

   if (num) *num = nret;
   return ret;

_ecore_x_randr_window_crtcs_get_fail:
#endif
   if (num) *num = 0;
   return NULL;
}

EAPI Ecore_X_Randr_Output *
ecore_x_randr_outputs_get(Ecore_X_Window root,
                          int *num)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(NULL);
   XRRScreenResources *res = NULL;
   Ecore_X_Randr_Output *ret = NULL;

   if (num && root &&
       (res = _ecore_x_randr_get_screen_resources (_ecore_x_disp, root)))
     {
        if ((ret = malloc(sizeof(Ecore_X_Randr_Output) * res->noutput)))
          {
             memcpy(ret, res->outputs,
                    (sizeof(Ecore_X_Randr_Output) * res->noutput));
             if (num)
               *num = res->noutput;
          }

        if (res)
          XRRFreeScreenResources(res);
     }

   return ret;
#else
   return NULL;
#endif
}

//Per Crtc
/*
 * @brief get a CRTC's outputs.
 * @param root the root window which's screen will be queried
 * @param num number of outputs referenced by given CRTC
 */
EAPI Ecore_X_Randr_Output *
ecore_x_randr_crtc_outputs_get(Ecore_X_Window root,
                               Ecore_X_Randr_Crtc crtc,
                               int *num)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(NULL);
   XRRScreenResources *res = NULL;
   Ecore_X_Randr_Output *ret = NULL;
   XRRCrtcInfo *crtc_info = NULL;

   if (_ecore_x_randr_crtc_validate(root,
                                    crtc) &&
       (res =
          _ecore_x_randr_get_screen_resources (_ecore_x_disp,
                                               root)) &&
       (crtc_info = XRRGetCrtcInfo(_ecore_x_disp, res, crtc)))
     {
        if ((ret = malloc(sizeof(Ecore_X_Randr_Output) * crtc_info->noutput)))
          {
             memcpy(ret, crtc_info->outputs,
                    (sizeof(Ecore_X_Randr_Output) * crtc_info->noutput));
             if (num)
               *num = crtc_info->noutput;
          }

        if (crtc_info)
          XRRFreeCrtcInfo(crtc_info);

        if (res)
          XRRFreeScreenResources(res);
     }

   return ret;
#else
   return NULL;
#endif
}

/*
 * @brief get a CRTC's possible outputs.
 * @param root the root window which's screen will be queried
 * @param num number of possible outputs referenced by given CRTC
 */
EAPI Ecore_X_Randr_Output *
ecore_x_randr_crtc_possible_outputs_get(Ecore_X_Window root,
                                        Ecore_X_Randr_Crtc crtc,
                                        int *num)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(NULL);
   XRRScreenResources *res = NULL;
   Ecore_X_Randr_Output *ret = NULL;
   XRRCrtcInfo *crtc_info = NULL;

   if (_ecore_x_randr_crtc_validate(root,
                                    crtc) &&
       (res = _ecore_x_randr_get_screen_resources (_ecore_x_disp, root)))
     {
        if ((crtc_info = XRRGetCrtcInfo(_ecore_x_disp, res, crtc)))
          {
             if ((ret =
                    malloc(sizeof(Ecore_X_Randr_Output) * crtc_info->npossible)))
               {
                  memcpy(ret, crtc_info->possible,
                         (sizeof(Ecore_X_Randr_Output) * crtc_info->npossible));
                  if (num)
                    *num = res->ncrtc;
               }

             XRRFreeCrtcInfo(crtc_info);
          }

        XRRFreeScreenResources(res);
     }

   return ret;
#else
   return NULL;
#endif
}

EAPI void
ecore_x_randr_crtc_geometry_get(Ecore_X_Window root,
                                Ecore_X_Randr_Crtc crtc,
                                int *x,
                                int *y,
                                int *w,
                                int *h)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET();
   XRRScreenResources *res = NULL;
   XRRCrtcInfo *crtc_info = NULL;

   if (_ecore_x_randr_crtc_validate(root,
                                    crtc) &&
       (res =
          _ecore_x_randr_get_screen_resources (_ecore_x_disp,
                                               root)) &&
       (crtc_info = XRRGetCrtcInfo(_ecore_x_disp, res, crtc)))
     {
        if (x)
          *x = crtc_info->x;

        if (y)
          *y = crtc_info->y;

        if (w)
          *w = crtc_info->width;

        if (h)
          *h = crtc_info->height;

        XRRFreeCrtcInfo(crtc_info);
        XRRFreeScreenResources(res);
     }

#endif
}

/*
 * @brief sets the position of given CRTC within root window's screen
 * @param root the window's screen to be queried
 * @param crtc the CRTC which's position within the mentioned screen is to be altered
 * @param x position on the x-axis (0 == left) of the screen. if x < 0 current value will be kept.
 * @param y position on the y-ayis (0 == top) of the screen. if y < 0, current value will be kept.
 * @return EINA_TRUE if position could be successfully be altered.
 */
EAPI Eina_Bool
ecore_x_randr_crtc_pos_set(Ecore_X_Window root,
                           Ecore_X_Randr_Crtc crtc,
                           int x,
                           int y)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(EINA_FALSE);
   int w_c, h_c, w_new = 0, h_new = 0;
   Eina_Rectangle crtc_geo;

   ecore_x_randr_crtc_geometry_get(root,
                                   crtc,
                                   &crtc_geo.x,
                                   &crtc_geo.y,
                                   &crtc_geo.w,
                                   &crtc_geo.h);
   ecore_x_randr_screen_current_size_get(root, &w_c, &h_c, NULL, NULL);
   if (x < 0)
     x = crtc_geo.x;

   if (y < 0)
     y = crtc_geo.y;

   if ((x + crtc_geo.w) > w_c)
     w_new = x + crtc_geo.w;

   if ((y + crtc_geo.h) > h_c)
     h_new = y + crtc_geo.h;

   if ((w_new != 0) || (h_new != 0))
     if (!ecore_x_randr_screen_current_size_set(root, w_new, h_new, 0, 0))
       return EINA_FALSE;

   return ecore_x_randr_crtc_settings_set(root,
                                          crtc,
                                          NULL,
                                          Ecore_X_Randr_Unset,
                                          x,
                                          y,
                                          Ecore_X_Randr_Unset,
                                          Ecore_X_Randr_Unset);
#else
   return EINA_FALSE;
#endif
}

/**
 * @brief Get the current set mode of a given CRTC
 * @param root the window's screen to be queried
 * @param crtc the CRTC which's should be queried
 * @return currently set mode or - in case parameters are invalid -
 * Ecore_X_Randr_Unset
 */
EAPI Ecore_X_Randr_Mode
ecore_x_randr_crtc_mode_get(Ecore_X_Window root,
                            Ecore_X_Randr_Crtc crtc)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(Ecore_X_Randr_Unset);
   XRRScreenResources *res = NULL;
   XRRCrtcInfo *crtc_info = NULL;
   Ecore_X_Randr_Mode ret = Ecore_X_Randr_Unset;
   if (_ecore_x_randr_root_validate(root) &&
       _ecore_x_randr_crtc_validate(root,
                                    crtc) &&
       (res =
          _ecore_x_randr_get_screen_resources(_ecore_x_disp,
                                              root)) &&
       (crtc_info = XRRGetCrtcInfo(_ecore_x_disp, res, crtc)))
     {
        ret = crtc_info->mode;
        XRRFreeCrtcInfo(crtc_info);
        XRRFreeScreenResources(res);
     }

   return ret;
#else
   return Ecore_X_Randr_Unset;
#endif
}

/**
 * @brief sets a mode for a CRTC and the outputs attached to it
 * @param root the window's screen to be queried
 * @param crtc the CRTC which shall be set
 * @param outputs array of outputs which have to be compatible with the mode. If
 * NULL CRTC will be disabled.
 * @param noutputs number of outputs in array to be used. Use
 * Ecore_X_Randr_Unset (or -1) to use currently used outputs.
 * @param mode XID of the mode to be set. If set to 0 the CRTC will be disabled.
 * If set to -1 the call will fail.
 * @return EINA_TRUE if mode setting was successful. Else EINA_FALSE
 */
EAPI Eina_Bool
ecore_x_randr_crtc_mode_set(Ecore_X_Window root,
                            Ecore_X_Randr_Crtc crtc,
                            Ecore_X_Randr_Output *outputs,
                            int noutputs,
                            Ecore_X_Randr_Mode mode)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(EINA_FALSE);

   if (mode == Ecore_X_Randr_Unset)
     return EINA_FALSE;

   return ecore_x_randr_crtc_settings_set(root,
                                          crtc,
                                          outputs,
                                          noutputs,
                                          Ecore_X_Randr_Unset,
                                          Ecore_X_Randr_Unset,
                                          mode,
                                          Ecore_X_Randr_Unset);
#else
   return EINA_FALSE;
#endif
}

EAPI void
ecore_x_randr_crtc_size_get(Ecore_X_Window root,
                            Ecore_X_Randr_Crtc crtc,
                            int *w,
                            int *h)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET();
   ecore_x_randr_crtc_geometry_get(root, crtc, NULL, NULL, w, h);
#endif
}

EAPI Ecore_X_Randr_Refresh_Rate
ecore_x_randr_crtc_refresh_rate_get(Ecore_X_Window root,
                                    Ecore_X_Randr_Crtc crtc,
                                    Ecore_X_Randr_Mode mode)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(0.0);
   XRRScreenResources *res = NULL;
   XRRCrtcInfo *crtc_info = NULL;
   Ecore_X_Randr_Refresh_Rate ret = 0.0;
   int i;

   if (_ecore_x_randr_crtc_validate(root,
                                    crtc) &&
       (res = _ecore_x_randr_get_screen_resources (_ecore_x_disp, root)))
     {
        for (i = 0; i < res->nmode; i++)
          if (res->modes[i].id == mode)
            {
               if (res->modes[i].hTotal && res->modes[i].vTotal)
                 ret = ((double)res->modes[i].dotClock /
                        ((double)res->modes[i].hTotal *
                         (double)res->modes[i].vTotal));

               break;
            }
     }

   if (crtc_info)
     XRRFreeCrtcInfo(crtc_info);

   if (res)
     XRRFreeScreenResources(res);

   return ret;
#else
   return 0.0;
#endif
}

EAPI Ecore_X_Randr_Orientation
ecore_x_randr_crtc_orientations_get(Ecore_X_Window root,
                                    Ecore_X_Randr_Crtc crtc)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(Ecore_X_Randr_None);
   XRRCrtcInfo *crtc_info = NULL;
   XRRScreenResources *res = NULL;
   Ecore_X_Randr_Orientation ret = Ecore_X_Randr_None;

   if (_ecore_x_randr_crtc_validate(root,
                                    crtc) &&
       (res =
          _ecore_x_randr_get_screen_resources (_ecore_x_disp,
                                               root)) &&
       (crtc_info = XRRGetCrtcInfo(_ecore_x_disp, res, crtc)))
     {
        ret = crtc_info->rotations;
     }
   if (crtc_info)
     XRRFreeCrtcInfo(crtc_info);

   if (res)
     XRRFreeScreenResources(res);

   return ret;
#else
   return Ecore_X_Randr_None;
#endif
}

EAPI Ecore_X_Randr_Orientation
ecore_x_randr_crtc_orientation_get(Ecore_X_Window root,
                                   Ecore_X_Randr_Crtc crtc)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(Ecore_X_Randr_None);
   XRRCrtcInfo *crtc_info = NULL;
   XRRScreenResources *res = NULL;
   Ecore_X_Randr_Orientation ret = Ecore_X_Randr_None;

   if (_ecore_x_randr_crtc_validate(root,
                                    crtc) &&
       (res =
          _ecore_x_randr_get_screen_resources (_ecore_x_disp,
                                               root)) &&
       (crtc_info = XRRGetCrtcInfo(_ecore_x_disp, res, crtc)))
     {
        ret = crtc_info->rotation;
     }
   if (crtc_info)
     XRRFreeCrtcInfo(crtc_info);

   if (res)
     XRRFreeScreenResources(res);

   return ret;
#else
   return Ecore_X_Randr_None;
#endif
}

EAPI Eina_Bool
ecore_x_randr_crtc_orientation_set(Ecore_X_Window root,
                                   Ecore_X_Randr_Crtc crtc,
                                   Ecore_X_Randr_Orientation orientation)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(EINA_FALSE);
   Eina_Bool ret = EINA_FALSE;

   if (orientation != Ecore_X_Randr_None)
     {
        ret = ecore_x_randr_crtc_settings_set(root,
                                              crtc,
                                              NULL,
                                              Ecore_X_Randr_Unset,
                                              Ecore_X_Randr_Unset,
                                              Ecore_X_Randr_Unset,
                                              Ecore_X_Randr_Unset,
                                              orientation);
     }

   return ret;
#else
   return EINA_FALSE;
#endif
}

EAPI void
ecore_x_randr_crtc_pos_get(Ecore_X_Window root,
                           Ecore_X_Randr_Crtc crtc,
                           int *x,
                           int *y)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET();

   ecore_x_randr_crtc_geometry_get(root, crtc, x, y, NULL, NULL);
#endif
}

EAPI Eina_Bool
ecore_x_randr_crtc_clone_set(Ecore_X_Window root,
                             Ecore_X_Randr_Crtc original,
                             Ecore_X_Randr_Crtc clon)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(EINA_FALSE);

   XRRScreenResources *res = NULL;
   XRRCrtcInfo *clone_crtc_info = NULL;
   Ecore_X_Randr_Mode original_mode = Ecore_X_Randr_None;
   Ecore_X_Randr_Orientation original_orientation = Ecore_X_Randr_None;
   Eina_Bool ret = EINA_FALSE;
   int x, y;

   if (_ecore_x_randr_root_validate(root) &&
       _ecore_x_randr_crtc_validate(root,
                                    original) &&
       _ecore_x_randr_crtc_validate(root,
                                    clon) &&
       (res =
          _ecore_x_randr_get_screen_resources (_ecore_x_disp,
                                               root)) &&
       (clone_crtc_info = XRRGetCrtcInfo(_ecore_x_disp, res, clon)))
     {
        ecore_x_randr_crtc_geometry_get(root, original, &x, &y, NULL, NULL);
        original_mode = ecore_x_randr_crtc_mode_get(root, original);
        original_orientation = ecore_x_randr_crtc_orientation_get(root,
                                                                  original);
        ret = ecore_x_randr_crtc_settings_set(root,
                                              clon,
                                              NULL,
                                              Ecore_X_Randr_Unset,
                                              x,
                                              y,
                                              original_mode,
                                              original_orientation);
        XRRFreeCrtcInfo(clone_crtc_info);
        XRRFreeScreenResources(res);
     }

   return ret;
#else
   return EINA_FALSE;
#endif
}

/**
 * @brief sets the demanded parameters for a given CRTC. Note that the CRTC is
 * auto enabled in it's preferred mode, when it was disabled before.
 * @param root the root window which's default display will be queried
 * @param crtc the CRTC which's configuration should be altered
 * @param outputs an array of outputs, that should display this CRTC's content.
 * @param noutputs number of outputs in the array of outputs.
 * If set to Ecore_X_Randr_Unset, current outputs and number of outputs will be used.
 * If set to Ecore_X_Randr_None, CRTC will be disabled
 * @param x new x coordinate. If <0 (e.g. Ecore_X_Randr_Unset) the current x
 * corrdinate will be assumed.
 * @param y new y coordinate. If <0 (e.g. Ecore_X_Randr_Unset) the current y
 * corrdinate will be assumed.
 * @param mode the new mode to be set. If Ecore_X_Randr_None is passed, the
 * CRTC will be disabled. If Ecore_X_Randr_Unset is passed, the current mode is assumed.
 * @param orientation the new orientation to be set. If Ecore_X_Randr_Unset is used,
 * the current mode is assumed.
 * @return EINA_TRUE if the configuration alteration was successful, else
 * EINA_FALSE
 */
EAPI Eina_Bool
ecore_x_randr_crtc_settings_set(Ecore_X_Window root,
                                Ecore_X_Randr_Crtc crtc,
                                Ecore_X_Randr_Output *outputs,
                                int noutputs,
                                int x,
                                int y,
                                Ecore_X_Randr_Mode mode,
                                Ecore_X_Randr_Orientation orientation)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(EINA_FALSE);
   XRRScreenResources *res = NULL;
   XRRCrtcInfo *crtc_info = NULL;
   Eina_Bool ret = EINA_FALSE;

   if (_ecore_x_randr_crtc_validate(root,
                                    crtc) &&
       (res = _ecore_x_randr_get_screen_resources(_ecore_x_disp, root)))
     {
        if ((crtc_info = XRRGetCrtcInfo(_ecore_x_disp, res, crtc)))
          {
             if ((mode == Ecore_X_Randr_None) ||
                 (noutputs == Ecore_X_Randr_None))
               {
                  outputs = NULL;
                  noutputs = 0;
               }
             else if (noutputs == (int)Ecore_X_Randr_Unset)
               {
                  outputs = (Ecore_X_Randr_Output *)crtc_info->outputs;
                  noutputs = crtc_info->noutput;
               }

             if (mode == Ecore_X_Randr_Unset)
               mode = crtc_info->mode;

             if (x < 0)
               x = crtc_info->x;

             if (y < 0)
               y = crtc_info->y;

             if (orientation == Ecore_X_Randr_Unset)
               orientation = crtc_info->rotation;

             if (!XRRSetCrtcConfig(_ecore_x_disp, res, crtc, CurrentTime,
                                   x, y, mode, orientation, (RROutput *)outputs,
                                   noutputs))
               ret = EINA_TRUE;

             XRRFreeCrtcInfo(crtc_info);
          }

        XRRFreeScreenResources(res);
     }

   return ret;
#else
   return EINA_FALSE;
#endif
}

/**
 * @brief sets a CRTC relative to another one.
 * @param crtc_r1 the CRTC to be positioned.
 * @param crtc_r2 the CRTC the position should be relative to
 * @param policy the relation between the crtcs
 * @param alignment in case CRTCs size differ, aligns CRTC1 accordingly at CRTC2's
 * borders
 * @return EINA_TRUE if crtc could be successfully positioned. EINA_FALSE if
 * repositioning failed or if position of new crtc would be out of given screen's min/max bounds.
 */
EAPI Eina_Bool
ecore_x_randr_crtc_pos_relative_set(Ecore_X_Window root,
                                    Ecore_X_Randr_Crtc crtc_r1,
                                    Ecore_X_Randr_Crtc crtc_r2,
                                    Ecore_X_Randr_Output_Policy policy,
                                    Ecore_X_Randr_Relative_Alignment alignment)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(EINA_FALSE);

   Eina_Rectangle r1_geo, r2_geo;
   int w_max, h_max, cw, ch, x_n = Ecore_X_Randr_Unset, y_n =
     Ecore_X_Randr_Unset;
   /*
      int r1_noutputs, r2_noutputs, r1_nmodes, i, j, outputs_mode_found, mode_w, mode_h;
      Ecore_X_Randr_Output *r1_outputs, *r2_outputs, *r2_r1_outputs;
      Ecore_X_Randr_Mode *r1_modes, r2_mode, r1_mode;
      Eina_Bool ret;
    */

   if ((ecore_x_randr_crtc_mode_get(root, crtc_r1) == Ecore_X_Randr_None)
       || (ecore_x_randr_crtc_mode_get(root, crtc_r2) == Ecore_X_Randr_None))
     return EINA_FALSE;

   if (!_ecore_x_randr_crtc_validate(root, crtc_r1) ||
       (!(crtc_r1 != crtc_r2) &&
        !_ecore_x_randr_crtc_validate(root, crtc_r2)))
     return EINA_FALSE;

   ecore_x_randr_crtc_geometry_get(root,
                                   crtc_r1,
                                   &r1_geo.x,
                                   &r1_geo.y,
                                   &r1_geo.w,
                                   &r1_geo.h);
   ecore_x_randr_crtc_geometry_get(root,
                                   crtc_r2,
                                   &r2_geo.x,
                                   &r2_geo.y,
                                   &r2_geo.w,
                                   &r2_geo.h);
   ecore_x_randr_screen_size_range_get(root, NULL, NULL, &w_max, &h_max);
   ecore_x_randr_screen_current_size_get(root, &cw, &ch, NULL, NULL);

   switch (policy)
     {
      case ECORE_X_RANDR_OUTPUT_POLICY_RIGHT:
        //set r1 right of r2
        x_n = r2_geo.x + r2_geo.w;

        switch (alignment)
          {
           case ECORE_X_RANDR_RELATIVE_ALIGNMENT_NONE:
             y_n = Ecore_X_Randr_Unset;
             break;

           case ECORE_X_RANDR_RELATIVE_ALIGNMENT_CENTER_REL:
             y_n =
               ((int)(((double)r2_geo.h /
                       2.0) + (double)r2_geo.y - ((double)r1_geo.h / 2.0)));
             break;

           case ECORE_X_RANDR_RELATIVE_ALIGNMENT_CENTER_SCR:
             y_n = ((int)((double)ch / 2.0) - ((double)r1_geo.h / 2.0));
             break;
          }
        break;

      case ECORE_X_RANDR_OUTPUT_POLICY_LEFT:
        //set r1 left of r2
        x_n = r2_geo.x - r1_geo.w;

        switch (alignment)
          {
           case ECORE_X_RANDR_RELATIVE_ALIGNMENT_NONE:
             y_n = Ecore_X_Randr_Unset;
             break;

           case ECORE_X_RANDR_RELATIVE_ALIGNMENT_CENTER_REL:
             y_n =
               ((int)(((double)r2_geo.h /
                       2.0) + r2_geo.y - ((double)r1_geo.h / 2.0)));
             break;

           case ECORE_X_RANDR_RELATIVE_ALIGNMENT_CENTER_SCR:
             y_n = ((int)(((double)ch / 2.0) - ((double)r1_geo.h / 2.0)));
             break;
          }
        break;

      case ECORE_X_RANDR_OUTPUT_POLICY_BELOW:
        //set r1 below r2
        y_n = r2_geo.y + r2_geo.h;

        switch (alignment)
          {
           case ECORE_X_RANDR_RELATIVE_ALIGNMENT_NONE:
             x_n = Ecore_X_Randr_Unset;
             break;

           case ECORE_X_RANDR_RELATIVE_ALIGNMENT_CENTER_REL:
             x_n =
               ((int)((((double)r2_geo.x +
                        (double)r2_geo.w) / 2.0) - ((double)r1_geo.w / 2.0)));
             break;

           case ECORE_X_RANDR_RELATIVE_ALIGNMENT_CENTER_SCR:
             x_n = ((int)((double)cw / 2.0));
             break;
          }
        break;

      case ECORE_X_RANDR_OUTPUT_POLICY_ABOVE:
        y_n = r2_geo.y - r1_geo.h;

        //set r1 above r2
        switch (alignment)
          {
           case ECORE_X_RANDR_RELATIVE_ALIGNMENT_NONE:
             x_n = Ecore_X_Randr_Unset;
             break;

           case ECORE_X_RANDR_RELATIVE_ALIGNMENT_CENTER_REL:
             x_n =
               ((int)((((double)r2_geo.x +
                        (double)r2_geo.w) / 2.0) - ((double)r1_geo.w / 2.0)));
             break;

           case ECORE_X_RANDR_RELATIVE_ALIGNMENT_CENTER_SCR:
             x_n = ((int)((double)cw / 2.0));
             break;
          }
        break;

      case ECORE_X_RANDR_OUTPUT_POLICY_CLONE:
        return ecore_x_randr_crtc_pos_set(root, crtc_r1, r2_geo.x, r2_geo.y);

      /* entire cloning (including modesetting)
         //all outputs of crtc1 capable of crtc2's current mode?
         r2_mode = ecore_x_randr_crtc_mode_get(root, crtc_r2);
         if (!(r1_outputs =
               ecore_x_randr_crtc_outputs_get(root, crtc_r1,
                                              &r1_noutputs)) ||
          (r1_noutputs == 0))
         return EINA_FALSE;

         for (i = 0, outputs_mode_found = 0; i < r1_noutputs; i++)
         {
           if (!(r1_modes =
                    ecore_x_randr_output_modes_get(root, r1_outputs[i],
                                                   &r1_nmodes, NULL)))
             {
                free(r1_outputs);
                return EINA_FALSE;
             }

           for (j = 0; j < r1_nmodes; j++)
             {
                ecore_x_randr_mode_size_get(root,
                                            r1_modes[j],
                                            &mode_w,
                                            &mode_h);
                if ((mode_w == r2_geo.w) && (mode_h == r2_geo.h))
                  {
                     r1_mode = r1_modes[j];
         ++outputs_mode_found;
                     free(r1_modes);
                     r1_modes = NULL;
                     break;
                  }
             }
           if (r1_modes)
                     free(r1_modes);

           if (outputs_mode_found <= i)
             {
                //an output doesn't support the set mode, cancel!
                     free(r1_outputs);
                return EINA_FALSE;
             }
         }
                     free (r1_outputs);
         //CRTC 1's outputs support a mode of same geometry as CRTC 2.
         ret =
         (ecore_x_randr_crtc_mode_set(root, crtc_r1, Ecore_X_Randr_None,
                                      Ecore_X_Randr_None,
                                      r1_mode) &&
          ecore_x_randr_crtc_pos_set(root, crtc_r1, r2_geo.x, r2_geo.y));
         return ret;
       */

      /* entire cloning on same CRTC
         //all outputs of crtc1 capable of crtc2's current mode?
         r2_mode = ecore_x_randr_crtc_mode_get(root, crtc_r2);
         if (!(r1_outputs =
               ecore_x_randr_crtc_outputs_get(root, crtc_r1,
                                              &r1_noutputs)) ||
          (r1_noutputs == 0))
         return EINA_FALSE;

         for (i = 0, outputs_mode_found = 0; i < r1_noutputs; i++)
         {
           if (!(r1_modes =
                    ecore_x_randr_output_modes_get(root, r1_outputs[i],
                                                   &r1_nmodes, NULL)))
             {
                     free(r1_outputs);
                return EINA_FALSE;
             }

           for (j = 0; j < r1_nmodes; j++)
             {
                if (r1_modes[j] == r2_mode)
                  {
         ++outputs_mode_found;
                     free(r1_modes);
                     r1_modes = NULL;
                     break;
                  }
             }
           if (r1_modes)
                     free(r1_modes);

           if (outputs_mode_found <= i)
             {
                //an output doesn't support the set mode, cancel!
                     free(r1_outputs);
                return EINA_FALSE;
             }
         }
         //check whether crtc r2 can use all outputs of r1.
         if (!(r2_outputs =
               ecore_x_randr_crtc_possible_outputs_get(root, crtc_r2,
                                                       &r2_noutputs)) ||
          (r2_noutputs == 0))
         {
                free(r1_outputs);
           return EINA_FALSE;
         }

         for (i = 0; i < r1_noutputs; i++)
         {
           for (j = 0; j < r2_noutputs; )
             {
                if (r1_outputs[i] == r2_outputs[j])
                   break;

                j++;
             }
           if (j == r2_noutputs)
             {
                //didn't find the output!
                free (r1_outputs);
                free (r2_outputs);
                return EINA_FALSE;
             }
         }

         //apparently crtc2 supports all outputs of r1
         //TODO: check with the compatible list of outputs (property in RR1.3)
         r2_r1_outputs =
         malloc(sizeof(Ecore_X_Randr_Output) * (r1_noutputs + r2_noutputs));
         for (i = 0; i < r1_noutputs; i++)
         {
           r2_r1_outputs[i] = r1_outputs[i];
         }
         free(r1_outputs);
         for (; i < r2_noutputs; i++)
         {
           r2_r1_outputs[i] = r2_outputs[i];
         }
         free(r2_outputs);
         ret =
         ecore_x_randr_crtc_mode_set(root, crtc_r2, r2_r1_outputs,
                                     (r1_noutputs + r1_noutputs), r2_mode);
         free (r2_r1_outputs);
         return ret;
       */
      case ECORE_X_RANDR_OUTPUT_POLICY_NONE:
        break;
     }
   if ((x_n == r1_geo.x) && (y_n == r1_geo.x))
     return EINA_TRUE;

   //out of possible bounds?
   if (((y_n + r1_geo.h) > h_max) || ((x_n + r1_geo.w) > w_max))
     return EINA_FALSE;

   return ecore_x_randr_crtc_pos_set(root, crtc_r1, x_n, y_n);
#else
   return EINA_FALSE;
#endif
}

/*
 * @brief add given mode to given output
 * @param output the output the mode is added to
 * @param mode the mode added to the output
 * @return EINA_FALSE if output or mode equal Ecore_X_Randr_None, else EINA_TRUE
 * Additionally, if xcb backend is used, the success of the addition is reported
 * back directly.
 * @since 1.2.0
 */
EAPI Eina_Bool
ecore_x_randr_output_mode_add(Ecore_X_Randr_Output output,
                              Ecore_X_Randr_Mode mode)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(EINA_FALSE);

   if ((output == Ecore_X_Randr_None) || (mode == Ecore_X_Randr_None))
     return EINA_FALSE;

   XRRAddOutputMode(_ecore_x_disp, output, mode);
   return EINA_TRUE;
#else
   return EINA_FALSE;
#endif
}

/*
 * @brief delete given mode from given output
 * @param output the output the mode is removed from
 * @param mode the mode removed from the output
 * @since 1.2.0
 */
EAPI void
ecore_x_randr_output_mode_del(Ecore_X_Randr_Output output,
                              Ecore_X_Randr_Mode mode)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET();

   if ((output == Ecore_X_Randr_None) || (mode == Ecore_X_Randr_None))
     return;

   XRRDeleteOutputMode(_ecore_x_disp, output, mode);
#else
   return;
#endif
}

EAPI Ecore_X_Randr_Mode *
ecore_x_randr_output_modes_get(Ecore_X_Window root,
                               Ecore_X_Randr_Output output,
                               int *num,
                               int *npreferred)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(NULL);
   XRRScreenResources *res = NULL;
   XRROutputInfo *output_info = NULL;
   Ecore_X_Randr_Mode *modes = NULL;

   if ((output != Ecore_X_Randr_None)
       && (res = _ecore_x_randr_get_screen_resources(_ecore_x_disp, root))
       && (output_info =
             XRRGetOutputInfo(_ecore_x_disp, res, (RROutput)output)))
     {
        if ((modes = malloc(sizeof(Ecore_X_Randr_Mode) * output_info->nmode)))
          {
             memcpy(modes, output_info->modes,
                    (sizeof(Ecore_X_Randr_Mode) * output_info->nmode));
             if (num)
               *num = output_info->nmode;

             if (npreferred)
               *npreferred = output_info->npreferred;
          }
     }

   if (output_info)
     XRRFreeOutputInfo(output_info);

   if (res)
     XRRFreeScreenResources(res);

   return modes;
#else
   return NULL;
#endif
}

EAPI Ecore_X_Randr_Crtc *
ecore_x_randr_output_possible_crtcs_get(Ecore_X_Window root,
                                        Ecore_X_Randr_Output output,
                                        int *num)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(NULL);
   XRRScreenResources *res = NULL;
   XRROutputInfo *output_info = NULL;
   Ecore_X_Randr_Crtc *crtcs = NULL;

   if ((output != Ecore_X_Randr_None))
     {
        if ((res = _ecore_x_randr_get_screen_resources(_ecore_x_disp, root)))
          {
             if ((output_info = XRRGetOutputInfo(_ecore_x_disp, res, output)))
               {
                  if ((crtcs = malloc(sizeof(Ecore_X_Randr_Crtc) * output_info->ncrtc)))
                    {
                       memcpy(crtcs, output_info->crtcs, (sizeof(Ecore_X_Randr_Crtc) * output_info->ncrtc));
                       if (num) *num = output_info->ncrtc;
                    }
                  XRRFreeOutputInfo(output_info);
               }
             XRRFreeScreenResources(res);
          }
     }
   return crtcs;
#else
   return Ecore_X_Randr_None;
#endif
}

/**
 * @brief gets the the outputs which might be used simultenously on the same
 * CRTC.
 * @param root window that this information should be queried for.
 * @param output the output which's clones we concern
 * @param num number of possible clones
 */
EAPI Ecore_X_Randr_Output *
ecore_x_randr_output_clones_get(Ecore_X_Window root,
                                Ecore_X_Randr_Output output,
                                int *num)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(NULL);
   XRRScreenResources *res = NULL;
   XRROutputInfo *output_info = NULL;
   Ecore_X_Randr_Output *outputs = NULL;

   if ((output != Ecore_X_Randr_None))
     {
        if ((res = _ecore_x_randr_get_screen_resources(_ecore_x_disp, root)))
          {
             if ((output_info = XRRGetOutputInfo(_ecore_x_disp, res, output)))
               {
                  if ((outputs = malloc(sizeof(Ecore_X_Randr_Output) * output_info->nclone)))
                    {
                       memcpy(outputs, output_info->clones, (sizeof(Ecore_X_Randr_Output) * output_info->nclone));
                       if (num) *num = output_info->nclone;
                    }
                  XRRFreeOutputInfo(output_info);
               }
             XRRFreeScreenResources(res);
          }
     }
   return outputs;
#else
   return Ecore_X_Randr_None;
#endif
}

EAPI Ecore_X_Randr_Crtc
ecore_x_randr_output_crtc_get(Ecore_X_Window root,
                              Ecore_X_Randr_Output output)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(Ecore_X_Randr_None);
   XRRScreenResources *res = NULL;
   XRROutputInfo *output_info = NULL;
   Ecore_X_Randr_Crtc ret = Ecore_X_Randr_None;

   if ((output != Ecore_X_Randr_None))
     {
        if ((res = _ecore_x_randr_get_screen_resources(_ecore_x_disp, root)))
          {
             if ((output_info = XRRGetOutputInfo(_ecore_x_disp, res, output)))
               {
                  ret = output_info->crtc;
                  XRRFreeOutputInfo(output_info);
               }
             XRRFreeScreenResources(res);
          }
     }

   return ret;
#else
   return Ecore_X_Randr_None;
#endif
}

/**
 * @brief gets the given output's name as reported by X
 * @param root the window which's screen will be queried
 * @param len length of returned c-string.
 * @return name of the output as reported by X
 */
EAPI char *
ecore_x_randr_output_name_get(Ecore_X_Window root,
                              Ecore_X_Randr_Output output,
                              int *len)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(NULL);
   XRRScreenResources *res = NULL;
   XRROutputInfo *output_info = NULL;
   char *ret = NULL;

   if ((output != Ecore_X_Randr_None)
       && (res = _ecore_x_randr_get_screen_resources(_ecore_x_disp, root))
       && (output_info = XRRGetOutputInfo(_ecore_x_disp, res, output)))
     {
        /*
         * Actually the below command is correct, but due to a bug in libXrandr
         * it doesn't work. Therefore we stick with strlen().
         * Replace the line below with the following once this bug is
         * fixed within libXrandr.
         *
         *    *len = output_info->nameLen;
         *
         */
        if ((ret = strdup(output_info->name)) && len)
          *len = strlen(ret);

        XRRFreeOutputInfo(output_info);
     }

   if (res)
     XRRFreeScreenResources(res);

   return ret;
#else
   return NULL;
#endif
}

/**
 * @brief gets the width and hight of a given mode
 * @param mode the mode which's size is to be looked up
 * @param w width of given mode in px
 * @param h height of given mode in px
 */
EAPI void
ecore_x_randr_mode_size_get(Ecore_X_Window root,
                            Ecore_X_Randr_Mode mode,
                            int *w,
                            int *h)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET();
   XRRScreenResources *res = NULL;
   int i;

   if ((mode != Ecore_X_Randr_None)
       && (w || h)
       && (res = _ecore_x_randr_get_screen_resources(_ecore_x_disp, root)))
     {
        for (i = 0; i < res->nmode; i++)
          {
             if (res->modes[i].id == mode)
               {
                  if (w)
                    *w = res->modes[i].width;

                  if (h)
                    *h = res->modes[i].height;

                  break;
               }
          }
     }

   if (res)
     XRRFreeScreenResources(res);

#endif
}

/**
 * @brief gets the EDID information of an attached output if available.
 * Note that this information is not to be compared using ordinary string
 * comparison functions, since it includes 0-bytes.
 * @param root window this information should be queried from
 * @param output the XID of the output
 * @param length length of the byte-array. If NULL, request will fail.
 */
EAPI unsigned char *
ecore_x_randr_output_edid_get(Ecore_X_Window root,
                              Ecore_X_Randr_Output output,
                              unsigned long *length)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(NULL);
   Atom name = XInternAtom (_ecore_x_disp, RANDR_PROPERTY_EDID, False);
   unsigned char *prop_data, *ret = NULL;
   int actual_format;
   unsigned long nitems, bytes_after;
   Atom actual_type;

   if (!length || !_ecore_x_randr_output_validate(root, output))
     return NULL;

   if (XRRGetOutputProperty (_ecore_x_disp, output, name,
                             0, 100, False, False,
                             AnyPropertyType,
                             &actual_type, &actual_format,
                             &nitems, &bytes_after, &prop_data) == Success)
     {
        if (actual_type == XA_INTEGER && actual_format == 8)
          {
             if ((ret = malloc(nitems * sizeof(unsigned char))))
               {
                  if (length &&
                      (memcpy(ret, prop_data, (nitems * sizeof(unsigned char)))))
                    *length = nitems;

                  return ret;
               }
          }
     }

   return NULL;
#else
   return NULL;
#endif
}

EAPI Ecore_X_Randr_Connection_Status
ecore_x_randr_output_connection_status_get(Ecore_X_Window root,
                                           Ecore_X_Randr_Output output)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(ECORE_X_RANDR_CONNECTION_STATUS_UNKNOWN);
   XRRScreenResources *res = NULL;
   XRROutputInfo *output_info = NULL;
   Ecore_X_Randr_Connection_Status ret =
     ECORE_X_RANDR_CONNECTION_STATUS_UNKNOWN;

   if ((output != Ecore_X_Randr_None)
       && (res = _ecore_x_randr_get_screen_resources(_ecore_x_disp, root))
       && (output_info = XRRGetOutputInfo(_ecore_x_disp, res, output)))
     {
        ret = output_info->connection;
     }

   if (output_info)
     XRRFreeOutputInfo(output_info);

   if (res)
     XRRFreeScreenResources(res);

   return ret;
#else
   return ECORE_X_RANDR_CONNECTION_STATUS_UNKNOWN;
#endif
}

EAPI void
ecore_x_randr_output_size_mm_get(Ecore_X_Window root,
                                 Ecore_X_Randr_Output output,
                                 int *w_mm,
                                 int *h_mm)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET();
   XRRScreenResources *res = NULL;
   XRROutputInfo *output_info = NULL;

   if ((output != Ecore_X_Randr_None)
       && (res = _ecore_x_randr_get_screen_resources(_ecore_x_disp, root)))
     {
        if ((output_info =
               XRRGetOutputInfo(_ecore_x_disp, res, (RROutput)output)))
          {
             if (w_mm)
               *w_mm = output_info->mm_width;

             if (h_mm)
               *h_mm = output_info->mm_height;

             XRRFreeOutputInfo(output_info);
          }

        XRRFreeScreenResources(res);
     }

#endif
}

EAPI Eina_Bool
ecore_x_randr_move_all_crtcs_but(Ecore_X_Window root,
                                 const Ecore_X_Randr_Crtc *not_moved,
                                 int nnot_moved,
                                 int dx,
                                 int dy)
{
#ifdef ECORE_XRANDR
   Ecore_X_Randr_Crtc *crtcs_to_be_moved = NULL;
   XRRScreenResources *res = NULL;
   int i, j, k, n;
   Eina_Bool ret;

   if ((nnot_moved <= 0) || (!not_moved)
       || !_ecore_x_randr_root_validate(root)
       || !(res =
              _ecore_x_randr_get_screen_resources (_ecore_x_disp, root)))
     return EINA_FALSE;

   n = (res->ncrtc - nnot_moved);
   if ((crtcs_to_be_moved = malloc(sizeof(Ecore_X_Randr_Crtc) * n)))
     {
        for (i = 0, k = 0; (i < res->ncrtc) && (k < n); i++)
          {
             for (j = 0; j < nnot_moved; j++)
               {
                  if (res->crtcs[i] == not_moved[j])
                    break;
               }
             if (j == nnot_moved)
               //crtcs[i] is not in the 'not to move'-list
               crtcs_to_be_moved[k++] = res->crtcs[i];
          }
     }

   XRRFreeScreenResources(res);
   ret = ecore_x_randr_move_crtcs(root, crtcs_to_be_moved, n, dx, dy);
   free(crtcs_to_be_moved);
   return ret;
#else
   return EINA_FALSE;
#endif
}

/*
 * @brief move given CRTCs belonging to the given root window's screen dx/dy pixels relative to their current position. The screen size will be automatically adjusted if necessary and possible.
 * @param root window which's screen's resources are used
 * @param crtcs list of CRTCs to be moved
 * @param ncrtc number of CRTCs in array
 * @param dx amount of pixels the CRTCs should be moved in x direction
 * @param dy amount of pixels the CRTCs should be moved in y direction
 * @return EINA_TRUE if all crtcs could be moved successfully.
 */
EAPI Eina_Bool
ecore_x_randr_move_crtcs(Ecore_X_Window root,
                         const Ecore_X_Randr_Crtc *crtcs,
                         int ncrtc,
                         int dx,
                         int dy)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(EINA_FALSE);
   XRRScreenResources *res = NULL;
   XRRCrtcInfo **crtc_info = NULL;
   Eina_Bool ret = EINA_TRUE;
   int i, cw, ch, w_max, h_max, nw, nh;

   crtc_info = alloca(sizeof(XRRCrtcInfo *) * ncrtc);
   memset(crtc_info, 0, sizeof(XRRCrtcInfo *) * ncrtc);
   if (_ecore_x_randr_root_validate(root)
       && (res = _ecore_x_randr_get_screen_resources (_ecore_x_disp, root)))
     {
        ecore_x_randr_screen_size_range_get(root, NULL, NULL, &w_max, &h_max);
        ecore_x_randr_screen_current_size_get(root, &cw, &ch, NULL, NULL);
        nw = cw;
        nh = ch;

        for (i = 0;
             (i < ncrtc) &&
             (crtc_info[i] = XRRGetCrtcInfo(_ecore_x_disp, res, crtcs[i]));
             i++)
          {
             if (((crtc_info[i]->x + dx) < 0) ||
                 ((int)(crtc_info[i]->x + crtc_info[i]->width + dx) > w_max)
                 || ((crtc_info[i]->y + dy) < 0) ||
                 ((int)(crtc_info[i]->y + crtc_info[i]->height + dy) > h_max)
                 )
               goto _ecore_x_randr_move_crtcs_fail_free_crtc_info;

             nw = MAX((int)(crtc_info[i]->x + crtc_info[i]->width + dx), nw);
             nh = MAX((int)(crtc_info[i]->y + crtc_info[i]->height + dy), nh);
          }
        //not out of bounds

        //resize if necessary
        if (!(((nw > cw) ||
               (nh > ch)) ||
              ecore_x_randr_screen_current_size_set(root, nw, nh,
                                                    Ecore_X_Randr_Unset,
                                                    Ecore_X_Randr_Unset)))
          goto _ecore_x_randr_move_crtcs_fail_free_crtc_info;

        //actually move all the crtcs, keep their rotation and mode.
        for (i = 0; (i < ncrtc) && crtc_info[i]; i++)
          {
             if ((crtc_info[i]) &&
                 (!ecore_x_randr_crtc_settings_set(root, crtcs[i], NULL,
                                                   Ecore_X_Randr_Unset,
                                                   (crtc_info[i]->x + dx),
                                                   (crtc_info[i]->y + dy),
                                                   crtc_info[i]->mode,
                                                   crtc_info[i]->rotation)))
               {
                  ret = EINA_FALSE;
                  break;
               }
          }
        if (i < ncrtc)
          {
             //something went wrong, let's try to move the already moved crtcs
             //back.
             while ((i--) >= 0)
               {
                  if (crtc_info[i])
                    ecore_x_randr_crtc_settings_set(root,
                                                    crtcs[i],
                                                    NULL,
                                                    Ecore_X_Randr_Unset,
                                                    (crtc_info[i]->x - dx),
                                                    (crtc_info[i]->y - dy),
                                                    crtc_info[i]->mode,
                                                    crtc_info[i]->rotation);
               }
          }

        for (i = 0; i < ncrtc; i++)
          {
             if (crtc_info[i]) XRRFreeCrtcInfo(crtc_info[i]);
          }
     }

   XRRFreeScreenResources(res);

   return ret;
_ecore_x_randr_move_crtcs_fail_free_crtc_info:
   while (i-- > 0)
     XRRFreeCrtcInfo(crtc_info[i]);
   XRRFreeScreenResources(res);
   return EINA_FALSE;
#else
   return EINA_FALSE;
#endif
}

/**
 * @brief removes unused screen space. The most upper left CRTC is set to 0x0
 * and all other CRTCs dx,dy respectively.
 * @param root the window's screen which will be reset.
 */
EAPI void
ecore_x_randr_screen_reset(Ecore_X_Window root)
{
#ifdef ECORE_XRANDR
   XRRCrtcInfo *crtc_info = NULL;
   XRRScreenResources *res = NULL;
   //the 100000 are just a random huge number.
   int i, dx_min = 100000, dy_min = 100000, w_n = 0, h_n = 0, nenabled_crtcs = 0;

   if (!_ecore_x_randr_root_validate(root) ||
       !(res = _ecore_x_randr_get_screen_resources(_ecore_x_disp, root)))
     return;

   Ecore_X_Randr_Crtc enabled_crtcs[res->ncrtc];

   for (i = 0; i < res->ncrtc; i++)
     {
        if (!(crtc_info =
                XRRGetCrtcInfo(_ecore_x_disp, res,
                               res->crtcs[i])) ||
            (crtc_info->mode == Ecore_X_Randr_None) ||
            (crtc_info->mode == Ecore_X_Randr_Unset)
            || ((crtc_info->noutput == 0)))
          continue;

        enabled_crtcs[nenabled_crtcs++] = res->crtcs[i];

        if ((int)(crtc_info->x + crtc_info->width) > w_n)
          w_n = (crtc_info->x + crtc_info->width);

        if ((int)(crtc_info->y + crtc_info->height) > h_n)
          h_n = (crtc_info->y + crtc_info->height);

        if (crtc_info->x < dx_min)
          dx_min = crtc_info->x;
        if (crtc_info->y < dy_min)
          dy_min = crtc_info->y;

        XRRFreeCrtcInfo(crtc_info);
     }
   if ((dx_min > 0) || (dy_min > 0))
     {
        if (ecore_x_randr_move_crtcs(root, enabled_crtcs, nenabled_crtcs, -dx_min, -dy_min))
          {
             w_n -= dx_min;
             h_n -= dy_min;
          }
     }
   ecore_x_randr_screen_current_size_set(root,
                                         w_n,
                                         h_n,
                                         Ecore_X_Randr_Unset,
                                         Ecore_X_Randr_Unset);
#endif
}

/**
 * @brief set up the backlight level to the given level.
 * @param root the window's screen which will be set.
 * @param level of the backlight between 0 and 1
 */

EAPI void
ecore_x_randr_screen_backlight_level_set(Ecore_X_Window root,
                                         double level)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET();
   Atom _backlight;
   XRRScreenResources *resources = NULL;
   Ecore_X_Randr_Output output;
   int o;

   if ((level < 0) || (level > 1))
     {
        ERR("Wrong value for the backlight level. It should be between 0 and 1.");
        return;
     }

   /*
    * To make sure that the _backlight atomic property still exists.
    */
   _backlight = XInternAtom(_ecore_x_disp, RANDR_PROPERTY_BACKLIGHT, True);
   if (_backlight == None)
     {
        WRN("Backlight setting is not supported on this server or driver");
        return;
     }

   /* get the ressources */
   resources = _ecore_x_randr_get_screen_resources(_ecore_x_disp, root);
   if (!resources) return;

   for (o = 0; o < resources->noutput; o++)
     {
        output = resources->outputs[o];
        if (ecore_x_randr_output_backlight_level_get(root, output) >= 0)
          {
             ecore_x_randr_output_backlight_level_set(root, output, level);
          }
     }
   XRRFreeScreenResources(resources);
#endif
}

/*
 * @brief check if a backlight is available
 * @return whether a blacklight is available
 */

EAPI Eina_Bool
ecore_x_randr_output_backlight_available(void)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(-1);
   Atom _backlight;

   _backlight = XInternAtom(_ecore_x_disp, RANDR_PROPERTY_BACKLIGHT, True);

   return (_backlight == None) ? EINA_FALSE : EINA_TRUE;

#endif
   return EINA_FALSE;
}

/*
 * @brief get the backlight level of the given output
 * @param root window which's screen should be queried
 * @param output from which the backlight level should be retrieved
 * @return the backlight level
 */

EAPI double
ecore_x_randr_output_backlight_level_get(Ecore_X_Window root,
                                         Ecore_X_Randr_Output output)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(-1);
   Atom actual_type;
   Atom _backlight;
   XRRPropertyInfo *info = NULL;
   double dvalue;
   int actual_format;
   long value, max, min;
   unsigned long nitems;
   unsigned long bytes_after;
   unsigned char *prop = NULL;

   /* set backlight variable if not already done */

   _backlight = XInternAtom(_ecore_x_disp, RANDR_PROPERTY_BACKLIGHT, True);
   if (_backlight == None)
     {
        ERR("Backlight property is not suppported on this server or driver");
        return -1;
     }

   if (!_ecore_x_randr_output_validate(root, output))
     {
        ERR("Invalid output");
        return -1;
     }

   if (XRRGetOutputProperty(_ecore_x_disp, output, _backlight,
                            0, 4, False, False, None,
                            &actual_type, &actual_format,
                            &nitems, &bytes_after, &prop) != Success)
     {
        WRN("Backlight not supported on this output");
        return -1;
     }

   if ((actual_type != XA_INTEGER) || (nitems != 1) || (actual_format != 32)) return -1;

   value = *((long *)prop);
   free (prop);

   /* I have the current value of the backlight */
   /* Now retrieve the min and max intensities of the output */
   info = XRRQueryOutputProperty(_ecore_x_disp, output, _backlight);
   if (info)
     {
        dvalue = -1;
        if ((info->range) && (info->num_values == 2))
          {
             /* finally convert the current value in the interval [0..1] */
             min = info->values[0];
             max = info->values[1];
             dvalue = ((double)(value - min)) / ((double)(max - min));
          }
        free(info);
        return dvalue;
     }
#endif
   return -1;
}

/*
 * @brief set the backlight level of a given output
 * @param root window which's screen should be queried
 * @param output that should be set
 * @param level for which the backlight should be set
 * @return EINA_TRUE in case of success
 */

EAPI Eina_Bool
ecore_x_randr_output_backlight_level_set(Ecore_X_Window root,
                                         Ecore_X_Randr_Output output,
                                         double level)
{
#ifdef ECORE_XRANDR
   RANDR_CHECK_1_2_RET(EINA_FALSE);
   Atom _backlight;
   XRRPropertyInfo *info = NULL;
   double min, max, tmp;
   long new;

   if ((level < 0) || (level > 1))
     {
        ERR("Backlight level should be between 0 and 1");
        return EINA_FALSE;
     }

   if (!_ecore_x_randr_output_validate(root, output))
     {
        ERR("Wrong output value");
        return EINA_FALSE;
     }

   _backlight = XInternAtom(_ecore_x_disp, RANDR_PROPERTY_BACKLIGHT, True);
   if (_backlight == None)
     {
        WRN("Backlight property is not suppported on this server or driver");
        return EINA_FALSE;
     }

   info = XRRQueryOutputProperty(_ecore_x_disp, output, _backlight);
   if (info)
     {
        if ((info->range) && (info->num_values == 2))
          {
             min = info->values[0];
             max = info->values[1];
             tmp = (level * (max - min)) + min;
             new = tmp;
             if (new > max) new = max;
             if (new < min) new = min;
             XRRChangeOutputProperty(_ecore_x_disp, output, _backlight, XA_INTEGER, 32,
                                     PropModeReplace, (unsigned char *)&new, 1);
             XFlush(_ecore_x_disp);
          }
        free(info);
        return EINA_TRUE;
     }
#endif
   return EINA_FALSE;
}

/*
 * @brief get the outputs, which display a certain window
 * @param window window the displaying outputs shall be found for
 * @param num the number of outputs displaying the window
 * @return array of outputs that display a certain window. NULL if no outputs
 * was found that displays the specified window.
 */

EAPI Ecore_X_Randr_Output *
ecore_x_randr_window_outputs_get(Ecore_X_Window window,
                                 int *num)
{
#ifdef ECORE_XRANDR
   Ecore_X_Window root;
   Ecore_X_Randr_Crtc *crtcs;
   Ecore_X_Randr_Output *outputs, *ret = NULL;
   int ncrtcs, noutputs, i, nret = 0;

   if (_randr_version < RANDR_1_2) goto _ecore_x_randr_current_output_get_fail;

   root = ecore_x_window_root_get(window);
   if (!(crtcs = ecore_x_randr_window_crtcs_get(window, &ncrtcs)))
     goto _ecore_x_randr_current_output_get_fail;

   for (i = 0, nret = 0; i < ncrtcs; i++)
     {

        outputs = ecore_x_randr_crtc_outputs_get(root, crtcs[i],
              &noutputs);
        if (!outputs)
          goto _ecore_x_randr_current_output_get_fail_free;
        nret += noutputs;
        ret = realloc(ret, (nret * sizeof(Ecore_X_Randr_Output)));
        memcpy(&ret[nret], outputs, (noutputs * sizeof(Ecore_X_Randr_Output)));
        free(outputs);
     }
   free(crtcs);

   if (num)
     *num = nret;

   return ret;

_ecore_x_randr_current_output_get_fail_free:
   free(outputs);
   free(crtcs);
   free(ret);
_ecore_x_randr_current_output_get_fail:
#endif
   if (num) *num = 0;
   return NULL;
}

/*
 * @deprecated bad naming. Use ecore_x_randr_window_outputs_get instead.
 * @brief get the outputs, which display a certain window
 * @param window window the displaying outputs shall be found for
 * @param num the number of outputs displaying the window
 * @return array of outputs that display a certain window. NULL if no outputs
 * was found that displays the specified window.
 */

EAPI Ecore_X_Randr_Output *
ecore_x_randr_current_output_get(Ecore_X_Window window,
                                 int *num)
{
   return ecore_x_randr_window_outputs_get(window, num);
}