forked from palob/patchmatrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatchmatrix_db.c
More file actions
850 lines (720 loc) · 17.3 KB
/
patchmatrix_db.c
File metadata and controls
850 lines (720 loc) · 17.3 KB
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
/*
* Copyright (c) 2016-2021 Hanspeter Portner (dev@open-music-kontrollers.ch)
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the Artistic License 2.0 as published by
* The Perl Foundation.
*
* This source is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the iapplied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Artistic License 2.0 for more details.
*
* You should have received a copy of the Artistic License 2.0
* along the source as a COPYING file. If not, obtain it from
* http://www.perlfoundation.org/artistic_license_2_0.
*/
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <patchmatrix_db.h>
#include <patchmatrix_jack.h>
// client
#ifdef JACK_HAS_METADATA_API
static void
_client_get_or_set_pos_x(app_t *app, client_t *client, const char *property)
{
char *value = NULL;
char *type = NULL;
jack_get_property(client->uuid, property, &value, &type);
if(value)
{
client->pos.x = atof(value);
jack_free(value);
}
else // set, if not already set
{
char val [32];
snprintf(val, 32, "%f", client->pos.x);
jack_set_property(app->client, client->uuid, property, val, XSD__float);
}
if(type)
jack_free(type);
}
static void
_client_get_or_set_pos_y(app_t *app, client_t *client, const char *property)
{
char *value = NULL;
char *type = NULL;
jack_get_property(client->uuid, property, &value, &type);
if(value)
{
client->pos.y = atof(value);
jack_free(value);
}
else // set, if not already set
{
char val [32];
snprintf(val, 32, "%f", client->pos.y);
jack_set_property(app->client, client->uuid, property, val, XSD__float);
}
if(type)
jack_free(type);
}
#endif
client_t *
_client_add(app_t *app, const char *client_name, int client_flags)
{
client_t *client = calloc(1, sizeof(client_t));
if(client)
{
client->name = strdup(client_name);
client->pretty_name = NULL;
client->flags = client_flags;
const float w = 200.f * app->scale;
const float h = 25.f * app->scale;
float x;
float *nxt;
if(client->flags == JackPortIsOutput)
{
x = w/2 + 10;
nxt = &app->nxt_source;
}
else if(client->flags == JackPortIsInput)
{
x = app->win.cfg.width - w/2 - 10;
nxt = &app->nxt_sink;
}
else
{
x = app->win.cfg.width/2;
nxt = &app->nxt_default;
}
*nxt = fmodf(*nxt + 2*h, app->win.cfg.height);
client->pos = nk_vec2(x, *nxt);
client->dim = nk_vec2(w, h);
char *client_uuid_str = jack_get_uuid_for_client_name(app->client, client_name);
if(client_uuid_str)
{
jack_uuid_parse(client_uuid_str, &client->uuid);
jack_free(client_uuid_str);
}
#ifdef JACK_HAS_METADATA_API
{
char *value = NULL;
char *type = NULL;
jack_get_property(client->uuid, JACK_METADATA_PRETTY_NAME, &value, &type);
if(value)
{
client->pretty_name = strdup(value);
jack_free(value);
}
if(type)
jack_free(type);
}
if(client->flags == (JackPortIsInput | JackPortIsOutput) )
{
_client_get_or_set_pos_x(app, client, PATCHMATRIX__mainPositionX);
_client_get_or_set_pos_y(app, client, PATCHMATRIX__mainPositionY);
}
else if(client->flags == JackPortIsInput)
{
_client_get_or_set_pos_x(app, client, PATCHMATRIX__sinkPositionX);
_client_get_or_set_pos_y(app, client, PATCHMATRIX__sinkPositionY);
}
else if(client->flags == JackPortIsOutput)
{
_client_get_or_set_pos_x(app, client, PATCHMATRIX__sourcePositionX);
_client_get_or_set_pos_y(app, client, PATCHMATRIX__sourcePositionY);
}
#endif
if(!strncmp(client_name, PATCHMATRIX_MONITOR_ID, strlen(PATCHMATRIX_MONITOR_ID)))
client->monitor_shm = _monitor_add(client_name);
else if(!strncmp(client_name, PATCHMATRIX_MIXER_ID, strlen(PATCHMATRIX_MIXER_ID)))
client->mixer_shm = _mixer_add(client_name);
_hash_add(&app->clients, client);
}
return client;
}
void
_client_free(app_t *app, client_t *client)
{
HASH_FREE(&client->ports, port_ptr)
{
port_t *port = port_ptr;
_port_free(port);
}
_hash_free(&client->sources);
_hash_free(&client->sinks);
if(client->mixer_shm)
_mixer_free(client->mixer_shm);
else if(client->monitor_shm)
_monitor_free(client->monitor_shm);
free(client->name);
free(client->pretty_name);
free(client);
}
bool
_client_remove_cb(void *node, void *data)
{
client_conn_t *client_conn = node;
client_t *client = data;
if( (client_conn->source_client == client)
|| (client_conn->sink_client == client) )
{
_client_conn_free(client_conn);
return false;
}
return true;
}
void
_client_remove(app_t *app, client_t *client)
{
_hash_remove(&app->clients, client);
_hash_remove_cb(&app->conns, _client_remove_cb, client);
}
client_t *
_client_find_by_name(app_t *app, const char *client_name, int client_flags)
{
HASH_FOREACH(&app->clients, client_itr)
{
client_t *client = *client_itr;
if(!strcmp(client->name, client_name) && (client->flags == client_flags))
{
return client;
}
}
return NULL;
}
#ifdef JACK_HAS_METADATA_API
client_t *
_client_find_by_uuid(app_t *app, jack_uuid_t client_uuid, int client_flags)
{
HASH_FOREACH(&app->clients, client_itr)
{
client_t *client = *client_itr;
if(!jack_uuid_compare(client->uuid, client_uuid) && (client->flags == client_flags))
{
return client;
}
}
return NULL;
}
#endif
port_t *
_client_find_port_by_name(client_t *client, const char *port_name)
{
HASH_FOREACH(&client->ports, port_itr)
{
port_t *port = *port_itr;
if(!strcmp(port->name, port_name))
{
return port;
}
}
return NULL;
}
void
_client_refresh_type(client_t *client)
{
client->source_type = TYPE_NONE;
client->sink_type = TYPE_NONE;
HASH_FOREACH(&client->sources, port_itr)
{
port_t *port = *port_itr;
client->source_type |= port->type;
}
HASH_FOREACH(&client->sinks, port_itr)
{
port_t *port = *port_itr;
client->sink_type |= port->type;
}
}
static int
strcasenumcmp(const char *s1, const char *s2)
{
static const char *digits = "1234567890";
const char *d1 = strpbrk(s1, digits);
const char *d2 = strpbrk(s2, digits);
// do both s1 and s2 contain digits?
if(d1 && d2)
{
const size_t l1 = d1 - s1;
const size_t l2 = d2 - s2;
// do both s1 and s2 match up to the first digit?
if( (l1 == l2) && (strncmp(s1, s2, l1) == 0) )
{
char *e1 = NULL;
char *e2 = NULL;
const int n1 = strtol(d1, &e1, 10);
const int n2 = strtol(d2, &e2, 10);
// do both d1 and d2 contain a valid number?
if(e1 && e2)
{
// are the numbers equal? do the same for the substring
if(n1 == n2)
{
return strcasenumcmp(e1, e2);
}
// the numbers differ, e.g. return their ordering
return (n1 < n2) ? -1 : 1;
}
}
}
// no digits in either s1 or s2, do normal comparison
return strcasecmp(s1, s2);
}
static int
_client_port_sort(const void *a, const void *b)
{
const port_t *port_a = *(const port_t **)a;
const port_t *port_b = *(const port_t **)b;
if(port_a->order != port_b->order) // order according to metadata
return port_a->order - port_b->order;
return strcasenumcmp(port_a->name, port_b->name); // order according to name
}
void
_client_sort(client_t *client)
{
_hash_sort(&client->sources, _client_port_sort);
_hash_sort(&client->sinks, _client_port_sort);
}
// client connection
client_conn_t *
_client_conn_add(app_t *app, client_t *source_client, client_t *sink_client)
{
client_conn_t *client_conn = calloc(1, sizeof(client_conn_t));
if(client_conn)
{
client_conn->source_client = source_client;
client_conn->sink_client = sink_client;
client_conn->pos = nk_vec2(
(source_client->pos.x + sink_client->pos.x)/2,
(source_client->pos.y + sink_client->pos.y)/2);
client_conn->type = TYPE_NONE;
_hash_add(&app->conns, client_conn);
}
return client_conn;
}
void
_client_conn_free(client_conn_t *client_conn)
{
HASH_FREE(&client_conn->conns, port_conn_ptr)
{
port_conn_t *port_conn = port_conn_ptr;
_port_conn_free(port_conn);
}
free(client_conn);
}
void
_client_conn_remove(app_t *app, client_conn_t *client_conn)
{
_hash_remove(&app->conns, client_conn);
_client_conn_free(client_conn);
}
client_conn_t *
_client_conn_find(app_t *app, client_t *source_client, client_t *sink_client)
{
HASH_FOREACH(&app->conns, client_conn_itr)
{
client_conn_t *client_conn = *client_conn_itr;
if( (client_conn->source_client == source_client)
&& (client_conn->sink_client == sink_client) )
{
return client_conn;
}
}
return NULL;
}
client_conn_t *
_client_conn_find_or_add(app_t *app, client_t *source_client, client_t *sink_client)
{
client_conn_t *client_conn = _client_conn_find(app, source_client, sink_client);
if(!client_conn)
client_conn = _client_conn_add(app, source_client, sink_client);
return client_conn;
}
void
_client_conn_refresh_type(client_conn_t *client_conn)
{
client_conn->type = TYPE_NONE;
HASH_FOREACH(&client_conn->conns, port_conn_itr)
{
port_conn_t *port_conn = *port_conn_itr;
client_conn->type |= port_conn->source_port->type;
client_conn->type |= port_conn->sink_port->type;
}
}
// port connection
port_conn_t *
_port_conn_add(client_conn_t *client_conn, port_t *source_port, port_t *sink_port)
{
port_conn_t *port_conn = calloc(1, sizeof(port_conn_t));
if(port_conn)
{
port_conn->source_port = source_port;
port_conn->sink_port = sink_port;
_hash_add(&client_conn->conns, port_conn);
client_conn->type |= source_port->type | sink_port->type;
_client_conn_refresh_type(client_conn);
}
return port_conn;
}
void
_port_conn_free(port_conn_t *port_conn)
{
free(port_conn);
}
port_conn_t *
_port_conn_find(client_conn_t *client_conn, port_t *source_port, port_t *sink_port)
{
HASH_FOREACH(&client_conn->conns, port_conn_itr)
{
port_conn_t *port_conn = *port_conn_itr;
if( (port_conn->source_port == source_port)
&& (port_conn->sink_port == sink_port) )
{
return port_conn;
}
}
return NULL;
}
static bool
_port_conn_remove_cb(void *node, void *data)
{
port_conn_t *dst = node;
port_conn_t *ref = data;
if( (dst->source_port == ref->source_port)
&& (dst->sink_port == ref->sink_port) )
{
free(dst);
return false;
}
return true;
}
void
_port_conn_remove(app_t *app, client_conn_t *client_conn, port_t *source_port, port_t *sink_port)
{
port_conn_t port_conn = {
.source_port = source_port,
.sink_port = sink_port
};
_hash_remove_cb(&client_conn->conns, _port_conn_remove_cb, &port_conn);
_client_conn_refresh_type(client_conn);
if(_hash_size(&client_conn->conns) == 0)
_client_conn_remove(app, client_conn);
}
// port
port_t *
_port_add(app_t *app, jack_port_t *jport)
{
const int port_flags = jack_port_flags(jport);
const bool is_physical = port_flags & JackPortIsPhysical;
const bool is_input = port_flags & JackPortIsInput;
const int client_flags = is_physical
? (is_input ? JackPortIsInput : JackPortIsOutput)
: JackPortIsInput | JackPortIsOutput;
const port_type_t port_type = !strcmp(jack_port_type(jport), JACK_DEFAULT_AUDIO_TYPE)
? TYPE_AUDIO
: TYPE_NONE;
const char *port_name = jack_port_name(jport);
char *sep = strchr(port_name, ':');
if(!sep)
return NULL;
const char *port_short_name = sep + 1;
char *client_name = strndup(port_name, sep - port_name);
if(!client_name)
return NULL;
client_t *client = _client_find_by_name(app, client_name, client_flags);
if(!client)
client = _client_add(app, client_name, client_flags);
free(client_name);
if(!client)
return NULL;
port_t *port = calloc(1, sizeof(port_t));
if(port)
{
port->body = jport;
port->client = client;
port->uuid = jack_port_uuid(jport);
port->name = strdup(port_name);
port->short_name = strdup(port_short_name);
port->type = port_type;
port->designation = DESIGNATION_NONE;
#ifdef JACK_HAS_METADATA_API
{
char *value = NULL;
char *type = NULL;
jack_get_property(port->uuid, JACKEY_SIGNAL_TYPE, &value, &type);
if(value)
{
if(!strcasecmp(value, port_labels[TYPE_CV]))
port->type = TYPE_CV;
jack_free(value);
}
if(type)
jack_free(type);
}
{
char *value = NULL;
char *type = NULL;
jack_get_property(port->uuid, JACKEY_EVENT_TYPES, &value, &type);
if(value)
{
if(strcasestr(value, port_labels[TYPE_MIDI]))
port->type |= TYPE_MIDI;
if(strcasestr(value, port_labels[TYPE_OSC]))
port->type |= TYPE_OSC;
jack_free(value);
}
if(type)
jack_free(type);
}
{
char *value = NULL;
char *type = NULL;
jack_get_property(port->uuid, JACKEY_ORDER, &value, &type);
if(value)
{
port->order = atoi(value);
jack_free(value);
}
if(type)
jack_free(type);
}
{
char *value = NULL;
char *type = NULL;
jack_get_property(port->uuid, JACK_METADATA_PORT_GROUP, &value, &type);
if(value)
{
port->designation = _designation_get(value);
jack_free(value);
}
if(type)
jack_free(type);
}
{
char *value = NULL;
char *type = NULL;
jack_get_property(port->uuid, JACK_METADATA_PRETTY_NAME, &value, &type);
if(value)
{
port->pretty_name = strdup(value);
jack_free(value);
}
if(type)
jack_free(type);
}
#endif
if(port->type == TYPE_NONE)
port->type |= TYPE_MIDI; // fallback, if none defined
_hash_add(&client->ports, port);
if(is_input)
_hash_add(&client->sinks, port);
else
_hash_add(&client->sources, port);
_client_sort(client);
_client_refresh_type(client);
}
return port;
}
void
_port_free(port_t *port)
{
free(port->name);
free(port->short_name);
free(port->pretty_name);
free(port);
}
static bool
_port_remove_cb_cb(void *node, void *data)
{
port_conn_t *port_conn = node;
port_t *port = data;
if( (port_conn->source_port == port)
|| (port_conn->sink_port == port) )
{
_port_conn_free(port_conn);
return false;
}
return true;
}
static bool
_port_remove_cb(void *node, void *data)
{
client_conn_t *client_conn = node;
port_t *port = data;
if( (client_conn->source_client == port->client)
|| (client_conn->sink_client == port->client) )
{
_hash_remove_cb(&client_conn->conns, _port_remove_cb_cb, port);
_client_conn_refresh_type(client_conn);
}
// free when empty
if(_hash_size(&client_conn->conns) == 0)
{
_client_conn_free(client_conn);
return false;
}
return true;
}
void
_port_remove(app_t *app, port_t *port)
{
client_t *client = port->client;
_hash_remove(&client->ports, port);
_hash_remove(&client->sinks, port);
_hash_remove(&client->sources, port);
_hash_remove_cb(&app->conns, _port_remove_cb, port);
_client_refresh_type(client);
}
port_t *
_port_find_by_name(app_t *app, const char *port_name)
{
HASH_FOREACH(&app->clients, client_itr)
{
client_t *client = *client_itr;
HASH_FOREACH(&client->ports, port_itr)
{
port_t *port = *port_itr;
if(!strcmp(port->name, port_name))
{
return port;
}
}
}
return NULL;
}
#ifdef JACK_HAS_METADATA_API
port_t *
_port_find_by_uuid(app_t *app, jack_uuid_t port_uuid)
{
HASH_FOREACH(&app->clients, client_itr)
{
client_t *client = *client_itr;
HASH_FOREACH(&client->ports, port_itr)
{
port_t *port = *port_itr;
if(!jack_uuid_compare(port->uuid, port_uuid))
{
return port;
}
}
}
return NULL;
}
#endif
port_t *
_port_find_by_body(app_t *app, jack_port_t *body)
{
HASH_FOREACH(&app->clients, client_itr)
{
client_t *client = *client_itr;
HASH_FOREACH(&client->ports, port_itr)
{
port_t *port = *port_itr;
if(port->body == body)
{
return port;
}
}
}
return NULL;
}
// mixer
void
_mixer_spawn(app_t *app, unsigned nsinks, unsigned nsources)
{
pthread_t pid = vfork();
if(pid == 0) // child
{
char sink_nums[32];
snprintf(sink_nums, 32, "%u", nsources);
char source_nums [32];
snprintf(source_nums, 32, "%u", nsources);
char *const argv [] = {
PATCHMATRIX_MIXER,
"-t",
(char *)_port_type_to_string(app->type),
"-i",
sink_nums,
"-o",
source_nums,
app->server_name ? "-n" : NULL,
(char *)app->server_name,
NULL
};
execvp(argv[0], argv);
_exit(-errno);
}
}
mixer_shm_t *
_mixer_add(const char *client_name)
{
const size_t total_size = sizeof(mixer_shm_t);
const int fd = shm_open(client_name, O_RDWR, S_IRUSR | S_IWUSR);
if(fd == -1)
return NULL;
mixer_shm_t *mixer_shm;
if( (ftruncate(fd, total_size) == -1)
|| ((mixer_shm = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) )
{
close(fd);
return NULL;
}
close(fd);
return mixer_shm;
}
void
_mixer_free(mixer_shm_t *mixer_shm)
{
const size_t total_size = sizeof(mixer_shm_t);
munmap(mixer_shm, total_size);
}
// monitor
void
_monitor_spawn(app_t *app, unsigned nsinks)
{
pthread_t pid = vfork();
if(pid == 0) // child
{
char sink_nums [32];
snprintf(sink_nums, 32, "%u", nsinks);
char *const argv [] = {
PATCHMATRIX_MONITOR,
"-t",
(char *)_port_type_to_string(app->type),
"-i",
sink_nums,
app->server_name ? "-n" : NULL,
(char *)app->server_name,
NULL
};
execvp(argv[0], argv);
_exit(-errno);
}
}
monitor_shm_t *
_monitor_add(const char *client_name)
{
const size_t total_size = sizeof(monitor_shm_t);
const int fd = shm_open(client_name, O_RDWR, S_IRUSR | S_IWUSR);
if(fd == -1)
return NULL;
monitor_shm_t *monitor_shm;
if( (ftruncate(fd, total_size) == -1)
|| ((monitor_shm = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) )
{
close(fd);
return NULL;
}
close(fd);
return monitor_shm;
}
void
_monitor_free(monitor_shm_t *monitor_shm)
{
const size_t total_size = sizeof(monitor_shm_t);
munmap(monitor_shm, total_size);
}