osc_inspector_eo.c 22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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.
 */

18 19
#include <inttypes.h>

20 21 22 23
#include <sherlock.h>

#include <lv2_osc.h>

24 25
#include <Elementary.h>

26 27 28 29 30 31 32 33 34 35 36 37 38 39
#define COUNT_MAX 2048 // maximal amount of events shown
#define STRING_BUF_SIZE 2048
#define STRING_MAX 256
#define STRING_OFF (STRING_MAX - 4)

typedef struct _UI UI;

struct _UI {
	LV2UI_Write_Function write_function;
	LV2UI_Controller controller;
	
	LV2_URID_Map *map;
	LV2_Atom_Forge forge;
	LV2_URID event_transfer;
40
	LV2_URID midi_event;
41 42
	osc_forge_t oforge;

43
	Evas_Object *widget;
44 45 46
	Evas_Object *table;
	Evas_Object *list;
	Evas_Object *clear;
47 48
	Evas_Object *autoclear;
	Evas_Object *autoblock;
49 50
	Evas_Object *popup;

51
	Elm_Genlist_Item_Class *itc_group;
52 53
	Elm_Genlist_Item_Class *itc_packet;
	Elm_Genlist_Item_Class *itc_item;
54 55 56 57 58

	char string_buf [STRING_BUF_SIZE];
	char *logo_path;
};

59 60 61 62 63 64 65 66
// there is a bug in LV2 <= 0.10
#if defined(LV2_ATOM_TUPLE_FOREACH)
#	undef LV2_ATOM_TUPLE_FOREACH
#	define LV2_ATOM_TUPLE_FOREACH(tuple, iter) \
	for (LV2_Atom* (iter) = lv2_atom_tuple_begin(tuple); \
	     !lv2_atom_tuple_is_end(LV2_ATOM_BODY(tuple), (tuple)->atom.size, (iter)); \
	     (iter) = lv2_atom_tuple_next(iter))
#endif
67

Hanspeter Portner's avatar
Hanspeter Portner committed
68
#define CODE_PRE ("<font=Mono style=shadow,bottom>")
69
#define CODE_POST ("</font>")
70

71
#define PATH(VAL) ("<color=#b0b><b>"VAL"</b></color>")
Hanspeter Portner's avatar
Hanspeter Portner committed
72
#define BUNDLE(VAL) ("<color=#b0b><b>"VAL"</b></color>")
73 74 75 76 77 78 79 80 81
#define TYPE(TYP, VAL) ("<color=#0b0><b>"TYP"</b></color><color=#fff>"VAL"</color>")

#define TYPE_PRE(TYP, VAL) ("<color=#0b0><b>"TYP"</b></color><color=#fff>"VAL)
#define TYPE_POST(VAL) (VAL"</color>")

#define PUNKT(VAL) "<color=#00b>"VAL"</color>"

static inline char *
_timestamp_stringify(UI *ui, char *ptr, char *end, const LV2_Atom_Long *atom)
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
	const uint32_t sec = (uint64_t)atom->body >> 32;
	const uint32_t frac = (uint64_t)atom->body & 0xffffffff;
	const double part = frac * 0x1p-32;

	if(sec <= 1UL)
	{
		sprintf(ptr, TYPE(" t:", "immediate"));
	}
	else
	{
		const time_t ttime = sec - 0x83aa7e80;
		const struct tm *ltime = localtime(&ttime);

		char tmp [32];
		strftime(tmp, 32, "%d-%b-%Y %T", ltime);

		sprintf(ptr, TYPE(" t:", "%s.%06"PRIu32), tmp, (uint32_t)(part*1e6));
	}

	return ptr + strlen(ptr);
}

