/[svn]/gigedit/trunk/src/gigedit/gigedit.cpp
ViewVC logotype

Contents of /gigedit/trunk/src/gigedit/gigedit.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1823 - (show annotations) (download)
Sun Dec 28 09:58:05 2008 UTC (12 years, 8 months ago) by persson
File size: 8816 byte(s)
* Windows fixes: find translation files after install. Don't open
  a console window.

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
91 #ifdef WIN32
92 #if GLIB_CHECK_VERSION(2, 16, 0)
93 gchar* root =
94 g_win32_get_package_installation_directory_of_module(NULL);
95 #else
96 gchar* root =
97 g_win32_get_package_installation_directory(NULL, NULL);
98 #endif
99 gchar* temp = g_build_filename(root, "/share/locale", NULL);
100 g_free(root);
101 gchar* localedir = g_win32_locale_filename_from_utf8(temp);
102 g_free(temp);
103 bindtextdomain(GETTEXT_PACKAGE, localedir);
104 g_free(localedir);
105 #else
106 bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
107 #endif
108 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
109 textdomain(GETTEXT_PACKAGE);
110 #endif // HAVE_GETTEXT
111
112 // make sure thread_init() is called once and ONLY once per process
113 if (!Glib::thread_supported()) Glib::thread_init();
114
115 process_initialized = true;
116 }
117 }
118
119 void connect_signals(GigEdit* gigedit, MainWindow* mainwindow) {
120 // the signals of the "GigEdit" class are actually just proxies, that
121 // is they simply forward the signals of the internal classes to the
122 // outer world
123 mainwindow->signal_file_structure_to_be_changed().connect(
124 gigedit->signal_file_structure_to_be_changed().make_slot()
125 );
126 mainwindow->signal_file_structure_changed().connect(
127 gigedit->signal_file_structure_changed().make_slot()
128 );
129 mainwindow->signal_samples_to_be_removed().connect(
130 gigedit->signal_samples_to_be_removed().make_slot()
131 );
132 mainwindow->signal_samples_removed().connect(
133 gigedit->signal_samples_removed().make_slot()
134 );
135 mainwindow->signal_region_to_be_changed().connect(
136 gigedit->signal_region_to_be_changed().make_slot()
137 );
138 mainwindow->signal_region_changed().connect(
139 gigedit->signal_region_changed().make_slot()
140 );
141 mainwindow->signal_dimreg_to_be_changed().connect(
142 gigedit->signal_dimreg_to_be_changed().make_slot()
143 );
144 mainwindow->signal_dimreg_changed().connect(
145 gigedit->signal_dimreg_changed().make_slot()
146 );
147 mainwindow->signal_sample_ref_changed().connect(
148 gigedit->signal_sample_ref_changed().make_slot()
149 );
150 mainwindow->signal_keyboard_key_hit().connect(
151 gigedit->signal_keyboard_key_hit().make_slot()
152 );
153 mainwindow->signal_keyboard_key_released().connect(
154 gigedit->signal_keyboard_key_released().make_slot()
155 );
156 }
157
158 } // namespace
159
160 GigEdit::GigEdit() {
161 state = NULL;
162 }
163
164 int GigEdit::run(int argc, char* argv[]) {
165 init_app();
166
167 Gtk::Main kit(argc, argv);
168 MainWindow window;
169 connect_signals(this, &window);
170 if (argc >= 2) window.load_file(argv[1]);
171 kit.run(window);
172 return 0;
173 }
174
175 int GigEdit::run(gig::Instrument* pInstrument) {
176 init_app();
177
178 GigEditState state(this);
179 this->state = &state;
180 state.run(pInstrument);
181 this->state = NULL;
182 return 0;
183 }
184
185 void GigEdit::on_note_on_event(int key, int velocity) {
186 if (!this->state) return;
187 GigEditState* state = (GigEditState*) this->state;
188 state->window->signal_note_on().emit(key, velocity);
189 }
190
191 void GigEdit::on_note_off_event(int key, int velocity) {
192 if (!this->state) return;
193 GigEditState* state = (GigEditState*) this->state;
194 state->window->signal_note_off().emit(key, velocity);
195 }
196
197 sigc::signal<void, gig::File*>& GigEdit::signal_file_structure_to_be_changed() {
198 return file_structure_to_be_changed_signal;
199 }
200
201 sigc::signal<void, gig::File*>& GigEdit::signal_file_structure_changed() {
202 return file_structure_changed_signal;
203 }
204
205 sigc::signal<void, std::list<gig::Sample*> >& GigEdit::signal_samples_to_be_removed() {
206 return samples_to_be_removed_signal;
207 }
208
209 sigc::signal<void>& GigEdit::signal_samples_removed() {
210 return samples_removed_signal;
211 }
212
213 sigc::signal<void, gig::Region*>& GigEdit::signal_region_to_be_changed() {
214 return region_to_be_changed_signal;
215 }
216
217 sigc::signal<void, gig::Region*>& GigEdit::signal_region_changed() {
218 return region_changed_signal;
219 }
220
221 sigc::signal<void, gig::DimensionRegion*>& GigEdit::signal_dimreg_to_be_changed() {
222 return dimreg_to_be_changed_signal;
223 }
224
225 sigc::signal<void, gig::DimensionRegion*>& GigEdit::signal_dimreg_changed() {
226 return dimreg_changed_signal;
227 }
228
229 sigc::signal<void, gig::Sample*/*old*/, gig::Sample*/*new*/>& GigEdit::signal_sample_ref_changed() {
230 return sample_ref_changed_signal;
231 }
232
233 sigc::signal<void, int/*key*/, int/*velocity*/>& GigEdit::signal_keyboard_key_hit() {
234 return keyboard_key_hit_signal;
235 }
236
237 sigc::signal<void, int/*key*/, int/*velocity*/>& GigEdit::signal_keyboard_key_released() {
238 return keyboard_key_released_signal;
239 }
240
241 Glib::StaticMutex GigEditState::mutex = GLIBMM_STATIC_MUTEX_INIT;
242 Glib::Dispatcher* GigEditState::dispatcher = 0;
243 GigEditState* GigEditState::current = 0;
244
245 void GigEditState::open_window_static() {
246 GigEditState* c = GigEditState::current;
247 c->open.signal();
248 c->open_window();
249 }
250
251 void GigEditState::open_window() {
252 window = new MainWindow();
253
254 connect_signals(parent, window);
255 if (instrument) window->load_instrument(instrument);
256
257 window->signal_hide().connect(sigc::mem_fun(this,
258 &GigEditState::close_window));
259 window->present();
260 }
261
262 void GigEditState::close_window() {
263 delete window;
264 close.signal();
265 }
266
267 void GigEditState::main_loop_run(Cond* initialized) {
268 int argc = 1;
269 const char* argv_c[] = { "gigedit" };
270 char** argv = const_cast<char**>(argv_c);
271 Gtk::Main main_loop(argc, argv);
272
273 dispatcher = new Glib::Dispatcher();
274 dispatcher->connect(sigc::ptr_fun(&GigEditState::open_window_static));
275 initialized->signal();
276
277 main_loop.run();
278 }
279
280 void GigEditState::run(gig::Instrument* pInstrument) {
281 mutex.lock(); // lock access to static variables
282
283 static bool main_loop_started = false;
284 if (!main_loop_started) {
285 Cond initialized;
286 Glib::Thread::create(
287 sigc::bind(sigc::ptr_fun(&GigEditState::main_loop_run),
288 &initialized),
289 false);
290 initialized.wait();
291 main_loop_started = true;
292 }
293 instrument = pInstrument;
294 current = this;
295 dispatcher->emit();
296 open.wait(); // wait until the GUI thread has read current
297 mutex.unlock();
298 close.wait(); // sleep until window is closed
299 }

  ViewVC Help
Powered by ViewVC