aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/elementary/elm_gesture_layer.c
blob: 2ea1c695898c7986def6e73fdd2ab3bb346b4c03 (plain) (blame)
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
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#include <Elementary.h>
#include "elm_priv.h"
#include "elm_gesture_layer.eo.h"

#define MY_CLASS ELM_GESTURE_LAYER_CLASS

#define MY_CLASS_NAME "Elm_Gesture_Layer"
#define MY_CLASS_NAME_LEGACY "elm_gesture_layer"

/* Some defaults */
#define ELM_MOUSE_DEVICE             0
/* ELM_GESTURE_NEGATIVE_ANGLE - magic number says we didn't compute this yet */
#define ELM_GESTURE_NEGATIVE_ANGLE   (-1.0) /* Magic number */
#define ELM_GESTURE_MOMENTUM_DELAY   25
#define ELM_GESTURE_MOMENTUM_TIMEOUT 50
#define ELM_GESTURE_MULTI_TIMEOUT    50
#define ELM_GESTURE_MINIMUM_MOMENTUM 0.001

/* Some Trigo values */
#define RAD_90DEG                    M_PI_2
#define RAD_180DEG                   M_PI
#define RAD_270DEG                   (M_PI_2 * 3)
#define RAD_360DEG                   (M_PI * 2)

#define RAD2DEG(x) ((x) * 57.295779513)
#define DEG2RAD(x) ((x) / 57.295779513)

static void *
_glayer_buf_dup(void *buf, size_t size)
{
   void *p;

   p = malloc(size);
   memcpy(p, buf, size);

   return p;
}

#define COPY_EVENT_INFO(EV) _glayer_buf_dup(EV, sizeof(*EV))

#define SET_TEST_BIT(P)                               \
  do {                                                \
       P->test = P->cbs[ELM_GESTURE_STATE_START] || \
         P->cbs[ELM_GESTURE_STATE_MOVE] ||          \
         P->cbs[ELM_GESTURE_STATE_END] ||           \
         P->cbs[ELM_GESTURE_STATE_ABORT];           \
    } while (0)

#define IS_TESTED_GESTURE(gesture) \
  ((gesture) ? (gesture)->test : EINA_FALSE)

#define IS_TESTED(T) \
  ((sd->gesture[T]) ? sd->gesture[T]->test : EINA_FALSE)

#define ELM_GESTURE_LAYER_DATA_GET(o, sd) \
  Elm_Gesture_Layer_Data * sd = efl_data_scope_get(o, MY_CLASS)

#define ELM_GESTURE_LAYER_DATA_GET_OR_RETURN(o, ptr) \
  ELM_GESTURE_LAYER_DATA_GET(o, ptr);                \
  if (!ptr)                                          \
    {                                                \
       ERR("No widget data for object %p (%s)", \
                o, evas_object_type_get(o));         \
       return;                                       \
    }

#define ELM_GESTURE_LAYER_DATA_GET_OR_RETURN_VAL(o, ptr, val) \
  ELM_GESTURE_LAYER_DATA_GET(o, ptr);                         \
  if (!ptr)                                                   \
    {                                                         \
       ERR("No widget data for object %p (%s)",          \
                o, evas_object_type_get(o));                  \
       return val;                                            \
    }

#define ELM_GESTURE_LAYER_CHECK(obj)                                      \
  if (!obj || !efl_isa(obj, MY_CLASS)) \
    return

/**
 * @internal
 *
 * @struct _Pointer_Event
 * Struct holds pointer-event info
 * This is a generic pointer event structure
 *
 * @ingroup Elm_Gesture_Layer
 */
struct _Pointer_Event
{
   Evas_Coord         x, y;
   unsigned int       timestamp;
   int                device;
   Evas_Callback_Type event_type;
};

/**
 * @internal
 *
 * @typedef Pointer_Event
 * Type for generic pointer event structure
 *
 * @ingroup Elm_Gesture_Layer
 */
typedef struct _Pointer_Event Pointer_Event;

/**
 * @internal
 *
 * @struct _Func_Data
 * Struct holds callback information.
 *
 * @ingroup Elm_Gesture_Layer
 */
struct _Func_Data
{
   EINA_INLIST;
   void                *user_data; /**< Holds user data to CB (like sd) */
   Elm_Gesture_Event_Cb cb;
};

/**
 * @internal
 *
 * @typedef Func_Data
 * type for callback information
 *
 * @ingroup Elm_Gesture_Layer
 */
typedef struct _Func_Data Func_Data;

/**
 * @internal
 *
 * @struct _Gesture_Info
 * Struct holds gesture info
 *
 * @ingroup Elm_Gesture_Layer
 */
struct _Gesture_Info
{
   Evas_Object      *obj;
   void             *data; /**< Holds gesture intermediate processing data */
   Eina_Inlist      *cbs[ELM_GESTURE_STATE_ABORT + 1]; /**< Callback info (Func_Data) for states */
   Elm_Gesture_Type  g_type; /**< gesture type */
   Elm_Gesture_State state; /**< gesture state */
   void             *info; /**< Data for the state callback */
   Eina_Bool         test; /**< if true this gesture should be tested on input */
};

/**
 * @internal
 *
 * @typedef Gesture_Info
 * Type for _Gesture_Info
 *
 * @ingroup Elm_Gesture_Layer
 */
typedef struct _Gesture_Info Gesture_Info;

typedef struct
{
   void (*test)(Evas_Object *obj, Pointer_Event *pe,
                void *event_info, Evas_Callback_Type event_type,
                Elm_Gesture_Type g_type);
   void (*reset)(Gesture_Info *gesture);
   void (*cont_reset)(Gesture_Info *gesture); /* Can be NULL. */
} Tests_Array_Funcs;

/* functions referred by _glayer_tests_array */
static void _tap_gesture_test(Evas_Object *obj,
                              Pointer_Event *pe,
                              void *event_info,
                              Evas_Callback_Type event_type,
                              Elm_Gesture_Type g_type);
static void _tap_gestures_test_reset(Gesture_Info *gesture);
static void _n_long_tap_test(Evas_Object *obj,
                             Pointer_Event *pe,
                             void *event_info,
                             Evas_Callback_Type event_type,
                             Elm_Gesture_Type g_type);
static void _n_long_tap_test_reset(Gesture_Info *gesture);
static void _momentum_test(Evas_Object *obj,
                           Pointer_Event *pe,
                           void *event_info,
                           Evas_Callback_Type event_type,
                           Elm_Gesture_Type g_type);
static void _momentum_test_reset(Gesture_Info *gesture);
static void _n_line_test(Evas_Object *obj,
                         Pointer_Event *pe,
                         void *event_info,
                         Evas_Callback_Type event_type,
                         Elm_Gesture_Type g_type);
static void _line_test_reset(Gesture_Info *gesture);
static void _zoom_test(Evas_Object *obj,
                       Pointer_Event *pe,
                       void *event_info,
                       Evas_Callback_Type event_type,
                       Elm_Gesture_Type g_type);
static void _zoom_test_reset(Gesture_Info *gesture);
static void _rotate_test(Evas_Object *obj,
                         Pointer_Event *pe,
                         void *event_info,
                         Evas_Callback_Type event_type,
                         Elm_Gesture_Type g_type);
static void _rotate_test_reset(Gesture_Info *gesture);

static void _event_process(void *data,
                           Evas_Object *obj,
                           void *event_info,
                           Evas_Callback_Type event_type);

static void _callbacks_unregister(Evas_Object *obj);

/* Should be the same order as _Elm_Gesture_Type */
static Tests_Array_Funcs _glayer_tests_array[] = {
   { NULL, NULL, NULL },     /** Because someone made an awful mistake. */
   { _tap_gesture_test, _tap_gestures_test_reset, NULL },
   /* ELM_GESTURE_N_TAPS */
   { _n_long_tap_test, _n_long_tap_test_reset, NULL },
   /* ELM_GESTURE_N_LONG_TAPS */
   { _tap_gesture_test, _tap_gestures_test_reset, NULL },
   /* ELM_GESTURE_N_DOUBLE_TAPS */
   { _tap_gesture_test, _tap_gestures_test_reset, NULL },
   /* ELM_GESTURE_N_TRIPLE_TAPS */
   { _momentum_test, _momentum_test_reset, _momentum_test_reset },
   /* ELM_GESTURE_MOMENTUM */
   { _n_line_test, _line_test_reset, _line_test_reset },
   /* ELM_GESTURE_N_LINES */
   { _n_line_test, _line_test_reset, _line_test_reset },
   /* ELM_GESTURE_N_FLICKS */
   { _zoom_test, _zoom_test_reset, _zoom_test_reset },
   /* ELM_GESTURE_ZOOM */
   { _rotate_test, _rotate_test_reset, _rotate_test_reset },
   /* ELM_GESTURE_ROTATE */
   { NULL, NULL, NULL }
};

/**
 * @internal
 *
 * @struct _Event_History
 * Struct holds event history.
 * These events are repeated if no gesture found.
 *
 * @ingroup Elm_Gesture_Layer
 */
struct _Event_History
{
   EINA_INLIST;
   void              *event;
   Evas_Callback_Type event_type;
};

/**
 * @internal
 *
 * @typedef Event_History
 * Type for _Event_History
 *
 * @ingroup Elm_Gesture_Layer
 */
typedef struct _Event_History Event_History;

/* All *Type structs hold result for the user in 'info' field
 * The rest is gesture processing intermediate data.
 * NOTE: info field must be FIRST in the struct.
 * This is used when reporting ABORT in _event_history_clear() */
struct _Taps_Type
{
   Elm_Gesture_Taps_Info info;
   unsigned int          sum_x;
   unsigned int          sum_y;
   unsigned int          n_taps_needed;
   unsigned int          n_taps;
   Eina_List            *l;
};
typedef struct _Taps_Type Taps_Type;

struct _Long_Tap_Type
{
   Elm_Gesture_Taps_Info info;
   Evas_Coord            center_x;
   Evas_Coord            center_y;
   Ecore_Timer          *timeout; /* When this expires, long tap STARTed */
   Eina_List            *touched;
};
typedef struct _Long_Tap_Type Long_Tap_Type;

struct _Momentum_Type /* Fields used by _line_test() */
{
   Elm_Gesture_Momentum_Info info;
   Evas_Coord_Point          line_st;
   Evas_Coord_Point          line_end;
   unsigned int              t_st_x; /* Time start on X */
   unsigned int              t_st_y; /* Time start on Y */
   unsigned int              t_end; /* Time end        */
   unsigned int              t_up; /* Recent up event time */
   int                       xdir, ydir;
};
typedef struct _Momentum_Type Momentum_Type;

struct _Line_Data
{
   Evas_Coord_Point line_st;
   Evas_Coord_Point line_end;
   Evas_Coord       line_length;
   unsigned int     t_st; /* Time start */
   unsigned int     t_end; /* Time end   */
   int              device;
   double           line_angle; /* Current angle of line */
};
typedef struct _Line_Data Line_Data;

struct _Line_Type /* Fields used by _line_test() */
{
   Elm_Gesture_Line_Info info;
   Eina_List            *list; /* List of Line_Data */
};
typedef struct _Line_Type Line_Type;

struct _Zoom_Type /* Fields used by _zoom_test() */
{
   Elm_Gesture_Zoom_Info   info;
   Pointer_Event           zoom_st;
   Pointer_Event           zoom_mv;
   Pointer_Event           zoom_st1;
   Pointer_Event           zoom_mv1;
   Evas_Event_Mouse_Wheel *zoom_wheel;
   Evas_Coord              zoom_base; /* Holds gap between fingers on
                                       * zoom-start  */
   Evas_Coord              zoom_distance_tolerance;
   unsigned int            m_st_tm; /* momentum start time */
   unsigned int            m_prev_tm; /* momentum prev time  */
   int                     dir; /* Direction: 1=zoom-in, (-1)=zoom-out */
   double                  m_base; /* zoom value when momentum starts */
   double                  next_step;
};
typedef struct _Zoom_Type Zoom_Type;

struct _Rotate_Type /* Fields used by _rotation_test() */
{
   Elm_Gesture_Rotate_Info info;
   Pointer_Event           rotate_st;
   Pointer_Event           rotate_mv;
   Pointer_Event           rotate_st1;
   Pointer_Event           rotate_mv1;
   unsigned int            prev_momentum_tm; /* timestamp of prev_momentum */
   double                  prev_momentum; /* Snapshot of momentum 0.01
                                           * sec ago */
   double                  accum_momentum;
   double                  rotate_angular_tolerance;
   double                  next_step;
};
typedef struct _Rotate_Type                  Rotate_Type;

typedef struct _Elm_Gesture_Layer_Data Elm_Gesture_Layer_Data;
struct _Elm_Gesture_Layer_Data
{
   Evas_Object          *target; /* Target Widget */
   Event_History        *event_history_list;

   int                   line_min_length;
   Evas_Coord            zoom_distance_tolerance;
   Evas_Coord            line_distance_tolerance;
   double                line_angular_tolerance;
   double                zoom_wheel_factor; /* mouse wheel zoom steps */
   double                zoom_finger_factor; /* used for zoom factor */
   double                rotate_angular_tolerance;
   unsigned int          flick_time_limit_ms;
   double                long_tap_start_timeout;
   Eina_Bool             glayer_continues_enable;
   double                double_tap_timeout;

   double                zoom_step;
   double                rotate_step;

   Gesture_Info         *gesture[ELM_GESTURE_LAST];
   Eina_List            *pending; /* List of devices need to refeed
                                   * *UP event */
   Eina_List            *touched; /* Information  of touched devices */

   /* Taps Gestures */
   Evas_Coord           tap_finger_size;    /* Default from Config */
   Ecore_Timer          *gest_taps_timeout; /* When this expires, dbl
                                             * click/taps ABORTed  */

   Eina_Bool             repeat_events : 1;
};

/* START - Functions to manage touched-device list */
/**
 * @internal
 * This function is used to find if device is touched
 *
 * @ingroup Elm_Gesture_Layer
 */
static int
_device_compare(const void *data1,
                const void *data2)
{
   /* Compare the two device numbers */
   return ((Pointer_Event *)data1)->device - ((Pointer_Event *)data2)->device;
}

/**
 * @internal
 *
 * Remove Pointer Event from touched device list
 * @param list Pointer to touched device list.
 * @param Pointer_Event Pointer to PE.
 *
 * @ingroup Elm_Gesture_Layer
 */
static Eina_List *
_touched_device_remove(Eina_List *list,
                       Pointer_Event *pe)
{
   Eina_List *lst = NULL;
   Pointer_Event *p = eina_list_search_unsorted(list, _device_compare, pe);
   if (p)
     {
        lst = eina_list_remove(list, p);
        free(p);
        return lst;
     }

   return list;
}

/**
 * @internal
 *
 * Recoed Pointer Event in touched device list
 * Note: This fuction allocates memory for PE event
 * This memory is released in _touched_device_remove()
 * @param list Pointer to touched device list.
 * @param Pointer_Event Pointer to PE.
 *
 * @ingroup Elm_Gesture_Layer
 */
static Eina_List *
_touched_device_add(Eina_List *list,
                    Pointer_Event *pe)
{
   Pointer_Event *p = eina_list_search_unsorted(list, _device_compare, pe);

   if (p) /* We like to track device touch-position, overwrite info */
     {
        memcpy(p, pe, sizeof(Pointer_Event));
        return list;
     }

   if ((pe->event_type == EVAS_CALLBACK_MOUSE_DOWN) ||
       (pe->event_type == EVAS_CALLBACK_MULTI_DOWN)) /* Add touched
                                                      * device on DOWN
                                                      * event only */
     {
        p = malloc(sizeof(Pointer_Event));
        /* Freed in _touched_device_remove()    */
        memcpy(p, pe, sizeof(Pointer_Event));
        return eina_list_append(list, p);
     }

   return list;
}

/* END   - Functions to manage touched-device list */

/**
 * @internal
 *
 * Get event flag
 * @param event_info pointer to event.
 *
 * @ingroup Elm_Gesture_Layer
 */
static Evas_Event_Flags
_event_flag_get(void *event_info,
                Evas_Callback_Type event_type)
{
   switch (event_type)
     {
      case EVAS_CALLBACK_MOUSE_IN:
        return ((Evas_Event_Mouse_In *)event_info)->event_flags;

      case EVAS_CALLBACK_MOUSE_OUT:
        return ((Evas_Event_Mouse_Out *)event_info)->event_flags;

      case EVAS_CALLBACK_MOUSE_DOWN:
        return ((Evas_Event_Mouse_Down *)event_info)->event_flags;

      case EVAS_CALLBACK_MOUSE_MOVE:
        return ((Evas_Event_Mouse_Move *)event_info)->event_flags;

      case EVAS_CALLBACK_MOUSE_UP:
        return ((Evas_Event_Mouse_Up *)event_info)->event_flags;

      case EVAS_CALLBACK_MOUSE_WHEEL:
        return ((Evas_Event_Mouse_Wheel *)event_info)->event_flags;

      case EVAS_CALLBACK_MULTI_DOWN:
        return ((Evas_Event_Multi_Down *)event_info)->event_flags;

      case EVAS_CALLBACK_MULTI_MOVE:
        return ((Evas_Event_Multi_Move *)event_info)->event_flags;

      case EVAS_CALLBACK_MULTI_UP:
        return ((Evas_Event_Multi_Up *)event_info)->event_flags;

      case EVAS_CALLBACK_KEY_DOWN:
        return ((Evas_Event_Key_Down *)event_info)->event_flags;

      case EVAS_CALLBACK_KEY_UP:
        return ((Evas_Event_Key_Up *)event_info)->event_flags;

      default:
        return EVAS_EVENT_FLAG_NONE;
     }
}

