<commit_before>/*
  This file is part of Ingen.
  Copyright 2007-2015 <NAME> <http://drobilla.net/>

  Ingen is free software: you can redistribute it and/or modify it under the
  terms of the GNU Affero General Public License as published by the Free
  Software Foundation, either version 3 of the License, or any later version.

  Ingen 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 GNU Affero General Public License for details.

  You should have received a copy of the GNU Affero General Public License
  along with Ingen.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "ingen/Interface.hpp"
#include "ingen/Log.hpp"
#include "ingen/URIs.hpp"
#include "ingen/client/BlockModel.hpp"
#include "ingen/client/PluginUI.hpp"
#include "ingen/client/PortModel.hpp"
#include "lv2/lv2plug.in/ns/ext/atom/atom.h"
#include "lv2/lv2plug.in/ns/extensions/ui/ui.h"

using namespace std;

namespace Ingen {
namespace Client {

SuilHost* PluginUI::ui_host = NULL;

static SPtr<const PortModel>
get_port(PluginUI* ui, uint32_t port_index)
{
	if (port_index >= ui->block()->ports().size()) {
		ui->world()->log().error(
			fmt("%1% UI tried to access invalid port %2%\n")
			% ui->block()->plugin()->uri().c_str() % port_index);
		return SPtr<const PortModel>();
	}
	return ui->block()->ports()[port_index];
}

static void
lv2_ui_write(SuilController controller,
             uint32_t       port_index,
             uint32_t       buffer_size,
             uint32_t       format,
             const void*    buffer)
{
	PluginUI* const       ui   = (PluginUI*)controller;
	const URIs&           uris = ui->world()->uris();
	SPtr<const PortModel> port = get_port(ui, port_index);
	if (!port) {
		return;
	}

	// float (special case, always 0)
	if (format == 0) {
		if (buffer_size != 4) {
			ui->world()->log().error(
				fmt("%1% UI wrote corrupt float with bad size\n")
				% ui->block()->plugin()->uri().c_str());
			return;
		}
		const float value = *(const float*)buffer;
		if (port->value().type() == uris.atom_Float &&
		    value == port->value().get<float>()) {
			return;  // Ignore feedback
		}

		ui->signal_property_changed()(
			port->uri(),
			uris.ingen_value,
			ui->world()->forge().make(value));

	} else if (format == uris.atom_eventTransfer.urid.get<LV2_URID>()) {
		const LV2_Atom* atom = (const LV2_Atom*)buffer;
		Atom            val  = ui->world()->forge().alloc(
			atom->size, atom->type, LV2_ATOM_BODY_CONST(atom));
		ui->signal_property_changed()(port->uri(),
		                              uris.ingen_activity,
		                              val);

	} else {
		ui->world()->log().warn(
			fmt("Unknown value format %1% from LV2 UI\n")
			% format % ui->block()->plugin()->uri().c_str());
	}
}

static uint32_t
lv2_ui_port_index(SuilController controller, const char* port_symbol)
{
	PluginUI* const ui = (PluginUI*)controller;

	const BlockModel::Ports& ports = ui->block()->ports();
	for (uint32_t i = 0; i < ports.size(); ++i) {
		if (ports[i]->symbol() == port_symbol) {
			return i;
		}
	}
	return LV2UI_INVALID_PORT_INDEX;
}

static uint32_t
lv2_ui_subscribe(SuilController            controller,
                 uint32_t                  port_index,
                 uint32_t                  protocol,
                 const LV2_Feature* const* features)
{
	PluginUI* const       ui   = (PluginUI*)controller;
	SPtr<const PortModel> port = get_port(ui, port_index);
	if (!port) {
		return 1;
	}

	ui->signal_property_changed()(
		ui->block()->ports()[port_index]->uri(),
		ui->world()->uris().ingen_broadcast,
		ui->world()->forge().make(true));

	return 0;
}

static uint32_t
lv2_ui_unsubscribe(SuilController            controller,
                   uint32_t                  port_index,
                   uint32_t                  protocol,
                   const LV2_Feature* const* features)
{
	PluginUI* const       ui   = (PluginUI*)controller;
	SPtr<const PortModel> port = get_port(ui, port_index);
	if (!port) {
		return 1;
	}

	ui->signal_property_changed()(
		ui->block()->ports()[port_index]->uri(),
		ui->world()->uris().ingen_broadcast,
		ui->world()->forge().make(false));

	return 0;
}

PluginUI::PluginUI(Ingen::World*          world,
                   SPtr<const BlockModel> block,
                   LilvUIs*               uis,
                   const LilvUI*          ui,
                   const LilvNode*        ui_type)
	: _world(world)
	, _block(block)
	, _instance(NULL)
	, _uis(uis)
	, _ui(ui)
	, _ui_node(lilv_node_duplicate(lilv_ui_get_uri(ui)))
	, _ui_type(lilv_node_duplicate(ui_type))
{
}

PluginUI::~PluginUI()
{
	for (uint32_t i : _subscribed_ports) {
		lv2_ui_unsubscribe(this, i, 0, NULL);
	}
	suil_instance_free(_instance);
	lilv_node_free(_ui_node);
	lilv_node_free(_ui_type);
	lilv_uis_free(_uis);
}

SPtr<PluginUI>
PluginUI::create(Ingen::World*          world,
                 SPtr<const BlockModel> block,
                 const LilvPlugin*      plugin)
{
	const URIs& uris = world->uris();

	if (!PluginUI::ui_host) {
		PluginUI::ui_host = suil_host_new(lv2_ui_write,
		                                  lv2_ui_port_index,
		                                  lv2_ui_subscribe,
		                                  lv2_ui_unsubscribe);
	}

	static const char* gtk_ui_uri = LV2_UI__GtkUI;

	LilvNode* gtk_ui = lilv_new_uri(world->lilv_world(), gtk_ui_uri);

	LilvUIs*        uis     = lilv_plugin_get_uis(plugin);
	const LilvUI*   ui      = NULL;
	const LilvNode* ui_type = NULL;
	LILV_FOREACH(uis, u, uis) {
		const LilvUI* this_ui = lilv_uis_get(uis, u);
		if (lilv_ui_is_supported(this_ui,
		                         suil_ui_supported,
		                         gtk_ui,
		                         &ui_type)) {
			// TODO: Multiple UI support
			ui = this_ui;
			break;
		}
	}

	if (!ui) {
		lilv_node_free(gtk_ui);<commit_msg>Remove unused variable<commit_after>/*
  This file is part of Ingen.
  Copyright 2007-2015 <NAME> <http://drobilla.net/>

  Ingen is free software: you can redistribute it and/or modify it under the
  terms of the GNU Affero General Public License as published by the Free
  Software Foundation, either version 3 of the License, or any later version.

  Ingen 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 GNU Affero General Public License for details.

  You should have received a copy of the GNU Affero General Public License
  along with Ingen.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "ingen/Interface.hpp"
#include "ingen/Log.hpp"
#include "ingen/URIs.hpp"
#include "ingen/client/BlockModel.hpp"
#include "ingen/client/PluginUI.hpp"
#include "ingen/client/PortModel.hpp"
#include "lv2/lv2plug.in/ns/ext/atom/atom.h"
#include "lv2/lv2plug.in/ns/extensions/ui/ui.h"

using namespace std;

namespace Ingen {
namespace Client {

SuilHost* PluginUI::ui_host = NULL;

static SPtr<const PortModel>
get_port(PluginUI* ui, uint32_t port_index)
{
	if (port_index >= ui->block()->ports().size()) {
		ui->world()->log().error(
			fmt("%1% UI tried to access invalid port %2%\n")
			% ui->block()->plugin()->uri().c_str() % port_index);
		return SPtr<const PortModel>();
	}
	return ui->block()->ports()[port_index];
}

static void
lv2_ui_write(SuilController controller,
             uint32_t       port_index,
             uint32_t       buffer_size,
             uint32_t       format,
             const void*    buffer)
{
	PluginUI* const       ui   = (PluginUI*)controller;
	const URIs&           uris = ui->world()->uris();
	SPtr<const PortModel> port = get_port(ui, port_index);
	if (!port) {
		return;
	}

	// float (special case, always 0)
	if (format == 0) {
		if (buffer_size != 4) {
			ui->world()->log().error(
				fmt("%1% UI wrote corrupt float with bad size\n")
				% ui->block()->plugin()->uri().c_str());
			return;
		}
		const float value = *(const float*)buffer;
		if (port->value().type() == uris.atom_Float &&
		    value == port->value().get<float>()) {
			return;  // Ignore feedback
		}

		ui->signal_property_changed()(
			port->uri(),
			uris.ingen_value,
			ui->world()->forge().make(value));

	} else if (format == uris.atom_eventTransfer.urid.get<LV2_URID>()) {
		const LV2_Atom* atom = (const LV2_Atom*)buffer;
		Atom            val  = ui->world()->forge().alloc(
			atom->size, atom->type, LV2_ATOM_BODY_CONST(atom));
		ui->signal_property_changed()(port->uri(),
		                              uris.ingen_activity,
		                              val);

	} else {
		ui->world()->log().warn(
			fmt("Unknown value format %1% from LV2 UI\n")
			% format % ui->block()->plugin()->uri().c_str());
	}
}

static uint32_t
lv2_ui_port_index(SuilController controller, const char* port_symbol)
{
	PluginUI* const ui = (PluginUI*)controller;

	const BlockModel::Ports& ports = ui->block()->ports();
	for (uint32_t i = 0; i < ports.size(); ++i) {
		if (ports[i]->symbol() == port_symbol) {
			return i;
		}
	}
	return LV2UI_INVALID_PORT_INDEX;
}

static uint32_t
lv2_ui_subscribe(SuilController            controller,
                 uint32_t                  port_index,
                 uint32_t                  protocol,
                 const LV2_Feature* const* features)
{
	PluginUI* const       ui   = (PluginUI*)controller;
	SPtr<const PortModel> port = get_port(ui, port_index);
	if (!port) {
		return 1;
	}

	ui->signal_property_changed()(
		ui->block()->ports()[port_index]->uri(),
		ui->world()->uris().ingen_broadcast,
		ui->world()->forge().make(true));

	return 0;
}

static uint32_t
lv2_ui_unsubscribe(SuilController            controller,
                   uint32_t                  port_index,
                   uint32_t                  protocol,
                   const LV2_Feature* const* features)
{
	PluginUI* const       ui   = (PluginUI*)controller;
	SPtr<const PortModel> port = get_port(ui, port_index);
	if (!port) {
		return 1;
	}

	ui->signal_property_changed()(
		ui->block()->ports()[port_index]->uri(),
		ui->world()->uris().ingen_broadcast,
		ui->world()->forge().make(false));

	return 0;
}

PluginUI::PluginUI(Ingen::World*          world,
                   SPtr<const BlockModel> block,
                   LilvUIs*               uis,
                   const LilvUI*          ui,
                   const LilvNode*        ui_type)
	: _world(world)
	, _block(block)
	, _instance(NULL)
	, _uis(uis)
	, _ui(ui)
	, _ui_node(lilv_node_duplicate(lilv_ui_get_uri(ui)))
	, _ui_type(lilv_node_duplicate(ui_type))
{
}

PluginUI::~PluginUI()
{
	for (uint32_t i : _subscribed_ports) {
		lv2_ui_unsubscribe(this, i, 0, NULL);
	}
	suil_instance_free(_instance);
	lilv_node_free(_ui_node);
	lilv_node_free(_ui_type);
	lilv_uis_free(_uis);
}

SPtr<PluginUI>
PluginUI::create(Ingen::World*          world,
                 SPtr<const BlockModel> block,
                 const LilvPlugin*      plugin)
{
	if (!PluginUI::ui_host) {
		PluginUI::ui_host = suil_host_new(lv2_ui_write,
		                                  lv2_ui_port_index,
		                                  lv2_ui_subscribe,
		                                  lv2_ui_unsubscribe);
	}

	static const char* gtk_ui_uri = LV2_UI__GtkUI;

	LilvNode* gtk_ui = lilv_new_uri(world->lilv_world(), gtk_ui_uri);

	LilvUIs*        uis     = lilv_plugin_get_uis(plugin);
	const LilvUI*   ui      = NULL;
	const LilvNode* ui_type = NULL;
	LILV_FOREACH(uis, u, uis) {
		const LilvUI* this_ui = lilv_uis_get(uis, u);
		if (lilv_ui_is_supported(this_ui,
		                         suil_ui_supported,
		                         gtk_ui,
		                         &ui_type)) {
			// TODO: Multiple UI support
			ui = this_ui;
			break;
		}
	}

	if (!ui) {
		lilv_node_free(gtk_ui);
{'fraction_non_alphanumeric': 0.13291192583227981, 'fraction_numerical': 0.011799410029498525, 'mean_word_length': 2.609978703985397, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 138, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4003217', 'n_tokens_mistral': 4331, 'n_tokens_neox': 3994, 'n_words': 881}