Fixes and startup-notification - Alex
[terminatorX.git] / src / tX_mastergui.cc
1 /*
2     terminatorX - realtime audio scratching software
3     Copyright (C) 1999-2004  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: tX_mastergui.cc
20  
21     Description: This implements the main (aka master) gtk+ GUI of terminatorX
22                  It serves as a container for the vtt-guis.
23 */    
24
25 #include <gtk/gtk.h>
26 #include <gdk/gdkx.h>
27 #include <X11/Xlib.h>
28 #include <math.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <gdk/gdkkeysyms.h>
32 #include "version.h"
33 #include "tX_global.h"
34 #include "tX_engine.h"
35 #include "tX_vttgui.h"
36 #include "tX_vtt.h"
37 #include "tX_flash.h"
38 #include "tX_dialog.h"
39 #include "tX_loaddlg.h"
40 #include "tX_seqpar.h"
41 #include "tX_pbutton.h"
42 #include "tX_sequencer.h"
43 #include "tX_mastergui.h"
44 #include "tX_knobloader.h"
45 #include "tX_glade_interface.h"
46 #include "tX_glade_support.h"
47 #include <sys/time.h>
48 #include <sys/wait.h>
49 #include "tX_midiin.h"
50
51 #ifdef USE_SCHEDULER
52 #include <sys/resource.h>
53 #endif
54
55 #define TX_SET_ID_10 "terminatorX turntable set file - version 1.0 - data:"
56 #define TX_SET_ID_11 "terminatorX turntable set file - version 1.1 - data:"
57 #define TX_SET_ID_12 "terminatorX turntable set file - version 1.2 - data:"
58 #define TX_SET_ID_13 "terminatorX turntable set file - version 1.3 - data:"
59 #define TX_SET_ID_14 "terminatorX turntable set file - version 1.4 - data:"
60
61 int audioon=0;
62 int sequencer_ready=1;
63
64 bool tX_shutdown=false;
65
66 GtkWidget *tt_parent;
67 GtkWidget *control_parent;
68 GtkWidget *audio_parent;
69 GtkWidget *main_window;
70 GtkWidget *grab_button;
71 GtkWidget *main_flash;
72
73 GtkWidget *seq_rec_btn;
74 GtkWidget *seq_play_btn;
75 GtkWidget *seq_stop_btn;
76 GtkAdjustment *seq_adj;
77 GtkWidget *seq_slider;
78 GtkWidget *seq_entry;
79 GtkWidget *panel_bar;
80
81 int buttons_on_panel_bar=0;
82
83 int seq_adj_care=1;
84 int seq_stop_override=0;
85
86 GtkAdjustment *volume_adj;
87 GtkAdjustment *pitch_adj;
88
89 /* seq-pars */
90 tX_seqpar_master_volume sp_master_volume;
91 tX_seqpar_master_pitch sp_master_pitch;
92
93 GtkWidget *engine_btn;
94
95 GtkWidget *main_menubar;
96 GtkWidget *rec_menu_item;
97 GtkWidget *fullscreen_item;
98
99 int rec_dont_care=0;
100 gint update_tag;
101
102 #define connect_entry(wid, func, ptr); g_signal_connect(G_OBJECT(wid), "activate", (GtkSignalFunc) func, (void *) ptr);
103 #define connect_adj(wid, func, ptr); g_signal_connect(G_OBJECT(wid), "value_changed", (GtkSignalFunc) func, (void *) ptr);
104 #define connect_button(wid, func, ptr); g_signal_connect(G_OBJECT(wid), "clicked", (GtkSignalFunc) func, (void *) ptr);
105
106 Window xwindow;
107 #define WID_DYN TRUE, TRUE, 0
108 #define WID_FIX FALSE, FALSE, 0
109 extern void add_vtt(GtkWidget *ctrl, GtkWidget *audio, char *fn);
110 extern void destroy_gui(vtt_class *vtt);
111 extern void gui_show_frame(vtt_class *vtt, int show);
112
113 #ifndef USE_FILECHOOSER
114 GdkWindow *save_dialog_win=NULL;
115 GdkWindow *load_dialog_win=NULL;
116
117 GtkWidget *save_dialog=NULL;
118 GtkWidget *load_dialog=NULL;
119 #endif
120
121 GdkWindow *rec_dialog_win=NULL;
122 GtkWidget *rec_dialog=NULL;
123
124 //GtkWidget *no_of_vtts=NULL;
125 GtkWidget *used_mem=NULL;
126
127 int stop_update=0;
128 int update_delay;
129
130 vtt_class *old_focus=NULL;
131
132 int grab_status=0;
133 int last_grab_status=0;
134
135 GtkWidget *delete_all_item=NULL;
136 GtkWidget *delete_all_vtt_item=NULL;
137
138 GtkTooltips *gui_tooltips=NULL;
139
140 void gui_set_tooltip(GtkWidget *wid, char *tip)
141 {
142         gtk_tooltips_set_tip(gui_tooltips, wid, tip, NULL);
143 }
144
145 void turn_audio_off(void)
146 {
147         if (audioon) {
148                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 0);
149                 while (gtk_events_pending()) gtk_main_iteration();              
150         }
151 }
152
153 gint pos_update(gpointer data)
154 {
155         f_prec temp,temp2;
156
157         if (stop_update) {              
158                 cleanup_all_vtts();
159                 tX_seqpar :: update_all_graphics();
160                 if (old_focus) gui_show_frame(old_focus, 0);
161                 old_focus=NULL;
162                 gtk_tx_flash_clear(main_flash);
163                 gdk_flush();    
164                 update_tag=0;
165                 return FALSE;
166         } else {
167                 update_all_vtts();
168                 
169                 /*main vu meter */
170                 temp=vtt_class::mix_max_l;
171                 vtt_class::mix_max_l=0;
172                 temp2=vtt_class::mix_max_r;
173                 vtt_class::mix_max_r=0;
174                 gtk_tx_flash_set_level(main_flash, temp/FL_SHRT_MAX, temp2/FL_SHRT_MAX);
175
176                 if (vtt_class::focused_vtt!=old_focus) {
177                         if (old_focus) gui_show_frame(old_focus, 0);
178                         old_focus=vtt_class::focused_vtt;
179                         if (old_focus) gui_show_frame(old_focus, 1);                    
180                 }
181                 if (grab_status!=last_grab_status) {
182                         last_grab_status=grab_status;
183                         if (!grab_status) {
184                                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(grab_button), 0);
185                         }
186                 }
187                 gdk_flush();    
188                 update_delay--;
189                 
190                 if (update_delay < 0) {
191                         seq_update();
192                         tX_seqpar::update_all_graphics();
193                         update_delay=globals.update_delay;
194                 }
195                 
196                 if (tX_engine::get_instance()->check_error()) {
197                         tX_error("ouch - error while playback...");
198                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 0);                 
199                         return FALSE;
200                 }               
201                 
202                 // let the audio engine we got the chance to do something
203                 tX_engine::get_instance()->reset_cycles_ctr();
204                 
205                 return TRUE;
206         }
207 }
208
209 void mg_update_status()
210 {
211         FILE *procfs;
212         pid_t mypid;
213         char filename[PATH_MAX];
214         char buffer[256];
215         int found=0;    
216         int mem;
217         
218         mypid=getpid();
219         sprintf(filename, "/proc/%i/status", mypid);
220         procfs=fopen(filename, "r");
221         if (procfs) {
222                 while((!feof(procfs)) && !found) {
223                         fgets(buffer, 256, procfs);
224                         
225                         if (strncmp("VmSize:", buffer, 7)==0) {
226                                 found=1;
227                                 sscanf(buffer, "VmSize: %i kB", &mem);
228                                 sprintf(buffer, "%i", mem);
229                                 gtk_label_set_text(GTK_LABEL(used_mem), buffer);
230                         }
231                 }
232                 fclose(procfs); 
233         } else {
234                 gtk_label_set_text(GTK_LABEL(used_mem), "");
235         }
236         
237         /*sprintf(buffer, "%i", vtt_class::vtt_amount);
238         gtk_label_set_text(GTK_LABEL(no_of_vtts), buffer);*/
239 }
240
241 GtkSignalFunc new_table(GtkWidget *, char *fn)
242 {
243         turn_audio_off();
244                 
245         if (fn) {
246                 ld_create_loaddlg(TX_LOADDLG_MODE_SINGLE, 1);
247                 ld_set_filename(fn);
248         }
249         
250         add_vtt(control_parent, audio_parent, fn);                              
251         
252         if (fn) ld_destroy();           
253         mg_update_status();
254         
255 #ifdef USE_ALSA_MIDI_IN
256         if (globals.auto_assign_midi) tX_midiin::auto_assign_midi_mappings(NULL, NULL);
257 #endif
258         
259         return NULL;
260 }
261
262 bool tx_mg_have_setname=false;
263 char tx_mg_current_setname[PATH_MAX]="";
264
265 GtkSignalFunc new_tables() {
266         GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window), 
267                 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
268                 "Are you sure you want to loose all turntables and events?");
269         
270         int res=gtk_dialog_run(GTK_DIALOG(dialog));
271         gtk_widget_destroy(dialog);
272                 
273         if (res!=GTK_RESPONSE_YES) {
274                 return NULL;
275         }
276
277         vtt_class::delete_all();
278         new_table(NULL, NULL);
279
280 #ifdef USE_ALSA_MIDI_IN
281         if (globals.auto_assign_midi) tX_midiin::auto_assign_midi_mappings(NULL, NULL);
282 #endif
283         
284         gtk_window_set_title(GTK_WINDOW(main_window), "terminatorX");
285
286         return NULL;
287 }
288
289 /* Loading saved setups */
290 #ifndef USE_FILECHOOSER
291 GtkSignalFunc cancel_load_tables(GtkWidget *wid)
292 {
293         gtk_widget_destroy(load_dialog);
294         load_dialog=NULL;
295         load_dialog_win=NULL;
296         return(0);
297 }
298 #endif
299
300 void load_tt_part(char * buffer)
301 {
302         char wbuf[PATH_MAX];
303         xmlDocPtr doc;
304 #ifdef ENABLE_TX_LEGACY
305         FILE *in;
306 #endif  
307         turn_audio_off();
308         
309         strncpy(globals.tables_filename, buffer, sizeof(globals.tables_filename));
310         
311         doc = xmlParseFile(buffer);
312         if (doc) {
313                 vtt_class::load_all(doc, buffer);
314                 xmlFreeDoc(doc);
315         } 
316         
317 #ifdef ENABLE_TX_LEGACY
318         else {  
319                 in=fopen(buffer, "r");  
320         
321                 if (in) {
322                         char idbuff[256];
323                         
324                         fread(idbuff, strlen(TX_SET_ID_10), 1, in);
325                         if (strncmp(idbuff, TX_SET_ID_10, strlen(TX_SET_ID_10))==0) {
326                                 if (vtt_class::load_all_10(in, buffer)) tx_note("Error while reading set.", true);
327                         } else if (strncmp(idbuff, TX_SET_ID_11, strlen(TX_SET_ID_11))==0)      {
328                                 if (vtt_class::load_all_11(in, buffer)) tx_note("Error while reading set.", true);                      
329                         } else if (strncmp(idbuff, TX_SET_ID_12, strlen(TX_SET_ID_12))==0) {
330                                 if (vtt_class::load_all_12(in, buffer)) tx_note("Error while reading set.", true);                      
331                         } else if (strncmp(idbuff, TX_SET_ID_13, strlen(TX_SET_ID_13))==0) {
332                                 if (vtt_class::load_all_13(in, buffer)) tx_note("Error while reading set.", true);                      
333                         } else if (strncmp(idbuff, TX_SET_ID_14, strlen(TX_SET_ID_14))==0) {
334                                 if (vtt_class::load_all_14(in, buffer)) tx_note("Error while reading set.", true);                      
335                         }       else {
336                                 tx_note("This file is not a terminatorX set-file.", true);
337                                 fclose(in);
338                                 return;
339                         }
340                         fclose(in);     
341                 } else {
342                         char message[PATH_MAX+256];
343                         sprintf(message, "Failed to acesss file: \"%s\"", globals.tables_filename);
344                         tx_note(message, true);
345                         
346                         return;
347                 }
348         }
349 #else
350         else {
351                 char message[PATH_MAX+256];
352                 sprintf(message, "Failed to acesss file: \"%s\"", globals.tables_filename);
353                 tx_note(message, true);
354                 
355                 return;
356         }
357 #endif  
358         
359         tx_mg_have_setname=true;
360         strcpy(tx_mg_current_setname, buffer);
361         
362         tX_seqpar :: init_all_graphics();
363         vg_init_all_non_seqpars();
364                 
365         gtk_adjustment_set_value(volume_adj, globals.volume);
366         gtk_adjustment_set_value(pitch_adj, globals.pitch);
367         sprintf(wbuf,"terminatorX - %s", strip_path(buffer));
368 #ifdef USE_ALSA_MIDI_IN
369         if (globals.auto_assign_midi) tX_midiin::auto_assign_midi_mappings(NULL, NULL);
370 #endif  
371         gtk_window_set_title(GTK_WINDOW(main_window), wbuf);            
372 }
373
374 #ifndef USE_FILECHOOSER
375 void do_load_tables(GtkWidget *wid)
376 {
377         char buffer[PATH_MAX];
378         
379         strcpy(buffer, gtk_file_selection_get_filename(GTK_FILE_SELECTION(load_dialog)));
380         
381         gtk_widget_destroy(load_dialog);
382         
383         load_dialog=NULL;
384         load_dialog_win=NULL;
385
386         tX_cursor::set_cursor(tX_cursor::WAIT_CURSOR);
387         load_tt_part(buffer);
388         tX_cursor::reset_cursor();
389 }
390 #endif
391
392 GtkSignalFunc load_tables()
393 {
394 #ifdef USE_FILECHOOSER  
395         GtkWidget * dialog = gtk_file_chooser_dialog_new ("Open Set File",
396                 GTK_WINDOW(main_window), GTK_FILE_CHOOSER_ACTION_OPEN,
397                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,  
398             GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
399                                       
400
401         GtkFileFilter *filter=gtk_file_filter_new();
402         gtk_file_filter_add_pattern (filter, "*.tX");
403         gtk_file_filter_add_pattern (filter, "*.tx");
404         gtk_file_filter_set_name(filter, "terminatorX Set Files");
405         gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter); 
406
407         filter=gtk_file_filter_new();
408         gtk_file_filter_add_pattern (filter, "*");
409         gtk_file_filter_set_name(filter, "All Files");
410         gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter); 
411         
412         if (strlen(globals.tables_filename)) {
413                 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), globals.tables_filename);
414         }       
415         
416         if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
417         char * filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
418                 gtk_widget_hide(dialog);
419                 tX_cursor::set_cursor(tX_cursor::WAIT_CURSOR);
420                 load_tt_part(filename);
421                 tX_cursor::reset_cursor();              
422         }       
423         
424         gtk_widget_destroy(dialog);
425 #else
426         if (load_dialog_win) {
427                 gdk_window_raise(load_dialog_win);
428                 return 0;
429         }
430
431         load_dialog=gtk_file_selection_new("Load Set"); 
432         
433         gtk_file_selection_show_fileop_buttons(GTK_FILE_SELECTION(load_dialog));
434         gtk_file_selection_complete(GTK_FILE_SELECTION(load_dialog), "*.tX");
435         
436         if (strlen(globals.tables_filename)) {
437                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(load_dialog), globals.tables_filename);
438         }
439         
440         gtk_widget_show(load_dialog);
441         
442         load_dialog_win=load_dialog->window;
443         
444         g_signal_connect (G_OBJECT(GTK_FILE_SELECTION(load_dialog)->ok_button), "clicked", G_CALLBACK(do_load_tables), NULL);
445         g_signal_connect (G_OBJECT(GTK_FILE_SELECTION(load_dialog)->cancel_button), "clicked", G_CALLBACK (cancel_load_tables), NULL);  
446         g_signal_connect (G_OBJECT(load_dialog), "delete-event", G_CALLBACK(cancel_load_tables), NULL); 
447         
448 #endif
449
450         return NULL;
451 }
452
453
454
455 GtkSignalFunc drop_set(GtkWidget *widget, GdkDragContext *context,
456                 gint x, gint y, GtkSelectionData *selection_data,
457                 guint info, guint time, vtt_class *vtt)
458 {
459         char filename[PATH_MAX];
460         char *fn;
461         
462         strncpy(filename, (char *) selection_data->data, (size_t) selection_data->length);
463         filename[selection_data->length]=0;
464
465         fn = strchr (filename, '\r');
466         *fn=0;  
467         
468         fn = strchr (filename, ':');
469         if (fn) fn++; else fn=(char *) selection_data->data;
470         
471         load_tt_part(fn);
472
473         strcpy (filename, "dont segfault workaround ;)");
474         return NULL;
475 }
476
477 /* save tables */
478
479 #ifndef USE_FILECHOOSER
480 GtkSignalFunc cancel_save_tables(GtkWidget *wid)
481 {
482         gtk_widget_destroy(save_dialog);
483         save_dialog=NULL;
484         save_dialog_win=NULL;
485         return(0);
486 }
487
488 gboolean do_save_from_selection(GtkWidget *wid) {
489         char buffer[PATH_MAX];
490         
491         if (wid) {
492                 strcpy(buffer, gtk_file_selection_get_filename(GTK_FILE_SELECTION(save_dialog)));
493                 int len=strlen(buffer);
494                 if (!len || (buffer[len-1]=='/')) {                     
495                         tx_note("Invalid filename for set file.", true);                        
496                         return FALSE;
497                 }
498                 gtk_widget_destroy(save_dialog);
499                 save_dialog=NULL;
500                 save_dialog_win=NULL;
501         } else {
502                 strcpy(buffer, tx_mg_current_setname);
503         }       
504         
505         do_save_tables(buffer);
506         
507         return TRUE;
508 }
509 #endif
510
511 void do_save_tables(char *buffer)
512 {
513         FILE *out;
514         gzFile zout;
515         char wbuf[PATH_MAX];
516         char *ext;
517         
518         ext=strrchr(buffer, '.');
519         
520         if (ext) {
521                 if (strcmp(ext, ".tX")) strcat(buffer, ".tX");
522         } else {
523                 strcat(buffer, ".tX");
524         }
525
526         tx_mg_have_setname=true;
527         strcpy(tx_mg_current_setname, buffer);
528         strcpy(globals.tables_filename, buffer);
529         
530         if (globals.compress_set_files) {
531                 _store_compress_xml=1;
532                 out=NULL;
533                 zout=gzopen(buffer, "w");
534         } else {
535                 _store_compress_xml=0;
536                 out=fopen(buffer, "w");
537                 zout=NULL;
538         }
539         
540         if (out || zout) {
541                 if (vtt_class::save_all(out, zout)) tx_note("Error while saving set.", true);
542                 if (out) fclose(out); 
543                 else if (zout) gzclose(zout);
544                 sprintf(wbuf,"terminatorX - %s", strip_path(buffer));
545                 gtk_window_set_title(GTK_WINDOW(main_window), wbuf);                            
546         } else {
547                 tx_note("Failed to open file for write access.", true);
548         }
549 }
550
551 GtkSignalFunc save_tables_as()
552 {
553 #ifdef USE_FILECHOOSER
554         GtkWidget * dialog = gtk_file_chooser_dialog_new ("Save Set",
555                 GTK_WINDOW(main_window), GTK_FILE_CHOOSER_ACTION_SAVE, 
556                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, 
557                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,NULL);
558         
559         if (tx_mg_have_setname) {
560                 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER (dialog), tx_mg_current_setname);
561         }
562                                       
563         if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
564         char buffer[PATH_MAX];
565                 char *filename=gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
566                 strcpy(buffer, filename);
567                 g_free(filename);
568                 do_save_tables(buffer);
569         }       
570         
571         gtk_widget_destroy(dialog);     
572 #else
573         if (save_dialog_win) {
574                 gtk_widget_destroy(save_dialog);
575                 save_dialog=NULL;
576                 save_dialog_win=NULL;
577         }
578         
579         save_dialog=gtk_file_selection_new("Save Set"); 
580
581         if (tx_mg_have_setname) {
582                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(save_dialog), tx_mg_current_setname);
583         }
584         
585         gtk_widget_show(save_dialog);
586         
587         save_dialog_win=save_dialog->window;
588         
589         g_signal_connect (G_OBJECT(GTK_FILE_SELECTION(save_dialog)->ok_button), "clicked", G_CALLBACK(do_save_from_selection), NULL);
590         g_signal_connect (G_OBJECT(GTK_FILE_SELECTION(save_dialog)->cancel_button), "clicked", G_CALLBACK (cancel_save_tables), NULL);  
591         g_signal_connect (G_OBJECT(save_dialog), "delete-event", G_CALLBACK(cancel_save_tables), NULL); 
592 #endif
593         return NULL;
594 }
595
596 GtkSignalFunc save_tables()
597 {
598         if (!tx_mg_have_setname) {
599                 save_tables_as();
600         } else {
601                 do_save_tables(tx_mg_current_setname);
602         }
603         
604         return NULL;
605 }
606
607 GtkSignalFunc master_volume_changed (GtkWidget *wid, void *d)
608 {
609         sp_master_volume.receive_gui_value((float) GTK_ADJUSTMENT(wid)->value);
610         return NULL;    
611 }
612
613 GtkSignalFunc master_pitch_changed(GtkWidget *wid, void *d)
614 {
615         sp_master_pitch.receive_gui_value((float) GTK_ADJUSTMENT(wid)->value);  
616         return NULL;    
617 }
618
619 void mg_enable_critical_buttons(int enable)
620 {
621         gtk_widget_set_sensitive(seq_rec_btn, enable);
622         gtk_widget_set_sensitive(seq_play_btn, enable);
623         gtk_widget_set_sensitive(seq_slider, enable);
624
625         gtk_widget_set_sensitive(rec_menu_item, enable);
626         gtk_widget_set_sensitive(delete_all_item, enable);
627         gtk_widget_set_sensitive(delete_all_vtt_item, enable);
628         
629         vg_enable_critical_buttons(enable);
630 }
631
632 GtkSignalFunc seq_stop(GtkWidget *w, void *);
633
634 static bool stop_override=false;
635
636 GtkSignalFunc audio_on(GtkWidget *w, void *d)
637 {
638         tX_engine_error res;
639         
640         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) {               
641                 sequencer_ready=0;
642                 mg_enable_critical_buttons(0);
643                 res=tX_engine::get_instance()->run();
644
645                 if (res!=NO_ERROR) {
646                         mg_enable_critical_buttons(1);
647                         stop_override=true;
648                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 0);
649                         stop_override=false;                    
650
651                         switch(res) {
652                                 case ERROR_BUSY:
653                                 tx_note("Error starting engine: engine is already running.", true);
654                                 break;
655                                 case ERROR_AUDIO:
656                                 tx_note("Error starting engine: failed to access audiodevice.\nPlease check the audio device settings in the \"Preferences\" dialog.", true);
657                                 break;
658                                 case ERROR_TAPE:
659                                 tx_note("Error starting engine: failed to open the recording file.", true);
660                                 break;
661                                 default:tx_note("Error starting engine: Unknown error.", true);
662                         }
663                         
664                         return 0;
665                 }
666                 
667                 sequencer_ready=1;
668                 stop_update=0;
669                 audioon=1;
670                 update_delay=globals.update_delay;
671                 update_tag=gtk_timeout_add(globals.update_idle, (GtkFunction) pos_update, NULL);
672                 gtk_widget_set_sensitive(grab_button, 1);
673         } else {        
674                 if (stop_override) return NULL;
675                 if (!sequencer_ready) return NULL;
676                 gtk_widget_set_sensitive(grab_button, 0);
677                 tX_engine::get_instance()->stop();
678                 stop_update=1;
679                 audioon=0;
680                 if (tX_engine::get_instance()->get_recording_request()) {
681                         tX_engine::get_instance()->set_recording_request(false);
682                         rec_dont_care=1;
683                         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(rec_menu_item), 0);
684                         rec_dont_care=0;
685                 }
686                 
687                 seq_stop(NULL, NULL);
688                 mg_enable_critical_buttons(1);
689                 
690                 if (tX_engine::get_instance()->get_runtime_error()) {
691                         tx_note("Fatal: The audio device broke down while playing\nback audio. Note that that some audio devices can not\nrecover from such a breakdown.", true);
692                 }
693                 if (tX_engine::get_instance()->get_overload_error()) {
694                         tx_note("Fatal: The audio engine was stopped due to an overload\ncondition. Try reducing the amount of plugins or\nturntables.", true);
695                 }
696         }
697         
698         return NULL;
699 }
700
701 GtkSignalFunc cancel_rec(GtkWidget *wid)
702 {
703         gtk_widget_destroy(rec_dialog);
704         rec_dialog=NULL;
705         rec_dialog_win=NULL;
706         rec_dont_care=0;
707         return(0);
708 }
709
710 void do_rec(GtkWidget *wid)
711 {
712         char buffer[PATH_MAX];
713         
714         strcpy(buffer, gtk_file_selection_get_filename(GTK_FILE_SELECTION(rec_dialog)));
715
716         if (strlen(buffer)) {
717                 strcpy(globals.record_filename, buffer);                
718                 tX_engine::get_instance()->set_recording_request(true);
719                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(rec_menu_item), 1);
720         }
721         
722         rec_dont_care=0;
723         
724         gtk_widget_destroy(rec_dialog);
725         
726         rec_dialog=NULL;
727         rec_dialog_win=NULL;
728 }
729
730 GtkSignalFunc select_rec_file()
731 {
732         if (rec_dialog_win) {
733                 gdk_window_raise(rec_dialog_win);
734                 return 0;
735         }
736         
737         rec_dialog=gtk_file_selection_new("Record To Disk");    
738         
739         if (strlen(globals.record_filename)) {
740                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(rec_dialog), globals.record_filename);
741         }
742         
743         gtk_widget_show(rec_dialog);
744         
745         rec_dialog_win=rec_dialog->window;
746         
747         g_signal_connect (G_OBJECT(GTK_FILE_SELECTION(rec_dialog)->ok_button), "clicked", G_CALLBACK(do_rec), NULL);
748         g_signal_connect (G_OBJECT(GTK_FILE_SELECTION(rec_dialog)->cancel_button), "clicked", G_CALLBACK (cancel_rec), NULL);   
749         g_signal_connect (G_OBJECT(rec_dialog), "delete-event", G_CALLBACK(cancel_rec), NULL);  
750         
751         return NULL;
752 }
753
754 GtkSignalFunc tape_on(GtkWidget *w, void *d)
755 {
756         if (rec_dont_care) return 0;
757
758         if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(w))) {   
759                 rec_dont_care=1;
760                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(w), 0);
761                 select_rec_file();
762         } else {
763                         tX_engine::get_instance()->set_recording_request(false);
764         }
765         
766         return NULL;
767 }
768
769 void grab_on(GtkWidget *w, void *d)
770 {
771         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) {
772                 tX_engine::get_instance()->set_grab_request();
773         }
774         grab_status=1;
775 }
776
777 void grab_off()
778 {
779         grab_status=0;
780 }
781
782 gboolean quit()
783 {       
784         if (globals.quit_confirm) {
785                 GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window), 
786                 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
787                 "Exit terminatorX and loose all unsaved data?");
788                 
789                 int res=gtk_dialog_run(GTK_DIALOG(dialog));
790                 gtk_widget_destroy(dialog);
791                         
792                 if (res!=GTK_RESPONSE_YES) {
793                         return TRUE;
794                 }
795         }
796         
797         tX_shutdown=true;
798         
799         turn_audio_off();
800         vtt_class::delete_all();
801
802         if (update_tag)
803         gtk_timeout_remove(update_tag);
804         globals.width=main_window->allocation.width;
805         globals.height=main_window->allocation.height;
806
807         gtk_main_quit();
808         
809         return true;
810 }
811
812 void mplcfitx()
813 /* Most Probably Least Called Function In terminatorX :) */
814 {
815         show_about(0);
816 }
817
818 GtkSignalFunc seq_play(GtkWidget *w, void *)
819 {
820         if ((sequencer.is_empty()) &&   (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(seq_rec_btn)))) {
821                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
822                  {
823                         tx_note("Sequencer playback triggered - but no events recorded yet - nothing to playback!\n\nTo perform live with terminatorX just activate the audio engine with the \"Power\" button.");
824                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 0);
825                  }
826         } else {
827                 if (seq_stop_override) return NULL;
828                         
829                 seq_adj_care=0;
830                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 1);
831                 sequencer.trig_play();
832         
833                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 1);
834         }
835         
836         return NULL;
837 }
838
839 GtkSignalFunc seq_stop(GtkWidget *w, void *)
840 {
841         if (!sequencer_ready) return NULL;
842         sequencer.trig_stop();
843         seq_adj_care=1;
844         seq_stop_override=1;
845         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(seq_play_btn), 0);
846         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(seq_rec_btn), 0);
847         while (gtk_events_pending()) gtk_main_iteration();              
848         seq_stop_override=0;
849         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 0); 
850         gtk_widget_set_sensitive(seq_slider, 1);        
851         gtk_widget_set_sensitive(engine_btn, 1);
852         gtk_widget_set_sensitive(seq_rec_btn, 1);
853
854         return NULL;
855 }
856
857 GtkSignalFunc seq_rec(GtkWidget *w, void *)
858 {
859         seq_adj_care=0;
860         gtk_widget_set_sensitive(seq_slider, 0);
861
862         if (seq_stop_override) return NULL;
863         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 1);
864         gtk_widget_set_sensitive(engine_btn, 0);
865         gtk_widget_set_sensitive(seq_rec_btn, 0);
866         sequencer.trig_rec();
867         
868         return NULL;
869 }
870
871 void seq_update_entry(const guint32 timestamp)
872 {
873         char buffer[20];
874         guint32 samples;
875         guint32 minu,sec,hun;   
876         guint32 sr;
877         
878         samples=timestamp*vtt_class::get_mix_buffer_size();
879         sr=vtt_class::get_last_sample_rate();
880         
881         if (samples>0) {
882                 minu=samples/(sr*60);
883                 samples-=(sr*60)*minu;
884         
885                 sec=samples/sr;
886                 samples-=sr*sec;
887         
888                 hun=samples/(sr/100);
889         } else {
890                 minu=sec=hun=0;
891         }
892         
893         sprintf(buffer, "%02i:%02i.%02i", minu, sec, hun);
894         gtk_entry_set_text(GTK_ENTRY(seq_entry), buffer);
895 }
896
897 void seq_update()
898 {
899         seq_update_entry(sequencer.get_timestamp());
900         gtk_adjustment_set_value(seq_adj, sequencer.get_timestamp_as_float());
901         
902 }
903 gboolean seq_slider_released(GtkWidget *wid, void *d)
904 {
905         seq_adj_care=0;
906         gtk_widget_set_sensitive(seq_slider, 0);        
907         sequencer.forward_to_start_timestamp(0);
908         gtk_widget_set_sensitive(seq_slider, 1);        
909         seq_adj_care=1;
910         
911         return FALSE;
912 }
913 void sequencer_move(GtkWidget *wid, void *d)
914 {
915         guint32 pos;
916         
917         if (seq_adj_care) {
918                 pos=sequencer.set_start_timestamp((float) GTK_ADJUSTMENT(wid)->value);
919                 seq_update_entry(pos);  
920         }
921 }
922
923 #define add_sep();      dummy=gtk_hseparator_new ();\
924         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);\
925         gtk_widget_show(dummy);\
926
927 #define add_sep2();     dummy=gtk_hseparator_new ();\
928         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);\
929         gtk_widget_show(dummy);\
930
931 void fullscreen_toggle(GtkCheckMenuItem *item, gpointer data);
932 void display_help();
933 void display_browser();
934
935 tX_seqpar *del_sp=NULL;
936 vtt_class *del_vtt=NULL;
937 tx_menu_del_mode menu_del_mode=ALL_EVENTS_ALL_TURNTABLES;
938
939 GtkWidget *del_dialog=NULL;
940
941 GCallback menu_delete_all_events(GtkWidget *, void *param)
942 {       
943         del_dialog=create_tx_del_mode();
944         tX_set_icon(del_dialog);
945         
946         GtkWidget *label=lookup_widget(del_dialog, "delmode_label");
947         
948         menu_del_mode=ALL_EVENTS_ALL_TURNTABLES;
949         
950         gtk_label_set_markup(GTK_LABEL(label), "Delete <b>all</b> events for <b>all</b> turntables.");
951         gtk_widget_show(del_dialog);
952         
953         return NULL;
954 }
955
956 GCallback menu_delete_all_events_for_vtt(GtkWidget *, vtt_class *vtt)
957 {       
958         if (!vtt) {
959                 tX_error("No vtt passed to menu_delete_all_events_for_vtt().");
960                 return FALSE;
961         }
962         
963         char label_str[512];
964         
965         del_dialog=create_tx_del_mode();
966         tX_set_icon(del_dialog);
967
968         del_vtt=vtt;
969         GtkWidget *label=lookup_widget(del_dialog, "delmode_label");
970         
971         menu_del_mode=ALL_EVENTS_FOR_TURNTABLE;
972         
973         sprintf(label_str, "Delete <b>all</b> events for turntable <b>%s</b>.", vtt->name);
974         gtk_label_set_markup(GTK_LABEL(label), label_str);
975         gtk_widget_show(del_dialog);
976         
977         return NULL;
978 }
979
980 GCallback menu_delete_all_events_for_sp(GtkWidget *, tX_seqpar *sp)
981 {       
982         if (!sp) {
983                 tX_error("No sp passed to menu_delete_all_events_for_sp().");
984                 return FALSE;
985         }
986         
987         char label_str[512];
988         
989         del_dialog=create_tx_del_mode();
990         tX_set_icon(del_dialog);
991         
992         GtkWidget *label=lookup_widget(del_dialog, "delmode_label");
993         
994         menu_del_mode=ALL_EVENTS_FOR_SP;
995         del_sp=sp;
996         sprintf(label_str, "Delete all <b>%s</b> events for turntable '%s'.", sp->get_name(), sp->get_vtt_name());
997         gtk_label_set_markup(GTK_LABEL(label), label_str);
998         gtk_widget_show(del_dialog);
999
1000         return NULL;
1001 }
1002
1003 static GtkWidget *table_menu=NULL;
1004 static GtkWidget *table_menu_item=NULL;
1005
1006 GCallback create_table_sequencer_menu(GtkWidget *widget, void *param) 
1007 {
1008         char label[328];
1009         table_menu=gtk_menu_new();
1010         
1011         list <vtt_class *> :: iterator vtt;
1012
1013         for (vtt=vtt_class::main_list.begin(); vtt!=vtt_class::main_list.end(); vtt++) {
1014                 GtkWidget *menu_item=gtk_menu_item_new_with_label((*vtt)->name);
1015                 gtk_container_add (GTK_CONTAINER (table_menu), menu_item);
1016                 gtk_widget_show(menu_item);
1017                 
1018                 GtkWidget *seqpar_menu=gtk_menu_new();
1019                 list <tX_seqpar *> :: iterator sp;
1020                 
1021                 GtkWidget *all=gtk_menu_item_new_with_label("Delete All Events");
1022                 gtk_container_add (GTK_CONTAINER (seqpar_menu), all);
1023                 g_signal_connect(all, "activate", (GCallback) menu_delete_all_events_for_vtt, (*vtt));
1024                 gtk_widget_show(all);
1025
1026                 GtkWidget *sep = gtk_menu_item_new ();
1027                 gtk_widget_show(sep);
1028                 gtk_container_add(GTK_CONTAINER (seqpar_menu), sep);
1029                 gtk_widget_set_sensitive (sep, FALSE);
1030                 
1031                 for (sp=tX_seqpar::all.begin(); sp!=tX_seqpar::all.end(); sp++) {
1032                         if ((*sp)->vtt==(*vtt)) {
1033                                 sprintf(label, "Delete '%s' Events", (*sp)->get_name());
1034                                 GtkWidget *menu_item=gtk_menu_item_new_with_label(label);
1035                                 g_signal_connect(menu_item, "activate", (GCallback) menu_delete_all_events_for_sp, (*sp));
1036                                 gtk_container_add(GTK_CONTAINER(seqpar_menu), menu_item);
1037                                 gtk_widget_show(menu_item);
1038                         }
1039                 }
1040                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), seqpar_menu);
1041         }
1042         
1043         gtk_menu_item_set_submenu(GTK_MENU_ITEM(table_menu_item), table_menu);
1044         
1045         return NULL;
1046 }
1047
1048 GCallback toggle_confirm_events(GtkWidget *widget, void *dummy)
1049 {       
1050         globals.confirm_events=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));     
1051         return NULL;    
1052 }
1053
1054 GCallback toggle_auto_assign(GtkWidget *widget, void *dummy)
1055 {       
1056         globals.auto_assign_midi=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
1057 #ifdef USE_ALSA_MIDI_IN
1058         tX_midiin::auto_assign_midi_mappings(NULL, NULL);
1059 #endif
1060 /*      if (globals.auto_assign_midi) {
1061                 GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window), 
1062                 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, GTK_BUTTONS_YES_NO,
1063                 "Note: Enabling \"Auto Assign Default MIDI Settings\" will constantly overwrite the\
1064 MIDI mappings for the standard parameters. \
1065 Are you sure you really want this?");
1066                 
1067                 int res=gtk_dialog_run(GTK_DIALOG(dialog));
1068                 gtk_widget_destroy(dialog);
1069                         
1070                 if (res!=GTK_RESPONSE_YES) {
1071                         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), 0);
1072                 }                       
1073         } */
1074         return NULL;    
1075 }
1076
1077 void create_master_menu() 
1078 {
1079         GtkWidget *menu_item;
1080         GtkWidget *sub_menu;
1081         GtkAccelGroup* accel_group=gtk_accel_group_new();
1082         gtk_window_add_accel_group(GTK_WINDOW(main_window), accel_group);
1083
1084         /* FILE */
1085         menu_item = gtk_menu_item_new_with_mnemonic ("_File");
1086         gtk_widget_show (menu_item);
1087         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1088
1089         sub_menu = gtk_menu_new ();
1090         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1091
1092         menu_item = gtk_image_menu_item_new_from_stock ("gtk-new", accel_group);
1093         gtk_widget_show (menu_item);
1094         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1095         g_signal_connect(menu_item, "activate", (GCallback) new_tables, NULL);
1096
1097         menu_item = gtk_image_menu_item_new_from_stock ("gtk-open", accel_group);
1098         gtk_widget_show (menu_item);
1099         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1100         g_signal_connect(menu_item, "activate", (GCallback) load_tables, NULL);
1101
1102         menu_item = gtk_image_menu_item_new_from_stock ("gtk-save", accel_group);
1103         gtk_widget_show (menu_item);
1104         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1105         g_signal_connect(menu_item, "activate", (GCallback) save_tables, NULL);
1106
1107         menu_item = gtk_image_menu_item_new_from_stock ("gtk-save-as", accel_group);
1108         gtk_widget_show (menu_item);
1109         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1110         g_signal_connect(menu_item, "activate", (GCallback) save_tables_as, NULL);
1111
1112         menu_item = gtk_menu_item_new ();
1113         gtk_widget_show (menu_item);
1114         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1115         gtk_widget_set_sensitive (menu_item, FALSE);
1116
1117         menu_item = gtk_image_menu_item_new_from_stock ("gtk-quit", accel_group);
1118         gtk_widget_show (menu_item);
1119         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1120         g_signal_connect(menu_item, "activate", (GCallback) quit, NULL);
1121
1122         /* Turntables */
1123         menu_item = gtk_menu_item_new_with_mnemonic ("_Turntables");
1124         gtk_widget_show (menu_item);
1125         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1126         
1127         sub_menu = gtk_menu_new ();
1128         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1129
1130         menu_item = gtk_menu_item_new_with_mnemonic("_Add Turntable");
1131         gtk_widget_show (menu_item);
1132         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1133         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_A, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);    
1134         g_signal_connect(menu_item, "activate", (GCallback) new_table, NULL);
1135
1136         menu_item = gtk_menu_item_new ();
1137         gtk_widget_show (menu_item);
1138         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1139         gtk_widget_set_sensitive (menu_item, FALSE);
1140
1141         menu_item = gtk_menu_item_new_with_mnemonic("Assign _Default MIDI Mappings");
1142         gtk_widget_show (menu_item);
1143         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1144         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_M, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
1145
1146 #ifdef USE_ALSA_MIDI_IN
1147         g_signal_connect(menu_item, "activate", G_CALLBACK(tX_midiin::auto_assign_midi_mappings), (void *) true);
1148 #else
1149         gtk_widget_set_sensitive(menu_item, FALSE);
1150 #endif
1151
1152         menu_item = gtk_check_menu_item_new_with_mnemonic("A_uto Assign Default MIDI Mappings");
1153         gtk_widget_show (menu_item);
1154         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1155         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), globals.auto_assign_midi);
1156 #ifdef USE_ALSA_MIDI_IN 
1157         g_signal_connect(menu_item, "activate", (GCallback) toggle_auto_assign, NULL);
1158 #else
1159         gtk_widget_set_sensitive(menu_item, FALSE);
1160 #endif
1161
1162         menu_item = gtk_menu_item_new_with_mnemonic("_Clear MIDI Mappings");
1163         gtk_widget_show (menu_item);
1164         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1165         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_C, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
1166
1167 #ifdef USE_ALSA_MIDI_IN
1168         g_signal_connect(menu_item, "activate", G_CALLBACK(tX_midiin::clear_midi_mappings), (void *) true);
1169 #else
1170         gtk_widget_set_sensitive(menu_item, FALSE);
1171 #endif
1172
1173         menu_item = gtk_menu_item_new ();
1174         gtk_widget_show (menu_item);
1175         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1176         gtk_widget_set_sensitive (menu_item, FALSE);
1177
1178         menu_item = gtk_check_menu_item_new_with_mnemonic("_Record Audio To Disk");
1179         rec_menu_item = menu_item;
1180         gtk_widget_show (menu_item);
1181         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1182         g_signal_connect(menu_item, "activate", (GCallback) tape_on, NULL);
1183
1184         /* Sequencer */
1185         
1186         menu_item = gtk_menu_item_new_with_mnemonic("_Sequencer");
1187         gtk_widget_show (menu_item);
1188         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1189
1190         sub_menu = gtk_menu_new ();
1191         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1192                 
1193         table_menu = gtk_menu_new();
1194         menu_item = gtk_menu_item_new_with_mnemonic("Delete _Events");
1195         delete_all_vtt_item = menu_item;
1196         table_menu_item = menu_item;
1197         gtk_menu_item_set_submenu(GTK_MENU_ITEM(table_menu_item), table_menu);
1198         
1199         gtk_widget_show (menu_item);
1200         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1201         g_signal_connect_swapped (G_OBJECT (menu_item), "select", G_CALLBACK (create_table_sequencer_menu), NULL);
1202         
1203         menu_item = gtk_menu_item_new_with_mnemonic("Delete _All Events");
1204         delete_all_item = menu_item;
1205         gtk_widget_show (menu_item);
1206         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1207         g_signal_connect(menu_item, "activate", (GCallback) menu_delete_all_events, NULL);
1208
1209         menu_item = gtk_menu_item_new ();
1210         gtk_widget_show (menu_item);
1211         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1212         gtk_widget_set_sensitive (menu_item, FALSE);
1213
1214         menu_item = gtk_check_menu_item_new_with_mnemonic("_Confirm Recorded Events");
1215         //rec_menu_item = menu_item;
1216         gtk_widget_show (menu_item);
1217         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1218         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), globals.confirm_events);
1219         g_signal_connect(menu_item, "activate", (GCallback) toggle_confirm_events, NULL);
1220
1221         /* Options */
1222         menu_item = gtk_menu_item_new_with_mnemonic ("_Options");
1223         gtk_widget_show (menu_item);
1224         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1225
1226         sub_menu = gtk_menu_new ();
1227         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1228                 
1229         menu_item = gtk_check_menu_item_new_with_mnemonic("_Fullscreen");
1230         fullscreen_item = menu_item;
1231         gtk_widget_show (menu_item);
1232         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1233
1234         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), globals.fullscreen_enabled);
1235         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_F11, (GdkModifierType) 0, GTK_ACCEL_VISIBLE);
1236         g_signal_connect(menu_item, "activate", (GCallback) fullscreen_toggle, NULL);
1237         
1238         menu_item = gtk_menu_item_new ();
1239         gtk_widget_show (menu_item);
1240         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1241         gtk_widget_set_sensitive (menu_item, FALSE);
1242
1243         menu_item = gtk_image_menu_item_new_from_stock ("gtk-preferences", accel_group);
1244         gtk_widget_show (menu_item);
1245         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1246         g_signal_connect(menu_item, "activate", (GCallback) display_options, NULL);
1247
1248         /* HELP */ 
1249         menu_item = gtk_menu_item_new_with_mnemonic ("_Help");
1250         gtk_widget_show (menu_item);
1251         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
1252         gtk_menu_item_set_right_justified(GTK_MENU_ITEM(menu_item), TRUE);
1253         
1254         sub_menu = gtk_menu_new ();
1255         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
1256
1257         menu_item = gtk_menu_item_new_with_mnemonic ("_Contents");
1258         gtk_widget_show (menu_item);
1259         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1260         g_signal_connect(menu_item, "activate", (GCallback) display_help, NULL);
1261         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_F1, (GdkModifierType) 0, GTK_ACCEL_VISIBLE);
1262
1263         menu_item = gtk_menu_item_new_with_mnemonic ("_About");
1264         gtk_widget_show (menu_item);
1265         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1266         g_signal_connect(menu_item, "activate", (GCallback) mplcfitx, NULL);
1267         
1268         menu_item = gtk_menu_item_new ();
1269         gtk_widget_show (menu_item);
1270         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1271         gtk_widget_set_sensitive (menu_item, FALSE);
1272
1273         menu_item = gtk_menu_item_new_with_mnemonic ("_Visit terminatorX.cx");
1274         gtk_widget_show (menu_item);
1275         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1276         g_signal_connect(menu_item, "activate", (GCallback) display_browser, NULL);
1277 }
1278
1279 void create_mastergui(int x, int y)
1280 {
1281         GtkWidget *mother_of_all_boxen;
1282         GtkWidget *main_vbox;
1283         GtkWidget *right_hbox;
1284         GtkWidget *left_hbox;
1285         GtkWidget *control_box;
1286         //GtkWidget *sequencer_box;
1287         GtkAdjustment *dumadj;
1288         GtkWidget *dummy;
1289         GtkWidget *master_vol_box;
1290         GtkWidget *status_box;
1291         
1292         gui_tooltips=gtk_tooltips_new();
1293
1294         main_window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
1295
1296         gtk_window_set_wmclass(GTK_WINDOW(main_window), "terminatorX", "tX_mastergui");
1297
1298         gtk_window_set_title(GTK_WINDOW(main_window), "terminatorX");
1299         
1300         gtk_container_set_border_width(GTK_CONTAINER(main_window), 5);
1301
1302         gtk_widget_realize(main_window);
1303         
1304         mother_of_all_boxen=gtk_vbox_new(FALSE, 5);
1305         gtk_container_add(GTK_CONTAINER(main_window), mother_of_all_boxen);
1306         gtk_widget_show(mother_of_all_boxen);   
1307         
1308         main_menubar=gtk_menu_bar_new();
1309         gtk_box_pack_start(GTK_BOX(mother_of_all_boxen), main_menubar, WID_FIX);
1310         gtk_widget_show(main_menubar);  
1311         
1312         create_master_menu();
1313         
1314         main_vbox=gtk_hbox_new(FALSE, 5);
1315         gtk_box_pack_start(GTK_BOX(mother_of_all_boxen), main_vbox, WID_DYN);
1316         gtk_widget_show(main_vbox);
1317         
1318         left_hbox=gtk_vbox_new(FALSE, 5);
1319         gtk_box_pack_start(GTK_BOX(main_vbox), left_hbox, WID_DYN);
1320         gtk_widget_show(left_hbox);
1321         
1322         control_box=gtk_hbox_new(FALSE, 5);
1323         gtk_box_pack_start(GTK_BOX(left_hbox), control_box, WID_FIX);
1324         gtk_widget_show(control_box);
1325         
1326         dummy=gtk_hseparator_new();
1327         gtk_box_pack_start(GTK_BOX(left_hbox), dummy, WID_FIX);
1328         gtk_widget_show(dummy);
1329
1330     /* control_box contents */
1331
1332         dummy=tx_xpm_label_box(AUDIOENGINE, "Audio");
1333         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1334         gtk_widget_show(dummy);
1335         
1336         dummy=tx_xpm_button_new(POWER,"Power ", 1);
1337         connect_button(dummy,audio_on, NULL);
1338         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1339         gui_set_tooltip(dummy, "Turn the audio engine on/off.");
1340         gtk_widget_show(dummy);
1341         engine_btn=dummy;
1342         
1343         grab_button=tx_xpm_button_new(GRAB, "Mouse Grab ", 1);
1344         gtk_box_pack_start(GTK_BOX(control_box), grab_button, WID_FIX);
1345         connect_button(grab_button, grab_on, NULL);
1346         gui_set_tooltip(grab_button, "Enter the mouse grab mode operation. Press <ESCAPE> to exit grab mode.");
1347         gtk_widget_show(grab_button);
1348
1349         dummy=gtk_vseparator_new();
1350         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1351         gtk_widget_show(dummy);
1352     
1353         dummy=tx_xpm_label_box(SEQUENCER, "Seq.");
1354         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1355         gtk_widget_show(dummy);
1356
1357         dummy=tx_xpm_button_new(PLAY,"Play ", 1);
1358         connect_button(dummy, seq_play, NULL);
1359         seq_play_btn=dummy;
1360         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1361         gui_set_tooltip(dummy, "Playback previously recorded events from the sequencer. This will turn on the audio engine automagically.");
1362         gtk_widget_show(dummy);
1363
1364         dummy=tx_xpm_button_new(STOP,"Stop ", 0);
1365         seq_stop_btn=dummy;
1366         connect_button(dummy, seq_stop, NULL);  
1367         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1368         gui_set_tooltip(dummy, "Stop the playback of sequencer events.");
1369         gtk_widget_show(dummy);
1370
1371         dummy=tx_xpm_button_new(RECORD,"Record ", 1);
1372         connect_button(dummy, seq_rec, NULL);
1373         seq_rec_btn=dummy;
1374         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1375         gui_set_tooltip(dummy, "Enable recording of *events* into the sequencer. All touched controls will be recorded. Existing events for the song-time recording will be overwritten for touched controls.");
1376         gtk_widget_show(dummy);
1377
1378         dummy=gtk_label_new("Pos:");
1379         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1380         gtk_widget_show(dummy);
1381         
1382         dummy=gtk_entry_new();
1383         gtk_entry_set_max_length(GTK_ENTRY(dummy), 12);
1384         seq_entry=dummy;
1385         //gtk_widget_set_usize(dummy, 65, 20);
1386         gtk_entry_set_text(GTK_ENTRY(dummy), "00:00.00");
1387         gtk_entry_set_width_chars(GTK_ENTRY(dummy), 9);
1388         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1389         gtk_widget_show(dummy);
1390
1391         dumadj=(GtkAdjustment*) gtk_adjustment_new(0, 0, 100, 0.1, 1, 1);
1392         seq_adj=dumadj;
1393         connect_adj(dumadj, sequencer_move, NULL);      
1394         dummy=gtk_hscale_new(dumadj);
1395         gtk_widget_set_size_request(dummy, 65, 20);
1396         seq_slider=dummy;
1397         g_signal_connect(G_OBJECT(seq_slider), "button-release-event", (GtkSignalFunc) seq_slider_released, NULL);
1398         gtk_scale_set_draw_value(GTK_SCALE(dummy), FALSE);
1399         
1400         gui_set_tooltip(dummy, "Select the start position for the sequencer in song-time.");
1401         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_DYN);
1402         gtk_widget_show(dummy);
1403         
1404         dummy=gtk_hbox_new(FALSE,2); //gtk_hpaned_new ();
1405         gtk_box_pack_start(GTK_BOX(left_hbox), dummy, WID_DYN);
1406         gtk_widget_show(dummy);
1407         
1408         tt_parent=dummy;
1409
1410     panel_bar=gtk_hbox_new(TRUE,2);
1411         gtk_box_pack_start(GTK_BOX(left_hbox), panel_bar, WID_FIX);
1412
1413         control_parent=gtk_hbox_new(FALSE,0);
1414         gtk_box_pack_start(GTK_BOX(tt_parent), control_parent, WID_FIX);
1415         gtk_widget_show(control_parent);
1416
1417         dummy=gtk_vseparator_new();
1418         gtk_box_pack_start(GTK_BOX(tt_parent), dummy, WID_FIX);
1419         gtk_widget_show(dummy);
1420
1421         audio_parent=gtk_vbox_new(FALSE,0);
1422         gtk_box_pack_start(GTK_BOX(tt_parent), audio_parent, WID_DYN);
1423         gtk_widget_show(audio_parent);
1424         
1425         dummy=gtk_vseparator_new();
1426         gtk_box_pack_start(GTK_BOX(main_vbox), dummy, WID_FIX);
1427         gtk_widget_show(dummy);
1428                 
1429         right_hbox=gtk_vbox_new(FALSE, 5);
1430         gtk_box_pack_start(GTK_BOX(main_vbox), right_hbox, WID_FIX);
1431         gtk_widget_show(right_hbox);
1432
1433         /* Master */
1434         
1435         dummy=gtk_label_new(NULL);
1436         gtk_label_set_markup(GTK_LABEL(dummy),"<b>Master</b>");
1437         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1438         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1439         gtk_widget_show(dummy); 
1440
1441         dummy=gtk_hseparator_new();
1442         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1443         gtk_widget_show(dummy);
1444
1445          /* Pitch */
1446          
1447         /*dummy=gtk_label_new("Pitch:");
1448         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1449         gtk_widget_show(dummy);*/
1450
1451         dumadj=(GtkAdjustment*) gtk_adjustment_new(globals.pitch, -3, 3, 0.001, 0.001, 0.01);
1452         pitch_adj=dumadj;
1453         connect_adj(dumadj, master_pitch_changed, NULL);
1454         
1455         tX_extdial *pdial=new tX_extdial("Pitch", pitch_adj, &sp_master_pitch, true);
1456         gtk_box_pack_start(GTK_BOX(right_hbox), pdial->get_widget(), WID_FIX);
1457         gui_set_tooltip(pdial->get_entry(), "Use this dial to adjust the master pitch (affecting *all* turntables).");
1458         
1459         dummy=gtk_hseparator_new();
1460         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1461         gtk_widget_show(dummy);
1462         
1463         /* Volume */
1464         master_vol_box=gtk_hbox_new(FALSE, 5);
1465         gtk_box_pack_start(GTK_BOX(right_hbox), master_vol_box, WID_DYN);
1466         gtk_widget_show(master_vol_box);        
1467         
1468         dumadj=(GtkAdjustment*) gtk_adjustment_new(globals.volume, 0, 2, 0.01, 0.05, 0.000);
1469         volume_adj=dumadj;
1470
1471         connect_adj(dumadj, master_volume_changed, NULL);       
1472         dummy=gtk_vscale_new(dumadj);
1473         gtk_range_set_inverted(GTK_RANGE(dummy), TRUE);
1474         gtk_scale_set_draw_value(GTK_SCALE(dummy), False);
1475         g_signal_connect(G_OBJECT(dummy), "button_press_event", (GtkSignalFunc) tX_seqpar::tX_seqpar_press, &sp_master_volume); 
1476         
1477         gtk_box_pack_end(GTK_BOX(master_vol_box), dummy, WID_FIX);
1478         gtk_widget_show(dummy); 
1479         gui_set_tooltip(dummy, "Adjust the master volume. This parameter will effect *all* turntables in the set.");
1480         
1481         main_flash=gtk_tx_flash_new();
1482         gtk_box_pack_end(GTK_BOX(master_vol_box), main_flash, WID_DYN);
1483         gtk_widget_show(main_flash);
1484
1485         dummy=gtk_label_new("Volume");
1486         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1487         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1488         gtk_widget_show(dummy);
1489
1490         /* STATUS BOX */ 
1491         dummy=gtk_hseparator_new();
1492         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1493         gtk_widget_show(dummy);
1494         
1495         status_box=gtk_vbox_new(FALSE, 0);
1496         gtk_box_pack_start(GTK_BOX(right_hbox), status_box, WID_FIX);
1497         gtk_widget_show(status_box);
1498         
1499         dummy=gtk_label_new("0");
1500         used_mem=dummy;
1501         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1502         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1503         gtk_widget_show(dummy);
1504
1505         dummy=gtk_label_new("Mem/kB:");
1506         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1507         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1508         gtk_widget_show(dummy);
1509         
1510         /*add_sep2();
1511
1512         dummy=gtk_label_new("1");
1513         no_of_vtts=dummy;
1514         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1515         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1516         gtk_widget_show(dummy);
1517
1518         dummy=gtk_label_new("Vtts:");
1519         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1520         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1521         gtk_widget_show(dummy);*/
1522
1523         add_sep2();
1524
1525         dummy=gtk_label_new("Rel. "VERSION);
1526         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1527         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1528         gtk_widget_show(dummy);
1529
1530         /*dummy=gtk_label_new("Release:");
1531         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1532         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1533         gtk_widget_show(dummy);*/
1534         
1535         add_sep2();
1536
1537         dummy=gtk_label_new(NULL);
1538         gtk_label_set_markup(GTK_LABEL(dummy), "<b>Status</b>");
1539         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1540         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1541         gtk_widget_show(dummy);
1542         
1543         /* END GUI */
1544         
1545         gtk_window_set_default_size(GTK_WINDOW(main_window), x, y);     
1546         gtk_widget_set_sensitive(grab_button, 0);
1547
1548         new_table(NULL, NULL); // to give the user something to start with ;)
1549
1550         g_signal_connect (G_OBJECT(main_window), "delete-event", (GtkSignalFunc) quit, NULL);
1551         
1552         if (globals.tooltips) gtk_tooltips_enable(gui_tooltips);
1553         else gtk_tooltips_disable(gui_tooltips);
1554 }
1555
1556 gfloat old_percent=-1;
1557
1558 void note_destroy(GtkWidget *widget, GtkWidget *mbox)
1559 {
1560         gtk_widget_destroy(GTK_WIDGET(mbox));
1561 }
1562
1563 void tx_note(const char *message, bool isError, GtkWindow *window)
1564 {
1565         char buffer[4096]="terminatorX ";
1566         if (isError) {
1567                 strcat(buffer, "note:\n\n");
1568         } else {
1569                 strcat(buffer, "error:\n\n");
1570         }
1571         
1572         if (!window) window==GTK_WINDOW(main_window);
1573         
1574         GtkWidget *dialog=gtk_message_dialog_new(window,
1575                 GTK_DIALOG_DESTROY_WITH_PARENT,
1576                 isError ? GTK_MESSAGE_ERROR : GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "%s", message);
1577         gtk_dialog_run(GTK_DIALOG(dialog));
1578         gtk_widget_destroy(dialog);     
1579 }
1580
1581
1582 void tx_l_note(const char *message)
1583 {
1584         char buffer[4096]="Plugin info:\n\n";
1585         strcat(buffer, message);
1586         
1587         GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window),
1588                 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "%s", message);
1589         gtk_dialog_run(GTK_DIALOG(dialog));
1590         gtk_widget_destroy(dialog);     
1591 }
1592
1593 void add_to_panel_bar(GtkWidget *button) 
1594 {
1595         buttons_on_panel_bar++;
1596         gtk_box_pack_start(GTK_BOX(panel_bar), button, WID_DYN);
1597         gtk_widget_show(panel_bar);
1598 }
1599
1600 void remove_from_panel_bar(GtkWidget *button) 
1601 {
1602         buttons_on_panel_bar--;
1603         gtk_container_remove(GTK_CONTAINER(panel_bar), button);
1604         if (buttons_on_panel_bar==0) gtk_widget_hide(panel_bar);
1605 }
1606
1607 /* Fullscreen code... */
1608 #define _WIN_LAYER_TOP          -1
1609 #define _WIN_LAYER_NORMAL       4
1610 #define _NET_WM_STATE_REMOVE    0
1611 #define _NET_WM_STATE_ADD       1
1612 #define _NET_WM_STATE_TOGGLE    2
1613
1614 void fullscreen_toggle(GtkCheckMenuItem *item, gpointer data) {
1615         XEvent xev;
1616         Window win=GDK_WINDOW_XID(main_window->window);
1617         Display *disp=GDK_WINDOW_XDISPLAY(main_window->window);
1618         
1619         globals.fullscreen_enabled=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(fullscreen_item));
1620         
1621         /* Top layer.. */
1622         xev.xclient.type = ClientMessage;
1623         xev.xclient.serial = 0;
1624         xev.xclient.send_event = True;
1625         xev.xclient.display = disp;
1626         xev.xclient.window = win;
1627         xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_WIN_LAYER");
1628         xev.xclient.format = 32;
1629         xev.xclient.data.l[0] = globals.fullscreen_enabled ? _WIN_LAYER_TOP : _WIN_LAYER_NORMAL ;
1630         XSendEvent(disp, GDK_WINDOW_XID (gdk_get_default_root_window ()),
1631                 False, SubstructureRedirectMask | SubstructureNotifyMask,
1632                 &xev);
1633         
1634         /* Fullscreen */
1635         xev.xclient.type = ClientMessage;
1636         xev.xclient.serial = 0;
1637         xev.xclient.send_event = True;
1638         xev.xclient.display = disp;
1639         xev.xclient.window = win;
1640         xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_NET_WM_STATE");
1641         xev.xclient.format = 32;
1642         xev.xclient.data.l[0] = globals.fullscreen_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1643         xev.xclient.data.l[1] = gdk_x11_atom_to_xatom (gdk_atom_intern ("_NET_WM_STATE_FULLSCREEN", TRUE));
1644         xev.xclient.data.l[2] = gdk_x11_atom_to_xatom (GDK_NONE);
1645         XSendEvent(gdk_display, GDK_WINDOW_XID (gdk_get_default_root_window ()),
1646                 False, SubstructureRedirectMask | SubstructureNotifyMask,
1647                 &xev);  
1648 }
1649
1650 void fullscreen_setup() {
1651         if (globals.fullscreen_enabled) {
1652                 fullscreen_toggle(NULL, NULL);
1653         }
1654 }
1655
1656 void display_mastergui()
1657 {
1658         GtkWidget *top;
1659         gtk_widget_realize(main_window);
1660         tX_set_icon(main_window);
1661         load_knob_pixs();
1662         gtk_widget_show(main_window);
1663         fullscreen_setup();     
1664         top=gtk_widget_get_toplevel(main_window);
1665         xwindow=GDK_WINDOW_XWINDOW(top->window);
1666 }
1667
1668 pid_t help_child=0;
1669 GTimer *help_timer=NULL;
1670 int help_tag=-1;
1671
1672 int help_checker()
1673 {
1674         gdouble time;
1675         gulong ms;
1676         int status;
1677         int result=waitpid(help_child, &status, WNOHANG);
1678         
1679         if (result==0) {
1680                 time=g_timer_elapsed(help_timer, &ms);
1681                 if (time > 5) {
1682                         /* 5 seconds and it's still running - so we assume everything's OK. */
1683                         tX_debug("No longer waiting for gnome-help..");
1684                         gtk_idle_remove(help_tag);
1685                         help_tag=-1;
1686                 }
1687         } else {
1688                 /* We are still here and the child exited - that could mean trouble. */
1689                 tx_note("Couldn't run the gnome-help command (alias \"yelp\") to display the terminatorX manual. Please ensure that \"yelp\" is installed.", true);             
1690                 
1691                 gtk_idle_remove(help_tag);
1692                 help_tag=-1;
1693         }
1694         return TRUE;    
1695 }
1696
1697 #ifndef INSTALL_PREFIX
1698 #define INSTALL_PREFIX "/usr/local/share"
1699 #endif
1700
1701 void display_help()
1702 {       
1703         help_child=fork();
1704
1705         if (help_tag!=-1) {
1706                 gtk_idle_remove(help_tag);
1707                 if (help_timer) g_timer_destroy(help_timer);
1708                 help_child=0;
1709                 help_tag=-1;
1710                 help_timer=NULL;
1711         }
1712         
1713         if (help_child==0) {
1714                 // child
1715                 // execlp("gnome-help","gnome-help","ghelp:/" INSTALL_PREFIX "/terminatorX/doc/terminatorX-manual/C/terminatorX-manual.xml", NULL);
1716                 execlp("gnome-help","gnome-help","ghelp:/" XML_MANUAL, NULL);           
1717                 _exit(-1);
1718         } else if (help_child==-1) {
1719                 tx_note("System error: couldn't fork() to run the help process.", true);
1720         } else {
1721                 help_timer=g_timer_new();
1722                 g_timer_start(help_timer);
1723         
1724                 help_tag=gtk_idle_add((GtkFunction) help_checker, NULL);
1725         }
1726 }
1727
1728 pid_t browser_child=0;
1729 GTimer *browser_timer=NULL;
1730 int browser_tag=-1;
1731
1732 int browser_checker()
1733 {
1734         gdouble time;
1735         gulong ms;
1736         int status;
1737         int result=waitpid(browser_child, &status, WNOHANG);
1738         
1739         if (result==0) {
1740                 time=g_timer_elapsed(browser_timer, &ms);
1741                 if (time > 5) {
1742                         /* 5 seconds and it's still running - so we assume everything's OK. */
1743                         tX_debug("No longer waiting for a browser..");
1744                         gtk_idle_remove(browser_tag);
1745                         browser_tag=-1;
1746                 }
1747         } else {
1748                 /* We are still here and the child exited - that could mean trouble. */
1749                 tx_note("Failed to run a suitable web browser - if there's one installed on this system, please run it and forward yourself to:\nhttp://terminatorX.cx", true);         
1750                 
1751                 gtk_idle_remove(browser_tag);
1752                 browser_tag=-1;
1753         }
1754         return TRUE;    
1755 }
1756
1757 void display_browser()
1758 {       
1759         browser_child=fork();
1760
1761         if (browser_tag!=-1) {
1762                 gtk_idle_remove(browser_tag);
1763                 if (browser_timer) g_timer_destroy(browser_timer);
1764                 browser_child=0;
1765                 browser_tag=-1;
1766                 browser_timer=NULL;
1767         }
1768         
1769         if (browser_child==0) {
1770                 // child
1771                 execlp("mozilla","mozilla","http://terminatorX.cx", NULL);
1772                 execlp("netscape","netscape","http://terminatorX.cx", NULL);
1773                 execlp("galeon","galeon","http://terminatorX.cx", NULL);
1774                 execlp("konqueror","konqueror","http://terminatorX.cx", NULL);          
1775                 _exit(-1);
1776         } else if (browser_child==-1) {
1777                 tx_note("System error: couldn't fork() to run the browser process.", true);
1778         } else {
1779                 browser_timer=g_timer_new();
1780                 g_timer_start(browser_timer);
1781         
1782                 browser_tag=gtk_idle_add((GtkFunction) browser_checker, NULL);
1783         }
1784 }
1785
1786
1787
1788 GdkCursor *tX_cursor::cursors[MAX_CURSOR]={NULL, NULL, NULL};
1789 tX_cursor::cursor_shape tX_cursor::current_shape=tX_cursor::DEFAULT_CURSOR;
1790
1791 void tX_cursor::set_cursor(cursor_shape shape)
1792 {
1793         switch (shape) {
1794                 case DEFAULT_CURSOR:
1795                         cursors[shape]=NULL;
1796                         break;
1797                 
1798                 case WAIT_CURSOR:
1799                         if (!cursors[shape]) cursors[shape]=gdk_cursor_new(GDK_WATCH);
1800                         break;
1801                 
1802                 case WAIT_A_SECOND_CURSOR:
1803                         /* FIXME: What's that short-time wait cursor's id? */
1804                         if (!cursors[shape]) cursors[shape]=gdk_cursor_new(GDK_WATCH);
1805                         break;
1806                 
1807                 default:
1808                         tX_debug("No such cursor shape.");
1809                         return;
1810         }
1811         
1812         /* Still here? Ok... */
1813         current_shape=shape;
1814         
1815         gdk_window_set_cursor(main_window->window, cursors[shape]);
1816 }
1817
1818 GdkCursor *tX_cursor::get_cursor()
1819 {
1820         return cursors[current_shape];
1821 }
1822
1823 void tX_cursor::reset_cursor()
1824 {
1825         current_shape=DEFAULT_CURSOR;
1826         gdk_window_set_cursor(main_window->window, cursors[current_shape]);
1827 }