File: virtualizer.c

package info (click to toggle)
ladish 1%2Bdfsg0-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,940 kB
  • sloc: ansic: 36,406; python: 11,237; cpp: 705; makefile: 22; ruby: 20; sh: 17
file content (1623 lines) | stat: -rw-r--r-- 43,814 bytes parent folder | download | duplicates (3)
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
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*
 * LADI Session Handler (ladish)
 *
 * Copyright (C) 2009, 2010, 2011 Nedko Arnaudov <nedko@arnaudov.name>
 *
 **************************************************************************
 * This file contains implementation of the graph virtualizer object
 **************************************************************************
 *
 * LADI Session Handler is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * LADI Session Handler 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 LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
 * or write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "virtualizer.h"
#include "../dbus_constants.h"
#include "../proxies/a2j_proxy.h"
#include "../proxies/jmcore_proxy.h"
#include "procfs.h"
#include "app_supervisor.h"
#include "studio_internal.h"
#include "../common/catdup.h"
#include "room.h"
#include "studio.h"
#include "../alsapid/alsapid.h"

struct virtualizer
{
  graph_proxy_handle jack_graph_proxy;
  ladish_graph_handle jack_graph;
  uint64_t system_client_id;
  unsigned int our_clients_count;
};

/* 47c1cd18-7b21-4389-bec4-6e0658e1d6b1 */
UUID_DEFINE(g_system_capture_uuid,0x47,0xC1,0xCD,0x18,0x7B,0x21,0x43,0x89,0xBE,0xC4,0x6E,0x06,0x58,0xE1,0xD6,0xB1);

/* b2a0bb06-28d8-4bfe-956e-eb24378f9629 */
UUID_DEFINE(g_system_playback_uuid,0xB2,0xA0,0xBB,0x06,0x28,0xD8,0x4B,0xFE,0x95,0x6E,0xEB,0x24,0x37,0x8F,0x96,0x29);

/* be23a242-e2b2-11de-b795-002618af5e42 */
UUID_DEFINE(g_a2j_uuid,0xBE,0x23,0xA2,0x42,0xE2,0xB2,0x11,0xDE,0xB7,0x95,0x00,0x26,0x18,0xAF,0x5E,0x42);

struct app_find_context
{
  pid_t pid;
  ladish_graph_handle graph;
  ladish_app_handle app;
};

#define app_find_context_ptr ((struct app_find_context *)context)

static bool lookup_app_in_supervisor(void * context, ladish_graph_handle graph, ladish_app_supervisor_handle app_supervisor)
{
  pid_t pid;
  ladish_app_handle app;

  /* we stop iteration when app is found */
  ASSERT(app_find_context_ptr->app == NULL && app_find_context_ptr->graph == NULL);

  //log_info("checking app supervisor \"%s\" for pid %llu", ladish_app_supervisor_get_name(app_supervisor), (unsigned long long)pid);

  pid = app_find_context_ptr->pid;
  do
  {
    app = ladish_app_supervisor_find_app_by_pid(app_supervisor, pid);
    if (app != NULL)
      break;

    pid = (pid_t)procfs_get_process_parent((unsigned long long)pid);
#if 0
    if (pid != 0)
    {
      log_info("parent pid %llu", (unsigned long long)pid);
    }
#endif
  }
  while (pid != 0);

  if (app == NULL)
  {                            /* app not found in current supervisor */
    return true;               /* continue app supervisor iteration */
  }

  app_find_context_ptr->app = app;
  app_find_context_ptr->graph = graph;

  return false;               /* stop app supervisor iteration */
}

#undef app_find_context_ptr

ladish_app_handle ladish_find_app_by_pid(pid_t pid, ladish_graph_handle * graph_ptr)
{
  struct app_find_context context;

  context.pid = pid;
  context.app = NULL;
  context.graph = NULL;

  ladish_studio_iterate_virtual_graphs(&context, lookup_app_in_supervisor);

  if (context.app == NULL)
  { /* app not found */
    ASSERT(context.graph == NULL);
    return NULL;
  }

  ASSERT(context.graph != NULL);
  if (graph_ptr != NULL)
  {
    *graph_ptr = context.graph;
  }

  return context.app;
}

struct find_link_port_context
{
  uuid_t uuid;
  uint64_t jack_id;
  ladish_port_handle port;
  ladish_graph_handle graph;
};

#define find_link_port_context_ptr ((struct find_link_port_context *)context)

static bool find_link_port_vgraph_callback_by_uuid(void * context, ladish_graph_handle graph, ladish_app_supervisor_handle app_supervisor)
{
  ladish_port_handle port;

  port = ladish_graph_find_port_by_uuid(graph, find_link_port_context_ptr->uuid, true, NULL);
  if (port != NULL)
  {
    find_link_port_context_ptr->port = port;
    find_link_port_context_ptr->graph = graph;
    return false;
  }

  return true;                  /* continue vgraph iteration */
}

static bool find_link_port_vgraph_callback_by_jack_id(void * context, ladish_graph_handle graph, ladish_app_supervisor_handle app_supervisor)
{
  ladish_port_handle port;
  bool room;

  log_info("searching link port with jack id %"PRIu64" in graph %s", find_link_port_context_ptr->jack_id, ladish_graph_get_description(graph));

  room = graph != g_studio.studio_graph;

  port = ladish_graph_find_port_by_jack_id(graph, find_link_port_context_ptr->jack_id, room, !room);
  if (port != NULL)
  {
    find_link_port_context_ptr->port = port;
    find_link_port_context_ptr->graph = graph;
    return false;
  }

  return true;                  /* continue vgraph iteration */
}

#undef find_link_port_context_ptr

static ladish_graph_handle find_link_port_vgraph_by_uuid(struct virtualizer * virtualizer_ptr, const char * port_name, ladish_port_handle * port_ptr)
{
  struct find_link_port_context context;

  uuid_parse(port_name, context.uuid);
  context.graph = NULL;
  context.port = NULL;

  ladish_studio_iterate_virtual_graphs(&context, find_link_port_vgraph_callback_by_uuid);

  if (port_ptr != NULL && context.graph != NULL)
  {
    *port_ptr = context.port;
  }

  return context.graph;
}