static inline char *
_atom_stringify(UI *ui, char *ptr, char *end, const LV2_Atom *atom)
{
	//FIXME check for buffer overflows!!!
109 110 111 112 113 114 115 116 117
	const LV2_Atom_Object *obj = (const LV2_Atom_Object *)atom;

	if(osc_atom_is_message(&ui->oforge, obj))
	{
		const LV2_Atom_String *path = NULL;
		const LV2_Atom_String *fmt = NULL;
		const LV2_Atom_Tuple *tup = NULL;
		osc_atom_message_unpack(&ui->oforge, obj, &path, &fmt, &tup);

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
		sprintf(ptr, CODE_PRE);
		ptr += strlen(ptr);

		if(path && fmt)
		{
			sprintf(ptr, PATH(" %s"), LV2_ATOM_BODY_CONST(path));
		}
		else
		{
			sprintf(ptr, " unknown path and/or format string");
		}
		ptr += strlen(ptr);

		const LV2_Atom *itm = lv2_atom_tuple_begin(tup);
		for(const char *type = LV2_ATOM_BODY_CONST(fmt); *type; type++)
		{
			bool advance = true;

			switch(*type)
			{
				case 'i':
				{
					if(itm->type == ui->forge.Int)
					{
						sprintf(ptr, TYPE(" i:", "%"PRIi32), ((const LV2_Atom_Int *)itm)->body);
						ptr += strlen(ptr);
					}
					break;
				}
				case 'f':
				{
					if(itm->type == ui->forge.Float)
					{
						sprintf(ptr, TYPE(" f:", "%f"), ((const LV2_Atom_Float *)itm)->body);
						ptr += strlen(ptr);
					}
					break;
				}
				case 's': // fall-through
				case 'S':
				{
					if(itm->type == ui->forge.String)
					{
161 162 163 164
						const char *str = LV2_ATOM_BODY_CONST(itm);
						if(itm->size == 0)
							str = "";
						sprintf(ptr, TYPE(" s:", "%s"), str);
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
						ptr += strlen(ptr);
					}
					break;
				}
				case 'b':
				{
					if(itm->type == ui->forge.Chunk)
					{
						const uint8_t *chunk = LV2_ATOM_BODY_CONST(itm);
						sprintf(ptr, TYPE_PRE(" b:", PUNKT("[")));
						ptr += strlen(ptr);
						if(itm->size)
						{
							sprintf(ptr, "%02"PRIX8, chunk[0]);
							ptr += strlen(ptr);

							for(unsigned i=1; i<itm->size; i++)
							{
								sprintf(ptr, " %02"PRIX8, chunk[i]);
								ptr += strlen(ptr);
							}
						}
						sprintf(ptr, TYPE_POST(PUNKT("]")));
						ptr += strlen(ptr);
					}
					break;
				}

				case 'h':
				{
					if(itm->type == ui->forge.Long)
					{
						sprintf(ptr, TYPE(" h:", "%"PRIi64), ((const LV2_Atom_Long *)itm)->body);
						ptr += strlen(ptr);
					}
					break;
				}
				case 'd':
				{
					if(itm->type == ui->forge.Double)
					{
						sprintf(ptr, TYPE(" d:", "%lf"), ((const LV2_Atom_Double *)itm)->body);
						ptr += strlen(ptr);
					}
					break;
				}
				case 't':
				{
					if(itm->type == ui->forge.Long)
					{
						ptr = _timestamp_stringify(ui, ptr, end, (const LV2_Atom_Long *)itm);
					}
					break;
				}

				case 'c':
				{
					//if(itm->type == ui->forge.Char)
					{
						sprintf(ptr, TYPE(" c:", "%c"), ((const LV2_Atom_Int *)itm)->body);
						ptr += strlen(ptr);
					}
					break;
				}
				case 'm':
				{
					if(itm->type == ui->midi_event)
					{
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
						const uint8_t *chunk = LV2_ATOM_BODY_CONST(itm);
						sprintf(ptr, TYPE_PRE(" m:", PUNKT("[")));
						ptr += strlen(ptr);
						if(itm->size)
						{
							sprintf(ptr, "%02"PRIX8, chunk[0]);
							ptr += strlen(ptr);

							for(unsigned i=1; i<itm->size; i++)
							{
								sprintf(ptr, " %02"PRIX8, chunk[i]);
								ptr += strlen(ptr);
							}
						}
						sprintf(ptr, TYPE_POST(PUNKT("]")));
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
						ptr += strlen(ptr);
					}
					break;
				}

				case 'T':
				{
					//if(itm->type == ui->forge.Bool)
					{
						sprintf(ptr, TYPE(" T:", "true"));
						ptr += strlen(ptr);
					}
					advance = false;
					break;
				}
				case 'F':
				{
					//if(itm->type == ui->forge.Bool)
					{
						sprintf(ptr, TYPE(" F:", "false"));
						ptr += strlen(ptr);
					}
					advance = false;
					break;
				}
				case 'N':
				{
					//if(itm->type == 0)
					{
						sprintf(ptr, TYPE(" N:", "nil"));
						ptr += strlen(ptr);
					}
					advance = false;
					break;
				}
				case 'I':
				{
					//if(itm->type == ui->forge.Impulse)
					{
						sprintf(ptr, TYPE(" I:", "impulse"));
						ptr += strlen(ptr);
					}
					advance = false;
					break;
				}

				default:
				{
					{
						sprintf(ptr, TYPE(" %c:", "unknown"), *type);
						ptr += strlen(ptr);
					}
					break;
				}
			}

			if(advance && !lv2_atom_tuple_is_end(LV2_ATOM_BODY(tup), tup->atom.size, itm))
				itm = lv2_atom_tuple_next(itm);
		}

		sprintf(ptr, CODE_POST);

		return ptr + strlen(ptr);
311 312 313
	}
	else if(osc_atom_is_bundle(&ui->oforge, obj))
	{
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
		const LV2_Atom_Long *timestamp = NULL;
		const LV2_Atom_Tuple *tup = NULL;
		osc_atom_bundle_unpack(&ui->oforge, obj, &timestamp, &tup);
		
		sprintf(ptr, CODE_PRE);
		ptr += strlen(ptr);

		sprintf(ptr, BUNDLE(" #bundle"));
		ptr += strlen(ptr);

		ptr = _timestamp_stringify(ui, ptr, end, timestamp);

		sprintf(ptr, CODE_POST);

		return ptr + strlen(ptr);
329 330 331 332 333 334
	}

	return NULL;
}

