~ubuntu-branches/ubuntu/dapper/terminatorx/dapper

« back to all changes in this revision

Viewing changes to src/main.cc

  • Committer: Bazaar Package Importer
  • Author(s): Mike Furr
  • Date: 2004-04-26 21:20:09 UTC
  • Revision ID: james.westby@ubuntu.com-20040426212009-acjw8flkt05j945f
Tags: upstream-3.81
Import upstream version 3.81

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    terminatorX - realtime audio scratching software
 
3
    Copyright (C) 1999-2003  Alexander K�nig
 
4
 
 
5
    This program is free software; you can redistribute it and/or modify
 
6
    it under the terms of the GNU General Public License as published by
 
7
    the Free Software Foundation; either version 2 of the License, or
 
8
    (at your option) any later version.
 
9
 
 
10
    This program is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with this program; if not, write to the Free Software
 
17
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
    
 
19
    File: main.c
 
20
    
 
21
    Description: This contains the main() function. All the initializing
 
22
                 happens here.
 
23
    
 
24
    Changes:
 
25
    
 
26
    19 Mar 1999: Applied a patch by Andrew C. Bul+hac?k (eMail: acb@zikzak.net)
 
27
                 that fixes wavfile reading routine for the overreading bug.
 
28
                 
 
29
    20 Mar 1999: Big endian support.
 
30
    
 
31
    23 Mar 1999: display of new keys (<-, ->)
 
32
    
 
33
    4 October 1999: Rewrite ;) - back to C++
 
34
*/
 
35
 
 
36
#define TX_GTKRC "/usr/share/themes/terminatorX/gtk/gtkrc"
 
37
 
 
38
#define BENCH_CYCLES 100000
 
39
 
 
40
#include <stdio.h>
 
41
#include "tX_mastergui.h"
 
42
#include <malloc.h>
 
43
#include <math.h>
 
44
#include <stdio.h>
 
45
#ifndef WIN32
 
46
#include <unistd.h>
 
47
#endif
 
48
 
 
49
#ifdef HAVE_CONFIG_H
 
50
#include <config.h>
 
51
#endif
 
52
 
 
53
#include "tX_endian.h"
 
54
#include "tX_types.h"
 
55
#include "tX_global.h"
 
56
#include "tX_audiodevice.h"
 
57
#include "version.h"
 
58
#include "tX_dialog.h"
 
59
#include <gtk/gtk.h>
 
60
#include <glib.h>
 
61
#include <string.h>
 
62
 
 
63
#include "tX_ladspa.h"
 
64
#include "tX_ladspa_class.h"
 
65
#include "tX_engine.h"
 
66
#include "tX_capabilities.h"
 
67
 
 
68
#ifdef CREATE_BENCHMARK 
 
69
#include "tX_vtt.h"
 
70
#endif
 
71
 
 
72
#ifdef USE_SCHEDULER
 
73
#include <sched.h>
 
74
#include <sys/types.h>
 
75
#include <unistd.h>
 
76
#endif
 
77
 
 
78
#ifdef USE_JACK 
 
79
void jack_check()
 
80
{
 
81
        if ((!tX_jack_client::get_instance()) && (globals.audiodevice_type==JACK)) {
 
82
                tx_note("Couldn't connect to JACK server - JACK output not available.\n\nIf you want to use JACK, ensure the JACK daemon is running before you start terminatorX.", true);
 
83
        }
 
84
}
 
85
#endif // USE_JACK
 
86
 
 
87
static bool timesup=false;
 
88
 
 
89
gboolean timeout(void *)
 
90
{
 
91
        timesup=true;
 
92
        return FALSE;
 
93
}
 
94
 
 
95
void show_help()
 