static ladish_graph_handle find_link_port_vgraph_by_jack_id(struct virtualizer * virtualizer_ptr, uint64_t jack_id, ladish_port_handle * port_ptr)
{
  struct find_link_port_context context;

  context.jack_id = jack_id;
  context.graph = NULL;
  context.port = NULL;

  ladish_studio_iterate_virtual_graphs(&context, find_link_port_vgraph_callback_by_jack_id);

  if (port_ptr != NULL && context.graph != NULL)
  {
    *port_ptr = context.port;
  }

  return context.graph;
}

static
bool
lookup_port(
  struct virtualizer * virtualizer_ptr,
  uint64_t port_id,
  ladish_port_handle * port_ptr,
  ladish_graph_handle * vgraph_ptr)
{
  ladish_port_handle port;
  ladish_graph_handle vgraph;

  port = ladish_graph_find_port_by_jack_id(virtualizer_ptr->jack_graph, port_id, true, true);
  if (port == NULL)
  {
    log_error("Unknown JACK port with id %"PRIu64" (dis)connected", port_id);
    return false;
  }

  vgraph = ladish_port_get_vgraph(port);
  if (vgraph == NULL)
  {
    vgraph = find_link_port_vgraph_by_jack_id(virtualizer_ptr, port_id, NULL);
    if (vgraph == NULL)
    {
      log_error("Cannot find vgraph for (dis)connected jmcore port");
      return false;
    }

    log_info("link port found in graph %s", ladish_graph_get_description(vgraph));
  }

  *port_ptr = port;
  *vgraph_ptr = vgraph;
  return true;
}

#define virtualizer_ptr ((struct virtualizer *)context)

static void clear(void * context)
{
  log_info("clear");
}

static void client_appeared(void * context, uint64_t id, const char * jack_name)
{
  ladish_client_handle client;
  const char * a2j_name;
  bool is_a2j;
  ladish_app_handle app;
  uuid_t app_uuid;
  const char * name;
  pid_t pid;
  ladish_graph_handle graph;
  bool jmcore;

  log_info("client_appeared(%"PRIu64", %s)", id, jack_name);

  a2j_name = a2j_proxy_get_jack_client_name_cached();
  is_a2j = a2j_name != NULL && strcmp(a2j_name, jack_name) == 0;

  name = jack_name;
  app = NULL;
  graph = NULL;
  jmcore = false;

  if (!graph_proxy_get_client_pid(virtualizer_ptr->jack_graph_proxy, id, &pid))
  {
    log_info("client %"PRIu64" pid is unknown", id);
  }
  else
  {
    log_info("client pid is %"PRId64, (int64_t)pid);

    if (pid != 0) /* skip internal clients that will match the pending clients in the graph, both have zero pid */
    {
      jmcore = pid == jmcore_proxy_get_pid_cached();
      if (jmcore)
      {
        log_info("jmcore client appeared");
      }
      else
      {
        app = ladish_find_app_by_pid(pid, &graph);
        if (app != NULL)
        {
          ladish_app_get_uuid(app, app_uuid);
          ASSERT(!uuid_is_null(app_uuid));
          name = ladish_app_get_name(app);
          log_info("app name is '%s'", name);
        }
      }
    }
  }

  if (!jmcore)
  {
    if (is_a2j)
    {
      client = ladish_graph_find_client_by_uuid(virtualizer_ptr->jack_graph, g_a2j_uuid);
    }
    else
    {
      if (app != NULL)
      {
        client = ladish_graph_find_client_by_app(virtualizer_ptr->jack_graph, app_uuid);
        if (client == NULL)
        {
          log_info("Lookup by app uuid failed, attempting lookup by name '%s'", name);
          goto find_by_name;
        }
      }
      else
      {
      find_by_name:
        client = ladish_graph_find_client_by_name(virtualizer_ptr->jack_graph, name, true);
      }
    }

    if (client != NULL)
    {
      log_info("found existing client");
      if (ladish_client_get_jack_id(client) != 0)
      {
        log_error("Ignoring client with duplicate name '%s' ('%s')", name, jack_name);
        goto exit;
      }

      ladish_client_set_jack_name(client, jack_name);

      ladish_client_set_jack_id(client, id);
      ladish_graph_show_client(virtualizer_ptr->jack_graph, client);
      goto done;
    }
  }

  if (!ladish_client_create(is_a2j ? g_a2j_uuid : NULL, &client))
  {
    log_error("ladish_client_create() failed. Ignoring client %"PRIu64" (%s)", id, jack_name);
    goto exit;
  }

  ladish_client_set_jack_id(client, id);
  ladish_client_set_jack_name(client, jack_name);

  if (!ladish_graph_add_client(virtualizer_ptr->jack_graph, client, name, false))
  {
    log_error("ladish_graph_add_client() failed to add client %"PRIu64" (%s) to JACK graph", id, name);
    ladish_client_destroy(client);
    goto exit;
  }

done:
  if (strcmp(jack_name, "system") == 0)
  {
    virtualizer_ptr->system_client_id = id;
  }

  if (app != NULL)
  {
    /* interlink client and app */
    ladish_app_add_pid(app, pid);
    ladish_client_set_pid(client, pid);
    ladish_client_set_app(client, app_uuid);

    ASSERT(graph);
    ladish_client_set_vgraph(client, graph);
    virtualizer_ptr->our_clients_count++;
  }
  else if (jmcore)
  {
    ladish_client_set_pid(client, pid);
    ASSERT(ladish_client_get_vgraph(client) == NULL);
  }
  else
  {
    /* unknown and internal clients appear in the studio graph */
    ladish_client_set_vgraph(client, g_studio.studio_graph);
  }

exit:
  return;
}

static void port_disappeared(void * context, uint64_t client_id, uint64_t port_id);

bool
force_port_disappear(
  void * context,
  ladish_graph_handle graph_handle,
  bool hidden,
  ladish_client_handle client_handle,
  const char * client_name,
  ladish_port_handle port_handle,
  const char * port_name,
  uint32_t port_type,
  uint32_t port_flags)
{
  uint64_t client_id;
  uint64_t port_id;

  if (hidden)
  {
    return true;
  }

  log_error("forcing disappear of port '%s':'%s'", client_name, port_name);

  client_id = ladish_client_get_jack_id(client_handle);
  port_id = ladish_port_get_jack_id(port_handle);
  port_disappeared(context, client_id, port_id);

  return true;
}