static char *
335
_packet_label_get(void *data, Evas_Object *obj, const char *part)
336 337 338 339 340 341 342 343 344
{
	UI *ui = evas_object_data_get(obj, "ui");
	const LV2_Atom_Event *ev = data;

	if(!ui)
		return NULL;

	if(!strcmp(part, "elm.text"))
	{
345 346 347 348 349 350 351 352 353
		char *buf = ui->string_buf;
		char *ptr = buf;
		char *end = buf + STRING_BUF_SIZE;

		ptr = _atom_stringify(ui, ptr, end, &ev->body);

		return ptr
			? strdup(buf)
			: NULL;
354 355 356 357 358 359
	}

	return NULL;
}

static Evas_Object * 
360
_packet_content_get(void *data, Evas_Object *obj, const char *part)
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
{
	UI *ui = evas_object_data_get(obj, "ui");
	const LV2_Atom_Event *ev = data;
	
	if(!ui)
		return NULL;

	if(!strcmp(part, "elm.swallow.icon"))
	{
		char *buf = ui->string_buf;

		sprintf(buf, "<color=#bb0 font=Mono>%04ld</color>", ev->time.frames);

		Evas_Object *label = elm_label_add(obj);
		if(label)
		{
			elm_object_part_text_set(label, "default", buf);
			evas_object_show(label);
		}

		return label;
	}
	else if(!strcmp(part, "elm.swallow.end"))
	{
		char *buf = ui->string_buf;

		sprintf(buf, "<color=#0bb font=Mono>%4u</color>", ev->body.size);

		Evas_Object *label = elm_label_add(obj);
		if(label)
		{
			elm_object_part_text_set(label, "default", buf);
			evas_object_show(label);
		}

		return label;
	}

	return NULL;
}

static void
403
_del(void *data, Evas_Object *obj)
404 405 406 407
{
	free(data);
}

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
static char *
_item_label_get(void *data, Evas_Object *obj, const char *part)
{
	UI *ui = evas_object_data_get(obj, "ui");
	const LV2_Atom *atom = data;

	if(!ui)
		return NULL;

	if(!strcmp(part, "elm.text"))
	{
		char *buf = ui->string_buf;
		char *ptr = buf;
		char *end = buf + STRING_BUF_SIZE;

		ptr = _atom_stringify(ui, ptr, end, atom);

		return ptr
			? strdup(buf)
			: NULL;
	}

	return NULL;
}

static Evas_Object * 
_item_content_get(void *data, Evas_Object *obj, const char *part)
{
	UI *ui = evas_object_data_get(obj, "ui");
	const LV2_Atom *atom = data;
	
	if(!ui)
		return NULL;

	if(!strcmp(part, "elm.swallow.end"))
	{
		char *buf = ui->string_buf;

		sprintf(buf, "<color=#0bb font=Mono>%4u</color>", atom->size);

		Evas_Object *label = elm_label_add(obj);
		if(label)
		{
			elm_object_part_text_set(label, "default", buf);
			evas_object_show(label);
		}

		return label;
	}

	return NULL;
}

