fix crash when copy'ing latent plugins
[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 <fst.h>
24 #include "ardour/plugin_insert.h"
25 #include "ardour/windows_vst_plugin.h"
26
27 #include "windows_vst_plugin_ui.h"
28
29 #ifdef PLATFORM_WINDOWS
30 #include <gdk/gdkwin32.h>
31 #elif defined __APPLE__
32 // TODO
33 #else
34 #include <gdk/gdkx.h>
35 #endif
36
37 using namespace Gtk;
38 using namespace ARDOUR;
39 using namespace PBD;
40
41 WindowsVSTPluginUI::WindowsVSTPluginUI (boost::shared_ptr<PluginInsert> pi, boost::shared_ptr<VSTPlugin> vp, GtkWidget *parent)
42         : VSTPluginUI (pi, vp)
43 {
44
45 #ifdef GDK_WINDOWING_WIN32
46         gtk_widget_realize(parent);
47         void* hWndHost = gdk_win32_drawable_get_handle(parent->window);
48
49         fst_run_editor (_vst->state(), hWndHost);
50         // TODO pack a placeholder (compare to VSTPluginUI::VSTPluginUI X11 socket)
51         // have placeholder use VSTPluginUI::get_preferred_height(), width()
52         // TODO pack plugin_analysis_expander at the bottom.
53 #else
54         fst_run_editor (_vst->state(), NULL);
55         pack_start (plugin_analysis_expander, true, true);
56 #endif
57 }
58
59 WindowsVSTPluginUI::~WindowsVSTPluginUI ()
60 {
61         // plugin destructor destroys the custom GUI, via Windows fun-and-games,
62         // and then our PluginUIWindow does the rest
63 }
64
65 int
66 WindowsVSTPluginUI::package (Gtk::Window& win)
67 {
68         VSTPluginUI::package (win);
69
70         fst_move_window_into_view (_vst->state ());
71
72         return 0;
73 }
74
75 void
76 WindowsVSTPluginUI::forward_key_event (GdkEventKey* ev)
77 {
78         if (ev->type != GDK_KEY_PRESS) {
79                 return;
80         }
81
82         VSTState* fst = _vst->state ();
83         pthread_mutex_lock (&fst->lock);
84
85         if (fst->n_pending_keys == (sizeof (fst->pending_keys) * sizeof (VSTKey))) {
86                 /* buffer full */
87                 return;
88         }
89
90         int special_windows_key = 0;
91         int character_windows_key = 0;
92
93         switch (ev->keyval) {
94         case GDK_Left:
95                 special_windows_key = 0x25;
96                 break;
97         case GDK_Right:
98                 special_windows_key = 0x27;
99                 break;
100         case GDK_Up:
101                 special_windows_key = 0x26;
102                 break;
103         case GDK_Down:
104                 special_windows_key = 0x28;
105                 break;
106         case GDK_Return:
107         case GDK_KP_Enter:
108                 special_windows_key = 0xd;
109                 break;
110         default:
111                 character_windows_key = ev->keyval;
112                 break;
113         }
114
115         fst->pending_keys[fst->n_pending_keys].special = special_windows_key;
116         fst->pending_keys[fst->n_pending_keys].character = character_windows_key;
117         fst->n_pending_keys++;
118
119         pthread_mutex_unlock (&fst->lock);
120 }
121
122 int
123 WindowsVSTPluginUI::get_XID ()
124 {
125         return _vst->state()->xid;
126 }
127
128 #ifdef GDK_WINDOWING_X11
129 typedef int (*error_handler_t)( Display *, XErrorEvent *);
130 static Display *the_gtk_display;
131 static error_handler_t wine_error_handler;
132 static error_handler_t gtk_error_handler;
133
134 static int
135 fst_xerror_handler (Display* disp, XErrorEvent* ev)
136 {
137         if (disp == the_gtk_display) {
138                 printf ("relaying error to gtk\n");
139                 return gtk_error_handler (disp, ev);
140         } else {
141                 printf( "relaying error to wine\n" );
142                 return wine_error_handler (disp, ev);
143         }
144 }
145 #endif
146
147 void
148 windows_vst_gui_init (int *argc, char **argv[])
149 {
150         gtk_init (argc, argv);
151
152 #ifdef GDK_WINDOWING_X11
153         wine_error_handler = XSetErrorHandler (NULL);
154         the_gtk_display = gdk_x11_display_get_xdisplay (gdk_display_get_default());
155         gtk_error_handler = XSetErrorHandler (fst_xerror_handler);
156 #endif
157 }
158