static void client_disappeared(void * context, uint64_t id)
{
  ladish_client_handle client;
  pid_t pid;
  uuid_t app_uuid;
  ladish_app_handle app;
  ladish_graph_handle vgraph;

  log_info("client_disappeared(%"PRIu64")", id);

  client = ladish_graph_find_client_by_jack_id(virtualizer_ptr->jack_graph, id);
  if (client == NULL)
  {
    log_error("Unknown JACK client with id %"PRIu64" disappeared", id);
    return;
  }

  log_info("client disappeared: '%s'", ladish_graph_get_client_name(virtualizer_ptr->jack_graph, client));

  /* This is a workaround for jack2/jackdbus bug. */
  ladish_graph_interate_client_ports(virtualizer_ptr->jack_graph, client, context, force_port_disappear);

  vgraph = ladish_client_get_vgraph(client);

  pid = ladish_client_get_pid(client);
  if (ladish_client_get_app(client, app_uuid))
  {
    virtualizer_ptr->our_clients_count--;
    app = ladish_studio_find_app_by_uuid(app_uuid);
    if (app != NULL)
    {
      ladish_app_del_pid(app, pid);
    }
    else
    {
      log_error("app of disappearing client %"PRIu64" not found. pid is %"PRIu64, id, (uint64_t)pid);
      ASSERT_NO_PASS;
    }
  }

  if (id == virtualizer_ptr->system_client_id)
  {
    virtualizer_ptr->system_client_id = 0;
  }

  if (vgraph != NULL && ladish_graph_is_persist(vgraph)) /* if client is supposed to be persisted */
  {
    ladish_client_set_jack_id(client, 0);
    ladish_graph_hide_client(virtualizer_ptr->jack_graph, client);
  }
  else
  {
    ladish_graph_remove_client(virtualizer_ptr->jack_graph, client);
    ladish_client_destroy(client);
    /* no need to clear vclient interlink because it either does not exist (vgraph is NULL) or
     * it will be destroyed before it is accessed (persist flag is cleared on room deletion) */
  }
}