/**
 * @internal
 *
 * Sets event flag to value returned from user callback
 * @param sd Widget Data
 * @param event_info pointer to event.
 * @param event_type what type was ev (mouse down, etc...)
 * @param ev_flags event flags
 *
 * @ingroup Elm_Gesture_Layer
 */
static void
_event_consume(Elm_Gesture_Layer_Data *sd,
               void *event_info,
               Evas_Callback_Type event_type,
               Evas_Event_Flags ev_flags)
{
   /* Mark EVAS_EVENT_FLAG_ON_HOLD on events that are used by gesture layer */
   /* ev_flags != EVAS_EVENT_FLAG_NONE means target used event and g-layer  */
   /* should not refeed this event.  */
   if (!event_info)
     return;  /* This happens when restarting gestures  */

   if (!sd->repeat_events) ev_flags |= EVAS_EVENT_FLAG_ON_HOLD;

   if (ev_flags)
     {
        switch (event_type)
          {
           case EVAS_CALLBACK_MOUSE_DOWN:
             ((Evas_Event_Mouse_Down *)event_info)->event_flags |= ev_flags;
             break;

           case EVAS_CALLBACK_MOUSE_MOVE:
             ((Evas_Event_Mouse_Move *)event_info)->event_flags |= ev_flags;
             break;

           case EVAS_CALLBACK_MOUSE_UP:
             ((Evas_Event_Mouse_Up *)event_info)->event_flags |= ev_flags;
             break;

           case EVAS_CALLBACK_MOUSE_WHEEL:
             ((Evas_Event_Mouse_Wheel *)event_info)->event_flags |= ev_flags;
             break;

           case EVAS_CALLBACK_MULTI_DOWN:
             ((Evas_Event_Multi_Down *)event_info)->event_flags |= ev_flags;
             break;

           case EVAS_CALLBACK_MULTI_MOVE:
             ((Evas_Event_Multi_Move *)event_info)->event_flags |= ev_flags;
             break;

           case EVAS_CALLBACK_MULTI_UP:
             ((Evas_Event_Multi_Up *)event_info)->event_flags |= ev_flags;
             break;

           case EVAS_CALLBACK_KEY_DOWN:
             ((Evas_Event_Key_Down *)event_info)->event_flags |= ev_flags;
             break;

           case EVAS_CALLBACK_KEY_UP:
             ((Evas_Event_Key_Up *)event_info)->event_flags |= ev_flags;
             break;

           default:
             return;
          }
     }
}

/**
 * @internal
 *
 * Report current state of a gesture by calling user callback.
 * @param gesture what gesture state we report.
 * @param info inforamtion for user callback
 *
 * @ingroup Elm_Gesture_Layer
 */
static Evas_Event_Flags
_state_report(Gesture_Info *gesture,
              void *info)
{
   Evas_Event_Flags flags = EVAS_EVENT_FLAG_NONE;
   /* We report current state (START, MOVE, END, ABORT), once */
   if ((gesture->state != ELM_GESTURE_STATE_UNDEFINED) &&
       (gesture->cbs[gesture->state])) /* Fill state-info struct and
                                        * send ptr to user
                                        * callback */
     {
        Func_Data *cb_info;
        EINA_INLIST_FOREACH(gesture->cbs[gesture->state], cb_info)
           flags |= cb_info->cb(cb_info->user_data, info);
     }

   return EVAS_EVENT_FLAG_NONE;
}

/**
 * @internal
 *
 * Update state for a given gesture.
 * We may update gesture state to:
 * - @c UNDEFINED - current input did not start gesture yet.
 * - @c START - gesture started according to input.
 * - @c MOVE - gesture in progress.
 * - @c END - gesture completed according to input.
 * - @c ABORT - input does not matches gesture.
 * note that we may move from UNDEFINED to ABORT
 * because we may detect that gesture will not START
 * with a given input.
 *
 * @param g given gesture to change state.
 * @param s gesture new state.
 * @param info buffer to be sent to user callback on report_state.
 * @param force makes report_state to report the new-state even
 * if its same as current state. Works for MOVE - gesture in progress.
 *
 * @ingroup Elm_Gesture_Layer
 */
static Evas_Event_Flags
_state_set(Gesture_Info *g,
           Elm_Gesture_State s,
           void *info,
           Eina_Bool force)
{
   Elm_Gesture_State old_state;

   if ((g->state == s) && (!force))
     return EVAS_EVENT_FLAG_NONE;

   old_state = g->state;

   g->state = s;
   g->info = info;  /* Information for user callback */
   if ((g->state == ELM_GESTURE_STATE_ABORT) ||
       (g->state == ELM_GESTURE_STATE_END))
     g->test = EINA_FALSE;

   if ((g->state != ELM_GESTURE_STATE_UNDEFINED) &&
       (!((old_state == ELM_GESTURE_STATE_UNDEFINED) &&
          (s == ELM_GESTURE_STATE_ABORT))))
     return _state_report(g, g->info);

   return EVAS_EVENT_FLAG_NONE;
}

/**
 * @internal
 *
 * This resets all gesture states and sets test-bit.
 * this is used for restarting gestures to listen to input.
 * happens after we complete a gesture or no gesture was detected.
 * @param sd Widget data of the gesture-layer object.
 *
 * @ingroup Elm_Gesture_Layer
 */
static void
_states_reset(Elm_Gesture_Layer_Data *sd)
{
   int i;
   Gesture_Info *p;

   for (i = ELM_GESTURE_FIRST; i < ELM_GESTURE_LAST; i++)
     {
        p = sd->gesture[i];
        if (p)
          {
             _state_set(p, ELM_GESTURE_STATE_UNDEFINED, NULL, EINA_FALSE);
             SET_TEST_BIT(p);
          }
     }
}

/**
 * @internal
 *
 * This function is used to save input events in an abstract struct
 * to be used later by getsure-testing functions.
 *
 * @param data The gesture-layer object.
 * @param event_info Pointer to recent input event.
 * @param event_type Recent input event type.
 * @param pe The abstract data-struct (output).
 *
 * @ingroup Elm_Gesture_Layer
 */
static Eina_Bool
_pointer_event_make(void *data EINA_UNUSED,
                    void *event_info,
                    Evas_Callback_Type event_type,
                    Pointer_Event *pe)
{
   memset(pe, '\0', sizeof(*pe));
   switch (event_type)
     {
      case EVAS_CALLBACK_MOUSE_DOWN:
        pe->x = ((Evas_Event_Mouse_Down *)event_info)->canvas.x;
        pe->y = ((Evas_Event_Mouse_Down *)event_info)->canvas.y;
        pe->timestamp = ((Evas_Event_Mouse_Down *)event_info)->timestamp;
        pe->device = ELM_MOUSE_DEVICE;
        break;

      case EVAS_CALLBACK_MOUSE_UP:
        pe->x = ((Evas_Event_Mouse_Up *)event_info)->canvas.x;
        pe->y = ((Evas_Event_Mouse_Up *)event_info)->canvas.y;
        pe->timestamp = ((Evas_Event_Mouse_Up *)event_info)->timestamp;
        pe->device = ELM_MOUSE_DEVICE;
        break;

      case EVAS_CALLBACK_MOUSE_MOVE:
        pe->x = ((Evas_Event_Mouse_Move *)event_info)->cur.canvas.x;
        pe->y = ((Evas_Event_Mouse_Move *)event_info)->cur.canvas.y;
        pe->timestamp = ((Evas_Event_Mouse_Move *)event_info)->timestamp;
        pe->device = ELM_MOUSE_DEVICE;
        break;

      case EVAS_CALLBACK_MULTI_DOWN:
        pe->x = ((Evas_Event_Multi_Down *)event_info)->canvas.x;
        pe->y = ((Evas_Event_Multi_Down *)event_info)->canvas.y;
        pe->timestamp = ((Evas_Event_Multi_Down *)event_info)->timestamp;
        pe->device = ((Evas_Event_Multi_Down *)event_info)->device;
        break;

      case EVAS_CALLBACK_MULTI_UP:
        pe->x = ((Evas_Event_Multi_Up *)event_info)->canvas.x;
        pe->y = ((Evas_Event_Multi_Up *)event_info)->canvas.y;
        pe->timestamp = ((Evas_Event_Multi_Up *)event_info)->timestamp;
        pe->device = ((Evas_Event_Multi_Up *)event_info)->device;
        break;

      case EVAS_CALLBACK_MULTI_MOVE:
        pe->x = ((Evas_Event_Multi_Move *)event_info)->cur.canvas.x;
        pe->y = ((Evas_Event_Multi_Move *)event_info)->cur.canvas.y;
        pe->timestamp = ((Evas_Event_Multi_Move *)event_info)->timestamp;
        pe->device = ((Evas_Event_Multi_Move *)event_info)->device;
        break;

      default:
        return EINA_FALSE;
     }

   pe->event_type = event_type;
   return EINA_TRUE;
}

/**
 * @internal
 *
 * This function copies input events.
 * We copy event info before adding it to history.
 * The memory is freed when we clear history.
 *
 * @param event the event to copy
 * @param event_type event type to copy
 *
 * @ingroup Elm_Gesture_Layer
 */
static void *
_event_info_copy(void *event,
                 Evas_Callback_Type event_type)
{
   switch (event_type)
     {
      case EVAS_CALLBACK_MOUSE_DOWN:
        return COPY_EVENT_INFO((Evas_Event_Mouse_Down *)event);
        break;

      case EVAS_CALLBACK_MOUSE_MOVE:
        return COPY_EVENT_INFO((Evas_Event_Mouse_Move *)event);
        break;

      case EVAS_CALLBACK_MOUSE_UP:
        return COPY_EVENT_INFO((Evas_Event_Mouse_Up *)event);
        break;

      case EVAS_CALLBACK_MOUSE_WHEEL:
        return COPY_EVENT_INFO((Evas_Event_Mouse_Wheel *)event);
        break;

      case EVAS_CALLBACK_MULTI_DOWN:
        return COPY_EVENT_INFO((Evas_Event_Multi_Down *)event);
        break;

      case EVAS_CALLBACK_MULTI_MOVE:
        return COPY_EVENT_INFO((Evas_Event_Multi_Move *)event);
        break;

      case EVAS_CALLBACK_MULTI_UP:
        return COPY_EVENT_INFO((Evas_Event_Multi_Up *)event);
        break;

      case EVAS_CALLBACK_KEY_DOWN:
        return COPY_EVENT_INFO((Evas_Event_Key_Down *)event);
        break;

      case EVAS_CALLBACK_KEY_UP:
        return COPY_EVENT_INFO((Evas_Event_Key_Up *)event);
        break;

      default:
        return NULL;
     }
}

static Eina_Bool
_event_history_add(Evas_Object *obj,
                   void *event,
                   Evas_Callback_Type event_type)
{
   Event_History *ev;

   ELM_GESTURE_LAYER_DATA_GET(obj, sd);

   ev = malloc(sizeof(Event_History));
   if (!ev) return EINA_FALSE;

   ev->event = _event_info_copy(event, event_type);  /* Freed on
                                                      * _event_history_clear */
   ev->event_type = event_type;
   sd->event_history_list = (Event_History *)eina_inlist_append(
       EINA_INLIST_GET(sd->event_history_list), EINA_INLIST_GET(ev));

   return EINA_TRUE;
}

/**
 * For all _mouse_* / multi_* functions wethen send this event to
 * _event_process function.
 *
 * @param data The gesture-layer object.
 * @param event_info Pointer to recent input event.
 *
 * @ingroup Elm_Gesture_Layer
 */
static void
_mouse_down_cb(void *data,
               Evas *e EINA_UNUSED,
               Evas_Object *obj EINA_UNUSED,
               void *event_info)
{
   if (((Evas_Event_Mouse_Down *)event_info)->button != 1)
     return;  /* We only process left-click at the moment */

   _event_process(data, obj, event_info, EVAS_CALLBACK_MOUSE_DOWN);
}

static void
_mouse_move_cb(void *data,
               Evas *e EINA_UNUSED,
               Evas_Object *obj EINA_UNUSED,
               void *event_info)
{
   _event_process(data, obj, event_info, EVAS_CALLBACK_MOUSE_MOVE);
}

static void
_key_down_cb(void *data,
             Evas *e EINA_UNUSED,
             Evas_Object *obj EINA_UNUSED,
             void *event_info)
{
   _event_process(data, obj, event_info, EVAS_CALLBACK_KEY_DOWN);
}

static void
_key_up_cb(void *data,
           Evas *e EINA_UNUSED,
           Evas_Object *obj EINA_UNUSED,
           void *event_info)
{
   _event_process(data, obj, event_info, EVAS_CALLBACK_KEY_UP);
}

static void
_mouse_up_cb(void *data,
             Evas *e EINA_UNUSED,
             Evas_Object *obj EINA_UNUSED,
             void *event_info)
{
   if (((Evas_Event_Mouse_Up *)event_info)->button != 1)
     return;  /* We only process left-click at the moment */

   _event_process(data, obj, event_info, EVAS_CALLBACK_MOUSE_UP);
}

static void
_mouse_wheel_cb(void *data,
                Evas *e EINA_UNUSED,
                Evas_Object *obj EINA_UNUSED,
                void *event_info)
{
   _event_process(data, obj, event_info, EVAS_CALLBACK_MOUSE_WHEEL);
}

static void
_multi_down_cb(void *data,
               Evas *e EINA_UNUSED,
               Evas_Object *obj EINA_UNUSED,
               void *event_info)
{
   /* Skip the mouse duplicates. */
   if (((Evas_Event_Multi_Down *) event_info)->device == 0) return;

   _event_process(data, obj, event_info, EVAS_CALLBACK_MULTI_DOWN);
}

static void
_multi_move_cb(void *data,
               Evas *e EINA_UNUSED,
               Evas_Object *obj EINA_UNUSED,
               void *event_info)
{
   /* Skip the mouse duplicates. */
   if (((Evas_Event_Multi_Move *) event_info)->device == 0) return;

   _event_process(data, obj, event_info, EVAS_CALLBACK_MULTI_MOVE);
}

static void
_multi_up_cb(void *data,
             Evas *e EINA_UNUSED,
             Evas_Object *obj EINA_UNUSED,
             void *event_info)
{
   /* Skip the mouse duplicates. */
   if (((Evas_Event_Multi_Up *) event_info)->device == 0) return;

   _event_process(data, obj, event_info, EVAS_CALLBACK_MULTI_UP);
}

static void
_target_del_cb(void *data,
               Evas *e EINA_UNUSED,
               Evas_Object *obj EINA_UNUSED,
               void *event_info EINA_UNUSED)
{
   _callbacks_unregister(data);
   ELM_GESTURE_LAYER_DATA_GET(data, sd);
   sd->target = NULL;
}

/**
 * @internal
 *
 * We register callbacks when gesture layer is attached to an object
 * or when its enabled after disable.
 *
 * @param obj The gesture-layer object.
 *
 * @ingroup Elm_Gesture_Layer
 */
static void
_callbacks_register(Evas_Object *obj)
{
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);

   if (!sd->target) return;

   evas_object_event_callback_add
     (sd->target, EVAS_CALLBACK_MOUSE_DOWN, _mouse_down_cb, obj);
   evas_object_event_callback_add
     (sd->target, EVAS_CALLBACK_MOUSE_MOVE, _mouse_move_cb, obj);
   evas_object_event_callback_add
     (sd->target, EVAS_CALLBACK_MOUSE_UP, _mouse_up_cb, obj);

   evas_object_event_callback_add
     (sd->target, EVAS_CALLBACK_MOUSE_WHEEL, _mouse_wheel_cb, obj);

   evas_object_event_callback_add
     (sd->target, EVAS_CALLBACK_MULTI_DOWN, _multi_down_cb, obj);
   evas_object_event_callback_add
     (sd->target, EVAS_CALLBACK_MULTI_MOVE, _multi_move_cb, obj);
   evas_object_event_callback_add
     (sd->target, EVAS_CALLBACK_MULTI_UP, _multi_up_cb, obj);

   evas_object_event_callback_add
     (sd->target, EVAS_CALLBACK_KEY_DOWN, _key_down_cb, obj);
   evas_object_event_callback_add
     (sd->target, EVAS_CALLBACK_KEY_UP, _key_up_cb, obj);

   evas_object_event_callback_add
     (sd->target, EVAS_CALLBACK_DEL, _target_del_cb, obj);
}

