Fix windows VST build.
[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<VSTPlugin> vp)
35         : VSTPluginUI (pi, vp)
36 {
37         fst_run_editor (_vst->state());
38
39         pack_start (plugin_analysis_expander, true, true);
40 }
41
42 WindowsVSTPluginUI::~WindowsVSTPluginUI ()
43 {
44         // plugin destructor destroys the custom GUI, via Windows fun-and-games,
45         // and then our PluginUIWindow does the rest
46 }
47
48 int
49 WindowsVSTPluginUI::package (Gtk::Window& win)
50 {
51         VSTPluginUI::package (win);
52
53         fst_move_window_into_view (_vst->state ());
54
55         return 0;
56 }
57
58 void
59 WindowsVSTPluginUI::forward_key_event (GdkEventKey* ev)
60 {
61         if (ev->type != GDK_KEY_PRESS) {
62                 return;
63         }
64
65         VSTState* fst = _vst->state ();
66         pthread_mutex_lock (&fst->lock);
67
68         if (fst->n_pending_keys == (sizeof (fst->pending_keys) * sizeof (VSTKey))) {
69                 /* buffer full */
70                 return;
71         }
72         
73         int special_windows_key = 0;
74         int character_windows_key = 0;
75         
76         switch (ev->keyval) {
77         case GDK_Left:
78                 special_windows_key = 0x25;
79                 break;
80         case GDK_Right:
81                 special_windows_key = 0x27;
82                 break;
83         case GDK_Up:
84                 special_windows_key = 0x26;
85                 break;
86         case GDK_Down:
87                 special_windows_key = 0x28;
88                 break;
89         case GDK_Return:
90         case GDK_KP_Enter:
91                 special_windows_key = 0xd;
92                 break;
93         default:
94                 character_windows_key = ev->keyval;
95                 break;
96         }
97         
98         fst->pending_keys[fst->n_pending_keys].special = special_windows_key;
99         fst->pending_keys[fst->n_pending_keys].character = character_windows_key;
100         fst->n_pending_keys++;
101         
102         pthread_mutex_unlock (&fst->lock);
103 }
104
105 int
106 WindowsVSTPluginUI::get_XID ()
107 {
108         return _vst->state()->xid;
109 }
110
111 typedef int (*error_handler_t)( Display *, XErrorEvent *);
112 static Display *the_gtk_display;
113 static error_handler_t wine_error_handler;
114 static error_handler_t gtk_error_handler;
115
116 static int
117 fst_xerror_handler (Display* disp, XErrorEvent* ev)
118 {
119         if (disp == the_gtk_display) {
120                 printf ("relaying error to gtk\n");
121                 return gtk_error_handler (disp, ev);
122         } else {
123                 printf( "relaying error to wine\n" );
124                 return wine_error_handler (disp, ev);
125         }
126 }
127
128 void
129 windows_vst_gui_init (int *argc, char **argv[])
130 {
131         wine_error_handler = XSetErrorHandler (NULL);
132         gtk_init (argc, argv);
133         the_gtk_display = gdk_x11_display_get_xdisplay (gdk_display_get_default());
134         gtk_error_handler = XSetErrorHandler (fst_xerror_handler);
135 }
136