summaryrefslogtreecommitdiff
path: root/src/lib/ecore/ecore_main.c
blob: 4b27e180a20cf01051635c14f5810e9f0d338d66 (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
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#define EINA_SLSTR_INTERNAL

#ifdef _WIN32
# ifndef USER_TIMER_MINIMUM
#  define USER_TIMER_MINIMUM 0x0a
# endif
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/time.h>

#ifdef HAVE_SYSTEMD
# include <systemd/sd-daemon.h>
#endif

#ifdef HAVE_IEEEFP_H
# include <ieeefp.h> // for Solaris
#endif

#ifdef HAVE_ISFINITE
# define ECORE_FINITE(t)  isfinite(t)
#else
# define ECORE_FINITE(t) finite(t)
#endif

//#define FIX_HZ 1

#ifdef FIX_HZ
# include <sys/param.h>
# ifndef HZ
#  define HZ 100
# endif
#endif

#ifdef _WIN32
# include <Evil.h>
#endif

#include "Ecore.h"
#include "ecore_private.h"

#include "ecore_main_common.h"

#include "eina_internal.h"

#ifdef USE_G_MAIN_LOOP
# include <glib.h>
#endif

#ifdef HAVE_LIBUV
# ifdef HAVE_NODE_UV_H
#  include <node/uv.h>
# elif defined(HAVE_NODEJS_DEPS_UV_UV_H)
#  include <nodejs/deps/uv/uv.h>
# elif defined(HAVE_NODEJS_DEPS_UV_INCLUDE_UV_H)
#  include <nodejs/deps/uv/include/uv.h>
# elif defined(HAVE_NODEJS_SRC_UV_H)
#  include <nodejs/src/uv.h>
# elif defined(HAVE_UV_H)
#  include <uv.h>
# else
#  error No uv.h header found?
# endif

// XXX: FIXME: use eina_module
# if defined(HAVE_DLOPEN) && !defined(_WIN32)
#  include <dlfcn.h>
# endif

static uv_prepare_t _ecore_main_uv_prepare;
static uv_check_t _ecore_main_uv_check;
static uv_timer_t _ecore_main_uv_handle_timers;
static Eina_Bool _ecore_main_uv_idling;

static int (*_dl_uv_loop_alive)(uv_loop_t*) = 0;
static int (*_dl_uv_run)(uv_loop_t*, uv_run_mode mode) = 0;
static int (*_dl_uv_stop)(uv_loop_t*) = 0;
static uv_loop_t* (*_dl_uv_default_loop)() = 0;
static int (*_dl_uv_poll_init_socket)(uv_loop_t* loop, uv_poll_t* handle, uv_os_sock_t fd) = 0;
static int (*_dl_uv_poll_init)(uv_loop_t* loop, uv_poll_t* handle, int fd) = 0;
static int (*_dl_uv_poll_start)(uv_poll_t* handle, int events, uv_poll_cb cb) = 0;
static int (*_dl_uv_poll_stop)(uv_poll_t* handle) = 0;
static int (*_dl_uv_timer_init)(uv_loop_t*, uv_timer_t* handle);
static int (*_dl_uv_timer_start)(uv_timer_t* handle,
                                 uv_timer_cb cb,
                                 uint64_t timeout,
                                 uint64_t repeat);
static int (*_dl_uv_timer_stop)(uv_timer_t* handle);
static int (*_dl_uv_prepare_init)(uv_loop_t*, uv_prepare_t* prepare);
static int (*_dl_uv_prepare_start)(uv_prepare_t* prepare, uv_prepare_cb cb);
static int (*_dl_uv_prepare_stop)(uv_prepare_t* prepare);
static int (*_dl_uv_check_init)(uv_loop_t*, uv_check_t* prepare);
static int (*_dl_uv_check_start)(uv_check_t* prepare, uv_check_cb cb);
static int (*_dl_uv_check_stop)(uv_check_t* prepare);
static int (*_dl_uv_close)(uv_handle_t* handle, uv_close_cb close_cb);
#endif

#define NS_PER_SEC (1000000000.0)

struct _Ecore_Fd_Handler
{
   EINA_INLIST;
   ECORE_MAGIC;
   Ecore_Fd_Handler       *next_ready;
   int                     fd;
   Ecore_Fd_Handler_Flags  flags;
   Eo                     *handler;
   Eo                     *loop;
   Efl_Loop_Data          *loop_data;
   Ecore_Fd_Cb             func;
   void                   *data;
   Ecore_Fd_Cb             buf_func;
   void                   *buf_data;
   Ecore_Fd_Prep_Cb        prep_func;
   void                   *prep_data;
   int                     references;
#if defined(USE_G_MAIN_LOOP)
   GPollFD                 gfd;
#endif
#ifdef HAVE_LIBUV
   uv_poll_t               uv_handle;
#endif
   Eina_Bool               read_active : 1;
   Eina_Bool               write_active : 1;
   Eina_Bool               error_active : 1;
   Eina_Bool               delete_me : 1;
   Eina_Bool               file : 1;
   Eina_Bool               legacy : 1;
};
GENERIC_ALLOC_SIZE_DECLARE(Ecore_Fd_Handler);

#ifdef _WIN32
struct _Ecore_Win32_Handler
{
   EINA_INLIST;
   ECORE_MAGIC;
   HANDLE                  h;
   Eo                     *handler;
   Eo                     *loop;
   Efl_Loop_Data          *loop_data;
   Ecore_Win32_Handle_Cb   func;
   void                   *data;
   int                     references;
   Eina_Bool               delete_me : 1;
};
GENERIC_ALLOC_SIZE_DECLARE(Ecore_Win32_Handler);
#endif

#if !defined(USE_G_MAIN_LOOP) && !defined(HAVE_LIBUV)
static int  _ecore_main_select(Eo *obj, Efl_Loop_Data *pd, double timeout);
#endif
static void _ecore_main_prepare_handlers(Eo *obj, Efl_Loop_Data *pd);
static void _ecore_main_fd_handlers_cleanup(Eo *obj, Efl_Loop_Data *pd);
#ifndef _WIN32
# ifndef USE_G_MAIN_LOOP
static void _ecore_main_fd_handlers_bads_rem(Eo *obj, Efl_Loop_Data *pd);
# endif
#endif
static void _ecore_main_fd_handlers_call(Eo *obj, Efl_Loop_Data *pd);
static int  _ecore_main_fd_handlers_buf_call(Eo *obj, Efl_Loop_Data *pd);
#ifndef USE_G_MAIN_LOOP
static void _ecore_main_loop_iterate_internal(Eo *obj, Efl_Loop_Data *pd, int once_only);
#endif

#ifdef _WIN32
static int _ecore_main_win32_select(int             nfds,
                                    fd_set         *readfds,
                                    fd_set         *writefds,
                                    fd_set         *exceptfds,
                                    struct timeval *timeout);
static void _ecore_main_win32_handlers_cleanup(Eo *obj, Efl_Loop_Data *pd);
#endif

static void _ecore_main_loop_setup(Eo *obj, Efl_Loop_Data *pd);
static void _ecore_main_loop_clear(Eo *obj, Efl_Loop_Data *pd);

// for legacy mainloop only and not other loops
int in_main_loop = 0;
#ifndef USE_G_MAIN_LOOP
static double t1 = 0.0;
static double t2 = 0.0;
#endif

#ifdef _WIN32
Ecore_Select_Function        main_loop_select    = _ecore_main_win32_select;
static Ecore_Select_Function general_loop_select = _ecore_main_win32_select;
#else
# include <sys/select.h>
Ecore_Select_Function        main_loop_select    = select;
static Ecore_Select_Function general_loop_select = select;
#endif

#ifdef USE_G_MAIN_LOOP
static GPollFD    ecore_epoll_fd;
static GPollFD    ecore_timer_fd;
static GSource   *ecore_glib_source;
static guint      ecore_glib_source_id;
static GMainLoop *ecore_main_loop;
static gboolean   ecore_idling;
static gboolean   _ecore_glib_idle_enterer_called;
static gboolean   ecore_fds_ready;
#endif

#ifdef EFL_EXTRA_SANITY_CHECKS
static inline void
_ecore_fd_valid(Eo *obj EINA_UNUSED, Efl_Loop_Data *pd EINA_UNUSED)
{
# ifdef HAVE_SYS_EPOLL_H
   if ((pd->epoll_fd >= 0) &&
       (fcntl(pd->epoll_fd, F_GETFD) < 0))
     {
        ERR("arghhh you caught me! report a backtrace to edevel!");
#  ifdef HAVE_PAUSE
        pause();
#  else
        sleep(60);
#  endif
     }
# endif
}
#endif

static inline void
_ecore_try_add_to_call_list(Eo *obj EINA_UNUSED, Efl_Loop_Data *pd, Ecore_Fd_Handler *fdh)
{
   // check if this fdh is already in the list
   if (fdh->next_ready)
     {
        DBG("next_ready");
        return;
     }
   if (fdh->read_active || fdh->write_active || fdh->error_active)
     {
        DBG("added");
        // make sure next_ready is non-null by pointing to ourselves
        // use that to indicate this fdh is in the ready list
        // insert at the head of the list to avoid trouble
        fdh->next_ready = pd->fd_handlers_to_call ? pd->fd_handlers_to_call : fdh;
        pd->fd_handlers_to_call = fdh;
     }
}

static inline void
_throttle_do(Efl_Loop_Data *pd)
{
   if (pd->throttle == 0) return
   eina_evlog("+throttle", NULL, 0.0, NULL);
   usleep(pd->throttle);
   eina_evlog("-throttle", NULL, 0.0, NULL);
}

#ifdef HAVE_SYS_EPOLL_H
static inline int
_ecore_get_epoll_fd(Eo *obj, Efl_Loop_Data *pd)
{
   if (pd->epoll_pid && (pd->epoll_pid != getpid())) // forked!
     _ecore_main_loop_clear(obj, pd);
   if ((pd->epoll_pid == 0) && (pd->epoll_fd < 0))
     _ecore_main_loop_setup(obj, pd);
   return pd->epoll_fd;
}

static inline int
_ecore_epoll_add(int   efd,
                 int   fd,
                 int   events,
                 void *ptr)
{
   struct epoll_event ev;

   memset(&ev, 0, sizeof (ev));
   ev.events = events;
   ev.data.ptr = ptr;
   DBG("adding poll on %d %08x", fd, events);
   return epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ev);
}

static inline int
_ecore_poll_events_from_fdh(Ecore_Fd_Handler *fdh)
{
   int events = 0;
   if (fdh->flags & ECORE_FD_READ)  events |= EPOLLIN | EPOLLHUP;
   if (fdh->flags & ECORE_FD_WRITE) events |= EPOLLOUT | EPOLLHUP;
   if (fdh->flags & ECORE_FD_ERROR) events |= EPOLLERR | EPOLLPRI | EPOLLHUP;
   return events;
}
#endif

#ifdef USE_G_MAIN_LOOP
static inline int
_gfd_events_from_fdh(Ecore_Fd_Handler *fdh)
{
   int events = 0;
   if (fdh->flags & ECORE_FD_READ)  events |= G_IO_IN | G_IO_HUP;
   if (fdh->flags & ECORE_FD_WRITE) events |= G_IO_OUT | G_IO_HUP;
   if (fdh->flags & ECORE_FD_ERROR) events |= G_IO_ERR | G_IO_HUP;
   return events;
}
#endif

#ifdef HAVE_LIBUV
static void
_ecore_main_uv_poll_cb(uv_poll_t *handle, int status, int events)
{
   Eo *obj = ML_OBJ;
   Efl_Loop_Data *pd = ML_DAT;

   DBG("_ecore_main_uv_poll_cb %p status %d events %d",
       (void *)handle->data, status, events);
   Ecore_Fd_Handler *fdh = handle->data;

   if (_ecore_main_uv_idling)
     {
        DBG("not IDLE anymore");
        _ecore_main_uv_idling = EINA_FALSE;
        efl_event_callback_call(obj, EFL_LOOP_EVENT_IDLE_EXIT, NULL);
        _ecore_animator_run_reset();
     }

  if (status)               fdh->error_active = EINA_TRUE;
  if (events & UV_READABLE) fdh->read_active  = EINA_TRUE;
  if (events & UV_WRITABLE) fdh->write_active = EINA_TRUE;

  if (events & UV_DISCONNECT)
     {
        fdh->read_active  = EINA_TRUE;
        fdh->write_active = EINA_TRUE;
        fdh->error_active = EINA_TRUE;
     }
  _ecore_try_add_to_call_list(obj, pd, fdh);

  _ecore_main_fd_handlers_call(obj, pd);
  if (pd->fd_handlers_with_buffer) _ecore_main_fd_handlers_buf_call(obj, pd);
  _ecore_signal_received_process(obj, pd);
  efl_loop_message_process(obj);
  _ecore_main_fd_handlers_cleanup(obj, pd);
  _efl_loop_timer_expired_timers_call(obj, pd, pd->loop_time);
}

