1 |
/* |
/* |
2 |
* Copyright (C) 2007 Andreas Persson |
* Copyright (C) 2007-2009 Andreas Persson |
3 |
* |
* |
4 |
* This program is free software; you can redistribute it and/or |
* This program is free software; you can redistribute it and/or |
5 |
* modify it under the terms of the GNU General Public License as |
* modify it under the terms of the GNU General Public License as |
19 |
|
|
20 |
#include "gigedit.h" |
#include "gigedit.h" |
21 |
|
|
22 |
|
#include <glibmm/dispatcher.h> |
23 |
|
#include <glibmm/main.h> |
24 |
#include <gtkmm/main.h> |
#include <gtkmm/main.h> |
25 |
|
|
26 |
#include "mainwindow.h" |
#include "mainwindow.h" |
27 |
|
|
28 |
#include "global.h" |
#include "global.h" |
42 |
GigEditState(GigEdit* parent) : parent(parent) { } |
GigEditState(GigEdit* parent) : parent(parent) { } |
43 |
void run(gig::Instrument* pInstrument); |
void run(gig::Instrument* pInstrument); |
44 |
|
|
45 |
|
MainWindow* window; |
46 |
|
|
47 |
private: |
private: |
48 |
|
|
49 |
// simple condition variable abstraction |
// simple condition variable abstraction |
50 |
class Cond { |
class Cond { |
51 |
private: |
private: |
52 |
bool pred; |
bool pred; |
53 |
Glib::Mutex mutex; |
Glib::Threads::Mutex mutex; |
54 |
Glib::Cond cond; |
Glib::Threads::Cond cond; |
55 |
public: |
public: |
56 |
Cond() : pred(false) { } |
Cond() : pred(false) { } |
57 |
void signal() { |
void signal() { |
58 |
Glib::Mutex::Lock lock(mutex); |
Glib::Threads::Mutex::Lock lock(mutex); |
59 |
pred = true; |
pred = true; |
60 |
cond.signal(); |
cond.signal(); |
61 |
} |
} |
62 |
void wait() { |
void wait() { |
63 |
Glib::Mutex::Lock lock(mutex); |
Glib::Threads::Mutex::Lock lock(mutex); |
64 |
while (!pred) cond.wait(mutex); |
while (!pred) cond.wait(mutex); |
65 |
} |
} |
66 |
}; |
}; |
67 |
|
|
68 |
|
#ifdef OLD_THREADS |
69 |
static Glib::StaticMutex mutex; |
static Glib::StaticMutex mutex; |
70 |
|
#else |
71 |
|
static Glib::Threads::Mutex mutex; |
72 |
|
#endif |
73 |
static Glib::Dispatcher* dispatcher; |
static Glib::Dispatcher* dispatcher; |
74 |
static GigEditState* current; |
static GigEditState* current; |
75 |
|
|
80 |
Cond open; |
Cond open; |
81 |
Cond close; |
Cond close; |
82 |
gig::Instrument* instrument; |
gig::Instrument* instrument; |
|
MainWindow* window; |
|
83 |
|
|
84 |
void open_window(); |
void open_window(); |
85 |
void close_window(); |
void close_window(); |
86 |
}; |
}; |
87 |
|
|
88 |
|
#ifdef WIN32 |
89 |
|
HINSTANCE gigedit_dll_handle = 0; |
90 |
|
#endif |
91 |
|
|
92 |
void init_app() { |
void init_app() { |
93 |
static bool process_initialized = false; |
static bool process_initialized = false; |
94 |
if (!process_initialized) { |
if (!process_initialized) { |
97 |
setlocale(LC_ALL, ""); |
setlocale(LC_ALL, ""); |
98 |
|
|
99 |
#if HAVE_GETTEXT |
#if HAVE_GETTEXT |
100 |
|
|
101 |
|
#ifdef WIN32 |
102 |
|
#if GLIB_CHECK_VERSION(2, 16, 0) |
103 |
|
gchar* root = |
104 |
|
g_win32_get_package_installation_directory_of_module(gigedit_dll_handle); |
105 |
|
#else |
106 |
|
gchar* root = |
107 |
|
g_win32_get_package_installation_directory(NULL, NULL); |
108 |
|
#endif |
109 |
|
gchar* temp = g_build_filename(root, "/share/locale", NULL); |
110 |
|
g_free(root); |
111 |
|
gchar* localedir = g_win32_locale_filename_from_utf8(temp); |
112 |
|
g_free(temp); |
113 |
|
bindtextdomain(GETTEXT_PACKAGE, localedir); |
114 |
|
g_free(localedir); |
115 |
|
#else |
116 |
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR); |
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR); |
117 |
|
#endif |
118 |
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8"); |
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8"); |
119 |
textdomain(GETTEXT_PACKAGE); |
textdomain(GETTEXT_PACKAGE); |
120 |
#endif // HAVE_GETTEXT |
#endif // HAVE_GETTEXT |
121 |
|
|
122 |
|
#ifdef OLD_THREADS |
123 |
// make sure thread_init() is called once and ONLY once per process |
// make sure thread_init() is called once and ONLY once per process |
124 |
if (!Glib::thread_supported()) Glib::thread_init(); |
if (!Glib::thread_supported()) Glib::thread_init(); |
125 |
|
#endif |
126 |
process_initialized = true; |
process_initialized = true; |
127 |
} |
} |
128 |
} |
} |
155 |
mainwindow->signal_dimreg_changed().connect( |
mainwindow->signal_dimreg_changed().connect( |
156 |
gigedit->signal_dimreg_changed().make_slot() |
gigedit->signal_dimreg_changed().make_slot() |
157 |
); |
); |
158 |
|
mainwindow->signal_sample_changed().connect( |
159 |
|
gigedit->signal_sample_changed().make_slot() |
160 |
|
); |
161 |
mainwindow->signal_sample_ref_changed().connect( |
mainwindow->signal_sample_ref_changed().connect( |
162 |
gigedit->signal_sample_ref_changed().make_slot() |
gigedit->signal_sample_ref_changed().make_slot() |
163 |
); |
); |
164 |
|
mainwindow->signal_keyboard_key_hit().connect( |
165 |
|
gigedit->signal_keyboard_key_hit().make_slot() |
166 |
|
); |
167 |
|
mainwindow->signal_keyboard_key_released().connect( |
168 |
|
gigedit->signal_keyboard_key_released().make_slot() |
169 |
|
); |
170 |
} |
} |
171 |
|
|
172 |
} |
} // namespace |
173 |
|
|
174 |
|
GigEdit::GigEdit() { |
175 |
|
state = NULL; |
176 |
|
} |
177 |
|
|
178 |
int GigEdit::run(int argc, char* argv[]) { |
int GigEdit::run(int argc, char* argv[]) { |
179 |
init_app(); |
init_app(); |
190 |
init_app(); |
init_app(); |
191 |
|
|
192 |
GigEditState state(this); |
GigEditState state(this); |
193 |
|
this->state = &state; |
194 |
state.run(pInstrument); |
state.run(pInstrument); |
195 |
|
this->state = NULL; |
196 |
return 0; |
return 0; |
197 |
} |
} |
198 |
|
|
199 |
|
void GigEdit::on_note_on_event(int key, int velocity) { |
200 |
|
if (!this->state) return; |
201 |
|
GigEditState* state = (GigEditState*) this->state; |
202 |
|
state->window->signal_note_on().emit(key, velocity); |
203 |
|
} |
204 |
|
|
205 |
|
void GigEdit::on_note_off_event(int key, int velocity) { |
206 |
|
if (!this->state) return; |
207 |
|
GigEditState* state = (GigEditState*) this->state; |
208 |
|
state->window->signal_note_off().emit(key, velocity); |
209 |
|
} |
210 |
|
|
211 |
sigc::signal<void, gig::File*>& GigEdit::signal_file_structure_to_be_changed() { |
sigc::signal<void, gig::File*>& GigEdit::signal_file_structure_to_be_changed() { |
212 |
return file_structure_to_be_changed_signal; |
return file_structure_to_be_changed_signal; |
213 |
} |
} |
240 |
return dimreg_changed_signal; |
return dimreg_changed_signal; |
241 |
} |
} |
242 |
|
|
243 |
|
sigc::signal<void, gig::Sample*>& GigEdit::signal_sample_changed() { |
244 |
|
return sample_changed_signal; |
245 |
|
} |
246 |
|
|
247 |
sigc::signal<void, gig::Sample*/*old*/, gig::Sample*/*new*/>& GigEdit::signal_sample_ref_changed() { |
sigc::signal<void, gig::Sample*/*old*/, gig::Sample*/*new*/>& GigEdit::signal_sample_ref_changed() { |
248 |
return sample_ref_changed_signal; |
return sample_ref_changed_signal; |
249 |
} |
} |
250 |
|
|
251 |
|
sigc::signal<void, int/*key*/, int/*velocity*/>& GigEdit::signal_keyboard_key_hit() { |
252 |
|
return keyboard_key_hit_signal; |
253 |
|
} |
254 |
|
|
255 |
|
sigc::signal<void, int/*key*/, int/*velocity*/>& GigEdit::signal_keyboard_key_released() { |
256 |
|
return keyboard_key_released_signal; |
257 |
|
} |
258 |
|
|
259 |
|
#ifdef OLD_THREADS |
260 |
Glib::StaticMutex GigEditState::mutex = GLIBMM_STATIC_MUTEX_INIT; |
Glib::StaticMutex GigEditState::mutex = GLIBMM_STATIC_MUTEX_INIT; |
261 |
|
#else |
262 |
|
Glib::Threads::Mutex GigEditState::mutex; |
263 |
|
#endif |
264 |
Glib::Dispatcher* GigEditState::dispatcher = 0; |
Glib::Dispatcher* GigEditState::dispatcher = 0; |
265 |
GigEditState* GigEditState::current = 0; |
GigEditState* GigEditState::current = 0; |
266 |
|
|
305 |
static bool main_loop_started = false; |
static bool main_loop_started = false; |
306 |
if (!main_loop_started) { |
if (!main_loop_started) { |
307 |
Cond initialized; |
Cond initialized; |
308 |
|
#ifdef OLD_THREADS |
309 |
Glib::Thread::create( |
Glib::Thread::create( |
310 |
sigc::bind(sigc::ptr_fun(&GigEditState::main_loop_run), |
sigc::bind(sigc::ptr_fun(&GigEditState::main_loop_run), |
311 |
&initialized), |
&initialized), |
312 |
false); |
false); |
313 |
|
#else |
314 |
|
Glib::Threads::Thread::create( |
315 |
|
sigc::bind(sigc::ptr_fun(&GigEditState::main_loop_run), |
316 |
|
&initialized)); |
317 |
|
#endif |
318 |
initialized.wait(); |
initialized.wait(); |
319 |
main_loop_started = true; |
main_loop_started = true; |
320 |
} |
} |
325 |
mutex.unlock(); |
mutex.unlock(); |
326 |
close.wait(); // sleep until window is closed |
close.wait(); // sleep until window is closed |
327 |
} |
} |
328 |
|
|
329 |
|
#if defined(WIN32) |
330 |
|
extern "C" { |
331 |
|
BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) |
332 |
|
{ |
333 |
|
switch (reason) { |
334 |
|
case DLL_PROCESS_ATTACH: |
335 |
|
gigedit_dll_handle = instance; |
336 |
|
break; |
337 |
|
} |
338 |
|
return TRUE; |
339 |
|
} |
340 |
|
} |
341 |
|
#endif |