a-fluidsynth: implement LV2_BANKPATCH__notify
[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 <gtkmm.h>
21 #include <gtk/gtk.h>
22 #include <gtk/gtksocket.h>
23 #include "gtkmm2ext/gui_thread.h"
24 #include "fst.h"
25 #include "ardour/plugin_insert.h"
26 #include "ardour/windows_vst_plugin.h"
27
28 #include "windows_vst_plugin_ui.h"
29
30 #ifdef PLATFORM_WINDOWS
31 #include <gdk/gdkwin32.h>
32 #elif defined __APPLE__
33 // TODO
34 #else
35 #include <gdk/gdkx.h>
36 #endif
37
38 using namespace Gtk;
39 using namespace ARDOUR;
40 using namespace PBD;
41
42 WindowsVSTPluginUI::WindowsVSTPluginUI (boost::shared_ptr<PluginInsert> pi, boost::shared_ptr<VSTPlugin> vp, GtkWidget *parent)
43         : VSTPluginUI (pi, vp)
44 {
45
46 #ifdef GDK_WINDOWING_WIN32
47         gtk_widget_realize(parent);
48         void* hWndHost = gdk_win32_drawable_get_handle(parent->window);
49
50         fst_run_editor (_vst->state(), hWndHost);
51         // TODO pack a placeholder (compare to VSTPluginUI::VSTPluginUI X11 socket)
52         // have placeholder use VSTPluginUI::get_preferred_height(), width()
53         // TODO pack plugin_analysis_expander at the bottom.
54 #else
55         fst_run_editor (_vst->state(), NULL);
56         pack_start (plugin_analysis_expander, true, true);
57 #endif
58 }
59
60 WindowsVSTPluginUI::~WindowsVSTPluginUI ()
61 {
62         fst_destroy_editor (_vst->state());
63
64         // plugin destructor destroys the custom GUI, via Windows fun-and-games,
65         // and then our PluginUIWindow does the rest
66 }
67
68 void
69 WindowsVSTPluginUI::top_box_allocated (Gtk::Allocation& a)
70 {
71         int h = a.get_height() + 12; // 2 * 6px spacing
72         if (_vst->state()->voffset != h) {
73 #ifndef NDEBUG
74                 printf("WindowsVSTPluginUI:: update voffset to %d px\n", h);
75 #endif
76                 _vst->state()->voffset = h;
77                 resize_callback ();
78         }
79 }
80
81 void
82 WindowsVSTPluginUI::resize_callback ()
83 {
84         void* gtk_parent_window = _vst->state()->gtk_window_parent;
85         if (gtk_parent_window) {
86                 int width  = _vst->state()->width + _vst->state()->hoffset;
87                 int height = _vst->state()->height + _vst->state()->voffset;
88 #ifndef NDEBUG
89                 printf ("WindowsVSTPluginUI::resize_callback %d x %d\n", width, height);
90 #endif
91                 set_size_request (width, height);
92                 ((Gtk::Window*) gtk_parent_window)->set_size_request (width, height);
93                 ((Gtk::Window*) gtk_parent_window)->resize (width, height);
94                 fst_move_window_into_view (_vst->state ());
95         }
96 }
97
98 int
99 WindowsVSTPluginUI::package (Gtk::Window& win)
100 {
101 #ifndef NDEBUG
102         printf ("WindowsVSTPluginUI::package\n");
103 #endif
104         VSTPluginUI::package (win);
105         _vst->state()->gtk_window_parent = (void*) (&win);
106
107         _vst->VSTSizeWindow.connect (_resize_connection, invalidator (*this), boost::bind (&WindowsVSTPluginUI::resize_callback, this), gui_context());
108
109         resize_callback ();
110
111         return 0;
112 }
113
114 void
115 WindowsVSTPluginUI::forward_key_event (GdkEventKey* ev)
116 {
117         if (ev->type != GDK_KEY_PRESS) {
118                 return;
119         }
120
121         VSTState* fst = _vst->state ();
122         pthread_mutex_lock (&fst->lock);
123
124         if (fst->n_pending_keys == (sizeof (fst->pending_keys) * sizeof (VSTKey))) {
125                 /* buffer full */
126                 return;
127         }
128
129         int special_windows_key = 0;
130         int character_windows_key = 0;
131
132         switch (ev->keyval) {
133         case GDK_Left:
134                 special_windows_key = 0x25;
135                 break;
136         case GDK_Right:
137                 special_windows_key = 0x27;
138                 break;
139         case GDK_Up:
140                 special_windows_key = 0x26;
141                 break;
142         case GDK_Down:
143                 special_windows_key = 0x28;
144                 break;
145         case GDK_Return:
146         case GDK_KP_Enter:
147                 special_windows_key = 0xd;
148                 break;
149         default:
150                 character_windows_key = ev->keyval;
151                 break;
152         }
153
154         fst->pending_keys[fst->n_pending_keys].special = special_windows_key;
155         fst->pending_keys[fst->n_pending_keys].character = character_windows_key;
156         fst->n_pending_keys++;
157
158         pthread_mutex_unlock (&fst->lock);
159 }
160
161 int
162 WindowsVSTPluginUI::get_XID ()
163 {
164         return _vst->state()->xid;
165 }
166
167 #ifdef GDK_WINDOWING_X11
168 typedef int (*error_handler_t)( Display *, XErrorEvent *);
169 static Display *the_gtk_display;
170 static error_handler_t wine_error_handler;
171 static error_handler_t gtk_error_handler;
172
173 static int
174 fst_xerror_handler (Display* disp, XErrorEvent* ev)
175 {
176         if (disp == the_gtk_display) {
177                 printf ("relaying error to gtk\n");
178                 return gtk_error_handler (disp, ev);
179         } else {
180                 printf( "relaying error to wine\n" );
181                 return wine_error_handler (disp, ev);
182         }
183 }
184 #endif
185
186 void
187 windows_vst_gui_init (int *argc, char **argv[])
188 {
189         gtk_init (argc, argv);
190
191 #ifdef GDK_WINDOWING_X11
192         wine_error_handler = XSetErrorHandler (NULL);
193         the_gtk_display = gdk_x11_display_get_xdisplay (gdk_display_get_default());
194         gtk_error_handler = XSetErrorHandler (fst_xerror_handler);
195 #endif
196 }
197