Rename windows VST stuff with a Windows prefix.
[ardour.git] / gtk2_ardour / windows_vst_plugin_ui.cc
1 /*
2     Copyright (C) 2004 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <fst.h>
21 #include <gtk/gtk.h>
22 #include <gtk/gtksocket.h>
23 #include "ardour/plugin_insert.h"
24 #include "ardour/windows_vst_plugin.h"
25
26 #include "windows_vst_plugin_ui.h"
27
28 #include <gdk/gdkx.h>
29
30 using namespace Gtk;
31 using namespace ARDOUR;
32 using namespace PBD;
33
34 WindowsVSTPluginUI::WindowsVSTPluginUI (boost::shared_ptr<PluginInsert> pi, boost::shared_ptr<WindowsVSTPlugin> vp)
35         : PlugUIBase (pi),
36           vst (vp)
37 {
38         fst_run_editor (vst->fst());
39
40         preset_box.set_spacing (6);
41         preset_box.set_border_width (6);
42         preset_box.pack_end (focus_button, false, false);
43         preset_box.pack_end (bypass_button, false, false, 10);
44         preset_box.pack_end (delete_button, false, false);
45         preset_box.pack_end (save_button, false, false);
46         preset_box.pack_end (add_button, false, false);
47         preset_box.pack_end (_preset_box, false, false);
48
49         bypass_button.set_active (!insert->active());
50
51         pack_start (preset_box, false, false);
52         pack_start (socket, true, true);
53         pack_start (plugin_analysis_expander, true, true);
54 }
55
56 WindowsVSTPluginUI::~WindowsVSTPluginUI ()
57 {
58         // plugin destructor destroys the custom GUI, via Windows fun-and-games,
59         // and then our PluginUIWindow does the rest
60 }
61
62 void
63 WindowsVSTPluginUI::preset_selected ()
64 {
65         socket.grab_focus ();
66         PlugUIBase::preset_selected ();
67 }
68
69 int
70 WindowsVSTPluginUI::get_preferred_height ()
71 {
72         return vst->fst()->height;
73 }
74
75 int
76 WindowsVSTPluginUI::get_preferred_width ()
77 {
78         return vst->fst()->width;
79 }
80
81 int
82 WindowsVSTPluginUI::package (Gtk::Window& win)
83 {
84         /* forward configure events to plugin window */
85
86         win.signal_configure_event().connect (sigc::bind (sigc::mem_fun (*this, &WindowsVSTPluginUI::configure_handler), &socket), false);
87
88         /*
89            this assumes that the window's owner understands the XEmbed protocol.
90         */
91
92         socket.add_id (fst_get_XID (vst->fst()));
93
94         fst_move_window_into_view (vst->fst());
95
96         return 0;
97 }
98
99 bool
100 WindowsVSTPluginUI::configure_handler (GdkEventConfigure* ev, Gtk::Socket *socket)
101 {
102         XEvent event;
103         gint x, y;
104         GdkWindow* w;
105
106         if (socket == 0 || ((w = socket->gobj()->plug_window) == 0)) {
107                 return false;
108         }
109
110         event.xconfigure.type = ConfigureNotify;
111         event.xconfigure.event = GDK_WINDOW_XWINDOW (w);
112         event.xconfigure.window = GDK_WINDOW_XWINDOW (w);
113
114         /* The ICCCM says that synthetic events should have root relative
115          * coordinates. We still aren't really ICCCM compliant, since
116          * we don't send events when the real toplevel is moved.
117          */
118         gdk_error_trap_push ();
119         gdk_window_get_origin (w, &x, &y);
120         gdk_error_trap_pop ();
121
122         event.xconfigure.x = x;
123         event.xconfigure.y = y;
124         event.xconfigure.width = GTK_WIDGET(socket->gobj())->allocation.width;
125         event.xconfigure.height = GTK_WIDGET(socket->gobj())->allocation.height;
126
127         event.xconfigure.border_width = 0;
128         event.xconfigure.above = None;
129         event.xconfigure.override_redirect = False;
130
131         gdk_error_trap_push ();
132         XSendEvent (GDK_WINDOW_XDISPLAY (w), GDK_WINDOW_XWINDOW (w), False, StructureNotifyMask, &event);
133         gdk_error_trap_pop ();
134
135         return false;
136 }
137
138 void
139 WindowsVSTPluginUI::forward_key_event (GdkEventKey* ev)
140 {
141         if (ev->type == GDK_KEY_PRESS) {
142
143                 FST* fst = vst->fst ();
144                 pthread_mutex_lock (&fst->lock);
145
146                 if (fst->n_pending_keys == (sizeof (fst->pending_keys) * sizeof (FSTKey))) {
147                         /* buffer full */
148                         return;
149                 }
150
151                 int special_windows_key = 0;
152                 int character_windows_key = 0;
153
154                 switch (ev->keyval) {
155                 case GDK_Left:
156                         special_windows_key = 0x25;
157                         break;
158                 case GDK_Right:
159                         special_windows_key = 0x27;
160                         break;
161                 case GDK_Up:
162                         special_windows_key = 0x26;
163                         break;
164                 case GDK_Down:
165                         special_windows_key = 0x28;
166                         break;
167                 case GDK_Return:
168                 case GDK_KP_Enter:
169                         special_windows_key = 0xd;
170                         break;
171                 default:
172                         character_windows_key = ev->keyval;
173                         break;
174                 }
175
176                 fst->pending_keys[fst->n_pending_keys].special = special_windows_key;
177                 fst->pending_keys[fst->n_pending_keys].character = character_windows_key;
178                 fst->n_pending_keys++;
179
180                 pthread_mutex_unlock (&fst->lock);
181         }
182 }
183
184 typedef int (*error_handler_t)( Display *, XErrorEvent *);
185 static Display *the_gtk_display;
186 static error_handler_t wine_error_handler;
187 static error_handler_t gtk_error_handler;
188
189 static int
190 fst_xerror_handler( Display *disp, XErrorEvent *ev )
191 {
192         if (disp == the_gtk_display) {
193                 printf ("relaying error to gtk\n");
194                 return gtk_error_handler (disp, ev);
195         } else {
196                 printf( "relaying error to wine\n" );
197                 return wine_error_handler (disp, ev);
198         }
199 }
200
201 void
202 windows_vst_gui_init (int *argc, char **argv[])
203 {
204         wine_error_handler = XSetErrorHandler (NULL);
205         gtk_init (argc, argv);
206         the_gtk_display = gdk_x11_display_get_xdisplay (gdk_display_get_default());
207         gtk_error_handler = XSetErrorHandler( fst_xerror_handler );
208 }
209