static int
_ecore_main_uv_events_from_fdh(Ecore_Fd_Handler *fdh)
{
   int events = 0;
   if (fdh->flags & ECORE_FD_READ)  events |= UV_READABLE;
   if (fdh->flags & ECORE_FD_WRITE) events |= UV_WRITABLE;
   DBG("events is %d", (int)events);
   return events;
}
#endif

static inline int
_ecore_main_fdh_poll_add(Efl_Loop_Data *pd EINA_UNUSED, Ecore_Fd_Handler *fdh)
{
   DBG("_ecore_main_fdh_poll_add");
   int r = 0;

#ifdef HAVE_SYS_EPOLL_H
# ifdef HAVE_LIBUV
   if (!_dl_uv_run)
# endif
     {
        if ((!fdh->file) && (pd->epoll_fd >= 0))
          r = _ecore_epoll_add(_ecore_get_epoll_fd(fdh->loop, pd), fdh->fd,
                               _ecore_poll_events_from_fdh(fdh), fdh);
     }
# ifdef HAVE_LIBUV
   else
# endif
#endif
     {
#ifdef HAVE_LIBUV
        if (!fdh->file)
          {
             DBG("_ecore_main_fdh_poll_add libuv socket %p", fdh);
             fdh->uv_handle.data = fdh;
             DBG("_ecore_main_fdh_poll_add2 %p", fdh);
             _dl_uv_poll_init_socket(_dl_uv_default_loop(),
                                     &fdh->uv_handle, fdh->fd);
             DBG("_ecore_main_fdh_poll_add3 %p", fdh->uv_handle.data);
             _dl_uv_poll_start(&fdh->uv_handle,
                               _ecore_main_uv_events_from_fdh(fdh)
                               , _ecore_main_uv_poll_cb);
             DBG("_ecore_main_fdh_poll_add libuv DONE");
          }
        else
          {
             DBG("_ecore_main_fdh_poll_add libuv file");
             fdh->uv_handle.data = fdh;
             DBG("_ecore_main_fdh_poll_add2 %p", fdh);
             _dl_uv_poll_init(_dl_uv_default_loop(),
                              &fdh->uv_handle, fdh->fd);
             DBG("_ecore_main_fdh_poll_add3 %p", fdh->uv_handle.data);
             _dl_uv_poll_start(&fdh->uv_handle,
                               _ecore_main_uv_events_from_fdh(fdh)
                               , _ecore_main_uv_poll_cb);
             DBG("_ecore_main_fdh_poll_add libuv DONE");
          }
#elif defined(USE_G_MAIN_LOOP)
        fdh->gfd.fd = fdh->fd;
        fdh->gfd.events = _gfd_events_from_fdh(fdh);
        fdh->gfd.revents = 0;
        DBG("adding gpoll on %d %08x", fdh->fd, fdh->gfd.events);
        g_source_add_poll(ecore_glib_source, &fdh->gfd);
#endif
     }
   return r;
}

static inline void
_ecore_main_fdh_poll_del(Efl_Loop_Data *pd, Ecore_Fd_Handler *fdh)
{
#ifdef HAVE_SYS_EPOLL_H
# ifdef HAVE_LIBUV
   if (!_dl_uv_run)
# endif
     {
        if (!pd)
          {
             WRN("Efl_Loop_Data is NULL!");
             return;
          }

        if ((!fdh->file) && (pd->epoll_fd >= 0))
          {
             struct epoll_event ev;
             int efd = _ecore_get_epoll_fd(fdh->loop, pd);

             memset(&ev, 0, sizeof (ev));
             DBG("removing poll on %d", fdh->fd);
             // could get an EBADF if somebody closed the FD before removing
             if ((epoll_ctl(efd, EPOLL_CTL_DEL, fdh->fd, &ev) < 0))
               {
                  if (errno == EBADF)
                    {
                       WRN("fd %d closed, can't remove from epoll - reinit!",
                           fdh->fd);
                       _ecore_main_loop_clear(fdh->loop, pd);
                       _ecore_main_loop_setup(fdh->loop, pd);
                    }
                  else ERR("Failed to delete epoll fd %d! (errno=%d)",
                           fdh->fd, errno);
               }
          }
     }
# ifdef HAVE_LIBUV
   else
# endif
#endif
     {
#ifdef HAVE_LIBUV
        DBG("_ecore_main_fdh_poll_del libuv %p", fdh);
        uv_handle_t* h = (uv_handle_t*)&fdh->uv_handle;
        _dl_uv_close(h, 0);
        DBG("_ecore_main_fdh_poll_del libuv DONE");
#elif USE_G_MAIN_LOOP
        fdh->gfd.fd = fdh->fd;
        fdh->gfd.events = _gfd_events_from_fdh(fdh);
        fdh->gfd.revents = 0;
        DBG("removing gpoll on %d %08x", fdh->fd, fdh->gfd.events);
        g_source_remove_poll(ecore_glib_source, &fdh->gfd);
#endif
     }
}

static inline int
_ecore_main_fdh_poll_modify(Efl_Loop_Data *pd EINA_UNUSED, Ecore_Fd_Handler *fdh)
{
   DBG("_ecore_main_fdh_poll_modify %p", fdh);
   int r = 0;
#ifdef HAVE_SYS_EPOLL_H
# ifdef HAVE_LIBUV
   if (!_dl_uv_run)
# endif
     {
        if ((!fdh->file) && (pd->epoll_fd >= 0))
          {
             struct epoll_event ev;
             int efd = _ecore_get_epoll_fd(fdh->loop, pd);

             memset(&ev, 0, sizeof (ev));
             ev.events = _ecore_poll_events_from_fdh(fdh);
             ev.data.ptr = fdh;
             DBG("modifing epoll on %d to %08x", fdh->fd, ev.events);
             r = epoll_ctl(efd, EPOLL_CTL_MOD, fdh->fd, &ev);
          }
     }
# ifdef HAVE_LIBUV
   else
# endif
#endif
     {
#ifdef HAVE_LIBUV
        _dl_uv_poll_start(&fdh->uv_handle, _ecore_main_uv_events_from_fdh(fdh)
                          , _ecore_main_uv_poll_cb);
#elif defined(USE_G_MAIN_LOOP)
        fdh->gfd.fd = fdh->fd;
        fdh->gfd.events = _gfd_events_from_fdh(fdh);
        fdh->gfd.revents = 0;
        DBG("modifing gpoll on %d to %08x", fdh->fd, fdh->gfd.events);
#endif
     }
   return r;
}

static Eina_Bool
_ecore_main_idlers_exist(Efl_Loop_Data *pd)
{
   return pd->idlers || eina_freeq_ptr_pending(eina_freeq_main_get());
}

static void
_ecore_main_idler_all_call(Eo *loop)
{
   efl_event_callback_call(loop, EFL_LOOP_EVENT_IDLE, NULL);
   // just spin in an idler until the free queue is empty freeing 84 items
   // from the free queue each time.for now this seems like an ok balance
   // between going in and out of a reduce func with mutexes around it
   // vs blocking mainloop for too long. this number is up for discussion
   eina_freeq_reduce(eina_freeq_main_get(), 84);
}

#ifdef HAVE_SYS_EPOLL_H
static inline int
_ecore_main_fdh_epoll_mark_active(Eo *obj, Efl_Loop_Data *pd)
{
   DBG("_ecore_main_fdh_epoll_mark_active");
   struct epoll_event ev[32];
   int i, ret;
   int efd = _ecore_get_epoll_fd(obj, pd);

   memset(&ev, 0, sizeof (ev));
   ret = epoll_wait(efd, ev, sizeof(ev) / sizeof(struct epoll_event), 0);
   if (ret < 0)
     {
        if (errno == EINTR) return -1;
        ERR("epoll_wait failed on fd: %d %s", efd, strerror(errno));
        return -1;
     }

   for (i = 0; i < ret; i++)
     {
        Ecore_Fd_Handler *fdh;

        fdh = ev[i].data.ptr;
        if (!ECORE_MAGIC_CHECK(fdh, ECORE_MAGIC_FD_HANDLER))
          {
             ECORE_MAGIC_FAIL(fdh, ECORE_MAGIC_FD_HANDLER,
                              "_ecore_main_fdh_epoll_mark_active");
             continue;
          }
        if (fdh->delete_me)
          {
             ERR("deleted fd in epoll");
             continue;
          }

        if (ev[i].events & EPOLLIN)  fdh->read_active  = EINA_TRUE;
        if (ev[i].events & EPOLLOUT) fdh->write_active = EINA_TRUE;
        if (ev[i].events & EPOLLERR) fdh->error_active = EINA_TRUE;

        if (ev[i].events & EPOLLHUP)
          {
             fdh->read_active  = EINA_TRUE;
             fdh->write_active = EINA_TRUE;
             fdh->error_active = EINA_TRUE;
          }

        _ecore_try_add_to_call_list(obj, pd, fdh);
     }
   return ret;
}
#endif

#ifdef USE_G_MAIN_LOOP
static inline int
_ecore_main_fdh_glib_mark_active(Eo *obj, Efl_Loop_Data *pd)
{
   Ecore_Fd_Handler *fdh;
   int ret = 0;

   // call the prepare callback for all handlers
   EINA_INLIST_FOREACH(pd->fd_handlers, fdh)
     {
        if (fdh->delete_me) continue;

        if (fdh->gfd.revents & G_IO_IN)  fdh->read_active  = EINA_TRUE;
        if (fdh->gfd.revents & G_IO_OUT) fdh->write_active = EINA_TRUE;
        if (fdh->gfd.revents & G_IO_ERR) fdh->error_active = EINA_TRUE;

        if (fdh->gfd.revents & G_IO_HUP)
          {
             fdh->read_active  = EINA_TRUE;
             fdh->write_active = EINA_TRUE;
             fdh->error_active = EINA_TRUE;
          }

        _ecore_try_add_to_call_list(obj, pd, fdh);

        if (fdh->gfd.revents & (G_IO_IN | G_IO_OUT | G_IO_ERR)) ret++;
     }

   return ret;
}

// like we are about to enter main_loop_select in  _ecore_main_select
static gboolean
_ecore_main_gsource_prepare(GSource *source EINA_UNUSED,
                            gint    *next_time)
{
   Eo *obj = ML_OBJ;
   Efl_Loop_Data *pd = ML_DAT;
   gboolean ready = FALSE;

   in_main_loop++;
   pd->in_loop = in_main_loop;

   if ((!ecore_idling) && (!_ecore_glib_idle_enterer_called))
     {
        pd->loop_time = ecore_time_get();
        _efl_loop_timer_expired_timers_call(obj, pd, pd->loop_time);

        efl_event_callback_call(obj, EFL_LOOP_EVENT_IDLE_ENTER, NULL);
        _ecore_throttle();
        _throttle_do(pd);
        _ecore_glib_idle_enterer_called = FALSE;

        if (pd->fd_handlers_with_buffer)
          _ecore_main_fd_handlers_buf_call(obj, pd);
     }

   _ecore_signal_received_process(obj, pd);

   // don't check fds if somebody quit
   if (g_main_loop_is_running(ecore_main_loop))
     {
        // only set idling state in dispatch
        if (ecore_idling && (!_ecore_main_idlers_exist(pd)) &&
            (!pd->message_queue))
          {
             if (_efl_loop_timers_exists(obj, pd))
               {
                  int r = -1;
                  double t = _efl_loop_timer_next_get(obj, pd);

                  if ((pd->timer_fd >= 0) && (t > 0.0))
                    {
                       struct itimerspec ts;

                       ts.it_interval.tv_sec = 0;
                       ts.it_interval.tv_nsec = 0;
                       ts.it_value.tv_sec = t;
                       ts.it_value.tv_nsec = fmod(t * NS_PER_SEC, NS_PER_SEC);

                       // timerfd cannot sleep for 0 time
                       if (ts.it_value.tv_sec || ts.it_value.tv_nsec)
                         {
                            r = timerfd_settime(pd->timer_fd, 0, &ts, NULL);
                            if (r < 0)
                              {
                                 ERR("timer set returned %d (errno=%d)",
                                     r, errno);
                                 close(pd->timer_fd);
                                 pd->timer_fd = -1;
                              }
                            else INF("sleeping for %ld s %06ldus",
                                     ts.it_value.tv_sec,
                                     ts.it_value.tv_nsec / 1000);
                         }
                    }
                  if (r == -1)
                    {
                       *next_time = ceil(t * 1000.0);
                       if (t == 0.0) ready = TRUE;
                    }
               }
             else *next_time = -1;
          }
        else
          {
             *next_time = 0;
             if (pd->message_queue) ready = TRUE;
          }

        if (pd->fd_handlers_with_prep) _ecore_main_prepare_handlers(obj, pd);
     }
   else ready = TRUE;

   in_main_loop--;
   pd->in_loop = in_main_loop;
   DBG("leave, timeout = %d", *next_time);

   // ready if we're not running (about to quit)
   return ready;
}

