summaryrefslogtreecommitdiff
path: root/packaging.in/murphy.lua
blob: b625d1fd2313dcac4a1a26f3d40ff4a75aca7920 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
with_system_controller = false
with_amb = false
verbose = 0

m = murphy.get()

-- try loading the various logging plugins
m:try_load_plugin('systemd')
m:try_load_plugin('dlog')

-- load the console plugin
m:try_load_plugin('console')

m:try_load_plugin('console.disabled', 'webconsole', {
                  address = 'wsck:127.0.0.1:3000/murphy',
                  httpdir = '/usr/share/murphy/webconsole' });

-- load the dbus plugin
if m:plugin_exists('dbus') then
    m:load_plugin('dbus')
end

-- load the native resource plugin
if m:plugin_exists('resource-native') then
    m:load_plugin('resource-native')
    m:info("native resource plugin loaded")
else
    m:info("No native resource plugin found...")
end

-- load the dbus resource plugin
m:try_load_plugin('resource-dbus', {
    dbus_bus = "system",
    dbus_service = "org.Murphy",
    dbus_track = true,
    default_zone = "driver",
    default_class = "implicit"
})

-- load the domain control plugin
if m:plugin_exists('domain-control') then
    m:load_plugin('domain-control')
else
    m:info("No domain-control plugin found...")
end

if m:plugin_exists('glib') then
    m:load_plugin('glib')
else
    m:info("No glib plugin found...")
end

if m:plugin_exists("gam-resource-manager") then

function get_general_priorities(self)
    print("*** get_general_priorities\n")
    return { "USB Headset", "wiredHeadset", "speakers" }
end

function get_phone_priorities(self)
    print("*** get_phone_priorities\n")
    return { "wiredHeadset", "USB Headset" }
end

m:load_plugin('gam-resource-manager', {
    config_dir = '/etc/murphy/gam',
    decision_names = 'gam-wrtApplication-4',
    max_active = 4,
    app_mapping = {
        ['t8j6HTRpuz.MediaPlayer'] = 'wrtApplication',
        ['pacat'] = 'icoApplication'
    },
    app_default = 'icoApplication'
})

routing_sink_priority {
    application_class = "player",
    priority_queue = get_general_priorities
}

routing_sink_priority {
    application_class = "game",
    priority_queue = get_general_priorities
}

routing_sink_priority {
    application_class = "implicit",
    priority_queue = get_general_priorities
}

routing_sink_priority {
    application_class = "phone",
    priority_queue = get_phone_priorities
}

routing_sink_priority {
    application_class = "basic",
    priority_queue = get_phone_priorities
}

routing_sink_priority {
    application_class = "event",
    priority_queue = get_phone_priorities
}
end

-- load the AMB plugin
if m:plugin_exists('amb') then
    m:try_load_plugin('amb')

    if builtin.method.amb_initiate and
       builtin.method.amb_update
    then
        with_amb = true
    end
else
    m:info("No amb plugin found...")
end

-- load the ASM resource plugin
if m:plugin_exists('resource-asm') then
    m:try_load_plugin('resource-asm', {
        zone = "driver",
        share_mmplayer = "player:AVP,mandatory,exclusive,strict",
        ignored_argv0 = "WebProcess"
    })
else
    m:info("No audio session manager plugin found...")
end

if m:plugin_exists('system-controller') then
    with_system_controller = true
elseif m:plugin_exists('ivi-resource-manager') then
    m:load_plugin('ivi-resource-manager')
    with_system_controller = false
end

-- define application classes
application_class {
    name     = "interrupt",
    priority = 99,
    modal    = true ,
    share    = false,
    order    = "fifo"
}

application_class {
    name     = "emergency",
    priority = 80,
    modal    = false,
    share    = false,
    order    = "fifo"
}
application_class {
    name     = "system",
    priority = 52,
    modal    = false,
    share    = true,
    order    = "lifo"
}
application_class {
    name     = "alert",
    priority = 51,
    modal    = false,
    share    = false,
    order    = "fifo"
}

application_class {
    name     = "navigator",
    priority = 50,
    modal    = false,
    share    = true,
    order    = "fifo"
}

application_class {
    name     = "phone",
    priority = 6 ,
    modal    = false,
    share    = false,
    order    = "lifo"
}
application_class {
    name     = "camera",
    priority = 5,
    modal    = false,
    share    = false,
    order    = "lifo"
}

application_class { name="event"    , priority=4 , modal=false, share=true , order="fifo" }
application_class { name="game"     , priority=3 , modal=false, share=false, order="lifo" }
--# doesn't need to be created here, ivi-resource-manager creates it if loaded
--#application_class { name="basic"    , priority=2 , modal=false, share=false, order="lifo" }
application_class { name="player"   , priority=1 , modal=false, share=true , order="lifo" }
application_class { name="implicit" , priority=0 , modal=false, share=false, order="lifo" }

-- define zone attributes
zone.attributes {
    type = {mdb.string, "common", "rw"},
    location = {mdb.string, "anywhere", "rw"}
}

-- define zones
zone {
     name = "driver",
     attributes = {
         type = "common",
         location = "front-left"
     }
}

zone {
     name = "passanger1",
     attributes = {
         type = "private",
         location = "front-right"
     }
}

zone {
     name = "passanger2",
     attributes = {
         type = "private",
         location = "back-left"
     }
}

zone {
     name = "passanger3",
     attributes = {
         type = "private",
         location = "back-right"
     }
}

zone {
     name = "passanger4",
     attributes = {
         type = "private",
         location = "back-left"
     }
}


-- define resource classes
if not m:plugin_exists('ivi-resource-manager') and
   not with_system_controller and
   not m:plugin_exists('gam-resource-manager')
then
   resource.class {
        name = "audio_playback",
        shareable = true,
        attributes = {
            role    = { mdb.string, "music", "rw" },
            pid     = { mdb.string, "<unknown>", "rw" },
            policy  = { mdb.string, "relaxed", "rw" },
            source  = { mdb.string, "webkit", "rw" },
            conn_id = { mdb.unsigned, 0, "rw" },
            name    = { mdb.string, "<unknown>", "rw" },
        }
   }
end

if not m:plugin_exists('gam-resource-manager') then
    resource.class {
        name = "audio_recording",
        shareable = true,
        attributes = {
             role   = { mdb.string, "music"    , "rw" },
             pid    = { mdb.string, "<unknown>", "rw" },
             policy = { mdb.string, "relaxed"  , "rw" },
             name   = { mdb.string, "<unknown>", "rw" },
         }
    }
end

resource.class {
     name = "video_playback",
     shareable = false,
}

resource.class {
     name = "video_recording",
     shareable = false
}

resource.class {
     name = "speech_recognition",
     shareable = true
}

resource.class {
     name = "speech_synthesis",
     shareable = true
}

-- PulseAudio volume context
mdb.table {
    name = "volume_context",
    index = { "id" },
    create = true,
    columns = {
        { "id", mdb.unsigned },
        { "value", mdb.string, 64 },
    }
}

-- put default volume context to the table
mdb.table.volume_context:insert({ id = 1, value = "default" })

if not m:plugin_exists('ivi-resource-manager') and
   not with_system_controller
then
    resource.method.veto = {
        function(zone, rset, grant, owners, req_set)
            return true
         end
    }
end

-- test for creating selections
mdb.select {
           name = "audio_owner",
           table = "audio_playback_owner",
           columns = {"application_class"},
           condition = "zone_name = 'driver'"
}

mdb.select {
           name = "vehicle_speed",
           table = "amb_vehicle_speed",
           columns = {"value"},
           condition = "key = 'VehicleSpeed'"
}

