aboutsummaryrefslogtreecommitdiff
path: root/tracker_sequencer_ui.c
blob: a2f094a6aa8eb407a08c461da8d446f9b322dcc5 (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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
/*
 * 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 <math.h>
#include <inttypes.h>

#include <tracker.h>

#include <d2tk/frontend_pugl.h>

#define MAX_COLS 128 //FIXME
#define MAX_ROWS 1024 //FIXME

typedef struct _row_t row_t;
typedef struct _plughandle_t plughandle_t;

struct _row_t {
	bool note [MAX_COLS];
};

struct _plughandle_t {
	LV2_URID_Map *map;
	LV2_URID_Unmap *unmap;
	LV2_Atom_Forge forge;

	LV2_URID atom_eventTransfer;
	LV2_URID atom_beatTime;
	LV2_URID midi_MidiEvent;
	LV2_URID tracker_offset;
	LV2_URID tracker_beats;
	LV2_URID tracker_fractions;
	LV2_URID tracker_repeats;
	LV2_URID tracker_position;
	LV2_URID tracker_mode;
	LV2_URID tracker_graph;

	LV2_Log_Log *log;
	LV2_Log_Logger logger;

	d2tk_pugl_config_t config;
	d2tk_pugl_t *dpugl;

	LV2UI_Controller *controller;
	LV2UI_Write_Function writer;

	PROPS_T(props, MAX_NPROPS);
	plugstate_t state;
	plugstate_t stash;
	uint32_t mode_size;
	uint32_t graph_size;

	row_t matrix [MAX_ROWS];

	unsigned position;

	atom_ser_t ser;

	float sample_rate;

	bool is_down;
	unsigned J;
	unsigned I;
};

static void
_set_property(plughandle_t *handle, LV2_URID property)
{
	atom_ser_t *ser = &handle->ser;
	ser->offset = 0;
	lv2_atom_forge_set_sink(&handle->forge, _sink, _deref, ser);

	LV2_Atom_Forge_Ref ref = 1;
	props_set(&handle->props, &handle->forge, 0, property, &ref);

	const LV2_Atom_Event *ev = ser->ev;
	const LV2_Atom *atom = &ev->body;
	handle->writer(handle->controller, 0, lv2_atom_total_size(atom),
		handle->atom_eventTransfer, atom);
}

static void
_save_state(plughandle_t *handle)
{
	LV2_Atom_Forge *forge = &handle->forge;
	LV2_Atom_Forge_Frame tup_frame;
	LV2_Atom_Forge_Frame seq_frame;

	atom_ser_t *ser = &handle->ser;

	// serialize graph
	ser->offset = 0;
	lv2_atom_forge_set_sink(forge, _sink, _deref, ser);
	lv2_atom_forge_tuple(forge, &tup_frame);
	lv2_atom_forge_sequence_head(forge, &seq_frame, handle->atom_beatTime);
	{
		for(int j = 0; j < MAX_ROWS; j++)
		{
			// first handle note on
			for(int i = 0; i < MAX_COLS; i++)
			{
				const bool prev = (j == 0)
					? false
					: handle->matrix[j-1].note[i];

				if(handle->matrix[j].note[i] && !prev)
				{
					const uint8_t msg [3] = {
						LV2_MIDI_MSG_NOTE_ON | 0x0,
						i,
						0x7f
					};
					double beat_time = (j + 0.0) / handle->state.fractions;

					lv2_atom_forge_beat_time(forge, beat_time);
					lv2_atom_forge_atom(forge, sizeof(msg), handle->midi_MidiEvent);
					lv2_atom_forge_write(forge, msg, sizeof(msg));
					lv2_log_note(&handle->logger, "adding NoteOn: %lf\n", beat_time);
				}
			}

			// then handle note off
			for(int i = 0; i < MAX_COLS; i++)
			{
				const bool next = (j == MAX_ROWS - 1)
					? false
					: handle->matrix[j+1].note[i];

				if(handle->matrix[j].note[i] && !next)
				{
					const uint8_t msg [3] = {
						LV2_MIDI_MSG_NOTE_OFF | 0x0,
						i,
						0x0
					};
					double beat_time = (j + 1 - 1.0/handle->sample_rate) / handle->state.fractions;

					lv2_atom_forge_beat_time(forge, beat_time);
					lv2_atom_forge_atom(forge, sizeof(msg), handle->midi_MidiEvent);
					lv2_atom_forge_write(forge, msg, sizeof(msg));
					lv2_log_note(&handle->logger, "adding NoteOff: %lf\n", beat_time);
				}
			}
		}
	}
	lv2_atom_forge_pop(forge, &seq_frame);
	lv2_atom_forge_pop(forge, &tup_frame);

	const LV2_Atom *atom = ser->atom;
	if(atom->size <= GRAPH_SIZE)
	{
		//FIXME
		props_impl_t *impl = _props_impl_get(&handle->props, handle->tracker_graph);
		if(impl)
		{
			impl->value.size = atom->size;
			memcpy(handle->state.graph, LV2_ATOM_BODY_CONST(atom), atom->size);
		}
	}

	// send all patch:Set messages
	_set_property(handle, handle->tracker_offset);
	_set_property(handle, handle->tracker_beats);
	_set_property(handle, handle->tracker_fractions);
	_set_property(handle, handle->tracker_repeats);
	_set_property(handle, handle->tracker_mode);
	_set_property(handle, handle->tracker_graph);
}

static void
_restore_state(plughandle_t *handle)
{
	atom_ser_t *ser = &handle->ser;
	ser->offset = 0;
	lv2_atom_forge_set_sink(&handle->forge, _sink, _deref, ser);

	LV2_Atom_Forge_Frame frame;
	lv2_atom_forge_object(&handle->forge, &frame, 0, handle->props.urid.patch_get);
	lv2_atom_forge_pop(&handle->forge, &frame);

	const LV2_Atom *atom = ser->atom;
	handle->writer(handle->controller, 0, lv2_atom_total_size(atom),
		handle->atom_eventTransfer, atom);
}

static void
_load_state(plughandle_t *handle, uint32_t size, const void *body)
{
	// clear matrix
	memset(handle->matrix, 0x0, sizeof(handle->matrix));

	LV2_ATOM_TUPLE_BODY_FOREACH(body, size, item)
	{
		const LV2_Atom_Sequence *seq = (const LV2_Atom_Sequence *)item;

		LV2_ATOM_SEQUENCE_FOREACH(seq, ev)
		{
			const LV2_Atom *atom = &ev->body;

			const int j = round(ev->time.beats * handle->state.fractions);

			if( (atom->size == 3) && (atom->type == handle->midi_MidiEvent) )
			{
				const uint8_t *msg = LV2_ATOM_BODY_CONST(atom);

				const uint8_t cmd = msg[0] & 0xf0;
				//const uint8_t cha = msg[0] & 0x0f;

				switch(cmd)
				{
					case LV2_MIDI_MSG_NOTE_ON:
					{
						const uint8_t note = msg[1];
						const uint8_t vel = msg[2];
						const int i = note;

						handle->matrix[j].note[i] = true;
					} break;
					case LV2_MIDI_MSG_NOTE_OFF:
					{
						const uint8_t note = msg[1];
						const uint8_t vel = msg[2];
						const int i = note;

						for(int k = j-1; k >= 0; k--)
						{
							if(handle->matrix[k].note[i]) // found NoteOn //FIXME
							{
								break;
							}

							handle->matrix[k].note[i] = true; //FIXME
						}
					} break;
				}
			}
		}
	}
}

static void
_intercept_position(void *data, int64_t frames, props_impl_t *impl)
{
	plughandle_t *handle = data;

	const unsigned position = handle->state.position * handle->state.fractions;

	if(position != handle->position)
	{
		d2tk_pugl_redisplay(handle->dpugl);
		handle->position = position;
	}
}

static void
_intercept_graph(void *data, int64_t frames, props_impl_t *impl)
{
	plughandle_t *handle = data;

	handle->graph_size = impl->value.size;
	_load_state(handle, handle->graph_size, handle->state.graph);
}

static const props_def_t defs [MAX_NPROPS] = {
	{
		.property = TRACKER__offset,
		.offset = offsetof(plugstate_t, offset),
		.type = LV2_ATOM__Int
	},
	{
		.property = TRACKER__beats,
		.offset = offsetof(plugstate_t, beats),
		.type = LV2_ATOM__Int,
		//FIXME .event_cb = _intercept
	},
	{
		.property = TRACKER__fractions,
		.offset = offsetof(plugstate_t, fractions),
		.type = LV2_ATOM__Int,
		//FIXME .event_cb = _intercept
	},
	{
		.property = TRACKER__repeats,
		.offset = offsetof(plugstate_t, repeats),
		.type = LV2_ATOM__Int
	},
	{
		.property = TRACKER__position,
		.offset = offsetof(plugstate_t, position),
		.type = LV2_ATOM__Double,
		.event_cb = _intercept_position
	},
	{
		.property = TRACKER__mode,
		.offset = offsetof(plugstate_t, mode),
		.type = LV2_ATOM__Tuple,
		.max_size = MODE_SIZE,
		//FIXME .event_cb = _intercept_mode
	},
	{
		.property = TRACKER__graph,
		.offset = offsetof(plugstate_t, graph),
		.type = LV2_ATOM__Tuple,
		.max_size = GRAPH_SIZE,
		.event_cb = _intercept_graph
	}
};

static const char *note_lbl [12] = {
	"C", "#",
	"D", "#",
	"E",
	"F", "#",
	"G", "#",
	"A", "#",
	"B"
};

static const char *octave_lbl [11] = {
	"-1", "+0", "+1", "+2", "+3", "+4", "+5", "+6", "+7", "+8", "+9"
};

static inline void
_render_seq(plughandle_t *handle, const d2tk_rect_t *rect)
{
	const int N = MAX_COLS;
	const int M = MAX_ROWS;
	const int R = handle->state.beats * handle->state.fractions;
	const int NO = rect->w / 16;
	const int MO = NO * rect->h / rect->w;
	d2tk_pugl_t *dpugl = handle->dpugl;
	d2tk_base_t *base = d2tk_pugl_get_base(dpugl);
	bool state_changed = false;

	D2TK_BASE_SCROLLBAR(base, rect, D2TK_ID, D2TK_FLAG_SCROLL,
		N, R, NO, MO, scrollbar)
	{
		const d2tk_rect_t *sub = d2tk_scrollbar_get_rect(scrollbar);
		const float offset_x = d2tk_scrollbar_get_offset_x(scrollbar);
		const float offset_y = d2tk_scrollbar_get_offset_y(scrollbar);

		d2tk_style_t style = *d2tk_base_get_default_style();
		style.border_width = 1;
		style.padding = 0;
		style.rounding = 0;
		d2tk_base_set_style(base, &style);

		D2TK_BASE_TABLE(sub, NO, MO, tab)
		{
			const int k = d2tk_table_get_index(tab);
			const int x = d2tk_table_get_index_x(tab);
			const int y = d2tk_table_get_index_y(tab);
			const d2tk_rect_t *bnd = d2tk_table_get_rect(tab);
			const d2tk_id_t id = D2TK_ID_IDX(k);

			if( (x <= 1) && (y <= 1) )
			{
				continue;
			}

			const int i = x - 2 + offset_x;
			const int j = y - 2 + offset_y;

			if( (j > M ) || (j >= R) )
			{
				break;
			}

			if(y == 0)
			{
				if( (i % 12) == 0)
				{
					const char *lbl = octave_lbl[i / 12];
					d2tk_base_label(base, -1, lbl, 0.7f, bnd, D2TK_ALIGN_CENTERED);
				}

				continue;
			}
			else if(y == 1)
			{
				const char *lbl = note_lbl[i % 12];
				d2tk_base_label(base, -1, lbl, 1.f, bnd, D2TK_ALIGN_CENTERED);

				continue;
			}

			if(x == 0)
			{
				if( (j % handle->state.fractions) == 0)
				{
					char lbl [4];
					const size_t lbl_len = snprintf(lbl, sizeof(lbl), "%03i", (j / handle->state.fractions + handle->state.offset) % 1000);
					d2tk_base_label(base, lbl_len, lbl, 0.7f, bnd, D2TK_ALIGN_RIGHT | D2TK_ALIGN_MIDDLE);
				}

				continue;
			}
			else if(x == 1)
			{
				char lbl [3];
				const size_t lbl_len = snprintf(lbl, sizeof(lbl), "%i", j % handle->state.fractions + 1);
				d2tk_base_label(base, lbl_len, lbl, 1.f, bnd, D2TK_ALIGN_CENTERED);

				continue;
			}

			const bool I = (i / 12) % 2;
			const bool J = (j / handle->state.fractions) % 2;

			const uint32_t fill_color_active = style.fill_color[D2TK_TRIPLE_ACTIVE];
			const uint32_t fill_color_active_hot = style.fill_color[D2TK_TRIPLE_ACTIVE_HOT];

			if(j == round(handle->state.position * handle->state.fractions) )
			{
				style.fill_color[D2TK_TRIPLE_NONE] = 0x5f5f5fff;
				style.fill_color[D2TK_TRIPLE_ACTIVE] = 0x7f7f00ff;
				style.fill_color[D2TK_TRIPLE_ACTIVE_HOT] = 0xffff00ff;
			}
			else if( (I && !J) || (!I && J) )
			{
				style.fill_color[D2TK_TRIPLE_NONE] = 0x4f4f4fff;
			}
			else
			{
				style.fill_color[D2TK_TRIPLE_NONE] = 0x3f3f3fff;
			}

			bool *val = &handle->matrix[j].note[i];
			const d2tk_state_t stat = d2tk_base_toggle(base, id, bnd, val);

			if(d2tk_state_is_changed(stat))
			{
				lv2_log_note(&handle->logger, "toggle %016"PRIx64" %s\n", id, *val ? "HOLD" : "NONE");
				state_changed = true;
			}

			style.fill_color[D2TK_TRIPLE_ACTIVE] = fill_color_active;
			style.fill_color[D2TK_TRIPLE_ACTIVE_HOT] = fill_color_active_hot;
		}

		d2tk_base_set_default_style(base);
	}

	if(state_changed)
	{
		//FIXME mark dirty state
	}
}

static inline void
_render_props(plughandle_t *handle, const d2tk_rect_t *rect)
{
	d2tk_pugl_t *dpugl = handle->dpugl;
	d2tk_base_t *base = d2tk_pugl_get_base(dpugl);

	D2TK_BASE_TABLE(rect, 8, 1, tab)
	{
		const int k = d2tk_table_get_index(tab);
		const d2tk_rect_t *bnd = d2tk_table_get_rect(tab);
		const d2tk_id_t id = D2TK_ID_IDX(k);

		switch(k)
		{
			case 0:
			{
				d2tk_base_label(base, -1, "Offset", 0.5f, bnd,
					D2TK_ALIGN_RIGHT | D2TK_ALIGN_MIDDLE);
			} break;
			case 1:
			{
				if(d2tk_base_prop_int32_is_changed(base, id, bnd,
					1, &handle->state.offset, 1000))
				{
					lv2_log_note(&handle->logger, "offset: %"PRIi32"\n", handle->state.offset); //FIXME
				}
			} break;

			case 2:
			{
				d2tk_base_label(base, -1, "Beats", 0.5f, bnd,
					D2TK_ALIGN_RIGHT | D2TK_ALIGN_MIDDLE);
			} break;
			case 3:
			{
				if(d2tk_base_prop_int32_is_changed(base, id, bnd,
					1, &handle->state.beats, 1000))
				{
					lv2_log_note(&handle->logger, "beats: %"PRIi32"\n", handle->state.beats); //FIXME
				}
			} break;

			case 4:
			{
				d2tk_base_label(base, -1, "Fractions", 0.5f, bnd,
					D2TK_ALIGN_RIGHT | D2TK_ALIGN_MIDDLE);
			} break;
			case 5:
			{
				if(d2tk_base_prop_int32_is_changed(base, id, bnd,
					1, &handle->state.fractions, 256))
				{
					lv2_log_note(&handle->logger, "fractions: %"PRIi32"\n", handle->state.fractions); //FIXME
				}
			} break;

			case 6:
			{
				d2tk_base_label(base, -1, "Repeats", 0.5f, bnd,
					D2TK_ALIGN_RIGHT | D2TK_ALIGN_MIDDLE);
			} break;
			case 7:
			{
				if(d2tk_base_prop_int32_is_changed(base, id, bnd,
					0, &handle->state.repeats, 1024))
				{
					lv2_log_note(&handle->logger, "repeats: %"PRIi32"\n", handle->state.repeats); //FIXME
				}
			} break;
		}
	}
}

static inline void
_render_menu(plughandle_t *handle, const d2tk_rect_t *rect)
{
	d2tk_pugl_t *dpugl = handle->dpugl;
	d2tk_base_t *base = d2tk_pugl_get_base(dpugl);

	D2TK_BASE_TABLE(rect, 3, 1, tab)
	{
		const int k = d2tk_table_get_index(tab);
		const d2tk_rect_t *bnd = d2tk_table_get_rect(tab);
		const d2tk_id_t id = D2TK_ID_IDX(k);

		switch(k)
		{
			case 0:
			{
				if(d2tk_base_button_label_is_changed(base, id, -1, "Restore", bnd))
				{
					lv2_log_note(&handle->logger, "restoring\n"); //FIXME
					_restore_state(handle);
				}
			} break;
			case 1:
			{
				if(d2tk_base_button_label_is_changed(base, id, -1, "Commit", bnd))
				{
					lv2_log_note(&handle->logger, "commiting\n"); //FIXME
					_save_state(handle);
				}
			} break;
			case 2:
			{
				d2tk_base_label(base, -1, "Tracker#Sequencer "TRACKER_VERSION, 0.6f,
					bnd, D2TK_ALIGN_RIGHT | D2TK_ALIGN_BOTTOM);
			} break;
		}
	}
}

static int
_expose(void *data, d2tk_coord_t w, d2tk_coord_t h)
{
	plughandle_t *handle = data;
	d2tk_pugl_t *dpugl = handle->dpugl;
	d2tk_base_t *base = d2tk_pugl_get_base(dpugl);
	const d2tk_coord_t frac [3] = { 30, 0, 30};

	D2TK_BASE_LAYOUT(&D2TK_RECT(0, 0, w, h), 3, frac, D2TK_FLAG_LAYOUT_Y_ABS, vlay)
	{
		const d2tk_rect_t *vrect = d2tk_layout_get_rect(vlay);
		const unsigned k = d2tk_layout_get_index(vlay);

		switch(k)
		{
			case 0:
			{
				_render_props(handle, vrect);
			} break;
			case 1:
			{
				_render_seq(handle, vrect);
			} break;
			case 2:
			{
				_render_menu(handle, vrect);
			} break;
		}
	}

	return 0;
}

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;
	LV2_Options_Option *opts = 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_URID__unmap))
			handle->unmap = features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_LOG__log))
			handle->log = features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_OPTIONS__options))
			opts = 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);

	if(!props_init(&handle->props, plugin_uri,
		defs, MAX_NPROPS, &handle->state, &handle->stash,
		handle->map, handle))
	{
		fprintf(stderr, "props_init failed\n");
		free(handle);
		return NULL;
	}

	handle->atom_eventTransfer = handle->map->map(handle->map->handle, LV2_ATOM__eventTransfer);
	handle->atom_beatTime = handle->map->map(handle->map->handle, LV2_ATOM__beatTime);
	handle->midi_MidiEvent = handle->map->map(handle->map->handle, LV2_MIDI__MidiEvent);
	handle->tracker_offset = handle->map->map(handle->map->handle, TRACKER__offset);
	handle->tracker_beats = handle->map->map(handle->map->handle, TRACKER__beats);
	handle->tracker_fractions = handle->map->map(handle->map->handle, TRACKER__fractions);
	handle->tracker_repeats = handle->map->map(handle->map->handle, TRACKER__repeats);
	handle->tracker_position = handle->map->map(handle->map->handle, TRACKER__position);
	handle->tracker_mode = handle->map->map(handle->map->handle, TRACKER__mode);
	handle->tracker_graph = handle->map->map(handle->map->handle, TRACKER__graph);

	const LV2_URID params_sample_rate = handle->map->map(handle->map->handle,
		LV2_PARAMETERS__sampleRate);

	handle->sample_rate = 48000.f; // fall-back

	for(LV2_Options_Option *opt = opts;
		opt && (opt->key != 0) && (opt->value != NULL);
		opt++)
	{
		if( (opt->key == params_sample_rate) && (opt->type == handle->forge.Float) )
			handle->sample_rate = *(float*)opt->value;
		//TODO handle more options
	}

	handle->controller = controller;
	handle->writer = write_function;

	const d2tk_coord_t w = 800;
	const d2tk_coord_t h = 450;

	d2tk_pugl_config_t *config = &handle->config;
	config->parent = (uintptr_t)parent;
	config->bundle_path = bundle_path;
	config->min_w = w/2;
	config->min_h = h/2;
	config->w = w;
	config->h = h;
	config->fixed_size = false;
	config->fixed_aspect = false;
	config->expose = _expose;
	config->data = handle;

	handle->dpugl = d2tk_pugl_new(config, (uintptr_t *)widget);
	if(!handle->dpugl)
	{
		free(handle);
		return NULL;
	}

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

	//FIXME
	handle->state.offset = 1;
	handle->state.repeats = 0;

	atom_ser_t *ser = &handle->ser;
	ser->size = 1024;
	ser->buf = malloc(ser->size);

	_restore_state(handle);

	return handle;
}

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

	d2tk_pugl_free(handle->dpugl);

	atom_ser_t *ser = &handle->ser;
	if(ser->buf)
		free(ser->buf);

	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)
	{
		const LV2_Atom_Object *obj = buf;

		atom_ser_t *ser = &handle->ser;
		ser->offset = 0;
		lv2_atom_forge_set_sink(&handle->forge, _sink, _deref, ser);

		LV2_Atom_Forge_Ref ref = 0;
		if(props_advance(&handle->props, &handle->forge, 0, obj, &ref))
		{
#if 0
			nk_pugl_post_redisplay(&handle->win);
#endif
		}
	}
}

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

	return d2tk_pugl_step(handle->dpugl);
}

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

static int
_resize(LV2UI_Handle instance, int width, int height)
{
	plughandle_t *handle = instance;

	return d2tk_pugl_resize(handle->dpugl, width, height);
}

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

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 tracker_sequencer_ui = {
	.URI            = TRACKER_PREFIX"sequencer_ui",
	.instantiate    = instantiate,
	.cleanup        = cleanup,
	.port_event     = port_event,
	.extension_data = extension_data
};