static void
_item_expand_request(void *data, Evas_Object *obj, void *event_info)
{
	Elm_Object_Item *itm = event_info;
	UI *ui = data;

	elm_genlist_item_expanded_set(itm, EINA_TRUE);
}

static void
_item_contract_request(void *data, Evas_Object *obj, void *event_info)
{
	Elm_Object_Item *itm = event_info;
	UI *ui = data;

	elm_genlist_item_expanded_set(itm, EINA_FALSE);
}

static void
_item_expanded(void *data, Evas_Object *obj, void *event_info)
{
	Elm_Object_Item *itm = event_info;
	UI *ui = data;

	const Elm_Genlist_Item_Class *class = elm_genlist_item_item_class_get(itm);
	const void *udata = elm_object_item_data_get(itm);

	if(!udata)
		return;

	const LV2_Atom_Object *_obj = NULL;

	if(class == ui->itc_packet)
	{
		const LV2_Atom_Event *ev = udata;
		_obj = (const LV2_Atom_Object *)&ev->body;
	}
	else if(class == ui->itc_item)
	{
		_obj = udata;
	}

	if(_obj && osc_atom_is_bundle(&ui->oforge, _obj))
	{
		const LV2_Atom_Long *timestamp = NULL;
		const LV2_Atom_Tuple *tup = NULL;
		osc_atom_bundle_unpack(&ui->oforge, _obj, &timestamp, &tup);

		if(tup)
		{
			LV2_ATOM_TUPLE_FOREACH(tup, atom)
			{
				const LV2_Atom_Object *_obj2 = (const LV2_Atom_Object *)atom;

				Elm_Object_Item *itm2 = elm_genlist_item_append(obj, ui->itc_item,
					_obj2, itm, ELM_GENLIST_ITEM_TREE, NULL, NULL);
				elm_genlist_item_select_mode_set(itm2, ELM_OBJECT_SELECT_MODE_DEFAULT);
				if(osc_atom_is_bundle(&ui->oforge, _obj2))
					elm_genlist_item_expanded_set(itm2, EINA_TRUE);
			}
		}
	}
}

static void
_item_contracted(void *data, Evas_Object *obj, void *event_info)
{
	Elm_Object_Item *itm = event_info;
	UI *ui = data;

	elm_genlist_item_subitems_clear(itm);
}

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
static Evas_Object * 
_group_item_content_get(void *data, Evas_Object *obj, const char *part)
{
	UI *ui = evas_object_data_get(obj, "ui");
	const position_t *pos = data;

	if(!ui)
		return NULL;

	if(!strcmp(part, "elm.swallow.icon"))
	{
		char *buf = ui->string_buf;

		sprintf(buf, "<color=#000 font=Mono>0x%"PRIx64"</color>", pos->offset);

		Evas_Object *label = elm_label_add(obj);
		if(label)
		{
			elm_object_part_text_set(label, "default", buf);
			evas_object_show(label);
		}

		return label;
	}
	else if(!strcmp(part, "elm.swallow.end"))
	{
		char *buf = ui->string_buf;

		sprintf(buf, "<color=#0bb font=Mono>%"PRIu32"</color>", pos->nsamples);

		Evas_Object *label = elm_label_add(obj);
		if(label)
		{
			elm_object_part_text_set(label, "default", buf);
			evas_object_show(label);
		}

		return label;
	}

	return NULL;
}

577 578 579 580 581 582 583
static void
_clear_update(UI *ui, int count)
{
	if(!ui->clear)
		return;

	char *buf = ui->string_buf;
584
	sprintf(buf, "Clear (%"PRIi32" of %"PRIi32")", count, COUNT_MAX);
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
	elm_object_text_set(ui->clear, buf);
}

static void
_clear_clicked(void *data, Evas_Object *obj, void *event_info)
{
	UI *ui = data;

	if(ui->list)
		elm_genlist_clear(ui->list);

	_clear_update(ui, 0);			
}

static void
_info_clicked(void *data, Evas_Object *obj, void *event_info)
{
	UI *ui = data;

	// toggle popup
	if(ui->popup)
	{
		if(evas_object_visible_get(ui->popup))
			evas_object_hide(ui->popup);
		else
			evas_object_show(ui->popup);
	}
}

614 615
static void
_content_free(void *data, Evas *e, Evas_Object *obj, void *event_info)
616
{
617 618 619 620 621 622 623 624 625
	UI *ui = data;

	ui->widget = NULL;
}

