root/trunk/src/cli/main.cpp @ 2253

Revision 2253, 12.4 KB (checked in by jeremyz, 23 months ago)

Filesystem: drumkit_path=>drumkit_path_search, drumkit_location=>drumkit_dir_search + fix comments

Line 
1/*
2 * A headless attempt for hydrogen
3 * Copyright(c) 2009 by Sebastian Moors
4 *
5 * http://www.hydrogen-music.org
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY, without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 *
21 */
22
23#include <QLibraryInfo>
24#include <hydrogen/config.h>
25#include <hydrogen/version.h>
26#include <getopt.h>
27
28
29
30#ifdef H2CORE_HAVE_LASH
31#include <hydrogen/LashClient.h>
32#endif
33
34#include <hydrogen/basics/song.h>
35#include <hydrogen/midiMap.h>
36#include <hydrogen/audio_engine.h>
37#include <hydrogen/hydrogen.h>
38#include <hydrogen/globals.h>
39#include <hydrogen/event_queue.h>
40#include <hydrogen/Preferences.h>
41#include <hydrogen/h2_exception.h>
42#include <hydrogen/helpers/filesystem.h>
43
44#include <iostream>
45using namespace std;
46
47void showInfo();
48void showUsage();
49
50
51#define HAS_ARG 1
52static struct option long_opts[] = {
53        {"driver", required_argument, NULL, 'd'},
54        {"song", required_argument, NULL, 's'},
55        {"version", 0, NULL, 'v'},
56        {"nosplash", 0, NULL, 'n'},
57        {"verbose", optional_argument, NULL, 'V'},
58        {"help", 0, NULL, 'h'},
59        {"install", required_argument, NULL, 'i'},
60        {"drumkit", required_argument, NULL, 'k'},
61        {0, 0, 0, 0},
62};
63
64#define NELEM(a) ( sizeof(a)/sizeof((a)[0]) )
65
66
67
68
69int main(int argc, char *argv[])
70{
71        try {
72                // Options...
73                char *cp;
74                struct option *op;
75                char opts[NELEM(long_opts) * 3 + 1];
76
77                // Build up the short option QString
78                cp = opts;
79                for (op = long_opts; op < &long_opts[NELEM(long_opts)]; op++) {
80                        *cp++ = op->val;
81                        if (op->has_arg)
82                                *cp++ = ':';
83                        if (op->has_arg == optional_argument )
84                                *cp++ = ':';  // gets another one
85                }
86
87
88                // Deal with the options
89                QString songFilename;
90                bool bNoSplash = false;
91                QString sSelectedDriver;
92                bool showVersionOpt = false;
93                const char* logLevelOpt = "Error";
94                bool showHelpOpt = false;
95                QString drumkitName;
96                QString drumkitToLoad;
97
98                int c;
99                for (;;) {
100                        c = getopt_long(argc, argv, opts, long_opts, NULL);
101                        if (c == -1)
102                                break;
103
104                        switch(c) {
105                                case 'd':
106                                        sSelectedDriver = QString::fromLocal8Bit(optarg);
107                                        break;
108
109                                case 's':
110                                        songFilename = QString::fromLocal8Bit(optarg);
111                                        break;
112
113                                case 'i':
114                                        //install h2drumkit
115                                        drumkitName = QString::fromLocal8Bit(optarg);
116                                        break;
117
118                                case 'k':
119                                        //load Drumkit
120                                        drumkitToLoad = QString::fromLocal8Bit(optarg);
121                                        break;
122
123                                case 'v':
124                                        showVersionOpt = true;
125                                        break;
126
127                                case 'V':
128                                        if( optarg ) {
129                                                logLevelOpt = optarg;
130                                        } else {
131                                                logLevelOpt = "Warning";
132                                        }
133                                        break;
134                                case 'n':
135                                        bNoSplash = true;
136                                        break;
137
138                                case 'h':
139                                case '?':
140                                        showHelpOpt = true;
141                                        break;
142                        }
143                }
144
145                if( showVersionOpt ) {
146                        std::cout << H2Core::get_version() << std::endl;
147                        exit(0);
148                }
149                showInfo();
150                if( showHelpOpt ) {
151                        showUsage();
152                        exit(0);
153                }
154
155                // Man your battle stations... this is not a drill.
156                H2Core::Logger* logger = H2Core::Logger::bootstrap( H2Core::Logger::parse_log_level( logLevelOpt ) );
157                H2Core::Object::bootstrap( logger, logger->should_log( H2Core::Logger::Debug ) );
158                H2Core::Filesystem::bootstrap( logger );
159                MidiMap::create_instance();
160                H2Core::Preferences::create_instance();
161                // See below for H2Core::Hydrogen.
162
163
164                ___INFOLOG( QString("Using QT version ") + QString( qVersion() ) );
165                ___INFOLOG( "Using data path: " + H2Core::Filesystem::sys_data_path() );
166
167
168                H2Core::Preferences *pPref = H2Core::Preferences::get_instance();
169
170#ifdef H2CORE_HAVE_LASH
171
172                LashClient::create_instance("hydrogen", "Hydrogen", &argc, &argv);
173                LashClient* lashClient = LashClient::get_instance();
174
175#endif
176                if( ! drumkitName.isEmpty() ){
177                    H2Core::Drumkit::install( drumkitName );
178                    exit(0);
179                }
180
181                if (sSelectedDriver == "auto") {
182                        pPref->m_sAudioDriver = "Auto";
183                }
184                else if (sSelectedDriver == "jack") {
185                        pPref->m_sAudioDriver = "Jack";
186                }
187                else if ( sSelectedDriver == "oss" ) {
188                        pPref->m_sAudioDriver = "Oss";
189                }
190                else if ( sSelectedDriver == "alsa" ) {
191                        pPref->m_sAudioDriver = "Alsa";
192                }
193                if (sSelectedDriver == "CoreAudio") {
194                        pPref->m_sAudioDriver = "CoreAudio";
195                }
196
197
198
199
200#ifdef H2CORE_HAVE_LASH
201        if ( H2Core::Preferences::get_instance()->useLash() ){
202                if (lashClient->isConnected())
203                {
204                        lash_event_t* lash_event = lashClient->getNextEvent();
205                        if (lash_event && lash_event_get_type(lash_event) == LASH_Restore_File)
206                        {
207                                // notify client that this project was not a new one
208                                lashClient->setNewProject(false);
209
210                                songFilename = "";
211                                songFilename.append( QString::fromLocal8Bit(lash_event_get_string(lash_event)) );
212                                songFilename.append("/hydrogen.h2song");
213
214//                              H2Core::Logger::get_instance()->log("[LASH] Restore file: " + songFilename);
215
216                                lash_event_destroy(lash_event);
217                        }
218                        else if (lash_event)
219                        {
220//                              H2Core::Logger::get_instance()->log("[LASH] ERROR: Instead of restore file got event: " + lash_event_get_type(lash_event));
221                                lash_event_destroy(lash_event);
222                        }
223                }
224        }
225#endif
226                H2Core::Hydrogen::create_instance();
227
228
229
230                // Load default song
231                H2Core::Song *song = NULL;
232                if ( !songFilename.isEmpty() ) {
233                        song = H2Core::Song::load( songFilename );
234                        if (song == NULL) {
235                                song = H2Core::Song::get_empty_song();
236                                song->set_filename( "" );
237                        }
238                }
239                else {
240                        H2Core::Preferences *pref = H2Core::Preferences::get_instance();
241                        bool restoreLastSong = pref->isRestoreLastSongEnabled();
242                        QString filename = pref->getLastSongFilename();
243                        if ( restoreLastSong && ( !filename.isEmpty() )) {
244                                song = H2Core::Song::load( filename );
245                                if (song == NULL) {
246                                        ___INFOLOG("Starting with empty song");
247                                        song = H2Core::Song::get_empty_song();
248                                        song->set_filename( "" );
249                                }
250                        }
251                        else {
252                                song = H2Core::Song::get_empty_song();
253                                song->set_filename( "" );
254                        }
255                }
256
257
258                H2Core::Hydrogen::get_instance()->setSong( song );
259                H2Core::Preferences::get_instance()->setLastSongFilename(  songFilename);
260
261
262
263                if( ! drumkitToLoad.isEmpty() ){
264                    H2Core::Drumkit* drumkitInfo = H2Core::Drumkit::load( H2Core::Filesystem::drumkit_path_search( drumkitToLoad ), true );
265                    H2Core::Hydrogen::get_instance()->loadDrumkit( drumkitInfo );
266                }
267
268                while( true ){
269
270                }
271
272
273                delete pPref;
274                delete H2Core::EventQueue::get_instance();
275                delete H2Core::AudioEngine::get_instance();
276
277                delete MidiMap::get_instance();
278                delete ActionManager::get_instance();
279
280                ___INFOLOG( "Quitting..." );
281                cout << "\nBye..." << endl;
282                delete H2Core::Logger::get_instance();
283
284                int nObj = H2Core::Object::objects_count();
285                if (nObj != 0) {
286                        std::cerr << "\n\n\n " << nObj << " alive objects\n\n" << std::endl << std::endl;
287                        H2Core::Object::write_objects_map_to_cerr();
288                }
289
290
291        }
292        catch ( const H2Core::H2Exception& ex ) {
293                std::cerr << "[main] Exception: " << ex.what() << std::endl;
294        }
295        catch (...) {
296                std::cerr << "[main] Unknown exception X-(" << std::endl;
297        }
298
299        return 0;
300}
301
302
303
304/**
305 * Show some information
306 */
307void showInfo()
308{
309        cout << "\nHydrogen " + H2Core::get_version() + " [" + __DATE__ + "]  [http://www.hydrogen-music.org]" << endl;
310        cout << "Copyright 2002-2008 Alessandro Cominu" << endl;
311//      _INFOLOG( "Compiled modules: " + QString(COMPILED_FEATURES) << endl;
312
313        if ( H2Core::Object::count_active() ) {
314                cout << "\nObject counting active" << endl;
315        }
316
317        cout << "\nHydrogen comes with ABSOLUTELY NO WARRANTY" << endl;
318        cout << "This is free software, and you are welcome to redistribute it" << endl;
319        cout << "under certain conditions. See the file COPYING for details\n" << endl;
320}
321
322
323
324/**
325 * Show the correct usage
326 */
327void showUsage()
328{
329        std::cout << "Usage: hydrogen [-v] [-h] -s file" << std::endl;
330        std::cout << "   -d, --driver AUDIODRIVER - Use the selected audio driver (jack, alsa, oss)" << std::endl;
331        std::cout << "   -s, --song FILE - Load a song (*.h2song) at startup" << std::endl;
332        std::cout << "   -k, --kit drumkit_name - Load a drumkit at startup" << std::endl;
333        std::cout << "   -i, --install FILE - install a drumkit (*.h2drumkit)" << std::endl;
334#ifdef H2CORE_HAVE_LASH
335        std::cout << "   --lash-no-start-server - If LASH server not running, don't start" << endl
336                  << "                            it (LASH 0.5.3 and later)." << std::endl;
337        std::cout << "   --lash-no-autoresume - Tell LASH server not to assume I'm returning" << std::endl
338                  << "                          from a crash." << std::endl;
339#endif
340        std::cout << "   -n, --nosplash - Hide splash screen" << std::endl;
341        std::cout << "   -V[Level], --verbose[=Level] - Print a lot of debugging info" << std::endl;
342        std::cout << "                 Level, if present, may be None, Error, Warning, Info, Debug or 0xHHHH" << std::endl;
343        std::cout << "   -v, --version - Show version info" << std::endl;
344        std::cout << "   -h, --help - Show this help message" << std::endl;
345}
Note: See TracBrowser for help on using the browser.