element.lua {
   name    = "speed2volume",
   inputs  = { speed = mdb.select.vehicle_speed, param = 9 },
   outputs = {  mdb.table { name = "speedvol",
                            index = {"zone", "device"},
                            columns = {{"zone", mdb.string, 16},
                                       {"device", mdb.string, 16},
                                       {"value", mdb.floating}},
                            create = true
                           }
             },
   oldvolume = 0.0,
   update  = function(self)
                speed = self.inputs.speed.single_value
                if (speed) then
                    volume = (speed - 144.0) / 7.0
                else
                    volume = 0.0
                end
                diff = volume - self.oldvolume
                if (diff*diff > self.inputs.param) then
                    print("*** element "..self.name.." update "..volume)
                    self.oldvolume = volume
                    mdb.table.speedvol:replace({zone = "driver", device = "speakers", value = volume})
                end
             end
}

mdb.select {
    name = "amb_state",
    table = "amb_state",
    columns = { "state" },
    condition = "id = 0"
}

-- Night mode processing chain

mdb.select {
    name = "exterior_brightness",
    table = "amb_exterior_brightness",
    columns = { "value" },
    condition = "key = 'ExteriorBrightness'"
}

element.lua {
    name    = "nightmode",
    inputs  = { brightness = mdb.select.exterior_brightness },
    oldmode = -1;
    outputs = {
        mdb.table {
            name = "amb_nightmode",
            index = { "id" },
            create = true,
            columns = {
                { "id", mdb.unsigned },
                { "night_mode", mdb.unsigned }
            }
        }
    },
    update = function(self)
        -- This is a trivial function to calculate night mode. Later, we will
        -- need a better threshold value and hysteresis to prevent oscillation.

        brightness = self.inputs.brightness.single_value

        if not brightness then
            return
        end

        print("*** element "..self.name.." update brightness: "..brightness)

        if brightness > 300 then
            mode = 0
        else
            mode = 1
        end

        print("*** resulting mode: ".. mode)

        if not (mode == self.oldmode) then
            mdb.table.amb_nightmode:replace({ id = 0, night_mode = mode })
        end

        self.oldmode = mode
    end
}

mdb.select {
    name = "select_night_mode",
    table = "amb_nightmode",
    columns = { "night_mode" },
    condition = "id = 0"
}

if with_amb then
    sink.lua {
        name = "night_mode",
        inputs = { NightMode = mdb.select.select_night_mode,
                   amb_state = mdb.select.amb_state },
        property = "NightMode",
        type = "b",
        initiate = builtin.method.amb_initiate,
        update = builtin.method.amb_update
    }
end

-- Night mode general handlers

if with_system_controller then
    sink.lua {
        name = "nightmode_homescreen",
        inputs = { owner = mdb.select.select_night_mode },
        initiate = function(self)
                -- data = mdb.select.select_night_mode.single_value
                return true
            end,
        update = function(self)
                send_night_mode_to(homescreen)
            end
    }
end

-- Driving mode processing chain

element.lua {
    name    = "drivingmode",
    inputs  = { speed = mdb.select.vehicle_speed },
    oldmode = -1;
    outputs = {
        mdb.table {
            name = "amb_drivingmode",
            index = { "id" },
            create = true,
            columns = {
                { "id", mdb.unsigned },
                { "driving_mode", mdb.unsigned }
            }
        }
    },
    update = function(self)

        speed = self.inputs.speed.single_value

        if not speed then
            return
        end

        if speed == 0 then
            mode = 0
        else
            mode = 1
        end

        if not (mode == self.oldmode) then
            mdb.table.amb_drivingmode:replace({ id = 0, driving_mode = mode })
        end

        self.oldmode = mode
    end
}

mdb.select {
    name = "select_driving_mode",
    table = "amb_drivingmode",
    columns = { "driving_mode" },
    condition = "id = 0"
}

if with_amb then
    sink.lua {
        name = "driving_mode",
        inputs = { DrivingMode = mdb.select.select_driving_mode,
                   amb_state = mdb.select.amb_state },
        property = "DrivingMode",
        type = "u",
        initiate = builtin.method.amb_initiate,
        update = builtin.method.amb_update
    }
end

-- turn signals (left, right)

mdb.select {
    name = "winker",
    table = "amb_turn_signal",
    columns = { "value" },
    condition = "key = 'TurnSignal'"
}

-- define three categories

mdb.select {
    name = "undefined_applications",
    table = "aul_applications",
    columns = { "appid" },
    condition = "category = '<undefined>'"
}

mdb.select {
    name = "basic_applications",
    table = "aul_applications",
    columns = { "appid" },
    condition = "category = 'basic'"
}

mdb.select {
    name = "entertainment_applications",
    table = "aul_applications",
    columns = { "appid" },
    condition = "category = 'entertainment'"
}

function ft(t)
    -- filter the object garbage out of the tables
    ret = {}

    for k,v in pairs(t) do
        if k ~= "userdata" and k ~= "new" then
            ret[k] = v
        end
    end

    return ret
end

function getApplication(appid)
    local conf = nil

    -- find the correct local application definition

    for k,v in pairs(ft(application)) do
        if appid == v.appid then
            conf = v
            break
        end
    end

    return conf
end

function regulateApplications(t, regulation)
    for k,v in pairs(ft(t)) do

        whitelisted = false

        -- our local application config, which takes precedence
        local conf = getApplication(v.appid)

        if conf then
            if conf.resource_class ~= "player" then
                whitelisted = true
            end

	       if conf.requisites and conf.requisites.screen then
                if conf.requisites.screen.driving then
                    whitelisted = true
                end
            end
        end

        if whitelisted then
            -- override, don't disable
            resmgr:disable_screen_by_appid("*", "*", v.appid, false, false)
        else
            resmgr:disable_screen_by_appid("*", "*", v.appid, regulation == 1, false)
        end
    end
    resource.method.recalc("driver")
end

-- regulation (on), use "select_driving_mode"

sink.lua {
    name = "driving_regulation",
    inputs = { owner = mdb.select.select_driving_mode },
    initiate = function(self)
        -- local data = mdb.select.select_driving_mode.single_value
        return true
    end,
    update = function(self)
        local data = mdb.select.select_driving_mode.single_value

        if verbose > 1 then
            print("Driving mode updated: " .. tostring(data))
        end

        if not sc then
            return true
        end

        -- tell homescreen that driving mode was updated
        send_driving_mode_to(homescreen)

        regulateApplications(ft(mdb.select.entertainment_applications), data)
        regulateApplications(ft(mdb.select.undefined_applications), data)

        return true
    end
}
--[[
sink.lua {
    name = "regulated_app_change",
    inputs = { undef = mdb.select.undefined_applications,
               entertainment = mdb.select.entertainment_applications },
    initiate = function(self)
        return true
    end,
    update = function(self)
        local data = mdb.select.select_driving_mode.single_value

        if not sc then
            return
        end

        if verbose > 1 then
            print("regulated application list was changed")
        end

        regulateApplications(ft(mdb.select.entertainment_applications), data)
        regulateApplications(ft(mdb.select.undefined_applications), data)

        return true
    end
}
--]]

-- shift position (parking, reverse, other)

mdb.select {
    name = "gear_position",
    table = "amb_gear_position",
    columns = { "value" },
    condition = "key = 'GearPosition'"
}

-- cameras (back, front, left, right)

