controller_iface_patchbay.c 55.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* -*- Mode: C ; c-basic-offset: 4 -*- */
/*
    Copyright (C) 2008 Nedko Arnaudov
    Copyright (C) 2008 Juuso Alasuutari

    This program 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.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

21
22
23
24
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

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
#define _GNU_SOURCE            /* PTHREAD_MUTEX_RECURSIVE */

#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <dbus/dbus.h>
#include <pthread.h>

#include "jackdbus.h"
#include "controller_internal.h"
#include "list.h"

#define JACK_DBUS_IFACE_NAME "org.jackaudio.JackPatchbay"

/* FIXME: these need to be retrieved from common headers */
#define JACK_CLIENT_NAME_SIZE 64
#define JACK_PORT_NAME_SIZE 256

struct jack_graph
{
    uint64_t version;
    struct list_head clients;
    struct list_head ports;
    struct list_head connections;
};

struct jack_graph_client
{
    uint64_t id;
    char * name;
    int pid;
    struct list_head siblings;
    struct list_head ports;
};

struct jack_graph_port
{
    uint64_t id;
    char * name;
    uint32_t flags;
    uint32_t type;
    struct list_head siblings_graph;
    struct list_head siblings_client;
    struct jack_graph_client * client;
};

struct jack_graph_connection
{
    uint64_t id;
    struct jack_graph_port * port1;
    struct jack_graph_port * port2;
    struct list_head siblings;
};

struct jack_controller_patchbay
{
    pthread_mutex_t lock;
    struct jack_graph graph;
    uint64_t next_client_id;
    uint64_t next_port_id;
    uint64_t next_connection_id;
};

void
jack_controller_patchbay_send_signal_graph_changed(
    dbus_uint64_t new_graph_version)
{

    jack_dbus_send_signal(
        JACK_CONTROLLER_OBJECT_PATH,
        JACK_DBUS_IFACE_NAME,
        "GraphChanged",
        DBUS_TYPE_UINT64,
        &new_graph_version,
        DBUS_TYPE_INVALID);
}

void
jack_controller_patchbay_send_signal_client_appeared(
    dbus_uint64_t new_graph_version,
    dbus_uint64_t client_id,
    const char * client_name)
{

    jack_dbus_send_signal(
        JACK_CONTROLLER_OBJECT_PATH,
        JACK_DBUS_IFACE_NAME,
        "ClientAppeared",
        DBUS_TYPE_UINT64,
        &new_graph_version,
        DBUS_TYPE_UINT64,
        &client_id,
        DBUS_TYPE_STRING,
        &client_name,
        DBUS_TYPE_INVALID);
}

void
jack_controller_patchbay_send_signal_client_disappeared(
    dbus_uint64_t new_graph_version,
    dbus_uint64_t client_id,
    const char * client_name)
{

    jack_dbus_send_signal(
        JACK_CONTROLLER_OBJECT_PATH,
        JACK_DBUS_IFACE_NAME,
        "ClientDisappeared",
        DBUS_TYPE_UINT64,
        &new_graph_version,
        DBUS_TYPE_UINT64,
        &client_id,
        DBUS_TYPE_STRING,
        &client_name,
        DBUS_TYPE_INVALID);
}

void
jack_controller_patchbay_send_signal_port_appeared(
    dbus_uint64_t new_graph_version,
    dbus_uint64_t client_id,
    const char * client_name,
    dbus_uint64_t port_id,
    const char * port_name,
    dbus_uint32_t port_flags,
    dbus_uint32_t port_type)
{

    jack_dbus_send_signal(
        JACK_CONTROLLER_OBJECT_PATH,
        JACK_DBUS_IFACE_NAME,
        "PortAppeared",
        DBUS_TYPE_UINT64,
        &new_graph_version,
        DBUS_TYPE_UINT64,
        &client_id,
        DBUS_TYPE_STRING,
        &client_name,
        DBUS_TYPE_UINT64,
        &port_id,
        DBUS_TYPE_STRING,
        &port_name,
        DBUS_TYPE_UINT32,
        &port_flags,
        DBUS_TYPE_UINT32,
        &port_type,
        DBUS_TYPE_INVALID);
}

