summaryrefslogtreecommitdiff
path: root/src/lv2_gui.cc
blob: 858e09762aab5de38845bbee5ba73a8e966e15bb (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            lv2.cc
 *
 *  Wed Jul 13 13:50:33 CEST 2011
 *  Copyright 2011 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of DrumGizmo.
 *
 *  DrumGizmo is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  DrumGizmo 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with DrumGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "lv2_gui.h"

#include <stdio.h>
#include <string.h>

#include <lv2/lv2plug.in/ns/ext/instance-access/instance-access.h>
#include <lv2/lv2plug.in/ns/extensions/ui/ui.h>

#include "lv2_instance.h"

// From: http://codesearch.google.com/#50sg5qT6WNE/src/lv2_ui_dssi.c
// git://repo.or.cz/nekobee.git/src/lv2_ui_dssi.c

#define DRUMGIZMO_UI_URI "http://drumgizmo.org/lv2-gui"

#include <plugingui.h>

/**
 * When LV2_EXTERNAL_UI_URI UI is instantiated, the returned
 * LV2UI_Widget handle must be cast to pointer to struct lv2_external_ui.
 * UI is created in invisible state.
 */
struct lv2_external_ui
{
  /**
   * Host calls this function regulary. UI library implementing the
   * callback may do IPC or redraw the UI.
   *
   * @param _this_ the UI context
   */
  void (* run)(struct lv2_external_ui * _this_);

  /**
   * Host calls this function to make the plugin UI visible.
   *
   * @param _this_ the UI context
   */
  void (* show)(struct lv2_external_ui * _this_);

  /**
   * Host calls this function to make the plugin UI invisible again.
   *
   * @param _this_ the UI context
   */
  void (* hide)(struct lv2_external_ui * _this_);
};

/** UI extension suitable for out-of-process UIs */
#define LV2_EXTERNAL_UI_URI "http://lv2plug.in/ns/extensions/ui#external"

/**
 * On UI instantiation, host must supply LV2_EXTERNAL_UI_URI
 * feature. LV2_Feature::data must be pointer to struct lv2_external_ui_host. */
struct lv2_external_ui_host
{
  /**
   * Callback that plugin UI will call
   * when UI (GUI window) is closed by user.
   * This callback wil; be called during execution of lv2_external_ui::run()
   * (i.e. not from background thread).
   *
   * After this callback is called, UI is defunct. Host must call
   * LV2UI_Descriptor::cleanup(). If host wants to make the UI visible
   * again UI must be reinstantiated.
   *
   * @param controller Host context associated with plugin UI, as
   * supplied to LV2UI_Descriptor::instantiate()
   */
  void (* ui_closed)(LV2UI_Controller controller);

  /**
   * Optional (may be NULL) "user friendly" identifier which the UI
   * may display to allow a user to easily associate this particular
   * UI instance with the correct plugin instance as it is represented
   * by the host (e.g. "track 1" or "channel 4").
   *
   * If supplied by host, the string will be referenced only during
   * LV2UI_Descriptor::instantiate()
   */
  const char * plugin_human_id;
};

struct DG_GUI {
  struct lv2_external_ui virt;

  LV2_Handle instance_handle;
  LV2_Extension_Data_Feature *data_access;
  DrumGizmo *instance;
  LV2UI_Controller controller;

  GUI::PluginGUI *gui;
  struct lv2_external_ui_host *ui_host_ptr;
};

static void ui_run(struct lv2_external_ui * _this_)
{
  struct DG_GUI *dggui = (struct DG_GUI *)_this_;
  dggui->gui->processEvents();
}

static void ui_show(struct lv2_external_ui * _this_)
{
  struct DG_GUI *dggui = (struct DG_GUI *)_this_;
  dggui->gui->show();
}

static void ui_hide(struct lv2_external_ui * _this_)
{
  struct DG_GUI *dggui = (struct DG_GUI *)_this_;
  if(dggui->gui) dggui->gui->hide();
}

static void closeHandler(void *ptr)
{
  struct DG_GUI *gui = (struct DG_GUI *)ptr;

  if(gui->ui_host_ptr && gui->ui_host_ptr->ui_closed) {
    gui->ui_host_ptr->ui_closed(gui->controller);
  }

  delete gui->gui;
  gui->gui = NULL;
}

static LV2UI_Handle ui_instantiate(const struct _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)
{
  printf("ui_instantiate\n");

  struct DG_GUI* pt = new struct DG_GUI;

  pt->ui_host_ptr = NULL;
  pt->controller = controller;

  while (*features != NULL) {
    std::string uri = (*features)->URI;
    void *data = (*features)->data;

    printf("DGUI: feature: %s\n", uri.c_str());

    if(uri == LV2_INSTANCE_ACCESS_URI) {
      pt->instance_handle = data;
    }

    if(uri == LV2_DATA_ACCESS_URI) {
      pt->data_access = (LV2_Extension_Data_Feature *)data;
    }

    if(uri == LV2_EXTERNAL_UI_URI) {
      pt->ui_host_ptr = (struct lv2_external_ui_host *)data;
    }
    features++;
  }

  LV2_DrumGizmo_Descriptor *dgd =
    (LV2_DrumGizmo_Descriptor *)(*pt->data_access->data_access)(PLUGIN_INSTANCE_URI);

  pt->instance = dgd->get_pci(pt->instance_handle);
  pt->virt.run = ui_run;
  pt->virt.show = ui_show;
  pt->virt.hide = ui_hide;
  pt->gui = new GUI::PluginGUI();
  pt->gui->setWindowClosedCallback(closeHandler, pt);

  *widget = (LV2UI_Widget)pt;

  return pt;
}

static void ui_cleanup(LV2UI_Handle ui)
{
  struct DG_GUI* pt = (struct DG_GUI*)ui;
  delete pt->gui;
  pt->gui = NULL;
  delete pt;
}

static void ui_port_event(LV2UI_Handle ui,
                          uint32_t port_index,
                          uint32_t buffer_size,
                          uint32_t format,
                          const void * buffer)
{
}

#ifdef __cplusplus
extern "C" {
#endif

static LV2UI_Descriptor descriptor = {
  DRUMGIZMO_UI_URI,
  ui_instantiate,
  ui_cleanup,
  ui_port_event,
  NULL
};

const LV2UI_Descriptor *lv2ui_descriptor(uint32_t index)
{
  if(index == 0) return &descriptor;
  return NULL;
}

#ifdef __cplusplus
}
#endif