e09e26e4ea66a2d671f82a54a4f7cbe7f1ae374b
[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         vtt_class::delete_all();
751         
752         if (update_tag)
753         gtk_timeout_remove(update_tag);
754         globals.width=main_window->allocation.width;
755         globals.height=main_window->allocation.height;
756
757         gtk_main_quit();
758 }
759
760 void mplcfitx()
761 /* Most Probably Least Called Function In terminatorX :) */
762 {
763         show_about(0);
764 }
765
766 GtkSignalFunc seq_play(GtkWidget *w, void *)
767 {
768         if ((sequencer.is_empty()) &&   (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(seq_rec_btn)))) {
769                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
770                  {
771                         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.");
772                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 0);
773                  }
774         } else {
775                 if (seq_stop_override) return NULL;
776                         
777                 seq_adj_care=0;
778                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 1);
779                 sequencer.trig_play();
780         
781                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 1);
782         }
783         
784         return NULL;
785 }
786
787 GtkSignalFunc seq_stop(GtkWidget *w, void *)
788 {
789         if (!sequencer_ready) return NULL;
790         sequencer.trig_stop();
791         seq_adj_care=1;
792         seq_stop_override=1;
793         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(seq_play_btn), 0);
794         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(seq_rec_btn), 0);
795         while (gtk_events_pending()) gtk_main_iteration();              
796         seq_stop_override=0;
797         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(engine_btn), 0); 
798         gtk_widget_set_sensitive(seq_slider, 1);        
799         gtk_widget_set_sensitive(engine_btn, 1);
800         gtk_widget_set_sensitive(seq_rec_btn, 1);
801
802         return NULL;
803 }
804
805 GtkSignalFunc seq_rec(GtkWidget *w, void *)
806 {
807         seq_adj_care=0;
808         gtk_widget_set_sensitive(seq_slider, 0);
809
810         if (seq_stop_override) return NULL;
811         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), 1);
812         gtk_widget_set_sensitive(engine_btn, 0);
813         gtk_widget_set_sensitive(seq_rec_btn, 0);
814         sequencer.trig_rec();
815         
816         return NULL;
817 }
818
819 void seq_update_entry(const guint32 timestamp)
820 {
821         char buffer[20];
822         guint32 samples;
823         guint32 minu,sec,hun;   
824         guint32 sr;
825         
826         samples=timestamp*vtt_class::get_mix_buffer_size();
827         sr=vtt_class::get_last_sample_rate();
828         
829         if (samples>0)
830         {
831                 minu=samples/(sr*60);
832                 samples-=(sr*60)*minu;
833         
834                 sec=samples/sr;
835                 samples-=sr*sec;
836         
837                 hun=samples/(sr/100);
838         }
839         else
840         {
841                 minu=sec=hun=0;
842         }
843         
844         sprintf(buffer, "%02i:%02i.%02i", minu, sec, hun);
845         gtk_entry_set_text(GTK_ENTRY(seq_entry), buffer);
846 }
847
848 void seq_update()
849 {
850         seq_update_entry(sequencer.get_timestamp());
851         gtk_adjustment_set_value(seq_adj, sequencer.get_timestamp_as_float());
852         
853 }
854 void seq_slider_released(GtkWidget *wid, void *d)
855 {
856         seq_adj_care=0;
857         gtk_widget_set_sensitive(seq_slider, 0);        
858         sequencer.forward_to_start_timestamp(0);
859         gtk_widget_set_sensitive(seq_slider, 1);        
860         seq_adj_care=1;
861 }
862 void sequencer_move(GtkWidget *wid, void *d)
863 {
864         guint32 pos;
865         
866         if (seq_adj_care)
867         {
868                 pos=sequencer.set_start_timestamp((float) GTK_ADJUSTMENT(wid)->value);
869                 seq_update_entry(pos);  
870         }
871 }
872
873 #define add_sep();      dummy=gtk_hseparator_new ();\
874         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);\
875         gtk_widget_show(dummy);\
876
877 #define add_sep2();     dummy=gtk_hseparator_new ();\
878         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);\
879         gtk_widget_show(dummy);\
880
881 void fullscreen_toggle(GtkCheckMenuItem *item, gpointer data);
882 void display_help();
883 void display_browser();
884
885 void create_master_menu() {
886         GtkWidget *menu_item;
887         GtkWidget *sub_menu;
888         GtkAccelGroup* accel_group=gtk_accel_group_new();
889         gtk_window_add_accel_group(GTK_WINDOW(main_window), accel_group);
890
891         /* FILE */
892         menu_item = gtk_menu_item_new_with_mnemonic ("_File");
893         gtk_widget_show (menu_item);
894         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
895
896         sub_menu = gtk_menu_new ();
897         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
898
899         menu_item = gtk_image_menu_item_new_from_stock ("gtk-new", accel_group);
900         gtk_widget_show (menu_item);
901         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
902         g_signal_connect(menu_item, "activate", (GCallback) new_tables, NULL);
903
904         menu_item = gtk_image_menu_item_new_from_stock ("gtk-open", accel_group);
905         gtk_widget_show (menu_item);
906         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
907         g_signal_connect(menu_item, "activate", (GCallback) load_tables, NULL);
908
909         menu_item = gtk_image_menu_item_new_from_stock ("gtk-save", accel_group);
910         gtk_widget_show (menu_item);
911         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
912         g_signal_connect(menu_item, "activate", (GCallback) save_tables, NULL);
913
914         menu_item = gtk_image_menu_item_new_from_stock ("gtk-save-as", accel_group);
915         gtk_widget_show (menu_item);
916         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
917         g_signal_connect(menu_item, "activate", (GCallback) save_tables_as, NULL);
918
919         menu_item = gtk_menu_item_new ();
920         gtk_widget_show (menu_item);
921         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
922         gtk_widget_set_sensitive (menu_item, FALSE);
923
924         menu_item = gtk_image_menu_item_new_from_stock ("gtk-quit", accel_group);
925         gtk_widget_show (menu_item);
926         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
927         g_signal_connect(menu_item, "activate", (GCallback) quit, NULL);
928
929         /* Turntables */
930         menu_item = gtk_menu_item_new_with_mnemonic ("_Turntables");
931         gtk_widget_show (menu_item);
932         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
933         
934         sub_menu = gtk_menu_new ();
935         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
936
937         menu_item = gtk_menu_item_new_with_mnemonic("_Add Turntable");
938         gtk_widget_show (menu_item);
939         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
940         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_A, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);    
941         g_signal_connect(menu_item, "activate", (GCallback) new_table, NULL);
942
943         menu_item = gtk_check_menu_item_new_with_mnemonic("_Record Audio To Disk");
944         rec_menu_item = menu_item;
945         gtk_widget_show (menu_item);
946         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
947         g_signal_connect(menu_item, "activate", (GCallback) tape_on, NULL);
948
949         /* Options */
950         menu_item = gtk_menu_item_new_with_mnemonic ("_Options");
951         gtk_widget_show (menu_item);
952         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
953
954         sub_menu = gtk_menu_new ();
955         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
956                 
957         menu_item = gtk_check_menu_item_new_with_mnemonic("_Fullscreen");
958         fullscreen_item = menu_item;
959         gtk_widget_show (menu_item);
960         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
961
962         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), globals.fullscreen_enabled);
963         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_F11, (GdkModifierType) 0, GTK_ACCEL_VISIBLE);
964         g_signal_connect(menu_item, "activate", (GCallback) fullscreen_toggle, NULL);
965         
966         menu_item = gtk_menu_item_new ();
967         gtk_widget_show (menu_item);
968         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
969         gtk_widget_set_sensitive (menu_item, FALSE);
970
971         menu_item = gtk_menu_item_new_with_mnemonic("_Preferences");
972         gtk_widget_show (menu_item);
973         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
974         g_signal_connect(menu_item, "activate", (GCallback) display_options, NULL);
975
976         /* HELP */ 
977         menu_item = gtk_menu_item_new_with_mnemonic ("_Help");
978         gtk_widget_show (menu_item);
979         gtk_container_add (GTK_CONTAINER (main_menubar), menu_item);
980         gtk_menu_item_set_right_justified(GTK_MENU_ITEM(menu_item), TRUE);
981         
982         sub_menu = gtk_menu_new ();
983         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), sub_menu);
984
985         menu_item = gtk_menu_item_new_with_mnemonic ("_Contents");
986         gtk_widget_show (menu_item);
987         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
988         g_signal_connect(menu_item, "activate", (GCallback) display_help, NULL);
989         gtk_widget_add_accelerator (menu_item, "activate", accel_group, GDK_F1, (GdkModifierType) 0, GTK_ACCEL_VISIBLE);
990
991         menu_item = gtk_menu_item_new_with_mnemonic ("_About");
992         gtk_widget_show (menu_item);
993         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
994         g_signal_connect(menu_item, "activate", (GCallback) mplcfitx, NULL);
995         
996         menu_item = gtk_menu_item_new ();
997         gtk_widget_show (menu_item);
998         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
999         gtk_widget_set_sensitive (menu_item, FALSE);
1000
1001         menu_item = gtk_menu_item_new_with_mnemonic ("_Visit terminatorX.cx");
1002         gtk_widget_show (menu_item);
1003         gtk_container_add (GTK_CONTAINER (sub_menu), menu_item);
1004         g_signal_connect(menu_item, "activate", (GCallback) display_browser, NULL);
1005 }
1006
1007 void create_mastergui(int x, int y)
1008 {
1009         GtkWidget *mother_of_all_boxen;
1010         GtkWidget *main_vbox;
1011         GtkWidget *right_hbox;
1012         GtkWidget *left_hbox;
1013         GtkWidget *control_box;
1014         //GtkWidget *sequencer_box;
1015         GtkAdjustment *dumadj;
1016         GtkWidget *dummy;
1017         GtkWidget *master_vol_box;
1018         GtkWidget *status_box;
1019         
1020         static GtkTargetEntry drop_types [] = {
1021                 { "text/uri-list", 0, 0}
1022         };
1023         static gint n_drop_types = sizeof (drop_types) / sizeof(drop_types[0]);
1024         
1025         gui_tooltips=gtk_tooltips_new();
1026
1027         main_window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
1028
1029         gtk_window_set_wmclass(GTK_WINDOW(main_window), "terminatorX", "tX_mastergui");
1030
1031         gtk_window_set_title(GTK_WINDOW(main_window), "terminatorX");
1032         
1033         gtk_container_set_border_width(GTK_CONTAINER(main_window), 5);
1034
1035         gtk_widget_realize(main_window);
1036         
1037         mother_of_all_boxen=gtk_vbox_new(FALSE, 5);
1038         gtk_container_add(GTK_CONTAINER(main_window), mother_of_all_boxen);
1039         gtk_widget_show(mother_of_all_boxen);   
1040         
1041         main_menubar=gtk_menu_bar_new();
1042         gtk_box_pack_start(GTK_BOX(mother_of_all_boxen), main_menubar, WID_FIX);
1043         gtk_widget_show(main_menubar);  
1044         
1045         create_master_menu();
1046         
1047         main_vbox=gtk_hbox_new(FALSE, 5);
1048         gtk_box_pack_start(GTK_BOX(mother_of_all_boxen), main_vbox, WID_DYN);
1049         gtk_widget_show(main_vbox);
1050         
1051         left_hbox=gtk_vbox_new(FALSE, 5);
1052         gtk_box_pack_start(GTK_BOX(main_vbox), left_hbox, WID_DYN);
1053         gtk_widget_show(left_hbox);
1054         
1055         control_box=gtk_hbox_new(FALSE, 5);
1056         gtk_box_pack_start(GTK_BOX(left_hbox), control_box, WID_FIX);
1057         gtk_widget_show(control_box);
1058         
1059 /*      dummy=gtk_hseparator_new();
1060         gtk_box_pack_start(GTK_BOX(left_hbox), dummy, WID_FIX);
1061         gtk_widget_show(dummy);*/
1062
1063
1064         dummy=gtk_hseparator_new();
1065         gtk_box_pack_start(GTK_BOX(left_hbox), dummy, WID_FIX);
1066         gtk_widget_show(dummy);
1067
1068     /* control_box contents */
1069
1070         dummy=tx_xpm_label_box(TX_ICON_AUDIOENGINE, "Audio");
1071         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1072         gtk_widget_show(dummy);
1073         
1074         dummy=tx_xpm_button_new(TX_ICON_POWER,"Power ", 1);
1075         connect_button(dummy,audio_on, NULL);
1076         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1077         gui_set_tooltip(dummy, "Turn the audio engine on/off.");
1078         gtk_widget_show(dummy);
1079         engine_btn=dummy;
1080         
1081         grab_button=tx_xpm_button_new(TX_ICON_GRAB, "Mouse Grab ", 1);
1082         gtk_box_pack_start(GTK_BOX(control_box), grab_button, WID_FIX);
1083         connect_button(grab_button, grab_on, NULL);
1084         gui_set_tooltip(grab_button, "Enter the mouse grab mode operation. Press <ESCAPE> to exit grab mode.");
1085         gtk_widget_show(grab_button);
1086
1087         /*dummy=gtk_check_button_new_with_label("Record");
1088         rec_btn=dummy;
1089         connect_button(dummy,tape_on, NULL);
1090         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1091         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.");
1092         gtk_widget_show(dummy);*/
1093
1094         dummy=gtk_vseparator_new();
1095         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1096         gtk_widget_show(dummy);
1097     
1098         dummy=tx_xpm_label_box(TX_ICON_SEQUENCER, "Seq.");
1099         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1100         gtk_widget_show(dummy);
1101
1102         dummy=tx_xpm_button_new(TX_ICON_PLAY,"Play ", 1);
1103         connect_button(dummy, seq_play, NULL);
1104         seq_play_btn=dummy;
1105         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1106         gui_set_tooltip(dummy, "Playback previously recorded events from the sequencer. This will turn on the audio engine automagically.");
1107         gtk_widget_show(dummy);
1108
1109         dummy=tx_xpm_button_new(TX_ICON_STOP,"Stop ", 0);
1110         seq_stop_btn=dummy;
1111         connect_button(dummy, seq_stop, NULL);  
1112         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1113         gui_set_tooltip(dummy, "Stop the playback of sequencer events.");
1114         gtk_widget_show(dummy);
1115
1116         dummy=tx_xpm_button_new(TX_ICON_RECORD,"Record ", 1);
1117         connect_button(dummy, seq_rec, NULL);
1118         seq_rec_btn=dummy;
1119         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1120         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.");
1121         gtk_widget_show(dummy);
1122
1123         dummy=gtk_label_new("Pos:");
1124         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1125         gtk_widget_show(dummy);
1126         
1127         dummy=gtk_entry_new_with_max_length(12);
1128         seq_entry=dummy;
1129         //gtk_widget_set_usize(dummy, 65, 20);
1130         gtk_entry_set_text(GTK_ENTRY(dummy), "00:00.00");
1131         gtk_entry_set_width_chars(GTK_ENTRY(dummy), 9);
1132         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1133         gtk_widget_show(dummy);
1134
1135         dumadj=(GtkAdjustment*) gtk_adjustment_new(0, 0, 100, 0.1, 1, 1);
1136         seq_adj=dumadj;
1137         connect_adj(dumadj, sequencer_move, NULL);      
1138         dummy=gtk_hscale_new(dumadj);
1139         gtk_widget_set_usize(dummy, 65, 20);
1140         seq_slider=dummy;
1141         gtk_signal_connect(GTK_OBJECT(seq_slider), "button-release-event", (GtkSignalFunc) seq_slider_released, NULL);
1142         gtk_scale_set_draw_value(GTK_SCALE(dummy), FALSE);
1143         
1144         gui_set_tooltip(dummy, "Select the start position for the sequencer in song-time.");
1145         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_DYN);
1146         gtk_widget_show(dummy);
1147         
1148         dummy=gtk_hbox_new(FALSE,2); //gtk_hpaned_new ();
1149         gtk_box_pack_start(GTK_BOX(left_hbox), dummy, WID_DYN);
1150         gtk_widget_show(dummy);
1151         
1152         tt_parent=dummy;
1153
1154     panel_bar=gtk_hbox_new(TRUE,2);
1155         gtk_box_pack_start(GTK_BOX(left_hbox), panel_bar, WID_FIX);
1156
1157         control_parent=gtk_hbox_new(FALSE,0);
1158         gtk_box_pack_start(GTK_BOX(tt_parent), control_parent, WID_FIX);
1159         //gtk_paned_pack1(GTK_PANED(tt_parent), control_parent, FALSE, FALSE);
1160         gtk_widget_show(control_parent);
1161
1162         dummy=gtk_vseparator_new();
1163         gtk_box_pack_start(GTK_BOX(tt_parent), dummy, WID_FIX);
1164         gtk_widget_show(dummy);
1165
1166         audio_parent=gtk_vbox_new(FALSE,0);
1167         gtk_box_pack_start(GTK_BOX(tt_parent), audio_parent, WID_DYN);
1168         //gtk_paned_pack2(GTK_PANED(tt_parent), audio_parent, TRUE, FALSE);
1169         gtk_widget_show(audio_parent);
1170         
1171         dummy=gtk_vseparator_new();
1172         gtk_box_pack_start(GTK_BOX(main_vbox), dummy, WID_FIX);
1173         gtk_widget_show(dummy);
1174                 
1175         right_hbox=gtk_vbox_new(FALSE, 5);
1176         gtk_box_pack_start(GTK_BOX(main_vbox), right_hbox, WID_FIX);
1177         gtk_widget_show(right_hbox);
1178
1179         /* Master */
1180         
1181         dummy=gtk_label_new(NULL);
1182         //gtk_label_set_markup(GTK_LABEL(dummy),"<span foreground='#000080'>Master</span>");
1183         gtk_label_set_markup(GTK_LABEL(dummy),"<b>Master</b>");
1184         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1185         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1186         gtk_widget_show(dummy); 
1187
1188         dummy=gtk_hseparator_new();
1189         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1190         gtk_widget_show(dummy);
1191
1192          /* Pitch */
1193          
1194         /*dummy=gtk_label_new("Pitch:");
1195         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_FIX);
1196         gtk_widget_show(dummy);*/
1197
1198         dumadj=(GtkAdjustment*) gtk_adjustment_new(globals.pitch, -3, 3, 0.001, 0.001, 0.01);
1199         pitch_adj=dumadj;
1200         connect_adj(dumadj, master_pitch_changed, NULL);
1201         
1202         tX_extdial *pdial=new tX_extdial("Pitch", pitch_adj, true);
1203         gtk_box_pack_start(GTK_BOX(right_hbox), pdial->get_widget(), WID_FIX);
1204         gui_set_tooltip(pdial->get_entry(), "Use this dial to adjust the master pitch (affecting *all* turntables).");
1205         
1206 /*      dummy=gtk_hscale_new(dumadj);
1207         gtk_scale_set_digits(GTK_SCALE(dummy), 2);
1208         gtk_scale_set_value_pos(GTK_SCALE(dummy), GTK_POS_LEFT);
1209         gtk_box_pack_start(GTK_BOX(control_box), dummy, WID_DYN);
1210         gui_set_tooltip(dummy, "Use this scale to adjust the master pitch (affecting *all* turntables).");
1211         gtk_widget_show(dummy);
1212 */
1213         dummy=gtk_hseparator_new();
1214         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1215         gtk_widget_show(dummy);
1216         
1217         /* Volume */
1218         master_vol_box=gtk_hbox_new(FALSE, 2);
1219         gtk_box_pack_start(GTK_BOX(right_hbox), master_vol_box, WID_DYN);
1220         gtk_widget_show(master_vol_box);        
1221         
1222         dumadj=(GtkAdjustment*) gtk_adjustment_new(2.0-globals.volume, 0, 2, 0.01, 0.05, 0.005);
1223         volume_adj=dumadj;
1224
1225         connect_adj(dumadj, master_volume_changed, NULL);       
1226         dummy=gtk_vscale_new(dumadj);
1227         gtk_scale_set_draw_value(GTK_SCALE(dummy), False);
1228         gtk_box_pack_end(GTK_BOX(master_vol_box), dummy, WID_FIX);
1229         gtk_widget_show(dummy); 
1230         gui_set_tooltip(dummy, "Adjust the master volume. This parameter will effect *all* turntables in the set.");
1231         
1232         main_flash_r=gtk_tx_flash_new();
1233         gtk_box_pack_end(GTK_BOX(master_vol_box), main_flash_r, WID_DYN);
1234         gtk_widget_show(main_flash_r);
1235
1236         main_flash_l=gtk_tx_flash_new();
1237         gtk_box_pack_end(GTK_BOX(master_vol_box), main_flash_l, WID_DYN);
1238         gtk_widget_show(main_flash_l);
1239
1240         dummy=gtk_label_new("Volume");
1241         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1242         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1243         gtk_widget_show(dummy);
1244
1245         /* STATUS BOX */ 
1246         dummy=gtk_hseparator_new();
1247         gtk_box_pack_start(GTK_BOX(right_hbox), dummy, WID_FIX);
1248         gtk_widget_show(dummy);
1249         
1250         status_box=gtk_vbox_new(FALSE, 0);
1251         gtk_box_pack_start(GTK_BOX(right_hbox), status_box, WID_FIX);
1252         gtk_widget_show(status_box);
1253         
1254         dummy=gtk_label_new("0");
1255         used_mem=dummy;
1256         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1257         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1258         gtk_widget_show(dummy);
1259
1260         dummy=gtk_label_new("Mem/kB:");
1261         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1262         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1263         gtk_widget_show(dummy);
1264         
1265         add_sep2();
1266
1267         dummy=gtk_label_new("1");
1268         no_of_vtts=dummy;
1269         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1270         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1271         gtk_widget_show(dummy);
1272
1273         dummy=gtk_label_new("Vtts:");
1274         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1275         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1276         gtk_widget_show(dummy);
1277
1278         add_sep2();
1279
1280         dummy=gtk_label_new(VERSION);
1281         gtk_misc_set_alignment(GTK_MISC(dummy), 1, 0.5);
1282         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1283         gtk_widget_show(dummy);
1284
1285         dummy=gtk_label_new("Release:");
1286         gtk_misc_set_alignment(GTK_MISC(dummy), 0, 0.5);
1287         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1288         gtk_widget_show(dummy);
1289         
1290         add_sep2();
1291
1292         dummy=gtk_label_new(NULL);
1293         gtk_label_set_markup(GTK_LABEL(dummy), "<b>Status</b>");
1294         gtk_misc_set_alignment(GTK_MISC(dummy), 0.5, 0.5);
1295         gtk_box_pack_end(GTK_BOX(status_box), dummy, WID_FIX);
1296         gtk_widget_show(dummy);
1297         
1298         /* END GUI */
1299         
1300         gtk_window_set_default_size(GTK_WINDOW(main_window), x, y);     
1301         gtk_widget_set_sensitive(grab_button, 0);
1302
1303         new_table(NULL, NULL); // to give the user something to start with ;)
1304
1305         gtk_signal_connect (GTK_OBJECT(main_window), "delete-event", (GtkSignalFunc) quit, NULL);       
1306         
1307         if (globals.tooltips) gtk_tooltips_enable(gui_tooltips);
1308         else gtk_tooltips_disable(gui_tooltips);
1309 }
1310
1311 gfloat old_percent=-1;
1312
1313 void note_destroy(GtkWidget *widget, GtkWidget *mbox)
1314 {
1315         gtk_widget_destroy(GTK_WIDGET(mbox));
1316 }
1317
1318 void tx_note(const char *message, bool isError)
1319 {
1320         char buffer[4096]="terminatorX ";
1321         if (isError) {
1322                 strcat(buffer, "note:\n\n");
1323         } else {
1324                 strcat(buffer, "error:\n\n");
1325         }
1326         
1327         strcat(buffer, message);
1328         GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window),
1329                 GTK_DIALOG_DESTROY_WITH_PARENT,
1330                 isError ? GTK_MESSAGE_ERROR : GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, message);
1331         gtk_dialog_run(GTK_DIALOG(dialog));
1332         gtk_widget_destroy(dialog);     
1333 }
1334
1335
1336 void tx_l_note(const char *message)
1337 {
1338         char buffer[4096]="Plugin info:\n\n";
1339         strcat(buffer, message);
1340         
1341         GtkWidget *dialog=gtk_message_dialog_new(GTK_WINDOW(main_window),
1342                 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, message);
1343         gtk_dialog_run(GTK_DIALOG(dialog));
1344         gtk_widget_destroy(dialog);     
1345 }
1346
1347
1348 void add_to_panel_bar(GtkWidget *button) {
1349         buttons_on_panel_bar++;
1350         gtk_box_pack_start(GTK_BOX(panel_bar), button, WID_DYN);
1351         gtk_widget_show(panel_bar);
1352 }
1353
1354 void remove_from_panel_bar(GtkWidget *button) {
1355         buttons_on_panel_bar--;
1356         gtk_container_remove(GTK_CONTAINER(panel_bar), button);
1357         if (buttons_on_panel_bar==0) gtk_widget_hide(panel_bar);
1358 }
1359
1360 /* Fullscreen code... */
1361 #define _WIN_LAYER_TOP          -1
1362 #define _WIN_LAYER_NORMAL       4
1363 #define _NET_WM_STATE_REMOVE    0
1364 #define _NET_WM_STATE_ADD       1
1365 #define _NET_WM_STATE_TOGGLE    2
1366
1367 void fullscreen_toggle(GtkCheckMenuItem *item, gpointer data) {
1368         XEvent xev;
1369         Window win=GDK_WINDOW_XID(main_window->window);
1370         Display *disp=GDK_WINDOW_XDISPLAY(main_window->window);
1371         
1372         globals.fullscreen_enabled=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(fullscreen_item));
1373         
1374         /* Top layer.. */
1375         xev.xclient.type = ClientMessage;
1376         xev.xclient.serial = 0;
1377         xev.xclient.send_event = True;
1378         xev.xclient.display = disp;
1379         xev.xclient.window = win;
1380         xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_WIN_LAYER");
1381         xev.xclient.format = 32;
1382         xev.xclient.data.l[0] = globals.fullscreen_enabled ? _WIN_LAYER_TOP : _WIN_LAYER_NORMAL ;
1383         XSendEvent(disp, GDK_WINDOW_XID (gdk_get_default_root_window ()),
1384                 False, SubstructureRedirectMask | SubstructureNotifyMask,
1385                 &xev);
1386         
1387         /* Fullscreen */
1388         xev.xclient.type = ClientMessage;
1389         xev.xclient.serial = 0;
1390         xev.xclient.send_event = True;
1391         xev.xclient.display = disp;
1392         xev.xclient.window = win;
1393         xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_NET_WM_STATE");
1394         xev.xclient.format = 32;
1395         xev.xclient.data.l[0] = globals.fullscreen_enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1396         xev.xclient.data.l[1] = gdk_x11_atom_to_xatom (gdk_atom_intern ("_NET_WM_STATE_FULLSCREEN", TRUE));
1397         xev.xclient.data.l[2] = gdk_x11_atom_to_xatom (GDK_NONE);
1398         XSendEvent(gdk_display, GDK_WINDOW_XID (gdk_get_default_root_window ()),
1399                 False, SubstructureRedirectMask | SubstructureNotifyMask,
1400                 &xev);  
1401 }
1402
1403 void fullscreen_setup() {
1404         if (globals.fullscreen_enabled) {
1405                 fullscreen_toggle(NULL, NULL);
1406         }
1407 }
1408
1409 void display_mastergui()
1410 {
1411         GtkWidget *top;
1412         gtk_widget_realize(main_window);
1413         tX_set_icon(main_window, "terminatorX");
1414         load_knob_pixs(main_window);
1415         gtk_widget_show(main_window);
1416         fullscreen_setup();     
1417         top=gtk_widget_get_toplevel(main_window);
1418         xwindow=GDK_WINDOW_XWINDOW(top->window);
1419 }
1420
1421 pid_t help_child=0;
1422 GTimer *help_timer=NULL;
1423 int help_tag=-1;
1424
1425 int help_checker() {
1426         gdouble time;
1427         gulong ms;
1428         int status;
1429         int result=waitpid(help_child, &status, WNOHANG);
1430         
1431         if (result==0) {
1432                 time=g_timer_elapsed(help_timer, &ms);
1433                 if (time > 5) {
1434                         /* 5 seconds and it's still running - so we assume everything's OK. */
1435                         tX_debug("No longer waiting for gnome-help..");
1436                         gtk_idle_remove(help_tag);
1437                         help_tag=-1;
1438                 }
1439         } else {
1440                 /* We are still here and the child exited - that could mean trouble. */
1441                 tx_note("Couldn't run the gnome-help command (alias \"yelp\") to display the terminatorX manual. Please ensure that \"yelp\" is installed.", true);             
1442                 
1443                 gtk_idle_remove(help_tag);
1444                 help_tag=-1;
1445         }
1446         return TRUE;    
1447 }
1448
1449 #ifndef INSTALL_PREFIX
1450 #define INSTALL_PREFIX "/usr/local/share"
1451 #endif
1452
1453 void display_help() {   
1454         help_child=fork();
1455
1456         if (help_tag!=-1) {
1457                 gtk_idle_remove(help_tag);
1458                 if (help_timer) g_timer_destroy(help_timer);
1459                 help_child=0;
1460                 help_tag=-1;
1461                 help_timer=NULL;
1462         }
1463         
1464         if (help_child==0) {
1465                 // child
1466                 // execlp("gnome-help","gnome-help","ghelp:/" INSTALL_PREFIX "/terminatorX/doc/terminatorX-manual/C/terminatorX-manual.xml", NULL);
1467                 execlp("gnome-help","gnome-help","ghelp:/" XML_MANUAL, NULL);           
1468                 _exit(-1);
1469         } else if (help_child==-1) {
1470                 tx_note("System error: couldn't fork() to run the help process.", true);
1471         } else {
1472                 help_timer=g_timer_new();
1473                 g_timer_start(help_timer);
1474         
1475                 help_tag=gtk_idle_add((GtkFunction) help_checker, NULL);
1476         }
1477 }
1478
1479 pid_t browser_child=0;
1480 GTimer *browser_timer=NULL;
1481 int browser_tag=-1;
1482
1483 int browser_checker() {
1484         gdouble time;
1485         gulong ms;
1486         int status;
1487         int result=waitpid(browser_child, &status, WNOHANG);
1488         
1489         if (result==0) {
1490                 time=g_timer_elapsed(browser_timer, &ms);
1491                 if (time > 5) {
1492                         /* 5 seconds and it's still running - so we assume everything's OK. */
1493                         tX_debug("No longer waiting for a browser..");
1494                         gtk_idle_remove(browser_tag);
1495                         browser_tag=-1;
1496                 }
1497         } else {
1498                 /* We are still here and the child exited - that could mean trouble. */
1499                 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);         
1500                 
1501                 gtk_idle_remove(browser_tag);
1502                 browser_tag=-1;
1503         }
1504         return TRUE;    
1505 }
1506
1507 void display_browser() {        
1508         browser_child=fork();
1509
1510         if (browser_tag!=-1) {
1511                 gtk_idle_remove(browser_tag);
1512                 if (browser_timer) g_timer_destroy(browser_timer);
1513                 browser_child=0;
1514                 browser_tag=-1;
1515                 browser_timer=NULL;
1516         }
1517         
1518         if (browser_child==0) {
1519                 // child
1520                 execlp("mozilla","mozilla","http://terminatorX.cx", NULL);
1521                 execlp("netscape","netscape","http://terminatorX.cx", NULL);
1522                 execlp("galeon","galeon","http://terminatorX.cx", NULL);
1523                 execlp("konqueror","konqueror","http://terminatorX.cx", NULL);          
1524                 _exit(-1);
1525         } else if (browser_child==-1) {
1526                 tx_note("System error: couldn't fork() to run the browser process.", true);
1527         } else {
1528                 browser_timer=g_timer_new();
1529                 g_timer_start(browser_timer);
1530         
1531                 browser_tag=gtk_idle_add((GtkFunction) browser_checker, NULL);
1532         }
1533 }