/**
 * @internal
 *
 * We unregister callbacks when gesture layer is disabled.
 *
 * @param obj The gesture-layer object.
 *
 * @ingroup Elm_Gesture_Layer
 */
static void
_callbacks_unregister(Evas_Object *obj)
{
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);

   if (!sd->target) return;

   evas_object_event_callback_del_full
     (sd->target, EVAS_CALLBACK_MOUSE_DOWN, _mouse_down_cb, obj);
   evas_object_event_callback_del_full
     (sd->target, EVAS_CALLBACK_MOUSE_MOVE, _mouse_move_cb, obj);
   evas_object_event_callback_del_full
     (sd->target, EVAS_CALLBACK_MOUSE_UP, _mouse_up_cb, obj);

   evas_object_event_callback_del_full
     (sd->target, EVAS_CALLBACK_MOUSE_WHEEL, _mouse_wheel_cb, obj);

   evas_object_event_callback_del_full
     (sd->target, EVAS_CALLBACK_MULTI_DOWN, _multi_down_cb, obj);

   evas_object_event_callback_del_full
     (sd->target, EVAS_CALLBACK_MULTI_MOVE, _multi_move_cb, obj);

   evas_object_event_callback_del_full
     (sd->target, EVAS_CALLBACK_MULTI_UP, _multi_up_cb, obj);

   evas_object_event_callback_del_full
     (sd->target, EVAS_CALLBACK_KEY_DOWN, _key_down_cb, obj);
   evas_object_event_callback_del_full
     (sd->target, EVAS_CALLBACK_KEY_UP, _key_up_cb, obj);

   evas_object_event_callback_del_full
     (sd->target, EVAS_CALLBACK_DEL, _target_del_cb, obj);
}

/**
 * @internal
 * This function is used to find if device number
 * is found in a list of devices.
 * The list contains devices for refeeding *UP event
 *
 * @ingroup Elm_Gesture_Layer
 */
static int
_device_in_pending_cmp(const void *data1,
                       const void *data2)
{
   /* Compare the two device numbers */
   return ((intptr_t)data1) - ((intptr_t)data2);
}

/**
 * @internal
 *
 * This functions returns pending-device node
 * @ingroup Elm_Gesture_Layer
 */
static Eina_List *
_device_is_pending(Eina_List *list,
                   void *event,
                   Evas_Callback_Type event_type)
{
   int device = ELM_MOUSE_DEVICE;

   switch (event_type)
     {
      case EVAS_CALLBACK_MOUSE_UP:
        break;

      case EVAS_CALLBACK_MULTI_UP:
        device = ((Evas_Event_Multi_Up *)event)->device;
        break;

      default:
        return NULL;
     }

   return eina_list_search_unsorted_list
            (list, _device_in_pending_cmp, (void *)(intptr_t)device);
}

/**
 * @internal
 *
 * This functions adds device to refeed-pending device list
 * @ingroup Elm_Gesture_Layer
 */
static Eina_List *
_pending_device_add(Eina_List *list,
                    void *event,
                    Evas_Callback_Type event_type)
{
   int device = ELM_MOUSE_DEVICE;

   switch (event_type)
     {
      case EVAS_CALLBACK_MOUSE_DOWN:
        break;

      case EVAS_CALLBACK_MULTI_DOWN:
        device = ((Evas_Event_Multi_Down *)event)->device;
        break;

      default:
        return list;
     }

   if (!eina_list_search_unsorted_list
         (list, _device_in_pending_cmp, (void *)(intptr_t)device))
     {
        return eina_list_append(list, (void *)(intptr_t)device);
     }

   return list;
}

/**
 * @internal
 *
 * This function reports ABORT to all none-detected gestures
 * Then resets test bits for all desired gestures
 * and clears input-events history.
 * note: if no gesture was detected, events from history list
 * are streamed to the widget because it's unused by layer.
 * user may cancel refeed of events by setting repeat events.
 *
 * @param obj The gesture-layer object.
 *
 * @ingroup Elm_Gesture_Layer
 */
static Eina_Bool
_event_history_clear(Evas_Object *obj)
{
   int i;
   Gesture_Info *p;
   Evas *e = evas_object_evas_get(obj);
   Eina_Bool gesture_found = EINA_FALSE;

   ELM_GESTURE_LAYER_DATA_GET(obj, sd);

   for (i = ELM_GESTURE_FIRST; i < ELM_GESTURE_LAST; i++)
     {
        p = sd->gesture[i];
        if (p)
          {
             if (p->state == ELM_GESTURE_STATE_END)
               {
                  gesture_found = EINA_TRUE;
               }
             else
               {  /* Report ABORT to all gestures that still not finished */
                  if (sd->target)
                    _state_set(p, ELM_GESTURE_STATE_ABORT,
                          sd->gesture[i]->info, EINA_FALSE);
               }
          }
     }

   _states_reset(sd); /* we are ready to start testing for gestures again */

   /* Clear all gestures intermediate data */
   {
      /* FIXME: +1 because of the mistake in the enum. */
      Gesture_Info **gitr = sd->gesture + 1;
      Tests_Array_Funcs *fitr = _glayer_tests_array + 1;
      for (; fitr->reset; fitr++, gitr++)
        {
           if (IS_TESTED_GESTURE(*gitr))
             fitr->reset(*gitr);
        }
   }

   /* Disable gesture layer so refeeded events won't be consumed by it */
   _callbacks_unregister(obj);
   while (sd->event_history_list)
     {
        Event_History *t;
        t = sd->event_history_list;
        Eina_List *pending = _device_is_pending
            (sd->pending, sd->event_history_list->event,
            sd->event_history_list->event_type);

        /* Refeed events if no gesture matched input */
        if (pending || ((!gesture_found) && (!sd->repeat_events)))
          {
             evas_event_refeed_event(e, sd->event_history_list->event,
                                     sd->event_history_list->event_type);

             if (pending)
               {
                  sd->pending = eina_list_remove_list(sd->pending, pending);
               }
             else
               {
                  sd->pending = _pending_device_add
                      (sd->pending, sd->event_history_list->event,
                      sd->event_history_list->event_type);
               }
          }

        free(sd->event_history_list->event);
        sd->event_history_list = (Event_History *)eina_inlist_remove(
            EINA_INLIST_GET(sd->event_history_list),
            EINA_INLIST_GET(sd->event_history_list));
        free(t);
     }
   _callbacks_register(obj);
   return EINA_TRUE;
}

/**
 * @internal
 *
 * if gesture was NOT detected AND we only have gestures in ABORT state
 * we clear history immediately to be ready for input.
 *
 * @param obj The gesture-layer object.
 * @return TRUE on event history_clear
 *
 * @ingroup Elm_Gesture_Layer
 */
static Eina_Bool
_clear_if_finished(Evas_Object *obj)
{
   int i;
   Eina_Bool reset_s = EINA_TRUE, all_undefined = EINA_TRUE;

   ELM_GESTURE_LAYER_DATA_GET(obj, sd);

   /* Clear history if all we have aborted gestures */
   for (i = ELM_GESTURE_FIRST; i < ELM_GESTURE_LAST; i++)
     {  /* If no gesture started and all we have aborted gestures, reset all */
       Gesture_Info *p = sd->gesture[i];

       if ((p) && (p->state != ELM_GESTURE_STATE_UNDEFINED))
         {
            if ((p->state == ELM_GESTURE_STATE_START) ||
                (p->state == ELM_GESTURE_STATE_MOVE))
              reset_s = EINA_FALSE;

            all_undefined = EINA_FALSE;
         }
     }

   if (reset_s && (!all_undefined))
     return _event_history_clear(obj);

   return EINA_FALSE;
}

/**
 * @internal
 *
 * This function restarts line, flick, zoom and rotate gestures
 * when gesture-layer continues-gestures enabled.
 * Example of continues-gesture:
 * When doing a line, user stops moving finger but keeps fingers on touch.
 * This will cause line-end, then as user continues moving his finger
 * it re-starts line gesture.
 * When continue mode is disabled, user has to lift finger from touch
 * to end a gesture. Them touch-again to start a new one.
 *
 * @param data The gesture-layer object.
 * @param sd gesture layer widget data.
 * @param states_reset flag that marks gestures were reset in history clear.
 *
 * @ingroup Elm_Gesture_Layer
 */
static void
_continues_gestures_restart(void *data,
                            Eina_Bool states_reset)
{
   ELM_GESTURE_LAYER_DATA_GET(data, sd);

   /* Test all the gestures */
   {
      /* FIXME: +1 because of the mistake in the enum. */
      Gesture_Info **gitr = sd->gesture + 1;
      Tests_Array_Funcs *fitr = _glayer_tests_array + 1;
      for (; fitr->test; fitr++, gitr++)
        {
           Gesture_Info *g = *gitr;
           Eina_Bool tmp = (g) ?
             ((states_reset) || ((g->state != ELM_GESTURE_STATE_START)
                                 && (g->state != ELM_GESTURE_STATE_MOVE)))
             : EINA_FALSE;
           if (tmp && fitr->cont_reset)
             {
                fitr->cont_reset(g);
                _state_set(g, ELM_GESTURE_STATE_UNDEFINED, NULL, EINA_FALSE);
                SET_TEST_BIT(g);
             }
        }
   }
}

/**
 * @internal
 *
 * This function the core-function where input handling is done.
 * Here we get user input and stream it to gesture testing.
 * We notify user about any gestures with new state:
 * Valid states are:
 * START - gesture started.
 * MOVE - gesture is ongoing.
 * END - gesture was completed.
 * ABORT - gesture was aborted after START, MOVE (will NOT be completed)
 *
 * We also check if a gesture was detected, then reset event history
 * If no gestures were found we reset gesture test flag
 * after streaming event-history to widget.
 * (stream to the widget all events not consumed as a gesture)
 *
 * @param data The gesture-layer object.
 * @param event_info Pointer to recent input event.
 * @param event_type Recent input event type.
 *
 * @ingroup Elm_Gesture_Layer
 */
static void
_event_process(void *data,
               Evas_Object *obj EINA_UNUSED,
               void *event_info,
               Evas_Callback_Type event_type)
{
   Pointer_Event _pe;
   Pointer_Event *pe = NULL;

   ELM_GESTURE_LAYER_DATA_GET(data, sd);

   /* Start testing candidate gesture from here */
   if (_pointer_event_make(data, event_info, event_type, &_pe))
     pe = &_pe;

   /* Test all the gestures */
   {
      /* FIXME: +1 because of the mistake in the enum. */
      Gesture_Info **gitr = sd->gesture + 1;
      Tests_Array_Funcs *fitr = _glayer_tests_array + 1;
      for (; fitr->test; fitr++, gitr++)
        {
           if (IS_TESTED_GESTURE(*gitr))
             fitr->test(data, pe, event_info, event_type, (*gitr)->g_type);
        }
   }

   if (_event_flag_get(event_info, event_type) & EVAS_EVENT_FLAG_ON_HOLD)
     _event_history_add(data, event_info, event_type);

   /* we maintain list of touched devices              */
   /* We also use move to track current device x.y pos */
   if ((event_type == EVAS_CALLBACK_MOUSE_DOWN) ||
       (event_type == EVAS_CALLBACK_MULTI_DOWN) ||
       (event_type == EVAS_CALLBACK_MOUSE_MOVE) ||
       (event_type == EVAS_CALLBACK_MULTI_MOVE))
     {
        sd->touched = _touched_device_add(sd->touched, pe);
     }
   else if ((event_type == EVAS_CALLBACK_MOUSE_UP) ||
            (event_type == EVAS_CALLBACK_MULTI_UP))
     {
        sd->touched = _touched_device_remove(sd->touched, pe);
     }

   /* Report current states and clear history if needed */
   Eina_Bool states_reset = _clear_if_finished(data);
   if (sd->glayer_continues_enable)
     _continues_gestures_restart(data, states_reset);
}

static Eina_Bool
_inside(Evas_Coord xx1,
        Evas_Coord yy1,
        Evas_Coord xx2,
        Evas_Coord yy2,
        Evas_Coord w)
{
   w >>= 1; /* Use half the distance, from center to all directions */
   if (!w)  /* use system default instead */
     w = elm_config_finger_size_get() >> 1; /* Finger size divided by 2 */

   if (xx1 < (xx2 - w))
     return EINA_FALSE;

   if (xx1 > (xx2 + w))
     return EINA_FALSE;

   if (yy1 < (yy2 - w))
     return EINA_FALSE;

   if (yy1 > (yy2 + w))
     return EINA_FALSE;

   return EINA_TRUE;
}

/* All *test_reset() funcs are called to clear
 * gesture intermediate data.
 * This happens when we need to reset our tests.
 * for example when gesture is detected or all ABORTed. */
static void
_tap_gestures_test_reset(Gesture_Info *gesture)
{
   Eina_List *data;
   Pointer_Event *pe;

   EINA_SAFETY_ON_NULL_RETURN(gesture);
   ELM_GESTURE_LAYER_DATA_GET(gesture->obj, sd);

   ELM_SAFE_FREE(sd->gest_taps_timeout, ecore_timer_del);

   if (!gesture->data)
     return;

   EINA_LIST_FREE(((Taps_Type *)gesture->data)->l, data)
     EINA_LIST_FREE(data, pe)
       free(pe);

   memset(gesture->data, 0, sizeof(Taps_Type));
}

/* All *test_reset() funcs are called to clear
 * gesture intermediate data.
 * This happens when we need to reset our tests.
 * for example when gesture is detected or all ABORTed. */
static void
_n_long_tap_test_reset(Gesture_Info *gesture)
{
   Pointer_Event *p;
   Long_Tap_Type *st;

   EINA_SAFETY_ON_NULL_RETURN(gesture);
   if (!gesture->data) return;

   st = gesture->data;

   EINA_LIST_FREE(st->touched, p)
     free(p);
   st->touched = NULL;

   ELM_SAFE_FREE(st->timeout, ecore_timer_del);
   memset(gesture->data, 0, sizeof(Long_Tap_Type));
}

static void
_momentum_test_reset(Gesture_Info *gesture)
{
   EINA_SAFETY_ON_NULL_RETURN(gesture);
   if (!gesture->data) return;

   memset(gesture->data, 0, sizeof(Momentum_Type));
}

static void
_line_data_reset(Line_Data *st)
{
   if (!st)
     return;

   memset(st, 0, sizeof(Line_Data));
   st->line_angle = ELM_GESTURE_NEGATIVE_ANGLE;
}

static void
_line_test_reset(Gesture_Info *gesture)
{
   Line_Type *st;
   Line_Data *t_line;

   EINA_SAFETY_ON_NULL_RETURN(gesture);
   if (!gesture->data) return;

   st = gesture->data;

   EINA_LIST_FREE(st->list, t_line)
     free(t_line);
   st->list = NULL;
}

static void
_zoom_test_reset(Gesture_Info *gesture)
{
   Zoom_Type *st;

   EINA_SAFETY_ON_NULL_RETURN(gesture);
   if (!gesture->data) return;
   ELM_GESTURE_LAYER_DATA_GET(gesture->obj, sd);

   st = gesture->data;
   efl_canvas_object_key_ungrab(sd->target, "Control_L", EFL_INPUT_MODIFIER_CONTROL, 0);
   efl_canvas_object_key_ungrab(sd->target, "Control_R", EFL_INPUT_MODIFIER_CONTROL, 0);

   memset(st, 0, sizeof(Zoom_Type));
   st->zoom_distance_tolerance = sd->zoom_distance_tolerance;
   st->info.zoom = 1.0;
}

static void
_rotate_test_reset(Gesture_Info *gesture)
{
   Rotate_Type *st;

   EINA_SAFETY_ON_NULL_RETURN(gesture);
   if (!gesture->data) return;

   ELM_GESTURE_LAYER_DATA_GET(gesture->obj, sd);
   st = gesture->data;

   memset(st, 0, sizeof(Rotate_Type));
   st->info.base_angle = ELM_GESTURE_NEGATIVE_ANGLE;
   st->rotate_angular_tolerance = sd->rotate_angular_tolerance;
}

static Eina_List *
_match_fingers_compare(Eina_List *list,
                       Pointer_Event *pe1,
                       Evas_Coord w)
{
    /* Compare coords of first item in list to cur coords */
   Eina_List *pe_list;
   Eina_List *l;

   EINA_LIST_FOREACH(list, l, pe_list)
     {
        Pointer_Event *pe2 = eina_list_data_get(pe_list);

        if (_inside(pe1->x, pe1->y, pe2->x, pe2->y, w))
          return pe_list;
     }

  return NULL;
}

static int
_pe_device_compare(const void *data1,
                   const void *data2)
{
   /* Compare device of first item in list to our pe device */
   const Pointer_Event *pe1 = eina_list_data_get(data1);
   const Pointer_Event *pe2 = data2;

   if (pe1->device == pe2->device)
     return 0;
   else if (pe1->device < pe2->device)
     return -1;
   else
     return 1;
}