static
void
port_appeared(
  void * context,
  uint64_t client_id,
  uint64_t port_id,
  const char * real_jack_port_name,
  bool is_input,
  bool is_terminal,
  bool is_midi)
{
  ladish_client_handle jack_client;
  ladish_client_handle vclient;
  ladish_port_handle port;
  uint32_t type;
  uint32_t flags;
  const char * jack_client_name;
  const char * vclient_name;
  bool is_a2j;
  uuid_t vclient_uuid;
  pid_t pid;
  ladish_app_handle app;
  bool has_app;
  uuid_t app_uuid;
  char * alsa_client_name;
  char * alsa_port_name;
  char * a2j_fake_jack_port_name = NULL;
  uint32_t alsa_client_id;
  const char * jack_port_name;
  const char * vport_name;
  ladish_graph_handle vgraph;

  log_info("port_appeared(%"PRIu64", %"PRIu64", %s (%s, %s))", client_id, port_id, real_jack_port_name, is_input ? "in" : "out", is_midi ? "midi" : "audio");

  type = is_midi ? JACKDBUS_PORT_TYPE_MIDI : JACKDBUS_PORT_TYPE_AUDIO;
  flags = is_input ? JACKDBUS_PORT_FLAG_INPUT : JACKDBUS_PORT_FLAG_OUTPUT;
  if (is_terminal)
  {
    flags |= JACKDBUS_PORT_FLAG_TERMINAL;
  }

  /********************/
  /* gather info about the appeared port */

  jack_client = ladish_graph_find_client_by_jack_id(virtualizer_ptr->jack_graph, client_id);
  if (jack_client == NULL)
  {
    log_error("Port of unknown JACK client with id %"PRIu64" appeared", client_id);
    goto exit;
  }

  has_app = ladish_client_get_app(jack_client, app_uuid);

  /* find the virtual graph that owns the app that owns the client that owns the appeared port */
  vgraph = ladish_client_get_vgraph(jack_client);
  if (vgraph == NULL)
  {
    vgraph = find_link_port_vgraph_by_uuid(virtualizer_ptr, real_jack_port_name, &port);
    if (vgraph == NULL)
    {
      log_error("Cannot find vgraph for appeared jmcore port '%s'", real_jack_port_name);
      goto exit;
    }

    /* jmcore port appeared */

    log_info("jmcore port appeared in vgraph %s", ladish_graph_get_description(vgraph));

    if (!ladish_graph_add_port(virtualizer_ptr->jack_graph, jack_client, port, real_jack_port_name, type, flags, false))
    {
      log_error("ladish_graph_add_port() failed.");
      goto exit;
    }

    if (vgraph == g_studio.studio_graph)
    {
      ladish_port_set_jack_id(port, port_id);
    }
    else
    {
      ladish_port_set_jack_id_room(port, port_id);
    }

    vclient = ladish_graph_get_port_client(vgraph, port);
    if (vclient == NULL)
    {
      log_error("link port client not found in vgraph %s", ladish_graph_get_description(vgraph));
      ASSERT_NO_PASS;
      goto exit;
    }

    ladish_graph_show_port(vgraph, port);
    goto exit;
  }
  else
  {
    //log_info("Port of virtual graph '%s'", ladish_graph_get_description(vgraph));
  }

  jack_client_name = ladish_graph_get_client_name(virtualizer_ptr->jack_graph, jack_client);

  is_a2j = ladish_virtualizer_is_a2j_client(jack_client);
  if (is_a2j)
  {
    log_info("a2j port appeared");
    if (!a2j_proxy_map_jack_port(real_jack_port_name, &alsa_client_name, &alsa_port_name, &alsa_client_id))
    {
      is_a2j = false;
      alsa_client_name = catdup("FAILED ", jack_client_name);
      if (alsa_client_name == NULL)
      {
        log_error("catdup failed to duplicate a2j jack client name after map failure");
        goto exit;
      }

      alsa_port_name = strdup(real_jack_port_name);
      if (alsa_port_name == NULL)
      {
        log_error("catdup failed to duplicate a2j jack port name after map failure");
        free(alsa_client_name);
        goto exit;
      }

      vclient_name = alsa_client_name;
    }
    else
    {
      log_info("a2j: '%s':'%s' (%"PRIu32")", alsa_client_name, alsa_port_name, alsa_client_id);
      vclient_name = alsa_client_name;
      if (alsapid_get_pid(alsa_client_id, &pid))
      {
        log_info("ALSA client pid is %lld", (long long)pid);

        app = ladish_find_app_by_pid(pid, &vgraph);
        if (app != NULL)
        {
          ladish_app_get_uuid(app, app_uuid);
          ASSERT(!uuid_is_null(app_uuid));
          vclient_name = ladish_app_get_name(app);
          has_app = true;
          log_info("ALSA app name is '%s'", vclient_name);
          ladish_app_add_pid(app, pid);
        }
      }
      else
      {
        log_error("UNKNOWN ALSA client pid");
      }
    }

    a2j_fake_jack_port_name = catdup4(vclient_name, is_input ? " (playback)" : " (capture)", ": ", alsa_port_name);
    if (a2j_fake_jack_port_name == NULL)
    {
      log_error("catdup4() failed");
      goto free_alsa_names;
    }

    jack_port_name = a2j_fake_jack_port_name;
  }
  else
  {
    vclient_name = jack_client_name;
    jack_port_name = real_jack_port_name;
  }

  /********************/

  /* search (by name) the appeared port in jack graph
   * if found - show it in both graphs.
   * if not found - create new port and add it to the jack graph.
   * Then process to adding it to virtual graph */

  port = ladish_graph_find_port_by_name(virtualizer_ptr->jack_graph, jack_client, jack_port_name, vgraph);
  if (port != NULL)
  {
    log_info("found existing port %p", port);

    if (ladish_port_get_jack_id(port) != 0)
    {
      log_error("Ignoring duplicate JACK port '%s':'%s'", jack_client_name, jack_port_name);
      goto free_alsa_names;
    }

    ladish_port_set_jack_id(port, port_id);
    ladish_graph_adjust_port(virtualizer_ptr->jack_graph, port, type, flags);
    ladish_graph_show_port(virtualizer_ptr->jack_graph, port);

    vclient = ladish_graph_get_port_client(vgraph, port);
    if (vclient == NULL)
    {
      log_error("JACK port not found in virtual graph '%s'", ladish_graph_get_description(vgraph));
      ladish_graph_dump(g_studio.jack_graph);
      ladish_graph_dump(vgraph);
      ASSERT_NO_PASS;
      goto free_alsa_names;
    }

    /* for normal ports, one can find the app_uuid through the jack client,
       but for a2j ports the jack client is shared between graphs */
    /* clients and ports can be reused if they were started externally then through ladish */
    if (has_app)
    {
      ladish_port_set_app(port, app_uuid);
      ladish_port_set_pid(port, pid);
      if (!ladish_client_has_app(vclient))
      {
        ladish_client_set_app(vclient, app_uuid);
      }
    }

    ladish_dict_set(ladish_port_get_dict(port), URI_A2J_PORT, is_a2j ? "yes" : "no");

    ladish_client_set_jack_id(vclient, client_id);
    ladish_graph_adjust_port(vgraph, port, type, flags);
    ladish_graph_show_port(vgraph, port);
    goto free_alsa_names;
  }

  if (!ladish_port_create(NULL, false, &port))
  {
    log_error("ladish_port_create() failed.");
    goto free_alsa_names;
  }

  /* set port jack id so invisible connections to/from it can be restored */
  ladish_port_set_jack_id(port, port_id);

  /* for normal ports, one can find the vgraph and app_uuid through the jack client,
     but for a2j ports the jack client is shared between graphs */
  ladish_port_set_vgraph(port, vgraph);
  if (has_app)
  {
    ladish_port_set_app(port, app_uuid);
    ladish_port_set_pid(port, pid);
  }

  ladish_dict_set(ladish_port_get_dict(port), URI_A2J_PORT, is_a2j ? "yes" : "no");

  if (!ladish_graph_add_port(virtualizer_ptr->jack_graph, jack_client, port, jack_port_name, type, flags, false))
  {
    log_error("ladish_graph_add_port() failed.");
    ladish_port_destroy(port);
    goto free_alsa_names;
  }

  /********************/
  /* find/create the virtual client where port will be added */

  if (is_a2j)
  {
    vclient = ladish_graph_find_client_by_name(vgraph, vclient_name, false);
    if (vclient == NULL)
    {
      if (!ladish_client_create(NULL, &vclient))
      {
        log_error("ladish_client_create() failed.");
        goto free_alsa_names;
      }

      if (has_app)
      {
        ladish_client_set_app(vclient, app_uuid);
      }

      if (!ladish_graph_add_client(vgraph, vclient, vclient_name, false))
      {
        log_error("ladish_graph_add_client() failed.");
        ladish_client_destroy(vclient);
        goto free_alsa_names;
      }
    }
  }
  else if (client_id == virtualizer_ptr->system_client_id)
  {
    log_info("system client port appeared");

    if (!is_input)
    { /* output capture port */

      vclient = ladish_graph_find_client_by_uuid(vgraph, g_system_capture_uuid);
      if (vclient == NULL)
      {
        if (!ladish_client_create(g_system_capture_uuid, &vclient))
        {
          log_error("ladish_client_create() failed.");
          goto free_alsa_names;
        }

        if (!ladish_graph_add_client(vgraph, vclient, "Hardware Capture", false))
        {
          log_error("ladish_graph_add_client() failed.");
          ladish_client_destroy(vclient);
          goto free_alsa_names;
        }
      }
    }
    else
    { /* input playback port */
      vclient = ladish_graph_find_client_by_uuid(vgraph, g_system_playback_uuid);
      if (vclient == NULL)
      {
        if (!ladish_client_create(g_system_playback_uuid, &vclient))
        {
          log_error("ladish_client_create() failed.");
          goto free_alsa_names;
        }

        if (!ladish_graph_add_client(vgraph, vclient, "Hardware Playback", false))
        {
          ladish_client_destroy(vclient);
          goto free_alsa_names;
        }
      }
    }
  }
  else
  { /* non-system client */
    log_info("non-system client port appeared");

    if (ladish_client_get_interlink(jack_client, vclient_uuid))
    {
      vclient = ladish_graph_find_client_by_uuid(vgraph, vclient_uuid);
      ASSERT(vclient != NULL);
    }
    else
    {
      if (has_app)
      {
        vclient = ladish_graph_find_client_by_app(vgraph, app_uuid);
        if (vclient == NULL)
        {
          log_info("Lookup by app uuid failed, attempting lookup by name '%s'", vclient_name);
          goto find_by_name;
        }
      }
      else
      {
      find_by_name:
        vclient = ladish_graph_find_client_by_name(vgraph, vclient_name, true);
      }

      if (vclient == NULL)
      {
        log_info("creating new vclient");
        if (!ladish_client_create(NULL, &vclient))
        {
          log_error("ladish_client_create() failed.");
          goto free_alsa_names;
        }

        ladish_client_interlink(vclient, jack_client);

        if (has_app)
        {
          ladish_client_set_app(vclient, app_uuid);
        }

        if (!ladish_graph_add_client(vgraph, vclient, vclient_name, false))
        {
          log_error("ladish_graph_add_client() failed to add client '%s' to virtual graph", jack_client_name);
          ladish_client_destroy(vclient);
          goto free_alsa_names;
        }
      }
      else
      {
        /* vclient exists but is not interlinked with vclient */
        /* this can happen when client is created because of a2j port appear */
        ladish_client_interlink(vclient, jack_client);
      }
    }
  }

  /********************/
  /* add newly appeared port to the virtual graph */

  if (is_a2j)
  {
    vport_name = alsa_port_name;
  }
  else
  {
    vport_name = jack_port_name;
  }

  if (!ladish_graph_add_port(vgraph, vclient, port, vport_name, type, flags, false))
  {
    log_error("ladish_graph_add_port() failed.");
    goto free_alsa_names;
  }

free_alsa_names:
  if (a2j_fake_jack_port_name != NULL)
  {
    free(a2j_fake_jack_port_name);
  }

  if (is_a2j)
  {
    free(alsa_client_name);
    free(alsa_port_name);
  }

exit:
  return;
}

