merge with master, primarily for adrian's maximise-mixer change
[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 <gtkmm.h>
22 #include <gtk/gtk.h>
23 #include <gtk/gtksocket.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)
42         : VSTPluginUI (pi, vp)
43 {
44
45 #ifdef GDK_WINDOWING_WIN32
46
47 #if 0 // TODO verify window vs vbox-widget WRT to plugin_analysis_expander
48         GtkWindow* wobj = GTK_WINDOW(gtk_widget_get_toplevel(this->gobj()));
49 #else
50         GtkVBox* wobj = this->gobj();
51 #endif
52
53         gtk_widget_realize(GTK_WIDGET(wobj));
54         void* hWndHost = gdk_win32_drawable_get_handle(GTK_WIDGET(wobj)->window);
55
56         fst_run_editor (_vst->state(), hWndHost);
57 #else
58         fst_run_editor (_vst->state(), NULL);
59         pack_start (plugin_analysis_expander, true, true);
60 #endif
61 }
62
63 WindowsVSTPluginUI::~WindowsVSTPluginUI ()
64 {
65         // plugin destructor destroys the custom GUI, via Windows fun-and-games,
66         // and then our PluginUIWindow does the rest
67 }
68
69 int
70 WindowsVSTPluginUI::package (Gtk::Window& win)
71 {
72         VSTPluginUI::package (win);
73
74         fst_move_window_into_view (_vst->state ());
75
76         return 0;
77 }
78
79 void
80 WindowsVSTPluginUI::forward_key_event (GdkEventKey* ev)
81 {
82         if (ev->type != GDK_KEY_PRESS) {
83                 return;
84         }
85
86         VSTState* fst = _vst->state ();
87         pthread_mutex_lock (&fst->lock);
88
89         if (fst->n_pending_keys == (sizeof (fst->pending_keys) * sizeof (VSTKey))) {
90                 /* buffer full */
91                 return;
92         }
93         
94         int special_windows_key = 0;
95         int character_windows_key = 0;
96         
97         switch (ev->keyval) {
98         case GDK_Left:
99                 special_windows_key = 0x25;
100                 break;
101         case GDK_Right:
102                 special_windows_key = 0x27;
103                 break;
104         case GDK_Up:
105                 special_windows_key = 0x26;
106                 break;
107         case GDK_Down:
108                 special_windows_key = 0x28;
109                 break;
110         case GDK_Return:
111         case GDK_KP_Enter:
112                 special_windows_key = 0xd;
113                 break;
114         default:
115                 character_windows_key = ev->keyval;
116                 break;
117         }
118         
119         fst->pending_keys[fst->n_pending_keys].special = special_windows_key;
120         fst->pending_keys[fst->n_pending_keys].character = character_windows_key;
121         fst->n_pending_keys++;
122         
123         pthread_mutex_unlock (&fst->lock);
124 }
125
126 int
127 WindowsVSTPluginUI::get_XID ()
128 {
129         return _vst->state()->xid;
130 }
131
132 #ifdef GDK_WINDOWING_X11
133 typedef int (*error_handler_t)( Display *, XErrorEvent *);
134 static Display *the_gtk_display;
135 static error_handler_t wine_error_handler;
136 static error_handler_t gtk_error_handler;
137
138 static int
139 fst_xerror_handler (Display* disp, XErrorEvent* ev)
140 {
141         if (disp == the_gtk_display) {
142                 printf ("relaying error to gtk\n");
143                 return gtk_error_handler (disp, ev);
144         } else {
145                 printf( "relaying error to wine\n" );
146                 return wine_error_handler (disp, ev);
147         }
148 }
149 #endif
150
151 void
152 windows_vst_gui_init (int *argc, char **argv[])
153 {
154         gtk_init (argc, argv);
155
156 #ifdef GDK_WINDOWING_X11
157         wine_error_handler = XSetErrorHandler (NULL);
158         the_gtk_display = gdk_x11_display_get_xdisplay (gdk_display_get_default());
159         gtk_error_handler = XSetErrorHandler (fst_xerror_handler);
160 #endif
161 }
162