void
jack_controller_patchbay_send_signal_port_disappeared(
    dbus_uint64_t new_graph_version,
    dbus_uint64_t client_id,
    const char * client_name,
    dbus_uint64_t port_id,
    const char * port_name)
{

    jack_dbus_send_signal(
        JACK_CONTROLLER_OBJECT_PATH,
        JACK_DBUS_IFACE_NAME,
        "PortDisappeared",
        DBUS_TYPE_UINT64,
        &new_graph_version,
        DBUS_TYPE_UINT64,
        &client_id,
        DBUS_TYPE_STRING,
        &client_name,
        DBUS_TYPE_UINT64,
        &port_id,
        DBUS_TYPE_STRING,
        &port_name,
        DBUS_TYPE_INVALID);
}

void
jack_controller_patchbay_send_signal_ports_connected(
    dbus_uint64_t new_graph_version,
    dbus_uint64_t client1_id,
    const char * client1_name,
    dbus_uint64_t port1_id,
    const char * port1_name,
    dbus_uint64_t client2_id,
    const char * client2_name,
    dbus_uint64_t port2_id,
    const char * port2_name,
    dbus_uint64_t connection_id)
{

    jack_dbus_send_signal(
        JACK_CONTROLLER_OBJECT_PATH,
        JACK_DBUS_IFACE_NAME,
        "PortsConnected",
        DBUS_TYPE_UINT64,
        &new_graph_version,
        DBUS_TYPE_UINT64,
        &client1_id,
        DBUS_TYPE_STRING,
        &client1_name,
        DBUS_TYPE_UINT64,
        &port1_id,
        DBUS_TYPE_STRING,
        &port1_name,
        DBUS_TYPE_UINT64,
        &client2_id,
        DBUS_TYPE_STRING,
        &client2_name,
        DBUS_TYPE_UINT64,
        &port2_id,
        DBUS_TYPE_STRING,
        &port2_name,
        DBUS_TYPE_UINT64,
        &connection_id,
        DBUS_TYPE_INVALID);
}

void
jack_controller_patchbay_send_signal_ports_disconnected(
    dbus_uint64_t new_graph_version,
    dbus_uint64_t client1_id,
    const char * client1_name,
    dbus_uint64_t port1_id,
    const char * port1_name,
    dbus_uint64_t client2_id,
    const char * client2_name,
    dbus_uint64_t port2_id,
    const char * port2_name,
    dbus_uint64_t connection_id)
{

    jack_dbus_send_signal(
        JACK_CONTROLLER_OBJECT_PATH,
        JACK_DBUS_IFACE_NAME,
        "PortsDisconnected",
        DBUS_TYPE_UINT64,
        &new_graph_version,
        DBUS_TYPE_UINT64,
        &client1_id,
        DBUS_TYPE_STRING,
        &client1_name,
        DBUS_TYPE_UINT64,
        &port1_id,
        DBUS_TYPE_STRING,
        &port1_name,
        DBUS_TYPE_UINT64,
        &client2_id,
        DBUS_TYPE_STRING,
        &client2_name,
        DBUS_TYPE_UINT64,
        &port2_id,
        DBUS_TYPE_STRING,
        &port2_name,
        DBUS_TYPE_UINT64,
        &connection_id,
        DBUS_TYPE_INVALID);
}

static
struct jack_graph_client *
jack_controller_patchbay_find_client(
    struct jack_controller_patchbay *patchbay_ptr,
    const char *client_name,    /* not '\0' terminated */
    size_t client_name_len)     /* without terminating '\0' */
{
    struct list_head *node_ptr;
    struct jack_graph_client *client_ptr;

    list_for_each(node_ptr, &patchbay_ptr->graph.clients)
    {
        client_ptr = list_entry(node_ptr, struct jack_graph_client, siblings);
        if (strncmp(client_ptr->name, client_name, client_name_len) == 0)
        {
            return client_ptr;
        }
    }

    return NULL;
}

static
struct jack_graph_client *
jack_controller_patchbay_find_client_by_id(
    struct jack_controller_patchbay *patchbay_ptr,
    uint64_t id)
{
    struct list_head *node_ptr;
    struct jack_graph_client *client_ptr;

    list_for_each(node_ptr, &patchbay_ptr->graph.clients)
    {
        client_ptr = list_entry(node_ptr, struct jack_graph_client, siblings);
        if (client_ptr->id == id)
        {
            return client_ptr;
        }
    }

    return NULL;
}

