aboutsummaryrefslogtreecommitdiff
path: root/app/synthpod_app_worker.c
blob: 8384e52e784e4f4287b709c1cae54477cf2aec3e (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
/*
 * Copyright (c) 2015-2016 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 implied 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 <synthpod_app_private.h>
#include <synthpod_patcher.h>

static inline void *
__sp_worker_to_app_request(sp_app_t *app, size_t minimum, size_t *maximum)
{
	if(app->driver->to_app_request)
		return app->driver->to_app_request(minimum, maximum, app->data);

	sp_app_log_error(app, "%s: buffer request failed\n", __func__);
	return NULL;
}
#define _sp_worker_to_app_request(APP, MINIMUM) \
	ASSUME_ALIGNED(__sp_worker_to_app_request((APP), (MINIMUM), NULL))
#define _sp_worker_to_app_request_max(APP, MINIMUM, MAXIMUM) \
	ASSUME_ALIGNED(__sp_worker_to_app_request((APP), (MINIMUM), (MAXIMUM)))

static inline void
_sp_worker_to_app_advance(sp_app_t *app, size_t size)
{
	if(app->driver->to_app_advance)
		app->driver->to_app_advance(size, app->data);
	else
		sp_app_log_error(app, "%s: buffer advance failed\n", __func__);
}

bool
sp_app_from_worker(sp_app_t *app, uint32_t len, const void *data)
{
	if(!advance_work[app->block_state])
		return false; // we are blocking

	const job_t *job = ASSUME_ALIGNED(data);

	switch(job->reply)
	{
		case JOB_TYPE_REPLY_MODULE_SUPPORTED:
		{
#if 0
			//signal to UI
			size_t size = sizeof(transmit_module_supported_t)
				+ lv2_atom_pad_size(strlen(job->uri) + 1);
			transmit_module_supported_t *trans = _sp_app_to_ui_request(app, size);
			if(trans)
			{
				_sp_transmit_module_supported_fill(&app->regs, &app->forge, trans, size,
					job->status, job->uri);
				_sp_app_to_ui_advance(app, size);
			}
#endif

			break;
		}
		case JOB_TYPE_REPLY_MODULE_ADD:
		{
			mod_t *mod = job->mod;

			if(app->num_mods >= MAX_MODS)
				break; //TODO delete mod

			// inject module into module graph
			app->mods[app->num_mods] = app->mods[app->num_mods-1]; // system sink
			app->mods[app->num_mods-1] = mod;
			app->num_mods += 1;

			_sp_app_order(app);

			//signal to NK
			LV2_Atom *answer = _sp_app_to_ui_request_atom(app);
			if(answer)
			{
				LV2_Atom_Forge_Ref ref = synthpod_patcher_add(&app->regs, &app->forge,
					0, 0, app->regs.synthpod.module_list.urid, //TODO subject
					sizeof(uint32_t), app->forge.URID, &mod->urn);
				if(ref)
				{
					_sp_app_to_ui_advance_atom(app, answer);
				}
				else
				{
					_sp_app_to_ui_overflow(app);
				}
			}
			else
			{
				_sp_app_to_ui_overflow(app);
			}

			break;
		}
		case JOB_TYPE_REPLY_MODULE_DEL:
		{
			const LV2_URID urn = job->urn;

			// signal to NK
			size_t maximum;
			LV2_Atom *answer = _sp_app_to_ui_request_atom(app);
			if(answer)
			{
				LV2_Atom_Forge_Ref ref = synthpod_patcher_remove(&app->regs, &app->forge,
					0, 0, app->regs.synthpod.module_list.urid, //TODO subject
				 	sizeof(uint32_t), app->forge.URID, &urn);
				if(ref)
				{
					_sp_app_to_ui_advance_atom(app, answer);
				}
				else
				{
					_sp_app_to_ui_overflow(app);
				}
			}
			else
			{
				_sp_app_to_ui_overflow(app);
			}

			break;
		}
		case JOB_TYPE_REPLY_MODULE_REINSTANTIATE:
		{
			mod_t *mod = job->mod;

			assert(app->block_state == BLOCKING_STATE_WAIT);
			app->block_state = BLOCKING_STATE_RUN; // release block
			mod->bypassed = false;

			if(app->silence_state == SILENCING_STATE_WAIT)
			{
				app->silence_state = SILENCING_STATE_RUN;

				// ramping
				for(unsigned p1=0; p1<mod->num_ports; p1++)
				{
					port_t *port = &mod->ports[p1];

					// desilence sinks
					for(unsigned m=0; m<app->num_mods; m++)
					{
						for(unsigned p2=0; p2<app->mods[m]->num_ports; p2++)
						{
							_sp_app_port_desilence(app, port, &app->mods[m]->ports[p2]);
						}
					}
				}
			}

			// signal to ui
			LV2_Atom *answer = _sp_app_to_ui_request_atom(app);
			if(answer)
			{
				const int64_t dsp_instance = (intptr_t )mod->inst;
				LV2_Atom_Forge_Ref ref = synthpod_patcher_set(
					&app->regs, &app->forge, mod->urn, 0, app->regs.ui.instance_access.urid, //FIXME seqnum
					sizeof(int64_t), app->forge.Long, &dsp_instance);
				if(ref)
				{
					_sp_app_to_ui_advance_atom(app, answer);
				}
				else
				{
					_sp_app_to_ui_overflow(app);
				}
			}
			else
			{
				_sp_app_to_ui_overflow(app);
			}

			break;
		}
		case JOB_TYPE_REPLY_PRESET_LOAD:
		{
			//printf("app: preset loaded\n");
			mod_t *mod = job->mod;

			assert(app->block_state == BLOCKING_STATE_WAIT);
			app->block_state = BLOCKING_STATE_RUN; // release block
			mod->bypassed = false;

			if(app->silence_state == SILENCING_STATE_WAIT)
			{
				app->silence_state = SILENCING_STATE_RUN;

				// ramping
				for(unsigned p1=0; p1<mod->num_ports; p1++)
				{
					port_t *port = &mod->ports[p1];

					// desilence sinks
					for(unsigned m=0; m<app->num_mods; m++)
					{
						for(unsigned p2=0; p2<app->mods[m]->num_ports; p2++)
						{
							_sp_app_port_desilence(app, port, &app->mods[m]->ports[p2]);
						}
					}
				}
			}

#if 0
			//signal to UI
			size_t size = sizeof(transmit_module_preset_load_t);
			transmit_module_preset_load_t *trans = _sp_app_to_ui_request(app, size);
			if(trans)
			{
				_sp_transmit_module_preset_load_fill(&app->regs, &app->forge, trans, size,
					mod->uid, NULL);
				_sp_app_to_ui_advance(app, size);
			}
#endif

			break;
		}
		case JOB_TYPE_REPLY_PRESET_SAVE:
		{
			//printf("app: preset saved\n");

			assert(app->block_state == BLOCKING_STATE_WAIT);
			app->block_state = BLOCKING_STATE_RUN; // release block

			// signal to NK
			size_t maximum;
			LV2_Atom *answer = _sp_app_to_ui_request_atom(app);
			if(answer)
			{
				LV2_Atom_Forge_Ref ref = synthpod_patcher_copy(&app->regs, &app->forge,
					job->mod->urn, 0, job->urn);
				if(ref)
				{
					_sp_app_to_ui_advance_atom(app, answer);
				}
				else
				{
					_sp_app_to_ui_overflow(app);
				}
			}
			else
			{
				_sp_app_to_ui_overflow(app);
			}

			break;
		}
		case JOB_TYPE_REPLY_BUNDLE_LOAD:
		{
			//printf("app: bundle loaded\n");

			assert(app->block_state == BLOCKING_STATE_WAIT);
			app->block_state = BLOCKING_STATE_RUN; // releae block
			assert(app->load_bundle == true);
			app->load_bundle = false; // for sp_app_bypassed

			// signal to worker
			job_t *job1 = _sp_app_to_worker_request(app, sizeof(job_t));
			if(job1)
			{
				job1->reply = JOB_TYPE_REQUEST_BUNDLE_LOAD_STATUS;
				job1->status = job->status;
				_sp_app_to_worker_advance(app, sizeof(job_t));
			}
			else
			{
				sp_app_log_trace(app, "%s: buffer request failed\n", __func__);
			}

#if 0
			// signal to UI
			size_t size = sizeof(transmit_bundle_load_t)
				+ lv2_atom_pad_size(strlen(job->uri) + 1);
			transmit_bundle_load_t *trans = _sp_app_to_ui_request(app, size);
			if(trans)
			{
				_sp_transmit_bundle_load_fill(&app->regs, &app->forge, trans, size,
					job->status, job->uri);
				_sp_app_to_ui_advance(app, size);
			}
#endif

			break;
		}
		case JOB_TYPE_REPLY_BUNDLE_SAVE:
		{
			//printf("app: bundle saved\n");

			assert(app->block_state == BLOCKING_STATE_WAIT);
			app->block_state = BLOCKING_STATE_RUN; // release block
			assert(app->load_bundle == false);

			// signal to worker
			job_t *job1 = _sp_app_to_worker_request(app, sizeof(job_t));
			if(job1)
			{
				job1->reply = JOB_TYPE_REQUEST_BUNDLE_SAVE_STATUS;
				job1->status = job->status;
				_sp_app_to_worker_advance(app, sizeof(job_t));
			}
			else
			{
				sp_app_log_trace(app, "%s: buffer request failed\n", __func__);
			}

#if 0
			// signal to UI
			size_t size = sizeof(transmit_bundle_save_t)
				+ lv2_atom_pad_size(strlen(job->uri) + 1);
			transmit_bundle_save_t *trans = _sp_app_to_ui_request(app, size);
			if(trans)
			{
				_sp_transmit_bundle_save_fill(&app->regs, &app->forge, trans, size,
					job->status, job->uri);
				_sp_app_to_ui_advance(app, size);
			}
#endif

			break;
		}
		case JOB_TYPE_REPLY_DRAIN:
		{
			assert(app->block_state == BLOCKING_STATE_DRAIN);
			app->block_state = BLOCKING_STATE_BLOCK;

			break;
		}
	}

	return advance_work[app->block_state];
}

void
sp_worker_from_app(sp_app_t *app, uint32_t len, const void *data)
{
	const job_t *job = ASSUME_ALIGNED(data);

	switch(job->request)
	{
		case JOB_TYPE_REQUEST_MODULE_SUPPORTED:
		{
#if 0
			const int32_t status= _sp_app_mod_is_supported(app, job->uri) ? 1 : 0;

			// signal to app
			size_t job_size = sizeof(job_t) + strlen(job->uri) + 1;
			job_t *job1 = _sp_worker_to_app_request(app, job_size);
			if(job1)
			{
				job1->reply = JOB_TYPE_REPLY_MODULE_SUPPORTED;
				job1->status = status;
				memcpy(job1->uri, job->uri, strlen(job->uri) + 1);
				_sp_worker_to_app_advance(app, job_size);
			}
#endif
			break;
		}
		case JOB_TYPE_REQUEST_MODULE_ADD:
		{
			const char *uri = app->driver->unmap->unmap(app->driver->unmap->handle, job->urn);
			mod_t *mod = uri ? _sp_app_mod_add(app, uri, 0, 0, NULL) : NULL;
			if(!mod)
				break; //TODO report

			// signal to app
			job_t *job1 = _sp_worker_to_app_request(app, sizeof(job_t));
			if(job1)
			{
				job1->reply = JOB_TYPE_REPLY_MODULE_ADD;
				job1->mod = mod;
				_sp_worker_to_app_advance(app, sizeof(job_t));
			}
			else
			{
				sp_app_log_error(app, "%s: buffer request failed\n", __func__);
			}

			break;
		}
		case JOB_TYPE_REQUEST_MODULE_DEL:
		{
			const LV2_URID urn = job->mod->urn;
			int status = _sp_app_mod_del(app, job->mod);

			// signal to app
			job_t *job1 = _sp_worker_to_app_request(app, sizeof(job_t));
			if(job1)
			{
				job1->reply = JOB_TYPE_REPLY_MODULE_DEL;
				job1->urn = urn;
				_sp_worker_to_app_advance(app, sizeof(job_t));
			}
			else
			{
				sp_app_log_error(app, "%s: buffer request failed\n", __func__);
			}

			break;
		}
		case JOB_TYPE_REQUEST_MODULE_REINSTANTIATE:
		{
			mod_t *mod = job->mod;
			if(!mod)
				break; //TODO report

			_sp_app_mod_reinstantiate(app, mod);

			// signal to app
			job_t *job1 = _sp_worker_to_app_request(app, sizeof(job_t));
			if(job1)
			{
				job1->reply = JOB_TYPE_REPLY_MODULE_REINSTANTIATE;
				job1->mod = job->mod;
				_sp_worker_to_app_advance(app, sizeof(job_t));
			}
			else
			{
				sp_app_log_error(app, "%s: buffer request failed\n", __func__);
			}

			break;
		}
		case JOB_TYPE_REQUEST_MODULE_SYSTEM_PORTS_UPDATE:
		{
			mod_t *mod = job->mod;

			if(mod->system_ports && strlen(mod->alias) && app->driver->system_port_set)
			{
				for(unsigned i=0; i<mod->num_ports - 4; i++) // - automation/debug ports
				{
					port_t *tar = &mod->ports[i];

					app->driver->system_port_set(app->data, tar->sys.data,
						SYNTHPOD_PREFIX"#moduleAlias", mod->alias);
				}
			}

			break;
		}
		case JOB_TYPE_REQUEST_PRESET_LOAD:
		{
			const char *uri = app->driver->unmap->unmap(app->driver->unmap->handle, job->urn);
			int status = _sp_app_state_preset_load(app, job->mod, uri, true); (void)status; //FIXME check this

			// signal to app
			job_t *job1 = _sp_worker_to_app_request(app, sizeof(job_t));
			if(job1)
			{
				job1->reply = JOB_TYPE_REPLY_PRESET_LOAD;
				job1->mod = job->mod;
				_sp_worker_to_app_advance(app, sizeof(job_t));
			}
			else
			{
				sp_app_log_error(app, "%s: buffer request failed\n", __func__);
			}

			break;
		}
		case JOB_TYPE_REQUEST_PRESET_SAVE:
		{
			const char *uri = app->driver->unmap->unmap(app->driver->unmap->handle, job->urn);
			int status = _sp_app_state_preset_save(app, job->mod, uri);
			(void)status; //FIXME check this

			// signal to app
			job_t *job1 = _sp_worker_to_app_request(app, sizeof(job_t));
			if(job1)
			{
				job1->reply = JOB_TYPE_REPLY_PRESET_SAVE;
				job1->mod = job->mod;
				job1->urn = job->urn;
				_sp_worker_to_app_advance(app, sizeof(job_t));
			}
			else
			{
				sp_app_log_error(app, "%s: buffer request failed\n", __func__);
			}

			break;
		}
		case JOB_TYPE_REQUEST_BUNDLE_LOAD:
		{
			sp_app_bundle_load(app, job->urn, true);

			break;
		}
		case JOB_TYPE_REQUEST_BUNDLE_SAVE:
		{
			sp_app_bundle_save(app, job->urn, true);

			break;
		}
		case JOB_TYPE_REQUEST_BUNDLE_LOAD_STATUS:
		{
			if(app->driver->opened)
			{
				app->driver->opened(app->data, job->status);
			}

			break;
		}
		case JOB_TYPE_REQUEST_BUNDLE_SAVE_STATUS:
		{
			if(app->driver->saved)
			{
				app->driver->saved(app->data, job->status);
			}

			break;
		}
		case JOB_TYPE_REQUEST_DRAIN:
		{
			// signal to app
			job_t *job1 = _sp_worker_to_app_request(app, sizeof(job_t));
			if(job1)
			{
				job1->reply = JOB_TYPE_REPLY_DRAIN;
				job1->status = 0;
				_sp_worker_to_app_advance(app, sizeof(job_t));
			}
			else
			{
				sp_app_log_error(app, "%s: buffer request failed\n", __func__);
			}

			break;
		}
	}
}

void
sp_app_bundle_load(sp_app_t *app, LV2_URID urn, bool via_app)
{
	if(!via_app) //FIXME not rt-safe
	{
		// manually switch to blocking state and wait for initial bundle to be loaded
		app->block_state = BLOCKING_STATE_WAIT; // wait for job
		app->load_bundle = true; // for sp_app_bypassed
		// TODO keep in sync with synthpod_app_ui
	}

	const char *uri = app->driver->unmap->unmap(app->driver->unmap->handle, urn);
	int status = _sp_app_state_bundle_load(app, uri);
	sp_app_log_note(app, "%s: <%s>\n", __func__, uri);

	// signal to app
	job_t *job1 = _sp_worker_to_app_request(app, sizeof(job_t));
	if(job1)
	{
		job1->reply = JOB_TYPE_REPLY_BUNDLE_LOAD;
		job1->status = status;
		job1->urn = urn;
		_sp_worker_to_app_advance(app, sizeof(job_t));
	}
	else
	{
		sp_app_log_error(app, "%s: buffer request failed\n", __func__);
	}
}

void
sp_app_bundle_save(sp_app_t *app, LV2_URID urn, bool via_app)
{
	if(!via_app) // FIXME not rt-safe
	{
		// manually switch to blocking state and wait for bundle to be saved
		app->block_state = BLOCKING_STATE_WAIT; // wait for job
	}

	const char *uri = app->driver->unmap->unmap(app->driver->unmap->handle, urn);
	const int status = _sp_app_state_bundle_save(app, uri);
	sp_app_log_note(app, "%s: <%s>\n", __func__, uri);

	// signal to app
	job_t *job1 = _sp_worker_to_app_request(app, sizeof(job_t));
	if(job1)
	{
		job1->reply = JOB_TYPE_REPLY_BUNDLE_SAVE;
		job1->status = status;
		job1->urn = urn;
		_sp_worker_to_app_advance(app, sizeof(job_t));
	}
	else
	{
		sp_app_log_error(app, "%s: buffer request failed\n", __func__);
	}
}

void
sp_app_apply(sp_app_t *app, LV2_Atom_Object *obj, char *bundle_path)
{
	//FIXME not rt-safe
	// manually switch to blocking state and wait for bundle to be saved
	app->block_state = BLOCKING_STATE_WAIT; // wait for job

	sp_app_restore(app, sp_app_state_retrieve, obj,
		LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE, 
		sp_app_state_features(app, bundle_path));
	sp_app_log_note(app, "%s: <%s>\n", __func__, bundle_path);

	const int status = 0; //FIXME
	const LV2_URID urn = 0; //FIXME

	// signal to app
	job_t *job1 = _sp_worker_to_app_request(app, sizeof(job_t));
	if(job1)
	{
		job1->reply = JOB_TYPE_REPLY_BUNDLE_SAVE;
		job1->status = status;
		job1->urn = urn;
		_sp_worker_to_app_advance(app, sizeof(job_t));
	}
	else
	{
		sp_app_log_error(app, "%s: buffer request failed\n", __func__);
	}
}