aboutsummaryrefslogtreecommitdiff
path: root/lv2_eo_ui.h
blob: b98a0b9b770e7d6812617584c5e83a0ed208b58c (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
/*
 * Copyright (c) 2015 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.
 */

#ifndef _EO_UI_H
#define _EO_UI_H

#include <Elementary.h>

#if defined(X11_UI_WRAP)
#	include <Ecore_X.h>
#endif

#include <lv2/lv2plug.in/ns/extensions/ui/ui.h>
#include <lv2_external_ui.h> // kxstudio external-ui extension

typedef enum _eo_ui_driver_t eo_ui_driver_t;
typedef struct _eo_ui_t eo_ui_t;
typedef Evas_Object *(*eo_ui_content_get)(eo_ui_t *eoui);

enum _eo_ui_driver_t {
	EO_UI_DRIVER_NONE	= 0,
	EO_UI_DRIVER_EO,
	EO_UI_DRIVER_X11,
	EO_UI_DRIVER_UI,
	EO_UI_DRIVER_KX
};

struct _eo_ui_t {
	eo_ui_driver_t driver;
  LV2UI_Controller controller;
	int w, h;

	Evas_Object *win; // main window
	Evas_Object *bg; // background

	Evas_Object *content;
	eo_ui_content_get content_get;

	union {
		// eo iface
		struct {
			Evas_Object *parent;
			LV2UI_Resize *resize;
		} eo;

		// show iface
		struct {
			volatile int done;
		} ui;

#if defined(X11_UI_WRAP)
		// X11 iface
		struct {
			Ecore_X_Window parent;
			Ecore_X_Window child;
			LV2UI_Resize *resize;

			Ecore_Evas *ee;
		} x11;
#endif

		// external-ui iface
		struct {
			LV2_External_UI_Widget widget;
			const LV2_External_UI_Host *host;
		} kx;
	};
};

// Idle interface
static inline int
_idle_cb(LV2UI_Handle instance)
{
	eo_ui_t *eoui = instance;
	if(!eoui)
		return -1;

	ecore_main_loop_iterate();

	return eoui->ui.done;
}

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

static inline void
_show_delete_request(void *data, Evas_Object *obj, void *event_info)
{
	eo_ui_t *eoui = data;
	if(!eoui)
		return;

	// set done flag, host will then call _hide_cb
	eoui->ui.done = 1;
}

// Show Interface
static inline int
_show_cb(LV2UI_Handle instance)
{
	eo_ui_t *eoui = instance;
	if(!eoui)
		return -1;

	// create main window
	eoui->win = elm_win_add(NULL, "EoUI", ELM_WIN_BASIC);
	if(!eoui->win)
		return -1;
	evas_object_smart_callback_add(eoui->win, "delete,request",
		_show_delete_request, eoui);
	evas_object_resize(eoui->win, eoui->w, eoui->h);
	evas_object_show(eoui->win);

	eoui->bg = elm_bg_add(eoui->win);
	if(eoui->bg)
	{
		elm_bg_color_set(eoui->bg, 64, 64, 64);
		evas_object_size_hint_weight_set(eoui->bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
		evas_object_size_hint_align_set(eoui->bg, EVAS_HINT_FILL, EVAS_HINT_FILL);
		evas_object_show(eoui->bg);
		elm_win_resize_object_add(eoui->win, eoui->bg);
	}

	eoui->content = eoui->content_get(eoui);
	if(eoui->content)
	{
		evas_object_size_hint_weight_set(eoui->content, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
		evas_object_size_hint_align_set(eoui->content, EVAS_HINT_FILL, EVAS_HINT_FILL);
		evas_object_show(eoui->content);
		elm_win_resize_object_add(eoui->win, eoui->content);
	}

	return 0;
}

static inline int
_hide_cb(LV2UI_Handle instance)
{
	eo_ui_t *eoui = instance;
	if(!eoui)
		return -1;

	// hide & delete bg&main window
	if(eoui->win)
	{
		if(eoui->content)
		{
			elm_win_resize_object_del(eoui->win, eoui->content);
			evas_object_del(eoui->content);
			eoui->content = NULL;
		}
		if(eoui->bg)
		{
			elm_win_resize_object_del(eoui->win, eoui->bg);
			evas_object_del(eoui->bg);
			eoui->bg = NULL;
		}
		evas_object_del(eoui->win);
		eoui->win = NULL;
	}

	// reset done flag
	eoui->ui.done = 0;

	return 0;
}

static const LV2UI_Show_Interface show_ext = {
	.show = _show_cb,
	.hide = _hide_cb
};

// External-UI Interface
static inline void
_kx_run(LV2_External_UI_Widget *widget)
{
	eo_ui_t *eoui = widget
		? (void *)widget - offsetof(eo_ui_t, kx.widget)
		: NULL;
	if(!eoui)
		return;

	ecore_main_loop_iterate();
}

static inline void
_kx_hide(LV2_External_UI_Widget *widget)
{
	eo_ui_t *eoui = widget
		? (void *)widget - offsetof(eo_ui_t, kx.widget)
		: NULL;
	if(!eoui)
		return;

	// hide & delete bg & main window
	if(eoui->win)
	{
		if(eoui->content)
		{
			elm_win_resize_object_del(eoui->win, eoui->content);
			evas_object_del(eoui->content);
			eoui->content = NULL;
		}
		if(eoui->bg)
		{
			elm_win_resize_object_del(eoui->win, eoui->bg);
			evas_object_del(eoui->bg);
			eoui->bg = NULL;
		}
		evas_object_del(eoui->win); // will call _kx_free
		eoui->win = NULL;
	}
}

static inline void
_kx_free(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
	eo_ui_t *eoui = data;
	if(!eoui)
		return;

	eoui->content = NULL;
	eoui->bg = NULL;
	eoui->win = NULL;

	if(eoui->kx.host->ui_closed && eoui->controller)
		eoui->kx.host->ui_closed(eoui->controller);
}

static inline void
_kx_show(LV2_External_UI_Widget *widget)
{
	eo_ui_t *eoui = widget
		? (void *)widget - offsetof(eo_ui_t, kx.widget)
		: NULL;
	if(!eoui || eoui->win)
		return;

	// create main window
	const char *title = eoui->kx.host->plugin_human_id
		? eoui->kx.host->plugin_human_id
		: "EoUI";
	eoui->win = elm_win_add(NULL, title, ELM_WIN_BASIC);
	if(!eoui->win)
		return;
	elm_win_autodel_set(eoui->win, EINA_TRUE);
	evas_object_event_callback_add(eoui->win, EVAS_CALLBACK_FREE, _kx_free, eoui);
	evas_object_resize(eoui->win, eoui->w, eoui->h);
	evas_object_show(eoui->win);

	eoui->bg = elm_bg_add(eoui->win);
	if(eoui->bg)
	{
		elm_bg_color_set(eoui->bg, 64, 64, 64);
		evas_object_size_hint_weight_set(eoui->bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
		evas_object_size_hint_align_set(eoui->bg, EVAS_HINT_FILL, EVAS_HINT_FILL);
		evas_object_show(eoui->bg);
		elm_win_resize_object_add(eoui->win, eoui->bg);
	}

	eoui->content = eoui->content_get(eoui);
	if(eoui->content)
	{
		evas_object_size_hint_weight_set(eoui->content, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
		evas_object_size_hint_align_set(eoui->content, EVAS_HINT_FILL, EVAS_HINT_FILL);
		evas_object_show(eoui->content);
		elm_win_resize_object_add(eoui->win, eoui->content);
	}
}

// Resize Interface
static inline int
_ui_resize_cb(LV2UI_Feature_Handle instance, int w, int h)
{
	eo_ui_t *eoui = instance;
	if(!eoui)
		return -1;

	// check whether size actually needs any update
	if( (eoui->w == w) && (eoui->h == h) )
		return 0;

	// update size
	eoui->w = w;
	eoui->h = h;

	// resize main window
#if defined(X11_UI_WRAP)
	if(eoui->x11.ee)
		ecore_evas_resize(eoui->x11.ee, eoui->w, eoui->h);
#endif
	if(eoui->win)
		evas_object_resize(eoui->win, eoui->w, eoui->h);
	if(eoui->bg)
		evas_object_resize(eoui->bg, eoui->w, eoui->h);
	if(eoui->content)
		evas_object_resize(eoui->content, eoui->w, eoui->h);
  
  return 0;
}

static const LV2UI_Resize resize_ext = {
	.handle = NULL,
	.ui_resize = _ui_resize_cb
};

#if defined(X11_UI_WRAP)
static void
_x11_ui_wrap_mouse_in(Ecore_Evas *ee)
{
	eo_ui_t *eoui = ecore_evas_data_get(ee, "eoui");
	if(!eoui)
		return;

	if(eoui->x11.parent)
		ecore_x_window_focus(eoui->x11.parent);
}
#endif

static inline int
eoui_instantiate(eo_ui_t *eoui, 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)
{
	//eoui->driver = NULL; set by ui plugin
	eoui->controller = controller;
	eoui->w = eoui->w > 0 ? eoui->w : 400; // fall-back if w == 0
	eoui->h = eoui->h > 0 ? eoui->h : 400; // fall-back if h == 0

	*widget = NULL;

	switch(eoui->driver)
	{
		case EO_UI_DRIVER_EO:
		{
			eoui->eo.parent = NULL; // mandatory
			eoui->eo.resize = NULL; // optional
			for(int i=0; features[i]; i++)
			{
				if(!strcmp(features[i]->URI, LV2_UI__parent))
					eoui->eo.parent = features[i]->data;
				else if(!strcmp(features[i]->URI, LV2_UI__resize))
					eoui->eo.resize = (LV2UI_Resize *)features[i]->data;
			}
			if(!eoui->eo.parent)
				return -1;

			eoui->win = eoui->eo.parent;

			eoui->content = eoui->content_get(eoui);

			*(Evas_Object **)widget = eoui->content;

			if(eoui->eo.resize)
				eoui->eo.resize->ui_resize(eoui->eo.resize->handle, eoui->w, eoui->h);

			break;
		}

		case EO_UI_DRIVER_UI:
		{
			// according to the LV2 spec, the host MUST signal availability of
			// idle interface via features, thus we test for it here
			int host_provides_idle_iface = 0; // mandatory
			for(int i=0; features[i]; i++)
			{
				if(!strcmp(features[i]->URI, LV2_UI__idleInterface))
					host_provides_idle_iface = 1;
			}
			if(!host_provides_idle_iface)
				return -1;

			// initialize elementary library
			_elm_startup_time = ecore_time_unix_get();
			elm_init(0, NULL);

			break;
		}

#if defined(X11_UI_WRAP)
		case EO_UI_DRIVER_X11:
		{
			eoui->x11.parent = 0; // mandatory
			eoui->x11.resize = NULL; // optional
			int host_provides_idle_iface = 0; // mandatory
			for(int i=0; features[i]; i++)
			{
				if(!strcmp(features[i]->URI, LV2_UI__parent))
					eoui->x11.parent = (Ecore_X_Window)(uintptr_t)features[i]->data;
				else if(!strcmp(features[i]->URI, LV2_UI__resize))
					eoui->x11.resize = (LV2UI_Resize *)features[i]->data;
				else if(!strcmp(features[i]->URI, LV2_UI__idleInterface))
					host_provides_idle_iface = 1;
			}
			if(!eoui->x11.parent || !host_provides_idle_iface)
				return -1;

			// initialize elementary library
			_elm_startup_time = ecore_time_unix_get();
			elm_init(0, NULL);

			eoui->x11.ee = ecore_evas_gl_x11_new(NULL, eoui->x11.parent, 0, 0,
				eoui->w, eoui->h);
			if(!eoui->x11.ee)
				eoui->x11.ee = ecore_evas_software_x11_new(NULL, eoui->x11.parent, 0, 0,
					eoui->w, eoui->h);
			if(!eoui->x11.ee)
			{
				//elm_shutdown();
				return -1;
			}
			ecore_evas_data_set(eoui->x11.ee, "eoui", eoui);
			ecore_evas_callback_mouse_in_set(eoui->x11.ee, _x11_ui_wrap_mouse_in);
			ecore_evas_show(eoui->x11.ee);
	
			eoui->win = elm_win_fake_add(eoui->x11.ee);
			if(eoui->win)
			{
				evas_object_resize(eoui->win, eoui->w, eoui->h);
				evas_object_show(eoui->win);

				eoui->bg = elm_bg_add(eoui->win);
				if(eoui->bg)
				{
					elm_bg_color_set(eoui->bg, 64, 64, 64);
					evas_object_size_hint_weight_set(eoui->bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
					evas_object_size_hint_align_set(eoui->bg, EVAS_HINT_FILL, EVAS_HINT_FILL);
					evas_object_resize(eoui->bg, eoui->w, eoui->h);
					evas_object_show(eoui->bg);
				}

				eoui->content = eoui->content_get(eoui);
				if(eoui->content)
				{
					evas_object_size_hint_weight_set(eoui->content, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
					evas_object_size_hint_align_set(eoui->content, EVAS_HINT_FILL, EVAS_HINT_FILL);
					evas_object_resize(eoui->content, eoui->w, eoui->h);
					evas_object_show(eoui->content);
				}
			}

			eoui->x11.child = elm_win_xwindow_get(eoui->win);
			*(uintptr_t *)widget = eoui->x11.child;

			if(eoui->x11.resize)
				eoui->x11.resize->ui_resize(eoui->x11.resize->handle, eoui->w, eoui->h);

			break;
		}
#endif

		case EO_UI_DRIVER_KX:
		{
			eoui->kx.host = NULL; // mandatory
			for(int i=0; features[i]; i++)
			{
				if(!strcmp(features[i]->URI, LV2_EXTERNAL_UI__Host))
					eoui->kx.host = features[i]->data;
			}
			if(!eoui->kx.host)
				return -1;

			// initialize elementary library
			_elm_startup_time = ecore_time_unix_get();
			elm_init(0, NULL);

			eoui->kx.widget.run = _kx_run;
			eoui->kx.widget.show = _kx_show;
			eoui->kx.widget.hide = _kx_hide;

			*(LV2_External_UI_Widget **)widget = &eoui->kx.widget;

			break;
		}

		default:
			break;
	}

	return 0;
}

static inline void
eoui_cleanup(eo_ui_t *eoui)
{
	switch(eoui->driver)
	{
		case EO_UI_DRIVER_EO:
		{
			eoui->content = NULL;

			break;
		}

		case EO_UI_DRIVER_UI:
		{
			//elm_shutdown();

			break;
		}

#if defined(X11_UI_WRAP)
		case EO_UI_DRIVER_X11:
		{
			if(eoui->win)
			{
				if(eoui->content)
				{
					elm_win_resize_object_del(eoui->win, eoui->content);
					evas_object_del(eoui->content);
				}
				if(eoui->bg)
				{
					elm_win_resize_object_del(eoui->win, eoui->bg);
					evas_object_del(eoui->bg);
				}
				evas_object_del(eoui->win);
			}
			if(eoui->x11.ee)
			{
				//ecore_evas_free(eoui->x11.ee);
			}

			//elm_shutdown();

			break;
		}
#endif

		case EO_UI_DRIVER_KX:
		{
			//elm_shutdown();

			break;
		}

		default:
			break;
	}

	// clear eoui
	memset(eoui, 0, sizeof(eo_ui_t));
}

// extension data callback for EoUI
static inline const void *
eoui_eo_extension_data(const char *uri)
{
	return NULL;
}

// extension data callback for show interface UI
static inline const void *
eoui_ui_extension_data(const char *uri)
{
	if(!strcmp(uri, LV2_UI__idleInterface))
		return &idle_ext;
	else if(!strcmp(uri, LV2_UI__showInterface))
		return &show_ext;
		
	return NULL;
}

// extension data callback for X11UI
static inline const void *
eoui_x11_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;
}

// extension data callback for external-ui
static inline const void *
eoui_kx_extension_data(const char *uri)
{
	return NULL;
}

#endif // _EO_UI_H