element.lua {
    name    = "camera_state",
    inputs  = { winker = mdb.select.winker, gear = mdb.select.gear_position },
    oldmode = -1;
    outputs = {
        mdb.table {
            name = "target_camera_state",
            index = { "id" },
            create = true,
            columns = {
                { "id", mdb.unsigned },
                { "front_camera", mdb.unsigned },
                { "back_camera", mdb.unsigned },
                { "right_camera", mdb.unsigned },
                { "left_camera", mdb.unsigned }
            }
        }
    },
    update = function(self)

        front_camera = 0
        back_camera = 0
        right_camera = 0
        left_camera = 0

        if self.inputs.gear == 128 then
            back_camera = 1
        elseif self.inputs.winker == 1 then
            right_camera = 1
        elseif self.inputs.winker == 2 then
            left_camera = 1
        end

        mdb.table.target_camera_state:replace({ id = 0, front_camera = front_camera, back_camera = back_camera, right_camera = right_camera, left_camera = left_camera })

    end
}

-- system controller test setup

if not with_system_controller then
   -- ok, we should have 'audio_playback' defined by now
   m:try_load_plugin('telephony')
   return
end

m:load_plugin('system-controller')

onscreen_counter = 0

window_manager_operation_names = {
    [1] = "create",
    [2] = "destroy"
}

function window_manager_operation_name(oper)
    local name = window_manager_operation_names[oper]
    if name then return name end
    return "<unknown " .. tostring(oper) .. ">"
end

window_operation_names = {
    [1] = "create",
    [2] = "destroy",
    [3] = "name_change",
    [4] = "visible",
    [5] = "configure",
    [6] = "active",
    [7] = "map",
    [8] = "hint"
}

function window_operation_name(oper)
    local name = window_operation_names[oper]
    if name then return name end
    return "<unknown " .. tostring(oper) .. ">"
end

layer_operation_names = {
    [1] = "create",
    [2] = "destroy",
    [3] = "visible"
}

function layer_operation_name(oper)
    local name = layer_operation_names[oper]
    if name then return name end
    return "<unknown " .. tostring(oper) .. ">"
end

input_manager_operation_names = {
    [1] = "create",
    [2] = "destroy",
    [3] = "ready"
}

function input_manager_operation_name(oper)
    local name = input_manager_operation_names[oper]
    if name then return name end
    return "<unknown " .. tostring(oper) .. ">"
end

input_operation_names = {
    [1] = "create",
    [2] = "destroy",
    [3] = "update"
}

function input_operation_name(oper)
    local name = input_operation_names[oper]
    if name then return name end
    return "<unknown " .. tostring(oper) .. ">"
end

code_operation_names = {
    [1] = "create",
    [2] = "destroy",
    [3] = "state_change"
}

function code_operation_name(oper)
    local name = code_operation_names[oper]
    if name then return name end
    return "<unknown " .. tostring(oper) .. ">"
end

command_names = {
    [0x00001] = "send_appid",
    [0x10001] = "create",
    [0x10002] = "destroy",
    [0x10003] = "show",
    [0x10004] = "hide",
    [0x10005] = "move",
    [0x10006] = "animation",
    [0x10007] = "change_active",
    [0x10008] = "change_layer",
    [0x10009] = "change_attr",
    [0x10010] = "name",
    [0x10020] = "map_thumb",
    [0x10021] = "unmap_thumb",
    [0x10022] = "map_get",
    [0x10030] = "show layer",
    [0x10031] = "hide_layer",
    [0x10032] = "change_layer_attr",
    [0x20001] = "add_input",
    [0x20002] = "del_input",
    [0x30001] = "change_user",
    [0x30002] = "get_userlist",
    [0x30003] = "get_lastinfo",
    [0x30004] = "set_lastinfo",
    [0x40001] = "acquire_res",
    [0x40002] = "release_res",
    [0x40003] = "deprive_res",
    [0x40004] = "waiting_res",
    [0x40005] = "revert_res",
    [0x40006] = "window_id_res",
    [0x40011] = "create_res",
    [0x40012] = "destroy_res",
    [0x50001] = "set_region",
    [0x50002] = "unset_region",
    [0x60001] = "change_state"
}

function command_name(command)
    local name = command_names[command]
    if name then return name end
    return "<unknown " .. tostring(command) .. ">"
end

input_layer = {
   [101] = true, -- input
   [102] = true, -- touch
   [103] = true  -- cursor
}

-- some day this should be merged with wmgr.layers
ico_layer_type = {
   [1]   = 0x1000, -- background
   [2]   = 0x2000, -- application
   [3]   = 0x2000, -- homescreen
   [4]   = 0x2000, -- interrupt application
   [5]   = 0x2000, -- onscreen application
   [6]   = 0xc000, -- startup
   [7]   = 0x3000, -- fullscreen
   [101] = 0x4000, -- input
   [102] = 0xa000, -- touch
   [103] = 0xb000  -- cursor
}

resmgr = resource_manager {
  screen_event_handler = function(self, ev)
                             local event = ev.event
                             local surface = ev.surface

                             if event == "init" then
                                 if verbose > 0 then
                                     print("*** init screen resource allocation -- disable all 'player'")
                                 end
                                 resmgr:disable_audio_by_appid("*", "player", "*", true, false)
                             elseif event == "preallocate" then
                                 if verbose > 0 then
                                     print("*** preallocate screen resource "..
                                           "for '" .. ev.appid .. "' -- enable 'player', if any")
                                 end
                                 resmgr:disable_audio_by_appid("*", "player", ev.appid, false, false)
                             elseif event == "grant" then
                                 if verbose > 0 then
                                    print("*** make visible surface "..surface)
                                 end
                                 local a = animation({})
                                 local r = m:JSON({surface = surface,
                                                   visible = 1,
                                                   raise   = 1})
                                 if ev.appid == onscreen then
                                     onscreen_counter = onscreen_counter + 1
                                     wmgr:layer_request(m:JSON({layer = 5, visible = 1}))
                                 end

                                 wmgr:window_request(r,a,0)
                             elseif event == "revoke" then
                                 if verbose > 0 then
                                    print("*** hide surface "..surface)
                                 end
                                 local a = animation({})
                                 local r = m:JSON({surface = ev.surface,
                                                   visible = 0})
                                 if ev.appid == onscreen then
                                     onscreen_counter = onscreen_counter - 1
                                     if onscreen_counter <= 0 then
                                        onscreen_counter = 0
                                        wmgr:layer_request(m:JSON({layer = 5, visible = 0}))
                                     end
                                 end
                                 wmgr:window_request(r,a,0)

                             elseif event == "create" then

                                if verbose > 0 then
                                    print("*** screen resource event: " ..
                                          tostring(ev))
                                end

                                local regulation = mdb.select.select_driving_mode.single_value

                                if regulation == 1 then

                                    local blacklisted = false

                                    -- applications which have their category set to "entertainment"
                                    -- or "undefined" are blacklisted, meaning they should be regulated

                                    for i,v in pairs(ft(mdb.select.undefined_applications)) do
                                        if v.appid == ev.appid then
                                            if verbose > 0 then
                                                print(ev.appid .. " was blacklisted (undefined)")
                                            end
                                            blacklisted = true
                                            break
                                        end
                                    end

                                    if not blacklisted then
                                        for i,v in pairs(ft(mdb.select.entertainment_applications)) do
                                            if v.appid == ev.appid then
                                                if verbose > 0 then
                                                    print(ev.appid .. " was blacklisted (entertainment)")
                                                end
                                                blacklisted = true
                                                break
                                            end
                                        end
                                    end

                                    -- our local application config, which takes precedence
                                    local conf = getApplication(ev.appid)

                                    if not conf then
                                        blacklisted = true
                                    else
                                        if conf.resource_class == "player" then
                                            blacklisted = true
                                        end

                                        -- check the exceptions
                                        if conf.requisites and conf.requisites.screen then
                                            if conf.requisites.screen.driving then
                                                blacklisted = false
                                            end
                                        end
                                    end

                                    -- disable only non-whitelisted applications
				                    if blacklisted then
                                        if verbose > 0 then
                                            print("disabling screen for " .. ev.appid)
                                        end
                                        resmgr:disable_screen_by_appid("*", "*", ev.appid, true, true)
                                    end
                                end

                             elseif event == "destroy" then
                               if verbose > 0 then
                                    print("*** screen resource event: " ..
                                          tostring(ev))
                                 end
                             else
                                 if verbose > 0 then
                                    print("*** screen resource event: " ..
                                          tostring(ev))
                                 end
                             end
                         end,
  audio_event_handler = function(self, ev)
                             local event = ev.event
                             local appid = ev.appid
                             local audioid = ev.audioid

                             if event == "grant" then
                                 if verbose > 0 then
                                    print("*** grant audio to "..appid..
                                          " ("..audioid..") in '" ..
                                          ev.zone .. "' zone")
                                 end
                             elseif event == "revoke" then
                                 if verbose > 0 then
                                    print("*** revoke audio from "..appid..
                                          " ("..audioid..") in '" ..
                                          ev.zone .. "' zone")
                                 end
                             else
                                 if verbose > 0 then
                                    print("*** audio resource event: " ..
                                          tostring(ev))
                                 end
                             end
                        end
}