static void
_content_del(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
	UI *ui = data;
626

627 628 629 630 631 632 633
	evas_object_del(ui->widget);
}

static Evas_Object *
_content_get(UI *ui, Evas_Object *parent)
{
	ui->table = elm_table_add(parent);
634 635 636 637
	if(ui->table)
	{
		elm_table_homogeneous_set(ui->table, EINA_FALSE);
		elm_table_padding_set(ui->table, 0, 0);
638 639 640
		evas_object_size_hint_min_set(ui->table, 600, 800);
		evas_object_event_callback_add(ui->table, EVAS_CALLBACK_FREE, _content_free, ui);
		evas_object_event_callback_add(ui->table, EVAS_CALLBACK_DEL, _content_del, ui);
641 642 643 644 645 646 647 648

		ui->list = elm_genlist_add(ui->table);
		if(ui->list)
		{
			elm_genlist_select_mode_set(ui->list, ELM_OBJECT_SELECT_MODE_DEFAULT);
			elm_genlist_homogeneous_set(ui->list, EINA_FALSE); // TRUE for lazy-loading
			elm_genlist_mode_set(ui->list, ELM_LIST_SCROLL);
			evas_object_data_set(ui->list, "ui", ui);
649 650 651 652 653 654
			evas_object_smart_callback_add(ui->list, "expand,request",
				_item_expand_request, ui);
			evas_object_smart_callback_add(ui->list, "contract,request",
				_item_contract_request, ui);
			evas_object_smart_callback_add(ui->list, "expanded", _item_expanded, ui);
			evas_object_smart_callback_add(ui->list, "contracted", _item_contracted, ui);
655 656 657
			evas_object_size_hint_weight_set(ui->list, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
			evas_object_size_hint_align_set(ui->list, EVAS_HINT_FILL, EVAS_HINT_FILL);
			evas_object_show(ui->list);
658
			elm_table_pack(ui->table, ui->list, 0, 0, 4, 1);
659 660 661 662 663 664 665 666 667 668 669 670 671
		}

		ui->clear = elm_button_add(ui->table);
		if(ui->clear)
		{
			_clear_update(ui, 0);
			evas_object_smart_callback_add(ui->clear, "clicked", _clear_clicked, ui);
			evas_object_size_hint_weight_set(ui->clear, EVAS_HINT_EXPAND, 0.f);
			evas_object_size_hint_align_set(ui->clear, EVAS_HINT_FILL, EVAS_HINT_FILL);
			evas_object_show(ui->clear);
			elm_table_pack(ui->table, ui->clear, 0, 1, 1, 1);
		}

672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
		ui->autoclear = elm_check_add(ui->table);
		if(ui->autoclear)
		{
			elm_object_text_set(ui->autoclear, "overwrite");
			evas_object_size_hint_weight_set(ui->autoclear, 0.f, 0.f);
			evas_object_size_hint_align_set(ui->autoclear, EVAS_HINT_FILL, EVAS_HINT_FILL);
			evas_object_show(ui->autoclear);
			elm_table_pack(ui->table, ui->autoclear, 1, 1, 1, 1);
		}

		ui->autoblock = elm_check_add(ui->table);
		if(ui->autoblock)
		{
			elm_object_text_set(ui->autoblock, "block");
			evas_object_size_hint_weight_set(ui->autoblock, 0.f, 0.f);
			evas_object_size_hint_align_set(ui->autoblock, EVAS_HINT_FILL, EVAS_HINT_FILL);
			evas_object_show(ui->autoblock);
			elm_table_pack(ui->table, ui->autoblock, 2, 1, 1, 1);
		}

692 693 694 695 696 697 698
		Evas_Object *info = elm_button_add(ui->table);
		if(info)
		{
			evas_object_smart_callback_add(info, "clicked", _info_clicked, ui);
			evas_object_size_hint_weight_set(info, 0.f, 0.f);
			evas_object_size_hint_align_set(info, 1.f, EVAS_HINT_FILL);
			evas_object_show(info);
699
			elm_table_pack(ui->table, info, 3, 1, 1, 1);
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
				
			Evas_Object *icon = elm_icon_add(info);
			if(icon)
			{
				elm_image_file_set(icon, ui->logo_path, NULL);
				evas_object_size_hint_min_set(icon, 20, 20);
				evas_object_size_hint_max_set(icon, 32, 32);
				//evas_object_size_hint_aspect_set(icon, EVAS_ASPECT_CONTROL_BOTH, 1, 1);
				evas_object_show(icon);
				elm_object_part_content_set(info, "icon", icon);
			}
		}

		ui->popup = elm_popup_add(ui->table);
		if(ui->popup)
		{
			elm_popup_allow_events_set(ui->popup, EINA_TRUE);

			Evas_Object *hbox = elm_box_add(ui->popup);
			if(hbox)
			{
				elm_box_horizontal_set(hbox, EINA_TRUE);
				elm_box_homogeneous_set(hbox, EINA_FALSE);
				elm_box_padding_set(hbox, 10, 0);
				evas_object_show(hbox);
				elm_object_content_set(ui->popup, hbox);

				Evas_Object *icon = elm_icon_add(hbox);
				if(icon)
				{
					elm_image_file_set(icon, ui->logo_path, NULL);
					evas_object_size_hint_min_set(icon, 128, 128);
					evas_object_size_hint_max_set(icon, 256, 256);
					evas_object_size_hint_aspect_set(icon, EVAS_ASPECT_CONTROL_BOTH, 1, 1);
					evas_object_show(icon);
					elm_box_pack_end(hbox, icon);
				}

				Evas_Object *label = elm_label_add(hbox);
				if(label)
				{
					elm_object_text_set(label,
						"<color=#b00 shadow_color=#fff font_size=20>"
						"Sherlock - OSC Inspector"
						"</color></br><align=left>"
						"Version "SHERLOCK_VERSION"</br></br>"
						"Copyright (c) 2015 Hanspeter Portner</br></br>"
						"This is free and libre software</br>"
						"Released under Artistic License 2.0</br>"
						"By Open Music Kontrollers</br></br>"
						"<color=#bbb>"
						"http://open-music-kontrollers.ch/lv2/sherlock</br>"
						"dev@open-music-kontrollers.ch"
						"</color></align>");

					evas_object_show(label);
					elm_box_pack_end(hbox, label);
				}
			}
		}
	}

	return ui->table;
}

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)
{
	if(strcmp(plugin_uri, SHERLOCK_OSC_INSPECTOR_URI))
		return NULL;

	UI *ui = calloc(1, sizeof(UI));
	if(!ui)
		return NULL;

	ui->write_function = write_function;
	ui->controller = controller;
780 781

	Evas_Object *parent = NULL;
782 783 784
	for(int i=0; features[i]; i++)
	{
		if(!strcmp(features[i]->URI, LV2_URID__map))
785 786 787
			ui->map = features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_UI__parent))
			parent = features[i]->data;