static gboolean
_ecore_main_gsource_check(GSource *source EINA_UNUSED)
{
   Eo *obj = ML_OBJ;
   Efl_Loop_Data *pd = ML_DAT;
   gboolean ret = FALSE;

   in_main_loop++;
   pd->in_loop = in_main_loop;
   // check if old timers expired
   if (ecore_idling && (!_ecore_main_idlers_exist(pd)) &&
       (!pd->message_queue))
     {
        if (pd->timer_fd >= 0)
          {
             uint64_t count = 0;
             int r = read(pd->timer_fd, &count, sizeof count);
             if ((r == -1) && (errno == EAGAIN))
               {
               }
             else if (r == sizeof count) ret = TRUE;
             else
               {
                  // unexpected things happened... fail back to old way
                  ERR("timer read returned %d (errno=%d)", r, errno);
                  close(pd->timer_fd);
                  pd->timer_fd = -1;
               }
          }
     }
   else ret = TRUE;

   // check if fds are ready
#ifdef HAVE_SYS_EPOLL_H
   if (pd->epoll_fd >= 0)
     ecore_fds_ready = (_ecore_main_fdh_epoll_mark_active(obj, pd) > 0);
   else
#endif
     ecore_fds_ready = (_ecore_main_fdh_glib_mark_active(obj, pd) > 0);
   _ecore_main_fd_handlers_cleanup(obj, pd);
   if (ecore_fds_ready) ret = TRUE;

   // check timers after updating loop time
   if (!ret && _efl_loop_timers_exists(obj, pd))
     ret = (0.0 == _efl_loop_timer_next_get(obj, pd));

   in_main_loop--;
   pd->in_loop = in_main_loop;
   return ret;
}

// like we just came out of main_loop_select in  _ecore_main_select
static gboolean
_ecore_main_gsource_dispatch(GSource    *source EINA_UNUSED,
                             GSourceFunc callback EINA_UNUSED,
                             gpointer    user_data EINA_UNUSED)
{
   Eo *obj = ML_OBJ;
   Efl_Loop_Data *pd = ML_DAT;
   gboolean events_ready, timers_ready, idlers_ready;
   double next_time;

   pd->loop_time = ecore_time_get();
   _efl_loop_timer_enable_new(obj, pd);
   next_time = _efl_loop_timer_next_get(obj, pd);

   events_ready = pd->message_queue ? 1 : 0;
   timers_ready = _efl_loop_timers_exists(obj, pd) && (0.0 == next_time);
   idlers_ready = _ecore_main_idlers_exist(pd);

   in_main_loop++;
   pd->in_loop = in_main_loop;
   DBG("enter idling=%d fds=%d events=%d timers=%d (next=%.2f) idlers=%d",
       ecore_idling, ecore_fds_ready, events_ready,
       timers_ready, next_time, idlers_ready);

   if (ecore_idling && events_ready)
     {
        _ecore_animator_run_reset();
        efl_event_callback_call(obj, EFL_LOOP_EVENT_IDLE_EXIT, NULL);
        ecore_idling = 0;
     }
   else if (!ecore_idling && !events_ready) ecore_idling = 1;

   if (ecore_idling)
     {
        _ecore_main_idler_all_call(obj);

        events_ready = pd->message_queue ? 1 : 0;

        if (ecore_fds_ready || events_ready || timers_ready)
          {
             _ecore_animator_run_reset();
             efl_event_callback_call(obj, EFL_LOOP_EVENT_IDLE_EXIT, NULL);
             ecore_idling = 0;
          }
     }

   // process events
   if (!ecore_idling)
     {
        _ecore_main_fd_handlers_call(obj, pd);
        if (pd->fd_handlers_with_buffer)
          _ecore_main_fd_handlers_buf_call(obj, pd);
        _ecore_signal_received_process(obj, pd);
        efl_loop_message_process(obj);
        _ecore_main_fd_handlers_cleanup(obj, pd);

        _efl_loop_timer_expired_timers_call(obj, pd, pd->loop_time);

        efl_event_callback_call(obj, EFL_LOOP_EVENT_IDLE_ENTER, NULL);
        _ecore_throttle();
        _throttle_do(pd);
        _ecore_glib_idle_enterer_called = TRUE;

        if (pd->fd_handlers_with_buffer)
          _ecore_main_fd_handlers_buf_call(obj, pd);
     }

   in_main_loop--;
   pd->in_loop = in_main_loop;

   return TRUE; // what should be returned here?
}

static void
_ecore_main_gsource_finalize(GSource *source EINA_UNUSED)
{
}

static GSourceFuncs ecore_gsource_funcs =
{
   .prepare  = _ecore_main_gsource_prepare,
   .check    = _ecore_main_gsource_check,
   .dispatch = _ecore_main_gsource_dispatch,
   .finalize = _ecore_main_gsource_finalize,
};
#endif

#ifdef HAVE_LIBUV
static inline void _ecore_main_loop_uv_check(uv_check_t *handle);
static void _ecore_main_loop_uv_prepare(uv_prepare_t *handle);

static void
_ecore_main_loop_timer_run(uv_timer_t *timer EINA_UNUSED)
{
   Eo *obj = ML_OBJ;
   Efl_Loop_Data *pd = ML_DAT;

   if (_ecore_main_uv_idling)
     {
        _ecore_main_uv_idling = EINA_FALSE;
        efl_event_callback_call(obj, EFL_LOOP_EVENT_IDLE_EXIT, NULL);
        _ecore_animator_run_reset();
     }
   pd->loop_time = ecore_time_get();
   _ecore_main_loop_uv_check(NULL);
   _ecore_main_loop_uv_prepare(NULL);
}

static inline void
_ecore_main_loop_uv_check(uv_check_t *handle EINA_UNUSED)
{
   Eo *obj = ML_OBJ;
   Efl_Loop_Data *pd = ML_DAT;

   DBG("_ecore_main_loop_uv_check idling? %d", (int)_ecore_main_uv_idling);
   in_main_loop++;
   pd->in_loop = in_main_loop;

   if (pd->do_quit) goto quit;

   do
     {
        _ecore_main_fd_handlers_call(obj, pd);
        if (pd->fd_handlers_with_buffer)
          _ecore_main_fd_handlers_buf_call(obj, pd);
        _ecore_signal_received_process(obj, pd);
        efl_loop_message_process(obj);
        _ecore_main_fd_handlers_cleanup(obj, pd);
        _efl_loop_timer_expired_timers_call(obj, pd, pd->loop_time);
     }
   while (pd->fd_handlers_to_call);
quit:
   in_main_loop--;
   pd->in_loop = in_main_loop;
}
#endif

static void
_ecore_main_loop_setup(Eo *obj, Efl_Loop_Data *pd)
{
   // Please note that this function is being also called in case of a bad
   // fd to reset the main loop.
#ifdef HAVE_SYS_EPOLL_H
   pd->epoll_fd = epoll_create(1);
   if (pd->epoll_fd < 0) WRN("Failed to create epoll fd!");
   else
     {
        eina_file_close_on_exec(pd->epoll_fd, EINA_TRUE);

        pd->epoll_pid = getpid();

        // add polls on all our file descriptors
        Ecore_Fd_Handler *fdh;
        EINA_INLIST_FOREACH(pd->fd_handlers, fdh)
          {
             if (fdh->delete_me) continue;
             _ecore_epoll_add(pd->epoll_fd, fdh->fd,
                              _ecore_poll_events_from_fdh(fdh), fdh);
             _ecore_main_fdh_poll_add(pd, fdh);
          }
     }
#endif

   if (obj == ML_OBJ)
     {
#ifdef HAVE_LIBUV
        // XXX: FIXME: the below uv init should not assert but gracefully
        // fail with errors
        DBG("loading lib uv");
# ifdef HAVE_NODEJS
        void *lib = dlopen(NULL, RTLD_LAZY);
# else
        void *lib = dlopen("libuv.so.1", RTLD_GLOBAL | RTLD_LAZY);
# endif

        if (lib && dlsym(lib, "uv_run"))
          {
             DBG("loaded lib uv");
             _dl_uv_run = dlsym(lib, "uv_run");
             assert(!!_dl_uv_run);
             _dl_uv_stop = dlsym(lib, "uv_stop");
             assert(!!_dl_uv_stop);
             _dl_uv_default_loop = dlsym(lib, "uv_default_loop");
             assert(!!_dl_uv_default_loop);
             _dl_uv_poll_init_socket = dlsym(lib, "uv_poll_init_socket");
             assert(!!_dl_uv_poll_init_socket);
             _dl_uv_poll_init = dlsym(lib, "uv_poll_init");
             assert(!!_dl_uv_poll_init);
             _dl_uv_poll_start = dlsym(lib, "uv_poll_start");
             assert(!!_dl_uv_poll_start);
             _dl_uv_poll_stop = dlsym(lib, "uv_poll_stop");
             assert(!!_dl_uv_poll_stop);
             _dl_uv_timer_init = dlsym(lib, "uv_timer_init");
             assert(!!_dl_uv_timer_init);
             _dl_uv_timer_start = dlsym(lib, "uv_timer_start");
             assert(!!_dl_uv_timer_start);
             _dl_uv_timer_stop = dlsym(lib, "uv_timer_stop");
             assert(!!_dl_uv_timer_stop);
             _dl_uv_prepare_init = dlsym(lib, "uv_prepare_init");
             assert(!!_dl_uv_prepare_init);
             _dl_uv_prepare_start = dlsym(lib, "uv_prepare_start");
             assert(!!_dl_uv_prepare_start);
             _dl_uv_prepare_stop = dlsym(lib, "uv_prepare_stop");
             assert(!!_dl_uv_prepare_stop);
             _dl_uv_check_init = dlsym(lib, "uv_check_init");
             assert(!!_dl_uv_check_init);
             _dl_uv_check_start = dlsym(lib, "uv_check_start");
             assert(!!_dl_uv_check_start);
             _dl_uv_check_stop = dlsym(lib, "uv_check_stop");
             assert(!!_dl_uv_check_stop);
             _dl_uv_close = dlsym(lib, "uv_close");
             assert(!!_dl_uv_close);
             _dl_uv_loop_alive = dlsym(lib, "uv_loop_alive");
             assert(!!_dl_uv_loop_alive);

             //dlclose(lib);

             DBG("_dl_uv_prepare_init");
             _dl_uv_prepare_init(_dl_uv_default_loop(), &_ecore_main_uv_prepare);
             DBG("_dl_uv_prepare_start");
             _dl_uv_prepare_start(&_ecore_main_uv_prepare, &_ecore_main_loop_uv_prepare);
             DBG("_dl_uv_prepare_started");

             DBG("_dl_uv_check_init");
             _dl_uv_check_init(_dl_uv_default_loop(), &_ecore_main_uv_check);
             DBG("_dl_uv_check_start");
             _dl_uv_check_start(&_ecore_main_uv_check, &_ecore_main_loop_uv_check);
             DBG("_dl_uv_check_started");

             _dl_uv_timer_init(_dl_uv_default_loop(),  &_ecore_main_uv_handle_timers);
          }
        // else
        //   DBG("did not load uv");
        DBG("loaded dlsyms uv");
#endif

        // setup for the g_main_loop only integration
#ifdef USE_G_MAIN_LOOP
        ecore_glib_source = g_source_new(&ecore_gsource_funcs,
                                         sizeof(GSource));
        if (!ecore_glib_source) CRI("Failed to create glib source for epoll!");
        else
          {
             g_source_set_priority(ecore_glib_source,
                                   G_PRIORITY_HIGH_IDLE + 20);
# ifdef HAVE_SYS_EPOLL_H
             if (pd->epoll_fd >= 0)
               {
                  // epoll multiplexes fds into the g_main_loop
                  ecore_epoll_fd.fd = pd->epoll_fd;
                  ecore_epoll_fd.events = G_IO_IN;
                  ecore_epoll_fd.revents = 0;
                  g_source_add_poll(ecore_glib_source, &ecore_epoll_fd);
               }
# endif
             // timerfd gives us better than millisecond accuracy
             pd->timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
             if (pd->timer_fd < 0) WRN("failed to create timer fd!");
             else
               {
                  eina_file_close_on_exec(pd->timer_fd, EINA_TRUE);
                  ecore_timer_fd.fd = pd->timer_fd;
                  ecore_timer_fd.events = G_IO_IN;
                  ecore_timer_fd.revents = 0;
                  g_source_add_poll(ecore_glib_source, &ecore_timer_fd);
               }

             ecore_glib_source_id = g_source_attach(ecore_glib_source, NULL);
             if (ecore_glib_source_id <= 0)
               CRI("Failed to attach glib source to default context");
          }
#endif
     }
   _ecore_main_timechanges_start(obj);
}