static Eina_List *
_pointer_event_record(Taps_Type *st,
                      Eina_List *pe_list,
                      Pointer_Event *pe,
                      Elm_Gesture_Layer_Data *sd,
                      void *event_info,
                      Evas_Callback_Type event_type)
{
   /* Keep copy of pe and record it in list */
   Pointer_Event *p = malloc(sizeof(Pointer_Event));

   memcpy(p, pe, sizeof(Pointer_Event));
   _event_consume(sd, event_info, event_type, EVAS_EVENT_FLAG_NONE);

   st->sum_x += pe->x;
   st->sum_y += pe->y;
   st->n_taps++;

   /* This will also update middle-point to report to user later */
   st->info.x = st->sum_x / st->n_taps;
   st->info.y = st->sum_y / st->n_taps;
   st->info.timestamp = pe->timestamp;

   if (!pe_list)
     {
        pe_list = eina_list_append(pe_list, p);
        st->l = eina_list_append(st->l, pe_list);
     }
   else
     pe_list = eina_list_append(pe_list, p);

   return pe_list;
}

/**
 * @internal
 *
 * This function computes minimum rect to bound taps at idx index
 *
 * @param taps [in] List of lists containing taps info.
 * @param idx [in] index of events taken from lists.
 * @param r [out] rect object to save info
 * @return EINA_TRUE if managed to compute rect.
 *
 * @ingroup Elm_Gesture_Layer
 */
static Eina_Bool
_taps_rect_get(Eina_List *taps, int idx, Eina_Rectangle *r)
{  /* Build a rect bounding all taps at index idx */
   Eina_List *l;
   Evas_Coord bx = 0, by = 0;
   Eina_List *pe_list;
   Eina_Bool was_init = EINA_FALSE;

   EINA_LIST_FOREACH(taps, l, pe_list)
     {
        Pointer_Event *pe = eina_list_nth(pe_list, idx);
        if (!pe) continue;  /* Not suppose to happen */

        if (was_init)
          {
             if (pe->x < r->x) r->x = pe->x;
             if (pe->y < r->y) r->y = pe->y;
             if (pe->x > bx) bx = pe->x;
             if (pe->y > by) by = pe->y;
          }
        else
          {
             r->x = bx = pe->x;
             r->y = by = pe->y;
             was_init = EINA_TRUE;
          }
     }

   r->w = bx - r->x;
   r->h = by - r->y;
   return was_init;
}

/**
 * @internal
 *
 * This function checks if the tap gesture is done.
 *
 * @param data gesture info pointer
 * @return EINA_TRUE if it is done.
 *
 * @ingroup Elm_Gesture_Layer
 */
static Eina_Bool
_tap_gesture_check_finish(Gesture_Info *gesture)
{
   /* Here we check if taps-gesture was completed successfully */
   /* Count how many taps were received on each device then   */
   /* determine if it matches n_taps_needed defined on START  */
   unsigned int i;
   Taps_Type *st = gesture->data;
   Eina_List *l;
   Eina_List *pe_list;
   Eina_Rectangle base = {0, 0, 0, 0};
   Eina_Rectangle tmp = {0, 0, 0, 0};

   ELM_GESTURE_LAYER_DATA_GET(gesture->obj, sd);

   if (!st->l) return EINA_FALSE;
   EINA_LIST_FOREACH(st->l, l, pe_list)
     {
        /* No match taps number on device, ABORT */
        if (eina_list_count(pe_list) != st->n_taps_needed)
          {
             return EINA_FALSE;
          }
     }

   /* Now bound each tap touches in a rect, compare diff within tolerance */
   /* Get rect based on first DOWN events for all devices */
   if (!_taps_rect_get(st->l, 0, &base))
     return EINA_FALSE;  /* Should not happen */

   for (i = 1; i < st->n_taps_needed; i++)
     {  /* Compare all other rects to base, tolerance is finger size */
        if (_taps_rect_get(st->l, i, &tmp))
          {
             if (abs(tmp.x - base.x) > sd->tap_finger_size)
               return EINA_FALSE;

             if (abs(tmp.y - base.y) > sd->tap_finger_size)
               return EINA_FALSE;

             if (abs((tmp.x + tmp.w) - (base.x + base.w)) > sd->tap_finger_size)
               return EINA_FALSE;

             if (abs((tmp.y + tmp.h) - (base.y + base.h)) > sd->tap_finger_size)
               return EINA_FALSE;
          }
     }

   return EINA_TRUE;
}

/**
 * @internal
 *
 * This function sets state a tap-gesture to END or ABORT
 *
 * @param data gesture info pointer
 *
 * @ingroup Elm_Gesture_Layer
 */
static void
_tap_gesture_finish(void *data)
{
   /* This function will test each tap gesture when timer expires */
   Elm_Gesture_State s = ELM_GESTURE_STATE_ABORT;
   Gesture_Info *gesture = data;
   Taps_Type *st = gesture->data;

   if (_tap_gesture_check_finish(gesture))
     {
        s = ELM_GESTURE_STATE_END;
     }

   st->info.n = eina_list_count(st->l);
   _state_set(gesture, s, gesture->info, EINA_FALSE);
   _tap_gestures_test_reset(gesture);
}

/**
 * @internal
 *
 * when this timer expires we finish tap gestures.
 *
 * @param data The gesture-layer object.
 * @return cancels callback for this timer.
 *
 * @ingroup Elm_Gesture_Layer
 */
static Eina_Bool
_multi_tap_timeout(void *data)
{
   ELM_GESTURE_LAYER_DATA_GET(data, sd);

   if (IS_TESTED(ELM_GESTURE_N_TAPS))
     _tap_gesture_finish(sd->gesture[ELM_GESTURE_N_TAPS]);

   if (IS_TESTED(ELM_GESTURE_N_DOUBLE_TAPS))
     _tap_gesture_finish(sd->gesture[ELM_GESTURE_N_DOUBLE_TAPS]);

   if (IS_TESTED(ELM_GESTURE_N_TRIPLE_TAPS))
     _tap_gesture_finish(sd->gesture[ELM_GESTURE_N_TRIPLE_TAPS]);

   _clear_if_finished(data);
   sd->gest_taps_timeout = NULL;

   return ECORE_CALLBACK_CANCEL;
}

/**
 * @internal
 *
 * when this timer expires we START long tap gesture
 *
 * @param data The gesture-layer object.
 * @return cancels callback for this timer.
 *
 * @ingroup Elm_Gesture_Layer
 */
static Eina_Bool
_long_tap_timeout(void *data)
{
   Gesture_Info *gesture = data;
   Long_Tap_Type *st = gesture->data;

   st->info.timestamp = ecore_time_get() * 1000;

   _state_set(gesture, ELM_GESTURE_STATE_MOVE,
              gesture->data, EINA_TRUE);

   return ECORE_CALLBACK_RENEW;
}

/**
 * @internal
 *
 * This function checks the state of a tap gesture.
 *
 * @param sd Gesture Layer Widget Data.
 * @param pe The recent input event as stored in pe struct.
 * @param event_info Original input event pointer.
 * @param event_type Type of original input event.
 * @param gesture what gesture is tested
 * @param how many taps for this gesture (1, 2 or 3)
 *
 * @ingroup Elm_Gesture_Layer
 */
static void
_tap_gesture_test(Evas_Object *obj,
                  Pointer_Event *pe,
                  void *event_info,
                  Evas_Callback_Type event_type,
                  Elm_Gesture_Type g_type)
{
   int taps = 0;
   Taps_Type *st;
   Gesture_Info *gesture;
   Eina_List *pe_list = NULL;
   Pointer_Event *pe_last = NULL;
   Evas_Event_Flags ev_flag = EVAS_EVENT_FLAG_NONE;

   /* Here we fill Tap struct */
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);

   if (!pe)
     return;

   gesture = sd->gesture[g_type];
   if (!gesture) return;

   switch (g_type)
     {
      case ELM_GESTURE_N_TAPS:
         taps = 1;
         break;

      case ELM_GESTURE_N_DOUBLE_TAPS:
         taps = 2;
         break;

      case ELM_GESTURE_N_TRIPLE_TAPS:
         taps = 3;
         break;

      default:
         taps = 0;
         break;
     }

   st = gesture->data;
   if (!st) /* Allocated once on first time */
     {
        st = calloc(1, sizeof(Taps_Type));
        if (!st) return;

        gesture->data = st;
        _tap_gestures_test_reset(gesture);
     }

   switch (pe->event_type)
     {
      case EVAS_CALLBACK_MULTI_DOWN:
      case EVAS_CALLBACK_MOUSE_DOWN:
         /* Each device taps (DOWN, UP event) registered in same list    */
         /* Find list for this device or start a new list if not found   */
         pe_list = eina_list_search_unsorted(st->l, _pe_device_compare, pe);
         if (pe_list)
           {  /* This device touched before, verify that this tap is on  */
              /* top of a previous tap (including a tap of other device) */
              if (!_match_fingers_compare(st->l, pe, sd->tap_finger_size))
                {  /* New DOWN event is not on top of any prev touch     */
                   ev_flag = _state_set(gesture, ELM_GESTURE_STATE_ABORT,
                         &st->info, EINA_FALSE);
                   _event_consume(sd, event_info, event_type, ev_flag);

                   return;
                }
           }

         /* All tests are good, register this tap in device list */
         pe_list = _pointer_event_record
            (st, pe_list, pe, sd, event_info, event_type);

         if (!sd->gest_taps_timeout)
           {
              if (sd->double_tap_timeout > 0.0)
                {
                   sd->gest_taps_timeout =
                      ecore_timer_add(sd->double_tap_timeout,
                            _multi_tap_timeout, gesture->obj);
                }
           }
         else  /* We re-allocate gest_taps_timeout between taps */
           ecore_timer_reset(sd->gest_taps_timeout);

         if ((pe->device == 0) && (eina_list_count(pe_list) == 1))
           {  /* This is the first mouse down we got */
              ev_flag = _state_set(gesture, ELM_GESTURE_STATE_START,
                    &st->info, EINA_FALSE);
              _event_consume(sd, event_info, event_type, ev_flag);

              st->n_taps_needed = taps * 2;  /* count DOWN and UP */

              return;
           }
         else if (eina_list_count(pe_list) > st->n_taps_needed)
           {  /* If we already got too many touches for this gesture. */
              _state_set(gesture, ELM_GESTURE_STATE_ABORT,
                    &st->info, EINA_FALSE);
           }

         if (gesture->state == ELM_GESTURE_STATE_MOVE)
           {  /* Report MOVE if all devices have same DOWN/UP count */
              /* Should be in MOVE state from last UP event */
              Eina_List *l;
              Eina_Bool move = EINA_TRUE;
              unsigned int n = 0;

              EINA_LIST_FOREACH(st->l, l, pe_list)
                {
                   if (n == 0)
                     {
                        n = eina_list_count(pe_list);
                     }
                   else if (n != eina_list_count(pe_list))
                     {
                        move = EINA_FALSE;
                     }
                }

              if (move && (n > 0))
                {
                   _state_set(gesture, ELM_GESTURE_STATE_MOVE,
                         &st->info, EINA_TRUE);
                }
           }

         break;

      case EVAS_CALLBACK_MULTI_UP:
      case EVAS_CALLBACK_MOUSE_UP:
         pe_list = eina_list_search_unsorted(st->l, _pe_device_compare, pe);
         if (!pe_list) return;

         _pointer_event_record(st, pe_list, pe, sd, event_info, event_type);

         if (((gesture->g_type == ELM_GESTURE_N_TAPS) &&
                  !IS_TESTED(ELM_GESTURE_N_DOUBLE_TAPS) &&
                  !IS_TESTED(ELM_GESTURE_N_TRIPLE_TAPS)) ||
               ((gesture->g_type == ELM_GESTURE_N_DOUBLE_TAPS) &&
                !IS_TESTED(ELM_GESTURE_N_TRIPLE_TAPS)))
           {  /* Test for finish immediately, not waiting for timeout */
              if (_tap_gesture_check_finish(gesture))
                {
                   _tap_gesture_finish(gesture);
                   return;
                }
           }

         if ((gesture->state == ELM_GESTURE_STATE_START) ||
               (gesture->state == ELM_GESTURE_STATE_MOVE))
           {  /* Tap gesture started, no finger on surface. Report MOVE */
              Eina_List *l;
              Eina_Bool move = EINA_TRUE;
              unsigned int n = 0;

              /* Report move only if all devices have same DOWN/UP count */
              EINA_LIST_FOREACH(st->l, l, pe_list)
                {
                   if (n == 0)
                     {
                        n = eina_list_count(pe_list);
                     }
                   else if (n != eina_list_count(pe_list))
                     {
                        move = EINA_FALSE;
                     }
                }

              if ((move && (n > 0)) && (n < st->n_taps_needed))
                {  /* Set number of fingers and report MOVE */
                   /* We don't report MOVE when (n >= st->n_taps_needed)
                      because will be END or ABORT at this stage */
                   st->info.n = eina_list_count(st->l);
                   _state_set(gesture, ELM_GESTURE_STATE_MOVE,
                         &st->info, EINA_TRUE);
                }
           }

         break;

      case EVAS_CALLBACK_MULTI_MOVE:
      case EVAS_CALLBACK_MOUSE_MOVE:
         /* Verify that user didn't move out of tap area before next tap */
         /* BUT: we need to skip some MOVE events coming before DOWN     */
         /* when user taps next tap. So fetch LAST recorded event for    */
         /* device (DOWN or UP event), ignore all MOVEs if last was UP   */
         pe_last = eina_list_data_get(eina_list_last(
                  eina_list_search_unsorted(st->l, _pe_device_compare, pe)));

         if (pe_last)
           {  /* pe_last is the last event recorded for this device */
              if ((pe_last->event_type == EVAS_CALLBACK_MOUSE_DOWN) ||
                    (pe_last->event_type == EVAS_CALLBACK_MULTI_DOWN))
                {  /* Test only MOVE events that come after DOWN event */
                   if (!_inside(pe_last->x, pe_last->y, pe->x, pe->y,
                            sd->tap_finger_size))
                     {
                        ev_flag = _state_set(gesture, ELM_GESTURE_STATE_ABORT,
                              &st->info, EINA_FALSE);
                        _event_consume(sd, event_info, event_type, ev_flag);
                     }
                }
           }
         break;

      default:
         return;
     }
}

/**
 * @internal
 *
 * This function computes center-point for  long-tap gesture
 *
 * @param st Long Tap gesture info pointer
 * @param pe The recent input event as stored in pe struct.
 *
 * @ingroup Elm_Gesture_Layer
 */
static void
_compute_taps_center(Long_Tap_Type *st,
                     Evas_Coord *x_out,
                     Evas_Coord *y_out,
                     Pointer_Event *pe)
{
   Eina_List *l;
   Pointer_Event *p;
   Evas_Coord x = 0, y = 0;

   if (!eina_list_count(st->touched))
     return;

   EINA_LIST_FOREACH(st->touched, l, p)
     {  /* Accumulate all then take average */
       if (p->device == pe->device) /* This will take care of values
                                     * coming from MOVE event */
         {
            x += pe->x;
            y += pe->y;
         }
       else
         {
            x += p->x;
            y += p->y;
         }
     }

   *x_out = x / eina_list_count(st->touched);
   *y_out = y / eina_list_count(st->touched);
}

/**
 * @internal
 *
 * This function checks N long-tap gesture.
 *
 * @param obj The gesture-layer object.
 * @param pe The recent input event as stored in pe struct.
 * @param event_info Original input event pointer.
 * @param event_type Type of original input event.
 * @param g_type what Gesture we are testing.
 * @param taps How many click/taps we test for.
 *
 * @ingroup Elm_Gesture_Layer
 */
