aboutsummaryrefslogtreecommitdiff
path: root/plugins/synthpod_common_d2tk.c
blob: 56d685660535be0f93bdfce49e4ad4029c653060 (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
/*
 * Copyright (c) 2015-2019 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 <fnmatch.h>

#include "lv2/lv2plug.in/ns/ext/log/log.h"
#include "lv2/lv2plug.in/ns/ext/log/logger.h"

#include <lilv/lilv.h>

#include <synthpod_lv2.h>
#include <synthpod_common.h>

#include <d2tk/frontend_pugl.h>

typedef enum _view_type_t {
	VIEW_TYPE_PLUGIN_LIST = 0,
	VIEW_TYPE_PRESET_LIST,
	VIEW_TYPE_PATCH_BAY,

	VIEW_TYPE_MAX
} view_type_t;

typedef struct _entry_t entry_t;
typedef struct _view_t view_t;
typedef struct _plughandle_t plughandle_t;

struct _entry_t {
	const void *data;
	const char *label;
};

struct _view_t {
	view_type_t type;
	bool selector [VIEW_TYPE_MAX];
};

struct _plughandle_t {
	LilvWorld *world;

	const LilvPlugins *plugs;
	unsigned nplugs;
	entry_t *lplugs;
	char pplugs[32];

	LV2_URID_Map *map;
	LV2_Atom_Forge forge;

	LV2_Log_Log *log;
	LV2_Log_Logger logger;

	d2tk_pugl_config_t config;
	d2tk_pugl_t *dpugl;

	LV2UI_Controller *controller;
	LV2UI_Write_Function writer;

	unsigned nviews;
	view_t views [32];
};

static int
_plug_cmp_name(const void *a, const void *b)
{
	const entry_t *entry_a = (const entry_t *)a;
	const entry_t *entry_b = (const entry_t *)b;

	return strcasecmp(entry_a->label, entry_b->label);
}

static int
_plug_populate(plughandle_t *handle, const char *pattern)
{
	pattern = pattern ? pattern : "**";
	handle->nplugs = 0;

	LILV_FOREACH(plugins, i, handle->plugs)
	{
		const LilvPlugin *plug = lilv_plugins_get(handle->plugs, i);
		LilvNode *node = lilv_plugin_get_name(plug);
		const char *label = lilv_node_as_string(node);

		if(fnmatch(pattern, label, FNM_CASEFOLD | FNM_EXTMATCH) == 0)
		{
			entry_t *entry = &handle->lplugs[handle->nplugs++];
			entry->data = plug;
			entry->label = label;
		}
	}

	qsort(handle->lplugs, handle->nplugs, sizeof(entry_t), _plug_cmp_name);

	return 0;
}

static int
_expose_view(plughandle_t *handle, unsigned iview, const d2tk_rect_t *rect)
{
	d2tk_pugl_t *dpugl = handle->dpugl;
	d2tk_base_t *base = d2tk_pugl_get_base(dpugl);
	view_t *view = &handle->views[iview];

	static const d2tk_coord_t vfrac [3] = { 24, 0, 16 };
	D2TK_BASE_LAYOUT(rect, 3, vfrac, D2TK_FLAG_LAYOUT_Y_ABS, vlay)
	{
		const d2tk_rect_t *vrect = d2tk_layout_get_rect(vlay);
		const uint32_t vy = d2tk_layout_get_index(vlay);

		switch(vy)
		{
			case 0:
			{
				d2tk_base_label(base, -1, "View", 1.f, vrect,
					D2TK_ALIGN_MIDDLE | D2TK_ALIGN_LEFT);

				if(d2tk_base_text_field_is_changed(base, D2TK_ID_IDX(iview), vrect,
					sizeof(handle->pplugs), handle->pplugs,
					D2TK_ALIGN_MIDDLE | D2TK_ALIGN_LEFT, NULL))
				{
					_plug_populate(handle, handle->pplugs);
				}
			} break;
			case 1:
			{
				switch(view->type)
				{
					case VIEW_TYPE_PLUGIN_LIST:
					{
						const unsigned dn = 25;

						D2TK_BASE_SCROLLBAR(base, vrect, D2TK_ID_IDX(iview), D2TK_FLAG_SCROLL_Y,
							0, handle->nplugs, 0, dn, vscroll)
						{
							const float voffset = d2tk_scrollbar_get_offset_y(vscroll);
							const d2tk_rect_t *col = d2tk_scrollbar_get_rect(vscroll);

							D2TK_BASE_TABLE(col, 1, dn, trow)
							{
								const unsigned k = d2tk_table_get_index_y(trow) + voffset;

								if(k >= handle->nplugs)
								{
									break;
								}

								const d2tk_rect_t *row = d2tk_table_get_rect(trow);
								const d2tk_id_t id = D2TK_ID_IDX(iview*dn + k);

								if(d2tk_base_button_label_is_changed(base, id,
									-1, handle->lplugs[k].label, row))
								{
									//FIXME
								}
							}
						}
					} break;
					case VIEW_TYPE_PRESET_LIST:
					{
						//FIXME
					} break;
					case VIEW_TYPE_PATCH_BAY:
					{
						//FIXME
					} break;

					case VIEW_TYPE_MAX:
					{
						// never reached
					} break;
				}
			} break;
			case 2:
			{
				static const d2tk_coord_t hfrac [VIEW_TYPE_MAX + 1] = {
					16, 16, 16,
					0
				};

				D2TK_BASE_LAYOUT(vrect, VIEW_TYPE_MAX + 1, hfrac, D2TK_FLAG_LAYOUT_X_ABS, hlay)
				{
					const d2tk_rect_t *hrect = d2tk_layout_get_rect(hlay);
					const uint32_t vx = d2tk_layout_get_index(hlay);
					const d2tk_id_t id = D2TK_ID_IDX(iview*VIEW_TYPE_MAX + vx);

					switch(vx)
					{
						case VIEW_TYPE_PLUGIN_LIST:
							// fall-through
						case VIEW_TYPE_PRESET_LIST:
							// fall-through
						case VIEW_TYPE_PATCH_BAY:
						{
							if(d2tk_base_toggle_is_changed(base, id, hrect, &view->selector[vx]))
							{
								//FIXME
							}
						} break;
						case VIEW_TYPE_MAX:
						{
							// never reached
						} break;
					}
				}
			} break;
		}
	}

	return 0;
}

static int
_expose(void *data, d2tk_coord_t w, d2tk_coord_t h)
{
	plughandle_t *handle = data;

	d2tk_pugl_t *dpugl = handle->dpugl;
	d2tk_base_t *base = d2tk_pugl_get_base(dpugl);
	const d2tk_rect_t rect = D2TK_RECT(0, 0, w, h);

	static const d2tk_coord_t vfrac [3] = { 24, 0, 16 };
	D2TK_BASE_LAYOUT(&rect, 3, vfrac, D2TK_FLAG_LAYOUT_Y_ABS, vlay)
	{
		const d2tk_rect_t *vrect = d2tk_layout_get_rect(vlay);
		const uint32_t vy = d2tk_layout_get_index(vlay);

		switch(vy)
		{
			case 0:
			{
				d2tk_base_label(base, -1, "Menu", 1.f, vrect,
					D2TK_ALIGN_MIDDLE| D2TK_ALIGN_LEFT);
			} break;
			case 1:
			{
				D2TK_BASE_PANE(base, vrect, D2TK_ID, D2TK_FLAG_PANE_X,
					0.05f, 0.95f, 0.05f, hpane)
				{
					const d2tk_rect_t *prect =  d2tk_pane_get_rect(hpane);
					const uint32_t px = d2tk_pane_get_index(hpane);

					_expose_view(handle, px, prect);
				}
			} break;
			case 2:
			{
				d2tk_base_label(base, -1, "Synthpod "SYNTHPOD_VERSION, 1.f, vrect,
					D2TK_ALIGN_MIDDLE| D2TK_ALIGN_RIGHT);
			} break;
		}
	}

	return 0;
}

static LV2UI_Handle
instantiate(const LV2UI_Descriptor *descriptor, const char *plugin_uri,
	const char *bundle_path, LV2UI_Write_Function write_function,
	LV2UI_Controller controller, LV2UI_Widget *widget,
	const LV2_Feature *const *features)
{
	plughandle_t *handle = calloc(1, sizeof(plughandle_t));
	if(!handle)
		return NULL;

	void *parent = NULL;
	LV2UI_Resize *host_resize = NULL;
	for(int i=0; features[i]; i++)
	{
		if(!strcmp(features[i]->URI, LV2_UI__parent))
			parent = features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_UI__resize))
			host_resize = features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_URID__map))
			handle->map = features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_LOG__log))
			handle->log = features[i]->data;
	}

	if(!parent)
	{
		fprintf(stderr,
			"%s: Host does not support ui:parent\n", descriptor->URI);
		free(handle);
		return NULL;
	}
	if(!handle->map)
	{
		fprintf(stderr,
			"%s: Host does not support urid:map\n", descriptor->URI);
		free(handle);
		return NULL;
	}

	if(handle->log)
		lv2_log_logger_init(&handle->logger, handle->map, handle->log);

	lv2_atom_forge_init(&handle->forge, handle->map);

	handle->controller = controller;
	handle->writer = write_function;

	const d2tk_coord_t w = 800;
	const d2tk_coord_t h = 450;

	d2tk_pugl_config_t *config = &handle->config;
	config->parent = (uintptr_t)parent;
	config->bundle_path = bundle_path;
	config->min_w = w/2;
	config->min_h = h/2;
	config->w = w;
	config->h = h;
	config->fixed_size = false;
	config->fixed_aspect = false;
	config->expose = _expose;
	config->data = handle;

	handle->dpugl = d2tk_pugl_new(config, (uintptr_t *)widget);
	if(!handle->dpugl)
	{
		free(handle);
		return NULL;
	}

	if(host_resize)
	{
		host_resize->ui_resize(host_resize->handle, w, h);
	}

	handle->world = lilv_world_new();

	LilvNode *node_false = lilv_new_bool(handle->world, false);
	if(node_false)
	{
		lilv_world_set_option(handle->world, LILV_OPTION_DYN_MANIFEST, node_false);
		lilv_node_free(node_false);
	}
	lilv_world_load_all(handle->world);

	handle->plugs = lilv_world_get_all_plugins(handle->world);
	handle->nplugs = lilv_plugins_size(handle->plugs);
	handle->lplugs = calloc(1, handle->nplugs * sizeof(entry_t));


	strncpy(handle->pplugs, "*", sizeof(handle->plugs));
	_plug_populate(handle, handle->pplugs);

	return handle;
}

static void
cleanup(LV2UI_Handle instance)
{
	plughandle_t *handle = instance;

	d2tk_pugl_free(handle->dpugl);

	free(handle->lplugs);

	lilv_world_free(handle->world);

	free(handle);
}

static void
port_event(LV2UI_Handle instance, uint32_t port_index, uint32_t size,
	uint32_t format, const void *buffer)
{
	plughandle_t *handle = instance;

	d2tk_pugl_redisplay(handle->dpugl);
}

static int
_idle(LV2UI_Handle instance)
{
	plughandle_t *handle = instance;

	return d2tk_pugl_step(handle->dpugl);
}

static const LV2UI_Idle_Interface idle_ext = {
	.idle = _idle
};

static int
_resize(LV2UI_Handle instance, int width, int height)
{
	plughandle_t *handle = instance;

	return d2tk_pugl_resize(handle->dpugl, width, height);
}

static const LV2UI_Resize resize_ext = {
	.ui_resize = _resize
};

static const void *
extension_data(const char *uri)
{
	if(!strcmp(uri, LV2_UI__idleInterface))
		return &idle_ext;
	else if(!strcmp(uri, LV2_UI__resize))
		return &resize_ext;

	return NULL;
}

const LV2UI_Descriptor synthpod_common_5_d2tk = {
	.URI						= SYNTHPOD_COMMON_D2TK_URI,
	.instantiate		= instantiate,
	.cleanup				= cleanup,
	.port_event			= port_event,
	.extension_data	= extension_data
};

const LV2UI_Descriptor synthpod_root_5_d2tk = {
	.URI						= SYNTHPOD_ROOT_D2TK_URI,
	.instantiate		= instantiate,
	.cleanup				= cleanup,
	.port_event			= port_event,
	.extension_data	= extension_data
};