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
|
/**
* Navit, a modular navigation system.
* Copyright (C) 2005-2008 Navit Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <string.h>
#define DBUS_API_SUBJECT_TO_CHANGE
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include "config.h"
#include "config_.h"
#include "navit.h"
#include "coord.h"
#include "point.h"
#include "plugin.h"
#include "debug.h"
#include "item.h"
#include "xmlconfig.h"
#include "attr.h"
#include "layout.h"
#include "navigation.h"
#include "command.h"
#include "callback.h"
#include "graphics.h"
#include "track.h"
#include "vehicle.h"
#include "vehicleprofile.h"
#include "map.h"
#include "mapset.h"
#include "osd.h"
#include "route.h"
#include "search.h"
#include "callback.h"
#include "gui.h"
#include "layout.h"
#include "roadprofile.h"
#include "util.h"
#include "transform.h"
#include "event.h"
static DBusConnection *connection;
static dbus_uint32_t dbus_serial;
static char *service_name = "org.navit_project.navit";
static char *object_path = "/org/navit_project/navit";
char *navitintrospectxml_head1 = "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
"\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
"<node name=\"";
char *navitintrospectxml_head2 = "\">\n"
" <interface name=\"org.freedesktop.DBus.Introspectable\">\n"
" <method name=\"Introspect\">\n"
" <arg direction=\"out\" type=\"s\" />\n"
" </method>\n"
" </interface>\n";
GHashTable *object_hash;
GHashTable *object_hash_rev;
GHashTable *object_count;
struct dbus_callback {
struct callback *callback;
char *signal;
};
static char *
object_new(char *type, void *object)
{
int id;
char *ret;
dbg(lvl_debug,"enter %s", type);
if ((ret=g_hash_table_lookup(object_hash_rev, object)))
return ret;
id=GPOINTER_TO_INT(g_hash_table_lookup(object_count, type));
g_hash_table_insert(object_count, type, GINT_TO_POINTER((id+1)));
ret=g_strdup_printf("%s/%s/%d", object_path, type, id);
g_hash_table_insert(object_hash, ret, object);
g_hash_table_insert(object_hash_rev, object, ret);
dbg(lvl_debug,"return %s", ret);
return (ret);
}
static void *
object_get(const char *path)
{
return g_hash_table_lookup(object_hash, path);
}
static void
object_destroy(const char *path, void *object)
{
if (!path && !object)
return;
if (!object)
object=g_hash_table_lookup(object_hash, path);
if (!path)
path=g_hash_table_lookup(object_hash_rev, object);
g_hash_table_remove(object_hash, path);
g_hash_table_remove(object_hash_rev, object);
}
static void *
resolve_object(const char *opath, char *type)
{
char *prefix;
const char *oprefix;
void *ret=NULL;
char *def_navit="/default_navit";
char *def_gui="/default_gui";
char *def_graphics="/default_graphics";
char *def_vehicle="/default_vehicle";
char *def_vehicleprofile="/default_vehicleprofile";
char *def_mapset="/default_mapset";
char *def_map="/default_map";
char *def_navigation="/default_navigation";
char *def_route="/default_route";
char *def_tracking="/default_tracking";
struct attr attr;
if (strncmp(opath, object_path, strlen(object_path))) {
dbg(lvl_error,"wrong object path %s",opath);
return NULL;
}
prefix=g_strdup_printf("%s/%s/", object_path, type);
if (!strncmp(prefix, opath, strlen(prefix))) {
ret=object_get(opath);
g_free(prefix);
return ret;
}
g_free(prefix);
oprefix=opath+strlen(object_path);
if (!strncmp(oprefix,def_navit,strlen(def_navit))) {
oprefix+=strlen(def_navit);
struct attr navit;
if (!config_get_attr(config, attr_navit, &navit, NULL))
return NULL;
if (!oprefix[0]) {
dbg(lvl_debug,"default_navit");
return navit.u.navit;
}
if (!strncmp(oprefix,def_graphics,strlen(def_graphics))) {
if (navit_get_attr(navit.u.navit, attr_graphics, &attr, NULL)) {
return attr.u.graphics;
}
return NULL;
}
if (!strncmp(oprefix,def_gui,strlen(def_gui))) {
if (navit_get_attr(navit.u.navit, attr_gui, &attr, NULL)) {
return attr.u.gui;
}
return NULL;
}
if (!strncmp(oprefix,def_vehicleprofile,strlen(def_vehicleprofile))) {
if (navit_get_attr(navit.u.navit, attr_vehicleprofile, &attr, NULL)) {
return attr.u.vehicleprofile;
}
return NULL;
}
if (!strncmp(oprefix,def_vehicle,strlen(def_vehicle))) {
if (navit_get_attr(navit.u.navit, attr_vehicle, &attr, NULL)) {
return attr.u.vehicle;
}
return NULL;
}
if (!strncmp(oprefix,def_mapset,strlen(def_mapset))) {
oprefix+=strlen(def_mapset);
if (navit_get_attr(navit.u.navit, attr_mapset, &attr, NULL)) {
if (!oprefix[0]) {
return attr.u.mapset;
}
if (!strncmp(oprefix,def_map,strlen(def_map))) {
if (mapset_get_attr(attr.u.mapset, attr_map, &attr, NULL)) {
return attr.u.map;
}
return NULL;
}
}
return NULL;
}
if (!strncmp(oprefix,def_navigation,strlen(def_navigation))) {
if (navit_get_attr(navit.u.navit, attr_navigation, &attr, NULL)) {
return attr.u.navigation;
}
return NULL;
}
if (!strncmp(oprefix,def_route,strlen(def_route))) {
oprefix+=strlen(def_route);
if (navit_get_attr(navit.u.navit, attr_route, &attr, NULL)) {
return attr.u.route;
}
return NULL;
}
if (!strncmp(oprefix,def_tracking,strlen(def_tracking))) {
if (navit_get_attr(navit.u.navit, attr_trackingo, &attr, NULL)) {
return attr.u.tracking;
}
return NULL;
}
}
return NULL;
}
static void *
object_get_from_message_arg(DBusMessageIter *iter, char *type)
{
char *opath;
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_OBJECT_PATH)
return NULL;
dbus_message_iter_get_basic(iter, &opath);
dbus_message_iter_next(iter);
return resolve_object(opath, type);
}
static void *
object_get_from_message(DBusMessage *message, char *type)
{
return resolve_object(dbus_message_get_path(message), type);
}
static enum attr_type
attr_type_get_from_message(DBusMessageIter *iter)
{
char *attr_type;
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING)
return attr_none;
dbus_message_iter_get_basic(iter, &attr_type);
dbus_message_iter_next(iter);
return attr_from_name(attr_type);
}
static void
encode_variant_string(DBusMessageIter *iter, char *str)
{
DBusMessageIter variant;
dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, DBUS_TYPE_STRING_AS_STRING, &variant);
dbus_message_iter_append_basic(&variant, DBUS_TYPE_STRING, &str);
dbus_message_iter_close_container(iter, &variant);
}
static void
encode_dict_string_variant_string(DBusMessageIter *iter, char *key, char *value)
{
DBusMessageIter dict;
dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &dict);
dbus_message_iter_append_basic(&dict, DBUS_TYPE_STRING, &key);
encode_variant_string(&dict, value);
dbus_message_iter_close_container(iter, &dict);
}
static int
encode_attr(DBusMessageIter *iter1, struct attr *attr)
{
char *name=attr_to_name(attr->type);
DBusMessageIter iter2,iter3;
dbus_message_iter_append_basic(iter1, DBUS_TYPE_STRING, &name);
if (attr->type >= attr_type_int_begin && attr->type < attr_type_boolean_begin) {
dbus_message_iter_open_container(iter1, DBUS_TYPE_VARIANT, DBUS_TYPE_INT32_AS_STRING, &iter2);
dbus_message_iter_append_basic(&iter2, DBUS_TYPE_INT32, &attr->u.num);
dbus_message_iter_close_container(iter1, &iter2);
}
if (attr->type >= attr_type_boolean_begin && attr->type <= attr_type_int_end) {
dbus_message_iter_open_container(iter1, DBUS_TYPE_VARIANT, DBUS_TYPE_BOOLEAN_AS_STRING, &iter2);
dbus_message_iter_append_basic(&iter2, DBUS_TYPE_BOOLEAN, &attr->u.num);
dbus_message_iter_close_container(iter1, &iter2);
}
if (attr->type >= attr_type_string_begin && attr->type <= attr_type_string_end) {
encode_variant_string(iter1, attr->u.str);
}
if ((attr->type >= attr_type_item_type_begin && attr->type <= attr_type_item_type_end) || attr->type == attr_item_type) {
encode_variant_string(iter1, item_to_name(attr->u.item_type));
}
if (attr->type >= attr_type_pcoord_begin && attr->type <= attr_type_pcoord_end) {
dbus_message_iter_open_container(iter1, DBUS_TYPE_VARIANT, "ai", &iter2);
dbus_message_iter_open_container(&iter2, DBUS_TYPE_ARRAY, "i", &iter3);
if (attr->u.pcoord) {
dbus_message_iter_append_basic(&iter3, DBUS_TYPE_INT32, &attr->u.pcoord->pro);
dbus_message_iter_append_basic(&iter3, DBUS_TYPE_INT32, &attr->u.pcoord->x);
dbus_message_iter_append_basic(&iter3, DBUS_TYPE_INT32, &attr->u.pcoord->y);
}
dbus_message_iter_close_container(&iter2, &iter3);
dbus_message_iter_close_container(iter1, &iter2);
}
if (attr->type >= attr_type_object_begin && attr->type <= attr_type_object_end) {
char *object=object_new(attr_to_name(attr->type), attr->u.data);
dbus_message_iter_open_container(iter1, DBUS_TYPE_VARIANT, DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter2);
dbus_message_iter_append_basic(&iter2, DBUS_TYPE_OBJECT_PATH, &object);
dbus_message_iter_close_container(iter1, &iter2);
}
if (attr->type == attr_item_types) {
char *str=attr_to_text(attr,NULL,0);
encode_variant_string(iter1, str);
g_free(str);
}
return 1;
}
static DBusHandlerResult
empty_reply(DBusConnection *connection, DBusMessage *message)
{
DBusMessage *reply;
reply = dbus_message_new_method_return(message);
dbus_connection_send (connection, reply, NULL);
dbus_message_unref (reply);
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult
dbus_error(DBusConnection *connection, DBusMessage *message, char *error, char *msg)
{
DBusMessage *reply;
reply = dbus_message_new_error(message, error, msg);
dbus_connection_send (connection, reply, NULL);
dbus_message_unref (reply);
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult
dbus_error_invalid_attr_type(DBusConnection *connection, DBusMessage *message)
{
return dbus_error(connection, message, DBUS_ERROR_INVALID_ARGS, "attribute type invalid");
}
static DBusHandlerResult
dbus_error_invalid_parameter(DBusConnection *connection, DBusMessage *message)
{
return dbus_error(connection, message, DBUS_ERROR_INVALID_ARGS, "parameter invalid");
}
static DBusHandlerResult
dbus_error_invalid_object_path(DBusConnection *connection, DBusMessage *message)
{
return dbus_error(connection, message, DBUS_ERROR_BAD_ADDRESS, "object path invalid");
}
static DBusHandlerResult
dbus_error_invalid_object_path_parameter(DBusConnection *connection, DBusMessage *message)
{
return dbus_error(connection, message, DBUS_ERROR_BAD_ADDRESS, "object path parameter invalid");
}
static DBusHandlerResult
dbus_error_navigation_not_configured(DBusConnection *connection, DBusMessage *message)
{
return dbus_error(connection, message, DBUS_ERROR_FAILED, "navigation is not configured (no <navigation> element in config file?)");
}
static DBusHandlerResult
dbus_error_no_data_available(DBusConnection *connection, DBusMessage *message)
{
#if 1
return dbus_error(connection, message, DBUS_ERROR_FILE_NOT_FOUND, "no data available");
#else
return empty_reply(connection, message);
#endif
}
#if 0
static void
dbus_dump_iter(char *prefix, DBusMessageIter *iter)
{
char *prefixr,*vals;
int arg,vali;
DBusMessageIter iterr;
while ((arg=dbus_message_iter_get_arg_type(iter)) != DBUS_TYPE_INVALID) {
switch (arg) {
case DBUS_TYPE_INT32:
dbus_message_iter_get_basic(iter, &vali);
dbg(lvl_debug,"%sDBUS_TYPE_INT32: %d",prefix,vali);
break;
case DBUS_TYPE_STRING:
dbus_message_iter_get_basic(iter, &vals);
dbg(lvl_debug,"%sDBUS_TYPE_STRING: %s",prefix,vals);
break;
case DBUS_TYPE_STRUCT:
dbg(lvl_debug,"%sDBUS_TYPE_STRUCT:",prefix);
prefixr=g_strdup_printf("%s ",prefix);
dbus_message_iter_recurse(iter, &iterr);
dbus_dump_iter(prefixr, &iterr);
g_free(prefixr);
break;
case DBUS_TYPE_VARIANT:
dbg(lvl_debug,"%sDBUS_TYPE_VARIANT:",prefix);
prefixr=g_strdup_printf("%s ",prefix);
dbus_message_iter_recurse(iter, &iterr);
dbus_dump_iter(prefixr, &iterr);
g_free(prefixr);
break;
case DBUS_TYPE_DICT_ENTRY:
dbg(lvl_debug,"%sDBUS_TYPE_DICT_ENTRY:",prefix);
prefixr=g_strdup_printf("%s ",prefix);
dbus_message_iter_recurse(iter, &iterr);
dbus_dump_iter(prefixr, &iterr);
g_free(prefixr);
break;
case DBUS_TYPE_ARRAY:
dbg(lvl_debug,"%sDBUS_TYPE_ARRAY:",prefix);
prefixr=g_strdup_printf("%s ",prefix);
dbus_message_iter_recurse(iter, &iterr);
dbus_dump_iter(prefixr, &iterr);
g_free(prefixr);
break;
default:
dbg(lvl_debug,"%c",arg);
}
dbus_message_iter_next(iter);
}
}
static void
dbus_dump(DBusMessage *message)
{
DBusMessageIter iter;
dbus_message_iter_init(message, &iter);
dbus_dump_iter("",&iter);
}
#endif
/**
* Extracts a struct pcoord from a DBus message
*
* @param message The DBus message
* @param iter Sort of pointer that points on that (iii)-object in the message
* @param pc Pointer where the data should get stored
* @returns Returns 1 when everything went right, otherwise 0
*/
static int
pcoord_get_from_message(DBusMessage *message, DBusMessageIter *iter, struct pcoord *pc)
{
if(!strcmp(dbus_message_iter_get_signature(iter), "s")) {
char *coordstring;
dbus_message_iter_get_basic(iter, &coordstring);
if(!pcoord_parse(coordstring, projection_mg, pc))
return 0;
return 1;
} else {
DBusMessageIter iter2;
dbus_message_iter_recurse(iter, &iter2);
if(!strcmp(dbus_message_iter_get_signature(iter), "(is)")) {
char *coordstring;
int projection;
dbus_message_iter_get_basic(&iter2, &projection);
dbus_message_iter_next(&iter2);
dbus_message_iter_get_basic(&iter2, &coordstring);
if(!pcoord_parse(coordstring, projection, pc))
return 0;
return 1;
} else if(!strcmp(dbus_message_iter_get_signature(iter), "(iii)")) {
dbus_message_iter_get_basic(&iter2, &pc->pro);
dbus_message_iter_next(&iter2);
dbus_message_iter_get_basic(&iter2, &pc->x);
dbus_message_iter_next(&iter2);
dbus_message_iter_get_basic(&iter2, &pc->y);
return 1;
}
}
return 0;
}
static void
pcoord_encode(DBusMessageIter *iter, struct pcoord *pc)
{
DBusMessageIter iter2;
dbus_message_iter_open_container(iter,DBUS_TYPE_STRUCT,NULL,&iter2);
if (pc) {
dbus_message_iter_append_basic(&iter2, DBUS_TYPE_INT32, &pc->pro);
dbus_message_iter_append_basic(&iter2, DBUS_TYPE_INT32, &pc->x);
dbus_message_iter_append_basic(&iter2, DBUS_TYPE_INT32, &pc->y);
} else {
int n=0;
dbus_message_iter_append_basic(&iter2, DBUS_TYPE_INT32, &n);
dbus_message_iter_append_basic(&iter2, DBUS_TYPE_INT32, &n);
dbus_message_iter_append_basic(&iter2, DBUS_TYPE_INT32, &n);
}
dbus_message_iter_close_container(iter, &iter2);
}
static enum attr_type
decode_attr_type_from_iter(DBusMessageIter *iter)
{
char *attr_type;
enum attr_type ret;
if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING)
return attr_none;
dbus_message_iter_get_basic(iter, &attr_type);
dbus_message_iter_next(iter);
ret=attr_from_name(attr_type);
dbg(lvl_debug, "attr value: 0x%x string: %s", ret, attr_type);
return ret;
}
static int
decode_attr_from_iter(DBusMessageIter *iter, struct attr *attr)
{
DBusMessageIter iterattr, iterstruct;
int ret=1;
double d;
attr->type=decode_attr_type_from_iter(iter);
if (attr->type == attr_none)
return 0;
dbus_message_iter_recurse(iter, &iterattr);
dbus_message_iter_next(iter);
dbg(lvl_debug, "seems valid. signature: %s", dbus_message_iter_get_signature(&iterattr));
if (attr->type >= attr_type_item_begin && attr->type <= attr_type_item_end)
return 0;
if (attr->type >= attr_type_int_begin && attr->type <= attr_type_boolean_begin) {
if (dbus_message_iter_get_arg_type(&iterattr) == DBUS_TYPE_INT32) {
int val;
dbus_message_iter_get_basic(&iterattr, &val);
attr->u.num=val;
return 1;
}
return 0;
}
if(attr->type >= attr_type_boolean_begin && attr->type <= attr_type_int_end) {
if (dbus_message_iter_get_arg_type(&iterattr) == DBUS_TYPE_BOOLEAN) {
int val;
dbus_message_iter_get_basic(&iterattr, &val);
attr->u.num=val;
return 1;
}
return 0;
}
if(attr->type >= attr_type_string_begin && attr->type <= attr_type_string_end) {
if (dbus_message_iter_get_arg_type(&iterattr) == DBUS_TYPE_STRING) {
dbus_message_iter_get_basic(&iterattr, &attr->u.str);
return 1;
}
return 0;
}
if(attr->type >= attr_type_double_begin && attr->type <= attr_type_double_end) {
if (dbus_message_iter_get_arg_type(&iterattr) == DBUS_TYPE_DOUBLE) {
attr->u.numd=g_new(typeof(*attr->u.numd),1);
dbus_message_iter_get_basic(&iterattr, attr->u.numd);
return 1;
}
}
if(attr->type >= attr_type_object_begin && attr->type <= attr_type_object_end) {
if (dbus_message_iter_get_arg_type(&iterattr) == DBUS_TYPE_OBJECT_PATH) {
char *obj;
dbus_message_iter_get_basic(&iterattr, &obj);
attr->u.data=object_get(obj);
if (attr->u.data) {
return 1;
}
}
return 0;
}
if(attr->type >= attr_type_coord_geo_begin && attr->type <= attr_type_coord_geo_end) {
if (dbus_message_iter_get_arg_type(&iterattr) == DBUS_TYPE_STRUCT) {
attr->u.coord_geo=g_new(typeof(*attr->u.coord_geo),1);
dbus_message_iter_recurse(&iterattr, &iterstruct);
if (dbus_message_iter_get_arg_type(&iterstruct) == DBUS_TYPE_DOUBLE) {
dbus_message_iter_get_basic(&iterstruct, &d);
dbus_message_iter_next(&iterstruct);
attr->u.coord_geo->lng=d;
} else
ret=0;
if (dbus_message_iter_get_arg_type(&iterstruct) == DBUS_TYPE_DOUBLE) {
dbus_message_iter_get_basic(&iterstruct, &d);
attr->u.coord_geo->lat=d;
} else
ret=0;
if (!ret) {
g_free(attr->u.coord_geo);
attr->u.coord_geo=NULL;
}
return ret;
}
}
if(attr->type >= attr_type_pcoord_begin && attr->type <= attr_type_pcoord_end) {
int i;
if (dbus_message_iter_get_arg_type(&iterattr) == DBUS_TYPE_STRUCT) {
attr->u.pcoord=g_new(typeof(*attr->u.pcoord),1);
dbus_message_iter_recurse(&iterattr, &iterstruct);
if (dbus_message_iter_get_arg_type(&iterstruct) == DBUS_TYPE_INT32) {
dbus_message_iter_get_basic(&iterstruct, &i);
dbus_message_iter_next(&iterstruct);
attr->u.pcoord->pro=i;
} else
ret=0;
if (dbus_message_iter_get_arg_type(&iterstruct) == DBUS_TYPE_INT32) {
dbus_message_iter_get_basic(&iterstruct, &i);
dbus_message_iter_next(&iterstruct);
attr->u.pcoord->x=i;
} else
ret=0;
if (dbus_message_iter_get_arg_type(&iterstruct) == DBUS_TYPE_INT32) {
dbus_message_iter_get_basic(&iterstruct, &i);
attr->u.pcoord->y=i;
} else
ret=0;
if (!ret) {
g_free(attr->u.pcoord);
attr->u.pcoord=NULL;
}
return ret;
}
}
if (attr->type == attr_callback) {
struct dbus_callback *callback=object_get_from_message_arg(&iterattr, "callback");
if (callback) {
attr->u.callback=callback->callback;
return 1;
}
}
return 0;
}
static int
decode_attr(DBusMessage *message, struct attr *attr)
{
DBusMessageIter iter;
dbus_message_iter_init(message, &iter);
return decode_attr_from_iter(&iter, attr);
}
static void
destroy_attr(struct attr *attr)
{
if(attr->type > attr_type_double_begin && attr->type < attr_type_double_end) {
g_free(attr->u.numd);
}
}
static char *
get_iter_name(char *type)
{
return g_strdup_printf("%s_attr_iter",type);
}
static DBusHandlerResult
request_attr_iter(DBusConnection *connection, DBusMessage *message, char *type, struct attr_iter *(*func)(void))
{
DBusMessage *reply;
char *iter_name;
char *opath;
struct attr_iter *attr_iter;
attr_iter=(*func)();
iter_name=get_iter_name(type);
opath=object_new(iter_name,attr_iter);
g_free(iter_name);
reply = dbus_message_new_method_return(message);
dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &opath, DBUS_TYPE_INVALID);
dbus_connection_send (connection, reply, NULL);
dbus_message_unref (reply);
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult
request_attr_iter_destroy(DBusConnection *connection, DBusMessage *message, char *type, void (*func)(struct attr_iter *))
{
struct attr_iter *attr_iter;
DBusMessageIter iter;
char *iter_name;
dbus_message_iter_init(message, &iter);
iter_name=get_iter_name(type);
attr_iter=object_get_from_message_arg(&iter, iter_name);
g_free(iter_name);
if (! attr_iter)
return dbus_error_invalid_object_path_parameter(connection, message);
object_destroy(NULL, attr_iter);
func(attr_iter);
return empty_reply(connection, message);
}
static DBusHandlerResult
request_destroy(DBusConnection *connection, DBusMessage *message, char *type, void *data, void (*func)(void *))
{
if (!data)
data=object_get_from_message(message, type);
if (!data)
return dbus_error_invalid_object_path(connection, message);
object_destroy(NULL, data);
func(data);
return empty_reply(connection, message);
}
static DBusHandlerResult
request_dup(DBusConnection *connection, DBusMessage *message, char *type, void *data, void *(*func)(void *))
{
DBusMessage *reply;
char *opath;
void *obj;
if (!data)
data=object_get_from_message(message, type);
if (!data)
return dbus_error_invalid_object_path(connection, message);
obj=func(data);
opath=object_new(type,obj);
reply = dbus_message_new_method_return(message);
dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &opath, DBUS_TYPE_INVALID);
dbus_connection_send (connection, reply, NULL);
dbus_message_unref (reply);
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult
request_get_attr(DBusConnection *connection, DBusMessage *message, char *type, void *data, int (*func)(void *data, enum attr_type type, struct attr *attr, struct attr_iter *iter))
{
DBusMessage *reply;
DBusMessageIter iter;
struct attr attr;
enum attr_type attr_type;
struct attr_iter *attr_iter;
char *iter_name;
if (! data)
data = object_get_from_message(message, type);
if (! data)
return dbus_error_invalid_object_path(connection, message);
dbus_message_iter_init(message, &iter);
attr_type=attr_type_get_from_message(&iter);
if (attr_type == attr_none)
return dbus_error_invalid_attr_type(connection, message);
iter_name=get_iter_name(type);
attr_iter=object_get_from_message_arg(&iter, iter_name);
g_free(iter_name);
if (func(data, attr_type, &attr, attr_iter)) {
DBusMessageIter iter1;
reply = dbus_message_new_method_return(message);
dbus_message_iter_init_append(reply, &iter1);
encode_attr(&iter1, &attr);
dbus_connection_send (connection, reply, NULL);
dbus_message_unref (reply);
return DBUS_HANDLER_RESULT_HANDLED;
}
return dbus_error_no_data_available(connection, message);
}
static DBusHandlerResult
request_command(DBusConnection *connection, DBusMessage *message, char *type, void *data, int (*func)(void *data, enum attr_type type, struct attr *attr, struct attr_iter *iter))
{
DBusMessageIter iter;
struct attr attr;
char *command;
if (! data)
data = object_get_from_message(message, type);
if (! data)
return dbus_error_invalid_object_path(connection, message);
dbus_message_iter_init(message, &iter);
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
return dbus_error_invalid_parameter(connection, message);
dbus_message_iter_get_basic(&iter, &command);
dbus_message_iter_next(&iter);
if (func(data, attr_callback_list, &attr, NULL)) {
int valid=0;
callback_list_call_attr_4(attr.u.callback_list, attr_command, command, NULL, NULL, &valid);
}
return empty_reply(connection, message);
}
static DBusHandlerResult
request_set_add_remove_attr(DBusConnection *connection, DBusMessage *message, char *type, void *data, int (*func)(void *data, struct attr *attr))
{
struct attr attr;
int ret;
if (! data)
data = object_get_from_message(message, type);
if (! data)
return dbus_error_invalid_object_path(connection, message);
if (decode_attr(message, &attr)) {
ret=(*func)(data, &attr);
destroy_attr(&attr);
if (ret)
return empty_reply(connection, message);
dbg(lvl_error,"failed to set/add/remove attr");
} else {
dbg(lvl_error,"failed to decode attr");
}
return dbus_error_invalid_parameter(connection, message);
}
/* callback */
static void
dbus_callback_emit_signal(struct dbus_callback *dbus_callback)
{
DBusMessage* msg;
msg = dbus_message_new_signal(object_path, service_name, dbus_callback->signal);
if (msg) {
dbus_connection_send(connection, msg, &dbus_serial);
dbus_connection_flush(connection);
dbus_message_unref(msg);
}
}
static void
request_callback_destroy_do(struct dbus_callback *data)
{
callback_destroy(data->callback);
g_free(data->signal);
g_free(data);
}
static DBusHandlerResult
request_callback_destroy(DBusConnection *connection, DBusMessage *message)
{
return request_destroy(connection, message, "callback", NULL, (void (*)(void *)) request_callback_destroy_do);
}
static DBusHandlerResult
request_callback_new(DBusConnection *connection, DBusMessage *message)
{
DBusMessageIter iter;
DBusMessage *reply;
struct dbus_callback *callback;
char *signal,*opath;
enum attr_type type;
dbus_message_iter_init(message, &iter);
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
return dbus_error_invalid_parameter(connection, message);
dbus_message_iter_get_basic(&iter, &signal);
dbus_message_iter_next(&iter);
callback=g_new0(struct dbus_callback, 1);
callback->signal=g_strdup(signal);
if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING) {
type=attr_type_get_from_message(&iter);
callback->callback=callback_new_attr_1(callback_cast(dbus_callback_emit_signal), type, callback);
} else
callback->callback=callback_new_1(callback_cast(dbus_callback_emit_signal), callback);
opath=object_new("callback", callback);
reply = dbus_message_new_method_return(message);
dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &opath, DBUS_TYPE_INVALID);
dbus_connection_send (connection, reply, NULL);
dbus_message_unref (reply);
return DBUS_HANDLER_RESULT_HANDLED;
}
/* config */
static DBusHandlerResult
request_config_get_attr(DBusConnection *connection, DBusMessage *message)
{
return request_get_attr(connection, message, "config", config, (int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))config_get_attr);
}
static DBusHandlerResult
request_config_attr_iter(DBusConnection *connection, DBusMessage *message)
{
return request_attr_iter(connection, message, "config", (struct attr_iter * (*)(void))config_attr_iter_new);
}
static DBusHandlerResult
request_config_attr_iter_destroy(DBusConnection *connection, DBusMessage *message)
{
return request_attr_iter_destroy(connection, message, "config", (void (*)(struct attr_iter *))config_attr_iter_destroy);
}
/* graphics */
static DBusHandlerResult
request_graphics_get_data(DBusConnection *connection, DBusMessage *message)
{
struct graphics *graphics;
char *data;
struct graphics_data_image *image;
DBusMessage *reply;
graphics = object_get_from_message(message, "graphics");
if (! graphics)
return dbus_error_invalid_object_path(connection, message);
if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &data, DBUS_TYPE_INVALID))
return dbus_error_invalid_parameter(connection, message);
image=graphics_get_data(graphics, data);
if (image) {
DBusMessageIter iter1,iter2;
reply = dbus_message_new_method_return(message);
#if 0
dbus_message_append_args(reply, DBUS_TYPE_STRING, &result, DBUS_TYPE_INVALID);
#endif
dbus_message_iter_init_append(reply, &iter1);
dbus_message_iter_open_container(&iter1, DBUS_TYPE_ARRAY, "y", &iter2);
if (image->data && image->size)
dbus_message_iter_append_fixed_array(&iter2, DBUS_TYPE_BYTE, &image->data, image->size);
dbus_message_iter_close_container(&iter1, &iter2);
dbus_connection_send (connection, reply, NULL);
dbus_message_unref (reply);
return DBUS_HANDLER_RESULT_HANDLED;
}
return dbus_error_no_data_available(connection, message);
}
/* gui */
static DBusHandlerResult
request_gui_get_attr(DBusConnection *connection, DBusMessage *message)
{
return request_get_attr(connection, message, "gui", NULL, (int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))gui_get_attr);
}
static DBusHandlerResult
request_gui_command(DBusConnection *connection, DBusMessage *message)
{
return request_command(connection, message, "gui", NULL, (int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))gui_get_attr);
}
static DBusHandlerResult
request_graphics_set_attr(DBusConnection *connection, DBusMessage *message)
{
return request_set_add_remove_attr(connection, message, "graphics", NULL, (int (*)(void *, struct attr *))graphics_set_attr);
}
/* layout */
static DBusHandlerResult
request_layout_get_attr(DBusConnection *connection, DBusMessage *message)
{
return request_get_attr(connection, message, "layout", NULL, (int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))layout_get_attr);
}
/* map */
static DBusHandlerResult
request_map_get_attr(DBusConnection *connection, DBusMessage *message)
{
return request_get_attr(connection, message, "map", NULL, (int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))map_get_attr);
}
static DBusHandlerResult
request_map_set_attr(DBusConnection *connection, DBusMessage *message)
{
return request_set_add_remove_attr(connection, message, "map", NULL, (int (*)(void *, struct attr *))map_set_attr);
}
static DBusHandlerResult
request_map_dump(DBusConnection *connection, DBusMessage *message)
{
DBusMessageIter iter;
struct map *map;
map=object_get_from_message(message, "map");
if (! map)
return dbus_error_invalid_object_path(connection, message);
dbus_message_iter_init(message, &iter);
if(!strcmp(dbus_message_iter_get_signature(&iter), "s")) {
char *file;
FILE *f;
dbus_message_iter_get_basic(&iter, &file);
/* dbg(0,"File '%s'",file); */
f=fopen(file,"w");
map_dump_filedesc(map,f);
fclose(f);
return empty_reply(connection, message);
}
return dbus_error_invalid_parameter(connection, message);
}
/* mapset */
static DBusHandlerResult
request_mapset_attr_iter(DBusConnection *connection, DBusMessage *message)
{
return request_attr_iter(connection, message, "mapset", (struct attr_iter * (*)(void))mapset_attr_iter_new);
}
static DBusHandlerResult
request_mapset_attr_iter_destroy(DBusConnection *connection, DBusMessage *message)
{
return request_attr_iter_destroy(connection, message, "mapset", (void (*)(struct attr_iter *))mapset_attr_iter_destroy);
}
static DBusHandlerResult
request_mapset_get_attr(DBusConnection *connection, DBusMessage *message)
{
return request_get_attr(connection, message, "mapset", NULL, (int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))mapset_get_attr);
}
/* navigation */
static DBusHandlerResult
request_navigation_get_attr(DBusConnection *connection, DBusMessage *message)
{
return request_get_attr(connection, message, "navigation", NULL, (int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))navigation_get_attr);
}
/* osd */
static DBusHandlerResult
request_osd_get_attr(DBusConnection *connection, DBusMessage *message)
{
return request_get_attr(connection, message, "osd", NULL, (int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))osd_get_attr);
}
static DBusHandlerResult
request_osd_set_attr(DBusConnection *connection, DBusMessage *message)
{
return request_set_add_remove_attr(connection, message, "osd", NULL, (int (*)(void *, struct attr *))osd_set_attr);
}
/* roadprofile */
static DBusHandlerResult
request_roadprofile_get_attr(DBusConnection *connection, DBusMessage *message)
{
return request_get_attr(connection, message, "roadprofile", NULL, (int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))roadprofile_get_attr);
}
static DBusHandlerResult
request_roadprofile_set_attr(DBusConnection *connection, DBusMessage *message)
{
return request_set_add_remove_attr(connection, message, "roadprofile", NULL, (int (*)(void *, struct attr *))roadprofile_set_attr);
}
static DBusHandlerResult
request_roadprofile_attr_iter(DBusConnection *connection, DBusMessage *message)
{
return request_attr_iter(connection, message, "roadprofile", (struct attr_iter * (*)(void))roadprofile_attr_iter_new);
}
static DBusHandlerResult
request_roadprofile_attr_iter_destroy(DBusConnection *connection, DBusMessage *message)
{
return request_attr_iter_destroy(connection, message, "roadprofile", (void (*)(struct attr_iter *))roadprofile_attr_iter_destroy);
}
/* route */
static DBusHandlerResult
request_route_get_attr(DBusConnection *connection, DBusMessage *message)
{
return request_get_attr(connection, message, "route", NULL, (int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))route_get_attr);
}
static DBusHandlerResult
request_route_set_attr(DBusConnection *connection, DBusMessage *message)
{
return request_set_add_remove_attr(connection, message, "route", NULL, (int (*)(void *, struct attr *))route_set_attr);
}
static DBusHandlerResult
request_route_add_attr(DBusConnection *connection, DBusMessage *message)
{
return request_set_add_remove_attr(connection, message, "route", NULL, (int (*)(void *, struct attr *))route_add_attr);
}
static DBusHandlerResult
request_route_remove_attr(DBusConnection *connection, DBusMessage *message)
{
return request_set_add_remove_attr(connection, message, "route", NULL, (int (*)(void *, struct attr *))route_remove_attr);
}
static DBusHandlerResult
request_route_destroy(DBusConnection *connection, DBusMessage *message)
{
return request_destroy(connection, message, "route", NULL, (void (*)(void *)) route_destroy);
}
static DBusHandlerResult
request_route_dup(DBusConnection *connection, DBusMessage *message)
{
return request_dup(connection, message, "route", NULL, (void *(*)(void *)) route_dup);
}
/* navit */
static DBusHandlerResult
request_navit_draw(DBusConnection *connection, DBusMessage *message)
{
struct navit *navit;
navit=object_get_from_message(message, "navit");
if (! navit)
return dbus_error_invalid_object_path(connection, message);
navit_draw(navit);
return empty_reply(connection, message);
}
/**
* Extracts a struct point from a DBus message
*
* @param message The DBus message
* @param iter Sort of pointer that points on that (ii)-object in the message
* @param p Pointer where the data should get stored
* @returns Returns 1 when everything went right, otherwise 0
*/
static int
point_get_from_message(DBusMessage *message, DBusMessageIter *iter, struct point *p)
{
DBusMessageIter iter2;
dbg(lvl_debug,"%s", dbus_message_iter_get_signature(iter));
dbus_message_iter_recurse(iter, &iter2);
if (dbus_message_iter_get_arg_type(&iter2) != DBUS_TYPE_INT32)
return 0;
dbus_message_iter_get_basic(&iter2, &p->x);
dbus_message_iter_next(&iter2);
if (dbus_message_iter_get_arg_type(&iter2) != DBUS_TYPE_INT32)
return 0;
dbus_message_iter_get_basic(&iter2, &p->y);
dbg(lvl_debug, " x -> %x y -> %x", p->x, p->y);
dbus_message_iter_next(&iter2);
if (dbus_message_iter_get_arg_type(&iter2) != DBUS_TYPE_INVALID)
return 0;
return 1;
}
/**
* @brief Shows up a message
* @param connection The DBusConnection object through which \a message arrived
* @param message The DBusMessage containing the coordinates
* @returns An empty reply if everything went right, otherwise DBUS_HANDLER_RESULT_NOT_YET_HANDLED
*/
static DBusHandlerResult
request_navit_add_message(DBusConnection *connection, DBusMessage *message)
{
struct navit *navit;
char *usermessage;
DBusMessageIter iter;
navit=object_get_from_message(message, "navit");
if (! navit)
return dbus_error_invalid_object_path(connection, message);
dbus_message_iter_init(message, &iter);
dbus_message_iter_get_basic(&iter, &usermessage);
navit_add_message(navit, usermessage);
return empty_reply(connection, message);
}
/**
* @brief Centers the screen on a specified position \a pc on the world
* @param connection The DBusConnection object through which \a message arrived
* @param message The DBusMessage containing the coordinates
* @returns An empty reply if everything went right, otherwise DBUS_HANDLER_RESULT_NOT_YET_HANDLED
*/
static DBusHandlerResult
request_navit_set_center(DBusConnection *connection, DBusMessage *message)
{
struct pcoord pc;
struct navit *navit;
DBusMessageIter iter;
navit=object_get_from_message(message, "navit");
if (! navit)
return dbus_error_invalid_object_path(connection, message);
dbus_message_iter_init(message, &iter);
if (!pcoord_get_from_message(message, &iter, &pc))
return dbus_error_invalid_parameter(connection, message);
navit_set_center(navit, &pc, 0);
return empty_reply(connection, message);
}
/**
* @brief Centers the screen on a specified position \a p shown on the screen
* @param connection The DBusConnection object through which \a message arrived
* @param message The DBusMessage containing the x and y value
* @returns An empty reply if everything went right, otherwise DBUS_HANDLER_RESULT_NOT_YET_HANDLED
*/
static DBusHandlerResult
request_navit_set_center_screen(DBusConnection *connection, DBusMessage *message)
{
struct point p;
struct navit *navit;
DBusMessageIter iter;
navit=object_get_from_message(message, "navit");
if (! navit)
return dbus_error_invalid_object_path(connection, message);
dbus_message_iter_init(message, &iter);
if (!point_get_from_message(message, &iter, &p))
return dbus_error_invalid_parameter(connection, message);
navit_set_center_screen(navit, &p, 0);
return empty_reply(connection, message);
}
/**
* @brief Sets the layout to \a new_layout_name extracted from \a message
* @param connection The DBusConnection object through which \a message arrived
* @param message The DBusMessage containing the name of the layout
* @returns An empty reply if everything went right, otherwise DBUS_HANDLER_RESULT_NOT_YET_HANDLED
*/
static DBusHandlerResult
request_navit_set_layout(DBusConnection *connection, DBusMessage *message)
{
char *new_layout_name;
struct navit *navit;
struct attr attr;
struct attr_iter *iter;
navit=object_get_from_message(message, "navit");
if (! navit)
return dbus_error_invalid_object_path(connection, message);
if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &new_layout_name, DBUS_TYPE_INVALID))
return dbus_error_invalid_parameter(connection, message);
iter=navit_attr_iter_new();
while(navit_get_attr(navit, attr_layout, &attr, iter)) {
if (strcmp(attr.u.layout->name, new_layout_name) == 0) {
navit_set_attr(navit, &attr);
}
}
return empty_reply(connection, message);
}
/**
* @brief Gracefully exit Navit by registering a one-second callback to allow clean shutdown of the dbus connection
* @param connection The DBusConnection object through which \a message arrived
* @param message The DBusMessage
* @returns An empty reply if everything went right, otherwise DBUS_HANDLER_RESULT_NOT_YET_HANDLED
*/
static DBusHandlerResult
request_navit_quit(DBusConnection *connection, DBusMessage *message)
{
dbg(lvl_debug,"Got a quit request from DBUS");
struct attr navit;
navit.type=attr_navit;
struct navit *nav;
nav = object_get_from_message(message, "navit");
if (! nav)
return dbus_error_invalid_object_path(connection, message);
navit.u.navit=nav;
config_remove_attr(config, &navit);
struct callback *callback;
callback=callback_new_1(callback_cast(event_main_loop_quit), NULL);
event_add_timeout(1000, 1, callback);
return empty_reply(connection, message);
}
static DBusHandlerResult
request_navit_zoom(DBusConnection *connection, DBusMessage *message)
{
int factor;
struct point p, *pp=NULL;
struct navit *navit;
DBusMessageIter iter;
navit = object_get_from_message(message, "navit");
if (! navit)
return dbus_error_invalid_object_path(connection, message);
dbus_message_iter_init(message, &iter);
dbg(lvl_debug,"%s", dbus_message_iter_get_signature(&iter));
dbus_message_iter_get_basic(&iter, &factor);
if (dbus_message_iter_has_next(&iter))
{
dbus_message_iter_next(&iter);
if (!point_get_from_message(message, &iter, &p))
return dbus_error_invalid_parameter(connection, message);
pp=&p;
}
if (factor > 1)
navit_zoom_in(navit, factor, pp);
else if (factor < -1)
navit_zoom_out(navit, 0-factor, pp);
return empty_reply(connection, message);
}
static DBusHandlerResult
request_navit_zoom_to_route(DBusConnection *connection, DBusMessage *message)
{
struct navit *navit;
DBusMessageIter iter;
navit = object_get_from_message(message, "navit");
if (! navit)
return dbus_error_invalid_object_path(connection, message);
dbus_message_iter_init(message, &iter);
dbg(lvl_debug,"%s", dbus_message_iter_get_signature(&iter));
navit_zoom_to_route(navit,0);
return empty_reply(connection, message);
}
/**
* @brief Exports the current route as a GPX file.
*
* @param connection The DBusConnection object through which a message arrived
* @param message The DBusMessage including the `filename` parameter
* @returns An empty reply if everything went right, otherwise `DBUS_HANDLER_RESULT_NOT_YET_HANDLED`
*/
static DBusHandlerResult
request_navit_route_export_gpx(DBusConnection *connection, DBusMessage *message)
{
char * filename;
struct navit *navit;
DBusMessageIter iter;
navit = object_get_from_message(message, "navit");
if (! navit)
return dbus_error_invalid_object_path(connection, message);
dbus_message_iter_init(message, &iter);
dbus_message_iter_get_basic(&iter, &filename);
struct navigation *nav = navit_get_navigation(navit);
if(!nav) {
return dbus_error_navigation_not_configured(connection, message);
}
dbg(lvl_debug,"Dumping route from dbus to %s", filename);
struct map * map=NULL;
struct map_rect * mr=NULL;
struct item *item = NULL;
struct attr attr;
struct coord c;
struct coord_geo g;
char *header = "<?xml version='1.0' encoding='UTF-8'?>\n"
"<gpx version='1.1' creator='Navit http://navit.sourceforge.net'\n"
" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'\n"
" xmlns:navit='http://www.navit-project.org/schema/navit'\n"
" xmlns='http://www.topografix.com/GPX/1/1'\n"
" xsi:schemaLocation='http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd'>\n"
"<rte>\n";
char *trailer = "</rte>\n</gpx>\n";
map = navigation_get_map(nav);
if(map)
mr = map_rect_new(map,NULL);
FILE *fp;
fp = fopen(filename,"w");
fprintf(fp, "%s", header);
while((item = map_rect_get_item(mr))) {
if(item_attr_get(item,attr_navigation_long,&attr)) {
item_coord_get(item, &c, 1);
transform_to_geo (projection_mg, &c, &g);
fprintf(fp,"<rtept lon='%4.16f' lat='%4.16f'><type>%s</type><name>%s</name></rtept>\n",g.lng, g.lat, item_to_name(item->type), map_convert_string_tmp(item->map,attr.u.str));
}
}
fprintf(fp,"%s",trailer);
fclose(fp);
return empty_reply(connection, message);
}
/**
* @brief Exports the current route as a GeoJSON file
* @param connection The DBusConnection object through which a message arrived
* @param message The DBusMessage including the 'filename' parameter
* @returns An empty reply if everything went right, otherwise DBUS_HANDLER_RESULT_NOT_YET_HANDLED
*/
static DBusHandlerResult
request_navit_route_export_geojson(DBusConnection *connection, DBusMessage *message)
{
char * filename;
struct point p;
struct navit *navit;
DBusMessageIter iter;
navit = object_get_from_message(message, "navit");
if (! navit)
return dbus_error_invalid_object_path(connection, message);
dbus_message_iter_init(message, &iter);
dbus_message_iter_get_basic(&iter, &filename);
if (dbus_message_iter_has_next(&iter))
{
dbus_message_iter_next(&iter);
if (!point_get_from_message(message, &iter, &p))
return dbus_error_invalid_parameter(connection, message);
}
dbg(lvl_debug,"Dumping route from dbus to %s", filename);
struct map * map=NULL;
struct navigation * nav = NULL;
struct map_rect * mr=NULL;
struct item *item = NULL;
struct attr attr;
struct coord c;
struct coord_geo g;
char *header = "{\n"
" \"type\": \"FeatureCollection\",\n"
" \"features\": [\n"
" {\n"
" \"type\": \"Feature\",\n"
" \"properties\": {\n"
" \"name\": \"Navit route export\",\n"
" \"stroke\": \"red\",\n"
" \"stroke-width\": \"5px\"\n"
" },\n"
" \"geometry\": {\n"
" \"type\": \"LineString\",\n"
" \"coordinates\": [\n";
nav = navit_get_navigation(navit);
if(!nav) {
return dbus_error_navigation_not_configured(connection, message);
}
map = navigation_get_map(nav);
if(map)
mr = map_rect_new(map,NULL);
FILE *fp;
fp = fopen(filename,"w");
fprintf(fp, "%s", header);
int is_first=1;
char * instructions;
instructions=g_strdup("");
while((item = map_rect_get_item(mr))) {
if(item_attr_get(item,attr_navigation_long,&attr)) {
item_coord_get(item, &c, 1);
transform_to_geo (projection_mg, &c, &g);
if(!is_first){
fprintf(fp,",\n");
instructions=g_strconcat_printf(instructions,",\n");
}
fprintf(fp,"[ %4.16f, %4.16f ]",g.lng, g.lat);
instructions=g_strconcat_printf(instructions, g_strdup_printf(" { \"type\": \"Feature\", \"properties\": { \"Instruction\": \"%s\", \"name\": \"\" }, \"geometry\": { \"type\": \"Point\", \"coordinates\": [ %4.16f, %4.16f ] } }", map_convert_string_tmp(item->map,attr.u.str), g.lng, g.lat));
/* fprintf(fp,"<rtept lon='%4.16f' lat='%4.16f'><type>%s</type><name>%s</name></rtept>\n",g.lng, g.lat, item_to_name(item->type), map_convert_string_tmp(item->map,attr.u.str)); */
is_first=0;
}
}
fprintf(fp," ]}\n },\n%s ]\n }\n",instructions);
fclose(fp);
return empty_reply(connection, message);
}
static DBusHandlerResult
request_navit_block(DBusConnection *connection, DBusMessage *message)
{
int mode;
struct navit *navit;
DBusMessageIter iter;
navit = object_get_from_message(message, "navit");
if (! navit)
return dbus_error_invalid_object_path(connection, message);
dbus_message_iter_init(message, &iter);
dbus_message_iter_get_basic(&iter, &mode);
navit_block(navit, mode);
return empty_reply(connection, message);
}
static DBusHandlerResult
request_navit_resize(DBusConnection *connection, DBusMessage *message)
{
struct navit *navit;
int w, h;
DBusMessageIter iter;
navit = object_get_from_message(message, "navit");
if (! navit)
return dbus_error_invalid_object_path(connection, message);
dbus_message_iter_init(message, &iter);
dbg(lvl_debug,"%s", dbus_message_iter_get_signature(&iter));
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INT32)
return dbus_error_invalid_parameter(connection, message);
dbus_message_iter_get_basic(&iter, &w);
dbus_message_iter_next(&iter);
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INT32)
return dbus_error_invalid_parameter(connection, message);
dbus_message_iter_get_basic(&iter, &h);
dbg(lvl_debug, " w -> %i h -> %i", w, h);
navit_handle_resize(navit, w, h);
return empty_reply(connection, message);
}
static DBusHandlerResult
request_navit_get_attr(DBusConnection *connection, DBusMessage *message)
{
return request_get_attr(connection, message, "navit", NULL, (int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))navit_get_attr);
}
static DBusHandlerResult
request_navit_attr_iter(DBusConnection *connection, DBusMessage *message)
{
DBusMessage *reply;
struct attr_iter *attr_iter=navit_attr_iter_new();
char *opath=object_new("navit_attr_iter",attr_iter);
reply = dbus_message_new_method_return(message);
dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &opath, DBUS_TYPE_INVALID);
dbus_connection_send (connection, reply, NULL);
dbus_message_unref (reply);
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult
request_navit_attr_iter_destroy(DBusConnection *connection, DBusMessage *message)
{
struct attr_iter *attr_iter;
DBusMessageIter iter;
dbus_message_iter_init(message, &iter);
attr_iter=object_get_from_message_arg(&iter, "navit_attr_iter");
if (! attr_iter)
return dbus_error_invalid_object_path_parameter(connection, message);
navit_attr_iter_destroy(attr_iter);
return empty_reply(connection, message);
}
static DBusHandlerResult
request_navit_set_attr(DBusConnection *connection, DBusMessage *message)
{
return request_set_add_remove_attr(connection, message, "navit", NULL, (int (*)(void *, struct attr *))navit_set_attr);
}
static DBusHandlerResult
request_navit_add_attr(DBusConnection *connection, DBusMessage *message)
{
return request_set_add_remove_attr(connection, message, "navit", NULL, (int (*)(void *, struct attr *))navit_add_attr);
}
static DBusHandlerResult
request_navit_remove_attr(DBusConnection *connection, DBusMessage *message)
{
return request_set_add_remove_attr(connection, message, "navit", NULL, (int (*)(void *, struct attr *))navit_remove_attr);
}
static DBusHandlerResult
request_navit_set_position(DBusConnection *connection, DBusMessage *message)
{
struct pcoord pc;
struct navit *navit;
DBusMessageIter iter;
navit = object_get_from_message(message, "navit");
if (! navit)
return dbus_error_invalid_object_path(connection, message);
dbus_message_iter_init(message, &iter);
if (!pcoord_get_from_message(message, &iter, &pc))
return dbus_error_invalid_parameter(connection, message);
navit_set_position(navit, &pc);
return empty_reply(connection, message);
}
static DBusHandlerResult
request_navit_set_destination(DBusConnection *connection, DBusMessage *message)
{
struct pcoord pc;
struct navit *navit;
DBusMessageIter iter;
char *description;
navit = object_get_from_message(message, "navit");
if (! navit)
return dbus_error_invalid_object_path(connection, message);
dbus_message_iter_init(message, &iter);
if (!pcoord_get_from_message(message, &iter, &pc))
return dbus_error_invalid_parameter(connection, message);
dbus_message_iter_next(&iter);
dbus_message_iter_get_basic(&iter, &description);
dbg(lvl_debug, " destination -> %s", description);
navit_set_destination(navit, &pc, description, 1);
return empty_reply(connection, message);
}
static DBusHandlerResult
request_navit_clear_destination(DBusConnection *connection, DBusMessage *message)
{
struct navit *navit;
navit = object_get_from_message(message, "navit");
if (! navit)
return dbus_error_invalid_object_path(connection, message);
navit_set_destination(navit, NULL, NULL, 0);
return empty_reply(connection, message);
}
static DBusHandlerResult
request_navit_evaluate(DBusConnection *connection, DBusMessage *message)
{
struct navit *navit;
char *command;
char *result;
struct attr attr;
DBusMessage *reply;
int error;
navit = object_get_from_message(message, "navit");
if (! navit)
return dbus_error_invalid_object_path(connection, message);
attr.type=attr_navit;
attr.u.navit=navit;
if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &command, DBUS_TYPE_INVALID))
return dbus_error_invalid_parameter(connection, message);
result=command_evaluate_to_string(&attr, command, &error);
reply = dbus_message_new_method_return(message);
if (error)
dbus_message_append_args(reply, DBUS_TYPE_INT32, &error, DBUS_TYPE_INVALID);
else if (result)
dbus_message_append_args(reply, DBUS_TYPE_STRING, &result, DBUS_TYPE_INVALID);
dbus_connection_send (connection, reply, NULL);
dbus_message_unref (reply);
return DBUS_HANDLER_RESULT_HANDLED;
}
/* search_list */
static DBusHandlerResult
request_search_list_destroy(DBusConnection *connection, DBusMessage *message)
{
return request_destroy(connection, message, "search_list", NULL, (void (*)(void *)) search_list_destroy);
}
static void
request_search_list_common(struct search_list_common *slc, DBusMessageIter *iter4)
{
if (slc->postal)
encode_dict_string_variant_string(iter4, "postal", slc->postal);
if (slc->postal_mask)
encode_dict_string_variant_string(iter4, "postal_mask", slc->postal_mask);
}
static DBusHandlerResult
request_search_list_get_result(DBusConnection *connection, DBusMessage *message)
{
struct search_list *search_list;
struct search_list_result *result;
DBusMessage *reply;
DBusMessageIter iter,iter2,iter3,iter4;
char *country="country";
char *town="town";
char *street="street";
char *house_number="housenumber";
search_list = object_get_from_message(message, "search_list");
if (! search_list)
return dbus_error_invalid_object_path(connection, message);
result=search_list_get_result(search_list);
if (!result)
return dbus_error_no_data_available(connection, message);
reply = dbus_message_new_method_return(message);
dbus_message_iter_init_append(reply, &iter);
dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &result->id);
pcoord_encode(&iter, result->c);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sa{sv}}", &iter2);
if (result->country && (result->country->car || result->country->iso2 || result->country->iso3 || result->country->name)) {
dbus_message_iter_open_container(&iter2, DBUS_TYPE_DICT_ENTRY, NULL, &iter3);
dbus_message_iter_append_basic(&iter3, DBUS_TYPE_STRING, &country);
dbus_message_iter_open_container(&iter3, DBUS_TYPE_ARRAY, "{sv}", &iter4);
if (result->country->car)
encode_dict_string_variant_string(&iter4, "car", result->country->car);
if (result->country->iso2)
encode_dict_string_variant_string(&iter4, "iso2", result->country->iso2);
if (result->country->iso3)
encode_dict_string_variant_string(&iter4, "iso3", result->country->iso3);
if (result->country->name)
encode_dict_string_variant_string(&iter4, "name", result->country->name);
dbus_message_iter_close_container(&iter3, &iter4);
dbus_message_iter_close_container(&iter2, &iter3);
}
if (result->town && (result->town->common.district_name || result->town->common.town_name)) {
dbus_message_iter_open_container(&iter2, DBUS_TYPE_DICT_ENTRY, NULL, &iter3);
dbus_message_iter_append_basic(&iter3, DBUS_TYPE_STRING, &town);
dbus_message_iter_open_container(&iter3, DBUS_TYPE_ARRAY, "{sv}", &iter4);
request_search_list_common(&result->town->common,&iter4);
if (result->town->common.district_name)
encode_dict_string_variant_string(&iter4, "district", result->town->common.district_name);
if (result->town->common.town_name)
encode_dict_string_variant_string(&iter4, "name", result->town->common.town_name);
dbus_message_iter_close_container(&iter3, &iter4);
dbus_message_iter_close_container(&iter2, &iter3);
}
if (result->street && result->street->name) {
dbus_message_iter_open_container(&iter2, DBUS_TYPE_DICT_ENTRY, NULL, &iter3);
dbus_message_iter_append_basic(&iter3, DBUS_TYPE_STRING, &street);
dbus_message_iter_open_container(&iter3, DBUS_TYPE_ARRAY, "{sv}", &iter4);
request_search_list_common(&result->street->common,&iter4);
if (result->street->name)
encode_dict_string_variant_string(&iter4, "name", result->street->name);
dbus_message_iter_close_container(&iter3, &iter4);
dbus_message_iter_close_container(&iter2, &iter3);
}
if (result->house_number && result->house_number->house_number) {
dbus_message_iter_open_container(&iter2, DBUS_TYPE_DICT_ENTRY, NULL, &iter3);
dbus_message_iter_append_basic(&iter3, DBUS_TYPE_STRING, &house_number);
dbus_message_iter_open_container(&iter3, DBUS_TYPE_ARRAY, "{sv}", &iter4);
request_search_list_common(&result->house_number->common,&iter4);
encode_dict_string_variant_string(&iter4, "name", result->house_number->house_number);
dbus_message_iter_close_container(&iter3, &iter4);
dbus_message_iter_close_container(&iter2, &iter3);
}
dbus_message_iter_close_container(&iter, &iter2);
dbus_connection_send (connection, reply, NULL);
dbus_message_unref (reply);
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult
request_search_list_new(DBusConnection *connection, DBusMessage *message)
{
DBusMessageIter iter;
DBusMessage *reply;
struct mapset *mapset;
struct search_list *search_list;
char *opath;
dbus_message_iter_init(message, &iter);
mapset=object_get_from_message_arg(&iter, "mapset");
if (! mapset)
return dbus_error_invalid_object_path_parameter(connection, message);
search_list=search_list_new(mapset);
opath=object_new("search_list", search_list);
reply = dbus_message_new_method_return(message);
dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &opath, DBUS_TYPE_INVALID);
dbus_connection_send (connection, reply, NULL);
dbus_message_unref (reply);
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult
request_search_list_search(DBusConnection *connection, DBusMessage *message)
{
DBusMessageIter iter;
struct search_list *search_list;
struct attr attr;
int partial;
search_list = object_get_from_message(message, "search_list");
if (! search_list)
return dbus_error_invalid_object_path(connection, message);
dbus_message_iter_init(message, &iter);
if (!decode_attr_from_iter(&iter, &attr))
return dbus_error_invalid_parameter(connection, message);
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INT32)
return dbus_error_invalid_parameter(connection, message);
dbus_message_iter_get_basic(&iter, &partial);
search_list_search(search_list, &attr, partial);
return empty_reply(connection, message);
}
static DBusHandlerResult
request_search_list_select(DBusConnection *connection, DBusMessage *message)
{
DBusMessageIter iter;
struct search_list *search_list;
int id, mode;
enum attr_type attr_type;
search_list = object_get_from_message(message, "search_list");
if (! search_list)
return dbus_error_invalid_object_path(connection, message);
dbus_message_iter_init(message, &iter);
attr_type=decode_attr_type_from_iter(&iter);
if (attr_type == attr_none)
return dbus_error_invalid_parameter(connection, message);
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INT32)
return dbus_error_invalid_parameter(connection, message);
dbus_message_iter_get_basic(&iter, &id);
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INT32)
return dbus_error_invalid_parameter(connection, message);
dbus_message_iter_get_basic(&iter, &mode);
search_list_select(search_list, attr_type, id, mode);
return empty_reply(connection, message);
}
/* tracking */
static DBusHandlerResult
request_tracking_get_attr(DBusConnection *connection, DBusMessage *message)
{
return request_get_attr(connection, message, "tracking", NULL, (int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))tracking_get_attr);
}
/* vehicle */
static DBusHandlerResult
request_vehicle_set_attr(DBusConnection *connection, DBusMessage *message)
{
struct vehicle *vehicle;
struct attr attr;
int ret;
vehicle = object_get_from_message(message, "vehicle");
if (! vehicle)
return dbus_error_invalid_object_path(connection, message);
if (decode_attr(message, &attr)) {
ret=vehicle_set_attr(vehicle, &attr);
destroy_attr(&attr);
if (ret)
return empty_reply(connection, message);
}
return dbus_error_invalid_parameter(connection, message);
}
/* vehicleprofile */
static DBusHandlerResult
request_vehicleprofile_get_attr(DBusConnection *connection, DBusMessage *message)
{
return request_get_attr(connection, message, "vehicleprofile", NULL, (int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))vehicleprofile_get_attr);
}
static DBusHandlerResult
request_vehicleprofile_set_attr(DBusConnection *connection, DBusMessage *message)
{
return request_set_add_remove_attr(connection, message, "vehicleprofile", NULL, (int (*)(void *, struct attr *))vehicleprofile_set_attr);
}
static DBusHandlerResult
request_vehicleprofile_attr_iter(DBusConnection *connection, DBusMessage *message)
{
return request_attr_iter(connection, message, "vehicleprofile", (struct attr_iter * (*)(void))vehicleprofile_attr_iter_new);
}
static DBusHandlerResult
request_vehicleprofile_attr_iter_destroy(DBusConnection *connection, DBusMessage *message)
{
return request_attr_iter_destroy(connection, message, "vehicleprofile", (void (*)(struct attr_iter *))vehicleprofile_attr_iter_destroy);
}
struct dbus_method {
char *path;
char *method;
char *signature;
char *signature_name;
char *response;
char *response_name;
DBusHandlerResult(*func)(DBusConnection *connection, DBusMessage *message);
} dbus_methods[] = {
{"", "attr_iter", "", "", "o", "attr_iter", request_config_attr_iter},
{"", "attr_iter_destroy", "o", "attr_iter", "", "", request_config_attr_iter_destroy},
{"", "get_attr", "s", "attrname", "sv", "attrname,value",request_config_get_attr},
{"", "get_attr_wi", "so", "attrname,attr_iter", "sv", "attrname,value",request_config_get_attr},
{"", "callback_new", "s", "signalname", "o", "callback",request_callback_new},
{"", "callback_attr_new", "ss", "signalname,attribute", "o", "callback",request_callback_new},
{"", "search_list_new", "o", "mapset", "o", "search",request_search_list_new},
{".callback","destroy", "", "", "", "", request_callback_destroy},
{".graphics","get_data", "s", "type", "ay", "data", request_graphics_get_data},
{".graphics","set_attr", "sv", "attribute,value", "", "", request_graphics_set_attr},
{".gui", "get_attr", "s", "attribute", "sv", "attrname,value", request_gui_get_attr},
{".gui", "command_parameter", "sa{sa{sv}}","command,parameter", "a{sa{sv}}", "return", request_gui_command},
{".gui", "command", "s", "command", "a{sa{sv}}", "return", request_gui_command},
{".navit", "draw", "", "", "", "", request_navit_draw},
{".navit", "add_message", "s", "message", "", "", request_navit_add_message},
{".navit", "set_center_by_string","s", "(coordinates)", "", "", request_navit_set_center},
{".navit", "set_center", "(is)", "(projection,coordinates)", "", "", request_navit_set_center},
{".navit", "set_center", "(iii)", "(projection,longitude,latitude)", "", "", request_navit_set_center},
{".navit", "set_center_screen", "(ii)", "(pixel_x,pixel_y)", "", "", request_navit_set_center_screen},
{".navit", "set_layout", "s", "layoutname", "", "", request_navit_set_layout},
{".navit", "zoom", "i(ii)", "factor(pixel_x,pixel_y)", "", "", request_navit_zoom},
{".navit", "zoom", "i", "factor", "", "", request_navit_zoom},
{".navit", "zoom_to_route", "", "", "", "", request_navit_zoom_to_route},
{".navit", "quit", "", "", "", "", request_navit_quit},
{".navit", "export_as_gpx", "s", "filename", "", "", request_navit_route_export_gpx},
{".navit", "export_as_geojson", "s", "filename", "", "", request_navit_route_export_geojson},
{".navit", "block", "i", "mode", "", "", request_navit_block},
{".navit", "resize", "ii", "upperleft,lowerright", "", "", request_navit_resize},
{".navit", "attr_iter", "", "", "o", "attr_iter", request_navit_attr_iter},
{".navit", "attr_iter_destroy", "o", "attr_iter", "", "", request_navit_attr_iter_destroy},
{".navit", "get_attr", "s", "attribute", "sv", "attrname,value", request_navit_get_attr},
{".navit", "get_attr_wi", "so", "attribute,attr_iter", "sv", "attrname,value", request_navit_get_attr},
{".navit", "set_attr", "sv", "attribute,value", "", "", request_navit_set_attr},
{".navit", "add_attr", "sv", "attribute,value", "", "", request_navit_add_attr},
{".navit", "remove_attr", "sv", "attribute,value", "", "", request_navit_remove_attr},
{".navit", "set_position", "s", "(coordinates)", "", "", request_navit_set_position},
{".navit", "set_position", "(is)", "(projection,coordinated)", "", "", request_navit_set_position},
{".navit", "set_position", "(iii)", "(projection,longitude,latitude)", "", "", request_navit_set_position},
{".navit", "set_destination", "ss", "coordinates,comment", "", "", request_navit_set_destination},
{".navit", "set_destination", "(is)s", "(projection,coordinates)comment", "", "", request_navit_set_destination},
{".navit", "set_destination", "(iii)s", "(projection,longitude,latitude)comment", "", "", request_navit_set_destination},
{".navit", "clear_destination", "", "", "", "", request_navit_clear_destination},
{".navit", "evaluate", "s", "command", "s", "", request_navit_evaluate},
{".layout", "get_attr", "s", "attribute", "sv", "attrname,value", request_layout_get_attr},
{".map", "get_attr", "s", "attribute", "sv", "attrname,value", request_map_get_attr},
{".map", "set_attr", "sv", "attribute,value", "", "", request_map_set_attr},
{".map", "dump", "s", "file", "", "", request_map_dump},
{".mapset", "attr_iter", "", "", "o", "attr_iter", request_mapset_attr_iter},
{".mapset", "attr_iter_destroy", "o", "attr_iter", "", "", request_mapset_attr_iter_destroy},
{".mapset", "get_attr", "s", "attribute", "sv", "attrname,value", request_mapset_get_attr},
{".mapset", "get_attr_wi", "so", "attribute,attr_iter", "sv", "attrname,value", request_mapset_get_attr},
{".navigation","get_attr", "s", "attribute", "", "", request_navigation_get_attr},
{".osd", "get_attr", "s", "attribute", "sv", "attrname,value", request_osd_get_attr},
{".osd", "set_attr", "sv", "attribute,value", "", "", request_osd_set_attr},
{".roadprofile", "get_attr", "s", "attribute", "sv", "attrname,value", request_roadprofile_get_attr},
{".roadprofile", "get_attr_wi", "so", "attribute,attr_iter", "", "", request_roadprofile_get_attr},
{".roadprofile", "set_attr", "sv", "attribute,value", "", "", request_roadprofile_set_attr},
{".roadprofile", "attr_iter", "", "", "o", "attr_iter", request_roadprofile_attr_iter},
{".roadprofile", "attr_iter_destroy","o", "attr_iter", "", "", request_roadprofile_attr_iter_destroy},
{".route", "get_attr", "s", "attribute", "sv", "attrname,value", request_route_get_attr},
{".route", "set_attr", "sv", "attribute,value", "", "", request_route_set_attr},
{".route", "add_attr", "sv", "attribute,value", "", "", request_route_add_attr},
{".route", "remove_attr", "sv", "attribute,value", "", "", request_route_remove_attr},
{".route", "destroy", "", "", "", "", request_route_destroy},
{".route", "dup", "", "", "", "", request_route_dup},
{".search_list","destroy", "", "", "", "", request_search_list_destroy},
{".search_list","destroy", "", "", "", "", request_search_list_destroy},
{".search_list","destroy", "", "", "", "", request_search_list_destroy},
{".search_list","get_result", "", "", "i(iii)a{sa{sv}}", "id,coord,dict", request_search_list_get_result},
{".search_list","search", "svi", "attribute,value,partial", "", "", request_search_list_search},
{".search_list","select", "sii", "attribute_type,id,mode", "", "", request_search_list_select},
{".tracking","get_attr", "s", "attribute", "", "", request_tracking_get_attr},
{".vehicle","set_attr", "sv", "attribute,value", "", "", request_vehicle_set_attr},
{".vehicleprofile","get_attr", "s", "attribute", "", "", request_vehicleprofile_get_attr},
{".vehicleprofile","get_attr_wi", "so", "attribute,attr_iter", "", "", request_vehicleprofile_get_attr},
{".vehicleprofile","set_attr", "sv", "attribute,value", "", "", request_vehicleprofile_set_attr},
{".vehicleprofile","attr_iter", "", "", "o", "attr_iter", request_vehicleprofile_attr_iter},
{".vehicleprofile","attr_iter_destroy","o", "attr_iter", "", "", request_vehicleprofile_attr_iter_destroy},
};
static char *
introspect_path(const char *object)
{
char *ret;
int i;
char *def=".default_";
int def_len=strlen(def);
if (strncmp(object, object_path, strlen(object_path)))
return NULL;
ret=g_strdup(object+strlen(object_path));
dbg(lvl_debug,"path=%s",ret);
for (i = strlen(ret)-1 ; i >= 0 ; i--) {
if (ret[i] == '/' || (ret[i] >= '0' && ret[i] <= '9'))
ret[i]='\0';
else
break;
}
for (i = 0 ; i < strlen(ret); i++)
if (ret[i] == '/')
ret[i]='.';
for (i = strlen(ret)-1 ; i >= 0 ; i--) {
if (!strncmp(ret+i, def, def_len)) {
memmove(ret+1,ret+i+def_len,strlen(ret+i+def_len)+1);
break;
}
}
return ret;
}
static char *
generate_navitintrospectxml(const char *object)
{
int i,methods_size,n=0;
char *navitintrospectxml;
char *path=introspect_path(object);
if (!path)
return NULL;
dbg(lvl_debug,"path=%s",path);
// write header and make navit introspectable
navitintrospectxml = g_strdup_printf("%s%s%s\n", navitintrospectxml_head1, object, navitintrospectxml_head2);
methods_size=sizeof(dbus_methods)/sizeof(struct dbus_method);
for (i = 0 ; i < methods_size ; i++) {
// start new interface if it's the first method or it changed
if (strcmp(dbus_methods[i].path, path))
continue;
if ((n == 0) || strcmp(dbus_methods[i-1].path, dbus_methods[i].path))
navitintrospectxml = g_strconcat_printf(navitintrospectxml, " <interface name=\"%s%s\">\n", service_name, dbus_methods[i].path);
n++;
// start the new method
navitintrospectxml = g_strconcat_printf(navitintrospectxml, " <method name=\"%s\">\n", dbus_methods[i].method);
// set input signature if existent
if (strcmp(dbus_methods[i].signature, ""))
navitintrospectxml = g_strconcat_printf(navitintrospectxml, " <arg direction=\"in\" name=\"%s\" type=\"%s\" />\n", dbus_methods[i].signature_name, dbus_methods[i].signature);
// set response signature if existent
if (strcmp(dbus_methods[i].response, ""))
navitintrospectxml = g_strconcat_printf(navitintrospectxml, " <arg direction=\"out\" name=\"%s\" type=\"%s\" />\n", dbus_methods[i].response_name, dbus_methods[i].response);
// close the method
navitintrospectxml = g_strconcat_printf(navitintrospectxml, " </method>\n");
// close the interface if we reached the last method or the interface changes
if ((methods_size == i+1) || ((methods_size > i+1) && strcmp(dbus_methods[i+1].path, dbus_methods[i].path)))
navitintrospectxml = g_strconcat_printf(navitintrospectxml, " </interface>\n\n");
}
// close the "mother tag"
navitintrospectxml = g_strconcat_printf(navitintrospectxml, "</node>\n");
return navitintrospectxml;
}
static DBusHandlerResult
navit_handler_func(DBusConnection *connection, DBusMessage *message, void *user_data)
{
int i;
char *path;
dbg(lvl_debug,"type=%s interface=%s path=%s member=%s signature=%s", dbus_message_type_to_string(dbus_message_get_type(message)), dbus_message_get_interface(message), dbus_message_get_path(message), dbus_message_get_member(message), dbus_message_get_signature(message));
if (dbus_message_is_method_call (message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
DBusMessage *reply;
char *navitintrospectxml = generate_navitintrospectxml(dbus_message_get_path(message));
dbg(lvl_debug,"Introspect %s:Result:%s",dbus_message_get_path(message), navitintrospectxml);
if (navitintrospectxml) {
reply = dbus_message_new_method_return(message);
dbus_message_append_args(reply, DBUS_TYPE_STRING, &navitintrospectxml, DBUS_TYPE_INVALID);
dbus_connection_send (connection, reply, NULL);
dbus_message_unref (reply);
g_free(navitintrospectxml);
return DBUS_HANDLER_RESULT_HANDLED;
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
for (i = 0 ; i < sizeof(dbus_methods)/sizeof(struct dbus_method) ; i++) {
path=g_strdup_printf("%s%s", service_name, dbus_methods[i].path);
if (dbus_message_is_method_call(message, path, dbus_methods[i].method) &&
dbus_message_has_signature(message, dbus_methods[i].signature)) {
g_free(path);
return dbus_methods[i].func(connection, message);
}
g_free(path);
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
static DBusObjectPathVTable dbus_navit_vtable = {
NULL,
navit_handler_func,
NULL
};
#if 0
DBusHandlerResult
filter(DBusConnection *connection, DBusMessage *message, void *user_data)
{
dbg(lvl_debug,"type=%s interface=%s path=%s member=%s signature=%s", dbus_message_type_to_string(dbus_message_get_type(message)), dbus_message_get_interface(message), dbus_message_get_path(message), dbus_message_get_member(message), dbus_message_get_signature(message));
if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
#endif
static int
dbus_cmd_send_signal(struct navit *navit, char *command, struct attr **in, struct attr ***out)
{
DBusMessage* msg;
char *opath=object_new("navit",navit);
char *interface=g_strdup_printf("%s%s", service_name, ".navit");
dbg(lvl_debug,"enter %s %s %s",opath,command,interface);
msg = dbus_message_new_signal(opath, interface, "signal");
if (msg) {
DBusMessageIter iter1,iter2,iter3;
dbus_message_iter_init_append(msg, &iter1);
dbus_message_iter_open_container(&iter1, DBUS_TYPE_ARRAY, "{sv}", &iter2);
if (in) {
while (*in) {
dbus_message_iter_open_container(&iter2, DBUS_TYPE_DICT_ENTRY, NULL, &iter3);
encode_attr(&iter3, *in);
dbus_message_iter_close_container(&iter2, &iter3);
in++;
}
}
dbus_message_iter_close_container(&iter1, &iter2);
dbus_connection_send(connection, msg, &dbus_serial);
dbus_connection_flush(connection);
dbus_message_unref(msg);
}
g_free(interface);
return 0;
}
static struct command_table commands[] = {
{"dbus_send_signal",command_cast(dbus_cmd_send_signal)},
};
static void
dbus_main_navit(struct navit *navit, int added)
{
struct attr attr;
if (added==1) {
DBusMessage* msg;
char *opath=object_new("navit",navit);
char *interface=g_strdup_printf("%s%s", service_name, ".navit");
command_add_table_attr(commands, sizeof(commands)/sizeof(struct command_table), navit, &attr);
navit_add_attr(navit, &attr);
msg = dbus_message_new_signal(opath, interface, "startup");
dbus_connection_send(connection, msg, &dbus_serial);
dbus_connection_flush(connection);
dbus_message_unref(msg);
g_free(interface);
}
}
void plugin_init(void)
{
DBusError error;
struct attr callback;
object_hash=g_hash_table_new(g_str_hash, g_str_equal);
object_hash_rev=g_hash_table_new(NULL, NULL);
object_count=g_hash_table_new(g_str_hash, g_str_equal);
dbg(lvl_debug,"enter");
dbus_error_init(&error);
#ifdef DBUS_USE_SYSTEM_BUS
connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
#else
connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
#endif
if (!connection) {
dbg(lvl_error,"Failed to open connection to session message bus: %s", error.message);
dbus_error_free(&error);
return;
}
dbus_connection_setup_with_g_main(connection, NULL);
#if 0
dbus_connection_add_filter(connection, filter, NULL, NULL);
dbus_bus_add_match(connection, "type='signal',""interface='" DBUS_INTERFACE_DBUS "'", &error);
#endif
dbus_connection_register_fallback(connection, object_path, &dbus_navit_vtable, NULL);
dbus_bus_request_name(connection, service_name, 0, &error);
if (dbus_error_is_set(&error)) {
dbg(lvl_error,"Failed to request name: %s", error.message);
dbus_error_free (&error);
}
callback.type=attr_callback;
callback.u.callback=callback_new_attr_0(callback_cast(dbus_main_navit),attr_navit);
config_add_attr(config, &callback);
}
|