static void
_n_long_tap_test(Evas_Object *obj,
                 Pointer_Event *pe,
                 void *event_info,
                 Evas_Callback_Type event_type,
                 Elm_Gesture_Type g_type)
{
   Evas_Event_Flags ev_flag = EVAS_EVENT_FLAG_NONE;
   Gesture_Info *gesture;
   Long_Tap_Type *st;

   /* Here we fill Recent_Taps struct and fire-up click/tap timers */
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);

   if (!pe) /* this happens when unhandled event arrived */
     return;  /* see _make_pointer_event function */

   gesture = sd->gesture[g_type];
   if (!gesture) return;

   st = gesture->data;
   if (!st) /* Allocated once on first time */
     {
        st = calloc(1, sizeof(Long_Tap_Type));
        if (!st) return;

        gesture->data = st;
        _n_long_tap_test_reset(gesture);
     }

   switch (pe->event_type)
     {
      case EVAS_CALLBACK_MULTI_DOWN:
      case EVAS_CALLBACK_MOUSE_DOWN:
        st->touched = _touched_device_add(st->touched, pe);
        st->info.n = eina_list_count(st->touched);

        _event_consume(sd, event_info, event_type, ev_flag);
        _compute_taps_center(st, &st->info.x, &st->info.y, pe);
        st->center_x = st->info.x;  /* Update coords for */
        st->center_y = st->info.y;  /* reporting START  */
        st->info.timestamp = pe->timestamp;

        /* This is the first mouse down we got */
        if (eina_list_count(st->touched) == 1)
          {
             _state_set(gesture, ELM_GESTURE_STATE_START,
                   gesture->data, EINA_FALSE);

             /* To test long tap */
             /* When this timer expires, gesture STARTED */
             if ((!st->timeout) && (sd->long_tap_start_timeout > 0.0))
               st->timeout = ecore_timer_add(sd->long_tap_start_timeout,
                                             _long_tap_timeout, gesture);
          }
        else
          {
             if (st->timeout)
                ecore_timer_reset(st->timeout);
          }

        break;

      case EVAS_CALLBACK_MULTI_UP:
      case EVAS_CALLBACK_MOUSE_UP:
        st->touched = _touched_device_remove(st->touched, pe);
        _compute_taps_center(st, &st->center_x, &st->center_y, pe);
        st->info.timestamp = pe->timestamp;
        if (st->info.n)
          {
             if (gesture->state == ELM_GESTURE_STATE_MOVE)
               ev_flag = _state_set(gesture, ELM_GESTURE_STATE_END,
                                    &st->info, EINA_FALSE);
             else
               ev_flag = _state_set(gesture, ELM_GESTURE_STATE_ABORT,
                                    &st->info, EINA_FALSE);

             ELM_SAFE_FREE(st->timeout, ecore_timer_del);
             _event_consume(sd, event_info, event_type, ev_flag);
          }

        break;

      case EVAS_CALLBACK_MULTI_MOVE:
      case EVAS_CALLBACK_MOUSE_MOVE:
        if (st->info.n &&
            ((gesture->state == ELM_GESTURE_STATE_START) ||
             /* Report MOVE only if STARTED */
             (gesture->state == ELM_GESTURE_STATE_MOVE)))
          {
             Evas_Coord x = 0;
             Evas_Coord y = 0;

             _compute_taps_center(st, &x, &y, pe);
             st->info.timestamp = pe->timestamp;
             /* ABORT if user moved fingers out of tap area */
             if (!_inside(x, y, st->center_x, st->center_y,
                      sd->tap_finger_size))
               {
                  ELM_SAFE_FREE(st->timeout, ecore_timer_del);

                  /* Report MOVE if gesture started */
                  ev_flag = _state_set(gesture, ELM_GESTURE_STATE_ABORT,
                        &st->info, EINA_FALSE);
               }

             _event_consume(sd, event_info, event_type, ev_flag);
          }
        break;

      default:
        return;
     }
}

/**
 * @internal
 *
 * This function computes momentum for MOMENTUM, LINE and FLICK gestures
 * This momentum value will be sent to widget when gesture is completed.
 *
 * @param momentum pointer to buffer where we record momentum value.
 * @param xx1 x coord where user started gesture.
 * @param yy1 y coord where user started gesture.
 * @param xx2 x coord where user completed gesture.
 * @param yy2 y coord where user completed gesture.
 * @param t1x timestamp for X, when user started gesture.
 * @param t1y timestamp for Y, when user started gesture.
 * @param t2  timestamp when user completed gesture.
 *
 * @ingroup Elm_Gesture_Layer
 */
static void
_momentum_set(Elm_Gesture_Momentum_Info *momentum,
              Evas_Coord xx1,
              Evas_Coord yy1,
              Evas_Coord xx2,
              Evas_Coord yy2,
              unsigned int t1x,
              unsigned int t1y,
              unsigned int t2)
{
   Evas_Coord velx = 0, vely = 0, vel;
   Evas_Coord dx = xx2 - xx1;
   Evas_Coord dy = yy2 - yy1;
   int dtx = t2 - t1x;
   int dty = t2 - t1y;

   if (dtx > 0)
     velx = (dx * 1000) / dtx;

   if (dty > 0)
     vely = (dy * 1000) / dty;

   vel = sqrt((velx * velx) + (vely * vely));

   if ((_elm_config->thumbscroll_friction > 0.0) &&
       (vel > _elm_config->thumbscroll_momentum_threshold)) /* report
                                                             * momentum */
     {
        momentum->mx = velx;
        momentum->my = vely;
     }
   else
     {
        momentum->mx = 0;
        momentum->my = 0;
     }
}

/**
 * @internal
 *
 * This function is used for computing rotation angle (DEG).
 *
 * @param xx1 first finger x location.
 * @param yy1 first finger y location.
 * @param xx2 second finger x location.
 * @param yy2 second finger y location.
 *
 * @return angle of the line between (xx1,yy1), (xx2,yy2) in Deg.
 * Angles now are given in DEG, not RAD.
 * ZERO angle at 12-oclock, growing clockwise.
 *
 * @ingroup Elm_Gesture_Layer
 */
static double
_angle_get(Evas_Coord xx1,
           Evas_Coord yy1,
           Evas_Coord xx2,
           Evas_Coord yy2)
{
   double a, xx, yy, rt = (-1);

   xx = abs(xx2 - xx1);
   yy = abs(yy2 - yy1);

   if (((int)xx) && ((int)yy))
     {
        rt = a = RAD2DEG(atan(yy / xx));
        if (xx1 < xx2)
          {
             if (yy1 < yy2) rt = 360 - a;
             else rt = a;
          }
        else
          {
             if (yy1 < yy2) rt = 180 + a;
             else rt = 180 - a;
          }
     }

   if (rt < 0) /* Do this only if rt is not set */
     {
        if (((int)xx)) /* Horizontal line */
          {
             if (xx2 < xx1) rt = 180;
             else rt = 0.0;
          }
        else
          {  /* Vertical line */
            if (yy2 < yy1) rt = 90;
            else rt = 270;
          }
     }

   /* Now we want to change from:
    *                      90                   0
    * original circle   180   0   We want:  270   90
    *                     270                 180
    */
   rt = 450 - rt;
   if (rt >= 360) rt -= 360;

   return rt;
}

/**
 * @internal
 *
 * This function is used for computing the magnitude and direction
 * of vector between two points.
 *
 * @param xx1 first finger x location.
 * @param yy1 first finger y location.
 * @param xx2 second finger x location.
 * @param yy2 second finger y location.
 * @param l length computed (output)
 * @param a angle computed (output)
 *
 * @ingroup Elm_Gesture_Layer
 */
static void
_vector_get(Evas_Coord xx1,
            Evas_Coord yy1,
            Evas_Coord xx2,
            Evas_Coord yy2,
            Evas_Coord *l,
            double *a)
{
   Evas_Coord xx, yy;

   xx = xx2 - xx1;
   yy = yy2 - yy1;
   *l = (Evas_Coord)sqrt((xx * xx) + (yy * yy));
   *a = _angle_get(xx1, yy1, xx2, yy2);
}

static int
_direction_get(Evas_Coord xx1,
               Evas_Coord xx2)
{
   if (xx2 < xx1) return -1;
   if (xx2 > xx1) return 1;

   return 0;
}

/**
 * @internal
 *
 * This function tests momentum gesture.
 * @param obj The gesture-layer object.
 * @param pe The recent input event as stored in pe struct.
 * @param event_info recent input event.
 * @param event_type recent event type.
 * @param g_type what Gesture we are testing.
 *
 * @ingroup Elm_Gesture_Layer
 */
static void
_momentum_test(Evas_Object *obj,
               Pointer_Event *pe,
               void *event_info,
               Evas_Callback_Type event_type,
               Elm_Gesture_Type g_type)
{
   Eina_List *l;
   Pointer_Event *p;
   Momentum_Type *st;
   Gesture_Info *gesture;
   Pointer_Event pe_local;
   Evas_Event_Flags ev_flag = EVAS_EVENT_FLAG_NONE;
   Elm_Gesture_State state_to_report = ELM_GESTURE_STATE_MOVE;
   unsigned int cnt = 1; /* We start counter counting current pe event */

   ELM_GESTURE_LAYER_DATA_GET(obj, sd);

   gesture = sd->gesture[g_type];
   if (!gesture) return;

   /* When continues enable = TRUE a gesture may START on MOVE event */
   /* We don't allow this to happen with the if-statement below.     */
   /* When continues enable = FALSE a gesture may START on DOWN only */
   /* Therefor it would NOT start on MOVE event.                     */
   /* NOTE that touched list is updated AFTER this function returns  */
   /* so (count == 0) when we get here on first touch on surface.    */
   if ((sd->glayer_continues_enable) && (!eina_list_count(sd->touched)))
     return;  /* Got move on mouse-over move */

   st = gesture->data;
   if (!st) /* Allocated once on first time */
     {
        st = calloc(1, sizeof(Momentum_Type));
        if (!st) return;

        gesture->data = st;
        _momentum_test_reset(gesture);
     }

   if (!pe)
     return;

   /* First make average of all touched devices to determine center point */
   pe_local = *pe; /* Copy pe event info to local */
   EINA_LIST_FOREACH(sd->touched, l, p)
     if (p->device != pe_local.device)
       {
          pe_local.x += p->x;
          pe_local.y += p->y;
          cnt++;
       }

   /* Compute average to get center point */
   pe_local.x /= cnt;
   pe_local.y /= cnt;

   /* If user added finger - reset gesture */
   if ((st->info.n) && (st->info.n < cnt))
     state_to_report = ELM_GESTURE_STATE_ABORT;

   if (st->info.n < cnt)
     st->info.n = cnt;

   switch (event_type)
     {
      case EVAS_CALLBACK_MOUSE_DOWN:
      case EVAS_CALLBACK_MULTI_DOWN:
      case EVAS_CALLBACK_MOUSE_MOVE:
      case EVAS_CALLBACK_MULTI_MOVE:
        if (!st->t_st_x)
          {
             if ((event_type == EVAS_CALLBACK_MOUSE_DOWN) ||
                 (event_type == EVAS_CALLBACK_MULTI_DOWN) ||
                 (sd->glayer_continues_enable))    /* start also on MOVE */
               {   /* We start on MOVE when cont-enabled only */
                 st->line_st.x = st->line_end.x = pe_local.x;
                 st->line_st.y = st->line_end.y = pe_local.y;
                 st->t_st_x = st->t_st_y = st->t_end = pe_local.timestamp;
                 st->xdir = st->ydir = 0;
                 st->info.x2 = st->info.x1 = pe_local.x;
                 st->info.y2 = st->info.y1 = pe_local.y;
                 st->info.tx = st->info.ty = pe_local.timestamp;
                 ev_flag = _state_set(gesture, ELM_GESTURE_STATE_START,
                                      &st->info, EINA_FALSE);
                 _event_consume(sd, event_info, event_type, ev_flag);
               }

             return;
          }

        if (st->t_up)
          {
             Eina_Bool force = EINA_TRUE;   /* for move state */

             /* ABORT if got DOWN or MOVE event after UP+timeout */
             if ((st->t_up + ELM_GESTURE_MULTI_TIMEOUT) < pe_local.timestamp)
               {
                  state_to_report = ELM_GESTURE_STATE_ABORT;
                  force = EINA_FALSE;
               }

             /* We report state but don't compute momentum now */
             ev_flag = _state_set(gesture, state_to_report, &st->info,
                                  force);
             _event_consume(sd, event_info, event_type, ev_flag);
             return;  /* Stop computing when user remove finger */
          }

        /*  Too long of a wait, reset all values */
        if ((pe_local.timestamp - ELM_GESTURE_MOMENTUM_TIMEOUT) > st->t_end)
          {
             st->line_st.x = pe_local.x;
             st->line_st.y = pe_local.y;
             st->t_st_y = st->t_st_x = pe_local.timestamp;
             st->info.tx = st->t_st_x;
             st->info.ty = st->t_st_y;
             st->xdir = st->ydir = 0;
          }
        else
          {
             int xdir, ydir;

             xdir = _direction_get(st->line_end.x, pe_local.x);
             ydir = _direction_get(st->line_end.y, pe_local.y);
             if (xdir && (xdir != st->xdir))
               {
                  st->line_st.x = st->line_end.x;
                  st->info.tx = st->t_st_x = st->t_end;
                  st->xdir = xdir;
               }

             if (ydir && (ydir != st->ydir))
               {
                  st->line_st.y = st->line_end.y;
                  st->info.ty = st->t_st_y = st->t_end;
                  st->ydir = ydir;
               }
          }

        st->info.x2 = st->line_end.x = pe_local.x;
        st->info.y2 = st->line_end.y = pe_local.y;
        st->t_end = pe_local.timestamp;
        _momentum_set(&st->info, st->line_st.x, st->line_st.y,
                      pe_local.x, pe_local.y, st->t_st_x, st->t_st_y,
                      pe_local.timestamp);

        ev_flag = _state_set(gesture, state_to_report, &st->info,
                             EINA_TRUE);
        _event_consume(sd, event_info, event_type, ev_flag);
        break;

      case EVAS_CALLBACK_MOUSE_UP:
      case EVAS_CALLBACK_MULTI_UP:
        st->t_up = pe_local.timestamp; /* Record recent up event time */
        if ((cnt > 1) || /* Ignore if more fingers touch surface        */
            (!st->t_st_x)) /* IGNORE if info was cleared, long press,move */
          return;

        /* Too long of a wait, reset all values */
        if ((pe_local.timestamp - ELM_GESTURE_MOMENTUM_TIMEOUT) > st->t_end)
          {
             st->line_st.x = pe_local.x;
             st->line_st.y = pe_local.y;
             st->t_st_y = st->t_st_x = pe_local.timestamp;
             st->xdir = st->ydir = 0;
          }

        st->info.x2 = pe_local.x;
        st->info.y2 = pe_local.y;
        st->line_end.x = pe_local.x;
        st->line_end.y = pe_local.y;
        st->t_end = pe_local.timestamp;

        // FIXME: mx,my are int while the momentum is float. Fishy logic here.
        if ((abs(st->info.mx) > ELM_GESTURE_MINIMUM_MOMENTUM) ||
            (abs(st->info.my) > ELM_GESTURE_MINIMUM_MOMENTUM))
          state_to_report = ELM_GESTURE_STATE_END;
        else
          state_to_report = ELM_GESTURE_STATE_ABORT;

        ev_flag = _state_set(gesture, state_to_report, &st->info,
                             EINA_FALSE);
        _event_consume(sd, event_info, event_type, ev_flag);
        return;

      default:
        return;
     }
}

static int
_line_device_compare(const void *data1,
                     const void *data2)
{
   /* Compare device component of line struct */
   const Line_Data *ln1 = data1;
   const int *device = data2;

   if (ln1->t_st) /* Compare only with lines that started */
     return ln1->device - (*device);

   return -1;
}

/**
 * @internal
 *
 * This function construct line struct from input.
 * @param info pointer to store line momentum.
 * @param st line info to store input data.
 * @param pe The recent input event as stored in pe struct.
 *
 * @ingroup Elm_Gesture_Layer
 */
static Eina_Bool
_single_line_process(Elm_Gesture_Line_Info *info,
                     Line_Data *st,
                     Pointer_Event *pe,
                     Evas_Callback_Type event_type)
{
   /* Record events and set momentum for line pointed by st */
   if (!pe)
     return EINA_FALSE;

   switch (event_type)
     {
      case EVAS_CALLBACK_MOUSE_DOWN:
      case EVAS_CALLBACK_MOUSE_MOVE:
      case EVAS_CALLBACK_MULTI_DOWN:
      case EVAS_CALLBACK_MULTI_MOVE:
        if (!st->t_st) /* This happens only when line starts */
          {
             st->line_st.x = pe->x;
             st->line_st.y = pe->y;
             st->t_st = pe->timestamp;
             st->device = pe->device;
             info->momentum.x1 = pe->x;
             info->momentum.y1 = pe->y;
             info->momentum.tx = pe->timestamp;
             info->momentum.ty = pe->timestamp;

             return EINA_TRUE;
          }

        break;

      case EVAS_CALLBACK_MOUSE_UP:
      case EVAS_CALLBACK_MULTI_UP:
        /* IGNORE if line info was cleared, like long press, move */
        if (!st->t_st)
          return EINA_FALSE;

        st->line_end.x = pe->x;
        st->line_end.y = pe->y;
        st->t_end = pe->timestamp;
        break;

      default:
        return EINA_FALSE;
     }

   if (!st->t_st)
     {
        _line_data_reset(st);
        return EINA_FALSE;
     }

   info->momentum.x2 = pe->x;
   info->momentum.y2 = pe->y;
   _momentum_set(&info->momentum, st->line_st.x, st->line_st.y, pe->x, pe->y,
                 st->t_st, st->t_st, pe->timestamp);

   return EINA_TRUE;
}

/**
 * @internal
 *
 * This function test for (n) line gesture.
 * @param obj The gesture-layer object.
 * @param pe The recent input event as stored in pe struct.
 * @param event_info Original input event pointer.
 * @param event_type Type of original input event.
 * @param g_type what Gesture we are testing.
 *
 * @ingroup Elm_Gesture_Layer
 */
