aboutsummaryrefslogtreecommitdiff
path: root/test/canvas_ui.c
blob: d6850e6cad6a64512399e973d585e564cc461cc5 (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
/*
 * 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;
	LV2UI_Resize *host_resize;

	PuglView *view;
	int done;

	LV2_Canvas canvas;

	LV2_URID patch_Get;
	LV2_URID patch_Set;
	LV2_URID patch_Put;
	LV2_URID patch_property;
	LV2_URID patch_value;
	LV2_URID patch_body;
	LV2_URID atom_eventTransfer;

	float aspect_ratio;
	LV2_Atom_Tuple *graph;

	LV2UI_Write_Function writer;
	LV2UI_Controller controller;

	union {
		LV2_Atom atom;
		uint8_t buf [512];
	} dst;
};

static inline void
_event_request(plughandle_t *handle)
{
	lv2_atom_forge_set_buffer(&handle->forge, handle->dst.buf, 512);
}

static inline void
_event_commit(plughandle_t *handle)
{
	const uint32_t sz = lv2_atom_total_size(&handle->dst.atom);
	handle->writer(handle->controller, 0, sz, handle->atom_eventTransfer, &handle->dst.atom);
}

static inline void
_refresh(plughandle_t *handle)
{
	LV2_Atom_Forge_Frame obj_frame;
	LV2_Atom_Forge_Ref ref;

	_event_request(handle);

	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_aspectRatio);
	if(ref)
		lv2_atom_forge_pop(&handle->forge, &obj_frame);

	if(ref)
		_event_commit(handle);

	_event_request(handle);

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

	if(ref)
		_event_commit(handle);
}

static inline LV2_Atom_Forge_Ref
_input_request(plughandle_t *handle, LV2_Atom_Forge_Frame frame [2])
{
	LV2_Atom_Forge_Ref ref;

	_event_request(handle);

	ref = lv2_atom_forge_object(&handle->forge, &frame[0], 0, handle->patch_Put);
	if(ref)
		ref = lv2_atom_forge_key(&handle->forge, handle->patch_body);
	if(ref)
		ref = lv2_atom_forge_object(&handle->forge, &frame[1], 0, 0);

	return ref;
}

static inline void
_input_commit(plughandle_t *handle, LV2_Atom_Forge_Frame frame [2])
{
	lv2_atom_forge_pop(&handle->forge, &frame[1]);
	lv2_atom_forge_pop(&handle->forge, &frame[0]);

	_event_commit(handle);
}

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
_relative_position(PuglView *view, double xa, double ya, double *xr, double *yr)
{
	int width;
	int height;

	puglGetSize(view, &width, &height);

	*xr = (double)xa / width;
	*yr = (double)ya / height;
}

static void
_event_func(PuglView *view, const PuglEvent *e)
{
	plughandle_t *handle = puglGetHandle(view);
	LV2_Atom_Forge_Ref ref;
	LV2_Atom_Forge_Frame frame [2];

	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_FOCUS_IN:
			// fall-through
		case PUGL_FOCUS_OUT:
		{
			puglPostRedisplay(handle->view);
		} break;

		case PUGL_ENTER_NOTIFY:
		case PUGL_LEAVE_NOTIFY:
		{
			double x, y;
			_relative_position(view, e->crossing.x, e->crossing.y, &x, &y);

			ref = _input_request(handle, frame);
			if(ref)
				ref = lv2_atom_forge_key(&handle->forge, handle->canvas.urid.Canvas_mousePositionX);
			if(ref)
				ref = lv2_atom_forge_double(&handle->forge, x);
			if(ref)
				ref = lv2_atom_forge_key(&handle->forge, handle->canvas.urid.Canvas_mousePositionY);
			if(ref)
				ref = lv2_atom_forge_double(&handle->forge, y);
			if(ref)
				ref = lv2_atom_forge_key(&handle->forge, handle->canvas.urid.Canvas_mouseFocus);
			if(ref)
				ref = lv2_atom_forge_bool(&handle->forge, e->type == PUGL_ENTER_NOTIFY ? 1 : 0);
			if(ref)
				_input_commit(handle, frame);

			puglPostRedisplay(handle->view);
		} break;

		case PUGL_BUTTON_PRESS:
		case PUGL_BUTTON_RELEASE:
		{
			double x, y;
			_relative_position(view, e->button.x, e->button.y, &x, &y);

			ref = _input_request(handle, frame);
			if(ref)
				ref = lv2_atom_forge_key(&handle->forge, handle->canvas.urid.Canvas_mousePositionX);
			if(ref)
				ref = lv2_atom_forge_double(&handle->forge, x);
			if(ref)
				ref = lv2_atom_forge_key(&handle->forge, handle->canvas.urid.Canvas_mousePositionY);
			if(ref)
				ref = lv2_atom_forge_double(&handle->forge, y);
			LV2_URID button_urid;
			switch(e->button.button)
			{
				case 3:
					button_urid = handle->canvas.urid.Canvas_mouseButtonRight;
					break;
				case 2:
					button_urid = handle->canvas.urid.Canvas_mouseButtonMiddle;
					break;
				case 1:
				default:
					button_urid = handle->canvas.urid.Canvas_mouseButtonLeft;
					break;
			}
			if(ref)
				ref = lv2_atom_forge_key(&handle->forge, button_urid);
			if(ref)
				ref = lv2_atom_forge_bool(&handle->forge, e->type == PUGL_BUTTON_PRESS ? 1 : 0);
			if(ref)
				_input_commit(handle, frame);
		} break;

		case PUGL_MOTION_NOTIFY:
		{
			double x, y;
			_relative_position(view, e->motion.x, e->motion.y, &x, &y);

			ref = _input_request(handle, frame);
			if(ref)
				ref = lv2_atom_forge_key(&handle->forge, handle->canvas.urid.Canvas_mousePositionX);
			if(ref)
				ref = lv2_atom_forge_double(&handle->forge, x);
			if(ref)
				ref = lv2_atom_forge_key(&handle->forge, handle->canvas.urid.Canvas_mousePositionY);
			if(ref)
				ref = lv2_atom_forge_double(&handle->forge, y);
			if(ref)
				_input_commit(handle, frame);
		} break;

		case PUGL_SCROLL:
		{
			double x, y;
			_relative_position(view, e->scroll.x, e->scroll.y, &x, &y);

			ref = _input_request(handle, frame);
			if(ref)
				ref = lv2_atom_forge_key(&handle->forge, handle->canvas.urid.Canvas_mousePositionX);
			if(ref)
				ref = lv2_atom_forge_double(&handle->forge, x);
			if(ref)
				ref = lv2_atom_forge_key(&handle->forge, handle->canvas.urid.Canvas_mousePositionY);
			if(ref)
				ref = lv2_atom_forge_double(&handle->forge, y);
			if(ref)
				ref = lv2_atom_forge_key(&handle->forge, handle->canvas.urid.Canvas_mouseWheelX);
			if(ref)
				ref = lv2_atom_forge_double(&handle->forge, e->scroll.dx);
			if(ref)
				ref = lv2_atom_forge_key(&handle->forge, handle->canvas.urid.Canvas_mouseWheelY);
			if(ref)
				ref = lv2_atom_forge_double(&handle->forge, e->scroll.dy);
			if(ref)
				_input_commit(handle, frame);
		} break;

		case PUGL_KEY_PRESS:
			// fall-through
		case PUGL_KEY_RELEASE:
			// fall-through
		case PUGL_NOTHING:
			// fall-through
		{
			// 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;
	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))
			handle->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_Put = handle->map->map(handle->map->handle, LV2_PATCH__Put);
	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->patch_body = handle->map->map(handle->map->handle, LV2_PATCH__body);
	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 = 640;
	const unsigned h = 640;;
	puglInitWindowClass(handle->view, "canvas");
	puglInitWindowParent(handle->view, (intptr_t)parent);
	puglInitWindowSize(handle->view, w, h);
	puglInitWindowMinSize(handle->view, w/8, h/8);
	//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(handle->host_resize)
		handle->host_resize->ui_resize(handle->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(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 *value = NULL;

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

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

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

					puglPostRedisplay(handle->view);
				}
				else if(  property
					&& (property->atom.type == handle->forge.URID)
					&& (property->body == handle->canvas.urid.Canvas_aspectRatio)
					&& value
					&& (value->type == handle->forge.Float) )
				{
					handle->aspect_ratio =  ((const LV2_Atom_Float *)value)->body;

					int w;
					int h;

					puglGetSize(handle->view, &w, &h);

					int W;
					int H;

					if(handle->aspect_ratio == 0.f)
					{
						W = w;
						H = h;
					}
					else if(handle->aspect_ratio <= 1.f)
					{
						W = h * handle->aspect_ratio;
						H = h;
					}
					else if(handle->aspect_ratio > 1.f)
					{
						W = w;
						H = w / handle->aspect_ratio;
					}

					if(handle->host_resize && ( (W != w) || (H != h) ) )
						handle->host_resize->ui_resize(handle->host_resize->handle, W, H);

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