static
struct jack_graph_client *
jack_controller_patchbay_create_client(
    struct jack_controller_patchbay *patchbay_ptr,
    const char *client_name,    /* not '\0' terminated */
    size_t client_name_len)     /* without terminating '\0' */
{
    struct jack_graph_client * client_ptr;

    client_ptr = malloc(sizeof(struct jack_graph_client));
    if (client_ptr == NULL)
    {
        jack_error("Memory allocation of jack_graph_client structure failed.");
        goto fail;
    }

    client_ptr->name = malloc(client_name_len + 1);
    if (client_ptr->name == NULL)
    {
        jack_error("malloc() failed to allocate memory for client name.");
        goto fail_free_client;
    }

    memcpy(client_ptr->name, client_name, client_name_len);
    client_ptr->name[client_name_len] = 0;

    client_ptr->pid = jack_get_client_pid(client_ptr->name);
    jack_info("New client '%s' with PID %d", client_ptr->name, client_ptr->pid);

    client_ptr->id = patchbay_ptr->next_client_id++;
    INIT_LIST_HEAD(&client_ptr->ports);


    pthread_mutex_lock(&patchbay_ptr->lock);
    list_add_tail(&client_ptr->siblings, &patchbay_ptr->graph.clients);
    patchbay_ptr->graph.version++;
    jack_controller_patchbay_send_signal_client_appeared(patchbay_ptr->graph.version, client_ptr->id, client_ptr->name);
    jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
    pthread_mutex_unlock(&patchbay_ptr->lock);

    return client_ptr;

fail_free_client:
    free(client_ptr);

fail:
    return NULL;
}

static
void
jack_controller_patchbay_destroy_client(
    struct jack_controller_patchbay *patchbay_ptr,
    struct jack_graph_client *client_ptr)
{
382
383
    jack_info("Client '%s' with PID %d is out", client_ptr->name, client_ptr->pid);

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
    pthread_mutex_lock(&patchbay_ptr->lock);
    list_del(&client_ptr->siblings);
    patchbay_ptr->graph.version++;
    jack_controller_patchbay_send_signal_client_disappeared(patchbay_ptr->graph.version, client_ptr->id, client_ptr->name);
    jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
    pthread_mutex_unlock(&patchbay_ptr->lock);

    free(client_ptr->name);
    free(client_ptr);
}

static
void
jack_controller_patchbay_destroy_client_by_name(
    struct jack_controller_patchbay *patchbay_ptr,
    const char *client_name)    /* '\0' terminated */
{
    struct jack_graph_client *client_ptr;

    client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, client_name, strlen(client_name));
    if (client_ptr == NULL)
    {
        jack_error("Cannot destroy unknown client '%s'", client_name);
        return;
    }

    jack_controller_patchbay_destroy_client(patchbay_ptr, client_ptr);
}

static
void
jack_controller_patchbay_new_port(
    struct jack_controller_patchbay *patchbay_ptr,
    const char *port_full_name,
    uint32_t port_flags,
    uint32_t port_type)
{
    struct jack_graph_client *client_ptr;
    struct jack_graph_port *port_ptr;
    const char *port_short_name;
    size_t client_name_len;

sletz's avatar
sletz committed
426
    //jack_info("new port: %s", port_full_name);
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

    port_short_name = strchr(port_full_name, ':');
    if (port_short_name == NULL)
    {
        jack_error("port name '%s' does not contain ':' separator char", port_full_name);
        return;
    }

    port_short_name++;          /* skip ':' separator char */

    client_name_len = port_short_name - port_full_name - 1; /* without terminating '\0' */

    client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, port_full_name, client_name_len);
    if (client_ptr == NULL)
    {
        client_ptr = jack_controller_patchbay_create_client(patchbay_ptr, port_full_name, client_name_len);
        if (client_ptr == NULL)
        {
            jack_error("Creation of new jack_graph client failed.");
            return;
        }
    }

    port_ptr = malloc(sizeof(struct jack_graph_port));
    if (port_ptr == NULL)
    {
        jack_error("Memory allocation of jack_graph_port structure failed.");
        return;
    }

    port_ptr->name = strdup(port_short_name);
    if (port_ptr->name == NULL)
    {
        jack_error("strdup() call for port name '%s' failed.", port_short_name);
        free(port_ptr);
        return;
    }

    port_ptr->id = patchbay_ptr->next_port_id++;
    port_ptr->flags = port_flags;
    port_ptr->type = port_type;
    port_ptr->client = client_ptr;

    pthread_mutex_lock(&patchbay_ptr->lock);
    list_add_tail(&port_ptr->siblings_client, &client_ptr->ports);
    list_add_tail(&port_ptr->siblings_graph, &patchbay_ptr->graph.ports);
    patchbay_ptr->graph.version++;
    jack_controller_patchbay_send_signal_port_appeared(
        patchbay_ptr->graph.version,
        client_ptr->id,
        client_ptr->name,
        port_ptr->id,
        port_ptr->name,
        port_ptr->flags,
        port_ptr->type);
    jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
    pthread_mutex_unlock(&patchbay_ptr->lock);
}