static void
_n_line_test(Evas_Object *obj,
             Pointer_Event *pe,
             void *event_info,
             Evas_Callback_Type event_type,
             Elm_Gesture_Type g_type)
{
   unsigned cnt;
   Line_Type *st;
   Eina_List *list;
   Gesture_Info *gesture;
   Line_Data *line = NULL;

   if (!pe)
     return;

   ELM_GESTURE_LAYER_DATA_GET(obj, sd);

   gesture = sd->gesture[g_type];
   if (!gesture ) return;

   /* When continues enable = TRUE a gesture may START on MOVE event */
   /* We don't allow this to happen with the if-statement below.     */
   /* When continues enable = FALSE a gesture may START on DOWN only */
   /* Therefor it would NOT start on MOVE event.                     */
   /* NOTE that touched list is updated AFTER this function returns  */
   /* so (count == 0) when we get here on first touch on surface.    */
   if ((sd->glayer_continues_enable) && (!eina_list_count(sd->touched)))
     return;  /* Got move on mouse-over move */

   st = gesture->data;
   if (!st)
     {
        st = calloc(1, sizeof(Line_Type));
        if (!st) return;

        gesture->data = st;
     }

   list = st->list;
   cnt = eina_list_count(list);

   if (cnt) /* list is not empty, locate this device on list */
     {
        line = (Line_Data *)eina_list_search_unsorted
            (st->list, _line_device_compare, &pe->device);
     }

   if (!line) /* List is empty or device not found, new line-struct on
               * START only */
     {
        if ((event_type == EVAS_CALLBACK_MOUSE_DOWN) ||
            (event_type == EVAS_CALLBACK_MULTI_DOWN) ||
            ((sd->glayer_continues_enable) &&   /* START on MOVE also */
             ((event_type == EVAS_CALLBACK_MOUSE_MOVE) ||
              /* Allocate new item on START only */
              (event_type == EVAS_CALLBACK_MULTI_MOVE))))
          {
             line = calloc(1, sizeof(Line_Data));
             _line_data_reset(line);
             list = eina_list_append(list, line);
             st->list = list;
          }
     }

   if (!line) /* This may happen on MOVE that comes before DOWN      */
     return;  /* No line-struct to work with, can't continue testing */

   /* update st with input */
   if (_single_line_process(&st->info, line, pe, event_type))
     _event_consume(sd, event_info, event_type, EVAS_EVENT_FLAG_NONE);

   /* Get direction and magnitude of the line */
   double angle;
   _vector_get(line->line_st.x, line->line_st.y, pe->x, pe->y,
               &line->line_length, &angle);

   /* These are used later to compare lines length */
   Evas_Coord shortest_line_len = line->line_length;
   Evas_Coord longest_line_len = line->line_length;
   Evas_Event_Flags ev_flag = EVAS_EVENT_FLAG_NONE;

   /* Now update line-state */
   if (line->t_st) /* Analyze line only if line started */
     {
        if (line->line_angle >= 0.0) /* if line direction was set, we
                                      * test if broke tolerance */
          {
             double a = fabs(angle - line->line_angle);
             /* Distance from line */
             double d = (tan(DEG2RAD(a))) * line->line_length;
             /* Broke tolerance: abort line and start a new one */
             if ((d > sd->line_distance_tolerance) ||
                 (a > sd->line_angular_tolerance))
               {
                  ev_flag = _state_set(gesture, ELM_GESTURE_STATE_ABORT,
                                       &st->info, EINA_FALSE);
                  _event_consume(sd, event_info, event_type, ev_flag);
                  return;
               }

             /* We may finish line if momentum is zero */
             if (sd->glayer_continues_enable)
               {
                  /* This is for continues-gesture */
                  /* Finish line on zero momentum for continues gesture */
                  if ((!st->info.momentum.mx) && (!st->info.momentum.my))
                    {
                       line->line_end.x = pe->x;
                       line->line_end.y = pe->y;
                       line->t_end = pe->timestamp;
                    }
               }
          }
        else
          {  /* Record the line angle as it broke minimum length for line */
            if (line->line_length >= sd->line_min_length)
              st->info.angle = line->line_angle = angle;
          }

        if (line->t_end)
          {
             if (line->line_angle < 0.0) /* it's not a line, too short
                                          * more close to a tap */
               {
                  ev_flag = _state_set(gesture, ELM_GESTURE_STATE_ABORT,
                                       &st->info, EINA_FALSE);
                  _event_consume(sd, event_info, event_type, ev_flag);
                  return;
               }
          }
     }

   /* Count how many lines already started / ended */
   int started = 0;
   int ended = 0;
   unsigned int tm_start = pe->timestamp;
   unsigned int tm_end = pe->timestamp;
   Eina_List *l;
   Line_Data *t_line;
   double base_angle = ELM_GESTURE_NEGATIVE_ANGLE;
   Eina_Bool lines_parallel = EINA_TRUE;
   EINA_LIST_FOREACH(list, l, t_line)
     {
        if (base_angle < 0)
          base_angle = t_line->line_angle;
        else
          {
             if (t_line->line_angle >= 0) /* Compare angle only with
                                           * lines with direction
                                           * defined */
               {
                  if (fabs(base_angle - t_line->line_angle) >
                      sd->line_angular_tolerance)
                    lines_parallel = EINA_FALSE;
               }
          }

        if (t_line->line_length) /* update only if this line is used */
          {
             if (shortest_line_len > t_line->line_length)
               shortest_line_len = t_line->line_length;

             if (longest_line_len < t_line->line_length)
               longest_line_len = t_line->line_length;
          }

        if (t_line->t_st)
          {
             started++;
             if (t_line->t_st < tm_start)
               tm_start = t_line->t_st;
          }

        if (t_line->t_end)
          {
             ended++;
             if (t_line->t_end < tm_end)
               tm_end = t_line->t_end;
          }
     }

   st->info.momentum.n = started;

   if (ended &&
       ((event_type == EVAS_CALLBACK_MOUSE_DOWN) ||
        /* user lift one finger then starts again without line-end - ABORT */
        (event_type == EVAS_CALLBACK_MULTI_DOWN)))
     {
        ev_flag = _state_set(gesture, ELM_GESTURE_STATE_ABORT, &st->info,
                             EINA_FALSE);
        _event_consume(sd, event_info, event_type, ev_flag);
        return;
     }

   if (!lines_parallel) /* Lines are NOT at same direction, abort this
                         * gesture */
     {
        ev_flag = _state_set(gesture, ELM_GESTURE_STATE_ABORT, &st->info,
                             EINA_FALSE);
        _event_consume(sd, event_info, event_type, ev_flag);
        return;
     }

   /* We report ABORT if lines length are NOT matching when fingers are up */
   if ((longest_line_len - shortest_line_len) >
       (elm_config_finger_size_get() * 2))
     {
        ev_flag = _state_set(gesture, ELM_GESTURE_STATE_ABORT, &st->info,
                             EINA_FALSE);
        _event_consume(sd, event_info, event_type, ev_flag);
        return;
     }

   /* We consider FLICK as a fast line.ABORT if take too long to finish */
   if ((g_type == ELM_GESTURE_N_FLICKS) && ((tm_end - tm_start) >
                                            sd->flick_time_limit_ms))
     {
        ev_flag = _state_set(gesture, ELM_GESTURE_STATE_ABORT, &st->info,
                             EINA_FALSE);
        _event_consume(sd, event_info, event_type, ev_flag);
        return;
     }

   switch (event_type)
     {
      case EVAS_CALLBACK_MOUSE_UP:
      case EVAS_CALLBACK_MULTI_UP:
        if ((started) && (started == ended))
          {
             ev_flag = _state_set(gesture, ELM_GESTURE_STATE_END,
                                  &st->info, EINA_FALSE);
             _event_consume(sd, event_info, event_type, ev_flag);
          }

        return;

      case EVAS_CALLBACK_MOUSE_DOWN:
      case EVAS_CALLBACK_MULTI_DOWN:
      case EVAS_CALLBACK_MOUSE_MOVE:
      case EVAS_CALLBACK_MULTI_MOVE:
        if (started)
          {
             /* For continues gesture */
             if (sd->glayer_continues_enable && (started == ended))
               {
                  ev_flag = _state_set(gesture, ELM_GESTURE_STATE_END,
                                       &st->info, EINA_FALSE);
                  _event_consume(sd, event_info, event_type, ev_flag);
               }
             else
               {   /* When continues, may START on MOVE event too */
                 Elm_Gesture_State s = ELM_GESTURE_STATE_MOVE;

                 /* This happens when: on n > 1 lines then one finger up */
                 /* caused abort, then put finger down.                  */
                 /* This will stop line from starting again.             */
                 /* Number of lines, MUST match touched-device in list   */
                 if ((!sd->glayer_continues_enable) &&
                     (eina_list_count(st->list) <
                      eina_list_count(sd->touched)))
                   s = ELM_GESTURE_STATE_ABORT;

                 if (gesture->state == ELM_GESTURE_STATE_UNDEFINED)
                   s = ELM_GESTURE_STATE_START;

                 ev_flag = _state_set(gesture, s, &st->info, EINA_TRUE);
                 _event_consume(sd, event_info, event_type, ev_flag);
               }
          }
        break;

      default:
        return;   /* Unhandeld event type */
     }
}

/**
 * @internal
 *
 * This function is used to check if rotation gesture started.
 * @param st Contains current rotation values from user input.
 * @return TRUE/FALSE if we need to set rotation START.
 *
 * @ingroup Elm_Gesture_Layer
 */
static Eina_Bool
_on_rotation_broke_tolerance(Rotate_Type *st)
{
   if (st->info.base_angle < 0)
     return EINA_FALSE;  /* Angle has to be computed first */

   if (st->rotate_angular_tolerance < 0)
     return EINA_TRUE;

   double low = st->info.base_angle - st->rotate_angular_tolerance;
   double high = st->info.base_angle + st->rotate_angular_tolerance;
   double t = st->info.angle;

   if (low < 0)
     {
        low += 180;
        high += 180;

        if (t < 180)
          t += 180;
        else
          t -= 180;
     }

   if (high > 360)
     {
        low -= 180;
        high -= 180;

        if (t < 180)
          t += 180;
        else
          t -= 180;
     }

   if ((t < low) || (t > high)) /* This marks that rotation action has
                                 * started */
     {
        st->rotate_angular_tolerance = ELM_GESTURE_NEGATIVE_ANGLE;
        st->info.base_angle = st->info.angle; /* Avoid jump in angle value */
        return EINA_TRUE;
     }

   return EINA_FALSE;
}

/**
 * @internal
 *
 * This function is used for computing the gap between fingers.
 * It returns the length and center point between fingers.
 *
 * @param xx1 first finger x location.
 * @param yy1 first finger y location.
 * @param xx2 second finger x location.
 * @param yy2 second finger y location.
 * @param x  Get center point x cord (output)
 * @param y  Get center point y cord (output)
 *
 * @return length of the line between (xx1,yy1), (xx2,yy2) in pixels.
 *
 * @ingroup Elm_Gesture_Layer
 */
static Evas_Coord
_finger_gap_length_get(Evas_Coord xx1,
                       Evas_Coord yy1,
                       Evas_Coord xx2,
                       Evas_Coord yy2,
                       Evas_Coord *x,
                       Evas_Coord *y)
{
   double a, b, xx, yy, gap;
   xx = abs(xx2 - xx1);
   yy = abs(yy2 - yy1);
   gap = sqrt((xx * xx) + (yy * yy));

   /* START - Compute zoom center point */
   /* The triangle defined as follows:
    *             B
    *           / |
    *          /  |
    *     gap /   | a
    *        /    |
    *       A-----C
    *          b
    * http://en.wikipedia.org/wiki/Trigonometric_functions
    *************************************/
   if (((int)xx) && ((int)yy))
     {
        double A = atan((yy / xx));
        a = (Evas_Coord)((gap / 2) * sin(A));
        b = (Evas_Coord)((gap / 2) * cos(A));
        *x = (Evas_Coord)((xx2 > xx1) ? (xx1 + b) : (xx2 + b));
        *y = (Evas_Coord)((yy2 > yy1) ? (yy1 + a) : (yy2 + a));
     }
   else
     {
        if ((int)xx) /* horiz line, take half width */
          {
             *x = (Evas_Coord)((xx1 + xx2) / 2);
             *y = (Evas_Coord)(yy1);
          }

        if ((int)yy) /* vert line, take half width */
          {
             *x = (Evas_Coord)(xx1);
             *y = (Evas_Coord)((yy1 + yy2) / 2);
          }
     }
   /* END   - Compute zoom center point */

   return (Evas_Coord)gap;
}

/**
 * @internal
 *
 * This function is used for computing zoom value.
 *
 * @param st Pointer to zoom data based on user input.
 * @param tm_end Recent input event timestamp.
 * @param zoom_val Current computed zoom value.
 *
 * @return zoom momentum
 *
 * @ingroup Elm_Gesture_Layer
 */
static double
_zoom_momentum_get(Zoom_Type *st,
                   unsigned int tm_end,
                   double zoom_val)
{
   unsigned int tm_total;
   if (!st->m_st_tm) /* Init, and we don't start computing momentum yet */
     {
        st->m_st_tm = st->m_prev_tm = tm_end;
        st->m_base = zoom_val;
        return 0.0;
     }

   if ((tm_end - ELM_GESTURE_MOMENTUM_DELAY) < st->m_st_tm)
     return 0.0;  /* we don't start to compute momentum yet */

   if (st->dir) /* if direction was already defined, check if changed */
     {
        if (((st->dir < 0) && (zoom_val > st->info.zoom)) ||
            /* Direction changed, reset momentum */
            ((st->dir > 0) && (zoom_val < st->info.zoom)))
          {
             st->m_st_tm = 0;
             st->dir = (-st->dir);
             return 0.0;
          }
     }
   else
     st->dir = (zoom_val > st->info.zoom) ? 1 : -1;  /* init */

   if ((tm_end - ELM_GESTURE_MOMENTUM_TIMEOUT) > st->m_prev_tm)
     {
        st->m_st_tm = 0; /* Rest momentum when waiting too long */
        return 0.0;
     }

   st->m_prev_tm = tm_end;
   tm_total = tm_end - st->m_st_tm;

   if (tm_total)
     return ((zoom_val - st->m_base) * 1000) / tm_total;
   else
     return 0.0;
}

/**
 * @internal
 *
 * This function is used for computing zoom value.
 *
 * @param st Pointer to zoom data based on user input.
 * @param xx1 first finger x location.
 * @param yy1 first finger y location.
 * @param xx2 second finger x location.
 * @param yy2 second finger y location.
 * @param factor zoom-factor, used to determine how fast zoom works.
 *
 * @return zoom value, when 1.0 means no zoom, 0.5 half size...
 *
 * @ingroup Elm_Gesture_Layer
 */
static double
_zoom_compute(Zoom_Type *st,
              Evas_Coord xx1,
              Evas_Coord yy1,
              Evas_Coord xx2,
              Evas_Coord yy2,
              double zoom_finger_factor)
{
   double rt = 1.0;
   unsigned int tm_end = (st->zoom_mv.timestamp > st->zoom_mv1.timestamp) ?
     st->zoom_mv.timestamp : st->zoom_mv1.timestamp;

   Evas_Coord diam = _finger_gap_length_get(xx1, yy1, xx2, yy2,
                                            &st->info.x, &st->info.y);

   st->info.radius = diam / 2;

   if (!st->zoom_base)
     {
        st->zoom_base = diam;
        return st->info.zoom;
     }

   if (st->zoom_distance_tolerance) /* zoom tolerance <> ZERO, means
                                    * zoom action NOT started yet */
     {
        /* avoid jump with zoom value when break tolerance */
        if (diam < (st->zoom_base - st->zoom_distance_tolerance))
          {
             st->zoom_base -= st->zoom_distance_tolerance;
             st->zoom_distance_tolerance = 0;
          }

        /* avoid jump with zoom value when break tolerance */
        if (diam > (st->zoom_base + st->zoom_distance_tolerance))
          {
             st->zoom_base += st->zoom_distance_tolerance;
             st->zoom_distance_tolerance = 0;
          }

        return rt;
     }

   /* We use factor only on the difference between gap-base   */
   /* if gap=120, base=100, we get ((120-100)/100)=0.2*factor */
   rt = ((1.0) + ((((float)diam - (float)st->zoom_base) /
                   (float)st->zoom_base) * zoom_finger_factor));

   /* Momentum: zoom per second: */
   st->info.momentum = _zoom_momentum_get(st, tm_end, rt);

   return rt;
}

/**
 * @internal
 *
 * This function handles zoom with mouse wheel.
 * thats a combination of wheel + CTRL key.
 * @param obj The gesture-layer object.
 * @param event_info Original input event pointer.
 * @param event_type Type of original input event.
 * @param g_type what Gesture we are testing.
 *
 * @ingroup Elm_Gesture_Layer
 */
