aboutsummaryrefslogtreecommitdiff
path: root/patchmatrix_nk.c
blob: 54c4f25714176eaaf6a7557773aea27316170bda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
/*
 * 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 <limits.h>
#include <dirent.h>
#include <libgen.h>

#include <patchmatrix_jack.h>
#include <patchmatrix_db.h>
#include <patchmatrix_nk.h>

const struct nk_color grid_line_color = {40, 40, 40, 255};
const struct nk_color grid_background_color = {30, 30, 30, 255};
const struct nk_color hilight_color = {200, 100, 0, 255};
const struct nk_color button_border_color = {100, 100, 100, 255};
const struct nk_color wire_color = {100, 100, 100, 255};
const struct nk_color grab_handle_color = {100, 100, 100, 255};
const struct nk_color toggle_color = {150, 150, 150, 255};

static int
_client_moveable(struct nk_context *ctx, app_t *app, client_t *client,
	struct nk_rect *bounds)
{
	struct nk_input *in = &ctx->input;
	
	const bool is_hovering = nk_input_is_mouse_hovering_rect(in, *bounds);

	if(client->moving)
	{
		if(nk_input_is_mouse_released(in, NK_BUTTON_LEFT))
		{
			client->moving = false;

#ifdef JACK_HAS_METADATA_API
			if(client->flags == (JackPortIsInput | JackPortIsOutput) )
			{
				char val [32];

				snprintf(val, 32, "%f", client->pos.x);
				jack_set_property(app->client, client->uuid, PATCHMATRIX__mainPositionX, val, XSD__float);

				snprintf(val, 32, "%f", client->pos.y);
				jack_set_property(app->client, client->uuid, PATCHMATRIX__mainPositionY, val, XSD__float);
			}
			else if(client->flags == JackPortIsInput)
			{
				char val [32];

				snprintf(val, 32, "%f", client->pos.x);
				jack_set_property(app->client, client->uuid, PATCHMATRIX__sinkPositionX, val, XSD__float);

				snprintf(val, 32, "%f", client->pos.y);
				jack_set_property(app->client, client->uuid, PATCHMATRIX__sinkPositionY, val, XSD__float);
			}
			else if(client->flags == JackPortIsOutput)
			{
				char val [32];

				snprintf(val, 32, "%f", client->pos.x);
				jack_set_property(app->client, client->uuid, PATCHMATRIX__sourcePositionX, val, XSD__float);

				snprintf(val, 32, "%f", client->pos.y);
				jack_set_property(app->client, client->uuid, PATCHMATRIX__sourcePositionY, val, XSD__float);
			}
#endif
		}
		else
		{
			client->pos.x += in->mouse.delta.x;
			client->pos.y += in->mouse.delta.y;
			bounds->x += in->mouse.delta.x;
			bounds->y += in->mouse.delta.y;

			// move connections together with client
			HASH_FOREACH(&app->conns, client_conn_itr)
			{
				client_conn_t *client_conn = *client_conn_itr;

				if(client_conn->source_client == client)
				{
					client_conn->pos.x += in->mouse.delta.x/2;
					client_conn->pos.y += in->mouse.delta.y/2;
				}

				if(client_conn->sink_client == client)
				{
					client_conn->pos.x += in->mouse.delta.x/2;
					client_conn->pos.y += in->mouse.delta.y/2;
				}
			}
		}
	}
	else if(is_hovering
		&& nk_input_is_mouse_pressed(in, NK_BUTTON_LEFT)
		&& nk_input_is_key_down(in, NK_KEY_CTRL) )
	{
		client->moving = true;
	}

	if  (is_hovering
		&& nk_input_is_mouse_pressed(in, NK_BUTTON_RIGHT) )
	{
		// consume mouse event
		in->mouse.buttons[NK_BUTTON_RIGHT].down = nk_false;
		in->mouse.buttons[NK_BUTTON_RIGHT].clicked = nk_false;

		return true;
	}

	return false;
}

static void
_client_connectors(struct nk_context *ctx, app_t *app, client_t *client,
	struct nk_vec2 dim, int is_hilighted)
{
	struct node_editor *nodedit = &app->nodedit;
	const struct nk_input *in = &ctx->input;
	struct nk_command_buffer *canvas = nk_window_get_canvas(ctx);
	const struct nk_vec2 scrolling = nodedit->scrolling;

	const float cw = 4.f * app->scale;

	struct nk_rect bounds = nk_rect(
		client->pos.x - dim.x/2 - scrolling.x,
		client->pos.y - dim.y/2 - scrolling.y,
		dim.x, dim.y);

	// output connector
	if(client->source_type & app->type)
	{
		const float cx = client->pos.x - scrolling.x + dim.x/2 + 2*cw;
		const float cy = client->pos.y - scrolling.y;
		const struct nk_rect outer = nk_rect(
			cx - cw, cy - cw,
			4*cw, 4*cw
		);

		// start linking process
		const bool has_click_body = nk_input_has_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, bounds, nk_true);
		const bool has_click_handle = nk_input_has_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, outer, nk_true);
		if(  ((has_click_body && !client->mixer_shm) || has_click_handle)
			&& !nk_input_is_key_down(in, NK_KEY_CTRL))
		{
			nodedit->linking.active = true;
			nodedit->linking.source_client = client;
		}

		const bool is_hovering_handle= nk_input_is_mouse_hovering_rect(in, outer);
		nk_fill_arc(canvas, cx, cy, cw, 0.f, 2*NK_PI,
			is_hilighted ? hilight_color : grab_handle_color);
		if(  (is_hovering_handle && !nodedit->linking.active)
			|| (nodedit->linking.active && (nodedit->linking.source_client == client)) )
		{
			nk_stroke_arc(canvas, cx, cy, 2*cw, 0.f, 2*NK_PI, 1.f, hilight_color);
		}

		// draw line from linked node slot to mouse position
		if(  nodedit->linking.active
			&& (nodedit->linking.source_client == client) )
		{
			struct nk_vec2 m = in->mouse.pos;

			nk_stroke_line(canvas, cx, cy, m.x, m.y, 1.f, hilight_color);
		}
	}

	// input connector
	if(client->sink_type & app->type)
	{
		const float cx = client->mixer_shm
			? client->pos.x - scrolling.x
			: client->pos.x - scrolling.x - dim.x/2 - 2*cw;
		const float cy = client->mixer_shm
			? client->pos.y - scrolling.y - dim.y/2 - 2*cw
			: client->pos.y - scrolling.y;
		const struct nk_rect outer = nk_rect(
			cx - cw, cy - cw,
			4*cw, 4*cw
		);

		const bool is_hovering_body = nk_input_is_mouse_hovering_rect(in, bounds);
		const bool is_hovering_handle = nk_input_is_mouse_hovering_rect(in, outer);
		nk_fill_arc(canvas, cx, cy, cw, 0.f, 2*NK_PI,
			is_hilighted ? hilight_color : grab_handle_color);
		if(  (is_hovering_handle || is_hovering_body)
			&& nodedit->linking.active)
		{
			nk_stroke_arc(canvas, cx, cy, 2*cw, 0.f, 2*NK_PI, 1.f, hilight_color);
		}

		if(  nk_input_is_mouse_released(in, NK_BUTTON_LEFT)
			&& (is_hovering_handle || is_hovering_body)
			&& nodedit->linking.active)
		{
			nodedit->linking.active = false;

			client_t *src = nodedit->linking.source_client;
			if(src)
			{
				client_conn_t *client_conn = _client_conn_find(app, src, client);
				if(!client_conn) // does not yet exist
					client_conn = _client_conn_add(app, src, client);
				if(client_conn)
				{
					client_conn->type |= app->type;

					if(nk_input_is_key_down(in, NK_KEY_CTRL)) // automatic connection
					{
						unsigned i = 0;
						HASH_FOREACH(&src->sources, source_port_itr)
						{
							port_t *source_port = *source_port_itr;
							if(source_port->type != app->type)
								continue;

							unsigned j = 0;
							HASH_FOREACH(&client->sinks, sink_port_itr)
							{
								port_t *sink_port = *sink_port_itr;
								if(sink_port->type != app->type)
									continue;

								bool do_connect = false;
								if(  (!source_port->designation || !sink_port->designation)
									&& (i == j) )
								{
									// try do be smart
									do_connect = true;
								}
								else if( (source_port->designation && sink_port->designation)
									&& (source_port->designation == sink_port->designation) )
								{
									// connect matching designations
									do_connect = true;
								}

								if(do_connect)
									jack_connect(app->client, source_port->name, sink_port->name);

								j++;
							}

							i++;
						}
					}
				}
			}
		}
	}
}

static void
node_editor_mixer(struct nk_context *ctx, app_t *app, client_t *client)
{
	const bool editable = (client->source_type == app->type)
		|| (client->sink_type == app->type);

	struct node_editor *nodedit = &app->nodedit;
	struct nk_input *in = &ctx->input;
	struct nk_command_buffer *canvas = nk_window_get_canvas(ctx);
	const struct nk_vec2 scrolling = nodedit->scrolling;

	mixer_shm_t *shm = client->mixer_shm;
	if(atomic_load_explicit(&shm->closing, memory_order_acquire))
		return;

	const float ps = 32.f * app->scale;
	const unsigned nx = shm->nsinks;
	const unsigned ny = shm->nsources;

	client->dim.x = nx * ps;
	client->dim.y = ny * ps;

	struct nk_rect bounds = nk_rect(
		client->pos.x - client->dim.x/2 - scrolling.x,
		client->pos.y - client->dim.y/2 - scrolling.y,
		client->dim.x, client->dim.y);

	if(_client_moveable(ctx, app, client, &bounds))
	{
		sem_post(&shm->done);
	}

	client->hovered = nk_input_is_mouse_hovering_rect(in, bounds)
		&& !nodedit->linking.active;
	const bool is_hilighted = client->hilighted || client->hovered || client->moving;

	nk_layout_space_push(ctx, nk_layout_space_rect_to_local(ctx, bounds));

	struct nk_rect body;
	const enum nk_widget_layout_states states = nk_widget(&body, ctx);
	if(states != NK_WIDGET_INVALID)
	{
		struct nk_style_button *style = &ctx->style.button;

		struct nk_color fill_col = style->hover.data.color;
		struct nk_color stroke_col = style->border_color;
		struct nk_color hilight_col = is_hilighted ? hilight_color : style->border_color;
		struct nk_color wire_col = wire_color;
		struct nk_color toggle_col = toggle_color;

		if(!editable)
		{
			fill_col.a /= 3;
			stroke_col.a /= 3;
			hilight_col.a /= 3;
			wire_col.a /= 3;
			toggle_col.a /= 3;
		}

		nk_fill_rect(canvas, body, style->rounding, fill_col);

		for(float x = ps; x < body.w; x += ps)
		{
			nk_stroke_line(canvas,
				body.x + x, body.y,
				body.x + x, body.y + body.h,
				style->border, stroke_col);
		}

		for(float y = ps; y < body.h; y += ps)
		{
			nk_stroke_line(canvas,
				body.x, body.y + y,
				body.x + body.w, body.y + y,
				style->border, stroke_col);
		}

		float x = body.x + ps/2;
		for(unsigned i = 0; i < nx; i++)
		{
			float y = body.y + ps/2;
			for(unsigned j = 0; j < ny; j++)
			{
				int32_t mBFS = atomic_load_explicit(&shm->jgains[j][i], memory_order_acquire);

				const struct nk_rect tile = nk_rect(x - ps/2, y - ps/2, ps, ps);

				const struct nk_mouse_button *btn = &in->mouse.buttons[NK_BUTTON_LEFT];;
				const bool left_mouse_down = btn->down;
				const bool left_mouse_click_in_tile = nk_input_has_mouse_click_down_in_rect(in,
					NK_BUTTON_LEFT, tile, nk_true);
				const bool mouse_hovering_over_tile = nk_input_is_mouse_hovering_rect(in, tile);

				int32_t dd = 0;

				if(editable)
				{
					if(left_mouse_down && left_mouse_click_in_tile && !client->moving)
					{
						const float dx = in->mouse.delta.x;
						const float dy = in->mouse.delta.y;
						dd = fabs(dx) > fabs(dy) ? dx : -dy;
					}
					else if(mouse_hovering_over_tile)
					{
						if(in->mouse.scroll_delta.y != 0.f) // has scrolling
						{
							dd = in->mouse.scroll_delta.y;
							in->mouse.scroll_delta.y = 0.f;
						}
					}

					if(dd != 0)
					{
#if 0
						if( (dd > 0) && (mBFS == -3600) ) // disabled
						{
							mBFS = 0; // jump to 0 dBFS
						}
						else
#endif
						{
							const bool has_shift = nk_input_is_key_down(in, NK_KEY_SHIFT);
							const float mul = has_shift ? 10.f : 100.f;
							mBFS = NK_CLAMP(-3600, mBFS + dd*mul, 3600);
						}

						atomic_store_explicit(&shm->jgains[j][i], mBFS, memory_order_release);
					}
				}

				const float dBFS = mBFS / 100.f;

				if(mouse_hovering_over_tile && !client->moving)
				{
					char tmp [32];

					const struct nk_user_font *font = ctx->style.font;

					const float fh = font->height;

					{
						const size_t tmp_len = snprintf(tmp, 32, "[%u-%u]", i+1, j+1); //FIXME use port names
						const float fw = font->width(font->userdata, font->height, tmp, tmp_len);
						const float fy = body.y + body.h + fh/2;
						const struct nk_rect body2 = {
							.x = body.x + (body.w - fw)/2,
							.y = fy,
							.w = fw,
							.h = fh
						};
						nk_draw_text(canvas, body2, tmp, tmp_len, font,
							style->normal.data.color, style->text_normal);
					}

					{
						const size_t tmp_len = snprintf(tmp, 32, "%+2.2f dBFS", dBFS);
						const float fw = font->width(font->userdata, font->height, tmp, tmp_len);
						const float fy = body.y + body.h + fh + fh/2;
						const struct nk_rect body2 = {
							.x = body.x + (body.w - fw)/2,
							.y = fy,
							.w = fw,
							.h = fh
						};
						nk_draw_text(canvas, body2, tmp, tmp_len, font,
							style->normal.data.color, style->text_normal);
					}
				}

				if(mBFS > -3600)
				{
					const float alpha = (dBFS + 36.f) / 72.f;
					const float beta = NK_PI/2;

					nk_stroke_arc(canvas,
						x, y, 10.f * app->scale,
						beta + 0.2f*NK_PI, beta + 1.8f*NK_PI,
						1.f,
						wire_col);
					nk_stroke_arc(canvas,
						x, y, 7.f * app->scale,
						beta + 0.2f*NK_PI, beta + (0.2f + alpha*1.6f)*NK_PI,
						2.f,
						toggle_col);
				}

				y += ps;
			}

			x += ps;
		}

		nk_stroke_rect(canvas, body, style->rounding, style->border, hilight_col);
	}

	_client_connectors(ctx, app, client, nk_vec2(bounds.w, bounds.h), is_hilighted);
	app->animating = true;
}

static void
node_editor_monitor(struct nk_context *ctx, app_t *app, client_t *client)
{
	if(  !(client->source_type & app->type)
		&& !(client->sink_type & app->type) )
	{
		return;
	}

	struct node_editor *nodedit = &app->nodedit;
	struct nk_input *in = &ctx->input;
	struct nk_command_buffer *canvas = nk_window_get_canvas(ctx);
	const struct nk_vec2 scrolling = nodedit->scrolling;

	monitor_shm_t *shm = client->monitor_shm;
	if(atomic_load_explicit(&shm->closing, memory_order_acquire))
		return;

	const float ps = 24.f * app->scale;
	const unsigned ny = shm->nsinks;

	client->dim.x = 6 * ps;
	client->dim.y = ny * ps;

	struct nk_rect bounds = nk_rect(
		client->pos.x - client->dim.x/2 - scrolling.x,
		client->pos.y - client->dim.y/2 - scrolling.y,
		client->dim.x, client->dim.y);

	if(_client_moveable(ctx, app, client, &bounds))
	{
		sem_post(&shm->done);
	}

	client->hovered = nk_input_is_mouse_hovering_rect(in, bounds)
		&& !nodedit->linking.active;
	const bool is_hilighted = client->hilighted || client->hovered || client->moving;

	nk_layout_space_push(ctx, nk_layout_space_rect_to_local(ctx, bounds));

	struct nk_rect body;
	const enum nk_widget_layout_states states = nk_widget(&body, ctx);
	if(states != NK_WIDGET_INVALID)
	{
		struct nk_style_button *style = &ctx->style.button;

		nk_fill_rect(canvas, body, style->rounding, style->hover.data.color);

		if(client->sink_type == TYPE_AUDIO)
		{
			for(unsigned j = 0; j < ny; j++)
			{
				const int32_t mBFS = atomic_load_explicit(&shm->jgains[j], memory_order_relaxed);
				const float dBFS = mBFS / 100.f;

				struct nk_rect orig = nk_rect(body.x, body.y + j*ps, body.w, ps);
				struct nk_rect tile = orig;
				struct nk_rect outline;
				const float mx1 = 58.f / 70.f;
				const float mx2 = 12.f / 70.f;
				const uint8_t alph = 0x7f;
				const float e = (dBFS + 64.f) / 70.f;
				const float peak = NK_CLAMP(0.f, e, 1.f);

				{
					const float dbfs = NK_MIN(peak, mx1);
					const uint8_t dcol = 0xff * dbfs / mx1;
					const struct nk_color left = nk_rgba(0x00, 0xff, 0xff, alph);
					const struct nk_color bottom = left;
					const struct nk_color right = nk_rgba(dcol, 0xff, 0xff-dcol, alph);
					const struct nk_color top = right;

					const float ox = ctx->style.font->height/2 + ctx->style.property.border + ctx->style.property.padding.x;
					const float oy = ctx->style.property.border + ctx->style.property.padding.y;
					tile.x += ox;
					tile.y += oy;
					tile.w -= 2*ox;
					tile.h -= 2*oy;
					outline = tile;
					tile.w *= dbfs;

					nk_fill_rect_multi_color(canvas, tile, left, top, right, bottom);
				}

				// > 6dBFS
				if(peak > mx1)
				{
					const float dbfs = peak- mx1;
					const uint8_t dcol = 0xff * dbfs / mx2;
					const struct nk_color left = nk_rgba(0xff, 0xff, 0x00, alph);
					const struct nk_color bottom = left;
					const struct nk_color right = nk_rgba(0xff, 0xff - dcol, 0x00, alph);
					const struct nk_color top = right;

					tile= outline;
					tile.x += tile.w * mx1;
					tile.w *= dbfs;
					nk_fill_rect_multi_color(canvas, tile, left, top, right, bottom);
				}

				// draw 6dBFS lines from -60 to +6
				for(unsigned i = 4; i <= 70; i += 6)
				{
					const bool is_zero = (i == 64);
					const float dx = outline.w * i / 70.f;

					const float x0 = outline.x + dx;
					const float y0 = is_zero ? orig.y + 2.f : outline.y;

					const float border = (is_zero ? 2.f : 1.f) * ctx->style.window.group_border;

					const float x1 = x0;
					const float y1 = is_zero ? orig.y + orig.h - 2.f : outline.y + outline.h;

					nk_stroke_line(canvas, x0, y0, x1, y1, border, ctx->style.window.group_border_color);
				}

				nk_stroke_rect(canvas, outline, 0.f, ctx->style.window.group_border, ctx->style.window.group_border_color);
			}
		}
		else if(client->sink_type == TYPE_MIDI)
		{
			for(unsigned j = 0; j < ny; j++)
			{
				const int32_t cvel = atomic_load_explicit(&shm->jgains[j], memory_order_relaxed);
				const float vel = cvel / 100.f;

				struct nk_rect orig = nk_rect(body.x, body.y + j*ps, body.w, ps);
				struct nk_rect tile = orig;
				struct nk_rect outline;
				const float mx1 = 1.f;
				const uint8_t alph = 0x7f;
				const float e = vel / 127.f;
				const float peak = NK_CLAMP(0.f, e, 1.f);

				{
					const float dbfs = NK_MIN(peak, mx1);
					const uint8_t dcol = 0xff * dbfs / mx1;
					const struct nk_color left = nk_rgba(0x00, 0xff, 0xff, alph);
					const struct nk_color bottom = left;
					const struct nk_color right = nk_rgba(dcol, 0xff, 0xff-dcol, alph);
					const struct nk_color top = right;

					const float ox = ctx->style.font->height/2 + ctx->style.property.border + ctx->style.property.padding.x;
					const float oy = ctx->style.property.border + ctx->style.property.padding.y;
					tile.x += ox;
					tile.y += oy;
					tile.w -= 2*ox;
					tile.h -= 2*oy;
					outline = tile;
					tile.w *= dbfs;

					nk_fill_rect_multi_color(canvas, tile, left, top, right, bottom);
				}

				// draw lines
				for(unsigned i = 0; i <= 127; i += 16)
				{
					const float dx = outline.w * i / 127.f;

					const float x0 = outline.x + dx;
					const float y0 = outline.y;

					const float border = 1.f * ctx->style.window.group_border;

					const float x1 = x0;
					const float y1 = outline.y + outline.h;

					nk_stroke_line(canvas, x0, y0, x1, y1, border, ctx->style.window.group_border_color);
				}

				nk_stroke_rect(canvas, outline, 0.f, ctx->style.window.group_border, ctx->style.window.group_border_color);
			}
		}

		nk_stroke_rect(canvas, body, style->rounding, style->border,
			is_hilighted ? hilight_color : style->border_color);
	}

	_client_connectors(ctx, app, client, nk_vec2(bounds.w, bounds.h), is_hilighted);
	app->animating = true;
}

static unsigned
_client_num_sources(client_t *client, port_type_t type)
{
	if(client->source_type & type)
	{
		unsigned num = 0;

		HASH_FOREACH(&client->sources, port_itr)
		{
			port_t *port = *port_itr;

			if(port->type & type)
				num += 1;
		}

		return num;
	}

	return 0;
}

static unsigned
_client_num_sinks(client_t *client, port_type_t type)
{
	if(client->sink_type & type)
	{
		unsigned num = 0;

		HASH_FOREACH(&client->sinks, port_itr)
		{
			port_t *port = *port_itr;

			if(port->type & type)
				num += 1;
		}

		return num;
	}

	return 0;
}

static void
node_editor_client(struct nk_context *ctx, app_t *app, client_t *client)
{
	const bool editable = (client->source_type & app->type)
		|| (client->sink_type & app->type);

	struct node_editor *nodedit = &app->nodedit;
	const struct nk_input *in = &ctx->input;
	struct nk_command_buffer *canvas = nk_window_get_canvas(ctx);
	const struct nk_vec2 scrolling = nodedit->scrolling;

	client->dim.x = 200.f * app->scale;
	client->dim.y = app->dy;

	struct nk_rect bounds = nk_rect(
		client->pos.x - client->dim.x/2 - scrolling.x,
		client->pos.y - client->dim.y/2 - scrolling.y,
		client->dim.x, client->dim.y);

	if(_client_moveable(ctx, app, client, &bounds))
	{
		// nothing
	}

	client->hovered = nk_input_is_mouse_hovering_rect(in, bounds)
		&& !nodedit->linking.active;
	const bool is_hilighted = client->hilighted || client->hovered || client->moving;

	nk_layout_space_push(ctx, nk_layout_space_rect_to_local(ctx, bounds));

	struct nk_rect body;
	const enum nk_widget_layout_states states = nk_widget(&body, ctx);
	if(states != NK_WIDGET_INVALID)
	{
		struct nk_style_button *style = &ctx->style.button;
		const struct nk_user_font *font = ctx->style.font;

		struct nk_color fill_col = style->hover.data.color;
		struct nk_color stroke_col = is_hilighted ? hilight_color : style->border_color;

		if(!editable)
		{
			fill_col.a /= 3;
			stroke_col.a /= 3;
		}

		nk_fill_rect(canvas, body, style->rounding, fill_col);
		nk_stroke_rect(canvas, body, style->rounding, style->border, stroke_col);

		const float fh = font->height;
		const float fy = body.y + (body.h - fh)/2;
		{
			const char *client_name = client->pretty_name ? client->pretty_name : client->name;
			const size_t client_name_len = strlen(client_name);
			const float fw = font->width(font->userdata, font->height, client_name, client_name_len);
			const struct nk_rect body2 = {
				.x = body.x + (body.w - fw)/2,
				.y = fy,
				.w = fw,
				.h = fh
			};
			nk_draw_text(canvas, body2, client_name, client_name_len, font,
				style->normal.data.color, style->text_normal);
		}

		const unsigned nsources = _client_num_sources(client, app->type);
		const unsigned nsinks = _client_num_sinks(client, app->type);

		if(nsources)
		{
			char nums [32];
			snprintf(nums, 32, "%u", nsources);

			const size_t nums_len = strlen(nums);
			const float fw = font->width(font->userdata, font->height, nums, nums_len);
			const struct nk_rect body2 = {
				.x = body.x + body.w - fw - 4.f,
				.y = fy,
				.w = fw,
				.h = fh
			};
			nk_draw_text(canvas, body2, nums, nums_len, font,
				style->normal.data.color, style->text_normal);
		}

		if(nsinks)
		{
			char nums [32];
			snprintf(nums, 32, "%u", nsinks);

			const size_t nums_len = strlen(nums);
			const float fw = font->width(font->userdata, font->height, nums, nums_len);
			const struct nk_rect body2 = {
				.x = body.x + 4.f,
				.y = fy,
				.w = fw,
				.h = fh
			};
			nk_draw_text(canvas, body2, nums, nums_len, font,
				style->normal.data.color, style->text_normal);
		}
	}

	_client_connectors(ctx, app, client, nk_vec2(bounds.w, bounds.h), is_hilighted);
}

static void
node_editor_client_conn(struct nk_context *ctx, app_t *app,
	client_conn_t *client_conn, port_type_t port_type)
{
	if(  (client_conn->type != app->type)
		&& !(client_conn->type & port_type) )
	{
		return;
	}

	struct node_editor *nodedit = &app->nodedit;
	struct nk_input *in = &ctx->input;
	struct nk_command_buffer *canvas = nk_window_get_canvas(ctx);
	const struct nk_vec2 scrolling = nodedit->scrolling;

	client_t *src = client_conn->source_client;
	client_t *snk = client_conn->sink_client;

	if(!src || !snk)
		return;

	const unsigned nx = _client_num_sources(client_conn->source_client, port_type);
	const unsigned ny = _client_num_sinks(client_conn->sink_client, port_type);

	if( (nx == 0) || (ny == 0) )
	{
		return;
	}

	const float ps = 16.f * app->scale;
	const float pw = nx * ps;
	const float ph = ny * ps;
	struct nk_rect bounds = nk_rect(
		client_conn->pos.x - scrolling.x - pw/2,
		client_conn->pos.y - scrolling.y - ph/2,
		pw, ph
	);

	const int is_hovering = nk_input_is_mouse_hovering_rect(in, bounds)
		&& !nodedit->linking.active;

	if(client_conn->moving)
	{
		if(nk_input_is_mouse_released(in, NK_BUTTON_LEFT))
		{
			client_conn->moving = false;
		}
		else
		{
			client_conn->pos.x += in->mouse.delta.x;
			client_conn->pos.y += in->mouse.delta.y;
			bounds.x += in->mouse.delta.x;
			bounds.y += in->mouse.delta.y;
		}
	}
	else if(is_hovering
		&& nk_input_is_mouse_pressed(in, NK_BUTTON_LEFT)
		&& nk_input_is_key_down(in, NK_KEY_CTRL) )
	{
		client_conn->moving = true;
	}
	else if(is_hovering
		&& nk_input_is_mouse_pressed(in, NK_BUTTON_RIGHT) )
	{
		// consume mouse event
		in->mouse.buttons[NK_BUTTON_RIGHT].down = nk_false;
		in->mouse.buttons[NK_BUTTON_RIGHT].clicked = nk_false;

		unsigned count = 0;
		HASH_FOREACH(&client_conn->conns, port_conn_itr)
		{
			port_conn_t *port_conn = *port_conn_itr;

			if( (port_conn->source_port->type & app->type) && (port_conn->sink_port->type & app->type) )
			{
				jack_disconnect(app->client, port_conn->source_port->name, port_conn->sink_port->name);
				count += 1;
			}
		}

		if(count == 0) // is empty matrix, demask for current type
			client_conn->type &= ~(app->type);
	}

	const bool is_hilighted = client_conn->source_client->hovered
		|| client_conn->sink_client->hovered
		|| is_hovering || client_conn->moving;

	if(is_hilighted)
	{
		client_conn->source_client->hilighted = true;
		client_conn->sink_client->hilighted = true;
	}

	const float cs = 4.f * app->scale;

	{
		const float cx = client_conn->pos.x - scrolling.x;
		const float cxr = cx + pw/2;
		const float cy = client_conn->pos.y - scrolling.y;
		const float cyl = cy - ph/2;
		const struct nk_color col = is_hilighted ? hilight_color : grab_handle_color;

		const float l0x = src->pos.x - scrolling.x + src->dim.x/2 + cs*2;
		const float l0y = src->pos.y - scrolling.y;
		const float l1x = snk->mixer_shm
			? snk->pos.x - scrolling.x
			: snk->pos.x - scrolling.x - snk->dim.x/2 - cs*2;
		const float l1y = snk->mixer_shm
			? snk->pos.y - scrolling.y - snk->dim.y/2 - cs*2
			: snk->pos.y - scrolling.y;

		const float bend = 50.f * app->scale;
		nk_stroke_curve(canvas,
			l0x, l0y,
			l0x + bend, l0y,
			cx, cyl - bend,
			cx, cyl,
			1.f, col);
		nk_stroke_curve(canvas,
			cxr, cy,
			cxr + bend, cy,
			snk->mixer_shm ? l1x : l1x - bend, snk->mixer_shm ? l1y - bend : l1y,
			l1x, l1y,
			1.f, col);

		nk_fill_arc(canvas, cx, cyl, cs, 2*M_PI/2, 4*M_PI/2, col);
		nk_fill_arc(canvas, cxr, cy, cs, 3*M_PI/2, 5*M_PI/2, col);
	}

	nk_layout_space_push(ctx, nk_layout_space_rect_to_local(ctx, bounds));

	struct nk_rect body;
	const enum nk_widget_layout_states states = nk_widget(&body, ctx);
	if(states != NK_WIDGET_INVALID)
	{
		struct nk_style_button *style = &ctx->style.button;

		nk_fill_rect(canvas, body, style->rounding, style->normal.data.color);

		for(float x = ps; x < body.w; x += ps)
		{
			nk_stroke_line(canvas,
				body.x + x, body.y,
				body.x + x, body.y + body.h,
				style->border, style->border_color);
		}

		for(float y = ps; y < body.h; y += ps)
		{
			nk_stroke_line(canvas,
				body.x, body.y + y,
				body.x + body.w, body.y + y,
				style->border, style->border_color);
		}

		nk_stroke_rect(canvas, body, style->rounding, style->border,
			is_hilighted ? hilight_color : style->border_color);

		float x = body.x + ps/2;
		HASH_FOREACH(&client_conn->source_client->sources, source_port_itr)
		{
			port_t *source_port = *source_port_itr;

			if(!(source_port->type & port_type))
				continue;

			float y = body.y + ps/2;
			HASH_FOREACH(&client_conn->sink_client->sinks, sink_port_itr)
			{
				port_t *sink_port = *sink_port_itr;

				if(!(sink_port->type & port_type))
					continue;

				port_conn_t *port_conn = _port_conn_find(client_conn, source_port, sink_port);

				if(port_conn)
				{
					const bool is_automation = !strcmp(sink_port->short_name, "automation");

					if(is_automation)
					{
						nk_stroke_arc(canvas, x, y, cs, 0.f, 2*NK_PI, 1.f, toggle_color);
					}
					else // !is_automation
					{
						nk_fill_arc(canvas, x, y, cs, 0.f, 2*NK_PI, toggle_color);
					}
				}

				const struct nk_rect tile = nk_rect(x - ps/2, y - ps/2, ps, ps);

				if(  nk_input_is_mouse_hovering_rect(in, tile)
					&& is_hovering
					&& !client_conn->moving)
				{
					const char *source_name = source_port->pretty_name
						? source_port->pretty_name
						: source_port->short_name;

					const char *sink_name = sink_port->pretty_name
						? sink_port->pretty_name
						: sink_port->short_name;

					char tmp [128];
					const size_t tmp_len = snprintf(tmp, 128,
						"%s || %s", source_name, sink_name);

					const struct nk_user_font *font = ctx->style.font;

					const float fh = font->height;
					const float fy = body.y + body.h + fh/2;
					const float fw = font->width(font->userdata, font->height, tmp, tmp_len);
					const struct nk_rect body2 = {
						.x = body.x + (body.w - fw)/2,
						.y = fy,
						.w = fw,
						.h = fh
					};
					nk_draw_text(canvas, body2, tmp, tmp_len, font,
						style->normal.data.color, style->text_normal);

					float dd = 0.f;
					if(in->mouse.scroll_delta.y != 0.f) // has scrolling
					{
						dd = in->mouse.scroll_delta.y;
						in->mouse.scroll_delta.y = 0.f;
					}

					if(nk_input_is_mouse_pressed(in, NK_BUTTON_LEFT) || (dd != 0.f) )
					{
						if(port_conn)
							jack_disconnect(app->client, source_port->name, sink_port->name);
						else
							jack_connect(app->client, source_port->name, sink_port->name);
					}
				}

				y += ps;
			}

			x += ps;
		}
	}
}

static inline void
_expose(struct nk_context *ctx, struct nk_rect wbounds, void *data)
{
	app_t *app = data;

	app->animating = false;

	app->dy = 20.f * app->scale;

	const struct nk_input *in = &ctx->input;
	struct node_editor *nodedit = &app->nodedit;

	const char *window_name = "base";
	if(nk_begin(ctx, window_name, wbounds, NK_WINDOW_NO_SCROLLBAR))
	{
		struct nk_command_buffer *canvas = nk_window_get_canvas(ctx);

		nk_menubar_begin(ctx);
		{
			struct nk_style_button *style = &ctx->style.button;

#ifdef JACK_HAS_METADATA_API
			nk_layout_row_dynamic(ctx, app->dy, 4);
#else
			nk_layout_row_dynamic(ctx, app->dy, 2);
#endif
			const bool is_audio = app->type == TYPE_AUDIO;
			if(is_audio)
				nk_style_push_color(ctx, &style->border_color, hilight_color);
			if(nk_button_image_label(ctx, app->icons.audio, port_labels[TYPE_AUDIO], NK_TEXT_RIGHT))
				app->type = TYPE_AUDIO;
			if(is_audio)
				nk_style_pop_color(ctx);

			const bool is_midi = app->type == TYPE_MIDI;
			if(is_midi)
				nk_style_push_color(ctx, &style->border_color, hilight_color);
			if(nk_button_image_label(ctx, app->icons.midi, port_labels[TYPE_MIDI], NK_TEXT_RIGHT))
				app->type = TYPE_MIDI;
			if(is_midi)
				nk_style_pop_color(ctx);

#ifdef JACK_HAS_METADATA_API
			const bool is_cv = app->type == TYPE_CV;
			if(is_cv)
				nk_style_push_color(ctx, &style->border_color, hilight_color);
			if(nk_button_image_label(ctx, app->icons.cv, port_labels[TYPE_CV], NK_TEXT_RIGHT))
				app->type = TYPE_CV;
			if(is_cv)
				nk_style_pop_color(ctx);

			const bool is_osc = app->type == TYPE_OSC;
			if(is_osc)
				nk_style_push_color(ctx, &style->border_color, hilight_color);
			if(nk_button_image_label(ctx, app->icons.osc, port_labels[TYPE_OSC], NK_TEXT_RIGHT))
				app->type = TYPE_OSC;
			if(is_osc)
				nk_style_pop_color(ctx);
#endif
		}
		nk_menubar_end(ctx);

		const struct nk_rect total_space = nk_window_get_content_region(ctx);
		const float total_h = total_space.h
			- app->dy
			- 2*ctx->style.window.group_padding.y;

		/* allocate complete window space */
		nk_layout_space_begin(ctx, NK_STATIC, total_h,
			_hash_size(&app->clients) + _hash_size(&app->conns));
		{
			const struct nk_rect old_clip = canvas->clip;
			const struct nk_rect space_bounds= nk_layout_space_bounds(ctx);
			nk_push_scissor(canvas, space_bounds);

			// window content scrolling
			if(  nk_input_is_mouse_hovering_rect(in, space_bounds)
				&& nk_input_is_mouse_down(in, NK_BUTTON_MIDDLE))
			{
				nodedit->scrolling.x -= in->mouse.delta.x;
				nodedit->scrolling.y -= in->mouse.delta.y;
			}

			const struct nk_vec2 scrolling = nodedit->scrolling;

			{
				/* display grid */
				struct nk_rect ssize = nk_layout_space_bounds(ctx);
				ssize.h -= ctx->style.window.group_padding.y;
				const float grid_size = 28.0f * app->scale;

				nk_fill_rect(canvas, ssize, 0.f, grid_background_color);

				for(float x = fmod(ssize.x - scrolling.x, grid_size);
					x < ssize.w;
					x += grid_size)
				{
					nk_stroke_line(canvas, x + ssize.x, ssize.y, x + ssize.x, ssize.y + ssize.h,
						1.0f, grid_line_color);
				}

				for(float y = fmod(ssize.y - scrolling.y, grid_size);
					y < ssize.h;
					y += grid_size)
				{
					nk_stroke_line(canvas, ssize.x, y + ssize.y, ssize.x + ssize.w, y + ssize.y,
						1.0f, grid_line_color);
				}
			}

			HASH_FOREACH(&app->clients, client_itr)
			{
				client_t *client = *client_itr;

				if(client->mixer_shm)
					node_editor_mixer(ctx, app, client);
				else if(client->monitor_shm)
					node_editor_monitor(ctx, app, client);
				else
					node_editor_client(ctx, app, client);

				client->hilighted = false;
			}

			HASH_FOREACH(&app->conns, client_conn_itr)
			{
				client_conn_t *client_conn = *client_conn_itr;

				node_editor_client_conn(ctx, app, client_conn, app->type);
			}

			/* reset linking connection */
			if(  nodedit->linking.active
				&& nk_input_is_mouse_released(in, NK_BUTTON_LEFT))
			{
				nodedit->linking.active = false;
			}

			// contextual menu
			if(
#ifdef JACK_HAS_METADATA_API
				(app->type != TYPE_OSC) && (app->type != TYPE_CV) &&
#endif
				nk_contextual_begin(ctx, 0, nk_vec2(100, 220), nk_window_get_bounds(ctx)))
			{
				nk_layout_row_dynamic(ctx, app->dy, 1);
				if(nk_contextual_item_label(ctx, "Mixer 1x1", NK_TEXT_LEFT))
					_mixer_spawn(app, 1, 1);
				if(nk_contextual_item_label(ctx, "Mixer 2x2", NK_TEXT_LEFT))
					_mixer_spawn(app, 2, 2);
				if(nk_contextual_item_label(ctx, "Mixer 4x4", NK_TEXT_LEFT))
					_mixer_spawn(app, 4, 4);
				if(nk_contextual_item_label(ctx, "Mixer 8x8", NK_TEXT_LEFT))
					_mixer_spawn(app, 8, 8);
				if(nk_contextual_item_label(ctx, "Monitor x1", NK_TEXT_LEFT))
					_monitor_spawn(app, 1);
				if(nk_contextual_item_label(ctx, "Monitor x2", NK_TEXT_LEFT))
					_monitor_spawn(app, 2);
				if(nk_contextual_item_label(ctx, "Monitor x4", NK_TEXT_LEFT))
					_monitor_spawn(app, 4);
				if(nk_contextual_item_label(ctx, "Monitor x8", NK_TEXT_LEFT))
					_monitor_spawn(app, 8);

				nk_contextual_end(ctx);
			}

			nk_push_scissor(canvas, old_clip);
		}
		nk_layout_space_end(ctx);

		{
			nk_layout_row_dynamic(ctx, app->dy, 6);
			const int32_t buffer_size = nk_propertyi(ctx, "BufferSize: ", 1, app->buffer_size, 48000, 1, 0);
			if(buffer_size != app->buffer_size)
			{
				const bool lower = buffer_size < app->buffer_size;

				int32_t bufsz = 1;

				while(bufsz < buffer_size)
					bufsz <<= 1;

				if(lower)
					bufsz >>= 1;

				jack_set_buffer_size (app->client, bufsz);
			}

			nk_labelf(ctx, NK_TEXT_CENTERED, "SampleRate: %"PRIi32, app->sample_rate);

			if(nk_button_label(ctx,
				app->freewheel ? "FreeWheel: true" : "FreeWheel: false"))
			{
				jack_set_freewheel(app->client, !app->freewheel);
			}

			nk_labelf(ctx, NK_TEXT_CENTERED, "RealTime: %s", app->realtime? "true" : "false");

			char tmp [32];
			snprintf(tmp, 32, "XRuns: %"PRIi32, app->xruns);
			if(nk_button_label(ctx, tmp))
			{
				app->xruns = 0;
			}

			nk_label(ctx, "PatchMatrix: "PATCHMATRIX_VERSION, NK_TEXT_RIGHT);
		}
	}
	nk_end(ctx);
}