resclnt = resource_client {}

wmgr = window_manager {
  geometry = function(self, w,h, v)
                  if type(v) == "function" then
                      return v(w,h)
                  end
                  return v
             end,

  application = function(self, appid)
                     if appid then
                         local app = application_lookup(appid)
                         if not app then
                             app = application_lookup("default")
                         end
                         return app
                     end
                     return { privileges = {screen="none", audio="none"} }
                end,

  output_order = { 1, 0 },

  outputs = { { name  = "Mid",
                id    = 1,
                zone  = "driver",
                areas = { Full = {
                              id     = 20,
                              pos_x  = 0,
                              pos_y  = 0,
                              width  = function(w,h) return w end,
                              height = function(w,h) return h end
                          },
                          Left = {
                              id     = 21,
                              pos_x  = 0,
                              pos_y  = 0,
                              width  = 320,
                              height = function(w,h) return h end
                          },
                          Right = {
                              id     = 22,
                              pos_x  = function(w,h) return w-320 end,
                              pos_y  = 0,
                              width  = 320,
                              height = function(w,h) return h end
                          }
                        }
               },
               { name  = "Center",
                 id    = 4,
                 zone  = "driver",
                 areas = { Status = {
                              id     = 0,
                              pos_x  = 0,
                              pos_y  = 0,
                              width  = function(w,h) return w end,
                              height = 64
                           },
                           Full = {
                              id     = 1,
                              pos_x  = 0,
                              pos_y  = 64,
                              width  = function(w,h) return w end,
                              height = function(w,h) return h-64-128 end
                           },
                           Upper = {
                              id     = 2,
                              pos_x  = 0,
                              pos_y  = 64,
                              width  = function(w,h) return w end,
                              height = function(w,h) return (h-64-128)/2 end
                           },
                           Lower = {
                              id     = 3,
                              pos_x  = 0,
                              pos_y  = function(w,h) return (h-64-128)/2+64 end,
                              width  = function(w,h) return w end,
                              height = function(w,h) return (h-64-128)/2 end
                           },
                           UpperLeft = {
                              id     = 4,
                              pos_x  = 0,
                              pos_y  = 64,
                              width  = function(w,h) return w/2 end,
                              height = function(w,h) return (h-64-128)/2 end
                           },
                           UpperRight = {
                              id     = 5,
                              pos_x  = function(w,h) return w/2 end,
                              pos_y  = 64,
                              width  = function(w,h) return w/2 end,
                              height = function(w,h) return (h-64-128)/2 end
                           },
                           LowerLeft = {
                              id     = 6,
                              pos_x  = 0,
                              pos_y  = function(w,h) return (h-64-128/2)+64 end,
                              width  = function(w,h) return w/2 end,
                              height = function(w,h) return (h-64-128)/2 end
                           },
                           LowerRight = {
                              id     = 7,
                              pos_x  = function(w,h) return w/2 end,
                              pos_y  = function(w,h) return (h-64-128/2)+64 end,
                              width  = function(w,h) return w/2 end,
                              height = function(w,h) return (h-64-128)/2 end
                           },
                           SysApp = {
                              id     = 8,
                              pos_x  = 0,
                              pos_y  = 64,
                              width  = function(w,h) return w end,
                              height = function(w,h) return h-64-128 end
                           },
                           ["SysApp.Left"] = {
                              id     = 9,
                              pos_x  = 0,
                              pos_y  = 64,
                              width  = function(w,h) return w/2-181 end,
                              height = function(w,h) return h-64-128 end
                           },
                           ["SysApp.Right"] = {
                              id     = 10,
                              pos_x  = function(w,h) return w/2+181 end,
                              pos_y  = 64,
                              width  = function(w,h) return w/2-181 end,
                              height = function(w,h) return h-64-128 end
                           },
                           MobileFull = {
                              id     = 11,
                              pos_x  = 0,
                              pos_y  = 64,
                              width  = function(w,h) return w end,
                              height = function(w,h) return h-64-128 end
                           },
                           MobileUpper = {
                              id     = 12,
                              pos_x  = 0,
                              pos_y  = 64,
                              width  = function(w,h) return w end,
                              height = function(w,h) return (h-64-128)/2 end
                           },
                           MobileLower = {
                              id     = 13,
                              pos_x  = 0,
                              pos_y  = function(w,h) return (h-64-128)/2+64 end,
                              width  = function(w,h) return w end,
                              height = function(w,h) return (h-64-128)/2 end
                           },
                           Control = {
                              id     = 14,
                              pos_x  = 0,
                              pos_y  = function(w,h) return h-128 end,
                              width  = function(w,h) return w end,
                              height = 128
                           },
                        }
              }
  },
             --    id   name            type  output
  layers = { {      0, "BackGround"   ,    1, "Center" },
             {      1, "Application"  ,    2, "Center" },
             {      2, "HomeScreen"   ,    3, "Center" },
             {      3, "ControlBar"   ,    3, "Center" },
             {      4, "InterruptApp" ,    4, "Center" },
             {      5, "OnScreen"     ,    5, "Center" },
             {      6, "Touch"        ,  102, "Center" },
             {      7, "Cursor"       ,  103, "Center" }
  },


  manager_update = function(self, oper)
                       if verbose > 0 then
                           print("### <== WINDOW MANAGER UPDATE:" ..
                                 window_manager_operation_name(oper))
                       end
                       if oper == 1 then
                           local wumask = window_mask { --raise   = true,
                                                        visible = true,
                                                        active  = true }
                           local wrmask = window_mask { raise   = true,
                                                        active  = true,
                                                        layer   = true }
                           local lumask = layer_mask  { visible = true }
                           local lrmask = layer_mask  { visible = true }
                           local req = m:JSON({
                               passthrough_window_update  = wumask:tointeger(),
                               passthrough_window_request = wrmask:tointeger(),
                               passthrough_layer_update   = lumask:tointeger(),
                               passthrough_layer_request  = lrmask:tointeger()
                           })
                           self:manager_request(req)
                       end
                   end,

  window_update = function(self, oper, win, mask)
                      if verbose > 0 then
                          print("### <== WINDOW UPDATE oper:" ..
                                window_operation_name(oper) ..
                                " mask: " .. tostring(mask))
                          if verbose > 1 then
                              print(win)
                          end
                      end

                      local arg = m:JSON({ surface = win.surface,
                                           winname = win.name
                      })
                      local command = 0

                      if oper == 1 then -- create
                           local layertype = win.layertype
                           if layertype and input_layer[layertype] then
                               if verbose > 0 then
                                   print("ignoring input panel creation")
                               end
                               return
                           end
                           command     = 0x10001
                      elseif oper == 2 then -- destroy
                           command     = 0x10002
                      elseif oper == 3 then  -- namechange
                           command     = 0x10010
                      elseif oper == 4 or oper == 5 then -- visible/configure
                           command     = 0x10009
                           arg.zone    = win.area
                           arg.node    = win.node
                           if win.layertype then
                               arg.layertype = win.layertype
                           end
                           arg.layer   = win.layer
                           arg.pos_x   = win.pos_x
                           arg.pos_y   = win.pos_y
                           arg.width   = win.width
                           arg.height  = win.height
                           arg.raise   = win.raise
                           arg.visible = win.visible
                           if win.active == 0 then
                               arg.active = 0
                           else
                               arg.active = 1
                           end
                      elseif oper == 6 then -- active
                           if win.active == 0 then
                               if verbose > 0 then
                                   print("ignoring inactive event")
                               end
                               return
                           end
                           command = 0x10007
                      elseif oper == 7 then -- map
                           local map = win.map
                           if not map then
                               return
                           end
                           if win.mapped == 0 then
                               command = 0x10021
                           else
                               command = 0x10020
                           end
                           arg.attr = map.type
                           --arg.name = map.target
                           arg.width = map.width
                           arg.height = map.height
                           arg.stride = map.stride
                           arg.format = map.format
                      else
                           if verbose > 0 then
                               print("### nothing to do")
                           end
                           return
                      end

                      local msg = m:JSON({ command = command,
                                           appid   = win.appid,
                                           pid     = win.pid,
                                           arg     = arg
                      })
                      if verbose > 0 then
                          print("### <== sending " ..
                                command_name(msg.command) ..
                                " window message to '" .. homescreen .. "'")
                          if verbose > 1 then
                              print(msg)
                          end
                      end
                      sc:send_message(homescreen, msg)

                      if oper == 1 then -- create
                          local i = input_layer[win.layertype]
                          local p = self:application(win.appid)
                          local s = p.privileges.screen

                          if s == "system" then
                              local a = animation({})
                              local r = m:JSON({surface = win.surface,
                                                visible = 0,
                                                raise   = 1})
                              self:window_request(r,a,0)
                          else
                              if i then
                                  if verbose > 0 then
                                      print("do not make resource for " ..
                                            "input window")
                                  end
                              else
                                  resclnt:resource_set_create("screen",
                                                               "driver",
                                                               win.appid,
                                                               win.surface)
                                  special_screen_sets[win.surface] = true
                              end
                          end

                          if onscreen and win.appid == onscreen then
                              local resmsg = m:JSON({
                                        command = 0x40006, -- window_id_res
                                        appid   = win.appid,
                                        pid     = win.pid,
                                        res     = m:JSON({
                                            window  = m:JSON({
                                                ECU     = "",
                                                display = "",
                                                layer   = "",
                                                layout  = "",
                                                area    = "",
                                                dispatchApp = "",
                                                role        = win.name,
                                                resourceId  = win.surface
                                            })
                                        })
                              })
                              if verbose > 0 then
                                  print("### <== sending " ..
                                        command_name(resmsg.command) ..
                                        " message to '" .. onscreen .. "'")
                                  if verbose > 1 then
                                      print(resmsg)
                                  end
                              end
                              sc:send_message(onscreen, resmsg);
                          end
                      elseif oper == 2 then -- destroy
                          resclnt:resource_set_destroy("screen", win.surface)
                          special_screen_sets[win.surface] = nil
                      elseif oper == 6 then -- active
                          if win.active then
                              local i = input_layer[win.layertype]
                              local p = self:application(win.appid)
                              local s = p.privileges.screen
                              local surface = win.surface
                              if not i and s ~= "system" then
                                 resclnt:resource_set_acquire("screen",surface)
                                 resmgr:window_raise(win.appid, surface, 1)
                              end
                          end
                      end
                  end,

  layer_update = function(self, oper, layer, mask)
                      if verbose > 0 then
                          print("### LAYER UPDATE:" ..
                                layer_operation_name(oper) ..
                                " mask: " .. tostring(mask))
                          if verbose > 1 then
                              print(layer)
                          end
                      end
                      if oper == 3 then -- visible
                         local command = 0x10008 -- change_layer
                         local msg = m:JSON({
                                         command = command,
                                         appid = "",
                                         arg = m:JSON({layer = layer.id,
                                                       visible = layer.visible
                                         })
                                })
                         if verbose > 0 then
                            print("### <== sending "..command_name(command)..
                                  " layer message")
                            if verbose > 1 then
                                print(msg)
                            end
                         end
                         sc:send_message(homescreen, msg)
                      else
                           if verbose > 0 then
                               print("### nothing to do")
                           end
                      end
                 end,

  output_update = function(self, oper, out, mask)
                      local idx = out.index
                      local defidx = self.output_order[idx+1]
                      if verbose > 0 then
                          print("### OUTPUT UPDATE:" .. oper ..
                                " mask: "..tostring(mask))
                      end
                      if not defidx then
                          return
                      end
                      print(out)
                      local outdef = self.outputs[defidx+1]
                      if (oper == 1) then -- create
                          if outdef then
                              self:output_request(m:JSON({index = idx,
                                                          id    = outdef.id,
                                                          name  = outdef.name
                                                          }))
                          end
                      elseif (oper == 5) then -- done
                          local ads = outdef.areas
                          local on = outdef.name
                          if ads then
                              for name,ad in pairs(ads) do
                                  local can = wmgr:canonical_name(on.."."..name)
                                  local a = m:JSON({name   = name,
                                                    output = out.index})
                                  for fld,val in pairs(ad) do
                                      a[fld] = self:geometry(out.width,
                                                             out.height,
                                                             val)
                                   end
                                   self:area_create(a)
                                   resmgr:area_create(area[can], outdef.zone)
                              end
                          end
                      end
                  end
}