static void
_zoom_with_wheel_test(Evas_Object *obj,
                      void *event_info,
                      Evas_Callback_Type event_type,
                      Elm_Gesture_Type g_type)
{
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);

   if (!sd->gesture[g_type]) return;

   Gesture_Info *gesture_zoom = sd->gesture[g_type];
   Zoom_Type *st = gesture_zoom->data;
   Evas_Event_Flags ev_flag = EVAS_EVENT_FLAG_NONE;
   if (!st) /* Allocated once on first time, used for zoom intermediate data */
     {
        st = calloc(1, sizeof(Zoom_Type));
        if (!st) return;

        gesture_zoom->data = st;
        _zoom_test_reset(gesture_zoom);
     }

   switch (event_type)
     {
      case EVAS_CALLBACK_KEY_UP:
      {
         Evas_Event_Key_Up *p = event_info;
         if ((!strcmp(p->key, "Control_L")) ||
             /* Test if we ended a zoom gesture when releasing CTRL */
             (!strcmp(p->key, "Control_R")))
           {
              if ((st->zoom_wheel) &&
                  ((gesture_zoom->state == ELM_GESTURE_STATE_START) ||
                   /* User released CTRL after zooming */
                   (gesture_zoom->state == ELM_GESTURE_STATE_MOVE)))
                {
                   st->info.momentum = _zoom_momentum_get
                       (st, p->timestamp, st->info.zoom);

                   ev_flag = _state_set
                       (gesture_zoom, ELM_GESTURE_STATE_END, &st->info,
                       EINA_FALSE);
                   _event_consume(sd, event_info, event_type, ev_flag);

                   return;
                }
           }
         break;
      }

      case EVAS_CALLBACK_MOUSE_WHEEL:
      {
         Eina_Bool force;
         Elm_Gesture_State s;
         if (!evas_key_modifier_is_set(
               ((Evas_Event_Mouse_Wheel *)event_info)->modifiers,
               "Control")) /* if using wheel without CTRL after starting zoom */
           {
              if ((st->zoom_wheel) &&
                  ((gesture_zoom->state == ELM_GESTURE_STATE_START) ||
                   (gesture_zoom->state == ELM_GESTURE_STATE_MOVE)))
                {
                   ev_flag = _state_set
                       (gesture_zoom, ELM_GESTURE_STATE_END, &st->info,
                       EINA_FALSE);
                   _event_consume(sd, event_info, event_type, ev_flag);

                   return;
                }
              else
                return;  /* Ignore mouse-wheel without control */
           }

         /* Using mouse wheel with CTRL for zoom */
         /* (zoom_wheel == NULL) and (zoom_distance_tolerance == 0) we
          * continue a zoom gesture */
         if (st->zoom_wheel || (st->zoom_distance_tolerance == 0))
           {
              force = EINA_TRUE;
              s = ELM_GESTURE_STATE_MOVE;
           }
         else
           { /* On first wheel event, report START */
             force = EINA_FALSE;
             s = ELM_GESTURE_STATE_START;
             if (!efl_canvas_object_key_grab(sd->target, "Control_L",
                                             EFL_INPUT_MODIFIER_CONTROL, 0,
                                             EINA_FALSE))
               ERR("Failed to grab left Ctrl");
             if (!efl_canvas_object_key_grab(sd->target, "Control_R",
                                             EFL_INPUT_MODIFIER_CONTROL, 0,
                                             EINA_FALSE))
               ERR("Failed to grab right Ctrl");
           }

         st->zoom_distance_tolerance = 0; /* Cancel tolerance */
         st->zoom_wheel = (Evas_Event_Mouse_Wheel *)event_info;
         st->info.x = st->zoom_wheel->canvas.x;
         st->info.y = st->zoom_wheel->canvas.y;

         if (st->zoom_wheel->z < 0) /* zoom in */
           st->info.zoom += (sd->zoom_finger_factor * sd->zoom_wheel_factor);

         if (st->zoom_wheel->z > 0) /* zoom out */
           st->info.zoom -= (sd->zoom_finger_factor * sd->zoom_wheel_factor);

         if (st->info.zoom < 0.0)
           st->info.zoom = 0.0;

         st->info.momentum = _zoom_momentum_get
             (st, st->zoom_wheel->timestamp, st->info.zoom);

         ev_flag = _state_set(gesture_zoom, s, &st->info, force);
         _event_consume(sd, event_info, event_type, ev_flag);
         break;
      }

      default:
        return;
     }
}

/**
 * @internal
 *
 * This function is used to test zoom gesture.
 * user may combine zoom, rotation together.
 * so its possible that both will be detected from input.
 * (both are two-finger movement-oriented gestures)
 *
 * @param obj The gesture-layer object.
 * @param event_info Pointer to recent input event.
 * @param event_type Recent input event type.
 * @param g_type what Gesture we are testing.
 *
 * @ingroup Elm_Gesture_Layer
 */
static void
_zoom_test(Evas_Object *obj,
           Pointer_Event *pe,
           void *event_info,
           Evas_Callback_Type event_type,
           Elm_Gesture_Type g_type)
{
   /* Test for wheel zoom. */
   _zoom_with_wheel_test(obj, event_info, event_type, ELM_GESTURE_ZOOM);

   if (!_elm_config->glayer_zoom_finger_enable)
     return;

   if (!pe)
     return;
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);

   if (!sd->gesture[g_type]) return;

   Gesture_Info *gesture_zoom = sd->gesture[g_type];
   Zoom_Type *st = gesture_zoom->data;

   if (!st) /* Allocated once on first time, used for zoom data */
     {
        st = calloc(1, sizeof(Zoom_Type));
        if (!st) return;

        gesture_zoom->data = st;
        _zoom_test_reset(gesture_zoom);
     }

   /* Start - new zoom testing, letting all fingers start */
   Evas_Event_Flags ev_flag = EVAS_EVENT_FLAG_NONE;
   switch (event_type)
     {
      case EVAS_CALLBACK_MOUSE_MOVE:
      case EVAS_CALLBACK_MULTI_MOVE:
        /* if non-continues mode and gesture NOT started, ignore MOVE */
        if ((!sd->glayer_continues_enable) &&
            (!st->zoom_st.timestamp))
          return;
        EINA_FALLTHROUGH;
        // fallthrough is intentional
      case EVAS_CALLBACK_MOUSE_DOWN:
      case EVAS_CALLBACK_MULTI_DOWN:
      { /* Here we take care of zoom-start and zoom move */
        Eina_List *l;
        Pointer_Event *p;

        if (eina_list_count(sd->touched) > 2) /* Process zoom only
                                               * when 2 fingers on
                                               * surface */
          {
             ev_flag = _state_set
                 (gesture_zoom, ELM_GESTURE_STATE_ABORT, &st->info,
                 EINA_FALSE);
             _event_consume(sd, event_info, event_type, ev_flag);

             return;
          }

        if (!st->zoom_st.timestamp) /* Now scan touched-devices list
                                     * and find other finger */
          {
             EINA_LIST_FOREACH(sd->touched, l, p)
               { /* Device of other finger <> pe device */
                 if (p->device != pe->device)
                   break;
               }

             if (!p) /* Single finger on touch */
               return;

             /* Record down fingers */
             _event_consume(sd, event_info, event_type, ev_flag);
             memcpy(&st->zoom_st, pe, sizeof(Pointer_Event));
             memcpy(&st->zoom_st1, p, sizeof(Pointer_Event));

             /* Set mv field as well to be ready for MOVE events  */
             memcpy(&st->zoom_mv, pe, sizeof(Pointer_Event));
             memcpy(&st->zoom_mv1, p, sizeof(Pointer_Event));

             /* Here we have zoom_st, zoom_st1 set, report START  */
             /* Set zoom-base after BOTH down events  recorded    */
             /* Compute length of line between fingers zoom start */
             st->info.zoom = 1.0;
             st->zoom_base = _finger_gap_length_get
                 (st->zoom_st1.x, st->zoom_st1.y, st->zoom_st.x, st->zoom_st.y,
                 &st->info.x, &st->info.y);

             st->info.radius = st->zoom_base / 2;

             if ((gesture_zoom->state != ELM_GESTURE_STATE_START) &&
                 /* zoom started with mouse-wheel, don't report twice */
                 (gesture_zoom->state != ELM_GESTURE_STATE_MOVE))
               {
                  ev_flag = _state_set
                      (gesture_zoom, ELM_GESTURE_STATE_START, &st->info,
                      EINA_FALSE);
                  _event_consume(sd, event_info, event_type, ev_flag);
               }

             return; /* Zoom started */
          } /* End of ZOOM_START handling */

        /* if we got here, we have (exactly) two fingers on surface */
        /* we also after START, report MOVE */
        /* First detect which finger moved  */
        if (pe->device == st->zoom_mv.device)
          memcpy(&st->zoom_mv, pe, sizeof(Pointer_Event));
        else if (pe->device == st->zoom_mv1.device)
          memcpy(&st->zoom_mv1, pe, sizeof(Pointer_Event));

        /* Compute change in zoom as fingers move */
        st->info.zoom = _zoom_compute(st,
                                      st->zoom_mv.x, st->zoom_mv.y,
                                      st->zoom_mv1.x, st->zoom_mv1.y,
                                      sd->zoom_finger_factor);

        if (!st->zoom_distance_tolerance) /* Zoom broke tolerance,
                                           * report move */
          {
             double d = st->info.zoom - st->next_step;
             if (d < 0.0)
               d = (-d);

             if (d >= sd->zoom_step) /* Report move in steps */
               {
                  st->next_step = st->info.zoom;

                  ev_flag = _state_set(gesture_zoom,
                                       ELM_GESTURE_STATE_MOVE,
                                       &st->info, EINA_TRUE);
                  _event_consume(sd, event_info, event_type, ev_flag);
               }
          } /* End of ZOOM_MOVE handling */

        return;
      }

      case EVAS_CALLBACK_MOUSE_UP:
      case EVAS_CALLBACK_MULTI_UP:
        /* Reset timestamp of finger-up.This is used later
           by _zoom_test_reset() to retain finger-down data */
        _event_consume(sd, event_info, event_type, ev_flag);
        if (((st->zoom_wheel) || (st->zoom_base)) &&
            (st->zoom_distance_tolerance == 0))
          {
             ev_flag = _state_set(gesture_zoom, ELM_GESTURE_STATE_END,
                                  &st->info, EINA_FALSE);
             _event_consume(sd, event_info, event_type, ev_flag);

             return;
          }

        /* if we got here not a ZOOM */
        /* Must be != undefined, if gesture started */
        if (gesture_zoom->state != ELM_GESTURE_STATE_UNDEFINED)
          {
             ev_flag = _state_set
                 (gesture_zoom, ELM_GESTURE_STATE_ABORT, &st->info,
                 EINA_FALSE);
             _event_consume(sd, event_info, event_type, ev_flag);
          }

        _zoom_test_reset(gesture_zoom);

        return;

      default:
        return;
     }
}

static void
_rotate_properties_get(Rotate_Type *st,
                       Evas_Coord xx1,
                       Evas_Coord yy1,
                       Evas_Coord xx2,
                       Evas_Coord yy2,
                       double *angle)
{
   /* FIXME: Fix momentum computation, it's wrong */
   double prev_angle = *angle;

   st->info.radius = _finger_gap_length_get(xx1, yy1, xx2, yy2,
                                            &st->info.x, &st->info.y) / 2;

   *angle = _angle_get(xx1, yy1, xx2, yy2);

   if (angle == &st->info.angle) /* Fingers are moving, compute momentum */
     {
        unsigned int tm_start =
          (st->rotate_st.timestamp > st->rotate_st1.timestamp)
          ?  st->rotate_st.timestamp : st->rotate_st1.timestamp;
        unsigned int tm_end =
          (st->rotate_mv.timestamp > st->rotate_mv1.timestamp)
          ? st->rotate_mv.timestamp : st->rotate_mv1.timestamp;

        unsigned int tm_total = tm_end - tm_start;
        if (tm_total) /* Momentum computed as:
                         accumulated rotation angle (deg) divided by time */
          {
             double m = 0;
             if (((prev_angle < 90) && ((*angle) > 270)) ||
                 /* We circle passing ZERO point */
                 ((prev_angle > 270) && ((*angle) < 90)))
               {
                  prev_angle = (*angle);
               }
             else m = prev_angle - (*angle);

             st->accum_momentum += m;

             if ((tm_end - st->prev_momentum_tm) < 100)
               st->prev_momentum += m;
             else
               {
                  if (fabs(st->prev_momentum) < 0.002)
                    st->accum_momentum = 0.0;  /* reset momentum */

                  st->prev_momentum = 0.0; /* Start again    */
               }

             st->prev_momentum_tm = tm_end;
             st->info.momentum = (st->accum_momentum * 1000) / tm_total;
          }
     }
   else
     st->info.momentum = 0;
}

/**
 * @internal
 *
 * This function is used to test rotation gesture.
 * user may combine zoom, rotation together.
 * so its possible that both will be detected from input.
 * (both are two-finger movement-oriented gestures)
 *
 * @param obj The gesture-layer object.
 * @param event_info Pointer to recent input event.
 * @param event_type Recent input event type.
 * @param g_type what Gesture we are testing.
 *
 * @ingroup Elm_Gesture_Layer
 */
static void
_rotate_test(Evas_Object *obj,
             Pointer_Event *pe,
             void *event_info,
             Evas_Callback_Type event_type,
             Elm_Gesture_Type g_type)
{
   Evas_Event_Flags ev_flag = EVAS_EVENT_FLAG_NONE;
   Gesture_Info *gesture;
   Rotate_Type *st = NULL;

   if (!_elm_config->glayer_rotate_finger_enable)
     return;

   if (!pe)
     return;

   ELM_GESTURE_LAYER_DATA_GET(obj, sd);

   if (!sd->gesture[g_type]) return;

   gesture = sd->gesture[g_type];
   if (!gesture) return ;

   st = gesture->data;
   if (!st) /* Allocated once on first time */
     {
       st = calloc(1, sizeof(Rotate_Type));
       if (!st) return;

       gesture->data = st;
       _rotate_test_reset(gesture);
     }

   switch (event_type)
     {
      case EVAS_CALLBACK_MOUSE_MOVE:
      case EVAS_CALLBACK_MULTI_MOVE:
        /* if non-continues mode and gesture NOT started, ignore MOVE */
        if ((!sd->glayer_continues_enable) &&
            (!st->rotate_st.timestamp))
          return;
        EINA_FALLTHROUGH;
        // fallthrough is intentional
      case EVAS_CALLBACK_MOUSE_DOWN:
      case EVAS_CALLBACK_MULTI_DOWN:
      { /* Here we take care of rotate-start and rotate move */
        Eina_List *l;
        Pointer_Event *p;

        if (eina_list_count(sd->touched) > 2) /* Process rotate only
                                               * when 2 fingers on
                                               * surface */
          {
             ev_flag = _state_set
                 (gesture, ELM_GESTURE_STATE_ABORT, &st->info, EINA_FALSE);
             _event_consume(sd, event_info, event_type, ev_flag);

             return;
          }

        if (!st->rotate_st.timestamp) /* Now scan touched-devices list
                                       * and find other finger */
          {
             EINA_LIST_FOREACH(sd->touched, l, p)
               { /* Device of other finger <> pe device */
                 if (p->device != pe->device)
                   break;
               }

             if (!p)
               return;  /* Single finger on touch */

             /* Record down fingers */
             _event_consume(sd, event_info, event_type, ev_flag);
             memcpy(&st->rotate_st, pe, sizeof(Pointer_Event));
             memcpy(&st->rotate_st1, p, sizeof(Pointer_Event));

             /* Set mv field as well to be ready for MOVE events  */
             memcpy(&st->rotate_mv, pe, sizeof(Pointer_Event));
             memcpy(&st->rotate_mv1, p, sizeof(Pointer_Event));

             /* Here we have rotate_st, rotate_st1 set, report START  */
             /* Set rotate-base after BOTH down events  recorded    */
             /* Compute length of line between fingers rotate start */
             _rotate_properties_get(st,
                                    st->rotate_st.x, st->rotate_st.y,
                                    st->rotate_st1.x, st->rotate_st1.y,
                                    &st->info.base_angle);

             ev_flag = _state_set(gesture, ELM_GESTURE_STATE_START,
                                  &st->info, EINA_FALSE);
             _event_consume(sd, event_info, event_type, ev_flag);

             return; /* Rotate started */
          } /* End of ROTATE_START handling */

        /* if we got here, we have (exactly) two fingers on surface */
        /* we also after START, report MOVE */
        /* First detect which finger moved  */
        if (pe->device == st->rotate_mv.device)
          memcpy(&st->rotate_mv, pe, sizeof(Pointer_Event));
        else if (pe->device == st->rotate_mv1.device)
          memcpy(&st->rotate_mv1, pe, sizeof(Pointer_Event));

        /* Compute change in rotate as fingers move */
        _rotate_properties_get(st,
                               st->rotate_mv.x, st->rotate_mv.y,
                               st->rotate_mv1.x, st->rotate_mv1.y,
                               &st->info.angle);

        if (_on_rotation_broke_tolerance(st)) /* Rotation broke
                                               * tolerance, report
                                               * move */
          {
             double d = st->info.angle - st->next_step;
             if (d < 0)
               d = (-d);

             if (d >= sd->rotate_step) /* Report move in steps */
               {
                  st->next_step = st->info.angle;

                  ev_flag = _state_set
                      (gesture, ELM_GESTURE_STATE_MOVE, &st->info, EINA_TRUE);
                  _event_consume(sd, event_info, event_type, ev_flag);
               }
          } /* End of ROTATE_MOVE handling */

        return;
      }

      case EVAS_CALLBACK_MOUSE_UP:
      case EVAS_CALLBACK_MULTI_UP:
        _event_consume(sd, event_info, event_type, ev_flag);
        /* Reset timestamp of finger-up.This is used later
           by rotate_test_reset() to retain finger-down data */
        if (st->rotate_angular_tolerance < 0)
          {
             ev_flag = _state_set(gesture, ELM_GESTURE_STATE_END,
                                  &st->info, EINA_FALSE);
             _event_consume(sd, event_info, event_type, ev_flag);

             return;
          }

        /* Must be != undefined, if gesture started */
        if (gesture->state != ELM_GESTURE_STATE_UNDEFINED)
          {
             ev_flag = _state_set(gesture, ELM_GESTURE_STATE_ABORT,
                                  &st->info, EINA_FALSE);
             _event_consume(sd, event_info, event_type, ev_flag);
          }

        _rotate_test_reset(gesture);
        return;

      default:
        return;
     }
}