static struct nk_image
_icon_load(app_t *app, const char *path)
{
	return nk_pugl_icon_load(&app->win, path);
}

static void
_icon_unload(app_t *app, struct nk_image img)
{
	nk_pugl_icon_unload(&app->win, img);
}

static char *
_get_path(const char *data_dir, const char *fn)
{
	static char path [PATH_MAX];

	snprintf(path, sizeof(path), "%s%s", data_dir, fn);

	return path;
}

static const char *
_get_data_dir()
{
	// check whether build-time defined data dir exists
	DIR *dir = opendir(PATCHMATRIX_DATA_DIR);
	if(dir)
	{
		closedir(dir);

		return PATCHMATRIX_DATA_DIR;
	}

	// derive directory of executable
	char bin_path [PATH_MAX];
	if(readlink("/proc/self/exe", bin_path, sizeof(bin_path)) == -1)
	{
		return NULL;
	}
	char *base_path = dirname(bin_path);

	// derive data dir reative to executable
	static char rel_path [PATH_MAX];
	snprintf(rel_path, sizeof(rel_path), "%s/../share/patchmatrix/", base_path);

	return rel_path;
}

int
_ui_init(app_t *app)
{
	app->scale = nk_pugl_get_scale();

	const char *data_dir = _get_data_dir();
	if(!data_dir)
	{
		return 1;
	}

	// UI
	nk_pugl_config_t *cfg = &app->win.cfg;
	cfg->width = 1280 * app->scale;
	cfg->height = 720 * app->scale;
	cfg->resizable = true;
	cfg->ignore = false;
	cfg->class = "PatchMatrix";
	cfg->title = "PatchMatrix";
	cfg->parent = 0;
	cfg->threads = true;
	cfg->data = app;
	cfg->expose = _expose;
	cfg->font.face = _get_path(data_dir, "Cousine-Regular.ttf");
	cfg->font.size = 13 * app->scale;

	app->type = TYPE_AUDIO;
	app->designation = DESIGNATION_NONE;

	XInitThreads(); // for nk_pugl_async_redisplay

	nk_pugl_init(&app->win);
	nk_pugl_show(&app->win);

	// adjust styling
	struct nk_style *style = &app->win.ctx.style;
	style->button.border_color = button_border_color;

	app->icons.audio= _icon_load(app, _get_path(data_dir, "audio.png"));
	app->icons.midi = _icon_load(app, _get_path(data_dir, "midi.png"));
#ifdef JACK_HAS_METADATA_API
	app->icons.cv = _icon_load(app, _get_path(data_dir, "cv.png"));
	app->icons.osc = _icon_load(app, _get_path(data_dir, "osc.png"));
#endif

	return 0;
}

void
_ui_deinit(app_t *app)
{
	_icon_unload(app, app->icons.audio);
	_icon_unload(app, app->icons.midi);
#ifdef JACK_HAS_METADATA_API
	_icon_unload(app, app->icons.cv);
	_icon_unload(app, app->icons.osc);
#endif

	nk_pugl_hide(&app->win);
	nk_pugl_shutdown(&app->win);
}

void
_ui_signal(app_t *app)
{
	if(!atomic_load_explicit(&app->done, memory_order_acquire))
		nk_pugl_async_redisplay(&app->win);
}