96
{
 
97
                        
 
98
        fprintf(stderr, "\
 
99
usage: terminatorX [options]n\
 
100
\n\
 
101
  -h, --help                    Display help info\n\
 
102
  -f, --file                    Load saved terminatorX set file\n\
 
103
  -r, --rc-file [file]          Load alternate rc file\n\
 
104
  -d, --dont-save               Do not save settings at exit\n\
 
105
  -s, --std-out                 Use stdout for sound output\n\
 
106
  --device=[output device]      Use alternate device for sound output\n\
 
107
\n");
 
108
}
 
109
 
 
110
int parse_args(int *argc, char **argv)
 
111
{
 
112
        // pass over argv once to see if we need to load an alternate_rc file
 
113
        for (int i = 1 ; i != *argc ; ++i ) {
 
114
                if ((strcmp(argv[i], "-r") == 0) || (strcmp(argv[i], "--rc-file") == 0)) {
 
115
                        if (argv[i+1] ) {       
 
116
                                ++i;
 
117
                                fprintf(stderr, "tX: Loading alternate rc file %s\n", argv[i]);
 
118
                                globals.alternate_rc = argv[i];
 
119
                        } else {
 
120
                                show_help();    
 
121
                                exit(1);
 
122
                        }
 
123
                        break;
 
124
                }
 
125
        }
 
126
        
 
127
        // load up the global values
 
128
        load_globals();
 
129
 
 
130
        // default the flag options, or they'll be set from last execution... (think globals.no_gui ;)
 
131
        globals.no_gui = 0;
 
132
        globals.alternate_rc = 0;
 
133
        globals.store_globals = 1;
 
134
        globals.startup_set = 0;
 
135
                
 
136
        // then pass over again, this time setting passed values
 
137
        for (int i = 1 ; i < *argc ; ++i ) {
 
138
                if ((strcmp(argv[i], "-f") == 0) || (strcmp(argv[i], "--file") == 0)) {
 
139
                        ++i;
 
140
                        globals.startup_set = argv[i];
 
141
                } else if (((strcmp(argv[i], "-r") == 0) || (strcmp(argv[i], "--rc-file") == 0)) && (argv[i+1])) {
 
142
                        ++i;
 
143
                        globals.alternate_rc = argv[i];
 
144
                } else if ((strcmp(argv[i], "-d") == 0) || (strcmp(argv[i], "--dont-save") == 0)) {
 
145
                        fprintf(stderr, "tX: Do not save settings on exit\n");
 
146
                        globals.store_globals = 0;
 
147
 
 
148
                } else if ((strcmp(argv[i], "-s") == 0) || (strcmp(argv[i], "--std-out") == 0)) {
 
149
                        globals.use_stdout_cmdline = 1;
 
150
                        globals.use_stdout = 1;
 
151
                } else if ((strncmp(argv[i], "--device",8) == 0)) {
 
152
                        if (strlen(argv[i]+9)<=PATH_MAX)
 
153
                                strcpy(globals.oss_device,argv[i]+9);
 
154
                        else {
 
155
                                show_help();
 
156
                                exit(1);
 
157
                        }
 
158
                } else {
 
159
                        show_help();
 
160
                        exit(1);
 
161
                }
 
162
        }
 
163
        return 1;
 
164
}
 
165
 
 
166
void checkenv(const char *name)
 
167
{
 
168
        char *value;
 
169
        int length;
 
170
        
 
171
        value=getenv(name);
 
172
        if (value) {
 
173
                length=strlen(value);
 
174
                /*
 
175
                 strnlen requires extra macros...
 
176
                 length=strnlen(value, PATH_MAX+1);
 
177
                */
 
178
                
 
179
                if (length>=PATH_MAX) {
 
180
                        tX_error("Your \"%s\" environment variable seems malicious (%i chars).", name, length);
 
181
                        tX_error("Please correct that and restart terminatorX.");
 
182
                        exit(-1);
 
183
                }
 
184
        }
 
185
}
 
186
 
 
187
int main(int argc, char **argv)
 
188
{
 
189
        fprintf(stderr, "%s - Copyright (C) 1999-2003 by Alexander K�nig\n", VERSIONSTRING);
 
190
        fprintf(stderr, "terminatorX comes with ABSOLUTELY NO WARRANTY - for details read the license.\n");
 
191
 
 
192
#ifdef USE_CAPABILITIES 
 
193
        if (!geteuid()) {
 
194
                if (prctl(PR_SET_KEEPCAPS, 1, -1, -1, -1)) {
 
195
                        tX_error("failed to keep capabilites.");
 
196
                }
 
197
                set_nice_capability(CAP_PERMITTED);
 
198
        }
 
199
        
 
200
        if ((!geteuid()) && (getuid() != geteuid())) {
 
201
                tX_debug("main() - capabilites set, dropping root privileges.");
 
202
                
 
203
                int result=setuid(getuid());
 
204
                
 
205
                if (result) {
 
206
                        tX_error("main() Panic: can't drop root privileges.");
 
207
                        exit(2);
 
208
                }
 
209
        }
 
210
        
 
211
        set_nice_capability(CAP_EFFECTIVE);     
 
212
#endif
 
213
        
 
214
        checkenv("HOME");
 
215
        checkenv("XLOCALEDIR"); 
 
216
 
 
217
#ifndef USE_CAPABILITIES
 
218
        /* If we're not using capabilities we're still 
 
219
           running suid-root here. So we get rid of root
 
220
           before doing anything esle.
 
221
        */
 
222
        tX_engine *engine=tX_engine::get_instance();
 
223
#endif  
 
224
        
 
225
        gtk_init (&argc, &argv);
 
226
        gtk_set_locale();
 
227
        
 
228
        parse_args(&argc, argv); // loads settings
 
229
 
 
230
        if (globals.show_nag) { 
 
231
                show_about(1);
 
232
                g_timeout_add(2000, (GSourceFunc) timeout, NULL);
 
233
        }
 
234
 
 
235
#ifdef USE_CAPABILITIES
 
236
        /* If we have capabilities it's save to
 
237
           first read the config and then create 
 
238
           the engine.
 
239
        */
 
240
        tX_engine *engine=tX_engine::get_instance();
 
241
#endif  
 
242
 
 
243
        LADSPA_Class::init();
 
244
        LADSPA_Plugin::init();
 
245
#ifdef USE_JACK 
 
246
        tX_jack_client::init();
 
247
#endif  
 
248
 
 
249
#ifdef USE_SCHEDULER
 
250
        tX_debug("main() GUI thread is p:%i, t:%i and has policy %i.", getpid(), (int) pthread_self(), sched_getscheduler(getpid()));
 
251
#endif  
 
252
        
 
253
        create_mastergui(globals.width, globals.height);
 
254
        
 
255
        if (globals.show_nag) {
 
256
                while (!timesup) {
 
257
                        while (gtk_events_pending()) gtk_main_iteration(); 
 
258
                        gdk_flush();                            
 
259
                        usleep(250);
 
260
                }
 
261
                destroy_about();
 
262
        }
 
263
        
 
264
#ifdef USE_JACK
 
265
        jack_check();
 
266
#endif
 
267
        display_mastergui();
 
268
                
 
269
        if (globals.startup_set) {
 
270
                while (gtk_events_pending()) gtk_main_iteration(); gdk_flush(); 
 
271
                tX_cursor::set_cursor(tX_cursor::WAIT_CURSOR);
 
272
                load_tt_part(globals.startup_set);
 
273
                tX_cursor::reset_cursor();
 
274
        } else {
 
275
#ifdef USE_ALSA_MIDI_IN
 
276
                if (globals.auto_assign_midi) tX_midiin::auto_assign_midi_mappings(NULL, NULL);
 
277
#endif          
 
278
        }
 
279
 
 
280
#ifndef CREATE_BENCHMARK
 
281
        gtk_main();
 
282
 
 
283
        store_globals();
 
284
 
 
285
        delete engine;
 
286
#ifdef USE_JACK 
 
287
        if (tX_jack_client::get_instance()) {
 
288
                delete tX_jack_client::get_instance();
 
289
        }
 
290
#endif // USE_JACK
 
291
        
 
292
        fprintf(stderr, "Have a nice life.\n");
 
293
#else // CREATE_BENCHMARK
 
294
        gtk_widget_hide(main_window);
 
295
        while (gtk_events_pending()) gtk_main_iteration(); gdk_flush(); 
 
296
        gdk_flush();
 
297
        
 
298
        vtt_class::set_sample_rate(48000);
 
299
        
 
300
        printf("\n* BENCHMARKING *\n");
 
301
        
 
302
        GTimer *bench_time = g_timer_new();
 
303
        gulong micros;
 
304
        double ratio;
 
305
        double res;
 
306
        list <vtt_class *> :: iterator vtt;
 
307
        
 
308
        for (vtt=vtt_class::main_list.begin(); vtt!=vtt_class::main_list.end(); vtt++) {
 
309
                if ((*vtt)->autotrigger) (*vtt)->trigger();
 
310
        }
 
311
        
 
312
        sleep(3);       
 
313
        g_timer_start(bench_time);
 
314
        
 
315
        for (int i=0; i<BENCH_CYCLES; i++) {
 
316
                vtt_class::render_all_turntables();
 
317
        }
 
318
        g_timer_stop(bench_time);
 
319
        res=g_timer_elapsed(bench_time, &micros);
 
320
        
 
321
        ratio=((double) BENCH_CYCLES)/res;
 
322
        printf ("Rendered %i blocks in %f secons,\n=> %f blocks per second.\n\n", (long) BENCH_CYCLES, res, ratio);
 
323
#endif // CREATE_BENCHMARK
 
324
        return (0);
 
325
}