b8f99ab1318d1ada59f5a20cfbdc4c91adbd1702
[goodguy/history.git] / cinelerra-5.1 / cinelerra / pluginlv2ui.C
1
2 // shared between parent/child fork
3 #include "language.h"
4 #include "pluginlv2ui.h"
5
6 #include <gtk/gtk.h>
7 #include <gdk/gdk.h>
8
9 #include <ctype.h>
10 #include <string.h>
11
12 void PluginLV2UI::update_value(int idx, uint32_t bfrsz, uint32_t typ, const void *bfr)
13 {
14         if( idx >= config.nb_ports ) return;
15         for( int i=0, sz=config.size(); i<sz; ++i ) {
16                 PluginLV2Client_Opt *opt = config[i];
17                 if( opt->idx == idx ) {
18                         opt->set_value(*(const float*)bfr);
19                         updates |= UPDATE_HOST;
20                         break;
21                 }
22         }
23 }
24 void PluginLV2UI::write_from_ui(void *the, uint32_t idx,
25                 uint32_t bfrsz, uint32_t typ, const void *bfr)
26 {
27         ((PluginLV2UI*)the)->update_value(idx, bfrsz, typ, bfr);
28 }
29
30 uint32_t PluginLV2UI::port_index(void* obj, const char* sym)
31 {
32         PluginLV2UI *the = (PluginLV2UI*)obj;
33         for( int i=0, sz=the->config.size(); i<sz; ++i ) {
34                 PluginLV2Client_Opt *opt = the->config[i];
35                 if( !strcmp(sym, opt->sym) ) return opt->idx;
36         }
37         return UINT32_MAX;
38 }
39
40 void PluginLV2UI::update_control(int idx, uint32_t bfrsz, uint32_t typ, const void *bfr)
41 {
42         if( !sinst || idx >= config.nb_ports ) return;
43         suil_instance_port_event(sinst, idx, bfrsz, typ, bfr);
44 }
45
46
47 uint32_t PluginLV2UI::uri_to_id(LV2_URID_Map_Handle handle, const char *map, const char *uri)
48 {
49         return ((PluginLV2UriTable *)handle)->map(uri);
50 }
51
52 LV2_URID PluginLV2UI::uri_table_map(LV2_URID_Map_Handle handle, const char *uri)
53 {
54         return ((PluginLV2UriTable *)handle)->map(uri);
55 }
56
57 const char *PluginLV2UI::uri_table_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
58 {
59         return ((PluginLV2UriTable *)handle)->unmap(urid);
60 }
61
62 void PluginLV2UI::lv2ui_instantiate(void *parent)
63 {
64         if ( !ui_host ) {
65                 ui_host = suil_host_new(
66                         PluginLV2UI::write_from_ui,
67                         PluginLV2UI::port_index,
68                         0, 0);
69 //              suil_host_set_touch_func(ui_host, cb_touch);
70         }
71
72         features.remove();  // remove terminating zero
73         ui_features = features.size();
74         LV2_Handle lilv_handle = lilv_instance_get_handle(inst);
75         features.append(new Lv2Feature(NS_EXT "instance-access", lilv_handle));
76         const LV2_Descriptor *lilv_desc = lilv_instance_get_descriptor(inst);
77         ext_data = new LV2_Extension_Data_Feature();
78         ext_data->data_access = lilv_desc->extension_data;
79         features.append(new Lv2Feature(LV2_DATA_ACCESS_URI, ext_data));
80         features.append(new Lv2Feature(LV2_UI__parent, parent));
81         features.append(new Lv2Feature(LV2_UI__idleInterface, 0));
82         features.append(new Lv2Feature(LV2_EXTERNAL_UI_URI, &extui_host));
83         features.append(new Lv2Feature(LV2_EXTERNAL_UI_URI__KX__Host, &extui_host));
84         features.append(0); // add new terminating zero
85
86         const char* bundle_uri  = lilv_node_as_uri(lilv_ui_get_bundle_uri(lilv_ui));
87         char*       bundle_path = lilv_file_uri_parse(bundle_uri, NULL);
88         const char* binary_uri  = lilv_node_as_uri(lilv_ui_get_binary_uri(lilv_ui));
89         char*       binary_path = lilv_file_uri_parse(binary_uri, NULL);
90         sinst = suil_instance_new(ui_host, this, ui_type,
91                 lilv_node_as_uri(lilv_plugin_get_uri(lilv)),
92                 lilv_node_as_uri(lilv_ui_get_uri(lilv_ui)),
93                 lilv_node_as_uri(lilv_type),
94                 bundle_path, binary_path, features);
95
96         lilv_free(binary_path);
97         lilv_free(bundle_path);
98 }
99
100 bool PluginLV2UI::lv2ui_resizable()
101 {
102         if( !lilv_ui ) return false;
103         const LilvNode* s   = lilv_ui_get_uri(lilv_ui);
104         LilvNode *p   = lilv_new_uri(world, LV2_CORE__optionalFeature);
105         LilvNode *fs  = lilv_new_uri(world, LV2_UI__fixedSize);
106         LilvNode *nrs = lilv_new_uri(world, LV2_UI__noUserResize);
107         LilvNodes *fs_matches = lilv_world_find_nodes(world, s, p, fs);
108         LilvNodes *nrs_matches = lilv_world_find_nodes(world, s, p, nrs);
109         lilv_nodes_free(nrs_matches);
110         lilv_nodes_free(fs_matches);
111         lilv_node_free(nrs);
112         lilv_node_free(fs);
113         lilv_node_free(p);
114         return !fs_matches && !nrs_matches;
115 }
116
117 int PluginLV2UI::update_lv2_input(float *vals, int force)
118 {
119         int ret = 0;
120         float *ctls = config.ctls;
121         for( int i=0; i<config.size(); ++i ) {
122                 int idx = config[i]->idx;
123                 float val = vals[idx];
124                 if( !force && ctls[idx] == val ) continue;
125                 ctls[idx] = val;
126                 update_control(idx, sizeof(ctls[idx]), 0, &ctls[idx]);
127                 ++ret;
128         }
129         return ret;
130 }
131
132 void PluginLV2UI::update_lv2_output()
133 {
134         int *ports = config.ports;
135         float *ctls = config.ctls;
136         for( int i=0; i<config.nb_ports; ++i ) {
137                 if( !(ports[i] & PORTS_UPDATE) ) continue;
138                 ports[i] &= ~PORTS_UPDATE;
139                 update_control(i, sizeof(ctls[i]), 0, &ctls[i]);
140         }
141 }
142
143 void PluginLV2UI::run_lilv(int samples)
144 {
145         float ctls[config.nb_ports];
146         for( int i=0; i<config.nb_ports; ++i ) ctls[i] = config.ctls[i];
147
148         lilv_instance_run(inst, samples);
149
150         if( worker_iface ) worker_responses();
151
152         for( int i=0; i<config.nb_ports; ++i ) {
153                 if( !(config.ports[i] & PORTS_OUTPUT) ) continue;
154                 if( !(config.ports[i] & PORTS_CONTROL) ) continue;
155                 if( config.ctls[i] == ctls[i] ) continue;
156                 config.ports[i] |= PORTS_UPDATE;
157                 updates |= UPDATE_PORTS;
158         }
159 }
160
161 void PluginLV2ChildUI::start_gui()
162 {
163         gui->start_gui();
164         update_lv2_input(config.ctls, 1);
165         connect_ports(config, PORTS_CONTROL | PORTS_ATOM);
166         updates = 0;
167         run_lilv(0);
168         send_host(LV2_SHOW, 0, 0);
169         hidden = 0;
170 }
171
172 void PluginLV2UI::update_host()
173 {
174 // ignore update
175         if( running < 0 ) { running = 1;  return; }
176         send_host(LV2_UPDATE, config.ctls, sizeof(float)*config.nb_ports);
177 }
178
179
180 static void lv2ui_extui_host_ui_closed(void *p)
181 {
182         PluginLV2UI *ui = (PluginLV2UI *)p;
183         ui->hidden = -1;
184 }
185
186 int PluginLV2ChildUI::init_ui(const char *path, int sample_rate)
187 {
188         char *title = PluginLV2UI::title;
189         if( load_lv2(path, title) ) return 1;
190         if( init_lv2(config, sample_rate) ) return 1;
191
192         lilv_uis = lilv_plugin_get_uis(lilv);
193         if( !lilv_uis ) {
194                 printf("lv2: lilv_plugin_get_uis(%s) failed\n", path);
195                 return 1;
196         }
197
198         if( !gui && wgt_type ) {
199                 LilvNode *gui_type = lilv_new_uri(world, wgt_type);
200                 LILV_FOREACH(uis, i, lilv_uis) {
201                         const LilvUI *ui = lilv_uis_get(lilv_uis, i);
202                         if( lilv_ui_is_supported(ui, suil_ui_supported, gui_type, &lilv_type)) {
203                                 extui_host.ui_closed = lv2ui_extui_host_ui_closed;
204                                 gui = new PluginLV2ChildWgtUI(this);
205                                 lilv_ui = ui;
206                                 ui_type = wgt_type;
207                                 break;
208                         }
209                 }
210                 lilv_node_free(gui_type);
211         }
212         if( !gui && gtk_type ) {
213                 LilvNode *gui_type = lilv_new_uri(world, gtk_type);
214                 LILV_FOREACH(uis, i, lilv_uis) {
215                         const LilvUI *ui = lilv_uis_get(lilv_uis, i);
216                         if( lilv_ui_is_supported(ui, suil_ui_supported, gui_type, &lilv_type)) {
217                                 gui = new PluginLV2ChildGtkUI(this);
218                                 lilv_ui = ui;
219                                 ui_type = gtk_type;
220                                 break;
221                         }
222                 }
223                 lilv_node_free(gui_type);
224                 ui_type = gtk_type;
225         }
226
227         if( !gui ) {
228                 printf("lv2_gui: init_ui failed: %s\n", title);
229                 return 1;
230         }
231
232         lilv_instance_activate(inst);
233         return 0;
234 }
235
236 void PluginLV2ChildUI::reset_gui()
237 {
238         gui->reset_gui();
239         if( sinst )     { suil_instance_free(sinst);  sinst = 0; }
240         if( ui_host )   { suil_host_free(ui_host);    ui_host = 0; }
241
242         while( features.size() > ui_features ) features.remove_object();
243         features.append(0);
244         send_host(LV2_HIDE, 0, 0);
245         hidden = 1;
246 }
247
248 // main loop
249 int PluginLV2ChildUI::run_ui()
250 {
251         double last_time = 0, fps = 30;
252         int64_t frame_usecs = 1e6 / fps;
253         running = 1;
254         done = 0;
255         while( !done ) {
256                 if( hidden < 0 ) {
257                         gui->top_level = 0;
258                         reset_gui();
259                         done = -1;
260                         break;
261                 }
262                 if( updates ) {
263                         if( (updates & UPDATE_PORTS) )
264                                 update_lv2_output();
265                         if( (updates & UPDATE_HOST) )
266                                 update_host();
267                         updates = 0;
268                 }
269                 struct timeval tv;  gettimeofday(&tv, 0);
270                 double t = tv.tv_sec + tv.tv_usec / 1e6;
271                 double dt = t - last_time;  last_time = t;
272                 int64_t usec = frame_usecs - dt*1e6;
273                 if( usec < 0 ) usec = 0;
274                 if( child_iteration(usec) < 0 )
275                         done = 1;
276         }
277         running = 0;
278         return 0;
279 }
280
281 void PluginLV2ChildUI::run_buffer(int shmid)
282 {
283         if( !shm_buffer(shmid) ) return;
284         map_buffer();
285         connect_ports(config, PORTS_ALL);
286         run_lilv(shm_bfr->samples);
287         shm_bfr->done = 1;
288 }
289
290 int PluginLV2ChildUI::child_iteration(int64_t usec)
291 {
292         int ret = 0;
293         if( is_forked() )
294                 ret = read_child(usec);
295         else
296                 usleep(usec);
297         if( ret > 0 ) {
298                 switch( child_token ) {
299                 case LV2_OPEN: {
300                         open_bfr_t *open_bfr = (open_bfr_t *)child_data;
301                         if( init_ui(open_bfr->path, open_bfr->sample_rate) ) {
302                                 printf("lv2ui: unable to init: %s\n", open_bfr->path);
303                                 exit(1);
304                         }
305                         break; }
306                 case LV2_LOAD: {
307                         float *vals = (float *)child_data;
308                         update_lv2_input(vals, 1);
309                         break; }
310                 case LV2_UPDATE: {
311                         float *vals = (float *)child_data;
312                         update_lv2_input(vals, 0);
313                         break; }
314                 case LV2_SHOW: {
315                         start_gui();
316                         break; }
317                 case LV2_HIDE: {
318                         reset_gui();
319                         break; }
320                 case LV2_SHMID: {
321                         int shmid = *(int *)child_data;
322                         run_buffer(shmid);
323                         send_parent(LV2_SHMID, 0, 0);
324                         break; }
325                 case EXIT_CODE:
326                         return -1;
327                 }
328         }
329         if( ret >= 0 && gui )
330                 gui->handle_child();
331         return ret;
332 }
333
334 int PluginLV2ChildUI::send_host(int64_t token, const void *data, int bytes)
335 {
336         return is_forked() ? send_parent(token, data, bytes) : 0;
337 }
338
339 PluginLV2GUI::PluginLV2GUI(PluginLV2ChildUI *child_ui)
340 {
341         this->child_ui = child_ui;
342         top_level = 0;
343 }
344
345 PluginLV2GUI::~PluginLV2GUI()
346 {
347 }
348
349 PluginLV2ChildGtkUI::PluginLV2ChildGtkUI(PluginLV2ChildUI *child_ui)
350  : PluginLV2GUI(child_ui)
351 {
352         gtk_set_locale();
353         gtk_init(&child_ui->ac, &child_ui->av);
354 }
355
356 PluginLV2ChildGtkUI::~PluginLV2ChildGtkUI()
357 {
358 }
359
360 void PluginLV2ChildGtkUI::reset_gui()
361 {
362         GtkWidget *top_level = (GtkWidget *)this->top_level;
363         if( top_level ) { gtk_widget_destroy(top_level); this->top_level = 0; }
364 }
365
366 static void lilv_gtk_destroy(GtkWidget* widget, gpointer data)
367 {
368         PluginLV2ChildGtkUI *gui = (PluginLV2ChildGtkUI*)data;
369         gui->child_ui->hidden = -1;
370 }
371
372 void PluginLV2ChildGtkUI::start_gui()
373 {
374         this->top_level = (void *)gtk_window_new(GTK_WINDOW_TOPLEVEL);
375         GtkWidget *top_level = (GtkWidget *)this->top_level;
376         g_signal_connect(top_level, "destroy", G_CALLBACK(lilv_gtk_destroy), this);
377         char *title = child_ui->PluginLV2UI::title;
378         gtk_window_set_title(GTK_WINDOW(top_level), title);
379
380         GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
381         gtk_window_set_role(GTK_WINDOW(top_level), "plugin_ui");
382         gtk_container_add(GTK_CONTAINER(top_level), vbox);
383
384         GtkWidget *alignment = gtk_alignment_new(0.5, 0.5, 1.0, 1.0);
385         gtk_box_pack_start(GTK_BOX(vbox), alignment, TRUE, TRUE, 0);
386         gtk_widget_show(alignment);
387         child_ui->lv2ui_instantiate(alignment);
388         GtkWidget* widget = (GtkWidget*)suil_instance_get_widget(child_ui->sinst);
389         gtk_container_add(GTK_CONTAINER(alignment), widget);
390         gtk_window_set_resizable(GTK_WINDOW(top_level), child_ui->lv2ui_resizable());
391         gtk_widget_show_all(vbox);
392         gtk_widget_grab_focus(widget);
393         gtk_window_present(GTK_WINDOW(top_level));
394 }
395
396 int PluginLV2ChildGtkUI::handle_child()
397 {
398         if( gtk_events_pending() ) {
399                 gtk_main_iteration();
400         }
401         return 1;
402 }
403
404 PluginLV2ChildWgtUI::PluginLV2ChildWgtUI(PluginLV2ChildUI *child_ui)
405  : PluginLV2GUI(child_ui)
406 {
407 }
408
409 PluginLV2ChildWgtUI::~PluginLV2ChildWgtUI()
410 {
411 }
412
413 void PluginLV2ChildWgtUI::reset_gui()
414 {
415         lv2_external_ui *top_level = (lv2_external_ui *)this->top_level;
416         if( top_level ) { LV2_EXTERNAL_UI_HIDE(top_level); this->top_level = 0; }
417 }
418
419 void PluginLV2ChildWgtUI::start_gui()
420 {
421         child_ui->lv2ui_instantiate(0);
422         this->top_level = (void *)suil_instance_get_widget(child_ui->sinst);
423         lv2_external_ui *top_level = (lv2_external_ui *)this->top_level;
424         if( top_level ) LV2_EXTERNAL_UI_SHOW(top_level);
425 }
426
427 int PluginLV2ChildWgtUI::handle_child()
428 {
429         lv2_external_ui *top_level = (lv2_external_ui *)this->top_level;
430         if( top_level )
431                 LV2_EXTERNAL_UI_RUN(top_level);
432         return 1;
433 }
434