1 |
/* |
2 |
* Copyright (C) 2007, 2008 Andreas Persson |
3 |
* |
4 |
* This program is free software; you can redistribute it and/or |
5 |
* modify it under the terms of the GNU General Public License as |
6 |
* published by the Free Software Foundation; either version 2, or (at |
7 |
* your option) any later version. |
8 |
* |
9 |
* This program is distributed in the hope that it will be useful, but |
10 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
* General Public License for more details. |
13 |
* |
14 |
* You should have received a copy of the GNU General Public License |
15 |
* along with program; see the file COPYING. If not, write to the Free |
16 |
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
17 |
* 02110-1301 USA. |
18 |
*/ |
19 |
|
20 |
#include "gigedit.h" |
21 |
|
22 |
#include <gtkmm/main.h> |
23 |
#include <glibmm/main.h> |
24 |
#include "mainwindow.h" |
25 |
|
26 |
#include "global.h" |
27 |
|
28 |
namespace { |
29 |
|
30 |
// State for a gigedit thread. |
31 |
// |
32 |
// This class is only used when gigedit is run as a plugin and makes |
33 |
// sure that there's only one Gtk::Main event loop. The event loop is |
34 |
// started in a separate thread. The plugin thread just dispatches an |
35 |
// event to the main loop to open a window and then goes to sleep |
36 |
// until the window is closed. |
37 |
// |
38 |
class GigEditState : public sigc::trackable { |
39 |
public: |
40 |
GigEditState(GigEdit* parent) : parent(parent) { } |
41 |
void run(gig::Instrument* pInstrument); |
42 |
|
43 |
MainWindow* window; |
44 |
|
45 |
private: |
46 |
|
47 |
// simple condition variable abstraction |
48 |
class Cond { |
49 |
private: |
50 |
bool pred; |
51 |
Glib::Mutex mutex; |
52 |
Glib::Cond cond; |
53 |
public: |
54 |
Cond() : pred(false) { } |
55 |
void signal() { |
56 |
Glib::Mutex::Lock lock(mutex); |
57 |
pred = true; |
58 |
cond.signal(); |
59 |
} |
60 |
void wait() { |
61 |
Glib::Mutex::Lock lock(mutex); |
62 |
while (!pred) cond.wait(mutex); |
63 |
} |
64 |
}; |
65 |
|
66 |
static Glib::StaticMutex mutex; |
67 |
static Glib::Dispatcher* dispatcher; |
68 |
static GigEditState* current; |
69 |
|
70 |
static void main_loop_run(Cond* intialized); |
71 |
static void open_window_static(); |
72 |
|
73 |
GigEdit* parent; |
74 |
Cond open; |
75 |
Cond close; |
76 |
gig::Instrument* instrument; |
77 |
|
78 |
void open_window(); |
79 |
void close_window(); |
80 |
}; |
81 |
|
82 |
void init_app() { |
83 |
static bool process_initialized = false; |
84 |
if (!process_initialized) { |
85 |
std::cout << "Initializing 3rd party services needed by gigedit.\n" |
86 |
<< std::flush; |
87 |
setlocale(LC_ALL, ""); |
88 |
|
89 |
#if HAVE_GETTEXT |
90 |
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR); |
91 |
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8"); |
92 |
textdomain(GETTEXT_PACKAGE); |
93 |
#endif // HAVE_GETTEXT |
94 |
|
95 |
// make sure thread_init() is called once and ONLY once per process |
96 |
if (!Glib::thread_supported()) Glib::thread_init(); |
97 |
|
98 |
process_initialized = true; |
99 |
} |
100 |
} |
101 |
|
102 |
void connect_signals(GigEdit* gigedit, MainWindow* mainwindow) { |
103 |
// the signals of the "GigEdit" class are actually just proxies, that |
104 |
// is they simply forward the signals of the internal classes to the |
105 |
// outer world |
106 |
mainwindow->signal_file_structure_to_be_changed().connect( |
107 |
gigedit->signal_file_structure_to_be_changed().make_slot() |
108 |
); |
109 |
mainwindow->signal_file_structure_changed().connect( |
110 |
gigedit->signal_file_structure_changed().make_slot() |
111 |
); |
112 |
mainwindow->signal_samples_to_be_removed().connect( |
113 |
gigedit->signal_samples_to_be_removed().make_slot() |
114 |
); |
115 |
mainwindow->signal_samples_removed().connect( |
116 |
gigedit->signal_samples_removed().make_slot() |
117 |
); |
118 |
mainwindow->signal_region_to_be_changed().connect( |
119 |
gigedit->signal_region_to_be_changed().make_slot() |
120 |
); |
121 |
mainwindow->signal_region_changed().connect( |
122 |
gigedit->signal_region_changed().make_slot() |
123 |
); |
124 |
mainwindow->signal_dimreg_to_be_changed().connect( |
125 |
gigedit->signal_dimreg_to_be_changed().make_slot() |
126 |
); |
127 |
mainwindow->signal_dimreg_changed().connect( |
128 |
gigedit->signal_dimreg_changed().make_slot() |
129 |
); |
130 |
mainwindow->signal_sample_ref_changed().connect( |
131 |
gigedit->signal_sample_ref_changed().make_slot() |
132 |
); |
133 |
mainwindow->signal_keyboard_key_hit().connect( |
134 |
gigedit->signal_keyboard_key_hit().make_slot() |
135 |
); |
136 |
mainwindow->signal_keyboard_key_released().connect( |
137 |
gigedit->signal_keyboard_key_released().make_slot() |
138 |
); |
139 |
} |
140 |
|
141 |
} // namespace |
142 |
|
143 |
GigEdit::GigEdit() { |
144 |
state = NULL; |
145 |
} |
146 |
|
147 |
int GigEdit::run(int argc, char* argv[]) { |
148 |
init_app(); |
149 |
|
150 |
Gtk::Main kit(argc, argv); |
151 |
MainWindow window; |
152 |
connect_signals(this, &window); |
153 |
if (argc >= 2) window.load_file(argv[1]); |
154 |
kit.run(window); |
155 |
return 0; |
156 |
} |
157 |
|
158 |
int GigEdit::run(gig::Instrument* pInstrument) { |
159 |
init_app(); |
160 |
|
161 |
GigEditState state(this); |
162 |
this->state = &state; |
163 |
state.run(pInstrument); |
164 |
this->state = NULL; |
165 |
return 0; |
166 |
} |
167 |
|
168 |
void GigEdit::on_note_on_event(int key, int velocity) { |
169 |
if (!this->state) return; |
170 |
GigEditState* state = (GigEditState*) this->state; |
171 |
state->window->signal_note_on().emit(key, velocity); |
172 |
} |
173 |
|
174 |
void GigEdit::on_note_off_event(int key, int velocity) { |
175 |
if (!this->state) return; |
176 |
GigEditState* state = (GigEditState*) this->state; |
177 |
state->window->signal_note_off().emit(key, velocity); |
178 |
} |
179 |
|
180 |
sigc::signal<void, gig::File*>& GigEdit::signal_file_structure_to_be_changed() { |
181 |
return file_structure_to_be_changed_signal; |
182 |
} |
183 |
|
184 |
sigc::signal<void, gig::File*>& GigEdit::signal_file_structure_changed() { |
185 |
return file_structure_changed_signal; |
186 |
} |
187 |
|
188 |
sigc::signal<void, std::list<gig::Sample*> >& GigEdit::signal_samples_to_be_removed() { |
189 |
return samples_to_be_removed_signal; |
190 |
} |
191 |
|
192 |
sigc::signal<void>& GigEdit::signal_samples_removed() { |
193 |
return samples_removed_signal; |
194 |
} |
195 |
|
196 |
sigc::signal<void, gig::Region*>& GigEdit::signal_region_to_be_changed() { |
197 |
return region_to_be_changed_signal; |
198 |
} |
199 |
|
200 |
sigc::signal<void, gig::Region*>& GigEdit::signal_region_changed() { |
201 |
return region_changed_signal; |
202 |
} |
203 |
|
204 |
sigc::signal<void, gig::DimensionRegion*>& GigEdit::signal_dimreg_to_be_changed() { |
205 |
return dimreg_to_be_changed_signal; |
206 |
} |
207 |
|
208 |
sigc::signal<void, gig::DimensionRegion*>& GigEdit::signal_dimreg_changed() { |
209 |
return dimreg_changed_signal; |
210 |
} |
211 |
|
212 |
sigc::signal<void, gig::Sample*/*old*/, gig::Sample*/*new*/>& GigEdit::signal_sample_ref_changed() { |
213 |
return sample_ref_changed_signal; |
214 |
} |
215 |
|
216 |
sigc::signal<void, int/*key*/, int/*velocity*/>& GigEdit::signal_keyboard_key_hit() { |
217 |
return keyboard_key_hit_signal; |
218 |
} |
219 |
|
220 |
sigc::signal<void, int/*key*/, int/*velocity*/>& GigEdit::signal_keyboard_key_released() { |
221 |
return keyboard_key_released_signal; |
222 |
} |
223 |
|
224 |
Glib::StaticMutex GigEditState::mutex = GLIBMM_STATIC_MUTEX_INIT; |
225 |
Glib::Dispatcher* GigEditState::dispatcher = 0; |
226 |
GigEditState* GigEditState::current = 0; |
227 |
|
228 |
void GigEditState::open_window_static() { |
229 |
GigEditState* c = GigEditState::current; |
230 |
c->open.signal(); |
231 |
c->open_window(); |
232 |
} |
233 |
|
234 |
void GigEditState::open_window() { |
235 |
window = new MainWindow(); |
236 |
|
237 |
connect_signals(parent, window); |
238 |
if (instrument) window->load_instrument(instrument); |
239 |
|
240 |
window->signal_hide().connect(sigc::mem_fun(this, |
241 |
&GigEditState::close_window)); |
242 |
window->present(); |
243 |
} |
244 |
|
245 |
void GigEditState::close_window() { |
246 |
delete window; |
247 |
close.signal(); |
248 |
} |
249 |
|
250 |
void GigEditState::main_loop_run(Cond* initialized) { |
251 |
int argc = 1; |
252 |
const char* argv_c[] = { "gigedit" }; |
253 |
char** argv = const_cast<char**>(argv_c); |
254 |
Gtk::Main main_loop(argc, argv); |
255 |
|
256 |
dispatcher = new Glib::Dispatcher(); |
257 |
dispatcher->connect(sigc::ptr_fun(&GigEditState::open_window_static)); |
258 |
initialized->signal(); |
259 |
|
260 |
main_loop.run(); |
261 |
} |
262 |
|
263 |
void GigEditState::run(gig::Instrument* pInstrument) { |
264 |
mutex.lock(); // lock access to static variables |
265 |
|
266 |
static bool main_loop_started = false; |
267 |
if (!main_loop_started) { |
268 |
Cond initialized; |
269 |
Glib::Thread::create( |
270 |
sigc::bind(sigc::ptr_fun(&GigEditState::main_loop_run), |
271 |
&initialized), |
272 |
false); |
273 |
initialized.wait(); |
274 |
main_loop_started = true; |
275 |
} |
276 |
instrument = pInstrument; |
277 |
current = this; |
278 |
dispatcher->emit(); |
279 |
open.wait(); // wait until the GUI thread has read current |
280 |
mutex.unlock(); |
281 |
close.wait(); // sleep until window is closed |
282 |
} |