static void
_ecore_main_loop_clear(Eo *obj, Efl_Loop_Data *pd)
{
   if (!pd) return;
   // Please note that _ecore_main_loop_shutdown is called in cycle to
   // restart the main loop in case of a bad fd
   if (obj == ML_OBJ)
     {
        _ecore_main_timechanges_stop(obj);
#ifdef USE_G_MAIN_LOOP
        if (ecore_glib_source)
          {
             g_source_destroy(ecore_glib_source);
             ecore_glib_source = NULL;
          }
#endif
#ifdef HAVE_LIBUV
        if (_dl_uv_run)
          {
             DBG("_ecore_main_loop_shutdown");
             _dl_uv_timer_stop(&_ecore_main_uv_handle_timers);
             _dl_uv_close((uv_handle_t*)&_ecore_main_uv_handle_timers, 0);
          }
#endif
     }
# ifdef HAVE_SYS_EPOLL_H
   if (pd->epoll_fd >= 0)
     {
        close(pd->epoll_fd);
        pd->epoll_fd = -1;
     }
#endif
   if (pd->timer_fd >= 0)
     {
        close(pd->timer_fd);
        pd->timer_fd = -1;
     }
}

void
_ecore_main_loop_init(void)
{
   DBG("_ecore_main_loop_init");
   if (!efl_main_loop_get()) ERR("Cannot create main loop object");
   _ecore_main_loop_setup(ML_OBJ, ML_DAT);
}

void
_ecore_main_loop_shutdown(void)
{
   if (!ML_OBJ) return;
   _ecore_main_loop_clear(ML_OBJ, ML_DAT);
// XXX: this seemingly closes fd's it shouldn't.... :( fd 0?
   efl_replace(&ML_OBJ, NULL);
   ML_DAT = NULL;
}

void
_ecore_main_loop_iterate(Eo *obj, Efl_Loop_Data *pd)
{
   if (obj == ML_OBJ)
     {
#ifdef HAVE_LIBUV
        if (!_dl_uv_run)
          {
#endif
#ifndef USE_G_MAIN_LOOP
             pd->loop_time = ecore_time_get();
             _ecore_main_loop_iterate_internal(obj, pd, 1);
#else
             g_main_context_iteration(NULL, 0);
#endif
#ifdef HAVE_LIBUV
          }
        else
          _dl_uv_run(_dl_uv_default_loop(), UV_RUN_ONCE | UV_RUN_NOWAIT);
#endif
     }
   else
     {
#ifndef USE_G_MAIN_LOOP
        pd->loop_time = ecore_time_get();
        _ecore_main_loop_iterate_internal(obj, pd, 1);
#else
             g_main_context_iteration(NULL, 0);
#endif
     }
}

int
_ecore_main_loop_iterate_may_block(Eo *obj, Efl_Loop_Data *pd, int may_block)
{
   if (obj == ML_OBJ)
     {
#ifdef HAVE_LIBUV
        if (!_dl_uv_run)
          {
#endif
#ifndef USE_G_MAIN_LOOP
             in_main_loop++;
             pd->in_loop = in_main_loop;
             pd->loop_time = ecore_time_get();
             _ecore_main_loop_iterate_internal(obj, pd, !may_block);
             in_main_loop--;
             pd->in_loop = in_main_loop;
             return pd->message_queue ? 1 : 0;
#else
             return g_main_context_iteration(NULL, may_block);
#endif
#ifdef HAVE_LIBUV
          }
        else
          _dl_uv_run(_dl_uv_default_loop(),
                     may_block ? (UV_RUN_ONCE | UV_RUN_NOWAIT) : UV_RUN_ONCE);
#endif
     }
   else
     {
#ifndef USE_G_MAIN_LOOP
        pd->in_loop++;
        pd->loop_time = ecore_time_get();
        _ecore_main_loop_iterate_internal(obj, pd, !may_block);
        pd->in_loop--;
        return pd->message_queue ? 1 : 0;
#else
        return g_main_context_iteration(NULL, may_block);
#endif
     }
   return 0;
}

void
_ecore_main_loop_begin(Eo *obj, Efl_Loop_Data *pd)
{
   if (obj == ML_OBJ)
     {
#ifdef HAVE_SYSTEMD
        sd_notify(0, "READY=1");
#endif
#ifdef HAVE_LIBUV
        if (!_dl_uv_run)
          {
#endif
#ifndef USE_G_MAIN_LOOP
             in_main_loop++;
             pd->in_loop = in_main_loop;
             pd->loop_time = ecore_time_get();
             while (!pd->do_quit)
               _ecore_main_loop_iterate_internal(obj, pd, 0);
             pd->do_quit = 0;
             in_main_loop--;
             pd->in_loop = in_main_loop;
#else
             if (!pd->do_quit)
               {
                  if (!ecore_main_loop)
                    ecore_main_loop = g_main_loop_new(NULL, FALSE);
                  g_main_loop_run(ecore_main_loop);
               }
             pd->do_quit = 0;
#endif
#ifdef HAVE_LIBUV
          }
        else
          {
             DBG("uv_run");
             in_main_loop++;
             pd->in_loop = in_main_loop;
             pd->loop_time = ecore_time_get();
             while (!pd->do_quit)
               _dl_uv_run(_dl_uv_default_loop(), UV_RUN_DEFAULT);
             in_main_loop--;
             pd->in_loop = in_main_loop;
             pd->do_quit = 0;
             DBG("quit");
          }
#endif
     }
   else
     {
#ifndef USE_G_MAIN_LOOP
        pd->in_loop++;
        pd->loop_time = ecore_time_get();
        while (!pd->do_quit)
          _ecore_main_loop_iterate_internal(obj, pd, 0);
        pd->do_quit = 0;
        pd->in_loop--;
#else
        if (!pd->do_quit)
          {
             if (!ecore_main_loop)
               ecore_main_loop = g_main_loop_new(NULL, 1);
             g_main_loop_run(ecore_main_loop);
          }
        pd->do_quit = 0;
#endif
     }
}

void
_ecore_main_loop_quit(Eo *obj, Efl_Loop_Data *pd)
{
   pd->do_quit = 1;
   if (obj != ML_OBJ) return;
#ifdef USE_G_MAIN_LOOP
   if (ecore_main_loop) g_main_loop_quit(ecore_main_loop);
#elif defined(HAVE_LIBUV)
   if (_dl_uv_run) _dl_uv_stop(_dl_uv_default_loop());
#endif
}

EAPI void
ecore_main_loop_iterate(void)
{
   EINA_MAIN_LOOP_CHECK_RETURN;
   efl_loop_iterate(ML_OBJ);
}

EAPI int
ecore_main_loop_iterate_may_block(int may_block)
{
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(0);
   return efl_loop_iterate_may_block(ML_OBJ, may_block);
}

EAPI void
ecore_main_loop_begin(void)
{
   DBG("ecore_main_loop_begin");
   EINA_MAIN_LOOP_CHECK_RETURN;
   eina_evlog("+mainloop", NULL, 0.0, NULL);
   efl_loop_begin(ML_OBJ);
   eina_evlog("-mainloop", NULL, 0.0, NULL);
}

EAPI void
ecore_main_loop_quit(void)
{
   Eina_Value v = EINA_VALUE_EMPTY;

   eina_value_setup(&v, EINA_VALUE_TYPE_INT);
   eina_value_set(&v, 0);
   EINA_MAIN_LOOP_CHECK_RETURN;
   efl_loop_quit(ML_OBJ, v);
}

EAPI int
ecore_main_loop_nested_get(void)
{
   return in_main_loop;
}

EAPI Eina_Bool
ecore_main_loop_animator_ticked_get(void)
{
   DBG("ecore_main_loop_animator_ticked_get");
   return _ecore_animator_run_get();
}

EAPI void
ecore_main_loop_select_func_set(Ecore_Select_Function func)
{
   EINA_MAIN_LOOP_CHECK_RETURN;
   main_loop_select = func;
}

EAPI Ecore_Select_Function
ecore_main_loop_select_func_get(void)
{
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
   return main_loop_select;
}

Ecore_Fd_Handler *
_ecore_main_fd_handler_add(Eo                    *obj,
                           Efl_Loop_Data         *pd,
                           Eo                    *handler,
                           int                    fd,
                           Ecore_Fd_Handler_Flags flags,
                           Ecore_Fd_Cb            func,
                           const void            *data,
                           Ecore_Fd_Cb            buf_func,
                           const void            *buf_data,
                           Eina_Bool              is_file)
{
   DBG("_ecore_main_fd_handler_add");
   Ecore_Fd_Handler *fdh = NULL;

   if ((fd < 0) || (flags == 0) || (!func)) return NULL;

   fdh = ecore_fd_handler_calloc(1);
   if (!fdh) return NULL;
   ECORE_MAGIC_SET(fdh, ECORE_MAGIC_FD_HANDLER);
   fdh->loop = obj;
   fdh->loop_data = pd;
   fdh->handler = handler;
   fdh->fd = fd;
   fdh->flags = flags;
   fdh->file = is_file;
   if (_ecore_main_fdh_poll_add(pd, fdh) < 0)
     {
        int err = errno;
        ERR("Failed to add poll on fd %d (errno = %d: %s)!",
            fd, err, strerror(err));
        ecore_fd_handler_mp_free(fdh);
        return NULL;
     }
   fdh->func = func;
   fdh->data = (void *)data;
   fdh->buf_func = buf_func;
   if (buf_func)
     pd->fd_handlers_with_buffer = eina_list_append
       (pd->fd_handlers_with_buffer, fdh);
   fdh->buf_data = (void *)buf_data;
   if (is_file)
     pd->file_fd_handlers = eina_list_append
       (pd->file_fd_handlers, fdh);
   pd->fd_handlers = (Ecore_Fd_Handler *)
     eina_inlist_append(EINA_INLIST_GET(pd->fd_handlers),
                        EINA_INLIST_GET(fdh));
   return fdh;
}