static
void
jack_controller_patchbay_remove_port(
    struct jack_controller_patchbay *patchbay_ptr,
    struct jack_graph_port *port_ptr)
{
sletz's avatar
sletz committed
492
493
    //jack_info("remove port: %s", port_ptr->name);

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
    pthread_mutex_lock(&patchbay_ptr->lock);
    list_del(&port_ptr->siblings_client);
    list_del(&port_ptr->siblings_graph);
    patchbay_ptr->graph.version++;
    jack_controller_patchbay_send_signal_port_disappeared(patchbay_ptr->graph.version, port_ptr->client->id, port_ptr->client->name, port_ptr->id, port_ptr->name);
    jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
    pthread_mutex_unlock(&patchbay_ptr->lock);

    free(port_ptr->name);
    free(port_ptr);
}

static
struct jack_graph_port *
jack_controller_patchbay_find_port_by_id(
    struct jack_controller_patchbay *patchbay_ptr,
    uint64_t port_id)
{
    struct list_head *node_ptr;
    struct jack_graph_port *port_ptr;

    list_for_each(node_ptr, &patchbay_ptr->graph.ports)
    {
        port_ptr = list_entry(node_ptr, struct jack_graph_port, siblings_graph);
        if (port_ptr->id == port_id)
        {
            return port_ptr;
        }
    }

    return NULL;
}

static
struct jack_graph_port *
jack_controller_patchbay_find_client_port_by_name(
    struct jack_controller_patchbay *patchbay_ptr,
    struct jack_graph_client *client_ptr,
    const char *port_name)
{
    struct list_head *node_ptr;
    struct jack_graph_port *port_ptr;

    list_for_each(node_ptr, &client_ptr->ports)
    {
        port_ptr = list_entry(node_ptr, struct jack_graph_port, siblings_client);
        if (strcmp(port_ptr->name, port_name) == 0)
        {
            return port_ptr;
        }
    }

    return NULL;
}

static
struct jack_graph_port *
jack_controller_patchbay_find_port_by_full_name(
    struct jack_controller_patchbay *patchbay_ptr,
    const char *port_full_name)
{
    const char *port_short_name;
    size_t client_name_len;
    struct jack_graph_client *client_ptr;

    //jack_info("name: %s", port_full_name);

    port_short_name = strchr(port_full_name, ':');
    if (port_short_name == NULL)
    {
        jack_error("port name '%s' does not contain ':' separator char", port_full_name);
        return NULL;
    }

    port_short_name++;          /* skip ':' separator char */

    client_name_len = port_short_name - port_full_name - 1; /* without terminating '\0' */

    client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, port_full_name, client_name_len);
    if (client_ptr == NULL)
    {
        jack_error("cannot find client of port '%s'", port_full_name);
        return NULL;
    }

    return jack_controller_patchbay_find_client_port_by_name(patchbay_ptr, client_ptr, port_short_name);
}

static
struct jack_graph_port *
jack_controller_patchbay_find_port_by_names(
    struct jack_controller_patchbay *patchbay_ptr,
    const char *client_name,
    const char *port_name)
{
    struct jack_graph_client *client_ptr;

    client_ptr = jack_controller_patchbay_find_client(patchbay_ptr, client_name, strlen(client_name));
    if (client_ptr == NULL)
    {
        jack_error("cannot find client '%s'", client_name);
        return NULL;
    }

    return jack_controller_patchbay_find_client_port_by_name(patchbay_ptr, client_ptr, port_name);
}