imgr = input_manager {
  inputs = {{ name = "G27 Racing Wheel",
              id = 0,
              switch = { [2] = {appid="org.tizen.ico.app-soundsample"      },
                         [3] = {appid="org.tizen.ico.homescreen", keycode=1},
                         [4] = {appid="org.tizen.ico.app-soundsample"      },
                         [5] = {appid="org.tizen.ico.homescreen", keycode=2}
             }}
  },

  manager_update = function(self, oper)
                       if verbose > 0 then
                           print("### <== INPUT MANAGER UPDATE:" ..
                                 input_manager_operation_name(oper))
                       end
                   end,

  input_update = function(self, oper, inp, mask)
                     if verbose > 0 then
                         print("### INPUT UPDATE:" ..
                                input_operation_name(oper) ..
                                " mask: " .. tostring(mask))
                          if verbose > 1 then
                              print(inp)
                          end
                      end
                 end,
  code_update  = function(self, oper, code, mask)
                     if verbose > 0 then
                         print("### CODE UPDATE: mask: " .. tostring(mask))
                         if verbose > 1 then
                             print(code)
                         end
                     end
                     local msg = m:JSON({ command = 1,
                                          appid = "org.tizen.ico.homescreen",
                                          arg = m:JSON({ device = code.device,
                                                         time = code.time,
                                                         input = code.input,
                                                         code = code.id,
                                                         state = code.state
                                           })
                     })
                     if verbose > 0 then
                         print("### <== sending " ..
                               command_name(msg.command) ..
                               " input message")
                         if verbose > 1 then
                             print(msg)
                         end
                     end
                     sc:send_message(homescreen, msg)
                 end
}

