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