static
struct jack_graph_connection *
jack_controller_patchbay_create_connection(
    struct jack_controller_patchbay *patchbay_ptr,
    struct jack_graph_port *port1_ptr,
    struct jack_graph_port *port2_ptr)
{
    struct jack_graph_connection * connection_ptr;

    connection_ptr = malloc(sizeof(struct jack_graph_connection));
    if (connection_ptr == NULL)
    {
        jack_error("Memory allocation of jack_graph_connection structure failed.");
        return NULL;
    }

    connection_ptr->id = patchbay_ptr->next_connection_id++;
    connection_ptr->port1 = port1_ptr;
    connection_ptr->port2 = port2_ptr;

    pthread_mutex_lock(&patchbay_ptr->lock);
    list_add_tail(&connection_ptr->siblings, &patchbay_ptr->graph.connections);
    patchbay_ptr->graph.version++;
    jack_controller_patchbay_send_signal_ports_connected(
        patchbay_ptr->graph.version,
        port1_ptr->client->id,
        port1_ptr->client->name,
        port1_ptr->id,
        port1_ptr->name,
        port2_ptr->client->id,
        port2_ptr->client->name,
        port2_ptr->id,
        port2_ptr->name,
        connection_ptr->id);
    jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
    pthread_mutex_unlock(&patchbay_ptr->lock);

    return connection_ptr;
}

static
void
jack_controller_patchbay_destroy_connection(
    struct jack_controller_patchbay *patchbay_ptr,
    struct jack_graph_connection *connection_ptr)
{
    pthread_mutex_lock(&patchbay_ptr->lock);
    list_del(&connection_ptr->siblings);
    patchbay_ptr->graph.version++;
    jack_controller_patchbay_send_signal_ports_disconnected(
        patchbay_ptr->graph.version,
        connection_ptr->port1->client->id,
        connection_ptr->port1->client->name,
        connection_ptr->port1->id,
        connection_ptr->port1->name,
        connection_ptr->port2->client->id,
        connection_ptr->port2->client->name,
        connection_ptr->port2->id,
        connection_ptr->port2->name,
        connection_ptr->id);
    jack_controller_patchbay_send_signal_graph_changed(patchbay_ptr->graph.version);
    pthread_mutex_unlock(&patchbay_ptr->lock);

    free(connection_ptr);
}

static
struct jack_graph_connection *
jack_controller_patchbay_find_connection(
    struct jack_controller_patchbay *patchbay_ptr,
    struct jack_graph_port *port1_ptr,
    struct jack_graph_port *port2_ptr)
{
    struct list_head *node_ptr;
    struct jack_graph_connection *connection_ptr;

    list_for_each(node_ptr, &patchbay_ptr->graph.connections)
    {
        connection_ptr = list_entry(node_ptr, struct jack_graph_connection, siblings);
        if ((connection_ptr->port1 == port1_ptr &&
             connection_ptr->port2 == port2_ptr) ||
            (connection_ptr->port1 == port2_ptr &&
             connection_ptr->port2 == port1_ptr))
        {
            return connection_ptr;
        }
    }

    return NULL;
}

static
struct jack_graph_connection *
jack_controller_patchbay_find_connection_by_id(
    struct jack_controller_patchbay *patchbay_ptr,
    uint64_t connection_id)
{
    struct list_head *node_ptr;
    struct jack_graph_connection *connection_ptr;

    list_for_each(node_ptr, &patchbay_ptr->graph.connections)
    {
        connection_ptr = list_entry(node_ptr, struct jack_graph_connection, siblings);
        if (connection_ptr->id == connection_id)
        {
            return connection_ptr;
        }
    }

    return NULL;
}