static void maybe_clear_a2j_port_pid(ladish_graph_handle vgraph, ladish_client_handle jclient, ladish_port_handle port)
{
  const char * opath;
  uuid_t app_uuid;
  ladish_app_handle app;
  ladish_app_supervisor_handle app_supervisor;
  pid_t pid;

  if (ladish_virtualizer_is_a2j_client(jclient) && ladish_port_get_app(port, app_uuid))
  {
    pid = ladish_port_get_pid(port);
    opath = ladish_graph_get_opath(vgraph);
    log_info("releasing reference for pid %d of a2j port in %s", (int)pid, ladish_graph_get_description(vgraph));
    app_supervisor = ladish_studio_find_app_supervisor(opath);
    app = ladish_app_supervisor_find_app_by_uuid(app_supervisor, app_uuid);
    if (app != NULL && pid != 0)
    {
      ladish_app_del_pid(app, pid);
    }
  }
}

static void port_disappeared(void * context, uint64_t client_id, uint64_t port_id)
{
  ladish_client_handle jclient;
  ladish_client_handle vclient;
  ladish_port_handle port;
  ladish_graph_handle vgraph;
  bool jmcore;

  log_info("port_disappeared(%"PRIu64", %"PRIu64")", client_id, port_id);

  jclient = ladish_graph_find_client_by_jack_id(virtualizer_ptr->jack_graph, client_id);
  if (jclient == NULL)
  {
    log_error("Port of unknown JACK client with id %"PRIu64" disappeared", client_id);
    return;
  }

  port = ladish_graph_find_port_by_jack_id(virtualizer_ptr->jack_graph, port_id, true, true);
  if (port == NULL)
  {
    log_error("Unknown JACK port with id %"PRIu64" disappeared", port_id);
    return;
  }

  /* find the virtual graph that owns the app that owns the client that owns the disappeared port */
  jmcore = false;
  vgraph = ladish_port_get_vgraph(port);
  if (vgraph == NULL)
  {
    vgraph = find_link_port_vgraph_by_uuid(virtualizer_ptr, ladish_graph_get_port_name(virtualizer_ptr->jack_graph, port), NULL);
    if (vgraph == NULL)
    {
      log_error("Cannot find vgraph for disappeared jmcore port");
      ASSERT_NO_PASS;
      return;
    }

    jmcore = true;
    ladish_graph_remove_port_by_jack_id(virtualizer_ptr->jack_graph, port_id, true, true);
  }
  else
  {
    maybe_clear_a2j_port_pid(vgraph, jclient, port);
  }

  ladish_port_set_pid(port, 0);

  if (ladish_graph_is_persist(vgraph)) /* if port is supposed to be persisted */
  {
    if (!jmcore)
    {
      ladish_port_set_jack_id(port, 0);
      ladish_graph_hide_port(virtualizer_ptr->jack_graph, port);
    }
    if (vgraph != NULL)
    {
      ladish_graph_hide_port(vgraph, port);
      vclient = ladish_graph_get_port_client(vgraph, port);
      if (ladish_graph_client_looks_empty(vgraph, vclient))
      {
        ladish_graph_hide_client(vgraph, vclient);
      }
    }
  }
  else
  {
    if (!jmcore)
    {
      ladish_graph_remove_port(virtualizer_ptr->jack_graph, port);
    }

    if (vgraph != NULL)
    {
      vclient = ladish_graph_remove_port(vgraph, port);
      if (vclient != NULL)
      {
        if (ladish_graph_client_is_empty(vgraph, vclient))
        {
          ladish_graph_remove_client(vgraph, vclient);
          ladish_client_clear_interlink(jclient);
        }
      }
    }
  }
}

