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