EOLIAN static Eina_Bool
_elm_gesture_layer_efl_ui_widget_on_disabled_update(Eo *obj, Elm_Gesture_Layer_Data *_pd EINA_UNUSED, Eina_Bool disabled)
{
   if (disabled)
     _callbacks_unregister(obj);
   else
     _callbacks_register(obj);

   return EINA_TRUE;
}

EOLIAN static void
_elm_gesture_layer_efl_canvas_group_group_add(Eo *obj, Elm_Gesture_Layer_Data *priv)
{
   efl_canvas_group_add(efl_super(obj, MY_CLASS));
   elm_widget_sub_object_parent_add(obj);

   priv->line_min_length =
     _elm_config->glayer_line_min_length * elm_config_finger_size_get();
   priv->zoom_distance_tolerance = _elm_config->glayer_zoom_distance_tolerance
     * elm_config_finger_size_get();
   priv->line_distance_tolerance = _elm_config->glayer_line_distance_tolerance
     * elm_config_finger_size_get();
   priv->zoom_finger_factor = _elm_config->glayer_zoom_finger_factor;
   /* mouse wheel zoom steps */
   priv->zoom_wheel_factor = _elm_config->glayer_zoom_wheel_factor;
   priv->rotate_angular_tolerance =
     _elm_config->glayer_rotate_angular_tolerance;
   priv->line_angular_tolerance = _elm_config->glayer_line_angular_tolerance;
   priv->flick_time_limit_ms = _elm_config->glayer_flick_time_limit_ms;
   priv->long_tap_start_timeout = _elm_config->glayer_long_tap_start_timeout;
   priv->repeat_events = EINA_TRUE;
   priv->glayer_continues_enable = _elm_config->glayer_continues_enable;
   priv->tap_finger_size = _elm_config->glayer_tap_finger_size;

   /* FIXME: Hack to get around old configs - if too small, enlarge. */
   if (_elm_config->glayer_double_tap_timeout < 0.00001)
     _elm_config->glayer_double_tap_timeout = 0.25;
   priv->double_tap_timeout = _elm_config->glayer_double_tap_timeout;

   memset(priv->gesture, 0, sizeof(priv->gesture));
}

static void _cbs_clean(Elm_Gesture_Layer_Data *sd, Elm_Gesture_Type idx, Elm_Gesture_State cb_type);

EOLIAN static void
_elm_gesture_layer_efl_canvas_group_group_del(Eo *obj, Elm_Gesture_Layer_Data *sd)
{
   Pointer_Event *data;
   int i;

   /* Clear all gestures intermediate data, stop any timers */
   {
      /* FIXME: +1 because of the mistake in the enum. */
      Gesture_Info **gitr = sd->gesture + 1;
      Tests_Array_Funcs *fitr = _glayer_tests_array + 1;
      for (; fitr->reset; fitr++, gitr++)
        {
           if (IS_TESTED_GESTURE(*gitr))
             fitr->reset(*gitr);
        }
   }

   /* First Free all gestures internal data structures */
   for (i = 0; i < ELM_GESTURE_LAST; i++)
     if (sd->gesture[i])
       {
          if (sd->gesture[i]->data)
            free(sd->gesture[i]->data);

          _cbs_clean(sd, i, ELM_GESTURE_STATE_START);
          _cbs_clean(sd, i, ELM_GESTURE_STATE_MOVE);
          _cbs_clean(sd, i, ELM_GESTURE_STATE_END);
          _cbs_clean(sd, i, ELM_GESTURE_STATE_ABORT);
          free(sd->gesture[i]);
          sd->gesture[i] = NULL; /* Referenced by _event_history_clear */
       }
   ecore_timer_del(sd->gest_taps_timeout);

   /* Then take care of clearing events */
   _event_history_clear(obj);
   sd->pending = eina_list_free(sd->pending);

   EINA_LIST_FREE(sd->touched, data)
     free(data);

   if (!elm_widget_disabled_get(obj))
     _callbacks_unregister(obj);

   efl_canvas_group_del(efl_super(obj, MY_CLASS));
}

EAPI Evas_Object *
elm_gesture_layer_add(Evas_Object *parent)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
   return elm_legacy_add(MY_CLASS, parent);
}

EOLIAN static Eo *
_elm_gesture_layer_efl_object_constructor(Eo *obj, Elm_Gesture_Layer_Data *_pd EINA_UNUSED)
{
   obj = efl_constructor(efl_super(obj, MY_CLASS));
   efl_canvas_object_type_set(obj, MY_CLASS_NAME_LEGACY);

   return obj;
}

EOLIAN static Eina_Bool
_elm_gesture_layer_hold_events_get(const Eo *obj EINA_UNUSED, Elm_Gesture_Layer_Data *sd)
{
   return !sd->repeat_events;
}

EOLIAN static void
_elm_gesture_layer_hold_events_set(Eo *obj EINA_UNUSED, Elm_Gesture_Layer_Data *sd, Eina_Bool hold_events)
{
   sd->repeat_events = !(!!hold_events);
}

EOLIAN static double
_elm_gesture_layer_zoom_step_get(const Eo *obj EINA_UNUSED, Elm_Gesture_Layer_Data *sd)
{
   return sd->zoom_step;
}

EOLIAN static void
_elm_gesture_layer_zoom_step_set(Eo *obj EINA_UNUSED, Elm_Gesture_Layer_Data *sd, double step)
{
   if (step < 0) return;

   sd->zoom_step = step;
}

EOLIAN static double
_elm_gesture_layer_rotate_step_get(const Eo *obj EINA_UNUSED, Elm_Gesture_Layer_Data *sd)
{
   return sd->rotate_step;
}

EOLIAN static void
_elm_gesture_layer_rotate_step_set(Eo *obj EINA_UNUSED, Elm_Gesture_Layer_Data *sd, double step)
{
   if (step < 0) return;

   sd->rotate_step = step;
}

EOLIAN static Eina_Bool
_elm_gesture_layer_attach(Eo *obj, Elm_Gesture_Layer_Data *sd, Evas_Object *target)
{
   if (!target) return EINA_FALSE;

   /* if was attached before, unregister callbacks first */
   if (sd->target)
     _callbacks_unregister(obj);

   sd->target = target;

   _callbacks_register(obj);
   return EINA_TRUE;
}

static void
_cbs_clean(Elm_Gesture_Layer_Data *sd,
          Elm_Gesture_Type idx,
          Elm_Gesture_State cb_type)
{
   if (!sd->gesture[idx]) return;

   Func_Data *cb_info;
   EINA_INLIST_FREE(sd->gesture[idx]->cbs[cb_type], cb_info)
     {
        sd->gesture[idx]->cbs[cb_type] = eina_inlist_remove(
              sd->gesture[idx]->cbs[cb_type], EINA_INLIST_GET(cb_info));
        free(cb_info);
     }
   SET_TEST_BIT(sd->gesture[idx]);
}

EOLIAN static void
_elm_gesture_layer_cb_set(Eo *obj, Elm_Gesture_Layer_Data *sd, Elm_Gesture_Type idx, Elm_Gesture_State cb_type, Elm_Gesture_Event_Cb cb, void *data)
{
   /* Clear gesture intermediate data, stop any timers */
   if (IS_TESTED_GESTURE(sd->gesture[idx]))
     _glayer_tests_array[idx].reset(sd->gesture[idx]);

   _cbs_clean(sd, idx, cb_type); // for ABI compat.
   elm_obj_gesture_layer_cb_add(obj, idx, cb_type, cb, data);
}

EOLIAN static void
_elm_gesture_layer_cb_add(Eo *obj, Elm_Gesture_Layer_Data *sd, Elm_Gesture_Type idx, Elm_Gesture_State cb_type, Elm_Gesture_Event_Cb cb, void *data)
{
   if (!cb) return;

   Gesture_Info *p;

   if (!sd->gesture[idx])
     sd->gesture[idx] = calloc(1, sizeof(Gesture_Info));
   if (!sd->gesture[idx]) return;

   Func_Data *cb_info = calloc(1, sizeof(*cb_info));
   if (!cb_info) return;
   cb_info->cb = cb;
   cb_info->user_data = data;

   p = sd->gesture[idx];
   p->obj = obj;
   p->g_type = idx;
   p->cbs[cb_type] = eina_inlist_append(p->cbs[cb_type],
         EINA_INLIST_GET(cb_info));
   p->state = ELM_GESTURE_STATE_UNDEFINED;
   SET_TEST_BIT(p);
}

EOLIAN static void
_elm_gesture_layer_cb_del(Eo *obj EINA_UNUSED, Elm_Gesture_Layer_Data *sd, Elm_Gesture_Type idx, Elm_Gesture_State cb_type, Elm_Gesture_Event_Cb cb, void *data)
{
   if (!sd->gesture[idx]) return;

   Eina_Inlist *itr;
   Func_Data *cb_info;
   EINA_INLIST_FOREACH_SAFE(sd->gesture[idx]->cbs[cb_type], itr, cb_info)
     {
        if (cb_info->cb == cb && cb_info->user_data == data)
          {
             /* Clear gesture intermediate data, stop any timers */
             if (IS_TESTED_GESTURE(sd->gesture[idx]))
                _glayer_tests_array[idx].reset(sd->gesture[idx]);

             sd->gesture[idx]->cbs[cb_type] = eina_inlist_remove(
                   sd->gesture[idx]->cbs[cb_type], EINA_INLIST_GET(cb_info));
             free(cb_info);
             SET_TEST_BIT(sd->gesture[idx]);
             return;
          }
     }
}

EAPI void
elm_gesture_layer_line_min_length_set(Evas_Object *obj, int line_min_length)
{
   ELM_GESTURE_LAYER_CHECK(obj);
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   sd->line_min_length = line_min_length;
}

EAPI int
elm_gesture_layer_line_min_length_get(const Evas_Object *obj)
{
   ELM_GESTURE_LAYER_CHECK(obj) 0;
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   return sd->line_min_length;
}

EAPI void
elm_gesture_layer_zoom_distance_tolerance_set(Evas_Object *obj, Evas_Coord zoom_distance_tolerance)
{
   ELM_GESTURE_LAYER_CHECK(obj);
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   sd->zoom_distance_tolerance = zoom_distance_tolerance;
}

EAPI Evas_Coord
elm_gesture_layer_zoom_distance_tolerance_get(const Evas_Object *obj)
{
   ELM_GESTURE_LAYER_CHECK(obj) 0;
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   return sd->zoom_distance_tolerance;
}

EAPI void
elm_gesture_layer_line_distance_tolerance_set(Evas_Object *obj, Evas_Coord line_distance_tolerance)
{
   ELM_GESTURE_LAYER_CHECK(obj);
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   sd->line_distance_tolerance = line_distance_tolerance;
}

EAPI Evas_Coord
elm_gesture_layer_line_distance_tolerance_get(const Evas_Object *obj)
{
   ELM_GESTURE_LAYER_CHECK(obj) 0;
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   return sd->line_distance_tolerance;
}

EAPI void
elm_gesture_layer_line_angular_tolerance_set(Evas_Object *obj, double line_angular_tolerance)
{
   ELM_GESTURE_LAYER_CHECK(obj);
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   sd->line_angular_tolerance = line_angular_tolerance;
}

EAPI double
elm_gesture_layer_line_angular_tolerance_get(const Evas_Object *obj)
{
   ELM_GESTURE_LAYER_CHECK(obj) 0.0;
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   return sd->line_angular_tolerance;
}

EAPI void
elm_gesture_layer_zoom_wheel_factor_set(Evas_Object *obj, double zoom_wheel_factor)
{
   ELM_GESTURE_LAYER_CHECK(obj);
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   sd->zoom_wheel_factor = zoom_wheel_factor;
}

EAPI double
elm_gesture_layer_zoom_wheel_factor_get(const Evas_Object *obj)
{
   ELM_GESTURE_LAYER_CHECK(obj) 0.0;
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   return sd->zoom_wheel_factor;
}

EAPI void
elm_gesture_layer_zoom_finger_factor_set(Evas_Object *obj, double zoom_finger_factor)
{
   ELM_GESTURE_LAYER_CHECK(obj);
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   sd->zoom_finger_factor = zoom_finger_factor;
}

EAPI double
elm_gesture_layer_zoom_finger_factor_get(const Evas_Object *obj)
{
   ELM_GESTURE_LAYER_CHECK(obj) 0.0;
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   return sd->zoom_finger_factor;
}

EAPI void
elm_gesture_layer_rotate_angular_tolerance_set(Evas_Object *obj, double rotate_angular_tolerance)
{
   ELM_GESTURE_LAYER_CHECK(obj);
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   sd->rotate_angular_tolerance = rotate_angular_tolerance;
}

EAPI double
elm_gesture_layer_rotate_angular_tolerance_get(const Evas_Object *obj)
{
   ELM_GESTURE_LAYER_CHECK(obj) 0.0;
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   return sd->rotate_angular_tolerance;
}

EAPI void
elm_gesture_layer_flick_time_limit_ms_set(Evas_Object *obj, unsigned int flick_time_limit_ms)
{
   ELM_GESTURE_LAYER_CHECK(obj);
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   sd->flick_time_limit_ms = flick_time_limit_ms;
}

EAPI unsigned int
elm_gesture_layer_flick_time_limit_ms_get(const Evas_Object *obj)
{
   ELM_GESTURE_LAYER_CHECK(obj) 0;
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   return sd->flick_time_limit_ms;
}

EAPI void
elm_gesture_layer_long_tap_start_timeout_set(Evas_Object *obj, double long_tap_start_timeout)
{
   ELM_GESTURE_LAYER_CHECK(obj);
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   sd->long_tap_start_timeout = ((long_tap_start_timeout > 0) ? long_tap_start_timeout : 0);
}

EAPI double
elm_gesture_layer_long_tap_start_timeout_get(const Evas_Object *obj)
{
   ELM_GESTURE_LAYER_CHECK(obj) 0.0;
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   return sd->long_tap_start_timeout;
}

EAPI void
elm_gesture_layer_continues_enable_set(Evas_Object *obj, Eina_Bool continues_enable)
{
   ELM_GESTURE_LAYER_CHECK(obj);
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   sd->glayer_continues_enable = continues_enable;
}

EAPI Eina_Bool
elm_gesture_layer_continues_enable_get(const Evas_Object *obj)
{
   ELM_GESTURE_LAYER_CHECK(obj) 0.0;
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   return sd->glayer_continues_enable;
}

EAPI void
elm_gesture_layer_double_tap_timeout_set(Evas_Object *obj, double double_tap_timeout)
{
   ELM_GESTURE_LAYER_CHECK(obj);
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   sd->double_tap_timeout = ((double_tap_timeout > 0) ? double_tap_timeout : 0);
}

EAPI double
elm_gesture_layer_double_tap_timeout_get(const Evas_Object *obj)
{
   ELM_GESTURE_LAYER_CHECK(obj) 0.0;
   ELM_GESTURE_LAYER_DATA_GET(obj, sd);
   return sd->double_tap_timeout;
}

EOLIAN static void
_elm_gesture_layer_tap_finger_size_set(Eo *obj EINA_UNUSED, Elm_Gesture_Layer_Data *sd, Evas_Coord sz)
{
   if (sz < 0)
      sz = 0;  /* Should not be negative, will reset to system value */

   sd->tap_finger_size = sz;
}

EOLIAN static Evas_Coord
_elm_gesture_layer_tap_finger_size_get(const Eo *obj EINA_UNUSED, Elm_Gesture_Layer_Data *sd)
{
   return sd->tap_finger_size;
}

static void
_elm_gesture_layer_class_constructor(Efl_Class *klass)
{
   evas_smart_legacy_type_register(MY_CLASS_NAME_LEGACY, klass);
}

/* Internal EO APIs and hidden overrides */

#define ELM_GESTURE_LAYER_EXTRA_OPS \
   EFL_CANVAS_GROUP_ADD_DEL_OPS(elm_gesture_layer)

#include "elm_gesture_layer.eo.c"