static void port_renamed(void * context, uint64_t client_id, uint64_t port_id, const char * old_port_name, const char * new_port_name)
{
  ladish_port_handle port;
  ladish_graph_handle vgraph;

  log_info("port_renamed(%"PRIu64", '%s', '%s')", port_id, old_port_name, new_port_name);

  port = ladish_graph_find_port_by_jack_id(virtualizer_ptr->jack_graph, port_id, true, true);
  if (port == NULL)
  {
    log_error("Unknown JACK port with id %"PRIu64" was renamed", port_id);
    return;
  }

  /* find the virtual graph that owns the app that owns the client that owns the renamed port */
  vgraph = ladish_port_get_vgraph(port);

  if (!ladish_graph_rename_port(virtualizer_ptr->jack_graph, port, new_port_name))
  {
    log_error("renaming of port in jack graph failed");
  }

  if (!ladish_graph_rename_port(vgraph, port, new_port_name))
  {
    log_error("renaming of port in virtual graph failed");
  }
}

static bool ports_connect_request(void * context, ladish_graph_handle graph_handle, ladish_port_handle port1, ladish_port_handle port2)
{
  uint64_t port1_id;
  uint64_t port2_id;

  ASSERT(ladish_graph_get_opath(graph_handle)); /* studio or room virtual graph */
  log_info("virtualizer: ports connect request");

  if (graph_handle == g_studio.studio_graph)
  {
    port1_id = ladish_port_get_jack_id(port1);
    port2_id = ladish_port_get_jack_id(port2);
  }
  else
  {
    port1_id = ladish_port_get_jack_id_room(port1);
    port2_id = ladish_port_get_jack_id_room(port2);
  }

  return graph_proxy_connect_ports(virtualizer_ptr->jack_graph_proxy, port1_id, port2_id);
}

static bool ports_disconnect_request(void * context, ladish_graph_handle graph_handle, uint64_t connection_id)
{
  ladish_port_handle port1;
  ladish_port_handle port2;
  uint64_t port1_id;
  uint64_t port2_id;

  ASSERT(ladish_graph_get_opath(graph_handle)); /* studio or room virtual graph */
  log_info("virtualizer: ports disconnect request");

  if (!ladish_graph_get_connection_ports(graph_handle, connection_id, &port1, &port2))
  {
    log_error("cannot find ports that are disconnect-requested");
    ASSERT_NO_PASS;
    return false;
  }

  if (graph_handle == g_studio.studio_graph)
  {
    port1_id = ladish_port_get_jack_id(port1);
    port2_id = ladish_port_get_jack_id(port2);
  }
  else
  {
    port1_id = ladish_port_get_jack_id_room(port1);
    port2_id = ladish_port_get_jack_id_room(port2);
  }

  return graph_proxy_disconnect_ports(virtualizer_ptr->jack_graph_proxy, port1_id, port2_id);
}

static void ports_connected(void * context, uint64_t client1_id, uint64_t port1_id, uint64_t client2_id, uint64_t port2_id)
{
  ladish_port_handle port1;
  ladish_port_handle port2;
  uint64_t connection_id;
  ladish_graph_handle vgraph1;
  ladish_graph_handle vgraph2;

  log_info("ports_connected %"PRIu64":%"PRIu64" %"PRIu64":%"PRIu64"", client1_id, port1_id, client2_id, port2_id);

  if (!lookup_port(virtualizer_ptr, port1_id, &port1, &vgraph1))
  {
    return;
  }

  if (!lookup_port(virtualizer_ptr, port2_id, &port2, &vgraph2))
  {
    return;
  }

  if (vgraph1 != vgraph2)
  {
    /* TODO */
    log_error("ignoring connection with endpoints in different vgraphs");
    return;
  }

  ladish_graph_add_connection(virtualizer_ptr->jack_graph, port1, port2, false);

  if (ladish_graph_find_connection(vgraph1, port1, port2, &connection_id))
  {
    log_info("showing hidden virtual connection");
    ladish_graph_show_connection(vgraph1, connection_id);
  }
  else
  {
    log_info("creating new virtual connection");
    ladish_graph_add_connection(vgraph1, port1, port2, false);
  }
}

static void ports_disconnected(void * context, uint64_t client1_id, uint64_t port1_id, uint64_t client2_id, uint64_t port2_id)
{
  ladish_port_handle port1;
  ladish_port_handle port2;
  uint64_t connection_id;
  ladish_graph_handle vgraph1;
  ladish_graph_handle vgraph2;

  log_info("ports_disconnected %"PRIu64":%"PRIu64" %"PRIu64":%"PRIu64"", client1_id, port1_id, client2_id, port2_id);

  if (!lookup_port(virtualizer_ptr, port1_id, &port1, &vgraph1))
  {
    return;
  }

  if (!lookup_port(virtualizer_ptr, port2_id, &port2, &vgraph2))
  {
    return;
  }

  if (vgraph1 != vgraph2)
  {
    /* TODO */
    log_error("ignoring connection with endpoints in different vgraphs");
    return;
  }

  if (ladish_graph_find_connection(virtualizer_ptr->jack_graph, port1, port2, &connection_id))
  {
    ladish_graph_remove_connection(virtualizer_ptr->jack_graph, connection_id, true);
  }
  else
  {
    log_error("ports %"PRIu64":%"PRIu64" and %"PRIu64":%"PRIu64" are not connected in the JACK graph", client1_id, port1_id, client2_id, port2_id);
  }

  if (ladish_graph_find_connection(vgraph1, port1, port2, &connection_id))
  {
    ladish_graph_remove_connection(vgraph1, connection_id, false);
  }
  else
  {
    log_error("ports %"PRIu64":%"PRIu64" and %"PRIu64":%"PRIu64" are not connected in the virtual graph", client1_id, port1_id, client2_id, port2_id);
  }
}

#undef virtualizer_ptr

bool
ladish_virtualizer_create(
  graph_proxy_handle jack_graph_proxy,
  ladish_graph_handle jack_graph,
  ladish_virtualizer_handle * handle_ptr)
{
  struct virtualizer * virtualizer_ptr;

  virtualizer_ptr = malloc(sizeof(struct virtualizer));
  if (virtualizer_ptr == NULL)
  {
    log_error("malloc() failed for struct virtualizer");
    return false;
  }

  virtualizer_ptr->jack_graph_proxy = jack_graph_proxy;
  virtualizer_ptr->jack_graph = jack_graph;
  virtualizer_ptr->system_client_id = 0;
  virtualizer_ptr->our_clients_count = 0;

  if (!graph_proxy_attach(
        jack_graph_proxy,
        virtualizer_ptr,
        clear,
        client_appeared,
        NULL,                   /* jackdbus does not have client rename functionality (yet) */
        client_disappeared,
        port_appeared,
        port_renamed,
        port_disappeared,
        ports_connected,
        ports_disconnected))
  {
    free(virtualizer_ptr);
    return false;
  }

  *handle_ptr = (ladish_virtualizer_handle)virtualizer_ptr;
  return true;
}