static
bool
jack_controller_patchbay_connect(
    struct jack_dbus_method_call *dbus_call_ptr,
    struct jack_controller *controller_ptr,
    struct jack_graph_port *port1_ptr,
    struct jack_graph_port *port2_ptr)
{
    int ret;
    char port1_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
    char port2_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];

    sprintf(port1_name, "%s:%s", port1_ptr->client->name, port1_ptr->name);
    sprintf(port2_name, "%s:%s", port2_ptr->client->name, port2_ptr->name);

    ret = jack_connect(controller_ptr->client, port1_name, port2_name);
    if (ret != 0)
    {
        jack_dbus_error(dbus_call_ptr, JACK_DBUS_ERROR_GENERIC, "jack_connect() failed with %d", ret);
        return false;
    }

    return true;
}

static
bool
jack_controller_patchbay_disconnect(
    struct jack_dbus_method_call *dbus_call_ptr,
    struct jack_controller *controller_ptr,
    struct jack_graph_port *port1_ptr,
    struct jack_graph_port *port2_ptr)
{
    int ret;
    char port1_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
    char port2_name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];

    sprintf(port1_name, "%s:%s", port1_ptr->client->name, port1_ptr->name);
    sprintf(port2_name, "%s:%s", port2_ptr->client->name, port2_ptr->name);

    ret = jack_disconnect(controller_ptr->client, port1_name, port2_name);
    if (ret != 0)
    {
        jack_dbus_error(dbus_call_ptr, JACK_DBUS_ERROR_GENERIC, "jack_connect() failed with %d", ret);
        return false;
    }

    return true;
}

#define controller_ptr ((struct jack_controller *)call->context)
#define patchbay_ptr ((struct jack_controller_patchbay *)controller_ptr->patchbay_context)

static
void
jack_controller_dbus_get_all_ports(
    struct jack_dbus_method_call * call)
{
    struct list_head * client_node_ptr;
    struct list_head * port_node_ptr;
    struct jack_graph_client * client_ptr;
    struct jack_graph_port * port_ptr;
    DBusMessageIter iter, sub_iter;
    char fullname[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
    char *fullname_var = fullname;

    if (!controller_ptr->started)
    {
        jack_dbus_error(
            call,
            JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
            "Can't execute this method with stopped JACK server");
        return;
    }

    call->reply = dbus_message_new_method_return (call->message);
    if (!call->reply)
    {
        goto fail;
    }

    dbus_message_iter_init_append (call->reply, &iter);

    if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "s", &sub_iter))
    {
        goto fail_unref;
    }

    pthread_mutex_lock(&patchbay_ptr->lock);

    list_for_each(client_node_ptr, &patchbay_ptr->graph.clients)
    {
        client_ptr = list_entry(client_node_ptr, struct jack_graph_client, siblings);

        list_for_each(port_node_ptr, &client_ptr->ports)
        {
            port_ptr = list_entry(port_node_ptr, struct jack_graph_port, siblings_client);

            jack_info("%s:%s", client_ptr->name, port_ptr->name);
            sprintf(fullname, "%s:%s", client_ptr->name, port_ptr->name);
            if (!dbus_message_iter_append_basic (&sub_iter, DBUS_TYPE_STRING, &fullname_var))
            {
                pthread_mutex_unlock(&patchbay_ptr->lock);
                dbus_message_iter_close_container (&iter, &sub_iter);
                goto fail_unref;
            }
        }
    }

    pthread_mutex_unlock(&patchbay_ptr->lock);

    if (!dbus_message_iter_close_container (&iter, &sub_iter))
    {
        goto fail_unref;
    }

    return;

fail_unref:
    dbus_message_unref (call->reply);
    call->reply = NULL;

fail:
    jack_error ("Ran out of memory trying to construct method return");
}

static
void
jack_controller_dbus_get_graph(
    struct jack_dbus_method_call * call)
{
    struct list_head * client_node_ptr;
    struct list_head * port_node_ptr;
    struct list_head * connection_node_ptr;
    struct jack_graph_client * client_ptr;
    struct jack_graph_port * port_ptr;
    struct jack_graph_connection * connection_ptr;
    DBusMessageIter iter;
    DBusMessageIter clients_array_iter;
    DBusMessageIter client_struct_iter;
    DBusMessageIter ports_array_iter;
    DBusMessageIter port_struct_iter;
    dbus_uint64_t version;
    DBusMessageIter connections_array_iter;
    DBusMessageIter connection_struct_iter;

    if (!controller_ptr->started)
    {
        jack_dbus_error(
            call,
            JACK_DBUS_ERROR_SERVER_NOT_RUNNING,
            "Can't execute this method with stopped JACK server");
        return;
    }

