Code: Select all
/*
LV2 Sampler Example Plugin UI
Copyright 2011-2012 David Robillard <d@drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/**
@file ui.c Sampler Plugin UI
*/
#include <stdlib.h>
#include <gtk/gtk.h>
#include "lv2/lv2plug.in/ns/ext/atom/atom.h"
#include "lv2/lv2plug.in/ns/ext/atom/forge.h"
#include "lv2/lv2plug.in/ns/ext/atom/util.h"
#include "lv2/lv2plug.in/ns/ext/patch/patch.h"
#include "lv2/lv2plug.in/ns/ext/urid/urid.h"
#include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
#include "./uris.h"
#define SAMPLER_UI_URI "http://lv2plug.in/plugins/eg-sampler#ui"
typedef struct {
LV2_Atom_Forge forge;
LV2_URID_Map* map;
SamplerURIs uris;
LV2UI_Write_Function write;
LV2UI_Controller controller;
GtkWidget* box;
GtkWidget* button;
GtkWidget* label;
} SamplerUI;
static void
on_load_clicked(GtkWidget* widget,
void* handle)
{
SamplerUI* ui = (SamplerUI*)handle;
/* Create a dialog to select a sample file. */
GtkWidget* dialog = gtk_file_chooser_dialog_new(
"Load Sample",
NULL,
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
NULL);
/* Run the dialog, and return if it is cancelled. */
if (gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_ACCEPT) {
gtk_widget_destroy(dialog);
return;
}
/* Get the file path from the dialog. */
char* filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
g_print ("opening %s\n", filename);
/* Got what we need, destroy the dialog. */
gtk_widget_destroy(dialog);
#define OBJ_BUF_SIZE 1024
uint8_t obj_buf[OBJ_BUF_SIZE];
lv2_atom_forge_set_buffer(&ui->forge, obj_buf, OBJ_BUF_SIZE);
LV2_Atom* msg = write_set_file(&ui->forge, &ui->uris,
filename, strlen(filename));
ui->write(ui->controller, 0, lv2_atom_total_size(msg),
ui->uris.atom_eventTransfer,
msg);
g_free(filename);
}
/******************************************************************************/
/* Define a list of data types called "targets" that a destination widget will
* accept. The string type is arbitrary, and negotiated between DnD widgets by
* the developer. An enum or GQuark can serve as the integer target id. */
enum {
TARGET_INT32,
TARGET_STRING,
TARGET_ROOTWIN,
TARGET_URI_LIST
};
/* datatype (string), restrictions on DnD (GtkTargetFlags), datatype (int) */
static GtkTargetEntry target_list[] = {
{ "INTEGER", 0, TARGET_INT32 },
{ "STRING", 0, TARGET_STRING },
{ "text/plain", 0, TARGET_STRING },
{ "application/x-rootwindow-drop", 0, TARGET_ROOTWIN },
{ "text/uri-list", 0, TARGET_URI_LIST }
};
static guint n_targets = G_N_ELEMENTS (target_list);
/******************************************************************************/
/* Signal receivable by destination */
/* Emitted when the data has been received from the source. It should check
* the GtkSelectionData sent by the source, and do something with it. Finally
* it needs to finish the operation by calling gtk_drag_finish, which will emit
* the "data-delete" signal if told to. */
static void
drag_data_received_handl(GtkWidget *widget,
GdkDragContext *context,
gint x,
gint y,
GtkSelectionData *selection_data,
guint target_type,
guint time,
gpointer data,
void* handle)
{
SamplerUI* ui = (SamplerUI*)handle;
gchar *filename;
gboolean dnd_success = FALSE;
gboolean delete_selection_data = FALSE;
const gchar *name = gtk_widget_get_name (widget);
/* Deal with what we are given from source */
if((selection_data != NULL) && (gtk_selection_data_get_length(selection_data) >= 0))
{
if (gdk_drag_context_get_suggested_action(context) == GDK_ACTION_ASK)
{
/* Ask the user to move or copy, then set the context action. */
}
if (gdk_drag_context_get_suggested_action(context) == GDK_ACTION_MOVE)
delete_selection_data = TRUE;
/* Check that we got the format we can use */
if (target_type == TARGET_URI_LIST)
{
filename = g_filename_from_uri(
(gchar*)gtk_selection_data_get_data(selection_data),
NULL,
NULL);
g_print ("opening %s\n", filename);
dnd_success = TRUE;
#ifndef OBJ_BUF_SIZE
#define OBJ_BUF_SIZE 1024
#endif
uint8_t obj_buf[OBJ_BUF_SIZE];
lv2_atom_forge_set_buffer(&ui->forge, obj_buf, OBJ_BUF_SIZE);
LV2_Atom* msg = write_set_file(&ui->forge, &ui->uris,
filename, strlen(filename));
ui->write(ui->controller, 0, lv2_atom_total_size(msg),
ui->uris.atom_eventTransfer,
msg);
g_free(filename);
}
}
gtk_drag_finish (context, dnd_success, delete_selection_data, time);
}
/* Emitted when the user releases (drops) the selection. It should check that
* the drop is over a valid part of the widget (if its a complex widget), and
* itself to return true if the operation should continue. Next choose the
* target type it wishes to ask the source for. Finally call gtk_drag_get_data
* which will emit "drag-data-get" on the source. */
static gboolean
drag_drop_handl
(GtkWidget *widget, GdkDragContext *context, gint x, gint y, guint time,
gpointer user_data)
{
gboolean is_valid_drop_site;
GdkAtom target_type;
const gchar *name = gtk_widget_get_name (widget);
/* Check to see if (x,y) is a valid drop site within widget */
is_valid_drop_site = TRUE;
/* If the source offers a target */
if (gdk_drag_context_list_targets (context))
{
/* Choose the best target type */
target_type = GDK_POINTER_TO_ATOM
(g_list_nth_data (gdk_drag_context_list_targets(context), TARGET_INT32));
/* Request the data from the source. */
gtk_drag_get_data
(
widget, /* will receive 'drag-data-received' signal */
context, /* represents the current state of the DnD */
target_type, /* the target type we want */
time /* time stamp */
);
}
/* No target offered by source => error */
else
{
is_valid_drop_site = FALSE;
}
return is_valid_drop_site;
}
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)
{
SamplerUI* ui = (SamplerUI*)malloc(sizeof(SamplerUI));
ui->map = NULL;
ui->write = write_function;
ui->controller = controller;
ui->box = NULL;
ui->button = NULL;
ui->label = NULL;
*widget = NULL;
for (int i = 0; features[i]; ++i) {
if (!strcmp(features[i]->URI, LV2_URID_URI "#map")) {
ui->map = (LV2_URID_Map*)features[i]->data;
}
}
if (!ui->map) {
fprintf(stderr, "sampler_ui: Host does not support urid:Map\n");
free(ui);
return NULL;
}
map_sampler_uris(ui->map, &ui->uris);
lv2_atom_forge_init(&ui->forge, ui->map);
ui->box = gtk_hbox_new(FALSE, 4);
ui->label = gtk_label_new("?");
ui->button = gtk_button_new_with_label("Load Sample");
gtk_box_pack_start(GTK_BOX(ui->box), ui->label, TRUE, TRUE, 4);
gtk_box_pack_start(GTK_BOX(ui->box), ui->button, FALSE, TRUE, 4);
g_signal_connect(ui->button, "clicked",
G_CALLBACK(on_load_clicked),
ui);
gtk_drag_dest_set
(
ui->box, /* widget that will accept a drop */
GTK_DEST_DEFAULT_MOTION /* default actions for dest on DnD */
| GTK_DEST_DEFAULT_HIGHLIGHT,
target_list, /* lists of target to support */
n_targets, /* size of list */
GDK_ACTION_COPY /* what to do with data after dropped */
);
/* All possible destination signals */
g_signal_connect (ui->box, "drag-data-received",
G_CALLBACK(drag_data_received_handl), ui);
g_signal_connect (ui->box, "drag-drop",
G_CALLBACK (drag_drop_handl), NULL);
*widget = ui->box;
return ui;
}
static void
cleanup(LV2UI_Handle handle)
{
SamplerUI* ui = (SamplerUI*)handle;
gtk_widget_destroy(ui->button);
free(ui);
}
static void
port_event(LV2UI_Handle handle,
uint32_t port_index,
uint32_t buffer_size,
uint32_t format,
const void* buffer)
{
SamplerUI* ui = (SamplerUI*)handle;
if (format == ui->uris.atom_eventTransfer) {
LV2_Atom* atom = (LV2_Atom*)buffer;
if (atom->type == ui->uris.atom_Blank) {
LV2_Atom_Object* obj = (LV2_Atom_Object*)atom;
const LV2_Atom* file_uri = read_set_file(&ui->uris, obj);
if (!file_uri) {
fprintf(stderr, "Unknown message sent to UI.\n");
return;
}
const char* uri = (const char*)LV2_ATOM_BODY(file_uri);
gtk_label_set_text(GTK_LABEL(ui->label), uri);
} else {
fprintf(stderr, "Unknown message type.\n");
}
} else {
fprintf(stderr, "Unknown format.\n");
}
}
const void*
extension_data(const char* uri)
{
return NULL;
}
static const LV2UI_Descriptor descriptor = {
SAMPLER_UI_URI,
instantiate,
cleanup,
port_event,
extension_data
};
LV2_SYMBOL_EXPORT
const LV2UI_Descriptor*
lv2ui_descriptor(uint32_t index)
{
switch (index) {
case 0:
return &descriptor;
default:
return NULL;
}
}