sc = m:get_system_controller()

-- resource sets
sets = {}

-- special screen resource sets
-- TODO: just rewrite screen resource handling to use regular resource API

special_screen_sets = {}

-- user manager
um = m:UserManager()

connected = false
homescreen = ""
onscreen = ""

cids = {}

-- these shoud be before wmgr:connect() is called
if verbose > 0 then
   print("====== creating applications ======")
end
application {
    appid          = "default",
    area           = "Center.Full",
    privileges     = { screen = "none", audio = "none" },
    resource_class = "player",
    screen_priority = 0
}

application {
    appid           = "weston",
    area            = "Center.Full",
    privileges      = { screen = "system", audio = "none" },
    resource_class  = "implicit",
    screen_priority = 30
}

application {
    appid           = "org.tizen.ico.homescreen",
    area            = "Center.Full",
    windows         = { {'ico_hs_controlbarwindow', 'Center.Control'} },
    privileges      = { screen = "system", audio = "system" },
    resource_class  = "player",
    screen_priority = 20
}

application {
    appid           = "org.tizen.ico.statusbar",
    area            = "Center.Status",
    privileges      = { screen = "system", audio = "none" },
    resource_class  = "player",
    screen_priority = 20
}

application {
    appid           = "org.tizen.ico.onscreen",
    area            = "Center.Full",
    privileges      = { screen = "system", audio = "system" },
    resource_class  = "player",
    screen_priority = 20
}

application {
    appid           = "org.tizen.ico.login",
    area            = "Center.Full",
    privileges      = { screen = "system", audio = "system" },
    resource_class  = "player",
    screen_priority = 20
}

application {
    appid           = "org.tizen.ico.camera_left",
    area            = "Center.SysApp.Left",
    privileges      = { screen = "system", audio = "none" },
    requisites      = { screen = "blinker_left", audio = "none" },
    resource_class  = "player",
    screen_priority = 30
}

application {
    appid           = "org.tizen.ico.camera_right",
    area            = "Center.SysApp.Right",
    privileges      = { screen = "system", audio = "none" },
    requisites      = { screen = "blinker_right", audio = "none" },
    resource_class  = "player",
    screen_priority = 30
}

application {
    appid           = "net.zmap.navi",
    area            = "Center.Full",
    privileges      = { screen = "none", audio = "none" },
    resource_class  = "navigator",
    screen_priority = 30
}

application {
    appid           = "GV3ySIINq7.GhostCluster",
    area            = "Center.Full",
    privileges      = { screen = "none", audio = "none" },
    resource_class  = "system",
    requisites      = { screen = "driving", audio = "none" },
    screen_priority = 30
}

application {
    appid           = "MediaPlayer",
    area            = "Center.Full",
    privileges      = { screen = "none", audio = "none" },
    requisites      = { screen = "driving", audio = "none" },
    resource_class  = "player",
    screen_priority = 0
}

application {
    appid           = "MyMediaPlayer",
    area            = "Center.Full",
    privileges      = { screen = "none", audio = "none" },
    requisites      = { screen = "driving", audio = "none" },
    resource_class  = "player",
    screen_priority = 0
}

application {
    appid           = "MeterWidget",
    area            = "Center.Full",
    privileges      = { screen = "none", audio = "none" },
    requisites      = { screen = "driving", audio = "none" },
    resource_class  = "player",
    screen_priority = 0
}

application {
    appid           = "org.tizen.ico.app-soundsample",
    area            = "Center.Full",
    privileges      = { screen = "none", audio = "none" },
    -- uncomment the next line to make the app exempt from regulation
    -- requisites      = { screen = "driving", audio = "none" },
    resource_class  = "player",
    screen_priority = 0
}


if sc then
    sc.client_handler = function (self, cid, msg)
        local command = msg.command
        local appid = msg.appid
        if verbose > 0 then
            print('### ==> client handler:')
            if verbose > 1 then
                print(msg)
            end
        end

        -- known commands: 1 for SEND_APPID, synthetic command 0xFFFF for
        -- disconnection

        if command == 0xFFFF then
            if verbose > 1 then
                print('client ' .. cid .. ' disconnected')
            end
            if msg.appid == homescreen then
                homescreen = ""
                for i,v in pairs(special_screen_sets) do
                    resclnt:resource_set_destroy("screen", i)
                    special_screen_sets[i] = nil
                end
            end
            return
        end

        -- handle the connection to weston

        if appid then
            if appid == "org.tizen.ico.homescreen" then
                print('Setting homescreen='..appid)
                homescreen = appid
                if command and command == 1 then
                    send_driving_mode_to(homescreen)
                    send_night_mode_to(homescreen)
                end
            elseif appid == "org.tizen.ico.onscreen" then
                onscreen = appid
                if command and command == 1 then
                    send_driving_mode_to(onscreen)
                    send_night_mode_to(onscreen)
                end
            end

        if not connected and appid == "org.tizen.ico.homescreen" then
                print('Trying to connect to weston...')
                connected = wmgr:connect()
            end
            cids[cid] = appid
        end
    end

    sc.generic_handler = function (self, cid, msg)
        if verbose > 0 then
            print('### ==> generic handler:')
            if verbose > 1 then
               print(msg)
            end
        end
    end

    sc.window_handler = function (self, cid, msg)
        if verbose > 0 then
            print('### ==> received ' ..
                   command_name(msg.command) .. ' message from ' .. cids[cid])
            if verbose > 1 then
                print(tostring(msg))
            end
        end

        local a = animation({})
        local nores = false
        if msg.command == 0x10003 then       -- ico SHOW command
            local raise_mask = 0x01000000
            local lower_mask = 0x02000000
            local nores_mask = 0x40000000
            local time_mask  = 0x00ffffff

            local surface        = msg.arg.surface
            local system_surface = false
            local appdb          = wmgr:application(msg.appid)
            system_surface       = appdb.privileges.screen == "system"

            msg.arg.visible = 1

            if msg.arg then
                local time = 200
                if  msg.arg.anim_time then
                    local t = msg.arg.anim_time
                    -- the actual time for the animation
                    time = m:AND(t, time_mask)
                    -- flag for ignoring resource control
                    nores = m:AND(t, nores_mask)
                    if m:AND(t, raise_mask) then
                        msg.arg.raise = 1
                    elseif m:AND(t, lower_mask) then
                        msg.arg.raise = 0
                    end
                end
                if msg.arg.anim_name then
                    a.show = { msg.arg.anim_name, time }
                    print('time: ' .. tostring(time))
                end
            end

            -- all show messages from system surfaces are forced
            if not nores and system_surface then
                nores = true
                if not msg.arg.raise then
                    msg.arg.raise = 1
                end
            end

            if verbose > 2 then
                print('### ==> SHOW')
                print(tostring(msg.arg) .. ", system_surface=" .. tostring(system_surface))
            end

            if nores then
                wmgr:window_request(msg.arg, a, 0)

                -- only non-system surfaces have resource sets
                if not system_surface then
                    resclnt:resource_set_acquire("screen", surface)
                end
            else
                resclnt:resource_set_acquire("screen", surface)
                resmgr:window_raise(msg.appid, surface, 1)
            end
        elseif msg.command == 0x10004 then   -- ico HIDE command
            local raise_mask = 0x01000000
            local lower_mask = 0x02000000
            local nores_mask = 0x40000000
            local time_mask  = 0x00ffffff
            msg.arg.visible = 0
            if msg.arg then
                local time = 200
                if msg.arg.anim_time then
                    local t = msg.arg.anim_time
                    -- the actual time for the animation
                    time = m:AND(t, time_mask)
                    -- flag for ignoring resource control
                    nores = m:AND(t, nores_mask)
                end
                if msg.arg.anim_name then
                    a.hide = { msg.arg.anim_name, time }
                    print('hide animation time: ' .. tostring(a.hide[2]))
                end
            end
            if not nores then
                local p = wmgr:application(msg.appid)
                local s = p.privileges.screen
                if s == "system" then
                    nores = true
                    msg.arg.raise = 0
                end
            end
            if verbose > 2 then
                print('### ==> HIDE REQUEST')
                print(tostring(msg.arg))
            end
            if nores then
                wmgr:window_request(msg.arg, a, 0)
            else
                resmgr:window_raise(msg.appid, msg.arg.surface, -1)
            end
        elseif msg.command == 0x10005 then   -- ico MOVE
            if verbose > 2 then
                print('### ==> MOVE REQUEST')
                print(tostring(msg.arg))
            end
            if msg.arg.zone then
                msg.arg.area = msg.arg.zone
            end
            wmgr:window_request(msg.arg, a, 0)
            -- TODO: handle if area changed
        elseif msg.command == 0x10007 then   -- ico CHANGE_ACTIVE
            if not msg.arg.active then
                msg.arg.active = 3 -- pointer + keyboard
            end
            if verbose > 2 then
                print('### ==> CHANGE_ACTIVE REQUEST')
                print(tostring(msg.arg))
            end
            wmgr:window_request(msg.arg, a, 0)
        elseif msg.command == 0x10008 then   -- ico CHANGE_LAYER
            if verbose > 2 then
                print('### ==> CHANGE_LAYER REQUEST')
                print(tostring(msg.arg))
            end
            --[[
            if msg.arg.layer ~= 4 or msg.arg.layer ~= 5 then
                print("do not change layer for other than cursor or touch")
                return
            end
            --]]
            wmgr:window_request(msg.arg, a, 0)
        elseif msg.command == 0x10020 then   -- ico MAP_THUMB
            local framerate = msg.arg.framerate
            local animname  = msg.arg.anim_name
            if animname then
               a.map = { animname, 1 }
               if not framerate or framerate < 0 then
                  framerate = 5
               end
               msg.arg.mapped = 1
               if verbose > 2 then
                  print('### ==> MAP_THUMB REQUEST')
                  print(msg.arg)
                  print('framerate: '..framerate)
               end
               wmgr:window_request(msg.arg, a, framerate)
            end
        elseif msg.command == 0x10021 then   -- ico UNMAP_THUMB
            msg.arg.mapped = 0
            if verbose > 2 then
                print('### ==> UNMAP_THUMB REQUEST')
                print(msg.arg)
            end
            wmgr:window_request(msg.arg, a, 0)