void *
_ecore_main_fd_handler_del(Eo *obj EINA_UNUSED,
                           Efl_Loop_Data *pd,
                           Ecore_Fd_Handler *fd_handler)
{
   void *r = fd_handler->data;

   DBG("_ecore_main_fd_handler_del %p", fd_handler);
   if (fd_handler->delete_me)
     {
        ERR("fdh %p deleted twice", fd_handler);
        return NULL;
     }

   fd_handler->handler = NULL;
   fd_handler->delete_me = EINA_TRUE;
   if (pd)
     {
        _ecore_main_fdh_poll_del(pd, fd_handler);
        pd->fd_handlers_to_delete = eina_list_append
          (pd->fd_handlers_to_delete, fd_handler);
        if (fd_handler->prep_func && pd->fd_handlers_with_prep)
          pd->fd_handlers_with_prep = eina_list_remove
            (pd->fd_handlers_with_prep, fd_handler);
        if (fd_handler->buf_func && pd->fd_handlers_with_buffer)
          pd->fd_handlers_with_buffer = eina_list_remove
            (pd->fd_handlers_with_buffer, fd_handler);
     }
   else
     {
        // The main loop is dead by now, so cleanup is required.
        ECORE_MAGIC_SET(fd_handler, ECORE_MAGIC_NONE);
        ecore_fd_handler_mp_free(fd_handler);
     }
   return r;
}

EAPI Ecore_Fd_Handler *
ecore_main_fd_handler_add(int                    fd,
                          Ecore_Fd_Handler_Flags flags,
                          Ecore_Fd_Cb            func,
                          const void            *data,
                          Ecore_Fd_Cb            buf_func,
                          const void            *buf_data)
{
   Ecore_Fd_Handler *fdh = NULL;
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
   fdh = _ecore_main_fd_handler_add(efl_main_loop_get(),
                                    ML_DAT, NULL, fd, flags, func, data,
                                    buf_func, buf_data, EINA_FALSE);
   if (fdh) fdh->legacy = EINA_TRUE;
   return fdh;
}

EAPI Ecore_Fd_Handler *
ecore_main_fd_handler_file_add(int                    fd,
                               Ecore_Fd_Handler_Flags flags,
                               Ecore_Fd_Cb            func,
                               const void            *data,
                               Ecore_Fd_Cb            buf_func,
                               const void            *buf_data)
{
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
   return _ecore_main_fd_handler_add(efl_main_loop_get(),
                                     ML_DAT, NULL, fd, flags, func, data,
                                     buf_func, buf_data, EINA_TRUE);
}

EAPI void *
ecore_main_fd_handler_del(Ecore_Fd_Handler *fd_handler)
{
   if (!fd_handler) return NULL;
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);

   if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
     {
        ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
                         "ecore_main_fd_handler_del");
        return NULL;
     }
   return _ecore_main_fd_handler_del(ML_OBJ, ML_DAT, fd_handler);
}

#ifdef _WIN32
EAPI Ecore_Win32_Handler *
_ecore_main_win32_handler_add(Eo                    *obj,
                              Efl_Loop_Data         *pd,
                              Eo                    *handler,
                              void                  *h,
                              Ecore_Win32_Handle_Cb  func,
                              const void            *data)
{
   Ecore_Win32_Handler *wh;

   EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
   if (!h || !func) return NULL;

   wh = ecore_win32_handler_calloc(1);
   if (!wh) return NULL;
   ECORE_MAGIC_SET(wh, ECORE_MAGIC_WIN32_HANDLER);
   wh->loop = obj;
   wh->loop_data = pd;
   wh->handler = handler;
   wh->h = (HANDLE)h;
   wh->func = func;
   wh->data = (void *)data;
   pd->win32_handlers = (Ecore_Win32_Handler *)
     eina_inlist_append(EINA_INLIST_GET(pd->win32_handlers),
                        EINA_INLIST_GET(wh));
   return wh;
}

void *
_ecore_main_win32_handler_del(Eo *obj EINA_UNUSED,
                              Efl_Loop_Data *pd,
                              Ecore_Win32_Handler *win32_handler)
{
   if (win32_handler->delete_me)
     {
        ERR("win32 handler %p deleted twice", win32_handler);
        return NULL;
     }

   win32_handler->delete_me = EINA_TRUE;
   win32_handler->handler = NULL;
   pd->win32_handlers_to_delete = eina_list_append
     (pd->win32_handlers_to_delete, win32_handler);
   return win32_handler->data;
}

EAPI Ecore_Win32_Handler *
ecore_main_win32_handler_add(void                  *h,
                             Ecore_Win32_Handle_Cb  func,
                             const void            *data)
{
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
   return _ecore_main_win32_handler_add(ML_OBJ, ML_DAT, NULL, h, func, data);
}

EAPI void *
ecore_main_win32_handler_del(Ecore_Win32_Handler *win32_handler)
{
   void *ret = NULL;

   if (!win32_handler) return NULL;
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
   if (!ECORE_MAGIC_CHECK(win32_handler, ECORE_MAGIC_WIN32_HANDLER))
     {
        ECORE_MAGIC_FAIL(win32_handler, ECORE_MAGIC_WIN32_HANDLER,
                         "ecore_main_win32_handler_del");
        return NULL;
     }
   ret = _ecore_main_win32_handler_del(ML_OBJ, ML_DAT, win32_handler);
   return ret;
}
#else
EAPI Ecore_Win32_Handler *
_ecore_main_win32_handler_add(Eo                    *obj EINA_UNUSED,
                              Efl_Loop_Data         *pd EINA_UNUSED,
                              Eo                    *handler EINA_UNUSED,
                              void                  *h EINA_UNUSED,
                              Ecore_Win32_Handle_Cb  func EINA_UNUSED,
                              const void            *data EINA_UNUSED)
{
   return NULL;
}

void *
_ecore_main_win32_handler_del(Eo *obj EINA_UNUSED,
                              Efl_Loop_Data *pd EINA_UNUSED,
                              Ecore_Win32_Handler *win32_handler EINA_UNUSED)
{
   return NULL;
}

EAPI Ecore_Win32_Handler *
ecore_main_win32_handler_add(void                  *h EINA_UNUSED,
                             Ecore_Win32_Handle_Cb  func EINA_UNUSED,
                             const void            *data EINA_UNUSED)
{
   return NULL;
}

EAPI void *
ecore_main_win32_handler_del(Ecore_Win32_Handler *win32_handler EINA_UNUSED)
{
   return NULL;
}
#endif

EAPI void
ecore_main_fd_handler_prepare_callback_set(Ecore_Fd_Handler *fd_handler,
                                           Ecore_Fd_Prep_Cb  func,
                                           const void       *data)
{
   if (!fd_handler) return;
   Efl_Loop_Data *pd = fd_handler->loop_data;
   EINA_MAIN_LOOP_CHECK_RETURN;

   if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
     {
        ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
                         "ecore_main_fd_handler_prepare_callback_set");
        return;
     }
   fd_handler->prep_func = func;
   fd_handler->prep_data = (void *)data;
   if ((!pd->fd_handlers_with_prep) ||
       (pd->fd_handlers_with_prep &&
        (!eina_list_data_find(pd->fd_handlers_with_prep, fd_handler))))
     // FIXME: THIS WILL NOT SCALE WITH LOTS OF PREP FUNCTIONS!!!
     pd->fd_handlers_with_prep = eina_list_append
       (pd->fd_handlers_with_prep, fd_handler);
}

EAPI int
ecore_main_fd_handler_fd_get(Ecore_Fd_Handler *fd_handler)
{
   if (!fd_handler) return -1;

   EINA_MAIN_LOOP_CHECK_RETURN_VAL(-1);

   if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
     {
        ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
                         "ecore_main_fd_handler_fd_get");
        return -1;
     }
   return fd_handler->fd;
}

EAPI Eina_Bool
ecore_main_fd_handler_active_get(Ecore_Fd_Handler      *fd_handler,
                                 Ecore_Fd_Handler_Flags flags)
{
   int ret = EINA_FALSE;

   EINA_MAIN_LOOP_CHECK_RETURN_VAL(EINA_FALSE);

   if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
     {
        ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
                         "ecore_main_fd_handler_active_get");
        return EINA_FALSE;
     }
   if ((flags & ECORE_FD_READ) &&  (fd_handler->read_active))  ret = EINA_TRUE;
   if ((flags & ECORE_FD_WRITE) && (fd_handler->write_active)) ret = EINA_TRUE;
   if ((flags & ECORE_FD_ERROR) && (fd_handler->error_active)) ret = EINA_TRUE;
   return ret;
}

EAPI void
ecore_main_fd_handler_active_set(Ecore_Fd_Handler      *fd_handler,
                                 Ecore_Fd_Handler_Flags flags)
{
   int ret = -1;

   if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
     {
        ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
                         "ecore_main_fd_handler_active_set");
        return;
     }
   fd_handler->flags = flags;
   if (fd_handler->loop_data)
     ret = _ecore_main_fdh_poll_modify(fd_handler->loop_data, fd_handler);
   if (ret < 0)
     ERR("Failed to mod epoll fd %d, loop data=%p: %s!",
         fd_handler->fd, fd_handler->loop_data, strerror(errno));
}

void
_ecore_main_content_clear(Eo *obj, Efl_Loop_Data *pd)
{
   __eina_promise_cancel_data(obj);

   while (pd->fd_handlers)
     {
        Ecore_Fd_Handler *fdh = pd->fd_handlers;

        pd->fd_handlers = (Ecore_Fd_Handler *)
          eina_inlist_remove(EINA_INLIST_GET(pd->fd_handlers),
                             EINA_INLIST_GET(fdh));
        if ((fdh->handler) && (fdh->legacy)) efl_del(fdh->handler);
        else
          {
// XXX: can't do this because this fd handler is legacy and might
// be cleaned up later in object destructors
//             ECORE_MAGIC_SET(fdh, ECORE_MAGIC_NONE);
//             ecore_fd_handler_mp_free(fdh);
          }
     }
   if (pd->fd_handlers_with_buffer)
     pd->fd_handlers_with_buffer =
       eina_list_free(pd->fd_handlers_with_buffer);
   if (pd->fd_handlers_with_prep)
     pd->fd_handlers_with_prep =
       eina_list_free(pd->fd_handlers_with_prep);
   if (pd->file_fd_handlers)
     pd->file_fd_handlers =
       eina_list_free(pd->file_fd_handlers);
   if (pd->fd_handlers_to_delete)
     pd->fd_handlers_to_delete =
       eina_list_free(pd->fd_handlers_to_delete);
   pd->fd_handlers_to_call = NULL;
   pd->fd_handlers_to_call_current = NULL;

   pd->do_quit = 0;

#ifdef _WIN32
   while (pd->win32_handlers)
     {
        Ecore_Win32_Handler *wh = pd->win32_handlers;

        pd->win32_handlers = (Ecore_Win32_Handler *)
          eina_inlist_remove(EINA_INLIST_GET(pd->win32_handlers),
                             EINA_INLIST_GET(wh));
        if (wh->handler) efl_del(wh->handler);
        else
          {
             ECORE_MAGIC_SET(wh, ECORE_MAGIC_NONE);
             ecore_win32_handler_mp_free(wh);
          }
     }
   if (pd->win32_handlers_to_delete)
     pd->win32_handlers_to_delete =
       eina_list_free(pd->win32_handlers_to_delete);
   pd->win32_handler_current = NULL;
#endif
}

void
_ecore_main_shutdown(void)
{
   Efl_Loop_Data *pd = ML_DAT;

   if (pd->in_loop)
     {
        ERR("Calling ecore_shutdown() while still in the main loop!!!");
        return;
     }
}

static void
_ecore_main_prepare_handlers(Eo *obj EINA_UNUSED, Efl_Loop_Data *pd)
{
   Ecore_Fd_Handler *fdh;
   Eina_List *l, *l2;

   // call the prepare callback for all handlers with prep functions
   EINA_LIST_FOREACH_SAFE(pd->fd_handlers_with_prep, l, l2, fdh)
     {
        if (!fdh)
          {
             pd->fd_handlers_with_prep = eina_list_remove_list
               (l, pd->fd_handlers_with_prep);
             continue;
          }
        if (!fdh->delete_me && fdh->prep_func)
          {
             fdh->references++;
             _ecore_call_prep_cb(fdh->prep_func, fdh->prep_data, fdh);
             fdh->references--;
          }
        else
          pd->fd_handlers_with_prep = eina_list_remove_list
            (pd->fd_handlers_with_prep, l);
     }
}

