aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/elementary/efl_ui_focus_manager_calc.c
blob: 2e1b10a1d306d0f35511caf9599f09b1be0081d9 (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
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif
#define EFL_UI_FOCUS_OBJECT_PROTECTED


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

#define MY_CLASS EFL_UI_FOCUS_MANAGER_CALC_CLASS
#define FOCUS_DATA(obj) Efl_Ui_Focus_Manager_Calc_Data *pd = efl_data_scope_get(obj, MY_CLASS);

#define NODE_DIRECTIONS_COUNT 4

#define DIRECTION_IS_LOGICAL(dir) (dir >= EFL_UI_FOCUS_DIRECTION_PREVIOUS && dir < EFL_UI_FOCUS_DIRECTION_UP)
#define DIRECTION_IS_2D(dir) (dir >= EFL_UI_FOCUS_DIRECTION_UP && dir < EFL_UI_FOCUS_DIRECTION_LAST)
#define DIRECTION_CHECK(dir) (dir >= EFL_UI_FOCUS_DIRECTION_PREVIOUS && dir < EFL_UI_FOCUS_DIRECTION_LAST)

//#define CALC_DEBUG
#define DEBUG_TUPLE(obj) efl_name_get(obj), efl_class_name_get(obj)

static int _focus_log_domain = -1;

#define F_CRI(...) EINA_LOG_DOM_CRIT(_focus_log_domain, __VA_ARGS__)
#define F_ERR(...) EINA_LOG_DOM_ERR(_focus_log_domain, __VA_ARGS__)
#define F_WRN(...) EINA_LOG_DOM_WARN(_focus_log_domain, __VA_ARGS__)
#define F_INF(...) EINA_LOG_DOM_INFO(_focus_log_domain, __VA_ARGS__)
#define F_DBG(...) EINA_LOG_DOM_DBG(_focus_log_domain, __VA_ARGS__)

#define DIRECTION_ACCESS(V, ID) ((V)->graph.directions[(ID) - 2])

typedef enum {
    DIMENSION_X = 0,
    DIMENSION_Y = 1,
} Dimension;

typedef struct _Border Border;
typedef struct _Node Node;

struct _Border {
  Eina_List *partners; //partners that are linked in both directions
  Eina_List *one_direction; //partners that are linked in one direction
  Eina_List *cleanup_nodes; //a list of nodes that needs to be cleaned up when this node is deleted
};

typedef enum {
  NODE_TYPE_NORMAL = 0,
  NODE_TYPE_ONLY_LOGICAL = 2,
} Node_Type;

struct _Node{
  Node_Type type; //type of the node

  Efl_Ui_Focus_Object *focusable;
  Efl_Ui_Focus_Manager *manager;
  Efl_Ui_Focus_Manager *redirect_manager;

  struct _Tree_Node{
    Node *parent; //the parent of the tree
    Eina_List *children; //this saves the original set of elements
    Eina_List *saved_order;
  }tree;

  struct _Graph_Node {
    Border directions[NODE_DIRECTIONS_COUNT];
  } graph;
};

#define T(n) (n->tree)
#define G(n) (n->graph)

typedef struct {
    Eina_List *focus_stack;
    Eina_Hash *node_hash;
    Efl_Ui_Focus_Manager *redirect;
    Eina_List *dirty;

    Node *root;
} Efl_Ui_Focus_Manager_Calc_Data;

static void
_manager_in_chain_set(Eo *obj, Efl_Ui_Focus_Manager_Calc_Data *pd)
{
   Eo *manager;

   EINA_SAFETY_ON_NULL_RETURN(pd->root);

   if (!efl_isa(pd->root->focusable, EFL_UI_WIN_CLASS))
     EINA_SAFETY_ON_NULL_RETURN(efl_ui_focus_user_focus_manager_get(pd->root->focusable));

   //so we dont run infinitly this does not fix it, but at least we only have a error
   EINA_SAFETY_ON_TRUE_RETURN(efl_ui_focus_user_focus_manager_get(pd->root->focusable) == obj);

   manager = efl_ui_focus_user_focus_manager_get(pd->root->focusable);
   if (manager)
     efl_ui_focus_manager_focus_set(manager, pd->root->focusable);
   else
      DBG("No focus manager for focusable %s@%p",
          efl_class_name_get(pd->root->focusable), pd->root->focusable);
}

static Efl_Ui_Focus_Direction
_complement(Efl_Ui_Focus_Direction dir)
{
    #define COMP(a,b) \
        if (dir == a) return b; \
        if (dir == b) return a;

    COMP(EFL_UI_FOCUS_DIRECTION_RIGHT, EFL_UI_FOCUS_DIRECTION_LEFT)
    COMP(EFL_UI_FOCUS_DIRECTION_UP, EFL_UI_FOCUS_DIRECTION_DOWN)
    COMP(EFL_UI_FOCUS_DIRECTION_PREVIOUS, EFL_UI_FOCUS_DIRECTION_NEXT)

    #undef COMP

    return EFL_UI_FOCUS_DIRECTION_LAST;
}

/*
 * Set this new list of partners to the border.
 * All old partners will be deleted
 */
static void
border_partners_set(Node *node, Efl_Ui_Focus_Direction direction, Eina_List *list)
{
   Node *partner;
   Eina_List *lnode;
   Border *border;

   EINA_SAFETY_ON_FALSE_RETURN(DIRECTION_IS_2D(direction));

   border = &DIRECTION_ACCESS(node, direction);

   EINA_LIST_FREE(border->partners, partner)
     {
        Border *comp_border = &DIRECTION_ACCESS(partner, _complement(direction));

        comp_border->partners = eina_list_remove(comp_border->partners, node);
     }

   border->partners = list;

   EINA_LIST_FOREACH(border->partners, lnode, partner)
     {
        Border *comp_border = &DIRECTION_ACCESS(partner,_complement(direction));

        comp_border->partners = eina_list_append(comp_border->partners, node);
     }
}

static void
border_onedirection_set(Node *node, Efl_Ui_Focus_Direction direction, Eina_List *list)
{
   Node *partner;
   Eina_List *lnode;
   Border *border;

   EINA_SAFETY_ON_FALSE_RETURN(DIRECTION_IS_2D(direction));

   border = &DIRECTION_ACCESS(node, direction);

   EINA_LIST_FREE(border->one_direction, partner)
     {
        Border *b = &DIRECTION_ACCESS(partner, _complement(direction));
        b->cleanup_nodes = eina_list_remove(b->cleanup_nodes, node);
     }

   border->one_direction = list;

   EINA_LIST_FOREACH(border->one_direction, lnode, partner)
     {
        Border *comp_border = &DIRECTION_ACCESS(partner,_complement(direction));

        comp_border->cleanup_nodes = eina_list_append(comp_border->cleanup_nodes, node);
     }
}

static void
border_onedirection_cleanup(Node *node, Efl_Ui_Focus_Direction direction)
{
   Node *partner;
   Border *border;

   EINA_SAFETY_ON_FALSE_RETURN(DIRECTION_IS_2D(direction));

   border = &DIRECTION_ACCESS(node, direction);

   EINA_LIST_FREE(border->cleanup_nodes, partner)
     {
        Border *b = &DIRECTION_ACCESS(partner, _complement(direction));
        b->one_direction = eina_list_remove(b->one_direction, node);
     }
}
/**
 * Create a new node
 */
static Node*
node_new(Efl_Ui_Focus_Object *focusable, Efl_Ui_Focus_Manager *manager)
{
    Node *node;

    node = calloc(1, sizeof(Node));

    node->focusable = focusable;
    node->manager = manager;

    return node;
}

/**
 * Looks up given focus object from the focus manager.
 *
 * @returns node found, or NULL if focusable was not found in the manager.
 */
static Node*
node_get(Efl_Ui_Focus_Manager *obj, Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Object *focusable)
{
   Node *ret;

   ret = eina_hash_find(pd->node_hash, &focusable);

   if (ret) return ret;

   ERR("Focusable %p (%s) not registered in manager %p", focusable, efl_class_name_get(focusable), obj);

   return NULL;
}

/**
 * Free a node item and unlink this item from all direction
 */
static void
node_item_free(Node *item)
{
   Node *n;
   Eina_List *l;
   //free the graph items
   for(int i = EFL_UI_FOCUS_DIRECTION_UP;i < EFL_UI_FOCUS_DIRECTION_LAST; i++)
     {
        border_partners_set(item, i, NULL);
        border_onedirection_cleanup(item, i);
        border_onedirection_set(item, i, NULL);
     }

   //free the tree items
   if (!item->tree.parent && item->tree.children)
     {
        ERR("Freeing the root with children is going to break the logical tree!");
     }

   if (item->tree.parent && item->tree.children)
     {
        Node *parent;

        parent = item->tree.parent;
        //reparent everything into the next layer
        EINA_LIST_FOREACH(item->tree.children, l, n)
          {
             n->tree.parent = item->tree.parent;
          }
        parent->tree.children = eina_list_merge(parent->tree.children , item->tree.children);
     }

   if (item->tree.parent)
     {
        Node *parent;

        parent = item->tree.parent;
        T(parent).children = eina_list_remove(T(parent).children, item);
     }

   //free the safed order
   ELM_SAFE_FREE(T(item).saved_order, eina_list_free);

   free(item);
}
//FOCUS-STACK HELPERS

static Efl_Ui_Focus_Object*
_focus_stack_unfocus_last(Efl_Ui_Focus_Manager_Calc_Data *pd)
{
   Efl_Ui_Focus_Object *focusable = NULL;
   Node *n;

   n = eina_list_last_data_get(pd->focus_stack);

   if (n)
     focusable = n->focusable;

   pd->focus_stack = eina_list_remove(pd->focus_stack, n);

   if (n)
     efl_ui_focus_object_focus_set(n->focusable, EINA_FALSE);

   return focusable;
}

static Node*
_focus_stack_last_regular(Efl_Ui_Focus_Manager_Calc_Data *pd)
{
   Eina_List *l;
   Node *upper;

   l = eina_list_last(pd->focus_stack);
   upper = eina_list_last_data_get(pd->focus_stack);

   while (upper && upper->type != NODE_TYPE_NORMAL)
     {
        l = eina_list_prev(l);
        upper = eina_list_data_get(l);
     }

   return upper;
}

//CALCULATING STUFF

static inline int
_distance(Eina_Rect node, Eina_Rect op, Dimension dim)
{
    int min, max, point;
    int v1, v2;

    if (dim == DIMENSION_X)
      {
         min = op.x;
         max = eina_rectangle_max_x(&op.rect);
         point = node.x + node.w/2;
      }
    else
      {
         min = op.y;
         max = eina_rectangle_max_y(&op.rect);
         point = node.y + node.h/2;
      }

    v1 = min - point;
    v2 = max - point;

    if (abs(v1) < abs(v2))
      return v1;
    else
      return v2;
}

static inline void
_min_max_gen(Dimension dim, Eina_Rect rect, int *min, int *max)
{
   if (dim == DIMENSION_X)
     {
        *min = rect.y;
        *max = eina_rectangle_max_y(&rect.rect);
     }
   else
     {
        *min = rect.x;
        *max = eina_rectangle_max_x(&rect.rect);
     }
}

static inline void
_calculate_node(Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Object *node, Dimension dim, Eina_List **pos, Eina_List **neg)
{
   int dim_min, dim_max, cur_pos_min = 0, cur_neg_min = 0;
   Efl_Ui_Focus_Object *op;
   Eina_Iterator *nodes;
   Eina_Rect rect;
   Node *n;

   *pos = NULL;
   *neg = NULL;

   rect = efl_ui_focus_object_focus_geometry_get(node);
   nodes = eina_hash_iterator_data_new(pd->node_hash);
   _min_max_gen(dim, rect, &dim_min, &dim_max);

   EINA_ITERATOR_FOREACH(nodes, n)
     {
        Eina_Rect op_rect;
        int min, max;

        op = n->focusable;
        if (op == node) continue;

        if (n->type == NODE_TYPE_ONLY_LOGICAL) continue;

        op_rect = efl_ui_focus_object_focus_geometry_get(op);

        _min_max_gen(dim, op_rect, &min, &max);

        /* The only way the calculation does make sense is if the two number
         * lines are not disconnected.
         * If they are connected one point of the 4 lies between the min and max of the other line
         */
        if (!((min <= max && max <= dim_min && dim_min <= dim_max) ||
              (dim_min <= dim_max && dim_max <= min && min <= max)) &&
              !eina_rectangle_intersection(&op_rect.rect, &rect.rect))
          {
             //this thing hits horizontal
             int tmp_dis;

             tmp_dis = _distance(rect, op_rect, dim);

             if (tmp_dis < 0)
               {
                  if (tmp_dis == cur_neg_min)
                    {
                       //add it
                       *neg = eina_list_append(*neg, op);
                    }
                  else if (tmp_dis > cur_neg_min
                    || cur_neg_min == 0) //init case
                    {
                       //nuke the old and add
#ifdef CALC_DEBUG
                       printf("CORRECTION FOR %s-%s\n found anchor %s-%s in distance %d\n (%d,%d,%d,%d)\n (%d,%d,%d,%d)\n\n", DEBUG_TUPLE(node), DEBUG_TUPLE(op),
                         tmp_dis,
                         op_rect.x, op_rect.y, op_rect.w, op_rect.h,
                         rect.x, rect.y, rect.w, rect.h);
#endif
                       *neg = eina_list_free(*neg);
                       *neg = eina_list_append(NULL, op);
                       cur_neg_min = tmp_dis;
                    }
               }
             else
               {
                  if (tmp_dis == cur_pos_min)
                    {
                       //add it
                       *pos = eina_list_append(*pos, op);
                    }
                  else if (tmp_dis < cur_pos_min
                    || cur_pos_min == 0) //init case
                    {
                       //nuke the old and add
#ifdef CALC_DEBUG
                       printf("CORRECTION FOR %s-%s\n found anchor %s-%s in distance %d\n (%d,%d,%d,%d)\n (%d,%d,%d,%d)\n\n", DEBUG_TUPLE(node), DEBUG_TUPLE(op),
                         tmp_dis,
                         op_rect.x, op_rect.y, op_rect.w, op_rect.h,
                         rect.x, rect.y, rect.w, rect.h);
#endif
                       *pos = eina_list_free(*pos);
                       *pos = eina_list_append(NULL, op);
                       cur_pos_min = tmp_dis;
                    }
               }


#if 0
             printf("(%d,%d,%d,%d)%s vs(%d,%d,%d,%d)%s\n", rect.x, rect.y, rect.w, rect.h, elm_widget_part_text_get(node, NULL), op_rect.x, op_rect.y, op_rect.w, op_rect.h, elm_widget_part_text_get(op, NULL));
             printf("(%d,%d,%d,%d)\n", min, max, dim_min, dim_max);
             printf("Candidate %d\n", tmp_dis);
             if (anchor->anchor == NULL || abs(tmp_dis) < abs(distance)) //init case
               {
                  distance = tmp_dis;
                  anchor->positive = tmp_dis > 0 ? EINA_FALSE : EINA_TRUE;
                  anchor->anchor = op;
                  //Helper for debugging wrong calculations

               }
#endif
         }


     }
   eina_iterator_free(nodes);
   nodes = NULL;

}

static inline Eina_Position2D
_relative_position_rects(Eina_Rect *a, Eina_Rect *b)
{
   Eina_Position2D a_pos = {a->rect.x + a->rect.w/2, a->rect.y + a->rect.h/2};
   Eina_Position2D b_pos = {b->rect.x + b->rect.w/2, b->rect.y + b->rect.h/2};

   return (Eina_Position2D){b_pos.x - a_pos.x, b_pos.y - b_pos.y};
}

static inline Eina_Rectangle_Outside
_direction_to_outside(Efl_Ui_Focus_Direction direction)
{
   if (direction == EFL_UI_FOCUS_DIRECTION_RIGHT) return EINA_RECTANGLE_OUTSIDE_RIGHT;
   if (direction == EFL_UI_FOCUS_DIRECTION_LEFT) return EINA_RECTANGLE_OUTSIDE_LEFT;
   if (direction == EFL_UI_FOCUS_DIRECTION_DOWN) return EINA_RECTANGLE_OUTSIDE_BOTTOM;
   if (direction == EFL_UI_FOCUS_DIRECTION_UP) return EINA_RECTANGLE_OUTSIDE_TOP;

   return -1;
}

static inline void
_calculate_node_indirection(Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Object *node, Efl_Ui_Focus_Direction direction, Eina_List **lst)
{
   Efl_Ui_Focus_Object *op;
   Eina_Iterator *nodes;
   int min_distance = 0;
   Node *n;
   Eina_Rect rect;

   rect = efl_ui_focus_object_focus_geometry_get(node);
   nodes = eina_hash_iterator_data_new(pd->node_hash);

   EINA_ITERATOR_FOREACH(nodes, n)
     {
        Eina_Rectangle_Outside outside, outside_dir;
        Eina_Position2D pos;
        int distance;
        Eina_Rect op_rect;

        op = n->focusable;

        if (op == node) continue;
        if (n->type == NODE_TYPE_ONLY_LOGICAL) continue;

        op_rect = efl_ui_focus_object_focus_geometry_get(op);
        outside = eina_rectangle_outside_position(&rect.rect, &op_rect.rect);
        outside_dir = _direction_to_outside(direction);
        //calculate relative position of the nodes
        pos = _relative_position_rects(&rect, &op_rect);
        //calculate distance
        distance = pow(pos.x, 2) + pow(pos.y, 2);

        if (outside & outside_dir)
          {
             if (min_distance == 0 || min_distance > distance)
               {
                  min_distance = distance;
                  *lst = eina_list_free(*lst);
                  *lst = eina_list_append(*lst, op);
               }
             else if (min_distance == distance)
               {
                  *lst = eina_list_append(*lst, op);
               }
          }
     }
}

#ifdef CALC_DEBUG
static void
_debug_node(Node *node)
{
   Eina_List *tmp = NULL;

   if (!node) return;

   printf("NODE %s-%s\n", DEBUG_TUPLE(node->focusable));

#define DIR_LIST(dir) DIRECTION_ACCESS(node,dir).partners

#define DIR_OUT(dir)\
   tmp = DIR_LIST(dir); \
   { \
      Eina_List *list_node; \
      Node *partner; \
      printf("-"#dir"-> ("); \
      EINA_LIST_FOREACH(tmp, list_node, partner) \
        printf("%s-%s,", DEBUG_TUPLE(partner->focusable)); \
      printf(")\n"); \
   }

   DIR_OUT(EFL_UI_FOCUS_DIRECTION_RIGHT)
   DIR_OUT(EFL_UI_FOCUS_DIRECTION_LEFT)
   DIR_OUT(EFL_UI_FOCUS_DIRECTION_UP)
   DIR_OUT(EFL_UI_FOCUS_DIRECTION_DOWN)

}
#endif

static void
convert_set(Efl_Ui_Focus_Manager *obj, Efl_Ui_Focus_Manager_Calc_Data *pd, Node *node, Eina_List *focusable_list, Efl_Ui_Focus_Direction dir, void (*converter)(Node *node, Efl_Ui_Focus_Direction direction, Eina_List *list))
{
   Eina_List *partners = NULL;
   Efl_Ui_Focus_Object *fobj;

   EINA_LIST_FREE(focusable_list, fobj)
     {
        Node *entry;

        entry = node_get(obj, pd, fobj);
        if (!entry)
          {
             CRI("Found a obj in graph without node-entry!");
             return;
          }
        partners = eina_list_append(partners, entry);
     }

   converter(node, dir, partners);
}

static void
dirty_flush_node(Efl_Ui_Focus_Manager *obj EINA_UNUSED, Efl_Ui_Focus_Manager_Calc_Data *pd, Node *node)
{
   Eina_List *x_partners_pos, *x_partners_neg;
   Eina_List *y_partners_pos, *y_partners_neg;

   _calculate_node(pd, node->focusable, DIMENSION_X, &x_partners_pos, &x_partners_neg);
   _calculate_node(pd, node->focusable, DIMENSION_Y, &y_partners_pos, &y_partners_neg);

   convert_set(obj, pd, node, x_partners_pos, EFL_UI_FOCUS_DIRECTION_RIGHT, border_partners_set);
   convert_set(obj, pd, node, x_partners_neg, EFL_UI_FOCUS_DIRECTION_LEFT, border_partners_set);
   convert_set(obj, pd, node, y_partners_neg, EFL_UI_FOCUS_DIRECTION_UP, border_partners_set);
   convert_set(obj, pd, node, y_partners_pos, EFL_UI_FOCUS_DIRECTION_DOWN, border_partners_set);


   /*
    * Stage 2: if there is still no relation in a special direction,
    *          just take every single node that is in the given direction
    *          and take the one with the shortest direction
    */
   for(int i = EFL_UI_FOCUS_DIRECTION_UP; i < EFL_UI_FOCUS_DIRECTION_LAST; i++)
     {
        if (!DIRECTION_ACCESS(node, i).partners)
          {
             Eina_List *tmp = NULL;

             _calculate_node_indirection(pd, node->focusable, i, &tmp);
             convert_set(obj, pd, node, tmp, i, border_onedirection_set);
          }
     }

#ifdef CALC_DEBUG
   _debug_node(node);
#endif
}

static void
dirty_flush(Efl_Ui_Focus_Manager *obj, Efl_Ui_Focus_Manager_Calc_Data *pd, Node *node)
{
   if (!eina_list_data_find(pd->dirty, node)) return;

   efl_event_callback_call(obj, EFL_UI_FOCUS_MANAGER_EVENT_FLUSH_PRE, NULL);

   pd->dirty = eina_list_remove(pd->dirty, node);

   dirty_flush_node(obj, pd, node);
}

static void
dirty_flush_all(Efl_Ui_Focus_Manager *obj, Efl_Ui_Focus_Manager_Calc_Data *pd)
{
   Node *node;

   efl_event_callback_call(obj, EFL_UI_FOCUS_MANAGER_EVENT_FLUSH_PRE, NULL);

   EINA_LIST_FREE(pd->dirty, node)
     {
        dirty_flush_node(obj, pd, node);
     }
}

static void
dirty_add(Eo *obj, Efl_Ui_Focus_Manager_Calc_Data *pd, Node *dirty)
{
   if (dirty->type == NODE_TYPE_ONLY_LOGICAL)
     {
        ERR("Only not only logical nodes can be marked dirty");
        return;
     }

   //if (eina_list_data_find(pd->dirty, dirty)) return;
   pd->dirty = eina_list_remove(pd->dirty, dirty);
   pd->dirty = eina_list_append(pd->dirty, dirty);

   efl_event_callback_call(obj, EFL_UI_FOCUS_MANAGER_EVENT_COORDS_DIRTY, NULL);
}


static void
_node_new_geometry_cb(void *data, const Efl_Event *event)
{
   Node *node;
   FOCUS_DATA(data)

   node = node_get(data, pd, event->object);
   if (!node)
      return;

   dirty_add(data, pd, node);

   return;
}

static void
_object_del_cb(void *data, const Efl_Event *event)
{
   /*
    * Lets just implicitly delete items that are deleted
    * Otherwise we have later just a bunch of errors
    */
   efl_ui_focus_manager_calc_unregister(data, event->object);
}

EFL_CALLBACKS_ARRAY_DEFINE(focusable_node,
    {EFL_GFX_EVENT_RESIZE, _node_new_geometry_cb},
    {EFL_GFX_EVENT_MOVE, _node_new_geometry_cb},
    {EFL_EVENT_DEL, _object_del_cb},
);

//=============================

static Node*
_register(Eo *obj, Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Object *child, Node *parent)
{
   Node *node;
   node = eina_hash_find(pd->node_hash, &child);
   if (node)
     {
        ERR("Child %p is already registered in the graph (%s)", child, node->type == NODE_TYPE_ONLY_LOGICAL ? "logical" : "regular");
        return NULL;
     }

   node = node_new(child, obj);
   eina_hash_add(pd->node_hash, &child, node);

   //add the parent
   if (parent)
     {
        T(node).parent = parent;
        T(parent).children = eina_list_append(T(parent).children, node);
     }

   return node;
}
EOLIAN static Eina_Bool
_efl_ui_focus_manager_calc_register_logical(Eo *obj, Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Object *child, Efl_Ui_Focus_Object *parent,  Efl_Ui_Focus_Manager *redirect)
{
   Node *node = NULL;
   Node *pnode = NULL;

   EINA_SAFETY_ON_NULL_RETURN_VAL(child, EINA_FALSE);
   EINA_SAFETY_ON_NULL_RETURN_VAL(parent, EINA_FALSE);

   if (redirect)
     EINA_SAFETY_ON_FALSE_RETURN_VAL(efl_isa(redirect, EFL_UI_FOCUS_MANAGER_INTERFACE), EINA_FALSE);

   F_DBG("Manager: %p register %p %p %p", obj, child, parent, redirect);

   pnode = node_get(obj, pd, parent);
   if (!pnode) return EINA_FALSE;

   node = _register(obj, pd, child, pnode);
   if (!node) return EINA_FALSE;

   node->type = NODE_TYPE_ONLY_LOGICAL;
   node->redirect_manager = redirect;

   //set again
   if (T(pnode).saved_order)
     {
        Eina_List *tmp;

        tmp = eina_list_clone(T(pnode).saved_order);
        efl_ui_focus_manager_calc_update_order(obj, parent, tmp);
     }

   return EINA_TRUE;
}


EOLIAN static Eina_Bool
_efl_ui_focus_manager_calc_register(Eo *obj, Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Object *child, Efl_Ui_Focus_Object *parent, Efl_Ui_Focus_Manager *redirect)
{
   Node *node = NULL;
   Node *pnode = NULL;

   EINA_SAFETY_ON_NULL_RETURN_VAL(child, EINA_FALSE);
   EINA_SAFETY_ON_NULL_RETURN_VAL(parent, EINA_FALSE);

   if (redirect)
     EINA_SAFETY_ON_FALSE_RETURN_VAL(efl_isa(redirect, EFL_UI_FOCUS_MANAGER_INTERFACE), EINA_FALSE);

   F_DBG("Manager: %p register %p %p %p", obj, child, parent, redirect);

   pnode = node_get(obj, pd, parent);
   if (!pnode) return EINA_FALSE;

   node = _register(obj, pd, child, pnode);
   if (!node) return EINA_FALSE;

   //listen to changes
   efl_event_callback_array_add(child, focusable_node(), obj);

   node->type = NODE_TYPE_NORMAL;
   node->redirect_manager = redirect;

   //mark dirty
   dirty_add(obj, pd, node);

   //set again
   if (T(pnode).saved_order)
     {
        Eina_List *tmp;

        tmp = eina_list_clone(T(pnode).saved_order);
        efl_ui_focus_manager_calc_update_order(obj, parent, tmp);
     }

   return EINA_TRUE;
}

EOLIAN static Eina_Bool
_efl_ui_focus_manager_calc_update_redirect(Eo *obj, Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Object *child, Efl_Ui_Focus_Manager *redirect)
{
   Node *node = node_get(obj, pd, child);
   if (!node) return EINA_FALSE;

   if (redirect)
     EINA_SAFETY_ON_FALSE_RETURN_VAL(efl_isa(redirect, MY_CLASS), EINA_FALSE);

   node->redirect_manager = redirect;

   return EINA_TRUE;
}

EOLIAN static Eina_Bool
_efl_ui_focus_manager_calc_update_parent(Eo *obj EINA_UNUSED, Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Object *child, Efl_Ui_Focus_Object *parent_obj)
{
   Node *node;
   Node *parent;

   EINA_SAFETY_ON_NULL_RETURN_VAL(parent_obj, EINA_FALSE);
   EINA_SAFETY_ON_NULL_RETURN_VAL(child, EINA_FALSE);

   node = node_get(obj, pd, child);
   parent = node_get(obj, pd, parent_obj);

   if (!node || !parent) return EINA_FALSE;

   if (T(node).parent)
     {
        Node *old_parent;

        old_parent = T(node).parent;

        T(old_parent).children = eina_list_remove(T(old_parent).children, node);
     }

   T(node).parent = parent;

   if (T(node).parent)
     {
        T(parent).children = eina_list_append(T(parent).children, node);
     }

   return EINA_TRUE;
}

static Eina_List*
_set_a_without_b(Eina_List *a, Eina_List *b)
{
   Eina_List *a_out = NULL, *node;
   void *data;

   a_out = eina_list_clone(a);

   EINA_LIST_FOREACH(b, node, data)
     {
        a_out = eina_list_remove(a_out, data);
     }

   return a_out;
}

static Eina_Bool
_equal_set(Eina_List *none_nodes, Eina_List *nodes)
{
   Eina_List *n;
   Node *node;

   if (eina_list_count(nodes) != eina_list_count(none_nodes)) return EINA_FALSE;

   EINA_LIST_FOREACH(nodes, n, node)
     {
        if (!eina_list_data_find(none_nodes, node))
          return EINA_FALSE;
     }

   return EINA_TRUE;
}

EOLIAN static void
_efl_ui_focus_manager_calc_update_order(Eo *obj, Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Object *parent, Eina_List *order)
{
   Node *pnode;
   Efl_Ui_Focus_Object *o;
   Eina_List *node_order = NULL, *not_ordered, *trash, *node_order_clean, *n;

   F_DBG("Manager_update_order on %p %p", obj, parent);

   pnode = node_get(obj, pd, parent);
   if (!pnode)
     return;

   ELM_SAFE_FREE(T(pnode).saved_order, eina_list_free);
   T(pnode).saved_order = order;

   //get all nodes from the subset
   EINA_LIST_FOREACH(order, n, o)
     {
        Node *tmp;

        tmp = eina_hash_find(pd->node_hash, &o);

        if (!tmp) continue;

        node_order = eina_list_append(node_order, tmp);
     }

   not_ordered = _set_a_without_b(T(pnode).children, node_order);
   trash = _set_a_without_b(node_order, T(pnode).children);
   node_order_clean = _set_a_without_b(node_order, trash);

   eina_list_free(node_order);
   eina_list_free(trash);

   eina_list_free(T(pnode).children);
   T(pnode).children = eina_list_merge(node_order_clean, not_ordered);

   return;
}

EOLIAN static Eina_Bool
_efl_ui_focus_manager_calc_update_children(Eo *obj EINA_UNUSED, Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Object *parent, Eina_List *order)
{
   Node *pnode;
   Efl_Ui_Focus_Object *o;
   Eina_Bool fail = EINA_FALSE;
   Eina_List *node_order = NULL;

   pnode = node_get(obj, pd, parent);
   if (!pnode)
     return EINA_FALSE;

   //get all nodes from the subset
   EINA_LIST_FREE(order, o)
     {
        Node *tmp;

        tmp = node_get(obj, pd, o);
        if (!tmp)
          fail = EINA_TRUE;
        node_order = eina_list_append(node_order, tmp);
     }

   if (fail)
     {
        eina_list_free(node_order);
        return EINA_FALSE;
     }

   if (!_equal_set(node_order, T(pnode).children))
     {
        ERR("Set of children is not equal");
        return EINA_FALSE;
     }

   eina_list_free(T(pnode).children);
   T(pnode).children = node_order;

   return EINA_TRUE;
}

EOLIAN static void
_efl_ui_focus_manager_calc_unregister(Eo *obj EINA_UNUSED, Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Object *child)
{
   Node *node;
   Eina_Bool refocus = EINA_FALSE;

   node = eina_hash_find(pd->node_hash, &child);

   if (!node) return;

   F_DBG("Manager: %p unregister %p", obj, child);

   if (eina_list_last_data_get(pd->focus_stack) == node)
     {
        //unfocus the current head
        efl_ui_focus_object_focus_set(child, EINA_FALSE);
        refocus = EINA_TRUE;
     }


   //remove the object from the stack if it hasn't done that until now
   //after this it's not at the top anymore
   //elm_widget_focus_set(node->focusable, EINA_FALSE);
   //delete again from the list, for the case it was not at the top
   pd->focus_stack = eina_list_remove(pd->focus_stack, node);

   if (refocus)
     {
        Node *n = eina_list_last_data_get(pd->focus_stack);
        if (n)
          efl_ui_focus_object_focus_set(n->focusable, EINA_TRUE);
     }

   //add all neighbors of the node to the dirty list
   for(int i = EFL_UI_FOCUS_DIRECTION_UP; i < EFL_UI_FOCUS_DIRECTION_LAST; i++)
     {
        Node *partner;
        Eina_List *n;

        EINA_LIST_FOREACH(DIRECTION_ACCESS(node, i).partners, n, partner)
          {
             dirty_add(obj, pd, partner);
          }
     }

   //remove from the dirty parts
   pd->dirty = eina_list_remove(pd->dirty, node);

   eina_hash_del_by_key(pd->node_hash, &child);
}

EOLIAN static void
_efl_ui_focus_manager_calc_efl_ui_focus_manager_redirect_set(Eo *obj, Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Manager *redirect)
{
   Efl_Ui_Focus_Manager *old_manager;

   if (pd->redirect == redirect) return;

   F_DBG("Manager: %p setting redirect from %p to %p", obj, pd->redirect, redirect);

   old_manager = pd->redirect;

   if (pd->redirect)
     efl_wref_del(pd->redirect, &pd->redirect);

   pd->redirect = redirect;

   if (pd->redirect)
     efl_wref_add(pd->redirect, &pd->redirect);

   efl_ui_focus_manager_reset_history(old_manager);

   //we might have saved a logical element at the top, remove that if there is one
   {
      Node *n = NULL;

      n = eina_list_last_data_get(pd->focus_stack);

      if (n && n->type == NODE_TYPE_ONLY_LOGICAL && n->redirect_manager == old_manager)
        pd->focus_stack = eina_list_remove(pd->focus_stack, n);
   }

   //adjust focus property of the most upper element
   {
      Node *n = NULL;

      n = eina_list_last_data_get(pd->focus_stack);

      if (n)
        {
           if (!pd->redirect && old_manager)
             efl_ui_focus_object_focus_set(n->focusable, EINA_TRUE);
           else if (pd->redirect && !old_manager)
             efl_ui_focus_object_focus_set(n->focusable, EINA_FALSE);
        }
   }

   efl_event_callback_call(obj, EFL_UI_FOCUS_MANAGER_EVENT_REDIRECT_CHANGED , old_manager);

   //just set the root of the new redirect as focused, so it is in a known state
   if (redirect)
     {
        efl_ui_focus_manager_setup_on_first_touch(redirect, EFL_UI_FOCUS_DIRECTION_LAST, NULL);
     }
}

EOLIAN static Efl_Ui_Focus_Manager *
_efl_ui_focus_manager_calc_efl_ui_focus_manager_redirect_get(Eo *obj EINA_UNUSED, Efl_Ui_Focus_Manager_Calc_Data *pd)
{
   return pd->redirect;
}

static void
_free_node(void *data)
{
   Node *node = data;
   FOCUS_DATA(node->manager);

   efl_event_callback_array_del(node->focusable, focusable_node(), node->manager);

   if (pd->root != data)
     {
        node_item_free(node);
     }
}

EOLIAN static Efl_Object *
_efl_ui_focus_manager_calc_efl_object_constructor(Eo *obj, Efl_Ui_Focus_Manager_Calc_Data *pd)
{
   obj = efl_constructor(efl_super(obj, MY_CLASS));
   pd->node_hash = eina_hash_pointer_new(_free_node);
   return obj;
}

EOLIAN static Efl_Object *
_efl_ui_focus_manager_calc_efl_object_provider_find(const Eo *obj, Efl_Ui_Focus_Manager_Calc_Data *pd EINA_UNUSED, const Efl_Object *klass)
{
   if (klass == MY_CLASS)
     return (Efl_Object *) obj;

   return efl_provider_find(efl_super(obj, MY_CLASS), klass);
}

EOLIAN static void
_efl_ui_focus_manager_calc_efl_object_destructor(Eo *obj, Efl_Ui_Focus_Manager_Calc_Data *pd)
{
   eina_list_free(pd->focus_stack);
   eina_list_free(pd->dirty);

   eina_hash_free(pd->node_hash);

   efl_ui_focus_manager_redirect_set(obj, NULL);

   if (pd->root)
     node_item_free(pd->root);
   pd->root = NULL;

   efl_destructor(efl_super(obj, MY_CLASS));
}

typedef struct {
   Eina_Iterator iterator;
   Eina_Iterator *real_iterator;
   Efl_Ui_Focus_Manager *object;
} Border_Elements_Iterator;

static Eina_Bool
_iterator_next(Border_Elements_Iterator *it, void **data)
{
   Node *node;

   EINA_ITERATOR_FOREACH(it->real_iterator, node)
     {
        for(int i = EFL_UI_FOCUS_DIRECTION_UP ;i < EFL_UI_FOCUS_DIRECTION_LAST; i++)
          {
             if (node->type != NODE_TYPE_ONLY_LOGICAL &&
                 !DIRECTION_ACCESS(node, i).partners)
               {
                  *data = node->focusable;
                  return EINA_TRUE;
               }
          }
     }
   return EINA_FALSE;
}

static Eo *
_iterator_get_container(Border_Elements_Iterator *it)
{
   return it->object;
}

static void
_iterator_free(Border_Elements_Iterator *it)
{
   eina_iterator_free(it->real_iterator);
   free(it);
}

EOLIAN static Eina_Iterator*
_efl_ui_focus_manager_calc_efl_ui_focus_manager_border_elements_get(Eo *obj, Efl_Ui_Focus_Manager_Calc_Data *pd)
{
   Border_Elements_Iterator *it;

   dirty_flush_all(obj, pd);

   it = calloc(1, sizeof(Border_Elements_Iterator));

   EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);

   it->real_iterator = eina_hash_iterator_data_new(pd->node_hash);
   it->iterator.version = EINA_ITERATOR_VERSION;
   it->iterator.next = FUNC_ITERATOR_NEXT(_iterator_next);
   it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(_iterator_get_container);
   it->iterator.free = FUNC_ITERATOR_FREE(_iterator_free);
   it->object = obj;

   return (Eina_Iterator*) it;
}

static Node*
_no_history_element(Eina_Hash *node_hash)
{
   //nothing is selected yet, just try to use the first element in the iterator
   Eina_Iterator *iter;
   Node *upper;

   iter = eina_hash_iterator_data_new(node_hash);

   EINA_ITERATOR_FOREACH(iter, upper)
     {
        if (upper->type == NODE_TYPE_NORMAL)
          break;
     }

   eina_iterator_free(iter);

   if (upper->type != NODE_TYPE_NORMAL)
     return NULL;

   return upper;
}

static void
_get_middle(Evas_Object *obj, Eina_Vector2 *elem)
{
   Eina_Rect geom;

   geom = efl_ui_focus_object_focus_geometry_get(obj);
   elem->x = geom.x + geom.w/2;
   elem->y = geom.y + geom.h/2;
}

static Node*
_coords_movement(Efl_Ui_Focus_Manager_Calc_Data *pd, Node *upper, Efl_Ui_Focus_Direction direction)
{
   Node *candidate;
   Eina_List *node_list;
   Eina_List *lst;

   EINA_SAFETY_ON_FALSE_RETURN_VAL(DIRECTION_IS_2D(direction), NULL);

   //decide which direction we take
   lst = DIRECTION_ACCESS(upper, direction).partners;
   if (!lst)
     lst = DIRECTION_ACCESS(upper, direction).one_direction;

   //we are searching which of the partners is lower to the history
   EINA_LIST_REVERSE_FOREACH(pd->focus_stack, node_list, candidate)
     {
        //we only calculate partners for normal nodes
        if (candidate->type == NODE_TYPE_NORMAL) continue;

        if (eina_list_data_find(lst, candidate))
          {
             //this is the next accessible part
             return candidate;
          }
     }

   //if we haven't found anything in the history, use the widget with the smallest distance
   {
      Eina_List *n;
      Node *node, *min = NULL;
      Eina_Vector2 elem, other;
      float min_distance = 0.0;

      _get_middle(upper->focusable, &elem);

      EINA_LIST_FOREACH(lst, n, node)
        {
           _get_middle(node->focusable, &other);
           float tmp = eina_vector2_distance_get(&other, &elem);
           if (!min || tmp < min_distance)
             {
                min = node;
                min_distance = tmp;
             }
        }
      candidate = min;
   }
   return candidate;
}


static Node*
_prev_item(Node *node)
{
   Node *parent;
   Eina_List *lnode;

   parent = T(node).parent;

   //we are accessing the parents children, prepare!
   efl_ui_focus_object_prepare_logical(parent->focusable);

   lnode = eina_list_data_find_list(T(parent).children, node);
   lnode = eina_list_prev(lnode);

   if (lnode)
     return eina_list_data_get(lnode);
   return NULL;
}

static Node*
_next(Node *node)
{
   Node *n;

   //Case 1 we are having children
   //But only enter the children if it does NOT have a redirect manager
   if (T(node).children && !node->redirect_manager)
     {
        return eina_list_data_get(T(node).children);
     }

   //case 2 we are the root and we don't have children, return ourself
   if (!T(node).parent)
     {
        return node;
     }

   //case 3 we are not at the end of the parents list
   n = node;
   while(T(n).parent)
     {
        Node *parent;
        Eina_List *lnode;

        parent = T(n).parent;

        //we are accessing the parents children, prepare!
        efl_ui_focus_object_prepare_logical(parent->focusable);

        lnode = eina_list_data_find_list(T(parent).children, n);
        lnode = eina_list_next(lnode);

        if (lnode)
          {
             return eina_list_data_get(lnode);
          }

        n = parent;
     }

   //this is then the root again
   return NULL;
}

static Node*
_prev(Node *node)
{
   Node *n = NULL;

   //this is the root there is no parent
   if (!T(node).parent)
     return NULL;

   n =_prev_item(node);

   //we are accessing prev items children, prepare them!
   if (n && n->focusable)
     efl_ui_focus_object_prepare_logical(n->focusable);

   //case 1 there is a item in the parent previous to node, which has children
   if (n && T(n).children && !n->redirect_manager)
     {
        do
          {
              n = eina_list_last_data_get(T(n).children);
          }
        while (T(n).children && !n->redirect_manager);

        return n;
     }

   //case 2 there is a item in the parent previous to node, which has no children
   if (n)
     return n;

   //case 3 there is a no item in the parent previous to this one
   return T(node).parent;
}


static Node*
_logical_movement(Efl_Ui_Focus_Manager_Calc_Data *pd EINA_UNUSED, Node *upper, Efl_Ui_Focus_Direction direction)
{
   Node* (*deliver)(Node *n);
   Node *result;
   Eina_List *stack = NULL;

   EINA_SAFETY_ON_FALSE_RETURN_VAL(DIRECTION_IS_LOGICAL(direction), NULL);

   if (direction == EFL_UI_FOCUS_DIRECTION_NEXT)
     deliver = _next;
   else
     deliver = _prev;

   //search as long as we have a none logical parent
   result = upper;
   do
     {
        //give up, if we have already been here
        if (!!eina_list_data_find(stack, result))
          {
             eina_list_free(stack);
             ERR("Warning cycle detected\n");
             return NULL;
          }

        stack = eina_list_append(stack, result);

        if (direction == EFL_UI_FOCUS_DIRECTION_NEXT)
          efl_ui_focus_object_prepare_logical(result->focusable);

        result = deliver(result);
   } while(result && result->type != NODE_TYPE_NORMAL && !result->redirect_manager);

   eina_list_free(stack);

   return result;
}

static Efl_Ui_Focus_Object*
_request_move(Eo *obj EINA_UNUSED, Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Direction direction, Node *upper)
{
   Node *dir = NULL;

   if (!upper)
     upper = _focus_stack_last_regular(pd);

   if (!upper)
     {
        upper = _no_history_element(pd->node_hash);
        if (upper)
          return upper->focusable;
        return NULL;

     }

   dirty_flush(obj, pd, upper);

   if (direction == EFL_UI_FOCUS_DIRECTION_PREVIOUS
    || direction == EFL_UI_FOCUS_DIRECTION_NEXT)
      dir = _logical_movement(pd, upper, direction);
   else
      dir = _coords_movement(pd, upper, direction);

   //return the widget
   if (dir)
     return dir->focusable;
   else
     return NULL;
}

EOLIAN static Efl_Ui_Focus_Object*
_efl_ui_focus_manager_calc_efl_ui_focus_manager_request_move(Eo *obj EINA_UNUSED, Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Direction direction)
{
   EINA_SAFETY_ON_FALSE_RETURN_VAL(DIRECTION_CHECK(direction), NULL);

   if (pd->redirect)
     return efl_ui_focus_manager_request_move(pd->redirect, direction);
   else
     {
        Node *upper = NULL;

        upper = eina_list_last_data_get(pd->focus_stack);

        if (!upper)
          {
             upper = _no_history_element(pd->node_hash);
             if (upper)
               return upper->focusable;
             return NULL;
          }

        return _request_move(obj, pd, direction, upper);
     }
}

static Node*
_request_subchild(Node *node)
{
   //important! if there are no children _next would return the parent of node which will exceed the limit of children of node
   Node *target = NULL;

   if (node->tree.children)
     {
        target = node;

        //try to find a child that is not logical or has a redirect manager
        while (target && target->type == NODE_TYPE_ONLY_LOGICAL && !target->redirect_manager)
          {
             if (target != node)
               efl_ui_focus_object_prepare_logical(target->focusable);

             target = _next(target);
             //abort if we are exceeding the childrens of node
             if (target == node) target = NULL;
          }

        F_DBG("Found node %p", target);
     }

   return target;
}

EOLIAN static void
_efl_ui_focus_manager_calc_efl_ui_focus_manager_manager_focus_set(Eo *obj, Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Object *focus)
{
   Node *node, *last;
   Efl_Ui_Focus_Object *last_focusable = NULL, *new_focusable;
   Efl_Ui_Focus_Manager *redirect_manager;
   Node_Type node_type;

   EINA_SAFETY_ON_NULL_RETURN(focus);

   //check if node is part of this manager object
   node = node_get(obj, pd, focus);
   if (!node) return;

   if (node->type == NODE_TYPE_ONLY_LOGICAL && !node->redirect_manager)
     {
        Node *target = NULL;

        F_DBG(" %p is logical, fetching the next subnode that is either a redirect or a regular", obj);

        //important! if there are no children _next would return the parent of node which will exceed the limit of children of node
        efl_ui_focus_object_prepare_logical(node->focusable);

        target = _request_subchild(node);

        //check if we have found anything
        if (target)
          {
             node = target;
          }
        else
          {
             ERR("Could not fetch a node located at %p", node->focusable);
             return;
          }
     }

   F_DBG("Manager: %p focusing object %p %s", obj, node->focusable, efl_class_name_get(node->focusable));

   //make sure this manager is in the chain of redirects
   _manager_in_chain_set(obj, pd);

   if (eina_list_last_data_get(pd->focus_stack) == node)
     {
        //the correct one is focused
        if (node->redirect_manager == pd->redirect)
          return;
     }

   node_type = node->type;
   new_focusable = node->focusable;

   redirect_manager = node->redirect_manager;

   last = eina_list_last_data_get(pd->focus_stack);
   if (last)
     last_focusable = last->focusable;

   //remove the object from the list and add it again
   pd->focus_stack = eina_list_remove(pd->focus_stack, node);
   pd->focus_stack = eina_list_append(pd->focus_stack, node);

   if (pd->redirect)
     {
        Efl_Ui_Focus_Manager *m = obj;

        //completely unset the current redirect chain
        while (efl_ui_focus_manager_redirect_get(m))
         {
            Efl_Ui_Focus_Manager *old = m;

            m = efl_ui_focus_manager_redirect_get(m);
            efl_ui_focus_manager_redirect_set(old, NULL);
          }
     }

   //set to NULL here, from the event earlier this pointer could be dead.
   node = NULL;

   /*
     Only emit those signals if we are already at the top of the focus stack.
     Otherwise focus_get in the callback to that signal might return false.
    */
   if (node_type == NODE_TYPE_NORMAL)
     {
        //populate the new change
        if (last_focusable)
          efl_ui_focus_object_focus_set(last_focusable, EINA_FALSE);
        if (new_focusable)
          efl_ui_focus_object_focus_set(new_focusable, EINA_TRUE);
        efl_event_callback_call(obj, EFL_UI_FOCUS_MANAGER_EVENT_FOCUSED, last_focusable);
     }

   //now check if this is also a listener object
   if (redirect_manager)
     {
        //set the redirect
        efl_ui_focus_manager_redirect_set(obj, redirect_manager);
     }
}

EOLIAN static void
_efl_ui_focus_manager_calc_efl_ui_focus_manager_setup_on_first_touch(Eo *obj, Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Direction direction, Efl_Ui_Focus_Object *entry)
{
   if (direction == EFL_UI_FOCUS_DIRECTION_PREVIOUS && entry)
     {
        Efl_Ui_Focus_Manager_Logical_End_Detail last;
        Efl_Ui_Focus_Manager *rec_manager = obj;
        do
          {
             last = efl_ui_focus_manager_logical_end(rec_manager);
             EINA_SAFETY_ON_NULL_RETURN(last.element);
             efl_ui_focus_manager_focus_set(rec_manager, last.element);

             rec_manager = efl_ui_focus_manager_redirect_get(rec_manager);
          }
        while (rec_manager);
     }
   else if (DIRECTION_IS_2D(direction) && entry)
     efl_ui_focus_manager_focus_set(obj, entry);
   else
     efl_ui_focus_manager_focus_set(obj, pd->root->focusable);
}


EOLIAN static Efl_Ui_Focus_Object*
_efl_ui_focus_manager_calc_efl_ui_focus_manager_move(Eo *obj EINA_UNUSED, Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Direction direction)
{
   Efl_Ui_Focus_Object *candidate = NULL;
   Efl_Ui_Focus_Manager *early, *late;

   EINA_SAFETY_ON_FALSE_RETURN_VAL(DIRECTION_CHECK(direction), NULL);

   early = efl_ui_focus_manager_redirect_get(obj);

   if (pd->redirect)
     {
        Efl_Ui_Focus_Object *old_candidate = NULL;
        candidate = efl_ui_focus_manager_move(pd->redirect, direction);

        if (!candidate)
          {
             Efl_Ui_Focus_Object *new_candidate = NULL;

             if (DIRECTION_IS_LOGICAL(direction))
               {
                  // lets just take the last

                  Node *n = eina_list_last_data_get(pd->focus_stack);
                  new_candidate = _request_move(obj, pd, direction, n);

                  if (new_candidate)
                    efl_ui_focus_manager_focus_set(obj, new_candidate);

                  candidate = new_candidate;
               }
             else
               {
                  Node *n;

                  old_candidate = efl_ui_focus_manager_focus_get(pd->redirect);
                  n = eina_hash_find(pd->node_hash, &old_candidate);

                  if (n)
                    new_candidate = _request_move(obj, pd, direction, n);

                  if (new_candidate)
                    {
                       //redirect does not have smth. but we do have.
                       efl_ui_focus_manager_focus_set(obj, new_candidate);
                    }

                  candidate = new_candidate;
               }
          }
     }
   else
     {
        candidate = efl_ui_focus_manager_request_move(obj, direction);

        F_DBG("Manager: %p moved to %p %s in direction %d", obj, candidate, efl_class_name_get(candidate), direction);

        if (candidate)
          {
             efl_ui_focus_manager_focus_set(obj, candidate);
          }
     }

   late = efl_ui_focus_manager_redirect_get(obj);

   if (early != late)
     {
        //this is a new manager, we have to init its case!
        efl_ui_focus_manager_setup_on_first_touch(pd->redirect, direction, candidate);
     }

   return candidate;
}

EOLIAN static Eina_Bool
_efl_ui_focus_manager_calc_efl_ui_focus_manager_root_set(Eo *obj EINA_UNUSED, Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Object *root)
{
   Node *node;

   if (pd->root)
     {
        ERR("Root element can only be set once!");
        return EINA_FALSE;
     }

   node = _register(obj, pd, root, NULL);
   node->type = NODE_TYPE_ONLY_LOGICAL;

   pd->root = node;

   return EINA_TRUE;
}

EOLIAN static Efl_Ui_Focus_Object*
_efl_ui_focus_manager_calc_efl_ui_focus_manager_root_get(Eo *obj EINA_UNUSED, Efl_Ui_Focus_Manager_Calc_Data *pd)
{
   if (!pd->root) return NULL;

   return pd->root->focusable;
}

EOLIAN static Efl_Object*
_efl_ui_focus_manager_calc_efl_object_finalize(Eo *obj, Efl_Ui_Focus_Manager_Calc_Data *pd)
{
   Efl_Object *result;

   if (!pd->root)
     {
        ERR("Constructing failed. No root element set.");
        return NULL;
     }

   result = efl_finalize(efl_super(obj, MY_CLASS));

   return result;
}

static Eina_List*
_convert(Border b)
{
   Eina_List *n, *par = NULL;
   Node *node;

   EINA_LIST_FOREACH(b.partners, n, node)
     par = eina_list_append(par, node->focusable);

   EINA_LIST_FOREACH(b.one_direction, n, node)
     par = eina_list_append(par, node->focusable);

   return par;
}

EOLIAN static Efl_Ui_Focus_Object*
_efl_ui_focus_manager_calc_efl_ui_focus_manager_manager_focus_get(Eo *obj EINA_UNUSED, Efl_Ui_Focus_Manager_Calc_Data *pd)
{
   Node *upper = NULL;

   upper = eina_list_last_data_get(pd->focus_stack);

   if (!upper)
     return NULL;
   return upper->focusable;
}

EOLIAN static Efl_Ui_Focus_Relations*
_efl_ui_focus_manager_calc_efl_ui_focus_manager_fetch(Eo *obj, Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Object *child)
{
   Efl_Ui_Focus_Relations *res;
   Node *n, *tmp;

   n = node_get(obj, pd, child);
   if (!n)
      return NULL;

   res = calloc(1, sizeof(Efl_Ui_Focus_Relations));

   dirty_flush(obj, pd, n);

   //make sure to prepare_logical so next and prev are correctly
   if (n->tree.parent)
     efl_ui_focus_object_prepare_logical(n->tree.parent->focusable);
   efl_ui_focus_object_prepare_logical(n->focusable);

#define DIR_CLONE(dir) _convert(DIRECTION_ACCESS(n,dir));

   res->right = DIR_CLONE(EFL_UI_FOCUS_DIRECTION_RIGHT);
   res->left = DIR_CLONE(EFL_UI_FOCUS_DIRECTION_LEFT);
   res->top = DIR_CLONE(EFL_UI_FOCUS_DIRECTION_UP);
   res->down = DIR_CLONE(EFL_UI_FOCUS_DIRECTION_DOWN);
   res->next = (tmp = _next(n)) ? tmp->focusable : NULL;
   res->prev = (tmp = _prev(n)) ? tmp->focusable : NULL;
   res->position_in_history = eina_list_data_idx(pd->focus_stack, n);
   res->node = child;

   res->logical = (n->type == NODE_TYPE_ONLY_LOGICAL);

   if (T(n).parent)
     res->parent = T(n).parent->focusable;
   res->redirect = n->redirect_manager;
#undef DIR_CLONE

   return res;
}

EOLIAN static void
_efl_ui_focus_manager_calc_class_constructor(Efl_Class *c EINA_UNUSED)
{
   _focus_log_domain = eina_log_domain_register("elementary-focus", EINA_COLOR_CYAN);
}

EOLIAN static void
_efl_ui_focus_manager_calc_class_destructor(Efl_Class *c EINA_UNUSED)
{
   eina_log_domain_unregister(_focus_log_domain);
   _focus_log_domain = -1;
}

EOLIAN static Efl_Ui_Focus_Manager_Logical_End_Detail
_efl_ui_focus_manager_calc_efl_ui_focus_manager_logical_end(Eo *obj EINA_UNUSED, Efl_Ui_Focus_Manager_Calc_Data *pd)
{
   Node *child = pd->root;
   Efl_Ui_Focus_Manager_Logical_End_Detail ret = { 0, NULL};
   EINA_SAFETY_ON_NULL_RETURN_VAL(child, ret);

   //we need to return the most lower right element

   while ((child) && (T(child).children) && (!child->redirect_manager))
     child = eina_list_last_data_get(T(child).children);
   while ((child) && (child->type != NODE_TYPE_NORMAL) && (!child->redirect_manager))
     child = _prev(child);

   if (child)
     {
        ret.is_regular_end = child->type == NODE_TYPE_NORMAL;
        ret.element = child->focusable;
     }
   return ret;
}

EOLIAN static void
_efl_ui_focus_manager_calc_efl_ui_focus_manager_reset_history(Eo *obj EINA_UNUSED, Efl_Ui_Focus_Manager_Calc_Data *pd)
{
  Efl_Ui_Focus_Object *last_focusable;

  if (!pd->focus_stack) return;

  last_focusable = _focus_stack_unfocus_last(pd);

  pd->focus_stack = eina_list_free(pd->focus_stack);

  efl_event_callback_call(obj, EFL_UI_FOCUS_MANAGER_EVENT_FOCUSED, last_focusable);
}

EOLIAN static void
_efl_ui_focus_manager_calc_efl_ui_focus_manager_pop_history_stack(Eo *obj EINA_UNUSED, Efl_Ui_Focus_Manager_Calc_Data *pd)
{
  Efl_Ui_Focus_Object *last_focusable;
  Node *last;

  if (!pd->focus_stack) return;

  last_focusable = _focus_stack_unfocus_last(pd);

  //get now the highest, and unfocus that!
  last = eina_list_last_data_get(pd->focus_stack);
  if (last) efl_ui_focus_object_focus_set(last->focusable, EINA_TRUE);
  efl_event_callback_call(obj, EFL_UI_FOCUS_MANAGER_EVENT_FOCUSED, last_focusable);
}

EOLIAN static Efl_Ui_Focus_Object*
_efl_ui_focus_manager_calc_efl_ui_focus_manager_request_subchild(Eo *obj, Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Ui_Focus_Object *child_obj)
{
   Node *child, *target;

   child = node_get(obj, pd, child_obj);
   target = _request_subchild(child);

   if (target) return target->focusable;
   return NULL;
}

EOLIAN static void
_efl_ui_focus_manager_calc_efl_object_dbg_info_get(Eo *obj, Efl_Ui_Focus_Manager_Calc_Data *pd, Efl_Dbg_Info *root)
{
   efl_dbg_info_get(efl_super(obj, MY_CLASS), root);
   Efl_Dbg_Info *append, *group = EFL_DBG_INFO_LIST_APPEND(root, "Efl.Ui.Focus.Manager");
   Eina_Iterator *iter;
   Node *node;

   append = EFL_DBG_INFO_LIST_APPEND(group, "children");

   iter = eina_hash_iterator_data_new(pd->node_hash);
   EINA_ITERATOR_FOREACH(iter, node)
     {
        EFL_DBG_INFO_APPEND(append, "-", EINA_VALUE_TYPE_UINT64, node->focusable);
     }
   eina_iterator_free(iter);
}

#define EFL_UI_FOCUS_MANAGER_CALC_EXTRA_OPS \
   EFL_OBJECT_OP_FUNC(efl_dbg_info_get, _efl_ui_focus_manager_calc_efl_object_dbg_info_get)

#include "efl_ui_focus_manager_calc.eo.c"