aboutsummaryrefslogtreecommitdiff
path: root/canvas_ui.c
blob: cdbc86834992e6b726fb34aa8004e24ca92e771e (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
/*
 * 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 <canvas.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;

	LV2_Log_Log *log;
	LV2_Log_Logger logger;

	PuglView *view;
	int done;
	struct {
		double r;
		double g;
		double b;
	} col;
	struct {
		bool r;
		bool g;
		bool b;
	} dir;
};

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

	// Draw background
	int w, h;
	puglGetSize(handle->view, &w, &h);

	cairo_surface_set_device_scale(surf, w, h);

	cairo_set_source_rgba(ctx, 0.0, 0.0, 0.0, 1.0);
	cairo_rectangle(ctx, 0.0, 0.0, 1.0, 1.0);
	cairo_fill(ctx);

	cairo_set_source_rgba(ctx, handle->col.r, handle->col.g, handle->col.b, 1.0);
	cairo_arc(ctx, 0.5, 0.5, 0.4, 0.0, 2*M_PI);
	cairo_fill(ctx);
#endif
}

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

static inline void
_enter(plughandle_t *handle)
{
	puglPostRedisplay(handle->view);
}

static inline void
_leave(plughandle_t *handle)
{
	puglPostRedisplay(handle->view);
}

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

	switch(ev->type)
	{
		case PUGL_NOTHING:
			break;
		case PUGL_BUTTON_PRESS:
			break;
		case PUGL_BUTTON_RELEASE:
			break;
		case PUGL_CONFIGURE:
			break;
		case PUGL_EXPOSE:
			_expose(handle);
			break;
		case PUGL_CLOSE:
			_close(handle);
			break;
		case PUGL_KEY_PRESS:
			break;
		case PUGL_KEY_RELEASE:
			break;
		case PUGL_ENTER_NOTIFY:
			_enter(handle);
			break;
		case PUGL_LEAVE_NOTIFY:
			_leave(handle);
			break;
		case PUGL_MOTION_NOTIFY:
			break;
		case PUGL_SCROLL:
			break;
		case PUGL_FOCUS_IN:
			break;
		case PUGL_FOCUS_OUT:
			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;
		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->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);

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

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

	return handle;
}

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

	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)
{
	//FIXME
}

static int 
_resize(LV2UI_Handle instance, int w, int h)
{
	plughandle_t *handle = instance;

	//FIXME
	//puglInitWindowSize(handle->view, w, h);
	//puglInitWindowMinSize(handle->view, w, h);

	return 0;
}

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

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

	handle->col.r += 0.01 * (handle->dir.r ? 1.0 : -1.0);
	if(handle->dir.r && (handle->col.r > 1.0) )
	{
		handle->dir.r = false;
		handle->col.r = 1.0;
	}
	else if(!handle->dir.r && (handle->col.r < 0.0) )
	{
		handle->dir.r = true;
		handle->col.r = 0.0;
	}

	handle->col.g += 0.02 * (handle->dir.g ? 1.0 : -1.0);
	if(handle->dir.g && (handle->col.g > 1.0) )
	{
		handle->dir.g = false;
		handle->col.g = 1.0;
	}
	else if(!handle->dir.g && (handle->col.g < 0.0) )
	{
		handle->dir.g = true;
		handle->col.g = 0.0;
	}

	handle->col.b += 0.04 * (handle->dir.b ? 1.0 : -1.0);
	if(handle->dir.b && (handle->col.b > 1.0) )
	{
		handle->dir.b = false;
		handle->col.b = 1.0;
	}
	else if(!handle->dir.b && (handle->col.b < 0.0) )
	{
		handle->dir.b = true;
		handle->col.b = 0.0;
	}

	puglPostRedisplay(handle->view);

	if(puglProcessEvents(handle->view) != PUGL_SUCCESS)
	{
		//TODO
	}

	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;
	else if(!strcmp(uri, LV2_UI__resize))
		return &resize_ext;
		
	return NULL;
}

const LV2UI_Descriptor canvas_canvas_ui = {
	.URI            = CANVAS_CANVAS_UI_URI,
	.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;
	}
}