#if !defined(USE_G_MAIN_LOOP)
static int
_ecore_main_select(Eo *obj, Efl_Loop_Data *pd, double timeout)
{
   struct timeval tv, *t;
   fd_set rfds, wfds, exfds;
   Ecore_Fd_Handler *fdh;
   Eina_List *l;
   int max_fd, ret;
#ifndef _WIN32
   int err_no;
#endif

   t = NULL;
   if ((!ECORE_FINITE(timeout)) || (EINA_DBL_EQ(timeout, 0.0)))
     { // finite() tests for NaN, too big, too small, and infinity.
        tv.tv_sec = 0;
        tv.tv_usec = 0;
        t = &tv;
     }
   else if (timeout > 0.0)
     {
        int sec, usec;

#ifdef FIX_HZ
        timeout += (0.5 / HZ);
        sec = (int)timeout;
        usec = (int)((timeout - (double)sec) * 1000000);
#else
        sec = (int)timeout;
        usec = (int)((timeout - (double)sec) * 1000000);
#endif
        tv.tv_sec = sec;
        tv.tv_usec = usec;
        t = &tv;
     }
   max_fd = 0;
   FD_ZERO(&rfds);
   FD_ZERO(&wfds);
   FD_ZERO(&exfds);

   // call the prepare callback for all handlers
   if (pd->fd_handlers_with_prep) _ecore_main_prepare_handlers(obj, pd);

#ifdef HAVE_SYS_EPOLL_H
   if (pd->epoll_fd < 0)
     {
#endif
        EINA_INLIST_FOREACH(pd->fd_handlers, fdh)
          {
             if (!fdh->delete_me)
               {
                  if (fdh->flags & ECORE_FD_READ)
                    {
                       FD_SET(fdh->fd, &rfds);
                       if (fdh->fd > max_fd) max_fd = fdh->fd;
                    }
                  if (fdh->flags & ECORE_FD_WRITE)
                    {
                       FD_SET(fdh->fd, &wfds);
                       if (fdh->fd > max_fd) max_fd = fdh->fd;
                    }
                  if (fdh->flags & ECORE_FD_ERROR)
                    {
                       FD_SET(fdh->fd, &exfds);
                       if (fdh->fd > max_fd) max_fd = fdh->fd;
                    }
               }
          }
#ifdef HAVE_SYS_EPOLL_H
     }
   else
     {
        // polling on the epoll fd will wake when fd in the epoll set is active
        max_fd = _ecore_get_epoll_fd(obj, pd);
        FD_SET(max_fd, &rfds);
     }
#endif
   EINA_LIST_FOREACH(pd->file_fd_handlers, l, fdh)
     {
        if (!fdh->delete_me)
          {
             if (fdh->flags & ECORE_FD_READ)
               {
                  FD_SET(fdh->fd, &rfds);
                  if (fdh->fd > max_fd) max_fd = fdh->fd;
               }
             if (fdh->flags & ECORE_FD_WRITE)
               {
                  FD_SET(fdh->fd, &wfds);
                  if (fdh->fd > max_fd) max_fd = fdh->fd;
               }
             if (fdh->flags & ECORE_FD_ERROR)
               {
                  FD_SET(fdh->fd, &exfds);
                  if (fdh->fd > max_fd) max_fd = fdh->fd;
               }
             if (fdh->fd > max_fd) max_fd = fdh->fd;
          }
     }
   if (_ecore_signal_count_get(obj, pd)) return -1;

   eina_evlog("<RUN", NULL, 0.0, NULL);
   eina_evlog("!SLEEP", NULL, 0.0, t ? "timeout" : "forever");
   if (obj == ML_OBJ)
     ret = main_loop_select(max_fd + 1, &rfds, &wfds, &exfds, t);
   else
     ret = general_loop_select(max_fd + 1, &rfds, &wfds, &exfds, t);
#ifndef _WIN32
   err_no = errno;
#endif
   eina_evlog("!WAKE", NULL, 0.0, NULL);
   eina_evlog(">RUN", NULL, 0.0, NULL);

   pd->loop_time = ecore_time_get();
   if (ret < 0)
     {
#ifndef _WIN32
        if (err_no == EINTR) return -1;
        else if (err_no == EBADF) _ecore_main_fd_handlers_bads_rem(obj, pd);
#endif
     }
   if (ret > 0)
     {
#ifdef HAVE_SYS_EPOLL_H
        if (pd->epoll_fd >= 0)
          _ecore_main_fdh_epoll_mark_active(obj, pd);
        else
#endif
          {
             EINA_INLIST_FOREACH(pd->fd_handlers, fdh)
               {
                  if (!fdh->delete_me)
                    {
                       if (FD_ISSET(fdh->fd, &rfds))
                         fdh->read_active  = EINA_TRUE;
                       if (FD_ISSET(fdh->fd, &wfds))
                         fdh->write_active = EINA_TRUE;
                       if (FD_ISSET(fdh->fd, &exfds))
                         fdh->error_active = EINA_TRUE;
                       _ecore_try_add_to_call_list(obj, pd, fdh);
                    }
               }
          }
        EINA_LIST_FOREACH(pd->file_fd_handlers, l, fdh)
          {
             if (!fdh->delete_me)
               {
                  if (FD_ISSET(fdh->fd, &rfds))
                    fdh->read_active  = EINA_TRUE;
                  if (FD_ISSET(fdh->fd, &wfds))
                    fdh->write_active = EINA_TRUE;
                  if (FD_ISSET(fdh->fd, &exfds))
                    fdh->error_active = EINA_TRUE;
                  _ecore_try_add_to_call_list(obj, pd, fdh);
               }
          }
        _ecore_main_fd_handlers_cleanup(obj, pd);
#ifdef _WIN32
        _ecore_main_win32_handlers_cleanup(obj, pd);
#endif
        return 1;
     }
   return 0;
}

#endif

#ifndef _WIN32
# ifndef USE_G_MAIN_LOOP
static void
_ecore_main_fd_handlers_bads_rem(Eo *obj, Efl_Loop_Data *pd)
{
   Ecore_Fd_Handler *fdh;
   Eina_Inlist *l;
   int found = 0;

   ERR("Removing bad fds");
   for (l = EINA_INLIST_GET(pd->fd_handlers); l; )
     {
        fdh = (Ecore_Fd_Handler *)l;
        l = l->next;
        errno = 0;

        if ((fcntl(fdh->fd, F_GETFD) < 0) && (errno == EBADF))
          {
             ERR("Found bad fd at index %d", fdh->fd);
             if (fdh->flags & ECORE_FD_ERROR)
               {
                  ERR("Fd set for error! calling user");
                  fdh->references++;
                  if (!_ecore_call_fd_cb(fdh->func, fdh->data, fdh))
                    {
                       ERR("Fd function err returned 0, remove it");
                       if (!fdh->delete_me)
                         {
                            fdh->delete_me = EINA_TRUE;
                            _ecore_main_fdh_poll_del(pd, fdh);
                            pd->fd_handlers_to_delete =
                              eina_list_append(pd->fd_handlers_to_delete, fdh);
                         }
                       found++;
                    }
                  fdh->references--;
               }
             else
               {
                  ERR("Problematic fd found at %d! setting it for delete",
                      fdh->fd);
                  if (!fdh->delete_me)
                    {
                       fdh->delete_me = EINA_TRUE;
                       _ecore_main_fdh_poll_del(pd, fdh);
                       pd->fd_handlers_to_delete =
                         eina_list_append(pd->fd_handlers_to_delete, fdh);
                    }

                  found++;
               }
          }
     }
   if (found == 0)
     {
#  ifdef HAVE_GLIB
        ERR("No bad fd found. Maybe a foreign fd from glib?");
#  else
        ERR("No bad fd found. EEEK!");
#  endif
     }
   _ecore_main_fd_handlers_cleanup(obj, pd);
}

# endif
#endif

static void
_ecore_main_fd_handlers_cleanup(Eo *obj EINA_UNUSED, Efl_Loop_Data *pd)
{
   Ecore_Fd_Handler *fdh, *last;
   Eina_List *l, *l2;

   // Cleanup deleted caller from the list
   last = NULL;
   fdh = pd->fd_handlers_to_call;
   while (fdh)
     {
        if (fdh->delete_me)
          {
             if (!last)
               pd->fd_handlers_to_call = fdh == fdh->next_ready ?
                 NULL : fdh->next_ready;
             else
               last->next_ready = fdh == fdh->next_ready ?
                 last : fdh->next_ready;
          }
        else last = fdh;

        if (fdh == fdh->next_ready) break;
        fdh = fdh->next_ready;
     }

   if (!pd->fd_handlers_to_delete) return;
   EINA_LIST_FOREACH_SAFE(pd->fd_handlers_to_delete, l, l2, fdh)
     {
        if (!fdh)
          {
             pd->fd_handlers_to_delete = eina_list_remove_list
               (l, pd->fd_handlers_to_delete);
             continue;
          }
        if (fdh->references) continue;
        if (pd->fd_handlers_to_call_current == fdh)
          pd->fd_handlers_to_call_current = NULL;
        if (fdh->buf_func && pd->fd_handlers_with_buffer)
          pd->fd_handlers_with_buffer = eina_list_remove
            (pd->fd_handlers_with_buffer, fdh);
        if (fdh->prep_func && pd->fd_handlers_with_prep)
          pd->fd_handlers_with_prep = eina_list_remove
           (pd->fd_handlers_with_prep, fdh);
        pd->fd_handlers = (Ecore_Fd_Handler *)
          eina_inlist_remove(EINA_INLIST_GET(pd->fd_handlers),
                             EINA_INLIST_GET(fdh));
        if (fdh->file)
          pd->file_fd_handlers = eina_list_remove(pd->file_fd_handlers, fdh);
        ECORE_MAGIC_SET(fdh, ECORE_MAGIC_NONE);
        ecore_fd_handler_mp_free(fdh);
        pd->fd_handlers_to_delete = eina_list_remove_list
          (pd->fd_handlers_to_delete, l);
     }
}

#ifdef _WIN32
static void
_ecore_main_win32_handlers_cleanup(Eo *obj EINA_UNUSED, Efl_Loop_Data *pd)
{
   Ecore_Win32_Handler *wh;
   Eina_List *l, *l2;

   if (!pd->win32_handlers_to_delete) return;
   EINA_LIST_FOREACH_SAFE(pd->win32_handlers_to_delete, l, l2, wh)
     {
        if (!wh)
          {
             pd->win32_handlers_to_delete = eina_list_remove_list
               (l, pd->win32_handlers_to_delete);
             continue;
          }
        // wh->delete_me should be set for all whs at the start of the list
        if (wh->references) continue;
        pd->win32_handlers = (Ecore_Win32_Handler *)
          eina_inlist_remove(EINA_INLIST_GET(pd->win32_handlers),
                             EINA_INLIST_GET(wh));
        ECORE_MAGIC_SET(wh, ECORE_MAGIC_NONE);
        ecore_win32_handler_mp_free(wh);
        pd->win32_handlers_to_delete = eina_list_remove_list
          (pd->win32_handlers_to_delete, l);
     }
}
#endif

static void
_ecore_main_fd_handlers_call(Eo *obj EINA_UNUSED, Efl_Loop_Data *pd)
{
   // grab a new list
   if (!pd->fd_handlers_to_call_current)
     {
        pd->fd_handlers_to_call_current = pd->fd_handlers_to_call;
        pd->fd_handlers_to_call = NULL;
     }

   if (!pd->fd_handlers_to_call_current) return;
   eina_evlog("+fd_handlers", NULL, 0.0, NULL);

   while (pd->fd_handlers_to_call_current)
     {
        Ecore_Fd_Handler *fdh = pd->fd_handlers_to_call_current;

        if (!fdh->delete_me)
          {
             if ((fdh->read_active) ||
                 (fdh->write_active) ||
                 (fdh->error_active))
               {
                  fdh->references++;
                  if (!_ecore_call_fd_cb(fdh->func, fdh->data, fdh))
                    {
                       if (!fdh->delete_me)
                         {
                            fdh->delete_me = EINA_TRUE;
                            _ecore_main_fdh_poll_del(pd, fdh);
                            pd->fd_handlers_to_delete = eina_list_append
                              (pd->fd_handlers_to_delete, fdh);
                         }
                    }
                  fdh->references--;
#ifdef EFL_EXTRA_SANITY_CHECKS
                  _ecore_fd_valid(obj, pd);
#endif
                  fdh->read_active  = EINA_FALSE;
                  fdh->write_active = EINA_FALSE;
                  fdh->error_active = EINA_FALSE;
               }
          }

        // stop when we point to ourselves
        if (fdh->next_ready == fdh)
          {
             fdh->next_ready = NULL;
             pd->fd_handlers_to_call_current = NULL;
             break;
          }

        pd->fd_handlers_to_call_current = fdh->next_ready;
        fdh->next_ready = NULL;
      }
   eina_evlog("-fd_handlers", NULL, 0.0, NULL);
}