788 789 790 791 792 793 794 795
  }

	if(!ui->map)
	{
		fprintf(stderr, "LV2 URID extension not supported\n");
		free(ui);
		return NULL;
	}
796 797 798 799 800
	if(!parent)
	{
		free(ui);
		return NULL;
	}
801 802
	
	ui->event_transfer = ui->map->map(ui->map->handle, LV2_ATOM__eventTransfer);
803
	ui->midi_event = ui->map->map(ui->map->handle, LV2_MIDI__MidiEvent);
804 805 806
	lv2_atom_forge_init(&ui->forge, ui->map);
	osc_forge_init(&ui->oforge, ui->map);

807 808
	ui->itc_packet = elm_genlist_item_class_new();
	if(ui->itc_packet)
809
	{
810 811 812 813
		ui->itc_packet->item_style = "default_style";
		ui->itc_packet->func.text_get = _packet_label_get;
		ui->itc_packet->func.content_get = _packet_content_get;
		ui->itc_packet->func.state_get = NULL;
814
		ui->itc_packet->func.del = _del;
815 816 817 818 819 820 821 822 823 824
	}

	ui->itc_item = elm_genlist_item_class_new();
	if(ui->itc_item)
	{
		ui->itc_item->item_style = "default_style";
		ui->itc_item->func.text_get = _item_label_get;
		ui->itc_item->func.content_get = _item_content_get;
		ui->itc_item->func.state_get = NULL;
		ui->itc_item->func.del = NULL;
825 826
	}

827 828 829 830 831 832 833 834 835 836
	ui->itc_group = elm_genlist_item_class_new();
	if(ui->itc_group)
	{
		ui->itc_group->item_style = "default_style";
		ui->itc_group->func.text_get = NULL;
		ui->itc_group->func.content_get = _group_item_content_get;
		ui->itc_group->func.state_get = NULL;
		ui->itc_group->func.del = _del;
	}

