98cfc1a77d7d67e49df6aaf8d4be45e724a8863f
[terminatorX.git] / src / tX_mastergui.cc
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: 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 <sys/time.h>
46 #include <sys/wait.h>
47
48 #ifdef USE_SCHEDULER
49 #include <sys/resource.h>
50 #endif
51
52 #define TX_SET_ID_10 "terminatorX turntable set file - version 1.0 - data:"
53 #define TX_SET_ID_11 "terminatorX turntable set file - version 1.1 - data:"
54 #define TX_SET_ID_12 "terminatorX turntable set file - version 1.2 - data:"
55 #define TX_SET_ID_13 "terminatorX turntable set file - version 1.3 - data:"
56 #define TX_SET_ID_14 "terminatorX turntable set file - version 1.4 - data:"
57
58 int audioon=0;
59 int sequencer_ready=1;
60
61 bool tX_shutdown=false;
62
63 GtkWidget *tt_parent;
64 GtkWidget *control_parent;
65 GtkWidget *audio_parent;
66 GtkWidget *main_window;
67 GtkWidget *grab_button;
68 GtkWidget *main_flash_l;
69 GtkWidget *main_flash_r;
70
71 GtkWidget *seq_rec_btn;
72 GtkWidget *seq_play_btn;
73 GtkWidget *seq_stop_btn;
74 GtkAdjustment *seq_adj;
75 GtkWidget *seq_slider;
76 GtkWidget *seq_entry;
77 GtkWidget *panel_bar;
78
79 int buttons_on_panel_bar=0;
80
81 int seq_adj_care=1;
82 int seq_stop_override=0;
83
84 GtkAdjustment *volume_adj;
85 GtkAdjustment *pitch_adj;
86
87 /* seq-pars */
88 tX_seqpar_master_volume sp_master_volume;
89 tX_seqpar_master_pitch sp_master_pitch;
90
91 GtkWidget *engine_btn;
92
93 GtkWidget *main_menubar;
94 GtkWidget *rec_menu_item;
95 GtkWidget *fullscreen_item;
96
97 int rec_dont_care=0;
98 gint update_tag;
99
100 #define connect_entry(wid, func, ptr); gtk_signal_connect(GTK_OBJECT(wid), "activate", (GtkSignalFunc) func, (void *) ptr);
101 #define connect_adj(wid, func, ptr); gtk_signal_connect(GTK_OBJECT(wid), "value_changed", (GtkSignalFunc) func, (void *) ptr);
102 #define connect_button(wid, func, ptr); gtk_signal_connect(GTK_OBJECT(wid), "clicked", (GtkSignalFunc) func, (void *) ptr);
103
104 Window xwindow;
105 #define WID_DYN TRUE, TRUE, 0
106 #define WID_FIX FALSE, FALSE, 0
107 extern void add_vtt(GtkWidget *ctrl, GtkWidget *audio, char *fn);
108 extern void destroy_gui(vtt_class *vtt);
109 extern void gui_show_frame(vtt_class *vtt, int show);
110
111 GdkWindow *save_dialog_win=NULL;
112 GdkWindow *load_dialog_win=NULL;
113 GtkWidget *save_dialog=NULL;
114 GtkWidget *load_dialog=NULL;
115
116 GdkWindow *rec_dialog_win=NULL;
117 GtkWidget *rec_dialog=NULL;
118
119 GtkWidget *no_of_vtts=NULL;
120 GtkWidget *used_mem=NULL;
121
122 int stop_update=0;
123 int update_delay;
124
125 vtt_class *old_focus=NULL;
126
127 int grab_status=0;
128 int last_grab_status=0;
129
130 GtkTooltips *gui_tooltips=NULL;
131
132 void gui_set_tooltip(GtkWidget *wid, char *tip)
133 {
134         gtk_tooltips_set_tip(gui_tooltips, wid, tip, NULL);
135 }
136
137 void turn_audio_off(void)
138 {
139         if (audioon) 
140         {
141                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 0);
142                 while (gtk_events_pending()) gtk_main_iteration();              
143         }
144 }
145
146
147 gint pos_update(gpointer data)
148 {
149         f_prec temp;
150
151         if (stop_update) 
152         {               
153                 cleanup_all_vtts();
154                 tX_seqpar :: update_all_graphics();
155                 if (old_focus) gui_show_frame(old_focus, 0);
156                 old_focus=NULL;
157                 gtk_tx_flash_clear(main_flash_l);
158                 gtk_tx_flash_clear(main_flash_r);
159                 gdk_flush();    
160                 update_tag=0;
161                 return(FALSE);
162         }
163         else
164         {
165                 update_all_vtts();
166                 
167                 /*left vu meter */
168                 temp=vtt_class::mix_max_l;
169                 vtt_class::mix_max_l=0;
170                 gtk_tx_flash_set_level(main_flash_l, temp);
171
172                 /*right vu meter */
173                 temp=vtt_class::mix_max_r;
174                 vtt_class::mix_max_r=0;
175                 gtk_tx_flash_set_level(main_flash_r, temp);
176                 
177                 if (vtt_class::focused_vtt!=old_focus)
178                 {
179                         if (old_focus) gui_show_frame(old_focus, 0);
180                         old_focus=vtt_class::focused_vtt;
181                         if (old_focus) gui_show_frame(old_focus, 1);                    
182                 }
183                 if (grab_status!=last_grab_status)
184                 {
185                         last_grab_status=grab_status;
186                         if (!grab_status) 
187                         {
188                                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(grab_button), 0);
189                         }
190                 }
191                 gdk_flush();    
192                 update_delay--;
193                 
194                 if (update_delay < 0)
195                 {
196                         seq_update();
197                         tX_seqpar :: update_all_graphics();
198                         update_delay=globals.update_delay;
199                 }
200                 return(TRUE);
201         }
202 }
203
204 void mg_update_status()
205 {
206         FILE *procfs;
207         pid_t mypid;
208         char filename[PATH_MAX];
209         char buffer[256];
210         int found=0;    
211         int mem;
212         
213         mypid=getpid();
214         sprintf(filename, "/proc/%i/status", mypid);
215         procfs=fopen(filename, "r");
216         if (procfs)
217         {
218                 while((!feof(procfs)) && !found)
219                 {
220                         fgets(buffer, 256, procfs);
221                         if (strncmp("VmSize:", buffer, 7)==0)
222                         {
223                                 found=1;
224                                 sscanf(buffer, "VmSize: %i kB", &mem);
225                                 sprintf(buffer, "%i", mem);
226                                 gtk_label_set(GTK_LABEL(used_mem), buffer);
227                         }
228                 }
229         }
230         fclose(procfs); 
231         
232         sprintf(buffer, "%i", vtt_class::vtt_amount);
233         gtk_label_set(GTK_LABEL(no_of_vtts), buffer);
234 }
235
236 GtkSignalFunc new_table(GtkWidget *, char *fn)
237 {
238         turn_audio_off();
239                 
240                 if (fn) 
241                 {
242                         ld_create_loaddlg(TX_LOADDLG_MODE_SINGLE, 1);
243                         ld_set_filename(fn);
244                 }
245                 
246                 add_vtt(control_parent, audio_parent, fn);                              
247                 
248                 if (fn) ld_destroy();           
249         mg_update_status();
250         return NULL;
251 }
252
253 GtkSignalFunc drop_new_table(GtkWidget *widget, GdkDragContext *context,
254                 gint x, gint y, GtkSelectionData *selection_data,
255                 guint info, guint time, vtt_class *vtt)
256 {
257         char filename[PATH_MAX];
258         char *fn;
259         
260         strncpy(filename, (char *) selection_data->data, (size_t) selection_data->length);
261         filename[selection_data->length]=0;
262
263         fn = strchr (filename, '\r');
264         *fn=0;  
265         
266         fn = strchr (filename, ':');
267         if (fn) fn++; else fn=(char *) selection_data->data;
268         
269         new_table(NULL, fn);
270
271         strcpy (filename, "dont segfault workaround ;)");
272         return NULL;
273 }
274
275 bool tx_mg_have_setname=false;
276 char tx_mg_current_setname[PATH_MAX]="";
277
278 GtkSignalFunc new_tables() {
279         GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window), 
280                 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
281                 "Are you sure you want to loose all turntables and events?");
282         
283         int res=gtk_dialog_run(GTK_DIALOG(dialog));
284         gtk_widget_destroy(dialog);
285                 
286         if (res!=GTK_RESPONSE_YES) {
287                 return NULL;
288         }
289
290         vtt_class::delete_all();
291         new_table(NULL, NULL);
292         sequencer.delete_all_events();
293         gtk_window_set_title(GTK_WINDOW(main_window), "terminatorX");
294
295         return NULL;
296 }
297
298 /* Loading saved setups */
299
300 GtkSignalFunc cancel_load_tables(GtkWidget *wid)
301 {
302         gtk_widget_destroy(load_dialog);
303         load_dialog=NULL;
304         load_dialog_win=NULL;
305         return(0);
306 }
307
308 void load_tt_part(char * buffer)
309 {
310         FILE *in;
311         char idbuff[256];
312         char wbuf[PATH_MAX];
313         xmlDocPtr doc;
314         
315         turn_audio_off();
316         
317         strcpy(globals.tables_filename, buffer);
318         
319         doc = xmlParseFile(buffer);
320         if (doc) {
321                 vtt_class::load_all(doc, buffer);
322                 xmlFreeDoc(doc);
323         } 
324         
325 #ifdef ENABLE_TX_LEGACY
326         else {  
327                 in=fopen(buffer, "r");  
328         
329                 if (in) {
330                         fread(idbuff, strlen(TX_SET_ID_10), 1, in);
331                         if (strncmp(idbuff, TX_SET_ID_10, strlen(TX_SET_ID_10))==0) {
332                                 if (vtt_class::load_all_10(in, buffer)) tx_note("Error while reading set.", true);
333                         } else if (strncmp(idbuff, TX_SET_ID_11, strlen(TX_SET_ID_11))==0)      {
334                                 if (vtt_class::load_all_11(in, buffer)) tx_note("Error while reading set.", true);                      
335                         } else if (strncmp(idbuff, TX_SET_ID_12, strlen(TX_SET_ID_12))==0) {
336                                 if (vtt_class::load_all_12(in, buffer)) tx_note("Error while reading set.", true);                      
337                         } else if (strncmp(idbuff, TX_SET_ID_13, strlen(TX_SET_ID_13))==0) {
338                                 if (vtt_class::load_all_13(in, buffer)) tx_note("Error while reading set.", true);                      
339                         } else if (strncmp(idbuff, TX_SET_ID_14, strlen(TX_SET_ID_14))==0) {
340                                 if (vtt_class::load_all_14(in, buffer)) tx_note("Error while reading set.", true);                      
341                         }       else {
342                                 tx_note("This file is not a terminatorX set-file.", true);
343                                 fclose(in);
344                                 return;
345                         }
346                         fclose(in);     
347                 } else {
348                         strcpy(idbuff, "Failed to access file: \"");    // I'm stealing the unrelated sting for a temp :)
349                         strcat(idbuff, globals.tables_filename);
350                         strcat(idbuff, "\"");
351                         tx_note(idbuff, true);
352                         
353                         return;
354                 }
355         }
356 #else
357         else {
358                 strcpy(idbuff, "Failed to access file: \"");    // I'm stealing the unrelated sting for a temp :)
359                 strcat(idbuff, globals.tables_filename);
360                 strcat(idbuff, "\"");
361                 tx_note(idbuff, true);
362                 
363                 return;
364         }
365 #endif  
366         
367         tx_mg_have_setname=true;
368         strcpy(tx_mg_current_setname, buffer);
369         
370         tX_seqpar :: init_all_graphics();
371         vg_init_all_non_seqpars();
372                 
373         gtk_adjustment_set_value(volume_adj, globals.volume);
374         gtk_adjustment_set_value(pitch_adj, globals.pitch);
375         sprintf(wbuf,"terminatorX - %s", strip_path(buffer));
376         gtk_window_set_title(GTK_WINDOW(main_window), wbuf);            
377 }
378
379 void do_load_tables(GtkWidget *wid)
380 {
381         char buffer[PATH_MAX];
382         
383         strcpy(buffer, gtk_file_selection_get_filename(GTK_FILE_SELECTION(load_dialog)));
384         
385         gtk_widget_destroy(load_dialog);
386         
387         load_dialog=NULL;
388         load_dialog_win=NULL;
389
390         load_tt_part(buffer);   
391 }
392
393 GtkSignalFunc load_tables()
394 {
395         if (load_dialog_win) 
396         {
397                 gdk_window_raise(load_dialog_win);
398                 return 0;
399         }
400         
401         load_dialog=gtk_file_selection_new("Load Set"); 
402         
403         gtk_file_selection_show_fileop_buttons(GTK_FILE_SELECTION(load_dialog));
404         gtk_file_selection_complete(GTK_FILE_SELECTION(load_dialog), "*.tX");
405         
406         if (strlen(globals.tables_filename))
407         {
408                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(load_dialog), globals.tables_filename);
409         }
410         
411         gtk_widget_show(load_dialog);
412         
413         load_dialog_win=load_dialog->window;
414         
415         gtk_signal_connect (GTK_OBJECT(GTK_FILE_SELECTION(load_dialog)->ok_button), "clicked", GTK_SIGNAL_FUNC(do_load_tables), NULL);
416         gtk_signal_connect (GTK_OBJECT(GTK_FILE_SELECTION(load_dialog)->cancel_button), "clicked", GTK_SIGNAL_FUNC (cancel_load_tables), NULL); 
417         gtk_signal_connect (GTK_OBJECT(load_dialog), "delete-event", GTK_SIGNAL_FUNC(cancel_load_tables), NULL);        
418         
419         return NULL;
420 }
421
422 GtkSignalFunc drop_set(GtkWidget *widget, GdkDragContext *context,
423                 gint x, gint y, GtkSelectionData *selection_data,
424                 guint info, guint time, vtt_class *vtt)
425 {
426         char filename[PATH_MAX];
427         char *fn;
428         
429         strncpy(filename, (char *) selection_data->data, (size_t) selection_data->length);
430         filename[selection_data->length]=0;
431
432         fn = strchr (filename, '\r');
433         *fn=0;  
434         
435         fn = strchr (filename, ':');
436         if (fn) fn++; else fn=(char *) selection_data->data;
437         
438         load_tt_part(fn);
439
440         strcpy (filename, "dont segfault workaround ;)");
441         return NULL;
442 }
443
444
445 /* save tables */
446
447 GtkSignalFunc cancel_save_tables(GtkWidget *wid)
448 {
449         gtk_widget_destroy(save_dialog);
450         save_dialog=NULL;
451         save_dialog_win=NULL;
452         return(0);
453 }
454
455 gboolean do_save_tables(GtkWidget *wid)
456 {
457         FILE *out;
458         gzFile zout;
459         char buffer[PATH_MAX];
460         char wbuf[PATH_MAX];
461         char *ext;
462         
463         if (wid) {
464                 strcpy(buffer, gtk_file_selection_get_filename(GTK_FILE_SELECTION(save_dialog)));
465                 int len=strlen(buffer);
466                 if (!len || (buffer[len-1]=='/')) {                     
467                         tx_note("Invalid filename for set file.", true);                        
468                         return FALSE;
469                 }
470                 strcpy(globals.tables_filename, buffer);
471                 gtk_widget_destroy(save_dialog);
472                 save_dialog=NULL;
473                 save_dialog_win=NULL;
474         } else {
475                 strcpy(buffer, tx_mg_current_setname);
476         }
477         
478         ext=strrchr(buffer, '.');
479         
480         if (ext)
481         {
482                 if (strcmp(ext, ".tX")) strcat(buffer, ".tX");
483         }
484         else
485         {
486                 strcat(buffer, ".tX");
487         }
488
489         tx_mg_have_setname=true;
490         strcpy(tx_mg_current_setname, buffer);
491         
492         if (globals.compress_set_files) {
493                 _store_compress_xml=1;
494                 out=NULL;
495                 zout=gzopen(buffer, "w");
496         } else {
497                 _store_compress_xml=0;
498                 out=fopen(buffer, "w");
499                 zout=NULL;
500         }
501         
502         if (out || zout)
503         {
504                 if (vtt_class::save_all(out, zout)) tx_note("Error while saving set.", true);
505                 if (out) fclose(out); 
506                 else if (zout) gzclose(zout);
507                 sprintf(wbuf,"terminatorX - %s", strip_path(buffer));
508                 gtk_window_set_title(GTK_WINDOW(main_window), wbuf);                            
509         }
510         else
511         {
512                 tx_note("Failed to open file for write access.", true);
513         }
514         
515         return FALSE;
516 }
517
518 GtkSignalFunc save_tables_as()
519 {
520         if (save_dialog_win) {
521                 gtk_widget_destroy(save_dialog);
522                 save_dialog=NULL;
523                 save_dialog_win=NULL;
524         }
525         
526         save_dialog=gtk_file_selection_new("Save Set"); 
527
528         if (tx_mg_have_setname) {
529                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(save_dialog), tx_mg_current_setname);
530         }
531         
532         gtk_widget_show(save_dialog);
533         
534         save_dialog_win=save_dialog->window;
535         
536         gtk_signal_connect (GTK_OBJECT(GTK_FILE_SELECTION(save_dialog)->ok_button), "clicked", GTK_SIGNAL_FUNC(do_save_tables), NULL);
537         gtk_signal_connect (GTK_OBJECT(GTK_FILE_SELECTION(save_dialog)->cancel_button), "clicked", GTK_SIGNAL_FUNC (cancel_save_tables), NULL); 
538         gtk_signal_connect (GTK_OBJECT(save_dialog), "delete-event", GTK_SIGNAL_FUNC(cancel_save_tables), NULL);        
539
540         return NULL;
541 }
542
543 GtkSignalFunc save_tables()
544 {
545         if (!tx_mg_have_setname) {
546                 save_tables_as();
547         } else {
548                 do_save_tables(NULL);
549         }
550         
551         return NULL;
552 }
553
554 GtkSignalFunc master_volume_changed (GtkWidget *wid, void *d)
555 {
556         sp_master_volume.receive_gui_value((float) 2.0-GTK_ADJUSTMENT(wid)->value);
557         return NULL;    
558 }
559
560 GtkSignalFunc master_pitch_changed(GtkWidget *wid, void *d)
561 {
562         sp_master_pitch.receive_gui_value((float) GTK_ADJUSTMENT(wid)->value);  
563         return NULL;    
564 }
565
566 GtkSignalFunc saturate_changed(GtkWidget *w, void *d)
567 {
568         vtt_class::enable_saturate (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)));
569         return NULL;    
570 }
571
572 void mg_enable_critical_buttons(int enable)
573 {
574         gtk_widget_set_sensitive(seq_rec_btn, enable);
575         gtk_widget_set_sensitive(seq_play_btn, enable);
576         gtk_widget_set_sensitive(seq_slider, enable);
577
578         gtk_widget_set_sensitive(rec_menu_item, enable);
579         vg_enable_critical_buttons(enable);
580 }
581
582
583 GtkSignalFunc seq_stop(GtkWidget *w, void *);
584
585 static bool stop_override=false;
586
587 GtkSignalFunc audio_on(GtkWidget *w, void *d)
588 {
589         tX_engine_error res;
590         
591         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
592         {               
593                 sequencer_ready=0;
594                 mg_enable_critical_buttons(0);
595                 res=tX_engine::get_instance()->run();
596
597                 if (res!=NO_ERROR)
598                 {
599                         mg_enable_critical_buttons(1);
600                         stop_override=true;
601                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 0);
602                         stop_override=false;                    
603
604                         switch(res)
605                         {
606                                 case ERROR_BUSY:
607                                 tx_note("Error starting engine: engine is already running.", true);
608                                 break;
609                                 case ERROR_AUDIO:
610                                 tx_note("Error starting engine: failed to access audiodevice.", true);
611                                 break;
612                                 case ERROR_TAPE:
613                                 tx_note("Error starting engine: failed to open the recording file.", true);
614                                 break;
615                                 default:tx_note("Error starting engine: Unknown error.", true);
616                         }
617                         
618                         return 0;
619                 }
620                 
621                 sequencer_ready=1;
622                 stop_update=0;
623                 audioon=1;
624                 update_delay=globals.update_delay;
625                 update_tag=gtk_timeout_add(globals.update_idle, (GtkFunction) pos_update, NULL);
626                 gtk_widget_set_sensitive(grab_button, 1);
627         }
628         else
629         {       
630                 if (stop_override) return NULL;
631                 if (!sequencer_ready) return NULL;
632                 gtk_widget_set_sensitive(grab_button, 0);
633                 tX_engine::get_instance()->stop();
634                 stop_update=1;
635                 audioon=0;
636                 if (tX_engine::get_instance()->get_recording_request()) {
637                         tX_engine::get_instance()->set_recording_request(false);
638                         rec_dont_care=1;
639                         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(rec_menu_item), 0);
640                         rec_dont_care=0;
641                 }
642                 seq_stop(NULL, NULL);
643                 mg_enable_critical_buttons(1);
644         }
645         
646         return NULL;
647 }
648
649 GtkSignalFunc cancel_rec(GtkWidget *wid)
650 {
651         gtk_widget_destroy(rec_dialog);
652         rec_dialog=NULL;
653         rec_dialog_win=NULL;
654         rec_dont_care=0;
655         return(0);
656 }
657
658 void do_rec(GtkWidget *wid)
659 {
660         char buffer[PATH_MAX];
661         
662         strcpy(buffer, gtk_file_selection_get_filename(GTK_FILE_SELECTION(rec_dialog)));
663
664         if (strlen(buffer))
665         {
666                 strcpy(globals.record_filename, buffer);                
667                 tX_engine::get_instance()->set_recording_request(true);
668                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(rec_menu_item), 1);
669         }
670         
671         rec_dont_care=0;
672         
673         gtk_widget_destroy(rec_dialog);
674         
675         rec_dialog=NULL;
676         rec_dialog_win=NULL;
677 }
678
679 GtkSignalFunc select_rec_file()
680 {
681         if (rec_dialog_win) 
682         {
683                 gdk_window_raise(rec_dialog_win);
684                 return 0;
685         }
686         
687         rec_dialog=gtk_file_selection_new("Record To Disk");    
688         
689         if (strlen(globals.record_filename))
690         {
691                 gtk_file_selection_set_filename(GTK_FILE_SELECTION(rec_dialog), globals.record_filename);
692         }
693         
694         gtk_widget_show(rec_dialog);
695         
696         rec_dialog_win=rec_dialog->window;
697         
698         gtk_signal_connect (GTK_OBJECT(GTK_FILE_SELECTION(rec_dialog)->ok_button), "clicked", GTK_SIGNAL_FUNC(do_rec), NULL);
699         gtk_signal_connect (GTK_OBJECT(GTK_FILE_SELECTION(rec_dialog)->cancel_button), "clicked", GTK_SIGNAL_FUNC (cancel_rec), NULL);  
700         gtk_signal_connect (GTK_OBJECT(rec_dialog), "delete-event", GTK_SIGNAL_FUNC(cancel_rec), NULL); 
701         
702         return NULL;
703 }
704
705 GtkSignalFunc tape_on(GtkWidget *w, void *d)
706 {
707         if (rec_dont_care) return 0;
708
709         if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(w)))
710         {       
711                 {
712                         rec_dont_care=1;
713                         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(w), 0);
714                         select_rec_file();
715                 }
716         }
717         else
718         {
719                         tX_engine::get_instance()->set_recording_request(false);
720         }
721         
722         return NULL;
723 }
724
725 void grab_on(GtkWidget *w, void *d)
726 {
727         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) {
728                 tX_engine::get_instance()->set_grab_request();
729         }
730         grab_status=1;
731 }
732
733 void grab_off()
734 {
735         grab_status=0;
736 }
737
738 gboolean quit()
739 {       
740         GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window), 
741         GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
742         "Exit terminatorX and loose all unsaved data?");
743         
744         int res=gtk_dialog_run(GTK_DIALOG(dialog));
745         gtk_widget_destroy(dialog);
746                 
747         if (res!=GTK_RESPONSE_YES) {
748                 return TRUE;
749         }
750
751         tX_shutdown=true;
752         
753         turn_audio_off();
754         vtt_class::delete_all();
755         
756         if (update_tag)
757         gtk_timeout_remove(update_tag);
758         globals.width=main_window->allocation.width;
759         globals.height=main_window->allocation.height;
760
761         gtk_main_quit();
762         
763         return true;
764 }
765
766 void mplcfitx()
767 /* Most Probably Least Called Function In terminatorX :) */
768 {
769         show_about(0);
770 }
771
772 GtkSignalFunc seq_play(GtkWidget *w, void *)
773 {
774         if ((sequencer.is_empty()) &&   (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(seq_rec_btn)))) {
775                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
776                  {
777                         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.");
778                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 0);
779                  }
780         } else {
781                 if (seq_stop_override) return NULL;
782                         
783                 seq_adj_care=0;
784                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 1);
785                 sequencer.trig_play();
786         
787                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 1);
788         }
789         
790         return NULL;
791 }
792
793 GtkSignalFunc seq_stop(GtkWidget *w, void *)
794 {
795         if (!sequencer_ready) return NULL;
796         sequencer.trig_stop();
797         seq_adj_care=1;
798         seq_stop_override=1;
799         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(seq_play_btn), 0);
800         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(seq_rec_btn), 0);
801         while (gtk_events_pending()) gtk_main_iteration();              
802         seq_stop_override=0;
803         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 0); 
804         gtk_widget_set_sensitive(seq_slider, 1);        
805         gtk_widget_set_sensitive(engine_btn, 1);
806         gtk_widget_set_sensitive(seq_rec_btn, 1);
807
808         return NULL;
809 }
810
811 GtkSignalFunc seq_rec(GtkWidget *w, void *)
812 {
813         seq_adj_care=0;
814         gtk_widget_set_sensitive(seq_slider, 0);
815
816         if (seq_stop_override) return NULL;
817         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 1);
818         gtk_widget_set_sensitive(engine_btn, 0);
819         gtk_widget_set_sensitive(seq_rec_btn, 0);
820         sequencer.trig_rec();
821         
822         return NULL;
823 }
824
825 void seq_update_entry(const guint32 timestamp)
826 {
827         char buffer[20];
828         guint32 samples;
829         guint32 minu,sec,hun;   
830         guint32 sr;
831         
832         samples=timestamp*vtt_class::get_mix_buffer_size();
833         sr=vtt_class::get_last_sample_rate();
834         
835         if (samples>0)
836         {
837                 minu=samples/(sr*60);
838                 samples-=(sr*60)*minu;
839         
840                 sec=samples/sr;
841                 samples-=sr*sec;
842         
843                 hun=samples/(sr/100);
844         }
845         else
846         {
847                 minu=sec=hun=0;
848         }
849         
850         sprintf(buffer, "%02i:%02i.%02i", minu, sec, hun);
851         gtk_entry_set_text(GTK_ENTRY(seq_entry), buffer);
852 }
853
854 void seq_update()
855 {
856         seq_update_entry(sequencer.get_timestamp());
857         gtk_adjustment_set_value(seq_adj, sequencer.get_timestamp_as_float());
858         
859 }
860 gboolean seq_slider_released(GtkWidget *wid, void *d)
861 {
862         seq_adj_care=0;
863         gtk_widget_set_sensitive(seq_slider, 0);        
864         sequencer.forward_to_start_timestamp(0);
865         gtk_widget_set_sensitive(seq_slider, 1);        
866         seq_adj_care=1;
867         
868         return FALSE;
869 }
870 void sequencer_move(GtkWidget *wid, void *d)
871 {
872         guint32 pos;
873         
874         if (seq_adj_care)
875         {
876                 pos=sequencer.set_start_timestamp((float) GTK_ADJUSTMENT(wid)->value);
877                 seq_update_entry(pos);  
878         }
879 }
880
881 #define add_sep();      dummy=gtk_hseparator_new ();\
882         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);\
883         gtk_widget_show(dummy);\
884
885 #define add_sep2();     dummy=gtk_hseparator_new ();\
886         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);\
887         gtk_widget_show(dummy);\
888
889 void fullscreen_toggle(GtkCheckMenuItem *item, gpointer data);
890 void display_help();
891 void display_browser();
892
893 void create_master_menu() {
894         GtkWidget *menu_item;
895         GtkWidget *sub_menu;
896         GtkAccelGroup* accel_group=gtk_accel_group_new();
897         gtk_window_add_accel_group(GTK_WINDOW(main_window), accel_group);
898
899         /* FILE */
900         menu_item = gtk_menu_item_new_with_mnemonic ("_File");
901         gtk_widget_show (menu_item);
902         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
903
904         sub_menu = gtk_menu_new ();
905         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
906
907         menu_item = gtk_image_menu_item_new_from_stock ("gtk-new", accel_group);
908         gtk_widget_show (menu_item);
909         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
910         g_signal_connect(menu_item, "activate", (GCallback) new_tables, NULL);
911
912         menu_item = gtk_image_menu_item_new_from_stock ("gtk-open", accel_group);
913         gtk_widget_show (menu_item);
914         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
915         g_signal_connect(menu_item, "activate", (GCallback) load_tables, NULL);
916
917         menu_item = gtk_image_menu_item_new_from_stock ("gtk-save", accel_group);
918         gtk_widget_show (menu_item);
919         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
920         g_signal_connect(menu_item, "activate", (GCallback) save_tables, NULL);
921
922         menu_item = gtk_image_menu_item_new_from_stock ("gtk-save-as", accel_group);
923         gtk_widget_show (menu_item);
924         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
925         g_signal_connect(menu_item, "activate", (GCallback) save_tables_as, NULL);
926
927         menu_item = gtk_menu_item_new ();
928         gtk_widget_show (menu_item);
929         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
930         gtk_widget_set_sensitive (menu_item, FALSE);
931
932         menu_item = gtk_image_menu_item_new_from_stock ("gtk-quit", accel_group);
933         gtk_widget_show (menu_item);
934         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
935         g_signal_connect(menu_item, "activate", (GCallback) quit, NULL);
936
937         /* Turntables */
938         menu_item = gtk_menu_item_new_with_mnemonic ("_Turntables");
939         gtk_widget_show (menu_item);
940         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
941         
942         sub_menu = gtk_menu_new ();
943         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
944
945         menu_item = gtk_menu_item_new_with_mnemonic("_Add Turntable");
946         gtk_widget_show (menu_item);
947         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
948         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_A, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);    
949         g_signal_connect(menu_item, "activate", (GCallback) new_table, NULL);
950
951         menu_item = gtk_check_menu_item_new_with_mnemonic("_Record Audio To Disk");
952         rec_menu_item = menu_item;
953         gtk_widget_show (menu_item);
954         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
955         g_signal_connect(menu_item, "activate", (GCallback) tape_on, NULL);
956
957         /* Options */
958         menu_item = gtk_menu_item_new_with_mnemonic ("_Options");
959         gtk_widget_show (menu_item);
960         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
961
962         sub_menu = gtk_menu_new ();
963         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
964                 
965         menu_item = gtk_check_menu_item_new_with_mnemonic("_Fullscreen");
966         fullscreen_item = menu_item;
967         gtk_widget_show (menu_item);
968         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
969
970         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), globals.fullscreen_enabled);
971         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_F11, (GdkModifierType) 0, GTK_ACCEL_VISIBLE);
972         g_signal_connect(menu_item, "activate", (GCallback) fullscreen_toggle, NULL);
973         
974         menu_item = gtk_menu_item_new ();
975         gtk_widget_show (menu_item);
976         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
977         gtk_widget_set_sensitive (menu_item, FALSE);
978
979         menu_item = gtk_image_menu_item_new_from_stock ("gtk-preferences", accel_group);
980         gtk_widget_show (menu_item);
981         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
982         g_signal_connect(menu_item, "activate", (GCallback) display_options, NULL);
983
984         /* HELP */ 
985         menu_item = gtk_menu_item_new_with_mnemonic ("_Help");
986         gtk_widget_show (menu_item);
987         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
988         gtk_menu_item_set_right_justified(GTK_MENU_ITEM(menu_item), TRUE);
989         
990         sub_menu = gtk_menu_new ();
991         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
992
993         menu_item = gtk_menu_item_new_with_mnemonic ("_Contents");
994         gtk_widget_show (menu_item);
995         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
996         g_signal_connect(menu_item, "activate", (GCallback) display_help, NULL);
997         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_F1, (GdkModifierType) 0, GTK_ACCEL_VISIBLE);
998
999         menu_item = gtk_menu_item_new_with_mnemonic ("_About");
1000         gtk_widget_show (menu_item);
1001         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1002         g_signal_connect(menu_item, "activate", (GCallback) mplcfitx, NULL);
1003         
1004         menu_item = gtk_menu_item_new ();
1005         gtk_widget_show (menu_item);
1006         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1007         gtk_widget_set_sensitive (menu_item, FALSE);
1008
1009         menu_item = gtk_menu_item_new_with_mnemonic ("_Visit terminatorX.cx");
1010         gtk_widget_show (menu_item);
1011         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1012         g_signal_connect(menu_item, "activate", (GCallback) display_browser, NULL);
1013 }
1014
1015 void create_mastergui(int x, int y)
1016 {
1017         GtkWidget *mother_of_all_boxen;
1018         GtkWidget *main_vbox;
1019         GtkWidget *right_hbox;
1020         GtkWidget *left_hbox;
1021         GtkWidget *control_box;
1022         //GtkWidget *sequencer_box;
1023         GtkAdjustment *dumadj;
1024         GtkWidget *dummy;
1025         GtkWidget *master_vol_box;
1026         GtkWidget *status_box;
1027         
1028         gui_tooltips=gtk_tooltips_new();
1029
1030         main_window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
1031
1032         gtk_window_set_wmclass(GTK_WINDOW(main_window), "terminatorX", "tX_mastergui");
1033
1034         gtk_window_set_title(GTK_WINDOW(main_window), "terminatorX");
1035         
1036         gtk_container_set_border_width(GTK_CONTAINER(main_window), 5);
1037
1038         gtk_widget_realize(main_window);
1039         
1040         mother_of_all_boxen=gtk_vbox_new(FALSE, 5);
1041         gtk_container_add(GTK_CONTAINER(main_window), mother_of_all_boxen);
1042         gtk_widget_show(mother_of_all_boxen);   
1043         
1044         main_menubar=gtk_menu_bar_new();
1045         gtk_box_pack_start(GTK_BOX(mother_of_all_boxen), main_menubar, WID_FIX);
1046         gtk_widget_show(main_menubar);  
1047         
1048         create_master_menu();
1049         
1050         main_vbox=gtk_hbox_new(FALSE, 5);
1051         gtk_box_pack_start(GTK_BOX(mother_of_all_boxen), main_vbox, WID_DYN);
1052         gtk_widget_show(main_vbox);
1053         
1054         left_hbox=gtk_vbox_new(FALSE, 5);
1055         gtk_box_pack_start(GTK_BOX(main_vbox), left_hbox, WID_DYN);
1056         gtk_widget_show(left_hbox);
1057         
1058         control_box=gtk_hbox_new(FALSE, 5);
1059         gtk_box_pack_start(GTK_BOX(left_hbox), control_box, WID_FIX);
1060         gtk_widget_show(control_box);
1061         
1062         dummy=gtk_hseparator_new();
1063         gtk_box_pack_start(GTK_BOX(left_hbox), dummy, WID_FIX);
1064         gtk_widget_show(dummy);
1065
1066     /* control_box contents */
1067
1068         dummy=tx_xpm_label_box(TX_ICON_AUDIOENGINE, "Audio");
1069         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1070         gtk_widget_show(dummy);
1071         
1072         dummy=tx_xpm_button_new(TX_ICON_POWER,"Power ", 1);
1073         connect_button(dummy,audio_on, NULL);
1074         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1075         gui_set_tooltip(dummy, "Turn the audio engine on/off.");
1076         gtk_widget_show(dummy);
1077         engine_btn=dummy;
1078         
1079         grab_button=tx_xpm_button_new(TX_ICON_GRAB, "Mouse Grab ", 1);
1080         gtk_box_pack_start(GTK_BOX(control_box), grab_button, WID_FIX);
1081         connect_button(grab_button, grab_on, NULL);
1082         gui_set_tooltip(grab_button, "Enter the mouse grab mode operation. Press <ESCAPE> to exit grab mode.");
1083         gtk_widget_show(grab_button);
1084
1085         dummy=gtk_vseparator_new();
1086         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1087         gtk_widget_show(dummy);
1088     
1089         dummy=tx_xpm_label_box(TX_ICON_SEQUENCER, "Seq.");
1090         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1091         gtk_widget_show(dummy);
1092
1093         dummy=tx_xpm_button_new(TX_ICON_PLAY,"Play ", 1);
1094         connect_button(dummy, seq_play, NULL);
1095         seq_play_btn=dummy;
1096         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1097         gui_set_tooltip(dummy, "Playback previously recorded events from the sequencer. This will turn on the audio engine automagically.");
1098         gtk_widget_show(dummy);
1099
1100         dummy=tx_xpm_button_new(TX_ICON_STOP,"Stop ", 0);
1101         seq_stop_btn=dummy;
1102         connect_button(dummy, seq_stop, NULL);  
1103         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1104         gui_set_tooltip(dummy, "Stop the playback of sequencer events.");
1105         gtk_widget_show(dummy);
1106
1107         dummy=tx_xpm_button_new(TX_ICON_RECORD,"Record ", 1);
1108         connect_button(dummy, seq_rec, NULL);
1109         seq_rec_btn=dummy;
1110         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1111         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.");
1112         gtk_widget_show(dummy);
1113
1114         dummy=gtk_label_new("Pos:");
1115         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1116         gtk_widget_show(dummy);
1117         
1118         dummy=gtk_entry_new_with_max_length(12);
1119         seq_entry=dummy;
1120         //gtk_widget_set_usize(dummy, 65, 20);
1121         gtk_entry_set_text(GTK_ENTRY(dummy), "00:00.00");
1122         gtk_entry_set_width_chars(GTK_ENTRY(dummy), 9);
1123         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1124         gtk_widget_show(dummy);
1125
1126         dumadj=(GtkAdjustment*) gtk_adjustment_new(0, 0, 100, 0.1, 1, 1);
1127         seq_adj=dumadj;
1128         connect_adj(dumadj, sequencer_move, NULL);      
1129         dummy=gtk_hscale_new(dumadj);
1130         gtk_widget_set_usize(dummy, 65, 20);
1131         seq_slider=dummy;
1132         gtk_signal_connect(GTK_OBJECT(seq_slider), "button-release-event", (GtkSignalFunc) seq_slider_released, NULL);
1133         gtk_scale_set_draw_value(GTK_SCALE(dummy), FALSE);
1134         
1135         gui_set_tooltip(dummy, "Select the start position for the sequencer in song-time.");
1136         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_DYN);
1137         gtk_widget_show(dummy);
1138         
1139         dummy=gtk_hbox_new(FALSE,2); //gtk_hpaned_new ();
1140         gtk_box_pack_start(GTK_BOX(left_hbox), dummy, WID_DYN);
1141         gtk_widget_show(dummy);
1142         
1143         tt_parent=dummy;
1144
1145     panel_bar=gtk_hbox_new(TRUE,2);
1146         gtk_box_pack_start(GTK_BOX(left_hbox), panel_bar, WID_FIX);
1147
1148         control_parent=gtk_hbox_new(FALSE,0);
1149         gtk_box_pack_start(GTK_BOX(tt_parent), control_parent, WID_FIX);
1150         gtk_widget_show(control_parent);
1151
1152         dummy=gtk_vseparator_new();
1153         gtk_box_pack_start(GTK_BOX(tt_parent), dummy, WID_FIX);
1154         gtk_widget_show(dummy);
1155
1156         audio_parent=gtk_vbox_new(FALSE,0);
1157         gtk_box_pack_start(GTK_BOX(tt_parent), audio_parent, WID_DYN);
1158         gtk_widget_show(audio_parent);
1159         
1160         dummy=gtk_vseparator_new();
1161         gtk_box_pack_start(GTK_BOX(main_vbox), dummy, WID_FIX);
1162         gtk_widget_show(dummy);
1163                 
1164         right_hbox=gtk_vbox_new(FALSE, 5);
1165         gtk_box_pack_start(GTK_BOX(main_vbox), right_hbox, WID_FIX);
1166         gtk_widget_show(right_hbox);
1167
1168         /* Master */
1169         
1170         dummy=gtk_label_new(NULL);
1171         gtk_label_set_markup(GTK_LABEL(dummy),"<b>Master</b>");
1172         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1173         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1174         gtk_widget_show(dummy); 
1175
1176         dummy=gtk_hseparator_new();
1177         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1178         gtk_widget_show(dummy);
1179
1180          /* Pitch */
1181          
1182         /*dummy=gtk_label_new("Pitch:");
1183         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1184         gtk_widget_show(dummy);*/
1185
1186         dumadj=(GtkAdjustment*) gtk_adjustment_new(globals.pitch, -3, 3, 0.001, 0.001, 0.01);
1187         pitch_adj=dumadj;
1188         connect_adj(dumadj, master_pitch_changed, NULL);
1189         
1190         tX_extdial *pdial=new tX_extdial("Pitch", pitch_adj, true);
1191         gtk_box_pack_start(GTK_BOX(right_hbox), pdial->get_widget(), WID_FIX);
1192         gui_set_tooltip(pdial->get_entry(), "Use this dial to adjust the master pitch (affecting *all* turntables).");
1193         
1194         dummy=gtk_hseparator_new();
1195         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1196         gtk_widget_show(dummy);
1197         
1198         /* Volume */
1199         master_vol_box=gtk_hbox_new(FALSE, 2);
1200         gtk_box_pack_start(GTK_BOX(right_hbox), master_vol_box, WID_DYN);
1201         gtk_widget_show(master_vol_box);        
1202         
1203         dumadj=(GtkAdjustment*) gtk_adjustment_new(globals.volume, 0, 2, 0.01, 0.05, 0.005);
1204         volume_adj=dumadj;
1205
1206         connect_adj(dumadj, master_volume_changed, NULL);       
1207         dummy=gtk_vscale_new(dumadj);
1208         //gtk_range_set_inverted(GTK_RANGE(dummy), TRUE);
1209         gtk_scale_set_draw_value(GTK_SCALE(dummy), False);
1210         gtk_box_pack_end(GTK_BOX(master_vol_box), dummy, WID_FIX);
1211         gtk_widget_show(dummy); 
1212         gui_set_tooltip(dummy, "Adjust the master volume. This parameter will effect *all* turntables in the set.");
1213         
1214         main_flash_r=gtk_tx_flash_new();
1215         gtk_box_pack_end(GTK_BOX(master_vol_box), main_flash_r, WID_DYN);
1216         gtk_widget_show(main_flash_r);
1217
1218         main_flash_l=gtk_tx_flash_new();
1219         gtk_box_pack_end(GTK_BOX(master_vol_box), main_flash_l, WID_DYN);
1220         gtk_widget_show(main_flash_l);
1221
1222         dummy=gtk_label_new("Volume");
1223         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1224         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1225         gtk_widget_show(dummy);
1226
1227         /* STATUS BOX */ 
1228         dummy=gtk_hseparator_new();
1229         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1230         gtk_widget_show(dummy);
1231         
1232         status_box=gtk_vbox_new(FALSE, 0);
1233         gtk_box_pack_start(GTK_BOX(right_hbox), status_box, WID_FIX);
1234         gtk_widget_show(status_box);
1235         
1236         dummy=gtk_label_new("0");
1237         used_mem=dummy;
1238         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1239         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1240         gtk_widget_show(dummy);
1241
1242         dummy=gtk_label_new("Mem/kB:");
1243         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1244         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1245         gtk_widget_show(dummy);
1246         
1247         add_sep2();
1248
1249         dummy=gtk_label_new("1");
1250         no_of_vtts=dummy;
1251         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1252         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1253         gtk_widget_show(dummy);
1254
1255         dummy=gtk_label_new("Vtts:");
1256         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1257         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1258         gtk_widget_show(dummy);
1259
1260         add_sep2();
1261
1262         dummy=gtk_label_new(VERSION);
1263         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1264         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1265         gtk_widget_show(dummy);
1266
1267         dummy=gtk_label_new("Release:");
1268         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1269         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1270         gtk_widget_show(dummy);
1271         
1272         add_sep2();
1273
1274         dummy=gtk_label_new(NULL);
1275         gtk_label_set_markup(GTK_LABEL(dummy), "<b>Status</b>");
1276         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1277         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1278         gtk_widget_show(dummy);
1279         
1280         /* END GUI */
1281         
1282         gtk_window_set_default_size(GTK_WINDOW(main_window), x, y);     
1283         gtk_widget_set_sensitive(grab_button, 0);
1284
1285         new_table(NULL, NULL); // to give the user something to start with ;)
1286
1287         gtk_signal_connect (GTK_OBJECT(main_window), "delete-event", (GtkSignalFunc) quit, NULL);       
1288         
1289         if (globals.tooltips) gtk_tooltips_enable(gui_tooltips);
1290         else gtk_tooltips_disable(gui_tooltips);
1291 }
1292
1293 gfloat old_percent=-1;
1294
1295 void note_destroy(GtkWidget *widget, GtkWidget *mbox)
1296 {
1297         gtk_widget_destroy(GTK_WIDGET(mbox));
1298 }
1299
1300 void tx_note(const char *message, bool isError)
1301 {
1302         char buffer[4096]="terminatorX ";
1303         if (isError) {
1304                 strcat(buffer, "note:\n\n");
1305         } else {
1306                 strcat(buffer, "error:\n\n");
1307         }
1308         
1309         strcat(buffer, message);
1310         GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window),
1311                 GTK_DIALOG_DESTROY_WITH_PARENT,
1312                 isError ? GTK_MESSAGE_ERROR : GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, message);
1313         gtk_dialog_run(GTK_DIALOG(dialog));
1314         gtk_widget_destroy(dialog);     
1315 }
1316
1317
1318 void tx_l_note(const char *message)
1319 {
1320         char buffer[4096]="Plugin info:\n\n";
1321         strcat(buffer, message);
1322         
1323         GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window),
1324                 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, message);
1325         gtk_dialog_run(GTK_DIALOG(dialog));
1326         gtk_widget_destroy(dialog);     
1327 }
1328
1329
1330 void add_to_panel_bar(GtkWidget *button) {
1331         buttons_on_panel_bar++;
1332         gtk_box_pack_start(GTK_BOX(panel_bar), button, WID_DYN);
1333         gtk_widget_show(panel_bar);
1334 }
1335
1336 void remove_from_panel_bar(GtkWidget *button) {
1337         buttons_on_panel_bar--;
1338         gtk_container_remove(GTK_CONTAINER(panel_bar), button);
1339         if (buttons_on_panel_bar==0) gtk_widget_hide(panel_bar);
1340 }
1341
1342 /* Fullscreen code... */
1343 #define _WIN_LAYER_TOP          -1
1344 #define _WIN_LAYER_NORMAL       4
1345 #define _NET_WM_STATE_REMOVE    0
1346 #define _NET_WM_STATE_ADD       1
1347 #define _NET_WM_STATE_TOGGLE    2
1348
1349 void fullscreen_toggle(GtkCheckMenuItem *item, gpointer data) {
1350         XEvent xev;
1351         Window win=GDK_WINDOW_XID(main_window->window);
1352         Display *disp=GDK_WINDOW_XDISPLAY(main_window->window);
1353         
1354         globals.fullscreen_enabled=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(fullscreen_item));
1355         
1356         /* Top layer.. */
1357         xev.xclient.type = ClientMessage;
1358         xev.xclient.serial = 0;
1359         xev.xclient.send_event = True;
1360         xev.xclient.display = disp;
1361         xev.xclient.window = win;
1362         xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_WIN_LAYER");
1363         xev.xclient.format = 32;
1364         xev.xclient.data.l[0] = globals.fullscreen_enabled ? _WIN_LAYER_TOP : _WIN_LAYER_NORMAL ;
1365         XSendEvent(disp, GDK_WINDOW_XID (gdk_get_default_root_window ()),
1366                 False, SubstructureRedirectMask | SubstructureNotifyMask,
1367                 &xev);
1368         
1369         /* Fullscreen */
1370         xev.xclient.type = ClientMessage;
1371         xev.xclient.serial = 0;
1372         xev.xclient.send_event = True;
1373         xev.xclient.display = disp;
1374         xev.xclient.window = win;
1375         xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_NET_WM_STATE");
1376         xev.xclient.format = 32;
1377         xev.xclient.data.l[0] = globals.fullscreen_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1378         xev.xclient.data.l[1] = gdk_x11_atom_to_xatom (gdk_atom_intern ("_NET_WM_STATE_FULLSCREEN", TRUE));
1379         xev.xclient.data.l[2] = gdk_x11_atom_to_xatom (GDK_NONE);
1380         XSendEvent(gdk_display, GDK_WINDOW_XID (gdk_get_default_root_window ()),
1381                 False, SubstructureRedirectMask | SubstructureNotifyMask,
1382                 &xev);  
1383 }
1384
1385 void fullscreen_setup() {
1386         if (globals.fullscreen_enabled) {
1387                 fullscreen_toggle(NULL, NULL);
1388         }
1389 }
1390
1391 void display_mastergui()
1392 {
1393         GtkWidget *top;
1394         gtk_widget_realize(main_window);
1395         tX_set_icon(main_window, "terminatorX");
1396         load_knob_pixs(main_window);
1397         gtk_widget_show(main_window);
1398         fullscreen_setup();     
1399         top=gtk_widget_get_toplevel(main_window);
1400         xwindow=GDK_WINDOW_XWINDOW(top->window);
1401 }
1402
1403 pid_t help_child=0;
1404 GTimer *help_timer=NULL;
1405 int help_tag=-1;
1406
1407 int help_checker() {
1408         gdouble time;
1409         gulong ms;
1410         int status;
1411         int result=waitpid(help_child, &status, WNOHANG);
1412         
1413         if (result==0) {
1414                 time=g_timer_elapsed(help_timer, &ms);
1415                 if (time > 5) {
1416                         /* 5 seconds and it's still running - so we assume everything's OK. */
1417                         tX_debug("No longer waiting for gnome-help..");
1418                         gtk_idle_remove(help_tag);
1419                         help_tag=-1;
1420                 }
1421         } else {
1422                 /* We are still here and the child exited - that could mean trouble. */
1423                 tx_note("Couldn't run the gnome-help command (alias \"yelp\") to display the terminatorX manual. Please ensure that \"yelp\" is installed.", true);             
1424                 
1425                 gtk_idle_remove(help_tag);
1426                 help_tag=-1;
1427         }
1428         return TRUE;    
1429 }
1430
1431 #ifndef INSTALL_PREFIX
1432 #define INSTALL_PREFIX "/usr/local/share"
1433 #endif
1434
1435 void display_help() {   
1436         help_child=fork();
1437
1438         if (help_tag!=-1) {
1439                 gtk_idle_remove(help_tag);
1440                 if (help_timer) g_timer_destroy(help_timer);
1441                 help_child=0;
1442                 help_tag=-1;
1443                 help_timer=NULL;
1444         }
1445         
1446         if (help_child==0) {
1447                 // child
1448                 // execlp("gnome-help","gnome-help","ghelp:/" INSTALL_PREFIX "/terminatorX/doc/terminatorX-manual/C/terminatorX-manual.xml", NULL);
1449                 execlp("gnome-help","gnome-help","ghelp:/" XML_MANUAL, NULL);           
1450                 _exit(-1);
1451         } else if (help_child==-1) {
1452                 tx_note("System error: couldn't fork() to run the help process.", true);
1453         } else {
1454                 help_timer=g_timer_new();
1455                 g_timer_start(help_timer);
1456         
1457                 help_tag=gtk_idle_add((GtkFunction) help_checker, NULL);
1458         }
1459 }
1460
1461 pid_t browser_child=0;
1462 GTimer *browser_timer=NULL;
1463 int browser_tag=-1;
1464
1465 int browser_checker() {
1466         gdouble time;
1467         gulong ms;
1468         int status;
1469         int result=waitpid(browser_child, &status, WNOHANG);
1470         
1471         if (result==0) {
1472                 time=g_timer_elapsed(browser_timer, &ms);
1473                 if (time > 5) {
1474                         /* 5 seconds and it's still running - so we assume everything's OK. */
1475                         tX_debug("No longer waiting for a browser..");
1476                         gtk_idle_remove(browser_tag);
1477                         browser_tag=-1;
1478                 }
1479         } else {
1480                 /* We are still here and the child exited - that could mean trouble. */
1481                 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);         
1482                 
1483                 gtk_idle_remove(browser_tag);
1484                 browser_tag=-1;
1485         }
1486         return TRUE;    
1487 }
1488
1489 void display_browser() {        
1490         browser_child=fork();
1491
1492         if (browser_tag!=-1) {
1493                 gtk_idle_remove(browser_tag);
1494                 if (browser_timer) g_timer_destroy(browser_timer);
1495                 browser_child=0;
1496                 browser_tag=-1;
1497                 browser_timer=NULL;
1498         }
1499         
1500         if (browser_child==0) {
1501                 // child
1502                 execlp("mozilla","mozilla","http://terminatorX.cx", NULL);
1503                 execlp("netscape","netscape","http://terminatorX.cx", NULL);
1504                 execlp("galeon","galeon","http://terminatorX.cx", NULL);
1505                 execlp("konqueror","konqueror","http://terminatorX.cx", NULL);          
1506                 _exit(-1);
1507         } else if (browser_child==-1) {
1508                 tx_note("System error: couldn't fork() to run the browser process.", true);
1509         } else {
1510                 browser_timer=g_timer_new();
1511                 g_timer_start(browser_timer);
1512         
1513                 browser_tag=gtk_idle_add((GtkFunction) browser_checker, NULL);
1514         }
1515 }