static int
_ecore_main_fd_handlers_buf_call(Eo *obj, Efl_Loop_Data *pd)
{
   Ecore_Fd_Handler *fdh;
   Eina_List *l, *l2;
   int ret;

   if (!pd->fd_handlers_with_buffer) return 0;
   eina_evlog("+fd_handlers_buf", NULL, 0.0, NULL);
   ret = 0;
   EINA_LIST_FOREACH_SAFE(pd->fd_handlers_with_buffer, l, l2, fdh)
     {
        if (!fdh)
          {
             pd->fd_handlers_with_buffer = eina_list_remove_list
               (l, pd->fd_handlers_with_buffer);
             continue;
          }
        if ((!fdh->delete_me) && fdh->buf_func)
          {
             fdh->references++;
             if (_ecore_call_fd_cb(fdh->buf_func, fdh->buf_data, fdh))
               {
                  ret |= _ecore_call_fd_cb(fdh->func, fdh->data, fdh);
                  fdh->read_active = EINA_TRUE;
                  _ecore_try_add_to_call_list(obj, pd, fdh);
               }
             fdh->references--;
          }
        else
          pd->fd_handlers_with_buffer = eina_list_remove_list
            (pd->fd_handlers_with_buffer, l);
     }
   eina_evlog("-fd_handlers_buf", NULL, 0.0, NULL);
   return ret;
}

#ifdef HAVE_LIBUV
static void
_ecore_main_loop_uv_prepare(uv_prepare_t *handle EINA_UNUSED)
{
   Eo *obj = ML_OBJ;
   Efl_Loop_Data *pd = ML_DAT;
   double t = -1;

   _dl_uv_timer_stop(&_ecore_main_uv_handle_timers);
   if ((pd->in_loop == 0) && (pd->do_quit))
     {
        _ecore_main_fd_handlers_cleanup(obj, pd);

        while (pd->fd_handlers)
          {
             Ecore_Fd_Handler *fdh = pd->fd_handlers;
             pd->fd_handlers = (Ecore_Fd_Handler *)
               eina_inlist_remove(EINA_INLIST_GET(pd->fd_handlers),
                                  EINA_INLIST_GET(fdh));
             fdh->delete_me = 1;
             _ecore_main_fdh_poll_del(pd, fdh);
             ECORE_MAGIC_SET(fdh, ECORE_MAGIC_NONE);
             ecore_fd_handler_mp_free(fdh);
          }
       if (pd->fd_handlers_with_buffer)
         pd->fd_handlers_with_buffer = eina_list_free(pd->fd_handlers_with_buffer);
       if (pd->fd_handlers_with_prep)
         pd->fd_handlers_with_prep = eina_list_free(pd->fd_handlers_with_prep);
       if (pd->fd_handlers_to_delete)
         pd->fd_handlers_to_delete = eina_list_free(pd->fd_handlers_to_delete);
       if (pd->file_fd_handlers)
         pd->file_fd_handlers = eina_list_free(pd->file_fd_handlers);

        pd->fd_handlers_to_call = NULL;
        pd->fd_handlers_to_call_current = NULL;

        _dl_uv_prepare_stop(&_ecore_main_uv_prepare);
        _dl_uv_check_stop(&_ecore_main_uv_check);
        _dl_uv_stop(_dl_uv_default_loop());

        return;
     }

   in_main_loop++;
   pd->in_loop = in_main_loop;

   if (!_ecore_main_uv_idling)
     {
        _ecore_main_uv_idling = EINA_TRUE;
        efl_event_callback_call(obj, EFL_LOOP_EVENT_IDLE_ENTER, NULL);
        _ecore_throttle();
        _throttle_do(pd);
     }

   if (_ecore_main_uv_idling)
     {
        _ecore_main_idler_all_call(obj);
        DBG("called idles");
        if (_ecore_main_idlers_exist(pd) || (pd->message_queue)) t = 0.0;
     }

   if (pd->do_quit)
     {
        DBG("do quit outside loop");

        if (_ecore_main_uv_idling)
          {
             efl_event_callback_call(obj, EFL_LOOP_EVENT_IDLE_EXIT, NULL);
             _ecore_animator_run_reset();
             _ecore_main_uv_idling = EINA_FALSE;
          }
        t = -1;
        pd->loop_time = ecore_time_get();
        _efl_loop_timer_enable_new(obj, pd);
        goto done;
     }

   assert(!pd->fd_handlers_to_call);

   pd->loop_time = ecore_time_get();
   _efl_loop_timer_enable_new(obj, pd);
   if (_efl_loop_timers_exists(obj, pd) || (t >= 0))
     {
        double tnext = _efl_loop_timer_next_get(obj, pd);

        if ((t < 0) || ((tnext >= 0) && (tnext < t))) t = tnext;
        DBG("Should awake after %f", t);

        if (t >= 0.0)
          {
             // _dl_uv_timer_stop(&_ecore_main_uv_handle_timers);
             _dl_uv_timer_start(&_ecore_main_uv_handle_timers,
                                &_ecore_main_loop_timer_run,
                                t * 1000, 0);
          }
        else DBG("Is not going to awake with timer");
     }
   else DBG("Is not going to awake with timer");

done:
   if (pd->fd_handlers_with_prep) _ecore_main_prepare_handlers(obj, pd);
   in_main_loop--;
   pd->in_loop = in_main_loop;
}
#endif

#if !defined(USE_G_MAIN_LOOP)
enum {
   SPIN_MORE,
   SPIN_RESTART,
   LOOP_CONTINUE
};

static int
_ecore_main_loop_spin_core(Eo *obj, Efl_Loop_Data *pd)
{
   // as we are spinning we need to update loop time per spin
   pd->loop_time = ecore_time_get();
   // call all idlers
   _ecore_main_idler_all_call(obj);
   // which returns false if no more idelrs exist
   if (!_ecore_main_idlers_exist(pd)) return SPIN_RESTART;
   // sneaky - drop through or if checks - the first one to succeed
   // drops through and returns "continue" so further ones dont run
   if ((_ecore_main_select(obj, pd, 0.0) > 0) || (pd->message_queue) ||
       (_ecore_signal_count_get(obj, pd) > 0) || (pd->do_quit))
     return LOOP_CONTINUE;
   // default - spin more
   return SPIN_MORE;
}

static int
_ecore_main_loop_spin_no_timers(Eo *obj, Efl_Loop_Data *pd)
{
   // if we have idlers we HAVE to spin and handle everything
   // in a polling way - spin in a tight polling loop
   for (;;)
     {
        int action = _ecore_main_loop_spin_core(obj, pd);
        if (action != SPIN_MORE) return action;
        // if an idler has added a timer then we need to go through
        // the start of the spin cycle again to handle cases properly
        if (_efl_loop_timers_exists(obj, pd)) return SPIN_RESTART;
     }
   // just contiune handling events etc.
   return LOOP_CONTINUE;
}

static int
_ecore_main_loop_spin_timers(Eo *obj, Efl_Loop_Data *pd)
{
   // if we have idlers we HAVE to spin and handle everything
   // in a polling way - spin in a tight polling loop
   for (;;)
     {
        int action = _ecore_main_loop_spin_core(obj, pd);
        if (action != SPIN_MORE) return action;
        // if next timer expires now or in the past - stop spinning and
        // continue the mainloop walk as our "select" timeout has
        // expired now
        if (_efl_loop_timer_next_get(obj, pd) <= 0.0) return LOOP_CONTINUE;
     }
   // just contiune handling events etc.
   return LOOP_CONTINUE;
}

static void
_ecore_fps_marker_1(void)
{
   if (!_ecore_fps_debug) return;
   t2 = ecore_time_get();
   if ((t1 > 0.0) && (t2 > 0.0)) _ecore_fps_debug_runtime_add(t2 - t1);
}

static void
_ecore_fps_marker_2(void)
{
   if (!_ecore_fps_debug) return;
   t1 = ecore_time_get();
}

static void
_ecore_main_loop_iterate_internal(Eo *obj, Efl_Loop_Data *pd, int once_only)
{
   double next_time = -1.0;

   if (obj == ML_OBJ)
     {
        in_main_loop++;
        pd->in_loop = in_main_loop;
     }
   // expire any timers
   _efl_loop_timer_expired_timers_call(obj, pd, pd->loop_time);
   // process signals into events ....
   if (obj == ML_OBJ) _ecore_signal_received_process(obj, pd);
   // if as a result of timers/animators or signals we have accumulated
   // events, then instantly handle them
   if (pd->message_queue)
     {
        // but first conceptually enter an idle state
        efl_event_callback_call(obj, EFL_LOOP_EVENT_IDLE_ENTER, NULL);
        _ecore_throttle();
        _throttle_do(pd);
        // now quickly poll to see which input fd's are active
        _ecore_main_select(obj, pd, 0.0);
        // allow newly queued timers to expire from now on
        _efl_loop_timer_enable_new(obj, pd);
        // go straight to processing the events we had queued
        goto process_all;
     }

   if (once_only)
     {
        // in once_only mode we should quickly poll for inputs, signals
        // if we got any events or signals, allow new timers to process.
        // use bitwise or to force both conditions to be tested and
        // merged together
        if (_ecore_main_select(obj, pd, 0.0) |
            _ecore_signal_count_get(obj, pd))
          {
             _efl_loop_timer_enable_new(obj, pd);
             goto process_all;
          }
     }
   else
     {
        // call idle enterers ...
        efl_event_callback_call(obj, EFL_LOOP_EVENT_IDLE_ENTER, NULL);
        _ecore_throttle();
        _throttle_do(pd);
     }

   // if these calls caused any buffered events to appear - deal with them
   if (pd->fd_handlers_with_buffer)
     _ecore_main_fd_handlers_buf_call(obj, pd);

   // if there are any (buffered fd handling may generate them)
   // then jump to processing them */
   if (pd->message_queue)
     {
        _ecore_main_select(obj, pd, 0.0);
        _efl_loop_timer_enable_new(obj, pd);
        goto process_all;
     }

   if (once_only)
     {
        // in once_only mode enter idle here instead and then return
        efl_event_callback_call(obj, EFL_LOOP_EVENT_IDLE_ENTER, NULL);
        _ecore_throttle();
        _throttle_do(pd);
        _efl_loop_timer_enable_new(obj, pd);
        goto done;
     }

   if (obj == ML_OBJ) _ecore_fps_marker_1();

   // start of the sleeping or looping section
start_loop: //-*************************************************************
   // any timers re-added as a result of these are allowed to go
   _efl_loop_timer_enable_new(obj, pd);
   // if we have been asked to quit the mainloop then exit at this point
   if (pd->do_quit)
     {
        _efl_loop_timer_enable_new(obj, pd);
        goto done;
     }
   if (!pd->message_queue)
     {
        // init flags
        next_time = _efl_loop_timer_next_get(obj, pd);
        // no idlers
        if (!_ecore_main_idlers_exist(pd))
          {
             // sleep until timeout or forever (-1.0) waiting for on fds
             _ecore_main_select(obj, pd, next_time);
          }
        else
          {
             int action = LOOP_CONTINUE;

             // no timers - spin
             if (next_time < 0) action = _ecore_main_loop_spin_no_timers(obj, pd);
             // timers - spin
             else action = _ecore_main_loop_spin_timers(obj, pd);
             if (action == SPIN_RESTART) goto start_loop;
          }
     }
   if (obj == ML_OBJ) _ecore_fps_marker_2();

   // actually wake up and deal with input, events etc.
process_all: //-*********************************************************

   // we came out of our "wait state" so idle has exited
   if (!once_only)
     {
        _ecore_animator_run_reset(); // XXX:
        efl_event_callback_call(obj, EFL_LOOP_EVENT_IDLE_EXIT, NULL);
     }
   // call the fd handler per fd that became alive...
   // this should read or write any data to the monitored fd and then
   // post events onto the ecore event pipe if necessary
   _ecore_main_fd_handlers_call(obj, pd);
   if (pd->fd_handlers_with_buffer) _ecore_main_fd_handlers_buf_call(obj, pd);
   // process signals into events ....
   _ecore_signal_received_process(obj, pd);
   // handle events ...
   efl_loop_message_process(obj);
   _ecore_main_fd_handlers_cleanup(obj, pd);

   if (once_only)
     {
        // if in once_only mode handle idle exiting
        efl_event_callback_call(obj, EFL_LOOP_EVENT_IDLE_ENTER, NULL);
        _ecore_throttle();
        _throttle_do(pd);
     }

done: //-*****************************************************************
   // Agressively flush animator
   _ecore_animator_flush();
   if (!once_only)
     eina_slstr_local_clear(); // Free all short lived strings

   if (obj == ML_OBJ)
     {
        in_main_loop--;
        pd->in_loop = in_main_loop;
     }
}

