aboutsummaryrefslogtreecommitdiff
path: root/test/canvas_ui.c
blob: 61b4b8854ca8f01e67d4854c594d22ae65d45e15 (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
/*
 * Copyright (c) 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 <stdlib.h>
#include <stdio.h>
#include <math.h>

#include <lv2/lv2plug.in/ns/ext/patch/patch.h>
#include <lv2/lv2plug.in/ns/extensions/ui/ui.h>

#include <canvas.lv2/forge.h>
#include <canvas.lv2/render.h>

#include <pugl/pugl.h>

#include <cairo/cairo.h>

typedef struct _plughandle_t plughandle_t;

struct _plughandle_t {
	LV2_URID_Map *map;
	LV2_Atom_Forge forge;

	PuglView *view;
	int done;

	LV2_Canvas canvas;

	LV2_URID patch_Get;
	LV2_URID patch_Set;
	LV2_URID patch_property;
	LV2_URID patch_value;
	LV2_URID atom_eventTransfer;

	LV2_Atom_Tuple *graph;

	LV2UI_Write_Function writer;
	LV2UI_Controller controller;
};

static inline void
_refresh(plughandle_t *handle)
{
	union {
		LV2_Atom atom;
		uint8_t buf [128];
	} dst;

	lv2_atom_forge_set_buffer(&handle->forge, dst.buf, 128);
	LV2_Atom_Forge_Ref ref;

	LV2_Atom_Forge_Frame obj_frame;
	ref = lv2_atom_forge_object(&handle->forge, &obj_frame, 0, handle->patch_Get);
	if(ref)
		ref = lv2_atom_forge_key(&handle->forge, handle->patch_property);
	if(ref)
		ref = lv2_atom_forge_urid(&handle->forge, handle->canvas.urid.Canvas_graph);
	if(ref)
		lv2_atom_forge_pop(&handle->forge, &obj_frame);

	const uint32_t sz = lv2_atom_total_size(&dst.atom);
	handle->writer(handle->controller, 0, sz, handle->atom_eventTransfer, &dst.atom);
}

static inline void
_expose(plughandle_t *handle)
{
#ifndef _WIN32 //FIXME
	cairo_t *ctx = puglGetContext(handle->view);

	if(ctx)
	{
		lv2_canvas_render(&handle->canvas, ctx, handle->graph);
	}
#endif
}

static inline void
_close(plughandle_t *handle)
{
	handle->done = 1;
}

static inline void
_configure(plughandle_t *handle, const PuglEventConfigure *e)
{
#ifndef _WIN32 //FIXME
	cairo_t *ctx = puglGetContext(handle->view);

	if(ctx)
	{
		cairo_surface_t *surf = cairo_get_target(ctx);
		cairo_surface_set_device_scale(surf, e->width, e->height);
	}
#endif
}

static void
_event_func(PuglView *view, const PuglEvent *e)
{
	plughandle_t *handle = puglGetHandle(view);

	switch(e->type)
	{
		case PUGL_CONFIGURE:
		{
			_configure(handle, (const PuglEventConfigure *)e);
			puglPostRedisplay(handle->view);
		}	break;
		case PUGL_EXPOSE:
		{
			_expose(handle);
		}	break;
		case PUGL_CLOSE:
		{
			_close(handle);
		}	break;
		case PUGL_ENTER_NOTIFY:
			// fall-through
		case PUGL_LEAVE_NOTIFY:
			// fall-through
		case PUGL_FOCUS_IN:
			// fall-through
		case PUGL_FOCUS_OUT:
		{
			puglPostRedisplay(handle->view);
		}	break;

		case PUGL_NOTHING:
			// fall-through
		case PUGL_BUTTON_PRESS:
			// fall-through
		case PUGL_BUTTON_RELEASE:
			// fall-through
		case PUGL_KEY_PRESS:
			// fall-through
		case PUGL_KEY_RELEASE:
			// fall-through
		case PUGL_MOTION_NOTIFY:
			// fall-through
		case PUGL_SCROLL:
		{
			// nothing
		}	break;
	}
}

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;
	}

	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;
	}

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

	handle->patch_Get = handle->map->map(handle->map->handle, LV2_PATCH__Get);
	handle->patch_Set = handle->map->map(handle->map->handle, LV2_PATCH__Set);
	handle->patch_property = handle->map->map(handle->map->handle, LV2_PATCH__property);
	handle->patch_value = handle->map->map(handle->map->handle, LV2_PATCH__value);
	handle->atom_eventTransfer= handle->map->map(handle->map->handle, LV2_ATOM__eventTransfer);

	handle->view = puglInit(NULL, NULL);
	if(!handle->view)
	{
		free(handle);
		return NULL;
	}

	const unsigned w = 400;
	const unsigned h = 400;;
	puglInitWindowClass(handle->view, "canvas");
	puglInitWindowParent(handle->view, (intptr_t)parent);
	puglInitWindowSize(handle->view, w, h);
	puglInitWindowMinSize(handle->view, w, h);
	puglInitWindowAspectRatio(handle->view, 1, 1, 1, 1);
	puglInitResizable(handle->view, true);
	puglInitTransientFor(handle->view, (intptr_t)parent);
	puglSetHandle(handle->view, handle);
	puglSetEventFunc(handle->view, _event_func);

	int stat;
#ifdef PUGL_HAVE_GL
	puglInitContextType(handle->view, PUGL_CAIRO_GL);
	stat = puglCreateWindow(handle->view, "CanvasGL");
	if(stat != 0)
#endif
	{
		fprintf(stderr, "falling back to non-GL\n");
		puglInitContextType(handle->view, PUGL_CAIRO);
		stat = puglCreateWindow(handle->view, "Canvas");
	}

	if(stat != 0)
	{
		puglDestroy(handle->view);
		free(handle);
		return NULL;
	}
	puglShowWindow(handle->view);

#ifndef _WIN32 //FIXME
	cairo_t *ctx = puglGetContext(handle->view);
	cairo_surface_t *surf = cairo_get_target(ctx);
	cairo_surface_set_device_scale(surf, w, h);
#endif

	const intptr_t child = puglGetNativeWindow(handle->view);
	*(intptr_t *)widget = child;

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

	lv2_canvas_init(&handle->canvas, handle->map);

	handle->controller = controller;
	handle->writer = write_function;
	_refresh(handle);

	return handle;
}

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

	if(handle->graph)
		free(handle->graph);

	if(handle->view)
	{
		if(puglGetVisible(handle->view))
			puglHideWindow(handle->view);
		puglDestroy(handle->view);
	}

	free(handle);
}

static void
port_event(LV2UI_Handle instance, uint32_t index, uint32_t size,
	uint32_t protocol, const void *buf)
{
	plughandle_t *handle = instance;

	if( (index == 1) && (protocol == handle->atom_eventTransfer) ) // notify
	{
		const LV2_Atom_Object *obj = buf;

		if(lv2_atom_forge_is_object_type(&handle->forge, obj->atom.type))
		{
			if(obj->body.otype == handle->patch_Set)
			{
				const LV2_Atom_URID *property = NULL;
				const LV2_Atom_Tuple *graph = NULL;

				lv2_atom_object_get(obj,
					handle->patch_property, &property,
					handle->patch_value, &graph,
					0);

				if(  property
					&& (property->atom.type == handle->forge.URID)
					&& (property->body == handle->canvas.urid.Canvas_graph)
					&& graph 
					&& (graph->atom.type == handle->forge.Tuple) )
				{
					if(handle->graph)
						free(handle->graph);

					const size_t sz = lv2_atom_total_size(&graph->atom);
					handle->graph = malloc(sz);
					if(handle->graph)
						memcpy(handle->graph, graph, sz);

					puglPostRedisplay(handle->view);
				}
			}
		}
	}
}

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

	puglProcessEvents(handle->view);

	return handle->done;
}

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

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

	return NULL;
}

const LV2UI_Descriptor canvas_canvas_ui = {
	.URI            = CANVAS_PREFIX"canvas_ui",
	.instantiate    = instantiate,
	.cleanup        = cleanup,
	.port_event     = port_event,
	.extension_data = extension_data
};

LV2_SYMBOL_EXPORT const LV2UI_Descriptor*
lv2ui_descriptor(uint32_t index)
{
	switch(index)
	{
		case 0:
			return &canvas_canvas_ui;

		default:
			return NULL;
	}
}