summaryrefslogtreecommitdiff
path: root/gtk2_ardour/lxvst_pluginui.cc
blob: dc62d907ae111b3a98186a0073654f1da217d4c5 (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
/*
    Copyright (C) 2004 Paul Davis 

    This program 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.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/******************************************************************/
/*linuxDSP - pluginui variant for LXVST (native Linux VST) Plugins*/
/******************************************************************/

#include <ardour/vstfx.h>
#include <gtk/gtk.h>
#include <gtk/gtksocket.h>
#include <ardour/processor.h>
#include <ardour/lxvst_plugin.h>

#include "ardour_ui.h"
#include "plugin_ui.h"
#include "lxvst_plugin_ui.h"

#include <gdk/gdkx.h>

#define LXVST_H_FIDDLE 40

using namespace Gtk;
using namespace ARDOUR;
using namespace PBD;

LXVSTPluginUI::LXVSTPluginUI (boost::shared_ptr<PluginInsert> pi, boost::shared_ptr<LXVSTPlugin> lxvp)
	: PlugUIBase (pi),
	  lxvst (lxvp)
{
	create_preset_store ();

	vstfx_run_editor (lxvst->vstfx());

	preset_box.set_spacing (6);
	preset_box.set_border_width (6);
	preset_box.pack_end (bypass_button, false, false, 10);
	preset_box.pack_end (save_button, false, false);
	preset_box.pack_end (lxvst_preset_combo, false, false);

	lxvst_preset_combo.signal_changed().connect (mem_fun (*this, &LXVSTPluginUI::preset_chosen));

	bypass_button.set_active (!insert->active());
	
	pack_start (preset_box, false, false);
	pack_start (socket, true, true);
}

LXVSTPluginUI::~LXVSTPluginUI ()
{

	_screen_update_connection.disconnect();	
	
	// plugin destructor destroys the custom GUI, via the vstfx engine,
	// and then our PluginUIWindow does the rest
}


bool
LXVSTPluginUI::start_updating (GdkEventAny* ignored)
{
	_screen_update_connection.disconnect();
	_screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect 
			(mem_fun(*this, &LXVSTPluginUI::resize_callback));
	return false;
}

bool
LXVSTPluginUI::stop_updating (GdkEventAny* ignored)
{
	_screen_update_connection.disconnect();
	return false;
}


void
LXVSTPluginUI::resize_callback()
{
	/*We could maybe use this to resize the plugin GTK parent window
	if required*/
	
	if(lxvst->vstfx()->want_resize)
	{
		int new_height = lxvst->vstfx()->height;
		int new_width = lxvst->vstfx()->width;
		
		void* gtk_parent_window = lxvst->vstfx()->extra_data;
		
		if(gtk_parent_window)
			((Gtk::Window*)gtk_parent_window)->resize(new_width, new_height + LXVST_H_FIDDLE);
		
		lxvst->vstfx()->want_resize = 0;
	}
}

void
LXVSTPluginUI::preset_selected ()
{
	socket.grab_focus ();
	PlugUIBase::preset_selected ();
}

void
LXVSTPluginUI::preset_chosen ()
{
	// we can't dispatch directly here, too many plugins only expects one GUI thread.
	
	lxvst->vstfx()->want_program = lxvst_preset_combo.get_active_row_number ();
	socket.grab_focus ();
}

int
LXVSTPluginUI::get_preferred_height ()
{	
	/*FIXME*/
	
	/*We have to return the required height of the plugin UI window +  a fiddle factor
	because we can't know how big the preset menu bar is until the window is realised
	and we can't realise it until we have told it how big we would like it to be
	which we can't do until it is realised etc*/

	return (lxvst->vstfx()->height) + LXVST_H_FIDDLE; //May not be 40 for all screen res etc
}

int
LXVSTPluginUI::get_preferred_width ()
{
	return lxvst->vstfx()->width;
}

int
LXVSTPluginUI::package (Gtk::Window& win)
{
	/* forward configure events to plugin window */

	win.signal_configure_event().connect (bind (mem_fun (*this, &LXVSTPluginUI::configure_handler), &socket), false);
	
	/*Map the UI start and stop updating events to 'Map' events on the Window*/
	
	win.signal_map_event().connect (mem_fun (*this, &LXVSTPluginUI::start_updating));
	win.signal_unmap_event().connect (mem_fun (*this, &LXVSTPluginUI::stop_updating));

	/* this assumes that the window's owner understands the XEmbed protocol. */
	
	socket.add_id (vstfx_get_XID (lxvst->vstfx()));

	vstfx_move_window_into_view (lxvst->vstfx());
	
	lxvst->vstfx()->extra_data = (void*)(&win);
	lxvst->vstfx()->want_resize = 0;

	return 0;
}

bool
LXVSTPluginUI::configure_handler (GdkEventConfigure* ev, Gtk::Socket *socket)
{
	XEvent event;
	gint x, y;
	GdkWindow* w;

	if (socket == 0 || ((w = socket->gobj()->plug_window) == 0)) {
		return false;
	}

	event.xconfigure.type = ConfigureNotify;
	event.xconfigure.event = GDK_WINDOW_XWINDOW (w);
	event.xconfigure.window = GDK_WINDOW_XWINDOW (w);

	/* The ICCCM says that synthetic events should have root relative
	 * coordinates. We still aren't really ICCCM compliant, since
	 * we don't send events when the real toplevel is moved.
	 */
	gdk_error_trap_push ();
	gdk_window_get_origin (w, &x, &y);
	gdk_error_trap_pop ();

	event.xconfigure.x = x;
	event.xconfigure.y = y;
	event.xconfigure.width = GTK_WIDGET(socket->gobj())->allocation.width;
	event.xconfigure.height = GTK_WIDGET(socket->gobj())->allocation.height;

	event.xconfigure.border_width = 0;
	event.xconfigure.above = None;
	event.xconfigure.override_redirect = False;

	gdk_error_trap_push ();
	XSendEvent (GDK_WINDOW_XDISPLAY (w), GDK_WINDOW_XWINDOW (w), False, StructureNotifyMask, &event);
	gdk_error_trap_pop ();

	return false;
}

void
LXVSTPluginUI::forward_key_event (GdkEventKey* ev)
{
	std::cerr << "LXVSTPluginUI : keypress forwarding to linuxVSTs unsupported" << std::endl;
}


void
LXVSTPluginUI::create_preset_store ()
{
	VSTFX* vstfx = lxvst->vstfx();
	
	int vst_version = vstfx->plugin->dispatcher (vstfx->plugin, effGetVstVersion, 0, 0, NULL, 0.0f);

	preset_model = ListStore::create (preset_columns);

	for (int i = 0; i < vstfx->plugin->numPrograms; ++i) {
		char buf[100];
		TreeModel::Row row = *(preset_model->append());
	
		snprintf (buf, 90, "preset %d", i);
	
		if (vst_version >= 2) {
			vstfx->plugin->dispatcher (vstfx->plugin, 29, i, 0, buf, 0.0);
		}
		
		row[preset_columns.name] = buf;
		row[preset_columns.number] = i;
	}
	
	if (vstfx->plugin->numPrograms > 0) {
		vstfx->plugin->dispatcher( vstfx->plugin, effSetProgram, 0, 0, NULL, 0.0 );
	}
	
	lxvst_preset_combo.set_model (preset_model);

	CellRenderer* renderer = manage (new CellRendererText());
	lxvst_preset_combo.pack_start (*renderer, true);
	lxvst_preset_combo.add_attribute (*renderer, "text", 0);

	if (lxvst->vstfx()->current_program != -1) {
		lxvst_preset_combo.set_active (lxvst->vstfx()->current_program);
	} else {
		lxvst_preset_combo.set_active (0);
	}
}

typedef int (*error_handler_t)( Display *, XErrorEvent *);
static Display *the_gtk_display;
static error_handler_t vstfx_error_handler;
static error_handler_t gtk_error_handler;

static int 
gtk_xerror_handler( Display *disp, XErrorEvent *ev )
{
	std::cerr << "** ERROR ** LXVSTPluginUI : Trapped an X Window System Error" << std::endl;
	
	return 0;
}

void
gui_init (int *argc, char **argv[])
{
	vstfx_error_handler = XSetErrorHandler (NULL);
	gtk_init (argc, argv);
	the_gtk_display = gdk_x11_display_get_xdisplay (gdk_display_get_default());
	gtk_error_handler = XSetErrorHandler( gtk_xerror_handler );
}