#define virtualizer_ptr ((struct virtualizer *)handle)

void
ladish_virtualizer_set_graph_connection_handlers(
  ladish_virtualizer_handle handle,
  ladish_graph_handle graph)
{
  ladish_graph_set_connection_handlers(graph, virtualizer_ptr, ports_connect_request, ports_disconnect_request);
}

unsigned int
ladish_virtualizer_get_our_clients_count(
  ladish_virtualizer_handle handle)
{
  return virtualizer_ptr->our_clients_count;
}

static bool app_has_a2j_ports(ladish_graph_handle jack_graph, const uuid_t app_uuid)
{
  ladish_client_handle a2jclient;

  a2jclient = ladish_graph_find_client_by_uuid(jack_graph, g_a2j_uuid);
  if (a2jclient == NULL)
  {
    return false;
  }

  return ladish_graph_client_has_visible_app_port(jack_graph, a2jclient, app_uuid);
}

bool
ladish_virtualizer_is_hidden_app(
  ladish_graph_handle jack_graph,
  const uuid_t app_uuid,
  const char * app_name)
{
  ladish_client_handle jclient;
  ladish_graph_handle vgraph;
  uuid_t vclient_uuid;
  ladish_client_handle vclient;

  //ladish_graph_dump(g_studio.jack_graph);

  if (app_has_a2j_ports(jack_graph, app_uuid))
  {
    log_info("app '%s' still has a2j ports", app_name);
    return false;
  }

  jclient = ladish_graph_find_client_by_app(jack_graph, app_uuid);
  if (jclient == NULL)
  {
    log_info("App without JACK client is treated as hidden one");
    return true;
  }

  ASSERT(!ladish_virtualizer_is_a2j_client(jclient)); /* a2j client has no app associated */

  vgraph = ladish_client_get_vgraph(jclient);
  if (vgraph == NULL)
  {
    ASSERT_NO_PASS;
    return true;
  }

  //ladish_graph_dump(vgraph);

  if (!ladish_graph_client_looks_empty(jack_graph, jclient) ||
      !ladish_graph_client_is_hidden(jack_graph, jclient))
  {
    return false;
  }

  if (!ladish_client_get_interlink(jclient, vclient_uuid))
  {
    if (ladish_graph_client_is_empty(jack_graph, jclient))
    {
      log_info("jack client of app '%s' has no interlinked vgraph client and no ports", app_name);
    }
    else
    {
      log_error("jack client of app '%s' has no interlinked vgraph client", app_name);
      ASSERT_NO_PASS;
    }
    return true;
  }

  vclient = ladish_graph_find_client_by_uuid(vgraph, vclient_uuid);
  if (vclient == NULL)
  {
    ASSERT_NO_PASS;
    return true;
  }

  if (!ladish_graph_client_looks_empty(vgraph, vclient))
  {
    return false;
  }

  ASSERT(ladish_graph_client_is_hidden(vgraph, vclient)); /* vclients are automatically hidden when they start looking empty (on port disappear) */
  return true;
}

struct app_remove_context
{
  uuid_t app_uuid;
  const char * app_name;
};

#define app_info_ptr ((struct app_remove_context *)context)

static
bool
remove_app_port(
  void * context,
  ladish_graph_handle graph_handle,
  bool hidden,
  void * client_iteration_context_ptr,
  ladish_client_handle client_handle,
  const char * client_name,
  ladish_port_handle port_handle,
  const char * port_name,
  uint32_t port_type,
  uint32_t port_flags)
{
  ladish_graph_handle vgraph;
  ladish_client_handle vclient;

  if (!ladish_port_belongs_to_app(port_handle, app_info_ptr->app_uuid))
  {
    return true;
  }

  //log_info("removing port '%s':'%s' (JACK) of app '%s'", client_name, port_name, app_info_ptr->app_name);

  vgraph = ladish_port_get_vgraph(port_handle);
  if (vgraph == NULL)
  {
    log_error("port '%s':'%s' of app '5s' has no vgraph", client_name, port_name, app_info_ptr->app_name);
    ASSERT_NO_PASS;
    return true;
  }

  vclient = ladish_graph_get_port_client(vgraph, port_handle);
  if (vgraph == NULL)
  {
    log_error("app port '%s':'%s' not found in vgraph '%s'", client_name, port_name, ladish_graph_get_description(vgraph));
    ASSERT_NO_PASS;
    return true;
  }

  log_info(
    "removing %s %s port %p of app '%s' ('%s':'%s' in %s)",
    port_type == JACKDBUS_PORT_TYPE_AUDIO ? "audio" : "midi",
    JACKDBUS_PORT_IS_INPUT(port_flags) ? "input" : "output",
    port_handle,
    app_info_ptr->app_name,
    ladish_graph_get_client_name(vgraph, vclient),
    ladish_graph_get_port_name(vgraph, port_handle),
    ladish_graph_get_description(vgraph));

  ladish_graph_remove_port(graph_handle, port_handle);
  ladish_graph_remove_port(vgraph, port_handle);

  return true;
}

#undef app_info_ptr