837 838 839
	sprintf(ui->string_buf, "%s/omk_logo_256x256.png", bundle_path);
	ui->logo_path = strdup(ui->string_buf);

840 841
	ui->widget = _content_get(ui, parent);
	if(!ui->widget)
842 843 844 845
	{
		free(ui);
		return NULL;
	}
846
	*(Evas_Object **)widget = ui->widget;
847 848 849 850 851 852 853 854 855
	
	return ui;
}

static void
cleanup(LV2UI_Handle handle)
{
	UI *ui = handle;

856 857
	if(ui->widget)
		evas_object_del(ui->widget);
858 859 860
	if(ui->logo_path)
		free(ui->logo_path);

861 862 863 864
	if(ui->itc_packet)
		elm_genlist_item_class_free(ui->itc_packet);
	if(ui->itc_item)
		elm_genlist_item_class_free(ui->itc_item);
865 866 867 868 869 870 871 872 873 874 875 876

	free(ui);
}

static void
port_event(LV2UI_Handle handle, uint32_t i, uint32_t size, uint32_t urid,
	const void *buf)
{
	UI *ui = handle;
			
	if( (i == 2) && (urid == ui->event_transfer) && ui->list)
	{
877 878 879 880
		const LV2_Atom_Tuple *tup = buf;
		const LV2_Atom_Long *offset = (const LV2_Atom_Long *)lv2_atom_tuple_begin(tup);
		const LV2_Atom_Int *nsamples = (const LV2_Atom_Int *)lv2_atom_tuple_next(&offset->atom);
		const LV2_Atom_Sequence *seq = (const LV2_Atom_Sequence *)lv2_atom_tuple_next(&nsamples->atom);
881 882
		int n = elm_genlist_items_count(ui->list);

883 884 885 886
		Elm_Object_Item *itm = NULL;
		if(seq->atom.size > sizeof(LV2_Atom_Sequence_Body)) // there are events
		{
			position_t *pos = malloc(sizeof(position_t));
887 888
			if(!pos)
				return;
889

890 891
			pos->offset = offset->body;
			pos->nsamples = nsamples->body;
892 893

			// check item count 
894 895 896 897 898 899 900 901 902
			if(n + 1 > COUNT_MAX)
			{
				if(elm_check_state_get(ui->autoclear))
				{
					elm_genlist_clear(ui->list);
					n = 0;
				}
				else
				{
903
					return;
904 905 906 907
				}
			}
			else if(elm_check_state_get(ui->autoblock))
			{
908
				return;
909
			}
910

911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
			LV2_ATOM_SEQUENCE_FOREACH(seq, elmnt)
			{
				size_t len = sizeof(LV2_Atom_Event) + elmnt->body.size;
				LV2_Atom_Event *ev = malloc(len);
				if(!ev)
					continue;

				memcpy(ev, elmnt, len);

				const LV2_Atom_Object *obj = (const LV2_Atom_Object *)&ev->body;
				Elm_Object_Item *itm2 = elm_genlist_item_append(ui->list, ui->itc_packet,
					ev, itm, ELM_GENLIST_ITEM_TREE, NULL, NULL);
				elm_genlist_item_select_mode_set(itm2, ELM_OBJECT_SELECT_MODE_DEFAULT);
				elm_genlist_item_expanded_set(itm2, EINA_FALSE);
				n++;
				
				// scroll to last item
				//elm_genlist_item_show(itm, ELM_GENLIST_ITEM_SCROLLTO_MIDDLE);
			}

			itm = elm_genlist_item_append(ui->list, ui->itc_group,
				pos, NULL, ELM_GENLIST_ITEM_GROUP, NULL, NULL);
			elm_genlist_item_select_mode_set(itm, ELM_OBJECT_SELECT_MODE_NONE);
934
		}
935

936 937 938 939 940 941 942 943 944 945
		if(seq->atom.size > sizeof(LV2_Atom_Sequence_Body))
			_clear_update(ui, n); // only update if there where any events
	}
}

const LV2UI_Descriptor osc_inspector_eo = {
	.URI						= SHERLOCK_OSC_INSPECTOR_EO_URI,
	.instantiate		= instantiate,
	.cleanup				= cleanup,
	.port_event			= port_event,
946
	.extension_data	= NULL
947
};