    if (!jack_dbus_get_method_args(call, DBUS_TYPE_UINT64, &version, DBUS_TYPE_INVALID))
    {
        /* The method call had invalid arguments meaning that
         * jack_dbus_get_method_args() has constructed an error for us.
         */
        goto exit;
    }

    //jack_info("Getting graph, know version is %" PRIu32, version);

    call->reply = dbus_message_new_method_return(call->message);
    if (!call->reply)
    {
        jack_error("Ran out of memory trying to construct method return");
        goto exit;
    }

    dbus_message_iter_init_append (call->reply, &iter);

    pthread_mutex_lock(&patchbay_ptr->lock);

    if (version > patchbay_ptr->graph.version)
    {
        jack_dbus_error(
            call,
            JACK_DBUS_ERROR_INVALID_ARGS,
            "known graph version %" PRIu64 " is newer than actual version %" PRIu64,
            version,
            patchbay_ptr->graph.version);
        pthread_mutex_unlock(&patchbay_ptr->lock);
        goto exit;
    }

    if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT64, &patchbay_ptr->graph.version))
    {
        goto nomem_unlock;
    }

    if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(tsa(tsuu))", &clients_array_iter))
    {
        goto nomem_unlock;
    }

    if (version < patchbay_ptr->graph.version)
    {
        list_for_each(client_node_ptr, &patchbay_ptr->graph.clients)
        {
            client_ptr = list_entry(client_node_ptr, struct jack_graph_client, siblings);

            if (!dbus_message_iter_open_container (&clients_array_iter, DBUS_TYPE_STRUCT, NULL, &client_struct_iter))
            {
                goto nomem_close_clients_array;
            }

            if (!dbus_message_iter_append_basic(&client_struct_iter, DBUS_TYPE_UINT64, &client_ptr->id))
            {
                goto nomem_close_client_struct;
            }

            if (!dbus_message_iter_append_basic(&client_struct_iter, DBUS_TYPE_STRING, &client_ptr->name))
            {
                goto nomem_close_client_struct;
            }

            if (!dbus_message_iter_open_container(&client_struct_iter, DBUS_TYPE_ARRAY, "(tsuu)", &ports_array_iter))
            {
                goto nomem_close_client_struct;
            }

            list_for_each(port_node_ptr, &client_ptr->ports)
            {
                port_ptr = list_entry(port_node_ptr, struct jack_graph_port, siblings_client);

                if (!dbus_message_iter_open_container(&ports_array_iter, DBUS_TYPE_STRUCT, NULL, &port_struct_iter))
                {
                    goto nomem_close_ports_array;
                }

                if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT64, &port_ptr->id))
                {
                    goto nomem_close_port_struct;
                }

                if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_STRING, &port_ptr->name))
                {
                    goto nomem_close_port_struct;
                }

                if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT32, &port_ptr->flags))
                {
                    goto nomem_close_port_struct;
                }

                if (!dbus_message_iter_append_basic(&port_struct_iter, DBUS_TYPE_UINT32, &port_ptr->type))
                {
                    goto nomem_close_port_struct;
                }

                if (!dbus_message_iter_close_container(&ports_array_iter, &port_struct_iter))
                {
                    goto nomem_close_ports_array;
                }
            }

            if (!dbus_message_iter_close_container(&client_struct_iter, &ports_array_iter))
            {
                goto nomem_close_client_struct;
            }

            if (!dbus_message_iter_close_container(&clients_array_iter, &client_struct_iter))
            {
                goto nomem_close_clients_array;
            }
        }
    }

    if (!dbus_message_iter_close_container(&iter, &clients_array_iter))
    {
        goto nomem_unlock;
    }

    if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(tstststst)", &connections_array_iter))
    {
        goto nomem_unlock;
    }

    if (version < patchbay_ptr->graph.version)
    {
        list_for_each(connection_node_ptr, &patchbay_ptr->graph.connections)
        {
            connection_ptr = list_entry(connection_node_ptr, struct jack_graph_connection, siblings);

            if (!dbus_message_iter_open_container(&connections_array_iter, DBUS_TYPE_STRUCT, NULL, &connection_struct_iter))
For faster browsing, not all history is shown. View entire blame