void
ladish_virtualizer_remove_app(
  ladish_graph_handle jack_graph,
  const uuid_t app_uuid,
  const char * app_name)
{
  ladish_client_handle jclient;
  ladish_graph_handle vgraph;
  uuid_t vclient_uuid;
  ladish_client_handle vclient;
  bool is_empty;
  struct app_remove_context ctx;

  //ladish_graph_dump(g_studio.jack_graph);

  uuid_copy(ctx.app_uuid, app_uuid);
  ctx.app_name = app_name;

  ladish_graph_iterate_nodes(jack_graph, &ctx, NULL, remove_app_port, NULL);

  jclient = ladish_graph_find_client_by_app(jack_graph, app_uuid);
  if (jclient == NULL)
  {
    log_info("removing app without JACK client");
    return;
  }

  ASSERT(!ladish_virtualizer_is_a2j_client(jclient)); /* a2j client has no app associated */

  vgraph = ladish_client_get_vgraph(jclient);
  if (vgraph == NULL)
  {
    ASSERT_NO_PASS;
    return;
  }

  //ladish_graph_dump(vgraph);

  /* check whether the client is empty because this cannot
     be checked later because the client was removed
     (see where is_empty is used) */
  is_empty = ladish_graph_client_is_empty(jack_graph, jclient);

  ladish_graph_remove_client(jack_graph, jclient);

  if (!ladish_client_get_interlink(jclient, vclient_uuid))
  {
    if (is_empty)
    {
      /* jack client without ports and thus without vgraph client */
      return;
    }

    log_error("jack client of app '%s' has no interlinked vgraph client", app_name);
    ladish_graph_dump(g_studio.jack_graph);
    ladish_graph_dump(vgraph);
    ASSERT_NO_PASS;
    return;
  }

  vclient = ladish_graph_find_client_by_uuid(vgraph, vclient_uuid);
  if (vclient == NULL)
  {
    ASSERT_NO_PASS;
    return;
  }

  ladish_graph_remove_client(vgraph, vclient);
  ladish_graph_dump(g_studio.jack_graph);
  ladish_graph_dump(vgraph);
}

void
ladish_virtualizer_destroy(
  ladish_virtualizer_handle handle)
{
  log_info("ladish_virtualizer_destroy() called");

  graph_proxy_detach((graph_proxy_handle)handle, virtualizer_ptr);
  free(virtualizer_ptr);
}

#undef virtualizer_ptr

#define vgraph ((ladish_graph_handle)vgraph_context)
void
ladish_virtualizer_rename_app(
  void * vgraph_context,
  const uuid_t uuid,
  const char * old_name,
  const char * new_app_name)
{
  ladish_client_handle client;

  client = ladish_graph_find_client_by_app(vgraph, uuid);
  if (client != NULL)
  {
    ladish_graph_rename_client(vgraph, client, new_app_name);
  }

  client = ladish_graph_find_client_by_app(g_studio.jack_graph, uuid);
  if (client != NULL)
  {
    ladish_graph_rename_client(g_studio.jack_graph, client, new_app_name);
  }
}
#undef vgraph

bool
ladish_virtualizer_is_system_client(
  uuid_t uuid)
{
  if (uuid_compare(uuid, g_system_capture_uuid) == 0)
  {
    return true;
  }

  if (uuid_compare(uuid, g_system_playback_uuid) == 0)
  {
    return true;
  }

  return false;
}

bool ladish_virtualizer_is_a2j_client(ladish_client_handle jclient)
{
  uuid_t jclient_uuid;

  ladish_client_get_uuid(jclient, jclient_uuid);
  return uuid_compare(jclient_uuid, g_a2j_uuid) == 0;
}

static
bool
move_capture_port_callback(
  void * context,
  ladish_graph_handle graph_handle,
  bool hidden,
  ladish_client_handle client_handle,
  const char * client_name,
  ladish_port_handle port_handle,
  const char * port_name,
  uint32_t port_type,
  uint32_t port_flags)
{
  ASSERT(client_handle != context); /* source and destination clients must be differ */

  if (JACKDBUS_PORT_IS_INPUT(port_flags))
  {
    ladish_graph_move_port(graph_handle, port_handle, context);
  }

  return true;
}

bool ladish_virtualizer_split_client(ladish_graph_handle vgraph, uint64_t client_id)
{
  ladish_client_handle vclient1;
  ladish_client_handle vclient2;
  const char * name;

  vclient1 = ladish_graph_find_client_by_id(vgraph, client_id);
  if (vclient1 == NULL)
  {
    log_error("Cannot find client %"PRIu64" in %s", client_id, ladish_graph_get_description(vgraph));
    return false;
  }

  name = ladish_graph_get_client_name(vgraph, vclient1);

  if (!ladish_client_create(NULL, &vclient2))
  {
    log_error("ladish_client_create() failed.");
    return false;
  }

  ladish_client_interlink_copy(vclient2, vclient1);
  ladish_client_copy_app(vclient2, vclient1);

  if (!ladish_graph_add_client(vgraph, vclient2, name, false))
  {
    log_error("ladish_graph_add_client() failed to add client '%s' to virtual graph", name);
    ladish_client_destroy(vclient2);
    return false;
  }

  return ladish_graph_interate_client_ports(vgraph, vclient1, vclient2, move_capture_port_callback);
}

static
bool
move_port_callback(
  void * context,
  ladish_graph_handle graph_handle,
  bool hidden,
  ladish_client_handle client_handle,
  const char * client_name,
  ladish_port_handle port_handle,
  const char * port_name,
  uint32_t port_type,
  uint32_t port_flags)
{
  ASSERT(client_handle != context); /* source and destination clients must be differ */
  ladish_graph_move_port(graph_handle, port_handle, context);
  return true;
}

bool
ladish_virtualizer_join_clients(
  ladish_graph_handle vgraph,
  uint64_t client1_id,
  uint64_t client2_id)
{
  ladish_client_handle vclient1;
  ladish_client_handle vclient2;

  if (client1_id == client2_id)
  {
    log_error("Cannot join same client");
    return false;
  }

  vclient1 = ladish_graph_find_client_by_id(vgraph, client1_id);
  if (vclient1 == NULL)
  {
    log_error("Cannot find client %"PRIu64" in %s", client1_id, ladish_graph_get_description(vgraph));
    return false;
  }

  vclient2 = ladish_graph_find_client_by_id(vgraph, client2_id);
  if (vclient2 == NULL)
  {
    log_error("Cannot find client %"PRIu64" in %s", client2_id, ladish_graph_get_description(vgraph));
    return false;
  }

  ladish_graph_interate_client_ports(vgraph, vclient2, vclient1, move_port_callback);

  ladish_graph_remove_client(vgraph, vclient2);

  return true;
}