#endif

#ifdef _WIN32
typedef struct
{
   DWORD   objects_nbr;
   HANDLE *objects;
   DWORD   timeout;
} Ecore_Main_Win32_Thread_Data;

static unsigned int __stdcall
_ecore_main_win32_objects_wait_thread(void *data)
{
   Ecore_Main_Win32_Thread_Data *td = (Ecore_Main_Win32_Thread_Data *)data;
   return MsgWaitForMultipleObjects(td->objects_nbr,
                                    (const HANDLE *)td->objects,
                                    FALSE,
                                    td->timeout,
                                    QS_ALLINPUT);
}

static DWORD
_ecore_main_win32_objects_wait(DWORD objects_nbr,
                               const HANDLE *objects,
                               DWORD timeout)
{
   Ecore_Main_Win32_Thread_Data *threads_data;
   HANDLE *threads_handles;
   DWORD threads_nbr, threads_remain, objects_idx, result, i;

   if (objects_nbr < MAXIMUM_WAIT_OBJECTS)
     return MsgWaitForMultipleObjects(objects_nbr,
                                      objects,
                                      EINA_FALSE,
                                      timeout, QS_ALLINPUT);
   // too much objects, so we launch a bunch of threads to
   // wait for, each one calls MsgWaitForMultipleObjects

   threads_nbr = objects_nbr / (MAXIMUM_WAIT_OBJECTS - 1);
   threads_remain = objects_nbr % (MAXIMUM_WAIT_OBJECTS - 1);
   if (threads_remain > 0) threads_nbr++;

   if (threads_nbr > MAXIMUM_WAIT_OBJECTS)
     {
        CRI("Too much objects to wait for (%lu).", objects_nbr);
        return WAIT_FAILED;
     }

   threads_handles = (HANDLE *)malloc(threads_nbr * sizeof(HANDLE));
   if (!threads_handles)
     {
        ERR("Can not allocate memory for the waiting thread.");
        return WAIT_FAILED;
     }

   threads_data = (Ecore_Main_Win32_Thread_Data *)
     malloc(threads_nbr * sizeof(Ecore_Main_Win32_Thread_Data));
   if (!threads_data)
     {
        ERR("Can not allocate memory for the waiting thread.");
        goto free_threads_handles;
     }

   objects_idx = 0;
   for (i = 0; i < threads_nbr; i++)
     {
        threads_data[i].timeout = timeout;
        threads_data[i].objects = (HANDLE *)objects + objects_idx;

        if ((i == (threads_nbr - 1)) && (threads_remain != 0))
          {
             threads_data[i].objects_nbr = threads_remain;
             objects_idx += threads_remain;
          }
        else
          {
             threads_data[i].objects_nbr = (MAXIMUM_WAIT_OBJECTS - 1);
             objects_idx += (MAXIMUM_WAIT_OBJECTS - 1);
          }

        threads_handles[i] = (HANDLE)_beginthreadex
          (NULL, 0, _ecore_main_win32_objects_wait_thread,
           &threads_data[i], 0, NULL);
        if (!threads_handles[i])
          {
             DWORD j;

             ERR("Can not create the waiting threads.");
             WaitForMultipleObjects(i, threads_handles, TRUE, INFINITE);
             for (j = 0; j < i; j++)
               CloseHandle(threads_handles[i]);

             goto free_threads_data;
          }
     }

   result = WaitForMultipleObjects(threads_nbr,
                                   threads_handles,
                                   FALSE, // we wait until one thread signaled
                                   INFINITE);

   if (result < (WAIT_OBJECT_0 + threads_nbr))
     {
        DWORD wait_res;

        // One of the thread callback has exited so we retrieve
        // its exit status, that is the returned value of
        // MsgWaitForMultipleObjects()
        if (GetExitCodeThread(threads_handles[result - WAIT_OBJECT_0],
                              &wait_res))
          {
             WaitForMultipleObjects(threads_nbr, threads_handles,
                                    TRUE, INFINITE);
             for (i = 0; i < threads_nbr; i++)
               CloseHandle(threads_handles[i]);
             free(threads_data);
             free(threads_handles);
             return wait_res;
          }
     }
   else
     {
        ERR("Error when waiting threads.");
        if (result == WAIT_FAILED)
          ERR("%s", evil_last_error_get());
        goto close_thread;
     }

close_thread:
   WaitForMultipleObjects(threads_nbr, threads_handles, TRUE, INFINITE);
   for (i = 0; i < threads_nbr; i++) CloseHandle(threads_handles[i]);
free_threads_data:
   free(threads_data);
free_threads_handles:
   free(threads_handles);

   return WAIT_FAILED;
}

static unsigned int
_stdin_wait_thread(void *data EINA_UNUSED)
{
   int c = getc(stdin);
   ungetc(c, stdin);
   return 0;
}

static int
_ecore_main_win32_select(int             nfds EINA_UNUSED,
                         fd_set         *readfds,
                         fd_set         *writefds,
                         fd_set         *exceptfds,
                         struct timeval *tv)
{
   Efl_Loop_Data *pd = ML_DAT;
   HANDLE *objects;
   int *sockets;
   Ecore_Fd_Handler *fdh;
   Ecore_Win32_Handler *wh;
   static HANDLE stdin_wait_thread = INVALID_HANDLE_VALUE;
   HANDLE stdin_handle;
   DWORD result, timeout;
   MSG msg;
   unsigned int fds_nbr = 0;
   unsigned int objects_nbr = 0;
   unsigned int events_nbr = 0;
   unsigned int i;
   int res;
   Eina_Bool stdin_thread_done = EINA_FALSE;

   fds_nbr = eina_inlist_count(EINA_INLIST_GET(pd->fd_handlers));
   sockets = (int *)malloc(fds_nbr * sizeof(int));
   if (!sockets) return -1;

   objects = (HANDLE)malloc((fds_nbr +
                             eina_inlist_count
                             (EINA_INLIST_GET(pd->win32_handlers)))
                            * sizeof(HANDLE));
   if (!objects)
     {
        free(sockets);
        return -1;
     }

   // Create an event object per socket
   EINA_INLIST_FOREACH(pd->fd_handlers, fdh)
     {
        if (fdh->delete_me) continue;

        WSAEVENT event;
        long network_event;

        network_event = 0;
        if (readfds)
          {
             if (FD_ISSET(fdh->fd, readfds))
               network_event |= FD_READ | FD_CONNECT | FD_ACCEPT;
          }
        if (writefds)
          {
             if (FD_ISSET(fdh->fd, writefds))
               network_event |= FD_WRITE | FD_CLOSE;
          }
        if (exceptfds)
          {
             if (FD_ISSET(fdh->fd, exceptfds))
               network_event |= FD_OOB;
          }

        if (network_event)
          {
             event = WSACreateEvent();
             WSAEventSelect(fdh->fd, event, network_event);
             objects[objects_nbr] = event;
             sockets[events_nbr] = fdh->fd;
             events_nbr++;
             objects_nbr++;
          }
     }
   stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
   // store the HANDLEs in the objects to wait for
   EINA_INLIST_FOREACH(pd->win32_handlers, wh)
     {
        if (wh->delete_me) continue;

        if (wh->h == stdin_handle)
          {
             if (stdin_wait_thread == INVALID_HANDLE_VALUE)
               stdin_wait_thread = (HANDLE)_beginthreadex(NULL,
                                                          0,
                                                          _stdin_wait_thread,
                                                          NULL,
                                                          0,
                                                          NULL);
             objects[objects_nbr] = stdin_wait_thread;
          }
        else objects[objects_nbr] = wh->h;
        objects_nbr++;
     }

   // Empty the queue before waiting
   while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
     {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
     }

   // Wait for any message sent or posted to this queue
   // or for one of the passed handles be set to signaled.
   if (!tv) timeout = INFINITE;
   else timeout = (DWORD)((tv->tv_sec * 1000.0) + (tv->tv_usec / 1000.0));

   if (timeout == 0)
     {
        res = 0;
        goto err;
     }

   result = _ecore_main_win32_objects_wait(objects_nbr,
                                           (const HANDLE *)objects,
                                           timeout);
   if (readfds)   FD_ZERO(readfds);
   if (writefds)  FD_ZERO(writefds);
   if (exceptfds) FD_ZERO(exceptfds);
   // The result tells us the type of event we have.
   if (result == WAIT_FAILED)
     {
        WRN("%s", evil_last_error_get());
        res = -1;
     }
   else if (result == WAIT_TIMEOUT)
     {
        INF("time-out interval elapsed.");
        res = 0;
     }
   else if (result == (WAIT_OBJECT_0 + objects_nbr))
     {
        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
          {
             TranslateMessage(&msg);
             DispatchMessage(&msg);
          }
        res = 0;
     }
   else if (result < WAIT_OBJECT_0 + events_nbr)
     {
        WSANETWORKEVENTS network_event;

        WSAEnumNetworkEvents(sockets[result], objects[result], &network_event);
        if ((network_event.lNetworkEvents &
             (FD_READ | FD_CONNECT | FD_ACCEPT)) && readfds)
          FD_SET(sockets[result], readfds);
        if ((network_event.lNetworkEvents &
             (FD_WRITE | FD_CLOSE)) && writefds)
          FD_SET(sockets[result], writefds);
        if ((network_event.lNetworkEvents & FD_OOB) && exceptfds)
          FD_SET(sockets[result], exceptfds);
        res = 1;
     }
   else if ((result >= (WAIT_OBJECT_0 + events_nbr)) &&
            (result < (WAIT_OBJECT_0 + objects_nbr)))
     {
        if (!pd->win32_handler_current)
          // regular main loop, start from head
          pd->win32_handler_current = pd->win32_handlers;
        else
          // recursive main loop, continue from where we were
          pd->win32_handler_current = (Ecore_Win32_Handler *)
            EINA_INLIST_GET(pd->win32_handler_current)->next;

        if (objects[result - WAIT_OBJECT_0] == stdin_wait_thread)
          stdin_thread_done = EINA_TRUE;

        while (pd->win32_handler_current)
          {
             wh = pd->win32_handler_current;

             if ((objects[result - WAIT_OBJECT_0] == wh->h) ||
                 ((objects[result - WAIT_OBJECT_0] == stdin_wait_thread) &&
                  (wh->h == stdin_handle)))
               {
                  if (!wh->delete_me)
                    {
                       wh->references++;
                       if (!wh->func(wh->data, wh))
                         {
                            wh->delete_me = EINA_TRUE;
                            pd->win32_handlers_to_delete = eina_list_append
                              (pd->win32_handlers_to_delete, wh);
                         }
                       wh->references--;
                    }
               }
             if (pd->win32_handler_current)
               // may have changed in recursive main loops
               pd->win32_handler_current = (Ecore_Win32_Handler *)
                 EINA_INLIST_GET(pd->win32_handler_current)->next;
          }
        res = 1;
     }
   else
     {
        ERR("unknown result...\n");
        res = -1;
     }

err:
   // Remove event objects again
   for (i = 0; i < events_nbr; i++) WSACloseEvent(objects[i]);

   if (stdin_thread_done) stdin_wait_thread = INVALID_HANDLE_VALUE;

   free(objects);
   free(sockets);
   return res;
}
#endif