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