1 |
/* |
/* |
2 |
* Copyright (C) 2007, 2008 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 <gtkmm/main.h> |
#include <glibmm/dispatcher.h> |
23 |
#include <glibmm/main.h> |
#include <glibmm/main.h> |
24 |
|
#include <gtkmm/main.h> |
25 |
|
|
26 |
#include "mainwindow.h" |
#include "mainwindow.h" |
27 |
|
|
28 |
#include "global.h" |
#include "global.h" |
29 |
|
|
|
GigEditJob::GigEditJob() { |
|
|
_msecs = 100; // 100ms by default |
|
|
} |
|
|
|
|
|
int GigEditJob::msecs() { |
|
|
return _msecs; |
|
|
} |
|
|
|
|
30 |
namespace { |
namespace { |
31 |
|
|
32 |
// State for a gigedit thread. |
// State for a gigedit thread. |
42 |
GigEditState(GigEdit* parent) : parent(parent) { } |
GigEditState(GigEdit* parent) : parent(parent) { } |
43 |
void run(gig::Instrument* pInstrument); |
void run(gig::Instrument* pInstrument); |
44 |
|
|
|
static std::vector< GigEditJob* > timeoutJobs; |
|
45 |
MainWindow* window; |
MainWindow* window; |
46 |
|
|
47 |
private: |
private: |
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 |
|
|
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 |
} // namespace |
196 |
return 0; |
return 0; |
197 |
} |
} |
198 |
|
|
|
void GigEdit::add_timeout_job(GigEditJob* job) { |
|
|
GigEditState::timeoutJobs.push_back(job); |
|
|
} |
|
|
|
|
199 |
void GigEdit::on_note_on_event(int key, int velocity) { |
void GigEdit::on_note_on_event(int key, int velocity) { |
200 |
if (!this->state) return; |
if (!this->state) return; |
201 |
GigEditState* state = (GigEditState*) this->state; |
GigEditState* state = (GigEditState*) this->state; |
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; |
|
std::vector<GigEditJob*> GigEditState::timeoutJobs; |
|
266 |
|
|
267 |
void GigEditState::open_window_static() { |
void GigEditState::open_window_static() { |
268 |
GigEditState* c = GigEditState::current; |
GigEditState* c = GigEditState::current; |
296 |
dispatcher->connect(sigc::ptr_fun(&GigEditState::open_window_static)); |
dispatcher->connect(sigc::ptr_fun(&GigEditState::open_window_static)); |
297 |
initialized->signal(); |
initialized->signal(); |
298 |
|
|
|
for (int i = 0; i < GigEditState::timeoutJobs.size(); i++) { |
|
|
GigEditJob* job = timeoutJobs[i]; |
|
|
const Glib::RefPtr<Glib::TimeoutSource> timeout_source = |
|
|
Glib::TimeoutSource::create(job->msecs()); |
|
|
timeout_source->connect( |
|
|
sigc::mem_fun(*job, &GigEditJob::runGigEditJob) |
|
|
); |
|
|
timeout_source->attach(Glib::MainContext::get_default()); |
|
|
} |
|
|
|
|
299 |
main_loop.run(); |
main_loop.run(); |
300 |
} |
} |
301 |
|
|
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 |
Glib::Thread::create( |
Glib::Threads::Thread::create( |
309 |
sigc::bind(sigc::ptr_fun(&GigEditState::main_loop_run), |
sigc::bind(sigc::ptr_fun(&GigEditState::main_loop_run), |
310 |
&initialized), |
&initialized), |
311 |
false); |
false); |
319 |
mutex.unlock(); |
mutex.unlock(); |
320 |
close.wait(); // sleep until window is closed |
close.wait(); // sleep until window is closed |
321 |
} |
} |
322 |
|
|
323 |
|
#if defined(WIN32) |
324 |
|
extern "C" { |
325 |
|
BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) |
326 |
|
{ |
327 |
|
switch (reason) { |
328 |
|
case DLL_PROCESS_ATTACH: |
329 |
|
gigedit_dll_handle = instance; |
330 |
|
break; |
331 |
|
} |
332 |
|
return TRUE; |
333 |
|
} |
334 |
|
} |
335 |
|
#endif |