--[[
        elseif msg.command == 0x10013 then -- ico MAP_BUFFER command
            local shmname = msg.arg.anim_name
            local bufsize = msg.arg.width
            local bufnum  = msg.arg.height
            if shmname and bufsize and bufnum then
                if verbose > 2 then
                    print('### ==> MAP_BUFFER REQUEST')
                    print("shmaname='" .. shmname ..
                          "' bufsize='" .. bufsize ..
                          " bufnum=" .. bufnum)
                end
                wmgr:buffer_request(shmname, bufsize, bufnum)
            end
--]]
        elseif msg.command == 0x10030 then   -- ico SHOW_LAYER command
            msg.arg.visible = 1
            if verbose > 2 then
                print('### ==> SHOW_LAYER REQUEST')
                print(msg.arg)
            end
            wmgr:layer_request(msg.arg)
        elseif msg.command == 0x10031 then   -- ico HIDE_LAYER command
            msg.arg.visible = 0
            if verbose > 2 then
                print('### ==> HIDE_LAYER REQUEST')
                print(msg.arg)
            end
            wmgr:layer_request(msg.arg)
        end
    end

    sc.input_handler = function (self, cid, msg)
        if verbose > 0 then
            print('### ==> input handler: ' .. command_name(msg.command))
            if verbose > 1 then
                print(msg)
            end
        end
        if msg.command == 0x20001 then -- add_input
            msg.arg.appid = msg.appid
            if verbose > 2 then
                print('### ==> ADD_INPUT REQUEST')
                print(tostring(msg.arg))
            end
            imgr:input_request(msg.arg)
        elseif msg.command == 0x20002 then -- del_input
            msg.arg.appid = ''
            if verbose > 2 then
                print('### ==> DEL_INPUT REQUEST')
                print(tostring(msg.arg))
            end
            imgr:input_request(msg.arg)
        -- elseif msg.command == 0x20003 then -- send_input
        end
    end

    sc.user_handler = function (self, cid, msg)
        if verbose > 0 then
            print('### ==> user handler: ' .. command_name(msg.command))
            if verbose > 1 then
                print(msg)
            end
        end

        if not um then
            print("User Manager not initialized")
            return
        end

        if msg.command == 0x30001 then -- change_user
            print("command CHANGE_USER")
            if not msg.arg then
                print("invalid message")
                return
            end

            username = msg.arg.user
            passwd = msg.arg.pass

            if not username then
                username = ""
            end

            if not passwd then
                passwd = ""
            end

            success = um:changeUser(username, passwd)

            if not success then
                reply = m.JSON({
                    appid = msg.appid,
                    command = cmd
                })
                if sc:send_message(msg.appid, reply) then
                    print('*** sent authentication failed message')
                else
                    print('*** failed to send authentication failed message')
                end
            end

        elseif msg.command == 0x30002 then -- get_userlist
            print("command GET_USERLIST")
            if not msg.appid then
                print("invalid message")
                return
            end

            users, currentUser = um:getUserList()

            if not users then
                print("failed to get user list")
                return
            end

            nUsers = 0

            for i,v in pairs(users) do
                nUsers = nUsers + 1
            end

            if not currentUser then
                currentUser = ""
            end

            if verbose > 1 then
                print("current user: " .. currentUser)
                print("user list:")
                for i,v in pairs(users) do
                    print(v)
                end
            end

            reply = m.JSON({
                appid = msg.appid,
                command = cmd,
                arg = m.JSON({
                    user_num = nUsers,
                    user_list = users,
                    user_login = currentUser
                })
            })

            if verbose > 1 then
                print("### <== GetUserList reply: " .. tostring(reply))
            end

            if sc:send_message(msg.appid, reply) then
                print('*** reply OK')
            else
                print('*** reply FAILED')
            end

        elseif msg.command == 0x30003 then -- get_lastinfo
            print("command GET_LASTINFO")
            if not msg.appid then
                print("invalid message")
                return
            end

            lastInfo = um:getLastInfo(msg.appid)

            if not lastInfo then
                print("failed to get last info for app" .. msg.appid)
                return
            end

            reply = m.JSON({
                appid = msg.appid,
                command = cmd,
                arg = m.JSON({
                    lastinfo = lastinfo
                })
            })

            if sc:send_message(msg.appid, reply) then
                print('*** reply OK')
            else
                print('*** reply FAILED')
            end

        elseif msg.command == 0x30004 then -- set_lastinfo
            print("command SET_LASTINFO")
            if not msg.arg or not msg.appid then
                print("invalid message")
                return
            end

            lastInfo = um:setLastInfo(msg.appid, msg.arg.lastinfo)
        end
    end

    sc.resource_handler = function (self, cid, msg)
        if verbose > 0 then
            print('### ==> resource handler: ' .. command_name(msg.command))
            if verbose > 1 then
                print(msg)
            end
        end

        getKey = function (msg)
            -- Field rset.key is an id for distinguishing between rsets. All
            -- resource types appear to contain a different key. Just pick one
            -- based on what we have on the message.

            key = nil

            if msg and msg.res then
                if msg.res.sound then
                    key = msg.res.sound.id
                elseif msg.res.input then
                    key = msg.res.input.name
                elseif msg.res.window then
                    -- alarm! this appars to be non-unique?
                    key = msg.res.window.resourceId
                end
            end

            return key
        end

        createResourceSet = function (ctl, client, msg)
            cb = function(rset)

                -- m:info("*** resource_cb: client = '" .. msg.appid .. "'")

                -- type is either basic (0) or interrupt (1)
                requestType = 0
                if msg.res.type then
                    requestType = msg.res.type
                end

                if rset.data.filter_first then
                    rset.data.filter_first = false
                    return
                end

                if rset.acquired then
                    cmd = 0x00040001 -- acquire

                    if msg.appid == onscreen then
                        -- notifications are valid only for a number of seconds
                        rset.timer = m:Timer({
                            interval = 5000,
                            oneshot = true,
                            callback = function (t)
                                m:info("notification timer expired")

                                if rset.data then
                                    reply.res.window = rset.data.window
                                end

                                rset:release()
                                rset.timer = nil

                                -- Send a "RELEASE" message to client.
                                -- This triggers the resource deletion
                                -- cycle in OnScreen.

                                reply = m.JSON({
                                    appid = msg.appid,
                                    command = cmd,
                                    res = {
                                        type = requestType
                                    }
                                })

                                sc:send_message(client, reply)
                            end
                        })
                    end
                else
                    cmd = 0x00040002 -- release
                    if rset.timer then
                       rset.timer.callback = nil
                       rset.timer = nil
                    end
                end

                reply = m.JSON({
                        appid = msg.appid,
                        command = cmd,
                        res = {
                            type = requestType
                        }
                    })

                if rset.data.sound then
                    reply.res.sound = rset.data.sound
                end

                if rset.data.window then
                    reply.res.window = rset.data.window
                end

                if rset.data.input then
                    reply.res.input = rset.data.input
                end

                if rset.acquired then
                    m:info("resource cb: 'acquire' reply to client " .. client)
                else
                    m:info("resource cb: 'release' reply to client " .. client)
                end

                if not sc:send_message(client, reply) then
                    m:info('*** reply FAILED')
                end
            end

            local rset = m:ResourceSet({
                    application_class = "player",
                    zone = "driver", -- msg.zone ("full")
                    callback = cb
                })

            rset.data = {
                cid = cid,
                ctl = ctl,
                filter_first = true
            }

            if msg.res.sound then
                rset:addResource({
                        resource_name = "audio_playback"
                    })
                rset.resources.audio_playback.attributes.pid = tostring(msg.pid)
                rset.resources.audio_playback.attributes.appid = msg.appid
                print("sound name: " .. msg.res.sound.name)
                print("sound zone:" .. msg.res.sound.zone)
                print("sound adjust: " .. tostring(msg.res.sound.adjust))

                rset.data.sound = msg.res.sound
            end

            if msg.res.input then
                rset:addResource({
                        resource_name = "input"
                    })
                rset.resources.input.attributes.pid = tostring(msg.pid)
                rset.resources.input.attributes.appid = msg.appid
                print("input name: " .. msg.res.input.name)
                print("input event:" .. tostring(msg.res.input.event))

                rset.data.input = msg.res.input
            end

            if msg.res.window then
                rset:addResource({
                        resource_name = "screen",
                        shared = true
                    })
                rset.resources.screen.attributes.pid = tostring(msg.pid)
                rset.resources.screen.attributes.appid = msg.appid
                rset.resources.screen.attributes.surface = msg.res.window.resourceId
                complete_area = msg.res.window.display .. '.' .. msg.res.window.area
                rset.resources.screen.attributes.area = complete_area
                if msg.appid == onscreen then
                    rset.resources.screen.attributes.classpri = 1
                else
                    rset.resources.screen.attributes.classpri = 0
                end

                rset.data.window = msg.res.window
            end

            rset.key = getKey(msg)

            return rset
        end

        -- parse the message

        -- fields common to all messages:
        --      msg.command
        --      msg.appid
        --      msg.pid

        if msg.command == 0x40011 then -- create_res
            print("command CREATE_RES")
            key = getKey(msg)

            if key then
                if not sets.cid then
                    sets.cid = {}
                end
                sets.cid[key] = createResourceSet(self, cid, msg)
            end

        elseif msg.command == 0x40012 then -- destroy_res
            print("command DESTROY_RES")
            key = getKey(msg)

            if key then
                if sets.cid and sets.cid[key] then
                    sets.cid[key]:release()
                    sets.cid[key].timer = nil
                    sets.cid[key] = nil -- garbage collecting
                end
            end

        elseif msg.command == 0x40001 then -- acquire_res
            print("command ACQUIRE_RES")
            key = getKey(msg)

            if key then
                if not sets.cid then
                    sets.cid = {}
                end
                if not sets.cid[key] then
                    sets.cid[key] = createResourceSet(self, cid, msg)
                end
                print("acquiring the resource set")
                sets.cid[key]:acquire()
            end

        elseif msg.command == 0x40002 then -- release_res
            print("command RELEASE_RES")

            key = getKey(msg)

            if key then
                if sets.cid and sets.cid[key] then
                    sets.cid[key]:release()

                    if msg.appid == onscreen then
                        -- in case of OnScreen, this actually means that the
                        -- resource set is never used again; let gc do its job
                        sets.cid[key].timer = nil
                        sets.cid[key] = nil -- garbage collecting
                    end
                end
            end

        elseif msg.command == 0x40003 then -- deprive_res
            print("command DEPRIVE_RES")

        elseif msg.command == 0x40004 then -- waiting_res
            print("command WAITING_RES")

        elseif msg.command == 0x40005 then -- revert_res
            print("command REVERT_RES")
        end
    end

    sc.inputdev_handler = function (self, cid, msg)
        if verbose > 0 then
            print('*** inputdev handler: ' .. command_name(msg.command))
            if verbose > 1 then
                print(msg)
            end
        end
    end

    sc.notify_handler = function (self, cid, msg)
        if verbose > 0 then
            print('### notify handler: ' .. command_name(msg.command))
            if verbose > 1 then
                print(msg)
            end
        end
    end
end

function send_driving_mode_to(client)
    if client == "" then
        return
    end

    local driving_mode = mdb.select.select_driving_mode.single_value

    if not driving_mode then driving_mode = 0 end

    local reply = m:JSON({ command = 0x60001,
                           arg     = m:JSON({ stateid = 1,
                                              state   = driving_mode
                                     })
                  })

    if verbose > 0 then
        print("### <== sending " .. command_name(reply.command) .. " message")
        if verbose > 1 then
            print(reply)
        end
    end

    sc:send_message(client, reply)
end

function send_night_mode_to(client)
    if client == "" then
        return
    end

    local night_mode = mdb.select.select_night_mode.single_value

    if not night_mode then night_mode = 0 end

    local reply = m:JSON({ command = 0x60001,
                           arg     = m:JSON({ stateid = 2,
                                              state   = night_mode
                                     })
                  })

    if verbose > 0 then
        print("### <== sending " .. command_name(reply.command) .. " message")
        if verbose > 1 then
            print(reply)
        end
     end

     sc:send_message(client, reply)
end

-- we should have 'audio_playback' defined by now
m:try_load_plugin('telephony')